Skip to content
Draft
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ exclude = [
"benches",
]
resolver = "2"

[patch.crates-io]
rand_core = { git = "https://github.com/rust-random/rand_core.git", branch = "reduce-block-code" }
17 changes: 8 additions & 9 deletions rand_hc/src/hc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! The HC-128 random number generator.

use core::fmt;
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
use rand_core::block::{BlockRng, CryptoGenerator, Generator};
use rand_core::{CryptoRng, RngCore, SeedableRng, le};

const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
Expand Down Expand Up @@ -75,12 +75,12 @@ pub struct Hc128Rng(BlockRng<Hc128Core>);
impl RngCore for Hc128Rng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
self.0.next_word()
}

#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
self.0.next_u64_from_u32()
}

#[inline]
Expand All @@ -94,7 +94,7 @@ impl SeedableRng for Hc128Rng {

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Hc128Rng(BlockRng::<Hc128Core>::from_seed(seed))
Hc128Rng(BlockRng::new(Hc128Core::from_seed(seed)))
}
}

Expand All @@ -121,11 +121,10 @@ impl fmt::Debug for Hc128Core {
}
}

impl BlockRngCore for Hc128Core {
type Item = u32;
type Results = [u32; 16];
impl Generator for Hc128Core {
type Output = [u32; 16];

fn generate(&mut self, results: &mut Self::Results) {
fn generate(&mut self, results: &mut Self::Output) {
assert!(self.counter1024 % 16 == 0);

let cc = self.counter1024 % 512;
Expand Down Expand Up @@ -342,7 +341,7 @@ impl SeedableRng for Hc128Core {
}
}

impl CryptoBlockRng for Hc128Core {}
impl CryptoGenerator for Hc128Core {}

// Custom PartialEq implementation as it can't currently be derived from an array of size 1024
impl PartialEq for Hc128Core {
Expand Down
2 changes: 1 addition & 1 deletion rand_isaac/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rust-version = "1.85"
all-features = true

[features]
serde = ["dep:serde", "rand_core/serde"]
serde = ["dep:serde"]

[dependencies]
rand_core = "0.10.0-rc-2"
Expand Down
22 changes: 10 additions & 12 deletions rand_isaac/src/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

//! The ISAAC random number generator.

use crate::isaac_array::IsaacArray;
use core::num::Wrapping as w;
use core::{fmt, slice};
use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::block::{BlockRng, Generator};
use rand_core::{RngCore, SeedableRng, TryRngCore, le};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -96,12 +95,12 @@ pub struct IsaacRng(BlockRng<IsaacCore>);
impl RngCore for IsaacRng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
self.0.next_word()
}

#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
self.0.next_u64_from_u32()
}

#[inline]
Expand All @@ -115,31 +114,31 @@ impl SeedableRng for IsaacRng {

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
IsaacRng(BlockRng::<IsaacCore>::from_seed(seed))
IsaacRng(BlockRng::new(IsaacCore::from_seed(seed)))
}

/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
#[inline]
fn seed_from_u64(seed: u64) -> Self {
IsaacRng(BlockRng::<IsaacCore>::seed_from_u64(seed))
IsaacRng(BlockRng::new(IsaacCore::seed_from_u64(seed)))
}

#[inline]
fn from_rng<R>(rng: &mut R) -> Self
where
R: RngCore + ?Sized,
{
IsaacRng(BlockRng::<IsaacCore>::from_rng(rng))
IsaacRng(BlockRng::new(IsaacCore::from_rng(rng)))
}

#[inline]
fn try_from_rng<S>(rng: &mut S) -> Result<Self, S::Error>
where
S: TryRngCore + ?Sized,
{
BlockRng::<IsaacCore>::try_from_rng(rng).map(IsaacRng)
IsaacCore::try_from_rng(rng).map(|core| IsaacRng(BlockRng::new(core)))
}
}

Expand Down Expand Up @@ -174,9 +173,8 @@ impl ::core::cmp::PartialEq for IsaacCore {
// Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE
impl ::core::cmp::Eq for IsaacCore {}

impl BlockRngCore for IsaacCore {
type Item = u32;
type Results = IsaacArray<Self::Item>;
impl Generator for IsaacCore {
type Output = [u32; RAND_SIZE];

/// Refills the output buffer, `results`. See also the pseudocode description
/// of the algorithm in the `IsaacRng` documentation.
Expand All @@ -198,7 +196,7 @@ impl BlockRngCore for IsaacCore {
/// make `fill_bytes` a memcopy. To maintain compatibility we fill in
/// reverse.
#[rustfmt::skip]
fn generate(&mut self, results: &mut IsaacArray<Self::Item>) {
fn generate(&mut self, results: &mut [u32; RAND_SIZE]) {
self.c += w(1);
// abbreviations
let mut a = self.a;
Expand Down
42 changes: 20 additions & 22 deletions rand_isaac/src/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

//! The ISAAC-64 random number generator.

use crate::isaac_array::IsaacArray;
use core::num::Wrapping as w;
use core::{fmt, slice};
use rand_core::block::{BlockRng64, BlockRngCore};
use rand_core::block::{BlockRng, Generator};
use rand_core::{RngCore, SeedableRng, TryRngCore, le};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -70,7 +69,7 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
/// }
/// ```
///
/// This implementation uses [`BlockRng64`] to implement the [`RngCore`] methods.
/// This implementation uses [`BlockRng`] to implement the [`RngCore`] methods.
///
/// See for more information the documentation of [`IsaacRng`].
///
Expand All @@ -79,20 +78,20 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
///
/// [`IsaacRng`]: crate::isaac::IsaacRng
/// [`rand_hc`]: https://docs.rs/rand_hc
/// [`BlockRng64`]: rand_core::block::BlockRng64
/// [`BlockRng`]: rand_core::block::BlockRng
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);
pub struct Isaac64Rng(BlockRng<Isaac64Core>);

impl RngCore for Isaac64Rng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
self.0.next_word() as u32
}

#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
self.0.next_word()
}

#[inline]
Expand All @@ -106,31 +105,31 @@ impl SeedableRng for Isaac64Rng {

#[inline]
fn from_seed(seed: Self::Seed) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::from_seed(seed))
Isaac64Rng(BlockRng::new(Isaac64Core::from_seed(seed)))
}

/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
#[inline]
fn seed_from_u64(seed: u64) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::seed_from_u64(seed))
Isaac64Rng(BlockRng::new(Isaac64Core::seed_from_u64(seed)))
}

#[inline]
fn from_rng<R>(rng: &mut R) -> Self
where
R: RngCore + ?Sized,
{
Isaac64Rng(BlockRng64::<Isaac64Core>::from_rng(rng))
Isaac64Rng(BlockRng::new(Isaac64Core::from_rng(rng)))
}

#[inline]
fn try_from_rng<S>(rng: &mut S) -> Result<Self, S::Error>
where
S: TryRngCore + ?Sized,
{
BlockRng64::<Isaac64Core>::try_from_rng(rng).map(Isaac64Rng)
Isaac64Core::try_from_rng(rng).map(|core| Isaac64Rng(BlockRng::new(core)))
}
}

Expand Down Expand Up @@ -165,9 +164,8 @@ impl ::core::cmp::PartialEq for Isaac64Core {
// Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE
impl ::core::cmp::Eq for Isaac64Core {}

impl BlockRngCore for Isaac64Core {
type Item = u64;
type Results = IsaacArray<Self::Item>;
impl Generator for Isaac64Core {
type Output = [u64; RAND_SIZE];

/// Refills the output buffer, `results`. See also the pseudocode description
/// of the algorithm in the `Isaac64Rng` documentation.
Expand All @@ -189,7 +187,7 @@ impl BlockRngCore for Isaac64Core {
/// make `fill_bytes` a memcopy. To maintain compatibility we fill in
/// reverse.
#[rustfmt::skip]
fn generate(&mut self, results: &mut IsaacArray<Self::Item>) {
fn generate(&mut self, results: &mut [u64; RAND_SIZE]) {
self.c += w(1);
// abbreviations
let mut a = self.a;
Expand Down Expand Up @@ -457,8 +455,8 @@ mod test {
}
// Subset of above values, as an LE u32 sequence
let expected = [
3477963620, 3509106075, 687845478, 1797495790, 227048253, 2523132918, 4044335064,
1260557630, 4079741768, 3001306521, 69157722, 3958365844,
3477963620, 687845478, 227048253, 4044335064, 4079741768, 69157722, 3912394646,
1204022051, 2459090310, 2151271855, 384864925, 1183723065,
];
assert_eq!(results, expected);
}
Expand All @@ -475,12 +473,12 @@ mod test {
// `test_isaac64_true_values_32`.
assert_eq!(rng.next_u64(), 15071495833797886820);
assert_eq!(rng.next_u32(), 687845478);
assert_eq!(rng.next_u32(), 1797495790);
assert_eq!(rng.next_u64(), 10836773366498097981);
assert_eq!(rng.next_u32(), 4044335064);
assert_eq!(rng.next_u32(), 227048253);
assert_eq!(rng.next_u64(), 5414053799617603544);
assert_eq!(rng.next_u32(), 4079741768);
// Skip one u32
assert_eq!(rng.next_u64(), 12890513357046278984);
assert_eq!(rng.next_u32(), 69157722);
assert_eq!(rng.next_u64(), 17001051845652595546);
assert_eq!(rng.next_u32(), 3912394646);
}

#[test]
Expand Down
Loading
Loading