| 1 | package merkletree |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "hash" |
| 6 | ) |
| 7 | |
| 8 | // VerifyProof takes a Merkle root, a proofSet, and a proofIndex and returns |
| 9 | // true if the first element of the proof set is a leaf of data in the Merkle |
| 10 | // root. False is returned if the proof set or Merkle root is nil, and if |
| 11 | // 'numLeaves' equals 0. |
| 12 | func VerifyProof(h hash.Hash, merkleRoot []byte, proofSet [][]byte, proofIndex uint64, numLeaves uint64) bool { |
| 13 | // Return false for nonsense input. A switch statement is used so that the |
| 14 | // cover tool will reveal if a case is not covered by the test suite. This |
| 15 | // would not be possible using a single if statement due to the limitations |
| 16 | // of the cover tool. |
| 17 | if merkleRoot == nil { |
| 18 | return false |
| 19 | } |
| 20 | if proofIndex >= numLeaves { |
| 21 | return false |
| 22 | } |
| 23 | |
| 24 | // In a Merkle tree, every node except the root node has a sibling. |
| 25 | // Combining the two siblings in the correct order will create the parent |
| 26 | // node. Each of the remaining hashes in the proof set is a sibling to a |
| 27 | // node that can be built from all of the previous elements of the proof |
| 28 | // set. The next node is built by taking: |
| 29 | // |
| 30 | // H(0x01 || sibling A || sibling B) |
| 31 | // |
| 32 | // The difficulty of the algorithm lies in determining whether the supplied |
| 33 | // hash is sibling A or sibling B. This information can be determined by |
| 34 | // using the proof index and the total number of leaves in the tree. |
| 35 | // |
| 36 | // A pair of two siblings forms a subtree. The subtree is complete if it |
| 37 | // has 1 << height total leaves. When the subtree is complete, the position |
| 38 | // of the proof index within the subtree can be determined by looking at |
| 39 | // the bounds of the subtree and determining if the proof index is in the |
| 40 | // first or second half of the subtree. |
| 41 | // |
| 42 | // When the subtree is not complete, either 1 or 0 of the remaining hashes |
| 43 | // will be sibling B. All remaining hashes after that will be sibling A. |
| 44 | // This is true because of the way that orphans are merged into the Merkle |
| 45 | // tree - an orphan at height n is elevated to height n + 1, and only |
| 46 | // hashed when it is no longer an orphan. Each subtree will therefore merge |
| 47 | // with at most 1 orphan to the right before becoming an orphan itself. |
| 48 | // Orphan nodes are always merged with larger subtrees to the left. |
| 49 | // |
| 50 | // One vulnerability with the proof verification is that the proofSet may |
| 51 | // not be long enough. Before looking at an element of proofSet, a check |
| 52 | // needs to be made that the element exists. |
| 53 | |
| 54 | // The first element of the set is the original data. A sibling at height 1 |
| 55 | // is created by getting the leafSum of the original data. |
| 56 | height := 0 |
| 57 | if len(proofSet) <= height { |
| 58 | return false |
| 59 | } |
| 60 | sum := leafSum(h, proofSet[height]) |
| 61 | height++ |
| 62 | |
| 63 | // While the current subtree (of height 'height') is complete, determine |
| 64 | // the position of the next sibling using the complete subtree algorithm. |
| 65 | // 'stableEnd' tells us the ending index of the last full subtree. It gets |
| 66 | // initialized to 'proofIndex' because the first full subtree was the |
| 67 | // subtree of height 1, created above (and had an ending index of |
| 68 | // 'proofIndex'). |
| 69 | stableEnd := proofIndex |
| 70 | for { |
| 71 | // Determine if the subtree is complete. This is accomplished by |
| 72 | // rounding down the proofIndex to the nearest 1 << 'height', adding 1 |
| 73 | // << 'height', and comparing the result to the number of leaves in the |
| 74 | // Merkle tree. |
| 75 | subTreeStartIndex := (proofIndex / (1 << uint(height))) * (1 << uint(height)) // round down to the nearest 1 << height |
| 76 | subTreeEndIndex := subTreeStartIndex + (1 << (uint(height))) - 1 // subtract 1 because the start index is inclusive |
| 77 | if subTreeEndIndex >= numLeaves { |
| 78 | // If the Merkle tree does not have a leaf at index |
| 79 | // 'subTreeEndIndex', then the subtree of the current height is not |
| 80 | // a complete subtree. |
| 81 | break |
| 82 | } |
| 83 | stableEnd = subTreeEndIndex |
| 84 | |
| 85 | // Determine if the proofIndex is in the first or the second half of |
| 86 | // the subtree. |
| 87 | if len(proofSet) <= height { |
| 88 | return false |
| 89 | } |
| 90 | if proofIndex-subTreeStartIndex < 1<<uint(height-1) { |
| 91 | sum = nodeSum(h, sum, proofSet[height]) |
| 92 | } else { |
| 93 | sum = nodeSum(h, proofSet[height], sum) |
| 94 | } |
| 95 | height++ |
| 96 | } |
| 97 | |
| 98 | // Determine if the next hash belongs to an orphan that was elevated. This |
| 99 | // is the case IFF 'stableEnd' (the last index of the largest full subtree) |
| 100 | // is equal to the number of leaves in the Merkle tree. |
| 101 | if stableEnd != numLeaves-1 { |
| 102 | if len(proofSet) <= height { |
| 103 | return false |
| 104 | } |
| 105 | sum = nodeSum(h, sum, proofSet[height]) |
| 106 | height++ |
| 107 | } |
| 108 | |
| 109 | // All remaining elements in the proof set will belong to a left sibling. |
| 110 | for height < len(proofSet) { |
| 111 | sum = nodeSum(h, proofSet[height], sum) |
| 112 | height++ |
| 113 | } |
| 114 | |
| 115 | // Compare our calculated Merkle root to the desired Merkle root. |
| 116 | if bytes.Equal(sum, merkleRoot) { |
| 117 | return true |
| 118 | } |
| 119 | return false |
| 120 | } |