<p>The speed of the hash is roughly linear in the number of bytes processed: SHA2 processes block of 64 bytes at a time, and the time taken is equal to the number of blocks processed.  Computing a hashtree processes more blocks due to the intermediate levels but almost all the processing can be done in parallel unlike the linear hash which must be done sequentially. </p>

<p>A node only verifies a block once.  For that purpose it doesn't matter much if the hash is a tree or linear though if the parallelism is exploited computing a tree hash is faster.</p>

<p>[And Bitcoin Core uses vector instructions to processes tree hashes in parallel for a several times speedup. It doesn't currently bother using multiple cores for that, though it could.]</p>

<p>A lite client, however, does not validate a whole block, it normally doesn't even fetch a whole block.  Instead, it wants to know if a transaction is in a block.  Here, the hash structure matters a lot. If a linear hash were used the only way to prove that a transaction is in a block to a lite client would be to send it the whole block.  With a tree hash one need only send it log2(n) values and the transaction itself.</p>

<p>The tree isn't stored in current Bitcoin implementations because there is no operation that would really benefit from it being stored in the implementations. Proving a lite client was paid is a relatively infrequent operation and recomputing the hash tree is inexpensive. An implementation could start storing it if it had a reason to do so.  TXIDs aren't stored either as they can just be computed from the transactions themselves.</p>

CC BY-SA 4.0
2018-09-14T19:09:05.760Z
2018-09-14T19:25:25.587Z
2018-09-14T19:25:25.587Z
G. Maxwell
<p>Suppose we have 8 transactions in a block <code>X</code> as follows and we know every node's hash value of the whole Merkle tree (that is, including intermediary nodes such as 45). Then if someone claims transaction <code>t</code> exists in <code>X</code> and it's located at <code>6</code>. To verify this, we only need to look up hash values at 7, 45, 0123 and hash up to the top and compare with Merkle root 01234567.</p>

<p>I assume (based on <a href="http://learnmeabitcoin.com/glossary/merkle-root" rel="nofollow noreferrer">this</a>) Merkle tree is faster than hashing the concatenation of TXID0 ~ TXID7 directly is because hash algorithm runs faster on small files (even run it <code>~log(N)</code> times) than on a large file (i.e., concatenation of <code>N</code> TXIDs, <code>N</code> is 8 here) at one time. </p>

<p>But reading <a href="https://bitcoin.stackexchange.com/questions/49086/where-is-the-merkle-tree-located">this</a> post, it seems that only Merkle root and <code>N</code> TXIDs are stored in a block and we have to recreate tree structure every time we verify a transaction, which would actually run hash functions <code>~N</code> times instead of <code>~log(N)</code> times (if I was correct mathematically).</p>

<hr>

<blockquote>
  <p>My questions are:</p>
  
  <ol>
  <li>Does hash <code>~N</code> times  still faster than hash one time on <code>N</code>
  concatenated hash values?</li>
  <li>Why is Merkle tree not stored? Is it due to storage consideration?</li>
  </ol>
</blockquote>

<p><a href="https://i.sstatic.net/Wl0NR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Wl0NR.png" alt="enter image description here"></a></p>

<transactions><blockchain><block><hash><merkle-tree>
How come Merkle tree is more efficient when tree branches are not stored in blockchain?
Nicholas
# How come Merkle tree is more efficient when tree branches are not stored in blockchain?

(Asked by: username_0 on 2018-09-14)

Suppose we have 8 transactions in a block `X` as follows and we know every node's hash value of the whole Merkle tree (that is, including intermediary nodes such as 45). Then if someone claims transaction `t` exists in `X` and it's located at `6`. To verify this, we only need to look up hash values at 7, 45, 0123 and hash up to the top and compare with Merkle root 01234567.

I assume (based on [this](http://learnmeabitcoin.com/glossary/merkle-root)) Merkle tree is faster than hashing the concatenation of TXID0 ~ TXID7 directly is because hash algorithm runs faster on small files (even run it `~log(N)` times) than on a large file (i.e., concatenation of `N` TXIDs, `N` is 8 here) at one time.

But reading [this](https://bitcoin.stackexchange.com/questions/49086/where-is-the-merkle-tree-located) post, it seems that only Merkle root and `N` TXIDs are stored in a block and we have to recreate tree structure every time we verify a transaction, which would actually run hash functions `~N` times instead of `~log(N)` times (if I was correct mathematically).

---

> My questions are:
>
> 1. Does hash `~N` times still faster than hash one time on `N`
>    concatenated hash values?
> 2. Why is Merkle tree not stored? Is it due to storage consideration?

[![enter image description here](https://i.sstatic.net/Wl0NR.png)](https://i.sstatic.net/Wl0NR.png)

**Tags:** transactions, blockchain, block, hash, merkle-tree


---

## 1 answers

### Answer by username_1 on 2018-09-14. Score: 3

The speed of the hash is roughly linear in the number of bytes processed: SHA2 processes block of 64 bytes at a time, and the time taken is equal to the number of blocks processed. Computing a hashtree processes more blocks due to the intermediate levels but almost all the processing can be done in parallel unlike the linear hash which must be done sequentially.

A node only verifies a block once. For that purpose it doesn't matter much if the hash is a tree or linear though if the parallelism is exploited computing a tree hash is faster.

[And Bitcoin Core uses vector instructions to processes tree hashes in parallel for a several times speedup. It doesn't currently bother using multiple cores for that, though it could.]

A lite client, however, does not validate a whole block, it normally doesn't even fetch a whole block. Instead, it wants to know if a transaction is in a block. Here, the hash structure matters a lot. If a linear hash were used the only way to prove that a transaction is in a block to a lite client would be to send it the whole block. With a tree hash one need only send it log2(n) values and the transaction itself.

The tree isn't stored in current Bitcoin implementations because there is no operation that would really benefit from it being stored in the implementations. Proving a lite client was paid is a relatively infrequent operation and recomputing the hash tree is inexpensive. An implementation could start storing it if it had a reason to do so. TXIDs aren't stored either as they can just be computed from the transactions themselves.


---

