rand/rngs/
std.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//! The standard RNG
10
11use crate::{CryptoRng, Error, RngCore, SeedableRng};
12
13#[cfg(all(any(test, feature = "std"), not(target_os = "emscripten")))]
14pub(crate) use rand_chacha::ChaCha12Core as Core;
15#[cfg(all(any(test, feature = "std"), target_os = "emscripten"))]
16pub(crate) use rand_hc::Hc128Core as Core;
17
18#[cfg(not(target_os = "emscripten"))] use rand_chacha::ChaCha12Rng as Rng;
19#[cfg(target_os = "emscripten")] use rand_hc::Hc128Rng as Rng;
20
21/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
22/// on the current platform, to be statistically strong and unpredictable
23/// (meaning a cryptographically secure PRNG).
24///
25/// The current algorithm used is the ChaCha block cipher with 12 rounds. Please
26/// see this relevant [rand issue] for the discussion. This may change as new 
27/// evidence of cipher security and performance becomes available.
28///
29/// The algorithm is deterministic but should not be considered reproducible
30/// due to dependence on configuration and possible replacement in future
31/// library versions. For a secure reproducible generator, we recommend use of
32/// the [rand_chacha] crate directly.
33///
34/// [rand_chacha]: https://crates.io/crates/rand_chacha
35/// [rand issue]: https://github.com/rust-random/rand/issues/932
36#[cfg_attr(doc_cfg, doc(cfg(feature = "std_rng")))]
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct StdRng(Rng);
39
40impl RngCore for StdRng {
41    #[inline(always)]
42    fn next_u32(&mut self) -> u32 {
43        self.0.next_u32()
44    }
45
46    #[inline(always)]
47    fn next_u64(&mut self) -> u64 {
48        self.0.next_u64()
49    }
50
51    #[inline(always)]
52    fn fill_bytes(&mut self, dest: &mut [u8]) {
53        self.0.fill_bytes(dest);
54    }
55
56    #[inline(always)]
57    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
58        self.0.try_fill_bytes(dest)
59    }
60}
61
62impl SeedableRng for StdRng {
63    type Seed = <Rng as SeedableRng>::Seed;
64
65    #[inline(always)]
66    fn from_seed(seed: Self::Seed) -> Self {
67        StdRng(Rng::from_seed(seed))
68    }
69
70    #[inline(always)]
71    fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
72        Rng::from_rng(rng).map(StdRng)
73    }
74}
75
76impl CryptoRng for StdRng {}
77
78
79#[cfg(test)]
80mod test {
81    use crate::rngs::StdRng;
82    use crate::{RngCore, SeedableRng};
83
84    #[test]
85    fn test_stdrng_construction() {
86        // Test value-stability of StdRng. This is expected to break any time
87        // the algorithm is changed.
88        #[rustfmt::skip]
89        let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0,
90                    0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
91
92        let target = [10719222850664546238, 14064965282130556830];
93
94        let mut rng0 = StdRng::from_seed(seed);
95        let x0 = rng0.next_u64();
96
97        let mut rng1 = StdRng::from_rng(rng0).unwrap();
98        let x1 = rng1.next_u64();
99
100        assert_eq!([x0, x1], target);
101    }
102}