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.

59 lines
1.8 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. Import lib:
  7. ```rust
  8. extern crate merkletree_rs;
  9. use merkletree_rs::{db, MerkleTree, TestValue, Value};
  10. ```
  11. Create new tree:
  12. ```rust
  13. // 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
  14. let mut sto = db::Db::new("test".to_string(), true);
  15. let mut mt = MerkleTree::new(&mut sto, 140 as u32);
  16. ```
  17. Add value to leaf:
  18. ```rust
  19. let val: TestValue = TestValue {
  20. bytes: "this is a test leaf".as_bytes().to_vec(),
  21. index_length: 15,
  22. };
  23. mt.add(&val).unwrap();
  24. ```
  25. Get proof:
  26. ```rust
  27. let mp = mt.generate_proof(val.hi());
  28. println!("{:?}", mp);
  29. ```
  30. Verify proof:
  31. ```rust
  32. // check if the value exist
  33. let v =
  34. merkletree_rs::verify_proof(mt.get_root(), &mp, val.hi(), val.ht(), mt.get_num_levels());
  35. println!("{:?}", v);
  36. // check if the don't value exist (in that case, the 'ht' will be an empty value)
  37. let v = merkletree_rs::verify_proof(
  38. mt.get_root(),
  39. &mp,
  40. val.hi(),
  41. merkletree_rs::constants::EMPTYNODEVALUE,
  42. mt.get_num_levels(),
  43. );
  44. println!("{:?}", v);
  45. ```
  46. Print current MerkleRoot:
  47. ```rust
  48. println!("{:?}", mt.get_root());
  49. ```