merkletree

package
v0.0.0-...-a1a22e4 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2017 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LeafPrefix is the domain separation prefix for leaf hashes.
	LeafPrefix = 0

	// NodePrefix is the domain separation prefix for internal tree nodes.
	NodePrefix = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CPPMerkleTree

type CPPMerkleTree struct {
	FullMerkleTreeInterface
	// contains filtered or unexported fields
}

CPPMerkleTree provides an interface to the C++ CT MerkleTree library. See the go/README file for details on how to build this.

func NewCPPMerkleTree

func NewCPPMerkleTree() *CPPMerkleTree

NewCPPMerkleTree returns a new wrapped C++ MerkleTree, using the Sha256Hasher. It is the caller's responsibility to call DeletePeer() when finished with the tree to deallocate its resources.

func (*CPPMerkleTree) AddLeaf

func (m *CPPMerkleTree) AddLeaf(leaf []byte) uint64

AddLeaf ads a new leaf to the hash tree. Stores the hash of the leaf data in the tree structure, does not store the data itself. Returns the position of the leaf in the tree.

func (*CPPMerkleTree) AddLeafHash

func (m *CPPMerkleTree) AddLeafHash(hash []byte) uint64

AddLeafHash adds a leaf hash directly to the tree. Returns the position of the leaf in the tree.

func (*CPPMerkleTree) CurrentRoot

func (m *CPPMerkleTree) CurrentRoot() ([]byte, error)

CurrentRoot returns the current root of the tree.

func (*CPPMerkleTree) DeletePeer

func (m *CPPMerkleTree) DeletePeer()

DeletePeer deallocates the memory used by the C++ MerkleTree peer.

func (*CPPMerkleTree) LeafCount

func (m *CPPMerkleTree) LeafCount() uint64

LeafCount is the number of leafs in the tree.

func (*CPPMerkleTree) LeafHash

func (m *CPPMerkleTree) LeafHash(leaf uint64) ([]byte, error)

LeafHash returns the leaf hash for the leaf at the requested index.

func (*CPPMerkleTree) LevelCount

func (m *CPPMerkleTree) LevelCount() uint64

LevelCount is the number of levels in the tree.

func (*CPPMerkleTree) PathToCurrentRoot

func (m *CPPMerkleTree) PathToCurrentRoot(leaf uint64) ([][]byte, error)

PathToCurrentRoot returns an audit path to the current root for a given leaf.

func (*CPPMerkleTree) PathToRootAtSnapshot

func (m *CPPMerkleTree) PathToRootAtSnapshot(leaf, snapshot uint64) ([][]byte, error)

PathToRootAtSnapshot returns an audit path to a given root for a given leaf.

func (*CPPMerkleTree) RootAtSnapshot

func (m *CPPMerkleTree) RootAtSnapshot(snapshot uint64) ([]byte, error)

RootAtSnapshot returns the root at a given index.

func (*CPPMerkleTree) SnapshotConsistency

func (m *CPPMerkleTree) SnapshotConsistency(snapshot1, snapshot2 uint64) ([][]byte, error)

SnapshotConsistency returns a consistency proof between two given snapshots.

type FullMerkleTreeInterface

type FullMerkleTreeInterface interface {
	MerkleTreeInterface

	// RootAtSnapshot returns the root hash at the tree size |snapshot|
	// which must be <= than the current tree size.
	RootAtSnapshot(snapshot uint64) ([]byte, error)

	// PathToCurrentRoot returns the Merkle path (or inclusion proof) from the
	// leaf hash at index |leaf| to the current root.
	PathToCurrentRoot(leaf uint64) ([]byte, error)

	// SnapshotConsistency returns a consistency proof between the two tree
	// sizes specified in |snapshot1| and |snapshot2|.
	SnapshotConsistency(snapshot1, snapshot2 uint64) ([]byte, error)
}

FullMerkleTreeInterface extends MerkleTreeInterface to the full range of operations that only a non-compact tree representation can implement.

type HasherFunc

type HasherFunc func([]byte) []byte

HasherFunc takes a slice of bytes and returns a cryptographic hash of those bytes.

type MerkleTreeInterface

type MerkleTreeInterface interface {
	// LeafCount returns the number of leaves in the tree
	LeafCount() uint64

	// LevelCount returns the number of levels in the tree
	LevelCount() uint64

	// AddLeaf adds the hash of |leaf| to the tree and returns the newly added
	// leaf index
	AddLeaf(leaf []byte) uint64

	// LeafHash returns the hash of the leaf at index |leaf| or a non-nil error.
	LeafHash(leaf uint64) ([]byte, error)

	// CurrentRoot returns the current root hash of the merkle tree.
	CurrentRoot() ([]byte, error)
}

MerkleTreeInterface represents the common interface for basic MerkleTree functions.

type MerkleVerifier

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

MerkleVerifier is a class which knows how to verify merkle inclusion and consistency proofs.

func NewMerkleVerifier

func NewMerkleVerifier(h HasherFunc) MerkleVerifier

NewMerkleVerifier returns a new MerkleVerifier for a tree based on the passed in hasher.

func (MerkleVerifier) RootFromInclusionProof

func (m MerkleVerifier) RootFromInclusionProof(leafIndex, treeSize int64, proof [][]byte, leaf []byte) ([]byte, error)

RootFromInclusionProof calculates the expected tree root given the proof and leaf. leafIndex starts at 0. treeSize starts at 1.

func (MerkleVerifier) VerifyConsistencyProof

func (m MerkleVerifier) VerifyConsistencyProof(snapshot1, snapshot2 int64, root1, root2 []byte, proof [][]byte) error

VerifyConsistencyProof checks that the passed in consistency proof is valid between the passed in tree snapshots.

func (MerkleVerifier) VerifyInclusionProof

func (m MerkleVerifier) VerifyInclusionProof(leafIndex, treeSize int64, proof [][]byte, root []byte, leaf []byte) error

VerifyInclusionProof verifies the correctness of the proof given the passed in information about the tree and leaf.

type RootMismatchError

type RootMismatchError struct {
	ExpectedRoot   []byte
	CalculatedRoot []byte
}

RootMismatchError occurs when an inclusion proof fails.

func (RootMismatchError) Error

func (e RootMismatchError) Error() string

type TreeHasher

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

TreeHasher performs the various hashing operations required when manipulating MerkleTrees.

func NewTreeHasher

func NewTreeHasher(h HasherFunc) *TreeHasher

NewTreeHasher returns a new TreeHasher based on the passed in hash.

func (TreeHasher) HashChildren

func (h TreeHasher) HashChildren(left, right []byte) []byte

HashChildren returns the merkle hash of the two passed in children.

func (TreeHasher) HashEmpty

func (h TreeHasher) HashEmpty() []byte

HashEmpty returns the hash of the empty string.

func (TreeHasher) HashLeaf

func (h TreeHasher) HashLeaf(leaf []byte) []byte

HashLeaf returns the hash of the passed in leaf, after applying domain separation.

Jump to

Keyboard shortcuts

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