python script updates
This commit is contained in:
parent
f496533acc
commit
28363d8ae3
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
|
|
||||||
class zcash_fpga:
|
class zcash_fpga:
|
||||||
import serial
|
import serial
|
||||||
import codecs
|
import codecs
|
||||||
|
@ -19,13 +19,13 @@ class zcash_fpga:
|
||||||
'RESET_FPGA_RPL':int('80000000', 16),
|
'RESET_FPGA_RPL':int('80000000', 16),
|
||||||
'VERIFY_SECP256K1_SIG_RPL':int('80000101', 16)}
|
'VERIFY_SECP256K1_SIG_RPL':int('80000101', 16)}
|
||||||
|
|
||||||
fpga_msg_dict = {fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']:{'name':'FPGA_IGNORE_RPL', 'feilds':[(8, 'index', byt_to_hex), (1, 'bm', byt_to_hex)]},
|
fpga_msg_dict = {fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']:{'name':'VERIFY_SECP256K1_SIG_RPL', 'feilds':[(8, 'index', byt_to_hex), (1, 'bm', byt_to_hex)]},
|
||||||
fpga_msg_type_dict['FPGA_IGNORE_RPL']:{'name':'FPGA_IGNORE_RPL', 'feilds':[(8, 'ignored_header', byt_to_hex)]},
|
fpga_msg_type_dict['FPGA_IGNORE_RPL']:{'name':'FPGA_IGNORE_RPL', 'feilds':[(8, 'ignored_header', byt_to_hex)]},
|
||||||
fpga_msg_type_dict['FPGA_STATUS_RPL']:{'name':'FPGA_STATUS_RPL', 'feilds':[(4, 'version', byt_to_ver), (8, 'build_date', byt_to_str), (8, 'buid_host', byt_to_str), (8, 'cmd_cap', byt_to_hex)]},
|
fpga_msg_type_dict['FPGA_STATUS_RPL']:{'name':'FPGA_STATUS_RPL', 'feilds':[(4, 'version', byt_to_ver), (8, 'build_date', byt_to_str), (8, 'buid_host', byt_to_str), (8, 'cmd_cap', byt_to_hex)]},
|
||||||
fpga_msg_type_dict['RESET_FPGA_RPL']:{'name':'RESET_FPGA_RPL', 'feilds':[]}}
|
fpga_msg_type_dict['RESET_FPGA_RPL']:{'name':'RESET_FPGA_RPL', 'feilds':[]}}
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, COM='COM4'):
|
def __init__(self, COM='COM4'):
|
||||||
self.s = self.serial.Serial(COM, 921600, timeout=1)
|
self.s = self.serial.Serial(COM, 921600, timeout=1)
|
||||||
#Test getting FPGA status
|
#Test getting FPGA status
|
||||||
self.get_status()
|
self.get_status()
|
||||||
|
@ -42,9 +42,9 @@ class zcash_fpga:
|
||||||
# Parse reply - should be reset
|
# Parse reply - should be reset
|
||||||
res = self.get_reply()
|
res = self.get_reply()
|
||||||
if (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['RESET_FPGA_RPL']):
|
if (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['RESET_FPGA_RPL']):
|
||||||
print("ERROR: Reply type was not RESET_FPGA_RPL")
|
print("ERROR: Reply type was not RESET_FPGA_RPL")
|
||||||
|
|
||||||
def get_reply(self):
|
def get_reply(self):
|
||||||
res = self.s.read(1024)
|
res = self.s.read(1024)
|
||||||
if len(res) > 0:
|
if len(res) > 0:
|
||||||
self.print_reply(res)
|
self.print_reply(res)
|
||||||
|
@ -52,9 +52,9 @@ class zcash_fpga:
|
||||||
else:
|
else:
|
||||||
print ("INFO: No reply received")
|
print ("INFO: No reply received")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def secp256k1_verify_sig(self, index, hsh, r, s, Qx, Qy):
|
def secp256k1_verify_sig(self, index, hsh, r, s, Qx, Qy):
|
||||||
cmd = '00000101000000B0'
|
cmd = '00000101000000B0'
|
||||||
cmd = format(index, 'x').ljust(16, '0') + cmd
|
cmd = format(index, 'x').ljust(16, '0') + cmd
|
||||||
cmd = format(s, 'x').ljust(64, '0') + cmd
|
cmd = format(s, 'x').ljust(64, '0') + cmd
|
||||||
cmd = format(r, 'x').ljust(64, '0') + cmd
|
cmd = format(r, 'x').ljust(64, '0') + cmd
|
||||||
|
@ -68,7 +68,16 @@ class zcash_fpga:
|
||||||
res = self.get_reply()
|
res = self.get_reply()
|
||||||
if res is not None and (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']):
|
if res is not None and (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']):
|
||||||
print("ERROR: Reply type was not VERIFY_SECP256K1_SIG_RPL")
|
print("ERROR: Reply type was not VERIFY_SECP256K1_SIG_RPL")
|
||||||
|
return False
|
||||||
|
if (self.struct.unpack('<I', res[8:16])[0] != index):
|
||||||
|
print("ERROR: Index did not match")
|
||||||
|
return False
|
||||||
|
if (self.struct.unpack('<I', res[16:17])[0] != 0):
|
||||||
|
print("ERROR: Result bitmask was non-zero")
|
||||||
|
return False
|
||||||
|
print("INFO: Secp256k1 signature verified correctly")
|
||||||
|
return True
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.s.close()
|
self.s.close()
|
||||||
print("Closed...")
|
print("Closed...")
|
||||||
|
@ -82,7 +91,7 @@ class zcash_fpga:
|
||||||
print("ERROR: Message length mismatch")
|
print("ERROR: Message length mismatch")
|
||||||
cmd = (self.struct.unpack('<I', msg[4:8])[0])
|
cmd = (self.struct.unpack('<I', msg[4:8])[0])
|
||||||
if (cmd not in self.fpga_msg_dict):
|
if (cmd not in self.fpga_msg_dict):
|
||||||
print("ERROR: Unknown message type:", cmd)
|
print("ERROR: Unknown message type:", cmd)
|
||||||
|
|
||||||
print ("INFO: Received ", self.fpga_msg_dict[cmd]['name'])
|
print ("INFO: Received ", self.fpga_msg_dict[cmd]['name'])
|
||||||
offset = 8
|
offset = 8
|
||||||
|
@ -94,7 +103,8 @@ class zcash_fpga:
|
||||||
#Example usages:
|
#Example usages:
|
||||||
def example_secp256k1_sig():
|
def example_secp256k1_sig():
|
||||||
zf = zcash_fpga()
|
zf = zcash_fpga()
|
||||||
|
zf.reset_fpga() # Reset incase somethign went wrong last run
|
||||||
|
|
||||||
index = 1234
|
index = 1234
|
||||||
hsh = 34597931798561447004034205848155169322219865803759328163562698792725658370004
|
hsh = 34597931798561447004034205848155169322219865803759328163562698792725658370004
|
||||||
r = 550117237093786687120086685263208063857013211911888854762107796665370524299
|
r = 550117237093786687120086685263208063857013211911888854762107796665370524299
|
||||||
|
|
Loading…
Reference in New Issue