iscc-lib 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2027 -0
- data/Cargo.toml +17 -6
- data/README.md +6 -3
- data/core/CLAUDE.md +177 -0
- data/core/Cargo.lock +959 -0
- data/core/Cargo.toml +131 -0
- data/core/README.md +84 -0
- data/core/benches/benchmarks.rs +294 -0
- data/core/benches/iai_benches.rs +171 -0
- data/core/src/cdc.rs +488 -0
- data/core/src/codec.rs +2074 -0
- data/core/src/conformance.rs +754 -0
- data/core/src/dct.rs +148 -0
- data/core/src/lib.rs +2693 -0
- data/core/src/minhash.rs +286 -0
- data/core/src/simhash.rs +356 -0
- data/core/src/streaming.rs +688 -0
- data/core/src/types.rs +116 -0
- data/core/src/utils.rs +361 -0
- data/core/src/wtahash.rs +389 -0
- data/core/src/xxh32.rs +134 -0
- data/core/tests/codec_clean.rs +247 -0
- data/core/tests/data.json +645 -0
- data/core/tests/test_algorithm_primitives.rs +303 -0
- data/core/tests/test_seq.rs +565 -0
- data/core/tests/test_strict_base32.rs +158 -0
- data/core/tests/test_text_utils.rs +157 -0
- data/lib/iscc_lib/version.rb +1 -1
- data/src/lib.rs +108 -25
- metadata +43 -4
data/core/src/wtahash.rs
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
//! Winner-Take-All Hash for video content fingerprinting.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements the WTA-Hash algorithm used to produce similarity-preserving
|
|
4
|
+
//! hashes from MPEG-7 video frame signature vectors. Ported from the
|
|
5
|
+
//! `iscc-core` Python reference implementation (`wtahash.py`).
|
|
6
|
+
|
|
7
|
+
use crate::{IsccError, IsccResult};
|
|
8
|
+
|
|
9
|
+
/// Permutation pairs for WTA-Hash video identification.
|
|
10
|
+
///
|
|
11
|
+
/// Each pair `(i, j)` indexes into a 380-element frame signature vector.
|
|
12
|
+
/// There are 256 pairs, supporting up to 256-bit output hashes.
|
|
13
|
+
const WTA_VIDEO_ID_PERMUTATIONS: [(usize, usize); 256] = [
|
|
14
|
+
(292, 16),
|
|
15
|
+
(219, 247),
|
|
16
|
+
(295, 7),
|
|
17
|
+
(105, 236),
|
|
18
|
+
(251, 142),
|
|
19
|
+
(334, 82),
|
|
20
|
+
(17, 266),
|
|
21
|
+
(250, 167),
|
|
22
|
+
(38, 127),
|
|
23
|
+
(184, 22),
|
|
24
|
+
(215, 71),
|
|
25
|
+
(308, 181),
|
|
26
|
+
(195, 215),
|
|
27
|
+
(145, 345),
|
|
28
|
+
(134, 233),
|
|
29
|
+
(89, 351),
|
|
30
|
+
(155, 338),
|
|
31
|
+
(185, 68),
|
|
32
|
+
(233, 122),
|
|
33
|
+
(225, 314),
|
|
34
|
+
(192, 22),
|
|
35
|
+
(298, 2),
|
|
36
|
+
(120, 68),
|
|
37
|
+
(99, 155),
|
|
38
|
+
(274, 187),
|
|
39
|
+
(122, 160),
|
|
40
|
+
(341, 281),
|
|
41
|
+
(230, 223),
|
|
42
|
+
(240, 33),
|
|
43
|
+
(334, 299),
|
|
44
|
+
(166, 256),
|
|
45
|
+
(80, 114),
|
|
46
|
+
(211, 122),
|
|
47
|
+
(18, 16),
|
|
48
|
+
(254, 154),
|
|
49
|
+
(310, 336),
|
|
50
|
+
(36, 273),
|
|
51
|
+
(41, 76),
|
|
52
|
+
(196, 290),
|
|
53
|
+
(191, 307),
|
|
54
|
+
(76, 57),
|
|
55
|
+
(49, 226),
|
|
56
|
+
(85, 97),
|
|
57
|
+
(178, 221),
|
|
58
|
+
(212, 228),
|
|
59
|
+
(125, 348),
|
|
60
|
+
(140, 73),
|
|
61
|
+
(316, 267),
|
|
62
|
+
(91, 61),
|
|
63
|
+
(136, 233),
|
|
64
|
+
(154, 84),
|
|
65
|
+
(338, 332),
|
|
66
|
+
(89, 90),
|
|
67
|
+
(245, 177),
|
|
68
|
+
(167, 222),
|
|
69
|
+
(114, 2),
|
|
70
|
+
(278, 364),
|
|
71
|
+
(22, 169),
|
|
72
|
+
(163, 124),
|
|
73
|
+
(40, 134),
|
|
74
|
+
(229, 207),
|
|
75
|
+
(298, 81),
|
|
76
|
+
(199, 253),
|
|
77
|
+
(344, 123),
|
|
78
|
+
(376, 268),
|
|
79
|
+
(139, 266),
|
|
80
|
+
(247, 308),
|
|
81
|
+
(255, 32),
|
|
82
|
+
(85, 250),
|
|
83
|
+
(345, 236),
|
|
84
|
+
(205, 69),
|
|
85
|
+
(215, 277),
|
|
86
|
+
(299, 178),
|
|
87
|
+
(275, 198),
|
|
88
|
+
(250, 359),
|
|
89
|
+
(84, 286),
|
|
90
|
+
(225, 50),
|
|
91
|
+
(212, 18),
|
|
92
|
+
(1, 224),
|
|
93
|
+
(274, 33),
|
|
94
|
+
(25, 179),
|
|
95
|
+
(47, 77),
|
|
96
|
+
(55, 311),
|
|
97
|
+
(232, 248),
|
|
98
|
+
(71, 234),
|
|
99
|
+
(223, 256),
|
|
100
|
+
(228, 175),
|
|
101
|
+
(371, 132),
|
|
102
|
+
(357, 234),
|
|
103
|
+
(216, 168),
|
|
104
|
+
(332, 266),
|
|
105
|
+
(267, 78),
|
|
106
|
+
(378, 121),
|
|
107
|
+
(165, 316),
|
|
108
|
+
(16, 351),
|
|
109
|
+
(100, 329),
|
|
110
|
+
(301, 294),
|
|
111
|
+
(321, 245),
|
|
112
|
+
(12, 59),
|
|
113
|
+
(151, 222),
|
|
114
|
+
(126, 367),
|
|
115
|
+
(148, 45),
|
|
116
|
+
(23, 305),
|
|
117
|
+
(281, 54),
|
|
118
|
+
(146, 83),
|
|
119
|
+
(343, 244),
|
|
120
|
+
(72, 184),
|
|
121
|
+
(304, 205),
|
|
122
|
+
(98, 179),
|
|
123
|
+
(93, 40),
|
|
124
|
+
(302, 99),
|
|
125
|
+
(218, 106),
|
|
126
|
+
(49, 350),
|
|
127
|
+
(157, 237),
|
|
128
|
+
(355, 267),
|
|
129
|
+
(369, 216),
|
|
130
|
+
(229, 340),
|
|
131
|
+
(284, 106),
|
|
132
|
+
(136, 305),
|
|
133
|
+
(186, 59),
|
|
134
|
+
(3, 107),
|
|
135
|
+
(217, 312),
|
|
136
|
+
(209, 195),
|
|
137
|
+
(333, 102),
|
|
138
|
+
(35, 216),
|
|
139
|
+
(45, 28),
|
|
140
|
+
(178, 130),
|
|
141
|
+
(184, 233),
|
|
142
|
+
(217, 99),
|
|
143
|
+
(321, 144),
|
|
144
|
+
(238, 355),
|
|
145
|
+
(150, 259),
|
|
146
|
+
(255, 259),
|
|
147
|
+
(134, 207),
|
|
148
|
+
(226, 327),
|
|
149
|
+
(174, 178),
|
|
150
|
+
(371, 141),
|
|
151
|
+
(247, 228),
|
|
152
|
+
(244, 300),
|
|
153
|
+
(245, 42),
|
|
154
|
+
(353, 276),
|
|
155
|
+
(368, 187),
|
|
156
|
+
(369, 207),
|
|
157
|
+
(86, 308),
|
|
158
|
+
(212, 368),
|
|
159
|
+
(288, 33),
|
|
160
|
+
(304, 375),
|
|
161
|
+
(156, 8),
|
|
162
|
+
(302, 167),
|
|
163
|
+
(333, 164),
|
|
164
|
+
(37, 379),
|
|
165
|
+
(203, 312),
|
|
166
|
+
(191, 144),
|
|
167
|
+
(310, 95),
|
|
168
|
+
(123, 86),
|
|
169
|
+
(157, 48),
|
|
170
|
+
(284, 27),
|
|
171
|
+
(112, 291),
|
|
172
|
+
(37, 215),
|
|
173
|
+
(98, 291),
|
|
174
|
+
(292, 224),
|
|
175
|
+
(303, 8),
|
|
176
|
+
(200, 103),
|
|
177
|
+
(173, 294),
|
|
178
|
+
(97, 267),
|
|
179
|
+
(288, 167),
|
|
180
|
+
(24, 336),
|
|
181
|
+
(354, 296),
|
|
182
|
+
(25, 18),
|
|
183
|
+
(289, 187),
|
|
184
|
+
(203, 166),
|
|
185
|
+
(307, 326),
|
|
186
|
+
(87, 80),
|
|
187
|
+
(60, 310),
|
|
188
|
+
(176, 84),
|
|
189
|
+
(15, 370),
|
|
190
|
+
(274, 261),
|
|
191
|
+
(178, 45),
|
|
192
|
+
(203, 224),
|
|
193
|
+
(295, 178),
|
|
194
|
+
(30, 74),
|
|
195
|
+
(227, 361),
|
|
196
|
+
(241, 312),
|
|
197
|
+
(231, 369),
|
|
198
|
+
(226, 309),
|
|
199
|
+
(89, 181),
|
|
200
|
+
(216, 175),
|
|
201
|
+
(286, 262),
|
|
202
|
+
(234, 198),
|
|
203
|
+
(99, 49),
|
|
204
|
+
(221, 328),
|
|
205
|
+
(78, 21),
|
|
206
|
+
(95, 327),
|
|
207
|
+
(324, 97),
|
|
208
|
+
(291, 219),
|
|
209
|
+
(184, 286),
|
|
210
|
+
(192, 25),
|
|
211
|
+
(309, 26),
|
|
212
|
+
(84, 159),
|
|
213
|
+
(114, 25),
|
|
214
|
+
(296, 90),
|
|
215
|
+
(51, 325),
|
|
216
|
+
(289, 184),
|
|
217
|
+
(95, 154),
|
|
218
|
+
(21, 202),
|
|
219
|
+
(306, 219),
|
|
220
|
+
(39, 176),
|
|
221
|
+
(99, 251),
|
|
222
|
+
(83, 86),
|
|
223
|
+
(207, 239),
|
|
224
|
+
(168, 19),
|
|
225
|
+
(88, 90),
|
|
226
|
+
(297, 361),
|
|
227
|
+
(215, 78),
|
|
228
|
+
(262, 328),
|
|
229
|
+
(356, 200),
|
|
230
|
+
(48, 203),
|
|
231
|
+
(60, 120),
|
|
232
|
+
(54, 216),
|
|
233
|
+
(369, 327),
|
|
234
|
+
(159, 370),
|
|
235
|
+
(148, 273),
|
|
236
|
+
(332, 50),
|
|
237
|
+
(176, 267),
|
|
238
|
+
(317, 243),
|
|
239
|
+
(311, 125),
|
|
240
|
+
(272, 148),
|
|
241
|
+
(6, 340),
|
|
242
|
+
(80, 346),
|
|
243
|
+
(197, 355),
|
|
244
|
+
(117, 49),
|
|
245
|
+
(261, 326),
|
|
246
|
+
(242, 51),
|
|
247
|
+
(295, 204),
|
|
248
|
+
(298, 111),
|
|
249
|
+
(147, 181),
|
|
250
|
+
(35, 96),
|
|
251
|
+
(318, 285),
|
|
252
|
+
(271, 13),
|
|
253
|
+
(38, 204),
|
|
254
|
+
(16, 8),
|
|
255
|
+
(334, 220),
|
|
256
|
+
(173, 91),
|
|
257
|
+
(372, 24),
|
|
258
|
+
(183, 166),
|
|
259
|
+
(320, 243),
|
|
260
|
+
(87, 9),
|
|
261
|
+
(105, 65),
|
|
262
|
+
(148, 103),
|
|
263
|
+
(197, 314),
|
|
264
|
+
(279, 299),
|
|
265
|
+
(304, 214),
|
|
266
|
+
(282, 15),
|
|
267
|
+
(64, 2),
|
|
268
|
+
(63, 14),
|
|
269
|
+
(28, 351),
|
|
270
|
+
];
|
|
271
|
+
|
|
272
|
+
/// Compute a WTA-Hash from an integer feature vector.
|
|
273
|
+
///
|
|
274
|
+
/// For each permutation pair `(i, j)`, compares `vec[i]` and `vec[j]`:
|
|
275
|
+
/// - If `vec[i] >= vec[j]`, output bit is 0
|
|
276
|
+
/// - Otherwise, output bit is 1
|
|
277
|
+
///
|
|
278
|
+
/// Bits are packed MSB-first into bytes, matching Python bitarray default
|
|
279
|
+
/// endianness. Returns `bits/8` bytes.
|
|
280
|
+
///
|
|
281
|
+
/// # Constraints
|
|
282
|
+
///
|
|
283
|
+
/// - `vec` must have at least 380 elements (permutation indices go up to 379)
|
|
284
|
+
/// - `bits` must be > 0, divisible by 8, and ≤ 256 (permutation table size)
|
|
285
|
+
pub fn alg_wtahash(vec: &[i64], bits: u32) -> IsccResult<Vec<u8>> {
|
|
286
|
+
if vec.len() < 380 {
|
|
287
|
+
return Err(IsccError::InvalidInput(format!(
|
|
288
|
+
"WTA-Hash input must have at least 380 elements, got {}",
|
|
289
|
+
vec.len()
|
|
290
|
+
)));
|
|
291
|
+
}
|
|
292
|
+
if bits == 0 || bits % 8 != 0 || bits > 256 {
|
|
293
|
+
return Err(IsccError::InvalidInput(format!(
|
|
294
|
+
"WTA-Hash bits must be > 0, divisible by 8, and ≤ 256, got {bits}"
|
|
295
|
+
)));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let n_bytes = (bits / 8) as usize;
|
|
299
|
+
let mut result = vec![0u8; n_bytes];
|
|
300
|
+
|
|
301
|
+
for (bit_idx, &(i, j)) in WTA_VIDEO_ID_PERMUTATIONS
|
|
302
|
+
.iter()
|
|
303
|
+
.enumerate()
|
|
304
|
+
.take(bits as usize)
|
|
305
|
+
{
|
|
306
|
+
if vec[i] < vec[j] {
|
|
307
|
+
// Output bit 1 when vec[i] < vec[j]
|
|
308
|
+
let byte_idx = bit_idx / 8;
|
|
309
|
+
result[byte_idx] |= 1 << (7 - (bit_idx % 8));
|
|
310
|
+
}
|
|
311
|
+
// When vec[i] >= vec[j], bit is 0 (already initialized)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
Ok(result)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
#[cfg(test)]
|
|
318
|
+
mod tests {
|
|
319
|
+
use super::*;
|
|
320
|
+
|
|
321
|
+
#[test]
|
|
322
|
+
fn test_alg_wtahash_all_zeros() {
|
|
323
|
+
// All-zero input should produce all-zero output (all comparisons are >=)
|
|
324
|
+
let vec = vec![0i64; 380];
|
|
325
|
+
let result = alg_wtahash(&vec, 64).unwrap();
|
|
326
|
+
assert_eq!(result, vec![0u8; 8]);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
#[test]
|
|
330
|
+
fn test_alg_wtahash_range() {
|
|
331
|
+
// Range 0..380 should produce deterministic non-zero output
|
|
332
|
+
let vec: Vec<i64> = (0..380).collect();
|
|
333
|
+
let result = alg_wtahash(&vec, 64).unwrap();
|
|
334
|
+
assert_eq!(result.len(), 8);
|
|
335
|
+
// Not all zeros since some pairs will have i < j
|
|
336
|
+
assert!(result.iter().any(|&b| b != 0));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#[test]
|
|
340
|
+
fn test_alg_wtahash_256_bits() {
|
|
341
|
+
// 256-bit output uses all 256 permutation pairs
|
|
342
|
+
let vec: Vec<i64> = (0..380).collect();
|
|
343
|
+
let result = alg_wtahash(&vec, 256).unwrap();
|
|
344
|
+
assert_eq!(result.len(), 32);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
#[test]
|
|
348
|
+
fn test_alg_wtahash_short_input_error() {
|
|
349
|
+
// Input with fewer than 380 elements must be rejected
|
|
350
|
+
let vec = vec![0i64; 100];
|
|
351
|
+
assert!(alg_wtahash(&vec, 64).is_err());
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
#[test]
|
|
355
|
+
fn test_alg_wtahash_zero_bits_error() {
|
|
356
|
+
// bits = 0 is invalid
|
|
357
|
+
let vec = vec![0i64; 380];
|
|
358
|
+
assert!(alg_wtahash(&vec, 0).is_err());
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
#[test]
|
|
362
|
+
fn test_alg_wtahash_non_divisible_bits_error() {
|
|
363
|
+
// bits = 7 is not divisible by 8
|
|
364
|
+
let vec = vec![0i64; 380];
|
|
365
|
+
assert!(alg_wtahash(&vec, 7).is_err());
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[test]
|
|
369
|
+
fn test_alg_wtahash_exceeds_permutations_error() {
|
|
370
|
+
// bits = 512 exceeds the 256-entry permutation table
|
|
371
|
+
let vec = vec![0i64; 380];
|
|
372
|
+
assert!(alg_wtahash(&vec, 512).is_err());
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
#[test]
|
|
376
|
+
fn test_permutation_table_length() {
|
|
377
|
+
// The permutation table has exactly 256 entries
|
|
378
|
+
assert_eq!(WTA_VIDEO_ID_PERMUTATIONS.len(), 256);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
#[test]
|
|
382
|
+
fn test_permutation_indices_in_range() {
|
|
383
|
+
// All permutation indices must be < 380 (frame signature length)
|
|
384
|
+
for &(i, j) in &WTA_VIDEO_ID_PERMUTATIONS {
|
|
385
|
+
assert!(i < 380, "index i={i} out of range");
|
|
386
|
+
assert!(j < 380, "index j={j} out of range");
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
data/core/src/xxh32.rs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
//! XXH32 feature hash for Data-Code and Text-Code.
|
|
2
|
+
//!
|
|
3
|
+
//! Implements the 32-bit xxHash algorithm in safe Rust, following the xxHash
|
|
4
|
+
//! specification (<https://github.com/Cyan4973/xxHash/blob/dev/doc/xxhash_spec.md>).
|
|
5
|
+
//! `iscc-core` hashes content-defined chunks and text n-grams with XXH32 at
|
|
6
|
+
//! seed 0, so the digest is part of the ISCC conformance contract.
|
|
7
|
+
|
|
8
|
+
// Multiplicative constants of the XXH32 specification.
|
|
9
|
+
const PRIME32_1: u32 = 0x9E37_79B1;
|
|
10
|
+
const PRIME32_2: u32 = 0x85EB_CA77;
|
|
11
|
+
const PRIME32_3: u32 = 0xC2B2_AE3D;
|
|
12
|
+
const PRIME32_4: u32 = 0x27D4_EB2F;
|
|
13
|
+
const PRIME32_5: u32 = 0x1656_67B1;
|
|
14
|
+
|
|
15
|
+
/// Mix the little-endian word at `offset` of a stripe into one lane accumulator.
|
|
16
|
+
fn round(lane: u32, stripe: &[u8; 16], offset: usize) -> u32 {
|
|
17
|
+
let word = u32::from_le_bytes([
|
|
18
|
+
stripe[offset],
|
|
19
|
+
stripe[offset + 1],
|
|
20
|
+
stripe[offset + 2],
|
|
21
|
+
stripe[offset + 3],
|
|
22
|
+
]);
|
|
23
|
+
lane.wrapping_add(word.wrapping_mul(PRIME32_2))
|
|
24
|
+
.rotate_left(13)
|
|
25
|
+
.wrapping_mul(PRIME32_1)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Consume every complete 16-byte stripe and return the merged lanes with the unread tail.
|
|
29
|
+
fn consume_stripes(mut data: &[u8], seed: u32) -> (u32, &[u8]) {
|
|
30
|
+
let mut v1 = seed.wrapping_add(PRIME32_1).wrapping_add(PRIME32_2);
|
|
31
|
+
let mut v2 = seed.wrapping_add(PRIME32_2);
|
|
32
|
+
let mut v3 = seed;
|
|
33
|
+
let mut v4 = seed.wrapping_sub(PRIME32_1);
|
|
34
|
+
while let Some((stripe, rest)) = data.split_first_chunk::<16>() {
|
|
35
|
+
v1 = round(v1, stripe, 0);
|
|
36
|
+
v2 = round(v2, stripe, 4);
|
|
37
|
+
v3 = round(v3, stripe, 8);
|
|
38
|
+
v4 = round(v4, stripe, 12);
|
|
39
|
+
data = rest;
|
|
40
|
+
}
|
|
41
|
+
let merged = v1
|
|
42
|
+
.rotate_left(1)
|
|
43
|
+
.wrapping_add(v2.rotate_left(7))
|
|
44
|
+
.wrapping_add(v3.rotate_left(12))
|
|
45
|
+
.wrapping_add(v4.rotate_left(18));
|
|
46
|
+
(merged, data)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Spread every input bit over the whole 32-bit digest.
|
|
50
|
+
fn avalanche(mut acc: u32) -> u32 {
|
|
51
|
+
acc ^= acc >> 15;
|
|
52
|
+
acc = acc.wrapping_mul(PRIME32_2);
|
|
53
|
+
acc ^= acc >> 13;
|
|
54
|
+
acc = acc.wrapping_mul(PRIME32_3);
|
|
55
|
+
acc ^ (acc >> 16)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Compute the XXH32 digest of `data` with `seed`.
|
|
59
|
+
pub(crate) fn xxh32(data: &[u8], seed: u32) -> u32 {
|
|
60
|
+
let (mut acc, mut tail) = if data.len() >= 16 {
|
|
61
|
+
consume_stripes(data, seed)
|
|
62
|
+
} else {
|
|
63
|
+
(seed.wrapping_add(PRIME32_5), data)
|
|
64
|
+
};
|
|
65
|
+
// The specification adds the input length modulo 2^32, so truncation is intended.
|
|
66
|
+
acc = acc.wrapping_add(data.len() as u32);
|
|
67
|
+
while let Some((word, rest)) = tail.split_first_chunk::<4>() {
|
|
68
|
+
acc = acc
|
|
69
|
+
.wrapping_add(u32::from_le_bytes(*word).wrapping_mul(PRIME32_3))
|
|
70
|
+
.rotate_left(17)
|
|
71
|
+
.wrapping_mul(PRIME32_4);
|
|
72
|
+
tail = rest;
|
|
73
|
+
}
|
|
74
|
+
for &byte in tail {
|
|
75
|
+
acc = acc
|
|
76
|
+
.wrapping_add(u32::from(byte).wrapping_mul(PRIME32_5))
|
|
77
|
+
.rotate_left(11)
|
|
78
|
+
.wrapping_mul(PRIME32_1);
|
|
79
|
+
}
|
|
80
|
+
avalanche(acc)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#[cfg(test)]
|
|
84
|
+
mod tests {
|
|
85
|
+
use super::*;
|
|
86
|
+
|
|
87
|
+
/// Return the first `len` bytes of the buffer the xxHash sanity check hashes.
|
|
88
|
+
fn sanity_buffer(len: usize) -> Vec<u8> {
|
|
89
|
+
let mut state = u64::from(PRIME32_1);
|
|
90
|
+
(0..len)
|
|
91
|
+
.map(|_| {
|
|
92
|
+
let byte = (state >> 56) as u8;
|
|
93
|
+
state = state.wrapping_mul(11_400_714_785_074_694_797);
|
|
94
|
+
byte
|
|
95
|
+
})
|
|
96
|
+
.collect()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// The XXH32 known answers of the xxHash sanity check: (length, seed, digest).
|
|
100
|
+
#[test]
|
|
101
|
+
fn test_xxh32_sanity_vectors() {
|
|
102
|
+
let cases = [
|
|
103
|
+
(0, 0, 0x02CC_5D05),
|
|
104
|
+
(0, PRIME32_1, 0x36B7_8AE7),
|
|
105
|
+
(1, 0, 0xCF65_B03E),
|
|
106
|
+
(1, PRIME32_1, 0xB454_5AA4),
|
|
107
|
+
(14, 0, 0x1208_E7E2),
|
|
108
|
+
(14, PRIME32_1, 0x6AF1_D1FE),
|
|
109
|
+
(222, 0, 0x5BD1_1DBD),
|
|
110
|
+
(222, PRIME32_1, 0x5880_3C5F),
|
|
111
|
+
];
|
|
112
|
+
for (len, seed, expected) in cases {
|
|
113
|
+
let digest = xxh32(&sanity_buffer(len), seed);
|
|
114
|
+
assert_eq!(digest, expected, "length {len}, seed {seed:#010x}");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// Every stripe count up to 16 with every tail length, for both sanity seeds.
|
|
119
|
+
///
|
|
120
|
+
/// The expected value is the fold of the digests that two independent
|
|
121
|
+
/// XXH32 implementations produce for the same inputs.
|
|
122
|
+
#[test]
|
|
123
|
+
fn test_xxh32_every_length_up_to_256() {
|
|
124
|
+
let buffer = sanity_buffer(256);
|
|
125
|
+
let mut folded = 0u32;
|
|
126
|
+
for len in 0..=buffer.len() {
|
|
127
|
+
for seed in [0, PRIME32_1] {
|
|
128
|
+
let digest = xxh32(&buffer[..len], seed);
|
|
129
|
+
folded = folded.wrapping_mul(31).wrapping_add(digest);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
assert_eq!(folded, 0x8B2B_990E);
|
|
133
|
+
}
|
|
134
|
+
}
|