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.

160 lines
4.9 KiB

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