|
@ -7,18 +7,18 @@ the tree the same size, but the left may be one greater. |
|
|
Use this for short deterministic trees, such as the validator list. |
|
|
Use this for short deterministic trees, such as the validator list. |
|
|
For larger datasets, use IAVLTree. |
|
|
For larger datasets, use IAVLTree. |
|
|
|
|
|
|
|
|
* |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
* * |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
* * * h6 |
|
|
|
|
|
/ \ / \ / \ |
|
|
|
|
|
h0 h1 h2 h3 h4 h5 |
|
|
|
|
|
|
|
|
* |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
/ \ |
|
|
|
|
|
* * |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
/ \ / \ |
|
|
|
|
|
* * * h6 |
|
|
|
|
|
/ \ / \ / \ |
|
|
|
|
|
h0 h1 h2 h3 h4 h5 |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
@ -31,7 +31,7 @@ import ( |
|
|
"github.com/tendermint/tendermint/binary" |
|
|
"github.com/tendermint/tendermint/binary" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func HashFromTwoHashes(left []byte, right []byte) []byte { |
|
|
|
|
|
|
|
|
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte { |
|
|
var n int64 |
|
|
var n int64 |
|
|
var err error |
|
|
var err error |
|
|
var hasher = sha256.New() |
|
|
var hasher = sha256.New() |
|
@ -43,7 +43,7 @@ func HashFromTwoHashes(left []byte, right []byte) []byte { |
|
|
return hasher.Sum(nil) |
|
|
return hasher.Sum(nil) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func HashFromHashes(hashes [][]byte) []byte { |
|
|
|
|
|
|
|
|
func SimpleHashFromHashes(hashes [][]byte) []byte { |
|
|
// Recursive impl.
|
|
|
// Recursive impl.
|
|
|
switch len(hashes) { |
|
|
switch len(hashes) { |
|
|
case 0: |
|
|
case 0: |
|
@ -51,23 +51,23 @@ func HashFromHashes(hashes [][]byte) []byte { |
|
|
case 1: |
|
|
case 1: |
|
|
return hashes[0] |
|
|
return hashes[0] |
|
|
default: |
|
|
default: |
|
|
left := HashFromHashes(hashes[:(len(hashes)+1)/2]) |
|
|
|
|
|
right := HashFromHashes(hashes[(len(hashes)+1)/2:]) |
|
|
|
|
|
return HashFromTwoHashes(left, right) |
|
|
|
|
|
|
|
|
left := SimpleHashFromHashes(hashes[:(len(hashes)+1)/2]) |
|
|
|
|
|
right := SimpleHashFromHashes(hashes[(len(hashes)+1)/2:]) |
|
|
|
|
|
return SimpleHashFromTwoHashes(left, right) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Convenience for HashFromHashes.
|
|
|
|
|
|
func HashFromBinaries(items []interface{}) []byte { |
|
|
|
|
|
|
|
|
// Convenience for SimpleHashFromHashes.
|
|
|
|
|
|
func SimpleHashFromBinaries(items []interface{}) []byte { |
|
|
hashes := [][]byte{} |
|
|
hashes := [][]byte{} |
|
|
for _, item := range items { |
|
|
for _, item := range items { |
|
|
hashes = append(hashes, HashFromBinary(item)) |
|
|
|
|
|
|
|
|
hashes = append(hashes, SimpleHashFromBinary(item)) |
|
|
} |
|
|
} |
|
|
return HashFromHashes(hashes) |
|
|
|
|
|
|
|
|
return SimpleHashFromHashes(hashes) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// General Convenience
|
|
|
// General Convenience
|
|
|
func HashFromBinary(item interface{}) []byte { |
|
|
|
|
|
|
|
|
func SimpleHashFromBinary(item interface{}) []byte { |
|
|
hasher, n, err := sha256.New(), new(int64), new(error) |
|
|
hasher, n, err := sha256.New(), new(int64), new(error) |
|
|
binary.WriteBinary(item, hasher, n, err) |
|
|
binary.WriteBinary(item, hasher, n, err) |
|
|
if *err != nil { |
|
|
if *err != nil { |
|
@ -76,103 +76,146 @@ func HashFromBinary(item interface{}) []byte { |
|
|
return hasher.Sum(nil) |
|
|
return hasher.Sum(nil) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Convenience for HashFromHashes.
|
|
|
|
|
|
func HashFromHashables(items []Hashable) []byte { |
|
|
|
|
|
|
|
|
// Convenience for SimpleHashFromHashes.
|
|
|
|
|
|
func SimpleHashFromHashables(items []Hashable) []byte { |
|
|
hashes := [][]byte{} |
|
|
hashes := [][]byte{} |
|
|
for _, item := range items { |
|
|
for _, item := range items { |
|
|
hash := item.Hash() |
|
|
hash := item.Hash() |
|
|
hashes = append(hashes, hash) |
|
|
hashes = append(hashes, hash) |
|
|
} |
|
|
} |
|
|
return HashFromHashes(hashes) |
|
|
|
|
|
|
|
|
return SimpleHashFromHashes(hashes) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type HashTrail struct { |
|
|
|
|
|
Hash []byte |
|
|
|
|
|
Parent *HashTrail |
|
|
|
|
|
Left *HashTrail |
|
|
|
|
|
Right *HashTrail |
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
type SimpleProof struct { |
|
|
|
|
|
Index uint |
|
|
|
|
|
Total uint |
|
|
|
|
|
LeafHash []byte |
|
|
|
|
|
InnerHashes [][]byte // Hashes from leaf's sibling to a root's child.
|
|
|
|
|
|
RootHash []byte |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (ht *HashTrail) Flatten() [][]byte { |
|
|
|
|
|
// Nonrecursive impl.
|
|
|
|
|
|
trail := [][]byte{} |
|
|
|
|
|
for ht != nil { |
|
|
|
|
|
if ht.Left != nil { |
|
|
|
|
|
trail = append(trail, ht.Left.Hash) |
|
|
|
|
|
} else if ht.Right != nil { |
|
|
|
|
|
trail = append(trail, ht.Right.Hash) |
|
|
|
|
|
} else { |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
// proofs[0] is the proof for items[0].
|
|
|
|
|
|
func SimpleProofsFromHashables(items []Hashable) (proofs []*SimpleProof) { |
|
|
|
|
|
trails, root := trailsFromHashables(items) |
|
|
|
|
|
proofs = make([]*SimpleProof, len(items)) |
|
|
|
|
|
for i, trail := range trails { |
|
|
|
|
|
proofs[i] = &SimpleProof{ |
|
|
|
|
|
Index: uint(i), |
|
|
|
|
|
Total: uint(len(items)), |
|
|
|
|
|
LeafHash: trail.Hash, |
|
|
|
|
|
InnerHashes: trail.FlattenInnerHashes(), |
|
|
|
|
|
RootHash: root.Hash, |
|
|
} |
|
|
} |
|
|
ht = ht.Parent |
|
|
|
|
|
} |
|
|
} |
|
|
return trail |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// returned trails[0].Hash is the leaf hash.
|
|
|
|
|
|
// trails[0].Parent.Hash is the hash above that, etc.
|
|
|
|
|
|
func HashTrailsFromHashables(items []Hashable) (trails []*HashTrail, root *HashTrail) { |
|
|
|
|
|
// Recursive impl.
|
|
|
|
|
|
switch len(items) { |
|
|
|
|
|
case 0: |
|
|
|
|
|
return nil, nil |
|
|
|
|
|
case 1: |
|
|
|
|
|
trail := &HashTrail{items[0].Hash(), nil, nil, nil} |
|
|
|
|
|
return []*HashTrail{trail}, trail |
|
|
|
|
|
default: |
|
|
|
|
|
lefts, leftRoot := HashTrailsFromHashables(items[:(len(items)+1)/2]) |
|
|
|
|
|
rights, rightRoot := HashTrailsFromHashables(items[(len(items)+1)/2:]) |
|
|
|
|
|
rootHash := HashFromTwoHashes(leftRoot.Hash, rightRoot.Hash) |
|
|
|
|
|
root := &HashTrail{rootHash, nil, nil, nil} |
|
|
|
|
|
leftRoot.Parent = root |
|
|
|
|
|
leftRoot.Right = rightRoot |
|
|
|
|
|
rightRoot.Parent = root |
|
|
|
|
|
rightRoot.Left = leftRoot |
|
|
|
|
|
return append(lefts, rights...), root |
|
|
|
|
|
|
|
|
// Verify that leafHash is a leaf hash of the simple-merkle-tree
|
|
|
|
|
|
// which hashes to rootHash.
|
|
|
|
|
|
func (sp *SimpleProof) Verify(leafHash []byte, rootHash []byte) bool { |
|
|
|
|
|
if !bytes.Equal(leafHash, sp.LeafHash) { |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Ensures that leafHash is part of rootHash.
|
|
|
|
|
|
func VerifyHashTrail(index uint, total uint, leafHash []byte, trail [][]byte, rootHash []byte) bool { |
|
|
|
|
|
computedRoot := ComputeRootFromTrail(index, total, leafHash, trail) |
|
|
|
|
|
if computedRoot == nil { |
|
|
|
|
|
|
|
|
if !bytes.Equal(rootHash, sp.RootHash) { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
computedHash := computeHashFromInnerHashes(sp.Index, sp.Total, sp.LeafHash, sp.InnerHashes) |
|
|
|
|
|
if computedHash == nil { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
return bytes.Equal(computedRoot, rootHash) |
|
|
|
|
|
|
|
|
if !bytes.Equal(computedHash, rootHash) { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
return true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Use the leafHash and trail to get the root merkle hash.
|
|
|
|
|
|
// If the length of the trail slice isn't exactly correct, the result is nil.
|
|
|
|
|
|
func ComputeRootFromTrail(index uint, total uint, leafHash []byte, trail [][]byte) []byte { |
|
|
|
|
|
|
|
|
// Use the leafHash and innerHashes to get the root merkle hash.
|
|
|
|
|
|
// If the length of the innerHashes slice isn't exactly correct, the result is nil.
|
|
|
|
|
|
func computeHashFromInnerHashes(index uint, total uint, leafHash []byte, innerHashes [][]byte) []byte { |
|
|
// Recursive impl.
|
|
|
// Recursive impl.
|
|
|
if index >= total { |
|
|
if index >= total { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
switch total { |
|
|
switch total { |
|
|
case 0: |
|
|
case 0: |
|
|
panic("Cannot call ComputeRootFromTrail() with 0 total") |
|
|
|
|
|
|
|
|
panic("Cannot call computeHashFromInnerHashes() with 0 total") |
|
|
case 1: |
|
|
case 1: |
|
|
if len(trail) != 0 { |
|
|
|
|
|
|
|
|
if len(innerHashes) != 0 { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
return leafHash |
|
|
return leafHash |
|
|
default: |
|
|
default: |
|
|
if len(trail) == 0 { |
|
|
|
|
|
|
|
|
if len(innerHashes) == 0 { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
numLeft := (total + 1) / 2 |
|
|
numLeft := (total + 1) / 2 |
|
|
if index < numLeft { |
|
|
if index < numLeft { |
|
|
leftRoot := ComputeRootFromTrail(index, numLeft, leafHash, trail[:len(trail)-1]) |
|
|
|
|
|
if leftRoot == nil { |
|
|
|
|
|
|
|
|
leftHash := computeHashFromInnerHashes(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1]) |
|
|
|
|
|
if leftHash == nil { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
return HashFromTwoHashes(leftRoot, trail[len(trail)-1]) |
|
|
|
|
|
|
|
|
return SimpleHashFromTwoHashes(leftHash, innerHashes[len(innerHashes)-1]) |
|
|
} else { |
|
|
} else { |
|
|
rightRoot := ComputeRootFromTrail(index-numLeft, total-numLeft, leafHash, trail[:len(trail)-1]) |
|
|
|
|
|
if rightRoot == nil { |
|
|
|
|
|
|
|
|
rightHash := computeHashFromInnerHashes(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1]) |
|
|
|
|
|
if rightHash == nil { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
return HashFromTwoHashes(trail[len(trail)-1], rightRoot) |
|
|
|
|
|
|
|
|
return SimpleHashFromTwoHashes(innerHashes[len(innerHashes)-1], rightHash) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Helper structure to construct merkle proof.
|
|
|
|
|
|
// The node and the tree is thrown away afterwards.
|
|
|
|
|
|
// Exactly one of node.Left and node.Right is nil, unless node is the root, in which case both are nil.
|
|
|
|
|
|
// node.Parent.Hash = hash(node.Hash, node.Right.Hash) or
|
|
|
|
|
|
// hash(node.Left.Hash, node.Hash), depending on whether node is a left/right child.
|
|
|
|
|
|
type SimpleProofNode struct { |
|
|
|
|
|
Hash []byte |
|
|
|
|
|
Parent *SimpleProofNode |
|
|
|
|
|
Left *SimpleProofNode // Left sibling (only one of Left,Right is set)
|
|
|
|
|
|
Right *SimpleProofNode // Right sibling (only one of Left,Right is set)
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Starting from a leaf SimpleProofNode, FlattenInnerHashes() will return
|
|
|
|
|
|
// the inner hashes for the item corresponding to the leaf.
|
|
|
|
|
|
func (spn *SimpleProofNode) FlattenInnerHashes() [][]byte { |
|
|
|
|
|
// Nonrecursive impl.
|
|
|
|
|
|
innerHashes := [][]byte{} |
|
|
|
|
|
for spn != nil { |
|
|
|
|
|
if spn.Left != nil { |
|
|
|
|
|
innerHashes = append(innerHashes, spn.Left.Hash) |
|
|
|
|
|
} else if spn.Right != nil { |
|
|
|
|
|
innerHashes = append(innerHashes, spn.Right.Hash) |
|
|
|
|
|
} else { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
spn = spn.Parent |
|
|
|
|
|
} |
|
|
|
|
|
return innerHashes |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// trails[0].Hash is the leaf hash for items[0].
|
|
|
|
|
|
// trails[i].Parent.Parent....Parent == root for all i.
|
|
|
|
|
|
func trailsFromHashables(items []Hashable) (trails []*SimpleProofNode, root *SimpleProofNode) { |
|
|
|
|
|
// Recursive impl.
|
|
|
|
|
|
switch len(items) { |
|
|
|
|
|
case 0: |
|
|
|
|
|
return nil, nil |
|
|
|
|
|
case 1: |
|
|
|
|
|
trail := &SimpleProofNode{items[0].Hash(), nil, nil, nil} |
|
|
|
|
|
return []*SimpleProofNode{trail}, trail |
|
|
|
|
|
default: |
|
|
|
|
|
lefts, leftRoot := trailsFromHashables(items[:(len(items)+1)/2]) |
|
|
|
|
|
rights, rightRoot := trailsFromHashables(items[(len(items)+1)/2:]) |
|
|
|
|
|
rootHash := SimpleHashFromTwoHashes(leftRoot.Hash, rightRoot.Hash) |
|
|
|
|
|
root := &SimpleProofNode{rootHash, nil, nil, nil} |
|
|
|
|
|
leftRoot.Parent = root |
|
|
|
|
|
leftRoot.Right = rightRoot |
|
|
|
|
|
rightRoot.Parent = root |
|
|
|
|
|
rightRoot.Left = leftRoot |
|
|
|
|
|
return append(lefts, rights...), root |
|
|
|
|
|
} |
|
|
|
|
|
} |