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

Update to rand master #53

Merged
merged 9 commits into from
Jun 4, 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
4 changes: 2 additions & 2 deletions .github/workflows/benches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
with:
toolchain: nightly
components: clippy, rustfmt
- name: Build
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
- name: Rustfmt
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
- name: Build
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ exclude = [
"benches",
]
resolver = "2"

[patch.crates-io.rand_core]
git = "https://github.com/rust-random/rand.git"
branch = "master"
6 changes: 5 additions & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false
[dev-dependencies]
criterion = "0.5.0"
criterion-cycles-per-byte = "0.6"
rand_core = { version = "0.6.4", features = ["getrandom"] }
rand_core = { version = "=0.9.0-alpha.1", features = ["getrandom"] }
rand_xoshiro = { path = "../rand_xoshiro", version = "0.6" }
rand_isaac = { path = "../rand_isaac", version = "0.3" }
rand_xorshift = { path = "../rand_xorshift", version = "0.3" }
Expand All @@ -18,3 +18,7 @@ rand_hc = { path = "../rand_hc", version = "0.3" }
name = "mod"
path = "mod.rs"
harness = false

[patch.crates-io.rand_core]
git = "https://github.com/rust-random/rand.git"
branch = "master"
94 changes: 47 additions & 47 deletions benches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ fn gen_bytes(c: &mut Criterion<CyclesPerByte>) {
};
}

gen_bytes!("xorshift", XorShiftRng::from_entropy());
gen_bytes!("xoshiro256starstar", Xoshiro256StarStar::from_entropy());
gen_bytes!("xoshiro256plus", Xoshiro256Plus::from_entropy());
gen_bytes!("xoshiro256plusplus", Xoshiro256PlusPlus::from_entropy());
gen_bytes!("xoshiro128starstar", Xoshiro128StarStar::from_entropy());
gen_bytes!("xoshiro128plus", Xoshiro128Plus::from_entropy());
gen_bytes!("xoshiro128plusplus", Xoshiro128PlusPlus::from_entropy());
gen_bytes!("xoroshiro128starstar", Xoroshiro128StarStar::from_entropy());
gen_bytes!("xoroshiro128plus", Xoroshiro128Plus::from_entropy());
gen_bytes!("xoroshiro64starstar", Xoroshiro64StarStar::from_entropy());
gen_bytes!("xoroshiro64star", Xoroshiro64Star::from_entropy());
gen_bytes!("splitmix64", SplitMix64::from_entropy());
gen_bytes!("hc128", Hc128Rng::from_entropy());
gen_bytes!("isaac", IsaacRng::from_entropy());
gen_bytes!("isaac64", Isaac64Rng::from_entropy());
gen_bytes!("xorshift", XorShiftRng::from_os_rng());
gen_bytes!("xoshiro256starstar", Xoshiro256StarStar::from_os_rng());
gen_bytes!("xoshiro256plus", Xoshiro256Plus::from_os_rng());
gen_bytes!("xoshiro256plusplus", Xoshiro256PlusPlus::from_os_rng());
gen_bytes!("xoshiro128starstar", Xoshiro128StarStar::from_os_rng());
gen_bytes!("xoshiro128plus", Xoshiro128Plus::from_os_rng());
gen_bytes!("xoshiro128plusplus", Xoshiro128PlusPlus::from_os_rng());
gen_bytes!("xoroshiro128starstar", Xoroshiro128StarStar::from_os_rng());
gen_bytes!("xoroshiro128plus", Xoroshiro128Plus::from_os_rng());
gen_bytes!("xoroshiro64starstar", Xoroshiro64StarStar::from_os_rng());
gen_bytes!("xoroshiro64star", Xoroshiro64Star::from_os_rng());
gen_bytes!("splitmix64", SplitMix64::from_os_rng());
gen_bytes!("hc128", Hc128Rng::from_os_rng());
gen_bytes!("isaac", IsaacRng::from_os_rng());
gen_bytes!("isaac64", Isaac64Rng::from_os_rng());
}

// Save a dependency on Rand:
Expand Down Expand Up @@ -105,102 +105,102 @@ fn gen_uint(c: &mut Criterion<CyclesPerByte>) {
let mut g = c.benchmark_group("gen_u32");
g.throughput(Throughput::Bytes(size_of::<u32>() as u64 * RAND_BENCH_N));

gen_uint!(g, "xorshift", u32, XorShiftRng::from_entropy());
gen_uint!(g, "xorshift", u32, XorShiftRng::from_os_rng());
gen_uint!(
g,
"xoshiro256starstar",
u32,
Xoshiro256StarStar::from_entropy()
Xoshiro256StarStar::from_os_rng()
);
gen_uint!(g, "xoshiro256plus", u32, Xoshiro256Plus::from_entropy());
gen_uint!(g, "xoshiro256plus", u32, Xoshiro256Plus::from_os_rng());
gen_uint!(
g,
"xoshiro256plusplus",
u32,
Xoshiro256PlusPlus::from_entropy()
Xoshiro256PlusPlus::from_os_rng()
);
gen_uint!(
g,
"xoshiro128starstar",
u32,
Xoshiro128StarStar::from_entropy()
Xoshiro128StarStar::from_os_rng()
);
gen_uint!(g, "xoshiro128plus", u32, Xoshiro128Plus::from_entropy());
gen_uint!(g, "xoshiro128plus", u32, Xoshiro128Plus::from_os_rng());
gen_uint!(
g,
"xoshiro128plusplus",
u32,
Xoshiro128PlusPlus::from_entropy()
Xoshiro128PlusPlus::from_os_rng()
);
gen_uint!(
g,
"xoroshiro128starstar",
u32,
Xoroshiro128StarStar::from_entropy()
Xoroshiro128StarStar::from_os_rng()
);
gen_uint!(g, "xoroshiro128plus", u32, Xoroshiro128Plus::from_entropy());
gen_uint!(g, "xoroshiro128plus", u32, Xoroshiro128Plus::from_os_rng());
gen_uint!(
g,
"xoroshiro64starstar",
u32,
Xoroshiro64StarStar::from_entropy()
Xoroshiro64StarStar::from_os_rng()
);
gen_uint!(g, "xoroshiro64star", u32, Xoroshiro64Star::from_entropy());
gen_uint!(g, "splitmix64", u32, SplitMix64::from_entropy());
gen_uint!(g, "hc128", u32, Hc128Rng::from_entropy());
gen_uint!(g, "isaac", u32, IsaacRng::from_entropy());
gen_uint!(g, "isaac64", u32, Isaac64Rng::from_entropy());
gen_uint!(g, "xoroshiro64star", u32, Xoroshiro64Star::from_os_rng());
gen_uint!(g, "splitmix64", u32, SplitMix64::from_os_rng());
gen_uint!(g, "hc128", u32, Hc128Rng::from_os_rng());
gen_uint!(g, "isaac", u32, IsaacRng::from_os_rng());
gen_uint!(g, "isaac64", u32, Isaac64Rng::from_os_rng());
}

{
let mut g = c.benchmark_group("gen_u64");
g.throughput(Throughput::Bytes(size_of::<u64>() as u64 * RAND_BENCH_N));

gen_uint!(g, "xorshift", u64, XorShiftRng::from_entropy());
gen_uint!(g, "xorshift", u64, XorShiftRng::from_os_rng());
gen_uint!(
g,
"xoshiro256starstar",
u64,
Xoshiro256StarStar::from_entropy()
Xoshiro256StarStar::from_os_rng()
);
gen_uint!(g, "xoshiro256plus", u64, Xoshiro256Plus::from_entropy());
gen_uint!(g, "xoshiro256plus", u64, Xoshiro256Plus::from_os_rng());
gen_uint!(
g,
"xoshiro256plusplus",
u64,
Xoshiro256PlusPlus::from_entropy()
Xoshiro256PlusPlus::from_os_rng()
);
gen_uint!(
g,
"xoshiro128starstar",
u64,
Xoshiro128StarStar::from_entropy()
Xoshiro128StarStar::from_os_rng()
);
gen_uint!(g, "xoshiro128plus", u64, Xoshiro128Plus::from_entropy());
gen_uint!(g, "xoshiro128plus", u64, Xoshiro128Plus::from_os_rng());
gen_uint!(
g,
"xoshiro128plusplus",
u64,
Xoshiro128PlusPlus::from_entropy()
Xoshiro128PlusPlus::from_os_rng()
);
gen_uint!(
g,
"xoroshiro128starstar",
u64,
Xoroshiro128StarStar::from_entropy()
Xoroshiro128StarStar::from_os_rng()
);
gen_uint!(g, "xoroshiro128plus", u64, Xoroshiro128Plus::from_entropy());
gen_uint!(g, "xoroshiro128plus", u64, Xoroshiro128Plus::from_os_rng());
gen_uint!(
g,
"xoroshiro64starstar",
u64,
Xoroshiro64StarStar::from_entropy()
Xoroshiro64StarStar::from_os_rng()
);
gen_uint!(g, "xoroshiro64star", u64, Xoroshiro64Star::from_entropy());
gen_uint!(g, "splitmix64", u64, SplitMix64::from_entropy());
gen_uint!(g, "hc128", u64, Hc128Rng::from_entropy());
gen_uint!(g, "isaac", u64, IsaacRng::from_entropy());
gen_uint!(g, "isaac64", u64, Isaac64Rng::from_entropy());
gen_uint!(g, "xoroshiro64star", u64, Xoroshiro64Star::from_os_rng());
gen_uint!(g, "splitmix64", u64, SplitMix64::from_os_rng());
gen_uint!(g, "hc128", u64, Hc128Rng::from_os_rng());
gen_uint!(g, "isaac", u64, IsaacRng::from_os_rng());
gen_uint!(g, "isaac64", u64, Isaac64Rng::from_os_rng());
}
}

Expand All @@ -210,8 +210,8 @@ fn init(c: &mut Criterion) {
macro_rules! init_gen {
($fnn:expr, $gen:ident) => {
g.bench_function($fnn, |b| {
let mut rng = XorShiftRng::from_entropy();
b.iter(|| $gen::from_rng(black_box(&mut rng)).unwrap())
let mut rng = XorShiftRng::from_os_rng();
b.iter(|| $gen::from_rng(black_box(&mut rng)))
});
};
}
Expand Down
2 changes: 1 addition & 1 deletion rand_hc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ edition = "2021"
rust-version = "1.61"

[dependencies]
rand_core = "0.6"
rand_core = "=0.9.0-alpha.1"
22 changes: 12 additions & 10 deletions rand_hc/src/hc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
//! The HC-128 random number generator.

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

const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv

Expand Down Expand Up @@ -87,13 +87,10 @@ impl RngCore for Hc128Rng {
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
}

rand_core::impl_try_crypto_rng_from_crypto_rng!(Hc128Rng);

impl SeedableRng for Hc128Rng {
type Seed = <Hc128Core as SeedableRng>::Seed;

Expand All @@ -103,8 +100,13 @@ impl SeedableRng for Hc128Rng {
}

#[inline]
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
fn from_rng(rng: impl RngCore) -> Self {
Hc128Rng(BlockRng::<Hc128Core>::from_rng(rng))
}

#[inline]
fn try_from_rng<R: TryRngCore>(rng: R) -> Result<Self, R::Error> {
BlockRng::<Hc128Core>::try_from_rng(rng).map(Hc128Rng)
}
Comment on lines 102 to 110
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In many cases, I think we need to implement both of these. Annoying, but not terrible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I suggested some time ago, in the new release cycle it may be worth to drop the block stuff completely. I will try to do a draft PR later.

}

Expand Down Expand Up @@ -352,7 +354,7 @@ impl SeedableRng for Hc128Core {
}
}

impl CryptoRng for Hc128Core {}
impl CryptoBlockRng 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.61"
serde1 = ["serde", "rand_core/serde1"]

[dependencies]
rand_core = { version = "0.6" }
rand_core = { version = "=0.9.0-alpha.1" }
serde = { version = "1.0.103", features = ["derive"], optional = true }
# Not a direct dependency but required to boost the minimum version:
serde_derive = { version = "1.0.103", optional = true }
Expand Down
39 changes: 29 additions & 10 deletions rand_isaac/src/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::isaac_array::IsaacArray;
use core::num::Wrapping as w;
use core::{fmt, slice};
use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::{le, Error, RngCore, SeedableRng};
use rand_core::{le, RngCore, SeedableRng, TryRngCore};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -108,13 +108,10 @@ impl RngCore for IsaacRng {
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}

#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
}

rand_core::impl_try_rng_from_rng_core!(IsaacRng);

impl SeedableRng for IsaacRng {
type Seed = <IsaacCore as SeedableRng>::Seed;

Expand All @@ -132,8 +129,13 @@ impl SeedableRng for IsaacRng {
}

#[inline]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
BlockRng::<IsaacCore>::from_rng(rng).map(IsaacRng)
fn from_rng(rng: impl RngCore) -> Self {
IsaacRng(BlockRng::<IsaacCore>::from_rng(rng))
}

#[inline]
fn try_from_rng<S: TryRngCore>(rng: S) -> Result<Self, S::Error> {
BlockRng::<IsaacCore>::try_from_rng(rng).map(IsaacRng)
}
}

Expand Down Expand Up @@ -365,7 +367,24 @@ impl SeedableRng for IsaacCore {
Self::init(key, 1)
}

fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
fn from_rng(mut rng: impl RngCore) -> Self {
// Custom `from_rng` implementation that fills a seed with the same size
// as the entire state.
let mut seed = [w(0u32); RAND_SIZE];
unsafe {
let ptr = seed.as_mut_ptr() as *mut u8;

let slice = slice::from_raw_parts_mut(ptr, RAND_SIZE * 4);
rng.fill_bytes(slice);
}
for i in seed.iter_mut() {
*i = w(i.0.to_le());
}

Self::init(seed, 2)
}

fn try_from_rng<R: TryRngCore>(mut rng: R) -> Result<Self, R::Error> {
// Custom `from_rng` implementation that fills a seed with the same size
// as the entire state.
let mut seed = [w(0u32); RAND_SIZE];
Expand Down Expand Up @@ -398,7 +417,7 @@ mod test {
let mut rng1 = IsaacRng::from_seed(seed);
assert_eq!(rng1.next_u32(), 2869442790);

let mut rng2 = IsaacRng::from_rng(rng1).unwrap();
let mut rng2 = IsaacRng::from_rng(rng1);
assert_eq!(rng2.next_u32(), 3094074039);
}

Expand Down
Loading
Loading