rs_merkle_tree/lib.rs
1// Copyright 2025 Bilinear Labs - MIT License
2
3/*!
4Merkle tree implementation in Rust with configurable storage backends and hash functions. This Merkle tree
5implementation features:
6
7* Fixed depth: All proofs have a constant size equal to the `Depth`.
8* Append-only: Leaves are added sequentially starting at index `0`. Once added, a leaf cannot be modified.
9* Optimized for Merkle proof retrieval: Intermediate leaves are stored so that Merkle proofs can be fetched
10 from memory without needing to be calculated lazily, resulting in very fast retrieval times.
11
12Add `rs-merkle-tree` as a dependency to your Rust `Cargo.toml`.
13
14```toml
15[dependencies]
16rs-merkle-tree = { git = "https://github.com/bilinearlabs/rs-merkle-tree.git" }
17```
18
19You can create a Merkle tree, add leaves, get the number of leaves and get the Merkle proof of a given index as
20follows. This creates a simple Merkle tree using **keccak256** hashing algorithm, a memory storage and a depth 32.
21
22```rust,ignore
23use rs_merkle_tree::to_node;
24use rs_merkle_tree::tree::MerkleTree32;
25
26fn main() {
27 let mut tree = MerkleTree32::default();
28 tree.add_leaves(&[to_node!(
29 "0x532c79f3ea0f4873946d1b14770eaa1c157255a003e73da987b858cc287b0482"
30 )])
31 .unwrap();
32
33 println!("root: {:?}", tree.root().unwrap());
34 println!("num leaves: {:?}", tree.num_leaves());
35 println!("proof: {:?}", tree.proof(0).unwrap().proof);
36}
37```
38
39Note: This example requires the `memory_store` feature to be enabled.
40
41You can customize your tree by choosing a different store, hash function, and depth as follows. This tree also uses
42keccak256 but persists the leaves in a key-value sled store and has a depth of 32.
43
44```rust,ignore
45use rs_merkle_tree::stores::SledStore;
46use rs_merkle_tree::tree::MerkleTree;
47use rs_merkle_tree::hasher::Keccak256Hasher;
48
49fn main() {
50 let mut tree: MerkleTree<Keccak256Hasher, SledStore, 32> =
51 MerkleTree::new(Keccak256Hasher, SledStore::new("sled.db", true));
52}
53```
54
55Note: This example requires the `sled_store` feature to be enabled.
56
57*/
58
59pub mod errors;
60pub mod hasher;
61pub mod node;
62pub mod tree;
63
64pub mod stores {
65 #[cfg(feature = "memory_store")]
66 mod memory_store;
67 pub mod store;
68 #[cfg(feature = "memory_store")]
69 pub use memory_store::MemoryStore;
70 #[cfg(feature = "sled_store")]
71 mod sled_store;
72 #[cfg(feature = "sled_store")]
73 pub use sled_store::SledStore;
74 #[cfg(feature = "sqlite_store")]
75 mod sqlite_store;
76 #[cfg(feature = "sqlite_store")]
77 pub use sqlite_store::SqliteStore;
78 #[cfg(feature = "rocksdb_store")]
79 mod rocksdb_store;
80 #[cfg(feature = "rocksdb_store")]
81 pub use rocksdb_store::RocksDbStore;
82}
83
84// Re-export the store module for easier access
85pub use stores::store;
86
87pub use errors::MerkleError;
88pub use node::Node;
89pub use stores::store::Store;
90
91// Re-export MerkleTree and MerkleTree32 based on available features
92#[cfg(feature = "memory_store")]
93pub use tree::MerkleTree32;
94
95// Re-export the generic MerkleTree for all store types
96pub use tree::MerkleTree;