Giter Club home page Giter Club logo

rusty_merkle_tree's Introduction

rusty_merkle_tree

Implementation of a simple Merkle Tree in Rust.

Introduction

Merkle trees are used in distributed systems for efficient data verification. They are efficient because they use hashes instead of full files. Hashes are ways of encoding files that are much smaller than the actual file itself. Currently, their main uses are in peer-to-peer networks such as Tor, Bitcoin, and Git.

Purpose

The purpose of this repository is to implement a basic Merkle Tree in Rust as a means of practicing the Rust programming language.

Features

  • The build_from method constructs a Merkle Tree from a given list of leaves.
  • The get_root method retrieves the root hash of the Merkle Tree.
  • The count_leaves method counts the number of leaves in the Merkle Tree.
  • The add_leaves method allows for the addition of new leaves to the Merkle Tree.
  • The contains_leaf method checks if a leaf exists in the Merkle Tree and generates a proof of existence if found.
  • The verify method verifies the authenticity of a leaf in the Merkle Tree using the provided proof and root hash.

Usage

You can import and use the MerkleTree struct in your Rust code as needed.

use merkle_tree::MerkleTree;

fn main() {
    // Example usage
    let leaves = vec!["leaf1".to_string(), "leaf2".to_string(), "leaf3".to_string()];
    let merkle_tree = MerkleTree::build_from(leaves).unwrap();
    let root = merkle_tree.get_root();
    println!("Root Hash: {}", root);
}

Validating Proof of Leaf Existence

Once you have generated a proof for the existence of a leaf in the Merkle Tree, you can validate the proof to ensure the integrity and authenticity of the leaf. Here's how you can do it:

  1. Get the Root Hash: Retrieve the root hash of the Merkle Tree using the get_root method.

  2. Verify the Proof: Use the verify method to verify the proof by providing the proof, root hash, leaf hash, and leaf index as arguments. The verify method will return true if the proof is valid, indicating that the leaf exists in the Merkle Tree.

Example:

use merkle_tree::MerkleTree;

// Construct the Merkle Tree (previous code)

// Generates the proof
if let (proof, index) = merkle_tree.contains_leaf(leaf1_hash) {
        
  // Verify the proof
  let root = merkle_tree.get_root();
  let is_valid = MerkleTree::verify(proof, root, leaf_hash, leaf_index);
  if is_valid {
      println!("Proof verified successfully. Leaf exists in the Merkle Tree.");
  } else {
      println!("Proof verification failed. Leaf does not exist in the Merkle Tree.");
  }
}

Running Tests

To run tests for this Merkle Tree implementation, you can use the provided Makefile. Simply execute the following command:

make test

Hash Function

This Merkle Tree implementation utilizes the Keccak256 hash function from the crypto crate for hashing leaf nodes and computing the Merkle Tree's internal nodes. Keccak256 is a cryptographic hash function that produces a 256-bit (32-byte) hash value.

rusty_merkle_tree's People

Contributors

avilagaston9 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.