arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
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)]
 6pub struct MmrProof {
 7    /// The state of the MMR when the MmrProof was created.
 8    pub forest: usize,
 9
10    /// The position of the leaf value on this MmrProof.
11    pub position: usize,
12
13    /// The Merkle opening, starting from the value's sibling up to and excluding the root of the
14    /// responsible tree.
15    pub merkle_path: MerklePath,
16}
17
18impl MmrProof {
19    /// Converts the leaf global position into a local position that can be used to verify the
20    /// merkle_path.
21    pub fn relative_pos(&self) -> usize {
22        let tree_bit = leaf_to_corresponding_tree(self.position, self.forest)
23            .expect("position must be part of the forest");
24        let forest_before = self.forest & high_bitmask(tree_bit + 1);
25        self.position - forest_before
26    }
27
28    pub fn peak_index(&self) -> usize {
29        let root = leaf_to_corresponding_tree(self.position, self.forest)
30            .expect("position must be part of the forest");
31        (self.forest.count_ones() - root - 1) as usize
32    }
33}