Skip to content

subhdotsol/MerkelTree-Implementaion-Rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌳 Merkle Tree in Rust

This project implements a Merkle Tree in Rust using SHA-256 hashing (via the sha2 crate`.
It supports:

  • Building a Merkle tree from a list of input values
  • Automatic leaf duplication for odd counts
  • SHA-256 hashing for leaves and parent nodes
  • Recursive Merkle tree construction
  • ASCII tree visualization

🧠 What is a Merkle Tree?

A Merkle Tree is a binary tree where:

  • Every leaf node contains the hash of some input data.
  • Each internal node contains the hash of the concatenation of its two child nodes.
  • The root hash cryptographically represents the entire dataset.

Merkle Trees are widely used in:

  • Blockchain systems
  • Distributed databases
  • File integrity verification
  • Peer-to-peer networks

📦 Features

✔ Build a Merkle Tree Automatically

Pass a Vec<T> and the tree takes care of hashing, balancing, and node creation.

✔ SHA-256 Hashing

All leaf and parent nodes use the hash algorithm provided by the sha2 crate.

✔ Automatic Balancing

If the number of leaves is odd, the last leaf is duplicated to maintain a complete binary structure.

✔ Tree Visualization

The ASCII printing function produces output like this:


└── 3af...91bc
├── 19c...a23f
│   ├── 98f...1ac3
│   └── 11b...98dd
└── 4b9...de20
├── 77c...8120
└── 6ea...9f11

Clean and easy to understand.


🚀 Usage

1. Input data

let mut data = vec![
    "a".to_owned(),
    "b".to_owned(),
    "c".to_owned(),
    "d".to_owned(),
    "e".to_owned(),
    "f".to_owned(),
    "g".to_owned(),
    "h".to_owned(),
];

2. Build the Merkle tree

let mut merkle_tree = MerkleTree::new();
merkle_tree.create(&mut data);

3. Print the tree

print_tree(Some(merkle_tree.root));

📁 Project Structure

├── src
│   ├── main.rs
│
├── Cargo.toml
└── README.md

🔧 Dependencies

Add this to your Cargo.toml:

[dependencies]
sha2 = "0.10"

🧩 Main Components

MerkelNode

Represents a node in the tree:

struct MerkelNode {
    data: String,
    left: Option<Box<MerkelNode>>,
    right: Option<Box<MerkelNode>>,
}

MerkleTree

Handles constructing the entire tree and storing the root.

sha512_hash()

Utility function that computes a SHA-256 hash of any input convertible to bytes.

print_tree()

Pretty-prints the tree using ASCII symbols (├──, └──, ).


▶ Running the Program

cargo run

You will see:

  • The computed Merkle root
  • The full tree printed in readable ASCII form

🎯 Possible Improvements

  • Merkle proof generation & verification
  • JSON or binary serialization
  • Colorized tree output
  • Parallel hashing for performance
  • Avoid unnecessary cloning of nodes

📝 License

This project is open-source and free to use.

About

Merkel Tree Implementation in rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages