iscc-lib 0.6.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
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
//! Streaming hash types for incremental ISCC code generation.
|
|
2
|
+
//!
|
|
3
|
+
//! Provides `DataHasher`, `InstanceHasher`, and `SumHasher` — streaming
|
|
4
|
+
//! counterparts to `gen_data_code_v0`, `gen_instance_code_v0`, and
|
|
5
|
+
//! `gen_sum_code_v0`. All follow the
|
|
6
|
+
//! `new() → update(&[u8]) → finalize()` pattern for incremental processing
|
|
7
|
+
//! of large files without loading entire contents into memory.
|
|
8
|
+
|
|
9
|
+
use crate::types::{DataCodeResult, InstanceCodeResult, SumCodeResult};
|
|
10
|
+
use crate::{IsccResult, cdc, codec, gen_iscc_code_v0, minhash, utils, xxh32};
|
|
11
|
+
|
|
12
|
+
/// Streaming Instance-Code generator.
|
|
13
|
+
///
|
|
14
|
+
/// Incrementally hashes data with BLAKE3 to produce an ISCC Instance-Code
|
|
15
|
+
/// identical to `gen_instance_code_v0` for the same byte stream.
|
|
16
|
+
pub struct InstanceHasher {
|
|
17
|
+
hasher: blake3::Hasher,
|
|
18
|
+
filesize: u64,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl InstanceHasher {
|
|
22
|
+
/// Create a new `InstanceHasher`.
|
|
23
|
+
pub fn new() -> Self {
|
|
24
|
+
Self {
|
|
25
|
+
hasher: blake3::Hasher::new(),
|
|
26
|
+
filesize: 0,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// Push data into the hasher.
|
|
31
|
+
pub fn update(&mut self, data: &[u8]) {
|
|
32
|
+
self.filesize += data.len() as u64;
|
|
33
|
+
self.hasher.update(data);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// Consume the hasher and produce an Instance-Code result.
|
|
37
|
+
///
|
|
38
|
+
/// Equivalent to calling `gen_instance_code_v0` with the concatenation
|
|
39
|
+
/// of all data passed to `update`.
|
|
40
|
+
///
|
|
41
|
+
/// # Errors
|
|
42
|
+
///
|
|
43
|
+
/// Returns `IsccError::InvalidInput` if `bits` is not a multiple of 32
|
|
44
|
+
/// from 32 to 256.
|
|
45
|
+
pub fn finalize(self, bits: u32) -> IsccResult<InstanceCodeResult> {
|
|
46
|
+
let digest = self.hasher.finalize();
|
|
47
|
+
let datahash = utils::blake3_multihash_hex(&digest);
|
|
48
|
+
let component = codec::encode_component(
|
|
49
|
+
codec::MainType::Instance,
|
|
50
|
+
codec::SubType::None,
|
|
51
|
+
codec::Version::V0,
|
|
52
|
+
bits,
|
|
53
|
+
digest.as_bytes(),
|
|
54
|
+
)?;
|
|
55
|
+
Ok(InstanceCodeResult {
|
|
56
|
+
iscc: format!("ISCC:{component}"),
|
|
57
|
+
datahash,
|
|
58
|
+
filesize: self.filesize,
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
impl Default for InstanceHasher {
|
|
64
|
+
/// Create a new `InstanceHasher` (delegates to `new()`).
|
|
65
|
+
fn default() -> Self {
|
|
66
|
+
Self::new()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Streaming Data-Code generator.
|
|
71
|
+
///
|
|
72
|
+
/// Incrementally processes data with content-defined chunking (CDC) and
|
|
73
|
+
/// MinHash to produce an ISCC Data-Code identical to `gen_data_code_v0`
|
|
74
|
+
/// for the same byte stream. Uses a persistent internal buffer to avoid
|
|
75
|
+
/// per-call heap allocations.
|
|
76
|
+
pub struct DataHasher {
|
|
77
|
+
chunk_features: Vec<u32>,
|
|
78
|
+
buf: Vec<u8>,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
impl DataHasher {
|
|
82
|
+
/// Create a new `DataHasher`.
|
|
83
|
+
pub fn new() -> Self {
|
|
84
|
+
Self {
|
|
85
|
+
chunk_features: Vec::new(),
|
|
86
|
+
buf: Vec::new(),
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Push data into the hasher.
|
|
91
|
+
///
|
|
92
|
+
/// Appends data to the internal buffer (which starts with the retained
|
|
93
|
+
/// tail from the previous call), runs CDC, hashes all complete chunks,
|
|
94
|
+
/// and shifts the last chunk (tail) to the front of the buffer for the
|
|
95
|
+
/// next call. The buffer is reused across calls to avoid allocations.
|
|
96
|
+
pub fn update(&mut self, data: &[u8]) {
|
|
97
|
+
self.buf.extend_from_slice(data);
|
|
98
|
+
|
|
99
|
+
let chunks = cdc::alg_cdc_chunks_unchecked(&self.buf, false, cdc::DATA_AVG_CHUNK_SIZE);
|
|
100
|
+
|
|
101
|
+
// Process all chunks except the last (which becomes the new tail).
|
|
102
|
+
// This mirrors the Python `push()` method's `prev_chunk` pattern.
|
|
103
|
+
let mut prev_chunk: Option<&[u8]> = None;
|
|
104
|
+
for chunk in &chunks {
|
|
105
|
+
if let Some(pc) = prev_chunk {
|
|
106
|
+
self.chunk_features.push(xxh32::xxh32(pc, 0));
|
|
107
|
+
}
|
|
108
|
+
prev_chunk = Some(chunk);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Extract tail length before dropping borrows on self.buf
|
|
112
|
+
let tail_len = prev_chunk.map_or(0, |c| c.len());
|
|
113
|
+
drop(chunks);
|
|
114
|
+
|
|
115
|
+
// Shift tail to front of buffer, reusing existing capacity
|
|
116
|
+
let tail_start = self.buf.len() - tail_len;
|
|
117
|
+
self.buf.copy_within(tail_start.., 0);
|
|
118
|
+
self.buf.truncate(tail_len);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Consume the hasher and produce a Data-Code result.
|
|
122
|
+
///
|
|
123
|
+
/// Equivalent to calling `gen_data_code_v0` with the concatenation
|
|
124
|
+
/// of all data passed to `update`.
|
|
125
|
+
///
|
|
126
|
+
/// # Errors
|
|
127
|
+
///
|
|
128
|
+
/// Returns `IsccError::InvalidInput` if `bits` is not a multiple of 32
|
|
129
|
+
/// from 32 to 256.
|
|
130
|
+
pub fn finalize(mut self, bits: u32) -> IsccResult<DataCodeResult> {
|
|
131
|
+
if !self.buf.is_empty() {
|
|
132
|
+
self.chunk_features.push(xxh32::xxh32(&self.buf, 0));
|
|
133
|
+
} else if self.chunk_features.is_empty() {
|
|
134
|
+
// Empty input: ensure at least one feature
|
|
135
|
+
self.chunk_features.push(xxh32::xxh32(b"", 0));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let digest = minhash::alg_minhash_256_inner(&self.chunk_features);
|
|
139
|
+
let component = codec::encode_component(
|
|
140
|
+
codec::MainType::Data,
|
|
141
|
+
codec::SubType::None,
|
|
142
|
+
codec::Version::V0,
|
|
143
|
+
bits,
|
|
144
|
+
&digest,
|
|
145
|
+
)?;
|
|
146
|
+
|
|
147
|
+
Ok(DataCodeResult {
|
|
148
|
+
iscc: format!("ISCC:{component}"),
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl Default for DataHasher {
|
|
154
|
+
/// Create a new `DataHasher` (delegates to `new()`).
|
|
155
|
+
fn default() -> Self {
|
|
156
|
+
Self::new()
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// Streaming composite ISCC-CODE (Sum) generator.
|
|
161
|
+
///
|
|
162
|
+
/// Runs the Data-Code (CDC/MinHash) and Instance-Code (BLAKE3) algorithms in a
|
|
163
|
+
/// single pass over the input by feeding the same bytes to an inner
|
|
164
|
+
/// `DataHasher` and `InstanceHasher`, then composes the final ISCC-CODE.
|
|
165
|
+
/// Produces output identical to `gen_sum_code_v0` for the same byte stream.
|
|
166
|
+
pub struct SumHasher {
|
|
167
|
+
data_hasher: DataHasher,
|
|
168
|
+
instance_hasher: InstanceHasher,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
impl SumHasher {
|
|
172
|
+
/// Create a new `SumHasher`.
|
|
173
|
+
pub fn new() -> Self {
|
|
174
|
+
Self {
|
|
175
|
+
data_hasher: DataHasher::new(),
|
|
176
|
+
instance_hasher: InstanceHasher::new(),
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Push data into both inner hashers in a single pass.
|
|
181
|
+
pub fn update(&mut self, data: &[u8]) {
|
|
182
|
+
self.data_hasher.update(data);
|
|
183
|
+
self.instance_hasher.update(data);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Consume the hasher and produce a composite ISCC-CODE result.
|
|
187
|
+
///
|
|
188
|
+
/// Finalizes the inner Data-Code and Instance-Code, then composes them via
|
|
189
|
+
/// `gen_iscc_code_v0`. When `add_units` is `true`, the result includes the
|
|
190
|
+
/// individual Data-Code and Instance-Code ISCC strings at the requested
|
|
191
|
+
/// `bits` precision. Equivalent to calling `gen_sum_code_v0` on a file
|
|
192
|
+
/// containing the concatenation of all data passed to `update`.
|
|
193
|
+
///
|
|
194
|
+
/// # Errors
|
|
195
|
+
///
|
|
196
|
+
/// Returns `IsccError::InvalidInput` if `bits` is not a multiple of 32
|
|
197
|
+
/// from 64 to 256.
|
|
198
|
+
pub fn finalize(self, bits: u32, wide: bool, add_units: bool) -> IsccResult<SumCodeResult> {
|
|
199
|
+
let data_result = self.data_hasher.finalize(bits)?;
|
|
200
|
+
let instance_result = self.instance_hasher.finalize(bits)?;
|
|
201
|
+
|
|
202
|
+
// Borrow strings for gen_iscc_code_v0 before potentially moving them into units.
|
|
203
|
+
let iscc_result = gen_iscc_code_v0(&[&data_result.iscc, &instance_result.iscc], wide)?;
|
|
204
|
+
|
|
205
|
+
let units = if add_units {
|
|
206
|
+
Some(vec![data_result.iscc, instance_result.iscc])
|
|
207
|
+
} else {
|
|
208
|
+
None
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
Ok(SumCodeResult {
|
|
212
|
+
iscc: iscc_result.iscc,
|
|
213
|
+
datahash: instance_result.datahash,
|
|
214
|
+
filesize: instance_result.filesize,
|
|
215
|
+
units,
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
impl Default for SumHasher {
|
|
221
|
+
/// Create a new `SumHasher` (delegates to `new()`).
|
|
222
|
+
fn default() -> Self {
|
|
223
|
+
Self::new()
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#[cfg(test)]
|
|
228
|
+
mod tests {
|
|
229
|
+
use super::*;
|
|
230
|
+
use crate::{gen_data_code_v0, gen_instance_code_v0, gen_iscc_code_v0, gen_sum_code_v0};
|
|
231
|
+
|
|
232
|
+
// ---- InstanceHasher tests ----
|
|
233
|
+
|
|
234
|
+
#[test]
|
|
235
|
+
fn test_instance_hasher_empty() {
|
|
236
|
+
let ih = InstanceHasher::new();
|
|
237
|
+
let streaming = ih.finalize(64).unwrap();
|
|
238
|
+
let oneshot = gen_instance_code_v0(b"", 64).unwrap();
|
|
239
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
240
|
+
assert_eq!(streaming.datahash, oneshot.datahash);
|
|
241
|
+
assert_eq!(streaming.filesize, oneshot.filesize);
|
|
242
|
+
assert_eq!(streaming.filesize, 0);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#[test]
|
|
246
|
+
fn test_instance_hasher_small_data() {
|
|
247
|
+
let data = b"Hello, ISCC World!";
|
|
248
|
+
let mut ih = InstanceHasher::new();
|
|
249
|
+
ih.update(data);
|
|
250
|
+
let streaming = ih.finalize(64).unwrap();
|
|
251
|
+
let oneshot = gen_instance_code_v0(data, 64).unwrap();
|
|
252
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
253
|
+
assert_eq!(streaming.datahash, oneshot.datahash);
|
|
254
|
+
assert_eq!(streaming.filesize, oneshot.filesize);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#[test]
|
|
258
|
+
fn test_instance_hasher_multi_chunk() {
|
|
259
|
+
let data = b"The quick brown fox jumps over the lazy dog";
|
|
260
|
+
let mut ih = InstanceHasher::new();
|
|
261
|
+
ih.update(&data[..10]);
|
|
262
|
+
ih.update(&data[10..25]);
|
|
263
|
+
ih.update(&data[25..]);
|
|
264
|
+
let streaming = ih.finalize(64).unwrap();
|
|
265
|
+
let oneshot = gen_instance_code_v0(data, 64).unwrap();
|
|
266
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
267
|
+
assert_eq!(streaming.datahash, oneshot.datahash);
|
|
268
|
+
assert_eq!(streaming.filesize, oneshot.filesize);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[test]
|
|
272
|
+
fn test_instance_hasher_byte_at_a_time() {
|
|
273
|
+
let data = b"streaming byte by byte";
|
|
274
|
+
let mut ih = InstanceHasher::new();
|
|
275
|
+
for &b in data.iter() {
|
|
276
|
+
ih.update(&[b]);
|
|
277
|
+
}
|
|
278
|
+
let streaming = ih.finalize(128).unwrap();
|
|
279
|
+
let oneshot = gen_instance_code_v0(data, 128).unwrap();
|
|
280
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
281
|
+
assert_eq!(streaming.datahash, oneshot.datahash);
|
|
282
|
+
assert_eq!(streaming.filesize, oneshot.filesize);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
#[test]
|
|
286
|
+
fn test_instance_hasher_default() {
|
|
287
|
+
let ih = InstanceHasher::default();
|
|
288
|
+
let streaming = ih.finalize(64).unwrap();
|
|
289
|
+
let oneshot = gen_instance_code_v0(b"", 64).unwrap();
|
|
290
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
#[test]
|
|
294
|
+
fn test_instance_hasher_various_bits() {
|
|
295
|
+
let data = b"test various bit widths";
|
|
296
|
+
for bits in [64, 128, 256] {
|
|
297
|
+
let mut ih = InstanceHasher::new();
|
|
298
|
+
ih.update(data);
|
|
299
|
+
let streaming = ih.finalize(bits).unwrap();
|
|
300
|
+
let oneshot = gen_instance_code_v0(data, bits).unwrap();
|
|
301
|
+
assert_eq!(streaming.iscc, oneshot.iscc, "bits={bits}");
|
|
302
|
+
assert_eq!(streaming.datahash, oneshot.datahash, "bits={bits}");
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#[test]
|
|
307
|
+
fn test_instance_hasher_conformance() {
|
|
308
|
+
let data = crate::conformance::test_data().unwrap();
|
|
309
|
+
let cases = crate::conformance::cases(&data, "gen_instance_code_v0").unwrap();
|
|
310
|
+
|
|
311
|
+
for (name, tc) in cases {
|
|
312
|
+
let (input_bytes, bits) = crate::conformance::stream_inputs(tc)
|
|
313
|
+
.unwrap_or_else(|| panic!("unparsable inputs in {name}"));
|
|
314
|
+
|
|
315
|
+
// One-shot reference
|
|
316
|
+
let oneshot = gen_instance_code_v0(&input_bytes, bits)
|
|
317
|
+
.unwrap_or_else(|e| panic!("gen_instance_code_v0 failed for {name}: {e}"));
|
|
318
|
+
|
|
319
|
+
// Streaming — single update
|
|
320
|
+
let mut ih = InstanceHasher::new();
|
|
321
|
+
ih.update(&input_bytes);
|
|
322
|
+
let streaming = ih
|
|
323
|
+
.finalize(bits)
|
|
324
|
+
.unwrap_or_else(|e| panic!("InstanceHasher failed for {name}: {e}"));
|
|
325
|
+
|
|
326
|
+
assert_eq!(
|
|
327
|
+
streaming.iscc, oneshot.iscc,
|
|
328
|
+
"ISCC mismatch in test case {name}"
|
|
329
|
+
);
|
|
330
|
+
assert_eq!(
|
|
331
|
+
streaming.datahash, oneshot.datahash,
|
|
332
|
+
"datahash mismatch in test case {name}"
|
|
333
|
+
);
|
|
334
|
+
assert_eq!(
|
|
335
|
+
streaming.filesize, oneshot.filesize,
|
|
336
|
+
"filesize mismatch in test case {name}"
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
// Streaming — multi-chunk (split into 256-byte chunks)
|
|
340
|
+
let mut ih2 = InstanceHasher::new();
|
|
341
|
+
for chunk in input_bytes.chunks(256) {
|
|
342
|
+
ih2.update(chunk);
|
|
343
|
+
}
|
|
344
|
+
let streaming2 = ih2
|
|
345
|
+
.finalize(bits)
|
|
346
|
+
.unwrap_or_else(|e| panic!("InstanceHasher multi-chunk failed for {name}: {e}"));
|
|
347
|
+
|
|
348
|
+
assert_eq!(
|
|
349
|
+
streaming2.iscc, oneshot.iscc,
|
|
350
|
+
"multi-chunk ISCC mismatch in test case {name}"
|
|
351
|
+
);
|
|
352
|
+
assert_eq!(
|
|
353
|
+
streaming2.datahash, oneshot.datahash,
|
|
354
|
+
"multi-chunk datahash mismatch in test case {name}"
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ---- DataHasher tests ----
|
|
360
|
+
|
|
361
|
+
#[test]
|
|
362
|
+
fn test_data_hasher_empty() {
|
|
363
|
+
let dh = DataHasher::new();
|
|
364
|
+
let streaming = dh.finalize(64).unwrap();
|
|
365
|
+
let oneshot = gen_data_code_v0(b"", 64).unwrap();
|
|
366
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
#[test]
|
|
370
|
+
fn test_data_hasher_small_data() {
|
|
371
|
+
let data = b"Hello, ISCC World!";
|
|
372
|
+
let mut dh = DataHasher::new();
|
|
373
|
+
dh.update(data);
|
|
374
|
+
let streaming = dh.finalize(64).unwrap();
|
|
375
|
+
let oneshot = gen_data_code_v0(data, 64).unwrap();
|
|
376
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
#[test]
|
|
380
|
+
fn test_data_hasher_multi_chunk_small() {
|
|
381
|
+
let data = b"The quick brown fox jumps over the lazy dog";
|
|
382
|
+
let mut dh = DataHasher::new();
|
|
383
|
+
dh.update(&data[..10]);
|
|
384
|
+
dh.update(&data[10..25]);
|
|
385
|
+
dh.update(&data[25..]);
|
|
386
|
+
let streaming = dh.finalize(64).unwrap();
|
|
387
|
+
let oneshot = gen_data_code_v0(data, 64).unwrap();
|
|
388
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
#[test]
|
|
392
|
+
fn test_data_hasher_byte_at_a_time() {
|
|
393
|
+
// Small data that fits within a single CDC chunk
|
|
394
|
+
let data = b"streaming byte by byte";
|
|
395
|
+
let mut dh = DataHasher::new();
|
|
396
|
+
for &b in data.iter() {
|
|
397
|
+
dh.update(&[b]);
|
|
398
|
+
}
|
|
399
|
+
let streaming = dh.finalize(64).unwrap();
|
|
400
|
+
let oneshot = gen_data_code_v0(data, 64).unwrap();
|
|
401
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#[test]
|
|
405
|
+
fn test_data_hasher_large_data_multi_chunk() {
|
|
406
|
+
// Generate data large enough to produce multiple CDC chunks
|
|
407
|
+
let data: Vec<u8> = (0..10_000).map(|i| (i % 256) as u8).collect();
|
|
408
|
+
for chunk_size in [1, 256, 1024, 4096] {
|
|
409
|
+
let mut dh = DataHasher::new();
|
|
410
|
+
for chunk in data.chunks(chunk_size) {
|
|
411
|
+
dh.update(chunk);
|
|
412
|
+
}
|
|
413
|
+
let streaming = dh.finalize(64).unwrap();
|
|
414
|
+
let oneshot = gen_data_code_v0(&data, 64).unwrap();
|
|
415
|
+
assert_eq!(
|
|
416
|
+
streaming.iscc, oneshot.iscc,
|
|
417
|
+
"chunk_size={chunk_size} mismatch"
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
#[test]
|
|
423
|
+
fn test_data_hasher_default() {
|
|
424
|
+
let dh = DataHasher::default();
|
|
425
|
+
let streaming = dh.finalize(64).unwrap();
|
|
426
|
+
let oneshot = gen_data_code_v0(b"", 64).unwrap();
|
|
427
|
+
assert_eq!(streaming.iscc, oneshot.iscc);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
#[test]
|
|
431
|
+
fn test_data_hasher_various_bits() {
|
|
432
|
+
let data = b"test various bit widths for data code";
|
|
433
|
+
for bits in [64, 128, 256] {
|
|
434
|
+
let mut dh = DataHasher::new();
|
|
435
|
+
dh.update(data);
|
|
436
|
+
let streaming = dh.finalize(bits).unwrap();
|
|
437
|
+
let oneshot = gen_data_code_v0(data, bits).unwrap();
|
|
438
|
+
assert_eq!(streaming.iscc, oneshot.iscc, "bits={bits}");
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
#[test]
|
|
443
|
+
fn test_data_hasher_conformance() {
|
|
444
|
+
let data = crate::conformance::test_data().unwrap();
|
|
445
|
+
let cases = crate::conformance::cases(&data, "gen_data_code_v0").unwrap();
|
|
446
|
+
|
|
447
|
+
for (name, tc) in cases {
|
|
448
|
+
let (input_bytes, bits) = crate::conformance::stream_inputs(tc)
|
|
449
|
+
.unwrap_or_else(|| panic!("unparsable inputs in {name}"));
|
|
450
|
+
|
|
451
|
+
// One-shot reference
|
|
452
|
+
let oneshot = gen_data_code_v0(&input_bytes, bits)
|
|
453
|
+
.unwrap_or_else(|e| panic!("gen_data_code_v0 failed for {name}: {e}"));
|
|
454
|
+
|
|
455
|
+
// Streaming — single update
|
|
456
|
+
let mut dh = DataHasher::new();
|
|
457
|
+
dh.update(&input_bytes);
|
|
458
|
+
let streaming = dh
|
|
459
|
+
.finalize(bits)
|
|
460
|
+
.unwrap_or_else(|e| panic!("DataHasher failed for {name}: {e}"));
|
|
461
|
+
|
|
462
|
+
assert_eq!(
|
|
463
|
+
streaming.iscc, oneshot.iscc,
|
|
464
|
+
"ISCC mismatch in test case {name}"
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
// Streaming — 256-byte chunks
|
|
468
|
+
let mut dh2 = DataHasher::new();
|
|
469
|
+
for chunk in input_bytes.chunks(256) {
|
|
470
|
+
dh2.update(chunk);
|
|
471
|
+
}
|
|
472
|
+
let streaming2 = dh2
|
|
473
|
+
.finalize(bits)
|
|
474
|
+
.unwrap_or_else(|e| panic!("DataHasher multi-chunk failed for {name}: {e}"));
|
|
475
|
+
|
|
476
|
+
assert_eq!(
|
|
477
|
+
streaming2.iscc, oneshot.iscc,
|
|
478
|
+
"multi-chunk ISCC mismatch in test case {name}"
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
// Streaming — 1-byte chunks (stress test)
|
|
482
|
+
let mut dh3 = DataHasher::new();
|
|
483
|
+
for &b in &input_bytes {
|
|
484
|
+
dh3.update(&[b]);
|
|
485
|
+
}
|
|
486
|
+
let streaming3 = dh3
|
|
487
|
+
.finalize(bits)
|
|
488
|
+
.unwrap_or_else(|e| panic!("DataHasher byte-at-a-time failed for {name}: {e}"));
|
|
489
|
+
|
|
490
|
+
assert_eq!(
|
|
491
|
+
streaming3.iscc, oneshot.iscc,
|
|
492
|
+
"byte-at-a-time ISCC mismatch in test case {name}"
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// ---- SumHasher tests ----
|
|
498
|
+
|
|
499
|
+
/// Compose the expected composite ISCC-CODE for `data` from the one-shot
|
|
500
|
+
/// Data-Code and Instance-Code functions (the streaming source of truth).
|
|
501
|
+
fn expected_sum(data: &[u8], bits: u32, wide: bool) -> SumCodeResult {
|
|
502
|
+
let data_result = gen_data_code_v0(data, bits).unwrap();
|
|
503
|
+
let instance_result = gen_instance_code_v0(data, bits).unwrap();
|
|
504
|
+
let iscc_result =
|
|
505
|
+
gen_iscc_code_v0(&[&data_result.iscc, &instance_result.iscc], wide).unwrap();
|
|
506
|
+
SumCodeResult {
|
|
507
|
+
iscc: iscc_result.iscc,
|
|
508
|
+
datahash: instance_result.datahash,
|
|
509
|
+
filesize: instance_result.filesize,
|
|
510
|
+
units: None,
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
#[test]
|
|
515
|
+
fn test_sum_hasher_empty() {
|
|
516
|
+
let streaming = SumHasher::new().finalize(64, false, false).unwrap();
|
|
517
|
+
let expected = expected_sum(b"", 64, false);
|
|
518
|
+
|
|
519
|
+
assert_eq!(streaming.iscc, expected.iscc);
|
|
520
|
+
assert_eq!(streaming.datahash, expected.datahash);
|
|
521
|
+
assert_eq!(streaming.filesize, expected.filesize);
|
|
522
|
+
assert_eq!(streaming.filesize, 0);
|
|
523
|
+
assert_eq!(streaming.units, None);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
#[test]
|
|
527
|
+
fn test_sum_hasher_small_data() {
|
|
528
|
+
let data = b"Hello, ISCC World!";
|
|
529
|
+
let mut sh = SumHasher::new();
|
|
530
|
+
sh.update(data);
|
|
531
|
+
let streaming = sh.finalize(64, false, false).unwrap();
|
|
532
|
+
let expected = expected_sum(data, 64, false);
|
|
533
|
+
|
|
534
|
+
assert_eq!(streaming.iscc, expected.iscc);
|
|
535
|
+
assert_eq!(streaming.datahash, expected.datahash);
|
|
536
|
+
assert_eq!(streaming.filesize, expected.filesize);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
#[test]
|
|
540
|
+
fn test_sum_hasher_multi_update_invariance() {
|
|
541
|
+
// Large enough to span multiple CDC chunks.
|
|
542
|
+
let data: Vec<u8> = (0..20_000).map(|i| (i % 256) as u8).collect();
|
|
543
|
+
|
|
544
|
+
let mut single = SumHasher::new();
|
|
545
|
+
single.update(&data);
|
|
546
|
+
let single_result = single.finalize(64, false, false).unwrap();
|
|
547
|
+
|
|
548
|
+
// Split the same bytes across three update calls.
|
|
549
|
+
let mut multi = SumHasher::new();
|
|
550
|
+
multi.update(&data[..3000]);
|
|
551
|
+
multi.update(&data[3000..12_000]);
|
|
552
|
+
multi.update(&data[12_000..]);
|
|
553
|
+
let multi_result = multi.finalize(64, false, false).unwrap();
|
|
554
|
+
|
|
555
|
+
assert_eq!(single_result.iscc, multi_result.iscc);
|
|
556
|
+
assert_eq!(single_result.datahash, multi_result.datahash);
|
|
557
|
+
assert_eq!(single_result.filesize, multi_result.filesize);
|
|
558
|
+
|
|
559
|
+
// And both match the one-shot composition.
|
|
560
|
+
let expected = expected_sum(&data, 64, false);
|
|
561
|
+
assert_eq!(multi_result.iscc, expected.iscc);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
#[test]
|
|
565
|
+
fn test_sum_hasher_units_toggle() {
|
|
566
|
+
let data = b"unit toggle test data";
|
|
567
|
+
|
|
568
|
+
let mut sh_on = SumHasher::new();
|
|
569
|
+
sh_on.update(data);
|
|
570
|
+
let with_units = sh_on.finalize(64, false, true).unwrap();
|
|
571
|
+
|
|
572
|
+
let data_result = gen_data_code_v0(data, 64).unwrap();
|
|
573
|
+
let instance_result = gen_instance_code_v0(data, 64).unwrap();
|
|
574
|
+
assert_eq!(
|
|
575
|
+
with_units.units,
|
|
576
|
+
Some(vec![data_result.iscc, instance_result.iscc])
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
let mut sh_off = SumHasher::new();
|
|
580
|
+
sh_off.update(data);
|
|
581
|
+
let without_units = sh_off.finalize(64, false, false).unwrap();
|
|
582
|
+
assert_eq!(without_units.units, None);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
#[test]
|
|
586
|
+
fn test_sum_hasher_wide_mode() {
|
|
587
|
+
let data = b"wide mode comparison test data";
|
|
588
|
+
|
|
589
|
+
let mut narrow = SumHasher::new();
|
|
590
|
+
narrow.update(data);
|
|
591
|
+
let narrow_result = narrow.finalize(128, false, false).unwrap();
|
|
592
|
+
|
|
593
|
+
let mut wide = SumHasher::new();
|
|
594
|
+
wide.update(data);
|
|
595
|
+
let wide_result = wide.finalize(128, true, false).unwrap();
|
|
596
|
+
|
|
597
|
+
// Wide mode at 128 bits produces a different (longer) composite code.
|
|
598
|
+
assert_ne!(narrow_result.iscc, wide_result.iscc);
|
|
599
|
+
// datahash and filesize are unaffected by wide mode.
|
|
600
|
+
assert_eq!(narrow_result.datahash, wide_result.datahash);
|
|
601
|
+
assert_eq!(narrow_result.filesize, wide_result.filesize);
|
|
602
|
+
|
|
603
|
+
// Each matches its one-shot composition.
|
|
604
|
+
assert_eq!(narrow_result.iscc, expected_sum(data, 128, false).iscc);
|
|
605
|
+
assert_eq!(wide_result.iscc, expected_sum(data, 128, true).iscc);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
#[test]
|
|
609
|
+
fn test_sum_hasher_datahash_filesize_match_instance() {
|
|
610
|
+
let data = b"datahash and filesize parity check";
|
|
611
|
+
let mut sh = SumHasher::new();
|
|
612
|
+
sh.update(data);
|
|
613
|
+
let streaming = sh.finalize(64, false, false).unwrap();
|
|
614
|
+
|
|
615
|
+
let instance_result = gen_instance_code_v0(data, 64).unwrap();
|
|
616
|
+
assert_eq!(streaming.datahash, instance_result.datahash);
|
|
617
|
+
assert_eq!(streaming.filesize, instance_result.filesize);
|
|
618
|
+
assert_eq!(streaming.filesize, data.len() as u64);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
#[test]
|
|
622
|
+
fn test_sum_hasher_default() {
|
|
623
|
+
let from_default = SumHasher::default().finalize(64, false, false).unwrap();
|
|
624
|
+
let from_new = SumHasher::new().finalize(64, false, false).unwrap();
|
|
625
|
+
assert_eq!(from_default.iscc, from_new.iscc);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
#[test]
|
|
629
|
+
fn test_sum_hasher_matches_gen_sum_code_v0() {
|
|
630
|
+
use std::io::Write;
|
|
631
|
+
|
|
632
|
+
// Large enough to span multiple CDC chunks.
|
|
633
|
+
let data: Vec<u8> = (0..20_000).map(|i| (i % 256) as u8).collect();
|
|
634
|
+
|
|
635
|
+
let mut tmp = tempfile::NamedTempFile::new().unwrap();
|
|
636
|
+
tmp.write_all(&data).unwrap();
|
|
637
|
+
tmp.flush().unwrap();
|
|
638
|
+
|
|
639
|
+
// Confirm SumHasher is the single source of truth across parameter
|
|
640
|
+
// combinations: same iscc/datahash/filesize/units as the file-based path.
|
|
641
|
+
for &(bits, wide, add_units) in &[
|
|
642
|
+
(64u32, false, false),
|
|
643
|
+
(64u32, false, true),
|
|
644
|
+
(128u32, true, true),
|
|
645
|
+
] {
|
|
646
|
+
let mut sh = SumHasher::new();
|
|
647
|
+
sh.update(&data);
|
|
648
|
+
let streaming = sh.finalize(bits, wide, add_units).unwrap();
|
|
649
|
+
|
|
650
|
+
let file_based = gen_sum_code_v0(tmp.path(), bits, wide, add_units).unwrap();
|
|
651
|
+
|
|
652
|
+
assert_eq!(
|
|
653
|
+
streaming.iscc, file_based.iscc,
|
|
654
|
+
"iscc mismatch (bits={bits}, wide={wide}, add_units={add_units})"
|
|
655
|
+
);
|
|
656
|
+
assert_eq!(streaming.datahash, file_based.datahash);
|
|
657
|
+
assert_eq!(streaming.filesize, file_based.filesize);
|
|
658
|
+
assert_eq!(streaming.units, file_based.units);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/// Assert that a result is an error that names the 256-bit limit.
|
|
663
|
+
fn assert_above_256_error<T: std::fmt::Debug>(result: IsccResult<T>) {
|
|
664
|
+
let err = result.expect_err("bits above 256 must be rejected");
|
|
665
|
+
assert!(err.to_string().contains("256"), "unexpected error: {err}");
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
#[test]
|
|
669
|
+
fn test_data_hasher_finalize_above_256() {
|
|
670
|
+
let mut hasher = DataHasher::new();
|
|
671
|
+
hasher.update(b"data");
|
|
672
|
+
assert_above_256_error(hasher.finalize(288));
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
#[test]
|
|
676
|
+
fn test_instance_hasher_finalize_above_256() {
|
|
677
|
+
let mut hasher = InstanceHasher::new();
|
|
678
|
+
hasher.update(b"data");
|
|
679
|
+
assert_above_256_error(hasher.finalize(288));
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
#[test]
|
|
683
|
+
fn test_sum_hasher_finalize_above_256() {
|
|
684
|
+
let mut hasher = SumHasher::new();
|
|
685
|
+
hasher.update(b"data");
|
|
686
|
+
assert_above_256_error(hasher.finalize(288, false, false));
|
|
687
|
+
}
|
|
688
|
+
}
|