From 704a2ac14d987f10e5e79c03903bbb472c35aa3d Mon Sep 17 00:00:00 2001 From: Ariel Gabizon Date: Wed, 13 Jun 2018 15:45:05 +0200 Subject: [PATCH] Implement ZIP 143 test vector generation No support for JoinSplits yet. Co-authored-by: Jack Grigg --- transaction.py | 117 ++++++++++++++++++++++++++++++++++++ zip_0143.py | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 transaction.py create mode 100644 zip_0143.py diff --git a/transaction.py b/transaction.py new file mode 100644 index 0000000..8cadb4a --- /dev/null +++ b/transaction.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 +import struct + +MAX_MONEY = 21000000 * 100000000 +TX_EXPIRY_HEIGHT_THRESHOLD = 500000000 + +OVERWINTER_VERSION_GROUP_ID = 0x03C48270 +OVERWINTER_TX_VERSION = 3 + + +RAND_OPCODES = [ + 0x00, # OP_FALSE, + 0x51, # OP_1, + 0x52, # OP_2, + 0x53, # OP_3, + 0xac, # OP_CHECKSIG, + 0x63, # OP_IF, + 0x65, # OP_VERIF, + 0x6a, # OP_RETURN, +] + +class Script(object): + def __init__(self, rand): + self._script = bytes([ + rand.a(RAND_OPCODES) for i in range(rand.u8() % 10) + ]) + + def raw(self): + return self._script + + def __bytes__(self): + return struct.pack('b', len(self._script)) + self._script + + +class OutPoint(object): + def __init__(self, rand): + self.txid = rand.b(32) + self.n = rand.u32() + + def __bytes__(self): + return self.txid + struct.pack('= 2: + ret += struct.pack('b', 0) + + return ret diff --git a/zip_0143.py b/zip_0143.py new file mode 100644 index 0000000..b2b279c --- /dev/null +++ b/zip_0143.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python3 +from pyblake2 import blake2b +import struct + +from transaction import ( + MAX_MONEY, + OVERWINTER_TX_VERSION, + Script, + Transaction, +) +from tv_output import render_args, render_tv +from tv_rand import Rand + + +SIGHASH_ALL = 1 +SIGHASH_NONE = 2 +SIGHASH_SINGLE = 3 +SIGHASH_ANYONECANPAY = 0x80 + +NOT_AN_INPUT = -1 # For portability of the test vectors + +def getHashPrevouts(tx): + digest = blake2b(digest_size=32, person=b'ZcashPrevoutHash') + for x in tx.vin: + digest.update(bytes(x.prevout)) + return digest.digest() + +def getHashSequence(tx): + digest = blake2b(digest_size=32, person=b'ZcashSequencHash') + for x in tx.vin: + digest.update(struct.pack(''), + ('scriptCode', 'Vec'), + ('nIn', 'u32'), + ('nHashType', 'u32'), + ('amount', 'u64'), + ('consensusBranchId', 'u32'), + ('sighash', '[u8; 32]'), + ), + test_vectors, + ) + + +if __name__ == '__main__': + main()