merkle

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: MPL-2.0 Imports: 2 Imported by: 2

README

go-merkle

go-merkle implements a simple merkle tree in Golang. It allows to obtain proofs and prove the validity of issued proofs.

Introduction

Merkle trees are binary trees built from the given data entries. The input entries i.e. leafs are first hashed. Then each subsequent row is built by hashing two element pairs from the current row using the smaller of the two as the first element when hashing. This is repeated until only 1 element remains.

Design

There are many different approaches to handling the remainder i.e. uneven input list. This implementation carries the odd remainder to the next level until consumed as shown in the diagram below:

     52ddb7875b4b  666eec13ab35  7e4729b49fce  31fcacdabf8a  8f5564afe39d
               \    /                      \    /                 |
                \  /                        \  /                  |
                 \/                          \/                   V
            40a65894031a                fed10b5a5178         8f5564afe39d
                             \    /                               |
                              \  /                                |
                               \/                                 V
                          21fd9dfe5c21                       8f5564afe39d
                                             \    /
                                              \  /
                                               \/
                                          8846d886ed99

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidProof

func IsValidProof(sh hash.Hash, leaf []byte, proof [][]byte) bool

Types

type Tree

type Tree [][][]byte

func NewTree

func NewTree(sh hash.Hash, leafs ...[]byte) Tree

NewTree builds and returns a new merkle tree with the given leaf nodes. Each input leaf is hashed before the tree is built

func (Tree) Height

func (t Tree) Height() int

func (Tree) LeafIndex

func (t Tree) LeafIndex(leaf []byte) int

LeafIndex returns the index of the given leaf and a -1 if the leaf is not found

func (Tree) Leafs

func (t Tree) Leafs() [][]byte

Leafs returns the leafs in the tree. This is the hashes of the original input data

func (Tree) Proof

func (t Tree) Proof(leaf []byte) (out [][]byte)

Proof returns the proof of a given leaf node.

func (Tree) Root

func (t Tree) Root() []byte

Root returns the root of the merkle tree i.e the merkle root

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL