Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rng parameter of ReseedingRng::new #1533

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
- Rename `Rng::gen_range` to `random_range`, `gen_bool` to `random_bool`, `gen_ratio` to `random_ratio` (#1505)
- Rename `Standard` to `StandardUniform` (#1526)
- Remove impl of `Distribution<Option<T>>` for `Standard` (#1526)
- Remove first parameter (`rng`) of `ReseedingRng::new` (#1533)

## [0.9.0-alpha.1] - 2024-03-18
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub fn reseeding_bytes(c: &mut Criterion) {
fn bench(g: &mut BenchmarkGroup<WallTime>, thresh: u64) {
let name = format!("chacha20_{}k", thresh);
g.bench_function(name.as_str(), |b| {
let mut rng = ReseedingRng::new(ChaCha20Core::from_os_rng(), thresh * 1024, OsRng);
let mut rng = ReseedingRng::<ChaCha20Core, _>::new(thresh * 1024, OsRng).unwrap();
let mut buf = [0u8; 1024 * 1024];
b.iter(|| {
rng.fill_bytes(&mut buf);
Expand Down
34 changes: 19 additions & 15 deletions src/rngs/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
/// use rand::rngs::OsRng;
/// use rand::rngs::ReseedingRng;
///
/// let prng = ChaCha20Core::from_os_rng();
/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng);
/// let mut reseeding_rng = ReseedingRng::<ChaCha20Core, _>::new(0, OsRng).unwrap();
///
/// println!("{}", reseeding_rng.random::<u64>());
///
Expand Down Expand Up @@ -88,8 +87,10 @@ where
/// `threshold` sets the number of generated bytes after which to reseed the
/// PRNG. Set it to zero to never reseed based on the number of generated
/// values.
pub fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self {
ReseedingRng(BlockRng::new(ReseedingCore::new(rng, threshold, reseeder)))
pub fn new(threshold: u64, reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
Ok(ReseedingRng(BlockRng::new(ReseedingCore::new(
threshold, reseeder,
)?)))
}

/// Immediately reseed the generator
Expand Down Expand Up @@ -177,7 +178,10 @@ where
Rsdr: TryRngCore,
{
/// Create a new `ReseedingCore`.
fn new(rng: R, threshold: u64, reseeder: Rsdr) -> Self {
///
/// `threshold` is the maximum number of bytes produced by
/// [`BlockRngCore::generate`] before attempting reseeding.
fn new(threshold: u64, mut reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
// Because generating more values than `i64::MAX` takes centuries on
// current hardware, we just clamp to that value.
// Also we set a threshold of 0, which indicates no limit, to that
Expand All @@ -190,12 +194,14 @@ where
i64::MAX
};

ReseedingCore {
inner: rng,
let inner = R::try_from_rng(&mut reseeder)?;

Ok(ReseedingCore {
inner,
reseeder,
threshold,
bytes_until_reseed: threshold,
}
})
}

/// Reseed the internal PRNG.
Expand Down Expand Up @@ -249,16 +255,15 @@ where
mod test {
use crate::rngs::mock::StepRng;
use crate::rngs::std::Core;
use crate::{Rng, SeedableRng};
use crate::Rng;

use super::ReseedingRng;

#[test]
fn test_reseeding() {
let mut zero = StepRng::new(0, 0);
let rng = Core::from_rng(&mut zero);
let zero = StepRng::new(0, 0);
let thresh = 1; // reseed every time the buffer is exhausted
let mut reseeding = ReseedingRng::new(rng, thresh, zero);
let mut reseeding = ReseedingRng::<Core, _>::new(thresh, zero).unwrap();

// RNG buffer size is [u32; 64]
// Debug is only implemented up to length 32 so use two arrays
Expand All @@ -276,9 +281,8 @@ mod test {
#[test]
#[allow(clippy::redundant_clone)]
fn test_clone_reseeding() {
let mut zero = StepRng::new(0, 0);
let rng = Core::from_rng(&mut zero);
let mut rng1 = ReseedingRng::new(rng, 32 * 4, zero);
let zero = StepRng::new(0, 0);
let mut rng1 = ReseedingRng::<Core, _>::new(32 * 4, zero).unwrap();

let first: u32 = rng1.random();
for _ in 0..10 {
Expand Down
8 changes: 3 additions & 5 deletions src/rngs/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fmt;
use std::rc::Rc;
use std::thread_local;

use rand_core::{CryptoRng, RngCore, SeedableRng};
use rand_core::{CryptoRng, RngCore};

use super::std::Core;
use crate::rngs::OsRng;
Expand Down Expand Up @@ -119,11 +119,9 @@ thread_local!(
// We require Rc<..> to avoid premature freeing when ThreadRng is used
// within thread-local destructors. See #968.
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = {
let r = Core::try_from_os_rng().unwrap_or_else(|err|
let rng = ReseedingRng::new(THREAD_RNG_RESEED_THRESHOLD,
OsRng).unwrap_or_else(|err|
panic!("could not initialize ThreadRng: {}", err));
let rng = ReseedingRng::new(r,
THREAD_RNG_RESEED_THRESHOLD,
OsRng);
Rc::new(UnsafeCell::new(rng))
}
);
Expand Down