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.

144 lines
4.2 KiB

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