1use alloc::vec::Vec;
2
3use crate::{hash_intermediate, Direction, Hash, Path};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Proof {
8 pub(crate) path: Path,
10 pub(crate) leaf_hash: Hash,
11 pub(crate) sibling_hashes: Vec<Hash>,
13}
14
15impl Proof {
16 #[inline]
18 pub fn height(&self) -> usize {
19 self.path.height()
20 }
21
22 #[inline]
24 pub fn leaves(&self) -> usize {
25 self.path.leaves()
26 }
27
28 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}