def calculate_midstate(data): # return struct.pack("<8I", *struct.unpack(">8I", SHA256.hash(struct.pack("<16I", *struct.unpack(">16I", data[:64])), False))) return struct.pack( "<8I", *struct.unpack( ">8I", BLAKE(256).midstate( struct.pack("<16I", *struct.unpack(">16I", data[:64])))))
def run(self): while True: try: work = bitcoin.getwork() self.block = work['data'] self.target = work['target'] except: print("RPC getwork error") # In this case, keep crunching with the old data. It will get # stale at some point, but it's better than doing nothing. # print("block " + self.block + " target " + self.target) # DEBUG # Target is unused in BLAKE so just disable warnings for now sdiff = self.target.decode('hex')[31:27:-1] intTarget = int(sdiff.encode('hex'), 16) if (intTarget < 1): #print "WARNING zero target, defaulting to diff=2", intTarget #print "target", self.target #print("sdiff", sdiff) # NB Need brackets here else prints binary self.target = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f0000" else: newdiff = 65536.0 / (intTarget + 1) # if (self.diff != newdiff): # print "New target diff =", newdiff self.diff = newdiff # Replace MSB 16 bits of target with clock (NB its reversed) self.target = self.target[0:60] + self.dynclock_hex self.dynclock_hex = "0000" # Once only # print("Sending data to FPGA") # DEBUG midstate_blake8 = BLAKE(256).midstate( struct.pack( "<16I", *struct.unpack(">16I", self.block.decode('hex')[:64]))) # print('midstate_blake8 %s' % (hexlify(midstate_blake8).decode())) midstate_blake8_swap = struct.pack( "<8I", *struct.unpack(">8I", midstate_blake8)) # print('midswap_blake8 %s' % (hexlify(midstate_blake8_conv).decode())) midstate = hexlify(midstate_blake8_swap) # for blakecoin send 16 bytes data plus midstate plus 4 bytes of 32 byte target (used for dynclock only) payload = self.target.decode('hex')[31:27:-1] + self.block.decode( 'hex')[79:63:-1] + midstate.decode('hex')[::-1] # print("Payload " + payload.encode('hex_codec')) # DEBUG ser.write(payload) result = golden.wait(askrate) if result: golden.clear()
def _writeJob(self, job): # We need the 256-bit midstate, and 12 bytes from data. # The first 64 bytes of data are already hashed (hence midstate), # so we skip that. Of the last 64 bytes, 52 bytes are constant and # not needed by the FPGA. start_time = time.time() # blakecoin DEBUG #midstatehex = '' #proc = subprocess.Popen(['./midstate',job.data],stdout=subprocess.PIPE) # OK for linux and windows #while True: # msline = proc.stdout.readline() # if (msline != ''): # midstatehex = msline.rstrip() # else: # break #print "\nmidstatehex=", midstatehex #midstate1 = hexstr2array(midstatehex) #print (midstate1) midstate_blake8 = BLAKE(256).midstate( struct.pack("<16I", *struct.unpack(">16I", job.data.decode('hex')[:64]))) # print('\nmidstate_blake8 %s' % (hexlify(midstate_blake8).decode())) midstate_blake8_swap = struct.pack( "<8I", *struct.unpack(">8I", midstate_blake8)) # print('\nmidswap_blake8 %s' % (hexlify(midstate_blake8_swap).decode())) # midstate = hexstr2array(job.midstate) // ORIGINAL midstate = hexstr2array(hexlify(midstate_blake8_swap).decode()) # print (midstate) data = hexstr2array(job.data)[64:64 + 12] data = midstate + data words = [] for i in range(11): word = data[i * 4] | (data[i * 4 + 1] << 8) | ( data[i * 4 + 2] << 16) | (data[i * 4 + 3] << 24) words.append(word) if not self._burstWrite(1, words): self.logger.reportDebug( "%d: ERROR: Loading job data failed; readback failure" % self.id) return self.logger.reportDebug("%d: Job data loaded in %.3f seconds" % (self.id, time.time() - start_time))
def run(self): # This thread will be created upon every submit, as they may # come in sooner than the submits finish. # print("Block found on " + ctime()) print("Share found on " + ctime() + " nonce " + self.nonce.encode('hex_codec')) sys.stdout.flush() hrnonce = self.nonce[::-1].encode('hex') data = self.block[:152] + hrnonce + self.block[160:] sys.stdout.flush() hash_blake8 = BLAKE(256).digest( struct.pack("<20I", *struct.unpack(">20I", data.decode('hex')[:80]))) hash_str = hexlify(hash_blake8).decode() print('hash %s' % hash_str) if (os.name == "nt"): os.system("echo checkblake " + data + ">>logmine-ms.log") # Log file is runnable (rename .BAT) else: os.system( "echo ./checkblake " + data + ">>logmine-ms.log") # Log file is runnable as a shell script # Uncomment ONE of the following to configure if (hash_str[56:64] == "00000000" ): # Submit diff=1 shares (POOL mining) # if (hash_str[55:64] == "000000000"): # Only submit diff=16 shares to reduce blakecoind loading # if (hash_str[54:64] == "0000000000"): # Only submit diff=256 shares to reduce blakecoind loading try: print("submitting " + data) result = bitcoin.getwork(data) print("Upstream result: " + str(result)) except: print("RPC send error") result = False else: if (hash_str[56:64] == "00000000" ): # Submit diff=1 shares (POOL mining) print("Share not submitted") else: print("HARDWARE ERROR INVALID HASH") disp.hw_err = disp.hw_err + 1 # Probably needs some sort of lock, should use results_queue instead result = False sys.stdout.flush() results_queue.put(result)
def checkNonce(gold): staticDataUnpacked = unpack('<' + 'I'*19, gold.job.data.decode('hex')[:76]) staticData = pack('>' + 'I'*19, *staticDataUnpacked) hashInput = pack('>76sI', staticData, gold.nonce) # hashOutput = sha256(sha256(hashInput).digest()).digest() // ORIGINAL hashOutput = BLAKE(256).digest(hashInput) # hash_str = hexlify(hashOutput).decode() # print('hash_str %s' % hash_str) # blakecoin DEBUG #gnhex = "{0:08x}".format(gold.nonce) #gnhexrev = gnhex[6:8]+gnhex[4:6]+gnhex[2:4]+gnhex[0:2] #hrnonce = gnhexrev #print "\nhrnonce=",hrnonce # DEBUG #chkdata = gold.job.data[:152] + hrnonce + gold.job.data[160:] #if (os.name == "nt"): # os.system ("echo checkblake " + chkdata + ">>logmine-ms.log") # Log file is runnable (rename .BAT) # os.system ("checkblake " + chkdata) #else: # os.system ("echo ./checkblake " + chkdata + ">>logmine-ms.log") # Log file is runnable as a shell script # os.system ("./checkblake " + chkdata) # Bypass checkTarget and sumbit ALL diff 1 shares regardless of target. This may not be # appropriate with vardiff pools or solo mining, but is safer so is now the default behaviour return True # NB comment this out to enable target check if checkTarget(BASE_TARGET, hashOutput): logger.reportValid(gold.fpgaID) else: logger.reportError(hex(gold.nonce)[2:], gold.fpgaID) return False if checkTarget(gold.job.target.decode('hex'), hashOutput): return True return False
def run(self): while self.go: try: # work = bitcoin.getwork() # self.block = work['data'] # self.target = work['target'] print "Tested", self.nonce_tested, " passed", self.nonce_ok, " fail", self.nonce_fail, " unmatched", self.nonce_tested - self.nonce_ok - self.nonce_fail self.line = self.infile.readline() if (len(self.line) != 257): print "EOF on test data" # Or its an error, but let's not be worrysome # quit() # Except it doesn't ... self.go = False # Terminating threads is a bit tricksy break self.nonce_tested = self.nonce_tested + 1 self.block = self.line.rstrip() # Hard-code a diff=32 target for test work # Replace MSB 16 bits of target with clock (NB its reversed) self.target = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff07" + self.dynclock_hex self.dynclock_hex = "0000" # Once only # print("block old " + self.block) # We need to subtract a few from the nonces in order to match (why?) nonce_bin = self.block.decode('hex')[79:75:-1] self.nonce = int(nonce_bin.encode('hex'), 16) # print "nonce old =", self.nonce nonce_new = self.nonce - 500000 if (nonce_new < 0): nonce_new = 0 # print "nonce new =", nonce_new nonce_hex = "{0:08x}".format(nonce_new) # print "encoded = ", nonce_hex nonce_hex_rev = nonce_hex[6:8] + nonce_hex[4:6] + nonce_hex[ 2:4] + nonce_hex[0:2] # print "reversed = ", nonce_hex_rev self.block = self.block[0:152] + nonce_hex_rev + self.block[ 160:] # print("block new " + self.block) except: print("RPC getwork error") # In this case, keep crunching with the old data. It will get # stale at some point, but it's better than doing nothing. # print("block " + self.block + " target " + self.target) # DEBUG sdiff = self.target.decode('hex')[31:27:-1] intTarget = int(sdiff.encode('hex'), 16) if (intTarget < 1): print "WARNING zero target", intTarget print "target", self.target print("sdiff", sdiff ) # NB Need brackets here else prints binary self.target = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f0000" else: newdiff = 65536 / intTarget if (self.diff != newdiff): print "New target diff =", newdiff self.diff = newdiff # print("Sending data to FPGA") # DEBUG midstate_blake8 = BLAKE(256).midstate( struct.pack( "<16I", *struct.unpack(">16I", self.block.decode('hex')[:64]))) # print('midstate_blake8 %s' % (hexlify(midstate_blake8).decode())) midstate_blake8_swap = struct.pack( "<8I", *struct.unpack(">8I", midstate_blake8)) # print('midswap_blake8 %s' % (hexlify(midstate_blake8_conv).decode())) midstate = hexlify(midstate_blake8_swap) # for blakecoin send 16 bytes data plus midstate plus 4 bytes of 32 byte target (used for dynclock only) payload = self.target.decode('hex')[31:27:-1] + self.block.decode( 'hex')[79:63:-1] + midstate.decode('hex')[::-1] # print("NEW " + payload.encode('hex_codec')) # DEBUG # original payload # payload = self.target.decode('hex')[31:27:-1] + self.block.decode('hex')[79:63:-1] # TEST HASH, this should match on nonce 0000318f # NB The pool will REJECT this share as it did not send the data... # UNCOMMENT the following two lines for testing... # test_payload ="000000014eb4577c82473a069ca0e95703254da62e94d1902ab6f0eae8b1e718565775af20c9ba6ced48fc9915ef01c54da2200090801b2d2afc406264d491c7dfc7b0b251e91f141b44717e00310000ff070000" # payload = test_payload.decode('hex')[::-1] # This is probably best commented out unless debugging ... print("Test " + payload.encode('hex_codec')) # DEBUG ser.write(payload) result = golden.wait(askrate) if result: golden.clear()
def blakehash(b): return BLAKE(256).digest(b)
def calculate_hash(data): # return sha256(sha256(struct.pack("<20I", *struct.unpack(">20I", data[:80]))).digest()).digest() return BLAKE(256).digest( struct.pack("<20I", *struct.unpack(">20I", data[:80])))
def hexprivkey2addr(privkey_hex): print privkey_hex, " priv key HEX" # Encode the private key in WIF see https://en.bitcoin.it/wiki/Base58Check_encoding data_hex = "80" + privkey_hex data_bin = data_hex.decode('hex') # hash = hashlib.sha256(hashlib.sha256(data_bin).digest()).digest() hash_blake8 = BLAKE(256).digest(data_bin) sha_hex = hash_blake8.encode('hex_codec') step2_hex = data_hex + sha_hex[:8] privkey = b58encode(step2_hex.decode('hex')) # print privkey, " private key WIF" # Now print the compressed WIF private key # bitaddress.org appends 01 so try that ... data_hex = "80" + privkey_hex + "01" data_bin = data_hex.decode('hex') # hash = hashlib.sha256(hashlib.sha256(data_bin).digest()).digest() hash_blake8 = BLAKE(256).digest(data_bin) sha_hex = hash_blake8.encode('hex_codec') step2_hex = data_hex + sha_hex[:8] privkey = b58encode(step2_hex.decode('hex')) # print "==========================================================" print "BLAKE PRIVKEY..." # print privkey, " private key WIF (comp)" print privkey # print "==========================================================" # =========================================== # The next step is to convert to a public key # =========================================== secret = int(privkey_hex,16) # Need this as decimal number # generate pubkey pubkey = Public_key( g, g * secret ) # print 'pubkey', hex(pubkey.point.x()), hex(pubkey.point.y()) # This is clunky, there is probably a better way ... x_hex = hex(pubkey.point.x()) x_hex = x_hex[2:] # Remove leading 0x x_hex = x_hex[:-1] # Remove trailing L # Pad with leading 0's to 64 chars... x_hex = x_hex.zfill(64) # print "x", x_hex y_hex = hex(pubkey.point.y()) y_hex = y_hex[2:] # Remove leading 0x y_hex = y_hex[:-1] # Remove trailing L # Probably need to pad with leading 0's to 64 chars... y_hex = y_hex.zfill(64) # print "y", y_hex pubkey_hex = "04" + x_hex + y_hex # print pubkey_hex, " public key" # ================== # Address generation # ================== pubkey = pubkey_hex data_bin = pubkey.decode('hex') # First step is a SHA256 data_bin = hashlib.sha256(data_bin).digest() # Second step is RIPEMD160 hash = hashlib.new('ripemd160') hash.update(data_bin) hash_digest = hash.digest() hash_hex = hash.hexdigest() # print hash_hex + " uncompressed hash (pubkey)" # Now encode the address data_hex = "1a" + hash_hex # 1a is version byte for BLAKE (26 decimal) data_bin = data_hex.decode('hex') #hash = hashlib.sha256(hashlib.sha256(data_bin).digest()).digest() hash_blake8 = BLAKE(256).digest(data_bin) sha_hex = hash_blake8.encode('hex_codec') step2_hex = data_hex + sha_hex[:8] # print b58encode(step2_hex.decode('hex')), " address" # ============================================================================================ # Now do the same for the COMPRESSED public key (see https://bitcointalk.org/index.php?topic=205490.0) # ============================================================================================ lastchar = pubkey[-2:] val = lastchar.decode('hex') if (ord(val)%2): # print "odd" xpubkey = "03" + pubkey[2:66] else: # print "even" xpubkey = "02" + pubkey[2:66] # print xpubkey + " comp pubkey" data_bin = xpubkey.decode('hex') # First step is a SHA256 data_bin = hashlib.sha256(data_bin).digest() # Second step is RIPEMD160 hash = hashlib.new('ripemd160') hash.update(data_bin) hash_digest = hash.digest() hash_hex = hash.hexdigest() # Saves the encoding step # print hash_hex + " hash (compressed pubkey)" # Now encode the address # print "==========================================================" print "BLAKE ADDRESS..." data_hex = "1a" + hash_hex # 1a is version byte for BLAKE (26 decimal) data_bin = data_hex.decode('hex') # !!!! WRONG, need to use blake hash, not sha256d for checksum !!!! # hash = hashlib.sha256(hashlib.sha256(data_bin).digest()).digest() hash_blake8 = BLAKE(256).digest(data_bin) sha_hex = hash_blake8.encode('hex_codec') step2_hex = data_hex + sha_hex[:8] # print b58encode(step2_hex.decode('hex')), " compressed address" print b58encode(step2_hex.decode('hex'))
def blake256(s): return BLAKE(256).digest(s)