diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-16 17:26:30 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-16 17:26:30 -0800 |
commit | 8350ac6afb2d678b74581000a6aafe1994b72231 (patch) | |
tree | 2330da01a806921b3849c9e64d2b9f506495e2c0 /rand/rand_xoshiro | |
parent | d6652b913b33e432a748187f9f5623cec1e9926e (diff) | |
download | nitrocli-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')
-rw-r--r-- | rand/rand_xoshiro/CHANGELOG.md | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/COPYRIGHT | 12 | ||||
-rw-r--r-- | rand/rand_xoshiro/Cargo.toml | 19 | ||||
-rw-r--r-- | rand/rand_xoshiro/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | rand/rand_xoshiro/LICENSE-MIT | 25 | ||||
-rw-r--r-- | rand/rand_xoshiro/README.md | 28 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/common.rs | 243 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/lib.rs | 106 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/splitmix64.rs | 150 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro128plus.rs | 132 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro128starstar.rs | 129 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro64star.rs | 97 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro64starstar.rs | 96 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro128plus.rs | 114 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro128starstar.rs | 113 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro256plus.rs | 133 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro256starstar.rs | 130 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro512plus.rs | 124 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro512starstar.rs | 124 |
19 files changed, 1984 insertions, 0 deletions
diff --git a/rand/rand_xoshiro/CHANGELOG.md b/rand/rand_xoshiro/CHANGELOG.md new file mode 100644 index 0000000..b23c990 --- /dev/null +++ b/rand/rand_xoshiro/CHANGELOG.md @@ -0,0 +1,8 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.1.0] - 2019-01-04 +Initial release. diff --git a/rand/rand_xoshiro/COPYRIGHT b/rand/rand_xoshiro/COPYRIGHT new file mode 100644 index 0000000..468d907 --- /dev/null +++ b/rand/rand_xoshiro/COPYRIGHT @@ -0,0 +1,12 @@ +Copyrights in the Rand project are retained by their contributors. No +copyright assignment is required to contribute to the Rand project. + +For full authorship information, see the version control history. + +Except as otherwise noted (below and/or in individual files), Rand is +licensed under the Apache License, Version 2.0 <LICENSE-APACHE> or +<http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +<LICENSE-MIT> or <http://opensource.org/licenses/MIT>, at your option. + +The Rand project includes code from the Rust project +published under these same licenses. diff --git a/rand/rand_xoshiro/Cargo.toml b/rand/rand_xoshiro/Cargo.toml new file mode 100644 index 0000000..8d174c7 --- /dev/null +++ b/rand/rand_xoshiro/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "rand_xoshiro" +version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs +authors = ["The Rand Project Developers"] +license = "MIT/Apache-2.0" +readme = "README.md" +repository = "https://github.com/rust-random/rand" +documentation = "https://docs.rs/rand_xoshiro" +homepage = "https://crates.io/crates/rand_xoshiro" +description = "Xoshiro, xoroshiro and splitmix64 random number generators" +keywords = ["random", "rng"] +categories = ["algorithms"] + +[dependencies] +byteorder = { version = "1", default-features=false } +rand_core = { path = "../rand_core", version = "0.3", default-features=false } + +[dev-dependencies] +rand = { path = "..", version = "0.6", default-features=false } # needed for doctests diff --git a/rand/rand_xoshiro/LICENSE-APACHE b/rand/rand_xoshiro/LICENSE-APACHE new file mode 100644 index 0000000..16fe87b --- /dev/null +++ b/rand/rand_xoshiro/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/rand/rand_xoshiro/LICENSE-MIT b/rand/rand_xoshiro/LICENSE-MIT new file mode 100644 index 0000000..a5e040c --- /dev/null +++ b/rand/rand_xoshiro/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2018 Developers of the Rand project + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/rand/rand_xoshiro/README.md b/rand/rand_xoshiro/README.md new file mode 100644 index 0000000..014477e --- /dev/null +++ b/rand/rand_xoshiro/README.md @@ -0,0 +1,28 @@ +# rand_xoshiro + +[![Build Status](https://travis-ci.org/rust-random/rand.svg?branch=master)](https://travis-ci.org/rust-random/rand) +[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/rand?svg=true)](https://ci.appveyor.com/project/rust-random/rand) +[![Latest version](https://img.shields.io/crates/v/rand_xoshiro.svg)](https://crates.io/crates/rand_xoshiro) +[![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/) +[![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_xoshiro) +[![API](https://docs.rs/rand_xoshiro/badge.svg)](https://docs.rs/rand_xoshiro) +[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements) + +Rust implementation of the [xoshiro, xoroshiro and splitmix64](http://xoshiro.di.unimi.it) random number generators. + +This crate depends on [rand_core](https://crates.io/crates/rand_core) and is +part of the [Rand project](https://github.com/rust-random/rand). + +Links: + +- [API documentation (master)](https://rust-random.github.io/rand/rand_xoshiro) +- [API documentation (docs.rs)](https://docs.rs/rand_xoshiro) +- [Changelog](CHANGELOG.md) + +## License + +`rand_xoshiro` is distributed under the terms of both the MIT license and the +Apache License (Version 2.0). + +See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and +[COPYRIGHT](COPYRIGHT) for details. 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); + } + } +} |