1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
pub(crate) mod algorithm;
pub(crate) mod consts;
#[cfg(test)]
mod property_tests {
use crate::crc16::consts::{CRC16_IBM_SDLC, CRC16_T10_DIF};
use crate::test::consts::{RUST_CRC16_IBM_SDLC, RUST_CRC16_T10_DIF};
use crate::test::miri_compatible_proptest_config;
use crate::{
checksum, checksum_combine, checksum_combine_with_params, checksum_with_params,
CrcAlgorithm, CrcParams,
};
use proptest::prelude::*;
proptest! {
#![proptest_config(miri_compatible_proptest_config())]
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// *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.
/// **Validates: Requirements 2.1, 2.2, 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_ibm_sdlc_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
let our_result = checksum(CrcAlgorithm::Crc16IbmSdlc, &data);
let reference_result = RUST_CRC16_IBM_SDLC.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16/IBM-SDLC mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// *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.
/// **Validates: Requirements 2.1, 2.2, 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_t10_dif_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
let our_result = checksum(CrcAlgorithm::Crc16T10Dif, &data);
let reference_result = RUST_CRC16_T10_DIF.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16/T10-DIF mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// Tests checksum_with_params for CRC-16/IBM-SDLC (reflected variant)
/// **Validates: Requirements 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_ibm_sdlc_with_params_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
let our_result = checksum_with_params(CRC16_IBM_SDLC, &data);
let reference_result = RUST_CRC16_IBM_SDLC.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16/IBM-SDLC (with_params) mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// Tests checksum_with_params for CRC-16/T10-DIF (forward variant)
/// **Validates: Requirements 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_t10_dif_with_params_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
let our_result = checksum_with_params(CRC16_T10_DIF, &data);
let reference_result = RUST_CRC16_T10_DIF.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16/T10-DIF (with_params) mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// Tests custom CRC-16 parameters (equivalent to IBM-SDLC) to validate CrcParams::new()
/// **Validates: Requirements 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_custom_reflected_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
// Custom CRC-16 parameters equivalent to CRC-16/IBM-SDLC
let custom_params = CrcParams::new(
"CRC-16/CUSTOM-REFLECTED",
16,
0x1021,
0xFFFF,
true, // reflected
0xFFFF,
0x906E,
);
let our_result = checksum_with_params(custom_params, &data);
let reference_result = RUST_CRC16_IBM_SDLC.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16 custom reflected mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 3: CRC-16 computation matches reference
/// Tests custom CRC-16 parameters (equivalent to T10-DIF) to validate CrcParams::new()
/// **Validates: Requirements 5.5, 6.1, 6.2, 6.3**
#[test]
fn prop_crc16_custom_forward_matches_reference(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
// Custom CRC-16 parameters equivalent to CRC-16/T10-DIF
let custom_params = CrcParams::new(
"CRC-16/CUSTOM-FORWARD",
16,
0x8BB7,
0x0000,
false, // forward (non-reflected)
0x0000,
0xD0DB,
);
let our_result = checksum_with_params(custom_params, &data);
let reference_result = RUST_CRC16_T10_DIF.checksum(&data) as u64;
prop_assert_eq!(
our_result, reference_result,
"CRC-16 custom forward mismatch for {} bytes: our=0x{:04X}, ref=0x{:04X}",
data.len(), our_result, reference_result
);
}
/// Feature: crc16-hardware-acceleration, Property 5: CRC-16 checksum combination round-trip
/// *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)`.
/// **Validates: Requirements 6.4**
#[test]
fn prop_crc16_ibm_sdlc_checksum_combine_roundtrip(
data_a in proptest::collection::vec(any::<u8>(), 0..512),
data_b in proptest::collection::vec(any::<u8>(), 0..512)
) {
let checksum_a = checksum(CrcAlgorithm::Crc16IbmSdlc, &data_a);
let checksum_b = checksum(CrcAlgorithm::Crc16IbmSdlc, &data_b);
let combined = checksum_combine(
CrcAlgorithm::Crc16IbmSdlc,
checksum_a,
checksum_b,
data_b.len() as u64,
);
let mut concatenated = data_a.clone();
concatenated.extend(&data_b);
let expected = checksum(CrcAlgorithm::Crc16IbmSdlc, &concatenated);
prop_assert_eq!(
combined, expected,
"CRC-16/IBM-SDLC combine mismatch: combined=0x{:04X}, expected=0x{:04X}, len_a={}, len_b={}",
combined, expected, data_a.len(), data_b.len()
);
}
/// Feature: crc16-hardware-acceleration, Property 5: CRC-16 checksum combination round-trip
/// *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)`.
/// **Validates: Requirements 6.4**
#[test]
fn prop_crc16_t10_dif_checksum_combine_roundtrip(
data_a in proptest::collection::vec(any::<u8>(), 0..512),
data_b in proptest::collection::vec(any::<u8>(), 0..512)
) {
let checksum_a = checksum(CrcAlgorithm::Crc16T10Dif, &data_a);
let checksum_b = checksum(CrcAlgorithm::Crc16T10Dif, &data_b);
let combined = checksum_combine(
CrcAlgorithm::Crc16T10Dif,
checksum_a,
checksum_b,
data_b.len() as u64,
);
let mut concatenated = data_a.clone();
concatenated.extend(&data_b);
let expected = checksum(CrcAlgorithm::Crc16T10Dif, &concatenated);
prop_assert_eq!(
combined, expected,
"CRC-16/T10-DIF combine mismatch: combined=0x{:04X}, expected=0x{:04X}, len_a={}, len_b={}",
combined, expected, data_a.len(), data_b.len()
);
}
/// Feature: crc16-hardware-acceleration, Property 5: CRC-16 checksum combination round-trip
/// Tests checksum_combine_with_params for CRC-16/IBM-SDLC
/// **Validates: Requirements 6.4**
#[test]
fn prop_crc16_ibm_sdlc_checksum_combine_with_params_roundtrip(
data_a in proptest::collection::vec(any::<u8>(), 0..512),
data_b in proptest::collection::vec(any::<u8>(), 0..512)
) {
let checksum_a = checksum_with_params(CRC16_IBM_SDLC, &data_a);
let checksum_b = checksum_with_params(CRC16_IBM_SDLC, &data_b);
let combined = checksum_combine_with_params(
CRC16_IBM_SDLC,
checksum_a,
checksum_b,
data_b.len() as u64,
);
let mut concatenated = data_a.clone();
concatenated.extend(&data_b);
let expected = checksum_with_params(CRC16_IBM_SDLC, &concatenated);
prop_assert_eq!(
combined, expected,
"CRC-16/IBM-SDLC combine_with_params mismatch: combined=0x{:04X}, expected=0x{:04X}, len_a={}, len_b={}",
combined, expected, data_a.len(), data_b.len()
);
}
/// Feature: crc16-hardware-acceleration, Property 5: CRC-16 checksum combination round-trip
/// Tests checksum_combine_with_params for CRC-16/T10-DIF
/// **Validates: Requirements 6.4**
#[test]
fn prop_crc16_t10_dif_checksum_combine_with_params_roundtrip(
data_a in proptest::collection::vec(any::<u8>(), 0..512),
data_b in proptest::collection::vec(any::<u8>(), 0..512)
) {
let checksum_a = checksum_with_params(CRC16_T10_DIF, &data_a);
let checksum_b = checksum_with_params(CRC16_T10_DIF, &data_b);
let combined = checksum_combine_with_params(
CRC16_T10_DIF,
checksum_a,
checksum_b,
data_b.len() as u64,
);
let mut concatenated = data_a.clone();
concatenated.extend(&data_b);
let expected = checksum_with_params(CRC16_T10_DIF, &concatenated);
prop_assert_eq!(
combined, expected,
"CRC-16/T10-DIF combine_with_params mismatch: combined=0x{:04X}, expected=0x{:04X}, len_a={}, len_b={}",
combined, expected, data_a.len(), data_b.len()
);
}
}
}