github.com/tendermint/tmlibs@v0.9.0/merkle/simple_proof.go (about)

     1  package merkle
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  type SimpleProof struct {
     9  	Aunts [][]byte `json:"aunts"` // Hashes from leaf's sibling to a root's child.
    10  }
    11  
    12  // proofs[0] is the proof for items[0].
    13  func SimpleProofsFromHashers(items []Hasher) (rootHash []byte, proofs []*SimpleProof) {
    14  	trails, rootSPN := trailsFromHashers(items)
    15  	rootHash = rootSPN.Hash
    16  	proofs = make([]*SimpleProof, len(items))
    17  	for i, trail := range trails {
    18  		proofs[i] = &SimpleProof{
    19  			Aunts: trail.FlattenAunts(),
    20  		}
    21  	}
    22  	return
    23  }
    24  
    25  func SimpleProofsFromMap(m map[string]Hasher) (rootHash []byte, proofs []*SimpleProof) {
    26  	sm := NewSimpleMap()
    27  	for k, v := range m {
    28  		sm.Set(k, v)
    29  	}
    30  	sm.Sort()
    31  	kvs := sm.kvs
    32  	kvsH := make([]Hasher, 0, len(kvs))
    33  	for _, kvp := range kvs {
    34  		kvsH = append(kvsH, KVPair(kvp))
    35  	}
    36  	return SimpleProofsFromHashers(kvsH)
    37  }
    38  
    39  // Verify that leafHash is a leaf hash of the simple-merkle-tree
    40  // which hashes to rootHash.
    41  func (sp *SimpleProof) Verify(index int, total int, leafHash []byte, rootHash []byte) bool {
    42  	computedHash := computeHashFromAunts(index, total, leafHash, sp.Aunts)
    43  	return computedHash != nil && bytes.Equal(computedHash, rootHash)
    44  }
    45  
    46  func (sp *SimpleProof) String() string {
    47  	return sp.StringIndented("")
    48  }
    49  
    50  func (sp *SimpleProof) StringIndented(indent string) string {
    51  	return fmt.Sprintf(`SimpleProof{
    52  %s  Aunts: %X
    53  %s}`,
    54  		indent, sp.Aunts,
    55  		indent)
    56  }
    57  
    58  // Use the leafHash and innerHashes to get the root merkle hash.
    59  // If the length of the innerHashes slice isn't exactly correct, the result is nil.
    60  // Recursive impl.
    61  func computeHashFromAunts(index int, total int, leafHash []byte, innerHashes [][]byte) []byte {
    62  	if index >= total || index < 0 || total <= 0 {
    63  		return nil
    64  	}
    65  	switch total {
    66  	case 0:
    67  		panic("Cannot call computeHashFromAunts() with 0 total")
    68  	case 1:
    69  		if len(innerHashes) != 0 {
    70  			return nil
    71  		}
    72  		return leafHash
    73  	default:
    74  		if len(innerHashes) == 0 {
    75  			return nil
    76  		}
    77  		numLeft := (total + 1) / 2
    78  		if index < numLeft {
    79  			leftHash := computeHashFromAunts(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1])
    80  			if leftHash == nil {
    81  				return nil
    82  			}
    83  			return SimpleHashFromTwoHashes(leftHash, innerHashes[len(innerHashes)-1])
    84  		}
    85  		rightHash := computeHashFromAunts(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1])
    86  		if rightHash == nil {
    87  			return nil
    88  		}
    89  		return SimpleHashFromTwoHashes(innerHashes[len(innerHashes)-1], rightHash)
    90  	}
    91  }
    92  
    93  // Helper structure to construct merkle proof.
    94  // The node and the tree is thrown away afterwards.
    95  // Exactly one of node.Left and node.Right is nil, unless node is the root, in which case both are nil.
    96  // node.Parent.Hash = hash(node.Hash, node.Right.Hash) or
    97  // hash(node.Left.Hash, node.Hash), depending on whether node is a left/right child.
    98  type SimpleProofNode struct {
    99  	Hash   []byte
   100  	Parent *SimpleProofNode
   101  	Left   *SimpleProofNode // Left sibling  (only one of Left,Right is set)
   102  	Right  *SimpleProofNode // Right sibling (only one of Left,Right is set)
   103  }
   104  
   105  // Starting from a leaf SimpleProofNode, FlattenAunts() will return
   106  // the inner hashes for the item corresponding to the leaf.
   107  func (spn *SimpleProofNode) FlattenAunts() [][]byte {
   108  	// Nonrecursive impl.
   109  	innerHashes := [][]byte{}
   110  	for spn != nil {
   111  		if spn.Left != nil {
   112  			innerHashes = append(innerHashes, spn.Left.Hash)
   113  		} else if spn.Right != nil {
   114  			innerHashes = append(innerHashes, spn.Right.Hash)
   115  		} else {
   116  			break
   117  		}
   118  		spn = spn.Parent
   119  	}
   120  	return innerHashes
   121  }
   122  
   123  // trails[0].Hash is the leaf hash for items[0].
   124  // trails[i].Parent.Parent....Parent == root for all i.
   125  func trailsFromHashers(items []Hasher) (trails []*SimpleProofNode, root *SimpleProofNode) {
   126  	// Recursive impl.
   127  	switch len(items) {
   128  	case 0:
   129  		return nil, nil
   130  	case 1:
   131  		trail := &SimpleProofNode{items[0].Hash(), nil, nil, nil}
   132  		return []*SimpleProofNode{trail}, trail
   133  	default:
   134  		lefts, leftRoot := trailsFromHashers(items[:(len(items)+1)/2])
   135  		rights, rightRoot := trailsFromHashers(items[(len(items)+1)/2:])
   136  		rootHash := SimpleHashFromTwoHashes(leftRoot.Hash, rightRoot.Hash)
   137  		root := &SimpleProofNode{rootHash, nil, nil, nil}
   138  		leftRoot.Parent = root
   139  		leftRoot.Right = rightRoot
   140  		rightRoot.Parent = root
   141  		rightRoot.Left = leftRoot
   142  		return append(lefts, rights...), root
   143  	}
   144  }