Describe hash functions with your own words
Hash function is like a one-way street. A course of action that cannot be undone or does not lead to reciprocation.
How are hash functions used in cryptocurrencies like bitcoin?
Bitcoin’s proof of work algorithm is based on SHA-256. Using this, Bitcoin miners solve computationally difficult math problems to add blocks into the blockchain.
Bitcoin blocks are added by verifying the hashes on a lottery basis. Yes, a lottery!! Let see how a miner wins the lottery…
To successfully mine a block, a miner needs to hash the block’s header in such a way that it is less than or equal to the “target”.
The target is that the SHA-256 hash of a block’s header must be a 256-bit alphanumeric string, and must start with 18 zeros. The target changes as the difficulty change every 2016 blocks.
And the miners arrive at this particular hash (or target) by varying a small portion of the block’s headers, which is called a “nonce.” A nonce always starts with “0” and is incremented every time for obtaining the required hash (or target).
Since the varying of the nonce is hit and miss, the chances of getting this particular hash (or target), which starts with these many zeros, is very low. Therefore, many attempts must be made by a miner by varying the nonce.
And this requires an enormous amount of computational power and hardware resources which proves that a large amount of work is carried out before mining any individual block. That’s why this is called “proof-of-work “.
And in this way, whichever miner first obtains the correct Bitcoin Hash will win the lottery and get the block reward of 6.25 BTC
What does it mean when we say that hash functions need to be collision resistant?
In practice, hash functions are used for “digesting” large data. For example, if you want to check the validity of a large file (potentially much larger than a few megabytes), you can check the hash value of that file with the expected hash. Therefore, it is desirable (especially for cryptographic hash functions) that the function is collision resistant. That is, it should be “hard” to find two inputs m1 and m2 for hash function h such that h(m1) = h(m2). Most modern hash functions hope to achieve security level of 264 or better, which means that the attacker needs to test more than 264 different inputs to find a collision. Unfortunately, MD4 and MD5 aimed to provide 264 security, but has been shown to be broken using 26 and 237 inputs respectively. SHA-1 aimed to provide 280 security, but has been shown (at least theoretically) to be no more than 261 security.