Reader's expectation: This paper is suitable for readers who have read the source code of the Bitcoin mining algorithm or understand the main process of mining algorithm.
Introduction:
A piece of pseudo code:
nonce=0;
while (Nonce < Uint_max)
if (sha256 (sha256 (blockheader[nonce)) < target):
success
Else nonce = nonce +1;
The process of digging is to change the nonce, to calculate the sha256 Double Data Digest of the block head, if the summary is less than the target value, the mining success (no discussion of Confirmation (confirmations)). sha256:
Blockheader:
--------------------------------------76 bytes---------------------------4bytes- | 不会变动的信息 | nonce|
--------------------------------------------------------------------------------------
SHA256: Divide any length of data into n*[64 bytes, and fill the remainder. The 32-byte data Digest is then calculated and the N-times results are added separately by 4 bytes. optimization 1 (OpenSSL validation has been used, every 1 million times, do reduce 1/3):
Based on the introduction, it can be seen that no matter what the nonce is, the first 64 bytes will not change, so when the initial value of the nonce= is computed once, this value will be used directly thereafter. Reduce the amount of computation by almost 1/3. Optimization 2:
The remaining 16 bytes, only the last 4 bytes of nonce will change.
In addition, we named the SHA256 64 data changes as ROUND1~ROUND64.
Round1~round3 because the data has not changed so the result is unchanged, you can only calculate once;
Round4:round4 (A,b,c,d,e,f,g,h), where d,h is the result of the calculation. The change relationship between D,h and Nonce is d1=d0+ (NONCE1-NONCE0) h1=h0+ (NONCE1-NONCE0). So you just have to compute it once and get the results quickly. Optimization 3:
Quit early in order to find a small enough result. So in the case where the 8 byte of the high position is not zero, the calculation can be introduced in advance.
Total Optimization:
Optimization of 1, almost 1/3 computational volume;
On the basis of optimizing 1, Optimizing 2 and Optimizing 3 can be promoted at 7/128 speed;
Sat Boolean gratification issues (to be continued)