|
1 | | -//! This module provides a function to combine CRCs of two sequences of bytes. |
| 1 | +//! CRC Combination Module |
2 | 2 | //! |
3 | | -//! It is based on the work of Mark Adler and is designed to be used with |
4 | | -//! different CRC algorithms. |
| 3 | +//! This module provides functionality to combine two CRC checksums into a single CRC |
| 4 | +//! value that represents the concatenated data sequence. This is useful for: |
| 5 | +//! |
| 6 | +//! - **Distributed computing**: Calculate CRCs on different chunks in parallel, then combine |
| 7 | +//! - **Incremental updates**: Append new data without recalculating the entire checksum |
| 8 | +//! - **Merkle-tree-like structures**: Build hierarchical CRC structures |
| 9 | +//! |
| 10 | +//! # Algorithm |
| 11 | +//! |
| 12 | +//! Based on Mark Adler's generalized CRC combination algorithm, which uses Galois Field (GF(2)) |
| 13 | +//! matrix operations to efficiently "insert" zeros between two checksums. The key insight is |
| 14 | +//! that CRC can be viewed as polynomial multiplication in GF(2), and appending zeros is |
| 15 | +//! equivalent to multiplying by x^n (where n is the number of zeros). |
| 16 | +//! |
| 17 | +//! # Performance |
| 18 | +//! |
| 19 | +//! The combination operation runs in O(log N) time where N is the length of the second |
| 20 | +//! sequence, achieved through matrix squaring to efficiently compute the zero-insertion operator. |
| 21 | +//! |
| 22 | +//! # References |
| 23 | +//! |
| 24 | +//! - [Mark Adler's StackOverflow answer](https://stackoverflow.com/questions/29915764/generic-crc-8-16-32-64-combine-implementation/29928573#29928573) |
| 25 | +//! - [Ross Williams' CRC tutorial](http://www.ross.net/crc/download/crc_v3.txt) |
5 | 26 | /* |
6 | 27 | Derived from this excellent answer by Mark Adler on StackOverflow: |
7 | 28 | https://stackoverflow.com/questions/29915764/generic-crc-8-16-32-64-combine-implementation/29928573#29928573 |
@@ -175,14 +196,6 @@ fn reflect_poly(poly: u64, width: u32) -> u64 { |
175 | 196 | shifted & mask |
176 | 197 | } |
177 | 198 |
|
178 | | -fn bit_reverse(mut forward: u64) -> u64 { |
179 | | - let mut reversed = 0; |
180 | | - |
181 | | - for _ in 0..64 { |
182 | | - reversed <<= 1; |
183 | | - reversed |= forward & 1; |
184 | | - forward >>= 1; |
185 | | - } |
186 | | - |
187 | | - reversed |
| 199 | +fn bit_reverse(forward: u64) -> u64 { |
| 200 | + forward.reverse_bits() |
188 | 201 | } |
0 commit comments