migrate ChaCha
diff --git a/rand_chacha/src/chacha.rs b/rand_chacha/src/chacha.rs index 17bcc55..fbcf8bd 100644 --- a/rand_chacha/src/chacha.rs +++ b/rand_chacha/src/chacha.rs
@@ -24,46 +24,39 @@ // number of 32-bit words per ChaCha block (fixed by algorithm definition) const BLOCK_WORDS: u8 = 16; -pub struct Array64<T>([T; 64]); -impl<T> Default for Array64<T> -where T: Default -{ - #[rustfmt::skip] +/// Type representing result of the ChaCha core iteration +#[derive(Eq, PartialEq)] +#[repr(align(4))] +pub struct Results([u8; 256]); + +impl Default for Results { fn default() -> Self { - Self([ - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), - ]) + Self([0; 256]) } } -impl<T> AsRef<[T]> for Array64<T> { - fn as_ref(&self) -> &[T] { +impl AsRef<[u8]> for Results { + fn as_ref(&self) -> &[u8] { &self.0 } } -impl<T> AsMut<[T]> for Array64<T> { - fn as_mut(&mut self) -> &mut [T] { + +impl AsMut<[u8]> for Results { + fn as_mut(&mut self) -> &mut [u8] { &mut self.0 } } -impl<T> Clone for Array64<T> -where T: Copy + Default -{ + +impl Clone for Results { fn clone(&self) -> Self { let mut new = Self::default(); new.0.copy_from_slice(&self.0); new } } -impl<T> fmt::Debug for Array64<T> { + +impl fmt::Debug for Results { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Array64 {{}}") + write!(f, "Results {{}}") } } @@ -83,14 +76,11 @@ } impl BlockRngCore for $ChaChaXCore { - type Item = u32; - type Results = Array64<u32>; + type Results = Results; #[inline] fn generate(&mut self, r: &mut Self::Results) { // Fill slice of words by writing to equivalent slice of bytes, then fixing endianness. - self.state.refill4($rounds, unsafe { - &mut *(&mut *r as *mut Array64<u32> as *mut [u8; 256]) - }); + self.state.refill4($rounds, &mut r.0); for x in r.as_mut() { *x = x.to_le(); } @@ -163,6 +153,10 @@ impl RngCore for $ChaChaXRng { #[inline] + fn next_bool(&mut self) -> bool { + self.rng.next_bool() + } + #[inline] fn next_u32(&mut self) -> u32 { self.rng.next_u32() } @@ -180,6 +174,7 @@ } } + /* impl $ChaChaXRng { // The buffer is a 4-block window, i.e. it is always at a block-aligned position in the // stream but if the stream has been seeked it may not be self-aligned. @@ -245,6 +240,7 @@ } } } + */ impl CryptoRng for $ChaChaXRng {} @@ -258,8 +254,7 @@ impl PartialEq<$ChaChaXRng> for $ChaChaXRng { fn eq(&self, rhs: &$ChaChaXRng) -> bool { - self.rng.core.state.stream64_eq(&rhs.rng.core.state) - && self.get_word_pos() == rhs.get_word_pos() + self.rng.eq(&rhs.rng) } } impl Eq for $ChaChaXRng {} @@ -344,7 +339,7 @@ ]; assert_eq!(results, expected); } - +/* #[test] fn test_chacha_true_values_c() { // Test vector 4 from @@ -506,4 +501,5 @@ rng.set_word_pos(0); assert_eq!(rng.get_word_pos(), 0); } +*/ }