Skip to content

Commit 34c5686

Browse files
committed
Move BlockRng* to new rand_core::block module; don't export from rand
These are almost exclusively for implementation of certain types of RNG, therefore exposing BlockRngCore in rand is not useful.
1 parent 76083d0 commit 34c5686

File tree

9 files changed

+489
-473
lines changed

9 files changed

+489
-473
lines changed

rand_core/src/block.rs

Lines changed: 474 additions & 0 deletions
Large diffs are not rendered by default.

rand_core/src/impls.rs

Lines changed: 2 additions & 408 deletions
Large diffs are not rendered by default.

rand_core/src/lib.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub use error::{ErrorKind, Error};
5959

6060

6161
mod error;
62+
pub mod block;
6263
pub mod impls;
6364
pub mod le;
6465

@@ -191,59 +192,6 @@ pub trait RngCore {
191192
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;
192193
}
193194

194-
/// A trait for RNGs which do not generate random numbers individually, but in
195-
/// blocks (typically `[u32; N]`). This technique is commonly used by
196-
/// cryptographic RNGs to improve performance.
197-
///
198-
/// Usage of this trait is optional, but provides two advantages:
199-
/// implementations only need to concern themselves with generation of the
200-
/// block, not the various [`RngCore`] methods (especially [`fill_bytes`], where the
201-
/// optimal implementations are not trivial), and this allows `ReseedingRng` to
202-
/// perform periodic reseeding with very low overhead.
203-
///
204-
/// # Example
205-
///
206-
/// ```norun
207-
/// use rand_core::BlockRngCore;
208-
/// use rand_core::impls::BlockRng;
209-
///
210-
/// struct MyRngCore;
211-
///
212-
/// impl BlockRngCore for MyRngCore {
213-
/// type Results = [u32; 16];
214-
///
215-
/// fn generate(&mut self, results: &mut Self::Results) {
216-
/// unimplemented!()
217-
/// }
218-
/// }
219-
///
220-
/// impl SeedableRng for MyRngCore {
221-
/// type Seed = unimplemented!();
222-
/// fn from_seed(seed: Self::Seed) -> Self {
223-
/// unimplemented!()
224-
/// }
225-
/// }
226-
///
227-
/// // optionally, also implement CryptoRng for MyRngCore
228-
///
229-
/// // Final RNG.
230-
/// type MyRng = BlockRng<u32, MyRngCore>;
231-
/// ```
232-
///
233-
/// [`RngCore`]: trait.RngCore.html
234-
/// [`fill_bytes`]: trait.RngCore.html#tymethod.fill_bytes
235-
pub trait BlockRngCore {
236-
/// Results element type, e.g. `u32`.
237-
type Item;
238-
239-
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
240-
/// generates, which will usually be an array like `[u32; 16]`.
241-
type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;
242-
243-
/// Generate a new block of results.
244-
fn generate(&mut self, results: &mut Self::Results);
245-
}
246-
247195
/// A marker trait used to indicate that an [`RngCore`] or [`BlockRngCore`]
248196
/// implementation is supposed to be cryptographically secure.
249197
///
@@ -266,7 +214,7 @@ pub trait BlockRngCore {
266214
/// weaknesses such as seeding from a weak entropy source or leaking state.
267215
///
268216
/// [`RngCore`]: trait.RngCore.html
269-
/// [`BlockRngCore`]: trait.BlockRngCore.html
217+
/// [`BlockRngCore`]: ../rand_core/block/trait.BlockRngCore.html
270218
pub trait CryptoRng {}
271219

272220
/// A random number generator that can be explicitly seeded.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ extern crate rand_core;
203203

204204

205205
// Re-exports from rand_core
206-
pub use rand_core::{RngCore, BlockRngCore, CryptoRng, SeedableRng};
206+
pub use rand_core::{RngCore, CryptoRng, SeedableRng};
207207
pub use rand_core::{ErrorKind, Error};
208208

209209
// Public exports

src/prng/chacha.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! The ChaCha random number generator.
1212
1313
use core::fmt;
14-
use rand_core::{BlockRngCore, CryptoRng, RngCore, SeedableRng, Error, le};
15-
use rand_core::impls::BlockRng;
14+
use rand_core::{CryptoRng, RngCore, SeedableRng, Error, le};
15+
use rand_core::block::{BlockRngCore, BlockRng};
1616

1717
const SEED_WORDS: usize = 8; // 8 words for the 256-bit key
1818
const STATE_WORDS: usize = 16;

src/prng/hc128.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! The HC-128 random number generator.
1212
1313
use core::fmt;
14-
use rand_core::{BlockRngCore, CryptoRng, RngCore, SeedableRng, Error, le};
15-
use rand_core::impls::BlockRng;
14+
use rand_core::{CryptoRng, RngCore, SeedableRng, Error, le};
15+
use rand_core::block::{BlockRngCore, BlockRng};
1616

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

src/prng/isaac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1313
use core::{fmt, slice};
1414
use core::num::Wrapping as w;
15-
use rand_core::{BlockRngCore, RngCore, SeedableRng, Error, le};
16-
use rand_core::impls::BlockRng;
15+
use rand_core::{RngCore, SeedableRng, Error, le};
16+
use rand_core::block::{BlockRngCore, BlockRng};
1717
use prng::isaac_array::IsaacArray;
1818

1919
#[allow(non_camel_case_types)]

src/prng/isaac64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1313
use core::{fmt, slice};
1414
use core::num::Wrapping as w;
15-
use rand_core::{BlockRngCore, RngCore, SeedableRng, Error, le};
16-
use rand_core::impls::BlockRng64;
15+
use rand_core::{RngCore, SeedableRng, Error, le};
16+
use rand_core::block::{BlockRngCore, BlockRng64};
1717
use prng::isaac_array::IsaacArray;
1818

1919
#[allow(non_camel_case_types)]

src/reseeding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
1414
use core::mem::size_of;
1515

16-
use rand_core::{RngCore, BlockRngCore, CryptoRng, SeedableRng, Error, ErrorKind};
17-
use rand_core::impls::BlockRng;
16+
use rand_core::{RngCore, CryptoRng, SeedableRng, Error, ErrorKind};
17+
use rand_core::block::{BlockRngCore, BlockRng};
1818

1919
/// A wrapper around any PRNG which reseeds the underlying PRNG after it has
2020
/// generated a certain number of random bytes.

0 commit comments

Comments
 (0)