1 | const GF2_DIM: usize = 32; |
2 | |
3 | fn gf2_matrix_times(mat: &[u32; GF2_DIM], mut vec: u32) -> u32 { |
4 | let mut sum: u32 = 0; |
5 | let mut idx: usize = 0; |
6 | while vec > 0 { |
7 | if vec & 1 == 1 { |
8 | sum ^= mat[idx]; |
9 | } |
10 | vec >>= 1; |
11 | idx += 1; |
12 | } |
13 | return sum; |
14 | } |
15 | |
16 | fn gf2_matrix_square(square: &mut [u32; GF2_DIM], mat: &[u32; GF2_DIM]) { |
17 | for n: usize in 0..GF2_DIM { |
18 | square[n] = gf2_matrix_times(mat, vec:mat[n]); |
19 | } |
20 | } |
21 | |
22 | pub(crate) fn combine(mut crc1: u32, crc2: u32, mut len2: u64) -> u32 { |
23 | let mut row: u32; |
24 | let mut even = [0u32; GF2_DIM]; /* even-power-of-two zeros operator */ |
25 | let mut odd = [0u32; GF2_DIM]; /* odd-power-of-two zeros operator */ |
26 | |
27 | /* degenerate case (also disallow negative lengths) */ |
28 | if len2 <= 0 { |
29 | return crc1; |
30 | } |
31 | |
32 | /* put operator for one zero bit in odd */ |
33 | odd[0] = 0xedb88320; /* CRC-32 polynomial */ |
34 | row = 1; |
35 | for n in 1..GF2_DIM { |
36 | odd[n] = row; |
37 | row <<= 1; |
38 | } |
39 | |
40 | /* put operator for two zero bits in even */ |
41 | gf2_matrix_square(&mut even, &odd); |
42 | |
43 | /* put operator for four zero bits in odd */ |
44 | gf2_matrix_square(&mut odd, &even); |
45 | |
46 | /* apply len2 zeros to crc1 (first square will put the operator for one |
47 | zero byte, eight zero bits, in even) */ |
48 | loop { |
49 | /* apply zeros operator for this bit of len2 */ |
50 | gf2_matrix_square(&mut even, &odd); |
51 | if len2 & 1 == 1 { |
52 | crc1 = gf2_matrix_times(&even, crc1); |
53 | } |
54 | len2 >>= 1; |
55 | |
56 | /* if no more bits set, then done */ |
57 | if len2 == 0 { |
58 | break; |
59 | } |
60 | |
61 | /* another iteration of the loop with odd and even swapped */ |
62 | gf2_matrix_square(&mut odd, &even); |
63 | if len2 & 1 == 1 { |
64 | crc1 = gf2_matrix_times(&odd, crc1); |
65 | } |
66 | len2 >>= 1; |
67 | |
68 | /* if no more bits set, then done */ |
69 | if len2 == 0 { |
70 | break; |
71 | } |
72 | } |
73 | |
74 | /* return combined crc */ |
75 | crc1 ^= crc2; |
76 | return crc1; |
77 | } |
78 | |