Skip to main content

qualia_core_db/q42/chunk_integrity/
mod.rs

1//! Full BLAKE3 chunk integrity. This is a digest, not a Bao range proof.
2//!
3//! A 64-byte trailer contains the complete 32-byte BLAKE3 digest of the record
4//! bytes followed by 32 zero bytes. Keeping the trailer aligned leaves the
5//! existing 64-byte records unchanged. Legacy truncated prefixes are not seals.
6
7pub const BLAKE3_TRAILER_BYTES: usize = 64;
8
9/// Construct an aligned trailer without allocating or truncating the digest.
10pub fn blake3_trailer(records: &[u8]) -> [u8; BLAKE3_TRAILER_BYTES] {
11    let mut trailer = [0; BLAKE3_TRAILER_BYTES];
12    trailer[..32].copy_from_slice(blake3::hash(records).as_bytes());
13    trailer
14}
15
16/// Verify all 256 digest bits, exact trailer size, and reserved zero bytes.
17pub fn verify_blake3_trailer(records: &[u8], trailer: &[u8]) -> bool {
18    trailer.len() == BLAKE3_TRAILER_BYTES
19        && trailer[32..].iter().all(|&b| b == 0)
20        && trailer[..32] == *blake3::hash(records).as_bytes()
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn every_digest_byte_and_reserved_byte_is_checked() {
29        let payload = b"full BLAKE3 digest";
30        let good = blake3_trailer(payload);
31        assert!(verify_blake3_trailer(payload, &good));
32        for i in 0..64 {
33            let mut bad = good;
34            bad[i] ^= 1;
35            assert!(!verify_blake3_trailer(payload, &bad), "byte {i}");
36        }
37        assert!(!verify_blake3_trailer(b"changed", &good));
38        assert!(!verify_blake3_trailer(payload, &good[..32]));
39    }
40}
41
42#[cfg(test)]
43mod container_tests;