diff --git a/rand_chacha/src/chacha.rs b/rand_chacha/src/chacha.rs index 5c1c6742..50da81bf 100644 --- a/rand_chacha/src/chacha.rs +++ b/rand_chacha/src/chacha.rs @@ -18,9 +18,6 @@ use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer}; -const STREAM_PARAM_NONCE: u32 = 1; -const STREAM_PARAM_BLOCK: u32 = 0; - // NB. this must remain consistent with some currently hard-coded numbers in this module const BUF_BLOCKS: u8 = 4; // number of 32-bit words per ChaCha block (fixed by algorithm definition) @@ -196,7 +193,7 @@ macro_rules! chacha_impl { #[inline] pub fn get_word_pos(&self) -> u128 { let buf_start_block = { - let buf_end_block = self.rng.core.state.get_stream_param(STREAM_PARAM_BLOCK); + let buf_end_block = self.rng.core.state.get_block_pos(); u64::wrapping_sub(buf_end_block, BUF_BLOCKS.into()) }; let (buf_offset_blocks, block_offset_words) = { @@ -221,7 +218,7 @@ macro_rules! chacha_impl { self.rng .core .state - .set_stream_param(STREAM_PARAM_BLOCK, block); + .set_block_pos(block); self.rng.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize); } @@ -241,7 +238,7 @@ macro_rules! chacha_impl { self.rng .core .state - .set_stream_param(STREAM_PARAM_NONCE, stream); + .set_nonce(stream); if self.rng.index() != 64 { let wp = self.get_word_pos(); self.set_word_pos(wp); @@ -254,7 +251,7 @@ macro_rules! chacha_impl { self.rng .core .state - .get_stream_param(STREAM_PARAM_NONCE) + .get_nonce() } /// Get the seed. diff --git a/rand_chacha/src/guts.rs b/rand_chacha/src/guts.rs index 992d1d0a..cee8cf75 100644 --- a/rand_chacha/src/guts.rs +++ b/rand_chacha/src/guts.rs @@ -21,6 +21,9 @@ const BUFBLOCKS: u64 = 1 << LOG2_BUFBLOCKS; pub(crate) const BUFSZ64: u64 = BLOCK64 * BUFBLOCKS; pub(crate) const BUFSZ: usize = BUFSZ64 as usize; +const STREAM_PARAM_NONCE: u32 = 1; +const STREAM_PARAM_BLOCK: u32 = 0; + #[derive(Clone, PartialEq, Eq)] pub struct ChaCha { pub(crate) b: vec128_storage, @@ -83,13 +86,23 @@ impl ChaCha { } #[inline(always)] - pub fn set_stream_param(&mut self, param: u32, value: u64) { - set_stream_param(self, param, value) + pub fn set_block_pos(&mut self, value: u64) { + set_stream_param(self, STREAM_PARAM_BLOCK, value) } #[inline(always)] - pub fn get_stream_param(&self, param: u32) -> u64 { - get_stream_param(self, param) + pub fn get_block_pos(&self) -> u64 { + get_stream_param(self, STREAM_PARAM_BLOCK) + } + + #[inline(always)] + pub fn set_nonce(&mut self, value: u64) { + set_stream_param(self, STREAM_PARAM_NONCE, value) + } + + #[inline(always)] + pub fn get_nonce(&self) -> u64 { + get_stream_param(self, STREAM_PARAM_NONCE) } #[inline(always)]