Skip to content

Commit 085cbff

Browse files
committed
Rework CryptoRng
1 parent 1e96eb4 commit 085cbff

File tree

5 files changed

+28
-50
lines changed

5 files changed

+28
-50
lines changed

rand_chacha/src/chacha.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use self::core::fmt;
1515
use crate::guts::ChaCha;
16-
use rand_core::block::{BlockRng, BlockRngCore};
16+
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
1717
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
1818

1919
#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer};
@@ -99,7 +99,7 @@ macro_rules! chacha_impl {
9999
}
100100
}
101101

102-
impl CryptoRng for $ChaChaXCore {}
102+
impl CryptoBlockRng for $ChaChaXCore {}
103103

104104
/// A cryptographically secure random number generator that uses the ChaCha algorithm.
105105
///
@@ -626,12 +626,12 @@ mod test {
626626

627627
#[test]
628628
fn test_trait_objects() {
629-
use rand_core::CryptoRngCore;
629+
use rand_core::CryptoRng;
630630

631-
let rng = &mut ChaChaRng::from_seed(Default::default()) as &mut dyn CryptoRngCore;
632-
let r1 = rng.next_u64();
633-
let rng: &mut dyn RngCore = rng.as_rngcore();
634-
let r2 = rng.next_u64();
635-
assert_ne!(r1, r2);
631+
let mut rng1 = ChaChaRng::from_seed(Default::default());
632+
let rng2 = &mut rng1.clone() as &mut dyn CryptoRng;
633+
for _ in 0..1000 {
634+
assert_eq!(rng1.next_u64(), rng2.next_u64());
635+
}
636636
}
637637
}

rand_core/src/block.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! }
4444
//! }
4545
//!
46-
//! // optionally, also implement CryptoRng for MyRngCore
46+
//! // optionally, also implement CryptoBlockRng for MyRngCore
4747
//!
4848
//! // Final RNG.
4949
//! let mut rng = BlockRng::<MyRngCore>::seed_from_u64(0);
@@ -54,7 +54,7 @@
5454
//! [`fill_bytes`]: RngCore::fill_bytes
5555
5656
use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks};
57-
use crate::{CryptoRng, Error, RngCore, SeedableRng};
57+
use crate::{Error, CryptoRng, RngCore, SeedableRng};
5858
use core::convert::AsRef;
5959
use core::fmt;
6060
#[cfg(feature = "serde1")]
@@ -77,6 +77,12 @@ pub trait BlockRngCore {
7777
fn generate(&mut self, results: &mut Self::Results);
7878
}
7979

80+
/// A marker trait used to indicate that an [`RngCore`] implementation is
81+
/// supposed to be cryptographically secure.
82+
///
83+
/// See [`CryptoRng`][crate::CryptoRng] docs for more information.
84+
pub trait CryptoBlockRng: BlockRngCore { }
85+
8086
/// A wrapper type implementing [`RngCore`] for some type implementing
8187
/// [`BlockRngCore`] with `u32` array buffer; i.e. this can be used to implement
8288
/// a full RNG from just a `generate` function.
@@ -256,6 +262,8 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
256262
}
257263
}
258264

265+
impl<R: CryptoBlockRng + BlockRngCore<Item = u32>> CryptoRng for BlockRng<R> {}
266+
259267
/// A wrapper type implementing [`RngCore`] for some type implementing
260268
/// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
261269
/// a full RNG from just a `generate` function.
@@ -422,7 +430,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
422430
}
423431
}
424432

425-
impl<R: BlockRngCore + CryptoRng> CryptoRng for BlockRng<R> {}
433+
impl<R: CryptoBlockRng + BlockRngCore<Item = u64>> CryptoRng for BlockRng64<R> {}
426434

427435
#[cfg(test)]
428436
mod test {

rand_core/src/lib.rs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ pub trait RngCore {
191191
}
192192
}
193193

194-
/// A marker trait used to indicate that an [`RngCore`] or [`BlockRngCore`]
195-
/// implementation is supposed to be cryptographically secure.
194+
/// A marker trait used to indicate that an [`RngCore`] implementation is
195+
/// supposed to be cryptographically secure.
196196
///
197197
/// *Cryptographically secure generators*, also known as *CSPRNGs*, should
198198
/// satisfy an additional properties over other generators: given the first
@@ -213,36 +213,7 @@ pub trait RngCore {
213213
/// weaknesses such as seeding from a weak entropy source or leaking state.
214214
///
215215
/// [`BlockRngCore`]: block::BlockRngCore
216-
pub trait CryptoRng {}
217-
218-
/// An extension trait that is automatically implemented for any type
219-
/// implementing [`RngCore`] and [`CryptoRng`].
220-
///
221-
/// It may be used as a trait object, and supports upcasting to [`RngCore`] via
222-
/// the [`CryptoRngCore::as_rngcore`] method.
223-
///
224-
/// # Example
225-
///
226-
/// ```
227-
/// use rand_core::CryptoRngCore;
228-
///
229-
/// #[allow(unused)]
230-
/// fn make_token(rng: &mut dyn CryptoRngCore) -> [u8; 32] {
231-
/// let mut buf = [0u8; 32];
232-
/// rng.fill_bytes(&mut buf);
233-
/// buf
234-
/// }
235-
/// ```
236-
pub trait CryptoRngCore: CryptoRng + RngCore {
237-
/// Upcast to an [`RngCore`] trait object.
238-
fn as_rngcore(&mut self) -> &mut dyn RngCore;
239-
}
240-
241-
impl<T: CryptoRng + RngCore> CryptoRngCore for T {
242-
fn as_rngcore(&mut self) -> &mut dyn RngCore {
243-
self
244-
}
245-
}
216+
pub trait CryptoRng: RngCore {}
246217

247218
/// A random number generator that can be explicitly seeded.
248219
///

src/distributions/weighted_index.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ impl<X> Distribution<usize> for WeightedIndex<X>
226226
where X: SampleUniform + PartialOrd
227227
{
228228
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
229-
use ::core::cmp::Ordering;
230229
let chosen_weight = self.weight_distribution.sample(rng);
231230
// Find the first item which has a weight *higher* than the chosen weight.
232231
self.cumulative_weights.partition_point(|w| w <= &chosen_weight)

src/rngs/adapter/reseeding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use core::mem::size_of;
1414

15-
use rand_core::block::{BlockRng, BlockRngCore};
15+
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
1616
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
1717

1818
/// A wrapper around any PRNG that implements [`BlockRngCore`], that adds the
@@ -147,8 +147,8 @@ where
147147

148148
impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr>
149149
where
150-
R: BlockRngCore + SeedableRng + CryptoRng,
151-
Rsdr: RngCore + CryptoRng,
150+
R: BlockRngCore<Item = u32> + SeedableRng + CryptoBlockRng,
151+
Rsdr: CryptoRng,
152152
{
153153
}
154154

@@ -276,10 +276,10 @@ where
276276
}
277277
}
278278

279-
impl<R, Rsdr> CryptoRng for ReseedingCore<R, Rsdr>
279+
impl<R, Rsdr> CryptoBlockRng for ReseedingCore<R, Rsdr>
280280
where
281-
R: BlockRngCore + SeedableRng + CryptoRng,
282-
Rsdr: RngCore + CryptoRng,
281+
R: BlockRngCore<Item = u32> + SeedableRng + CryptoBlockRng,
282+
Rsdr: CryptoRng,
283283
{
284284
}
285285

0 commit comments

Comments
 (0)