Skip to content

Commit 1989f97

Browse files
committed
Update rand_chacha
1 parent 48db240 commit 1989f97

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

rand_chacha/src/chacha.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
//! The ChaCha random number generator.
1010
1111
use crate::guts::ChaCha;
12+
use core::convert::Infallible;
1213
use core::fmt;
1314
use rand_core::block::{BlockRng, CryptoGenerator, Generator};
14-
use rand_core::{CryptoRng, RngCore, SeedableRng};
15+
use rand_core::{TryCryptoRng, SeedableRng, TryRngCore};
1516

1617
#[cfg(feature = "serde")]
1718
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -89,7 +90,7 @@ macro_rules! chacha_impl {
8990
/// ```
9091
///
9192
/// This implementation uses an output buffer of sixteen `u32` words, and uses
92-
/// [`BlockRng`] to implement the [`RngCore`] methods.
93+
/// [`BlockRng`] to implement the [`TryRngCore`] methods.
9394
///
9495
/// [^1]: D. J. Bernstein, [*ChaCha, a variant of Salsa20*](
9596
/// https://cr.yp.to/chacha.html)
@@ -113,20 +114,22 @@ macro_rules! chacha_impl {
113114
}
114115
}
115116

116-
impl RngCore for $ChaChaXRng {
117+
impl TryRngCore for $ChaChaXRng {
118+
type Error = Infallible;
119+
117120
#[inline]
118-
fn next_u32(&mut self) -> u32 {
119-
self.rng.next_word()
121+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
122+
Ok(self.rng.next_word())
120123
}
121124

122125
#[inline]
123-
fn next_u64(&mut self) -> u64 {
124-
self.rng.next_u64_from_u32()
126+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
127+
Ok(self.rng.next_u64_from_u32())
125128
}
126129

127130
#[inline]
128-
fn fill_bytes(&mut self, bytes: &mut [u8]) {
129-
self.rng.fill_bytes(bytes)
131+
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Infallible> {
132+
Ok(self.rng.fill_bytes(bytes))
130133
}
131134
}
132135

@@ -147,7 +150,7 @@ macro_rules! chacha_impl {
147150
u64::wrapping_sub(buf_end_block, BUF_BLOCKS.into())
148151
};
149152
let (buf_offset_blocks, block_offset_words) = {
150-
let buf_offset_words = self.rng.index() as u64;
153+
let buf_offset_words = self.rng.word_offset() as u64;
151154
let blocks_part = buf_offset_words / u64::from(BLOCK_WORDS);
152155
let words_part = buf_offset_words % u64::from(BLOCK_WORDS);
153156
(blocks_part, words_part)
@@ -167,7 +170,7 @@ macro_rules! chacha_impl {
167170
let block = (word_offset / u128::from(BLOCK_WORDS)) as u64;
168171
self.rng.core.state.set_block_pos(block);
169172
self.rng
170-
.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize);
173+
.reset_and_skip((word_offset % u128::from(BLOCK_WORDS)) as usize);
171174
}
172175

173176
/// Set the stream number.
@@ -184,7 +187,7 @@ macro_rules! chacha_impl {
184187
#[inline]
185188
pub fn set_stream(&mut self, stream: u64) {
186189
self.rng.core.state.set_nonce(stream);
187-
if self.rng.index() != 64 {
190+
if self.rng.word_offset() != 0 {
188191
let wp = self.get_word_pos();
189192
self.set_word_pos(wp);
190193
}
@@ -203,7 +206,7 @@ macro_rules! chacha_impl {
203206
}
204207
}
205208

206-
impl CryptoRng for $ChaChaXRng {}
209+
impl TryCryptoRng for $ChaChaXRng {}
207210

208211
impl From<$ChaChaXCore> for $ChaChaXRng {
209212
fn from(core: $ChaChaXCore) -> Self {
@@ -306,10 +309,9 @@ chacha_impl!(
306309

307310
#[cfg(test)]
308311
mod test {
309-
use rand_core::{RngCore, SeedableRng};
310-
311312
#[cfg(feature = "serde")]
312313
use super::{ChaCha8Rng, ChaCha12Rng, ChaCha20Rng};
314+
use rand_core::{SeedableRng, RngCore};
313315

314316
type ChaChaRng = super::ChaCha20Rng;
315317

@@ -596,7 +598,7 @@ mod test {
596598
use rand_core::CryptoRng;
597599

598600
let mut rng1 = ChaChaRng::from_seed(Default::default());
599-
let rng2 = &mut rng1.clone() as &mut dyn CryptoRng;
601+
let mut rng2 = &mut rng1.clone() as &mut dyn CryptoRng;
600602
for _ in 0..1000 {
601603
assert_eq!(rng1.next_u64(), rng2.next_u64());
602604
}

0 commit comments

Comments
 (0)