# How to achieve Proof of Work?

According to Wikipedia(2019), A Proof-of-Work (PoWsystem is an economic measure to deter denial of service attacks and other service abuses such as spam on a network by requiring some work from the service requester, usually meaning processing time by a computer. This protocol is adopted by the Bitcoin to enhance the security of the Bitcoin network.

In order to hack the Bitcoin network, the attacker needs to take over 51% of the nodes. However, it is almost impossible to achieve this task as it requires a tremendous amount of computational power and astronomical cost. Proof of work is attained via an activity called mining in Bitcoin and other cryptocurrencies.

Mining involves the process of producing a hash whose value is less than the target value. When this hash has been found, it is called a valid hash and hence proof of work is achieved.

The mining algorithm uses a counter known as the nonce to generate the hash using the SHA256 cryptographic function. A hash algorithm always produces the same arbitrary length data given the same inputs. It is impossible to compute the same hash with two different inputs. It is also impossible to predict the output of any given data in advance. The other important value in proof-of-work is difficulty. Difficulty is a measure of how difficult it is to find a hash below a given target.

The value of nonce is initialized to 0. Mining is finding the nonce, the only input that changes every time we run the hash function. The goal is to find a value for the nonce that will result in the hash lower than the current target. The formula to compute the current target is

Current target= maximum target ÷ difficulty

Therefre, the mining node might need to try billions or trillions of nonce values before it gets a valid hash. As you can see, mining is like playing the slot machine, there is no way to predict when can you strike a jackpot.

It is very easy to prove that the nonce found indeed produces a valid hash. All the information are available, everyone in the network can run the hash function and confirm if the hash is valid or not. Because it is also impossible to predict what the nonce will be, it also acts as a proof that the miner has indeed achieved Proof-of-Work.

### Calculation of the Valid Hash

(The numbers are based on block #540909)

The formula to calculate the current target of the block is

Current target= maximum target ÷ difficulty

Maximum target is set to

`0x00000000FFFF0000000000000000000000000000000000000000000000000000`

This is a hexadecimal number. After conversion to a decimal number

Maximum target=

`26959946667150639794667015087019630673637144422540572481103610249215`

Difficulty is (as given in the block)

`7019199231177.17`

Therefore,
Current target=

`(26959946667150639794667015087019630673637144422540572481103610249215)÷7019199231177.17`

3.84089×1054

The hash of the block is

`00000000000000000000ef17668e407e78c5a247f731b1138ad16f5bf79f1c0d`

After converted to a decimal number, the value is as follows:

`89454716205495239548871016846060264708718561584946189`

The hash value is approximately   8.95×1051

Clearly, the hash value is less than the current target, therefore it is a valid hash.

You can view mining simulation by clicking the following link:

http://javascript-tutor.net/jSample/mining.html