merkle

package
v0.0.0-...-e976260 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 7 Imported by: 4

Documentation

Overview

Package merkle is responsible for creating the tree and verify the proof

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Layers

type Layers []Leaves

Layers is a representation of slice of Leaves slice

type Leaves

type Leaves []types.Leaf

Leaves is a representation of slice of leaf

type PartialTree

type PartialTree struct {
	// contains filtered or unexported fields
}

PartialTree represents a part of the original tree that is enough to calculate the root. Used in to extract the root in a merkle proof, to apply diff to a tree or to merge multiple trees into one. It is a rare case when you need to use this struct on it's own. It's mostly used inside

func NewPartialTree

func NewPartialTree(hasher types.Hasher) PartialTree

NewPartialTree Takes hasher as an argument and build a Merkle Tree from them. Since it's a partial tree, hashes must be accompanied by their index in the original tree.

func (*PartialTree) Root

func (pt *PartialTree) Root() []byte

Root returns the root of the tree, it is the first item hash of the last layer

type Proof

type Proof struct {
	// contains filtered or unexported fields
}

Proof is used to parse, verify, calculate a root for Merkle proofs. Proof requires specifying hashing algorithm and hash size in order to work. The hashing algorithm is set through the Hasher interface, which is supplied as a generic parameter to the Proof.

func NewProof

func NewProof(leaves Leaves, proofHashes [][]byte, totalLeavesCount uint64, hasher types.Hasher) Proof

NewProof create new instance of merkle proof

func (Proof) ProofHashes

func (p Proof) ProofHashes() [][]byte

ProofHashes returns all hashes from the proof, sorted from the left to right, bottom to top.

func (Proof) ProofHashesHex

func (p Proof) ProofHashesHex() []string

ProofHashesHex returns all hashes from the proof, sorted from the left to right, bottom to top, as a slice of lower hex strings.

func (Proof) Root

func (p Proof) Root() ([]byte, error)

Root calculates Merkle root based on provided leaves and proof hashes. Used inside the Verify method, but sometimes can be used on its own.

func (Proof) RootHex

func (p Proof) RootHex() (string, error)

RootHex calculates the root and serializes it into a hex string.

func (Proof) Verify

func (p Proof) Verify(expectedRoot []byte) (bool, error)

Verify uses proof to verify that a given set of elements is contained in the original data set the proof was made for.

type Tree

type Tree struct {
	UncommittedLeaves [][]byte
	// contains filtered or unexported fields
}

Tree is a Merkle Tree that is well suited for both basic and advanced usage.

Basic features include the creation and verification of Merkle proofs from a set of leaves. This is often done in various cryptocurrencies.

Advanced features include being able to make transactional changes to a tree with being able to roll back to any previously committed state of the tree. This scenario is similar to Git and can be found in databases and file systems.

func NewTree

func NewTree(hasher types.Hasher) Tree

NewTree creates a new instance of merkle tree. requires a hash algorithm to be specified.

func (Tree) FromLeaves

func (t Tree) FromLeaves(leaves [][]byte) (Tree, error)

FromLeaves clones the leaves and builds the tree from them

func (*Tree) Proof

func (t *Tree) Proof(proofIndices []uint64) Proof

Proof Returns the Merkle proof required to prove the inclusion of items in a data set.

func (*Tree) Root

func (t *Tree) Root() []byte

Root returns the tree root - the top hash of the tree. Used in the inclusion proof verification.

func (*Tree) RootHex

func (t *Tree) RootHex() string

RootHex returns a hex encoded string instead of

Jump to

Keyboard shortcuts

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