trazaeo 0.5.7

Open-source provenance SDK and specification for verifiable EO and climate data workflows
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::content::{build_content_descriptor, ContentDescriptor, ContentDescriptorInput};
use crate::utils::{Chunk, Hash, HasherEngine};
use rayon::prelude::*;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::{self, BufReader, Read};
use std::path::Path;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashAlgorithm {
    Blake3,
}

pub const DEFAULT_STREAM_CHUNK_SIZE: usize = 1024 * 1024;

/// Validates hash algorithm.
pub fn validate_hash_algorithm(algorithm: HashAlgorithm) -> Result<(), &'static str> {
    match algorithm {
        HashAlgorithm::Blake3 => Ok(()),
    }
}

/// Handles init hasher.
pub fn init_hasher(pool_size: usize) -> HasherEngine {
    if pool_size > 1 {
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(pool_size)
            .build()
            .ok();
        HasherEngine { pool }
    } else {
        HasherEngine { pool: None }
    }
}

/// Hashes chunk.
pub fn hash_chunk(_engine: &HasherEngine, chunk: &Chunk) -> Hash {
    let h = blake3::hash(&chunk.data);
    Hash(*h.as_bytes())
}

/// Hashes chunks parallel.
pub fn hash_chunks_parallel(engine: &HasherEngine, chunks: Vec<Chunk>) -> Vec<Hash> {
    if let Some(pool) = &engine.pool {
        pool.install(|| {
            chunks
                .into_par_iter()
                .map(|c| hash_chunk(engine, &c))
                .collect()
        })
    } else {
        chunks.into_iter().map(|c| hash_chunk(engine, &c)).collect()
    }
}

/// Handles blake3 hash with threads.
pub fn blake3_hash_with_threads(data: &[u8], threads: usize) -> Hash {
    if threads <= 1 {
        let h = blake3::hash(data);
        Hash(*h.as_bytes())
    } else {
        let maybe_pool = rayon::ThreadPoolBuilder::new()
            .num_threads(threads)
            .build()
            .ok();
        if let Some(pool) = maybe_pool {
            pool.install(|| {
                let mut hasher = blake3::Hasher::new();
                hasher.update_rayon(data);
                let h = hasher.finalize();
                Hash(*h.as_bytes())
            })
        } else {
            let h = blake3::hash(data);
            Hash(*h.as_bytes())
        }
    }
}

/// Computes rolling hash.
pub fn compute_rolling_hash(chunks: &[Chunk]) -> Hash {
    let mut hasher = blake3::Hasher::new();
    for chunk in chunks {
        let len = chunk.data.len() as u64;
        hasher.update(&len.to_le_bytes());
        hasher.update(&chunk.data);
    }
    let h = hasher.finalize();
    Hash(*h.as_bytes())
}

/// Computes root from hashes.
pub fn compute_root_from_hashes(hashes: &[Hash]) -> Hash {
    let mut hasher = blake3::Hasher::new();
    for (idx, hash) in hashes.iter().enumerate() {
        hasher.update(&(idx as u64).to_le_bytes());
        hasher.update(&hash.0);
    }
    let h = hasher.finalize();
    Hash(*h.as_bytes())
}

fn validate_streaming_config(chunk_size: usize) -> io::Result<()> {
    if chunk_size == 0 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "chunk_size must be greater than zero",
        ));
    }
    validate_hash_algorithm(HashAlgorithm::Blake3).map_err(io::Error::other)
}

/// Hashes reader streaming.
pub fn hash_reader_streaming<R: Read>(
    reader: &mut R,
    chunk_size: usize,
    threads: usize,
) -> io::Result<(Vec<Hash>, Hash)> {
    let (hashes, root, _sha256, _byte_length) =
        hash_reader_streaming_with_sha256(reader, chunk_size, threads)?;
    Ok((hashes, root))
}

fn hash_reader_streaming_with_sha256<R: Read>(
    reader: &mut R,
    chunk_size: usize,
    threads: usize,
) -> io::Result<(Vec<Hash>, Hash, String, u64)> {
    validate_streaming_config(chunk_size)?;

    let engine = init_hasher(threads);
    let mut hashes = Vec::new();
    let mut buffer = vec![0u8; chunk_size];
    let mut root_hasher = blake3::Hasher::new();
    let mut c2pa_hasher = Sha256::new();
    let mut byte_length = 0u64;

    loop {
        let n = reader.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        let chunk = Chunk {
            data: buffer[..n].to_vec(),
        };
        byte_length += n as u64;
        root_hasher.update(&chunk.data);
        c2pa_hasher.update(&chunk.data);
        hashes.push(hash_chunk(&engine, &chunk));
    }

    Ok((
        hashes,
        Hash(*root_hasher.finalize().as_bytes()),
        hex::encode(c2pa_hasher.finalize()),
        byte_length,
    ))
}

/// Hashes reader streaming root.
pub fn hash_reader_streaming_root<R: Read>(
    reader: &mut R,
    chunk_size: usize,
    _threads: usize,
) -> io::Result<Hash> {
    validate_streaming_config(chunk_size)?;

    let mut buffer = vec![0u8; chunk_size];
    let mut root_hasher = blake3::Hasher::new();

    loop {
        let n = reader.read(&mut buffer)?;
        if n == 0 {
            break;
        }
        root_hasher.update(&buffer[..n]);
    }

    let h = root_hasher.finalize();
    Ok(Hash(*h.as_bytes()))
}

/// Hashes file streaming.
pub fn hash_file_streaming<P: AsRef<Path>>(
    path: P,
    chunk_size: usize,
    threads: usize,
) -> io::Result<(Vec<Hash>, Hash)> {
    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    hash_reader_streaming(&mut reader, chunk_size, threads)
}

/// Hashes file streaming root.
pub fn hash_file_streaming_root<P: AsRef<Path>>(
    path: P,
    chunk_size: usize,
    threads: usize,
) -> io::Result<Hash> {
    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    hash_reader_streaming_root(&mut reader, chunk_size, threads)
}

/// Hashes reader content descriptor.
pub fn hash_reader_content_descriptor<R: Read>(
    reader: &mut R,
    artifact_id: &str,
    chunk_size: usize,
    threads: usize,
    media_type: &str,
    created_at: &str,
) -> io::Result<ContentDescriptor> {
    let (hashes, root, sha256, byte_length) =
        hash_reader_streaming_with_sha256(reader, chunk_size, threads)?;
    let mut descriptor = build_content_descriptor(ContentDescriptorInput {
        artifact_id,
        root,
        chunk_size,
        leaf_count: hashes.len(),
        byte_length,
        media_type,
        created_at,
    });
    descriptor
        .c2pa_hard_bindings
        .push(crate::c2pa::C2paHardBinding {
            alg: crate::c2pa::C2PA_HASH_ALG_SHA256.to_string(),
            hash: sha256,
        });
    Ok(descriptor)
}

/// Hashes file content descriptor.
pub fn hash_file_content_descriptor<P: AsRef<Path>>(
    path: P,
    artifact_id: &str,
    chunk_size: usize,
    threads: usize,
    media_type: &str,
    created_at: &str,
) -> io::Result<ContentDescriptor> {
    let path_ref = path.as_ref();
    let byte_length = std::fs::metadata(path_ref)?.len();
    let mut reader = BufReader::new(File::open(path_ref)?);
    let (hashes, root, sha256, _byte_length) =
        hash_reader_streaming_with_sha256(&mut reader, chunk_size, threads)?;
    let mut descriptor = build_content_descriptor(ContentDescriptorInput {
        artifact_id,
        root,
        chunk_size,
        leaf_count: hashes.len(),
        byte_length,
        media_type,
        created_at,
    });
    descriptor
        .c2pa_hard_bindings
        .push(crate::c2pa::C2paHardBinding {
            alg: crate::c2pa::C2PA_HASH_ALG_SHA256.to_string(),
            hash: sha256,
        });
    Ok(descriptor)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;
    use std::io::Write;
    use tempfile::NamedTempFile;

    /// Tests that init hasher respects pool size.
    #[test]
    fn init_hasher_respects_pool_size() {
        assert!(init_hasher(1).pool.is_none());
        assert!(init_hasher(2).pool.is_some());
    }

    /// Tests that hash chunk is deterministic.
    #[test]
    fn hash_chunk_is_deterministic() {
        let engine = init_hasher(1);
        let chunk = Chunk {
            data: b"hello".to_vec(),
        };
        let a = hash_chunk(&engine, &chunk);
        let b = hash_chunk(&engine, &chunk);
        assert_eq!(a, b);
    }

    /// Tests that hash chunks parallel matches sequential.
    #[test]
    fn hash_chunks_parallel_matches_sequential() {
        let chunks = vec![
            Chunk {
                data: b"a".to_vec(),
            },
            Chunk {
                data: b"bc".to_vec(),
            },
            Chunk {
                data: b"def".to_vec(),
            },
        ];
        let seq = hash_chunks_parallel(&init_hasher(1), chunks.clone());
        let par = hash_chunks_parallel(&init_hasher(4), chunks);
        assert_eq!(seq, par);
    }

    /// Tests that blake3 hash with threads consistent across thread counts.
    #[test]
    fn blake3_hash_with_threads_consistent_across_thread_counts() {
        let data = b"consistency check data";
        let single = blake3_hash_with_threads(data, 1);
        let multi = blake3_hash_with_threads(data, 4);
        assert_eq!(single, multi);
    }

    /// Tests that validate hash algorithm accepts blake3.
    #[test]
    fn validate_hash_algorithm_accepts_blake3() {
        assert!(validate_hash_algorithm(HashAlgorithm::Blake3).is_ok());
    }

    /// Tests that compute rolling hash is deterministic.
    #[test]
    fn compute_rolling_hash_is_deterministic() {
        let chunks = vec![
            Chunk {
                data: b"ab".to_vec(),
            },
            Chunk {
                data: b"cd".to_vec(),
            },
        ];
        assert_eq!(compute_rolling_hash(&chunks), compute_rolling_hash(&chunks));
    }

    /// Tests that compute root from hashes is order sensitive.
    #[test]
    fn compute_root_from_hashes_is_order_sensitive() {
        let h1 = Hash([1u8; 32]);
        let h2 = Hash([2u8; 32]);
        let a = compute_root_from_hashes(&[h1.clone(), h2.clone()]);
        let b = compute_root_from_hashes(&[h2, h1]);
        assert_ne!(a, b);
    }

    /// Tests that hash reader streaming matches manual chunk hashes.
    #[test]
    fn hash_reader_streaming_matches_manual_chunk_hashes() {
        let mut reader = Cursor::new(b"abcdefgh".to_vec());
        let (hashes, root) = hash_reader_streaming(&mut reader, 3, 1).expect("streaming hash");
        let chunks = vec![
            Chunk {
                data: b"abc".to_vec(),
            },
            Chunk {
                data: b"def".to_vec(),
            },
            Chunk {
                data: b"gh".to_vec(),
            },
        ];
        let expected_hashes = hash_chunks_parallel(&init_hasher(1), chunks);
        assert_eq!(hashes, expected_hashes);
        assert_eq!(root, Hash(*blake3::hash(b"abcdefgh").as_bytes()));
    }

    /// Tests that hash reader streaming rejects zero chunk size.
    #[test]
    fn hash_reader_streaming_rejects_zero_chunk_size() {
        let mut reader = Cursor::new(b"abc".to_vec());
        let err = hash_reader_streaming(&mut reader, 0, 1).expect_err("must fail");
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
    }

    /// Tests that hash file streaming is deterministic.
    #[test]
    fn hash_file_streaming_is_deterministic() {
        let mut file = NamedTempFile::new().expect("temp file");
        file.write_all(b"streaming-file-content").expect("write");

        let (hashes_a, root_a) = hash_file_streaming(file.path(), 4, 1).expect("hash a");
        let (hashes_b, root_b) = hash_file_streaming(file.path(), 4, 4).expect("hash b");

        assert_eq!(hashes_a, hashes_b);
        assert_eq!(root_a, root_b);
    }

    /// Tests that hash reader streaming root matches full variant root.
    #[test]
    fn hash_reader_streaming_root_matches_full_variant_root() {
        let data = b"abcdefghijklmno".to_vec();
        let mut full_reader = Cursor::new(data.clone());
        let mut root_reader = Cursor::new(data);
        let (_hashes, full_root) = hash_reader_streaming(&mut full_reader, 4, 2).expect("full");
        let root_only = hash_reader_streaming_root(&mut root_reader, 4, 2).expect("root");
        assert_eq!(full_root, root_only);
    }

    /// Tests that hash reader content descriptor reports metadata.
    #[test]
    fn hash_reader_content_descriptor_reports_metadata() {
        let mut reader = Cursor::new(b"abcdefg".to_vec());
        let descriptor = hash_reader_content_descriptor(
            &mut reader,
            "artifact-1",
            3,
            1,
            "application/octet-stream",
            "2026-01-01T00:00:00Z",
        )
        .expect("descriptor");
        assert_eq!(descriptor.artifact_id, "artifact-1");
        assert_eq!(descriptor.leaf_count, 3);
    }
}