1 // Copyright 2018 Developers of the Rand project.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 //! The `BlockRngCore` trait and implementation helpers
11 //! The [`BlockRngCore`] trait exists to assist in the implementation of RNGs
12 //! which generate a block of data in a cache instead of returning generated
15 //! Usage of this trait is optional, but provides two advantages:
16 //! implementations only need to concern themselves with generation of the
17 //! block, not the various [`RngCore`] methods (especially [`fill_bytes`], where
18 //! the optimal implementations are not trivial), and this allows
19 //! `ReseedingRng` (see [`rand`](https://docs.rs/rand) crate) perform periodic
20 //! reseeding with very low overhead.
25 //! use rand_core::{RngCore, SeedableRng};
26 //! use rand_core::block::{BlockRngCore, BlockRng};
30 //! impl BlockRngCore for MyRngCore {
32 //! type Results = [u32; 16];
34 //! fn generate(&mut self, results: &mut Self::Results) {
39 //! impl SeedableRng for MyRngCore {
40 //! type Seed = [u8; 32];
41 //! fn from_seed(seed: Self::Seed) -> Self {
46 //! // optionally, also implement CryptoRng for MyRngCore
49 //! let mut rng = BlockRng::<MyRngCore>::seed_from_u64(0);
50 //! println!("First value: {}", rng.next_u32());
53 //! [`BlockRngCore`]: crate::block::BlockRngCore
54 //! [`fill_bytes`]: RngCore::fill_bytes
56 use crate::impls
::{fill_via_u32_chunks, fill_via_u64_chunks}
;
57 use crate::{CryptoRng, Error, RngCore, SeedableRng}
;
58 use core
::convert
::AsRef
;
60 #[cfg(feature = "serde1")]
61 use serde
::{Deserialize, Serialize}
;
63 /// A trait for RNGs which do not generate random numbers individually, but in
64 /// blocks (typically `[u32; N]`). This technique is commonly used by
65 /// cryptographic RNGs to improve performance.
67 /// See the [module][crate::block] documentation for details.
68 pub trait BlockRngCore
{
69 /// Results element type, e.g. `u32`.
72 /// Results type. This is the 'block' an RNG implementing `BlockRngCore`
73 /// generates, which will usually be an array like `[u32; 16]`.
74 type Results
: AsRef
<[Self::Item
]> + AsMut
<[Self::Item
]> + Default
;
76 /// Generate a new block of results.
77 fn generate(&mut self, results
: &mut Self::Results
);
80 /// A wrapper type implementing [`RngCore`] for some type implementing
81 /// [`BlockRngCore`] with `u32` array buffer; i.e. this can be used to implement
82 /// a full RNG from just a `generate` function.
84 /// The `core` field may be accessed directly but the results buffer may not.
85 /// PRNG implementations can simply use a type alias
86 /// (`pub type MyRng = BlockRng<MyRngCore>;`) but might prefer to use a
87 /// wrapper type (`pub struct MyRng(BlockRng<MyRngCore>);`); the latter must
88 /// re-implement `RngCore` but hides the implementation details and allows
89 /// extra functionality to be defined on the RNG
90 /// (e.g. `impl MyRng { fn set_stream(...){...} }`).
92 /// `BlockRng` has heavily optimized implementations of the [`RngCore`] methods
93 /// reading values from the results buffer, as well as
94 /// calling [`BlockRngCore::generate`] directly on the output array when
95 /// [`fill_bytes`] / [`try_fill_bytes`] is called on a large array. These methods
96 /// also handle the bookkeeping of when to generate a new batch of values.
98 /// No whole generated `u32` values are thown away and all values are consumed
99 /// in-order. [`next_u32`] simply takes the next available `u32` value.
100 /// [`next_u64`] is implemented by combining two `u32` values, least
101 /// significant first. [`fill_bytes`] and [`try_fill_bytes`] consume a whole
102 /// number of `u32` values, converting each `u32` to a byte slice in
103 /// little-endian order. If the requested byte length is not a multiple of 4,
104 /// some bytes will be discarded.
106 /// See also [`BlockRng64`] which uses `u64` array buffers. Currently there is
107 /// no direct support for other buffer types.
109 /// For easy initialization `BlockRng` also implements [`SeedableRng`].
111 /// [`next_u32`]: RngCore::next_u32
112 /// [`next_u64`]: RngCore::next_u64
113 /// [`fill_bytes`]: RngCore::fill_bytes
114 /// [`try_fill_bytes`]: RngCore::try_fill_bytes
116 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
117 pub struct BlockRng
<R
: BlockRngCore
+ ?Sized
> {
120 /// The *core* part of the RNG, implementing the `generate` function.
124 // Custom Debug implementation that does not expose the contents of `results`.
125 impl<R
: BlockRngCore
+ fmt
::Debug
> fmt
::Debug
for BlockRng
<R
> {
126 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
127 fmt
.debug_struct("BlockRng")
128 .field("core", &self.core
)
129 .field("result_len", &self.results
.as_ref().len())
130 .field("index", &self.index
)
135 impl<R
: BlockRngCore
> BlockRng
<R
> {
136 /// Create a new `BlockRng` from an existing RNG implementing
137 /// `BlockRngCore`. Results will be generated on first use.
139 pub fn new(core
: R
) -> BlockRng
<R
> {
140 let results_empty
= R
::Results
::default();
143 index
: results_empty
.as_ref().len(),
144 results
: results_empty
,
148 /// Get the index into the result buffer.
150 /// If this is equal to or larger than the size of the result buffer then
151 /// the buffer is "empty" and `generate()` must be called to produce new
154 pub fn index(&self) -> usize {
158 /// Reset the number of available results.
159 /// This will force a new set of results to be generated on next use.
161 pub fn reset(&mut self) {
162 self.index
= self.results
.as_ref().len();
165 /// Generate a new set of results immediately, setting the index to the
168 pub fn generate_and_set(&mut self, index
: usize) {
169 assert
!(index
< self.results
.as_ref().len());
170 self.core
.generate(&mut self.results
);
175 impl<R
: BlockRngCore
<Item
= u32>> RngCore
for BlockRng
<R
>
177 <R
as BlockRngCore
>::Results
: AsRef
<[u32]> + AsMut
<[u32]>,
180 fn next_u32(&mut self) -> u32 {
181 if self.index
>= self.results
.as_ref().len() {
182 self.generate_and_set(0);
185 let value
= self.results
.as_ref()[self.index
];
191 fn next_u64(&mut self) -> u64 {
192 let read_u64
= |results
: &[u32], index
| {
193 let data
= &results
[index
..=index
+ 1];
194 u64::from(data
[1]) << 32 | u64::from(data
[0])
197 let len
= self.results
.as_ref().len();
199 let index
= self.index
;
202 // Read an u64 from the current index
203 read_u64(self.results
.as_ref(), index
)
204 } else if index
>= len
{
205 self.generate_and_set(2);
206 read_u64(self.results
.as_ref(), 0)
208 let x
= u64::from(self.results
.as_ref()[len
- 1]);
209 self.generate_and_set(1);
210 let y
= u64::from(self.results
.as_ref()[0]);
216 fn fill_bytes(&mut self, dest
: &mut [u8]) {
217 let mut read_len
= 0;
218 while read_len
< dest
.len() {
219 if self.index
>= self.results
.as_ref().len() {
220 self.generate_and_set(0);
222 let (consumed_u32
, filled_u8
) =
223 fill_via_u32_chunks(&self.results
.as_ref()[self.index
..], &mut dest
[read_len
..]);
225 self.index
+= consumed_u32
;
226 read_len
+= filled_u8
;
231 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
232 self.fill_bytes(dest
);
237 impl<R
: BlockRngCore
+ SeedableRng
> SeedableRng
for BlockRng
<R
> {
241 fn from_seed(seed
: Self::Seed
) -> Self {
242 Self::new(R
::from_seed(seed
))
246 fn seed_from_u64(seed
: u64) -> Self {
247 Self::new(R
::seed_from_u64(seed
))
251 fn from_rng
<S
: RngCore
>(rng
: S
) -> Result
<Self, Error
> {
252 Ok(Self::new(R
::from_rng(rng
)?
))
256 /// A wrapper type implementing [`RngCore`] for some type implementing
257 /// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
258 /// a full RNG from just a `generate` function.
260 /// This is similar to [`BlockRng`], but specialized for algorithms that operate
263 /// No whole generated `u64` values are thrown away and all values are consumed
264 /// in-order. [`next_u64`] simply takes the next available `u64` value.
265 /// [`next_u32`] is however a bit special: half of a `u64` is consumed, leaving
266 /// the other half in the buffer. If the next function called is [`next_u32`]
267 /// then the other half is then consumed, however both [`next_u64`] and
268 /// [`fill_bytes`] discard the rest of any half-consumed `u64`s when called.
270 /// [`fill_bytes`] and [`try_fill_bytes`] consume a whole number of `u64`
271 /// values. If the requested length is not a multiple of 8, some bytes will be
274 /// [`next_u32`]: RngCore::next_u32
275 /// [`next_u64`]: RngCore::next_u64
276 /// [`fill_bytes`]: RngCore::fill_bytes
277 /// [`try_fill_bytes`]: RngCore::try_fill_bytes
279 #[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
280 pub struct BlockRng64
<R
: BlockRngCore
+ ?Sized
> {
283 half_used
: bool
, // true if only half of the previous result is used
284 /// The *core* part of the RNG, implementing the `generate` function.
288 // Custom Debug implementation that does not expose the contents of `results`.
289 impl<R
: BlockRngCore
+ fmt
::Debug
> fmt
::Debug
for BlockRng64
<R
> {
290 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
291 fmt
.debug_struct("BlockRng64")
292 .field("core", &self.core
)
293 .field("result_len", &self.results
.as_ref().len())
294 .field("index", &self.index
)
295 .field("half_used", &self.half_used
)
300 impl<R
: BlockRngCore
> BlockRng64
<R
> {
301 /// Create a new `BlockRng` from an existing RNG implementing
302 /// `BlockRngCore`. Results will be generated on first use.
304 pub fn new(core
: R
) -> BlockRng64
<R
> {
305 let results_empty
= R
::Results
::default();
308 index
: results_empty
.as_ref().len(),
310 results
: results_empty
,
314 /// Get the index into the result buffer.
316 /// If this is equal to or larger than the size of the result buffer then
317 /// the buffer is "empty" and `generate()` must be called to produce new
320 pub fn index(&self) -> usize {
324 /// Reset the number of available results.
325 /// This will force a new set of results to be generated on next use.
327 pub fn reset(&mut self) {
328 self.index
= self.results
.as_ref().len();
329 self.half_used
= false;
332 /// Generate a new set of results immediately, setting the index to the
335 pub fn generate_and_set(&mut self, index
: usize) {
336 assert
!(index
< self.results
.as_ref().len());
337 self.core
.generate(&mut self.results
);
339 self.half_used
= false;
343 impl<R
: BlockRngCore
<Item
= u64>> RngCore
for BlockRng64
<R
>
345 <R
as BlockRngCore
>::Results
: AsRef
<[u64]> + AsMut
<[u64]>,
348 fn next_u32(&mut self) -> u32 {
349 let mut index
= self.index
* 2 - self.half_used
as usize;
350 if index
>= self.results
.as_ref().len() * 2 {
351 self.core
.generate(&mut self.results
);
353 // `self.half_used` is by definition `false`
354 self.half_used
= false;
358 self.half_used
= !self.half_used
;
359 self.index
+= self.half_used
as usize;
361 // Index as if this is a u32 slice.
363 let results
= &*(self.results
.as_ref() as *const [u64] as *const [u32]);
364 if cfg
!(target_endian
= "little") {
365 *results
.get_unchecked(index
)
367 *results
.get_unchecked(index ^
1)
373 fn next_u64(&mut self) -> u64 {
374 if self.index
>= self.results
.as_ref().len() {
375 self.core
.generate(&mut self.results
);
379 let value
= self.results
.as_ref()[self.index
];
381 self.half_used
= false;
386 fn fill_bytes(&mut self, dest
: &mut [u8]) {
387 let mut read_len
= 0;
388 self.half_used
= false;
389 while read_len
< dest
.len() {
390 if self.index
as usize >= self.results
.as_ref().len() {
391 self.core
.generate(&mut self.results
);
395 let (consumed_u64
, filled_u8
) = fill_via_u64_chunks(
396 &self.results
.as_ref()[self.index
as usize..],
397 &mut dest
[read_len
..],
400 self.index
+= consumed_u64
;
401 read_len
+= filled_u8
;
406 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
407 self.fill_bytes(dest
);
412 impl<R
: BlockRngCore
+ SeedableRng
> SeedableRng
for BlockRng64
<R
> {
416 fn from_seed(seed
: Self::Seed
) -> Self {
417 Self::new(R
::from_seed(seed
))
421 fn seed_from_u64(seed
: u64) -> Self {
422 Self::new(R
::seed_from_u64(seed
))
426 fn from_rng
<S
: RngCore
>(rng
: S
) -> Result
<Self, Error
> {
427 Ok(Self::new(R
::from_rng(rng
)?
))
431 impl<R
: BlockRngCore
+ CryptoRng
> CryptoRng
for BlockRng
<R
> {}