From 86de12b6f6efb10d403e005489e23ac473b7c11a Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 24 Sep 2017 20:34:12 +0200 Subject: [PATCH] Remove `reseed` from `SeedableRng` --- rand_core/src/lib.rs | 6 ------ rand_core/src/mock.rs | 3 --- src/prng/chacha.rs | 31 ++++++++----------------------- src/prng/isaac.rs | 38 +++++++++----------------------------- src/prng/isaac64.rs | 38 +++++++++----------------------------- src/prng/xorshift.rs | 11 ----------- src/reseeding.rs | 17 ----------------- 7 files changed, 26 insertions(+), 118 deletions(-) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 86f8eab9586..651f9b51335 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -172,12 +172,6 @@ pub trait SeedFromRng: Sized { /// algorithm used in the future. This is to ensure that manual seeding of PRNGs /// actually does yield reproducible results. pub trait SeedableRng: Rng { - /// Reseed an RNG with the given seed. - /// - /// The type of `Seed` is specified by the implementation (implementation - /// for multiple seed types is possible). - fn reseed(&mut self, Seed); - /// Create a new RNG with the given seed. /// /// The type of `Seed` is specified by the implementation (implementation diff --git a/rand_core/src/mock.rs b/rand_core/src/mock.rs index ecfea3184a5..5abe444aa8c 100644 --- a/rand_core/src/mock.rs +++ b/rand_core/src/mock.rs @@ -85,9 +85,6 @@ impl SeedableRng for MockAddRng where MockAddRng: Rng, T: From, // for 1.into() { - fn reseed(&mut self, seed: T) { - self.v = w(seed); - } fn from_seed(seed: T) -> Self { MockAddRng::new(seed, 1.into()) } diff --git a/src/prng/chacha.rs b/src/prng/chacha.rs index 2f0a5f78794..bfd7133e565 100644 --- a/src/prng/chacha.rs +++ b/src/prng/chacha.rs @@ -255,23 +255,20 @@ impl SeedFromRng for ChaChaRng { } impl<'a> SeedableRng<&'a [u32]> for ChaChaRng { - fn reseed(&mut self, seed: &'a [u32]) { - // reset state - self.init(&[0u32; KEY_WORDS]); - // set key in place - let key = &mut self.state[4 .. 4+KEY_WORDS]; - for (k, s) in key.iter_mut().zip(seed.iter()) { - *k = w(*s); - } - } - /// Create a ChaCha generator from a seed, /// obtained from a variable-length u32 array. /// Only up to 8 words are used; if less than 8 /// words are used, the remaining are set to zero. fn from_seed(seed: &'a [u32]) -> ChaChaRng { let mut rng = EMPTY; - rng.reseed(seed); + rng.init(&[0u32; KEY_WORDS]); + // set key in place + { + let key = &mut rng.state[4 .. 4+KEY_WORDS]; + for (k, s) in key.iter_mut().zip(seed.iter()) { + *k = w(*s); + } + } rng } } @@ -301,18 +298,6 @@ mod test { iter(&mut rb).map(|rng| ascii_word_char(rng)).take(100))); } - #[test] - fn test_rng_reseed() { - let s = iter(&mut ::test::rng()).map(|rng| rng.next_u32()).take(8).collect::>(); - let mut r: ChaChaRng = SeedableRng::from_seed(&s[..]); - let string1: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - - r.reseed(&s); - - let string2: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - assert_eq!(string1, string2); - } - #[test] fn test_rng_true_values() { // Test vectors 1 and 2 from diff --git a/src/prng/isaac.rs b/src/prng/isaac.rs index 4cba80d4daa..8b762a03b31 100644 --- a/src/prng/isaac.rs +++ b/src/prng/isaac.rs @@ -306,22 +306,6 @@ impl SeedFromRng for IsaacRng { } impl<'a> SeedableRng<&'a [u32]> for IsaacRng { - fn reseed(&mut self, seed: &'a [u32]) { - // make the seed into [seed[0], seed[1], ..., seed[seed.len() - // - 1], 0, 0, ...], to fill rng.rsl. - let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u32)); - - for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) { - *rsl_elem = w(seed_elem); - } - self.cnt = 0; - self.a = w(0); - self.b = w(0); - self.c = w(0); - - self.init(true); - } - /// Create an ISAAC random number generator with a seed. This can /// be any length, although the maximum number of elements used is /// 256 and any more will be silently ignored. A generator @@ -329,7 +313,15 @@ impl<'a> SeedableRng<&'a [u32]> for IsaacRng { /// of values as all other generators constructed with that seed. fn from_seed(seed: &'a [u32]) -> IsaacRng { let mut rng = EMPTY; - rng.reseed(seed); + + // make the seed into [seed[0], seed[1], ..., seed[seed.len() + // - 1], 0, 0, ...], to fill rng.rsl. + let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u32)); + + for (rsl_elem, seed_elem) in rng.rsl.iter_mut().zip(seed_iter) { + *rsl_elem = w(seed_elem); + } + rng.init(true); rng } } @@ -364,18 +356,6 @@ mod test { iter(&mut rb).map(|rng| ascii_word_char(rng)).take(100))); } - #[test] - fn test_rng_32_reseed() { - let s = iter(&mut ::test::rng()).map(|rng| rng.next_u32()).take(256).collect::>(); - let mut r: IsaacRng = SeedableRng::from_seed(&s[..]); - let string1: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - - r.reseed(&s[..]); - - let string2: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - assert_eq!(string1, string2); - } - #[test] fn test_rng_32_true_values() { let seed: &[_] = &[1, 23, 456, 7890, 12345]; diff --git a/src/prng/isaac64.rs b/src/prng/isaac64.rs index d7f6168392f..3a538e1f01c 100644 --- a/src/prng/isaac64.rs +++ b/src/prng/isaac64.rs @@ -292,22 +292,6 @@ impl SeedFromRng for Isaac64Rng { } impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng { - fn reseed(&mut self, seed: &'a [u64]) { - // make the seed into [seed[0], seed[1], ..., seed[seed.len() - // - 1], 0, 0, ...], to fill rng.rsl. - let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u64)); - - for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) { - *rsl_elem = w(seed_elem); - } - self.cnt = 0; - self.a = w(0); - self.b = w(0); - self.c = w(0); - - self.init(true); - } - /// Create an ISAAC random number generator with a seed. This can /// be any length, although the maximum number of elements used is /// 256 and any more will be silently ignored. A generator @@ -315,7 +299,15 @@ impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng { /// of values as all other generators constructed with that seed. fn from_seed(seed: &'a [u64]) -> Isaac64Rng { let mut rng = EMPTY_64; - rng.reseed(seed); + + // make the seed into [seed[0], seed[1], ..., seed[seed.len() + // - 1], 0, 0, ...], to fill rng.rsl. + let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u64)); + + for (rsl_elem, seed_elem) in rng.rsl.iter_mut().zip(seed_iter) { + *rsl_elem = w(seed_elem); + } + rng.init(true); rng } } @@ -350,18 +342,6 @@ mod test { iter(&mut rb).map(|rng| ascii_word_char(rng)).take(100))); } - #[test] - fn test_rng_64_reseed() { - let s = iter(&mut ::test::rng()).map(|rng| rng.next_u64()).take(256).collect::>(); - let mut r: Isaac64Rng = SeedableRng::from_seed(&s[..]); - let string1: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - - r.reseed(&s[..]); - - let string2: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - assert_eq!(string1, string2); - } - #[test] fn test_rng_64_true_values() { let seed: &[_] = &[1, 23, 456, 7890, 12345]; diff --git a/src/prng/xorshift.rs b/src/prng/xorshift.rs index 1e2d6cca252..6181ad646fa 100644 --- a/src/prng/xorshift.rs +++ b/src/prng/xorshift.rs @@ -90,17 +90,6 @@ impl Rng for XorShiftRng { } impl SeedableRng<[u32; 4]> for XorShiftRng { - /// Reseed an XorShiftRng. This will panic if `seed` is entirely 0. - fn reseed(&mut self, seed: [u32; 4]) { - assert!(!seed.iter().all(|&x| x == 0), - "XorShiftRng.reseed called with an all zero seed."); - - self.x = w(seed[0]); - self.y = w(seed[1]); - self.z = w(seed[2]); - self.w = w(seed[3]); - } - /// Create a new XorShiftRng. This will panic if `seed` is entirely 0. fn from_seed(seed: [u32; 4]) -> XorShiftRng { assert!(!seed.iter().all(|&x| x == 0), diff --git a/src/reseeding.rs b/src/reseeding.rs index 2d38088024b..b01bab7067c 100644 --- a/src/reseeding.rs +++ b/src/reseeding.rs @@ -90,12 +90,6 @@ impl> Rng for ReseedingRng { impl, Rsdr: Reseeder> SeedableRng<(Rsdr, S)> for ReseedingRng { - fn reseed(&mut self, (rsdr, seed): (Rsdr, S)) { - self.rng.reseed(seed); - self.reseeder = rsdr; - self.bytes_generated = 0; - } - /// Create a new `ReseedingRng` from the given reseeder and /// seed. This uses a default value for `generation_threshold`. fn from_seed((rsdr, seed): (Rsdr, S)) -> ReseedingRng { @@ -167,17 +161,6 @@ mod test { iter(&mut rb).map(|rng| ascii_word_char(rng)).take(100))); } - #[test] - fn test_rng_reseed() { - let mut r: MyRng = SeedableRng::from_seed((ReseedMock, 3)); - let string1: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - - r.reseed((ReseedMock, 3)); - - let string2: String = iter(&mut r).map(|rng| ascii_word_char(rng)).take(100).collect(); - assert_eq!(string1, string2); - } - const FILL_BYTES_V_LEN: usize = 13579; #[test] fn test_rng_try_fill() {