Proof Of Work Algorithm : Explained

If you have been following the blockchain space you have probably heard of the term proof of work.

In this  article I will try to explain the concept both theoretically and practically in a way that at the end of this piece you should have a good grasp of the concept.

Note that this article was previously posted on my substack.

I. Understanding Hash Functions

To understand POW, one has to learn about hash function, you can skip this part if you already know about hash functions.

Simply put a hash function is a family of functions that take an input data ( can be a string of text, bytes, etc, ... ) and return a fixed length value ( 128 bits, 256 bits, 512 bits, etc, ... )

Note that there are hundreds of hash functions currently in use,  and the most widely used in the blockchain space is the SHA256 , In the example below we  use 2 hash functions  RIPEMD128  and SHA256.

> RIPEMD128_Of("Eddy") 
> bb8535858fed3f9f979367cc0de9f7f6 // RIPEMD128
  
> SHA256_Of("Eddy") 
>29b566345fb23ef6e9097cb2100ba8724d8253a39f0cfee35ca0c8480588449e 

// Since it's function we can even call it twice on the same value ~ Double hashing 

> SHA256_Of(SHA256_Of("Eddy")) // Double hashing 
>00693c845afdfccde0eeffbe9ad149028f019a1c48e139e52ab46d1beea903ea 

Online hash calculator

2. How is POW computed ?

The first step of POW computation is concatenating or adding  together different pieces of the block header data. [1]
The result is a piece of data on which we can perform  hash functions. One important piece of the header data is called nonce which is a 32 bits integer

NOTE : I won't be detailing  block header components  in this piece, the rabbit hole goes so deep that those  can be an articles on their own.

Once we have the block header data, the work can finally start. The way it works is start by :

  • Double hashing the data with SHA256 hash function,
  • Extract a number from the 256 bits output and finally
  • Compare the result number against the target, if it's bigger than the target, the process is repeated again.

With the current network difficulty level, this process can be done in extremely large number of sequences.

The current  network hash rate is ~ 90 quintillions ( 10^18) of hashes every second, this let to development of ASIC miners , which are hardware devices solely optimized for hash algorithm computation.

3. POW Steps In Pseudocode

  • Step 0 : Concatenate block header data.
    HeaderData = version + prevBlockHash + rootHash + time + targetNumber +nonce.
  • Step 1 : DoubleHash  =  SHA256_Of(SHA256_Of( HeaderData )) : The header data  is hashed twice with SHA256 hashing algorithm.
  • Step 2 : ResultNumber =  Number.from(DoubleHash) : The 256 bits output is converted to a large integer value ( BigInt value ).
  • Step 3 : Finally check if ResultNumber < targetNumber`  If the condition yileds true => we have found the right hash, POW is complete,
    Or else  we increment  the nonce integer and go back again to Step 0 until we find a result number that's smaller than the target number.

    Note that this process can be repeated quintillions of times until the right hash is found.


Next steps :

Proof of work - Bitcoin Wiki

https://en.wikipedia.org/wiki/Proof_of_work