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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
use super::{MerkleError, MerklePath, MerklePathSet, MerkleTree, NodeIndex, SimpleSmt, Vec, Word};
// MERKLE SET
// ================================================================================================
/// TODO: add docs
#[derive(Clone, Debug)]
pub enum MerkleSet {
MerkleTree(MerkleTree),
SparseMerkleTree(SimpleSmt),
MerklePathSet(MerklePathSet),
}
impl MerkleSet {
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------
/// Returns a new [MerkleSet] instantiated as a Merkle tree from the provided leaves.
///
/// For more information, check `[MerkleTree::new]`.
pub fn new_merkle_tree(leaves: Vec<Word>) -> Result<Self, MerkleError> {
MerkleTree::new(leaves).map(Self::MerkleTree)
}
/// Returns a new [MerkleSet] instantiated as a Sparse Merkle tree from the provided leaves.
///
/// For more information, check `[SimpleSmt::new]`.
pub fn new_sparse_merkle_tree(
keys: Vec<u64>,
values: Vec<Word>,
depth: u8,
) -> Result<Self, MerkleError> {
SimpleSmt::new(keys.into_iter().zip(values.into_iter()), depth).map(Self::SparseMerkleTree)
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns a root of this merkle set.
pub fn root(&self) -> Word {
match self {
Self::MerkleTree(tree) => tree.root(),
Self::SparseMerkleTree(tree) => tree.root(),
Self::MerklePathSet(set) => set.root(),
}
}
/// Returns the maximum depth of this merkle set.
pub fn depth(&self) -> u8 {
match self {
Self::MerkleTree(tree) => tree.depth(),
Self::SparseMerkleTree(tree) => tree.depth(),
Self::MerklePathSet(set) => set.depth(),
}
}
/// Returns a node located at the specified index.
///
/// For more information, check the following concrete implementations:
/// - `[MerkleTree::get_node]`
/// - `[SimpleSmt::get_node]`
/// - `[MerklePathSet::get_node]`
pub fn get_node(&self, index: NodeIndex) -> Result<Word, MerkleError> {
match self {
Self::MerkleTree(tree) => tree.get_node(index),
Self::SparseMerkleTree(tree) => tree.get_node(&index),
Self::MerklePathSet(set) => set.get_node(index),
}
}
/// Returns a Merkle path to a node located at the specified index. The node itself is not
/// included in the path.
///
/// For more information, check the following concrete implementations:
/// - `[MerkleTree::get_path]`
/// - `[SimpleSmt::get_path]`
/// - `[MerklePathSet::get_path]`
pub fn get_path(&self, index: NodeIndex) -> Result<MerklePath, MerkleError> {
match self {
Self::MerkleTree(tree) => tree.get_path(index),
Self::SparseMerkleTree(tree) => tree.get_path(index),
Self::MerklePathSet(set) => set.get_path(index),
}
}
// DATA MUTATORS
// --------------------------------------------------------------------------------------------
/// Replaces the leaf at the specified index with the provided value.
///
/// For more information, check the following concrete implementations:
/// - `[MerkleTree::update_leaf]`
/// - `[SimpleSmt::update_leaf]`
/// - `[MerklePathSet::update_leaf]`
pub fn update_leaf(&mut self, index: u64, value: Word) -> Result<(), MerkleError> {
match self {
Self::MerkleTree(tree) => tree.update_leaf(index, value),
Self::SparseMerkleTree(tree) => tree.update_leaf(index, value),
Self::MerklePathSet(set) => set.update_leaf(index, value),
}
}
}