Debian Codegraph @

debian/rust-vendor/rand/src/rngs/mod.rs

raw
1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! Random number generators and adapters
10 //!
11 //! ## Generators
12 //!
13 //! This crate provides a small selection of generators.
14 //! See also [Types of generators] and [Our RNGs] in the book.
15 //!
16 //! ##### Non-deterministic generators
17 //!
18 //! - [`SysRng`] is a stateless interface over the operating system's random number
19 //! source. This is typically secure with some form of periodic re-seeding.
20 //! - [`ThreadRng`], provided by [`crate::rng()`], is a handle to a
21 //! thread-local generator with periodic seeding from [`SysRng`]. Because this
22 //! is local, it is typically much faster than [`SysRng`]. It should be
23 //! secure, but see documentation on [`ThreadRng`].
24 //!
25 //! ##### Standard generators
26 //!
27 //! These use selected best-in-class algorithms. They are deterministic but not
28 //! portable: the algorithms may be changed in any release and may be
29 //! platform-dependent.
30 //!
31 //! - [`StdRng`] is a CSPRNG chosen for good performance and trust of security
32 //! (based on reviews, maturity and usage). The current algorithm is
33 //! [`ChaCha12Rng`], which is well established and rigorously analysed.
34 //! [`StdRng`] is the deterministic generator used by [`ThreadRng`] but
35 //! without the periodic reseeding or thread-local management.
36 //! - [`SmallRng`] is a relatively simple, insecure generator designed to be
37 //! fast, use little memory, and pass various statistical tests of
38 //! randomness quality. The current algorithm is one of the Xoshiro
39 //! generators below, depending on the target's pointer size.
40 //!
41 //! ##### Named portable generators
42 //!
43 //! These are similar to the [standard generators](#standard-generators), but
44 //! with the additional [guarantees of reproducibility]:
45 //!
46 //! - [`Xoshiro256PlusPlus`] is a very fast 64-bit insecure generator using
47 //! 256 bits of state with good performance in statistical tests of quality
48 //! - [`Xoshiro128PlusPlus`] is a very fast 32-bit insecure generator using
49 //! 128 bits of state with good performance in statistical tests of quality
50 //! - [`ChaCha8Rng`], [`ChaCha12Rng`] and [`ChaCha20Rng`] are generators over
51 //! the ChaCha stream cipher designed by Daniel J. Bernstein[^1].
52 //!
53 //! ### Additional generators
54 //!
55 //! - The [`rdrand`] crate provides an interface to the RDRAND and RDSEED
56 //! instructions available in modern Intel and AMD CPUs.
57 //! - The [`rand_jitter`] crate provides a user-space implementation of
58 //! entropy harvesting from CPU timer jitter, but is very slow and has
59 //! [security issues](https://github.com/rust-random/rand/issues/699).
60 //! - The [`rand_pcg`] crate provides portable implementations of a subset
61 //! of the [PCG] family of small, insecure generators
62 //! - The [`rand_xoshiro`] crate provides portable implementations of the
63 //! [xoshiro] family of small, insecure generators
64 //!
65 //! For more, search [crates with the `rng` tag].
66 //!
67 //! ## Traits and functionality
68 //!
69 //! All generators implement [`TryRng`]. Most implement [`Rng`] (i.e.
70 //! `TryRng<Error = Infallible>`) and thus also implement [`RngExt`].
71 //! See also the [Random Values] chapter in the book.
72 //!
73 //! Secure RNGs may additionally implement the [`CryptoRng`] trait.
74 //!
75 //! Use the [`rand_core`] crate when implementing your own RNGs.
76 //!
77 //! [^1]: D. J. Bernstein, [*ChaCha, a variant of Salsa20*](https://cr.yp.to/chacha.html)
78 //!
79 //! [guarantees of reproducibility]: https://rust-random.github.io/book/crate-reprod.html
80 //! [Types of generators]: https://rust-random.github.io/book/guide-gen.html
81 //! [Our RNGs]: https://rust-random.github.io/book/guide-rngs.html
82 //! [Random Values]: https://rust-random.github.io/book/guide-values.html
83 //! [`RngExt`]: crate::RngExt
84 //! [`TryRng`]: crate::TryRng
85 //! [`Rng`]: crate::Rng
86 //! [`CryptoRng`]: crate::CryptoRng
87 //! [`SeedableRng`]: crate::SeedableRng
88 //! [`rdrand`]: https://crates.io/crates/rdrand
89 //! [`rand_jitter`]: https://crates.io/crates/rand_jitter
90 //! [`rand_pcg`]: https://crates.io/crates/rand_pcg
91 //! [`rand_xoshiro`]: https://crates.io/crates/rand_xoshiro
92 //! [crates with the `rng` tag]: https://crates.io/keywords/rng
93 //! [chacha]: https://cr.yp.to/chacha.html
94 //! [PCG]: https://www.pcg-random.org/
95 //! [xoshiro]: https://prng.di.unimi.it/
96
97 mod small;
98 mod xoshiro128plusplus;
99 mod xoshiro256plusplus;
100
101 #[cfg(feature = "std_rng")]
102 mod std;
103 #[cfg(feature = "thread_rng")]
104 pub(crate) mod thread;
105
106 pub use self::small::SmallRng;
107 pub use xoshiro128plusplus::Xoshiro128PlusPlus;
108 pub use xoshiro256plusplus::Xoshiro256PlusPlus;
109
110 #[cfg(feature = "std_rng")]
111 pub use self::std::StdRng;
112 #[cfg(feature = "thread_rng")]
113 pub use self::thread::ThreadRng;
114
115 #[cfg(feature = "chacha")]
116 pub use chacha20::{ChaCha8Rng, ChaCha12Rng, ChaCha20Rng};
117
118 #[cfg(feature = "sys_rng")]
119 pub use getrandom::{Error as SysError, SysRng};