hashtree

package
v0.0.0-...-4f22fb0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

package hashtree implements a binary Merkle tree.

Merkle trees provide a simple way to verify the integrity of a large amount of data by way of "Merkle proofs" - a sequence of carefully selected hashes, one for each level of the tree.

Merkle trees can be n-ary trees, but here we implement binary Merkle trees, since they give the smallest sized proofs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Verify

func Verify(proof Proof, leaf, root Bytes32) bool

Verify verifies that a provided piece of data is contained within the merkle tree. True is returned if and only if the leaf is a member of this merkle tree.

Types

type Bytes32

type Bytes32 [sha256.Size]byte

Bytes32 is a convenience type to represent a 32 byte slice.

func (Bytes32) String

func (b Bytes32) String() string

String returns the string representation of the byte slice in hex.

type Proof

type Proof struct {
	Hashes    []Bytes32
	LeafIndex int
}

Proof represents a merkle proof, which is a sequence of sister hashes of a leaf node from the bottom to the root, that proves that a particular piece of data belongs to the tree. It also contains the index of the leaf in the bottom level of the tree to aid in verifying the proof.

type Tree

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

Tree represents a merkle tree.

func New

func New(data [][]byte) (*Tree, error)

New constructs a new merkle tree given some data.

func (*Tree) Height

func (t *Tree) Height() int

func (*Tree) ProofFor

func (t *Tree) ProofFor(i int) (p Proof, err error)

ProofFor returns the merkle proof for the leaf node at index i, or an error if that index does not exist.

func (*Tree) Root

func (t *Tree) Root() Bytes32

Root returns the root of the merkle tree. This is a single hash.

func (*Tree) String

func (t *Tree) String() string

func (*Tree) Update

func (t *Tree) Update(index int, data []byte) error

Update updates the leaf at the given index with the new data

Jump to

Keyboard shortcuts

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