mirror of
https://github.com/arnaucube/merkletree-rs.git
synced 2026-02-28 05:36:47 +01:00
update to external usage of the lib
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "merkletree-rs"
|
name = "merkletree-rs"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
authors = ["arnaucube <root@arnaucube.com>"]
|
authors = ["arnaucube <root@arnaucube.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
|
|||||||
35
README.md
35
README.md
@@ -7,32 +7,53 @@ Compatible with the Go version: https://github.com/arnaucube/go-merkletree
|
|||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
Import lib:
|
||||||
|
```rust
|
||||||
|
extern crate merkletree_rs;
|
||||||
|
use merkletree_rs::{db, MerkleTree, TestValue, Value};
|
||||||
|
```
|
||||||
|
|
||||||
Create new tree:
|
Create new tree:
|
||||||
```rust
|
```rust
|
||||||
// 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
|
// 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 sto = db::Db::new("test".to_string(), true);
|
||||||
let mut mt: MerkleTree::new(&mut sto, 140);
|
let mut mt = MerkleTree::new(&mut sto, 140 as u32);
|
||||||
```
|
```
|
||||||
|
|
||||||
Add value to leaf:
|
Add value to leaf:
|
||||||
```rust
|
```rust
|
||||||
let val = TestValue {
|
let val: TestValue = TestValue {
|
||||||
bytes: "this is a test leaf".as_bytes().to_vec(),
|
bytes: "this is a test leaf".as_bytes().to_vec(),
|
||||||
index_length: 15,
|
index_length: 15,
|
||||||
};
|
};
|
||||||
mt.add(&val);
|
mt.add(&val).unwrap();
|
||||||
```
|
```
|
||||||
|
|
||||||
Get proof:
|
Get proof:
|
||||||
```rust
|
```rust
|
||||||
let mp = mt.generate_proof(val.hi());
|
let mp = mt.generate_proof(val.hi());
|
||||||
|
println!("{:?}", mp);
|
||||||
```
|
```
|
||||||
|
|
||||||
Verify proof:
|
Verify proof:
|
||||||
```rust
|
```rust
|
||||||
// check if the value exist
|
// check if the value exist
|
||||||
let v = verify_proof(mt.root, mp, val.hi(), val.ht(), mt.num_levels);
|
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)
|
// check if the don't value exist (in that case, the 'ht' will be an empty value)
|
||||||
let v = verify_proof(mt.root, mp, val.hi(), EMPTYNODEVALUE, mt.num_levels);
|
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:
|
||||||
|
```rust
|
||||||
|
println!("{:?}", mt.get_root());
|
||||||
```
|
```
|
||||||
|
|||||||
20
src/lib.rs
20
src/lib.rs
@@ -14,8 +14,8 @@ pub mod utils;
|
|||||||
type Result<T> = std::result::Result<T, String>;
|
type Result<T> = std::result::Result<T, String>;
|
||||||
|
|
||||||
pub struct TestValue {
|
pub struct TestValue {
|
||||||
bytes: Vec<u8>,
|
pub bytes: Vec<u8>,
|
||||||
index_length: u32,
|
pub index_length: u32,
|
||||||
}
|
}
|
||||||
pub trait Value {
|
pub trait Value {
|
||||||
fn bytes(&self) -> &Vec<u8>;
|
fn bytes(&self) -> &Vec<u8>;
|
||||||
@@ -57,6 +57,12 @@ impl<'a> MerkleTree<'a> {
|
|||||||
sto: database,
|
sto: database,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn get_root(&mut self) -> [u8; 32] {
|
||||||
|
self.root
|
||||||
|
}
|
||||||
|
pub fn get_num_levels(&mut self) -> u32 {
|
||||||
|
self.num_levels
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, v: &TestValue) -> Result<()> {
|
pub fn add(&mut self, v: &TestValue) -> Result<()> {
|
||||||
// add the leaf that we are adding
|
// add the leaf that we are adding
|
||||||
@@ -366,7 +372,7 @@ impl<'a> MerkleTree<'a> {
|
|||||||
|
|
||||||
pub fn verify_proof(
|
pub fn verify_proof(
|
||||||
root: [u8; 32],
|
root: [u8; 32],
|
||||||
mp: Vec<u8>,
|
mp: &Vec<u8>,
|
||||||
hi: [u8; 32],
|
hi: [u8; 32],
|
||||||
ht: [u8; 32],
|
ht: [u8; 32],
|
||||||
num_levels: u32,
|
num_levels: u32,
|
||||||
@@ -548,7 +554,7 @@ mod tests {
|
|||||||
assert_eq!("0000000000000000000000000000000000000000000000000000000000000001fd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", mp.to_hex());
|
assert_eq!("0000000000000000000000000000000000000000000000000000000000000001fd8e1a60cdb23c0c7b2cf8462c99fafd905054dccb0ed75e7c8a7d6806749b6b", mp.to_hex());
|
||||||
|
|
||||||
// verify
|
// verify
|
||||||
let v = verify_proof(mt.root, mp, val2.hi(), val2.ht(), mt.num_levels);
|
let v = verify_proof(mt.root, &mp, val2.hi(), val2.ht(), mt.num_levels);
|
||||||
assert_eq!(true, v);
|
assert_eq!(true, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,7 +588,7 @@ mod tests {
|
|||||||
// verify that is a proof of an empty leaf (constants::EMPTYNODEVALUE)
|
// verify that is a proof of an empty leaf (constants::EMPTYNODEVALUE)
|
||||||
let v = verify_proof(
|
let v = verify_proof(
|
||||||
mt.root,
|
mt.root,
|
||||||
mp,
|
&mp,
|
||||||
val3.hi(),
|
val3.hi(),
|
||||||
constants::EMPTYNODEVALUE,
|
constants::EMPTYNODEVALUE,
|
||||||
mt.num_levels,
|
mt.num_levels,
|
||||||
@@ -609,7 +615,7 @@ mod tests {
|
|||||||
&hex::decode("786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
|
&hex::decode("786677808ba77bdd9090a969f1ef2cbd1ac5aecd9e654f340500159219106878")
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
let v = verify_proof(root, mp, hi, ht, 140);
|
let v = verify_proof(root, &mp, hi, ht, 140);
|
||||||
assert_eq!(true, v);
|
assert_eq!(true, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -627,7 +633,7 @@ mod tests {
|
|||||||
&hex::decode("a69792a4cff51f40b7a1f7ae596c6ded4aba241646a47538898f17f2a8dff647")
|
&hex::decode("a69792a4cff51f40b7a1f7ae596c6ded4aba241646a47538898f17f2a8dff647")
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
let v = verify_proof(root, mp, hi, constants::EMPTYNODEVALUE, 140);
|
let v = verify_proof(root, &mp, hi, constants::EMPTYNODEVALUE, 140);
|
||||||
assert_eq!(true, v);
|
assert_eq!(true, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user