In part two of this series (part 1 here), we will focus on a concept called hashing. I will use the same metaphor from the previous article to explain hashing. If we go back to our example of the train, the coupler is a mechanism used to connect carriages and the engine of the train, as shown in the image below.
The coupler comes in different shapes and sizes: buffer and chain, link and pin, and bell-and-hook are few examples of couplers. Just like a coupler for a train, the concept is similar and used in blockchains to connect blocks. The blocks of a blockchain are linked to its immediate predecessor using a hash. The first block in the blockchain is called the “Genesis” block or Block 0, and its link lies out in the system. The below diagram is a simplified version of how the Bitcoin blockchain looks and, generally, blockchains follow the same pattern.
At this point, you might be wondering: what is hashing and how is it useful in the whole blockchain ecosystem. Worry not, I will explain that in greater detail below.
Hashing is a simply process of taking a variable length input and creating a fixed size output, which is sometimes called the Digest. Just like different types of couplers for a train, there are also different types of hash functions. Some examples are the murmur hash, CityHash, SHA256, xxHash, and SHA-3.
Blockchain mainly uses cryptographic hash functions, such as SHA256 and Keccak-256. Example of blockchains using these functions are Bitcoin and Ethereum, respectively.
The main reasons for using cryptographic hash functions for blockchain lies in its properties, which are:
- Deterministic — each time you parse the same input through the hash function, you will get same hash value.
- Efficiency — for any given input, the hash function should be capable of returning the hash value quickly, or else the system is simply not efficient.
- Pre-image resistance — given a hash value, it should be difficult to find its input value. It is not impossible to determine the original input from its hash function.
- Collision resistance — there are two different inputs to a hash function that cannot have the same hash value.
- Slight change to input — making a small change to the input will change the hash value completely. For example, just changing the first letter of the Iron Maiden song “The Trooper” to “the Trooper” using SHA256 hash function changes the hash value completely.
Just to cap this article off, let’s say a malicious attacker changes a data in block 1 of the blockchain. Because of the properties of cryptographic hash functions, any slight change made in block 1 will change the hash value stored in block 2, and this will result in changes in block 3 and so on and so forth. Therefore, it will become very hard for the malicious attacker to compute an alternate chain from Block 1, which will catch-up with and overtake the one true chain.
If you want to learn more on Blockchain, you can download my ebook, “Introduction To Blockchain“.