Showing posts with label hashing. Show all posts
Showing posts with label hashing. Show all posts

Tuesday, 6 November 2018

Hashing in blockchain

SHA-3, SHA-256, and Keccak-256 are important hashing algorithms in BlockChains

Tree hashing is used wherever a variable number of arguments is involved in the block.

  
In Ethereum, hashing is used to generate:
  1. Account addresses
  2. Digital Signatures
  3. Transaction Hash
  4. State Hash
  5. Receipt Hash
  6. Block Header Hash


To manage the integrity of the blockchain transaction
  1. Secure and Unique Account Addresses
  2. Authorization of the transaction by the sender through digital signing
  3. Verifying that the content of the transaction is not modified

Generating address of the accounts
Address of the blockchain accounts are generated through use of public key-private key pair
Here are the steps involved:
  1. 256 bit random number is generated and designated as private key
    • It is usually kept secure using a password or passphrase
  2. ECC algorithm is applied to the private key to generate a unique public key. This completes the private-public key pair. (ECC: Elliptic-curve cryptography)
  3. Hashing technique is applied to public key to obtain an account address (20 bytes)

Transaction for transferring assets has to be:
  1. Authorized
  2. Non - repudiable
  3. Unmodifiable

Verification of a blockchain transaction
  1. Find the hash of the data fields of the transaction
  2. Encrypt the hash using the private key of the participant originating the transaction, thus, digitally signing the transaction to authorize and making the transaction as non-repudiable.
  3. This hash is added to the transaction and other participants can verify it by decrypting it using the public key of the sender of the transaction and recomputing the hash of the transaction.
  4. Then they compare the re-computed hash and the hash received with the signature.
  5. If there is a match, accept the transaction, otherwise, reject it.
  6. Note that this is not the only verification required for the transaction acceptance. Transaction timestamp, nonce, account balances and sufficiency of fees are also verified.

What is non-repudiable?
What is nonce?

Securing the blockchain
Main components of the Ethereum Block are
  1. Block header
  2. Transactions including the transaction hash
  3. Transaction Root
  4. State variables: State hash and State root


In Ethereum Block hash is a block of all the elements in the block header including the transaction root and state root hashes. It is computed by applying a variant of the SHA-3 algorithm called Keccuk on all the items of the block header.

A typical Bitcoin block has about 2000 transactions
A typical Ethereum block has about 100 transactions



What is the advantage of Tree hashing over flat hashing?
If any transaction has to be verified, only one pathway to the tree has to be checked. You don't have to go through the entire set of transactions.

So, smart contract execution in Ethereum results in state transitions. Every state change requires state root (hash) re-computation. Instead of re-computing the hash for all set of states, Only the affected path in the Merkle Tree needs to be recomputed and not the entire tree path.


Block hash computation
Block hash in Ethereum is computed by first computing the State root hash,  Transaction root hash and the Receipt root hash shown at the bottom row of the block header diagram.

These roots and all the other items in the header are hashed together along with the variable nonce, using Keccak-256 to solve the Pow (Proof of Work) puzzle.
Block Hash solves two important purposes:
  1. Verification of the integrity of the block and the transaction
  2. Formation of the "Chain link" by embedding the previous block hash in the current block header.

If any participant node tampers with the block, its hash value changes resulting in the mismatch of the hash values and rendering the local chain of the blocks into an invalid state. Any future blocks proposed by the node will be rejected by other miners due to hash mismatch. This enforces the immutability of the chain.

Public key Cryptography

For blockchains validation and verification, two techniques are used predominantly.
These are Hashing and Asymmetric key Encryption.

Blockchain security domain, 4 topics:
  1. Public key cryptography
  2. Secure Hashing
  3. Transaction Integrity
  4. Block Integrity

We will describe the concept of asymmetric key encryption.
Then hashing and what hashing algorithms are used by Blockchain. Then explain Techniques which use these algorithms to manage the integrity of the transaction and blockchain

Public Key Cryptography

Blockchain participants are not known to each other. Participants can join and leave the chain as they wish. They cannot be identified with conventional means like Driver's License.  In this context, how do you identify the participants?
How do you authorize the transactions? How do you detect the forged or faulty transactions?

This can be accomplished using public key cryptography algorithm.
Let us first understand simple key encryption. The same key is used for encryption and decryption, therefore it is called simple key encryption.
For example: Caesar Encryption
Alphabets in a message are shifted by a fixed number of positions. This number is called a key.
Consider the message: I AM GOING
Encrypted: G CO IQGPI
Letters shifted by 2 positions. Hence the key is 2.

Drawbacks: 1. Even the encryption key or mechanism is much more complex, it is easier to derive the secret key from the encrypted data.
  1. The key distribution to other participants is a challenge.
These challenges grow in magnitude in case of blockchains where participants are unknown to each other.


Solution to these issues: Public key encryption
  1. Two keys instead of a single key. These keys form a public key and private key pair.
  2. Public keys are published. Private keys are kept safe and secret using a passphrase.
  3. When a data is encrypted with a private key, it can be decrypted by a corresponding public key and vice versa.

Example of public key encryption: Popular implementation: RSA algorithm (Rivest Shamir Adelman)
It is used in an application for password less user authentication like in Amazon Virtual machine access.

But Blockchains need more secure algorithms. So it uses  Elliptic Curve Cryptography (ECC). Both Bitcoin and Ethereum use these algorithms to generate the blockchain. ECC is stronger than RSA.
256 bits ECC key pair is ~ (equivalent in strength) to 3072 bits RSA key pair