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:
- Account addresses
- Digital Signatures
- Transaction Hash
- State Hash
- Receipt Hash
- Block Header Hash
To manage the integrity of the blockchain transaction
- Secure and Unique Account Addresses
- Authorization of the transaction by the sender through digital signing
- 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:
- 256 bit random number is generated and designated as private key
- It is usually kept secure using a password or passphrase
- 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)
- Hashing technique is applied to public key to obtain an account address (20 bytes)
Transaction for transferring assets has to be:
- Authorized
- Non - repudiable
- Unmodifiable
Verification of a blockchain transaction
- Find the hash of the data fields of the transaction
- 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.
- 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.
- Then they compare the re-computed hash and the hash received with the signature.
- If there is a match, accept the transaction, otherwise, reject it.
- 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
- Block header
- Transactions including the transaction hash
- Transaction Root
- 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:
- Verification of the integrity of the block and the transaction
- 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.