比特币区块的hash算法

Block hashing algorithm

Bitcoin mining uses the hashcash proof of work function; the hashcash algorithm requires the following parameters: a service string, a nonce, and a counter. In bitcoin the service string is encoded in the block header data structure, and includes a version field, the hash of the previous block, the root hash of the merkle tree of all transactions in the block, the current time, and the difficulty. Bitcoin stores the nonce in the extraNonce field which is part of the coinbase transaction, which is stored as the left most leaf node in the merkle tree (the coinbase is the special first transaction in the block). The counter parameter is small at 32-bits so each time it wraps the extraNonce field must be incremented (or otherwise changed) to avoid repeating work. The basics of the hashcash algorithm are quite easy to understand and it is described in more detail here. When mining bitcoin, the hashcash algorithm repeatedly hashes the block header while incrementing the counter & extraNonce fields. Incrementing the extraNonce field entails recomputing the merkle tree, as the coinbase transaction is the left most leaf node. The block is also occasionally updated as you are working on it.

A block header contains these fields:

Field Purpose Updated when... Size (Bytes)
Version Block version number You upgrade the software and it specifies a new version 4
hashPrevBlock 256-bit hash of the previous block header A new block comes in 32
hashMerkleRoot 256-bit hash based on all of the transactions in the block A transaction is accepted 32
Time Current timestamp as seconds since 1970-01-01T00:00 UTC Every few seconds 4
Bits Current target in compact format The difficulty is adjusted 4
Nonce 32-bit number (starts at 0) A hash is tried (increments) 4

The body of the block contains the transactions. These are hashed only indirectly through the Merkle root. Because transactions aren‘t hashed directly, hashing a block with 1 transaction takes exactly the same amount of effort as hashing a block with 10,000 transactions.

The compact format of target is a special kind of floating-point encoding using 3 bytes mantissa, the leading byte as exponent (where only the 5 lowest bits are used) and its base is 256. Most of these fields will be the same for all users. There might be some minor variation in the timestamps. The nonce will usually be different, but it increases in a strictly linear way. "Nonce" starts at 0 and is incremented for each hash. Whenever Nonce overflows (which it does frequently), the extraNonce portion of the generation transaction is incremented, which changes the Merkle root.

Moreover, it is extremely unlikely for two people to have the same Merkle root because the first transaction in your block is a generation "sent" to one of your unique Bitcoin addresses. Since your block is different from everyone else‘s blocks, you are (nearly) guaranteed to produce different hashes. Every hash you calculate has the same chance of winning as every other hash calculated by the network.

Bitcoin uses: SHA256(SHA256(Block_Header)) but you have to be careful about byte-order.

For example, this python code will calculate the hash of the block with the smallest hash as of June 2011, Block 125552. The header is built from the six fields described above, concatenated together as little-endian values in hex notation:

import hashlib

header_hex = ("01000000" +  "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +  "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +  "c7f5d74d" +  "f2b9441a" +  "42a14695")

header_bin = header_hex.decode(‘hex‘)

hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()

print( hash[::-1].encode(‘hex_codec‘))

Endianess

Note that the hash, which is a 256-bit number, has lots of leading zero bytes when stored or printed as a big-endian hexadecimal constant, but it has trailing zero bytes when stored or printed in little-endian. For example, if interpreted as a string and the lowest (or start of) the string address keeps lowest significant byte, it is little-endian.

The output of blockexplorer displays the hash values as big-endian numbers; notation for numbers is usual (leading digits are the most significant digits read from left to right).

For another example, here is a version in plain C without any optimization, threading or error checking.

原文地址:https://www.cnblogs.com/timlong/p/9513770.html

时间: 2024-12-11 17:11:36

比特币区块的hash算法的相关文章

1.3.1 区块链中的加密算法——Hash算法(更新)

为了为保证存储于区块链中的信息的安全与完整,区块链中使用了包含密码哈希函数和椭圆曲线公钥密码技术在内的大量的现代密码学技术,同时,这些密码学技术也被用于设计基于工作量证明的共识算法并识别用户. 在前边的文章中已经系统的讲述了密码学中的哈希算法,在本节,将会给大家介绍Hash算法在区块链中的应用! 概念回顾: 哈希函数:是一类数学函数,可以在有限合理的时间内,将任意长度的消息压缩为固定长度的二进制串,其输出值称为哈希值,也称为散列值. 以哈希函数为基础构造的哈希算法,在现代密码学中扮演着重要的角色

比特币区块结构Merkle树及简单支付验证分析

在比特币网络中,不是每个节点都有能力储存完整的区块链数据,受限于存储空间的的限制,很多节点是以SPV(Simplified Payment Verification简单支付验证)钱包接入比特币网络,通过简单支付验证可以在不必存储完整区块链下对交易进行验证,本文将分析区块结构Merkle树及如何进行交易验证. 区块结构 在工作量证明中出现过一个区块信息截图: 细心的同学一定已经在里面发现了很多未讲的其他信息,如:时间戳,版本号,交易次数,二进制哈希树根(Merkle根)等. 我们来看看一个区块结构

1.4 [区块链] 共识算法之争(PBFT,Raft,PoW,PoS,DPoS,Ripple)

近几天对区块链中几种常见的共识机制(PBFT,Raft,PoW,PoS,DPoS,Ripple)进行了总结.尽量使用简单易懂语言,篇幅较大,想了解的可以只读每个算法介绍中前边的原理.本篇文章主要参考<区块链技术指南>,首先表示感谢! ---Begin--- 区块链架构是一种分布式的架构.其部署模式有公共链.联盟链.私有链三种,对应的是去中心化分布式系统.部分去中心化分布式系统和弱中心分布式系统. 在分布式系统中,多个主机通过异步通信方式组成网络集群.在这样的一个异步系统中,需要主机之间进行状态

如何将Bitcoin比特币区块链数据导入关系数据库

在接触了比特币和区块链后,我一直有一个想法,就是把所有比特币的区块链数据放入到关系数据库(比如SQL Server)中,然后当成一个数据仓库,做做比特币交易数据的各种分析.想法已经很久了,但是一直没有实施.最近正好有点时间,于是写了一个比特币区块链的导出导入程序. 之前我的一篇博客:在区块链上表白——使用C#将一句话放入比特币的区块链上  介绍了怎么发起一笔比特币的交易,今天我们仍然是使用C#+NBitcoin,读取比特币钱包Bitcoin Core下载到本地的全量区块链数据,并将这些数据写入数

[转帖]比特币区块链的数据结构解析

比特币区块链的数据结构解析 发布者: 无主之地 发表于: 2015-8-13 18:37 来自: 比巴克 16608 1 分享 http://8btc.com/article-1915-1.html 区块链(数据区块.数据块.数据块链)作为比特币的核心概念,对于理解比特币结构起着至关重要的作用.在<什么是数据区块>中,编者从数据区块分布式交易记录的角度简单的介绍了数据区块的概念.今天,编者从数据区块的程序结构上来详细的分析数据区块里面到底记录了什么信息. 数据区块文件的位置如果你用的是Bitc

第六章 比特币--区块链思想诞生的摇篮

一.比特币项目简介 特点: 去中心化:没有任何独立个体可以对交易进行破坏,任何交易请求都需要大多数参与者的共识 匿名性:账户地址是匿名的,无法从交易信息关联到具体的个体,这也意味着很难进行审计 通胀预防:发行量上限2100万个,无法超发,每四年减半. 1. 比特币大事记 2008.10.31,中本聪发布比特币白皮书 2009.1.3 18:15:05,中本聪挖出第一批50个币 2010.5.21 佛罗里达的程序员用1万个币买了价值25美元的披萨优惠券 2012.11.28 产量第一次减半 201

一致性hash算法 - consistent hashing

1.背景 我们都知道memcached服务器是不提供分布式功能的,memcached的分布式完全是由客户端来实现的.在部署memcached服务器集群时,我们需要把缓存请求尽可能分散到不同的缓存服务器中,这样可以使得所有的缓存空间都得到利用,而且可以降低单独一台缓存服务器的压力.     最简单的一种实现是,缓存请求时通过计算key的哈希值,取模后映射到不同的memcahed服务器.这种简单的实现在不考虑集群机器动态变化的情况下也是比较有效的一种方案,但是,在分布式集群系统中,简单取模的哈希算法

java8 hash算法

一.hash算法 哈希算法将任意长度的二进制值映射为较短的固定长度的二进制值,这个小的二进制值称为哈希值.哈希值是一段数据唯一且极其紧凑的数值表示形式.如果散列一段明文而且哪怕只更改该段落的一个字母,随后的哈希都将产生不同的值.要找到散列为同一个值的两个不同的输入,在计算上是不可能的,所以数据的哈希值可以检验数据的完整性.一般用于快速查找和加密算法. 二.jdk的hash算法实现 (1)Interger private final int value; @Override public int

一致性hash算法

一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简单哈希算法带来的问题,使得分布式哈希(DHT)可以在P2P环境中真正得到应用. 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance):平衡性是指哈希的结果能够尽可能分布到所有的缓冲中去,这样可以使得所有的缓冲空间都得到利用.很多哈希算法都能够满足