| 1 |
use crate::{ExtendableOutputReset, dev::TestVector}; |
| 2 |
use core::fmt::Debug; |
| 3 |
|
| 4 |
/// Resettable XOF test |
| 5 |
pub fn xof_reset_test<D: ExtendableOutputReset + Default + Debug + Clone>( |
| 6 |
&TestVector { input, output }: &TestVector, |
| 7 |
) -> Result<(), &'static str> { |
| 8 |
let mut hasher = D::default(); |
| 9 |
let mut buf = [0u8; 1024]; |
| 10 |
let buf = &mut buf[..output.len()]; |
| 11 |
// Test that it works when accepting the message all at once |
| 12 |
hasher.update(input); |
| 13 |
let mut hasher2 = hasher.clone(); |
| 14 |
hasher.finalize_xof_into(buf); |
| 15 |
if buf != output { |
| 16 |
return Err("whole message"); |
| 17 |
} |
| 18 |
buf.iter_mut().for_each(|b| *b = 0); |
| 19 |
|
| 20 |
// Test if reset works correctly |
| 21 |
hasher2.reset(); |
| 22 |
hasher2.update(input); |
| 23 |
hasher2.finalize_xof_reset_into(buf); |
| 24 |
if buf != output { |
| 25 |
return Err("whole message after reset"); |
| 26 |
} |
| 27 |
buf.iter_mut().for_each(|b| *b = 0); |
| 28 |
|
| 29 |
// Test that it works when accepting the message in chunks |
| 30 |
for n in 1..core::cmp::min(17, input.len()) { |
| 31 |
let mut hasher = D::default(); |
| 32 |
for chunk in input.chunks(n) { |
| 33 |
hasher.update(chunk); |
| 34 |
hasher2.update(chunk); |
| 35 |
} |
| 36 |
hasher.finalize_xof_into(buf); |
| 37 |
if buf != output { |
| 38 |
return Err("message in chunks"); |
| 39 |
} |
| 40 |
buf.iter_mut().for_each(|b| *b = 0); |
| 41 |
|
| 42 |
hasher2.finalize_xof_reset_into(buf); |
| 43 |
if buf != output { |
| 44 |
return Err("message in chunks"); |
| 45 |
} |
| 46 |
buf.iter_mut().for_each(|b| *b = 0); |
| 47 |
} |
| 48 |
|
| 49 |
Ok(()) |
| 50 |
} |