Course Content
Introduction to Blockchain
Introduction to Blockchain
Overview of Hashing
We have already mentioned hashing and block hashes a few times, but didn't really pay much attention to that, so it's time to discuss what hashing actually is.
Properties of Hash Functions
In blockchain, hash functions are used to:
- Secure transactions by creating a unique fingerprint for each one;
- Generate addresses from public keys;
- Create the links in the blockchain through block hashes.
With this in mind, a good hash function for blockchain has several key properties:
Hashing, especially using a good hash function, can be compared to making a smoothie. Let's take a look at the following illustration:
Well, in fact, it is possible to find the original input given a hash, however, it would take too much time to actually be somewhat practical.
Block Hashing
In order to get the hash of a certain block, Bitcoin uses the SHA-256 hash function, which outputs a 256-bit (32-byte) hash. It's a part of the SHA-2 family designed by the National Security Agency (NSA) and known for its strong security properties, making it resistant to collisions, preimage attacks, and other cryptographic vulnerabilities.
The hash of a Bitcoin block is computed by taking the block header data. This data is input into the SHA-256 hash function twice in a process known as double SHA-256.
Here is an image to make things clear:
The resulting 256-bit hash must meet certain criteria defined by the network's difficulty target. If the hash is not below the target, the nonce is adjusted and the hash is recomputed until a qualifying hash is found. This final hash is the block's unique identifier.
Merkle Root
As we have mentioned before, Merkle root is a single hash that represents all the transactions included in the block. Computation of a Merkle root starts with the hashes of each transaction in a block. Double SHA-256 is used here as well.
Each hash is then paired with another and they are hashed together to form a new hash. This process of pairing and hashing the new hashes continues until only one hash remains. The final hash is the Merkle root, representing all transactions in the block and ensuring their integrity by encoding the entire set of transactions into a single hash. Such process forms a Merkle tree.
Let's take a look at an example of a Merkle tree:
As you can see, our block contains 6 transactions (each labeled as Tx) and via hashing and pairing new hashes a Merkle root is computed.
Thanks for your feedback!