fullcodec_rand_xorshift/
lib.rs

1// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! The xorshift random number generator.
10
11#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
12       html_favicon_url = "https://www.rust-lang.org/favicon.ico",
13       html_root_url = "https://docs.rs/rand_xorshift/0.3.0")]
14
15#![deny(missing_docs)]
16#![deny(missing_debug_implementations)]
17
18#![no_std]
19
20use core::num::Wrapping as w;
21use core::{fmt, mem};
22use rand_core::{RngCore, SeedableRng, Error, impls, le};
23#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
24use parity_scale_codec::{Decode, Encode, Input, Error as ParityError};
25use sp_std::vec::Vec;
26
27/// An Xorshift random number generator.
28///
29/// The Xorshift[^1] algorithm is not suitable for cryptographic purposes
30/// but is very fast. If you do not know for sure that it fits your
31/// requirements, use a more secure one such as `StdRng` or `OsRng`.
32///
33/// When seeded with zero (i.e. `XorShiftRng::from_seed(0)` is called), this implementation
34/// actually uses `0xBAD_5EED_0BAD_5EED_0BAD_5EED_0BAD_5EED` for the seed. This arbitrary value is
35/// used because the underlying algorithm can't escape from an all-zero state, and the function is
36/// infallible so it can't signal this by returning an error.
37///
38/// [^1]: Marsaglia, George (July 2003).
39///       ["Xorshift RNGs"](https://www.jstatsoft.org/v08/i14/paper).
40///       *Journal of Statistical Software*. Vol. 8 (Issue 14).
41#[derive(Clone, PartialEq, Eq)]
42#[cfg_attr(feature="serde1", derive(Serialize,Deserialize))]
43pub struct XorShiftRng {
44    x: w<u32>,
45    y: w<u32>,
46    z: w<u32>,
47    w: w<u32>,
48}
49
50impl Encode for XorShiftRng {
51    fn encode(&self) -> Vec<u8>  {
52        let mut r = [0u8; mem::size_of::<XorShiftRng>()];
53        r[0] = (self.x.0 % 2_i32.pow(8) as u32) as u8;
54        r[1] = (self.x.0 / 2_i32.pow(8) as u32) as u8;
55        r[2] = (self.x.0 / 2_i32.pow(16) as u32) as u8;
56        r[3] = (self.x.0 / 2_i32.pow(24) as u32) as u8;
57        r[4] = (self.y.0 % 2_i32.pow(8) as u32) as u8;
58        r[5] = (self.y.0 / 2_i32.pow(8) as u32) as u8;
59        r[6] = (self.y.0 / 2_i32.pow(16) as u32) as u8;
60        r[7] = (self.y.0 / 2_i32.pow(24) as u32) as u8;
61        r[8] = (self.z.0 % 2_i32.pow(8) as u32) as u8;
62        r[9] = (self.z.0 / 2_i32.pow(8) as u32) as u8;
63        r[10] = (self.z.0 / 2_i32.pow(16) as u32) as u8;
64        r[11] = (self.z.0 / 2_i32.pow(24) as u32) as u8;
65        r[12] = (self.w.0 % 2_i32.pow(8) as u32) as u8;
66        r[13] = (self.w.0 / 2_i32.pow(8) as u32) as u8;
67        r[14] = (self.w.0 / 2_i32.pow(16) as u32) as u8;
68        r[15] = (self.w.0 / 2_i32.pow(24) as u32) as u8;
69        r.to_vec()
70    }
71}
72
73impl Decode for XorShiftRng {
74    fn decode<I: Input>(input: &mut I) -> Result<Self, ParityError> {
75        let mut buf = [0u8; mem::size_of::<XorShiftRng>()];
76		input.read(&mut buf).unwrap();
77        let mut xyzw: [u32; 4] = [0; 4];
78        for i in 0..xyzw.len() {
79            let mut num = 0;
80            for k in 0..4 {
81                num += buf[(i * 4) + k] as u32 * 2_i32.pow(8 * k as u32) as u32;
82            }
83            xyzw[i] = num;
84        }
85        Ok(XorShiftRng {
86            x: w(xyzw[0]),
87            y: w(xyzw[1]),
88            z: w(xyzw[2]),
89            w: w(xyzw[3]),
90        })
91    }
92}
93
94// Custom Debug implementation that does not expose the internal state
95impl fmt::Debug for XorShiftRng {
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        write!(f, "XorShiftRng {{}}")
98    }
99}
100
101impl RngCore for XorShiftRng {
102    #[inline]
103    fn next_u32(&mut self) -> u32 {
104        let x = self.x;
105        let t = x ^ (x << 11);
106        self.x = self.y;
107        self.y = self.z;
108        self.z = self.w;
109        let w_ = self.w;
110        self.w = w_ ^ (w_ >> 19) ^ (t ^ (t >> 8));
111        self.w.0
112    }
113
114    #[inline]
115    fn next_u64(&mut self) -> u64 {
116        impls::next_u64_via_u32(self)
117    }
118
119    #[inline]
120    fn fill_bytes(&mut self, dest: &mut [u8]) {
121        impls::fill_bytes_via_next(self, dest)
122    }
123
124    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
125        self.fill_bytes(dest);
126        Ok(())
127    }
128}
129
130impl SeedableRng for XorShiftRng {
131    type Seed = [u8; 16];
132
133    fn from_seed(seed: Self::Seed) -> Self {
134        let mut seed_u32 = [0u32; 4];
135        le::read_u32_into(&seed, &mut seed_u32);
136
137        // Xorshift cannot be seeded with 0 and we cannot return an Error, but
138        // also do not wish to panic (because a random seed can legitimately be
139        // 0); our only option is therefore to use a preset value.
140        if seed_u32.iter().all(|&x| x == 0) {
141            seed_u32 = [0xBAD_5EED, 0xBAD_5EED, 0xBAD_5EED, 0xBAD_5EED];
142        }
143
144        XorShiftRng {
145            x: w(seed_u32[0]),
146            y: w(seed_u32[1]),
147            z: w(seed_u32[2]),
148            w: w(seed_u32[3]),
149        }
150    }
151
152    fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
153        let mut b = [0u8; 16];
154        loop {
155            rng.try_fill_bytes(&mut b[..])?;
156            if !b.iter().all(|&x| x == 0) {
157                break;
158            }
159        }
160
161        Ok(XorShiftRng {
162            x: w(u32::from_le_bytes([b[0], b[1], b[2], b[3]])),
163            y: w(u32::from_le_bytes([b[4], b[5], b[6], b[7]])),
164            z: w(u32::from_le_bytes([b[8], b[9], b[10], b[11]])),
165            w: w(u32::from_le_bytes([b[12], b[13], b[14], b[15]])),
166        })
167    }
168}