author | unc0rr |
Sun, 14 Oct 2018 18:01:58 +0200 | |
changeset 13903 | e26573441808 |
parent 13886 | b6c35ac1c5ba |
child 13904 | 3f0576157749 |
permissions | -rw-r--r-- |
13886
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 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
7 |
fn new(init_values: &[u8]) -> Self { |
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 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
19 |
for i in 0..2048 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
20 |
prng.get_next(); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
21 |
} |
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 |
prng |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
24 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
25 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
26 |
#[inline] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
27 |
fn get_next(&mut self) -> u32 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
28 |
self.index = (self.index + 1) & 0x3f; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
29 |
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
|
30 |
+ self.circular_buffer[(self.index + 9) & 0x3f]) |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
31 |
& 0x7fffffff; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
32 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
33 |
self.circular_buffer[self.index] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
34 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
35 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
36 |
#[inline] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
37 |
fn get_random(&mut self, modulo: u32) -> u32 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
38 |
self.get_next(); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
39 |
self.get_next() % modulo |
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 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
42 |
#[inline] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
43 |
fn add_randomness(&mut self, value: u32) { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
44 |
self.index = (self.index + 1) & 0x3f; |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
45 |
self.circular_buffer[self.index] ^= value; |
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 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
48 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
49 |
#[cfg(test)] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
50 |
#[test] |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
51 |
fn compatibility() { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
52 |
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
|
53 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
54 |
assert_eq!(prng.get_random(1000), 418); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
55 |
assert_eq!(prng.get_random(1000000), 554064); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
56 |
assert_eq!(prng.get_random(0xffffffff), 239515837); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
57 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
58 |
prng.add_randomness(123); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
59 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
60 |
for i in 0..=100000 { |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
61 |
prng.get_random(2); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
62 |
} |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
63 |
|
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
64 |
assert_eq!(prng.get_random(0xffffffff), 525333582); |
b6c35ac1c5ba
Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff
changeset
|
65 |
} |