2018-06-04 21:32:30 -07:00
|
|
|
from binascii import hexlify
|
|
|
|
|
|
|
|
|
2018-06-04 21:10:43 -07:00
|
|
|
def chunk(h):
|
|
|
|
h = str(h, 'utf-8')
|
|
|
|
return '0x' + ', 0x'.join([h[i:i+2] for i in range(0, len(h), 2)])
|
2018-06-04 21:32:30 -07:00
|
|
|
|
2018-06-04 22:30:16 -07:00
|
|
|
def tv_bytes_rust(name, value, pad):
|
2018-06-04 22:06:05 -07:00
|
|
|
print('''%s%s: [
|
|
|
|
%s%s
|
|
|
|
%s],''' % (
|
|
|
|
pad,
|
|
|
|
name,
|
|
|
|
pad,
|
|
|
|
chunk(hexlify(value)),
|
|
|
|
pad,
|
|
|
|
))
|
2018-06-04 21:32:30 -07:00
|
|
|
|
2018-06-04 22:30:16 -07:00
|
|
|
def tv_int_rust(name, value, pad):
|
|
|
|
print('%s%s: %d,' % (pad, name, value))
|
|
|
|
|
|
|
|
def tv_part_rust(name, value, indent=3):
|
|
|
|
pad = ' ' * indent
|
|
|
|
if type(value) == bytes:
|
|
|
|
tv_bytes_rust(name, value, pad)
|
|
|
|
elif type(value) == int:
|
|
|
|
tv_int_rust(name, value, pad)
|
|
|
|
else:
|
|
|
|
raise ValueError('Invalid type(%s): %s' % (name, type(value)))
|
|
|
|
|
2018-06-04 21:32:30 -07:00
|
|
|
def tv_rust(filename, parts, vectors):
|
|
|
|
print(' struct TestVector {')
|
|
|
|
[print(' %s: %s,' % p) for p in parts]
|
|
|
|
print(''' };
|
|
|
|
|
|
|
|
// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/%s.py''' % (
|
|
|
|
filename,
|
|
|
|
))
|
|
|
|
if type(vectors) == type({}):
|
|
|
|
print(' let test_vector = TestVector {')
|
|
|
|
[tv_part_rust(p[0], vectors[p[0]]) for p in parts]
|
|
|
|
print(' };')
|
2018-06-04 22:06:05 -07:00
|
|
|
elif type(vectors) == type([]):
|
|
|
|
print(' let test_vectors = vec![')
|
|
|
|
for vector in vectors:
|
|
|
|
print(' TestVector {')
|
|
|
|
[tv_part_rust(p[0], vector[p[0]], 4) for p in parts]
|
|
|
|
print(' },')
|
|
|
|
print(' ];')
|
2018-06-04 21:32:30 -07:00
|
|
|
else:
|
|
|
|
raise ValueError('Invalid type(vectors)')
|