@@ -52,6 +52,32 @@ impl<'cs> KangarooTwelveCore<'cs> {
52
52
chain_length : 0usize ,
53
53
}
54
54
}
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
+ }
55
81
}
56
82
57
83
impl HashMarker for KangarooTwelveCore < ' _ > { }
@@ -68,27 +94,12 @@ impl UpdateCore for KangarooTwelveCore<'_> {
68
94
#[ inline]
69
95
fn update_blocks ( & mut self , blocks : & [ Block < Self > ] ) {
70
96
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 ( ) ;
87
99
}
88
100
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 ;
92
103
}
93
104
}
94
105
}
@@ -107,24 +118,27 @@ impl ExtendableOutputCore for KangarooTwelveCore<'_> {
107
118
|block| self . update_blocks ( block) ,
108
119
) ;
109
120
121
+ if self . bufpos == CHUNK_SIZE && buffer. get_pos ( ) != 0 {
122
+ self . process_chunk ( ) ;
123
+ }
124
+
110
125
// Read leftover data from buffer
111
126
self . buffer [ self . bufpos ..( self . bufpos + buffer. get_pos ( ) ) ]
112
127
. copy_from_slice ( buffer. get_data ( ) ) ;
113
128
self . bufpos += buffer. get_pos ( ) ;
114
129
115
130
// Calculate final node
116
131
if self . chain_length == 0 {
117
- // Input didnot exceed a single chaining value
132
+ // Input did not exceed a single chaining value
118
133
let tshk = TurboShake128 :: from_core ( <TurboShake128Core >:: new ( 0x07 ) )
119
134
. chain ( & self . buffer [ ..self . bufpos ] )
120
135
. finalize_xof_reset ( ) ;
121
136
return KangarooTwelveReaderCore { tshk } ;
122
137
}
138
+
123
139
// 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
+
128
142
// Pad final node calculation
129
143
self . final_tshk
130
144
. update ( length_encode ( self . chain_length , & mut lenbuf) ) ;
0 commit comments