merkletree

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 6 Imported by: 0

README

go-merkletree

The mht implemented for the podr2 algorithm can find the auxiliary nodes through several leaf nodes. mht root can be restored using leaf nodes and auxiliary nodes

package main

import (
	"crypto/sha256"
	"encoding/hex"
	"github.com/CESSProject/go-merkletree"
	"log"
)

//TestContent implements the Content interface provided by merkletree and represents the content stored in the tree.
type TestContent struct {
	x string
}

//CalculateHash hashes the values of a TestContent
func (t TestContent) CalculateHash() ([]byte, error) {
	h := sha256.New()
	if _, err := h.Write([]byte(t.x)); err != nil {
		return nil, err
	}

	return h.Sum(nil), nil
}

//Equals tests for equality of two Contents
func (t TestContent) Equals(other merkletree.Content) (bool, error) {
	return t.x == other.(TestContent).x, nil
}

func main() {
	//Build list of Content to build tree
	var list []merkletree.Content
	list = append(list, TestContent{x: "Dog"})
	list = append(list, TestContent{x: "Cat"})
	list = append(list, TestContent{x: "Bird"})
	list = append(list, TestContent{x: "Panda"})
	list = append(list, TestContent{x: "Wolf"})
	list = append(list, TestContent{x: "Dolphin"})
	list = append(list, TestContent{x: "Geoduck"})
	list = append(list, TestContent{x: "Eagle"})
	list = append(list, TestContent{x: "Giraffe"})
	//Create a new Merkle Tree from the list of Content
	t, err := merkletree.NewTree(list)
	if err != nil {
		log.Fatal(err)
	}

	//The index of the incoming leaf node.
	testLeafs := []int64{0,9}
	_, nodeMap, nodes, _ := t.GetMerkleAuxiliaryNode(merkletree.GetContentMap(testLeafs))

	for k := 0; k < len(nodes); k++ {
		log.Printf(" merkle path , %v is %s ,nodes location is:(%v;%v)\n", k, hex.EncodeToString(nodes[k].Hash), nodes[k].Height, nodes[k].Index)
	}

	//Get the structure of the reconstructed node, the incoming leaf node and its auxiliary node.
	ProofList,err:=t.RebuildNodeListWithTree(nodeMap,testLeafs)
	if err!=nil{
		log.Fatal(err)
	}

	//rebuild tree ,get root node
	root,err:=merkletree.NewTreeWithAuxiliaryNode(ProofList,sha256.New)
	log.Println("Rebuild MHT ROOT Hash is:", hex.EncodeToString(root.Hash))

	//Get the Merkle Root of the tree.
	mr := t.MerkleRoot()
	log.Println("MHT ROOT Hash is:", hex.EncodeToString(mr))

	//String representation
	log.Println(t)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContentMap

func GetContentMap(indexs []int64) *map[int64]struct{}

func MidOrder

func MidOrder(tree *Node, subtreeLeafs *[]Content)

func PostOrder

func PostOrder(tree *Node, subtreeLeafs *[]Content)

func RebuildNodeList added in v0.0.2

func RebuildNodeList(nodes *[]NodeSerializable) [][]*Node

func SortNodeIndex

func SortNodeIndex(nodes *[]*Node)

SortNodeIndex: Bubble Sort

func Swap

func Swap(arr *[]*Node, i, j int)

Types

type Content

type Content interface {
	CalculateHash() ([]byte, error)
	Equals(other Content) (bool, error)
}

Content represents the data that is stored and verified by the tree. A type that implements this interface can be used as an item in the tree.

type MerkleTree

type MerkleTree struct {
	Root *Node

	Leafs []*Node
	// contains filtered or unexported fields
}

MerkleTree is the container for the tree. It holds a pointer to the root of the tree, a list of pointers to the leaf nodes, and the merkle root.

func GenerateSubtreeMHT

func GenerateSubtreeMHT(subTreeRoot Node) (*MerkleTree, error)

GenerateSubtreeMHT Construct an MHT with a branch node as the root node

func NewTree

func NewTree(cs []Content) (*MerkleTree, error)

NewTree creates a new Merkle Tree using the content cs.

func NewTreeWithHashStrategy

func NewTreeWithHashStrategy(cs []Content, hashStrategy func() hash.Hash) (*MerkleTree, error)

NewTreeWithHashStrategy creates a new Merkle Tree using the content cs using the provided hash strategy. Note that the hash type used in the type that implements the Content interface must match the hash type profided to the tree.

func NewTreeWithIndexAndHeight

func NewTreeWithIndexAndHeight(cs []Content, index, height int64) (*MerkleTree, error)

NewTreeWithIndexAndHeight Used when constructing subtrees, create a tree through consecutive nodes of any height, but retain the index of its leaf nodes

func (*MerkleTree) GetMerkleAuxiliaryNode

func (m *MerkleTree) GetMerkleAuxiliaryNode(content *map[int64]struct{}) ([][]byte, map[int64][]int64, []*Node, error)

GetMerkleAuxiliaryNode: Get Merkle path ,merkle path map and list of auxiliary node

func (*MerkleTree) GetMerklePath

func (m *MerkleTree) GetMerklePath(content Content) ([][]byte, []int64, error)

GetMerklePath: Get Merkle path and indexes(left leaf or right leaf)

func (*MerkleTree) GetNodeFromCoordinate

func (m *MerkleTree) GetNodeFromCoordinate(Height, Index int64) (*Node, error)

func (*MerkleTree) MerkleRoot

func (m *MerkleTree) MerkleRoot() []byte

MerkleRoot returns the unverified Merkle Root (hash of the root node) of the tree.

func (*MerkleTree) RebuildNodeListWithTree added in v0.0.2

func (t *MerkleTree) RebuildNodeListWithTree(auxiliary map[int64][]int64, leaf []int64) ([][]*Node, error)

RebuildNodeListWithTree use map to describe the hierarchical relationship of each node participating in the construction of the tree auxiliary: the list of auxiliary nodes, and the list of original nodes can be traced back to the Root node original: a list of original nodes, and a list of auxiliary nodes can be traced back to the Root node

func (*MerkleTree) RebuildTree

func (m *MerkleTree) RebuildTree() error

RebuildTree is a helper function that will rebuild the tree reusing only the content that it holds in the leaves.

func (*MerkleTree) RebuildTreeWith

func (m *MerkleTree) RebuildTreeWith(cs []Content) error

RebuildTreeWith replaces the content of the tree and does a complete rebuild; while the root of the tree will be replaced the MerkleTree completely survives this operation. Returns an error if the list of content cs contains no entries.

func (*MerkleTree) String

func (m *MerkleTree) String() string

String returns a string representation of the tree. Only leaf nodes are included in the output.

func (*MerkleTree) VerifyContent

func (m *MerkleTree) VerifyContent(content Content) (bool, error)

VerifyContent indicates whether a given content is in the tree and the hashes are valid for that content. Returns true if the expected Merkle Root is equivalent to the Merkle root calculated on the critical path for a given content. Returns true if valid and false otherwise.

func (*MerkleTree) VerifyTree

func (m *MerkleTree) VerifyTree() (bool, error)

VerifyTree verify tree validates the hashes at each level of the tree and returns true if the resulting hash at the root of the tree matches the resulting root hash; returns false otherwise.

type Node

type Node struct {
	Tree   *MerkleTree
	Parent *Node
	Left   *Node
	Right  *Node

	Hash   []byte
	C      Content
	Height int64 //The height of the node, the height of the leaf node is 0
	Index  int64 //The serial number of the node in this layer, the serial number of the leftmost node is 0
	// contains filtered or unexported fields
}

Node represents a node, root, or leaf in the tree. It stores pointers to its immediate relationships, a hash, the content stored if it is a leaf, and other metadata.

func NewTreeWithAuxiliaryNode

func NewTreeWithAuxiliaryNode(evidence [][]*Node, hashStrategy func() hash.Hash) (*Node, error)

func (*Node) String

func (n *Node) String() string

String returns a string representation of the node.

type NodeSerializable added in v0.0.2

type NodeSerializable struct {
	Hash   []byte
	Height int64
	Index  int64
}

Jump to

Keyboard shortcuts

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