rand/rngs/mod.rs
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//! - [`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.
20//! - [`ThreadRng`], provided by [`crate::rng()`], is a handle to a
21//! thread-local generator with periodic seeding from [`OsRng`]. Because this
22//! is local, it is typically much faster than [`OsRng`]. 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 ChaCha12,
33//! 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//!
51//! ### Additional generators
52//!
53//! - The [`rdrand`] crate provides an interface to the RDRAND and RDSEED
54//! instructions available in modern Intel and AMD CPUs.
55//! - The [`rand_jitter`] crate provides a user-space implementation of
56//! entropy harvesting from CPU timer jitter, but is very slow and has
57//! [security issues](https://github.com/rust-random/rand/issues/699).
58//! - The [`rand_chacha`] crate provides portable implementations of
59//! generators derived from the [ChaCha] family of stream ciphers
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 [`RngCore`] and thus also [`Rng`][crate::Rng].
70//! See also the [Random Values] chapter in the book.
71//!
72//! Secure RNGs may additionally implement the [`CryptoRng`] trait.
73//!
74//! Use the [`rand_core`] crate when implementing your own RNGs.
75//!
76//! [guarantees of reproducibility]: https://rust-random.github.io/book/crate-reprod.html
77//! [Types of generators]: https://rust-random.github.io/book/guide-gen.html
78//! [Our RNGs]: https://rust-random.github.io/book/guide-rngs.html
79//! [Random Values]: https://rust-random.github.io/book/guide-values.html
80//! [`Rng`]: crate::Rng
81//! [`RngCore`]: crate::RngCore
82//! [`CryptoRng`]: crate::CryptoRng
83//! [`SeedableRng`]: crate::SeedableRng
84//! [`rdrand`]: https://crates.io/crates/rdrand
85//! [`rand_jitter`]: https://crates.io/crates/rand_jitter
86//! [`rand_chacha`]: https://crates.io/crates/rand_chacha
87//! [`rand_pcg`]: https://crates.io/crates/rand_pcg
88//! [`rand_xoshiro`]: https://crates.io/crates/rand_xoshiro
89//! [crates with the `rng` tag]: https://crates.io/keywords/rng
90//! [chacha]: https://cr.yp.to/chacha.html
91//! [PCG]: https://www.pcg-random.org/
92//! [xoshiro]: https://prng.di.unimi.it/
93
94mod reseeding;
95pub use reseeding::ReseedingRng;
96
97#[deprecated(since = "0.9.2")]
98pub mod mock; // Public so we don't export `StepRng` directly, making it a bit
99 // more clear it is intended for testing.
100
101#[cfg(feature = "small_rng")]
102mod small;
103#[cfg(feature = "small_rng")]
104mod xoshiro128plusplus;
105#[cfg(feature = "small_rng")]
106mod xoshiro256plusplus;
107
108#[cfg(feature = "std_rng")]
109mod std;
110#[cfg(feature = "thread_rng")]
111pub(crate) mod thread;
112
113#[cfg(feature = "small_rng")]
114pub use self::small::SmallRng;
115#[cfg(feature = "small_rng")]
116pub use xoshiro128plusplus::Xoshiro128PlusPlus;
117#[cfg(feature = "small_rng")]
118pub use xoshiro256plusplus::Xoshiro256PlusPlus;
119
120#[cfg(feature = "std_rng")]
121pub use self::std::StdRng;
122#[cfg(feature = "thread_rng")]
123pub use self::thread::ThreadRng;
124
125#[cfg(feature = "os_rng")]
126pub use rand_core::OsRng;