Merkle Trees

“A hash tree is a tree [usually binary tree] of hashes in which the leaves are hashes of data blocks in, for instance, a file or set of files.” [1] Why not just hash all of the data at once you say? Because it is not as efficient. In applications like Bitcoin when this is done fairly frequently (for tx validation, etc.) this gets costly.

Join people in 22 countries around the world in my course on Become a Bitcoin + Blockchain Programmer.

Calculation

Source: Investopedia

Lets assume that Ha and Hb have already been calculated from the data Ta and Tb and that:

Ha = b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082

Hb = f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16

First, we must reverse the bytes (they need to be little endian) because that is how Bitcoin calculates the hashes. You can use this python script, if you like:

#!/usr/bin/env python line = raw_input("Input original hex string

") n = 2

orig_list = [line[i:i+n] for i in range(0, len(line), n)]

reversed_list = orig_list[::-1]

reversed = ''.join(reversed_list) print reversed

We get:

Ha = 82501c1178fa0b222c1f3d474ec726b832013f0a532b44bb620cce8624a5feb1

Hb = 169e1e83e930853391bc6f35f605c6754cfead57cf8387639d3b4096c54f18f4

Then, the node (and root in this case) Hab is calculated by concatenating the two and hashing with sha256 twice (i.e. hash256 ):

$ printf "82501c1178fa0b222c1f3d474ec726b832013f0a532b44bb620cce8624a5feb1169e1e83e930853391bc6f35f605c6754cfead57cf8387639d3b4096c54f18f4" | xxd -r -p | openssl sha256 (stdin)= a24ee7fb7333f85c16560ed8850a1773d6977ce7a4936367eaf72f8fff33797e $ printf "a24ee7fb7333f85c16560ed8850a1773d6977ce7a4936367eaf72f8fff33797e" | xxd -r -p | openssl sha256 (stdin)= ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d

Now, reverse the bytes! (back to big endian):

7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff