|
8 | 8 |
|
9 | 9 | //! Random number generators and adapters
|
10 | 10 | //!
|
11 |
| -//! ## Background: Random number generators (RNGs) |
| 11 | +//! This crate provides a small selection of non-[portable] generators. |
| 12 | +//! See also [Types of generators] and [Our RNGs] in the book. |
12 | 13 | //!
|
13 |
| -//! Computers cannot produce random numbers from nowhere. We classify |
14 |
| -//! random number generators as follows: |
| 14 | +//! ## Generators |
15 | 15 | //!
|
16 |
| -//! - "True" random number generators (TRNGs) use hard-to-predict data sources |
17 |
| -//! (e.g. the high-resolution parts of event timings and sensor jitter) to |
18 |
| -//! harvest random bit-sequences, apply algorithms to remove bias and |
19 |
| -//! estimate available entropy, then combine these bits into a byte-sequence |
20 |
| -//! or an entropy pool. This job is usually done by the operating system or |
21 |
| -//! a hardware generator (HRNG). |
22 |
| -//! - "Pseudo"-random number generators (PRNGs) use algorithms to transform a |
23 |
| -//! seed into a sequence of pseudo-random numbers. These generators can be |
24 |
| -//! fast and produce well-distributed unpredictable random numbers (or not). |
25 |
| -//! They are usually deterministic: given algorithm and seed, the output |
26 |
| -//! sequence can be reproduced. They have finite period and eventually loop; |
27 |
| -//! with many algorithms this period is fixed and can be proven sufficiently |
28 |
| -//! long, while others are chaotic and the period depends on the seed. |
29 |
| -//! - "Cryptographically secure" pseudo-random number generators (CSPRNGs) |
30 |
| -//! are the sub-set of PRNGs which are secure. Security of the generator |
31 |
| -//! relies both on hiding the internal state and using a strong algorithm. |
| 16 | +//! This crate provides a small selection of non-[portable] random number generators: |
32 | 17 | //!
|
33 |
| -//! ## Traits and functionality |
34 |
| -//! |
35 |
| -//! All RNGs implement the [`RngCore`] trait, as a consequence of which the |
36 |
| -//! [`Rng`] extension trait is automatically implemented. Secure RNGs may |
37 |
| -//! additionally implement the [`CryptoRng`] trait. |
38 |
| -//! |
39 |
| -//! All PRNGs require a seed to produce their random number sequence. The |
40 |
| -//! [`SeedableRng`] trait provides three ways of constructing PRNGs: |
41 |
| -//! |
42 |
| -//! - `from_seed` accepts a type specific to the PRNG |
43 |
| -//! - `from_rng` allows a PRNG to be seeded from any other RNG |
44 |
| -//! - `seed_from_u64` allows any PRNG to be seeded from a `u64` insecurely |
45 |
| -//! - `from_os_rng` securely seeds a PRNG from system randomness source |
46 |
| -//! |
47 |
| -//! Use the [`rand_core`] crate when implementing your own RNGs. |
48 |
| -//! |
49 |
| -//! ## Our generators |
50 |
| -//! |
51 |
| -//! This crate provides several random number generators: |
52 |
| -//! |
53 |
| -//! - [`OsRng`] is an interface to the operating system's random number |
54 |
| -//! source. Typically the operating system uses a CSPRNG with entropy |
55 |
| -//! provided by a TRNG and some type of on-going re-seeding. |
| 18 | +//! - [`OsRng`] is a stateless interface over the operating system's random number |
| 19 | +//! source. This is typically secure with some form of periodic re-seeding. |
56 | 20 | //! - [`ThreadRng`], provided by the [`thread_rng`] function, is a handle to a
|
57 |
| -//! thread-local CSPRNG with periodic seeding from [`OsRng`]. Because this |
| 21 | +//! thread-local generator with periodic seeding from [`OsRng`]. Because this |
58 | 22 | //! is local, it is typically much faster than [`OsRng`]. It should be
|
59 |
| -//! secure, though the paranoid may prefer [`OsRng`]. |
| 23 | +//! secure, but see documentation on [`ThreadRng`]. |
60 | 24 | //! - [`StdRng`] is a CSPRNG chosen for good performance and trust of security
|
61 | 25 | //! (based on reviews, maturity and usage). The current algorithm is ChaCha12,
|
62 | 26 | //! which is well established and rigorously analysed.
|
63 |
| -//! [`StdRng`] provides the algorithm used by [`ThreadRng`] but without |
64 |
| -//! periodic reseeding. |
65 |
| -//! - [`SmallRng`] is an **insecure** PRNG designed to be fast, simple, require |
66 |
| -//! little memory, and have good output quality. |
| 27 | +//! [`StdRng`] is the deterministic generator used by [`ThreadRng`] but |
| 28 | +//! without the periodic reseeding or thread-local management. |
| 29 | +//! - [`SmallRng`] is a relatively simple, insecure generator designed to be |
| 30 | +//! fast, use little memory, and pass various statistical tests of |
| 31 | +//! randomness quality. |
67 | 32 | //!
|
68 | 33 | //! The algorithms selected for [`StdRng`] and [`SmallRng`] may change in any
|
69 |
| -//! release and may be platform-dependent, therefore they should be considered |
70 |
| -//! **not reproducible**. |
| 34 | +//! release and may be platform-dependent, therefore they are not |
| 35 | +//! [reproducible][portable]. |
| 36 | +//! |
| 37 | +//! ### Additional generators |
71 | 38 | //!
|
72 |
| -//! ## Additional generators |
| 39 | +//! - The [`rdrand`] crate provides an interface to the RDRAND and RDSEED |
| 40 | +//! instructions available in modern Intel and AMD CPUs. |
| 41 | +//! - The [`rand_jitter`] crate provides a user-space implementation of |
| 42 | +//! entropy harvesting from CPU timer jitter, but is very slow and has |
| 43 | +//! [security issues](https://github.com/rust-random/rand/issues/699). |
| 44 | +//! - The [`rand_chacha`] crate provides [portable] implementations of |
| 45 | +//! generators derived from the [ChaCha] family of stream ciphers |
| 46 | +//! - The [`rand_pcg`] crate provides [portable] implementations of a subset |
| 47 | +//! of the [PCG] family of small, insecure generators |
| 48 | +//! - The [`rand_xoshiro`] crate provides [portable] implementations of the |
| 49 | +//! [xoshiro] family of small, insecure generators |
73 | 50 | //!
|
74 |
| -//! **TRNGs**: The [`rdrand`] crate provides an interface to the RDRAND and |
75 |
| -//! RDSEED instructions available in modern Intel and AMD CPUs. |
76 |
| -//! The [`rand_jitter`] crate provides a user-space implementation of |
77 |
| -//! entropy harvesting from CPU timer jitter, but is very slow and has |
78 |
| -//! [security issues](https://github.com/rust-random/rand/issues/699). |
| 51 | +//! For more, search [crates with the `rng` tag]. |
| 52 | +//! |
| 53 | +//! ## Traits and functionality |
79 | 54 | //!
|
80 |
| -//! **PRNGs**: Several companion crates are available, providing individual or |
81 |
| -//! families of PRNG algorithms. These provide the implementations behind |
82 |
| -//! [`StdRng`] and [`SmallRng`] but can also be used directly, indeed *should* |
83 |
| -//! be used directly when **reproducibility** matters. |
84 |
| -//! Some suggestions are: [`rand_chacha`], [`rand_pcg`], [`rand_xoshiro`]. |
85 |
| -//! A full list can be found by searching for crates with the [`rng` tag]. |
| 55 | +//! All generators implement [`RngCore`] and thus also [`Rng`][crate::Rng]. |
| 56 | +//! See also the [Random Values] chapter in the book. |
| 57 | +//! |
| 58 | +//! Secure RNGs may additionally implement the [`CryptoRng`] trait. |
| 59 | +//! |
| 60 | +//! Use the [`rand_core`] crate when implementing your own RNGs. |
86 | 61 | //!
|
| 62 | +//! [portable]: https://rust-random.github.io/book/crate-reprod.html |
| 63 | +//! [Types of generators]: https://rust-random.github.io/book/guide-gen.html |
| 64 | +//! [Our RNGs]: https://rust-random.github.io/book/guide-rngs.html |
| 65 | +//! [Random Values]: https://rust-random.github.io/book/guide-values.html |
87 | 66 | //! [`Rng`]: crate::Rng
|
88 | 67 | //! [`RngCore`]: crate::RngCore
|
89 | 68 | //! [`CryptoRng`]: crate::CryptoRng
|
|
94 | 73 | //! [`rand_chacha`]: https://crates.io/crates/rand_chacha
|
95 | 74 | //! [`rand_pcg`]: https://crates.io/crates/rand_pcg
|
96 | 75 | //! [`rand_xoshiro`]: https://crates.io/crates/rand_xoshiro
|
97 |
| -//! [`rng` tag]: https://crates.io/keywords/rng |
| 76 | +//! [crates with the `rng` tag]: https://crates.io/keywords/rng |
| 77 | +//! [chacha]: https://cr.yp.to/chacha.html |
| 78 | +//! [PCG]: https://www.pcg-random.org/ |
| 79 | +//! [xoshiro]: https://prng.di.unimi.it/ |
98 | 80 |
|
99 | 81 | mod reseeding;
|
100 | 82 | pub use reseeding::ReseedingRng;
|
|
0 commit comments