merkle_tree_simple

Crates.iomerkle_tree_simple
lib.rsmerkle_tree_simple
version0.1.0
created_at2025-09-08 20:08:45.174321+00
updated_at2025-09-08 20:08:45.174321+00
descriptionA simple and efficient Merkle Tree
homepage
repositoryhttps://github.com/its-saeed/merkle-tree/
max_upload_size
id1829815
size35,980
Saeed Dadkhah (its-saeed)

documentation

README

Merkle Tree in Rust ๐ŸŒณ

A simple and efficient Merkle Tree implementation in Rust, with a command-line interface (CLI) and a reusable library.


๐ŸŒ What is a Merkle Tree?

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.

Example Use Cases:

  • Blockchain and cryptocurrencies
  • Version control systems (like Git)
  • Distributed systems and peer-to-peer networks

๐Ÿš€ Getting Started

๐Ÿ”ง Clone and Build

git clone https://github.com/its-saeed/merkle-tree.git
cd merkle-tree
cargo build --release

๐Ÿ“š Using the Library

You can use the library in your own Rust project by importing it as a dependency (if published to crates.io) or locally.

Example

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.


๐Ÿ–ฅ๏ธ Using the CLI

The CLI provides a simple interface for computing and verifying Merkle roots and inclusion proofs.

Build the CLI

cargo build --release

Run

๐Ÿ“Œ Compute the root from a list of hex-encoded inputs

cargo run -- root 01 02 03 04

#output: 98d658fb28540a2eca2a8a5930c309a9c37f89979d48d025a72c36a77a74510d

๐Ÿ›ก๏ธ Verify that a set of inputs reconstructs the given Merkle root

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

๐Ÿ“„ Generate a Merkle proof for a specific data element

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

โœ… Verify a Merkle inclusion proof

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

๐ŸŒณ Print a visual ASCII representation of the tree

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

๐Ÿง  Performance Considerations

To make the Merkle Tree efficient:

  • Preallocated memory: The internal hash vector is preallocated based on the number of input leaves to avoid reallocations.
  • Bottom-up construction: Tree is built from leaves up to the root in a single pass using index math instead of allocating node structs.
  • Zero-copy hash references in proofs: Proof hashes are borrowed (&Hash) instead of cloned wherever possible to reduce unnecessary memory usage.
  • Efficient hashing: Uses 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.


๐Ÿงช Testing

cargo test

Tests include:

  • Tree construction and root computation
  • Proof generation and verification
  • Valid/invalid data verification scenarios
Commit count: 16

cargo fmt