I am wondering how Bitcoin uses cryptography and secure hash algorithm such as SHA256 to link the records in the blockchain. And if possible I think it will give me a better understanding if there is a demonstration of a figure of blockchains.
1 Answer
A Bitcoin block is a container data structure that aggregates all the transactions. The block is made of a header (containing metadata), followed by a long list of transactions that are included in the block. The block header consists of merkle tree root, previous block header hash, timestamp amongst other metadata.
The Merkle tree root in simpler terms includes the summary of all the transactions in the block using the merkle tree. Every transaction in block is serialized and hashed twice with SHA256
to get the transaction id or txid
. These txid
s of all the transactions in the block is then double-hashed pairwise, with SHA256
, until there is only one node at the top called a merkle root. Since this merkle root is built from all the transactions in the block themselves, the root can be thought as containing the summary of all the transactions included. An illustrative example of Merkle root calculation is shown below:
The header portion of the block is hashed twice with SHA256
to get the block header hash (in the mining process). This block header hash is always referenced in the next block in the previousblockhash
field. Thus every block is linked to the previous block using its header hash forming a link that goes all the way back to the genesis block like the illustration shown below:
Since SHA256
is a one way mathematical function, there is no way to reverse engineer the data by just looking at the hash. SHA256
is also deterministic, in the sense that a particular data will always give a particular hash. Slightly changing the data will completely change the hash output.
So, if a party wants to change some data in a transaction that is included in a block somewhere in the blockchain, it will change the merkle root of that block. Since merkle root forms the part of the block header, changing the merkle root will completely change the block header hash. Since this hash is referenced in the next block, the current hash will be incompatible with the hash that was referenced and hence this block will not be accepted.
This is where proof of work in mining plays a key role. As I have said above that changing a data in a block completely changes the block header. So if you want to change a data in a past block, you will have to re-mine all subsequent blocks. So you will have to spend the electricity and compute cost to re-mine all the block AND outpace the network by forming the longest chain. The cost of doing such an action is very high, and may not outweigh the benefits that you derive from it, thus providing inherent security to Bitcoin.
Also, I would like to point out that you cannot change transaction data structure at your whims and fancies. Every transaction is signed with a private key that includes the entire transaction data as a message. So, slightly changing the transaction data will render the transaction invalid and including such invalid transactions in a block will render the block invalid. So, in a nutshell all that you can do is include/exclude certain transaction from previous blocks, or make slight cosmetic changes (like ECDSA signature malleability).