Documentation ¶
Overview ¶
Package merkletree provides tools for calculating the Merkle root of a dataset, for creating a proof that a piece of data is in a Merkle tree of a given root, and for verifying proofs that a piece of data is in a Merkle tree of a given root. The tree is implemented according to the specification for Merkle trees provided in RFC 6962.
Package merkletree also supports building roots and proofs from cached subroots of the Merkle tree. For example, a large file could be cached by building the Merkle root for each 4MB sector and remembering the Merkle roots of each sector. Using a cached tree, the Merkle root of the whole file can be computed by passing the cached tree each of the roots of the 4MB sector. Building proofs using these cached roots is also supported. A proof must be built within the target sector using a normal Tree, requiring the whole sector to be hashed. The results of that proof can then be passed into the Prove() function of a cached tree, which will create the full proof without needing to hash the entire file. Caching also makes it inexpensive to update the Merkle root of the file after changing or deleting segments of the larger file.
Examples can be found in the README for the package.
Index ¶
- Constants
- func BuildDiffProof(ranges []LeafRange, h SubtreeHasher, numLeaves uint64) (proof [][]byte, err error)
- func BuildMultiRangeProof(ranges []LeafRange, h SubtreeHasher) (proof [][]byte, err error)
- func BuildRangeProof(proofStart, proofEnd int, h SubtreeHasher) (proof [][]byte, err error)
- func BuildReaderProof(r io.Reader, h hash.Hash, segmentSize int, index uint64) (root []byte, proofSet [][]byte, numLeaves uint64, err error)
- func CompressLeafHashes(ranges []LeafRange, h SubtreeHasher) (compressed [][]byte, err error)
- func ConvertRangeProofToSingleProof(proof [][]byte, proofIndex int) [][]byte
- func ConvertSingleProofToRangeProof(proof [][]byte, proofIndex int) [][]byte
- func ReaderRoot(r io.Reader, h hash.Hash, segmentSize int) (root []byte, err error)
- func VerifyDiffProof(rangeHashes [][]byte, numLeaves uint64, h hash.Hash, ranges []LeafRange, ...) (bool, error)
- func VerifyMultiRangeProof(lh LeafHasher, h hash.Hash, ranges []LeafRange, proof [][]byte, root []byte) (bool, error)
- func VerifyProof(h hash.Hash, merkleRoot []byte, proofSet [][]byte, proofIndex uint64, ...) bool
- func VerifyRangeProof(lh LeafHasher, h hash.Hash, proofStart, proofEnd int, proof [][]byte, ...) (bool, error)
- type CachedLeafHasher
- type CachedSubtreeHasher
- type CachedTree
- type DefaultTreeHasher
- type LeafHasher
- type LeafHasherz
- type LeafRange
- type MixedSubtreeHasher
- type NodeHasher
- type ReaderLeafHasher
- type ReaderSubtreeHasher
- type SubtreeHasher
- type Tree
- func (t *Tree) Prove() (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64)
- func (t *Tree) Push(data []byte)
- func (t *Tree) PushSubTree(height int, sum []byte) error
- func (t *Tree) ReadAll(r io.Reader, segmentSize int) error
- func (t *Tree) Root() []byte
- func (t *Tree) SetIndex(i uint64) error
- type TreeHasher
Constants ¶
const ( // DEBUG indicates whether debugging is enabled. When debugging is enabled, // checks are performed on all stateful objects to make sure no supposedly // impossible conditions have occurred. The DEBUG flag is for developers. DEBUG = false )
Variables ¶
This section is empty.
Functions ¶
func BuildDiffProof ¶
func BuildDiffProof(ranges []LeafRange, h SubtreeHasher, numLeaves uint64) (proof [][]byte, err error)
BuildDiffProof constructs a Merkle diff for the specified leaf ranges, using the provided SubtreeHasher. The ranges must be sorted and non-overlapping.
func BuildMultiRangeProof ¶
func BuildMultiRangeProof(ranges []LeafRange, h SubtreeHasher) (proof [][]byte, err error)
BuildMultiRangeProof constructs a proof for the specified leaf ranges, using the provided SubtreeHasher. The ranges must be sorted and non-overlapping.
func BuildRangeProof ¶
func BuildRangeProof(proofStart, proofEnd int, h SubtreeHasher) (proof [][]byte, err error)
BuildRangeProof constructs a proof for the leaf range [proofStart, proofEnd) using the provided SubtreeHasher.
func BuildReaderProof ¶
func BuildReaderProof(r io.Reader, h hash.Hash, segmentSize int, index uint64) (root []byte, proofSet [][]byte, numLeaves uint64, err error)
BuildReaderProof returns a proof that certain data is in the merkle tree created by the data in the reader. The merkle root, set of proofs, and the number of leaves in the Merkle tree are all returned. All leaves will we 'segmentSize' bytes except the last leaf, which will not be padded out if there are not enough bytes remaining in the reader.
func CompressLeafHashes ¶
func CompressLeafHashes(ranges []LeafRange, h SubtreeHasher) (compressed [][]byte, err error)
CompressLeafHashes takes the ranges of modified leaves as an input together with a SubtreeHasher which can produce all modified leaf hashes to compress the leaf hashes into subtrees where possible. These compressed leaf hashes can be used as the 'rangeHashes' input to VerifyDiffProof.
func ConvertRangeProofToSingleProof ¶
ConvertRangeProofToSingleProof converts a single-leaf range proof to the equivalent proof produced by (*Tree).Prove. proofIndex must be >= 0.
func ConvertSingleProofToRangeProof ¶
ConvertSingleProofToRangeProof converts a proof produced by (*Tree).Prove to a single-leaf range proof. proofIndex must be >= 0.
func ReaderRoot ¶
ReaderRoot returns the Merkle root of the data read from the reader, where each leaf is 'segmentSize' long and 'h' is used as the hashing function. All leaves will be 'segmentSize' bytes except the last leaf, which will not be padded out if there are not enough bytes remaining in the reader.
func VerifyDiffProof ¶
func VerifyDiffProof(rangeHashes [][]byte, numLeaves uint64, h hash.Hash, ranges []LeafRange, proof [][]byte, root []byte) (bool, error)
VerifyDiffProof verifies a proof produced by BuildDiffProof using subtree hashes produced by sh, which must contain the concatenation of the subtree hashes within the proof ranges.
func VerifyMultiRangeProof ¶
func VerifyMultiRangeProof(lh LeafHasher, h hash.Hash, ranges []LeafRange, proof [][]byte, root []byte) (bool, error)
VerifyMultiRangeProof verifies a proof produced by BuildMultiRangeProof using leaf hashes produced by lh, which must contain the concatenation of the leaf hashes within the proof ranges.
func VerifyProof ¶
func VerifyProof(h hash.Hash, merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) bool
VerifyProof takes a Merkle root, a proofSet, and a proofIndex and returns true if the first element of the proof set is a leaf of data in the Merkle root. False is returned if the proof set or Merkle root is nil, and if 'numLeaves' equals 0.
func VerifyRangeProof ¶
func VerifyRangeProof(lh LeafHasher, h hash.Hash, proofStart, proofEnd int, proof [][]byte, root []byte) (bool, error)
VerifyRangeProof verifies a proof produced by BuildRangeProof using leaf hashes produced by lh, which must contain only the leaf hashes within the proof range.
Types ¶
type CachedLeafHasher ¶
type CachedLeafHasher struct {
// contains filtered or unexported fields
}
CachedLeafHasher implements the LeafHasher interface by returning precomputed leaf hashes.
func NewCachedLeafHasher ¶
func NewCachedLeafHasher(leafHashes [][]byte) *CachedLeafHasher
NewCachedLeafHasher creates a CachedLeafHasher from a set of precomputed leaf hashes.
func (*CachedLeafHasher) NextLeafHash ¶
func (clh *CachedLeafHasher) NextLeafHash() ([]byte, error)
NextLeafHash implements LeafHasher.
type CachedSubtreeHasher ¶
type CachedSubtreeHasher struct {
// contains filtered or unexported fields
}
CachedSubtreeHasher implements SubtreeHasher using a set of precomputed leaf hashes.
func NewCachedSubtreeHasher ¶
func NewCachedSubtreeHasher(leafHashes [][]byte, h hash.Hash) *CachedSubtreeHasher
NewCachedSubtreeHasher creates a CachedSubtreeHasher using the specified leaf hashes and hash function.
func (*CachedSubtreeHasher) NextSubtreeRoot ¶
func (csh *CachedSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error)
NextSubtreeRoot implements SubtreeHasher.
func (*CachedSubtreeHasher) Skip ¶
func (csh *CachedSubtreeHasher) Skip(n int) error
Skip implements SubtreeHasher.
type CachedTree ¶
type CachedTree struct { Tree // contains filtered or unexported fields }
A CachedTree can be used to build Merkle roots and proofs from the cached Merkle roots of smaller blocks of data. Each CachedTree has a height, meaning every element added to the CachedTree is the root of a full Merkle tree containing 2^height leaves.
func NewCachedTree ¶
func NewCachedTree(h hash.Hash, cachedNodeHeight uint64) *CachedTree
NewCachedTree initializes a CachedTree with a hash object, which will be used when hashing the input.
func (*CachedTree) Prove ¶
func (ct *CachedTree) Prove(cachedProofSet [][]byte) (merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64)
Prove will create a proof that the leaf at the indicated index is a part of the data represented by the Merkle root of the Cached Tree. The CachedTree needs the proof set proving that the index is an element of the cached element in order to create a correct proof. After proof is called, the CachedTree is unchanged, and can receive more elements.
func (*CachedTree) SetIndex ¶
func (ct *CachedTree) SetIndex(i uint64) error
SetIndex will inform the CachedTree of the index of the leaf for which a storage proof is being created. The index should be the index of the actual leaf, and not the index of the cached element containing the leaf. SetIndex must be called on empty CachedTree.
type DefaultTreeHasher ¶
type DefaultTreeHasher struct {
// contains filtered or unexported fields
}
func NewDefaultHasher ¶
func NewDefaultHasher(h hash.Hash) *DefaultTreeHasher
func (*DefaultTreeHasher) HashLeaf ¶
func (d *DefaultTreeHasher) HashLeaf(leaf []byte) []byte
func (*DefaultTreeHasher) HashNode ¶
func (d *DefaultTreeHasher) HashNode(l, r []byte) []byte
type LeafHasher ¶
A LeafHasher returns the leaves of a Merkle tree in sequential order. When no more leaves are available, NextLeafHash must return io.EOF.
type LeafHasherz ¶
type MixedSubtreeHasher ¶
type MixedSubtreeHasher struct {
// contains filtered or unexported fields
}
MixedSubtreeHasher implements SubtreeHasher by using cached subtree hashes when possible and otherwise reading leaf hashes from the underlying stream.
func NewMixedSubtreeHasher ¶
func NewMixedSubtreeHasher(nodeHashes [][]byte, leafReader io.Reader, leavesPerNode int, leafSize int, h hash.Hash) *MixedSubtreeHasher
NewMixedSubtreeHasher returns a new MixedSubtreeHasher that hashes nodeHashes which are already computed hashes of leavesPerNode leaves and also reads individual leaves from leafReader. The behavior of this implementation is greedy in regards to using the cached nodeHashes. A nodeHash will be consumed as soon as NextSubtreeRoot or Skip are called with a size greater than or equal to leavesPerNode.
func (*MixedSubtreeHasher) NextSubtreeRoot ¶
func (msh *MixedSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error)
NextSubtreeRoot implements SubtreeHasher.
func (*MixedSubtreeHasher) Skip ¶
func (msh *MixedSubtreeHasher) Skip(n int) error
Skip implements SubtreeHasher.
type NodeHasher ¶
type ReaderLeafHasher ¶
type ReaderLeafHasher struct {
// contains filtered or unexported fields
}
ReaderLeafHasher implements the LeafHasher interface by reading leaf data from the underlying stream.
func NewReaderLeafHasher ¶
NewReaderLeafHasher creates a ReaderLeafHasher with the specified stream, hash, and leaf size.
func (*ReaderLeafHasher) NextLeafHash ¶
func (rlh *ReaderLeafHasher) NextLeafHash() ([]byte, error)
NextLeafHash implements LeafHasher.
type ReaderSubtreeHasher ¶
type ReaderSubtreeHasher struct {
// contains filtered or unexported fields
}
ReaderSubtreeHasher implements SubtreeHasher by reading leaf data from an underlying stream.
func NewReaderSubtreeHasher ¶
NewReaderSubtreeHasher returns a new ReaderSubtreeHasher that reads leaf data from r.
func (*ReaderSubtreeHasher) NextSubtreeRoot ¶
func (rsh *ReaderSubtreeHasher) NextSubtreeRoot(subtreeSize int) ([]byte, error)
NextSubtreeRoot implements SubtreeHasher.
func (*ReaderSubtreeHasher) Skip ¶
func (rsh *ReaderSubtreeHasher) Skip(n int) (err error)
Skip implements SubtreeHasher.
type SubtreeHasher ¶
type SubtreeHasher interface { // NextSubtreeRoot returns the root of the next n leaves. If fewer than n // leaves are left in the tree, NextSubtreeRoot returns the root of those // leaves and nil. If no leaves are left, NextSubtreeRoot returns io.EOF. NextSubtreeRoot(n int) ([]byte, error) // Skip skips the next n leaves. If fewer than n leaves are left in the // tree, Skip returns io.ErrUnexpectedEOF. If exactly n leaves are left, // Skip returns nil (not io.EOF). Skip(n int) error }
A SubtreeHasher calculates subtree roots in sequential order, for use with BuildRangeProof.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
A Tree takes data as leaves and returns the Merkle root. Each call to 'Push' adds one leaf to the Merkle tree. Calling 'Root' returns the Merkle root. The Tree also constructs proof that a single leaf is a part of the tree. The leaf can be chosen with 'SetIndex'. The memory footprint of Tree grows in O(log(n)) in the number of leaves.
func New ¶
New creates a new Tree. The provided hash will be used for all hashing operations within the Tree.
func NewFromTreehasher ¶
func NewFromTreehasher(th TreeHasher) *Tree
NewFromTreehasher creates a new Tree. The provided TreeHasher will be used for all hashing operations within the Tree.
func (*Tree) Prove ¶
Prove creates a proof that the leaf at the established index (established by SetIndex) is an element of the Merkle tree. Prove will return a nil proof set if used incorrectly. Prove does not modify the Tree. Prove can only be called if SetIndex has been called previously.
func (*Tree) Push ¶
Push will add data to the set, building out the Merkle tree and Root. The tree does not remember all elements that are added, instead only keeping the log(n) elements that are necessary to build the Merkle root and keeping the log(n) elements necessary to build a proof that a piece of data is in the Merkle tree.
func (*Tree) PushSubTree ¶
PushSubTree pushes a cached subtree into the merkle tree. The subtree has to be smaller than the smallest subtree in the merkle tree, it has to be balanced and it can't contain the element that needs to be proven. Since we can't tell if a subTree is balanced, we can't sanity check for unbalanced trees. Therefore an unbalanced tree will cause silent errors, pain and misery for the person who wants to debug the resulting error.
func (*Tree) ReadAll ¶
ReadAll will read segments of size 'segmentSize' and push them into the tree until EOF is reached. Success will return 'err == nil', not 'err == EOF'. No padding is added to the data, so the last element may be smaller than 'segmentSize'.
type TreeHasher ¶
type TreeHasher interface { LeafHasherz NodeHasher }
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package merkletree provides tools for calculating the Merkle root of a dataset, for creating a proof that a piece of data is in a Merkle tree of a given root, and for verifying proofs that a piece of data is in a Merkle tree of a given root.
|
Package merkletree provides tools for calculating the Merkle root of a dataset, for creating a proof that a piece of data is in a Merkle tree of a given root, and for verifying proofs that a piece of data is in a Merkle tree of a given root. |