Crates.io | merkletree-rs |
lib.rs | merkletree-rs |
version | 0.0.3 |
source | src |
created_at | 2019-09-10 14:42:19.680104 |
updated_at | 2019-10-16 05:42:33.426102 |
description | Sparse MerkleTree |
homepage | |
repository | https://github.com/arnaucube/merkletree-rs |
max_upload_size | |
id | 163833 |
size | 70,518 |
Sparse MerkleTree implementation in Rust.
The MerkleTree is optimized in the design and concepts, to have a faster and lighter MerkleTree, maintaining compatibility with a non optimized MerkleTree. In this way, the MerkleRoot of the optimized MerkleTree will be the same that the MerkleRoot of the non optimized MerkleTree.
Compatible with the Go version: https://github.com/arnaucube/go-merkletree
Import lib:
extern crate merkletree_rs;
use merkletree_rs::{db, MerkleTree, TestValue, Value};
Create new tree:
// to build the storage, the first parameter is the path and the second parameter specifies if wants to use a in_memory database or a directory of the filesystem
let mut sto = db::Db::new("test".to_string(), true);
let mut mt = MerkleTree::new(&mut sto, 140 as u32);
Add value to leaf:
let val: TestValue = TestValue {
bytes: "this is a test leaf".as_bytes().to_vec(),
index_length: 15,
};
mt.add(&val).unwrap();
Get proof:
let mp = mt.generate_proof(val.hi());
println!("{:?}", mp);
Verify proof:
// check if the value exist
let v =
merkletree_rs::verify_proof(mt.get_root(), &mp, val.hi(), val.ht(), mt.get_num_levels());
println!("{:?}", v);
// check if the don't value exist (in that case, the 'ht' will be an empty value)
let v = merkletree_rs::verify_proof(
mt.get_root(),
&mp,
val.hi(),
merkletree_rs::constants::EMPTYNODEVALUE,
mt.get_num_levels(),
);
println!("{:?}", v);
Print current MerkleRoot:
println!("{:?}", mt.get_root());