-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from HerodotusDev/feat/incremental-merkle-tree
incremental merkle tree
- Loading branch information
Showing
9 changed files
with
647 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use accumulators::{ | ||
hasher::stark_poseidon::StarkPoseidonHasher, merkle_tree::incremental::IncrementalMerkleTree, | ||
store::sqlite::SQLiteStore, | ||
}; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
|
||
fn prepare_incremental(count: usize) -> IncrementalMerkleTree<SQLiteStore, StarkPoseidonHasher> { | ||
let hasher = StarkPoseidonHasher::new(Some(false)); | ||
|
||
let store = SQLiteStore::new(":memory:").unwrap(); | ||
store.init().expect("Failed to init store"); | ||
|
||
IncrementalMerkleTree::initialize(count, "0x0".to_string(), hasher, store, None) | ||
} | ||
|
||
fn bench(c: &mut Criterion) { | ||
{ | ||
let mut group = c.benchmark_group("Incremental Merkle Tree insertion"); | ||
let inputs = [10_000, 1_000_000]; | ||
|
||
for input in inputs.iter() { | ||
group.bench_with_input(BenchmarkId::new("times", input), &input, |b, &&size| { | ||
b.iter(|| prepare_incremental(size)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
criterion_group!( | ||
name = benches; | ||
config = Criterion::default().sample_size(10); | ||
targets = bench | ||
); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Incremental Merkle Tree | ||
|
||
Incremental Merkle Tree is a structure that contains a constant amount of hashes, allows updating a given hash and proving efficiently. Time complexity of both operations is O(log tree_size). | ||
|
||
## Example | ||
|
||
```rust | ||
use accumulators::{ | ||
hasher::stark_poseidon::StarkPoseidonHasher, merkle_tree::incremental::IncrementalMerkleTree, | ||
store::sqlite::SQLiteStore, | ||
}; | ||
let store = SQLiteStore::new(":memory:").unwrap(); | ||
let hasher = StarkPoseidonHasher::new(Some(false)); | ||
store.init().expect("Failed to init store"); | ||
let tree = IncrementalMerkleTree::initialize(16, "0x0".to_string(), hasher, store, None); | ||
|
||
let path = tree.get_inclusion_proof(10).unwrap(); | ||
let valid_proof = tree.verify_proof(10, "0x0", &path).unwrap(); | ||
assert_eq!(valid_proof, true); | ||
|
||
let invalid_proof = tree.verify_proof(10, "0x1", &path).unwrap(); | ||
assert_eq!(invalid_proof, false); | ||
``` | ||
|
||
### Benchmark | ||
|
||
```sh | ||
Incremental Merkle Tree insertion/times/10000 | ||
time: [154.39 ms 154.89 ms 155.38 ms] | ||
Incremental Merkle Tree insertion/times/1000000 | ||
time: [17.946 s 18.027 s 18.125 s] | ||
``` |
Oops, something went wrong.