#比特币挖矿part1# 挖矿算法

之前,我总以为挖矿就是把Nonce值都试一遍,然后用SHA256算法算一遍。后面发现自己把东西看得太简单,我尝试尝试讲讲比特币的挖矿算法的流程。

区块头

首先挖矿算法的目标对象只是区块中的区块头,共80个字节,我们来看看区块头有哪些字段:
#比特币挖矿part1# 挖矿算法

注意:

  1. Target(难度目标):该区块工作量证明算法的难度目标
  2. 其实区块头不包括Padding+Length部分,这个部分只是为了满足SHA256算法的使用条件。
  3. 字段的不同颜色代表该字段内容变化频率。绿色:像Version、Target、以及Padding+Length(有约定俗成的标准)这些内容相对出块的速度来说变化频率很低;黄色:像hashPreBlock、hashMerkleRoot、Timestamp这些几乎与出块的速度一致的频率;红色:像Nonce这个的变化频率远远快与出块速率。

挖矿算法流程

#比特币挖矿part1# 挖矿算法

为什么要做双重hash(SHA256(1)后还来个SHA256(2))

The SHA256 hashing algorithm, like all hashes constructed using the Merkle-Damgård paradigm, is vulnerable to this attack. The length extension attack allows an attacker who knows SHA256(x) to calculate SHA256(x||y) without the knowledge of x. Although it is unclear how length extension attacks may make the Bitcoin protocol susceptible to harm, it is believed that Satoshi Nakamoto decided to play it safe and include the double hashing in his design.

Another explanation [6] for this double hashing is that 128 rounds of SHA256 may remain safe longer if in the far future, a practical pre-image or a partial pre-image attack was found against SHA256.

挖矿算法实现以及编码方式

https://en.bitcoin.it/wiki/Block_hashing_algorithm