The Bitcoin protocol makes extensive use of so-called hash function to add new blocks to the blockchain through the mining process.
Generally, from a technical point of view, a hash function is an algorithm that, from data of arbitrary length, produces a hash of fixed length. This hash is a hexadecimal code that represents the original data packet used to generate it and is generally much shorter in length.
The main use of this function is create a synthetic code relatively short representing a much larger data packet. Considering that this process, if done in this way, inevitably involves some data loss, it is in fact irreversible, the hash cannot be traced back to the data packet used to generate it.
The algorithm used by Bitcoin for the hash function is SHA-256, which produces a 256-bit hash and requires a predictable amount of computing power.
To add a new block to the Bitcoin blockchain, a queued transaction data packet is used in the mempool waiting to be committed as input to the hash function.
However, the miner's job is not just to produce the hash of this input, but to produce a hash that starts with several additional zeros and matches the input data package.
In fact, although the hash cannot be traced back to the data that generated it, it is always possible to repeat the operation to verify that it was successful. In this way, since all the transaction data entered in the block are public, anyone can verify with SHA-256 that the hash produced by the miner is correct. Only if it is correct will the hash be added to the blockchain and the miner will receive the reward.
Hash function and mining difficulty
The number of additional zeros is changed every two weeks or so and corresponds to the so-called difficulty: the more zeros are needed, the more difficult it will be to find the hash, the longer it will take.
To find the hash with the extra zeros, the miner can only proceed by trial and error, which is why computing power plays a key role in the mining process.
Since each block can be mined by a single miner, and since the reward is given only to the miner who can find the hash that confirms the block, this generates a competition in which the miner with the highest computing power has the greatest advantage .
Further, even the smallest change in input data produces a completely different hash, so the only way to proceed is to process as many hashes as possible in the shortest time possible, executing the hash function many times per second.
For example, a hashrate of 130 Exahash / s means that the machines that are trying to mine Bitcoin blocks worldwide perform a total of 130 Exahash functions per second, or 130 billion (or 130 billion).
The Bitcoin protocol also uses the hash function to generate the hash of a transaction, which is the unique identifier used to identify each individual transaction.