<p>Yes, these kinds of attack do effect Bitcoin and are known and described. Bitcoin does not have any consensus level mitigations for them because doing so requires consensus changes which are difficult to do. It is known that Bitcoin's merkle tree design has multiple vulnerabilities.</p>
<p>The particular attack described on wikipedia is that an internal level can be presented as a valid document. In Bitcoin, this is theoretically possible, although it would take significant work to produce a block where the transactions produce hashes which can then be deserialized as valid transactions. If such a block were produced, it could result in a network partition due to behavior around the caching of invalid blocks. Obviously the solution to this is to be sure that cached invalid blocks are treating carefully to avoid rejecting blocks for things that are third party malleable.</p>
<p>In fact, this particular issue lead to a vulnerability in Bitcoin Core 0.13  that has since been fixed. A description of the vulnerability is available <a href="https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html" rel="nofollow noreferrer">on the bitcoin-dev mailing list</a>.</p>
<p>There is another related attack going the other direction - including a transaction in a block which could be interpreted as an internal node in a merkle tree. This attack could be used to target SPV wallets and trick those wallets into thinking a transaction was included in a block when it was not. This attack in described by Sergio Demian Lerner in <a href="https://bitslog.com/2018/06/09/leaf-node-weakness-in-bitcoin-merkle-tree-design/" rel="nofollow noreferrer">this blog post</a>.</p>
<p>The only true solution to this second attack requires some kind of consensus change to introduce the tree depth. But consensus changes are hard, and there are other things that can be done. SPV wallets can protect themselves by checking that no internal nodes can be serialized as valid transactions. Such an internal node is incredibly unlikely to appear randomly so this shouldn't have any real effect on the user. In Bitcoin Core, to help with protecting against this attack, 64 byte transactions won't be relayed. As 64 byte transactions are smaller than any standard transaction could be, this is safe.</p>

CC BY-SA 4.0
2020-09-30T22:21:39.770Z
2020-09-30T22:21:39.770Z
Ava Chow
<p>In the Wikipedia article about <a href="https://en.wikipedia.org/wiki/Merkle_tree" rel="nofollow noreferrer">Merkle trees</a>, I was just reading this, unable to  understand where the problem lies:</p>
<blockquote>
<h3>Second preimage attack</h3>
<p>The Merkle hash root does not indicate the tree depth, enabling a second-preimage attack in which an attacker creates a document other than the original that has the same Merkle hash root. For the example above, an attacker can create a new document containing two data blocks, where the first is hash 0-0 + hash 0-1, and the second is hash 1-0 + hash 1-1.</p>
<p>One simple fix is defined in <a href="https://www.rfc-editor.org/info/rfc6962" rel="nofollow noreferrer">Certificate Transparency</a>: when computing leaf node hashes, a 0x00 byte is prepended to the hash data, while 0x01 is prepended when computing internal node hashes. Limiting the hash tree size is a prerequisite of some formal security proofs, and helps in making some proofs tighter. Some implementations limit the tree depth using hash tree depth prefixes before hashes, so any extracted hash chain is defined to be valid only if the prefix decreases at each step and is still positive when the leaf is reached.</p>
</blockquote>
<p><strong>My first questions are:</strong> Is this really a problem in Bitcoin? If yes, how is it being solved in Bitcoin core?</p>
<p><strong>My second questions are:</strong> Could this problem be solved by storing the tree depth of each block directly in the block chain? Or speaking of Bitcoin, would that negatively affect the block validation procedure itself somehow?</p>

<bitcoin-core><transaction-verification><merkle-tree><block-validity>
Is this Merkle hash root problem existent in Bitcoin?
Marcus
# Is this Merkle hash root problem existent in Bitcoin?

(Asked by: username_0 on 2020-09-29)

In the Wikipedia article about [Merkle trees](https://en.wikipedia.org/wiki/Merkle_tree), I was just reading this, unable to understand where the problem lies:

> ### Second preimage attack
>
> The Merkle hash root does not indicate the tree depth, enabling a second-preimage attack in which an attacker creates a document other than the original that has the same Merkle hash root. For the example above, an attacker can create a new document containing two data blocks, where the first is hash 0-0 + hash 0-1, and the second is hash 1-0 + hash 1-1.
>
> One simple fix is defined in [Certificate Transparency](https://www.rfc-editor.org/info/rfc6962): when computing leaf node hashes, a 0x00 byte is prepended to the hash data, while 0x01 is prepended when computing internal node hashes. Limiting the hash tree size is a prerequisite of some formal security proofs, and helps in making some proofs tighter. Some implementations limit the tree depth using hash tree depth prefixes before hashes, so any extracted hash chain is defined to be valid only if the prefix decreases at each step and is still positive when the leaf is reached.

**My first questions are:** Is this really a problem in Bitcoin? If yes, how is it being solved in Bitcoin core?

**My second questions are:** Could this problem be solved by storing the tree depth of each block directly in the block chain? Or speaking of Bitcoin, would that negatively affect the block validation procedure itself somehow?

**Tags:** bitcoin-core, transaction-verification, merkle-tree, block-validity


---

## 1 answers

### Answer by username_1 on 2020-09-30. Score: 2 | Accepted answer

Yes, these kinds of attack do effect Bitcoin and are known and described. Bitcoin does not have any consensus level mitigations for them because doing so requires consensus changes which are difficult to do. It is known that Bitcoin's merkle tree design has multiple vulnerabilities.

The particular attack described on wikipedia is that an internal level can be presented as a valid document. In Bitcoin, this is theoretically possible, although it would take significant work to produce a block where the transactions produce hashes which can then be deserialized as valid transactions. If such a block were produced, it could result in a network partition due to behavior around the caching of invalid blocks. Obviously the solution to this is to be sure that cached invalid blocks are treating carefully to avoid rejecting blocks for things that are third party malleable.

In fact, this particular issue lead to a vulnerability in Bitcoin Core 0.13 that has since been fixed. A description of the vulnerability is available [on the bitcoin-dev mailing list](https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html).

There is another related attack going the other direction - including a transaction in a block which could be interpreted as an internal node in a merkle tree. This attack could be used to target SPV wallets and trick those wallets into thinking a transaction was included in a block when it was not. This attack in described by Sergio Demian Lerner in [this blog post](https://bitslog.com/2018/06/09/leaf-node-weakness-in-bitcoin-merkle-tree-design/).

The only true solution to this second attack requires some kind of consensus change to introduce the tree depth. But consensus changes are hard, and there are other things that can be done. SPV wallets can protect themselves by checking that no internal nodes can be serialized as valid transactions. Such an internal node is incredibly unlikely to appear randomly so this shouldn't have any real effect on the user. In Bitcoin Core, to help with protecting against this attack, 64 byte transactions won't be relayed. As 64 byte transactions are smaller than any standard transaction could be, this is safe.


---

