Cryptography is the foundation of blockchain. It is widely used throughout the blockchain domain. Hash functions are used to connect blocks in a tamper proof way and also in digitally signing documents. Bitcoin uses SHA256 hashing algorithm for mining. The ideal properties of a hash function:
- Collition free - The time taken to generate same hash is proportional to number of bits in the hash.
- Puzzle friendly- It is difficult to find k such that Z = H(M || k), where Z and M are known and M is concatenated with k.
- Deterministic- Always yields the same value on same input.
- Hiding- Nearly impossible to guess the input value looking at the output hash.
In Public Key Encryption or Public Key Infrastructure (PKI), the encryption of messages is done with the public key and decryption is done using the private key.
A cryptographic hash pointer is the location to which some information is stored and is also the hash of the information stored. If m is the information and h(m) is its hash, then h(m) is the cryptographic hash pointer to location where m is stored.
This way we can link two blocks of data in a tamper proof way, because if data in previous block is changed then the hash will change and as a result the link will be broken. We can make this tampering computationally difficult by concatenating a nonce to the data in the block and then putting certain restriction to the output hash, like the output hash should has x number of leading zeroes.
A merkle tree works in a similar fashion where each parent node is hash of its child nodes. In the world of blockchain, each such nodes is a transaction and each block of the blockchain contains the root hash, nonce, previous hash and the hash of the entire current block. See the image below:-
Blockchain is a hashchain which is connected by hash pointers.
Cryptography is sending of messages in an encrypted way so that unwanted recipients can’t read it.
There are two types of cryptography:
- Symmetric key cryptography- Same key for encryption and decryption.
- Public key Cryptography - Different keys for encryption and decryption.
For secure communication using public key cryptography, we encrypt the message with the public key of the receiver then send it to him. Since the message can only be decrypted with the private key of the same receiver hence we can be assured that only the intended receiver can only read it. Eg, RSA algorithm.
A Digital Signature is a digital code sent with the document which verify
i. The content is authenticated
ii. Identity of the sender
iii. Prevent non-repudiation- Sender can’t deny having sent something.
The sender hashes the message to be sent to generate a message digest. This message digest is encrypted using the sender’s private key. This is the digital signature. Now this signature is sent along with the message.
The receiver can verify the digital signature by, generating the hash of the message received and comparing it with the decrypted version of the received signature using the public key of the sender.
This ensures the above mentioned three points.
In blockchain, digital signatures are used for verifying transactions.