Can someone help me?
https://ift.tt/NactwjT
Hello guys, sorry for the bad english. I’m from Brazil and I’m trying to write a code in python to show my students how bitcoin mining works but I’m having a few problems. I’m using an article from IEEE called “[Double SHA-256 Hardware Architecture With Compact Message Expander for Bitcoin Mining](https://ift.tt/FdZhcjr;
Then I created a code in python to try to “mine” a block that has already been mined. My ideia is that if I input the correct bits on the function it should return the block hash with a lot of zeros.
My code:
def complete_right_rotation(valor,bits):
INT_BITS = 32
return (valor >> bits)|(valor << (INT_BITS – bits)) & 0xFFFFFFFF
def sigma_0(valor):
aux = complete_right_rotation(valor,7)^complete_right_rotation(valor,18)^(valor>>3)
return aux
def sigma_1(valor):
aux = complete_right_rotation(valor,17)^complete_right_rotation(valor,19)^(valor>>10)
return aux
def message_expander_1(message1):
W = [None]*64 # Faz uma lista de 64 itens vazios
W[0] = message1&0xFFFFFFFF
W[1] = (message1>>32)&0xFFFFFFFF
W[2] = (message1>>64)&0xFFFFFFFF
W[3] = (message1>>96)&0xFFFFFFFF
W[4] = (message1>>128)&0xFFFFFFFF
W[5] = (message1>>160)&0xFFFFFFFF
W[6] = (message1>>192)&0xFFFFFFFF
W[7] = (message1>>224)&0xFFFFFFFF
W[8] = (message1>>256)&0xFFFFFFFF
W[9] = (message1>>288)&0xFFFFFFFF
W[10] = (message1>>320)&0xFFFFFFFF
W[11] = (message1>>352)&0xFFFFFFFF
W[12] = (message1>>384)&0xFFFFFFFF
W[13] = (message1>>416)&0xFFFFFFFF
W[14] = (message1>>448)&0xFFFFFFFF
W[15] = (message1>>480)&0xFFFFFFFF
for aux in range(16,64):
W[aux]=(sigma_1(W[aux-2])+W[aux-7]+sigma_0(W[aux-15])+W[aux-16])&0xFFFFFFFF
return W
def sum_1(valor):
aux = complete_right_rotation(valor,6)^complete_right_rotation(valor,11)^complete_right_rotation(valor,25)
return aux
def bit_not(n, numbits):
aux1=1 << numbits
aux2 = aux1-1-n
return aux2
def Ch(valor1,valor2,valor3):
aux1 = valor1&valor2
aux2 = bit_not(valor1,32)&valor3
return aux1^aux2
def sum_0(valor):
aux = complete_right_rotation(valor,2)^complete_right_rotation(valor,13)^complete_right_rotation(valor,22)
return aux
def Maj(valor1,valor2,valor3):
aux1 = valor1&valor2
aux2 = valor1&valor3
aux3 = valor2&valor3
return aux1^aux2^aux3
def message_compressor_1(W):
K =[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
H1 = 0x6a09e667
H2 = 0xbb67ae85
H3 = 0x3c6ef372
H4 = 0xa54ff53a
H5 = 0x510e527f
H6 = 0x9b05688c
H7 = 0x1f83d9ab
H8 = 0x5be0cd19
a=H1
b=H2
c=H3
d=H4
e=H5
f=H6
g=H7
h=H8
for aux in range(64):
T1 = (h + sum_1(e) + Ch(e,f,g) + K[aux] + W[aux]) & 0xFFFFFFFF
T2 = (sum_0(a) + Maj(a,b,c)) & 0xFFFFFFFF
h = g
g = f
f = e
e = (d + T1) & 0xFFFFFFFF
d = c
c = b
b = a
a = (T1+T2) & 0xFFFFFFFF
HO1=(a+H1) & 0xFFFFFFFF
HO2=(b+H2) & 0xFFFFFFFF
HO3=(c+H3) & 0xFFFFFFFF
HO4=(d+H4) & 0xFFFFFFFF
HO5=(e+H5) & 0xFFFFFFFF
HO6=(f+H6) & 0xFFFFFFFF
HO7=(g+H7) & 0xFFFFFFFF
HO8=(h+H8) & 0xFFFFFFFF
return [HO1,HO2,HO3,HO4,HO5,HO6,HO7,HO8]
def SHA256_1(message1):
m_e_1 = message_expander_1(message1)
m_c_1 = message_compressor_1(m_e_1)
return m_c_1
def message_expander_2(message2):
W = [None]*64 # Faz uma lista de 64 itens vazios
W[0] = message2&0xFFFFFFFF
W[1] = (message2>>32)&0xFFFFFFFF
W[2] = (message2>>64)&0xFFFFFFFF
W[3] = (message2>>96)&0xFFFFFFFF
W[4] = (message2>>128)&0xFFFFFFFF
W[5] = (message2>>160)&0xFFFFFFFF
W[6] = (message2>>192)&0xFFFFFFFF
W[7] = (message2>>224)&0xFFFFFFFF
W[8] = (message2>>256)&0xFFFFFFFF
W[9] = (message2>>288)&0xFFFFFFFF
W[10] = (message2>>320)&0xFFFFFFFF
W[11] = (message2>>352)&0xFFFFFFFF
W[12] = (message2>>384)&0xFFFFFFFF
W[13] = (message2>>416)&0xFFFFFFFF
W[14] = (message2>>448)&0xFFFFFFFF
W[15] = (message2>>480)&0xFFFFFFFF
for aux in range(16,64):
W[aux]=(sigma_1(W[aux-2])+W[aux-7]+sigma_0(W[aux-15])+W[aux-16])&0xFFFFFFFF
return W
def message_compressor_2(W,midhash):
K =[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
H1 = midhash[0]
H2 = midhash[1]
H3 = midhash[2]
H4 = midhash[3]
H5 = midhash[4]
H6 = midhash[5]
H7 = midhash[6]
H8 = midhash[7]
a=H1
b=H2
c=H3
d=H4
e=H5
f=H6
g=H7
h=H8
for aux in range(64):
T1 = (h + sum_1(e) + Ch(e,f,g) + K[aux] + W[aux]) & 0xFFFFFFFF
T2 = (sum_0(a) + Maj(a,b,c)) & 0xFFFFFFFF
h = g
g = f
f = e
e = (d + T1) & 0xFFFFFFFF
d = c
c = b
b = a
a = (T1+T2) & 0xFFFFFFFF
HO1=(a+H1) & 0xFFFFFFFF
HO2=(b+H2) & 0xFFFFFFFF
HO3=(c+H3) & 0xFFFFFFFF
HO4=(d+H4) & 0xFFFFFFFF
HO5=(e+H5) & 0xFFFFFFFF
HO6=(f+H6) & 0xFFFFFFFF
HO7=(g+H7) & 0xFFFFFFFF
HO8=(h+H8) & 0xFFFFFFFF
return [HO1,HO2,HO3,HO4,HO5,HO6,HO7,HO8]
def SHA256_2(message2,midhash):
m_e_2 = message_expander_2(message2)
m_c_2 = message_compressor_2(m_e_2,midhash)
return m_c_2
def message_expander_3(message3,paddinglength):
### padding lenght sendo mais significativo
message4= (paddinglength<<256)+message3
W = [None]*64 # Faz uma lista de 64 itens vazios
W[0] = message4&0xFFFFFFFF
W[1] = (message4>>32)&0xFFFFFFFF
W[2] = (message4>>64)&0xFFFFFFFF
W[3] = (message4>>96)&0xFFFFFFFF
W[4] = (message4>>128)&0xFFFFFFFF
W[5] = (message4>>160)&0xFFFFFFFF
W[6] = (message4>>192)&0xFFFFFFFF
W[7] = (message4>>224)&0xFFFFFFFF
W[8] = (message4>>256)&0xFFFFFFFF
W[9] = (message4>>288)&0xFFFFFFFF
W[10] = (message4>>320)&0xFFFFFFFF
W[11] = (message4>>352)&0xFFFFFFFF
W[12] = (message4>>384)&0xFFFFFFFF
W[13] = (message4>>416)&0xFFFFFFFF
W[14] = (message4>>448)&0xFFFFFFFF
W[15] = (message4>>480)&0xFFFFFFFF
for aux in range(16,64):
W[aux]=(sigma_1(W[aux-2])+W[aux-7]+sigma_0(W[aux-15])+W[aux-16])&0xFFFFFFFF
return W
def message_compressor_3(W):
K =[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
H1 = 0x6a09e667
H2 = 0xbb67ae85
H3 = 0x3c6ef372
H4 = 0xa54ff53a
H5 = 0x510e527f
H6 = 0x9b05688c
H7 = 0x1f83d9ab
H8 = 0x5be0cd19
a=H1
b=H2
c=H3
d=H4
e=H5
f=H6
g=H7
h=H8
for aux in range(64):
T1 = (h + sum_1(e) + Ch(e,f,g) + K[aux] + W[aux]) & 0xFFFFFFFF
T2 = (sum_0(a) + Maj(a,b,c)) & 0xFFFFFFFF
h = g
g = f
f = e
e = (d + T1) & 0xFFFFFFFF
d = c
c = b
b = a
a = (T1+T2) & 0xFFFFFFFF
HO1=(a+H1) & 0xFFFFFFFF
HO2=(b+H2) & 0xFFFFFFFF
HO3=(c+H3) & 0xFFFFFFFF
HO4=(d+H4) & 0xFFFFFFFF
HO5=(e+H5) & 0xFFFFFFFF
HO6=(f+H6) & 0xFFFFFFFF
HO7=(g+H7) & 0xFFFFFFFF
HO8=(h+H8) & 0xFFFFFFFF
return [HO1,HO2,HO3,HO4,HO5,HO6,HO7,HO8]
def SHA256_3(message3,paddinglength):
m_e_3 = message_expander_3(message3,paddinglength)
m_c_3 = message_compressor_3(m_e_3)
return m_c_3
version = 0x2fffe000<<992
hash_of_previous_block = 0x00000000000000000cee3902a4c9bb2fc3369be368dccf9019bb6c3b94b96a29<<736
hash_of_merkle_root = 0x2b191b5c7e04c4b962632ee45f1d2f3cb5b08e2610aa2e494e728cf7a360be57<<480
time_stamp = 0x6201e1bc<<448
target = 0x1812079b<<416
Nonce = 0x3a56393a<<384
padding_length1 = (0x1<<383) + 0x640
padding_length2 = (0x1<<255) + 0x640
message = version+hash_of_previous_block+hash_of_merkle_root+time_stamp+target+Nonce+padding_length1
message_msb= message>>512 # Most Significative Bit
message_lsb= message&0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff # Less Significative Bit
aux_SHA256_1 = SHA256_1(message_msb)
aux_SHA256_2 = SHA256_2(message_lsb,aux_SHA256_1)
aux_SHA256_2 = (aux_SHA256_2[0]<<224)+(aux_SHA256_2[1]<<192)+(aux_SHA256_2[2]<<160)+(aux_SHA256_2[3]<<128)+(aux_SHA256_2[4]<<96)+(aux_SHA256_2[5]<<64)+(aux_SHA256_2[6]<<32)+(aux_SHA256_2[7])
aux_SHA256_3 = SHA256_3(padding_length2,aux_SHA256_2)
print(hex(aux_SHA256_3[0]))
print(hex(aux_SHA256_3[1]))
print(hex(aux_SHA256_3[2]))
print(hex(aux_SHA256_3[3]))
print(hex(aux_SHA256_3[4]))
print(hex(aux_SHA256_3[5]))
print(hex(aux_SHA256_3[6]))
print(hex(aux_SHA256_3[7]))
Cryptocurrency