Code Monkey home page Code Monkey logo

python-merkle-tree's Introduction

Merke Tree in python

GitHub Tag GitHub Actions Workflow Status Dynamic JSON Badge Static Badge Static Badge


This code aims to build a Merkle tree from a list of values. All data are hashed using the cryptographic hash algorithm SHA-3 (Secure Hash Algorithm 3).

It is recommended to hash the values provided to the MerkleTree class.

Features

  • Create a merkle tree
  • Create a merkle proof
  • Verify a value with a merkle proof
  • Get the merkle tree root
  • Get the merkle tree size
  • Get the merkle tree depth
  • Get/count the leaves

Installation

Clone the repository

git clone https://github.com/ArnaudSene/python-merkle-tree.git
cd python-merkle-tree

Prepare environment

This code use poetry to manage dependencies

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install poetry==1.4.0
poetry install 

How to use it?

Start a Python interactive shell or session

$ python
Python 3.11.2 (v3.11.2:878ead1ac1, Feb  7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Create a Merkle tree

data = [
    '43a26051362b8040b289abe93334a5e3662751aa691185ae9e9a2e1e0c169350',  
    'f2ff61e5ca30a7708bfa760e5749e10dc81452d9006f8fb35db69d8f490ed637', 
    '487dced11563793b12e4357f218d5a521c730fff52fc03552442bfac917691c0', 
    '0e3c7b0c7fed34610a5da469ea9e44e2965a3f6ae0ae2d5d1ba5e9f5ac9d345b', 
    '1938b5f01496fa8768bc8d5a808d6cd1a3005742a9c7c12e4a17cf9f070ee2d8'
]

from src.apps.merkle import MerkleTree
mt = MerkleTree()
mt.build_merkle_tree(data)

Get the merkle tree root

mt.get_root_tree()

'd84f7a7126b5b0ec9872893bb4c92741ee70c44d29043f2b0ff830116b9ee731'

Get the merkle tree

mt.get_merkle_tree()

[
    '43a26051362b8040b289abe93334a5e3662751aa691185ae9e9a2e1e0c169350', 
    'f2ff61e5ca30a7708bfa760e5749e10dc81452d9006f8fb35db69d8f490ed637', 
    '487dced11563793b12e4357f218d5a521c730fff52fc03552442bfac917691c0', 
    '0e3c7b0c7fed34610a5da469ea9e44e2965a3f6ae0ae2d5d1ba5e9f5ac9d345b', 
    '1938b5f01496fa8768bc8d5a808d6cd1a3005742a9c7c12e4a17cf9f070ee2d8', 
    'a1d17c81372869975854fe89a2f994a91caf5b337eeba198e40802ae047c373c', 
    'd5863df95241788aade3430587471066c6f23a6ed59aff3ee7559928acb518b1', 
    'd8f77eeaa7ae0f115390de80bb70082bbec539ab7b8e633ac149abd02ebc62a5', 
    'd84f7a7126b5b0ec9872893bb4c92741ee70c44d29043f2b0ff830116b9ee731'
]

Get a merkle proof by providing a value (a leaf)

mt.get_merkle_proof('43a26051362b8040b289abe93334a5e3662751aa691185ae9e9a2e1e0c169350')

[
    'f2ff61e5ca30a7708bfa760e5749e10dc81452d9006f8fb35db69d8f490ed637', 
    'd5863df95241788aade3430587471066c6f23a6ed59aff3ee7559928acb518b1', 
    '1938b5f01496fa8768bc8d5a808d6cd1a3005742a9c7c12e4a17cf9f070ee2d8'
]

Check if a value is in the Merkle tree

leaf = '43a26051362b8040b289abe93334a5e3662751aa691185ae9e9a2e1e0c169350'
proofs = [
    'f2ff61e5ca30a7708bfa760e5749e10dc81452d9006f8fb35db69d8f490ed637', 
    'd5863df95241788aade3430587471066c6f23a6ed59aff3ee7559928acb518b1', 
    '1938b5f01496fa8768bc8d5a808d6cd1a3005742a9c7c12e4a17cf9f070ee2d8'
]

root_tree = 'd84f7a7126b5b0ec9872893bb4c92741ee70c44d29043f2b0ff830116b9ee731'

assert mt.verify(proofs, root_tree, leaf) == True

Other Features

# Count leaves
mt.count_leaves()
5

# Get leaves
mt.get_leaves()
[
    '43a26051362b8040b289abe93334a5e3662751aa691185ae9e9a2e1e0c169350',  
    'f2ff61e5ca30a7708bfa760e5749e10dc81452d9006f8fb35db69d8f490ed637', 
    '487dced11563793b12e4357f218d5a521c730fff52fc03552442bfac917691c0', 
    '0e3c7b0c7fed34610a5da469ea9e44e2965a3f6ae0ae2d5d1ba5e9f5ac9d345b', 
    '1938b5f01496fa8768bc8d5a808d6cd1a3005742a9c7c12e4a17cf9f070ee2d8'
]

# Get depth of the Merkle tree
mt.get_depth()
4

# Get size of the Merkle tree
mt.get_size()
9

python-merkle-tree's People

Contributors

arnaudsene 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.