index.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `prng` mod in crate `rand`."><meta name="keywords" content="rust, rustlang, rust-lang, prng"><title>rand::prng - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../rand/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk.png' alt='logo' width='100'></a><p class='location'>Module prng</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li></ul></div><p class='location'><a href='../index.html'>rand</a></p><script>window.sidebarCurrent = {name: 'prng', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>rand</a>::<wbr><a class="mod" href=''>prng</a></span><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/rand/prng/mod.rs.html#11-330' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Pseudo-random number generators.</p>
  2. <p>Pseudo-random number generators are algorithms to produce apparently random
  3. numbers deterministically, and usually fairly quickly. See the documentation
  4. of the <a href="../rngs/index.html"><code>rngs</code> module</a> for some introduction to PRNGs.</p>
  5. <p>As mentioned there, PRNGs fall in two broad categories:</p>
  6. <ul>
  7. <li><a href="#basic-pseudo-random-number-generators-prngs">basic PRNGs</a>, primarily designed for simulations</li>
  8. <li><a href="#cryptographically-secure-pseudo-random-number-generators-csprngs">CSPRNGs</a>, primarily designed for cryptography</li>
  9. </ul>
  10. <p>In simple terms, the basic PRNGs are often predictable; CSPRNGs should not
  11. be predictable <em>when used correctly</em>.</p>
  12. <p>Contents of this documentation:</p>
  13. <ol>
  14. <li><a href="#the-generators">The generators</a></li>
  15. <li><a href="#performance">Performance and size</a></li>
  16. <li><a href="#quality">Quality and cycle length</a></li>
  17. <li><a href="#security">Security</a></li>
  18. <li><a href="#extra-features">Extra features</a></li>
  19. <li><a href="#further-reading">Further reading</a></li>
  20. </ol>
  21. <h1 id="the-generators" class="section-header"><a href="#the-generators">The generators</a></h1><h2 id="basic-pseudo-random-number-generators-prngs" class="section-header"><a href="#basic-pseudo-random-number-generators-prngs">Basic pseudo-random number generators (PRNGs)</a></h2>
  22. <p>The goal of regular, non-cryptographic PRNGs is usually to find a good
  23. balance between simplicity, quality, memory usage and performance. These
  24. algorithms are very important to Monte Carlo simulations, and also suitable
  25. for several other problems such as randomized algorithms and games (except
  26. where there is a risk of players predicting the next output value from
  27. previous values, in which case a CSPRNG should be used).</p>
  28. <p>Currently Rand provides only one PRNG, and not a very good one at that:</p>
  29. <table><thead><tr><th> name </th><th> full name </th><th> performance </th><th> memory </th><th> quality </th><th> period </th><th> features </th></tr></thead><tbody>
  30. <tr><td> <a href="struct.XorShiftRng.html"><code>XorShiftRng</code></a> </td><td> Xorshift 32/128 </td><td> ★★★☆☆ </td><td> 16 bytes </td><td> ★☆☆☆☆ </td><td> <code>u32</code> * 2<sup>128</sup> - 1 </td><td> — </td></tr>
  31. </tbody></table>
  32. <h2 id="cryptographically-secure-pseudo-random-number-generators-csprngs" class="section-header"><a href="#cryptographically-secure-pseudo-random-number-generators-csprngs">Cryptographically secure pseudo-random number generators (CSPRNGs)</a></h2>
  33. <p>CSPRNGs have much higher requirements than basic PRNGs. The primary
  34. consideration is security. Performance and simplicity are also important,
  35. but in general CSPRNGs are more complex and slower than regular PRNGs.
  36. Quality is no longer a concern, as it is a requirement for a
  37. CSPRNG that the output is basically indistinguishable from true randomness
  38. since any bias or correlation makes the output more predictable.</p>
  39. <p>There is a close relationship between CSPRNGs and cryptographic ciphers.
  40. Any block cipher can be turned into a CSPRNG by encrypting a counter. Stream
  41. ciphers are basically a CSPRNG and a combining operation, usually XOR. This
  42. means that we can easily use any stream cipher as a CSPRNG.</p>
  43. <p>Rand currently provides two trustworthy CSPRNGs and two CSPRNG-like PRNGs:</p>
  44. <table><thead><tr><th> name </th><th> full name </th><th> performance </th><th> initialization </th><th> memory </th><th> predictability </th><th> forward secrecy </th></tr></thead><tbody>
  45. <tr><td> <a href="chacha/struct.ChaChaRng.html"><code>ChaChaRng</code></a> </td><td> ChaCha20 </td><td> ★☆☆☆☆ </td><td> fast </td><td> 136 bytes </td><td> secure </td><td> no </td></tr>
  46. <tr><td> <a href="hc128/struct.Hc128Rng.html"><code>Hc128Rng</code></a> </td><td> HC-128 </td><td> ★★☆☆☆ </td><td> slow </td><td> 4176 bytes </td><td> secure </td><td> no </td></tr>
  47. <tr><td> <a href="isaac/struct.IsaacRng.html"><code>IsaacRng</code></a> </td><td> ISAAC </td><td> ★★☆☆☆ </td><td> slow </td><td> 2072 bytes </td><td> unknown </td><td> unknown </td></tr>
  48. <tr><td> <a href="isaac64/struct.Isaac64Rng.html"><code>Isaac64Rng</code></a> </td><td> ISAAC-64 </td><td> ★★☆☆☆ </td><td> slow </td><td> 4136 bytes</td><td> unknown </td><td> unknown </td></tr>
  49. </tbody></table>
  50. <p>It should be noted that the ISAAC generators are only included for
  51. historical reasons, they have been with the Rust language since the very
  52. beginning. They have good quality output and no attacks are known, but have
  53. received little attention from cryptography experts.</p>
  54. <h1 id="performance" class="section-header"><a href="#performance">Performance</a></h1>
  55. <p>First it has to be said most PRNGs are very fast, and will rarely be a
  56. performance bottleneck.</p>
  57. <p>Performance of basic PRNGs is a bit of a subtle thing. It depends a lot on
  58. the CPU architecture (32 vs. 64 bits), inlining, and also on the number of
  59. available registers. This often causes the performance to be affected by
  60. surrounding code due to inlining and other usage of registers.</p>
  61. <p>When choosing a PRNG for performance it is important to benchmark your own
  62. application due to interactions between PRNGs and surrounding code and
  63. dependence on the CPU architecture as well as the impact of the size of
  64. data requested. Because of all this, we do not include performance numbers
  65. here but merely a qualitative rating.</p>
  66. <p>CSPRNGs are a little different in that they typically generate a block of
  67. output in a cache, and pull outputs from the cache. This allows them to have
  68. good amortised performance, and reduces or completely removes the influence
  69. of surrounding code on the CSPRNG performance.</p>
  70. <h3 id="worst-case-performance" class="section-header"><a href="#worst-case-performance">Worst-case performance</a></h3>
  71. <p>Because CSPRNGs usually produce a block of values into a cache, they have
  72. poor worst case performance (in contrast to basic PRNGs, where the
  73. performance is usually quite regular).</p>
  74. <h2 id="state-size" class="section-header"><a href="#state-size">State size</a></h2>
  75. <p>Simple PRNGs often use very little memory, commonly only a few words, where
  76. a <em>word</em> is usually either <code>u32</code> or <code>u64</code>. This is not true for all
  77. non-cryptographic PRNGs however, for example the historically popular
  78. Mersenne Twister MT19937 algorithm requires 2.5 kB of state.</p>
  79. <p>CSPRNGs typically require more memory; since the seed size is recommended
  80. to be at least 192 bits and some more may be required for the algorithm,
  81. 256 bits would be approximately the minimum secure size. In practice,
  82. CSPRNGs tend to use quite a bit more, <a href="chacha/struct.ChaChaRng.html"><code>ChaChaRng</code></a> is relatively small with
  83. 136 bytes of state.</p>
  84. <h2 id="initialization-time" class="section-header"><a href="#initialization-time">Initialization time</a></h2>
  85. <p>The time required to initialize new generators varies significantly. Many
  86. simple PRNGs and even some cryptographic ones (including <a href="chacha/struct.ChaChaRng.html"><code>ChaChaRng</code></a>)
  87. only need to copy the seed value and some constants into their state, and
  88. thus can be constructed very quickly. In contrast, CSPRNGs with large state
  89. require an expensive key-expansion.</p>
  90. <h1 id="quality" class="section-header"><a href="#quality">Quality</a></h1>
  91. <p>Many basic PRNGs are not much more than a couple of bitwise and arithmetic
  92. operations. Their simplicity gives good performance, but also means there
  93. are small regularities hidden in the generated random number stream.</p>
  94. <p>How much do those hidden regularities matter? That is hard to say, and
  95. depends on how the RNG gets used. If there happen to be correlations between
  96. the random numbers and the algorithm they are used in, the results can be
  97. wrong or misleading.</p>
  98. <p>A random number generator can be considered good if it gives the correct
  99. results in as many applications as possible. The quality of PRNG
  100. algorithms can be evaluated to some extend analytically, to determine the
  101. cycle length and to rule out some correlations. Then there are empirical
  102. test suites designed to test how well a PRNG performs on a wide range of
  103. possible uses, the latest and most complete of which are <a href="http://simul.iro.umontreal.ca/testu01/tu01.html">TestU01</a> and
  104. <a href="http://pracrand.sourceforge.net/">PractRand</a>.</p>
  105. <p>CSPRNGs tend to be more complex, and have an explicit requirement to be
  106. unpredictable. This implies there must be no obvious correlations between
  107. output values.</p>
  108. <h3 id="quality-stars" class="section-header"><a href="#quality-stars">Quality stars:</a></h3>
  109. <p>PRNGs with 3 stars or more should be good enough for any purpose.
  110. 1 or 2 stars may be good enough for typical apps and games, but do not work
  111. well with all algorithms.</p>
  112. <h2 id="period" class="section-header"><a href="#period">Period</a></h2>
  113. <p>The <em>period</em> or <em>cycle length</em> of a PRNG is the number of values that can be
  114. generated after which it starts repeating the same random number stream.
  115. Many PRNGs have a fixed-size period, but for some only an expected average
  116. cycle length can be given, where the exact length depends on the seed.</p>
  117. <p>On today's hardware, even a fast RNG with a cycle length of <em>only</em>
  118. 2<sup>64</sup> can be used for centuries before cycling. Yet we recommend a
  119. period of 2<sup>128</sup> or more, which most modern PRNGs satisfy.
  120. Alternatively a PRNG with shorter period but support for multiple streams
  121. may be chosen. There are two reasons for this, as follows.</p>
  122. <p>If we see the entire period of an RNG as one long random number stream,
  123. every independently seeded RNG returns a slice of that stream. When multiple
  124. RNG are seeded randomly, there is an increasingly large chance to end up
  125. with a partially overlapping slice of the stream.</p>
  126. <p>If the period of the RNG is 2<sup>128</sup>, and an application consumes
  127. 2<sup>48</sup> values, it then takes about 2<sup>32</sup> random
  128. initializations to have a chance of 1 in a million to repeat part of an
  129. already used stream. This seems good enough for common usage of
  130. non-cryptographic generators, hence the recommendation of at least
  131. 2<sup>128</sup>. As an estimate, the chance of any overlap in a period of
  132. size <code>p</code> with <code>n</code> independent seeds and <code>u</code> values used per seed is
  133. approximately <code>1 - e^(-u * n^2 / (2 * p))</code>.</p>
  134. <p>Further, it is not recommended to use the full period of an RNG. Many
  135. PRNGs have a property called <em>k-dimensional equidistribution</em>, meaning that
  136. for values of some size (potentially larger than the output size), all
  137. possible values are produced the same number of times over the generator's
  138. period. This is not a property of true randomness. This is known as the
  139. generalized birthday problem, see the <a href="http://www.pcg-random.org/pdf/hmc-cs-2014-0905.pdf">PCG paper</a> for a good explanation.
  140. This results in a noticable bias on output after generating more values
  141. than the square root of the period (after 2<sup>64</sup> values for a
  142. period of 2<sup>128</sup>).</p>
  143. <h1 id="security" class="section-header"><a href="#security">Security</a></h1><h2 id="predictability" class="section-header"><a href="#predictability">Predictability</a></h2>
  144. <p>From the context of any PRNG, one can ask the question <em>given some previous
  145. output from the PRNG, is it possible to predict the next output value?</em>
  146. This is an important property in any situation where there might be an
  147. adversary.</p>
  148. <p>Regular PRNGs tend to be predictable, although with varying difficulty. In
  149. some cases prediction is trivial, for example plain Xorshift outputs part of
  150. its state without mutation, and prediction is as simple as seeding a new
  151. Xorshift generator from four <code>u32</code> outputs. Other generators, like
  152. <a href="http://www.pcg-random.org/predictability.html">PCG</a> and truncated Xorshift*
  153. are harder to predict, but not outside the realm of common mathematics and a
  154. desktop PC.</p>
  155. <p>The basic security that CSPRNGs must provide is the infeasibility to predict
  156. output. This requirement is formalized as the <a href="https://en.wikipedia.org/wiki/Next-bit_test">next-bit test</a>; this is
  157. roughly stated as: given the first <em>k</em> bits of a random sequence, the
  158. sequence satisfies the next-bit test if there is no algorithm able to
  159. predict the next bit using reasonable computing power.</p>
  160. <p>A further security that <em>some</em> CSPRNGs provide is forward secrecy:
  161. in the event that the CSPRNGs state is revealed at some point, it must be
  162. infeasible to reconstruct previous states or output. Note that many CSPRNGs
  163. <em>do not</em> have forward secrecy in their usual formulations.</p>
  164. <p>As an outsider it is hard to get a good idea about the security of an
  165. algorithm. People in the field of cryptography spend a lot of effort
  166. analyzing existing designs, and what was once considered good may now turn
  167. out to be weaker. Generally it is best to use algorithms well-analyzed by
  168. experts, such as those recommended by NIST or ECRYPT.</p>
  169. <h2 id="state-and-seeding" class="section-header"><a href="#state-and-seeding">State and seeding</a></h2>
  170. <p>It is worth noting that a CSPRNG's security relies absolutely on being
  171. seeded with a secure random key. Should the key be known or guessable, all
  172. output of the CSPRNG is easy to guess. This implies that the seed should
  173. come from a trusted source; usually either the OS or another CSPRNG. Our
  174. seeding helper trait, <a href="../trait.FromEntropy.html"><code>FromEntropy</code></a>, and the source it uses
  175. (<a href="../rngs/struct.EntropyRng.html"><code>EntropyRng</code></a>), should be secure. Additionally, <a href="../rngs/struct.ThreadRng.html"><code>ThreadRng</code></a> is a CSPRNG,
  176. thus it is acceptable to seed from this (although for security applications
  177. fresh/external entropy should be preferred).</p>
  178. <p>Further, it should be obvious that the internal state of a CSPRNG must be
  179. kept secret. With that in mind, our implementations do not provide direct
  180. access to most of their internal state, and <code>Debug</code> implementations do not
  181. print any internal state. This does not fully protect CSPRNG state; code
  182. within the same process may read this memory (and we allow cloning and
  183. serialisation of CSPRNGs for convenience). Further, a running process may be
  184. forked by the operating system, which may leave both processes with a copy
  185. of the same generator.</p>
  186. <h2 id="not-a-crypto-library" class="section-header"><a href="#not-a-crypto-library">Not a crypto library</a></h2>
  187. <p>It should be emphasised that this is not a cryptography library; although
  188. Rand does take some measures to provide secure random numbers, it does not
  189. necessarily take all recommended measures. Further, cryptographic processes
  190. such as encryption and authentication are complex and must be implemented
  191. very carefully to avoid flaws and resist known attacks. It is therefore
  192. recommended to use specialized libraries where possible, for example
  193. <a href="https://crates.io/crates/openssl">openssl</a>, <a href="https://crates.io/crates/ring">ring</a> and the <a href="https://github.com/RustCrypto">RustCrypto libraries</a>.</p>
  194. <h1 id="extra-features" class="section-header"><a href="#extra-features">Extra features</a></h1>
  195. <p>Some PRNGs may provide extra features, like:</p>
  196. <ul>
  197. <li>Support for multiple streams, which can help with parallel tasks.</li>
  198. <li>The ability to jump or seek around in the random number stream;
  199. with large periood this can be used as an alternative to streams.</li>
  200. </ul>
  201. <h1 id="further-reading" class="section-header"><a href="#further-reading">Further reading</a></h1>
  202. <p>There is quite a lot that can be said about PRNGs. The <a href="http://www.pcg-random.org/pdf/hmc-cs-2014-0905.pdf">PCG paper</a> is a
  203. very approachable explaining more concepts.</p>
  204. <p>A good paper about RNG quality is
  205. <a href="http://random.mat.sbg.ac.at/results/peter/A19final.pdf">&quot;Good random number generators are (not so) easy to find&quot;</a> by P. Hellekalek.</p>
  206. </div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
  207. <table><tr><td><code>pub use self::chacha::<a class="struct" href="../../rand/prng/chacha/struct.ChaChaRng.html" title="struct rand::prng::chacha::ChaChaRng">ChaChaRng</a>;</code></td></tr><tr><td><code>pub use self::hc128::<a class="struct" href="../../rand/prng/hc128/struct.Hc128Rng.html" title="struct rand::prng::hc128::Hc128Rng">Hc128Rng</a>;</code></td></tr><tr><td><code>pub use self::isaac::<a class="struct" href="../../rand/prng/isaac/struct.IsaacRng.html" title="struct rand::prng::isaac::IsaacRng">IsaacRng</a>;</code></td></tr><tr><td><code>pub use self::isaac64::<a class="struct" href="../../rand/prng/isaac64/struct.Isaac64Rng.html" title="struct rand::prng::isaac64::Isaac64Rng">Isaac64Rng</a>;</code></td></tr></table><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
  208. <table>
  209. <tr class=' module-item'>
  210. <td><a class="mod" href="chacha/index.html"
  211. title='mod rand::prng::chacha'>chacha</a></td>
  212. <td class='docblock-short'>
  213. <p>The ChaCha random number generator.</p>
  214. </td>
  215. </tr>
  216. <tr class=' module-item'>
  217. <td><a class="mod" href="hc128/index.html"
  218. title='mod rand::prng::hc128'>hc128</a></td>
  219. <td class='docblock-short'>
  220. <p>The HC-128 random number generator.</p>
  221. </td>
  222. </tr>
  223. <tr class=' module-item'>
  224. <td><a class="mod" href="isaac/index.html"
  225. title='mod rand::prng::isaac'>isaac</a></td>
  226. <td class='docblock-short'>
  227. <p>The ISAAC random number generator.</p>
  228. </td>
  229. </tr>
  230. <tr class=' module-item'>
  231. <td><a class="mod" href="isaac64/index.html"
  232. title='mod rand::prng::isaac64'>isaac64</a></td>
  233. <td class='docblock-short'>
  234. <p>The ISAAC-64 random number generator.</p>
  235. </td>
  236. </tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  237. <table>
  238. <tr class=' module-item'>
  239. <td><a class="struct" href="struct.XorShiftRng.html"
  240. title='struct rand::prng::XorShiftRng'>XorShiftRng</a></td>
  241. <td class='docblock-short'>
  242. <p>An Xorshift random number generator.</p>
  243. </td>
  244. </tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "rand";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>