Skip to content

Commit 1b99e67

Browse files
committed
Update rand
1 parent 3593c90 commit 1b99e67

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

src/distr/integer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl Distribution<__m128i> for StandardUniform {
112112
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> __m128i {
113113
// NOTE: It's tempting to use the u128 impl here, but confusingly this
114114
// results in different code (return via rdx, r10 instead of rax, rdx
115-
// with u128 impl) and is much slower (+130 time). This version calls
116-
// le::fill_bytes_via_next but performs well.
115+
// with u128 impl) and is much slower (+130 time).
117116

118117
let mut buf = [0_u8; core::mem::size_of::<__m128i>()];
119118
rng.fill_bytes(&mut buf);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ mod test {
367367
}
368368

369369
fn fill_bytes(&mut self, dst: &mut [u8]) {
370-
rand_core::le::fill_bytes_via_next(self, dst)
370+
rand_core::utils::fill_bytes_via_next_word(dst, || self.next_u64());
371371
}
372372
}
373373

src/rngs/reseeding.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
1313
use core::mem::size_of_val;
1414

15-
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
15+
use rand_core::block::{BlockRng, CryptoGenerator, Generator};
1616
use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
1717

18-
/// A wrapper around any PRNG that implements [`BlockRngCore`], that adds the
18+
/// A wrapper around any PRNG that implements [`Generator`], that adds the
1919
/// ability to reseed it.
2020
///
2121
/// `ReseedingRng` reseeds the underlying PRNG in the following cases:
@@ -53,7 +53,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
5353
///
5454
/// ```
5555
/// use chacha20::ChaCha20Core; // Internal part of ChaChaRng that
56-
/// // implements BlockRngCore
56+
/// // implements Generator
5757
/// use rand::prelude::*;
5858
/// use rand::rngs::SysRng;
5959
/// use rand::rngs::ReseedingRng;
@@ -63,18 +63,18 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
6363
/// println!("{}", reseeding_rng.random::<u64>());
6464
/// ```
6565
///
66-
/// [`BlockRngCore`]: rand_core::block::BlockRngCore
66+
/// [`Generator`]: rand_core::block::Generator
6767
/// [`ReseedingRng::new`]: ReseedingRng::new
6868
/// [`reseed()`]: ReseedingRng::reseed
6969
#[derive(Debug)]
70-
pub struct ReseedingRng<R, Rsdr>(BlockRng<ReseedingCore<R, Rsdr>>)
70+
pub struct ReseedingRng<G, Rsdr>(BlockRng<ReseedingCore<G, Rsdr>>)
7171
where
72-
R: BlockRngCore + SeedableRng,
72+
G: Generator + SeedableRng,
7373
Rsdr: TryRngCore;
7474

75-
impl<R, Rsdr> ReseedingRng<R, Rsdr>
75+
impl<const N: usize, G, Rsdr> ReseedingRng<G, Rsdr>
7676
where
77-
R: BlockRngCore + SeedableRng,
77+
G: Generator<Output = [u32; N]> + SeedableRng,
7878
Rsdr: TryRngCore,
7979
{
8080
/// Create a new `ReseedingRng` from an existing PRNG, combined with a RNG
@@ -100,71 +100,70 @@ where
100100

101101
// TODO: this should be implemented for any type where the inner type
102102
// implements RngCore, but we can't specify that because ReseedingCore is private
103-
impl<R, Rsdr> RngCore for ReseedingRng<R, Rsdr>
103+
impl<const N: usize, G, Rsdr> RngCore for ReseedingRng<G, Rsdr>
104104
where
105-
R: BlockRngCore<Item = u32> + SeedableRng,
105+
G: Generator<Output = [u32; N]> + SeedableRng,
106106
Rsdr: TryRngCore,
107107
{
108108
#[inline(always)]
109109
fn next_u32(&mut self) -> u32 {
110-
self.0.next_u32()
110+
self.0.next_word()
111111
}
112112

113113
#[inline(always)]
114114
fn next_u64(&mut self) -> u64 {
115-
self.0.next_u64()
115+
self.0.next_u64_from_u32()
116116
}
117117

118118
fn fill_bytes(&mut self, dest: &mut [u8]) {
119119
self.0.fill_bytes(dest)
120120
}
121121
}
122122

123-
impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr>
123+
impl<const N: usize, G, Rsdr> CryptoRng for ReseedingRng<G, Rsdr>
124124
where
125-
R: BlockRngCore<Item = u32> + SeedableRng + CryptoBlockRng,
125+
G: Generator<Output = [u32; N]> + SeedableRng + CryptoGenerator,
126126
Rsdr: TryCryptoRng,
127127
{
128128
}
129129

130130
#[derive(Debug)]
131-
struct ReseedingCore<R, Rsdr> {
132-
inner: R,
131+
struct ReseedingCore<G, Rsdr> {
132+
inner: G,
133133
reseeder: Rsdr,
134134
threshold: i64,
135135
bytes_until_reseed: i64,
136136
}
137137

138-
impl<R, Rsdr> BlockRngCore for ReseedingCore<R, Rsdr>
138+
impl<G, Rsdr> Generator for ReseedingCore<G, Rsdr>
139139
where
140-
R: BlockRngCore + SeedableRng,
140+
G: Generator + SeedableRng,
141141
Rsdr: TryRngCore,
142142
{
143-
type Item = <R as BlockRngCore>::Item;
144-
type Results = <R as BlockRngCore>::Results;
143+
type Output = <G as Generator>::Output;
145144

146-
fn generate(&mut self, results: &mut Self::Results) {
145+
fn generate(&mut self, results: &mut Self::Output) {
147146
if self.bytes_until_reseed <= 0 {
148147
// We get better performance by not calling only `reseed` here
149148
// and continuing with the rest of the function, but by directly
150149
// returning from a non-inlined function.
151150
return self.reseed_and_generate(results);
152151
}
153-
let num_bytes = size_of_val(results.as_ref());
152+
let num_bytes = size_of_val(results);
154153
self.bytes_until_reseed -= num_bytes as i64;
155154
self.inner.generate(results);
156155
}
157156
}
158157

159-
impl<R, Rsdr> ReseedingCore<R, Rsdr>
158+
impl<G, Rsdr> ReseedingCore<G, Rsdr>
160159
where
161-
R: BlockRngCore + SeedableRng,
160+
G: Generator + SeedableRng,
162161
Rsdr: TryRngCore,
163162
{
164163
/// Create a new `ReseedingCore`.
165164
///
166165
/// `threshold` is the maximum number of bytes produced by
167-
/// [`BlockRngCore::generate`] before attempting reseeding.
166+
/// [`Generator::generate`] before attempting reseeding.
168167
fn new(threshold: u64, mut reseeder: Rsdr) -> Result<Self, Rsdr::Error> {
169168
// Because generating more values than `i64::MAX` takes centuries on
170169
// current hardware, we just clamp to that value.
@@ -178,7 +177,7 @@ where
178177
i64::MAX
179178
};
180179

181-
let inner = R::try_from_rng(&mut reseeder)?;
180+
let inner = G::try_from_rng(&mut reseeder)?;
182181

183182
Ok(ReseedingCore {
184183
inner,
@@ -190,17 +189,17 @@ where
190189

191190
/// Reseed the internal PRNG.
192191
fn reseed(&mut self) -> Result<(), Rsdr::Error> {
193-
R::try_from_rng(&mut self.reseeder).map(|result| {
192+
G::try_from_rng(&mut self.reseeder).map(|result| {
194193
self.bytes_until_reseed = self.threshold;
195194
self.inner = result
196195
})
197196
}
198197

199198
#[inline(never)]
200-
fn reseed_and_generate(&mut self, results: &mut <Self as BlockRngCore>::Results) {
199+
fn reseed_and_generate(&mut self, results: &mut G::Output) {
201200
trace!("Reseeding RNG (periodic reseed)");
202201

203-
let num_bytes = size_of_val(results.as_ref());
202+
let num_bytes = size_of_val(results);
204203

205204
if let Err(e) = self.reseed() {
206205
warn!("Reseeding RNG failed: {}", e);
@@ -212,9 +211,9 @@ where
212211
}
213212
}
214213

215-
impl<R, Rsdr> CryptoBlockRng for ReseedingCore<R, Rsdr>
214+
impl<G, Rsdr> CryptoGenerator for ReseedingCore<G, Rsdr>
216215
where
217-
R: BlockRngCore<Item = u32> + SeedableRng + CryptoBlockRng,
216+
G: Generator + SeedableRng + CryptoGenerator,
218217
Rsdr: TryCryptoRng,
219218
{
220219
}

src/rngs/xoshiro128plusplus.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use rand_core::{RngCore, SeedableRng, le};
9+
use rand_core::{RngCore, SeedableRng, utils};
1010
#[cfg(feature = "serde")]
1111
use serde::{Deserialize, Serialize};
1212

@@ -31,8 +31,7 @@ impl SeedableRng for Xoshiro128PlusPlus {
3131
/// mapped to a different seed.
3232
#[inline]
3333
fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus {
34-
let mut state = [0; 4];
35-
le::read_u32_into(&seed, &mut state);
34+
let state = utils::read_words(&seed);
3635
// Check for zero on aligned integers for better code generation.
3736
// Furtermore, seed_from_u64(0) will expand to a constant when optimized.
3837
if state.iter().all(|&x| x == 0) {
@@ -88,12 +87,12 @@ impl RngCore for Xoshiro128PlusPlus {
8887

8988
#[inline]
9089
fn next_u64(&mut self) -> u64 {
91-
le::next_u64_via_u32(self)
90+
utils::next_u64_via_u32(self)
9291
}
9392

9493
#[inline]
9594
fn fill_bytes(&mut self, dst: &mut [u8]) {
96-
le::fill_bytes_via_next(self, dst)
95+
utils::fill_bytes_via_next_word(dst, || self.next_u32());
9796
}
9897
}
9998

src/rngs/xoshiro256plusplus.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use rand_core::{RngCore, SeedableRng, le};
9+
use rand_core::{RngCore, SeedableRng, utils};
1010
#[cfg(feature = "serde")]
1111
use serde::{Deserialize, Serialize};
1212

@@ -31,8 +31,7 @@ impl SeedableRng for Xoshiro256PlusPlus {
3131
/// mapped to a different seed.
3232
#[inline]
3333
fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus {
34-
let mut state = [0; 4];
35-
le::read_u64_into(&seed, &mut state);
34+
let state = utils::read_words(&seed);
3635
// Check for zero on aligned integers for better code generation.
3736
// Furtermore, seed_from_u64(0) will expand to a constant when optimized.
3837
if state.iter().all(|&x| x == 0) {
@@ -95,7 +94,7 @@ impl RngCore for Xoshiro256PlusPlus {
9594

9695
#[inline]
9796
fn fill_bytes(&mut self, dst: &mut [u8]) {
98-
le::fill_bytes_via_next(self, dst)
97+
utils::fill_bytes_via_next_word(dst, || self.next_u64());
9998
}
10099
}
101100

0 commit comments

Comments
 (0)