Documentation ¶
Overview ¶
Package merkle provides Merkle tree manipulation functions.
Index ¶
- Constants
- Variables
- func Order(a, b []byte) bool
- func Root(leaves [][]byte) crypto.Digest
- type Hasher
- type InMemoryMerkleTree
- func (mt *InMemoryMerkleTree) AddLeaf(leafData []byte) (int64, TreeEntry, error)
- func (mt *InMemoryMerkleTree) CurrentRoot() TreeEntry
- func (mt *InMemoryMerkleTree) LeafCount() int64
- func (mt *InMemoryMerkleTree) LeafHash(leaf int64) []byte
- func (mt *InMemoryMerkleTree) LevelCount() int64
- func (mt *InMemoryMerkleTree) NodeCount(level int64) int64
- func (mt *InMemoryMerkleTree) PathToCurrentRoot(leaf int64) []TreeEntryDescriptor
- func (mt *InMemoryMerkleTree) PathToRootAtSnapshot(leaf int64, snapshot int64) []TreeEntryDescriptor
- func (mt *InMemoryMerkleTree) RootAtSnapshot(snapshot int64) TreeEntry
- func (mt *InMemoryMerkleTree) SnapshotConsistency(snapshot1 int64, snapshot2 int64) []TreeEntryDescriptor
- type TreeEntry
- type TreeEntryDescriptor
Constants ¶
const ( LeafHashPrefix = 0 NodeHashPrefix = 1 )
Domain separation prefixes
Variables ¶
var DefaultHasher = New(crypto.SHA256) // XXX TODO make SHA512_256 and update test vectors
DefaultHasher is a SHA512-256 based LogHasher.
Functions ¶
Types ¶
type Hasher ¶
Hasher implements the RFC6962 tree hashing algorithm but with SHA512-256 instead of SHA256
func (*Hasher) HashChildren ¶
HashChildren returns the inner Merkle tree node hash of the the two child nodes l and r. The hashed structure is NodeHashPrefix||l||r.
type InMemoryMerkleTree ¶
type InMemoryMerkleTree struct {
// contains filtered or unexported fields
}
InMemoryMerkleTree holds a Merkle Tree in memory as a 2D node array
func NewInMemoryMerkleTree ¶
func NewInMemoryMerkleTree(hasher hashers.LogHasher) *InMemoryMerkleTree
NewInMemoryMerkleTree creates a new empty Merkle Tree using the specified Hasher
func (*InMemoryMerkleTree) AddLeaf ¶
func (mt *InMemoryMerkleTree) AddLeaf(leafData []byte) (int64, TreeEntry, error)
AddLeaf adds a new leaf to the hash tree. Stores the hash of the leaf data in the tree structure, does not store the data itself.
(We will evaluate the tree lazily, and not update the root here.)
Returns the position of the leaf in the tree. Indexing starts at 1, so position = number of leaves in the tree after this update.
func (*InMemoryMerkleTree) CurrentRoot ¶
func (mt *InMemoryMerkleTree) CurrentRoot() TreeEntry
CurrentRoot set the current root of the tree. Updates the root to reflect the current shape of the tree and returns the tree digest.
Returns the hash of an empty string if the tree has no leaves (and hence, no root).
func (*InMemoryMerkleTree) LeafCount ¶
func (mt *InMemoryMerkleTree) LeafCount() int64
LeafCount returns the number of leaves in the tree.
func (*InMemoryMerkleTree) LeafHash ¶
func (mt *InMemoryMerkleTree) LeafHash(leaf int64) []byte
LeafHash returns the hash of the requested leaf.
func (*InMemoryMerkleTree) LevelCount ¶
func (mt *InMemoryMerkleTree) LevelCount() int64
LevelCount returns the number of levels in the current Merkle tree
func (*InMemoryMerkleTree) NodeCount ¶
func (mt *InMemoryMerkleTree) NodeCount(level int64) int64
NodeCount gets the current node count (of the lazily evaluated tree). Caller is responsible for keeping track of the lazy evaluation status. This will not update the tree.
func (*InMemoryMerkleTree) PathToCurrentRoot ¶
func (mt *InMemoryMerkleTree) PathToCurrentRoot(leaf int64) []TreeEntryDescriptor
PathToCurrentRoot get the Merkle path from leaf to root for a given leaf.
Returns a slice of node hashes, ordered by levels from leaf to root. The first element is the sibling of the leaf hash, and the last element is one below the root. Returns an empty slice if the tree is not large enough or the leaf index is 0.
func (*InMemoryMerkleTree) PathToRootAtSnapshot ¶
func (mt *InMemoryMerkleTree) PathToRootAtSnapshot(leaf int64, snapshot int64) []TreeEntryDescriptor
PathToRootAtSnapshot gets the Merkle path from a leaf to the root for a previous snapshot.
Returns a slice of node hashes, ordered by levels from leaf to root. The first element is the sibling of the leaf hash, and the last element is one below the root. Returns an empty slice if the leaf index is 0, the snapshot requested is in the future or the snapshot tree is not large enough.
func (*InMemoryMerkleTree) RootAtSnapshot ¶
func (mt *InMemoryMerkleTree) RootAtSnapshot(snapshot int64) TreeEntry
RootAtSnapshot gets the root of the tree for a previous snapshot, where snapshot 0 is an empty tree, snapshot 1 is the tree with 1 leaf, etc.
Returns an empty string if the snapshot requested is in the future (i.e., the tree is not large enough).
func (*InMemoryMerkleTree) SnapshotConsistency ¶
func (mt *InMemoryMerkleTree) SnapshotConsistency(snapshot1 int64, snapshot2 int64) []TreeEntryDescriptor
SnapshotConsistency gets the Merkle consistency proof between two snapshots. Returns a slice of node hashes, ordered according to levels. Returns an empty slice if snapshot1 is 0, snapshot 1 >= snapshot2, or one of the snapshots requested is in the future.
type TreeEntry ¶
type TreeEntry struct {
// contains filtered or unexported fields
}
TreeEntry is used for nodes in the tree for better readability. Just holds a hash but could be extended
type TreeEntryDescriptor ¶
type TreeEntryDescriptor struct { Value TreeEntry XCoord int64 // The horizontal node coordinate YCoord int64 // The vertical node coordinate }
TreeEntryDescriptor wraps a node and is used to describe tree paths, which are useful to have access to when testing the code and examining how it works