1- use core:: fmt:: Debug ;
21use digest:: { CustomizedInit , ExtendableOutputReset } ;
32
4- pub ( crate ) fn cshake_reset_test < D , F > ( input : & [ u8 ] , output : & [ u8 ] , new : F ) -> Option < & ' static str >
3+ #[ derive( Debug , Clone , Copy ) ]
4+ pub struct TestVector {
5+ pub customization : & ' static [ u8 ] ,
6+ pub input : & ' static [ u8 ] ,
7+ pub output : & ' static [ u8 ] ,
8+ }
9+
10+ pub ( crate ) fn cshake_reset_test < D > (
11+ & TestVector {
12+ customization,
13+ input,
14+ output,
15+ } : & TestVector ,
16+ ) -> Result < ( ) , & ' static str >
517where
6- D : ExtendableOutputReset + Debug + Clone ,
7- F : Fn ( ) -> D ,
18+ D : CustomizedInit + ExtendableOutputReset + Clone ,
819{
9- let mut hasher = new ( ) ;
20+ let mut hasher = D :: new_customized ( customization ) ;
1021 let mut buf = [ 0u8 ; 1024 ] ;
1122 let buf = & mut buf[ ..output. len ( ) ] ;
1223 // Test that it works when accepting the message all at once
1324 hasher. update ( input) ;
1425 let mut hasher2 = hasher. clone ( ) ;
1526 hasher. finalize_xof_into ( buf) ;
1627 if buf != output {
17- return Some ( "whole message" ) ;
28+ return Err ( "whole message" ) ;
1829 }
1930 buf. iter_mut ( ) . for_each ( |b| * b = 0 ) ;
2031
@@ -23,67 +34,57 @@ where
2334 hasher2. update ( input) ;
2435 hasher2. finalize_xof_reset_into ( buf) ;
2536 if buf != output {
26- return Some ( "whole message after reset" ) ;
37+ return Err ( "whole message after reset" ) ;
2738 }
2839 buf. iter_mut ( ) . for_each ( |b| * b = 0 ) ;
2940
3041 // Test that it works when accepting the message in chunks
3142 for n in 1 ..core:: cmp:: min ( 17 , input. len ( ) ) {
32- let mut hasher = new ( ) ;
43+ let mut hasher = D :: new_customized ( customization ) ;
3344 for chunk in input. chunks ( n) {
3445 hasher. update ( chunk) ;
3546 hasher2. update ( chunk) ;
3647 }
3748 hasher. finalize_xof_into ( buf) ;
3849 if buf != output {
39- return Some ( "message in chunks" ) ;
50+ return Err ( "message in chunks" ) ;
4051 }
4152 buf. iter_mut ( ) . for_each ( |b| * b = 0 ) ;
4253
4354 hasher2. finalize_xof_reset_into ( buf) ;
4455 if buf != output {
45- return Some ( "message in chunks" ) ;
56+ return Err ( "message in chunks" ) ;
4657 }
4758 buf. iter_mut ( ) . for_each ( |b| * b = 0 ) ;
4859 }
4960
50- None
61+ Ok ( ( ) )
5162}
5263
5364macro_rules! new_cshake_test {
54- ( $name: ident, $test_name: expr, $hasher: ty, $test_func : ident $( , ) ?) => {
65+ ( $name: ident, $test_name: expr, $hasher: ty $( , ) ?) => {
5566 #[ test]
5667 fn $name( ) {
57- use digest:: dev:: blobby:: Blob3Iterator ;
58- let data = include_bytes!( concat!( "data/" , $test_name, ".blb" ) ) ;
68+ digest:: dev:: blobby:: parse_into_structs!(
69+ include_bytes!( concat!( "data/" , $test_name, ".blb" ) ) ;
70+ static TEST_VECTORS : & [ TestVector { customization, input, output } ] ;
71+ ) ;
5972
60- for ( i, row) in Blob3Iterator :: new( data) . unwrap( ) . enumerate( ) {
61- let [ customization, input, output] = row. unwrap( ) ;
62- if let Some ( desc) =
63- $test_func( input, output, || <$hasher>:: new_customized( customization) )
73+ for ( i, tv) in TEST_VECTORS . iter( ) . enumerate( ) {
74+ if let Err ( reason) =
75+ cshake_reset_test:: <$hasher>( tv)
6476 {
6577 panic!(
6678 "\n \
67- Failed test №{}: {}\n \
68- input:\t {:?}\n \
69- output:\t {:?}\n ",
70- i, desc, input, output,
79+ Failed test #{i}\n \
80+ reason:\t {reason}
81+ test vector:\t {tv:?}\n "
7182 ) ;
7283 }
7384 }
7485 }
7586 } ;
7687}
7788
78- new_cshake_test ! (
79- cshake128_reset,
80- "cshake128" ,
81- sha3:: CShake128 ,
82- cshake_reset_test
83- ) ;
84- new_cshake_test ! (
85- cshake256_reset,
86- "cshake256" ,
87- sha3:: CShake256 ,
88- cshake_reset_test
89- ) ;
89+ new_cshake_test ! ( cshake128_reset, "cshake128" , sha3:: CShake128 ) ;
90+ new_cshake_test ! ( cshake256_reset, "cshake256" , sha3:: CShake256 ) ;
0 commit comments