@@ -52,6 +52,32 @@ impl<'cs> KangarooTwelveCore<'cs> {
5252 chain_length : 0usize ,
5353 }
5454 }
55+
56+ fn process_chunk ( & mut self ) {
57+ debug_assert ! ( self . bufpos == CHUNK_SIZE ) ;
58+ if self . chain_length == 0 {
59+ self . final_tshk . update ( & self . buffer ) ;
60+ } else {
61+ self . process_chaining_chunk ( ) ;
62+ }
63+
64+ self . chain_length += 1 ;
65+ self . buffer = [ 0u8 ; CHUNK_SIZE ] ;
66+ self . bufpos = 0 ;
67+ }
68+
69+ fn process_chaining_chunk ( & mut self ) {
70+ debug_assert ! ( self . bufpos != 0 ) ;
71+ if self . chain_length == 1 {
72+ self . final_tshk
73+ . update ( & [ 0x03 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] ) ;
74+ }
75+
76+ let mut result = [ 0u8 ; CHAINING_VALUE_SIZE ] ;
77+ self . chain_tshk . update ( & self . buffer [ ..self . bufpos ] ) ;
78+ self . chain_tshk . finalize_xof_reset_into ( & mut result) ;
79+ self . final_tshk . update ( & result) ;
80+ }
5581}
5682
5783impl HashMarker for KangarooTwelveCore < ' _ > { }
@@ -68,27 +94,12 @@ impl UpdateCore for KangarooTwelveCore<'_> {
6894 #[ inline]
6995 fn update_blocks ( & mut self , blocks : & [ Block < Self > ] ) {
7096 for block in blocks {
71- self . buffer [ self . bufpos ..self . bufpos + 128 ] . clone_from_slice ( block) ;
72- self . bufpos += 128 ;
73-
74- if self . bufpos != CHUNK_SIZE {
75- continue ;
76- }
77-
78- if self . chain_length == 0 {
79- self . final_tshk . update ( & self . buffer ) ;
80- self . final_tshk
81- . update ( & [ 0x03 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] ) ;
82- } else {
83- let mut result = [ 0u8 ; CHAINING_VALUE_SIZE ] ;
84- self . chain_tshk . update ( & self . buffer ) ;
85- self . chain_tshk . finalize_xof_reset_into ( & mut result) ;
86- self . final_tshk . update ( & result) ;
97+ if self . bufpos == CHUNK_SIZE {
98+ self . process_chunk ( ) ;
8799 }
88100
89- self . chain_length += 1 ;
90- self . buffer = [ 0u8 ; CHUNK_SIZE ] ;
91- self . bufpos = 0 ;
101+ self . buffer [ self . bufpos ..self . bufpos + 128 ] . clone_from_slice ( block) ;
102+ self . bufpos += 128 ;
92103 }
93104 }
94105}
@@ -107,24 +118,27 @@ impl ExtendableOutputCore for KangarooTwelveCore<'_> {
107118 |block| self . update_blocks ( block) ,
108119 ) ;
109120
121+ if self . bufpos == CHUNK_SIZE && buffer. get_pos ( ) != 0 {
122+ self . process_chunk ( ) ;
123+ }
124+
110125 // Read leftover data from buffer
111126 self . buffer [ self . bufpos ..( self . bufpos + buffer. get_pos ( ) ) ]
112127 . copy_from_slice ( buffer. get_data ( ) ) ;
113128 self . bufpos += buffer. get_pos ( ) ;
114129
115130 // Calculate final node
116131 if self . chain_length == 0 {
117- // Input didnot exceed a single chaining value
132+ // Input did not exceed a single chaining value
118133 let tshk = TurboShake128 :: from_core ( <TurboShake128Core >:: new ( 0x07 ) )
119134 . chain ( & self . buffer [ ..self . bufpos ] )
120135 . finalize_xof_reset ( ) ;
121136 return KangarooTwelveReaderCore { tshk } ;
122137 }
138+
123139 // Calculate last chaining value
124- let mut result = [ 0u8 ; CHAINING_VALUE_SIZE ] ;
125- self . chain_tshk . update ( & self . buffer [ ..self . bufpos ] ) ;
126- self . chain_tshk . finalize_xof_reset_into ( & mut result) ;
127- self . final_tshk . update ( & result) ;
140+ self . process_chaining_chunk ( ) ;
141+
128142 // Pad final node calculation
129143 self . final_tshk
130144 . update ( length_encode ( self . chain_length , & mut lenbuf) ) ;
0 commit comments