Hey, everyone, welcome back to the course.
So after going through blockchain fundamentals, now let's understand the cryptography and the blockchain
algorithms which are running behind blockchain in general terms.
We have already mentioned that blockchain use cryptography in its core to define the cryptography.
You can take up two categories.
One is the symmetric cryptography and the other one is asymmetric cryptography.
So let's first understand symmetric and asymmetric cryptography, and then we will give you the points
on which different algorithms are employed by blockchain.
So this is an overview diagram for the symmetric cryptography algorithm.
And as you can see in the diagram, this algorithm employs the same key between both the parties.
So the person who is sending the data and the person who is receiving the data are going to use the
same secret key.
This key is used to encrypt and decrypt the data at both the ends.
Symmetric cryptography is a very straightforward encryption algorithm, which only employs one single
secret key for encrypting and decrypting the data.
The assumption about this cryptography algorithm is that both sender and receiver should know this secret
key, or there should be some process established through which this secret key is shared between both
the participants.
Some of the examples of symmetric cryptography are blowfish s, RC four, RC, five, RC six, etcetera.
As we are using the same key between both the participants, this makes the algorithm much more faster
compared to the other types of cryptography algorithm, but it also increases the possibility of man
in middle because if we want to use this cryptography, we need to devise a process through which we
are going to share the key.
And this sharing of the key could be an unsecure process as we could be sending these keys through a
open channel which is accessed by everyone.
Such a problem of sharing the keys between parties is tackled by the asymmetric cryptography.
Now let's go and look some of the key points about asymmetric cryptography.
So this is an overview diagram for a symmetric cryptography.
And as you can see, when you use the asymmetric cryptography algorithm, you always have the key pair.
You will have a public key and also you will have a secret key, which is also called private key.
The private key is the one which is stored at your end.
And the public key is the one which is distributed to everyone else.
People can use public key to decrypt the data which you have sent across.
Also, it works in a vice versa phase where you can decrypt the data which people have encrypted using
your public key by employing your secret key.
The generation of keys in asymmetric cryptography algorithm is a one way process, so it's possible
to generate public key from private key, but the vice versa process is nearly impossible.
Even a supercomputer might take multi-million years to generate the private key from the public key.
This is the major type of algorithm which is used behind blockchain to and within the blockchain world.
We employ such an algorithm to make sure that participants are able to authenticate themselves to verifiers
and make sure that the whole network is secure.
Now, as you know, asymmetric cryptography employs public key pairs, so the exchange of keys over
the Internet is much more simpler than the symmetric key algorithm.
It provisions you two keys.
One is a private key, which is a secure secret shared at your end, and the other is the public key,
which can be made freely available to anyone who wants to send data or transaction across to you.
The message could be encrypted using public key and decrypted on the other end using private key.
And also you can do the vice versa process where you use private key to encrypt the message and public
key to decrypt the message.
Some of the examples for asymmetric cryptography algorithms are RSA, DSA and ECC algorithm.
As we know, this is a public key cryptography algorithm, so there is no additional security available
on the public keys which are being passed over the Internet.
Moreover, as we are using two different keys, thus the performance is less compared to the symmetric
algorithm.
But by employing two different keys, we are making sure that the malicious persons are not able to
misuse the keys.
This is the major algorithm which we use behind blockchain.
Also, the private key and the public address generated in blockchain employs a symmetric cryptography
algorithm.
Although we are not directly using the private and the public key.
These keys goes through different transformation and checksums to generate the final private key and
public address which are accepted by blockchain.
Now let's look at one of the example of a symmetric cryptography algorithm, which is ecdsa algorithm.
This algorithm is heavily utilized with Bitcoin and Ethereum blockchains and other blockchain platforms
are also using these kind of algorithms in some other capacities.
So this is just an overview diagram for each algorithm.
Algorithm stands for Elliptic Curve Digital Signature Algorithm.
This algorithm helps you to plot a curve or define a quadratic equation which can be plotted on x and
Y axis.
There are different types of curves which are supported by Ecdsa algorithm.
Some of the curves are also accepted standards throughout the cryptographic world.
In very simple terms, if we want to explain this algorithm, we can take a straight line which is passing
through the curve.
This straight line helps us to plot two points A and B a point is taken as the private key and thus
defines the x and Y coordinate on top of the curve.
Whereas B point is just the mirror point for A and it is taken as the public key and also defines the
x and Y coordinate on the curve.
So in this way we plot two points, which acts as a private key and a public key on top of the curve
which is taken for the algorithm.
Ecdsa algorithm Encode is just an variant of digital signature algorithm and it utilizes elliptic curve
cryptography.
This algorithm is used heavily with Bitcoin and it ensure that the funds are only spent by the rightful
owners.
This algorithm is very secure algorithm compared to the other algorithms which are present under the
asymmetric cryptographic umbrella.
If you see an example, the public key for ecdsa algorithm is almost equal to 160 bits, but the performance
and security provided by this public key is almost equal to the 1024 bits DSA public key, which is
very huge.
So if we employ DSA 1024 bit public key, that will actually reduce our performance.
But we are able to get the same security by employing 160 bit key with Ecdsa algorithm.
The signature size for both the public keys will be same.
That's why Ecdsa algorithm is heavily favored in terms of blockchain and it is utilized to generate
the private key and public addresses with different blockchain platforms.
Another important algorithm which we use with blockchain is hashing function.
Hashing function is used to authenticate and verify the data which is sent across the blockchain.
If you see the example of Bitcoin and Ethereum, we use hashing in the sense of while we are taking
up the transaction or while we are generating the block.
Once the block has been formed, different components of the block has been performed.
We have added the block, height, block time and other components.
Finally, we pass this block header which has been generated to a hashing function in Bitcoin.
We use Sha 256 hashing function and thus a 256 bit output is generated through the hashing function
With Bitcoin.
We also employ a nonce value with the block header, thus making sure the hash which is generated through
the Sha256 function has some leading zeros associated with it as per the difficulty of the blockchain.
In general terms, if we want to define a hashing function, we can say that hashing function is used
to convert a variable length of data into a fixed length.
It doesn't matter what kind of size of data you are providing into the hashing function, be it one
byte or be it one terabyte, the output will always be the same.
For example, if we use Sha256, the output is always going to be 256 bits or 32 bytes.
So if you see the example here, how we can use hashing function.
So here we have two users user one and user two who wants to share the file between each other, but
they also want to make sure that there is no attacker in the middle who is going to modify this file.
So what we do is that we take the hash of this file so it doesn't matter what the size of this file
is, the hash will always be 256 bit.
If we are using Sha256 and other different algorithms will have other different output sizes.
Then we take this hash and we pass this hash across the network.
We can either pass in directly the hash across the network or we can also do a checksum of the hash.
So if you see that every data which is being shared across the network is in the form of bits which
are zeros and ones.
So we can perform a checksum to make sure that there is a single some output coming from the total hash.
This hash or the checksum can be passed across to the second user.
Once the second user receives the actual file, they can verify the contents of the file by again taking
the hash of this same file.
So if both the hashes match which is coming from the originator and which is generated by the user themselves,
that means the file has not been altered or tampered with and the file is correct.
Similarly, he can also map it using the checksum.
So as we mentioned, hash is a function which converts a variable length of data into a fixed length.
Hash is a one way function.
So you can take up the hash of the data, but you cannot generate the original data back from the hash.
So if you take an example of a piece of paper and keep on squeezing that piece of paper until you are
left with a round ball of trash and it's almost impossible to read the content.
That's how the hash function also works.
It takes the large data and it keeps on squeezing the data to a small format, but there is no way to
actually get back the original data.
The hashing algorithm is heavily used behind blockchain.
We use Sha256 algorithm when we are generating the private key and also we use Sha256 when we are creating
the blocks.
On the other hand, we use Richmond 160 algorithm which has an output of 160 bits.
While we are generating the blockchain addresses, there are some important key points while you are
taking the hash function, The first important key point is that the hash function which we are employing
should be fast for any kind of data.
The second point is that the hash which is taken cannot be regenerated back to the original message.
The third point is that any data which is passed through the hash function should not have collision.
It should not be that two different datas have the same hash generated.
The fourth important key point is that every change inside the message even be a small bit should completely
change the hash value.
So these four key important points make the hash function acceptable and this hash function inside blockchain
makes sure that we are able to authenticate and verify data at different ends of the blockchain network.
Another major algorithm which we use with blockchain is Merkle Tree.
Merkle Tree is used with Bitcoin and Ethereum to make sure that we have a single root through which
every transaction can be verified for the complete block.
So as an verifier or a miner, you don't need to go through each transaction.
You can only go on and verify the Merkle root and you will be able to verify all the transactions inside
a block.
The employment of Merkle root depends upon different blockchain platforms.
Some other blockchain platforms, like Corda, uses Merkle root for the transaction itself, so a single
transaction is divided inside a merkle root where each leaf nodes acts as one of the components of the
transaction, and that transaction is taken up until the final hash root is generated for the transaction.
This makes sure that only some of the components are accessible to different parties for the transaction.
A normal Merkle tree works where you have even number of nodes and you keep on taking the hashes of
the nodes.
For example, if you see in the diagram, you have transaction A, B, C and D, so you take the hash
of the transaction A and also you take the hash of the transaction B, then you combine this both hashes
together and finally take a cumulative hash of transaction A and B to generate a transaction hash of
a B.
Similarly, you do it for CD.
Also you perform the hash of C, hash of D, cumulated together to form the hash of CD.
And we keep on continuously do this process until and unless we arrive at one final hash value which
can be used to verify the transactions.
So in a summary, Merkle tree is used to verify all the transactions which are present with the block.
Merkle tree is employed in different fashions by different blockchain platforms.
You can also use Merkle Tree to make sure that only some part of the transaction is available to different
participants.
Merkle Tree are even trees, so they have even leaf nodes.
If your block doesn't have even nodes, then the final node or the transaction is replicated to make
it a even tree.
Merkle Trees always work in a bottom up approach where you start with the leaf nodes which are on the
bottom.
Keep on taking the hashes until you reach one single Merkle root.
Each leaf node inside the Merkle tree is the hash of transactional data and each non-leaf node inside
the Merkle tree is the hash of the previous hashes.
So now we have gone through different cryptographic algorithms and blockchain algorithms which are employed
with blockchain, but how all these fit together with blockchain.
You might have some idea on which algorithm is used, where just to accumulate all the things which
we have learned together.
You will see that Ecdsa algorithm, which is part of asymmetric cryptography, is used for generating
private key and public addresses for blockchain.
Private key is used for the ownership, and public key is used to verify the ownership of the data.
The Merkle root, which we talked about, is applied inside a block where all the transactions are formed
in such a way that by using one single root hash you can verify all the transactions inside a block,
thus saving time for the verifiers or miners.
Merkle Trees also allows the users to verify that their transactions have been included in the block
without downloading the entire blockchain.
Hashing algorithms, which we discussed provide security and makes the data immutable.
On top of blockchain, if someone tries to even change one bit of data in any past block, then the
hash output of that data block will be different.
Making sure that we are able to verify that there is some tampering or modification happening inside
our network.
So that was about blockchain and cryptographic algorithms, which we use within blockchain.
Why these cryptographic algorithms are important.
You need to make sure that while you are designing your blockchain system, you employ the security
and performance as per the algorithms available for your disposal.
It should be that you are employing an algorithm which is secure and still provides the performance.
We have seen the examples of few of the cryptographic algorithms which have broken before.
So as a blockchain architect you need to make sure that you are using secure cryptographic algorithm
and also you need to make sure that these algorithm function together to integrate the whole blockchain
network in a secure fashion.
In the next section, we are going to learn about how blockchain works with the help of an example,
and we are also going to see the process of hashing and how hashing makes sure that the data is securely
present over the blockchain.
If you have any queries, feel free to contact us at info at the rate v2 blocks.com.