by JAMES PEYTON
Understanding cryptography’s role is crucial to understanding how blockchain works. Cryptography is a method of techniques to prevent third parties from accessing data. In blockchain, cryptography is used to link all of the blocks together. The most common method is hashing. There are different hash functions. Bitcoin uses SHA-256, but they all work in similar ways. Hashing is a function in which an input is put through an equation to give a unique output of a fixed length called the hash. The hash function works one-way and the input cannot be determined by output. One input will always produce the same output.
Another important concept is Merkle trees. A Merkle tree is a branching data structure, and the components are called leaf nodes, non-leaf nodes, and the Merkle root. Each data point (leaf node) is paired with another, hashed using SHA-256 or another equation, and the hashes (non-leaf node) are again paired and hashed repeatedly until there is only one output. It looks like a family tree where each couple only has one child. In Bitcoin, the transactions are the leaf nodes. The Bitcoin miner pairs all of the transactions and hashes them down to one Merkle root, which is placed in the block header as we will see in a moment.
The block header is where cryptography comes into blockchain. Different blockchains will contain different data in their blocks and block headers, but the first four parts below will be common among all blockchains. Here we will take a look at Bitcoin’s block header:
- Version number of the system: Whenever the system’s software is changed, a new type of transaction is added for example, the version number will be updated.
- The hash of the previous block’s header: The previous block’s header is hashed and placed here in the new block’s header. This links all the chains on the blockchain.
- Transaction Merkle root: Here is where the Merkle root of the new block’s transaction list is placed.
- The Time Stamp: Simply the date and time the block is added.
- Hash Target: This is the mining difficulty. We will look at the mechanics of Bitcoin mining in a moment.
- Nonce: This is a dummy variable to achieve the target.
A Bitcoin miner adds a block to the blockchain through mining. Mining is simply running the block header through the hash function, and the objective is to have the hash below the hash target. The nonce is changed repeatedly until the header’s hash is below the target. It is a dummy variable, and this is where computational competition enters the fold. Bitcoin miners essentially just brute force their way through this computation, meaning they may start with a nonce of 0 and increase it by 1 until it meets the target.
Merkle roots are used here to improve scalability. Merkle roots allow there to just be a fixed length of data representing transactions placed into the block header. Otherwise, each block header would be filled with all the transactions in that block, which would also create an unnecessary replication of data. If someone doesn’t want to download every individual transaction in a blockchain, they can simply download the block headers of each block. They can then verify the positions of transactions in blocks and Merkle trees through Merkle proofs. A Merkle proof is looking through a Merkle tree to see that all of the hashing was done correctly to find the transaction.
As complex as these concepts are, they are just the fundamentals of blockchain, and I hope you now understand what makes a blockchain and keeps it all together. Some blockchains will be much more complex than others. For example, Bitcoin is relatively simple when compared to Ethereum, and it has to do with functionality. Bitcoin operates simply as a peer-to-peer transaction network. Ethereum can run programs, execute smart contracts, and operate as a peer-to-peer network. In order to be this versatile, Ethereum utilizes more Merkle Trees, Merkle proofs, and coding beyond the scope of this article.
About The Author: James Peyton
More posts by James Peyton