summaryrefslogtreecommitdiff
path: root/rand/rand_xoshiro/src
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-16 17:26:30 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-16 17:26:30 -0800
commit8350ac6afb2d678b74581000a6aafe1994b72231 (patch)
tree2330da01a806921b3849c9e64d2b9f506495e2c0 /rand/rand_xoshiro/src
parentd6652b913b33e432a748187f9f5623cec1e9926e (diff)
downloadnitrocli-8350ac6afb2d678b74581000a6aafe1994b72231.tar.gz
nitrocli-8350ac6afb2d678b74581000a6aafe1994b72231.tar.bz2
Update nitrokey crate to 0.3.3
This change updates the nitrokey crate to version 0.3.3. Along with that change we update rand to 0.6.4 because rand 0.6.1 does not yet contain a publicly accessible rand_os. Note that we no longer require all crates in rand's workspace, but only rand_os and rand_core, which is a significant reduction in the number of lines of code compiled. Import subrepo nitrokey/:nitrokey at 7cf747d56ddc0b7eeedc3caf36dcc909907a171c Import subrepo rand/:rand at 4336232dda03323634b10ec72ddf27914aebc3a2
Diffstat (limited to 'rand/rand_xoshiro/src')
-rw-r--r--rand/rand_xoshiro/src/common.rs243
-rw-r--r--rand/rand_xoshiro/src/lib.rs106
-rw-r--r--rand/rand_xoshiro/src/splitmix64.rs150
-rw-r--r--rand/rand_xoshiro/src/xoroshiro128plus.rs132
-rw-r--r--rand/rand_xoshiro/src/xoroshiro128starstar.rs129
-rw-r--r--rand/rand_xoshiro/src/xoroshiro64star.rs97
-rw-r--r--rand/rand_xoshiro/src/xoroshiro64starstar.rs96
-rw-r--r--rand/rand_xoshiro/src/xoshiro128plus.rs114
-rw-r--r--rand/rand_xoshiro/src/xoshiro128starstar.rs113
-rw-r--r--rand/rand_xoshiro/src/xoshiro256plus.rs133
-rw-r--r--rand/rand_xoshiro/src/xoshiro256starstar.rs130
-rw-r--r--rand/rand_xoshiro/src/xoshiro512plus.rs124
-rw-r--r--rand/rand_xoshiro/src/xoshiro512starstar.rs124
13 files changed, 1691 insertions, 0 deletions
diff --git a/rand/rand_xoshiro/src/common.rs b/rand/rand_xoshiro/src/common.rs
new file mode 100644
index 0000000..9ee09e2
--- /dev/null
+++ b/rand/rand_xoshiro/src/common.rs
@@ -0,0 +1,243 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/// Initialize a RNG from a `u64` seed using `SplitMix64`.
+macro_rules! from_splitmix {
+ ($seed:expr) => { {
+ let mut rng = ::SplitMix64::seed_from_u64($seed);
+ Self::from_rng(&mut rng).unwrap()
+ } }
+}
+
+/// Apply the ** scrambler used by some RNGs from the xoshiro family.
+macro_rules! starstar_u64 {
+ ($x:expr) => {
+ $x.wrapping_mul(5).rotate_left(7).wrapping_mul(9)
+ }
+}
+
+/// Apply the ** scrambler used by some RNGs from the xoshiro family.
+macro_rules! starstar_u32 {
+ ($x:expr) => {
+ $x.wrapping_mul(0x9E3779BB).rotate_left(5).wrapping_mul(5)
+ }
+}
+
+/// Implement a jump function for an RNG from the xoshiro family.
+macro_rules! impl_jump {
+ (u32, $self:expr, [$j0:expr, $j1:expr]) => {
+ const JUMP: [u32; 2] = [$j0, $j1];
+ let mut s0 = 0;
+ let mut s1 = 0;
+ for j in &JUMP {
+ for b in 0..32 {
+ if (j & 1 << b) != 0 {
+ s0 ^= $self.s0;
+ s1 ^= $self.s1;
+ }
+ $self.next_u32();
+ }
+ }
+ $self.s0 = s0;
+ $self.s1 = s1;
+ };
+ (u64, $self:expr, [$j0:expr, $j1:expr]) => {
+ const JUMP: [u64; 2] = [$j0, $j1];
+ let mut s0 = 0;
+ let mut s1 = 0;
+ for j in &JUMP {
+ for b in 0..64 {
+ if (j & 1 << b) != 0 {
+ s0 ^= $self.s0;
+ s1 ^= $self.s1;
+ }
+ $self.next_u64();
+ }
+ }
+ $self.s0 = s0;
+ $self.s1 = s1;
+ };
+ (u32, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr]) => {
+ const JUMP: [u32; 4] = [$j0, $j1, $j2, $j3];
+ let mut s0 = 0;
+ let mut s1 = 0;
+ let mut s2 = 0;
+ let mut s3 = 0;
+ for j in &JUMP {
+ for b in 0..32 {
+ if (j & 1 << b) != 0 {
+ s0 ^= $self.s[0];
+ s1 ^= $self.s[1];
+ s2 ^= $self.s[2];
+ s3 ^= $self.s[3];
+ }
+ $self.next_u32();
+ }
+ }
+ $self.s[0] = s0;
+ $self.s[1] = s1;
+ $self.s[2] = s2;
+ $self.s[3] = s3;
+ };
+ (u64, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr]) => {
+ const JUMP: [u64; 4] = [$j0, $j1, $j2, $j3];
+ let mut s0 = 0;
+ let mut s1 = 0;
+ let mut s2 = 0;
+ let mut s3 = 0;
+ for j in &JUMP {
+ for b in 0..64 {
+ if (j & 1 << b) != 0 {
+ s0 ^= $self.s[0];
+ s1 ^= $self.s[1];
+ s2 ^= $self.s[2];
+ s3 ^= $self.s[3];
+ }
+ $self.next_u64();
+ }
+ }
+ $self.s[0] = s0;
+ $self.s[1] = s1;
+ $self.s[2] = s2;
+ $self.s[3] = s3;
+ };
+ (u64, $self:expr, [$j0:expr, $j1:expr, $j2:expr, $j3:expr,
+ $j4:expr, $j5:expr, $j6:expr, $j7:expr]) => {
+ const JUMP: [u64; 8] = [$j0, $j1, $j2, $j3, $j4, $j5, $j6, $j7];
+ let mut s = [0; 8];
+ for j in &JUMP {
+ for b in 0..64 {
+ if (j & 1 << b) != 0 {
+ s[0] ^= $self.s[0];
+ s[1] ^= $self.s[1];
+ s[2] ^= $self.s[2];
+ s[3] ^= $self.s[3];
+ s[4] ^= $self.s[4];
+ s[5] ^= $self.s[5];
+ s[6] ^= $self.s[6];
+ s[7] ^= $self.s[7];
+ }
+ $self.next_u64();
+ }
+ }
+ $self.s = s;
+ };
+}
+
+/// Implement the xoroshiro iteration.
+macro_rules! impl_xoroshiro_u32 {
+ ($self:expr) => {
+ $self.s1 ^= $self.s0;
+ $self.s0 = $self.s0.rotate_left(26) ^ $self.s1 ^ ($self.s1 << 9);
+ $self.s1 = $self.s1.rotate_left(13);
+ }
+}
+
+/// Implement the xoroshiro iteration.
+macro_rules! impl_xoroshiro_u64 {
+ ($self:expr) => {
+ $self.s1 ^= $self.s0;
+ $self.s0 = $self.s0.rotate_left(24) ^ $self.s1 ^ ($self.s1 << 16);
+ $self.s1 = $self.s1.rotate_left(37);
+ }
+}
+
+/// Implement the xoshiro iteration for `u32` output.
+macro_rules! impl_xoshiro_u32 {
+ ($self:expr) => {
+ let t = $self.s[1] << 9;
+
+ $self.s[2] ^= $self.s[0];
+ $self.s[3] ^= $self.s[1];
+ $self.s[1] ^= $self.s[2];
+ $self.s[0] ^= $self.s[3];
+
+ $self.s[2] ^= t;
+
+ $self.s[3] = $self.s[3].rotate_left(11);
+ }
+}
+
+/// Implement the xoshiro iteration for `u64` output.
+macro_rules! impl_xoshiro_u64 {
+ ($self:expr) => {
+ let t = $self.s[1] << 17;
+
+ $self.s[2] ^= $self.s[0];
+ $self.s[3] ^= $self.s[1];
+ $self.s[1] ^= $self.s[2];
+ $self.s[0] ^= $self.s[3];
+
+ $self.s[2] ^= t;
+
+ $self.s[3] = $self.s[3].rotate_left(45);
+ }
+}
+
+/// Implement the large-state xoshiro iteration.
+macro_rules! impl_xoshiro_large {
+ ($self:expr) => {
+ let t = $self.s[1] << 11;
+
+ $self.s[2] ^= $self.s[0];
+ $self.s[5] ^= $self.s[1];
+ $self.s[1] ^= $self.s[2];
+ $self.s[7] ^= $self.s[3];
+ $self.s[3] ^= $self.s[4];
+ $self.s[4] ^= $self.s[5];
+ $self.s[0] ^= $self.s[6];
+ $self.s[6] ^= $self.s[7];
+
+ $self.s[6] ^= t;
+
+ $self.s[7] = $self.s[7].rotate_left(21);
+ }
+}
+
+/// Map an all-zero seed to a different one.
+macro_rules! deal_with_zero_seed {
+ ($seed:expr, $Self:ident) => {
+ if $seed.iter().all(|&x| x == 0) {
+ return $Self::seed_from_u64(0);
+ }
+ }
+}
+
+/// 512-bit seed for a generator.
+///
+/// This wrapper is necessary, because some traits required for a seed are not
+/// implemented on large arrays.
+#[derive(Clone)]
+pub struct Seed512(pub [u8; 64]);
+
+use core;
+impl Seed512 {
+ /// Return an iterator over the seed.
+ pub fn iter(&self) -> core::slice::Iter<u8> {
+ self.0.iter()
+ }
+}
+
+impl core::fmt::Debug for Seed512 {
+ fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
+ self.0[..].fmt(f)
+ }
+}
+
+impl Default for Seed512 {
+ fn default() -> Seed512 {
+ Seed512([0; 64])
+ }
+}
+
+impl AsMut<[u8]> for Seed512 {
+ fn as_mut(&mut self) -> &mut [u8] {
+ &mut self.0
+ }
+}
+
diff --git a/rand/rand_xoshiro/src/lib.rs b/rand/rand_xoshiro/src/lib.rs
new file mode 100644
index 0000000..634db31
--- /dev/null
+++ b/rand/rand_xoshiro/src/lib.rs
@@ -0,0 +1,106 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! This crate implements the [xoshiro] family of pseudorandom number generators
+//! designed by David Blackman and Sebastiano Vigna. They feature high
+//! perfomance and a small state and superseed the previous xorshift-based
+//! generators. However, they are no cryptographically secure and their output
+//! can be predicted by observing a few samples.
+//!
+//! The following generators are implemented:
+//!
+//! # 64-bit generators
+//! - [`Xoshiro256StarStar`]: Recommended for all purposes. Excellent speed and
+//! a state space (256 bits) large enough for any parallel application.
+//! - [`Xoshiro256Plus`]: Recommended for generating 64-bit floating-point
+//! numbers. About 15% faster than `Xoshiro256StarStar`, but has a [low linear
+//! complexity] in the lowest bits (which are discarded when generating
+//! floats), making it fail linearity tests. This is unlikely to have any
+//! impact in practise.
+//! - [`Xoroshiro128StarStar`]: An alternative to `Xoshiro256StarStar`, having
+//! the same speed but using half the state. Only suited for low-scale parallel
+//! applications.
+//! - [`Xoroshiro128Plus`]: An alternative to `Xoshiro256Plus`, having the same
+//! speed but using half the state. Only suited for low-scale parallel
+//! applications. Has a [low linear complexity] in the lowest bits (which are
+//! discarded when generating floats), making it fail linearity tests. This is
+//! unlikely to have any impact in practise.
+//! - [`Xoshiro512StarStar`]: An alternative to `Xoshiro256StarStar` with more
+//! state and the same speed.
+//! - [`Xoshiro512Plus`]: An alternative to `Xoshiro512Plus` with more
+//! state and the same speed. Has a [low linear complexity] in the lowest bits
+//! (which are discarded when generating floats), making it fail linearity
+//! tests. This is unlikely to have any impact in practise.
+//! - [`SplitMix64`]: Recommended for initializing generators of the xoshiro
+//! familiy from a 64-bit seed. Used for implementing `seed_from_u64`.
+//!
+//! # 32-bit generators
+//! - [`Xoshiro128StarStar`]: Recommended for all purposes. Excellent speed.
+//! - [`Xoshiro128Plus`]: Recommended for generating 32-bit floating-point
+//! numbers. Faster than `Xoshiro128StarStar`, but has a [low linear
+//! complexity] in the lowest bits (which are discarded when generating
+//! floats), making it fail linearity tests. This is unlikely to have any
+//! impact in practise.
+//! - [`Xoroshiro64StarStar`]: An alternative to `Xoshiro128StarStar`, having
+//! the same speed but using half the state.
+//! - [`Xoroshiro64Star`]: An alternative to `Xoshiro128Plus`, having the
+//! same speed but using half the state. Has a [low linear complexity] in the
+//! lowest bits (which are discarded when generating floats), making it fail
+//! linearity tests. This is unlikely to have any impact in practise.
+//!
+//! [xoshiro]: http://xoshiro.di.unimi.it/
+//! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php
+//! [`Xoshiro256StarStar`]: ./struct.Xoshiro256StarStar.html
+//! [`Xoshiro256Plus`]: ./struct.Xoshiro256Plus.html
+//! [`Xoroshiro128StarStar`]: ./struct.Xoroshiro128StarStar.html
+//! [`Xoroshiro128Plus`]: ./struct.Xoroshiro128Plus.html
+//! [`Xoshiro512StarStar`]: ./struct.Xoshiro512StarStar.html
+//! [`Xoshiro512Plus`]: ./struct.Xoshiro512Plus.html
+//! [`SplitMix64`]: ./struct.SplitMix64.html
+//! [`Xoshiro128StarStar`]: ./struct.Xoshiro128StarStar.html
+//! [`Xoshiro128Plus`]: ./struct.Xoshiro128Plus.html
+//! [`Xoroshiro64StarStar`]: ./struct.Xoroshiro64StarStar.html
+//! [`Xoroshiro64Star`]: ./struct.Xoroshiro64Star.html
+
+#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
+ html_favicon_url = "https://www.rust-lang.org/favicon.ico",
+ html_root_url = "https://docs.rs/rand_xoshiro/0.1.0")]
+
+#![deny(missing_docs)]
+#![deny(missing_debug_implementations)]
+#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
+#![no_std]
+extern crate byteorder;
+pub extern crate rand_core;
+
+#[macro_use]
+mod common;
+mod splitmix64;
+mod xoshiro128starstar;
+mod xoshiro128plus;
+mod xoshiro256starstar;
+mod xoshiro256plus;
+mod xoshiro512starstar;
+mod xoshiro512plus;
+mod xoroshiro128plus;
+mod xoroshiro128starstar;
+mod xoroshiro64starstar;
+mod xoroshiro64star;
+
+pub use splitmix64::SplitMix64;
+pub use xoshiro128starstar::Xoshiro128StarStar;
+pub use xoshiro128plus::Xoshiro128Plus;
+pub use xoshiro256starstar::Xoshiro256StarStar;
+pub use xoshiro256plus::Xoshiro256Plus;
+pub use common::Seed512;
+pub use xoshiro512starstar::Xoshiro512StarStar;
+pub use xoshiro512plus::Xoshiro512Plus;
+pub use xoroshiro128plus::Xoroshiro128Plus;
+pub use xoroshiro128starstar::Xoroshiro128StarStar;
+pub use xoroshiro64starstar::Xoroshiro64StarStar;
+pub use xoroshiro64star::Xoroshiro64Star;
diff --git a/rand/rand_xoshiro/src/splitmix64.rs b/rand/rand_xoshiro/src/splitmix64.rs
new file mode 100644
index 0000000..a7cac9f
--- /dev/null
+++ b/rand/rand_xoshiro/src/splitmix64.rs
@@ -0,0 +1,150 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use byteorder::{ByteOrder, LittleEndian};
+use rand_core::le::read_u64_into;
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::{RngCore, SeedableRng, Error};
+
+/// A splitmix64 random number generator.
+///
+/// The splitmix algorithm is not suitable for cryptographic purposes, but is
+/// very fast and has a 64 bit state.
+///
+/// The algorithm used here is translated from [the `splitmix64.c`
+/// reference source code](http://xoshiro.di.unimi.it/splitmix64.c) by
+/// Sebastiano Vigna. For `next_u32`, a more efficient mixing function taken
+/// from [`dsiutils`](http://dsiutils.di.unimi.it/) is used.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+pub struct SplitMix64 {
+ x: u64,
+}
+
+const PHI: u64 = 0x9e3779b97f4a7c15;
+
+impl RngCore for SplitMix64 {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.x = self.x.wrapping_add(PHI);
+ let mut z = self.x;
+ // David Stafford's
+ // (http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html)
+ // "Mix4" variant of the 64-bit finalizer in Austin Appleby's
+ // MurmurHash3 algorithm.
+ z = (z ^ (z >> 33)).wrapping_mul(0x62A9D9ED799705F5);
+ z = (z ^ (z >> 28)).wrapping_mul(0xCB24D0A5C88C35B3);
+ (z >> 32) as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ self.x = self.x.wrapping_add(PHI);
+ let mut z = self.x;
+ z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
+ z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
+ z ^ (z >> 31)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for SplitMix64 {
+ type Seed = [u8; 8];
+
+ /// Create a new `SplitMix64`.
+ fn from_seed(seed: [u8; 8]) -> SplitMix64 {
+ let mut state = [0; 1];
+ read_u64_into(&seed, &mut state);
+ SplitMix64 {
+ x: state[0],
+ }
+ }
+
+ /// Seed a `SplitMix64` from a `u64`.
+ fn seed_from_u64(seed: u64) -> SplitMix64 {
+ let mut x = [0; 8];
+ LittleEndian::write_u64(&mut x, seed);
+ SplitMix64::from_seed(x)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = SplitMix64::seed_from_u64(1477776061723855037);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/splitmix64.c
+ let expected : [u64 ; 50]= [
+ 1985237415132408290, 2979275885539914483, 13511426838097143398,
+ 8488337342461049707, 15141737807933549159, 17093170987380407015,
+ 16389528042912955399, 13177319091862933652, 10841969400225389492,
+ 17094824097954834098, 3336622647361835228, 9678412372263018368,
+ 11111587619974030187, 7882215801036322410, 5709234165213761869,
+ 7799681907651786826, 4616320717312661886, 4251077652075509767,
+ 7836757050122171900, 5054003328188417616, 12919285918354108358,
+ 16477564761813870717, 5124667218451240549, 18099554314556827626,
+ 7603784838804469118, 6358551455431362471, 3037176434532249502,
+ 3217550417701719149, 9958699920490216947, 5965803675992506258,
+ 12000828378049868312, 12720568162811471118, 245696019213873792,
+ 8351371993958923852, 14378754021282935786, 5655432093647472106,
+ 5508031680350692005, 8515198786865082103, 6287793597487164412,
+ 14963046237722101617, 3630795823534910476, 8422285279403485710,
+ 10554287778700714153, 10871906555720704584, 8659066966120258468,
+ 9420238805069527062, 10338115333623340156, 13514802760105037173,
+ 14635952304031724449, 15419692541594102413,
+ ];
+ for &e in expected.iter() {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+
+ #[test]
+ fn next_u32() {
+ let mut rng = SplitMix64::seed_from_u64(10);
+ // These values were produced with the reference implementation:
+ // http://dsiutils.di.unimi.it/dsiutils-2.5.1-src.tar.gz
+ let expected : [u32 ; 100]= [
+ 3930361779, 4016923089, 4113052479, 925926767, 1755287528,
+ 802865554, 954171070, 3724185978, 173676273, 1414488795, 12664133,
+ 1784889697, 1303817078, 261610523, 941280008, 2571813643,
+ 2954453492, 378291111, 2546873158, 3923319175, 645257028,
+ 3881821278, 2681538690, 3037029984, 1999958137, 1853970361,
+ 2989951788, 2126166628, 839962987, 3989679659, 3656977858,
+ 684284364, 1673258011, 170979192, 3037622326, 1600748179,
+ 1780764218, 1141430714, 4139736875, 3336905707, 2262051600,
+ 3830850262, 2430765325, 1073032139, 1668888979, 2716938970,
+ 4102420032, 40305196, 386350562, 2754480591, 622869439, 2129598760,
+ 2306038241, 4218338739, 412298926, 3453855056, 3061469690,
+ 4284292697, 994843708, 1591016681, 414726151, 1238182607, 18073498,
+ 1237631493, 351884714, 2347486264, 2488990876, 802846256, 645670443,
+ 957607012, 3126589776, 1966356370, 3036485766, 868696717,
+ 2808613630, 2070968151, 1025536863, 1743949425, 466212687,
+ 2994327271, 209776458, 1246125124, 3344380309, 2203947859,
+ 968313105, 2805485302, 197484837, 3472483632, 3931823935,
+ 3288490351, 4165666529, 3671080416, 689542830, 1272555356,
+ 1039141475, 3984640460, 4142959054, 2252788890, 2459379590,
+ 991872507,
+ ];
+ for &e in expected.iter() {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoroshiro128plus.rs b/rand/rand_xoshiro/src/xoroshiro128plus.rs
new file mode 100644
index 0000000..df032c8
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoroshiro128plus.rs
@@ -0,0 +1,132 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core;
+use rand_core::le::read_u64_into;
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::{RngCore, SeedableRng};
+
+/// A xoroshiro128+ random number generator.
+///
+/// The xoroshiro128+ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has good statistical properties, besides a low linear
+/// complexity in the lowest bits.
+///
+/// The algorithm used here is translated from [the `xoroshiro128plus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128plus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+pub struct Xoroshiro128Plus {
+ s0: u64,
+ s1: u64,
+}
+
+impl Xoroshiro128Plus {
+ /// Jump forward, equivalently to 2^64 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128Plus;
+ ///
+ /// let rng1 = Xoroshiro128Plus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
+ }
+
+ /// Jump forward, equivalently to 2^96 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^32 starting points, from each of which
+ /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
+ }
+}
+
+impl RngCore for Xoroshiro128Plus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ // The two lowest bits have some linear dependencies, so we use the
+ // upper bits instead.
+ (self.next_u64() >> 32) as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let r = self.s0.wrapping_add(self.s1);
+ impl_xoroshiro_u64!(self);
+ r
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for Xoroshiro128Plus {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoroshiro128Plus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ fn from_seed(seed: [u8; 16]) -> Xoroshiro128Plus {
+ deal_with_zero_seed!(seed, Self);
+ let mut s = [0; 2];
+ read_u64_into(&seed, &mut s);
+
+ Xoroshiro128Plus {
+ s0: s[0],
+ s1: s[1],
+ }
+ }
+
+ /// Seed a `Xoroshiro128Plus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoroshiro128Plus {
+ from_splitmix!(seed)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoroshiro128Plus::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128starstar.c
+ let expected = [
+ 3, 412333834243, 2360170716294286339, 9295852285959843169,
+ 2797080929874688578, 6019711933173041966, 3076529664176959358,
+ 3521761819100106140, 7493067640054542992, 920801338098114767,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoroshiro128starstar.rs b/rand/rand_xoshiro/src/xoroshiro128starstar.rs
new file mode 100644
index 0000000..2d27850
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoroshiro128starstar.rs
@@ -0,0 +1,129 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core;
+use rand_core::le::read_u64_into;
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::{RngCore, SeedableRng};
+
+/// A xoroshiro128** random number generator.
+///
+/// The xoroshiro128** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoroshiro128starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoroshiro128starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+pub struct Xoroshiro128StarStar {
+ s0: u64,
+ s1: u64,
+}
+
+impl Xoroshiro128StarStar {
+ /// Jump forward, equivalently to 2^64 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128StarStar;
+ ///
+ /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]);
+ }
+
+ /// Jump forward, equivalently to 2^96 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^32 starting points, from each of which
+ /// `jump()` will generate 2^32 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1]);
+ }
+}
+
+impl RngCore for Xoroshiro128StarStar {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let r = starstar_u64!(self.s0);
+ impl_xoroshiro_u64!(self);
+ r
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for Xoroshiro128StarStar {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoroshiro128StarStar`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ fn from_seed(seed: [u8; 16]) -> Xoroshiro128StarStar {
+ deal_with_zero_seed!(seed, Self);
+ let mut s = [0; 2];
+ read_u64_into(&seed, &mut s);
+
+ Xoroshiro128StarStar {
+ s0: s[0],
+ s1: s[1],
+ }
+ }
+
+ /// Seed a `Xoroshiro128StarStar` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoroshiro128StarStar {
+ from_splitmix!(seed)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoroshiro128StarStar::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128starstar.c
+ let expected = [
+ 5760, 97769243520, 9706862127477703552, 9223447511460779954,
+ 8358291023205304566, 15695619998649302768, 8517900938696309774,
+ 16586480348202605369, 6959129367028440372, 16822147227405758281,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoroshiro64star.rs b/rand/rand_xoshiro/src/xoroshiro64star.rs
new file mode 100644
index 0000000..86338fd
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoroshiro64star.rs
@@ -0,0 +1,97 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use byteorder::{ByteOrder, LittleEndian};
+use rand_core;
+use rand_core::le::read_u32_into;
+use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
+use rand_core::{RngCore, SeedableRng};
+
+/// A xoroshiro64* random number generator.
+///
+/// The xoroshiro64* algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has good statistical properties, besides a low linear
+/// complexity in the lowest bits.
+///
+/// The algorithm used here is translated from [the `xoroshiro64star.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64star.c) by
+/// David Blackman and Sebastiano Vigna.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+pub struct Xoroshiro64Star {
+ s0: u32,
+ s1: u32,
+}
+
+impl RngCore for Xoroshiro64Star {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ let r = self.s0.wrapping_mul(0x9E3779BB);
+ impl_xoroshiro_u32!(self);
+ r
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ next_u64_via_u32(self)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for Xoroshiro64Star {
+ type Seed = [u8; 8];
+
+ /// Create a new `Xoroshiro64Star`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ fn from_seed(seed: [u8; 8]) -> Xoroshiro64Star {
+ deal_with_zero_seed!(seed, Self);
+ let mut s = [0; 2];
+ read_u32_into(&seed, &mut s);
+
+ Xoroshiro64Star {
+ s0: s[0],
+ s1: s[1],
+ }
+ }
+
+ /// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoroshiro64Star {
+ let mut s = [0; 8];
+ LittleEndian::write_u64(&mut s, seed);
+ Xoroshiro64Star::from_seed(s)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoroshiro64Star::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro64star.c
+ let expected = [
+ 2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673,
+ 552743735, 1672597395, 1031040050, 2755315674,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoroshiro64starstar.rs b/rand/rand_xoshiro/src/xoroshiro64starstar.rs
new file mode 100644
index 0000000..a40baee
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoroshiro64starstar.rs
@@ -0,0 +1,96 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use byteorder::{ByteOrder, LittleEndian};
+use rand_core;
+use rand_core::le::read_u32_into;
+use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32};
+use rand_core::{RngCore, SeedableRng};
+
+/// A Xoroshiro64** random number generator.
+///
+/// The xoshiro64** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoroshiro64starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoroshiro64starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[allow(missing_copy_implementations)]
+#[derive(Debug, Clone)]
+pub struct Xoroshiro64StarStar {
+ s0: u32,
+ s1: u32,
+}
+
+impl RngCore for Xoroshiro64StarStar {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ let r = starstar_u32!(self.s0);
+ impl_xoroshiro_u32!(self);
+ r
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ next_u64_via_u32(self)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+impl SeedableRng for Xoroshiro64StarStar {
+ type Seed = [u8; 8];
+
+ /// Create a new `Xoroshiro64StarStar`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ fn from_seed(seed: [u8; 8]) -> Xoroshiro64StarStar {
+ deal_with_zero_seed!(seed, Self);
+ let mut s = [0; 2];
+ read_u32_into(&seed, &mut s);
+
+ Xoroshiro64StarStar {
+ s0: s[0],
+ s1: s[1],
+ }
+ }
+
+ /// Seed a `Xoroshiro64StarStar` from a `u64`.
+ fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar {
+ let mut s = [0; 8];
+ LittleEndian::write_u64(&mut s, seed);
+ Xoroshiro64StarStar::from_seed(s)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoroshiro64StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro64starstar.c
+ let expected = [
+ 3802928447, 813792938, 1618621494, 2955957307, 3252880261,
+ 1129983909, 2539651700, 1327610908, 1757650787, 2763843748,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro128plus.rs b/rand/rand_xoshiro/src/xoshiro128plus.rs
new file mode 100644
index 0000000..b0c7cc7
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro128plus.rs
@@ -0,0 +1,114 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
+use rand_core::le::read_u32_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro128+ random number generator.
+///
+/// The xoshiro128+ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has good statistical properties, besides a low linear
+/// complexity in the lowest bits.
+///
+/// The algorithm used here is translated from [the `xoshiro128starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro128Plus {
+ s: [u32; 4],
+}
+
+impl Xoshiro128Plus {
+ /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128StarStar;
+ ///
+ /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
+ }
+}
+
+impl SeedableRng for Xoshiro128Plus {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoshiro128Plus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 16]) -> Xoshiro128Plus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u32_into(&seed, &mut state);
+ Xoshiro128Plus { s: state }
+ }
+
+ /// Seed a `Xoshiro128Plus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro128Plus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro128Plus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ let result_plus = self.s[0].wrapping_add(self.s[3]);
+ impl_xoshiro_u32!(self);
+ result_plus
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ next_u64_via_u32(self)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro128Plus::from_seed(
+ [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128plus.c
+ let expected = [
+ 5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097,
+ 4110231701, 399823256, 2144435200,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro128starstar.rs b/rand/rand_xoshiro/src/xoshiro128starstar.rs
new file mode 100644
index 0000000..836864e
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro128starstar.rs
@@ -0,0 +1,113 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
+use rand_core::le::read_u32_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro128** random number generator.
+///
+/// The xoshiro128** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro128starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro128StarStar {
+ s: [u32; 4],
+}
+
+impl Xoshiro128StarStar {
+ /// Jump forward, equivalently to 2^64 calls to `next_u32()`.
+ ///
+ /// This can be used to generate 2^64 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoroshiro128StarStar;
+ ///
+ /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]);
+ }
+}
+
+impl SeedableRng for Xoshiro128StarStar {
+ type Seed = [u8; 16];
+
+ /// Create a new `Xoshiro128StarStar`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 16]) -> Xoshiro128StarStar {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u32_into(&seed, &mut state);
+ Xoshiro128StarStar { s: state }
+ }
+
+ /// Seed a `Xoshiro128StarStar` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro128StarStar {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro128StarStar {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ let result_starstar = starstar_u64!(self.s[0]);
+ impl_xoshiro_u32!(self);
+ result_starstar
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ next_u64_via_u32(self)
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro128StarStar::from_seed(
+ [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128starstar.c
+ let expected = [
+ 5760, 40320, 70819200, 3297914139, 2480851620, 1792823698,
+ 4118739149, 1251203317, 1581886583, 1721184582,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u32(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro256plus.rs b/rand/rand_xoshiro/src/xoshiro256plus.rs
new file mode 100644
index 0000000..08da5a8
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro256plus.rs
@@ -0,0 +1,133 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro256+ random number generator.
+///
+/// The xoshiro256+ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has good statistical properties, besides a low linear
+/// complexity in the lowest bits.
+///
+/// The algorithm used here is translated from [the `xoshiro256plus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro256plus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro256Plus {
+ s: [u64; 4],
+}
+
+impl Xoshiro256Plus {
+ /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoshiro256Plus;
+ ///
+ /// let rng1 = Xoshiro256Plus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
+ 0xa9582618e03fc9aa, 0x39abdc4529b1661c
+ ]);
+ }
+
+ /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 starting points, from each of which
+ /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
+ 0x77710069854ee241, 0x39109bb02acbe635
+ ]);
+ }
+}
+
+impl SeedableRng for Xoshiro256Plus {
+ type Seed = [u8; 32];
+
+ /// Create a new `Xoshiro256Plus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 32]) -> Xoshiro256Plus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u64_into(&seed, &mut state);
+ Xoshiro256Plus { s: state }
+ }
+
+ /// Seed a `Xoshiro256Plus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro256Plus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro256Plus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ // The lowest bits have some linear dependencies, so we use the
+ // upper bits instead.
+ (self.next_u64() >> 32) as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_plus = self.s[0].wrapping_add(self.s[3]);
+ impl_xoshiro_u64!(self);
+ result_plus
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro256Plus::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro256plus.c
+ let expected = [
+ 5, 211106232532999, 211106635186183, 9223759065350669058,
+ 9250833439874351877, 13862484359527728515, 2346507365006083650,
+ 1168864526675804870, 34095955243042024, 3466914240207415127,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro256starstar.rs b/rand/rand_xoshiro/src/xoshiro256starstar.rs
new file mode 100644
index 0000000..fc0a208
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro256starstar.rs
@@ -0,0 +1,130 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+/// A xoshiro256** random number generator.
+///
+/// The xoshiro256** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro256starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro256starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro256StarStar {
+ s: [u64; 4],
+}
+
+impl Xoshiro256StarStar {
+ /// Jump forward, equivalently to 2^128 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^128 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoshiro256StarStar;
+ ///
+ /// let rng1 = Xoshiro256StarStar::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c,
+ 0xa9582618e03fc9aa, 0x39abdc4529b1661c
+ ]);
+ }
+
+ /// Jump forward, equivalently to 2^192 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^64 starting points, from each of which
+ /// `jump()` will generate 2^64 non-overlapping subsequences for parallel
+ /// distributed computations.
+ pub fn long_jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3,
+ 0x77710069854ee241, 0x39109bb02acbe635
+ ]);
+ }
+}
+
+impl SeedableRng for Xoshiro256StarStar {
+ type Seed = [u8; 32];
+
+ /// Create a new `Xoshiro256StarStar`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: [u8; 32]) -> Xoshiro256StarStar {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 4];
+ read_u64_into(&seed, &mut state);
+ Xoshiro256StarStar { s: state }
+ }
+
+ /// Seed a `Xoshiro256StarStar` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro256StarStar {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro256StarStar {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_starstar = starstar_u64!(self.s[1]);
+ impl_xoshiro_u64!(self);
+ result_starstar
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro256StarStar::from_seed(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]);
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro128starstar.c
+ let expected = [
+ 11520, 0, 1509978240, 1215971899390074240, 1216172134540287360,
+ 607988272756665600, 16172922978634559625, 8476171486693032832,
+ 10595114339597558777, 2904607092377533576,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro512plus.rs b/rand/rand_xoshiro/src/xoshiro512plus.rs
new file mode 100644
index 0000000..fe982e4
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro512plus.rs
@@ -0,0 +1,124 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+use Seed512;
+
+/// A xoshiro512+ random number generator.
+///
+/// The xoshiro512+ algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has good statistical properties, besides a low linear
+/// complexity in the lowest bits.
+///
+/// The algorithm used here is translated from [the `xoshiro512plus.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro512plus.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro512Plus {
+ s: [u64; 8],
+}
+
+impl Xoshiro512Plus {
+ /// Jump forward, equivalently to 2^256 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^256 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoshiro512Plus;
+ ///
+ /// let rng1 = Xoshiro512Plus::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae,
+ 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c,
+ 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
+ ]);
+ }
+}
+
+impl SeedableRng for Xoshiro512Plus {
+ type Seed = Seed512;
+
+ /// Create a new `Xoshiro512Plus`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: Seed512) -> Xoshiro512Plus {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 8];
+ read_u64_into(&seed.0, &mut state);
+ Xoshiro512Plus { s: state }
+ }
+
+ /// Seed a `Xoshiro512Plus` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro512Plus {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro512Plus {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_plus = self.s[0].wrapping_add(self.s[2]);
+ impl_xoshiro_large!(self);
+ result_plus
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro512Plus::from_seed(Seed512(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
+ 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,
+ 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]));
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro512plus.c
+ let expected = [
+ 4, 8, 4113, 25169936, 52776585412635, 57174648719367,
+ 9223482039571869716, 9331471677901559830, 9340533895746033672,
+ 14078399799840753678,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}
diff --git a/rand/rand_xoshiro/src/xoshiro512starstar.rs b/rand/rand_xoshiro/src/xoshiro512starstar.rs
new file mode 100644
index 0000000..1a33f0a
--- /dev/null
+++ b/rand/rand_xoshiro/src/xoshiro512starstar.rs
@@ -0,0 +1,124 @@
+// Copyright 2018 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use rand_core::impls::fill_bytes_via_next;
+use rand_core::le::read_u64_into;
+use rand_core::{SeedableRng, RngCore, Error};
+
+use Seed512;
+
+/// A xoshiro512** random number generator.
+///
+/// The xoshiro512** algorithm is not suitable for cryptographic purposes, but
+/// is very fast and has excellent statistical properties.
+///
+/// The algorithm used here is translated from [the `xoshiro512starstar.c`
+/// reference source code](http://xoshiro.di.unimi.it/xoshiro512starstar.c) by
+/// David Blackman and Sebastiano Vigna.
+#[derive(Debug, Clone)]
+pub struct Xoshiro512StarStar {
+ s: [u64; 8],
+}
+
+impl Xoshiro512StarStar {
+ /// Jump forward, equivalently to 2^256 calls to `next_u64()`.
+ ///
+ /// This can be used to generate 2^256 non-overlapping subsequences for
+ /// parallel computations.
+ ///
+ /// ```
+ /// # extern crate rand;
+ /// # extern crate rand_xoshiro;
+ /// # fn main() {
+ /// use rand::SeedableRng;
+ /// use rand_xoshiro::Xoshiro512StarStar;
+ ///
+ /// let rng1 = Xoshiro512StarStar::seed_from_u64(0);
+ /// let mut rng2 = rng1.clone();
+ /// rng2.jump();
+ /// let mut rng3 = rng2.clone();
+ /// rng3.jump();
+ /// # }
+ /// ```
+ pub fn jump(&mut self) {
+ impl_jump!(u64, self, [
+ 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae,
+ 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c,
+ 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db
+ ]);
+ }
+}
+
+
+impl SeedableRng for Xoshiro512StarStar {
+ type Seed = Seed512;
+
+ /// Create a new `Xoshiro512StarStar`. If `seed` is entirely 0, it will be
+ /// mapped to a different seed.
+ #[inline]
+ fn from_seed(seed: Seed512) -> Xoshiro512StarStar {
+ deal_with_zero_seed!(seed, Self);
+ let mut state = [0; 8];
+ read_u64_into(&seed.0, &mut state);
+ Xoshiro512StarStar { s: state }
+ }
+
+ /// Seed a `Xoshiro512StarStar` from a `u64` using `SplitMix64`.
+ fn seed_from_u64(seed: u64) -> Xoshiro512StarStar {
+ from_splitmix!(seed)
+ }
+}
+
+impl RngCore for Xoshiro512StarStar {
+ #[inline]
+ fn next_u32(&mut self) -> u32 {
+ self.next_u64() as u32
+ }
+
+ #[inline]
+ fn next_u64(&mut self) -> u64 {
+ let result_starstar = starstar_u64!(self.s[1]);
+ impl_xoshiro_large!(self);
+ result_starstar
+ }
+
+ #[inline]
+ fn fill_bytes(&mut self, dest: &mut [u8]) {
+ fill_bytes_via_next(self, dest);
+ }
+
+ #[inline]
+ fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
+ self.fill_bytes(dest);
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference() {
+ let mut rng = Xoshiro512StarStar::from_seed(Seed512(
+ [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
+ 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
+ 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,
+ 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]));
+ // These values were produced with the reference implementation:
+ // http://xoshiro.di.unimi.it/xoshiro512starstar.c
+ let expected = [
+ 11520, 0, 23040, 23667840, 144955163520, 303992986974289920,
+ 25332796375735680, 296904390158016, 13911081092387501979,
+ 15304787717237593024,
+ ];
+ for &e in &expected {
+ assert_eq!(rng.next_u64(), e);
+ }
+ }
+}