Skip to content

Commit 5d54aca

Browse files
committed
rewrote soft.rs counter logic
1 parent a49df62 commit 5d54aca

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

chacha20/src/backends/soft.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<'_, R, V> {
2828
#[inline(always)]
2929
fn gen_ks_block(&mut self, block: &mut Block) {
3030
let res = run_rounds::<R>(&self.0.state);
31-
let no_carry = self.0.state[12].checked_add(1);
32-
if let Some(v) = no_carry {
33-
self.0.state[12] = v;
34-
} else {
35-
self.0.state[12] = 0;
36-
if size_of::<V::Counter>() == 8 {
37-
self.0.state[13] = self.0.state[13].wrapping_add(1);
38-
}
31+
let mut ctr = u64::from(self.0.state[13]) << 32 | u64::from(self.0.state[12]);
32+
ctr = ctr.wrapping_add(1);
33+
self.0.state[12] = ctr as u32;
34+
match size_of::<V::Counter>() == 8 {
35+
true => self.0.state[13] = (ctr >> 32) as u32,
36+
false => {}
3937
}
4038

4139
for (chunk, val) in block.chunks_exact_mut(4).zip(res.iter()) {

0 commit comments

Comments
 (0)