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.

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