Extract sapling_generators Rust rendering

This commit is contained in:
Jack Grigg 2018-06-05 16:32:30 +12:00
parent 7e37b8cc24
commit 9f4a41dcf3
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 46 additions and 40 deletions

View File

@ -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__':

View File

@ -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)')