|
| 1 | +//! # Hashed LeanIMT |
| 2 | +//! |
| 3 | +//! Lean Incremental Merkle Tree hashing function wrapper. |
| 4 | +
|
| 5 | +use crate::lean_imt::{LeanIMT, LeanIMTError, MerkleProof}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +/// LeanIMT hasher trait. |
| 9 | +pub trait LeanIMTHasher { |
| 10 | + fn hash(input: &[u8]) -> Vec<u8>; |
| 11 | +} |
| 12 | + |
| 13 | +/// Hashed LeanIMT struct. |
| 14 | +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] |
| 15 | +pub struct HashedLeanIMT<H> { |
| 16 | + /// LeanIMT |
| 17 | + tree: LeanIMT, |
| 18 | + /// Hasher |
| 19 | + hasher: H, |
| 20 | +} |
| 21 | + |
| 22 | +impl<H> HashedLeanIMT<H> |
| 23 | +where |
| 24 | + H: LeanIMTHasher, |
| 25 | +{ |
| 26 | + /// Creates a new tree with optional initial leaves. |
| 27 | + pub fn new(leaves: &[Vec<u8>], hasher: H) -> Result<Self, LeanIMTError> { |
| 28 | + let imt = LeanIMT::new(leaves, H::hash)?; |
| 29 | + |
| 30 | + Ok(Self { tree: imt, hasher }) |
| 31 | + } |
| 32 | + |
| 33 | + /// Returns the root, if it exists. |
| 34 | + pub fn root(&self) -> Option<Vec<u8>> { |
| 35 | + self.tree.root() |
| 36 | + } |
| 37 | + |
| 38 | + /// Returns the tree depth. |
| 39 | + pub fn depth(&self) -> usize { |
| 40 | + self.tree.depth() |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns the leaves. |
| 44 | + pub fn leaves(&self) -> &[Vec<u8>] { |
| 45 | + self.tree.leaves() |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns the number of leaves. |
| 49 | + pub fn size(&self) -> usize { |
| 50 | + self.tree.size() |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns the index of a leaf, if it exists. |
| 54 | + pub fn index_of(&self, leaf: &[u8]) -> Option<usize> { |
| 55 | + self.tree.index_of(leaf) |
| 56 | + } |
| 57 | + |
| 58 | + /// Checks if a leaf exists. |
| 59 | + pub fn contains(&self, leaf: &[u8]) -> bool { |
| 60 | + self.tree.contains(leaf) |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns the leaf at the given index. |
| 64 | + pub fn get_leaf(&self, index: usize) -> Option<Vec<u8>> { |
| 65 | + self.tree.get_leaf(index) |
| 66 | + } |
| 67 | + |
| 68 | + /// Inserts a single leaf. |
| 69 | + pub fn insert(&mut self, leaf: &[u8]) { |
| 70 | + self.tree.insert(leaf, H::hash) |
| 71 | + } |
| 72 | + |
| 73 | + /// Inserts multiple leaves. |
| 74 | + pub fn insert_many(&mut self, leaves: &[Vec<u8>]) -> Result<(), LeanIMTError> { |
| 75 | + self.tree.insert_many(leaves, H::hash) |
| 76 | + } |
| 77 | + |
| 78 | + /// Updates a leaf at the given index. |
| 79 | + pub fn update(&mut self, index: usize, new_leaf: &[u8]) -> Result<(), LeanIMTError> { |
| 80 | + self.tree.update(index, new_leaf, H::hash) |
| 81 | + } |
| 82 | + |
| 83 | + /// Generates a Merkle proof for a leaf at the given index. |
| 84 | + pub fn generate_proof(&self, index: usize) -> Result<MerkleProof, LeanIMTError> { |
| 85 | + self.tree.generate_proof(index) |
| 86 | + } |
| 87 | + |
| 88 | + /// Verifies a Merkle proof. |
| 89 | + pub fn verify_proof(proof: &MerkleProof) -> bool { |
| 90 | + LeanIMT::verify_proof(proof, H::hash) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::{HashedLeanIMT, LeanIMTHasher}; |
| 97 | + use std::collections::hash_map::DefaultHasher; |
| 98 | + use std::hash::{Hash, Hasher}; |
| 99 | + |
| 100 | + struct SampleHasher; |
| 101 | + |
| 102 | + impl LeanIMTHasher for SampleHasher { |
| 103 | + fn hash(input: &[u8]) -> Vec<u8> { |
| 104 | + let mut hasher = DefaultHasher::new(); |
| 105 | + for byte in input { |
| 106 | + byte.hash(&mut hasher); |
| 107 | + } |
| 108 | + let hash = hasher.finish(); |
| 109 | + |
| 110 | + let mut result = vec![0u8; 32]; |
| 111 | + result[..8].copy_from_slice(&hash.to_le_bytes()); |
| 112 | + result |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_new_empty_tree() { |
| 118 | + let tree = HashedLeanIMT::new(&[], SampleHasher).unwrap(); |
| 119 | + assert_eq!(tree.size(), 0); |
| 120 | + assert_eq!(tree.root(), None); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_insert_leaves() { |
| 125 | + let mut tree = HashedLeanIMT::new(&[], SampleHasher).unwrap(); |
| 126 | + |
| 127 | + // Insert a single leaf |
| 128 | + tree.insert(&[1; 32]); |
| 129 | + assert_eq!(tree.size(), 1); |
| 130 | + assert!(tree.contains(&[1; 32])); |
| 131 | + |
| 132 | + // Insert multiple leaves |
| 133 | + tree.insert_many(&[b"leaf2".to_vec(), b"leaf3".to_vec()]) |
| 134 | + .unwrap(); |
| 135 | + assert_eq!(tree.size(), 3); |
| 136 | + assert!(tree.contains(b"leaf2")); |
| 137 | + assert!(tree.contains(b"leaf3")); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_update_leaf() { |
| 142 | + let mut tree = |
| 143 | + HashedLeanIMT::new(&[b"leaf1".to_vec(), b"leaf2".to_vec()], SampleHasher).unwrap(); |
| 144 | + |
| 145 | + tree.update(0, b"updated_leaf").unwrap(); |
| 146 | + assert!(!tree.contains(b"leaf1")); |
| 147 | + assert!(tree.contains(b"updated_leaf")); |
| 148 | + |
| 149 | + assert!(tree.contains(b"leaf2")); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_merkle_proof() { |
| 154 | + let leaves = vec![ |
| 155 | + b"leaf1".to_vec(), |
| 156 | + b"leaf2".to_vec(), |
| 157 | + b"leaf3".to_vec(), |
| 158 | + b"leaf4".to_vec(), |
| 159 | + ]; |
| 160 | + |
| 161 | + let tree = HashedLeanIMT::new(&leaves, SampleHasher).unwrap(); |
| 162 | + |
| 163 | + let proof = tree.generate_proof(1).unwrap(); |
| 164 | + |
| 165 | + assert!(HashedLeanIMT::<SampleHasher>::verify_proof(&proof)); |
| 166 | + |
| 167 | + assert_eq!(proof.index, 1); |
| 168 | + assert_eq!(&proof.leaf, b"leaf2"); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_index_of_and_get_leaf() { |
| 173 | + let leaves = vec![b"leaf1".to_vec(), b"leaf2".to_vec(), b"leaf3".to_vec()]; |
| 174 | + |
| 175 | + let tree = HashedLeanIMT::new(&leaves, SampleHasher).unwrap(); |
| 176 | + |
| 177 | + assert_eq!(tree.index_of(b"leaf2"), Some(1)); |
| 178 | + assert_eq!(tree.index_of(b"nonexistent"), None); |
| 179 | + |
| 180 | + assert_eq!(tree.get_leaf(1), Some(b"leaf2".to_vec())); |
| 181 | + assert_eq!(tree.get_leaf(5), None); |
| 182 | + } |
| 183 | +} |
0 commit comments