Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Bitcoin-related byte operations are a bit tricky, as they tend to switch endianness at a whim. First of, we take the initial []byte array representing</p> <pre><code>00000001c570c4764aadb3f09895619f549000b8b51a789e7f58ea750000709700000000103ca064f8c76c390683f8203043e91466a7fcc40e6ebc428fbcc2d89b574a864db8345b1b00b5ac00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000 </code></pre> <p>Then, we separate out the first half of the array, obtaining:</p> <pre><code>00000001c570c4764aadb3f09895619f549000b8b51a789e7f58ea750000709700000000103ca06 4f8c76c390683f8203043e91466a7fcc40e6ebc428fbcc2d8 </code></pre> <p>After that, we need to swap some bytes around. We reverse the order of bytes in every slice of 4 bytes, thusly obtaining:</p> <pre><code>0100000076C470C5F0B3AD4A9F619598B80090549E781AB575EA587F977000000000000064A03C10396CC7F820F8830614E94330C4FCA76642BC6E0ED8C2BC8F </code></pre> <p>And that is the array we will be using for calculating the midstate. Now, we need to alter the file <code>hash.go</code>, adding to <code>type Hash interface</code>:</p> <pre><code>Midstate() []byte </code></pre> <p>And change the file <code>sha256.go</code>, adding this function:</p> <pre><code>func (d *digest) Midstate() []byte { var answer []byte for i:=0;i&lt;len(d.h);i++{ answer=append(answer[:], Uint322Hex(d.h[i])...) } return answer } </code></pre> <p>Where <code>Uint322Hex</code> converts an <code>uint32</code> variable into a <code>[]byte</code> variable. Having all that, we can call:</p> <pre><code>var h BitSHA.Hash = BitSHA.New() h.Write(Str2Hex("0100000076C470C5F0B3AD4A9F619598B80090549E781AB575EA587F977000000000000064A03C10396CC7F820F8830614E94330C4FCA76642BC6E0ED8C2BC8F")) log.Printf("%X", h.Midstate()) </code></pre> <p>Where <code>Str2Hex</code> turns a <code>string</code> into <code>[]byte</code>. The result is:</p> <pre><code>69FC72E76DB0E764615A858F483E3566E42D56B2BC7A03ADCE9492887010EDA8 </code></pre> <p>Remembering the proper answer:</p> <pre><code>e772fc6964e7b06d8f855a6166353e48b2562de4ad037abc889294cea8ed1070 </code></pre> <p>We can compare them:</p> <pre><code>69FC72E7 6DB0E764 615A858F 483E3566 E42D56B2 BC7A03AD CE949288 7010EDA8 e772fc69 64e7b06d 8f855a61 66353e48 b2562de4 ad037abc 889294ce a8ed1070 </code></pre> <p>So we can see that we just need to swap the bytes around a bit in each slice of 4 bytes and we will have the proper "midstate" used by Bitcoin pools and miners (until it will no longer be needed due to being deprecated).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload