use crate::hash;
#[derive(Clone, Debug)]
pub struct Proof {
pub levels: Vec<ProofLevel>,
}
#[derive(Clone, Debug)]
pub struct ProofLevel {
pub siblings: Vec<[u8; 32]>,
pub position: usize,
}
pub fn extract_siblings(children: &[[u8; 32]], position: usize) -> Vec<[u8; 32]> {
children.iter()
.enumerate()
.filter(|&(i, _)| i != position)
.map(|(_, h)| *h)
.collect()
}
pub fn reconstruct_children<const B: usize>(
siblings: &[[u8; 32]],
entry: [u8; 32],
position: usize,
) -> Vec<[u8; 32]> {
let mut children = Vec::with_capacity(B);
let mut sib_idx = 0;
for i in 0..B {
if i == position {
children.push(entry);
} else {
children.push(siblings[sib_idx]);
sib_idx += 1;
}
}
children
}
pub fn verify<const B: usize>(
proof: &Proof,
leaf_hash: [u8; 32],
expected_root: [u8; 32],
) -> bool {
let mut current = leaf_hash;
for level in &proof.levels {
if level.siblings.len() != B - 1 || level.position >= B {
return false;
}
let children = reconstruct_children::<B>(&level.siblings, current, level.position);
current = hash::hash_parent(&children);
}
current == expected_root
}