In this chapter, we will take a step-by-step approach to understanding the principles of "Bitcoin" and "blockchain", and their various features and characteristics. The buzzwords Bitcoin and blockchain relate to emerging technologies that have taken the world by storm. As we progress through the chapter, we will cover the fundamentals of these unique technologies in a detailed manner. The chapter will also take you through the essence of cryptography and how cryptocurrencies are using this innovation as a foundation. We will also look into the history of Bitcoin, the alternate cryptocurrencies available, and the various Bitcoin wallets, and we will get an understanding of the structuring of the Bitcoin block header. When it comes to blockchain, we will go through the different types of blockchain, blockchain structure, its various workings and features, the challenges faced, the various platforms available for blockchain, and more.
In this chapter, we will look into the following topics:
- Cryptography and cryptocurrency
- History and introduction to Bitcoin
- An introduction to blockchain
- Comparing private, public, and consortium blockchains
The blockchain technology is the backbone of cryptocurrency and it uses cryptography to keep the data secure. In this section of the chapter, we will discuss cryptography, cryptocurrency, and how cryptography is used in cryptocurrency implementation. We will also look into the workings of fiat currency cryptography, energy use in cryptocurrency, and also the security features of cryptography that are used. We will learn everything we need to know about decentralized digital money and its underlying architecture.
As we browse through the various parts of this section, we will learn about the history of Bitcoin, its alternative currencies, Bitcoin basics, and the technical operation of Bitcoin; then, we will browse through the technical foundation of the blockchain, the fundamentals of cryptography, and how it keeps data secure. From there, we will move on to understanding blockchain, its features, the underlying architecture on top of each Bitcoin, and how cryptocurrencies are implemented.
Before we understand cryptocurrency, let's first look at some basic currencies used around the world. These currencies are also known as fiat currencies.
They are shown in the following diagram:
Some of the characteristics of fiat currencies are as follows:
- These are owned by the governments of countries
- They are centrally controlled through banks or legal entities
- They are inflationary, meaning the value of a currency decreases
- They include various security properties to prevent counterfeiting and cheating, but it is not impossible to counterfeit them
Since it is not impossible to counterfeit currencies, law enforcement comes into the picture to stop and prosecute those involved in counterfeiting fiat currencies. We will now look at various security features that are implemented in fiat currencies, as shown in the following diagram:
You will find two watermarks. You'll also see micro lettering and identification marks being implemented. Most of the time, fluorescent ink is used on notes for detecting notes that are fake.
Cryptocurrency is a very interesting and intriguing topic that has become a global phenomenon. It is different from all the currencies that are found all over the world. Right from the working of cryptocurrency, to who owns or controls it, its benefits and limitations, its use and much more, are all based on new ideas and processes. Let's now see a brief overview of the various features of cryptocurrency.
The main features of cryptocurrency are as follows:
- It is a digital asset used as a medium of exchange
- It secures transactions and controls supply by using cryptography
- It is a subset of alternative currencies
- In 2008, the first decentralized cryptocurrency was conceptualized
- Cryptocurrency is digital money, while the underlying technology that enables the moving of digital coins or assets between individuals is called blockchain
The main cryptography techniques used in cryptocurrency are a hash function and a digital signature.
Let's look at each of these briefly. The hash function is a mathematical function with the following properties:
- Any input that we provide, be it a string, number, floating number, or anything, can be of any size
- It produces a fixed-size output such as a 128-bit hash outcome or even a 256-bit outcome
- It is collision-resistant
- It hides the data within it
Let's now look at an example of a hash function in the following diagram:
Let's look at the hash function being used here. In the preceding diagram, we gave Fox, one string, as an input to the encryption module and we applied a hash function to it. It gives a specific fixed hash outcome, which is also known as a digest hash sum. Let's say we pass another statement that says The red fox runs across the ice and we apply a hash function to it. It gives us a specific hash sum, also known as a digest. Then, we add another statement, The red fox walks across the ice, and we see that it changes the hash function as the input has been changed. This is the key feature that the hash function brings.
This is the second building block of cryptocurrency and is digitally analogous to the hand written signatures that we usually make.
The properties of the digital signature are as follows:
- You can create your own signatures, but they can be verified by another person too
- The signature is tied to a particular document or message so that it cannot be used again and again for different documents and messages
Let's now look at an example of a digital signature in the following diagram:
Let's take an example and say that there is a string in the document which states that "I agree to pay $5000 for the software". Since this is the document that is being signed, we first apply a hash function to it. It gives us a hash or digest of the document, which is again fixed in size. Then, we apply our private key, or a specific key, to encrypt it and we get the outcome as a digital signature. This digital signature is signed specifically for this input, which states that "I agreed to pay $5,000 for the software".
Here, we saw how cryptography and cryptocurrency are digital assets that are widely used. We also saw how cryptocurrency uses cryptography and its techniques, such as hash functions and digital signatures.
Bitcoin is the first decentralized digital currency, and as such it is a revolutionary technology invention. It changed the way we compute things and the way we operate software and computers. Bitcoin and blockchain are considered to be the next big wave of change after the internet.
Now, let's look at the following properties of Bitcoin:
- It's an international network of payments.
- It uses cryptography to control its creation and management, rather than relying on central authorities such as governments, banks, union territories, or intermediaries.
- It's not printed but is produced by people using software that solves mathematical problems.
- It is controlled and limited in supply, which arrests the hyperinflation problem. For example, whenever African countries were short of currency notes, they had to print more notes, which resulted in hyperinflation and brought the value of the currency down.
- Since the arrival of Bitcoin, the way Bitcoin programs are written means there will always be a maximum of 21 million Bitcoins available across the globe. The moment 21 million Bitcoins have been mined, the program will not generate any more new Bitcoins. Hence, Bitcoins will be limited in supply and this will arrest the problem of hyperinflation.
Bitcoin was conceptualized by Satoshi Nakamoto in 2008. We do not know whether it is a person or a group of people. This anonymous person or group of persons still remains a mystery. What we know is that Nakamoto has claimed to be a man living in Japan born on April 15th, 1975. However, there are a lot of theories and speculation about the identity of Nakamoto. Some people say that the identity of Nakamoto is based on a number of cryptography and computer science experts living in the US and the EU, not necessarily Japanese people. In November 2017, Nakamoto was believed to own up to roughly 1 million Bitcoins, the value of this 1 million comes to be 7.2 billion US dollars, which is a huge amount of money owned by someone who is not known to anybody in the world.
Altcoins is an alternate cryptocurrency to Bitcoin. Once Bitcoin became popular, people realized the value, robustness, and flexibility Bitcoin brought and also started liking the fact that Bitcoin appreciated in value. They simply took the source code of the Bitcoin protocol available from GitHub repositories, forked it, modified it as per their needs, and created alternative cryptocurrencies. With the increasing popularity of Bitcoin, the usage and rate of Bitcoin have skyrocketed. Leaving that aside, the next question that arises is "how do we store or possess this virtual currency securely in digital form?".
There are several types of wallet available, in which we can hold our Bitcoins safely. Each of these wallets has their own function and ways to operate.
The different kinds of the wallet are as follows:
- Software wallet: The software wallet is a Bitcoin application that sits on your computer's hard drive and allows you to completely control and secure your Bitcoins. Bitcoin Armory is an example of a software wallet and is supposed to be the most stable and secure wallet of all.
- Web wallet: The next type of wallet is a web wallet. Web wallets are more convenient than software wallets since they can be accessed to use your funds, Bitcoins, or assets from any device. So, you can access your web wallet on your Android or iOS device, your desktop and even on the internet.
- Cold wallet: The next type of wallet is called a cold wallet. Cold wallets are simply any kind of Bitcoin wallet that is not connected to the internet. These can be in paper form, or you can have wallets on USB drives as well.
- Brain wallet: The next type of wallet is the brain wallet. The brain wallet has its address generated by a computer program by hashing the passphrase with words that the user enters.
- Hardware wallet: The next type of wallet is the hardware wallet. The hardware wallet can only be accessed through physical contact with the wallet by the designated person, who owns the wallet. It stores the user's private keys in a secure hardware drive that is accessible only to the user and usually uses a fingerprint scanner or biometrics to access it.
In this section, we will look at the architecture of blockchain, how it works, and what the salient features are that make it so disruptive. Blockchain was created by Satoshi Nakamoto as an infrastructure for Bitcoin and it is treated as the biggest thing since the internet. Blockchain, at a high level, consists of three major components, which are shown in the following diagram:
Let's now look into the following three major components of blockchain:
- P2P Network: A peer-to-peer network (P2P) helps maintain a consistent copy of the distributed ledger. All the transactions that are captured on the blockchain in the form of blocks are maintained across the network of nodes running the blocks and programs by the distributed ledger.
- Private Key Cryptography: This component is used by blockchain for the security and hash functions that make it immutable.
- Blockchain Program: This component is used by blockchain as a protocol to execute steps that make it secure.
Now, let's look at how a blockchain implementation works and what all the things are that are involved in completing the flow of execution. Let's take the example of a Bitcoin, shown in the following workflow for blockchain:
In the preceding diagram, we see how blockchain works and the steps mentioned are as follows:
- Let's say someone requests a transaction in Bitcoin; for example, person A wants to send Bitcoin to person B. That person or entity requests a transaction that results in debiting person A's wallet and crediting person B's wallet with one Bitcoin.
- Now, this requested transaction is broadcasted to the P2P network, consisting of computers known as nodes, which are spread across the globe.
- Now, once the transaction is propagated, the transaction is picked up by the network of nodes, they validate the nodes that are running the blockchain programs, and they validate the transaction and the user status using known algorithms that are common across all nodes.
- In our example, we talked about person A sending one Bitcoin to person B, which involves the cryptocurrency as well. The verification part also involves checking whether person A initiating the transaction for one Bitcoin really owns that Bitcoin or not.
- Once verified, the transaction is then combined with other transactions to create a new block of data for a ledger. This freshly created block of a series of transactions then gets added to an existing blockchain in a way that is permanent (it's unalterable).
- With this block added to the blockchain, the transaction is complete.
- Once the block is added, it remains there for the rest of the blockchain's life.
We will now discuss the following features of blockchain:
- Secure: It is really impossible for anyone to tamper with transactions or ledger records present in the blockchain, which makes it more secure, so it is seen as a reliable source of information.
- Global reach: Blockchain has been adopted worldwide and has the backing of many investors from banking and non-banking sectors, which makes it a globally accepted technology stack.
- Automated operations: Operations are fully automated through software. Private companies are not needed to handle operations, which is why there is no mediation required to carry out the transactions, and trust is assured, so people can carry out their own transactions.
- Open source: Blockchain is an open source technology. All the operations are carried out by the open source community.
- Distributed: Blockchain works in a distributed mode, in which records are stored in all nodes in the network. If one node goes down, it doesn't impact any other nodes or any other records, because they are globally distributed across all the nodes.
- Flexible: Blockchain is programmable, using basic programming concepts and programming semantics, which makes blockchain very flexible.
In this section, we will learn about the following aspects of blockchain: its structure; its building blocks; and its core parts that make it disruptive, robust, strong, and tamperproof.
Let's have a look at the structure of the blockchain. The blockchain structure is very similar to that of linked lists or binary trees. Linked lists or binary trees are linked to each other using pointers, which point to the previous or next list elements on the nodes in the linked list. The structure of blockchain is not really different from that of binary trees, but the major difference is that blockchain is tamperproof and it is also very easy to find out if any tampering has taken place.
In the following diagram, we will look at a representation of how blockchain is constructed and how it is a linked list:
Let's now discuss the structure and elements of the blockchain.
The blockchain is a linked list that is built with hash pointers instead of pointers. This is the exact reason why blockchain, though it resembles a linked list, is different because, in the linked list we have been using the pointers to point to previous nodes for the elements in the lists, but in the case of the blockchain, the pointers are hash pointers and not just simple pointers.
So, typically, any block in the blockchain consists of three parts, or feet such as a Header, Merkle, and Transaction's Id list. This is a newly created block.
We can see the structure of blockchain in the following diagram:
We will now look into the following blocks and their elements:
- Header: This block contains the version information of the block, the nonce, the previous block ID, and the timestamp that is being hashed again at the time the block is created.
- Merkle: This block is a hash built from the block's transaction identifiers.
- Transaction's Id list: This block represents the transactions themselves. It's a list of records, identification hashes, that are included in the block's Merkle tree.
The block is then created with all of the preceding details. This newly created block gets added to the blockchain.
Now, let's look at what the Merkle tree is, as follows:
- In the blockchain structure, it's also known as a binary hash tree. It's a data structure used for summarizing and verifying the integrity of large sets of data.
- It not only summarizes the data that is being captured on a particular block, but it also verifies the integrity of the data, which makes sure that the data represented in the block is integrated and not tampered with. It contains cryptographic hashes, which are used to make sure integrity is maintained across the block.
- It's an upside-down tree where the root is at the top and the leaves are at the bottom.
Now, let's look at a representation of the Merkle tree in the following diagram:
Here is a brief overview of what the top hash, data blocks, and Merkle root stand for, as follows:
- In the preceding diagram, we can see Top Hash, which is the root of the Merkle tree, and there are leaf nodes or leaves that make up the entire tree.
- Data Blocks are transactions that have been captured and hashed. They are paired and hashed multiple times, and that's how you reach the top of the Merkle tree.
- A block of one or more new records is collected, and such records are then paired and hashed together multiple times until a single hash remains. This single hash is called the Merkle root of that Merkle tree.
If any change or tampering occurs in any part of the transaction data, we can see this compromise in a bold and clear manner. Hence, the Merkle tree is important in the implementation of blockchain and is a major contributor to making sure that blockchain data has not been tampered with.
Now, we will look into the structure of the Bitcoin block header, in which the header consists of a block and each block holds the following three sets of block metadata:
- A reference to a previous block hash
- The difficulty, timestamp, and nonce parts of the header
- The Merkle root
Let's look at the various terms represented in the following diagram:
The following is an elaboration of the terms mentioned in the preceding diagram:
- Version: The first part of the header is the 4 bytes of version. A version number tracks software protocol upgrades. It is really important to capture that data as part of the block itself because, if there are any changes made in subsequent versions of the program, it becomes necessary to capture which program or node is running each program to send the transactions and blocks across the internet.
- Previous Hash Block: The second part of the header is the 32 bytes previous hash block or previous hash block code. It's a reference to the hash of the previous block in the chain. It's not just a link to the previous block, it's a hashed pointer to the previous block, which makes sure that the new block added maintains the sequence of the blocks or the chain.
- Merkle Root: The third part of the header is the 32 bytes of the Merkle root. We just looked at the Merkle root, which is a hash root of the Merkle tree. It is a final hash code of all the transactions that were captured as part of the block.
- Timestamp: The fourth part of the header is the 4 bytes of timestamp. It is the approximate time taken for the creation of this block. We use the term approximate because once the block is created, it takes a few milliseconds to get added to the actual blockchain as the transactions are verified by different nodes.
- Difficulty target: The fifth part of the header is the 4 bytes that capture the difficulty target. It's the proof of work algorithm of the blocks. It is given for a particular node that is running the Bitcoin blockchain program.
- Nonce: The sixth part of the header is 4 bytes of the nonce. It is the counter used for the Proof of Work (PoW) algorithm. Whichever node is first in calculating and solving the mathematical problem actually yields the result of the problem-solving solution and the result contains this nonce, which is used to capture the transaction and verifies that the PoW being carried by the node is correct.
Thus, we can see that this is the typical structure of a Bitcoin block header, which is roughly 80 bytes of information that is captured in each of the blocks.
Now, we will look at the overall structure of the Bitcoin blockchain header in the following diagram:
In this example, Block 16 represents the previous block's hash and consists of the timestamp of creation, the transaction root is also known as the Merkle root, and the nonce is the algorithm-cum-counter that has to be verified. All of the hidden information on the transactions is hashed again, and that hash is captured in Block 17. It consists of the previous block's hash, the timestamp, the Merkle root, and the nonce.
In the sections for Data 1, Data 2, Data 3, and Data 4 in the preceding diagram, all the data is paired and hashed multiple times. Thus, the data keeps going in the upward direction until it gets to one final hash. These blocks keep moving and the entire Merkle tree is formed by the transactions that are captured in the blocks. This is what makes it so strong, robust, tamperproof, integrated, and immutable. This is the beauty of blockchain.
After looking at the building blocks, structure, and core parts of the blockchain, we are now going to learn about the various types of blockchain implementations available, and we will also look at their features and benefits. As we already know, the Bitcoin blockchain became popular for its immutability, security, robustness, and transparency. The industry was in need of the Bitcoin blockchain architecture, but there were challenges that came up.
The challenges faced by the industry are as follows:
- How will it cater to an enterprise's specific needs: We know that the Bitcoin blockchain is open source, which is open to all kinds of solutions. If the IT industry or various enterprises wanted to use this blockchain architecture, they would not be able to use it as their needs are different.
- How can we use other currencies with blockchain: Enterprises that were willing to use the blockchain architecture due to its immutability, security, and transparency wanted to track their own assets, which wasn't possible in Bitcoin blockchain as it was using Bitcoin and other Altcoins as currency.
- How will it allow us to define specific roles and permissions on the blockchain: Different enterprises have their own roles, privileges, and permissions that have to be captured and executed on the blockchain nodes. In the case of the Bitcoin blockchain, we know that each and every node plays the same role across the globe.
So, these challenges triggered the need for specific changes to blockchain for industries, enterprises, and IT companies, in order to use blockchain for their own purposes, which in turn gave birth to different blockchain versions or variants.
Let's have a look at the following types of blockchain:
- Public: Public blockchains have ledgers visible to everyone on the internet and anyone can verify and add a block of transactions to the blockchain. Some examples are Bitcoins, Ethereum, Dash, Factom, and the hundreds of Altcoins that are available in the market today.
- Private: The second type of blockchain is the private blockchain. All permissions are kept centralized to an organization. Private blockchains allow only specific people with specific roles in the organization to verify transaction blocks, but everyone on the internet is allowed to view them. This also depends on the organization's decision. Some examples are MultiChain and Blockstack.
- Consortium: The third and most popular type of blockchain is a consortium. It's controlled by a consortium of members. These members are from top companies who came forward to make changes to blockchain for specific purposes. So, only a predefined set of nodes have access to write data or blocks to the blockchain. Some examples are Ripple, R3, Hyperledger 1.0, and Hyperledger 2.0.
Now, let's look at the difference between a permission and a permissionless blockchain implementation, shown in the following diagram:
Let's explore the difference between a permission and a permissionless blockchain as follows:
- Permissionless blockchain: Since the network is open, it's called a permissionless blockchain. Anybody can participate and contribute to the consensus. Now, the consensus varies in all the nodes that are participating in blockchain mining. Even one individual can set up the mining and start running the blockchain implementation. It's a public network, that is available for everyone to play a specific role, or rather, a generic role.
- Permission blockchain: It restricts the actors who can contribute to the consensus of the system state and provides role-based access to the blockchain for actors who are participating in the implementation.
The following table depicts the difference between the public and private blockchains:
So, let's take a look at the following features that are being provided by these implementations:
- Access: In terms of access, the public blockchain is open with read and write access to the database for any node that is running the blockchain implementation. On the other hand, the private blockchain needs permission to have read or write access to the database.
- Speed: In terms of speed, the public blockchain is slower. The reason for this is that it's public and the volume, size, and number of nodes that participate in the blockchain are higher, so it takes some time to capture the transactions. When it comes to private blockchains, they have specific roles which are played by limited nodes that help to make the implementation of the transaction faster.
- Security: The way the public blockchain works is on the premise of PoW or the Proof of Stake (PoS), while the security in the private blockchain is provided by preapproved participants that can only participate or contribute to the blockchain.
- Identity: In terms of the public blockchain, it provides anonymity or a pseudonym to the actors contributing to the blockchain. But, in the private blockchain, the identity is known by including each of the nodes that are participating.
- Assets: The public provides native assets, even in cryptocurrencies like Bitcoin, Ether, and all Altcoins. But, in the private blockchain, the purpose is to use any asset, thus providing a feature that tracks the assets on the blockchain.
Here is an example. Let's say two organizations agree to execute some work, and transfer or exchange assets or currency after the transition gets executed. So, the agreement that they make can be an asset on the private blockchain.
Let's take a look at the various platforms that have implementations of blockchain:
- Hyperledger: The first is Hyperledger, which is consortium-built. It's an open source collaborative effort created to advance cross-industry blockchain technologies. It's a very famous platform.
- Ethereum: The second is Ethereum or Enterprise Ethereum, which has two variants. The first is an open blockchain platform that lets anyone build and use decentralized applications that run on blockchain technology. Like Bitcoin, no one controls or owns Ethereum, and Enterprise Ethereum is a similar consortium to Hyperledger.
- IBM Bluemix: This is a Platform as a Service (PaaS) that's built on top of the Hyperledger project, and it offers additional security and infrastructure facilities for enterprises to use the Hyperledger blockchain implementation for their own purposes.
- MultiChain: This is a platform for the creation and deployment of private blockchains or permission blockchains, either within or between organizations.
- Corda: It's a distributed ledger platform with a pluggable consensus that gives flexibility for enterprises to plug in their own consensus or smart contracts for their own purposes.
- Openchain: This is a well suited for organizations wishing to issue and manage digital assets. It takes a different approach than the standard Bitcoin approach in implementing blockchain as it works on the partitioned consensus system in which every Openchain instance has only one authority to verify transactions, based on the assets that are being exchanged.
These are just a few examples of blockchain implementations; other than these, there are numerous implementations of platforms available today.
In this chapter, we began with the fundamentals of cryptography and cryptocurrency. We also got an understanding of its workings and techniques. Then we moved on to an introduction to Bitcoin, its history, and the various wallets and alternate currency available apart from Bitcoin. We then looked at what blockchain is, its structure, features, and the various types available. This chapter has given us a bigger picture of what makes Bitcoin and blockchain the next major innovation after the internet.
In the next chapter, we will focus on decentralization. You will become familiar with decentralization, which sits at the core of blockchain as an innovation, and you will get insights into how decentralization works, what can be decentralized, and what its impact on the world is. We will also go through the pros and cons of decentralization, giving you a holistic overview of it. Stay tuned!