| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[cfg_attr(docsrs, 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 |
fn next_u32(&mut self) -> u32 { |
| 39 |
self.0.next_u32() |
| 40 |
} |
| 41 |
|
| 42 |
#[inline(always)] |
| 43 |
fn next_u64(&mut self) -> u64 { |
| 44 |
self.0.next_u64() |
| 45 |
} |
| 46 |
|
| 47 |
#[inline(always)] |
| 48 |
fn fill_bytes(&mut self, dest: &mut [u8]) { |
| 49 |
self.0.fill_bytes(dest); |
| 50 |
} |
| 51 |
|
| 52 |
#[inline(always)] |
| 53 |
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { |
| 54 |
self.0.try_fill_bytes(dest) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl SeedableRng for StdRng { |
| 59 |
type Seed = <Rng as SeedableRng>::Seed; |
| 60 |
|
| 61 |
#[inline(always)] |
| 62 |
fn from_seed(seed: Self::Seed) -> Self { |
| 63 |
StdRng(Rng::from_seed(seed)) |
| 64 |
} |
| 65 |
|
| 66 |
#[inline(always)] |
| 67 |
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> { |
| 68 |
Rng::from_rng(rng).map(StdRng) |
| 69 |
} |
| 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 |
|
| 83 |
|
| 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 |
} |