7

4

I'm trying to walk my way through the process by which a miner hashes.

Let's say the `getwork`

request returns a `data`

field of:

```
0000000244de6ceba49e1c8d438c0d8c584eefd9c8590545bfdfbf380000025c00000000fb3a091de1b4bbe5dc7acfe6bdacbc3fc3bb09bf1030adef8e2854db1b6ac42f5075c0051a057e08456c6f69000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
```

As far as I understand, the first step is to calculate the midstate. To do this, we first take the first half of the data string:

```
0000000244de6ceba49e1c8d438c0d8c584eefd9c8590545bfdfbf380000025c00000000fb3a091de1b4bbe5dc7acfe6bdacbc3fc3bb09bf1030adef8e2854db
```

We then reverse the endianness of each 32-bit unsigned int (represented as 8 hex digits in the string), yielding:

```
02000000eb6cde448d1c9ea48c0d8c43d9ef4e58450559c838bfdfbf5c020000000000001d093afbe5bbb4e1e6cf7adc3fbcacbdbf09bbc3efad3010db54288e
```

Next we transform this into sixteen 32-bit unsigned ints:

```
33554432, 3949780548, 2367463076, 2349698115, 3656339032, 1157978568, 952098751, 1543634944, 0, 487144187, 3854284001, 3872357084, 1069329597, 3205086147, 4021104656, 3679725710
```

We then input this int array into SHA-256's internal function, with the second input being the eight 32-bit numbers given on page 13 of the SHA-256 specs.

The output of this preliminary hash yields the following eight 32-bit ints as our midstate:

```
3045448562, 361056177, 1940413978, 3803584651, 1661283772, 3478943551, 2906109005, 300125848
```

**From this point on, I'm not sure how correct the steps are. Corrections are greatly appreciated!**

Next, we look at the second half of the input string:

```
1b6ac42f5075c0051a057e08456c6f69000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
```

Once again, we reverse the endianness, as everything is an 8-character hex string representing 32-bit unsigned ints:

```
2fc46a1b05c07550087e051a696f6c45800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280
```

Now, we break this up into sixteen 32-bit ints: `801401371, 96499024, 142476570, 1768909893, 2147483648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 640`

The fifth number (`21474836481`

) should be the nonce, according to this description.

**(Why isn't this nonce zero?)**

Now, starting at the given nonce, we use the SHA-256 function to hash the sixteen 32-bit ints from the second half of the data, using the midstate as the other eight int inputs. This yields: `3993002029, 2278477219, 3977673643, 191934125, 2075691039, 4115259165, 601235791, 2598049038`

**Now, what do I use as inputs for the second hash function in the "double-hash"? Or did calculating the midstate count as the first hash computation?**

**And when the nonce overflows, should I submit another getwork request or wait until I've checked the nonces in the range of [0, original_nonce_value)? (Assuming my analysis that the given nonce is 21474836481 is correct?)**

Lastly, if our `target`

value from the `getwork`

request is:

```
ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000
```

We need to switch the endianness of this value, yielding:

```
00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
```

And then we convert this to eight 32-bit unsigned ints, yielding: `0, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295`

And I believe after the second SHA-256 hash we should have eight 32-bit unsigned ints. Lastly, we should compare these eight output ints against our eight target ints (from left to right in the array, so comparing output[0] against the target's 0, then output[1] against 4294967295, etc.) and if our output is less than the target, we convert our eight int values to hex strings, switch the endianness, concatenate them in the same order (output[0]'s hex string is the first set of eight characters), and submit it back to the pool server in a `getwork`

completion `POST`

.

**How much of this is correct, and where am I misinterpreting the protocol?**

All help is appreciated; thanks so much!

The following thread looks like it might be a more complete answer: http://stackoverflow.com/questions/9245235/golang-midstate-sha-256-hash

– GuyPaddock – 2013-08-02T18:56:10.197Thanks for the edit! I was wondering how to do that formatting... – ConstableJoe – 2012-10-29T14:57:53.577