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.
15 use core::prelude::v1::*;
17 use {Rng, SeedableRng};
19 /// How many bytes of entropy the underling RNG is allowed to generate
20 /// before it is reseeded.
21 const DEFAULT_GENERATION_THRESHOLD: usize = 32 * 1024;
23 /// A wrapper around any RNG which reseeds the underlying RNG after it
24 /// has generated a certain number of random bytes.
25 pub struct ReseedingRng<R, Rsdr> {
27 generation_threshold: usize,
28 bytes_generated: usize,
29 /// Controls the behaviour when reseeding the RNG.
33 impl<R: Rng, Rsdr: Reseeder<R>> ReseedingRng<R, Rsdr> {
34 /// Create a new `ReseedingRng` with the given parameters.
38 /// * `rng`: the random number generator to use.
39 /// * `generation_threshold`: the number of bytes of entropy at which to reseed the RNG.
40 /// * `reseeder`: the reseeding object to use.
41 pub fn new(rng: R, generation_threshold: usize, reseeder: Rsdr) -> ReseedingRng<R,Rsdr> {
44 generation_threshold: generation_threshold,
50 /// Reseed the internal RNG if the number of bytes that have been
51 /// generated exceed the threshold.
52 pub fn reseed_if_necessary(&mut self) {
53 if self.bytes_generated >= self.generation_threshold {
54 self.reseeder.reseed(&mut self.rng);
55 self.bytes_generated = 0;
61 impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> {
62 fn next_u32(&mut self) -> u32 {
63 self.reseed_if_necessary();
64 self.bytes_generated += 4;
68 fn next_u64(&mut self) -> u64 {
69 self.reseed_if_necessary();
70 self.bytes_generated += 8;
74 fn fill_bytes(&mut self, dest: &mut [u8]) {
75 self.reseed_if_necessary();
76 self.bytes_generated += dest.len();
77 self.rng.fill_bytes(dest)
81 impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R> + Default>
82 SeedableRng<(Rsdr, S)> for ReseedingRng<R, Rsdr> {
83 fn reseed(&mut self, (rsdr, seed): (Rsdr, S)) {
84 self.rng.reseed(seed);
86 self.bytes_generated = 0;
89 /// Create a new `ReseedingRng` from the given reseeder and
90 /// seed. This uses a default value for `generation_threshold`.
91 fn from_seed((rsdr, seed): (Rsdr, S)) -> ReseedingRng<R, Rsdr> {
93 rng: SeedableRng::from_seed(seed),
94 generation_threshold: DEFAULT_GENERATION_THRESHOLD,
101 /// Something that can be used to reseed an RNG via `ReseedingRng`.
102 pub trait Reseeder<R> {
103 /// Reseed the given RNG.
104 fn reseed(&mut self, rng: &mut R);
107 /// Reseed an RNG using a `Default` instance. This reseeds by
108 /// replacing the RNG with the result of a `Default::default` call.
109 #[derive(Copy, Clone)]
110 pub struct ReseedWithDefault;
112 impl<R: Rng + Default> Reseeder<R> for ReseedWithDefault {
113 fn reseed(&mut self, rng: &mut R) {
114 *rng = Default::default();
117 #[stable(feature = "rust1", since = "1.0.0")]
118 impl Default for ReseedWithDefault {
119 #[stable(feature = "rust1", since = "1.0.0")]
120 fn default() -> ReseedWithDefault { ReseedWithDefault }
125 use std::prelude::v1::*;
127 use core::iter::{order, repeat};
128 use super::{ReseedingRng, ReseedWithDefault};
129 use {SeedableRng, Rng};
135 impl Rng for Counter {
136 fn next_u32(&mut self) -> u32 {
142 impl Default for Counter {
143 fn default() -> Counter {
147 impl SeedableRng<u32> for Counter {
148 fn reseed(&mut self, seed: u32) {
151 fn from_seed(seed: u32) -> Counter {
155 type MyRng = ReseedingRng<Counter, ReseedWithDefault>;
158 fn test_reseeding() {
159 let mut rs = ReseedingRng::new(Counter {i:0}, 400, ReseedWithDefault);
163 assert_eq!(rs.next_u32(), i % 100);
169 fn test_rng_seeded() {
170 let mut ra: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
171 let mut rb: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
172 assert!(order::equals(ra.gen_ascii_chars().take(100),
173 rb.gen_ascii_chars().take(100)));
177 fn test_rng_reseed() {
178 let mut r: MyRng = SeedableRng::from_seed((ReseedWithDefault, 3));
179 let string1: String = r.gen_ascii_chars().take(100).collect();
181 r.reseed((ReseedWithDefault, 3));
183 let string2: String = r.gen_ascii_chars().take(100).collect();
184 assert_eq!(string1, string2);
187 const FILL_BYTES_V_LEN: usize = 13579;
189 fn test_rng_fill_bytes() {
190 let mut v = vec![0; FILL_BYTES_V_LEN];
191 ::test::rng().fill_bytes(&mut v);
193 // Sanity test: if we've gotten here, `fill_bytes` has not infinitely
195 assert_eq!(v.len(), FILL_BYTES_V_LEN);
197 // To test that `fill_bytes` actually did something, check that the
198 // average of `v` is not 0.
203 assert!(sum / v.len() as f64 != 0.0);