# What is the optimal way to increment extranonce?

I'm trying to learn how to block headers are created and an efficient way of doing so. Given a list .of transactions, can I change the extranonce without having to rehash the entire list?

EDIT: To clarify, I am not asking about what the extranonce is but whether there are algorithms to recompute the merkletree better than others.

• If one has only one change in the Merkle tree the complexity to compute the hash is `O(lg(n))` because other branches are hashes are reused. No further speedup is possible (except Asicboost) as even one bit change in coinbase transaction causes avalon effect. Jun 18 '20 at 15:07
• Thanks! This answers the question. I take it that the coinbase Tx must go into the start then
– Wil
Jun 18 '20 at 17:20
• The extra nonce is indeed additional space in coinbase. I don't see any reason why you cannot precompute all the extranonce fields. So, even the `log(n)` time could be amortized Jun 19 '20 at 7:54
• I think you can get ln(n)/ln(2) with precomputation. But it is less if the coinbase tx didn’t go into the bottom. And I guess it needs to go into the bottom?
– Wil
Jun 20 '20 at 9:00
• Each transactions is a leaf at the bottom level of the Merkle tree.
– Murch
Jul 18 '20 at 17:20

The Merkle root of the transactions is calculated by building a binary tree from the transactions as leaves where each parent node is the hash of its two children. As the coinbase transaction is the first transaction in the block, you can store the hashing partners on the coinbase transaction's Merkle branch and only repeat all the hashes to which the coinbase transaction contributes. This reduces the required number of hashes to calculate the new Merkle root for n transactions from `O(n)` to `O(log(n))` given that no other transactions in the block were changed.