Documentation ¶
Overview ¶
Package merkletree implements a Merkle Tree capable of storing arbitrary content.
A Merkle Tree is a hash tree that provides an efficient way to verify the contents of a set data are present and untampered with. At its core, a Merkle Tree is a list of items representing the data that should be verified. Each of these items is inserted into a leaf node and a tree of hashes is constructed bottom up using a hash of the nodes left and right children's hashes. This means that the root node will effictively be a hash of all other nodes (hashes) in the tree. This property allows the tree to be reproduced and thus verified by on the hash of the root node of the tree. The benefit of the tree structure is verifying any single content entry in the tree will require only nlog2(n) steps in the worst case.
Creating a new merkletree requires that the type that the tree will be constructed from implements the Content interface.
type Content interface { CalculateHash() []byte Equals(other Content) bool }
A slice of the Content items should be created and then passed to the NewTree method.
t, err := merkle.NewTree(list)
t represents the Merkle Tree and can be verified and manipulated with the API methods described below.
Index ¶
- type Content
- type MerkleTree
- func (m *MerkleTree) GetMerklePath(content Content) ([][]byte, []int64, error)
- func (m *MerkleTree) MerkleRoot() []byte
- func (m *MerkleTree) RebuildTree() error
- func (m *MerkleTree) RebuildTreeWith(cs []Content) error
- func (m *MerkleTree) String() string
- func (m *MerkleTree) VerifyContent(content Content) (bool, error)
- func (m *MerkleTree) VerifyTree() (bool, error)
- type Node
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Content ¶
Content represents the data that is stored and verified by the tree. A type that implements this interface can be used as an item in the tree.
type MerkleTree ¶
MerkleTree is the container for the tree. It holds a pointer to the root of the tree, a list of pointers to the leaf nodes, and the merkle root.
func NewTree ¶
func NewTree(cs []Content) (*MerkleTree, error)
NewTree creates a new Merkle Tree using the content cs.
func NewTreeWithHashStrategy ¶ added in v0.2.0
func NewTreeWithHashStrategy(cs []Content, hashStrategy func() hash.Hash) (*MerkleTree, error)
NewTreeWithHashStrategy creates a new Merkle Tree using the content cs using the provided hash strategy. Note that the hash type used in the type that implements the Content interface must match the hash type profided to the tree.
func (*MerkleTree) GetMerklePath ¶
func (m *MerkleTree) GetMerklePath(content Content) ([][]byte, []int64, error)
GetMerklePath: Get Merkle path and indexes(left leaf or right leaf)
func (*MerkleTree) MerkleRoot ¶
func (m *MerkleTree) MerkleRoot() []byte
MerkleRoot returns the unverified Merkle Root (hash of the root node) of the tree.
func (*MerkleTree) RebuildTree ¶
func (m *MerkleTree) RebuildTree() error
RebuildTree is a helper function that will rebuild the tree reusing only the content that it holds in the leaves.
func (*MerkleTree) RebuildTreeWith ¶
func (m *MerkleTree) RebuildTreeWith(cs []Content) error
RebuildTreeWith replaces the content of the tree and does a complete rebuild; while the root of the tree will be replaced the MerkleTree completely survives this operation. Returns an error if the list of content cs contains no entries.
func (*MerkleTree) String ¶
func (m *MerkleTree) String() string
String returns a string representation of the tree. Only leaf nodes are included in the output.
func (*MerkleTree) VerifyContent ¶
func (m *MerkleTree) VerifyContent(content Content) (bool, error)
VerifyContent indicates whether a given content is in the tree and the hashes are valid for that content. Returns true if the expected Merkle Root is equivalent to the Merkle root calculated on the critical path for a given content. Returns true if valid and false otherwise.
func (*MerkleTree) VerifyTree ¶
func (m *MerkleTree) VerifyTree() (bool, error)
VerifyTree verify tree validates the hashes at each level of the tree and returns true if the resulting hash at the root of the tree matches the resulting root hash; returns false otherwise.
type Node ¶
type Node struct { Tree *MerkleTree Parent *Node Left *Node Right *Node Hash []byte C Content // contains filtered or unexported fields }
Node represents a node, root, or leaf in the tree. It stores pointers to its immediate relationships, a hash, the content stored if it is a leaf, and other metadata.