<div dir="ltr">On Thu, Sep 28, 2017 at 12:16 PM, Félix Cloutier <span dir="ltr">&lt;<a href="mailto:felixcloutier@icloud.com" target="_blank">felixcloutier@icloud.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><br><div><span class="gmail-"><br><blockquote type="cite"><div>Le 27 sept. 2017 à 17:29, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; a écrit :</div><div><div dir="auto" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div dir="auto" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">What I was trying to respond to, by contrast, is the design of a hierarchy of protocols CSPRNG : PRNG (or, in Alejandro&#39;s proposal, UnsafeRandomSource : RandomSource) and the appropriate APIs to expose on each. This is entirely inapplicable to your examples. It stands to reason that a non-instantiable source of random numbers does not require a protocol of its own (a hypothetical RNG : CSPRNG), since there is no reason to implement (if done correctly) more than a single publicly non-instantiable singleton type that could conform to it. For that matter, the concrete type itself probably doesn&#39;t need *any* public API at all. Instead, extensions to standard library types such as Int that implement conformance to the protocol that Alejandro names &quot;Randomizable&quot; could call internal APIs to provide all the necessary functionality, and third-party types that need to conform to &quot;Randomizable&quot; could then in turn use `Int.random()` or `Double.random()` to implement their own conformance. In fact, the concrete random number generator type doesn&#39;t need to be public at all. All public interaction could be through APIs such as `Int.random()`.</div></div></blockquote></span><div><div><br></div><div>If there is globally-available CSPRNG that people are encouraged to use, what is the benefit of a CSPRNG : PRNG hierarchy?</div></div></div></div></blockquote><div><br></div><div>There are plenty of use cases that do not require cryptographically secure pseudorandom sequences but can benefit from the speed of an algorithm like xoroshiro128+. For instance, suppose I want to simulate Brownian motion in an animation; I do not care about the cryptographic properties of my random number source. However, the underlying function to generate a normally distributed random number can rely on either a source of cyptographically secure or cryptographically insecure uniformly distributed random numbers. Put another way, a protocol hierarchy is justified because useful functions that produce random values in various desired distributions can be written that work with any PRNG, while there are (obviously) uses that are suitable for CSPRNGs only.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div>What is the benefit of clearly identifying an algorithm as crypto-secure if the recommendation is that you use *the* crypto-secure object/functions?</div></div></div></div></blockquote><div><br></div><div>The default source of random numbers is unseedable, but the user may instead require repeatable generation of a sequence of random numbers. The user may have a use case that requires the use of a particular specified CSPRNG.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><span class="gmail-"><blockquote type="cite"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Again, I&#39;m not only talking about urandom. As far as I&#39;m aware, every API to retrieve cryptographically secure sequences of random bits on every platform for which Swift is distributed can potentially return an error instead of random bits. The question is, what design for our API is the most sensible way to deal with this contingency? On rethinking, I do believe that consistently returning an Optional is the best way to go about it, allowing the user to either (a) supply a deterministic fallback; (b) raise an error of their own choosing; or (c) trap--all with a minimum of fuss. This seems very Swifty to me.</div></div></blockquote><div><br></div></span><div>With Linux&#39;s getrandom, if you read from urandom (the default) and ask for as much or less than 256 bytes, the <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" target="_blank">only possible error is that urandom hasn&#39;t been seeded</a>. (With more than 256 bytes, it also becomes possible for the system call to be interrupted by a signal.) OpenBSD&#39;s getentropy is <a href="https://github.com/openbsd/src/blob/master/sys/dev/rnd.c#L889" target="_blank">literally just arc4random running in the kernel</a>, and will only fail if you ask for more than 256 bytes because it is a hard-coded limit.</div></div></div></blockquote><div><br></div><div>Yes, but again, what do you think of the possible Swift API design choices to accommodate these errors? We have to pick one, but none of them are very appealing.</div><div><br></div><div>Meanwhile, getentropy() has the problem that, if there is insufficient randomness to initialize the entropy pool, it will block; on systems where there is never going to be sufficient randomness (i.e. a VM), it will block forever. By contrast, getrandom() permits a flag that will make this scenario non-blocking.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div>Ironically, I don&#39;t know how you get entropy on Darwin. If it&#39;s more failable that this, I&#39;d argue that it&#39;s time to improve a bit...</div><span class="gmail-HOEnZb"><font color="#888888"><div><br></div><div>Félix</div></font></span></div></div></blockquote></div><br></div></div>