Giter Club home page Giter Club logo

merkletree's Introduction

merkletree

September 27, 2019

Summary: There are 2 implementations, one in python and one in go. The go version is not work for deletion and I have not updated the verification function yet. The python version is fully working.This is the first try at implementing a merkle tree that supports insertion, deletion, finding the merkle path and and merkle path verification using only the RootHash, merkle_path, entry, and location of entry relative to other leaves. trees merkle path. The implementation also supports printing tree, display the combination of leaf nodes that make up that node. merkletree.py includes 3 classes: Node, Entry, and MerkleTree. The merkletree supports look ups by keys (hash value of node) through the attribute node_map. Additionally, it can map an Entry object that was initially inserted to the corresponding leaf nodes with the entries_map, using the Entry object as a key. Nodes maintain pointers to there parent, left child, and right child. The Entry object stores a key and value, both in string form.

Note: the entry field of a Node stores the entry and is equivalent to LeafValue

An example of how to use Insertion, generateMerklePath, VerifyMerklePath, and Delete:

import merkletree
t = MerkleTree() #create a MerkleTree instance
a = Entry("a") # create an entry instance, initializing value to "a"
a.makeKey() #create a key based on output of SHA512(value)
t.Insert(a) #insert the complete entry into the tree
#insert will return a string of the inserted nodes key in hexadecimal

b = Entry("b") # create an entry instance, initializing value to "a"
b.makeKey() #create a key based on output of SHA512(value)
t.Insert(b)

c = makeEntryFromValue("c") # new function added for easier interface
#creates entry with value and key in one step
t.Insert(c)

b_path = t.generateMerklePath(b) # Pass an Entry instance to the method
#returns [hexDecSHA512(a)] which is the merkle path of b

t.VerifyMerklePath(b, 1, b_path) # Takes entry, location and merkle_path
#outputs boolean, in this case true. Performed in lg (n) without the use
#of the tree, other then for accesing root hash field for final comparison

t.Delete(b) # Returns updated root hash

Testing: Additional utilities are available in test.py. There is a function to print the entire tree and one print the merkle path with readable values. Large test inserts 7 values into a tree and prints the tree horizontally after each insertion (root left), showing the node value as its name. It then generates and prints a readable version of the MerklePaths for leaf nodes a, b, c, e, and g. Updated: there our now test for just deletion, verification.

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.