Hash pointers are a data structure containing the previous block’s hash value and a pointer to that block. Here is a demonstration:
Here, we have four blocks that are connected. “Block 0” does not point to another block as it’s the first block in the chain. Block 0 is also identified as the
We use a hash function to convert the data in the previous block to a hash code or a hash value that we store in the following block.
Some properties of good hash functions are:
Hash functions are deterministic, meaning the hash function will always return the same output for the same input.
A good hash function is collision-free, meaning different inputs can’t be mapped to the same output.
After the hash function is applied to a string/input, it returns a different output. This property of the hash function helps us hide the actual data. A small change in the input will result in a significantly different hash value.
Hash functions are puzzle-friendly (puzzles are used in the consensus of the nodes in proof-of-work based blockchains, i.e., Bitcoin).
Good hash functions are computationally efficient to calculate. This ensures that the performance of the application is not compromised.
Hash functions are one-way functions, meaning the original input can’t be derived from the hash value, without an exhaustive search (brute-force attack).
Hash pointers are the building blocks of blockchain. Each block contains a hash pointer that points to the previous block and contains its hash. The immutability of blockchain is one of its most significant aspects, and hash pointers allow us to attain this property. Hash pointers help us validate if a block is modified or not. Let’s look at a demonstration:
Even a minor change in the blocks would completely change the hash code of that block. This is the reason why hash pointers are used to detect if the blockchain is altered or not. Mostly, we use
Our data can exceed the size of 512 bits or be less than 512 bits. If our data exceeds 512 bits, we divide it into chunks of 512 bits. To work with data of arbitrary length, SHA-256 uses the Merkle-Damgard transform to work with inputs of arbitrary lengths.
The Merkle‐Damgard transform is quite simple. There is a
For the genesis block, we combine it with an initialization vector of 256 bits and pass it to the compression function, which gives us an output of 256 bits. The output, along with the next chunk, is then forwarded to the compression function. The process continues until the last chunk, and the output we get at the end is the hash code of our entire block.
If we have a chunk with less than 512 bits, we pad a one and zeros to our chunk to get the desired size. Then, we pass it to the SHA-256 to calculate the hash code.
Here is an illustration of the Merkle-Damagard transform:
Hash pointers are the building blocks of the blockchain. They make the blockchain immutable. We can verify the blockchain using the hash values stored in the hash pointers. Even a minor change in one of the blocks would change the hash value of that block.
Free Resources