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
use gf256::p128;

#[derive(Debug, Clone, PartialEq)]
pub struct Lfsr(pub u64);

const POLYNOMIAL: p128 = p128(0x1000000000000001b);

impl Lfsr {
    pub fn new(mut seed: u64) -> Self {
        if seed == 0 {
            seed = 1;
        }

        Self(seed)
    }

    pub fn next(&mut self, bits: u64) -> u64 {
        debug_assert!(bits <= 64);
        let mut x = 0;
        for _ in 0..bits {
            let msb = self.0 >> 63;
            x = (x << 1) | msb;
            self.0 = (self.0 << 1) ^ if msb != 0 { POLYNOMIAL.0 as u64 } else { 0 };
        }
        x
    }

    pub fn prev(&mut self, bits: u64) -> u64 {
        debug_assert!(bits <= 64);
        let mut x = 0;
        for _ in 0..bits {
            let lsb = self.0 & 1;
            x = (x >> 1) | (lsb << (bits - 1));
            self.0 = (self.0 >> 1)
                ^ if lsb != 0 {
                    (POLYNOMIAL.0 >> 1) as u64
                } else {
                    0
                };
        }
        x
    }

    pub fn skip(&mut self, bits: u64) {
        // just iterate naively
        for _ in 0..bits {
            let msb = self.0 >> 63;
            self.0 = (self.0 << 1) ^ if msb != 0 { POLYNOMIAL.0 as u64 } else { 0 };
        }
    }

    pub fn skip_backwards(&mut self, bits: u64) {
        // just iterate naively
        for _ in 0..bits {
            let lsb = self.0 & 1;
            self.0 = (self.0 >> 1)
                ^ if lsb != 0 {
                    (POLYNOMIAL.0 >> 1) as u64
                } else {
                    0
                };
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::iter;

    #[test]
    fn lfsr_naive() {
        let mut lfsr64_naive = Lfsr::new(1);
        let buf = iter::repeat_with(|| lfsr64_naive.next(64))
            .take(8)
            .collect::<Vec<_>>();
        assert_eq!(
            buf,
            &[
                0x0000000000000001,
                0x000000000000001b,
                0x0000000000000145,
                0x0000000000001db7,
                0x0000000000011011,
                0x00000000001ab1ab,
                0x0000000001514515,
                0x000000001c6db6c7
            ]
        );
        let buf = iter::repeat_with(|| lfsr64_naive.prev(64))
            .take(8)
            .collect::<Vec<_>>();
        assert_eq!(
            buf,
            &[
                0x000000001c6db6c7,
                0x0000000001514515,
                0x00000000001ab1ab,
                0x0000000000011011,
                0x0000000000001db7,
                0x0000000000000145,
                0x000000000000001b,
                0x0000000000000001
            ]
        );
    }
}