1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A wrapper around another RNG that reseeds it after it
12 //! generates a certain number of random bytes.
14 use {Rng, SeedableRng};
16 /// How many bytes of entropy the underling RNG is allowed to generate
17 /// before it is reseeded.
18 const DEFAULT_GENERATION_THRESHOLD: usize = 32 * 1024;
20 /// A wrapper around any RNG which reseeds the underlying RNG after it
21 /// has generated a certain number of random bytes.
22 pub struct ReseedingRng<R, Rsdr> {
24 generation_threshold: usize,
25 bytes_generated: usize,
26 /// Controls the behaviour when reseeding the RNG.
30 impl<R: Rng, Rsdr: Reseeder<R>> ReseedingRng<R, Rsdr> {
31 /// Create a new `ReseedingRng` with the given parameters.
35 /// * `rng`: the random number generator to use.
36 /// * `generation_threshold`: the number of bytes of entropy at which to reseed the RNG.
37 /// * `reseeder`: the reseeding object to use.
38 pub fn new(rng: R, generation_threshold: usize, reseeder: Rsdr) -> ReseedingRng<R, Rsdr> {
41 generation_threshold: generation_threshold,
47 /// Reseed the internal RNG if the number of bytes that have been
48 /// generated exceed the threshold.
49 pub fn reseed_if_necessary(&mut self) {
50 if self.bytes_generated >= self.generation_threshold {
51 self.reseeder.reseed(&mut self.rng);
52 self.bytes_generated = 0;
58 impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> {
59 fn next_u32(&mut self) -> u32 {
60 self.reseed_if_necessary();
61 self.bytes_generated += 4;
65 fn next_u64(&mut self) -> u64 {
66 self.reseed_if_necessary();
67 self.bytes_generated += 8;
71 fn fill_bytes(&mut self, dest: &mut [u8]) {
72 self.reseed_if_necessary();
73 self.bytes_generated += dest.len();
74 self.rng.fill_bytes(dest)
78 impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R> + Default>
79 SeedableRng<(Rsdr, S)> for ReseedingRng<R, Rsdr> {
80 fn reseed(&mut self, (rsdr, seed): (Rsdr, S)) {
81 self.rng.reseed(seed);
83 self.bytes_generated = 0;
86 /// Create a new `ReseedingRng` from the given reseeder and
87 /// seed. This uses a default value for `generation_threshold`.
88 fn from_seed((rsdr, seed): (Rsdr, S)) -> ReseedingRng<R, Rsdr> {
90 rng: SeedableRng::from_seed(seed),
91 generation_threshold: DEFAULT_GENERATION_THRESHOLD,
98 /// Something that can be used to reseed an RNG via `ReseedingRng`.
99 pub trait Reseeder<R> {
100 /// Reseed the given RNG.
101 fn reseed(&mut self, rng: &mut R);
104 /// Reseed an RNG using a `Default` instance. This reseeds by
105 /// replacing the RNG with the result of a `Default::default` call.
106 #[derive(Copy, Clone)]
107 pub struct ReseedWithDefault;
109 impl<R: Rng + Default> Reseeder<R> for ReseedWithDefault {
110 fn reseed(&mut self, rng: &mut R) {
111 *rng = Default::default();
114 #[stable(feature = "rust1", since = "1.0.0")]
115 impl Default for ReseedWithDefault {
116 fn default() -> ReseedWithDefault {
123 use std::prelude::v1::*;
125 use core::iter::order;
126 use super::{ReseedingRng, ReseedWithDefault};
127 use {SeedableRng, Rng};
133 impl Rng for Counter {
134 fn next_u32(&mut self) -> u32 {
140 impl Default for Counter {
141 fn default() -> Counter {
145 impl SeedableRng<u32> for Counter {
146 fn reseed(&mut self, seed: u32) {
149 fn from_seed(seed: u32) -> Counter {
153 type MyRng = ReseedingRng<Counter, ReseedWithDefault>;
156 fn test_reseeding() {
157 let mut rs = ReseedingRng::new(Counter { i: 0 }, 400, ReseedWithDefault);
161 assert_eq!(rs.next_u32(), i % 100);
167 fn test_rng_seeded() {
168 let mut ra: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
169 let mut rb: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
170 assert!(order::equals(ra.gen_ascii_chars().take(100),
171 rb.gen_ascii_chars().take(100)));
175 fn test_rng_reseed() {
176 let mut r: MyRng = SeedableRng::from_seed((ReseedWithDefault, 3));
177 let string1: String = r.gen_ascii_chars().take(100).collect();
179 r.reseed((ReseedWithDefault, 3));
181 let string2: String = r.gen_ascii_chars().take(100).collect();
182 assert_eq!(string1, string2);
185 const FILL_BYTES_V_LEN: usize = 13579;
187 fn test_rng_fill_bytes() {
188 let mut v = vec![0; FILL_BYTES_V_LEN];
189 ::test::rng().fill_bytes(&mut v);
191 // Sanity test: if we've gotten here, `fill_bytes` has not infinitely
193 assert_eq!(v.len(), FILL_BYTES_V_LEN);
195 // To test that `fill_bytes` actually did something, check that the
196 // average of `v` is not 0.
201 assert!(sum / v.len() as f64 != 0.0);