In Getwork, how does one calculate the value of Hash1?

5

1

(I know the hash1 and midstate are eventually going to be deprecated, but I'm a little curious about them)

In Getwork there is a field hash1. From a sample call to bitcoind I received its value as:

'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000'

How exactly is hash1's value calculated?

ThePiachu

Posted 2012-02-11T01:59:49.223

Reputation: 41 594

Why are they "eventually going to be deprecated"?Geremia 2015-06-11T06:12:23.180

@Geremia since the information is redundant really - you can calculate that yourself.ThePiachu 2015-06-11T18:05:11.160

Answers

6

hash1 is a uint256 zero, padded to a multiple of 64 bytes by FormatHashBlocks() in main.cpp, and then reversed in 4 byte chunks.

FormatHashBlocks() puts a 0x80 right after the 32 zero bytes, and a 4 byte bit count at the end (00 00 01 00, meaning 256 bits).

the uint256 zero is:
0000000000000000 0000000000000000 0000000000000000 0000000000000000

it's padded to 64 bytes:
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000
                                                                    ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
has the 80 in the first byte of padding:
0000000000000000 0000000000000000 0000000000000000 0000000000000000 8000000000000000 0000000000000000 0000000000000000 0000000000000000
                                                                    ^^
and the length at the end:
0000000000000000 0000000000000000 0000000000000000 0000000000000000 8000000000000000 0000000000000000 0000000000000000 0000000000000100
                                                                                                                               ^^^^^^^^
and then has each group of 4 bytes reversed: 12 34 56 78 -> 78 56 34 12
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000008000000000 0000000000000000 0000000000000000 0000000000010000
                                                                    ^^^^^^^^                                                   ^^^^^^^^

A better question might be "why is hash1's value calculated like this"?

You didn't ask, but the data field returned by getwork is formatted the same way. Instead of a uint256 zero though, the input is:

  • version (4 bytes)
  • previous block's hash (32 bytes)
  • merkle root (32 bytes)
  • time (4 bytes)
  • bits (related to difficulty) (4 bytes)
  • nonce (4 bytes)

That's a total of 80 bytes, which is padded to 128 bytes by FormatHashBlocks().

Example:

00000001 (version)
0863bcb701d52fea 0e0c7b0d4696d7e7 8feb125a84e0911b 0000043e00000000 (prev hash)
bc656fe91e44907c 32d1704f900bd7a6 c2fa06386174477f a8b145addebc8d00 (merkle root)
4f35dde3 (time)
1a0c290b (bits)
00000000 (nonce)
0000008000000000 0000000000000000 0000000000000000 } (padding)
0000000000000000 0000000000000000 0000000080020000 } (80 bytes = 640 bits = 0x280)

Chris Moore

Posted 2012-02-11T01:59:49.223

Reputation: 13 952

1So it's always 00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000 ? Interesting...ThePiachu 2012-02-11T03:23:20.757

Yes. Right. That's it.Chris Moore 2012-02-11T04:22:18.857