## What Puzzle is solved by miners?What is Bitcoin cryptographic hash function?How bitcoin mining is done?What is SHA-256?

Everybody tells that bitcoin miners solve some complex mathematical cryptographic puzzle to earn new bitcoins. But only few explains what is that puzzle and how it works. Actually it not a complex puzzle.Miners do some guess work to find the solution. To understand this puzzle , first thing to understand is ‘what is cryptographic hash function?’

### What is Cryptographic Hash Function?

Cryptographic hash function is basically a mathematic function where you take an input or a message put it into the hash function, output appears to a random series of characters. Hash is different from encryption. In encryption encrypted data can be decrypted. Hash is a one way function – it cannot be decrypted back.. So cryptographic hash is preferred for secure transmission of passwords and data validation. Even if some one intercept hashed data, it will be meaning less to them since original data cannot be recovered. It is also useful for checking data loss while transmission. Data can be validated with it’s hash to ensure that correct and full data is received at end point.### Characterisits of a cryptographic hash function are:

1.It must be computationally easy to produce o/p from i/p2.It has to be computationally infeasible. That means it is not easy to find the input for a given output.

3.It has to be deterministic. It means for a the same input , output should be the same all the time.

4.if you change a message slightly, the output hash will be entirely different

5.It has to be infeasible for you to find two messages produce same hash.

### SHA-256 cryptographic Hash in bitcoin mining process

Hash function used in bitcoin is SHA-256(Secure Hash Algorithm). SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.We have discussed about block chain in previous article. In a block there will be block header.Block header contains:

1.Version

2.Timestamp

3.Hash of the previous block

4.Merkele root

5.Difficulty target

Version is the nothing but bitcoin’s current software version.

**Timestamp**is the approximate time at which block is generated.

**Previous block hash**is the Hash of the header of the previous block added to the block chain.

**Merkle root**is basically all the transactions in the block hashed together. It can be viewed as a signature for all the transactions in the block.

**Difficulty target**is to set difficulty level of the cryptographic puzzle, so that it takes 10 min for all bitcoin miners all together to solve the puzzle. It is a 64 bit hash looks like 000000000ad4bcf645…..

When bitcoin mining software try to find out the next block, it will take a block header and hash on them. If generated hash is less than or equal to difficulty target we have found next bitcoin block. It is extra ordinary to get a hash less than difficulty target. So, basically bitcoin mining software has to try billions or trillions of times to find a value less than target function.

But we know that Hash function is deterministic. If we perform hash on the same block header o/p is going to be the same all the time.That is where the role of ‘nounce’ comes.

**Example**:Here are few messages and their SHA-256 Hash.

hello hai

7363150066651935ba511625ab5a651043656b61f6e3930ae5ea1e89bfde9b35

hello hai1

7945f89190d35d8e3db52de7ece85a037559bb060190ff8c908add26de128f96

hello hai2

d4036e82fb36fba4a46bfe96c284b8cb5a1e838372abb435455bba725ba33fd1

hello hai3

45ffcad0a209fb2682049386ffaf32bb22278d02abae61e1018a9a35e53fd7be

hello hai4

398cad261b7743793fb0ad1e20f10eb53f8789aadae0da6c9d1b52ceeaa5e901

hello hai5

8214f44d7d5a0c12c751d0acbc70c8415a380da62c9a320c7cabf8d549eb4920

.

.

.

Hello hai10

0f8b75fade1c8da26c368afe72da279b68e31103ec5e3e30e17758e7a4081f80

__(You yourselves can try this at xorbin SHA 256 calculator)__Look at above example. It took 10 attempt to find a hash of ‘hello hai’ with one ‘0’ in the beginning. In bitcoin environment target hash is a extremely small hexadecimal number starts with 14 zeros. It may look likes 00000000000000adb5674fd67e……..Then you can image how many attempt will be required to find a number starting with 14 zeros and also less than that number.As the times goes on ,difficulty level will increased by appending more number of zeroes in the beginning side. Nounce in this example is 1 to 10.

The bitcoin mining software performs hash on the header if hash is not less than the target , it will increment the ‘nounce’ by 1, and performs hashing again. Actually hashing is done on concatenated value of header and nounce. If it still not valid, again nounce will get incremented by 1. As we have seen when input is modified slightly , output hash is entirely different. This process continues till it finds a hash that is valid and that will become next block in the chain. It will takes trillions of tries to find a valid hash. So , success of mining depends on computing power of the mining system. Higher the computing power ,higher the chance of win.