diff --git a/sapling_generators.py b/sapling_generators.py index 7ebb5ba..3007c6d 100644 --- a/sapling_generators.py +++ b/sapling_generators.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 -from binascii import hexlify from pyblake2 import blake2s from sapling_jubjub import Point, JUBJUB_COFACTOR -from tv_output import chunk +from tv_output import tv_rust # First 64 bytes of the BLAKE2s input during group hash. # This is chosen to be some random string that we couldn't have @@ -51,44 +50,25 @@ VALUE_COMMITMENT_RANDOMNESS_BASE = find_group_hash(b'Zcash_cv', b'r') def main(): - print(''' - struct SaplingGenerators { - skb: [u8; 32], - pkb: [u8; 32], - npb: [u8; 32], - wprb: [u8; 32], - vcvb: [u8; 32], - vcrb: [u8; 32], - }; - - // From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_generators.py - let sapling_generators = SaplingGenerators { - skb: [ - %s - ], - pkb: [ - %s - ], - npb: [ - %s - ], - wprb: [ - %s - ], - vcvb: [ - %s - ], - vcrb: [ - %s - ], - };''' % ( - chunk(hexlify(bytes(SPENDING_KEY_BASE))), - chunk(hexlify(bytes(PROVING_KEY_BASE))), - chunk(hexlify(bytes(NOTE_POSITION_BASE))), - chunk(hexlify(bytes(WINDOWED_PEDERSEN_RANDOMNESS_BASE))), - chunk(hexlify(bytes(VALUE_COMMITMENT_VALUE_BASE))), - chunk(hexlify(bytes(VALUE_COMMITMENT_RANDOMNESS_BASE))), - )) + tv_rust( + 'sapling_generators', + ( + ('skb', '[u8; 32]'), + ('pkb', '[u8; 32]'), + ('npb', '[u8; 32]'), + ('wprb', '[u8; 32]'), + ('vcvb', '[u8; 32]'), + ('vcrb', '[u8; 32]'), + ), + { + 'skb': bytes(SPENDING_KEY_BASE), + 'pkb': bytes(PROVING_KEY_BASE), + 'npb': bytes(NOTE_POSITION_BASE), + 'wprb': bytes(WINDOWED_PEDERSEN_RANDOMNESS_BASE), + 'vcvb': bytes(VALUE_COMMITMENT_VALUE_BASE), + 'vcrb': bytes(VALUE_COMMITMENT_RANDOMNESS_BASE), + }, + ) if __name__ == '__main__': diff --git a/tv_output.py b/tv_output.py index f8c389a..b0a848d 100644 --- a/tv_output.py +++ b/tv_output.py @@ -1,3 +1,29 @@ +from binascii import hexlify + + def chunk(h): h = str(h, 'utf-8') return '0x' + ', 0x'.join([h[i:i+2] for i in range(0, len(h), 2)]) + +def tv_part_rust(name, value): + print(''' %s: [ + %s + ],''' % ( + name, + chunk(hexlify(value)) + )) + +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(' };') + else: + raise ValueError('Invalid type(vectors)')