Skip to content

Commit

Permalink
aligned and tested set_word_pos. removed the index checks in the test…
Browse files Browse the repository at this point in the history
… since they were mainly for debugging
  • Loading branch information
nstilt1 committed Nov 19, 2023
1 parent cec3726 commit 5603c3c
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions chacha20/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl From<[u8; 5]> for WordPosInput {

impl From<u64> for WordPosInput {
fn from(value: u64) -> Self {
let shifted = (value >> 6).to_le_bytes();
let shifted = (value >> 4).to_le_bytes();
let original = value.to_le_bytes();
let mut result = [0u8; 5];
result[4] = original[0];
Expand Down Expand Up @@ -444,7 +444,7 @@ macro_rules! impl_chacha_rng {
{
*n = u32::from_le_bytes(chunk.try_into().unwrap());
}
offset += 16;
offset += BLOCK_WORDS as usize;
}

self.counter = self.counter.wrapping_add(1);
Expand Down Expand Up @@ -503,11 +503,11 @@ macro_rules! impl_chacha_rng {
// when not using `set_word_pos`, the block_pos is always a multiple of 4.
// This change follows those conventions, as well as maintaining the 6-bit
// index
self.core.block.set_block_pos(
u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap()) << 2,
);
self.core
.block
.set_block_pos(u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap()));
// generate will increase block_pos by 4
self.generate_and_set((word_offset.0[4] & 0x3F) as usize);
self.generate_and_set((word_offset.0[4] & 0x0F) as usize);
}

/// Set the stream number. The lower 96 bits are used and the rest are
Expand Down Expand Up @@ -762,20 +762,17 @@ mod tests {
assert_eq!(rng.next_u32(), u32_array[0]);

rng.set_word_pos(63);
assert_eq!(rng.core.block.get_block_pos(), 7);
assert_eq!(rng.get_word_pos(), 63);
assert_eq!(rng.index, 63);

assert_eq!(rng.next_u32(), u32_array[63]);
assert_eq!(rng.index, 64);
assert_eq!(rng.core.block.get_block_pos(), 4);
assert_eq!(rng.core.block.get_block_pos(), 7);
assert_eq!(rng.get_word_pos(), 64);

assert_eq!(rng.next_u32(), u32_array[64]);
assert_eq!(rng.index, 1);
assert_eq!(rng.get_word_pos(), 65);

assert_eq!(rng.next_u32(), u32_array[65]);
assert_eq!(rng.index, 2);
assert_eq!(rng.get_word_pos(), 66);

let test_word_pos = 1234567;
Expand Down

0 comments on commit 5603c3c

Please sign in to comment.