-
Notifications
You must be signed in to change notification settings - Fork 0
/
zclib.py
347 lines (296 loc) · 7.8 KB
/
zclib.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# monadic(!) crypto fun library
import segwit_addr
from collections import namedtuple
class ParseException(Exception):
pass
class GenerateException(Exception):
pass
class SignException(Exception):
pass
def getStr(st, byte_length):
if len(st) < byte_length:
raise ParseException("getStr failed")
return (st[byte_length:], st[:byte_length])
def getStrReversed(st, byte_length):
(st, data) = getStr(st, byte_length)
return (st, data[::-1])
def putStr(st, data):
return st + data
def putStrReversed(st, data):
return st + data[::-1]
def getIntLE(st, byte_length):
(st, data) = getStr(st, byte_length)
num = int.from_bytes(data, 'little')
return (st, num)
def putIntLE(st, byte_length, num):
data = num.to_bytes(byte_length, 'little')
return st + data
def getConst(st, data):
if not st[:len(data)] == data:
raise ParseException("getConst match failed")
return (st[len(data):], data)
putConst = putStr
def getOr(st, *funcs):
for f in funcs:
try:
(st, ret) = f(st)
return (st, ret)
except ParseException:
pass
raise ParseException("getOr all failed")
def getThen(st, *funcs):
for f in funcs:
(st, ret) = f(st)
return (st, ret)
def putThen(st, *funcs):
for f in funcs:
st = f(st)
return st
def getLoop(st, func, num):
retlist = []
for i in range(num):
(st, ret) = func(st)
retlist.append(ret)
return (st, retlist)
def putMap(st, func, arglist):
return putThen(st, *map(lambda x: c(func, x), arglist))
# make a function with no arguments other than st.
def c(func, *args):
return (lambda st: func(st, *args))
def getVI(st):
return getOr(st,
c(getThen, c(getConst, b"\xff"), c(getIntLE, 8)),
c(getThen, c(getConst, b"\xfe"), c(getIntLE, 4)),
c(getThen, c(getConst, b"\xfd"), c(getIntLE, 2)),
c(getIntLE, 1)
)
def putVI(st, num):
if num <= 0xfc:
return putIntLE(st, 1, num)
elif num <= 0xffff:
return putIntLE(putStr(st, b"\xfd"), 2, num)
elif num <= 0xffffffff:
return putIntLE(putStr(st, b"\xfe"), 4, num)
elif num <= 0xffffffffffffffff:
return putIntLE(putStr(st, b"\xff"), 8, num)
else:
raise GenerateException("putVI int too big")
Txin = namedtuple("Txin", ("hash", "index", "script", "seqno"))
def getTxin(st):
(st, hash) = getStrReversed(st, 32)
(st, index) = getIntLE(st, 4)
(st, vi) = getVI(st)
(st, script) = getStr(st, vi)
(st, seqno) = getIntLE(st, 4)
return (st, Txin(hash, index, script, seqno))
def putTxin(st, txin):
st = putStrReversed(st, txin.hash)
st = putIntLE(st, 4, txin.index)
st = putVI(st, len(txin.script))
st = putStr(st, txin.script)
st = putIntLE(st, 4, txin.seqno)
return st
Txout = namedtuple("Txout", ("amount", "script"))
def getTxout(st):
(st, amount) = getIntLE(st, 8)
(st, vi) = getVI(st)
(st, script) = getStr(st, vi)
return (st, Txout(amount, script))
def putTxout(st, txout):
st = putIntLE(st, 8, txout.amount)
st = putVI(st, len(txout.script))
st = putStr(st, txout.script)
return st
JoinSplit = namedtuple("JoinSplit", (
"vpub_old",
"vpub_new",
"anchor",
"nullifiers",
"commitments",
"ephemeralKey",
"randomSeed",
"vmacs",
"zkproof",
"encCiphertexts"
))
def getJoinSplit(st, version):
(st, vpub_old) = getIntLE(st, 8)
(st, vpub_new) = getIntLE(st, 8)
(st, anchor) = getStr(st, 32)
(st, nullifiers) = getLoop(st, c(getStr, 32), 2)
(st, commitments) = getLoop(st, c(getStr, 32), 2)
(st, ephemeralKey) = getStr(st, 32)
(st, randomSeed) = getStr(st, 32)
(st, vmacs) = getLoop(st, c(getStr, 32), 2)
if version == 2 or version == 3:
(st, zkproof) = getStr(st, 296)
else:
(st, zkproof) = getStr(st, 192)
(st, encCiphertexts) = getLoop(st, c(getStr, 601), 2)
return JoinSplit(
vpub_old,
vpub_new,
anchor,
nullifiers,
commitments,
ephemeralKey,
randomSeed,
vmacs,
zkproof,
encCiphertexts
)
ShieldedSpend = namedtuple("ShieldedSpend", (
"cv",
"anchor",
"nullifier",
"rk",
"zkproof",
"spendAuthSig",
))
def getShieldedSpend(st):
(st, cv) = getStr(st, 32)
(st, anchor) = getStr(st, 32)
(st, nullifier) = getStr(st, 32)
(st, rk) = getStr(st, 32)
(st, zkproof) = getStr(st, 192)
(st, spendAuthSig) = getStr(st, 64)
return (st, ShieldedSpend(
cv,
anchor,
nullifier,
rk,
zkproof,
spendAuthSig
))
ShieldedOutput = namedtuple("ShieldedOutput", (
"cv",
"cmu",
"ephemeralKey",
"encCipherText",
"outCipherText",
"zkproof",
))
def getShieldedOutput(st):
(st, cv) = getStr(st, 32)
(st, cmu) = getStr(st, 32)
(st, ephemeralKey) = getStr(st, 32)
(st, encCipherText) = getStr(st, 580)
(st, outCipherText) = getStr(st, 80)
(st, zkproof) = getStr(st, 192)
return (st, ShieldedOutput(
cv,
cmu,
ephemeralKey,
encCipherText,
outCipherText,
zkproof,
))
Transaction = namedtuple("Transaction", (
"version",
"groupid",
"txins",
"txouts",
"locktime",
"expiry",
"valuebalance",
"shieldedspends",
"shieldedoutputs",
"joinsplits",
"joinsplitpubkey",
"joinsplitsig",
"bindingsig"
))
def getTransaction(st):
(st, version) = getIntLE(st, 4)
version = version & 0x7FFFFFFF
groupid = None
if version >= 3:
(st, groupid) = getIntLE(st, 4)
(st, numTxins) = getVI(st)
(st, txins) = getLoop(st, getTxin, numTxins)
(st, numTxouts) = getVI(st)
(st, txouts) = getLoop(st, getTxout, numTxouts)
(st, locktime) = getIntLE(st, 4)
expiry = None
if version >= 3:
(st, expiry) = getIntLE(st, 4)
valuebalance = None
shieldedspends = None
shieldedoutputs = None
if version >= 4:
(st, valuebalance) = getIntLE(st, 8) # TODO to signed value
(st, numShieldedspends) = getVI(st)
(st, shieldedspends) = getLoop(st, getShieldedSpend, numShieldedspends)
(st, numShieldedoutputs) = getVI(st)
(st, shieldedoutputs) = getLoop(st, getShieldedOutput, numShieldedoutputs)
joinsplits = None
joinsplitpubkey = None
joinsplitsig = None
if version >= 2:
(st, numJoinSplits) = getVI(st)
(st, joinsplits) = getLoop(st, c(getJoinSplit, version), numJoinSplits)
if numJoinSplits > 0:
(st, joinsplitpubkey) = getStr(st, 32)
(st, joinsplitsig) = getStr(st, 64)
bindingsig = None
if version >= 4 and numShieldedspends + numShieldedoutputs > 0:
(st, bindingsig) = getStr(st, 64)
return (st, Transaction(
version,
groupid,
txins,
txouts,
locktime,
expiry,
valuebalance,
shieldedspends,
shieldedoutputs,
joinsplits,
joinsplitpubkey,
joinsplitsig,
bindingsig
))
def putTransaction(st, transaction):
st = putIntLE(st, 4, transaction.version)
st = putVI(st, len(transaction.txins))
st = putMap(st, putTxin, transaction.txins)
st = putVI(st, len(transaction.txouts))
st = putMap(st, putTxout, transaction.txouts)
st = putIntLE(st, 4, transaction.locktime)
return st
def putPushdata(st, data):
l = len(data)
if 1 <= l <= 75:
prefix = putIntLE(b'', 1, l)
elif 76 <= l <= 0xff:
prefix = putIntLE(b'\x4c', 1, l)
elif 0x100 <= l <= 0xffff:
prefix = putIntLE(b'\x4d', 2, l)
elif 0x10000 <= l <= 0xffffffff:
prefix = putIntLE(b'\x4e', 4, l)
else:
raise ParseException("putPushdata : invalid data length")
return st + prefix + data
def getPushdata(st):
(st, first_byte) = getIntLE(st, 1)
if first_byte == 0x4c:
(st, length) = getIntLE(st, 1)
elif first_byte == 0x4d:
(st, length) = getIntLE(st, 2)
elif first_byte == 0x4e:
(st, length) = getIntLE(st, 4)
elif 1 <= first_byte <= 75:
(st, length) = (st, first_byte)
else:
raise ParseException("getPushdata : invalid first byte")
(st, data) = getStr(st, length)
return (st, data)
SIGHASH_ALL = 1
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
SIGHASH_ANYONECANPAY = 0x80
def sha256d(x):
return sha256(sha256(x))
import hashlib
def sha256(text):
return hashlib.sha256(text).digest()