1- use bash_prg_hash:: { BashPrgHash1282 , BashPrgHash1921 , BashPrgHash2562 } ;
2- use digest:: ExtendableOutput ;
3- use digest:: dev:: TestVector ;
1+ use bash_prg_hash:: {
2+ BashPrgHash1281 , BashPrgHash1282 , BashPrgHash1921 , BashPrgHash1922 , BashPrgHash2561 ,
3+ BashPrgHash2562 ,
4+ } ;
5+ use digest:: { ExtendableOutput , TryCustomizedInit } ;
46use hex_literal:: hex;
7+ use std:: fmt:: Debug ;
8+
9+ #[ derive( Debug , Clone , Copy ) ]
10+ pub struct TestVector {
11+ pub customization : & ' static [ u8 ] ,
12+ pub input : & ' static [ u8 ] ,
13+ pub output : & ' static [ u8 ] ,
14+ }
515
6- fn xof_test < D > ( & TestVector { input, output } : & TestVector ) -> Result < ( ) , & ' static str >
16+ pub ( crate ) fn bash_prg_hash_test < D > (
17+ & TestVector {
18+ customization,
19+ input,
20+ output,
21+ } : & TestVector ,
22+ ) -> Result < ( ) , & ' static str >
723where
8- D : Default + ExtendableOutput ,
24+ D : TryCustomizedInit + ExtendableOutput + Clone ,
25+ <D as TryCustomizedInit >:: Error : Debug ,
926{
10- let mut hasher = D :: default ( ) ;
27+ let mut hasher = D :: try_new_customized ( customization ) . unwrap ( ) ;
1128 let mut buf = [ 0u8 ; 1024 ] ;
1229 let buf = & mut buf[ ..output. len ( ) ] ;
1330 // Test that it works when accepting the message all at once
2037
2138 // Test that it works when accepting the message in chunks
2239 for n in 1 ..core:: cmp:: min ( 17 , input. len ( ) ) {
23- let mut hasher = D :: default ( ) ;
40+ let mut hasher = D :: try_new_customized ( customization ) . unwrap ( ) ;
2441 for chunk in input. chunks ( n) {
2542 hasher. update ( chunk) ;
2643 }
@@ -34,19 +51,52 @@ where
3451 Ok ( ( ) )
3552}
3653
37- // Test vectors from STB 34.101.77-2020 (Appendix A, Table A.5)
38- digest:: new_test!( bashprg1282, BashPrgHash1282 , xof_test) ;
39- digest:: new_test!( bashprg1921, BashPrgHash1921 , xof_test) ;
40- // Not in STB 34.101.77-2020, but included for completeness
41- digest:: new_test!( bashprg2562, BashPrgHash2562 , xof_test) ;
54+ macro_rules! new_bash_prg_hash_test {
55+ ( $name: ident, $hasher: ty $( , ) ?) => {
56+ #[ test]
57+ fn $name( ) {
58+ digest:: dev:: blobby:: parse_into_structs!(
59+ include_bytes!( concat!( "data/" , stringify!( $name) , ".blb" ) ) ;
60+ static TEST_VECTORS : & [ TestVector { customization, input, output } ] ;
61+ ) ;
62+
63+ for ( i, tv) in TEST_VECTORS . iter( ) . enumerate( ) {
64+ if let Err ( reason) = bash_prg_hash_test:: <$hasher>( tv) {
65+ panic!(
66+ "\n \
67+ Failed test #{i}\n \
68+ reason:\t {reason}
69+ test vector:\t {tv:?}\n "
70+ ) ;
71+ }
72+ }
73+ }
74+ } ;
75+ }
76+
77+ // Test vectors generated with bee2 library: https://github.com/agievich/bee2
78+ // Messages is the first N bytes of `beltH()` (belt S-box constant) for N = 0, 127, 128, 143, 144, 150
79+ // Plus 3 tests with customization:
80+ // - 06075316 (4 bytes) + "Fifty four byte..." message
81+ // - 0102030405060708 (8 bytes) + "Fifty four byte..." message
82+ // - FFEEDDCC (4 bytes) + beltH()[0..100]
83+ new_bash_prg_hash_test ! ( bashprg_l128_d1, BashPrgHash1281 ) ;
84+ new_bash_prg_hash_test ! ( bashprg_l128_d2, BashPrgHash1282 ) ;
85+ new_bash_prg_hash_test ! ( bashprg_l192_d1, BashPrgHash1921 ) ;
86+ new_bash_prg_hash_test ! ( bashprg_l192_d2, BashPrgHash1922 ) ;
87+ new_bash_prg_hash_test ! ( bashprg_l256_d1, BashPrgHash2561 ) ;
88+ new_bash_prg_hash_test ! ( bashprg_l256_d2, BashPrgHash2562 ) ;
4289
4390macro_rules! test_bash_prg_rand {
4491 ( $name: ident, $hasher: ty, $expected: expr) => {
4592 #[ test]
4693 fn $name( ) {
94+ use digest:: CollisionResistance ;
95+ use digest:: typenum:: Unsigned ;
4796 let mut h = <$hasher>:: default ( ) ;
4897 digest:: dev:: feed_rand_16mib( & mut h) ;
49- let mut output = [ 0u8 ; 64 ] ;
98+ let mut output = [ 0u8 ;
99+ <<$hasher as CollisionResistance >:: CollisionResistance as Unsigned >:: USIZE * 2 ] ;
50100 h. finalize_xof_into( & mut output) ;
51101 assert_eq!( & output[ ..] , $expected) ;
52102 }
@@ -56,18 +106,15 @@ macro_rules! test_bash_prg_rand {
56106test_bash_prg_rand ! (
57107 bashprg1282_rand,
58108 BashPrgHash1282 ,
59- hex!(
60- "BF15805CDEAE220A9DD50C325A4A0BDF326C6ED853CFA89592A9E2BEB4D0585C"
61- "891AF66C1CA514390311FDFB51D467FC11439AE4907863A5C3861CDCF7F360EC"
62- )
109+ hex!( "BF15805CDEAE220A9DD50C325A4A0BDF326C6ED853CFA89592A9E2BEB4D0585C" )
63110) ;
64111
65112test_bash_prg_rand ! (
66113 bashprg1921_rand,
67114 BashPrgHash1921 ,
68115 hex!(
69116 "82176D6DAF4F631E251CA41A7688FEB643B954383186C7902AB09D80EB5AB17C"
70- "BA286D16912EBBACEC3D8143966107F6DFB5F4AC4F88B64F20AB49CEAD817E45 "
117+ "BA286D16912EBBACEC3D8143966107F6 "
71118 )
72119) ;
73120
0 commit comments