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

macro mmr-macro

使用merkle-heapless实现的Merkle Mountain Range宏

7次发布

0.0.7 2024年4月12日
0.0.6 2024年2月5日
0.0.5 2024年1月29日
0.0.4 2023年8月16日
0.0.3 2023年7月4日

5#mountain

Download history 10/week @ 2024-05-21 2/week @ 2024-06-11

452 每月下载量
merkle-heapless 中使用

MIT/Apache

22KB
407

Merkle Mountain Range宏

在merkle-heapless依赖中包含["mmr_macro"]功能

必需的编译器功能

// 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);

功能

Mountain Range的功能类似于Merkle树。

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"));

依赖项

~2MB
~44K SLoC