Blockchain and Cryptography

 Blockchain And Cryptography

"Understanding the wizardry of cryptography and its relation with blockchain"



Hi, My name is Mubashir Ali Baig and welcome to the part 2 of my blockchain series. In this post i'll try to explain the concepts of cryptography and its importance in the domain of blockchain.

Topics:

  • What is cryptography?
  • Symmetric vs asymmetric cryptography
  • Digital Signatures
  • Under the hood of a block in a chain
  • Nonce and Miners
  • What is a hash?
  • Merkle root

What is cryptography?



"Cryptography is the process of concealing information such that it can be only converted into its original form if it is received by its intended owner or recipient"

There are two major operations in cryptography.
  1. Encryption: Conversion of understandable data into an in-comprehensive format
  2. Decryption: Conversion of in-comprehensive data back to its original understandable format

Types of Cryptography:

  • Symmetric Cryptography
  • Asymmetric Cryptography

Symmetric cryptography: 


In symmetric cryptography, the data is encrypted using a single secret key and by key i mean some sort of special code which could change the text, for example if the key is a number let's say 1 and this number is used to move each character in text one place ahead in the ASCII range on the basis of their current position (Anas => Bobt). Now in order to decrypt "Bobt" back to "Anas", we will still use the key=1 but this time we will subtract the ASCII position such that each character moves back 1 place from its original position. Although symmetric cryptography does successfully encrypts the data but there is one major flaw in this approach, and that is if someone could guess or find that secret key, then they can easily exploit the privacy as that single key can both encrypt and decrypt the data.

Asymmetric Cryptography:



Asymmetric cryptography eliminates the possibility of privacy breach by adding public and private keys into the process of encryption and decryption. In simple words a public key can be co-related with the email of a user and private key as the password of that user, now people can be identified and searched using their public keys aka email but their accounts can be only be used if someone logins by providing both their public and private keys to the system, if either one of them is incorrect then their accounts will remain protected from the breach. Considering the scenario of text encryption with asymmetric cryptography, the sender encrypts the message with the public key of the recipient and then this message can only be decrypted by using the private key of the recipient. In this way even if someone guesses one key then they still have to figure out the other key to execute the successful breach.

Digital Signatures: 


Signatures have been the most integral of part of the verification of information travelling from one point to another. In the history there have been various ways to make signatures like symbols and written patterns but those signatures are easy to forge. So in order to eliminate the possibility of forgery digital signatures were introduced which uses asymmetric keys criteria. To sign a message,the sender signs it using their private key. Now to verify if that message was actually sent from the legitimate sender, the receiver inputs the senders public key along with the signed message into a verification program which then verifies it by comparing the two parameters.

Under the hood of a block in a blockchain:


The above figure represents a typical bitcoin blockchain node, here we have three blocks linked together with the hash of the previous block. Each block consists of block header, state and a transaction list. Inside the header there resides the metadata of that block. Let's go through some important elements inside the block header.

Nonce And Miners: 

Miners are those participants of the chain who use their computational power in order to mine the new blocks. In return they earn reward which varies from chain to chain depending upon the environment. The process of mining the new blocks is rather painstaking as the miners have to compete with each other to solve some mathematical puzzles using their computational resources. The criteria of success depends upon the correctness of the result of the puzzle which is known as nonce. Nonce can be defined as "Number used only once". Each time a random number which does not repeats itself is the answer of the puzzle and miners have to "guess" this number by trial and error essentially brute forcing their way to the real answer.

What is a hash?

A hash is any fixed sized value that represents any arbitrary size of data when passed through a hash function. The capacity of compression varies from algorithm to algorithm. The main purpose of creating a hash of any data is to represent it in an encrypted fixed sized string. You can hash almost anything from jpg files to binary media files. In simple words, a hash is a short representation of a data in arbitrary size. A hash can not be retraced back to the original data and even a minimal change in that data will result in the change of its hash string. This property of changing hash is called avalanche effect, and it is used in the applications where uniqueness of any entity is required so that nobody can impersonate.



The usage of hashing in blockchain is due to its uniqueness. As we know that each block is chained to the previous block through the hash of that block which means each block knows exactly what was the previous block using "the hash of N-1". So if someone tries to manipulate the identity of one block, it would create a ripple of change in the hash record of the successor block which would be denied as the successor block does not recognize the newly introduced block in place of its predecessor. Hash is also used to identify the transactions in each block, so if one block has 5 transactions in its transaction list, then each transaction can be verified using the hash instead of verifying all the data inside them. So if someone in future tries to change the transaction data in a certain transaction, the hash will be different and it will instantly be denied from the merkle tree which i'll explain next in this post.

Merkle Tree And Merkle Root:


Merkle tree is a binary tree data structure in which the data is stored in an inverted tree like structure such that the top most data entity is called root as it is singular and then this root has two children nodes. Except of root each node has a parent node and each child has its own two children (explains why we call it binary). The child or node which does not have its own two children nodes are called leafs and they are at the bottom of the tree. The specialty of a binary merkle tree is that it uses the avalanche property of hashes to generate the ultimate and singular proof of verification in the form of the root of the merkle tree, it is done by generating a new hash of the combined hashes of the immediate children and storing this newly generated hash in the parent node of those children nodes. So each parent nodes hash is the product of the hashes of its children nodes and so ultimately the hash generated for the root of the tree is the representation of the identity of all the nodes. 

Now the advantage of going through all of this hassle is that if the data even one node is tampered than it will trigger the avalanche of the change in the hashes of all the above parent nodes which will then certainly change the hash of the root, which then can trigger an alarm that there has been a discrepancy in the record ultimately causing the changes to be discarded and thus providing the system with transparency of the data. Oh and by the way, the hash at the root of the tree is known as Merkle Root.



Transparency is the exact reason why merkle root is an integral part of the blockchain block headers. As i explained earlier that each block maintains a list of all the transactions happened on that block. So to keep the integrity of the transactions, the hashes of each transaction are placed as the leafs in the form of pairs in the merkle tree and then the parent hashes are generated from these leaves successively until a root node is generated which does not have a further parent. Now hash on the root or root hash is the ultimate proof of all the transactions happened inside that block, so this root hash is saved in the block header as the meta data information of the transactions. Each time a new transaction is added, the tree initiates the formation of a new merkle root hash using the avalanche feature of the hashing algorithm, hence achieving the transparency of data which protects from the theft and the attempts of modification of even a single transaction data point.

I hope i've explained the basics of cryptography and its role in blockchain along with the overview of a block structure and their importance in this post. For any queries, leave a comment below or hit me up on Email: mubashirbaig98@gmail.com




   























Comments

Post a Comment

Popular posts from this blog

Introduction To Blockchain