SHARE
TWEET

bitcoin midstate calculation

a guest Jan 9th, 2014 192 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. # bitcoin midstate calculation, copyright by Frank Buss
  4. # call it with the "data" string of a getwork response. Example:
  5. # python midstate.py 00000001ab02cd818b9e567ee21793cddef299feb29ad444a41b85b8000008a300000000c2b620e3758dfcff8bdb2304ae42b91e1e950e71aff797d7b09288fc2b12fcf14dd7f5c71a44b9f200000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000
  6. # Output: 93c524951367c505ba69e616a010282d376ee807daa9562f69ce5bcd2dda787a
  7. # tested with Python 3.3.3 on Windows
  8.  
  9. # some helper functions are from https://github.com/bitcoin/bitcoin/blob/master/contrib/pyminer/pyminer.py
  10.  
  11. from sys import argv
  12. from struct import pack, unpack
  13. from binascii import unhexlify, hexlify
  14.  
  15. def uint32(x):
  16.         return x & 0xffffffff
  17.  
  18. def rotateright(i,p):
  19.     """i>>>p"""
  20.     p &= 0x1F # p mod 32
  21.     return i>>p | ((i<<(32-p)) & 0xFFFFFFFF)
  22.  
  23. def shiftright(i,p):
  24.     """i>>p"""
  25.     p &= 0x1F # p mod 32
  26.     return i>>p
  27.  
  28. def addu32(*i):
  29.     return sum(list(i))&0xFFFFFFFF
  30.  
  31. def CH(e, f, g):
  32.         return (e & f) ^ ((~e) & g)
  33.  
  34. def E0(a):
  35.         return rotateright(a, 2) ^ rotateright(a, 13) ^ rotateright(a, 22)
  36.  
  37. def E1(e):
  38.         return rotateright(e, 6) ^ rotateright(e, 11) ^ rotateright(e, 25)
  39.  
  40. def SIG0(x):
  41.         return rotateright(x, 7) ^ rotateright(x, 18) ^ shiftright(x, 3)
  42.  
  43. def SIG1(x):
  44.         return rotateright(x, 17) ^ rotateright(x, 19) ^ shiftright(x, 10)
  45.  
  46. def MAJ(a, b, c):
  47.         return (a & b) ^ (a & c) ^ (b & c)
  48.  
  49. # test data
  50. datastr = argv[1]
  51.  
  52. # convert to binary array
  53. data = unhexlify(datastr)
  54.  
  55. # initialize hash values
  56. h0 = 0x6a09e667
  57. h1 = 0xbb67ae85
  58. h2 = 0x3c6ef372
  59. h3 = 0xa54ff53a
  60. h4 = 0x510e527f
  61. h5 = 0x9b05688c
  62. h6 = 0x1f83d9ab
  63. h7 = 0x5be0cd19
  64.  
  65. # initialize array of round constants
  66. K = [
  67.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  68.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  69.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  70.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  71.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  72.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  73.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  74.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  75. ]
  76.  
  77. # copy chunk into first 16 words w[0..15] of the message schedule array
  78. w = list(unpack('<IIIIIIIIIIIIIIII', data[:64]))
  79.  
  80. # extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
  81. w += [0] * 48
  82. for i in range(16, 64):
  83.         w[i] = addu32(SIG1(w[i-2]), w[i-7], SIG0(w[i-15]), w[i-16])
  84.  
  85.  
  86. # initialize working variables to current hash value
  87. a = h0
  88. b = h1
  89. c = h2
  90. d = h3
  91. e = h4
  92. f = h5
  93. g = h6
  94. h = h7
  95.  
  96. # compression function main loop
  97. for i in range(64):
  98.         t1=addu32(h, E1(e), CH(e,f,g), K[i], w[i])
  99.         t2=addu32(E0(a), MAJ(a,b,c))
  100.         h=g
  101.         g=f
  102.         f=e
  103.         e=addu32(d, t1)
  104.         d=c
  105.         c=b
  106.         b=a
  107.         a=addu32(t1, t2)
  108.         if i == 0: a0 = a
  109.         if i == 1: a1 = a
  110.         if i == 2: a2 = a
  111.         if i == 0: e0 = e
  112.         if i == 1: e1 = e
  113.         if i == 2: e2 = e
  114.  
  115. # add the compressed chunk to the current hash value:
  116. h0 = addu32(h0, a)
  117. h1 = addu32(h1, b)
  118. h2 = addu32(h2, c)
  119. h3 = addu32(h3, d)
  120. h4 = addu32(h4, e)
  121. h5 = addu32(h5, f)
  122. h6 = addu32(h6, g)
  123. h7 = addu32(h7, h)
  124.  
  125. # produce the final hash value (would be big-endian for SHA256, but is little endian for bitcoin)
  126. midstate = pack('<IIIIIIII', h0, h1, h2, h3, h4, h5, h6, h7)
  127. midstatestr = hexlify(midstate).decode('utf-8')
  128. print(midstatestr)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top