@@ -135,28 +135,39 @@ fn (mut d Digest) squeeze(mut dst []u8) int {
135135}
136136
137137@[direct_array_access; inline]
138- fn ascon_generic_hash (mut s State, msg_ []u8 , size int ) []u8 {
138+ fn ascon_generic_hash (mut s State, msg []u8 , size int ) []u8 {
139139 // Assumed state was correctly initialized
140140 // Absorbing the message
141- mut msg := msg_.clone ()
142- for msg.len > = block_size {
143- s.e0 ^= binary.little_endian_u64 (msg[0 ..block_size])
144- unsafe {
145- msg = msg[block_size..]
141+ mut pos := 0
142+ // Check if msg has non-null length, if yes, absorb it.
143+ // Otherwise, just pad it
144+ if _likely_ (msg.len > 0 ) {
145+ mut msg_len := msg.len
146+ for msg_len > = block_size {
147+ s.e0 ^= binary.little_endian_u64 (msg[pos..pos + block_size])
148+ pos + = block_size
149+ msg_len - = block_size
150+ ascon_pnr (mut s, ascon_prnd_12 )
146151 }
147- ascon_pnr (mut s, ascon_prnd_12 )
152+ // Absorb the last partial message block
153+ last_block := unsafe { msg[pos..] }
154+ s.e0 ^= u64 (0x01 ) << (8 * last_block.len) // pad(last_block.len)
155+ if last_block.len > 0 {
156+ s.e0 ^= load_bytes (last_block, last_block.len)
157+ }
158+ } else {
159+ // Otherwise, just pad it
160+ s.e0 ^= u64 (0x01 )
148161 }
149- // Absorb the last partial message block
150- s.e0 ^= load_bytes (msg, msg.len)
151- s.e0 ^= pad (msg.len)
162+ // reset pos
163+ pos = 0
152164
153165 // Squeezing phase
154166 //
155167 // The squeezing phase begins after msg is absorbed with an
156168 // permutation 𝐴𝑠𝑐𝑜𝑛-𝑝[12] to the state:
157169 ascon_pnr (mut s, ascon_prnd_12 )
158170 mut out := []u8 {len: size}
159- mut pos := 0
160171 mut clen := out.len
161172 for clen > = block_size {
162173 binary.little_endian_put_u64 (mut out[pos..pos + 8 ], s.e0 )
0 commit comments