| 1 |
//! Exhaustive differential correctness tests for the SIMD-accelerated implementations. |
| 2 |
//! |
| 3 |
//! These compare the public `Hasher` (which selects the best implementation the running CPU |
| 4 |
//! supports) against an independent, table-free, bitwise CRC-32 reference across every length that |
| 5 |
//! crosses an implementation threshold, at several alignments, with several initial states, and |
| 6 |
//! for randomized streaming (multi-`update`) splits. |
| 7 |
|
| 8 |
use crc32fast::Hasher; |
| 9 |
|
| 10 |
/// Independent, bitwise CRC-32/ISO-HDLC reference. `state` uses the same convention as `Hasher` |
| 11 |
/// (the finalized CRC so far; 0 for a fresh hash). |
| 12 |
fn crc32_ref(state: u32, data: &[u8]) -> u32 { |
| 13 |
const POLY: u32 = 0xEDB8_8320; |
| 14 |
let mut c = !state; |
| 15 |
for &byte in data { |
| 16 |
c ^= byte as u32; |
| 17 |
for _ in 0..8 { |
| 18 |
let mask = (c & 1).wrapping_neg(); |
| 19 |
c = (c >> 1) ^ (POLY & mask); |
| 20 |
} |
| 21 |
} |
| 22 |
!c |
| 23 |
} |
| 24 |
|
| 25 |
/// Deterministic, well-mixed test buffer (independent of the reference/impl). |
| 26 |
fn make_buf(len: usize) -> Vec<u8> { |
| 27 |
let mut v = vec![0u8; len]; |
| 28 |
let mut s: u64 = 0x9E37_79B9_7F4A_7C15; |
| 29 |
for b in v.iter_mut() { |
| 30 |
s ^= s << 13; |
| 31 |
s ^= s >> 7; |
| 32 |
s ^= s << 17; |
| 33 |
*b = (s >> 33) as u8; |
| 34 |
} |
| 35 |
v |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn reference_matches_known_vectors() { |
| 40 |
// Anchors the independent reference to the canonical CRC-32 check values. |
| 41 |
assert_eq!(crc32_ref(0, b"123456789"), 0xCBF4_3926); |
| 42 |
assert_eq!(crc32_ref(0, b""), 0x0000_0000); |
| 43 |
assert_eq!( |
| 44 |
crc32_ref(0, b"The quick brown fox jumps over the lazy dog"), |
| 45 |
0x414F_A339 |
| 46 |
); |
| 47 |
// And that hash() agrees with it, i.e. the same convention. |
| 48 |
assert_eq!(crc32fast::hash(b"123456789"), 0xCBF4_3926); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn exhaustive_lengths_alignments_inits() { |
| 53 |
// Base buffer big enough for the largest length plus the largest alignment offset. |
| 54 |
let max_len = 1100usize; |
| 55 |
let base = make_buf(max_len + 64); |
| 56 |
|
| 57 |
let inits = [0u32, 0x1234_5678, 0xFFFF_FFFF, 0x0000_0001]; |
| 58 |
let aligns = [0usize, 1, 2, 3, 7, 8, 15, 16, 31]; |
| 59 |
|
| 60 |
for &align in &aligns { |
| 61 |
for len in 0..=max_len { |
| 62 |
let data = &base[align..align + len]; |
| 63 |
for &init in &inits { |
| 64 |
let mut h = Hasher::new_with_initial(init); |
| 65 |
h.update(data); |
| 66 |
assert_eq!( |
| 67 |
h.finalize(), |
| 68 |
crc32_ref(init, data), |
| 69 |
"single update mismatch: len={len} align={align} init={init:#010x}" |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn large_sizes() { |
| 78 |
for &len in &[ |
| 79 |
2047usize, |
| 80 |
2048, |
| 81 |
2049, |
| 82 |
2175, |
| 83 |
2176, |
| 84 |
2303, |
| 85 |
2304, |
| 86 |
4096, |
| 87 |
4097, |
| 88 |
8192, |
| 89 |
16 * 1024, |
| 90 |
16 * 1024 + 123, |
| 91 |
64 * 1024, |
| 92 |
1024 * 1024 + 7, |
| 93 |
] { |
| 94 |
let data = make_buf(len); |
| 95 |
for &init in &[0u32, 0xDEAD_BEEF] { |
| 96 |
let mut h = Hasher::new_with_initial(init); |
| 97 |
h.update(&data); |
| 98 |
assert_eq!( |
| 99 |
h.finalize(), |
| 100 |
crc32_ref(init, &data), |
| 101 |
"large mismatch: len={len} init={init:#010x}" |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn streaming_random_splits() { |
| 109 |
// Verifies state continuity across many update() calls, with split points that frequently land |
| 110 |
// both below and above the SIMD thresholds. |
| 111 |
let data = make_buf(9000); |
| 112 |
let mut rng: u64 = 0xD1B5_4A32_D192_ED03; |
| 113 |
let mut next = |bound: usize| -> usize { |
| 114 |
rng ^= rng << 13; |
| 115 |
rng ^= rng >> 7; |
| 116 |
rng ^= rng << 17; |
| 117 |
(rng as usize) % bound |
| 118 |
}; |
| 119 |
|
| 120 |
for _ in 0..2000 { |
| 121 |
let init = next(u32::MAX as usize) as u32; |
| 122 |
let mut pos = 0usize; |
| 123 |
let mut h = Hasher::new_with_initial(init); |
| 124 |
while pos < data.len() { |
| 125 |
let remaining = data.len() - pos; |
| 126 |
let chunk = if next(4) == 0 { |
| 127 |
1 + next(remaining.min(300)) |
| 128 |
} else { |
| 129 |
1 + next(remaining.min(600)) |
| 130 |
}; |
| 131 |
let end = (pos + chunk).min(data.len()); |
| 132 |
h.update(&data[pos..end]); |
| 133 |
pos = end; |
| 134 |
} |
| 135 |
assert_eq!( |
| 136 |
h.finalize(), |
| 137 |
crc32_ref(init, &data), |
| 138 |
"streaming mismatch: init={init:#010x}" |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn combine_matches_reference() { |
| 145 |
// Cross-checks Hasher::combine against a straight single-pass reference over the concatenation. |
| 146 |
let a = make_buf(3000); |
| 147 |
let b = make_buf(2500); |
| 148 |
// b uses the same generator, so perturb it to be distinct. |
| 149 |
let b: Vec<u8> = b.iter().map(|x| x ^ 0xA5).collect(); |
| 150 |
|
| 151 |
let mut ha = Hasher::new(); |
| 152 |
ha.update(&a); |
| 153 |
let mut hb = Hasher::new(); |
| 154 |
hb.update(&b); |
| 155 |
ha.combine(&hb); |
| 156 |
|
| 157 |
let mut concat = a.clone(); |
| 158 |
concat.extend_from_slice(&b); |
| 159 |
assert_eq!(ha.finalize(), crc32_ref(0, &concat)); |
| 160 |
} |