From ae365ef352eeb0a4a249b598dde7723ddf37a32d Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 10 Nov 2017 17:21:53 +0000 Subject: [PATCH] Add true_bytes tests for ChaCha and Isaac; fix 2 bugs in fill_bytes impls --- src/prng/chacha.rs | 16 +++++++++++++++- src/prng/isaac.rs | 14 ++++++++++++++ src/prng/isaac64.rs | 18 ++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/prng/chacha.rs b/src/prng/chacha.rs index db1d127bb9c..a9ae8996b09 100644 --- a/src/prng/chacha.rs +++ b/src/prng/chacha.rs @@ -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(); @@ -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]; diff --git a/src/prng/isaac.rs b/src/prng/isaac.rs index 996bc684f5b..82944144cb8 100644 --- a/src/prng/isaac.rs +++ b/src/prng/isaac.rs @@ -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 diff --git a/src/prng/isaac64.rs b/src/prng/isaac64.rs index 91c0186f3c7..1d84d555f72 100644 --- a/src/prng/isaac64.rs +++ b/src/prng/isaac64.rs @@ -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> { @@ -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