#merkle-tree #node-tree #tree-structure #leave #state #checkpoint

shardtree

A space-efficient Merkle tree with witnessing of marked leaves, checkpointing & state restoration

8 releases (5 breaking)

0.5.0 Oct 4, 2024
0.4.0 Aug 12, 2024
0.3.2 Dec 10, 2024
0.3.1 Apr 3, 2024
0.0.0 Dec 15, 2022

#510 in Data structures

Download history 1857/week @ 2024-08-23 1566/week @ 2024-08-30 1678/week @ 2024-09-06 888/week @ 2024-09-13 1362/week @ 2024-09-20 1639/week @ 2024-09-27 2231/week @ 2024-10-04 2537/week @ 2024-10-11 1907/week @ 2024-10-18 2038/week @ 2024-10-25 2167/week @ 2024-11-01 2495/week @ 2024-11-08 2572/week @ 2024-11-15 1511/week @ 2024-11-22 2592/week @ 2024-11-29 2915/week @ 2024-12-06

10,174 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

355KB
7.5K SLoC

shardtree

This is a Rust crate that provides an implementation of a fixed-depth Merkle tree structure that is densely filled from the left. It supports:

  • Out-of-order insertion: leaves and nodes may be inserted into the tree in arbitrary order. The structure will keep track of the right-most filled position as the frontier of the tree; any unfilled leaves to the left of this position are considered "missing", while any unfilled leaves to the right of this position are considered "empty".
  • Witnessing: Individual leaves of the Merkle tree may be marked such that witnesses will be maintained for the marked leaves as additional nodes are inserted into the tree, but leaf and node data not specifically required to maintain these witnesses is not retained, for space efficiency.
  • Checkpointing: the tree may be reset to a previously checkpointed state, up to a fixed number of checkpoints.

The tree is represented as an ordered collection of fixed-depth subtrees, or "shards". The roots of the shards form the leaves in the "cap".

Level
  3           root         \
              / \           |
            /     \         |
  2       /         \        } cap
        / \         / \     |
       /   \       /   \    |
  1   A     B     C     D  /  \
     / \   / \   / \   / \     } shards
  0 /\ /\ /\ /\ /\ /\ /\ /\   /

This structure enables witnesses for marked leaves to be advanced up to recent checkpoints or the latest state of the tree, without having to insert each intermediate leaf individually. Instead, only the roots of all complete shards between the one containing the marked leaf and the tree frontier need to be inserted, along with the necessary nodes to build a path from the marked leaf to the root of the shard containing it.

Documentation

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.4–1.2MB
~20K SLoC