You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB

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