Documentation ¶
Overview ¶
Package merkle
A merkle tree is a kind of binary tree where each node is labelled with a hash. Starting from the very bottom, leaves will be paired and hashed together to make their parent inner-node, recursively up to the root a.k.a merkle root.
Merkle trees, are commonly used in distributed systems to efficiently compare large data set ensuring validity of such data.
Some examples that leverage merkle trees are the : - Git version control - AWS's QLDB - Apache's Cassandra - blockchains
There are different flavours of implementations, this package doesn't attempt to build an abstraction for all the possible ones out there, it rather implements a fairly efficient specific one that can be used to experiment with the data structure and concepts.
With that said, if you're looking to use this package to validate merkle proofs for existing blockchains you should look elsewhere as their implementation may be different. For example, Bitcoin's merkle, duplicates eventual odd nodes to re-balance the tree and this implementation doesn't, thus producing a different merkle root and proof.
Index ¶
- func Verify(algo hash.Hash, leaf, root []byte, proof [][]byte) bool
- type Node
- type Nodes
- func (ns Nodes) IteratePair(fn func(i, j *Node)) (odd *Node)
- func (ns Nodes) IterateSortedPair(fn func(i, j *Node)) (odd *Node)
- func (ns Nodes) Len() int
- func (ns Nodes) Less(i, j int) bool
- func (ns Nodes) Swap(i, j int)
- func (ns Nodes) ToByteArrays() [][]byte
- func (ns Nodes) ToHexStrings() []string
- type Tree
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a merkle tree node.
func (*Node) Graphify ¶
Graphify builds up a hierarchical graphic representation from the Node to the very bottom of its children. Will write to the provided io.Writer for greater usability.
For example, to print in your terminal you may do :
n.Graphify(os.Stdout)
where n is the Node instance you want to print from.
func (*Node) Sibling ¶
Sibling returns its opposite sibling. Given 2 nodes i, j if Node is i returns j else returns i. Returns nil if root.
func (*Node) WalkPreOrder ¶
WalkPreOrder traverses from the tree *Node down to the very bottom using the "Pre Order" strategy.
type Nodes ¶
type Nodes []*Node
Nodes is slice type of *Node.
func (Nodes) IteratePair ¶
IteratePair iterates through all Nodes pairing with fn(i,j). If there is an odd number Nodes the last element Node len(n) - 1 will be returned.
func (Nodes) IterateSortedPair ¶
IterateSortedPair iterate same as IteratePair but with sorted ascending i,j.
func (Nodes) ToByteArrays ¶
ToByteArrays converts each Node in Nodes into a slice of byte array.
func (Nodes) ToHexStrings ¶
ToHexStrings converts each Node in Nodes into an hex strings.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree is a whole merkle tree.
func NewTree ¶
NewTree builds up a new merkle tree with the provided hashing algorithm and set of leaves that have been hashed with the same algorithm.