Skip to content

Commit

Permalink
Add true_bytes tests for ChaCha and Isaac; fix 2 bugs in fill_bytes i…
Browse files Browse the repository at this point in the history
…mpls
  • Loading branch information
dhardy committed Nov 10, 2017
1 parent 6e9c1ab commit ae365ef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Rng for ChaChaRng {
unsafe{ copy_nonoverlapping(
&self.buffer[self.index].0 as *const u32 as *const u8,
l.as_mut_ptr(),
words) };
4 * words) };
self.index += words;
}
let n = left.len();
Expand Down Expand Up @@ -351,6 +351,20 @@ mod test {
0x2c5bad8f, 0x898881dc, 0x5f1c86d9, 0xc1f8e7f4));
}

#[test]
fn test_rng_true_bytes() {
let seed : &[_] = &[0u32; 8];
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);
let mut buf = [0u8; 32];
ra.fill_bytes(&mut buf);
// Same as first values in test_isaac_true_values as bytes in LE order
assert_eq!(buf,
[118, 184, 224, 173, 160, 241, 61, 144,
64, 93, 106, 229, 83, 134, 189, 40,
189, 210, 25, 184, 160, 141, 237, 26,
168, 54, 239, 204, 139, 119, 13, 199]);
}

#[test]
fn test_rng_clone() {
let seed : &[_] = &[0u32; 8];
Expand Down
14 changes: 14 additions & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,20 @@ mod test {
141456972, 2478885421));
}

#[test]
fn test_isaac_true_bytes() {
let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng1 = IsaacRng::from_seed(seed);
let mut buf = [0u8; 32];
rng1.fill_bytes(&mut buf);
// Same as first values in test_isaac_true_values as bytes in LE order
assert_eq!(buf,
[82, 186, 128, 152, 71, 240, 20, 52,
45, 175, 180, 15, 86, 16, 99, 125,
101, 203, 81, 214, 97, 162, 134, 250,
103, 78, 203, 15, 150, 3, 210, 164]);
}

#[test]
fn test_isaac_new_uninitialized() {
// Compare the results from initializing `IsaacRng` with
Expand Down
18 changes: 16 additions & 2 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Rng for Isaac64Rng {
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
::rand_core::impls::fill_bytes_via_u32(self, dest);
::rand_core::impls::fill_bytes_via_u64(self, dest);
}

fn try_fill(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Expand Down Expand Up @@ -370,7 +370,21 @@ mod test {
596345674630742204, 9947027391921273664,
11788097613744130851, 10391409374914919106));
}


#[test]
fn test_isaac64_true_bytes() {
let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng1 = Isaac64Rng::from_seed(seed);
let mut buf = [0u8; 32];
rng1.fill_bytes(&mut buf);
// Same as first values in test_isaac64_true_values as bytes in LE order
assert_eq!(buf,
[140, 237, 103, 8, 93, 196, 151, 7,
156, 242, 26, 63, 54, 166, 135, 199,
141, 186, 192, 50, 116, 69, 205, 240,
98, 205, 127, 160, 83, 98, 49, 17]);
}

#[test]
fn test_isaac_new_uninitialized() {
// Compare the results from initializing `IsaacRng` with
Expand Down

0 comments on commit ae365ef

Please sign in to comment.