merkle

package module
v0.0.0-...-80439e0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2018 License: ISC Imports: 4 Imported by: 0

README

merkle

Go Report Card GoDoc GoCover

Package merkle implements a very simple, immutable, in-memory, generic, "hash function-agnostic" merkle tree.

Documentation is provided in godoc, as usual.

Documentation

Overview

Package merkle implements a very simple, immutable, in-memory, generic, "hash function-agnostic" merkle tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Datum

type Datum interface {
	// Serialize must return a serialized representation of the Datum.
	Serialize() []byte
}

Datum is the interface that any piece of data has to implement so as to be able to be contained in the leaves of the merkle tree.

type ErrHashUnavailable

type ErrHashUnavailable struct{}

ErrHashUnavailable signifies that the requested hash function has not been linked into the binary.

func (ErrHashUnavailable) Error

func (ErrHashUnavailable) Error() string

type ErrNoData

type ErrNoData struct{}

ErrNoData signifies that the piece of data requested is either nil or not present in the merkle tree.

func (ErrNoData) Error

func (ErrNoData) Error() string

type Tree

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

Tree is the exported struct to interact with the merkle tree.

func NewTree

func NewTree(hash crypto.Hash, data ...Datum) (*Tree, error)

NewTree creates a new merkle tree given one of the available (i.e. linked into the binary) hash functions and a bunch of data.

It returns a non-nil error either if the requested hash function has not been linked into the binary, or if data are not given at all.

func (*Tree) AppendAndReconstruct

func (t *Tree) AppendAndReconstruct(data ...Datum)

AppendAndReconstruct appends the given data as new tree leaves, and reconstructs the merkle tree to take them into account as well.

This obviously modifies the merkle root of the tree.

func (*Tree) DeleteAndReconstruct

func (t *Tree) DeleteAndReconstruct(data ...Datum)

DeleteAndReconstruct deletes the given data from the tree leaves, and reconstructs the merkle tree on the new (reduced) number of leaves.

This obviously modifies the merkle root of the tree.

func (*Tree) Height

func (t *Tree) Height() int

Height returns the height of the merkle tree, including both its leaves and the merkle nodes.

func (*Tree) Leaves

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

Leaves returns a slice of all pieces of Data stored in the merkle tree (in their serialized format) in the order that they were inserted by the user.

func (*Tree) MerkleRoot

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

MerkleRoot returns the hash digest of the root of the merkle tree.

func (*Tree) MerkleSize

func (t *Tree) MerkleSize() (merkleSize int)

MerkleSize returns the number of merkle nodes in the merkle trees, i.e. the total number of nodes in the merkle tree, excluding its leaves.

func (*Tree) NumLeaves

func (t *Tree) NumLeaves() (numLeaves int)

NumLeaves returns the number of leaves in the merkle tree.

func (*Tree) Size

func (t *Tree) Size() int

Size returns the total number of nodes in the merkle tree, including both its leaves and the merkle nodes.

func (*Tree) VerifyDatum

func (t *Tree) VerifyDatum(datum Datum) (bool, error)

VerifyDatum verifies that the given Datum is present in the merkle tree, in which case it returns true and a nil error value.

It requires O(log2(L)) search among the leaves and O(log2(L)) hash calculations.

If the given hash digest cannot be verified, VerifyDatum returns false. If the given hash digest cannot be found in one of the merkle tree's leaves, VerifyDatum returns false and a non-nil error value.

func (*Tree) VerifyDigest

func (t *Tree) VerifyDigest(digest []byte) (bool, error)

VerifyDigest verifies that the given (leaf) hash digest is present in the merkle tree, in which case it returns true and a nil error value.

It requires O(L) search among the leaves and O(log2(L)) hash calculations.

If the given hash digest cannot be verified, VerifyDigest returns false. If the given hash digest cannot be found in one of the merkle tree's leaves, VerifyDigest returns false and a non-nil error value.

func (*Tree) VerifyOrderedID

func (t *Tree) VerifyOrderedID(orderedID uint) (bool, error)

VerifyOrderedID verifies that the Datum with the given ordered ID (based on the order that the leaves were initially given) is present in the merkle tree, in which case it returns true and a nil error value.

It requires O(L) search among the leaves and O(log2(L)) hash calculations.

If the given hash digest cannot be verified, VerifyOrderedID returns false. If the given hash digest cannot be found in one of the merkle tree's leaves, VerifyOrderedID returns false and a non-nil error value.

func (*Tree) VerifySerializedDatum

func (t *Tree) VerifySerializedDatum(serializedDatum []byte) (bool, error)

VerifySerializedDatum verifies that the given Datum (given in its serialized format) is present in the merkle tree, in which case it returns true and a nil error value.

It requires O(log2(L)) search among the leaves and O(log2(L)) hash calculations.

If the given hash digest cannot be verified, VerifySerializedDatum returns false. If the given hash digest cannot be found in one of the merkle tree's leaves, VerifySerializedDatum returns false and a non-nil error value.

Jump to

Keyboard shortcuts

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