crc/
crc64.rs

1use crate::util::crc64;
2use crc_catalog::Algorithm;
3
4mod bytewise;
5mod nolookup;
6mod slice16;
7
8const fn init(algorithm: &Algorithm<u64>, initial: u64) -> u64 {
9    if algorithm.refin {
10        initial.reverse_bits() >> (64u8 - algorithm.width)
11    } else {
12        initial << (64u8 - algorithm.width)
13    }
14}
15
16const fn finalize(algorithm: &Algorithm<u64>, mut crc: u64) -> u64 {
17    if algorithm.refin ^ algorithm.refout {
18        crc = crc.reverse_bits();
19    }
20    if !algorithm.refout {
21        crc >>= 64u8 - algorithm.width;
22    }
23    crc ^ algorithm.xorout
24}
25
26const fn update_nolookup(mut crc: u64, algorithm: &Algorithm<u64>, bytes: &[u8]) -> u64 {
27    let poly = if algorithm.refin {
28        let poly = algorithm.poly.reverse_bits();
29        poly >> (64u8 - algorithm.width)
30    } else {
31        algorithm.poly << (64u8 - algorithm.width)
32    };
33
34    let mut i = 0;
35    if algorithm.refin {
36        while i < bytes.len() {
37            let to_crc = (crc ^ bytes[i] as u64) & 0xFF;
38            crc = crc64(poly, algorithm.refin, to_crc) ^ (crc >> 8);
39            i += 1;
40        }
41    } else {
42        while i < bytes.len() {
43            let to_crc = ((crc >> 56) ^ bytes[i] as u64) & 0xFF;
44            crc = crc64(poly, algorithm.refin, to_crc) ^ (crc << 8);
45            i += 1;
46        }
47    }
48    crc
49}
50
51const fn update_bytewise(mut crc: u64, reflect: bool, table: &[u64; 256], bytes: &[u8]) -> u64 {
52    let mut i = 0;
53    let len = bytes.len();
54    if reflect {
55        while i < len {
56            let table_index = ((crc ^ bytes[i] as u64) & 0xFF) as usize;
57            crc = table[table_index] ^ (crc >> 8);
58            i += 1;
59        }
60    } else {
61        while i < len {
62            let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize;
63            crc = table[table_index] ^ (crc << 8);
64            i += 1;
65        }
66    }
67    crc
68}
69
70const fn update_slice16(
71    mut crc: u64,
72    reflect: bool,
73    table: &[[u64; 256]; 16],
74    bytes: &[u8],
75) -> u64 {
76    let mut i = 0;
77    let len = bytes.len();
78    if reflect {
79        while i + 16 <= len {
80            let current0 = bytes[i] ^ crc as u8;
81            let current1 = bytes[i + 1] ^ (crc >> 8) as u8;
82            let current2 = bytes[i + 2] ^ (crc >> 16) as u8;
83            let current3 = bytes[i + 3] ^ (crc >> 24) as u8;
84            let current4 = bytes[i + 4] ^ (crc >> 32) as u8;
85            let current5 = bytes[i + 5] ^ (crc >> 40) as u8;
86            let current6 = bytes[i + 6] ^ (crc >> 48) as u8;
87            let current7 = bytes[i + 7] ^ (crc >> 56) as u8;
88
89            crc = table[0][bytes[i + 15] as usize]
90                ^ table[1][bytes[i + 14] as usize]
91                ^ table[2][bytes[i + 13] as usize]
92                ^ table[3][bytes[i + 12] as usize]
93                ^ table[4][bytes[i + 11] as usize]
94                ^ table[5][bytes[i + 10] as usize]
95                ^ table[6][bytes[i + 9] as usize]
96                ^ table[7][bytes[i + 8] as usize]
97                ^ table[8][current7 as usize]
98                ^ table[9][current6 as usize]
99                ^ table[10][current5 as usize]
100                ^ table[11][current4 as usize]
101                ^ table[12][current3 as usize]
102                ^ table[13][current2 as usize]
103                ^ table[14][current1 as usize]
104                ^ table[15][current0 as usize];
105
106            i += 16;
107        }
108
109        while i < len {
110            let table_index = ((crc ^ bytes[i] as u64) & 0xFF) as usize;
111            crc = table[0][table_index] ^ (crc >> 8);
112            i += 1;
113        }
114    } else {
115        while i + 16 <= len {
116            let current0 = bytes[i] ^ (crc >> 56) as u8;
117            let current1 = bytes[i + 1] ^ (crc >> 48) as u8;
118            let current2 = bytes[i + 2] ^ (crc >> 40) as u8;
119            let current3 = bytes[i + 3] ^ (crc >> 32) as u8;
120            let current4 = bytes[i + 4] ^ (crc >> 24) as u8;
121            let current5 = bytes[i + 5] ^ (crc >> 16) as u8;
122            let current6 = bytes[i + 6] ^ (crc >> 8) as u8;
123            let current7 = bytes[i + 7] ^ crc as u8;
124
125            crc = table[0][bytes[i + 15] as usize]
126                ^ table[1][bytes[i + 14] as usize]
127                ^ table[2][bytes[i + 13] as usize]
128                ^ table[3][bytes[i + 12] as usize]
129                ^ table[4][bytes[i + 11] as usize]
130                ^ table[5][bytes[i + 10] as usize]
131                ^ table[6][bytes[i + 9] as usize]
132                ^ table[7][bytes[i + 8] as usize]
133                ^ table[8][current7 as usize]
134                ^ table[9][current6 as usize]
135                ^ table[10][current5 as usize]
136                ^ table[11][current4 as usize]
137                ^ table[12][current3 as usize]
138                ^ table[13][current2 as usize]
139                ^ table[14][current1 as usize]
140                ^ table[15][current0 as usize];
141
142            i += 16;
143        }
144
145        while i < len {
146            let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize;
147            crc = table[0][table_index] ^ (crc << 8);
148            i += 1;
149        }
150    }
151    crc
152}
153
154#[cfg(test)]
155mod test {
156    use crate::*;
157    use crc_catalog::{Algorithm, CRC_64_ECMA_182};
158
159    /// Test this optimized version against the well known implementation to ensure correctness
160    #[test]
161    fn correctness() {
162        let data: &[&str] = &[
163        "",
164        "1",
165        "1234",
166        "123456789",
167        "0123456789ABCDE",
168        "01234567890ABCDEFGHIJK",
169        "01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
170    ];
171
172        pub const CRC_64_ECMA_182_REFLEX: Algorithm<u64> = Algorithm {
173            width: 64,
174            poly: 0x42f0e1eba9ea3693,
175            init: 0x0000000000000000,
176            refin: true,
177            refout: false,
178            xorout: 0x0000000000000000,
179            check: 0x6c40df5f0b497347,
180            residue: 0x0000000000000000,
181        };
182
183        let algs_to_test = [&CRC_64_ECMA_182, &CRC_64_ECMA_182_REFLEX];
184
185        for alg in algs_to_test {
186            for data in data {
187                let crc_slice16 = Crc::<u64, Table<16>>::new(alg);
188                let crc_nolookup = Crc::<u64, NoTable>::new(alg);
189                let expected = Crc::<u64, Table<1>>::new(alg).checksum(data.as_bytes());
190
191                // Check that doing all at once works as expected
192                assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
193                assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
194
195                let mut digest = crc_slice16.digest();
196                digest.update(data.as_bytes());
197                assert_eq!(digest.finalize(), expected);
198
199                let mut digest = crc_nolookup.digest();
200                digest.update(data.as_bytes());
201                assert_eq!(digest.finalize(), expected);
202
203                // Check that we didn't break updating from multiple sources
204                if data.len() > 2 {
205                    let data = data.as_bytes();
206                    let data1 = &data[..data.len() / 2];
207                    let data2 = &data[data.len() / 2..];
208                    let mut digest = crc_slice16.digest();
209                    digest.update(data1);
210                    digest.update(data2);
211                    assert_eq!(digest.finalize(), expected);
212                    let mut digest = crc_nolookup.digest();
213                    digest.update(data1);
214                    digest.update(data2);
215                    assert_eq!(digest.finalize(), expected);
216                }
217            }
218        }
219    }
220}