/root/bitcoin/src/consensus/merkle.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <consensus/merkle.h> |
6 | | |
7 | | #include <crypto/sha256.h> |
8 | | #include <hash.h> |
9 | | #include <primitives/block.h> |
10 | | #include <primitives/transaction.h> |
11 | | #include <util/check.h> |
12 | | |
13 | | #include <cstddef> |
14 | | #include <memory> |
15 | | #include <utility> |
16 | | |
17 | | /* WARNING! If you're reading this because you're learning about crypto |
18 | | and/or designing a new system that will use merkle trees, keep in mind |
19 | | that the following merkle tree algorithm has a serious flaw related to |
20 | | duplicate txids, resulting in a vulnerability (CVE-2012-2459). |
21 | | |
22 | | The reason is that if the number of hashes in the list at a given level |
23 | | is odd, the last one is duplicated before computing the next level (which |
24 | | is unusual in Merkle trees). This results in certain sequences of |
25 | | transactions leading to the same merkle root. For example, these two |
26 | | trees: |
27 | | |
28 | | A A |
29 | | / \ / \ |
30 | | B C B C |
31 | | / \ | / \ / \ |
32 | | D E F D E F F |
33 | | / \ / \ / \ / \ / \ / \ / \ |
34 | | 1 2 3 4 5 6 1 2 3 4 5 6 5 6 |
35 | | |
36 | | for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and |
37 | | 6 are repeated) result in the same root hash A (because the hash of both |
38 | | of (F) and (F,F) is C). |
39 | | |
40 | | The vulnerability results from being able to send a block with such a |
41 | | transaction list, with the same merkle root, and the same block hash as |
42 | | the original without duplication, resulting in failed validation. If the |
43 | | receiving node proceeds to mark that block as permanently invalid |
44 | | however, it will fail to accept further unmodified (and thus potentially |
45 | | valid) versions of the same block. We defend against this by detecting |
46 | | the case where we would hash two identical hashes at the end of the list |
47 | | together, and treating that identically to the block having an invalid |
48 | | merkle root. Assuming no double-SHA256 collisions, this will detect all |
49 | | known ways of changing the transactions without affecting the merkle |
50 | | root. |
51 | | */ |
52 | | |
53 | | |
54 | 4.91k | uint256 ComputeMerkleRoot(std::vector<uint256> hashes, bool* mutated) { |
55 | 4.91k | bool mutation = false; |
56 | 10.8k | while (hashes.size() > 1) { Branch (56:12): [True: 5.98k, False: 4.91k]
|
57 | 5.98k | if (mutated) { Branch (57:13): [True: 0, False: 5.98k]
|
58 | 0 | for (size_t pos = 0; pos + 1 < hashes.size(); pos += 2) { Branch (58:34): [True: 0, False: 0]
|
59 | 0 | if (hashes[pos] == hashes[pos + 1]) mutation = true; Branch (59:21): [True: 0, False: 0]
|
60 | 0 | } |
61 | 0 | } |
62 | 5.98k | if (hashes.size() & 1) { Branch (62:13): [True: 1.11k, False: 4.87k]
|
63 | 1.11k | hashes.push_back(hashes.back()); |
64 | 1.11k | } |
65 | 5.98k | SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2); |
66 | 5.98k | hashes.resize(hashes.size() / 2); |
67 | 5.98k | } |
68 | 4.91k | if (mutated) *mutated = mutation; Branch (68:9): [True: 0, False: 4.91k]
|
69 | 4.91k | if (hashes.size() == 0) return uint256(); Branch (69:9): [True: 0, False: 4.91k]
|
70 | 4.91k | return hashes[0]; |
71 | 4.91k | } |
72 | | |
73 | | |
74 | | uint256 BlockMerkleRoot(const CBlock& block, bool* mutated) |
75 | 3.82k | { |
76 | 3.82k | std::vector<uint256> leaves; |
77 | 3.82k | leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even |
78 | 13.5k | for (size_t s = 0; s < block.vtx.size(); s++) { Branch (78:24): [True: 9.70k, False: 3.82k]
|
79 | 9.70k | leaves.push_back(block.vtx[s]->GetHash().ToUint256()); |
80 | 9.70k | } |
81 | 3.82k | return ComputeMerkleRoot(std::move(leaves), mutated); |
82 | 3.82k | } |
83 | | |
84 | | uint256 BlockWitnessMerkleRoot(const CBlock& block) |
85 | 1.09k | { |
86 | 1.09k | std::vector<uint256> leaves; |
87 | 1.09k | leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even |
88 | 1.09k | leaves.emplace_back(); // The witness hash of the coinbase is 0. |
89 | 2.48k | for (size_t s = 1; s < block.vtx.size(); s++) { Branch (89:24): [True: 1.39k, False: 1.09k]
|
90 | 1.39k | leaves.push_back(block.vtx[s]->GetWitnessHash().ToUint256()); |
91 | 1.39k | } |
92 | 1.09k | return ComputeMerkleRoot(std::move(leaves)); |
93 | 1.09k | } |
94 | | |
95 | | /* This implements a constant-space merkle path calculator, limited to 2^32 leaves. */ |
96 | | static void MerkleComputation(const std::vector<uint256>& leaves, uint32_t leaf_pos, std::vector<uint256>& path) |
97 | 0 | { |
98 | 0 | path.clear(); |
99 | 0 | Assume(leaves.size() <= UINT32_MAX); |
100 | 0 | if (leaves.size() == 0) { Branch (100:9): [True: 0, False: 0]
|
101 | 0 | return; |
102 | 0 | } |
103 | | // count is the number of leaves processed so far. |
104 | 0 | uint32_t count = 0; |
105 | | // inner is an array of eagerly computed subtree hashes, indexed by tree |
106 | | // level (0 being the leaves). |
107 | | // For example, when count is 25 (11001 in binary), inner[4] is the hash of |
108 | | // the first 16 leaves, inner[3] of the next 8 leaves, and inner[0] equal to |
109 | | // the last leaf. The other inner entries are undefined. |
110 | 0 | uint256 inner[32]; |
111 | | // Which position in inner is a hash that depends on the matching leaf. |
112 | 0 | int matchlevel = -1; |
113 | | // First process all leaves into 'inner' values. |
114 | 0 | while (count < leaves.size()) { Branch (114:12): [True: 0, False: 0]
|
115 | 0 | uint256 h = leaves[count]; |
116 | 0 | bool matchh = count == leaf_pos; |
117 | 0 | count++; |
118 | 0 | int level; |
119 | | // For each of the lower bits in count that are 0, do 1 step. Each |
120 | | // corresponds to an inner value that existed before processing the |
121 | | // current leaf, and each needs a hash to combine it. |
122 | 0 | for (level = 0; !(count & ((uint32_t{1}) << level)); level++) { Branch (122:25): [True: 0, False: 0]
|
123 | 0 | if (matchh) { Branch (123:17): [True: 0, False: 0]
|
124 | 0 | path.push_back(inner[level]); |
125 | 0 | } else if (matchlevel == level) { Branch (125:24): [True: 0, False: 0]
|
126 | 0 | path.push_back(h); |
127 | 0 | matchh = true; |
128 | 0 | } |
129 | 0 | h = Hash(inner[level], h); |
130 | 0 | } |
131 | | // Store the resulting hash at inner position level. |
132 | 0 | inner[level] = h; |
133 | 0 | if (matchh) { Branch (133:13): [True: 0, False: 0]
|
134 | 0 | matchlevel = level; |
135 | 0 | } |
136 | 0 | } |
137 | | // Do a final 'sweep' over the rightmost branch of the tree to process |
138 | | // odd levels, and reduce everything to a single top value. |
139 | | // Level is the level (counted from the bottom) up to which we've sweeped. |
140 | 0 | int level = 0; |
141 | | // As long as bit number level in count is zero, skip it. It means there |
142 | | // is nothing left at this level. |
143 | 0 | while (!(count & ((uint32_t{1}) << level))) { Branch (143:12): [True: 0, False: 0]
|
144 | 0 | level++; |
145 | 0 | } |
146 | 0 | uint256 h = inner[level]; |
147 | 0 | bool matchh = matchlevel == level; |
148 | 0 | while (count != ((uint32_t{1}) << level)) { Branch (148:12): [True: 0, False: 0]
|
149 | | // If we reach this point, h is an inner value that is not the top. |
150 | | // We combine it with itself (Bitcoin's special rule for odd levels in |
151 | | // the tree) to produce a higher level one. |
152 | 0 | if (matchh) { Branch (152:13): [True: 0, False: 0]
|
153 | 0 | path.push_back(h); |
154 | 0 | } |
155 | 0 | h = Hash(h, h); |
156 | | // Increment count to the value it would have if two entries at this |
157 | | // level had existed. |
158 | 0 | count += ((uint32_t{1}) << level); |
159 | 0 | level++; |
160 | | // And propagate the result upwards accordingly. |
161 | 0 | while (!(count & ((uint32_t{1}) << level))) { Branch (161:16): [True: 0, False: 0]
|
162 | 0 | if (matchh) { Branch (162:17): [True: 0, False: 0]
|
163 | 0 | path.push_back(inner[level]); |
164 | 0 | } else if (matchlevel == level) { Branch (164:24): [True: 0, False: 0]
|
165 | 0 | path.push_back(h); |
166 | 0 | matchh = true; |
167 | 0 | } |
168 | 0 | h = Hash(inner[level], h); |
169 | 0 | level++; |
170 | 0 | } |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | 0 | static std::vector<uint256> ComputeMerklePath(const std::vector<uint256>& leaves, uint32_t position) { |
175 | 0 | std::vector<uint256> ret; |
176 | 0 | MerkleComputation(leaves, position, ret); |
177 | 0 | return ret; |
178 | 0 | } |
179 | | |
180 | | std::vector<uint256> TransactionMerklePath(const CBlock& block, uint32_t position) |
181 | 0 | { |
182 | 0 | std::vector<uint256> leaves; |
183 | 0 | leaves.resize(block.vtx.size()); |
184 | 0 | for (size_t s = 0; s < block.vtx.size(); s++) { Branch (184:24): [True: 0, False: 0]
|
185 | 0 | leaves[s] = block.vtx[s]->GetHash().ToUint256(); |
186 | 0 | } |
187 | 0 | return ComputeMerklePath(leaves, position); |
188 | 0 | } |