Proof of Work Algorithm
Miners solve a mathematical problem to get the chance to validate a transaction , so that they can earn reward. In this blog, we will discuss that Mathematical problem in detail.
To read more about theoretical concept of Proof of Work, refer this.
Some key terms before getting started :
Hashing — This mechanism converts one message to another encoded message so that the main message is not known to users. Finds usage in cryptography, compression, checksum generation, and data indexing.
SHA256 — used in some of the most popular authentication and encryption protocols. Helps to keep the messages secure. Converts any string to a 64 bit long encoded message. This encoded message cannot be decrypted to find out the main message.
Nonce — A Nonce in cryptography is just a number used to secure messages and communications.
https://emn178.github.io/online-tools/sha256.html
Just go to the above link and try to generate hash. For every distinct string(or data), the hash is different. For the same string, the hash would be the same.
A simple explanation before we dive to the algorithm
Take for instance you have 64 dice. And your objective is to roll those dice together till the time you generate a combination such that the sum of all the values of dice is less than 100. This obviously will take time and you will have to roll the dice multiple times.
Let’s dive to the Algorithm :
So the idea is you are given an output hash and the input data. You need to find a number(called nonce) which will generate the hash.
sha256(data + nonce) = hash
In the above equation the mathematical problem which a miner solves is to find the nonce.
Example :
Given Output hash : 55e0fbcbc68dde4f405d690a73d4ad38d2328631a210b099358e3a63cee65399
Given Input Data : abc
Mathematical problem : To find a nonce
‘abc’ + nonce = 55e0fbcbc68dde4f405d690a73d4ad38d2328631a210b099358e3a63cee65399
Screenshot below for the answer :
See the nonce is 999. So as a miner you need to find 999 to earn a reward.
You can check this to find the answer : https://emn178.github.io/online-tools/sha256.html
Algorithms are generally very similar to above, but there could be different variations of it.
For example one of the algorithms could be to find a number such that the first 4 digits of the hash is 0000.
sha(data + nonce) = 0000…………rest of the hash
Ethereum in particular uses this type of algorithm :
Open your JS dev console on chrome -
Type 0x
Then append a hash to this 0x :
0xd340daeb5293c2a4bd981476782b53424944f7d0d8207795266444884cccb972
This will generate a base 10 number . 9.555260018820248e+76
In Ethereum, the hash is converted to a base 10 number. The Ethereum Proof of Work consensus will find a nonce such that the base 10 number of the hash is less than a number (could be any number like 100,1000)
To solve this kind of mathematical equation you have to generate hashes. If you have more computing power, you can generate more hashes, so it is highly likely that your computer can solve the mathematical problem. As we advance, our computing power and hardware configuration also increases. So the mathematical problem which takes 10 minutes to solve today, might take 5 minutes tomorrow because of the advancement in technology. So according to the protocol the difficulty of the algorithm changes according to the time required to solve the mathematical problem in the past. If miners are taking more time which is more than 10minutes to validate a block(which includes solving mathematical problems); then the complexity of the mathematical problem would decrease and vice versa.
Conclusion:
Hope the article was helpful. Please feel free to drop a comment in case of any questions or suggestions.
https://vivek-sinless.medium.com/proof-of-work-and-proof-of-stake-2f8dabcbe34