El root hash o root hash, is the name given to the hash resulting from a merkle tree. Its main utility is to serve as a marker to indicate the reliability of the information of the Merkle tree in question. Something that is very useful within a P2P network.
To better explain this situation, imagine the following example:
Juan wants to install a node Bitcoin full. During its initialization, the node Bitcoin must be synchronized with the network. This in order to obtain all the information accumulated in the blockchain Bitcoin. To achieve this, the new Bitcoin node will download all of this information. However, Bitcoin is an unreliable network that bases its operation on a series of cryptographic functions. This in order to guarantee the veracity of the information without the need to trust third parties.
To do this, it makes use of cryptographic hash functions such as SHA-256. As the node downloads the information, it verifies that the "trusted superiors" hashes are correct. If so, the Bitcoin node continues its download work. If not, discard the information and start downloading the information from another new source where all trusted hashes match. Once the download is complete, the new node can begin its work, being sure that all the information has been verified and is correct.
The example given makes clear to us the usefulness of the root hash when verifying the authenticity of the data received. But it is also perfectly applicable to all blockchains and their complete synchronization processes.
How is a root hash created?
A root hash is generated from an incremental hashing of the information of each data block. This process ensures that each of the blocks of information is treated and tagged with a unique and unrepeatable hash. Take for example making a Merkle tree using the SHA-256 hashing algorithm. In this case, our data blocks are:
Once we have the data blocks at hand, we start to hash each one of them. For this we will use the SHA-256 function which would give us the following result.
Thus we have that each block is assigned a unique and unrepeatable SHA-256 hash. This will be our bookmarks to recognize authenticity and how we will organize our Merkle tree. Following the procedure, we are left to join the data blocks as follows:
As you can see, we concatenate the hashes of the nearby blocks with each other in pairs. So we have the pairs AB, CD, EF, GH. At the end of the first round, we continue to build the hash join but this time with the pairs ABCD and EFGH. And finally we end up concatenating everything into a single hash that includes the hashes of ABCD and EFGH to give rise to the root merkle (ABCDEFGH).
This last block at the top is our root hash or merkle root. It is the resulting hash of the Merkle Tree.