Coverage Report

Created: 2026-04-20 22:07

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