| Crates.io | merkle_tree_simple |
| lib.rs | merkle_tree_simple |
| version | 0.1.0 |
| created_at | 2025-09-08 20:08:45.174321+00 |
| updated_at | 2025-09-08 20:08:45.174321+00 |
| description | A simple and efficient Merkle Tree |
| homepage | |
| repository | https://github.com/its-saeed/merkle-tree/ |
| max_upload_size | |
| id | 1829815 |
| size | 35,980 |
A simple and efficient Merkle Tree implementation in Rust, with a command-line interface (CLI) and a reusable library.
A Merkle Tree is a binary tree where each leaf node is the hash of a piece of data, and each internal node is the hash of the concatenation of its children. It provides a way to verify the integrity of large datasets with minimal information.
git clone https://github.com/its-saeed/merkle-tree.git
cd merkle-tree
cargo build --release
You can use the library in your own Rust project by importing it as a dependency (if published to crates.io) or locally.
use merkle_tree::{MerkleTree, Data};
fn main() {
let data: Vec<Data> = vec![
vec![0x01].into(),
vec![0x02].into(),
vec![0x03].into(),
vec![0x04].into(),
];
let tree = MerkleTree::construct(&data);
let root = tree.root().unwrap();
println!("Merkle root: {}", root);
}
You can also verify data integrity or generate/verify proofs using MerkleTree::verify, prove, and verify_proof.
The CLI provides a simple interface for computing and verifying Merkle roots and inclusion proofs.
cargo build --release
cargo run -- root 01 02 03 04
#output: 98d658fb28540a2eca2a8a5930c309a9c37f89979d48d025a72c36a77a74510d
cargo run -- verify --root <hex_root> 01 02 03 04
# Example:
cargo run -- verify --root abcd1234deadbeef 01 02 03 04
# Output: โ
Verified or โ Not verified
cargo run -- prove --data <hex_value> 01 02 03 04
# Example:
cargo run -- prove --data 03 01 02 03 04
# output
#0: [R] e52d9c508c502347344d8c07ad91cbd6068afc75ff6292f062a09ca381c89e71
#1: [L] 42dbeeb4eb5d41bbdc93732c6a87ab3241ee03f44a0780a52ddf831f5fd88b53
cargo run -- verify-proof \
--data <hex_value> \
--root <hex_root> \
--proof <direction1:hash1> --proof<direction2:hash2> ...
# Example:
cargo run -- verify-proof \
--data 03 \
--root aabbccddeeff0011 \
--proof R:1234567890abcdef --proof L:abcdef1234567890
# Output: โ
Verified or โ Not verified
This helps you visualize the Merkle structure level-by-level.
cargo run -- print 01 02 03 04
# Output:
MerkleTree:
โโโโโ [6] e52d9c508c50
โโโโโ [2] 75a1abcc800e
โโโโโ [5] 084fed08b978
โโโโโ [0] 98d658fb2854
โโโโโ [4] dbc1b4c900ff
โโโโโ [1] 42dbeeb4eb5d
โโโโโ [3] 4bf5122f3445
To make the Merkle Tree efficient:
&Hash) instead of cloned wherever possible to reduce unnecessary memory usage.sha2::Sha256, a fast and secure hashing algorithm with optimized implementations.These optimizations ensure that the Merkle Tree can scale efficiently even for thousands of entries.
cargo test
Tests include: