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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ exclude = ["benches", "distr_test"]
rand_core = { version = "0.10.0-rc-2", default-features = false }
log = { version = "0.4.4", optional = true }
serde = { version = "1.0.103", features = ["derive"], optional = true }
chacha20 = { version = "=0.10.0-rc.5", default-features = false, features = ["rng"], optional = true }
chacha20 = { path = "rand_chacha", optional = true, package = "rand_chacha" }
getrandom = { version = "0.3.0", optional = true }

[dev-dependencies]
Expand All @@ -85,3 +85,7 @@ rand_pcg = { path = "rand_pcg", version = "0.10.0-rc.1" }
postcard = {version = "1.1.3", default-features = false, features = ["alloc"]}
rayon = "1.7"
serde_json = "1.0.140"

[patch.crates-io.rand_core]
git = "https://github.com/rust-random/rand_core.git"
rev = "29b1630b"
4 changes: 4 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ harness = false
[[bench]]
name = "weighted"
harness = false

[patch.crates-io.rand_core]
git = "https://github.com/rust-random/rand_core.git"
rev = "29b1630b"
63 changes: 8 additions & 55 deletions rand_chacha/src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

#[cfg(feature = "serde")]
Expand All @@ -21,52 +21,6 @@ const BUF_BLOCKS: u8 = 4;
// number of 32-bit words per ChaCha block (fixed by algorithm definition)
const BLOCK_WORDS: u8 = 16;

#[repr(transparent)]
pub struct Array64<T>([T; 64]);
impl<T> Default for Array64<T>
where
T: Default,
{
#[rustfmt::skip]
fn default() -> Self {
Self([
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
])
}
}
impl<T> AsRef<[T]> for Array64<T> {
fn as_ref(&self) -> &[T] {
&self.0
}
}
impl<T> AsMut<[T]> for Array64<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut self.0
}
}
impl<T> Clone for Array64<T>
where
T: Copy + Default,
{
fn clone(&self) -> Self {
let mut new = Self::default();
new.0.copy_from_slice(&self.0);
new
}
}
impl<T> fmt::Debug for Array64<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Array64 {{}}")
}
}

macro_rules! chacha_impl {
($ChaChaXCore:ident, $ChaChaXRng:ident, $rounds:expr, $doc:expr, $abst:ident,) => {
#[doc=$doc]
Expand All @@ -82,13 +36,12 @@ macro_rules! chacha_impl {
}
}

impl BlockRngCore for $ChaChaXCore {
type Item = u32;
type Results = Array64<u32>;
impl Generator for $ChaChaXCore {
type Output = [u32; 64];

#[inline]
fn generate(&mut self, r: &mut Self::Results) {
self.state.refill4($rounds, &mut r.0);
fn generate(&mut self, output: &mut Self::Output) {
self.state.refill4($rounds, output);
}
}

Expand All @@ -103,7 +56,7 @@ macro_rules! chacha_impl {
}
}

impl CryptoBlockRng for $ChaChaXCore {}
impl CryptoGenerator for $ChaChaXCore {}

/// A cryptographically secure random number generator that uses the ChaCha algorithm.
///
Expand Down Expand Up @@ -163,12 +116,12 @@ macro_rules! chacha_impl {
impl RngCore for $ChaChaXRng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.rng.next_u32()
self.rng.next_word()
}

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

#[inline]
Expand Down
12 changes: 5 additions & 7 deletions rand_pcg/src/pcg128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645;

use core::fmt;
use rand_core::{RngCore, SeedableRng, le};
use rand_core::{RngCore, SeedableRng, utils};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -126,8 +126,7 @@ impl SeedableRng for Lcg128Xsl64 {
/// We use a single 255-bit seed to initialise the state and select a stream.
/// One `seed` bit (lowest bit of `seed[8]`) is ignored.
fn from_seed(seed: Self::Seed) -> Self {
let mut seed_u64 = [0u64; 4];
le::read_u64_into(&seed, &mut seed_u64);
let seed_u64: [u64; 4] = utils::read_words(&seed);
let state = u128::from(seed_u64[0]) | (u128::from(seed_u64[1]) << 64);
let incr = u128::from(seed_u64[2]) | (u128::from(seed_u64[3]) << 64);

Expand All @@ -150,7 +149,7 @@ impl RngCore for Lcg128Xsl64 {

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
le::fill_bytes_via_next(self, dest)
utils::fill_bytes_via_next_word(dest, || self.next_u64());
}
}

Expand Down Expand Up @@ -232,8 +231,7 @@ impl SeedableRng for Mcg128Xsl64 {

fn from_seed(seed: Self::Seed) -> Self {
// Read as if a little-endian u128 value:
let mut seed_u64 = [0u64; 2];
le::read_u64_into(&seed, &mut seed_u64);
let seed_u64: [u64; 2] = utils::read_words(&seed);
let state = u128::from(seed_u64[0]) | (u128::from(seed_u64[1]) << 64);
Mcg128Xsl64::new(state)
}
Expand All @@ -253,7 +251,7 @@ impl RngCore for Mcg128Xsl64 {

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
le::fill_bytes_via_next(self, dest)
utils::fill_bytes_via_next_word(dest, || self.next_u64());
}
}

Expand Down
7 changes: 3 additions & 4 deletions rand_pcg/src/pcg128cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const MULTIPLIER: u64 = 15750249268501108917;

use core::fmt;
use rand_core::{RngCore, SeedableRng, le};
use rand_core::{RngCore, SeedableRng, utils};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -131,8 +131,7 @@ impl SeedableRng for Lcg128CmDxsm64 {
/// We use a single 255-bit seed to initialise the state and select a stream.
/// One `seed` bit (lowest bit of `seed[8]`) is ignored.
fn from_seed(seed: Self::Seed) -> Self {
let mut seed_u64 = [0u64; 4];
le::read_u64_into(&seed, &mut seed_u64);
let seed_u64: [u64; 4] = utils::read_words(&seed);
let state = u128::from(seed_u64[0]) | (u128::from(seed_u64[1]) << 64);
let incr = u128::from(seed_u64[2]) | (u128::from(seed_u64[3]) << 64);

Expand All @@ -156,7 +155,7 @@ impl RngCore for Lcg128CmDxsm64 {

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
le::fill_bytes_via_next(self, dest)
utils::fill_bytes_via_next_word(dest, || self.next_u64());
}
}

Expand Down
9 changes: 4 additions & 5 deletions rand_pcg/src/pcg64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! PCG random number generators

use core::fmt;
use rand_core::{RngCore, SeedableRng, le};
use rand_core::{RngCore, SeedableRng, utils};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -127,8 +127,7 @@ impl SeedableRng for Lcg64Xsh32 {
/// We use a single 127-bit seed to initialise the state and select a stream.
/// One `seed` bit (lowest bit of `seed[8]`) is ignored.
fn from_seed(seed: Self::Seed) -> Self {
let mut seed_u64 = [0u64; 2];
le::read_u64_into(&seed, &mut seed_u64);
let seed_u64: [u64; 2] = utils::read_words(&seed);

// The increment must be odd, hence we discard one bit:
Lcg64Xsh32::from_state_incr(seed_u64[0], seed_u64[1] | 1)
Expand All @@ -154,11 +153,11 @@ impl RngCore for Lcg64Xsh32 {

#[inline]
fn next_u64(&mut self) -> u64 {
le::next_u64_via_u32(self)
utils::next_u64_via_u32(self)
}

#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
le::fill_bytes_via_next(self, dest)
utils::fill_bytes_via_next_word(dest, || self.next_u32());
}
}
3 changes: 1 addition & 2 deletions src/distr/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ impl Distribution<__m128i> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> __m128i {
// NOTE: It's tempting to use the u128 impl here, but confusingly this
// results in different code (return via rdx, r10 instead of rax, rdx
// with u128 impl) and is much slower (+130 time). This version calls
// le::fill_bytes_via_next but performs well.
// with u128 impl) and is much slower (+130 time).

let mut buf = [0_u8; core::mem::size_of::<__m128i>()];
rng.fill_bytes(&mut buf);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ mod test {
}

fn fill_bytes(&mut self, dst: &mut [u8]) {
rand_core::le::fill_bytes_via_next(self, dst)
rand_core::utils::fill_bytes_via_next_word(dst, || self.next_u64());
}
}

Expand Down
Loading
Loading