Skip to content

Commit eece63f

Browse files
committed
Switch blocks to a constant-space Merkle root/branch algorithm.
This switches the Merkle tree logic for blocks to one that runs in constant (small) space. The old code is moved to tests, and a new test is added that for various combinations of block sizes, transaction positions to compute a branch for, and mutations: * Verifies that the old code and new code agree for the Merkle root. * Verifies that the old code and new code agree for the Merkle branch. * Verifies that the computed Merkle branch is valid. * Verifies that mutations don't change the Merkle root. * Verifies that mutations are correctly detected.
1 parent ee60e56 commit eece63f

File tree

12 files changed

+182
-75
lines changed

12 files changed

+182
-75
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ BITCOIN_TESTS =\
5757
test/dbwrapper_tests.cpp \
5858
test/main_tests.cpp \
5959
test/mempool_tests.cpp \
60+
test/merkle_tests.cpp \
6061
test/miner_tests.cpp \
6162
test/mruset_tests.cpp \
6263
test/multisig_tests.cpp \

src/chainparams.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "chainparams.h"
7+
#include "consensus/merkle.h"
78

89
#include "tinyformat.h"
910
#include "util.h"
@@ -32,7 +33,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
3233
genesis.nVersion = nVersion;
3334
genesis.vtx.push_back(txNew);
3435
genesis.hashPrevBlock.SetNull();
35-
genesis.hashMerkleRoot = genesis.ComputeMerkleRoot();
36+
genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
3637
return genesis;
3738
}
3839

src/consensus/merkle.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,23 @@ uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vector<uint2
150150
}
151151
return hash;
152152
}
153+
154+
uint256 BlockMerkleRoot(const CBlock& block, bool* mutated)
155+
{
156+
std::vector<uint256> leaves;
157+
leaves.resize(block.vtx.size());
158+
for (size_t s = 0; s < block.vtx.size(); s++) {
159+
leaves[s] = block.vtx[s].GetHash();
160+
}
161+
return ComputeMerkleRoot(leaves, mutated);
162+
}
163+
164+
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position)
165+
{
166+
std::vector<uint256> leaves;
167+
leaves.resize(block.vtx.size());
168+
for (size_t s = 0; s < block.vtx.size(); s++) {
169+
leaves[s] = block.vtx[s].GetHash();
170+
}
171+
return ComputeMerkleBranch(leaves, position);
172+
}

src/consensus/merkle.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@
88
#include <stdint.h>
99
#include <vector>
1010

11+
#include "primitives/transaction.h"
12+
#include "primitives/block.h"
1113
#include "uint256.h"
1214

1315
uint256 ComputeMerkleRoot(const std::vector<uint256>& leaves, bool* mutated = NULL);
1416
std::vector<uint256> ComputeMerkleBranch(const std::vector<uint256>& leaves, uint32_t position);
1517
uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vector<uint256>& branch, uint32_t position);
1618

19+
/*
20+
* Compute the Merkle root of the transactions in a block.
21+
* *mutated is set to true if a duplicated subtree was found.
22+
*/
23+
uint256 BlockMerkleRoot(const CBlock& block, bool* mutated = NULL);
24+
25+
/*
26+
* Compute the Merkle branch for the tree of transactions in a block, for a
27+
* given position.
28+
* This can be verified using ComputeMerkleRootFromBranch.
29+
*/
30+
std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position);
31+
1732
#endif

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "checkpoints.h"
1313
#include "checkqueue.h"
1414
#include "consensus/consensus.h"
15+
#include "consensus/merkle.h"
1516
#include "consensus/validation.h"
1617
#include "hash.h"
1718
#include "init.h"
@@ -2876,7 +2877,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
28762877
// Check the merkle root.
28772878
if (fCheckMerkleRoot) {
28782879
bool mutated;
2879-
uint256 hashMerkleRoot2 = block.ComputeMerkleRoot(&mutated);
2880+
uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
28802881
if (block.hashMerkleRoot != hashMerkleRoot2)
28812882
return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"),
28822883
REJECT_INVALID, "bad-txnmrklroot", true);

src/miner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "chainparams.h"
1111
#include "coins.h"
1212
#include "consensus/consensus.h"
13+
#include "consensus/merkle.h"
1314
#include "consensus/validation.h"
1415
#include "hash.h"
1516
#include "main.h"
@@ -373,7 +374,7 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
373374
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
374375

375376
pblock->vtx[0] = txCoinbase;
376-
pblock->hashMerkleRoot = pblock->ComputeMerkleRoot();
377+
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
377378
}
378379

379380
//////////////////////////////////////////////////////////////////////////////

src/primitives/block.cpp

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,6 @@ uint256 CBlockHeader::GetHash() const
1515
return SerializeHash(*this);
1616
}
1717

18-
uint256 CBlock::ComputeMerkleRoot(bool* fMutated) const
19-
{
20-
/* WARNING! If you're reading this because you're learning about crypto
21-
and/or designing a new system that will use merkle trees, keep in mind
22-
that the following merkle tree algorithm has a serious flaw related to
23-
duplicate txids, resulting in a vulnerability (CVE-2012-2459).
24-
25-
The reason is that if the number of hashes in the list at a given time
26-
is odd, the last one is duplicated before computing the next level (which
27-
is unusual in Merkle trees). This results in certain sequences of
28-
transactions leading to the same merkle root. For example, these two
29-
trees:
30-
31-
A A
32-
/ \ / \
33-
B C B C
34-
/ \ | / \ / \
35-
D E F D E F F
36-
/ \ / \ / \ / \ / \ / \ / \
37-
1 2 3 4 5 6 1 2 3 4 5 6 5 6
38-
39-
for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and
40-
6 are repeated) result in the same root hash A (because the hash of both
41-
of (F) and (F,F) is C).
42-
43-
The vulnerability results from being able to send a block with such a
44-
transaction list, with the same merkle root, and the same block hash as
45-
the original without duplication, resulting in failed validation. If the
46-
receiving node proceeds to mark that block as permanently invalid
47-
however, it will fail to accept further unmodified (and thus potentially
48-
valid) versions of the same block. We defend against this by detecting
49-
the case where we would hash two identical hashes at the end of the list
50-
together, and treating that identically to the block having an invalid
51-
merkle root. Assuming no double-SHA256 collisions, this will detect all
52-
known ways of changing the transactions without affecting the merkle
53-
root.
54-
*/
55-
std::vector<uint256> vMerkleTree;
56-
vMerkleTree.reserve(vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
57-
for (std::vector<CTransaction>::const_iterator it(vtx.begin()); it != vtx.end(); ++it)
58-
vMerkleTree.push_back(it->GetHash());
59-
int j = 0;
60-
bool mutated = false;
61-
for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
62-
{
63-
for (int i = 0; i < nSize; i += 2)
64-
{
65-
int i2 = std::min(i+1, nSize-1);
66-
if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
67-
// Two identical hashes at the end of the list at a particular level.
68-
mutated = true;
69-
}
70-
vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]),
71-
BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2])));
72-
}
73-
j += nSize;
74-
}
75-
if (fMutated) {
76-
*fMutated = mutated;
77-
}
78-
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
79-
}
80-
8118
std::string CBlock::ToString() const
8219
{
8320
std::stringstream s;

src/primitives/block.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,6 @@ class CBlock : public CBlockHeader
118118
return block;
119119
}
120120

121-
// Build the merkle tree for this block and return the merkle root.
122-
// If non-NULL, *mutated is set to whether mutation was detected in the merkle
123-
// tree (a duplication of transactions in the block leading to an identical
124-
// merkle root).
125-
uint256 ComputeMerkleRoot(bool* mutated = NULL) const;
126-
127121
std::string ToString() const;
128122
};
129123

src/test/main_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,4 @@ BOOST_AUTO_TEST_CASE(test_combiner_all)
7272
Test.disconnect(&ReturnTrue);
7373
BOOST_CHECK(Test());
7474
}
75-
7675
BOOST_AUTO_TEST_SUITE_END()

src/test/merkle_tests.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) 2015 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 "test/test_bitcoin.h"
7+
#include "random.h"
8+
9+
#include <boost/test/unit_test.hpp>
10+
11+
BOOST_FIXTURE_TEST_SUITE(merkle_tests, TestingSetup)
12+
13+
// Older version of the merkle root computation code, for comparison.
14+
static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::vector<uint256>& vMerkleTree)
15+
{
16+
vMerkleTree.clear();
17+
vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
18+
for (std::vector<CTransaction>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it)
19+
vMerkleTree.push_back(it->GetHash());
20+
int j = 0;
21+
bool mutated = false;
22+
for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
23+
{
24+
for (int i = 0; i < nSize; i += 2)
25+
{
26+
int i2 = std::min(i+1, nSize-1);
27+
if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
28+
// Two identical hashes at the end of the list at a particular level.
29+
mutated = true;
30+
}
31+
vMerkleTree.push_back(Hash(vMerkleTree[j+i].begin(), vMerkleTree[j+i].end(),
32+
vMerkleTree[j+i2].begin(), vMerkleTree[j+i2].end()));
33+
}
34+
j += nSize;
35+
}
36+
if (fMutated) {
37+
*fMutated = mutated;
38+
}
39+
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
40+
}
41+
42+
// Older version of the merkle branch computation code, for comparison.
43+
static std::vector<uint256> BlockGetMerkleBranch(const CBlock& block, const std::vector<uint256>& vMerkleTree, int nIndex)
44+
{
45+
std::vector<uint256> vMerkleBranch;
46+
int j = 0;
47+
for (int nSize = block.vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
48+
{
49+
int i = std::min(nIndex^1, nSize-1);
50+
vMerkleBranch.push_back(vMerkleTree[j+i]);
51+
nIndex >>= 1;
52+
j += nSize;
53+
}
54+
return vMerkleBranch;
55+
}
56+
57+
static inline int ctz(uint32_t i) {
58+
if (i == 0) return 0;
59+
int j = 0;
60+
while (!(i & 1)) {
61+
j++;
62+
i >>= 1;
63+
}
64+
return j;
65+
}
66+
67+
BOOST_AUTO_TEST_CASE(merkle_test)
68+
{
69+
for (int i = 0; i < 32; i++) {
70+
// Try 32 block sizes: all sizes from 0 to 16 inclusive, and then 15 random sizes.
71+
int ntx = (i <= 16) ? i : 17 + (insecure_rand() % 4000);
72+
// Try up to 3 mutations.
73+
for (int mutate = 0; mutate <= 3; mutate++) {
74+
int duplicate1 = mutate >= 1 ? 1 << ctz(ntx) : 0; // The last how many transactions to duplicate first.
75+
if (duplicate1 >= ntx) break; // Duplication of the entire tree results in a different root (it adds a level).
76+
int ntx1 = ntx + duplicate1; // The resulting number of transactions after the first duplication.
77+
int duplicate2 = mutate >= 2 ? 1 << ctz(ntx1) : 0; // Likewise for the second mutation.
78+
if (duplicate2 >= ntx1) break;
79+
int ntx2 = ntx1 + duplicate2;
80+
int duplicate3 = mutate >= 3 ? 1 << ctz(ntx2) : 0; // And for the the third mutation.
81+
if (duplicate3 >= ntx2) break;
82+
int ntx3 = ntx2 + duplicate3;
83+
// Build a block with ntx different transactions.
84+
CBlock block;
85+
block.vtx.resize(ntx);
86+
for (int j = 0; j < ntx; j++) {
87+
CMutableTransaction mtx;
88+
mtx.nLockTime = j;
89+
block.vtx[j] = mtx;
90+
}
91+
// Compute the root of the block before mutating it.
92+
bool unmutatedMutated = false;
93+
uint256 unmutatedRoot = BlockMerkleRoot(block, &unmutatedMutated);
94+
BOOST_CHECK(unmutatedMutated == false);
95+
// Optionally mutate by duplicating the last transactions, resulting in the same merkle root.
96+
block.vtx.resize(ntx3);
97+
for (int j = 0; j < duplicate1; j++) {
98+
block.vtx[ntx + j] = block.vtx[ntx + j - duplicate1];
99+
}
100+
for (int j = 0; j < duplicate2; j++) {
101+
block.vtx[ntx1 + j] = block.vtx[ntx1 + j - duplicate2];
102+
}
103+
for (int j = 0; j < duplicate3; j++) {
104+
block.vtx[ntx2 + j] = block.vtx[ntx2 + j - duplicate3];
105+
}
106+
// Compute the merkle root and merkle tree using the old mechanism.
107+
bool oldMutated = false;
108+
std::vector<uint256> merkleTree;
109+
uint256 oldRoot = BlockBuildMerkleTree(block, &oldMutated, merkleTree);
110+
// Compute the merkle root using the new mechanism.
111+
bool newMutated = false;
112+
uint256 newRoot = BlockMerkleRoot(block, &newMutated);
113+
BOOST_CHECK(oldRoot == newRoot);
114+
BOOST_CHECK(newRoot == unmutatedRoot);
115+
BOOST_CHECK((newRoot == uint256()) == (ntx == 0));
116+
BOOST_CHECK(oldMutated == newMutated);
117+
BOOST_CHECK(newMutated == !!mutate);
118+
// If no mutation was done (once for every ntx value), try up to 16 branches.
119+
if (mutate == 0) {
120+
for (int loop = 0; loop < std::min(ntx, 16); loop++) {
121+
// If ntx <= 16, try all branches. Otherise, try 16 random ones.
122+
int mtx = loop;
123+
if (ntx > 16) {
124+
mtx = insecure_rand() % ntx;
125+
}
126+
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
127+
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);
128+
BOOST_CHECK(oldBranch == newBranch);
129+
BOOST_CHECK(ComputeMerkleRootFromBranch(block.vtx[mtx].GetHash(), newBranch, mtx) == oldRoot);
130+
}
131+
}
132+
}
133+
}
134+
}
135+
136+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)