Skip to main content

qualia_core_db/net/qdnf/replication/
originals.rs

1//! Signed originals stored separately from derived projections (E06.4).
2//!
3//! Large objects are digested in [`STREAM_PAGE_BYTES`] pages with a running
4//! SHA-384. Length mismatch is [`QdnfError::Range`]. Digest mismatch is
5//! [`QdnfError::Conflict`]. This module never allocates a whole-graph buffer.
6//!
7//! E06.5: [`QnfExtensionAdopted`] is the evaluation result in
8//! [`crate::net::qdnf::replication::qnf_eval`], not a silent adoption.
9
10use sha2::{Digest, Sha384};
11
12use crate::net::qdnf::errors::QdnfError;
13#[cfg(test)]
14use crate::net::qdnf::replication::qnf_eval::QnfExtensionAdopted;
15use crate::net::qdnf::replication::stream::PAGE_BYTES;
16use crate::net::qdnf::types::StrongDigest;
17
18/// Page size for streaming original bytes. Not a dataset allocation.
19pub const STREAM_PAGE_BYTES: u32 = PAGE_BYTES as u32;
20
21/// Stored original identity: SHA-384 and exact length. Not a projection.
22#[repr(C)]
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub struct OriginalObject {
25    pub digest: StrongDigest,
26    pub len: u32,
27}
28
29/// Digest `bytes` in [`STREAM_PAGE_BYTES`] pages. `out_buf_cap_check` is the
30/// caller-owned buffer ceiling (not allocated here).
31///
32/// [`QdnfError::Malformed`] if `bytes` is empty. [`QdnfError::Capacity`] if
33/// `bytes.len()` exceeds `out_buf_cap_check` or `u32::MAX`.
34pub fn store_original(bytes: &[u8], out_buf_cap_check: u32) -> Result<OriginalObject, QdnfError> {
35    if bytes.is_empty() {
36        return Err(QdnfError::Malformed);
37    }
38    let len = u32::try_from(bytes.len()).map_err(|_| QdnfError::Range)?;
39    if len > out_buf_cap_check {
40        return Err(QdnfError::Capacity);
41    }
42    Ok(OriginalObject {
43        digest: digest_pages(bytes),
44        len,
45    })
46}
47
48/// Projection digest is never substituted for the original identity.
49#[inline]
50pub fn projection_replaces_original() -> bool {
51    false
52}
53
54fn digest_pages(bytes: &[u8]) -> StrongDigest {
55    let mut hasher = Sha384::new();
56    let mut off = 0usize;
57    while off < bytes.len() {
58        let end = core::cmp::min(off + PAGE_BYTES, bytes.len());
59        hasher.update(&bytes[off..end]);
60        off = end;
61    }
62    let out = hasher.finalize();
63    let mut d = StrongDigest::ZERO;
64    d.0.copy_from_slice(&out);
65    d
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use crate::crypto::network::digest::sha384;
72
73    #[test]
74    fn qnf_extension_flag_matches_evaluation() {
75        assert!(!QnfExtensionAdopted);
76        assert!(!projection_replaces_original());
77        assert_eq!(
78            QnfExtensionAdopted,
79            crate::net::qdnf::replication::qnf_eval::EVALUATION.adopted
80        );
81    }
82
83    #[test]
84    fn store_original_matches_whole_sha384() {
85        let bytes = [0x11u8; 80];
86        let orig = store_original(&bytes, 80).unwrap();
87        assert_eq!(orig.len, 80);
88        assert_eq!(orig.digest, sha384(&bytes));
89        assert_eq!(store_original(&bytes, 79), Err(QdnfError::Capacity));
90        assert_eq!(store_original(&[], 8), Err(QdnfError::Malformed));
91    }
92
93    #[test]
94    fn page_stream_digest_matches_across_page_boundary() {
95        let mut bytes = [0u8; PAGE_BYTES + 32];
96        let mut i = 0usize;
97        while i < bytes.len() {
98            bytes[i] = i as u8;
99            i += 1;
100        }
101        let expected = sha384(&bytes);
102        let orig = store_original(&bytes, bytes.len() as u32).unwrap();
103        assert_eq!(orig.digest, expected);
104        assert_eq!(orig.len, bytes.len() as u32);
105    }
106}