Skip to content

Commit 691be6b

Browse files
author
bloom
committed
some cleanup
1 parent a747382 commit 691be6b

File tree

7 files changed

+37
-118
lines changed

7 files changed

+37
-118
lines changed

Cargo.lock

Lines changed: 3 additions & 104 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chacha12-blake3/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ zeroize = ["dep:zeroize", "blake3/zeroize"]
3434

3535
[dev-dependencies]
3636
hex = "0.4"
37-
rand = "0.9"

chacha12/src/chacha_avx2.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ pub fn chacha_avx2<const ROUNDS: usize>(
8585
counter = counter.wrapping_add((input_blocks.len() as u64).div_ceil(64));
8686
}
8787

88-
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
89-
let last_keystream_block_offset = last_keystream_block_index * 64;
90-
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
88+
if input.len() % 64 != 0 {
89+
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
90+
let last_keystream_block_offset = last_keystream_block_index * 64;
91+
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
92+
}
9193

9294
return counter;
9395
}

chacha12/src/chacha_avx512.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ pub fn chacha_avx512<const ROUNDS: usize>(
8686
counter = counter.wrapping_add((input_blocks.len() as u64).div_ceil(64));
8787
}
8888

89-
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
90-
let last_keystream_block_offset = last_keystream_block_index * 64;
91-
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
89+
if input.len() % 64 != 0 {
90+
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
91+
let last_keystream_block_offset = last_keystream_block_index * 64;
92+
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
93+
}
9294

9395
return counter;
9496
}

chacha12/src/chacha_neon.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ pub fn chacha_neon<const ROUNDS: usize>(
7575
counter = counter.wrapping_add((input_blocks.len() as u64).div_ceil(64));
7676
}
7777

78-
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
79-
let last_keystream_block_offset = last_keystream_block_index * 64;
80-
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
78+
if input.len() % 64 != 0 {
79+
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
80+
let last_keystream_block_offset = last_keystream_block_index * 64;
81+
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
82+
}
8183

8284
return counter;
8385
}

chacha12/src/chacha_wasm_simd128.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ pub fn chacha_wasm_simd128<const ROUNDS: usize>(
6767
counter = counter.wrapping_add((input_blocks.len() as u64).div_ceil(64));
6868
}
6969

70-
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
71-
let last_keystream_block_offset = last_keystream_block_index * 64;
72-
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
70+
if input.len() % 64 != 0 {
71+
let last_keystream_block_index = ((input.len() - 1) / 64) % SIMD_LANES;
72+
let last_keystream_block_offset = last_keystream_block_index * 64;
73+
last_keystream_block.copy_from_slice(&keystream[last_keystream_block_offset..last_keystream_block_offset + 64]);
74+
}
7375

7476
return counter;
7577
}

chacha12/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ const STATE_WORDS: usize = 16;
3636
pub struct ChaCha<const ROUNDS: usize> {
3737
state: [u32; STATE_WORDS],
3838
counter: u64,
39+
/// ChaCha is a stream cipher that works with 64-byte blocks.
40+
/// It means that
41+
/// Thus calling multiple times `xor_keystream`:
42+
/// xor_keystream(plaintext[0..3]), xor_keystream(plaintext[3..50]), xor_keystream(plaintext[50..150]);
43+
/// Should be equal to calling it only once:
44+
/// xor_keystream(plaintext[0..150]);
45+
/// For that, we keep the last computed keystream block, as well as an index of where in the keystream
46+
/// we were after completing the last call.
47+
/// Then, when calling `xor_keystream` again, we first check if there is sone leftover form the last
48+
/// keystream.
49+
/// NOTE: the `last_keystream_block` is valid only if the previous call to `xor_keystream` had
50+
/// an input.len() % 64 != 0
3951
last_keystream_block: [u8; 64],
4052
last_keystream_block_index: usize,
4153
}
@@ -404,8 +416,9 @@ Expected: {}",
404416
// thus:
405417
// cipher.xor_keystream(plaintext[0..10])
406418
// cipher.xor_keystream(plaintext[10..30])
419+
// cipher.xor_keystream(plaintext[30..5])
407420
// should be equal to:
408-
// cipher.xor_keystream(plaintext[0..30])
421+
// cipher.xor_keystream(plaintext[0..35])
409422

410423
let mut cipher = ChaCha::<20>::new(&test.key, &test.nonce);
411424
cipher.xor_keystream(&mut plaintext);

0 commit comments

Comments
 (0)