2023-07-22

What is a hash?

A hash is a cryptographic function that plays a fundamental role in the operation of person-to-person electronic cash. It is used in multiple parts of the protocol and its correct implementation ensures the security of the network.

There are multiple hash function algorithms in bitcoin two are used being the main one The SHA-256 algorithm.

Features

A hash function is used to obtain, from any type of information, a number represented as bits of uniform size in such a way that only by applying the hash function to the original data can that number be obtained.

For example, if the SHA-256 function is applied to the text string Bitcoin the following result is obtained:

B4056DF6691F8DC72E56302DDAD345D65FEAD3EAD9299609A826E2344EB63AA4

If it is changed to Bitcoin Cash:

8A9851255D671C4E0AC3AD525AD0FF595CB31A1AD85327A77DF3D15129B0A245

As you can see, even if the input of the function has changed little, this does not mean that the result of the function changes little. It is clear that there is a big difference between the first number and the second (the numbers are represented in hexadecimal format).

In addition, hash functions have a high resistance to collisions. This means that it is not possible to determine in advance which number will result from the hash function without knowing the information and is therefore a way of verifying that there has been no manipulation or corruption of data. Collision resistance makes it extremely difficult to find two or more inputs to the function that result in the same hash. This is a desired advantage of the function, since otherwise the integrity of the information originally provided could not be verified. If this were not the case, a malicious agent could have manipulated the information and then tried to obtain the same hash.

For example, how do you know what the input is that produces the following hash?

682FC380BDA56463E1C21EC365D1D9A9596C1BA87656E603EF74440B0BAEA416

Without knowing the prior information, it is impossible to know. Guessing it by brute force may not be an option. In this case, it is the hash of the Abstract of the whitepaper.

Finally, hash functions are easy to do and difficult to undo. Therefore, validating the information is practically inexpensive, while generating a hash, especially if you are looking for a specific one, takes time, computational power, and energy.

Its use in Bitcoin

Working test

It is this last characteristic of hashing algorithms-that they are easy to make and hard to unmake-that makes it ideal for use as the key piece in the proof-of-work mechanism designed by Satoshi Nakamoto. If a miner wants to prove that he has spent time, computational power, and is therefore financially invested in securing the network, he is required to ensure that each block has a hash that starts with a set number of zeros. The greater the difficulty of the network, the greater the number of zeros.

And, given that due to the characteristics of the hashing algorithm, it is neither easy nor cheap to obtain a specific hash, this ensures that the provider of proof of work does not want that computational power to be wasted and acts in compliance with the network rules, since obtaining that computational power has cost him money and electricity.

At the root of the block

The hashes are also used to generate a Merkle tree, which is a structure that allows verifying the membership of a transaction to a block. It plays a fundamental role in SPV.