SHA256 * 2 on header - what am I doing wrong?

3

I've been experimenting with the php code here: https://en.bitcoin.it/wiki/Block_hashing_algorithm (after correcting the small bug of $x=; to $x=""; )

With these blocks 0 and 125552 (for example) it works fine and I get the expected hash.

With blocks 225552 and 300000, I get:

7a7bb195e8bcc8e73205df7906eb68f26144c6fbc577d59dd4e8a0cfdb8b90df (rather than 00000000000001f4f14b09019b23d71c222a2c1245ef3df52875d79ff4488709)

and

178d6fe6cbf696311e7f46263067eeb688797e4c790060c1f6edf6daec76c59e (rather than 000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254)

...respectively - which doesn't match the hash of either block.

What am I missing or doing incorrectly?

Additional note - I confirmed these results using "openssl dgst -sha256 -binary test-input.bin |openssl dgst -sha256"...

Further to this, using the python code from the same page:

>>> 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()
>>> hash.encode('hex_codec')
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'
>>> hash[::-1].encode('hex_codec')
'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

...works fine... but:

>>> import hashlib
>>> header_hex = ("01000000" +
...     "7ef055e1674d2e6551dba41cd214debbee34aeb544c7ec670000000000000000" +
...     "d3998963f80c5bab43fe8c26228e98d030edf4dcbe48a666f5c39e2d7a885c91" +
...     "02c86d53" +
...     "6c890019" +
...     "593a470d")
>>> header_bin = header_hex.decode('hex')
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> hash.encode('hex_codec')
'9ec576ecdaf6edf6c16000794c7e7988b6ee673026467f1e3196f6cbe66f8d17'
>>> hash[::-1].encode('hex_codec')
'178d6fe6cbf696311e7f46263067eeb688797e4c790060c1f6edf6daec76c59e'

...gets the same erroneous result as the php code.

Harwood

Posted 2014-05-14T02:10:59.830

Reputation: 33

Answers

1

You have the wrong block version number. Block 300000 is a version 2 block, whereas 125552 was version 1. So the first word in the block header should be "02000000" not "01000000".

Nate Eldredge

Posted 2014-05-14T02:10:59.830

Reputation: 22 182

Thank you so much - I think I'll update the bitcoin wiki page to reflect that... I spun my wheels for quite some time before posting my question - so I really do appreciate the answer. – Harwood – 2014-05-14T12:42:43.190

0

This is the header for block #300000

02 00 00 00 7E F0 55 E1 │ 67 4D 2E 65 51 DB A4 1C
D2 14 DE BB EE 34 AE B5 │ 44 C7 EC 67 00 00 00 00
00 00 00 00 D3 99 89 63 │ F8 0C 5B AB 43 FE 8C 26
22 8E 98 D0 30 ED F4 DC │ BE 48 A6 66 F5 C3 9E 2D
7A 88 5C 91 02 C8 6D 53 │ 6C 89 00 19 59 3A 47 0D

Version number is 2 (the very first digit), but you have hardcoded it to 1

header_hex = ("01000000" +

amaclin

Posted 2014-05-14T02:10:59.830

Reputation: 6 140

Thank you - the info is not present in the wiki page. Much appreciated. – Harwood – 2014-05-14T12:44:39.303