<p>Instead of looking at the source code, take a look at the <a href="https://en.bitcoin.it/wiki/Protocol_specification#Merkle_Trees" rel="nofollow">relevant wiki page</a>.</p>

<p>As <a href="https://blockexplorer.com/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" rel="nofollow">blockexplorer</a> will reveal, <strong>the merkle root of the genesis block is equal to the hash of the transaction in it</strong>. This is because building a hash tree of one transaction will simply result in that one transaction, and the hash of that tree will be the same as the hash of that one transaction.</p>

<p>When dealing with more transactions, simply put them in a tree and hash them from bottom to top, as the wiki page mentions.</p>

CC BY-SA 3.0
2013-04-14T18:32:50.010Z
But I do not understand in what order they are placed. Like the order of concatenation and of which data exactly. Because in the source code, serialization of objects is used, and I am unfamiliar with that concept so I know not in what order, which variables etc.
farmdve
CC BY-SA 3.0
2013-04-14T19:03:13.073Z
The bottom level of the tree should be sorted by hash, ascending from left to right. The rest is (hopefully) obvious. Forget about the serialization, that's completely irrelevant.
Tom van der Woerdt
CC BY-SA 3.0
2013-04-14T19:09:31.823Z
Since I do not plan to work with anything else than a genesis block, I believe that I don't need any trees for this.
farmdve
CC BY-SA 3.0
2013-04-14T19:13:32.677Z
Correct. When only working with the genesis block, just take the SHA256(SHA256(tx)).
Tom van der Woerdt
CC BY-SA 3.0
2013-04-14T19:33:11.173Z
Well, while I am working on constructing a structure to contain the genesis block's transaction, how would I later SHA256 all of it? Serialization in Bitcoin in C++ is done to convert a few variables regardless of their datatype to a sequence of bytes, in C I've yet to find any standard way of doing it.
farmdve
CC BY-SA 3.0
2013-04-14T20:19:32.150Z
I'd say take a look at [my own bitcoin relay](https://github.com/TvdW/bitpeer/blob/master/src/commands.c#L311), which happens to be written in C. The hash is taken of the entire transaction as sent over the network via the `tx` command. The `tx` command is described [here](https://en.bitcoin.it/wiki/Protocol_specification#tx). An example transaction is included there.
Tom van der Woerdt
CC BY-SA 3.0
2013-04-14T10:34:48.597Z
2013-04-14T10:34:48.597Z
Tom van der Woerdt
<p>I wanted to help out the community and create a pure C application independent of the reference client, that given a timestamp and a public key, will produce a merkle root for creation of a genesis block.</p>

<p>My knowledge of C++ is very little to none. After looking at the source code, this is the relevant code</p>

<pre><code>    const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
    CTransaction txNew;
    txNew.vin.resize(1);
    txNew.vout.resize(1);
    txNew.vin[0].scriptSig = CScript() &lt;&lt; 486604799 &lt;&lt; CBigNum(4) &lt;&lt; vector&lt;unsigned char&gt;((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
    txNew.vout[0].nValue = 50 * COIN;
    txNew.vout[0].scriptPubKey = CScript() &lt;&lt; ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") &lt;&lt; OP_CHECKSIG;
</code></pre>

<p>Which produces this merkle hash</p>

<pre><code> 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
</code></pre>

<p>I am not sure whether CScript() &lt;&lt; 486604799 &lt;&lt; CBigNum(4) &lt;&lt;... is bitshifting all of this, or simply writing it out to .scriptSig.
And I am going to assume that CScript() is the constructor that initializes .scriptSig.</p>

<p>As I delved into the source code and following what BuildMerkleTree calls I ended up in</p>

<pre><code>template&lt;typename T&gt;
uint256 SerializeHash(const T&amp; obj, int nType=SER_GETHASH, int     nVersion=PROTOCOL_VERSION)
{
    CHashWriter ss(nType, nVersion);
    ss &lt;&lt; obj;
    return ss.GetHash();
}
</code></pre>

<p>So I started following ChashWriter, and ended up in </p>

<pre><code>template&lt;typename T&gt;
CHashWriter&amp; operator&lt;&lt;(const T&amp; obj) {
    // Serialize to this stream
    ::Serialize(*this, obj, nType, nVersion);
    return (*this);
}
</code></pre>

<p>And I got lost there. But the closest I got was double SHA256-ing this</p>

<pre><code>04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73 5000000000 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG after supposedly converting to a byte array.
</code></pre>

<p>And I was obviously wrong about it, as the result was not correct. </p>

<p>Apparently, Bitcoin uses something known as Serialization to serialize the object's variables, perhaps concatenate them, but unfortunately due to my inexperience with C++ I could not understand which variables, in what order and so on. Basically, the Bitcoin source code was more complex than I had imagined.</p>

<p>So my question is how I may produce a merkle hash for a genesis block, without using serialization and in C?</p>

<merkle-tree>
How do I compute merkle root for genesis block?
farmdve
# How do I compute merkle root for genesis block?

(Asked by: username_0 on 2013-04-14)

I wanted to help out the community and create a pure C application independent of the reference client, that given a timestamp and a public key, will produce a merkle root for creation of a genesis block.

My knowledge of C++ is very little to none. After looking at the source code, this is the relevant code

```
    const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
    CTransaction txNew;
    txNew.vin.resize(1);
    txNew.vout.resize(1);
    txNew.vin[0].scriptSig = CScript() << 486604799 << CBigNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
    txNew.vout[0].nValue = 50 * COIN;
    txNew.vout[0].scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;

```

Which produces this merkle hash

```
 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b

```

I am not sure whether CScript() << 486604799 << CBigNum(4) <<... is bitshifting all of this, or simply writing it out to .scriptSig.
And I am going to assume that CScript() is the constructor that initializes .scriptSig.

As I delved into the source code and following what BuildMerkleTree calls I ended up in

```
template<typename T>
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int     nVersion=PROTOCOL_VERSION)
{
    CHashWriter ss(nType, nVersion);
    ss << obj;
    return ss.GetHash();
}

```

So I started following ChashWriter, and ended up in

```
template<typename T>
CHashWriter& operator<<(const T& obj) {
    // Serialize to this stream
    ::Serialize(*this, obj, nType, nVersion);
    return (*this);
}

```

And I got lost there. But the closest I got was double SHA256-ing this

```
04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73 5000000000 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG after supposedly converting to a byte array.

```

And I was obviously wrong about it, as the result was not correct.

Apparently, Bitcoin uses something known as Serialization to serialize the object's variables, perhaps concatenate them, but unfortunately due to my inexperience with C++ I could not understand which variables, in what order and so on. Basically, the Bitcoin source code was more complex than I had imagined.

So my question is how I may produce a merkle hash for a genesis block, without using serialization and in C?

**Tags:** merkle-tree


---

## 1 answers

### Answer by username_1 on 2013-04-14. Score: 3

Instead of looking at the source code, take a look at the [relevant wiki page](https://en.bitcoin.it/wiki/Protocol_specification#Merkle_Trees).

As [blockexplorer](https://blockexplorer.com/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f) will reveal, **the merkle root of the genesis block is equal to the hash of the transaction in it**. This is because building a hash tree of one transaction will simply result in that one transaction, and the hash of that tree will be the same as the hash of that one transaction.

When dealing with more transactions, simply put them in a tree and hash them from bottom to top, as the wiki page mentions.

> **Comments:**
> **username_0**: But I do not understand in what order they are placed. Like the order of concatenation and of which data exactly. Because in the source code, serialization of objects is used, and I am unfamiliar with that concept so I know not in what order, which variables etc.  
> **username_1**: The bottom level of the tree should be sorted by hash, ascending from left to right. The rest is (hopefully) obvious. Forget about the serialization, that's completely irrelevant.  
> **username_0**: Since I do not plan to work with anything else than a genesis block, I believe that I don't need any trees for this.  
> **username_1**: Correct. When only working with the genesis block, just take the SHA256(SHA256(tx)).  
> **username_0**: Well, while I am working on constructing a structure to contain the genesis block's transaction, how would I later SHA256 all of it? Serialization in Bitcoin in C++ is done to convert a few variables regardless of their datatype to a sequence of bytes, in C I've yet to find any standard way of doing it.  
> **username_1**: I'd say take a look at [my own bitcoin relay](https://github.com/TvdW/bitpeer/blob/master/src/commands.c#L311), which happens to be written in C. The hash is taken of the entire transaction as sent over the network via the `tx` command. The `tx` command is described [here](https://en.bitcoin.it/wiki/Protocol_specification#tx). An example transaction is included there.  

---

