2017-12-15 11:19:40 +00:00
|
|
|
// Copyright 2013-2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
2018-01-09 08:39:55 +02:00
|
|
|
// https://rust-lang.org/COPYRIGHT.
|
2017-12-15 11:19:40 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
2018-01-09 08:39:55 +02:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
2017-12-15 11:19:40 +00:00
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-01-25 17:34:28 +00:00
|
|
|
//! Helper functions for implementing `RngCore` functions.
|
2018-01-09 08:35:22 +02:00
|
|
|
//!
|
2017-12-15 11:19:40 +00:00
|
|
|
//! For cross-platform reproducibility, these functions all use Little Endian:
|
|
|
|
//! least-significant part first. For example, `next_u64_via_u32` takes `u32`
|
|
|
|
//! values `x, y`, then outputs `(y << 32) | x`. To implement `next_u32`
|
|
|
|
//! from `next_u64` in little-endian order, one should use `next_u64() as u32`.
|
2018-01-09 08:35:22 +02:00
|
|
|
//!
|
2017-12-15 11:19:40 +00:00
|
|
|
//! Byte-swapping (like the std `to_le` functions) is only needed to convert
|
|
|
|
//! to/from byte sequences, and since its purpose is reproducibility,
|
|
|
|
//! non-reproducible sources (e.g. `OsRng`) need not bother with it.
|
|
|
|
|
|
|
|
use core::intrinsics::transmute;
|
2018-01-20 19:55:18 +01:00
|
|
|
use core::ptr::copy_nonoverlapping;
|
2018-05-09 12:06:37 +01:00
|
|
|
use core::slice;
|
2017-12-15 11:19:40 +00:00
|
|
|
use core::cmp::min;
|
|
|
|
use core::mem::size_of;
|
2018-05-09 12:06:37 +01:00
|
|
|
use RngCore;
|
2017-12-15 11:19:40 +00:00
|
|
|
|
2018-03-24 07:39:41 +01:00
|
|
|
|
2017-12-15 11:19:40 +00:00
|
|
|
/// Implement `next_u64` via `next_u32`, little-endian order.
|
2018-01-25 17:34:28 +00:00
|
|
|
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
|
2017-12-15 11:19:40 +00:00
|
|
|
// Use LE; we explicitly generate one value before the next.
|
2018-03-27 14:54:05 +02:00
|
|
|
let x = u64::from(rng.next_u32());
|
|
|
|
let y = u64::from(rng.next_u32());
|
2017-12-15 11:19:40 +00:00
|
|
|
(y << 32) | x
|
|
|
|
}
|
|
|
|
|
2018-04-13 13:57:22 +02:00
|
|
|
/// Implement `fill_bytes` via `next_u64` and `next_u32`, little-endian order.
|
|
|
|
///
|
|
|
|
/// The fastest way to fill a slice is usually to work as long as possible with
|
|
|
|
/// integers. That is why this method mostly uses `next_u64`, and only when
|
|
|
|
/// there are 4 or less bytes remaining at the end of the slice it uses
|
|
|
|
/// `next_u32` once.
|
|
|
|
pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
|
|
|
|
let mut left = dest;
|
|
|
|
while left.len() >= 8 {
|
|
|
|
let (l, r) = {left}.split_at_mut(8);
|
|
|
|
left = r;
|
|
|
|
let chunk: [u8; 8] = unsafe {
|
|
|
|
transmute(rng.next_u64().to_le())
|
|
|
|
};
|
|
|
|
l.copy_from_slice(&chunk);
|
|
|
|
}
|
|
|
|
let n = left.len();
|
|
|
|
if n > 4 {
|
|
|
|
let chunk: [u8; 8] = unsafe {
|
|
|
|
transmute(rng.next_u64().to_le())
|
|
|
|
};
|
|
|
|
left.copy_from_slice(&chunk[..n]);
|
|
|
|
} else if n > 0 {
|
|
|
|
let chunk: [u8; 4] = unsafe {
|
|
|
|
transmute(rng.next_u32().to_le())
|
|
|
|
};
|
|
|
|
left.copy_from_slice(&chunk[..n]);
|
|
|
|
}
|
2017-12-15 11:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_uint_from_fill {
|
2017-12-15 12:36:34 +00:00
|
|
|
($rng:expr, $ty:ty, $N:expr) => ({
|
2017-12-15 11:19:40 +00:00
|
|
|
debug_assert!($N == size_of::<$ty>());
|
|
|
|
|
|
|
|
let mut int: $ty = 0;
|
|
|
|
unsafe {
|
|
|
|
let ptr = &mut int as *mut $ty as *mut u8;
|
|
|
|
let slice = slice::from_raw_parts_mut(ptr, $N);
|
2017-12-15 12:36:34 +00:00
|
|
|
$rng.fill_bytes(slice);
|
2017-12-15 11:19:40 +00:00
|
|
|
}
|
|
|
|
int
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! fill_via_chunks {
|
2018-01-21 14:04:49 +01:00
|
|
|
($src:expr, $dst:expr, $ty:ty, $size:expr) => ({
|
2018-01-20 19:55:18 +01:00
|
|
|
let chunk_size_u8 = min($src.len() * $size, $dst.len());
|
|
|
|
let chunk_size = (chunk_size_u8 + $size - 1) / $size;
|
|
|
|
if cfg!(target_endian="little") {
|
|
|
|
unsafe {
|
|
|
|
copy_nonoverlapping(
|
|
|
|
$src.as_ptr() as *const u8,
|
|
|
|
$dst.as_mut_ptr(),
|
|
|
|
chunk_size_u8);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (&n, chunk) in $src.iter().zip($dst.chunks_mut($size)) {
|
2018-01-21 14:04:49 +01:00
|
|
|
let tmp = n.to_le();
|
|
|
|
let src_ptr = &tmp as *const $ty as *const u8;
|
2018-01-20 19:55:18 +01:00
|
|
|
unsafe {
|
2018-01-21 14:04:49 +01:00
|
|
|
copy_nonoverlapping(src_ptr,
|
2018-01-20 19:55:18 +01:00
|
|
|
chunk.as_mut_ptr(),
|
|
|
|
chunk.len());
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 11:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(chunk_size, chunk_size_u8)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement `fill_bytes` by reading chunks from the output buffer of a block
|
|
|
|
/// based RNG.
|
|
|
|
///
|
|
|
|
/// The return values are `(consumed_u32, filled_u8)`.
|
|
|
|
///
|
|
|
|
/// `filled_u8` is the number of filled bytes in `dest`, which may be less than
|
|
|
|
/// the length of `dest`.
|
|
|
|
/// `consumed_u32` is the number of words consumed from `src`, which is the same
|
|
|
|
/// as `filled_u8 / 4` rounded up.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// (from `IsaacRng`)
|
|
|
|
///
|
2018-05-11 08:54:14 +02:00
|
|
|
/// ```ignore
|
2017-12-15 11:19:40 +00:00
|
|
|
/// fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
/// let mut read_len = 0;
|
|
|
|
/// while read_len < dest.len() {
|
|
|
|
/// if self.index >= self.rsl.len() {
|
|
|
|
/// self.isaac();
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let (consumed_u32, filled_u8) =
|
|
|
|
/// impls::fill_via_u32_chunks(&mut self.rsl[self.index..],
|
|
|
|
/// &mut dest[read_len..]);
|
|
|
|
///
|
|
|
|
/// self.index += consumed_u32;
|
|
|
|
/// read_len += filled_u8;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-01-20 19:55:18 +01:00
|
|
|
pub fn fill_via_u32_chunks(src: &[u32], dest: &mut [u8]) -> (usize, usize) {
|
2018-01-21 14:04:49 +01:00
|
|
|
fill_via_chunks!(src, dest, u32, 4)
|
2017-12-15 11:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement `fill_bytes` by reading chunks from the output buffer of a block
|
|
|
|
/// based RNG.
|
|
|
|
///
|
|
|
|
/// The return values are `(consumed_u64, filled_u8)`.
|
|
|
|
/// `filled_u8` is the number of filled bytes in `dest`, which may be less than
|
|
|
|
/// the length of `dest`.
|
|
|
|
/// `consumed_u64` is the number of words consumed from `src`, which is the same
|
|
|
|
/// as `filled_u8 / 8` rounded up.
|
|
|
|
///
|
|
|
|
/// See `fill_via_u32_chunks` for an example.
|
2018-01-20 19:55:18 +01:00
|
|
|
pub fn fill_via_u64_chunks(src: &[u64], dest: &mut [u8]) -> (usize, usize) {
|
2018-01-21 14:04:49 +01:00
|
|
|
fill_via_chunks!(src, dest, u64, 8)
|
2017-12-15 11:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement `next_u32` via `fill_bytes`, little-endian order.
|
2018-01-25 17:34:28 +00:00
|
|
|
pub fn next_u32_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u32 {
|
2017-12-15 11:19:40 +00:00
|
|
|
impl_uint_from_fill!(rng, u32, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement `next_u64` via `fill_bytes`, little-endian order.
|
2018-01-25 17:34:28 +00:00
|
|
|
pub fn next_u64_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
|
2017-12-15 11:19:40 +00:00
|
|
|
impl_uint_from_fill!(rng, u64, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: implement tests for the above
|