1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use super::RNG;
const PCG_DEFAULT_MULTIPLIER_128: u128 = 47026247687942121848144207491837523525;
#[cfg_attr(feature = "zeroize", derive(zeroize::Zeroize))]
#[cfg_attr(feature = "zeroizer", zeroize(drop))]
pub struct Pcg64 {
seed: u128,
state: u128,
inc: u128,
}
impl Pcg64 {
#[cfg(feature = "std")]
pub fn new() -> Self {
let mut entropy: [u8; std::mem::size_of::<u128>()] = Default::default();
entropy.copy_from_slice(&crate::entropy::entropy_from_system(std::mem::size_of::<
u128,
>()));
Self {
seed: u128::from_ne_bytes(entropy),
inc: 0,
state: 0,
}
}
pub fn new_seed(seed: u128) -> Self {
Self {
seed,
inc: 0,
state: 0,
}
}
#[inline(always)]
fn step(&mut self) {
self.state = self
.state
.wrapping_mul(PCG_DEFAULT_MULTIPLIER_128)
.wrapping_add(self.inc);
}
#[inline(always)]
fn rand128(&mut self) -> u64 {
self.state = 0;
self.inc = self.seed.rotate_left(1) | 1;
self.step();
self.state = self.state.wrapping_add(self.seed);
self.step();
self.step();
((self.state >> 64) as u64) ^ (self.state as u64)
}
}
#[cfg(feature = "std")]
impl Default for Pcg64 {
fn default() -> Self {
let mut entropy: [u8; std::mem::size_of::<u128>()] = Default::default();
entropy.copy_from_slice(&crate::entropy::entropy_from_system(std::mem::size_of::<
u128,
>()));
Self {
seed: u128::from_ne_bytes(entropy),
inc: 0,
state: 0,
}
}
}
impl RNG for Pcg64 {
type Output = [u8; 8];
fn rand(&mut self) -> Self::Output {
let ret = self.rand128();
self.seed = self.state ^ (ret as u128).rotate_right(64);
ret.to_ne_bytes()
}
fn rand_with_seed(_seed: &[u8]) -> Self::Output {
unimplemented!("Pcg64 requires a state!");
}
fn reseed(&mut self, new_seed: &[u8]) {
let mut seed = [0u8; 16];
seed.iter_mut().zip(new_seed).for_each(|(a, b)| *a = *b);
self.seed = u128::from_ne_bytes(seed);
}
}
impl Clone for Pcg64 {
fn clone(&self) -> Self {
Self {
seed: self.seed,
inc: self.inc,
state: self.state,
}
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for Pcg64 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Pcg64 ({:p})", self)
}
}