You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.0 KiB

  1. package merkle
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. )
  8. // SimpleProof represents a simple Merkle proof.
  9. // NOTE: The convention for proofs is to include leaf hashes but to
  10. // exclude the root hash.
  11. // This convention is implemented across IAVL range proofs as well.
  12. // Keep this consistent unless there's a very good reason to change
  13. // everything. This also affects the generalized proof system as
  14. // well.
  15. type SimpleProof struct {
  16. Total int `json:"total"` // Total number of items.
  17. Index int `json:"index"` // Index of item to prove.
  18. LeafHash []byte `json:"leaf_hash"` // Hash of item value.
  19. Aunts [][]byte `json:"aunts"` // Hashes from leaf's sibling to a root's child.
  20. }
  21. // SimpleProofsFromHashers computes inclusion proof for given items.
  22. // proofs[0] is the proof for items[0].
  23. func SimpleProofsFromHashers(items []Hasher) (rootHash []byte, proofs []*SimpleProof) {
  24. trails, rootSPN := trailsFromHashers(items)
  25. rootHash = rootSPN.Hash
  26. proofs = make([]*SimpleProof, len(items))
  27. for i, trail := range trails {
  28. proofs[i] = &SimpleProof{
  29. Total: len(items),
  30. Index: i,
  31. LeafHash: trail.Hash,
  32. Aunts: trail.FlattenAunts(),
  33. }
  34. }
  35. return
  36. }
  37. // SimpleProofsFromMap generates proofs from a map. The keys/values of the map will be used as the keys/values
  38. // in the underlying key-value pairs.
  39. // The keys are sorted before the proofs are computed.
  40. func SimpleProofsFromMap(m map[string]Hasher) (rootHash []byte, proofs map[string]*SimpleProof, keys []string) {
  41. sm := newSimpleMap()
  42. for k, v := range m {
  43. sm.Set(k, v)
  44. }
  45. sm.Sort()
  46. kvs := sm.kvs
  47. kvsH := make([]Hasher, 0, len(kvs))
  48. for _, kvp := range kvs {
  49. kvsH = append(kvsH, KVPair(kvp))
  50. }
  51. rootHash, proofList := SimpleProofsFromHashers(kvsH)
  52. proofs = make(map[string]*SimpleProof)
  53. keys = make([]string, len(proofList))
  54. for i, kvp := range kvs {
  55. proofs[string(kvp.Key)] = proofList[i]
  56. keys[i] = string(kvp.Key)
  57. }
  58. return
  59. }
  60. // Verify that the SimpleProof proves the root hash.
  61. // Check sp.Index/sp.Total manually if needed
  62. func (sp *SimpleProof) Verify(rootHash []byte, leafHash []byte) error {
  63. if sp.Total < 0 {
  64. return errors.New("Proof total must be positive")
  65. }
  66. if sp.Index < 0 {
  67. return errors.New("Proof index cannot be negative")
  68. }
  69. if !bytes.Equal(sp.LeafHash, leafHash) {
  70. return cmn.NewError("invalid leaf hash: wanted %X got %X", leafHash, sp.LeafHash)
  71. }
  72. computedHash := sp.ComputeRootHash()
  73. if !bytes.Equal(computedHash, rootHash) {
  74. return cmn.NewError("invalid root hash: wanted %X got %X", rootHash, computedHash)
  75. }
  76. return nil
  77. }
  78. // Compute the root hash given a leaf hash. Does not verify the result.
  79. func (sp *SimpleProof) ComputeRootHash() []byte {
  80. return computeHashFromAunts(
  81. sp.Index,
  82. sp.Total,
  83. sp.LeafHash,
  84. sp.Aunts,
  85. )
  86. }
  87. // String implements the stringer interface for SimpleProof.
  88. // It is a wrapper around StringIndented.
  89. func (sp *SimpleProof) String() string {
  90. return sp.StringIndented("")
  91. }
  92. // StringIndented generates a canonical string representation of a SimpleProof.
  93. func (sp *SimpleProof) StringIndented(indent string) string {
  94. return fmt.Sprintf(`SimpleProof{
  95. %s Aunts: %X
  96. %s}`,
  97. indent, sp.Aunts,
  98. indent)
  99. }
  100. // Use the leafHash and innerHashes to get the root merkle hash.
  101. // If the length of the innerHashes slice isn't exactly correct, the result is nil.
  102. // Recursive impl.
  103. func computeHashFromAunts(index int, total int, leafHash []byte, innerHashes [][]byte) []byte {
  104. if index >= total || index < 0 || total <= 0 {
  105. return nil
  106. }
  107. switch total {
  108. case 0:
  109. panic("Cannot call computeHashFromAunts() with 0 total")
  110. case 1:
  111. if len(innerHashes) != 0 {
  112. return nil
  113. }
  114. return leafHash
  115. default:
  116. if len(innerHashes) == 0 {
  117. return nil
  118. }
  119. numLeft := (total + 1) / 2
  120. if index < numLeft {
  121. leftHash := computeHashFromAunts(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  122. if leftHash == nil {
  123. return nil
  124. }
  125. return SimpleHashFromTwoHashes(leftHash, innerHashes[len(innerHashes)-1])
  126. }
  127. rightHash := computeHashFromAunts(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  128. if rightHash == nil {
  129. return nil
  130. }
  131. return SimpleHashFromTwoHashes(innerHashes[len(innerHashes)-1], rightHash)
  132. }
  133. }
  134. // SimpleProofNode is a helper structure to construct merkle proof.
  135. // The node and the tree is thrown away afterwards.
  136. // Exactly one of node.Left and node.Right is nil, unless node is the root, in which case both are nil.
  137. // node.Parent.Hash = hash(node.Hash, node.Right.Hash) or
  138. // hash(node.Left.Hash, node.Hash), depending on whether node is a left/right child.
  139. type SimpleProofNode struct {
  140. Hash []byte
  141. Parent *SimpleProofNode
  142. Left *SimpleProofNode // Left sibling (only one of Left,Right is set)
  143. Right *SimpleProofNode // Right sibling (only one of Left,Right is set)
  144. }
  145. // FlattenAunts will return the inner hashes for the item corresponding to the leaf,
  146. // starting from a leaf SimpleProofNode.
  147. func (spn *SimpleProofNode) FlattenAunts() [][]byte {
  148. // Nonrecursive impl.
  149. innerHashes := [][]byte{}
  150. for spn != nil {
  151. if spn.Left != nil {
  152. innerHashes = append(innerHashes, spn.Left.Hash)
  153. } else if spn.Right != nil {
  154. innerHashes = append(innerHashes, spn.Right.Hash)
  155. } else {
  156. break
  157. }
  158. spn = spn.Parent
  159. }
  160. return innerHashes
  161. }
  162. // trails[0].Hash is the leaf hash for items[0].
  163. // trails[i].Parent.Parent....Parent == root for all i.
  164. func trailsFromHashers(items []Hasher) (trails []*SimpleProofNode, root *SimpleProofNode) {
  165. // Recursive impl.
  166. switch len(items) {
  167. case 0:
  168. return nil, nil
  169. case 1:
  170. trail := &SimpleProofNode{items[0].Hash(), nil, nil, nil}
  171. return []*SimpleProofNode{trail}, trail
  172. default:
  173. lefts, leftRoot := trailsFromHashers(items[:(len(items)+1)/2])
  174. rights, rightRoot := trailsFromHashers(items[(len(items)+1)/2:])
  175. rootHash := SimpleHashFromTwoHashes(leftRoot.Hash, rightRoot.Hash)
  176. root := &SimpleProofNode{rootHash, nil, nil, nil}
  177. leftRoot.Parent = root
  178. leftRoot.Right = rightRoot
  179. rightRoot.Parent = root
  180. rightRoot.Left = leftRoot
  181. return append(lefts, rights...), root
  182. }
  183. }