forked from zcash/zcash-test-vectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip_0243.py
175 lines (149 loc) · 5.04 KB
/
zip_0243.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
from pyblake2 import blake2b
import struct
from transaction import (
MAX_MONEY,
SAPLING_TX_VERSION,
Script,
Transaction,
)
from tv_output import render_args, render_tv, Some
from tv_rand import Rand
from zip_0143 import (
getHashJoinSplits,
getHashOutputs,
getHashPrevouts,
getHashSequence,
NOT_AN_INPUT,
SIGHASH_ALL,
SIGHASH_ANYONECANPAY,
SIGHASH_NONE,
SIGHASH_SINGLE,
)
def getHashShieldedSpends(tx):
digest = blake2b(digest_size=32, person=b'ZcashSSpendsHash')
for desc in tx.vShieldedSpends:
# We don't pass in serialized form of desc as spendAuthSig is not part of the hash
digest.update(bytes(desc.cv))
digest.update(bytes(desc.anchor))
digest.update(desc.nullifier)
digest.update(bytes(desc.rk))
digest.update(bytes(desc.proof))
return digest.digest()
def getHashShieldedOutputs(tx):
digest = blake2b(digest_size=32, person=b'ZcashSOutputHash')
for desc in tx.vShieldedOutputs:
digest.update(bytes(desc))
return digest.digest()
def signature_hash(scriptCode, tx, nIn, nHashType, amount, consensusBranchId):
hashPrevouts = b'\x00'*32
hashSequence = b'\x00'*32
hashOutputs = b'\x00'*32
hashJoinSplits = b'\x00'*32
hashShieldedSpends = b'\x00'*32
hashShieldedOutputs = b'\x00'*32
if not (nHashType & SIGHASH_ANYONECANPAY):
hashPrevouts = getHashPrevouts(tx)
if (not (nHashType & SIGHASH_ANYONECANPAY)) and \
(nHashType & 0x1f) != SIGHASH_SINGLE and \
(nHashType & 0x1f) != SIGHASH_NONE:
hashSequence = getHashSequence(tx)
if (nHashType & 0x1f) != SIGHASH_SINGLE and \
(nHashType & 0x1f) != SIGHASH_NONE:
hashOutputs = getHashOutputs(tx)
elif (nHashType & 0x1f) == SIGHASH_SINGLE and \
0 <= nIn and nIn < len(tx.vout):
digest = blake2b(digest_size=32, person=b'ZcashOutputsHash')
digest.update(bytes(tx.vout[nIn]))
hashOutputs = digest.digest()
if len(tx.vJoinSplit) > 0:
hashJoinSplits = getHashJoinSplits(tx)
if len(tx.vShieldedSpends) > 0:
hashShieldedSpends = getHashShieldedSpends(tx)
if len(tx.vShieldedOutputs) > 0:
hashShieldedOutputs = getHashShieldedOutputs(tx)
digest = blake2b(
digest_size=32,
person=b'ZcashSigHash' + struct.pack('<I', consensusBranchId),
)
digest.update(struct.pack('<I', tx.header()))
digest.update(struct.pack('<I', tx.nVersionGroupId))
digest.update(hashPrevouts)
digest.update(hashSequence)
digest.update(hashOutputs)
digest.update(hashJoinSplits)
digest.update(hashShieldedSpends)
digest.update(hashShieldedOutputs)
digest.update(struct.pack('<I', tx.nLockTime))
digest.update(struct.pack('<I', tx.nExpiryHeight))
digest.update(struct.pack('<Q', tx.valueBalance))
digest.update(struct.pack('<I', nHashType))
if nIn != NOT_AN_INPUT:
digest.update(bytes(tx.vin[nIn].prevout))
digest.update(bytes(scriptCode))
digest.update(struct.pack('<Q', amount))
digest.update(struct.pack('<I', tx.vin[nIn].nSequence))
return digest.digest()
def main():
args = render_args()
from random import Random
rng = Random(0xabad533d)
def randbytes(l):
ret = []
while len(ret) < l:
ret.append(rng.randrange(0, 256))
return bytes(ret)
rand = Rand(randbytes)
consensusBranchId = 0x76b809bb # Sapling
test_vectors = []
for _ in range(10):
tx = Transaction(rand, SAPLING_TX_VERSION)
scriptCode = Script(rand)
nIn = rand.u8() % (len(tx.vin) + 1)
if nIn == len(tx.vin):
nIn = NOT_AN_INPUT
nHashType = SIGHASH_ALL if nIn == NOT_AN_INPUT else rand.a([
SIGHASH_ALL,
SIGHASH_NONE,
SIGHASH_SINGLE,
SIGHASH_ALL | SIGHASH_ANYONECANPAY,
SIGHASH_NONE | SIGHASH_ANYONECANPAY,
SIGHASH_SINGLE | SIGHASH_ANYONECANPAY,
])
amount = rand.u64() % (MAX_MONEY + 1)
sighash = signature_hash(
scriptCode,
tx,
nIn,
nHashType,
amount,
consensusBranchId,
)
test_vectors.append({
'tx': bytes(tx),
'script_code': scriptCode.raw(),
'transparent_input': nIn,
'hash_type': nHashType,
'amount': amount,
'consensus_branch_id': consensusBranchId,
'sighash': sighash,
})
render_tv(
args,
'zip_0243',
(
('tx', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('script_code', 'Vec<u8>'),
('transparent_input', {
'rust_type': 'Option<u32>',
'rust_fmt': lambda x: None if x == -1 else Some(x),
}),
('hash_type', 'u32'),
('amount', 'i64'),
('consensus_branch_id', 'u32'),
('sighash', '[u8; 32]'),
),
test_vectors,
)
if __name__ == '__main__':
main()