#merkle #range #merkle-tree #macro #mountain #mmr #merkle-heapless

macro mmr-macro

Macro for Merkle Mountain Range implementation with merkle-heapless

7 releases

new 0.0.7 Apr 12, 2024
0.0.6 Feb 5, 2024
0.0.5 Jan 29, 2024
0.0.4 Aug 16, 2023
0.0.3 Jul 4, 2023

#4 in #mountain

44 downloads per month
Used in merkle-heapless

MIT/Apache

22KB
407 lines

Merkle Mountain Range macro

Include ["mmr_macro"] feature in merkle-heapless dependency

Necessary compiler features

// compulsory at the beginning of the .rs file in order the macro to compile
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
// snip
### Declaration and instantiation
use merkle_heapless::{mmr_macro};
// declaration with expicit type name for your MMR
mmr_macro::mmr!(Type = FooMMR, BranchFactor = 2, Peaks = 3, Hash = StdHash, MaxInputWordLength = 10);
let mmr = FooMMR::default();
// implicitly creates MerkleMountainRange type
mmr_macro::mmr!(BranchFactor = 2, Peaks = 5, Hash = StdHash, MaxInputWordLength = 10);
// create with default current peak of height 0
let mmr = MerkleMountainRange::default();
// or create with current peak of height 2
let mmr = MerkleMountainRange::from_peak(MerkleMountainRangePeak::Peak3(Default::default()));
assert_eq!(mmr.peaks()[0].height(), 5 - 3);

Functionality

The functionality of Mountain Range is similar to that of the Merkle tree.

mmr.try_append(b"apple").unwrap();
// peak leaf numbers: [1, 0, 0, 0, 0]
assert_eq!(mmr.peaks()[0].height(), 0);
assert_eq!(mmr.peaks()[0].num_of_leaves(), 1);
assert_eq!(mmr.peaks()[1].num_of_leaves(), 0);
let proof = mmr.generate_proof(0);
assert!(proof.validate(b"apple"));

Dependencies

~2MB
~41K SLoC