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