miden-crypto/src/merkle/mmr/proof.rs
src/merkle/mmr/proof.rsRaw
1/// The representation of a single Merkle path.
2use super::super::MerklePath;
3use super::full::{high_bitmask, leaf_to_corresponding_tree};
4
5#[derive(Debug, Clone, PartialEq)]
6#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
7pub struct MmrProof {
8 /// The state of the MMR when the MmrProof was created.
9 pub forest: usize,
10
11 /// The position of the leaf value on this MmrProof.
12 pub position: usize,
13
14 /// The Merkle opening, starting from the value's sibling up to and excluding the root of the
15 /// responsible tree.
16 pub merkle_path: MerklePath,
17}
18
19impl MmrProof {
20 /// Converts the leaf global position into a local position that can be used to verify the
21 /// merkle_path.
22 pub fn relative_pos(&self) -> usize {
23 let tree_bit = leaf_to_corresponding_tree(self.position, self.forest)
24 .expect("position must be part of the forest");
25 let forest_before = self.forest & high_bitmask(tree_bit + 1);
26 self.position - forest_before
27 }
28
29 pub fn peak_index(&self) -> usize {
30 let root = leaf_to_corresponding_tree(self.position, self.forest)
31 .expect("position must be part of the forest");
32 (self.forest.count_ones() - root - 1) as usize
33 }
34}