crc/
crc64.rs

1use crate::table::crc64_table;
2use crate::util::crc64;
3use crate::*;
4use crc_catalog::Algorithm;
5
6impl<const L: usize> Crc<u64, Table<L>>
7where
8    Table<L>: private::Sealed,
9{
10    pub const fn new(algorithm: &'static Algorithm<u64>) -> Self {
11        Self {
12            algorithm,
13            data: crc64_table(algorithm.width, algorithm.poly, algorithm.refin),
14        }
15    }
16
17    pub const fn checksum(&self, bytes: &[u8]) -> u64 {
18        let mut crc = init(self.algorithm, self.algorithm.init);
19        crc = self.update(crc, bytes);
20        finalize(self.algorithm, crc)
21    }
22
23    const fn update(&self, crc: u64, bytes: &[u8]) -> u64 {
24        update_table(crc, self.algorithm, &self.data, bytes)
25    }
26
27    pub const fn digest(&self) -> Digest<u64, Table<L>> {
28        self.digest_with_initial(self.algorithm.init)
29    }
30
31    /// Construct a `Digest` with a given initial value.
32    ///
33    /// This overrides the initial value specified by the algorithm.
34    /// The effects of the algorithm's properties `refin` and `width`
35    /// are applied to the custom initial value.
36    pub const fn digest_with_initial(&self, initial: u64) -> Digest<u64, Table<L>> {
37        let value = init(self.algorithm, initial);
38        Digest::new(self, value)
39    }
40
41    pub const fn table(&self) -> &<Table<L> as Implementation>::Data<u64> {
42        &self.data
43    }
44}
45
46impl<'a, const L: usize> Digest<'a, u64, Table<L>>
47where
48    Table<L>: private::Sealed,
49{
50    const fn new(crc: &'a Crc<u64, Table<L>>, value: u64) -> Self {
51        Digest { crc, value }
52    }
53
54    pub fn update(&mut self, bytes: &[u8]) {
55        self.value = self.crc.update(self.value, bytes);
56    }
57
58    pub const fn finalize(self) -> u64 {
59        finalize(self.crc.algorithm, self.value)
60    }
61}
62
63const fn init(algorithm: &Algorithm<u64>, initial: u64) -> u64 {
64    if algorithm.refin {
65        initial.reverse_bits() >> (64u8 - algorithm.width)
66    } else {
67        initial << (64u8 - algorithm.width)
68    }
69}
70
71const fn finalize(algorithm: &Algorithm<u64>, mut crc: u64) -> u64 {
72    if algorithm.refin ^ algorithm.refout {
73        crc = crc.reverse_bits();
74    }
75    if !algorithm.refout {
76        crc >>= 64u8 - algorithm.width;
77    }
78    crc ^ algorithm.xorout
79}
80
81const fn update_table<const L: usize>(
82    mut crc: u64,
83    algorithm: &Algorithm<u64>,
84    table: &[[u64; 256]; L],
85    bytes: &[u8],
86) -> u64 {
87    let len = bytes.len();
88    let mut i = 0;
89    let reflect = algorithm.refin;
90
91    // Process 16 bytes at a time when L=16
92    if L == 16 {
93        while i + 16 <= len {
94            if reflect {
95                // XOR the first 8 bytes with the current CRC value
96                let current0 = bytes[i] ^ (crc as u8);
97                let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);
98                let current2 = bytes[i + 2] ^ ((crc >> 16) as u8);
99                let current3 = bytes[i + 3] ^ ((crc >> 24) as u8);
100                let current4 = bytes[i + 4] ^ ((crc >> 32) as u8);
101                let current5 = bytes[i + 5] ^ ((crc >> 40) as u8);
102                let current6 = bytes[i + 6] ^ ((crc >> 48) as u8);
103                let current7 = bytes[i + 7] ^ ((crc >> 56) as u8);
104
105                crc = table[0][bytes[i + 15] as usize]
106                    ^ table[1][bytes[i + 14] as usize]
107                    ^ table[2][bytes[i + 13] as usize]
108                    ^ table[3][bytes[i + 12] as usize]
109                    ^ table[4][bytes[i + 11] as usize]
110                    ^ table[5][bytes[i + 10] as usize]
111                    ^ table[6][bytes[i + 9] as usize]
112                    ^ table[7][bytes[i + 8] as usize]
113                    ^ table[8][current7 as usize]
114                    ^ table[9][current6 as usize]
115                    ^ table[10][current5 as usize]
116                    ^ table[11][current4 as usize]
117                    ^ table[12][current3 as usize]
118                    ^ table[13][current2 as usize]
119                    ^ table[14][current1 as usize]
120                    ^ table[15][current0 as usize];
121            } else {
122                // For non-reflected CRC64
123                let current0 = bytes[i] ^ ((crc >> 56) as u8);
124                let current1 = bytes[i + 1] ^ ((crc >> 48) as u8);
125                let current2 = bytes[i + 2] ^ ((crc >> 40) as u8);
126                let current3 = bytes[i + 3] ^ ((crc >> 32) as u8);
127                let current4 = bytes[i + 4] ^ ((crc >> 24) as u8);
128                let current5 = bytes[i + 5] ^ ((crc >> 16) as u8);
129                let current6 = bytes[i + 6] ^ ((crc >> 8) as u8);
130                let current7 = bytes[i + 7] ^ (crc as u8);
131
132                crc = table[0][bytes[i + 15] as usize]
133                    ^ table[1][bytes[i + 14] as usize]
134                    ^ table[2][bytes[i + 13] as usize]
135                    ^ table[3][bytes[i + 12] as usize]
136                    ^ table[4][bytes[i + 11] as usize]
137                    ^ table[5][bytes[i + 10] as usize]
138                    ^ table[6][bytes[i + 9] as usize]
139                    ^ table[7][bytes[i + 8] as usize]
140                    ^ table[8][current7 as usize]
141                    ^ table[9][current6 as usize]
142                    ^ table[10][current5 as usize]
143                    ^ table[11][current4 as usize]
144                    ^ table[12][current3 as usize]
145                    ^ table[13][current2 as usize]
146                    ^ table[14][current1 as usize]
147                    ^ table[15][current0 as usize];
148            }
149            i += 16;
150        }
151    }
152
153    // Process remaining bytes one at a time using the table (for L=1 and L=16)
154    if L > 0 {
155        if reflect {
156            while i < len {
157                let table_index = ((crc ^ bytes[i] as u64) & 0xFF) as usize;
158                crc = table[0][table_index] ^ (crc >> 8);
159                i += 1;
160            }
161        } else {
162            while i < len {
163                let table_index = (((crc >> 56) ^ bytes[i] as u64) & 0xFF) as usize;
164                crc = table[0][table_index] ^ (crc << 8);
165                i += 1;
166            }
167        }
168    } else {
169        // This section is for NoTable case (L=0)
170        let poly = if reflect {
171            let poly = algorithm.poly.reverse_bits();
172            poly >> (64u8 - algorithm.width)
173        } else {
174            algorithm.poly << (64u8 - algorithm.width)
175        };
176
177        if reflect {
178            while i < len {
179                let to_crc = (crc ^ bytes[i] as u64) & 0xFF;
180                crc = crc64(poly, reflect, to_crc) ^ (crc >> 8);
181                i += 1;
182            }
183        } else {
184            while i < len {
185                let to_crc = ((crc >> 56) ^ bytes[i] as u64) & 0xFF;
186                crc = crc64(poly, reflect, to_crc) ^ (crc << 8);
187                i += 1;
188            }
189        }
190    }
191
192    crc
193}
194
195#[cfg(test)]
196mod test {
197    use crate::*;
198    use crc_catalog::{Algorithm, CRC_64_ECMA_182};
199
200    /// Test this optimized version against the well known implementation to ensure correctness
201    #[test]
202    fn correctness() {
203        let data: &[&str] = &[
204            "",
205            "1",
206            "1234",
207            "123456789",
208            "0123456789ABCDE",
209            "01234567890ABCDEFGHIJK",
210            "01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
211        ];
212
213        pub const CRC_64_ECMA_182_REFLEX: Algorithm<u64> = Algorithm {
214            width: 64,
215            poly: 0x42f0e1eba9ea3693,
216            init: 0x0000000000000000,
217            refin: true,
218            refout: false,
219            xorout: 0x0000000000000000,
220            check: 0x6c40df5f0b497347,
221            residue: 0x0000000000000000,
222        };
223
224        let algs_to_test = [&CRC_64_ECMA_182, &CRC_64_ECMA_182_REFLEX];
225
226        for alg in algs_to_test {
227            for data in data {
228                let crc_slice16 = Crc::<u64, Table<16>>::new(alg);
229                let crc_nolookup = Crc::<u64, NoTable>::new(alg);
230                let expected = Crc::<u64, Table<1>>::new(alg).checksum(data.as_bytes());
231
232                // Check that doing all at once works as expected
233                assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
234                assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);
235
236                let mut digest = crc_slice16.digest();
237                digest.update(data.as_bytes());
238                assert_eq!(digest.finalize(), expected);
239
240                let mut digest = crc_nolookup.digest();
241                digest.update(data.as_bytes());
242                assert_eq!(digest.finalize(), expected);
243
244                // Check that we didn't break updating from multiple sources
245                if data.len() > 2 {
246                    let data = data.as_bytes();
247                    let data1 = &data[..data.len() / 2];
248                    let data2 = &data[data.len() / 2..];
249                    let mut digest = crc_slice16.digest();
250                    digest.update(data1);
251                    digest.update(data2);
252                    assert_eq!(digest.finalize(), expected);
253                    let mut digest = crc_nolookup.digest();
254                    digest.update(data1);
255                    digest.update(data2);
256                    assert_eq!(digest.finalize(), expected);
257                }
258            }
259        }
260    }
261}