1use digest::{Digest, Output};
2
3use crate::utils;
4
5#[derive(Debug, Clone)]
6pub enum ProofNode<D: Digest> {
7 Left(Output<D>),
8 Right(Output<D>),
9}
10
11impl<D: Digest> ProofNode<D> {
12 pub fn new(idx: usize, node: Output<D>) -> Self {
13 if idx & 1 == 0 {
14 Self::Left(node)
15 } else {
16 Self::Right(node)
17 }
18 }
19}
20
21#[derive(Debug, Clone)]
22pub struct MerkleProof<D: Digest> {
23 pub root: Output<D>,
24 pub nodes: Vec<ProofNode<D>>,
25 pub leaf: Output<D>,
26}
27
28impl<D: Digest> MerkleProof<D> {
29 pub fn verify(&self) -> bool {
30 let mut leaf = self.leaf.clone();
31
32 for node in &self.nodes {
33 leaf = match node {
34 ProofNode::Left(n) => utils::hash_2_node::<D>(n.clone(), leaf),
35 ProofNode::Right(n) => utils::hash_2_node::<D>(leaf, n.clone()),
36 }
37 }
38
39 self.root == leaf
40 }
41}