author | alfadur |
Wed, 28 Aug 2019 00:34:26 +0300 | |
changeset 15392 | 24a9afbf33c6 |
parent 15136 | de32299de704 |
child 15931 | 9b73594ac986 |
permissions | -rw-r--r-- |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
1 |
pub struct LaggedFibonacciPRNG { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
2 |
circular_buffer: [u32; 64], |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
3 |
index: usize, |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
4 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
5 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
6 |
impl LaggedFibonacciPRNG { |
14048 | 7 |
pub fn new(init_values: &[u8]) -> Self { |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
8 |
let mut buf = [0xa98765 + 68; 64]; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
9 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
10 |
for i in 0..std::cmp::min(init_values.len(), 54) { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
11 |
buf[i] = init_values[i] as u32; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
12 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
13 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
14 |
let mut prng = Self { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
15 |
circular_buffer: buf, |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
16 |
index: 54, |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
17 |
}; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
18 |
|
15136 | 19 |
prng.discard(2048); |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
20 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
21 |
prng |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
22 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
23 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
24 |
#[inline] |
15136 | 25 |
pub fn discard(&mut self, count: usize) { |
26 |
for _i in 0..count { |
|
27 |
self.get_next(); |
|
28 |
} |
|
29 |
} |
|
30 |
||
31 |
#[inline] |
|
14050 | 32 |
fn get_next(&mut self) -> u32 { |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
33 |
self.index = (self.index + 1) & 0x3f; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
34 |
self.circular_buffer[self.index] = (self.circular_buffer[(self.index + 40) & 0x3f] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
35 |
+ self.circular_buffer[(self.index + 9) & 0x3f]) |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
36 |
& 0x7fffffff; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
37 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
38 |
self.circular_buffer[self.index] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
39 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
40 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
41 |
#[inline] |
14048 | 42 |
pub fn get_random(&mut self, modulo: u32) -> u32 { |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
43 |
self.get_next(); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
44 |
self.get_next() % modulo |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
45 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
46 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
47 |
#[inline] |
14048 | 48 |
pub fn add_randomness(&mut self, value: u32) { |
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
49 |
self.index = (self.index + 1) & 0x3f; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
50 |
self.circular_buffer[self.index] ^= value; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
51 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
52 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
53 |
|
13925 | 54 |
impl Iterator for LaggedFibonacciPRNG { |
55 |
type Item = u32; |
|
56 |
||
13956
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
13925
diff
changeset
|
57 |
fn next(&mut self) -> Option<Self::Item> { |
13925 | 58 |
self.get_next(); |
59 |
Some(self.get_next()) |
|
60 |
} |
|
61 |
} |
|
62 |
||
13910
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
63 |
#[cfg(test)] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
64 |
#[test] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
65 |
fn compatibility() { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
66 |
let mut prng = LaggedFibonacciPRNG::new("{052e2aee-ce41-4720-97bd-559a413bf866}".as_bytes()); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
67 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
68 |
assert_eq!(prng.get_random(1000), 418); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
69 |
assert_eq!(prng.get_random(1000000), 554064); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
70 |
assert_eq!(prng.get_random(0xffffffff), 239515837); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
71 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
72 |
prng.add_randomness(123); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
73 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
74 |
for i in 0..=100000 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
75 |
prng.get_random(2); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
76 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
77 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
78 |
assert_eq!(prng.get_random(0xffffffff), 525333582); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
79 |
} |