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.

207 lines
6.4 KiB

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