merkle

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2020 License: MIT Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LeafHash

func LeafHash(h hash.Hash, out, in []byte) []byte

LeafHash produces the hash of a leaf of a Tree.

Types

type HTree

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

HTree accepts a sequence of leaf hashes via its Add method. A leaf hash is the result of calling LeafHash on a string. After adding all leaf hashes in the sequence, their merkle root hash may be read via the Root method.

Note that a Tree works by converting its input from a sequence of strings to the corresponding sequence of leaf hashes and feeding those to an HTree.

func NewHTree

func NewHTree(hasher hash.Hash) *HTree

NewHTree produces a new HTree.

func NewProofHTree

func NewProofHTree(hasher hash.Hash, ref []byte) *HTree

NewProofHTree produces a new HTree that can compactly prove a given reference hash is in it. After adding elements to the tree, call Proof to get the proof.

func (*HTree) Add

func (h *HTree) Add(item []byte)

Add adds a leaf hash to the sequence in h. The caller must not reuse the space in item. It is an error to call Add after a call to Root or Proof.

func (*HTree) Proof

func (h *HTree) Proof() Proof

Proof returns the merkle proof for the reference hash given to NewProofHTree. It is an error to call Add after a call to Proof.

func (*HTree) Root

func (h *HTree) Root() []byte

Root returns the merkle root hash for the sequence of leaf hashes that have been added to h with Add. It is an error to call Add after a call to Root.

type Proof

type Proof []ProofStep

Proof is a merkle proof.

func (Proof) Hash

func (p Proof) Hash(hasher hash.Hash, ref []byte) []byte

Hash computes the hash of a merkle proof. A valid merkle proof hash matches the root hash of the merkle tree it came from.

To validate a proof made with NewProofTree(..., x) (for some byte sequence x) it is necessary to call proof.Hash(..., LeafHash(..., ..., x))

To validate a proof made with NewProofHTree(..., x) it is only necessary to call proof.Hash(..., x).

type ProofStep

type ProofStep struct {
	H    []byte
	Left bool
}

ProofStep is one step in a merkle proof.

type Tree

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

Tree accepts a sequence of strings via its Add method. It builds a merkle hash tree from them. After adding all strings in the sequence, their merkle root hash may be read via the Root method.

func NewProofTree

func NewProofTree(hasher hash.Hash, ref []byte) *Tree

NewProofTree produces a new Tree that can compactly prove a given string is in it. After adding elements to the tree, call Proof to get the proof.

func NewTree

func NewTree(hasher hash.Hash) *Tree

NewTree produces a new Tree.

func (*Tree) Add

func (m *Tree) Add(str []byte)

Add adds a string to the sequence in m. The caller may reuse the space in str. It is an error to call Add after a call to Root or Proof.

func (*Tree) Proof

func (m *Tree) Proof() Proof

Proof returns the merkle proof for the reference string given to NewProofTree. It is an error to call Add after a call to Proof.

func (*Tree) Root

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

Root returns the merkle root hash for the sequence of strings that have been added to m with Add. It is an error to call Add after a call to Root.

Jump to

Keyboard shortcuts

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