Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

merkle.cpp
Go to the documentation of this file.
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 <hash.h>
7 #include <utilstrencodings.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 time
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 uint256 ComputeMerkleRoot(std::vector<uint256> hashes, bool* mutated) {
47  bool mutation = false;
48  while (hashes.size() > 1) {
49  if (mutated) {
50  for (size_t pos = 0; pos + 1 < hashes.size(); pos += 2) {
51  if (hashes[pos] == hashes[pos + 1]) mutation = true;
52  }
53  }
54  if (hashes.size() & 1) {
55  hashes.push_back(hashes.back());
56  }
57  SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2);
58  hashes.resize(hashes.size() / 2);
59  }
60  if (mutated) *mutated = mutation;
61  if (hashes.size() == 0) return uint256();
62  return hashes[0];
63 }
64 
65 
66 uint256 BlockMerkleRoot(const CBlock& block, bool* mutated)
67 {
68  std::vector<uint256> leaves;
69  leaves.resize(block.vtx.size());
70  for (size_t s = 0; s < block.vtx.size(); s++) {
71  leaves[s] = block.vtx[s]->GetHash();
72  }
73  return ComputeMerkleRoot(std::move(leaves), mutated);
74 }
75 
Definition: block.h:72
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
256-bit opaque blob.
Definition: uint256.h:123
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Definition: merkle.cpp:46
std::vector< CTransactionRef > vtx
Definition: block.h:76
void SHA256D64(unsigned char *out, const unsigned char *in, size_t blocks)
Compute multiple double-SHA256&#39;s of 64-byte blobs.
Definition: sha256.cpp:698
Released under the MIT license