Extract sapling_signatures Rust rendering

This commit is contained in:
Jack Grigg 2018-06-05 17:06:05 +12:00
parent 9f4a41dcf3
commit d5d7d70a65
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 45 additions and 59 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from binascii import hexlify
import os import os
from pyblake2 import blake2b from pyblake2 import blake2b
@ -7,7 +6,7 @@ from sapling_generators import SPENDING_KEY_BASE
from sapling_jubjub import Fr, Point, r_j from sapling_jubjub import Fr, Point, r_j
from sapling_key_components import to_scalar from sapling_key_components import to_scalar
from sapling_utils import cldiv, leos2ip from sapling_utils import cldiv, leos2ip
from tv_output import chunk from tv_output import tv_rust
def H(x): def H(x):
@ -75,20 +74,7 @@ def main():
return bytes(ret) return bytes(ret)
rj = RedJubjub(SPENDING_KEY_BASE, randbytes) rj = RedJubjub(SPENDING_KEY_BASE, randbytes)
print(''' test_vectors = []
struct TestVector {
sk: [u8; 32],
vk: [u8; 32],
alpha: [u8; 32],
rsk: [u8; 32],
rvk: [u8; 32],
m: [u8; 32],
sig: [u8; 64],
rsig: [u8; 64],
};
// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_signatures.py
let test_vectors = vec![''')
for i in range(0, 10): for i in range(0, 10):
sk = rj.gen_private() sk = rj.gen_private()
vk = rj.derive_public(sk) vk = rj.derive_public(sk)
@ -104,42 +90,31 @@ def main():
assert not rj.verify(vk, M, rsig) assert not rj.verify(vk, M, rsig)
assert not rj.verify(rvk, M, sig) assert not rj.verify(rvk, M, sig)
print(''' TestVector { test_vectors.append({
sk: [ 'sk': bytes(sk),
%s 'vk': bytes(vk),
], 'alpha': bytes(alpha),
vk: [ 'rsk': bytes(rsk),
%s 'rvk': bytes(rvk),
], 'm': M,
alpha: [ 'sig': sig,
%s 'rsig': rsig,
], })
rsk: [
%s tv_rust(
], 'sapling_signatures',
rvk: [ (
%s ('sk', '[u8; 32]'),
], ('vk', '[u8; 32]'),
m: [ ('alpha', '[u8; 32]'),
%s ('rsk', '[u8; 32]'),
], ('rvk', '[u8; 32]'),
sig: [ ('m', '[u8; 32]'),
%s ('sig', '[u8; 64]'),
], ('rsig', '[u8; 64]'),
rsig: [ ),
%s test_vectors,
], )
},''' % (
chunk(hexlify(bytes(sk))),
chunk(hexlify(bytes(vk))),
chunk(hexlify(bytes(alpha))),
chunk(hexlify(bytes(rsk))),
chunk(hexlify(bytes(rvk))),
chunk(hexlify(M)),
chunk(hexlify(sig)),
chunk(hexlify(rsig)),
))
print(' ];')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -5,13 +5,17 @@ def chunk(h):
h = str(h, 'utf-8') h = str(h, 'utf-8')
return '0x' + ', 0x'.join([h[i:i+2] for i in range(0, len(h), 2)]) return '0x' + ', 0x'.join([h[i:i+2] for i in range(0, len(h), 2)])
def tv_part_rust(name, value): def tv_part_rust(name, value, indent=3):
print(''' %s: [ pad = ' ' * indent
%s print('''%s%s: [
],''' % ( %s%s
name, %s],''' % (
chunk(hexlify(value)) pad,
)) name,
pad,
chunk(hexlify(value)),
pad,
))
def tv_rust(filename, parts, vectors): def tv_rust(filename, parts, vectors):
print(' struct TestVector {') print(' struct TestVector {')
@ -25,5 +29,12 @@ def tv_rust(filename, parts, vectors):
print(' let test_vector = TestVector {') print(' let test_vector = TestVector {')
[tv_part_rust(p[0], vectors[p[0]]) for p in parts] [tv_part_rust(p[0], vectors[p[0]]) for p in parts]
print(' };') print(' };')
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(' ];')
else: else:
raise ValueError('Invalid type(vectors)') raise ValueError('Invalid type(vectors)')