Skip to content

Commit be4f186

Browse files
committed
bash-prg-hash: remove Reset trait, add xof_test (copied from xof_reset_test from digest::dev)
1 parent 987f092 commit be4f186

2 files changed

Lines changed: 55 additions & 30 deletions

File tree

bash-prg-hash/src/lib.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ pub use variants::*;
1515

1616
use bash_f::{STATE_WORDS, bash_f};
1717
use core::fmt;
18-
use digest::{
19-
ExtendableOutput, ExtendableOutputReset, Reset, TryCustomizedInit, Update, XofReader,
20-
};
18+
use digest::{ExtendableOutput, TryCustomizedInit, Update, XofReader};
2119
use sponge_cursor::SpongeCursor;
2220

2321
/// Data type codes from Table 3 of STB 34.101.77-2020
@@ -118,28 +116,6 @@ impl<const RATE: usize, const CAPACITY: usize> ExtendableOutput for BashPrgHash<
118116
}
119117
}
120118

121-
impl<const RATE: usize, const CAPACITY: usize> ExtendableOutputReset
122-
for BashPrgHash<RATE, CAPACITY>
123-
{
124-
#[inline]
125-
fn finalize_xof_reset(&mut self) -> Self::Reader {
126-
let mut hasher_clone = self.clone();
127-
hasher_clone.commit(OUT);
128-
self.reset();
129-
BashPrgHashReader {
130-
state: hasher_clone.state,
131-
cursor: hasher_clone.cursor.clone(),
132-
}
133-
}
134-
}
135-
136-
impl<const RATE: usize, const CAPACITY: usize> Reset for BashPrgHash<RATE, CAPACITY> {
137-
#[inline]
138-
fn reset(&mut self) {
139-
*self = Self::default();
140-
}
141-
}
142-
143119
impl<const RATE: usize, const CAPACITY: usize> fmt::Debug for BashPrgHash<RATE, CAPACITY> {
144120
#[inline]
145121
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

bash-prg-hash/tests/mod.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
11
use bash_prg_hash::{BashPrgHash1282, BashPrgHash1921, BashPrgHash2562};
2-
use digest::ExtendableOutput;
3-
use digest::dev::xof_reset_test;
2+
use digest::dev::TestVector;
3+
use digest::{ExtendableOutput, XofReader};
44
use hex_literal::hex;
5+
use std::fmt::Debug;
6+
7+
pub fn xof_test<D: ExtendableOutput + Default + Debug + Clone>(
8+
&TestVector { input, output }: &TestVector,
9+
) -> Result<(), &'static str> {
10+
let mut hasher = D::default();
11+
let mut buf = [0u8; 1024];
12+
let buf = &mut buf[..output.len()];
13+
14+
// Test that it works when accepting the message all at once
15+
hasher.update(input);
16+
hasher.finalize_xof().read(buf);
17+
if buf != output {
18+
return Err("whole message");
19+
}
20+
buf.iter_mut().for_each(|b| *b = 0);
21+
22+
// Test with fresh hasher
23+
let mut hasher = D::default();
24+
hasher.update(input);
25+
hasher.finalize_xof().read(buf);
26+
if buf != output {
27+
return Err("whole message after reset");
28+
}
29+
buf.iter_mut().for_each(|b| *b = 0);
30+
31+
// Test that it works when accepting the message in chunks
32+
for n in 1..core::cmp::min(17, input.len()) {
33+
let mut hasher = D::default();
34+
let mut hasher2 = D::default();
35+
for chunk in input.chunks(n) {
36+
hasher.update(chunk);
37+
hasher2.update(chunk);
38+
}
39+
hasher.finalize_xof().read(buf);
40+
if buf != output {
41+
return Err("message in chunks");
42+
}
43+
buf.iter_mut().for_each(|b| *b = 0);
44+
45+
hasher2.finalize_xof().read(buf);
46+
if buf != output {
47+
return Err("message in chunks");
48+
}
49+
buf.iter_mut().for_each(|b| *b = 0);
50+
}
51+
52+
Ok(())
53+
}
554

655
// Test vectors from STB 34.101.77-2020 (Appendix A, Table A.5)
7-
digest::new_test!(bashprg1282, BashPrgHash1282, xof_reset_test);
8-
digest::new_test!(bashprg1921, BashPrgHash1921, xof_reset_test);
56+
digest::new_test!(bashprg1282, BashPrgHash1282, xof_test);
57+
digest::new_test!(bashprg1921, BashPrgHash1921, xof_test);
958
// Not in STB 34.101.77-2020, but included for completeness
10-
digest::new_test!(bashprg2562, BashPrgHash2562, xof_reset_test);
59+
digest::new_test!(bashprg2562, BashPrgHash2562, xof_test);
1160

1261
macro_rules! test_bash_prg_rand {
1362
($name:ident, $hasher:ty, $expected:expr) => {

0 commit comments

Comments
 (0)