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.

229 lines
6.9 KiB

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