Hash algorithm for Bitcoin block

Source: Internet
Author: User
Tags compact

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 was 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 are part of the Coinbase transaction, which are stored as the left Mo St leaf node in the Merkle tree (the Coinbase are the special first transaction in the block). The counter parameter is small at 32-bits so all time it wraps the Extranonce field must be incremented (or otherwise cha nged) to avoid repeating work. The basics of the Hashcash algorithm is quite easy to understand and it's described in more detail here. When mining Bitcoin, the Hashcash algorithm repeatedly hashes the block headers while incrementing the CounteR & extranonce fields. Incrementing the Extranonce field entails recomputing the Merkle tree, as the Coinbase transaction is the left most leaf n Ode. The block is also occasionally updated as you are working on it.

A Block Header contains these fields:

nonce
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 +
hashmerkleroot 256-bit Hash based on all of the transactions in The block A transaction is accepted all
time current timestamp as seconds since 1970-01-01t00:00 UTC every few seconds 4
Bits Curr Ent target in Compact Format the difficulty is adjusted 4
32-bit number (starts at 0) A Hash is tried (increments) 4

The body of the block contains the transactions. These is 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 a transactions.

The compact format of target is a special kind of floating-point encoding using 3 bytes Mantissa, the leading byte as Expo Nent (where only the 5 lowest bits was used) and its base is 256. Most of these fields would be the same for all users. There might is some minor variation in the timestamps. The nonce would usually is different, but it increases in a strictly linear the. "Nonce" starts at 0 and was 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 both people to has the same Merkle root because the first transaction in your block is a generation ' sent ' to one of the your unique Bitcoin addresses. Since your block is different from everyone else's blocks, you be (nearly) guaranteed to produce different hashes. Every hash you calculate have the same chance of winning as every other hash calculated by the network.

Bitcoin uses:sha256 (SHA256 (block_header)) but you had to be careful about Byte-order.

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

ImportHashlibheader_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, have lots of leading zero bytes when stored or printed as a Big-endian hexad ECIMAL constant, but it had 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 was the most significant digits read from left to right).

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

Hash algorithm for Bitcoin block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.