<p>There are a number of issues here, with different answers.</p>
<p><strong>Can Merkle trees use a commutative operation in general to combine hashes?</strong></p>
<p><em>Yes</em>, but only if they aren't intended to commit to the order of the leaves.</p>
<p>Clearly when a commutative operation is used, <em>[A,B,C,D]</em> and <em>[D,C,A,B]</em> will hash to the same thing. This is not a problem if the Merkle root is intended to be a commitment to the (multi)set of leaves, but it is if it is intended to be a commitment to the list.</p>
<p><strong>Could the Bitcoin transaction Merkle tree have used a commutative operation?</strong></p>
<p><em>Maybe</em>, it's hard to talk about hypotheticals.</p>
<p>The order of transactions in a block is relevant (transactions can spend outputs created by previous transactions in the same block), so you want to prevent a peer from reordering the transactions to invalidate it without breaking the commitment. Obviously alternative solutions could have been used here, either by encoding the order explicitly, or by performing a topological sort on the set of transactions before verification.</p>
<p>Obviously this cannot be changed anymore in the actual Bitcoin protocol without a very invasive hardfork.</p>
<p><strong>Can you use addition or xor as commutative hash combination function?</strong></p>
<p><em>Not without security reduction</em>.</p>
<p>Imagine a 2-element Merkle tree with leaf elements <em>A</em> and <em>B</em>. Their hash is <em>H = hash(leafhash(A) + leafhash(B))</em>. An attacker who knows <em>A</em> and <em>B</em> can use a <a href="https://www.iacr.org/archive/crypto2002/24420288/24420288.pdf" rel="noreferrer">generalized birthday attack</a> to find other leaves <em>C</em> and <em>D</em> such that <em>leafhash(C) + leafhash(D) = leafhash(A) + leafhash(B)</em>. Perhaps surprisingly, this only needs <em>~2<sup>128</sup></em> work if <em>leafhash</em> is a generic (and secure) <em>256</em>-bit hash function. By doing so, the attacker has managed to perform a second preimage attack on the Merkle tree construction, in only the square root of the time that would normally be expected for second preimage security.</p>
<p>There may be reductions in other security properties too.</p>
<p><strong>Are there other feasible commutative hash combination functions then?</strong></p>
<p><em>Yes, but they don't shrink the data.</em></p>
<p>For example, if the hashes are treated as integers modulo a large prime, then combining child hashes <em>x</em> and <em>y</em> as <em>(x+y,xy)</em> works (because the sum and product uniquely identify the inputs, but not their order). When working in a large characteristic-<em>2</em> finite field (e.g. <em>GF(2<sup>256</sup>)</em>), <em>(x+y,x<sup>3</sup>+y<sup>3</sup>)</em> also works.</p>
<p>Another much simpler possibility is simply sorting the elements: combining <em>x</em> and <em>y</em> as <em>(min(x,y),max(x,y))</em> works fine.</p>
<p><strong>If using secure commutative combination functions doesn't shrink the data, then is there any point?</strong></p>
<p><em>It means that you can prove an element is in the Merkle tree without revealing its position.</em></p>
<p>This is a minor bandwidth gain (<em>log<sub>2</sub>(n)</em> bits for a tree with <em>n</em> elements), reduces some implementation complexity, and may be a slight privacy improvement if the positions are sensitive. In fact, this approach is used in the proposed <a href="https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki" rel="noreferrer">Taproot</a> script trees (concatenating the hashes after sorting), precisely for the reason above.</p>
<p>Disclaimer: I'm a co-author of that proposal.</p>

CC BY-SA 4.0
2020-11-19T20:39:20.533Z
2020-11-19T20:51:39.880Z
2020-11-19T20:51:39.880Z
Pieter Wuille
<p>Why aren't the hashes combined using a commutative operation such as addition instead of concatenating them? Any possible vulnerabilities or security flaws?</p>
<p>thanks for your time and efforts.</p>

<hash><merkle-tree>
Adding instead of concatenating hashes in Merkle trees
Ayush
# Adding instead of concatenating hashes in Merkle trees

(Asked by: username_0 on 2020-11-19)

Why aren't the hashes combined using a commutative operation such as addition instead of concatenating them? Any possible vulnerabilities or security flaws?

thanks for your time and efforts.

**Tags:** hash, merkle-tree


---

## 1 answers

### Answer by username_1 on 2020-11-19. Score: 18 | Accepted answer

There are a number of issues here, with different answers.

**Can Merkle trees use a commutative operation in general to combine hashes?**

*Yes*, but only if they aren't intended to commit to the order of the leaves.

Clearly when a commutative operation is used, *[A,B,C,D]* and *[D,C,A,B]* will hash to the same thing. This is not a problem if the Merkle root is intended to be a commitment to the (multi)set of leaves, but it is if it is intended to be a commitment to the list.

**Could the Bitcoin transaction Merkle tree have used a commutative operation?**

*Maybe*, it's hard to talk about hypotheticals.

The order of transactions in a block is relevant (transactions can spend outputs created by previous transactions in the same block), so you want to prevent a peer from reordering the transactions to invalidate it without breaking the commitment. Obviously alternative solutions could have been used here, either by encoding the order explicitly, or by performing a topological sort on the set of transactions before verification.

Obviously this cannot be changed anymore in the actual Bitcoin protocol without a very invasive hardfork.

**Can you use addition or xor as commutative hash combination function?**

*Not without security reduction*.

Imagine a 2-element Merkle tree with leaf elements *A* and *B*. Their hash is *H = hash(leafhash(A) + leafhash(B))*. An attacker who knows *A* and *B* can use a [generalized birthday attack](https://www.iacr.org/archive/crypto2002/24420288/24420288.pdf) to find other leaves *C* and *D* such that *leafhash(C) + leafhash(D) = leafhash(A) + leafhash(B)*. Perhaps surprisingly, this only needs *~2128* work if *leafhash* is a generic (and secure) *256*-bit hash function. By doing so, the attacker has managed to perform a second preimage attack on the Merkle tree construction, in only the square root of the time that would normally be expected for second preimage security.

There may be reductions in other security properties too.

**Are there other feasible commutative hash combination functions then?**

*Yes, but they don't shrink the data.*

For example, if the hashes are treated as integers modulo a large prime, then combining child hashes *x* and *y* as *(x+y,xy)* works (because the sum and product uniquely identify the inputs, but not their order). When working in a large characteristic-*2* finite field (e.g. *GF(2256)*), *(x+y,x3+y3)* also works.

Another much simpler possibility is simply sorting the elements: combining *x* and *y* as *(min(x,y),max(x,y))* works fine.

**If using secure commutative combination functions doesn't shrink the data, then is there any point?**

*It means that you can prove an element is in the Merkle tree without revealing its position.*

This is a minor bandwidth gain (*log2(n)* bits for a tree with *n* elements), reduces some implementation complexity, and may be a slight privacy improvement if the positions are sensitive. In fact, this approach is used in the proposed [Taproot](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki) script trees (concatenating the hashes after sorting), precisely for the reason above.

Disclaimer: I'm a co-author of that proposal.


---

