A Bit of Cryptography
A Bit of Cryptography
Blockchain technology gained popularity due to the fact that its integrity can't easily
be compromised. A compromised blockchain can be recognized for what it is, and
rejected quite easily by anyone in a network. This integrity is achieved by
cryptography, which is what binds the blocks together; we'll study this idea of
cryptography in Chapter 2, A Bit of Cryptography.
Blockchain's promise of providing such robust integrity is what eventually paved the
way for the idea of sharing chains of data in untrusted peer-to-peer (P2P)
networks. Validation of the blocks in a blockchain is what makes sure that a
blockchain has a valid global state that can be accepted by everyone. Due to a
blockchain's ability to share information in an open P2P network without any central
authority governing it, the technology can have many different applications;
however, the technology could not simply just be deployed to these applications
immediately without any troubleshooting. Although blockchain technology, from the
beginning, had a huge role to play in the decentralization of applications, it still
faced several challenges with regards to its application in trustless environments.
One of the biggest challenges was keeping a blockchain consistent across all the
participants of a P2P network. This was solved by creating a consensus algorithm,
which agrees on how the blocks should be appended to grow the chain in a
trustless environment.
The term blockchain actually entails a number of concepts, including P2P network
management, consensus mechanism, and more, all contributing to the creation of a
decentralized application.
Hashing in blockchain
blocks that are chained together to form an open ledger by using cryptography as a key
ingredient. Each block in the blockchain is given an identity to mark that block as unique, and
this is achieved by using the hash functions that will generate a digest for that block. The
collision resistance property of the cryptographic hash function, as mentioned in the previous
chapter, Chapter 2, A Bit of Cryptography, ensures that it is infeasible to find two blocks that
will result in the same hash value. As a result, the hash function guarantees the uniqueness of
When a new block is created, it will backreference the previous block using the digest of the
previous block, thus linking that block to the blockchain. Modifying any of the blocks would
change the identity of that block due to the new hash value. As a result, this would break the
chain, as one of the block references will be invalid due to the newly generated hash value.
Therefore, it's infeasible to modify a block such that it generates the same hash value as
before. This is due to the pre-image resistance property of the cryptographic hash function,
which ensures that the data of the blocks cannot be predicted even if we possess the hash
value. This is why, once a chain of blocks is created, the integrity of the chain is ensured as
each block references the previous block. The only way to modify the data of a block is by
modifying all the subsequent blocks by updating its reference to the previous block:
Figure 3.1: Linking blocks using hashes from A Peer-to-Peer Electronic Cash System, S.
Nakamoto
The preceding Figure 3.1 shows the design of blockchain from the original paper, Bitcoin:A
Peer-to-Peer Electronic Cash System, by Satoshi Nakamoto, the creator of the original
reference implementation of Bitcoin. It shows that each block's hash value is affected by the
value of the previous block's hash value, thereby linking each block in the blockchain. Anyone
who holds a copy of the blockchain ledger will be able to verify whether all of the blocks in the
blockchain are valid just by verifying each block's hash with the next block.
Blocks in the blockchain are chained together by referencing the hash values of previous
blocks. SHA256 is the most popular hashing algorithm used in the blockchain platform since it
was used in the Bitcoin implementation. Firstly, we will define the structure and functionality of
the blocks, before finally constructing the blockchain with the help of the hashing algorithm.
Block structure
Let's consider a simple block whose header and data are combined to create a data structure
called Block. Each Block will contain the following: an index, the previous hash, a timestamp,
Copy
class Block(object):
"""A class representing the block for the blockchain"""
The preceding code snippet defines a Python class called Block that has all the basic
attributes of a blockchain block. Usually, a block will contain both a header and a body, with the
header containing metadata about the block. However, the preceding example doesn't
distinguish between the header and the body. A typical blockchain application, such as Bitcoin,
will have a huge set of data that could be in the form of transactions, but in the example, we will
After studying most of the concepts and applications of blockchain technology, it's
crucial to discuss its strengths and weaknesses in order to realize the level of
security that is required. In this chapter, we will be addressing some of the
significant challenges faced by blockchain technology. Along this journey, we'll also
point out the possible attacks you can encounter in the blockchain network and how
they can be prevented.