mod combine;
mod dispatch;
mod scalar;
#[cfg(target_arch = "aarch64")]
mod aarch64;
#[cfg(target_arch = "x86_64")]
mod x86;
use dispatch::update_state;
pub const CRC32C_POLYNOMIAL: u32 = 0x82F6_3B78;
pub const CRC32C_CHECK: u32 = 0xE306_9283;
#[must_use]
#[inline]
pub fn crc32c(data: &[u8]) -> u32 {
crc32c_append(0, data)
}
#[must_use]
#[inline]
pub fn crc32c_append(previous: u32, data: &[u8]) -> u32 {
!update_state(!previous, data)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Crc32c {
state: u32,
}
impl Crc32c {
#[must_use]
#[inline]
pub const fn new() -> Self {
Self { state: u32::MAX }
}
#[must_use]
#[inline]
pub const fn resume(checksum: u32) -> Self {
Self { state: !checksum }
}
#[inline]
pub fn update(&mut self, data: &[u8]) {
self.state = update_state(self.state, data);
}
#[must_use]
#[inline]
pub fn chain(mut self, data: &[u8]) -> Self {
self.update(data);
self
}
#[must_use]
#[inline]
pub const fn finalize(&self) -> u32 {
!self.state
}
}
impl Default for Crc32c {
fn default() -> Self {
Self::new()
}
}
impl core::hash::Hasher for Crc32c {
fn finish(&self) -> u64 {
u64::from(self.finalize())
}
fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn reference_crc32c(data: &[u8]) -> u32 {
let mut crc: u32 = u32::MAX;
for &byte in data {
crc ^= u32::from(byte);
for _ in 0..8 {
crc = if crc & 1 != 0 {
(crc >> 1) ^ CRC32C_POLYNOMIAL
} else {
crc >> 1
};
}
}
!crc
}
#[test]
fn matches_the_published_check_value() {
assert_eq!(crc32c(b"123456789"), CRC32C_CHECK);
}
#[test]
fn empty_input_is_zero() {
assert_eq!(crc32c(b""), 0);
assert_eq!(Crc32c::new().finalize(), 0);
}
#[test]
fn matches_rfc3720_appendix_vectors() {
assert_eq!(crc32c(&[0x00u8; 32]), 0x8A91_36AA);
assert_eq!(crc32c(&[0xFFu8; 32]), 0x62A8_AB43);
let ascending: Vec<u8> = (0u8..32).collect();
assert_eq!(crc32c(&ascending), 0x46DD_794E);
let descending: Vec<u8> = (0u8..32).rev().collect();
assert_eq!(crc32c(&descending), 0x113F_DB5C);
}
#[test]
fn matches_the_bitwise_reference_on_every_length_up_to_two_blocks() {
let data: Vec<u8> = (0..64u32)
.map(|i| (i.wrapping_mul(31) & 0xFF) as u8)
.collect();
for len in 0..=data.len() {
let slice = &data[..len];
assert_eq!(
crc32c(slice),
reference_crc32c(slice),
"mismatch at length {len}"
);
}
}
#[test]
fn matches_the_bitwise_reference_on_every_length_up_to_256() {
let data: Vec<u8> = (0..256u32)
.map(|i| (i.wrapping_mul(197).wrapping_add(11) & 0xFF) as u8)
.collect();
for len in 0..=data.len() {
let slice = &data[..len];
assert_eq!(
crc32c(slice),
reference_crc32c(slice),
"mismatch at length {len}"
);
}
}
#[test]
fn matches_the_bitwise_reference_on_a_large_buffer() {
let mut data = Vec::with_capacity(4096);
let mut x: u32 = 0x1234_5678;
for _ in 0..4096 {
x = x.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
data.push((x >> 24) as u8);
}
assert_eq!(crc32c(&data), reference_crc32c(&data));
}
#[test]
fn single_byte_values_match_the_reference() {
for byte in 0u8..=255 {
assert_eq!(crc32c(&[byte]), reference_crc32c(&[byte]), "byte {byte}");
}
}
#[test]
fn append_equals_one_shot_at_every_split() {
let data = b"the quick brown fox jumps over the lazy dog";
let whole = crc32c(data);
for split in 0..=data.len() {
let (head, tail) = data.split_at(split);
assert_eq!(crc32c_append(crc32c(head), tail), whole, "split {split}");
}
}
#[test]
fn streaming_equals_one_shot_at_every_split() {
let data = b"AS\x01\x03\x00\x00\x2a\x00\x00\x00payload-bytes";
let whole = crc32c(data);
for split in 0..=data.len() {
let (head, tail) = data.split_at(split);
let mut hasher = Crc32c::new();
hasher.update(head);
hasher.update(tail);
assert_eq!(hasher.finalize(), whole, "split {split}");
}
}
#[test]
fn resume_round_trips_a_finalised_checksum() {
let head = crc32c(b"header");
let mut hasher = Crc32c::resume(head);
hasher.update(b"payload");
assert_eq!(hasher.finalize(), crc32c(b"headerpayload"));
}
#[test]
fn chain_is_equivalent_to_update() {
let chained = Crc32c::new().chain(b"ab").chain(b"cd").finalize();
let mut updated = Crc32c::new();
updated.update(b"ab");
updated.update(b"cd");
assert_eq!(chained, updated.finalize());
assert_eq!(chained, crc32c(b"abcd"));
}
#[test]
fn a_single_flipped_bit_always_changes_the_checksum() {
let base = b"astrs frame payload under integrity check".to_vec();
let expected = crc32c(&base);
for index in 0..base.len() {
for bit in 0..8 {
let mut corrupted = base.clone();
corrupted[index] ^= 1 << bit;
assert_ne!(
crc32c(&corrupted),
expected,
"flipping bit {bit} of byte {index} went unnoticed"
);
}
}
}
#[test]
fn hasher_trait_reports_the_widened_checksum() {
use core::hash::Hasher as _;
let mut hasher = Crc32c::new();
hasher.write(b"123456789");
assert_eq!(hasher.finish(), u64::from(CRC32C_CHECK));
}
#[test]
fn default_matches_new() {
assert_eq!(Crc32c::default(), Crc32c::new());
}
#[test]
fn large_buffers_match_the_reference_across_a_range_of_odd_alignments() {
let mut backing = Vec::with_capacity(1 << 16);
let mut x: u32 = 0x9E37_79B9;
for _ in 0..(1 << 16) {
x = x.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
backing.push((x >> 24) as u8);
}
for offset in 0..8 {
let slice = &backing[offset..];
assert_eq!(crc32c(slice), reference_crc32c(slice), "offset {offset}");
}
}
}