Skip to content

Commit

Permalink
Remove reseed from SeedableRng
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dicker committed Sep 24, 2017
1 parent 82bd0df commit 2c84357
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 101 deletions.
6 changes: 0 additions & 6 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ pub trait SeedFromRng: Sized {
/// A random number generator that can be explicitly seeded to produce
/// the same stream of randomness multiple times.
pub trait SeedableRng<Seed>: 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
Expand Down
3 changes: 0 additions & 3 deletions rand_core/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ impl<T> SeedableRng<T> for MockAddRng<T> where
MockAddRng<T>: Rng,
T: From<u8>, // for 1.into()
{
fn reseed(&mut self, seed: T) {
self.v = w(seed);
}
fn from_seed(seed: T) -> Self {
MockAddRng::new(seed, 1.into())
}
Expand Down
31 changes: 8 additions & 23 deletions src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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::<Vec<u32>>();
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
Expand Down
38 changes: 9 additions & 29 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,22 @@ 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
/// constructed with a given seed will generate the same sequence
/// 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
}
}
Expand Down Expand Up @@ -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::<Vec<u32>>();
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];
Expand Down
38 changes: 9 additions & 29 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,22 @@ 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
/// constructed with a given seed will generate the same sequence
/// 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
}
}
Expand Down Expand Up @@ -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::<Vec<u64>>();
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];
Expand Down
11 changes: 0 additions & 11 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 2c84357

Please sign in to comment.