Take the 2-minute tour ×
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It's 100% free, no registration required.

RFC4231 gives test cases for SHA256 with several different sets of gozintas and gozoutas. I'm looking for something similar for the SHA256 hash as implemented in the Bitcoin mining algorithm.


Based on the answers given, here is a very simple test in Python. Note that the generated digest must be reversed before the check, as the 256-bit digest is apparently returned with the least significant bits first.

#!/usr/bin/python
import hashlib
GENESIS_BLOCK = \
    '0100000000000000000000000000000000000000000000000000000000000000' + \
    '000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA' + \
    '4B1E5E4A29AB5F49FFFF001D1DAC2B7C01010000000100000000000000000000' + \
    '00000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D' + \
    '0104455468652054696D65732030332F4A616E2F32303039204368616E63656C' + \
    '6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F75742066' + \
    '6F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE554827' + \
    '1967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4' + \
    'F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000'
GENESIS_HASH = \
    '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
def check_hash(block = GENESIS_BLOCK, blockhash = GENESIS_HASH):
    '''
    check that the calculated hash matches what Bitcoin expects

    >>> check_hash()
    True
    '''
    blockheader = block.decode('hex')[:80]
    header_hash = hashlib.sha256(blockheader).digest()
    check_hash = hashlib.sha256(header_hash).digest()
    return check_hash[::-1].encode('hex') == blockhash

if __name__ == '__main__':
    print check_hash()
share|improve this question

2 Answers 2

up vote 1 down vote accepted

First off, bitcoin uses SHA256d.

Second, the best testcase I can think of is the genesis block:

GetHash()      = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
txNew.vin[0].scriptSig     = 486604799 4 0x736B6E616220726F662074756F6C69616220646E6F63657320666F206B6E697262206E6F20726F6C6C65636E61684320393030322F6E614A2F33302073656D695420656854
txNew.vout[0].nValue       = 5000000000
txNew.vout[0].scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704 OP_CHECKSIG
block.nVersion = 1
block.nTime    = 1231006505
block.nBits    = 0x1d00ffff
block.nNonce   = 2083236893

CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
  CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
    CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
    CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
  vMerkleTree: 4a5e1e
share|improve this answer

You can take any raw block and its hash as an example. For example, use the Genesis Block:

0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A29AB5F49FFFF001D1DAC2B7C0101000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000

You need to take its header:

0100000000000000000000000000000000000000000000000000000000000000000000003BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A29AB5F49FFFF001D1DAC2B7C00

(note the last byte changes from the original 01 to 00, since block headers don't have transactions in them)

Which hashes to

000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

share|improve this answer
    
Actually, for the block header hash, you only hash up to the nonce, which is 2B7C in this case. –  Nick ODell May 20 '13 at 22:48
    
@NickODell Thanks, forgot about that. –  ThePiachu May 21 '13 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.