Extract sapling_key_components Rust rendering

This commit is contained in:
Jack Grigg 2018-06-05 17:30:16 +12:00
parent d5d7d70a65
commit 68263af2b5
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 52 additions and 80 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from binascii import hexlify
from pyblake2 import blake2b, blake2s from pyblake2 import blake2b, blake2s
from sapling_generators import PROVING_KEY_BASE, SPENDING_KEY_BASE, group_hash from sapling_generators import PROVING_KEY_BASE, SPENDING_KEY_BASE, group_hash
@ -7,7 +6,7 @@ from sapling_jubjub import Fr
from sapling_merkle_tree import MERKLE_DEPTH from sapling_merkle_tree import MERKLE_DEPTH
from sapling_notes import note_commit, note_nullifier from sapling_notes import note_commit, note_nullifier
from sapling_utils import leos2bsp, leos2ip from sapling_utils import leos2bsp, leos2ip
from tv_output import chunk from tv_output import tv_rust
# #
# Utilities # Utilities
@ -92,26 +91,7 @@ class SpendingKey(object):
def main(): def main():
print(''' test_vectors = []
struct TestVector {
sk: [u8; 32],
ask: [u8; 32],
nsk: [u8; 32],
ovk: [u8; 32],
ak: [u8; 32],
nk: [u8; 32],
ivk: [u8; 32],
default_d: [u8; 11],
default_pk_d: [u8; 32],
note_v: u64,
note_r: [u8; 32],
note_cm: [u8; 32],
note_pos: u64,
note_nf: [u8; 32],
};
// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_key_components.py
let test_vectors = vec![''')
for i in range(0, 10): for i in range(0, 10):
sk = SpendingKey(bytes([i] * 32)) sk = SpendingKey(bytes([i] * 32))
note_v = (2548793025584392057432895043257984320*i) % 2**64 note_v = (2548793025584392057432895043257984320*i) % 2**64
@ -123,62 +103,43 @@ def main():
note_v) note_v)
note_pos = (980705743285409327583205473820957432*i) % 2**MERKLE_DEPTH note_pos = (980705743285409327583205473820957432*i) % 2**MERKLE_DEPTH
note_nf = note_nullifier(sk.nk(), note_cm, Fr(note_pos)) note_nf = note_nullifier(sk.nk(), note_cm, Fr(note_pos))
print(''' TestVector { test_vectors.append({
sk: [ 'sk': sk.data,
%s 'ask': bytes(sk.ask()),
], 'nsk': bytes(sk.nsk()),
ask: [ 'ovk': sk.ovk(),
%s 'ak': bytes(sk.ak()),
], 'nk': bytes(sk.nk()),
nsk: [ 'ivk': bytes(sk.ivk()),
%s 'default_d': sk.default_d(),
], 'default_pk_d': bytes(sk.default_pkd()),
ovk: [ 'note_v': note_v,
%s 'note_r': bytes(note_r),
], 'note_cm': bytes(note_cm.u),
ak: [ 'note_pos': note_pos,
%s 'note_nf': note_nf,
], })
nk: [
%s tv_rust(
], 'sapling_key_components',
ivk: [ (
%s ('sk', '[u8; 32]'),
], ('ask', '[u8; 32]'),
default_d: [ ('nsk', '[u8; 32]'),
%s ('ovk', '[u8; 32]'),
], ('ak', '[u8; 32]'),
default_pk_d: [ ('nk', '[u8; 32]'),
%s ('ivk', '[u8; 32]'),
], ('default_d', '[u8; 11]'),
note_v: %s, ('default_pk_d', '[u8; 32]'),
note_r: [ ('note_v', 'u64'),
%s ('note_r', '[u8; 32]'),
], ('note_cm', '[u8; 32]'),
note_cm: [ ('note_pos', 'u64'),
%s ('note_nf', '[u8; 32]'),
], ),
note_pos: %s, test_vectors,
note_nf: [ )
%s
],
},''' % (
chunk(hexlify(sk.data)),
chunk(hexlify(bytes(sk.ask()))),
chunk(hexlify(bytes(sk.nsk()))),
chunk(hexlify(sk.ovk())),
chunk(hexlify(bytes(sk.ak()))),
chunk(hexlify(bytes(sk.nk()))),
chunk(hexlify(bytes(sk.ivk()))),
chunk(hexlify(sk.default_d())),
chunk(hexlify(bytes(sk.default_pkd()))),
note_v,
chunk(hexlify(bytes(note_r))),
chunk(hexlify(bytes(note_cm.u))),
note_pos,
chunk(hexlify(note_nf)),
))
print(' ];')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -5,8 +5,7 @@ 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, indent=3): def tv_bytes_rust(name, value, pad):
pad = ' ' * indent
print('''%s%s: [ print('''%s%s: [
%s%s %s%s
%s],''' % ( %s],''' % (
@ -17,6 +16,18 @@ def tv_part_rust(name, value, indent=3):
pad, pad,
)) ))
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)))
def tv_rust(filename, parts, vectors): def tv_rust(filename, parts, vectors):
print(' struct TestVector {') print(' struct TestVector {')
[print(' %s: %s,' % p) for p in parts] [print(' %s: %s,' % p) for p in parts]