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.

No comments:

Post a Comment