1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use alloc::vec::Vec;
use crate::{hash_intermediate, Direction, Hash, Path};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Proof {
pub(crate) path: Path,
pub(crate) leaf_hash: Hash,
pub(crate) sibling_hashes: Vec<Hash>,
}
impl Proof {
#[inline]
pub fn height(&self) -> usize {
self.path.height()
}
#[inline]
pub fn leaves(&self) -> usize {
self.path.leaves()
}
pub fn verify(&self, root_hash: Hash) -> bool {
if self.sibling_hashes.is_empty() {
return root_hash == self.leaf_hash;
}
let path = self.path.directions();
let mut sibling_hashes = self.sibling_hashes.iter();
let mut hash = self.leaf_hash;
for step in path {
hash = match step {
Direction::Left => hash_intermediate(&hash, sibling_hashes.next().unwrap()),
Direction::Right => hash_intermediate(sibling_hashes.next().unwrap(), &hash),
}
}
hash == root_hash
}
}