utreexo/
proof.rs

1use alloc::vec::Vec;
2
3use crate::{hash_intermediate, Direction, Hash, Path};
4
5/// Inclusion proof of a value in a merkle forest
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Proof {
8    // Path is from leaf node to root node
9    pub(crate) path: Path,
10    pub(crate) leaf_hash: Hash,
11    // Sibling hashes are from bottom to top
12    pub(crate) sibling_hashes: Vec<Hash>,
13}
14
15impl Proof {
16    /// Returns height of path in proof
17    #[inline]
18    pub fn height(&self) -> usize {
19        self.path.height()
20    }
21
22    /// Returns the number of leaves in the tree of this proof
23    #[inline]
24    pub fn leaves(&self) -> usize {
25        self.path.leaves()
26    }
27
28    /// Verifies current proof with given root hash
29    pub fn verify(&self, root_hash: Hash) -> bool {
30        if self.sibling_hashes.is_empty() {
31            return root_hash == self.leaf_hash;
32        }
33
34        let path = self.path.directions();
35        let mut sibling_hashes = self.sibling_hashes.iter();
36
37        let mut hash = self.leaf_hash;
38
39        for step in path {
40            hash = match step {
41                Direction::Left => hash_intermediate(&hash, sibling_hashes.next().unwrap()),
42                Direction::Right => hash_intermediate(sibling_hashes.next().unwrap(), &hash),
43            }
44        }
45
46        hash == root_hash
47    }
48}