This design extends the crc-fast library to support hardware-accelerated CRC-16 computation using the same PCLMULQDQ/PMULL-based approach already implemented for CRC-32 and CRC-64. The key insight from the reference implementations is that CRC-16 computation can be performed by scaling the 16-bit values to 32-bit space, using the existing CRC-32 algorithm infrastructure, and then scaling the result back to 16 bits.
6
7
The implementation follows a two-phase approach:
8
1.**Phase 1**: Extend the key generator (`generate.rs`) to produce correct CRC-16 folding keys
9
2.**Phase 2**: Extend the algorithm module to handle width=16 by leveraging the CRC-32 code path with appropriate scaling
The reference assembly implementations reveal that CRC-16 uses the same PCLMULQDQ algorithm as CRC-32, but with values scaled to 32-bit space:
45
46
**Forward (non-reflected) CRC-16:**
47
1.Scale initial CRC: `initial_crc << 16` (shift left 16 bits)
48
2. Process using CRC-32 algorithm with CRC-16 polynomial scaled to 32 bits
49
3. Scale result: `result >> 16` (shift right 16 bits)
50
51
**Reflected CRC-16:**
52
1.Initial CRC used directly (no scaling needed for reflected)
53
2. Process using CRC-32 algorithm with reflected CRC-16 polynomial
54
3. Result extracted from appropriate register position
55
56
##Components and Interfaces
57
58
###1. Key Generator Extension (`src/generate.rs`)
59
60
The key generator must be extended to handle width=16. CRC-16 uses the same exponents as CRC-32 (32*N distances) because the folding algorithm operates on 128-bit SIMD registers regardless of CRC width.
61
62
```rust
63
/// Exponents for CRC-16 key generation (same as CRC-32)
*A property is a characteristic or behavior that should hold true across all valid executions of a system—essentially, a formal statement about what the system should do. Properties serve as the bridge between human-readable specifications and machine-verifiable correctness guarantees.*
*For any* 16-bit polynomial value P, when generating the polynomial key for forward (non-reflected) CRC-16, the result SHALL equal `(P << 16) | (1 << 32)`.
*For any* valid CRC-16 parameters (polynomial, init, refin, refout, xorout) and any input byte sequence, the computed CRC-16 checksum SHALL match the result from the `crc` crate reference implementation.
###Property 4: CRC-32 and CRC-64 Backwards Compatibility
304
305
*For any* existing CRC-32 or CRC-64 configuration and any input byte sequence, the computed checksum SHALL match the result from the `crc` crate reference implementation, ensuring no regression from CRC-16 changes.
*For any* valid CRC-16 parameters and any two input byte sequences A and B, `checksum_combine(checksum(A), checksum(B), len(B))` SHALL equal `checksum(A + B)`.
312
313
**Validates: Requirements 6.4**
314
315
##Error Handling
316
317
###Invalid Width
318
319
When `CrcParams::new()` is called with an unsupported width (not 16, 32, or 64), the function SHALL panic with a descriptive error message.
320
321
###Invalid Polynomial
322
323
CRC-16 polynomials must fit in 16 bits. If a polynomial larger than 0xFFFF is provided for width=16, the behavior is undefined (caller's responsibility to provide valid parameters).
324
325
###Empty Input
326
327
Empty input (`&[]`) is valid and SHALL return the initial CRC value XORed with xorout, consistent with existing CRC-32 and CRC-64 behavior.
328
329
##Testing Strategy
330
331
###Unit Tests
332
333
Unit tests will verify specific examples and edge cases:
334
335
1.**Check Value Tests**: Verify "123456789" produces correct check values for CRC-16/IBM-SDLC (0x906E) and CRC-16/T10-DIF (0xD0DB)
336
2.**Key Generation Tests**: Verify generated keys match reference values in `KEYS_8BB7_FORWARD` and `KEYS_1021_REVERSE`
337
3.**Empty Input Tests**: Verify empty input returns expected value
338
4.**Constant Definition Tests**: Verify CRC16_IBM_SDLC and CRC16_T10_DIF have correct parameter values
339
340
###Property-Based Tests
341
342
Property-based tests will use the `proptest` crate (already used in the project) with minimum 100 iterations per property:
343
344
1.**Property 1 & 2**: Generate random 16-bit polynomials and verify polynomial key formatting
345
2.**Property 3**: Generate random CRC-16 parameters and random byte sequences, compare against `crc` crate
346
3.**Property 4**: Use existing CRC-32/64 configurations with random byte sequences, compare against `crc` crate
347
4.**Property 5**: Generate random CRC-16 parameters and two random byte sequences, verify combination