Skip to content

Commit 1b7b83f

Browse files
committed
Let set_block_pos use type u64
1 parent e998b15 commit 1b7b83f

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

chacha20/src/rng.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -365,27 +365,18 @@ macro_rules! impl_chacha_rng {
365365
/// together.
366366
///
367367
/// The word pos will be equal to `block_pos * 16 words per block`.
368-
///
369-
/// This method takes any of the following:
370-
/// * `u64`
371-
/// * `[u32; 2]`
372-
/// * `[u8; 8]`
373-
///
374-
/// Note: the arrays should be in little endian order.
375368
#[inline]
376369
#[allow(unused)]
377-
pub fn set_block_pos<B: Into<BlockPos>>(&mut self, block_pos: B) {
370+
pub fn set_block_pos(&mut self, block_pos: u64) {
378371
self.core.reset_and_skip(0);
379-
let block_pos = block_pos.into().0;
380-
self.core.core.state[12] = block_pos[0];
381-
self.core.core.state[13] = block_pos[1]
372+
self.core.core.set_block_pos(block_pos);
382373
}
383374

384375
/// Get the block pos.
385376
#[inline]
386377
#[allow(unused)]
387378
pub fn get_block_pos(&self) -> u64 {
388-
let counter = self.core.get_block_pos();
379+
let counter = self.core.core.get_block_pos();
389380
if self.core.word_offset() != 0 {
390381
counter - BUF_BLOCKS as u64 + self.core.word_offset() as u64 / 16
391382
} else {
@@ -419,7 +410,7 @@ macro_rules! impl_chacha_rng {
419410
///
420411
/// // set state[12] to 0, state[13] to 1, state[14] to 2, state[15] to 3
421412
/// rng.set_stream([2u32, 3u32]);
422-
/// rng.set_block_pos([0u32, 1u32]);
413+
/// rng.set_block_pos(0x1_0000_0000_u64);
423414
///
424415
/// // confirm that state is set correctly
425416
/// assert_eq!(rng.get_block_pos(), 1 << 32);
@@ -599,16 +590,12 @@ pub(crate) mod tests {
599590
rng.set_stream(11111111);
600591
assert_eq!(rng.get_stream(), 11111111);
601592

602-
// test set_block_pos with u32
593+
// test set_block_pos with u64
603594
rng.set_block_pos(58392);
604595
assert_eq!(rng.get_block_pos(), 58392);
605596
// test word_pos = 16 * block_pos
606597
assert_eq!(rng.get_word_pos(), 58392 * 16);
607598

608-
// test set_block_pos with [u8; 8]
609-
rng.set_block_pos([77, 0, 0, 0, 0, 0, 0, 0]);
610-
assert_eq!(rng.get_block_pos(), 77);
611-
612599
// test set_word_pos with u64
613600
rng.set_word_pos(8888);
614601
assert_eq!(rng.get_word_pos(), 8888);
@@ -735,7 +722,7 @@ pub(crate) mod tests {
735722

736723
// Test block 2 by using `set_block_pos` and [u8; 8]
737724
let mut rng4 = ChaChaRng::from_seed(seed);
738-
rng4.set_block_pos([2, 0, 0, 0, 0, 0, 0, 0]);
725+
rng4.set_block_pos(2);
739726
results = [0u32; 16];
740727
for i in results.iter_mut() {
741728
*i = rng4.next_u32();
@@ -806,7 +793,7 @@ pub(crate) mod tests {
806793

807794
let stream_id = hex!("0000004a00000000");
808795
rng.set_stream(stream_id);
809-
rng.set_block_pos(hex!("0000000000000009"));
796+
rng.set_block_pos(u64::from_le_bytes(hex!("0000000000000009")));
810797

811798
// The test vectors omit the first 64-bytes of the keystream
812799
let mut discard_first_64 = [0u8; 64];
@@ -1096,7 +1083,7 @@ pub(crate) mod tests {
10961083
let first_blocks_end_block_counter = rng.get_block_pos();
10971084

10981085
// get first four blocks after wrapping
1099-
rng.set_block_pos([u32::MAX, u32::MAX]);
1086+
rng.set_block_pos(u64::MAX);
11001087
let mut result = [0u8; 64 * 5];
11011088
rng.fill_bytes(&mut result);
11021089
assert_eq!(first_blocks_end_word_pos, rng.get_word_pos());

0 commit comments

Comments
 (0)