https://github.com/nervosnetwork/sparse-merkle-tree
https://github.com/nervosnetwork/sparse-merkle-tree
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/nervosnetwork/sparse-merkle-tree
- Owner: nervosnetwork
- Created: 2021-07-02T06:30:30.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-26T20:32:08.000Z (about 3 years ago)
- Last Synced: 2025-03-31T13:16:23.525Z (over 1 year ago)
- Language: Rust
- Size: 2.42 MB
- Stars: 50
- Watchers: 11
- Forks: 38
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sparse merkle tree
[](https://crates.io/crates/sparse-merkle-tree)
[Docs](https://docs.rs/sparse-merkle-tree)
An optimized sparse merkle tree.
| size | proof size | update | get | merkle proof | verify proof |
| --- | --- | --- | --- | --- | --- |
| 2n + log(n) | log(n) | log(n) | log(n) | log(n) | log(n) |
Features:
* Multi-leaves existence / non-existence merkle proof
* Customizable hash function
* Storage backend agnostic
* Rust `no_std` support
This article describes algorithm of this data structure [An optimized compacted sparse merkle tree](SMT.md)
## Construction
A sparse merkle tree is a perfectly balanced tree contains `2 ^ N` leaves:
``` txt
# N = 256 sparse merkle tree
height:
0
/ \
255 0 1
.............................
/ \ / \
1 0 1 0 1
/ \ / \ / \ / \
0 0 1 0 1 ... 0 1 0 1
0x00..00 0x00..01 ... 0x11..11
```
The above graph demonstrates a sparse merkle tree with `2 ^ 256` leaves, which can mapping every possible `H256` value into leaves. The height of the tree is `256`, from top to bottom, we denote `0` for each left branch and denote `1` for each right branch, so we can get a 256 bits path, which also can represent in `H256`, we use the path as the key of leaves, the most left leaf's key is `0x00..00`, and the next key is `0x00..01`, the most right key is `0x11..11`.
## License
MIT