-
Notifications
You must be signed in to change notification settings - Fork 8
/
op.py
948 lines (786 loc) · 20.2 KB
/
op.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
import hashlib
import ecc
import util
from logging import getLogger
LOGGER = getLogger(__name__)
def encode_num(num):
if num == 0:
return b''
abs_num = abs(num)
negative = num < 0
result = bytearray()
while abs_num:
result.append(abs_num & 0xff)
abs_num >>= 8
# if the top bit is set,
# for negative numbers we ensure that the top bit is set
# for positive numbers we ensure that the top bit is not set
if result[-1] & 0x80:
if negative:
result.append(0x80)
else:
result.append(0)
elif negative:
result[-1] |= 0x80
return bytes(result)
def decode_num(element):
if element == b'':
return 0
# reverse for big endian
big_endian = element[::-1]
# top bit being 1 means it's negative
if big_endian[0] & 0x80:
negative = True
result = big_endian[0] & 0x7f
else:
negative = False
result = big_endian[0]
for c in big_endian[1:]:
result <<= 8
result += c
if negative:
return -result
else:
return result
def op_0(stack):
stack.append(encode_num(0))
return True
def op_1negate(stack):
stack.append(encode_num(-1))
return True
def op_1(stack):
stack.append(encode_num(1))
return True
def op_2(stack):
stack.append(encode_num(2))
return True
def op_3(stack):
stack.append(encode_num(3))
return True
def op_4(stack):
stack.append(encode_num(4))
return True
def op_5(stack):
stack.append(encode_num(5))
return True
def op_6(stack):
stack.append(encode_num(6))
return True
def op_7(stack):
stack.append(encode_num(7))
return True
def op_8(stack):
stack.append(encode_num(8))
return True
def op_9(stack):
stack.append(encode_num(9))
return True
def op_10(stack):
stack.append(encode_num(10))
return True
def op_11(stack):
stack.append(encode_num(11))
return True
def op_12(stack):
stack.append(encode_num(12))
return True
def op_13(stack):
stack.append(encode_num(13))
return True
def op_14(stack):
stack.append(encode_num(14))
return True
def op_15(stack):
stack.append(encode_num(15))
return True
def op_16(stack):
stack.append(encode_num(16))
return True
def op_nop(stack):
return True
def op_if(stack, items):
if len(stack) < 1:
return False
# go through and re-make the items array based on the top stack element
true_items = []
false_items = []
current_array = true_items
found = False
num_endifs_needed = 1
while len(items) > 0:
item = items.pop(0)
if item in (99, 100):
# nested if, we have to go another endif
num_endifs_needed += 1
current_array.append(item)
elif num_endifs_needed == 1 and item == 103:
current_array = false_items
elif item == 104:
if num_endifs_needed == 1:
found = True
break
else:
num_endifs_needed -= 1
current_array.append(item)
else:
current_array.append(item)
if not found:
return False
element = stack.pop()
if decode_num(element) == 0:
items[:0] = false_items
else:
items[:0] = true_items
return True
def op_notif(stack, items):
if len(stack) < 1:
return False
# go through and re-make the items array based on the top stack element
true_items = []
false_items = []
current_array = true_items
found = False
num_endifs_needed = 1
while len(items) > 0:
item = items.pop(0)
if item in (99, 100):
# nested if, we have to go another endif
num_endifs_needed += 1
current_array.append(item)
elif num_endifs_needed == 1 and item == 103:
current_array = false_items
elif item == 104:
if num_endifs_needed == 1:
found = True
break
else:
num_endifs_needed -= 1
current_array.append(item)
else:
current_array.append(item)
if not found:
return False
element = stack.pop()
if decode_num(element) == 0:
items[:0] = true_items
else:
items[:0] = false_items
return True
def op_verify(stack):
if len(stack) < 1:
return False
element = stack.pop()
if decode_num(element) == 0:
return False
return True
def op_return(stack):
return False
def op_toaltstack(stack, altstack):
if len(stack) < 1:
return False
altstack.append(stack.pop())
return True
def op_fromaltstack(stack, altstack):
if len(altstack) < 1:
return False
stack.append(altstack.pop())
return True
def op_2drop(stack):
if len(stack) < 2:
return False
stack.pop()
stack.pop()
return True
def op_2dup(stack):
if len(stack) < 2:
return False
stack.extend(stack[-2:])
return True
def op_3dup(stack):
if len(stack) < 3:
return False
stack.extend(stack[-3:])
return True
def op_2over(stack):
if len(stack) < 4:
return False
stack.extend(stack[-4:-2])
return True
def op_2rot(stack):
if len(stack) < 6:
return False
stack.extend(stack[-6:-4])
return True
def op_2swap(stack):
if len(stack) < 4:
return False
stack[-4:] = stack[-2:] + stack[-4:-2]
return True
def op_ifdup(stack):
if len(stack) < 1:
return False
if decode_num(stack[-1]) != 0:
stack.append(stack[-1])
return True
def op_depth(stack):
stack.append(encode_num(len(stack)))
return True
def op_drop(stack):
if len(stack) < 1:
return False
stack.pop()
return True
def op_dup(stack):
if len(stack) < 1:
return False
stack.append(stack[-1])
return True
def op_nip(stack):
if len(stack) < 2:
return False
stack[-2:] = stack[-1:]
return True
def op_over(stack):
if len(stack) < 2:
return False
stack.append(stack[-2])
return True
def op_pick(stack):
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
stack.append(stack[-n - 1])
return True
def op_roll(stack):
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
if n == 0:
return True
stack.append(stack.pop(-n - 1))
return True
def op_rot(stack):
if len(stack) < 3:
return False
stack.append(stack.pop(-3))
return True
def op_swap(stack):
if len(stack) < 2:
return False
stack.append(stack.pop(-2))
return True
def op_tuck(stack):
if len(stack) < 2:
return False
stack.insert(-2, stack[-1])
return True
def op_size(stack):
if len(stack) < 1:
return False
stack.append(encode_num(len(stack[-1])))
return True
def op_equal(stack):
if len(stack) < 2:
return False
element1 = stack.pop()
element2 = stack.pop()
if element1 == element2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_equalverify(stack):
return op_equal(stack) and op_verify(stack)
def op_1add(stack):
if len(stack) < 1:
return False
element = decode_num(stack.pop())
stack.append(encode_num(element + 1))
return True
def op_1sub(stack):
if len(stack) < 1:
return False
element = decode_num(stack.pop())
stack.append(encode_num(element - 1))
return True
def op_negate(stack):
if len(stack) < 1:
return False
element = decode_num(stack.pop())
stack.append(encode_num(-element))
return True
def op_abs(stack):
if len(stack) < 1:
return False
element = decode_num(stack.pop())
if element < 0:
stack.append(encode_num(-element))
else:
stack.append(encode_num(element))
return True
def op_not(stack):
if len(stack) < 1:
return False
element = stack.pop()
if decode_num(element) == 0:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_0notequal(stack):
if len(stack) < 1:
return False
element = stack.pop()
if decode_num(element) == 0:
stack.append(encode_num(0))
else:
stack.append(encode_num(1))
return True
def op_add(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
stack.append(encode_num(element1 + element2))
return True
def op_sub(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
stack.append(encode_num(element2 - element1))
return True
def op_booland(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 and element2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_boolor(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 or element2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_numequal(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 == element2:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_numequalverify(stack):
return op_numequal(stack) and op_verify(stack)
def op_numnotequal(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 == element2:
stack.append(encode_num(0))
else:
stack.append(encode_num(1))
return True
def op_lessthan(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element2 < element1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_greaterthan(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element2 > element1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_lessthanorequal(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element2 <= element1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_greaterthanorequal(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element2 >= element1:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_min(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 < element2:
stack.append(encode_num(element1))
else:
stack.append(encode_num(element2))
return True
def op_max(stack):
if len(stack) < 2:
return False
element1 = decode_num(stack.pop())
element2 = decode_num(stack.pop())
if element1 > element2:
stack.append(encode_num(element1))
else:
stack.append(encode_num(element2))
return True
def op_within(stack):
if len(stack) < 3:
return False
maximum = decode_num(stack.pop())
minimum = decode_num(stack.pop())
element = decode_num(stack.pop())
if element >= minimum and element < maximum:
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_ripemd160(stack):
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hashlib.new('ripemd160', element).digest())
return True
def op_sha1(stack):
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hashlib.sha1(element).digest())
return True
def op_sha256(stack):
if len(stack) < 1:
return False
element = stack.pop()
stack.append(hashlib.sha256(element).digest())
return True
def op_hash160(stack):
# check that there's at least 1 element on the stack
if len(stack) < 1:
return False
# pop off the top element from the stack
element = stack.pop()
# push a hash160 of the popped off element to the stack
h160 = util.hash160(element)
stack.append(h160)
return True
def op_hash256(stack):
if len(stack) < 1:
return False
element = stack.pop()
stack.append(util.hash256(element))
return True
def op_checksig(stack, z):
# check that there are at least 2 elements on the stack
if len(stack) < 2:
return False
# the top element of the stack is the SEC pubkey
sec_pubkey = stack.pop()
# the next element of the stack is the DER signature
# take off the last byte of the signature as that's the hash_type
der_signature = stack.pop()[:-1]
# parse the serialized pubkey and signature into objects
try:
# point = S256Point.parse(sec_pubkey)
pub_key = ecc.parse_sec(sec_pubkey)
# sig = Signature.parse(der_signature)
sig = ecc.parse_der(der_signature)
except (ValueError, SyntaxError) as e:
LOGGER.info(e)
return False
# verify the signature using S256Point.verify()
# push an encoded 1 or 0 depending on whether the signature verified
# if point.verify(z, sig):
if ecc.verify_sig(pub_key, sig, z):
stack.append(encode_num(1))
else:
stack.append(encode_num(0))
return True
def op_checksigverify(stack, z):
return op_checksig(stack, z) and op_verify(stack)
def op_checkmultisig(stack, z):
if len(stack) < 1:
return False
n = decode_num(stack.pop())
if len(stack) < n + 1:
return False
sec_pubkeys = []
for _ in range(n):
sec_pubkeys.append(stack.pop())
m = decode_num(stack.pop())
if len(stack) < m + 1:
return False
der_signatures = []
for _ in range(m):
# signature is assumed to be using SIGHASH_ALL
der_signatures.append(stack.pop()[:-1])
# OP_CHECKMULTISIG bug
stack.pop()
try:
# parse all the points
# points = [S256Point.parse(sec) for sec in sec_pubkeys]
points = [ecc.parse_sec(sec) for sec in sec_pubkeys]
# parse all the signatures
# sigs = [Signature.parse(der) for der in der_signatures]
sigs = [ecc.der(der) for der in der_signatures]
# loop through the signatures
for sig in sigs:
# if we have no more points, signatures are no good
if len(points) == 0:
LOGGER.info("signatures no good or not in right order")
return False
# we loop until we find the point which works with this signature
while points:
# get the current point from the list of points
point = points.pop(0)
# we check if this signature goes with the current point
# if point.verify(z, sig):
if ecc.verify_sig(point, sig, z):
break
# the signatures are valid, so push a 1 to the stack
stack.append(encode_num(1))
except (ValueError, SyntaxError):
return False
return True
def op_checkmultisigverify(stack, z):
return op_checkmultisig(stack, z) and op_verify(stack)
def op_checklocktimeverify(stack, locktime, sequence):
if sequence == 0xffffffff:
return False
if len(stack) < 1:
return False
element = decode_num(stack[-1])
if element < 0:
return False
if element < 500000000 and locktime > 500000000:
return False
if locktime < element:
return False
return True
def op_checksequenceverify(stack, version, sequence):
if sequence & (1 << 31) == (1 << 31):
return False
if len(stack) < 1:
return False
element = decode_num(stack[-1])
if element < 0:
return False
if element & (1 << 31) == (1 << 31):
if version < 2:
return False
elif sequence & (1 << 31) == (1 << 31):
return False
elif element & (1 << 22) != sequence & (1 << 22):
return False
elif element & 0xffff > sequence & 0xffff:
return False
return True
OP_CODE_FUNCTIONS = {
0: op_0,
79: op_1negate,
81: op_1,
82: op_2,
83: op_3,
84: op_4,
85: op_5,
86: op_6,
87: op_7,
88: op_8,
89: op_9,
90: op_10,
91: op_11,
92: op_12,
93: op_13,
94: op_14,
95: op_15,
96: op_16,
97: op_nop,
99: op_if,
100: op_notif,
105: op_verify,
106: op_return,
107: op_toaltstack,
108: op_fromaltstack,
109: op_2drop,
110: op_2dup,
111: op_3dup,
112: op_2over,
113: op_2rot,
114: op_2swap,
115: op_ifdup,
116: op_depth,
117: op_drop,
118: op_dup,
119: op_nip,
120: op_over,
121: op_pick,
122: op_roll,
123: op_rot,
124: op_swap,
125: op_tuck,
130: op_size,
135: op_equal,
136: op_equalverify,
139: op_1add,
140: op_1sub,
143: op_negate,
144: op_abs,
145: op_not,
146: op_0notequal,
147: op_add,
148: op_sub,
154: op_booland,
155: op_boolor,
156: op_numequal,
157: op_numequalverify,
158: op_numnotequal,
159: op_lessthan,
160: op_greaterthan,
161: op_lessthanorequal,
162: op_greaterthanorequal,
163: op_min,
164: op_max,
165: op_within,
166: op_ripemd160,
167: op_sha1,
168: op_sha256,
169: op_hash160,
170: op_hash256,
172: op_checksig,
173: op_checksigverify,
174: op_checkmultisig,
175: op_checkmultisigverify,
176: op_nop,
177: op_checklocktimeverify,
178: op_checksequenceverify,
179: op_nop,
180: op_nop,
181: op_nop,
182: op_nop,
183: op_nop,
184: op_nop,
185: op_nop,
}
OP_CODE_NAMES = {
0: 'OP_0',
76: 'OP_PUSHDATA1',
77: 'OP_PUSHDATA2',
78: 'OP_PUSHDATA4',
79: 'OP_1NEGATE',
81: 'OP_1',
82: 'OP_2',
83: 'OP_3',
84: 'OP_4',
85: 'OP_5',
86: 'OP_6',
87: 'OP_7',
88: 'OP_8',
89: 'OP_9',
90: 'OP_10',
91: 'OP_11',
92: 'OP_12',
93: 'OP_13',
94: 'OP_14',
95: 'OP_15',
96: 'OP_16',
97: 'OP_NOP',
99: 'OP_IF',
100: 'OP_NOTIF',
103: 'OP_ELSE',
104: 'OP_ENDIF',
105: 'OP_VERIFY',
106: 'OP_RETURN',
107: 'OP_TOALTSTACK',
108: 'OP_FROMALTSTACK',
109: 'OP_2DROP',
110: 'OP_2DUP',
111: 'OP_3DUP',
112: 'OP_2OVER',
113: 'OP_2ROT',
114: 'OP_2SWAP',
115: 'OP_IFDUP',
116: 'OP_DEPTH',
117: 'OP_DROP',
118: 'OP_DUP',
119: 'OP_NIP',
120: 'OP_OVER',
121: 'OP_PICK',
122: 'OP_ROLL',
123: 'OP_ROT',
124: 'OP_SWAP',
125: 'OP_TUCK',
130: 'OP_SIZE',
135: 'OP_EQUAL',
136: 'OP_EQUALVERIFY',
139: 'OP_1ADD',
140: 'OP_1SUB',
143: 'OP_NEGATE',
144: 'OP_ABS',
145: 'OP_NOT',
146: 'OP_0NOTEQUAL',
147: 'OP_ADD',
148: 'OP_SUB',
154: 'OP_BOOLAND',
155: 'OP_BOOLOR',
156: 'OP_NUMEQUAL',
157: 'OP_NUMEQUALVERIFY',
158: 'OP_NUMNOTEQUAL',
159: 'OP_LESSTHAN',
160: 'OP_GREATERTHAN',
161: 'OP_LESSTHANOREQUAL',
162: 'OP_GREATERTHANOREQUAL',
163: 'OP_MIN',
164: 'OP_MAX',
165: 'OP_WITHIN',
166: 'OP_RIPEMD160',
167: 'OP_SHA1',
168: 'OP_SHA256',
169: 'OP_HASH160',
170: 'OP_HASH256',
171: 'OP_CODESEPARATOR',
172: 'OP_CHECKSIG',
173: 'OP_CHECKSIGVERIFY',
174: 'OP_CHECKMULTISIG',
175: 'OP_CHECKMULTISIGVERIFY',
176: 'OP_NOP1',
177: 'OP_CHECKLOCKTIMEVERIFY',
178: 'OP_CHECKSEQUENCEVERIFY',
179: 'OP_NOP4',
180: 'OP_NOP5',
181: 'OP_NOP6',
182: 'OP_NOP7',
183: 'OP_NOP8',
184: 'OP_NOP9',
185: 'OP_NOP10',
}