-
-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Initialize ChaChaRng
with arbitrary counter?
#1369
Comments
Ah, it looks like 'block position' is a synonym for 'counter'. But this function from #[inline(always)]
pub fn set_block_pos(&mut self, value: u64) {
set_stream_param(self, STREAM_PARAM_BLOCK, value)
} It gets used like this: /// Set the offset from the start of the stream, in 32-bit words.
///
/// As with `get_word_pos`, we use a 68-bit number. Since the generator
/// simply cycles at the end of its period (1 ZiB), we ignore the upper
/// 60 bits.
#[inline]
pub fn set_word_pos(&mut self, word_offset: u128) {
let block = (word_offset / u128::from(BLOCK_WORDS)) as u64;
self.rng
.core
.state
.set_block_pos(block);
self.rng.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize);
} The above function sets the counter value, indirectly. But it does additional work. Therefore, this isn't the simplest possible API. Take-away, there is room for a simpler API:
This accounts for common variations in how words 12, 13, 14, 15 are allocated between counter and nonce. |
I have a fork where I'm testing the addition of:
|
Yes, Feel free to make suggestions, though it helps also knowing your use-case. We're planning on changing the backing implementation anyway: #934. |
I don't see a way to initialize
ChaChaRng
with an arbitrary counter value.I picked ChaCha because it is a great PRNG with a counter / "random access" / "fast forwarding". From Wikipedia:
Not seeing a way to initialize the counter with the current API seems like an oversight. Or perhaps I'm misunderstanding how to appropriately use the counter? If my request makes sense, I would be happy to help with a PR.
Here is one way to construct a ChaCha RNG:
Below, you can see
ctr_nonce
corresponds to 4 32-bit words. In this implementation, the first word is the counter; the rest (3 words = 12 bytes) is the nonce. In the code below, the counter is fixed at 0.Also, for reference, from https://datatracker.ietf.org/doc/html/rfc8439 :
The text was updated successfully, but these errors were encountered: