Skip to main content

qualia_core_db/bundle/stream/storage/
leaf.rs

1//! Mathematical inclusion over actual borrowed bytes; no disk-presence or rights claim.
2use super::{StorageError, StorageIdentity};
3use crate::bundle::stream::proof::verify_leaf;
4use crate::net::qdnf::types::StrongDigest;
5pub struct VerifiedLeafView<'a> {
6    identity: StorageIdentity,
7    index: u64,
8    logical_len: usize,
9    bytes: &'a [u8],
10}
11impl<'a> VerifiedLeafView<'a> {
12    pub const fn identity(&self) -> &StorageIdentity {
13        &self.identity
14    }
15    pub const fn leaf_index(&self) -> u64 {
16        self.index
17    }
18    pub const fn logical_length(&self) -> usize {
19        self.logical_len
20    }
21    pub const fn bytes(&self) -> &'a [u8] {
22        self.bytes
23    }
24}
25/// A ciphertext view proves inclusion only, not AEAD validity or authenticated plaintext.
26pub fn verify_leaf_view<'a>(
27    identity: &StorageIdentity,
28    index: u64,
29    bytes: &'a [u8],
30    siblings: &[StrongDigest],
31) -> Result<VerifiedLeafView<'a>, StorageError> {
32    let spec = identity.tree_spec();
33    let logical_len = spec
34        .logical_leaf_length(index)
35        .map_err(StorageError::Proof)?;
36    verify_leaf(&spec, identity.fields().root, index, bytes, siblings)
37        .map_err(StorageError::Proof)?;
38    Ok(VerifiedLeafView {
39        identity: *identity,
40        index,
41        logical_len,
42        bytes,
43    })
44}
45
46impl core::fmt::Debug for VerifiedLeafView<'_> {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        f.debug_struct("VerifiedLeafView")
49            .field("index", &self.index)
50            .field("logical_length", &self.logical_len)
51            .field("encoded_length", &self.bytes.len())
52            .finish_non_exhaustive()
53    }
54}