9 releases

0.6.1 Nov 22, 2022
0.5.4 Sep 27, 2022
0.5.3 Nov 15, 2021
0.5.2-rc1 Jul 25, 2021
0.1.0-alpha2 Sep 2, 2019

#505 in Data structures

Download history 622/week @ 2023-12-12 627/week @ 2023-12-19 256/week @ 2023-12-26 397/week @ 2024-01-02 453/week @ 2024-01-09 304/week @ 2024-01-16 236/week @ 2024-01-23 565/week @ 2024-01-30 198/week @ 2024-02-06 357/week @ 2024-02-13 248/week @ 2024-02-20 528/week @ 2024-02-27 214/week @ 2024-03-05 242/week @ 2024-03-12 449/week @ 2024-03-19 381/week @ 2024-03-26

1,442 downloads per month
Used in 14 crates (via ckb-sdk)

MIT license

270KB
7K SLoC

C 4K SLoC // 0.1% comments Rust 3K SLoC // 0.0% comments

Sparse merkle tree

Crates.io Docs

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

Construction

A sparse merkle tree is a perfectly balanced tree contains 2 ^ N leaves:

# 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

Dependencies

~235KB