rs2/
gf.rs

1const LOG: &[i32] = &[
2    0x00, 0x00, 0x74, 0x09, 0xe8, 0x12, 0x7d, 0x38, 0x5d, 0x41, 0x86, 0x85, 0xf1, 0x51, 0xac, 0x1b,
3    0xd1, 0x24, 0xb5, 0x7b, 0xfa, 0x70, 0xf9, 0x5a, 0x66, 0x88, 0xc5, 0x4a, 0x21, 0x8e, 0x8f, 0x8a,
4    0x46, 0x93, 0x98, 0x89, 0x2a, 0x3e, 0xef, 0x97, 0x6f, 0xb6, 0xe4, 0x91, 0x6e, 0x53, 0xce, 0xe1,
5    0xda, 0xbd, 0xfc, 0x2d, 0x3a, 0x84, 0xbe, 0x1c, 0x95, 0x63, 0x03, 0x30, 0x04, 0xe9, 0xfe, 0x79,
6    0xba, 0x82, 0x08, 0xd3, 0x0d, 0x0b, 0xfd, 0xf2, 0x9e, 0x40, 0xb2, 0x6c, 0x64, 0x39, 0x0c, 0xc0,
7    0xe3, 0xa2, 0x2b, 0xc6, 0x59, 0x36, 0x06, 0x60, 0xe2, 0x25, 0xc7, 0x45, 0x43, 0xc2, 0x56, 0x8d,
8    0x4f, 0x20, 0x32, 0x9c, 0x71, 0x92, 0xa1, 0x61, 0xae, 0xa0, 0xf8, 0xa8, 0x33, 0x44, 0x90, 0x47,
9    0x0a, 0xea, 0xd7, 0x83, 0x77, 0xdd, 0xa4, 0x5c, 0x78, 0xb3, 0x5e, 0xbf, 0x73, 0x9a, 0xed, 0xd6,
10    0x2f, 0xdf, 0xf6, 0x6b, 0x7c, 0x54, 0x48, 0xa3, 0x81, 0x18, 0x7f, 0xbc, 0x72, 0xc8, 0x67, 0xf0,
11    0x13, 0x31, 0xb4, 0xf3, 0x27, 0x8c, 0xe0, 0xf5, 0xd8, 0x65, 0xad, 0xdb, 0x80, 0x3d, 0x35, 0xe6,
12    0x58, 0x68, 0x17, 0x29, 0x9f, 0xa5, 0x3b, 0x87, 0xcd, 0x6a, 0xaa, 0x55, 0x7a, 0x01, 0xd4, 0x9b,
13    0x57, 0x50, 0x99, 0x22, 0x3c, 0xd9, 0xb9, 0x4d, 0xb7, 0x5b, 0x37, 0xa9, 0xca, 0xb1, 0x02, 0xf4,
14    0xc3, 0x9d, 0x94, 0x8b, 0xa6, 0xdc, 0x11, 0xcc, 0xe5, 0xfb, 0x07, 0xaf, 0x16, 0x1a, 0xd5, 0x14,
15    0x23, 0xc9, 0x15, 0x1f, 0x6d, 0xde, 0x1d, 0x42, 0xa7, 0x10, 0xb8, 0x49, 0x05, 0x75, 0xbb, 0xee,
16    0x7e, 0x96, 0x5f, 0x0e, 0x4c, 0xc4, 0xf7, 0xcb, 0xeb, 0xc1, 0x52, 0x2e, 0x19, 0x4e, 0xd0, 0xb0,
17    0xec, 0x1e, 0x28, 0xab, 0xd2, 0xcf, 0x34, 0x2c, 0xe7, 0x69, 0x0f, 0x76, 0x62, 0x26, 0x4b, 0x3f,
18];
19
20const EXP: &[i32] = &[
21    0x01, 0xad, 0xbe, 0x3a, 0x3c, 0xdc, 0x56, 0xca, 0x42, 0x03, 0x70, 0x45, 0x4e, 0x44, 0xe3, 0xfa,
22    0xd9, 0xc6, 0x05, 0x90, 0xcf, 0xd2, 0xcc, 0xa2, 0x89, 0xec, 0xcd, 0x0f, 0x37, 0xd6, 0xf1, 0xd3,
23    0x61, 0x1c, 0xb3, 0xd0, 0x11, 0x59, 0xfd, 0x94, 0xf2, 0xa3, 0x24, 0x52, 0xf7, 0x33, 0xeb, 0x80,
24    0x3b, 0x91, 0x62, 0x6c, 0xf6, 0x9e, 0x55, 0xba, 0x07, 0x4d, 0x34, 0xa6, 0xb4, 0x9d, 0x25, 0xff,
25    0x49, 0x09, 0xd7, 0x5c, 0x6d, 0x5b, 0x20, 0x6f, 0x86, 0xdb, 0x1b, 0xfe, 0xe4, 0xb7, 0xed, 0x60,
26    0xb1, 0x0d, 0xea, 0x2d, 0x85, 0xab, 0x5e, 0xb0, 0xa0, 0x54, 0x17, 0xb9, 0x77, 0x08, 0x7a, 0xe2,
27    0x57, 0x67, 0xfc, 0x39, 0x4c, 0x99, 0x18, 0x8e, 0xa1, 0xf9, 0xa9, 0x83, 0x4b, 0xd4, 0x2c, 0x28,
28    0x15, 0x64, 0x8c, 0x7c, 0x02, 0xdd, 0xfb, 0x74, 0x78, 0x3f, 0xac, 0x13, 0x84, 0x06, 0xe0, 0x8a,
29    0x9c, 0x88, 0x41, 0x73, 0x35, 0x0b, 0x0a, 0xa7, 0x19, 0x23, 0x1f, 0xc3, 0x95, 0x5f, 0x1d, 0x1e,
30    0x6e, 0x2b, 0x65, 0x21, 0xc2, 0x38, 0xe1, 0x27, 0x22, 0xb2, 0x7d, 0xaf, 0x63, 0xc1, 0x48, 0xa4,
31    0x69, 0x66, 0x51, 0x87, 0x76, 0xa5, 0xc4, 0xd8, 0x6b, 0xbb, 0xaa, 0xf3, 0x0e, 0x9a, 0x68, 0xcb,
32    0xef, 0xbd, 0x4a, 0x79, 0x92, 0x12, 0x29, 0xb8, 0xda, 0xb6, 0x40, 0xde, 0x8b, 0x31, 0x36, 0x7b,
33    0x4f, 0xe9, 0x5d, 0xc0, 0xe5, 0x1a, 0x53, 0x5a, 0x8d, 0xd1, 0xbc, 0xe7, 0xc7, 0xa8, 0x2e, 0xf5,
34    0xee, 0x10, 0xf4, 0x43, 0xae, 0xce, 0x7f, 0x72, 0x98, 0xb5, 0x30, 0x9b, 0xc5, 0x75, 0xd5, 0x81,
35    0x96, 0x2f, 0x58, 0x50, 0x2a, 0xc8, 0x9f, 0xf8, 0x04, 0x3d, 0x71, 0xe8, 0xf0, 0x7e, 0xdf, 0x26,
36    0x8f, 0x0c, 0x47, 0x93, 0xbf, 0x97, 0x82, 0xe6, 0x6a, 0x16, 0x14, 0xc9, 0x32, 0x46, 0x3e, 0x01,
37    0xad, 0xbe, 0x3a, 0x3c, 0xdc, 0x56, 0xca, 0x42, 0x03, 0x70, 0x45, 0x4e, 0x44, 0xe3, 0xfa, 0xd9,
38    0xc6, 0x05, 0x90, 0xcf, 0xd2, 0xcc, 0xa2, 0x89, 0xec, 0xcd, 0x0f, 0x37, 0xd6, 0xf1, 0xd3, 0x61,
39    0x1c, 0xb3, 0xd0, 0x11, 0x59, 0xfd, 0x94, 0xf2, 0xa3, 0x24, 0x52, 0xf7, 0x33, 0xeb, 0x80, 0x3b,
40    0x91, 0x62, 0x6c, 0xf6, 0x9e, 0x55, 0xba, 0x07, 0x4d, 0x34, 0xa6, 0xb4, 0x9d, 0x25, 0xff, 0x49,
41    0x09, 0xd7, 0x5c, 0x6d, 0x5b, 0x20, 0x6f, 0x86, 0xdb, 0x1b, 0xfe, 0xe4, 0xb7, 0xed, 0x60, 0xb1,
42    0x0d, 0xea, 0x2d, 0x85, 0xab, 0x5e, 0xb0, 0xa0, 0x54, 0x17, 0xb9, 0x77, 0x08, 0x7a, 0xe2, 0x57,
43    0x67, 0xfc, 0x39, 0x4c, 0x99, 0x18, 0x8e, 0xa1, 0xf9, 0xa9, 0x83, 0x4b, 0xd4, 0x2c, 0x28, 0x15,
44    0x64, 0x8c, 0x7c, 0x02, 0xdd, 0xfb, 0x74, 0x78, 0x3f, 0xac, 0x13, 0x84, 0x06, 0xe0, 0x8a, 0x9c,
45    0x88, 0x41, 0x73, 0x35, 0x0b, 0x0a, 0xa7, 0x19, 0x23, 0x1f, 0xc3, 0x95, 0x5f, 0x1d, 0x1e, 0x6e,
46    0x2b, 0x65, 0x21, 0xc2, 0x38, 0xe1, 0x27, 0x22, 0xb2, 0x7d, 0xaf, 0x63, 0xc1, 0x48, 0xa4, 0x69,
47    0x66, 0x51, 0x87, 0x76, 0xa5, 0xc4, 0xd8, 0x6b, 0xbb, 0xaa, 0xf3, 0x0e, 0x9a, 0x68, 0xcb, 0xef,
48    0xbd, 0x4a, 0x79, 0x92, 0x12, 0x29, 0xb8, 0xda, 0xb6, 0x40, 0xde, 0x8b, 0x31, 0x36, 0x7b, 0x4f,
49    0xe9, 0x5d, 0xc0, 0xe5, 0x1a, 0x53, 0x5a, 0x8d, 0xd1, 0xbc, 0xe7, 0xc7, 0xa8, 0x2e, 0xf5, 0xee,
50    0x10, 0xf4, 0x43, 0xae, 0xce, 0x7f, 0x72, 0x98, 0xb5, 0x30, 0x9b, 0xc5, 0x75, 0xd5, 0x81, 0x96,
51    0x2f, 0x58, 0x50, 0x2a, 0xc8, 0x9f, 0xf8, 0x04, 0x3d, 0x71, 0xe8, 0xf0, 0x7e, 0xdf, 0x26, 0x8f,
52    0x0c, 0x47, 0x93, 0xbf, 0x97, 0x82, 0xe6, 0x6a, 0x16, 0x14, 0xc9, 0x32, 0x46, 0x3e, 0x01, 0xad,
53];
54
55fn mult_no_lut(x: i32, y: i32, prim: i32) -> i32 {
56    let mut r = 0;
57    let mut x = x;
58    let mut y = y;
59    while y > 0 {
60        if y & 1 > 0 {
61            r ^= x;
62        }
63        y = y >> 1;
64        x = x << 1;
65        if prim > 0 && x & 256 >= 1 {
66            x ^= prim;
67        }
68    }
69    r
70}
71
72#[allow(unused)]
73fn gen_exp_table(n: i32, prim: i32, gen: u8) -> ([i32; 512], [i32; 256]) {
74    let mut exp = [0i32; 512];
75    let mut log = [0i32; 256];
76    let mut x: i32 = 1;
77    for i in 0..n {
78        exp[i as usize] = x;
79        log[x as usize] = i;
80        x = mult_no_lut(x, gen as i32, prim);
81    }
82
83    (exp, log)
84}
85
86pub(super) fn mult(x: u8, y: u8) -> u8 {
87    if x == 0 || y == 0 {
88        0
89    } else {
90        EXP[(LOG[x as usize] + LOG[y as usize]) as usize] as u8
91    }
92}
93
94pub(super) fn div(x: u8, y: u8) -> u8 {
95    if y == 0 {
96        panic!("zero division");
97    }
98    if x == 0 {
99        return 0;
100    }
101    EXP[((LOG[x as usize] + 255 - LOG[y as usize]) % 255) as usize] as u8
102}
103
104pub(super) fn pow(x: u8, power: i32) -> u8 {
105    let mut power = power;
106    if power < 0 {
107        power = (255 - (-power % 255)) % 255;
108    }
109    EXP[((LOG[x as usize] * power) % 255) as usize] as u8
110}
111
112pub(super) fn inv(x: u8) -> u8 {
113    EXP[(255 - LOG[x as usize]) as usize] as u8
114}
115
116pub(super) fn poly_scale(p: &[u8], x: u8) -> Vec<u8> {
117    let mut r = vec![0u8; p.len()];
118    for i in 0..p.len() {
119        r[i] = mult(p[i], x);
120    }
121    r
122}
123
124pub(super) fn poly_add(p: &[u8], q: &[u8]) -> Vec<u8> {
125    let l = if p.len() > q.len() { p.len() } else { q.len() };
126    let mut r = vec![0u8; l];
127    let rl = r.len();
128    let pl = p.len();
129    let ql = q.len();
130
131    for i in 0..p.len() {
132        r[i + rl - pl] = p[i];
133    }
134    for i in 0..q.len() {
135        r[i + rl - ql] ^= q[i];
136    }
137    r
138}
139
140pub(super) fn poly_mult(p: &[u8], q: &[u8]) -> Vec<u8> {
141    let mut r = vec![0u8; p.len() + q.len() - 1];
142    for j in 0..q.len() {
143        for i in 0..p.len() {
144            r[i + j] ^= mult(p[i], q[j]);
145        }
146    }
147    r
148}
149
150// FIXME: use ndarray or simd here
151pub(super) fn poly_eval(p: &[u8], x: u8) -> u8 {
152    let mut y = p[0];
153    for i in 1..p.len() {
154        y = mult(y, x) ^ p[i];
155    }
156    y
157}
158
159/*
160#[cfg(test)]
161mod test {
162    extern crate test;
163    use super::*;
164    use rand::Rng;
165    use test::Bencher;
166
167    #[bench]
168    fn bench_poly_eval(b: &mut Bencher) {
169        let mut rng = rand::thread_rng();
170        let mut buf = [0u8; 1020];
171        for i in 0..buf.len() {
172            let f: u8 = rng.gen();
173            buf[i] = f;
174        }
175
176        b.iter(|| {
177            let x = poly_eval(&buf, 12);
178
179            rng.gen::<u8>() // return a value to avoid optimization issue
180        });
181    }
182}
183*/
184
185pub(super) fn poly_div(dividend: &[u8], divisor: &[u8]) -> (Vec<u8>, Vec<u8>) {
186    let mut out = Vec::with_capacity(dividend.len());
187    out.extend_from_slice(dividend);
188
189    for i in 0..(dividend.len() - divisor.len() - 1) {
190        let coef = out[i];
191        if coef != 0 {
192            for j in 1..divisor.len() {
193                if divisor[j] != 0 {
194                    out[i + j] ^= mult(divisor[j], coef);
195                }
196            }
197        }
198    }
199    let mid = dividend.len() - divisor.len() + 1;
200    let (head, tail) = out.split_at(mid);
201    (Vec::from(head), Vec::from(tail))
202}