Cryptography block chain (3): Cryptographic Hash algorithms and mining

Connect the text:

Cryptography (1) block in the chain: to enhance the knowledge of cryptography

The cryptographic block chain (2): basic introduction of cryptography

Turning to mining, the first choice should come to mind are:

 

But in fact, mining block in the chain like this:

 

Lined mining machine on a rack, keep doing arithmetic, but in doing Hash operation. This mining have called dark energy production, the electrical energy into digital currency. Then we talk about relationships today Hash algorithms and POW consensus algorithm.

 

01 Hash Algorithm

Turning Hash, Hash algorithm we have to distinguish between ordinary Hash algorithms and cryptography.

Common Hash algorithm, generally referred to as a hash table or a hash table, is a basic data structure. Generally used to achieve in O (1) query criteria, involving the selection of the hash function and hash collision solve the problem.

Its wide range of applications, such as C ++ standard library hashmap, Java the HashMap, Python in dict, Hash algorithms are used. Also I think a lot of caching products, memecached, redis are hash algorithm for large-scale applications.

The Hash algorithm cryptography, you can use a formula to describe:

Digest / hash / fingerprint = hash (message)

Input to the algorithm is a message, or a bunch of binary content. The final output is a binary string of fixed length, may be referred to digest, a hash value, such as a fingerprint. hash algorithm there are many. cryptographic hash algorithm has five important characteristics:

  1. Input message is the same, the same output values, and all the outputs are equal;

  2. Regardless of how long the input, computing speed;

  3. Includes a one-way algorithm, it is extremely difficult to obtain an input value the output value;

  4. Once the input information is modified, even very minor changes, the output values ​​are not the same;

  5. hash collision does not exist. That is hard to find two different messages, their output values ​​are the same;

Cryptographic Hash Algorithm many, such as MD4, SHA. However, MD5 is Professor Wang Xiaoyun China proved to be unsafe, it is a widely used algorithm SHA race. Bits are used in coin SHA-256 algorithm.

We can show you Python in use SHA-256 is:

 % pythonPython 3.7.3 (default, Nov 15 2019, 04:04:52)>>> import hashlib>>> hashlib.sha256("hello world!".encode('utf-8')).hexdigest()'7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'>>> hashlib.sha256("hello world".encode('utf-8')).hexdigest()'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'>>> hashlib.sha256("hello world, bitcoin ethereum consusus".encode('utf-8')).hexdigest()'39a0a566f63869830e769533c4efabd31cba7cf624cd23e67638a25354fa5c10'
 
Hash Algorithm cryptographic characteristics determine its scope of application, often used for authentication, all aspects of fingerprint files, messages, tamper-proof and so on.

Here, we are talking about an application with the block chain is extremely relevant, HashCash.

 

02 HashCash

 

HashCash us in the article " From the history of technology block watch block chain what is? "Mentioned by Adam Back design, specific thesis see: http: //www.hashcash.org/papers/hashcash.pdf. Adam Back still has great influence in today's block chain industry. He is Blockstream founder and CEO, Bitcoin core developers mostly Blockstream the company's employees.

 

HashCash first appeared to solve the network resources are lot of abuse, especially of spam flooding. How to do it?

 

HashCash idea is very simple, before mail can not start it a little job, I was doing the right thing to receive your mail. This job can not be too heavy, but at least to take up some of the resources of the sender.

 

In fact, this process it should be very familiar with:

 

12306 is occupied people's attention, HashCash is considered CPU power, and his way is:

 

  1. In the e-mail message header, adds a stamp (hashcash stamp) hashcash hash value;

  2. This hash is included in the recipient address, transmission time, Salt, the hash value is unique in that it must be at least the front 20 0 is a valid stamp hashcash

  3. In order to obtain legal hash value, the sender must go through many attempts (changing the salt value) to get.

 

 HashCash design ideas behind math problems based on hope, I hope you do some work, that is the price to pay to calculate the CPU (This concept is important, Bitcoin is also key in this), get the right result, in order to obtain certain resources (for example to send spam to your mailbox).

 

Hash Algorithm relatively fast speed, a little bit of difference of the input data, the hash value will lead to vastly different characteristic, selected Adam Back. After the return of resources to pay with cash on behalf of operator force, words very precise and appropriate, especially extended to use bitcoin as a core POW consensus algorithm, but it is appropriate.

 

 

03 Bitcoin mining

Currency bits represented by block chain technique called distributed books. Since it is the books, there is the very core of the problem, the accountant Where? Who is responsible for bookkeeping it?

在传统的中心化应用中,处理这个问题很简单,在应用的账户系统系统里面,设置不同的权限即可,把这种核心权限赋予极少部分的人来处理。

但比特币是一个纯粹的P2P系统,每个节点的权限和地位都是一样的,相当于很多人要一起决策一件事情,这个过程在区块链里面称之为共识(Consensus),共识的机制也就成称之为共识算法。

中本聪在设计比特币算法的时候,受到HashCash的启发,采取工作量证明的方式来实现记账过程,参与记账的节点,称之为矿工。工作量证明在比特币白皮书中明确提到,具体见:

https://github.com/ConsensusDev/whitepaper/blob/master/bitcoin.md(整理了各种版本),有详细的描述。

矿工合并收到的交易形成一个完整的区块,区块头由一些字段构成,其中最后一个字段nonce是可变的,矿工不断修改nonce的值,并计算整个区块头的hash值,当hash值小于某个目标值的时候,才算记账成功,本区块是一个合法区块,广播到全网,矿工获得记账奖励。目标值跟难度值相关联。难度值越大,目标值越小(hash值前面的0越多),挖矿难度越高。

核心寻找nonce的过程类似于下面一段代码:

def proof_of_work(header, difficulty_bits):      # calculate the difficulty target      target = 2 ** (256-difficulty_bits)       for nonce in xrange(max_nonce):             hash_result = hashlib.sha256(str(header)+str(nonce)).hexdigest()             # check if this is a valid result, below the target             if long(hash_result, 16) < target:                   print "Success with nonce %d" % nonce                   print "Hash is %s" % hash_result                   return (hash_result,nonce)        print "Failed after %d (max_nonce) tries" % nonce        return nonce

完整的代码见:

https://github.com/ConsensusDev/code/blob/master/pow/proof_of_work.py

运算难度随难度的增加指数增加,笔者电脑为2018年款的macbook,截取一些运算结果如下:

Difficulty: 1048576 (20 bits)Starting search...Success with nonce 237723Hash is 000005720acd8c7207cbf495e85733f196feb1e3692405bea0ee864104039350Elapsed Time: 0.5485 secondsHashing Power: 433381 hashes per secondDifficulty: 2097152 (21 bits)Starting search...Success with nonce 687438Hash is 000003a6eeee97491a9183e4c57458172edb6f9466377bf44afbd74e410f6eefElapsed Time: 1.5282 secondsHashing Power: 449829 hashes per secondDifficulty: 4194304 (22 bits)Starting search...Success with nonce 1759164Hash is 0000008bb8f0e731f0496b8e530da984e85fb3cd2bd81882fe8ba3610b6cefc3Elapsed Time: 3.9695 secondsHashing Power: 443166 hashes per secondDifficulty: 8388608 (23 bits)Starting search...Success with nonce 14214729Hash is 000001408cf12dbd20fcba6372a223e098d58786c6ff93488a9f74f5df4df0a3Elapsed Time: 32.6874 secondsHashing Power: 434868 hashes per secondDifficulty: 16777216 (24 bits)Starting search...Success with nonce 24586379Hash is 0000002c3d6b370fccd699708d1b7cb4a94388595171366b944d68b2acce8b95Elapsed Time: 55.3770 secondsHashing Power: 443981 hashes per second

我们这里只阐述了核心的挖矿过程,更详细的流程描述可以见:《精通比特币》一书。随着比特币价格的一路走高,越来越多的矿工加入算力军备竞赛,目前整个算力也达到恐怖的110.18 EH/s。比特币网络本身随着算力的变化,动态调整难度值。比特币网络的运行10年的难度曲线如下:

 

当然比特币网络也是这场算力竞争中最大的收益者,算力越大,系统越安全,越能形成最大的共识,因而让比特币的市值长期超过整个数字货币市场的50%。

密码学Hash算法在区块链技术中运用广泛,除了挖矿,在区块打包中也大放异彩,区块链不可篡改的算法保障就是Hash算法决定的。下次探讨这个问题,敬请期待。

发布了6 篇原创文章 · 获赞 2 · 访问量 921

Guess you like

Origin blog.csdn.net/noding2001/article/details/104236106