Skip to main content

qualia_core_db/bundle/stream/proof/
verify.rs

1use super::{hash::parent, leaf_hash, ProofError, TreeSpec, MAX_SIBLINGS};
2use crate::net::qdnf::types::StrongDigest;
3
4/// Verify exact bytes against a supplied root. This does not authorize consumption.
5pub fn verify_leaf(
6    spec: &TreeSpec,
7    root: StrongDigest,
8    index: u64,
9    bytes: &[u8],
10    siblings: &[StrongDigest],
11) -> Result<(), ProofError> {
12    let mut digest = leaf_hash(spec, index, bytes)?;
13    if siblings.len() > MAX_SIBLINGS {
14        return Err(ProofError::ProofLength);
15    }
16    let mut path = [super::ProofSiblingRange::EMPTY; MAX_SIBLINGS];
17    let depth = super::sibling_ranges(spec.leaf_count(), index, &mut path)?;
18    if siblings.len() != depth {
19        return Err(ProofError::ProofLength);
20    }
21    for (range, sibling) in path[..depth].iter().zip(siblings) {
22        digest = match range.side {
23            super::SiblingSide::Left => parent(*sibling, digest),
24            super::SiblingSide::Right => parent(digest, *sibling),
25        };
26    }
27    // Roots are public commitments; equality does not compare secret material.
28    if digest != root {
29        return Err(ProofError::RootMismatch);
30    }
31    Ok(())
32}