merkletree-rs/README.md
README.mdRaw
1# merkletree-rs [](https://crates.io/crates/merkletree-rs) [](https://travis-ci.org/arnaucube/merkletree-rs)
2Sparse MerkleTree implementation in Rust.
3
4The 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.
5
6Compatible with the Go version: https://github.com/arnaucube/go-merkletree
7
8
9## Usage
10Create new tree:
11```rust
12// 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
13let mut sto = db::Db::new("test".to_string(), true);
14let mut mt: MerkleTree::new(&mut sto, 140);
15```
16
17Add value to leaf:
18```rust
19let val = TestValue {
20 bytes: "this is a test leaf".as_bytes().to_vec(),
21 index_length: 15,
22};
23mt.add(&val);
24```
25
26Get proof:
27```rust
28let mp = mt.generate_proof(val.hi());
29```
30
31Verify proof:
32```rust
33// check if the value exist
34let v = verify_proof(mt.root, mp, val.hi(), val.ht(), mt.num_levels);
35
36// check if the don't value exist (in that case, the 'ht' will be an empty value)
37let v = verify_proof(mt.root, mp, val.hi(), EMPTYNODEVALUE, mt.num_levels);
38```