-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsherlock.c
1578 lines (1578 loc) · 37.1 KB
/
sherlock.c
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
# include "stdlib.h"
# include "assert.h"
# include "string.h"
# include "bitcoin.h"
void memswap (void * x₀, void * x₁, size_t n)
{
void * tmp = memcpy (__builtin_alloca (n), x₀, n);
memcpy (x₀, x₁, n), memcpy (x₁, tmp, n);
}
# ifndef min
# define min(x₀, x₁) ((x₀) < (x₁) ? (x₀) : (x₁))
# endif
# ifndef max
# define max(x₀, x₁) ((x₀) > (x₁) ? (x₀) : (x₁))
# endif
// ⋯
struct
{
uint8_t code; // Sherlock script opcode
uint8_t reference; // Reference counter
union
{
int64_t num; // Constant
uint32_t xₙ [3]; // Operands
struct
{
uint32_t x₀, x₁, x₂;
};
};
}
# define ÖP_MIN OP_CHECKSIGADD + 1
# define ÖP_MAX 0x3FFF
trace [ÖP_MAX + 1] =
{
[OP_0].code = OP_0,
[OP_1NEGATE] = { .code = OP_1NEGATE, .num = -1 },
[OP_1] = { .code = OP_1, .num = 1 },
[OP_2] = { .code = OP_2, .num = 2 },
[OP_3] = { .code = OP_3, .num = 3 },
[OP_4] = { .code = OP_4, .num = 4 },
[OP_5] = { .code = OP_5, .num = 5 },
[OP_6] = { .code = OP_6, .num = 6 },
[OP_7] = { .code = OP_7, .num = 7 },
[OP_8] = { .code = OP_8, .num = 8 },
[OP_9] = { .code = OP_9, .num = 9 },
[OP_10] = { .code = OP_10, .num = 10 },
[OP_11] = { .code = OP_11, .num = 11 },
[OP_12] = { .code = OP_12, .num = 12 },
[OP_13] = { .code = OP_13, .num = 13 },
[OP_14] = { .code = OP_14, .num = 14 },
[OP_15] = { .code = OP_15, .num = 15 },
[OP_16] = { .code = OP_16, .num = 16 },
};
uint32_t öp = ÖP_MIN, öp⁻¹ = ÖP_MAX; // Trace allocation pointer
// Read nop-terminated script
uint8_t preprocess (void)
{
uint8_t op;
uint16_t ap, ap⁻¹;
uint32_t stack [1000];
uint32_t x₀, x₁, x₂;
do switch (op = __builtin_stdin¹)
{
case OP_0:
__builtin_push (OP_0);
continue;
case 1:
case 2:
case 3:
case 4:
// Read multibyte integer
trace [öp⁻¹].num = __builtin_stdin (op);
assert (trace [öp⁻¹].num > 16 || trace [öp⁻¹].num <= -1);
assert (trace [öp⁻¹].num < +2147483648); // x < +2³¹
assert (trace [öp⁻¹].num > -2147483648); // x > -2³¹
for (uint32_t öp = ÖP_MAX; öp >= öp⁻¹; -- öp)
{
// öp⁻¹ + 0
if (öp == öp⁻¹)
{
// Allocate constant
assert (öp <= öp⁻¹);
trace [öp⁻¹].code = op;
__builtin_push (öp⁻¹ --);
break;
}
// öp⁻¹ + 1 ⋯ 2¹² - 1
if (trace [öp].code == op && trace [öp].num == trace [öp⁻¹].num)
{
// Recycle constant
__builtin_push (öp);
trace [öp⁻¹].num = 0;
break;
}
}
continue;
// ⋯
// OP_PUSHDATA1
// OP_PUSHDATA2
// OP_PUSHDATA4
case OP_1NEGATE:
__builtin_push (OP_1NEGATE);
continue;
case OP_RESERVED: // Function variable
assert (öp <= öp⁻¹);
// NOTE: OP_RESERVED "x₀"
trace [öp].code = OP_RESERVED;
trace [öp].num = öp - ÖP_MIN;
__builtin_push (öp ++);
continue;
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13:
case OP_14:
case OP_15:
case OP_16:
__builtin_push (op);
continue;
case OP_NOP: // nop-terminated
return OP_NOP;
case OP_VER: // Function definition
// NOTE: OP_VER "f₀"
continue; // WARNING
case OP_IF:
case OP_NOTIF:
{
x₀ = __builtin_drop;
memmove (stack, __builtin_stack, sizeof stack);
memmove (& ap, & __builtin_ap, sizeof ap);
memmove (& ap⁻¹, & __builtin_ap⁻¹, sizeof ap⁻¹);
switch (preprocess ( ))
{
case OP_ELSE:
memswap (stack, __builtin_stack, sizeof stack);
memswap (& ap, & __builtin_ap, sizeof ap);
memswap (& ap⁻¹, & __builtin_ap⁻¹, sizeof ap⁻¹);
if (preprocess ( ) != OP_ENDIF) fputs ("Assertion failed: OP_ENDIF\n", stderr);
memswap (stack, __builtin_stack, sizeof stack);
memswap (& ap, & __builtin_ap, sizeof ap);
memswap (& ap⁻¹, & __builtin_ap⁻¹, sizeof ap⁻¹);
// 🡇
case OP_ENDIF:
assert (__builtin_ap == ap);
assert (__builtin_ap⁻¹ == ap⁻¹);
break;
default:
fputs ("Assertion failed: OP_ENDIF\n", stderr); // WARNING
break;
}
// Merge (alt)stack
for (uint16_t ap = 0; ap <= 999; ++ ap)
{
if (ap == __builtin_ap)
{
ap = __builtin_ap⁻¹;
continue;
}
x₁ = __builtin_stack [ap];
x₂ = stack [ap];
if (x₁ == x₂) continue;
trace [öp].code = op;
trace [öp].x₀ = x₀;
trace [öp].x₁ = x₁;
trace [öp].x₂ = x₂;
__builtin_stack [ap] = öp ++;
}
continue;
}
// OP_VERIF
// OP_VERNOTIF
case OP_ELSE:
case OP_ENDIF:
return op;
// OP_VERIFY
// OP_RETURN
case OP_TOALTSTACK:
__builtin_stack [__builtin_ap⁻¹ --] = __builtin_drop;
continue;
case OP_FROMALTSTACK:
assert (__builtin_ap⁻¹ < 999);
__builtin_push (__builtin_stack [++ __builtin_ap⁻¹]);
continue;
case OP_2DROP:
__builtin_drop;
__builtin_drop;
continue;
case OP_2DUP:
__builtin_over;
__builtin_over;
continue;
case OP_3DUP:
__builtin_pick (2);
__builtin_pick (2);
__builtin_pick (2);
continue;
case OP_2OVER:
__builtin_pick (3);
__builtin_pick (3);
continue;
case OP_2ROT:
__builtin_roll (5);
__builtin_roll (5);
continue;
case OP_2SWAP:
__builtin_roll (3);
__builtin_roll (3);
continue;
// OP_IFDUP
case OP_DEPTH:
assert (öp <= öp⁻¹);
trace [öp].code = __builtin_ap ? OP_RESERVED + __builtin_ap : OP_0;
trace [öp].num = __builtin_ap;
__builtin_push (öp ++);
continue;
case OP_DROP:
__builtin_drop;
continue;
case OP_DUP:
__builtin_pick (0);
continue;
case OP_NIP:
__builtin_swap;
__builtin_drop;
continue;
case OP_OVER:
__builtin_over;
continue;
case OP_PICK:
__builtin_pick (trace [__builtin_drop].num);
continue;
case OP_ROLL:
__builtin_roll (trace [__builtin_drop].num);
continue;
case OP_ROT:
__builtin_roll (2);
continue;
case OP_SWAP:
__builtin_swap;
continue;
case OP_TUCK:
__builtin_swap;
__builtin_over;
continue;
// OP_CAT
// OP_SUBSTR
// OP_LEFT
// OP_RIGHT
// OP_SIZE
// OP_INVERT
// OP_AND
// OP_OR
// OP_XOR
// OP_EQUAL
// OP_EQUALVERIFY
case OP_RESERVED1: // Function inline
// NOTE: OP_RESERVED1 "f₀"
continue; // WARNING
case OP_RESERVED2: // Function call
// NOTE: OP_RESERVED2 "f₀"
continue; // WARNING
case OP_1ADD:
case OP_1SUB:
case OP_2MUL:
case OP_2DIV:
case OP_NEGATE:
case OP_ABS:
case OP_NOT:
case OP_0NOTEQUAL:
assert (öp <= öp⁻¹);
trace [öp].code = op;
trace [öp].x₀ = __builtin_drop;
__builtin_push (öp ++);
continue;
case OP_ADD:
assert (öp <= öp⁻¹);
if (__builtin_stack [__builtin_ap - 1] == __builtin_stack [__builtin_ap - 2]) // OP_ADD → OP_2MUL
{
trace [öp].code = OP_2MUL;
trace [öp].x₀ = __builtin_drop;
trace [öp].x₀ = __builtin_drop;
__builtin_push (öp ++);
continue;
}
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_LSHIFT:
case OP_RSHIFT:
case OP_BOOLAND:
case OP_BOOLOR:
case OP_NUMEQUAL:
case OP_NUMEQUALVERIFY:
case OP_NUMNOTEQUAL:
case OP_LESSTHAN:
case OP_GREATERTHAN:
case OP_LESSTHANOREQUAL:
case OP_GREATERTHANOREQUAL:
case OP_MIN:
case OP_MAX:
assert (öp <= öp⁻¹);
trace [öp].code = op;
trace [öp].x₁ = __builtin_drop;
trace [öp].x₀ = __builtin_drop;
__builtin_push (öp ++);
continue;
case OP_WITHIN:
assert (öp <= öp⁻¹);
trace [öp].code = OP_WITHIN;
trace [öp].x₂ = __builtin_drop;
trace [öp].x₁ = __builtin_drop;
trace [öp].x₀ = __builtin_drop;
__builtin_push (öp ++);
continue;
// OP_RIPEMD160
// OP_SHA1
// OP_SHA256
// OP_HASH160
// OP_HASH256
// OP_CODESEPARATOR
// OP_CHECKSIG
// OP_CHECKSIGVERIFY
// OP_CHECKMULTISIG
// OP_CHECKMULTISIGVERIFY
// OP_NOP1
// OP_NOP2
// OP_NOP3
// OP_NOP4
// OP_NOP5
// OP_NOP6
// OP_NOP7
// OP_NOP8
// OP_NOP9
// OP_NOP10
// OP_CHECKSIGADD
// ⋯
default:
continue; // WARNING
}
while (1);
}
// Select result
void attach (uint16_t x)
{
switch (trace [x].code)
{
case OP_0:
return;
case 1:
case 2:
case 3:
case 4:
++ trace [x].reference;
return;
// ⋯
// OP_PUSHDATA1
// OP_PUSHDATA2
// OP_PUSHDATA4
case OP_1NEGATE:
return;
case OP_RESERVED:
++ trace [x].reference;
return;
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13:
case OP_14:
case OP_15:
case OP_16:
return;
// OP_NOP
// OP_VER
case OP_IF:
case OP_NOTIF:
if (trace [x].reference ++) return;
attach (trace [x].x₀);
attach (trace [x].x₁);
attach (trace [x].x₂);
return;
// OP_VERIF
// OP_VERNOTIF
// OP_ELSE
// OP_ENDIF
// OP_VERIFY
// OP_RETURN
// ⋯
// OP_CAT
// OP_SUBSTR
// OP_LEFT
// OP_RIGHT
// OP_SIZE
// OP_INVERT
// OP_AND
// OP_OR
// OP_XOR
// OP_EQUAL
// OP_EQUALVERIFY
// OP_RESERVED1
// OP_RESERVED2
case OP_1ADD:
case OP_1SUB:
case OP_2MUL:
case OP_2DIV:
case OP_NEGATE:
case OP_ABS:
case OP_NOT:
case OP_0NOTEQUAL:
if (trace [x].reference ++) return;
attach (trace [x].x₀);
return;
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_LSHIFT:
case OP_RSHIFT:
case OP_BOOLAND:
case OP_BOOLOR:
case OP_NUMEQUAL:
case OP_NUMEQUALVERIFY:
case OP_NUMNOTEQUAL:
case OP_LESSTHAN:
case OP_GREATERTHAN:
case OP_LESSTHANOREQUAL:
case OP_GREATERTHANOREQUAL:
case OP_MIN:
case OP_MAX:
if (trace [x].reference ++) return;
attach (trace [x].x₀);
attach (trace [x].x₁);
return;
case OP_WITHIN:
if (trace [x].reference ++) return;
attach (trace [x].x₀);
attach (trace [x].x₁);
attach (trace [x].x₂);
return;
// OP_RIPEMD160
// OP_SHA1
// OP_SHA256
// OP_HASH160
// OP_HASH256
// OP_CODESEPARATOR
// OP_CHECKSIG
// OP_CHECKSIGVERIFY
// OP_CHECKMULTISIG
// OP_CHECKMULTISIGVERIFY
// OP_NOP1
// OP_NOP2
// OP_NOP3
// OP_NOP4
// OP_NOP5
// OP_NOP6
// OP_NOP7
// OP_NOP8
// OP_NOP9
// OP_NOP10
// OP_CHECKSIGADD
// ⋯
default:
return; // WARNING
}
}
uint32_t reference⁻¹ = 0x80000000;
// Count xᵢ in expression tree
uint16_t count⁺ (uint32_t xᵢ, uint32_t x₀, uint8_t _)
{
if (x₀ == 0x80000000) return 0; // WARNING
switch (trace [x₀].code)
{
case OP_0:
return x₀ == xᵢ;
case 1:
case 2:
case 3:
case 4:
return x₀ == xᵢ;
// ⋯
// OP_PUSHDATA1
// OP_PUSHDATA2
// OP_PUSHDATA4
case OP_1NEGATE:
return x₀ == xᵢ;
case OP_RESERVED:
return x₀ == xᵢ;
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13:
case OP_14:
case OP_15:
case OP_16:
return x₀ == xᵢ;
case OP_NOP:
return 0;
// OP_VER
case OP_IF:
case OP_NOTIF:
if (_ == 0) return x₀ == xᵢ;
return (x₀ == xᵢ) ? 1 : count⁺ (xᵢ, trace [x₀].x₀, 0) + count⁺ (xᵢ, trace [x₀].x₁, 0) + count⁺ (xᵢ, trace [x₀].x₂, 0);
// OP_VERIF
// OP_VERNOTIF
// OP_ELSE
// OP_ENDIF
// OP_VERIFY
// OP_RETURN
// ⋯
// OP_CAT
// OP_SUBSTR
// OP_LEFT
// OP_RIGHT
// OP_SIZE
// OP_INVERT
// OP_AND
// OP_OR
// OP_XOR
// OP_EQUAL
// OP_EQUALVERIFY
// OP_RESERVED1
// OP_RESERVED2
case OP_1ADD:
case OP_1SUB:
case OP_2MUL:
case OP_2DIV:
case OP_NEGATE:
case OP_ABS:
case OP_NOT:
case OP_0NOTEQUAL:
if (_ == 0) return x₀ == xᵢ;
return (x₀ == xᵢ) ? 1 : count⁺ (xᵢ, trace [x₀].x₀, 0);
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_LSHIFT:
case OP_RSHIFT:
case OP_BOOLAND:
case OP_BOOLOR:
case OP_NUMEQUAL:
case OP_NUMEQUALVERIFY:
case OP_NUMNOTEQUAL:
case OP_LESSTHAN:
case OP_GREATERTHAN:
case OP_LESSTHANOREQUAL:
case OP_GREATERTHANOREQUAL:
case OP_MIN:
case OP_MAX:
if (_ == 0) return x₀ == xᵢ;
return (x₀ == xᵢ) ? 1 : count⁺ (xᵢ, trace [x₀].x₀, 0) + count⁺ (xᵢ, trace [x₀].x₁, 0);
case OP_WITHIN:
if (_ == 0) return x₀ == xᵢ;
return (x₀ == xᵢ) ? 1 : count⁺ (xᵢ, trace [x₀].x₀, 0) + count⁺ (xᵢ, trace [x₀].x₁, 0) + count⁺ (xᵢ, trace [x₀].x₂, 0);
// OP_RIPEMD160
// OP_SHA1
// OP_SHA256
// OP_HASH160
// OP_HASH256
// OP_CODESEPARATOR
// OP_CHECKSIG
// OP_CHECKSIGVERIFY
// OP_CHECKMULTISIG
// OP_CHECKMULTISIGVERIFY
// OP_NOP1
// OP_NOP2
// OP_NOP3
// OP_NOP4
// OP_NOP5
// OP_NOP6
// OP_NOP7
// OP_NOP8
// OP_NOP9
// OP_NOP10
// OP_CHECKSIGADD
// ⋯
default:
return 0; // WARNING
}
}
typedef enum : char { LEEWAY, STRICT } workflow;
// Permute stack elements
void translate¹ (uint16_t x)
{
assert (__builtin_ap >= 1);
uint32_t apₓ = __builtin_find (x); assert (apₓ != 0x80000000);
uint16_t Δap = __builtin_ap - apₓ - 1;
uint8_t dup = trace [x].reference > count⁺ (x, reference⁻¹, 1) && __builtin_count (x) == 1;
uint32_t xₙ [ ] = { x };
switch (__builtin_check⁻¹ (xₙ, 1))
{
case 0b1:
assert (Δap == 0);
if (dup)
{
__builtin_swap;
__builtin_over;
__builtin_stdout¹ (OP_TUCK);
}
return;
case 0b10:
assert (Δap == 1);
if (dup)
{
__builtin_over;
__builtin_stdout¹ (OP_OVER);
}
else
{
__builtin_swap;
__builtin_stdout¹ (OP_SWAP);
}
return;
case 0b100:
assert (Δap == 2);
if (! dup)
{
__builtin_roll (2);
__builtin_stdout¹ (OP_ROT);
return;
}
// 🡇
case 0b1000:
case 0b10000:
case 0b100000:
case 0b1000000:
case 0b10000000:
case 0b100000000:
case 0b1000000000:
case 0b10000000000:
case 0b100000000000:
case 0b1000000000000:
case 0b10000000000000:
case 0b100000000000000:
case 0b1000000000000000:
case 0b10000000000000000:
assert (Δap <= 16);
__builtin_stdout¹ (OP_RESERVED + Δap);
break;
default:
assert (Δap > 16);
__builtin_stdout (Δap);
break;
}
assert (Δap >= 2);
if (dup)
{
__builtin_pick (Δap);
__builtin_stdout¹ (OP_PICK);
}
else
{
__builtin_roll (Δap);
__builtin_stdout¹ (OP_ROLL);
}
assert (__builtin_stack [__builtin_ap - 1] == x);
return;
}
// Permute stack elements
void translate² (uint16_t x₀, uint16_t x₁, workflow s)
{
uint32_t ap₀ = __builtin_find (x₀); assert (ap₀ != 0x80000000);
uint32_t ap₁ = __builtin_find (x₁); assert (ap₁ != 0x80000000);
uint16_t xˡʰˢ = __builtin_stack [min (ap₀, ap₁)];
uint16_t xʳʰˢ = __builtin_stack [max (ap₀, ap₁)];
uint32_t xₙ [ ] = {xˡʰˢ, xʳʰˢ};
uint16_t dupˡʰˢ = trace [xˡʰˢ].reference > count⁺ (xˡʰˢ, reference⁻¹, 1) && __builtin_count (xˡʰˢ) == 1;
uint16_t dupʳʰˢ = trace [xʳʰˢ].reference > count⁺ (xʳʰˢ, reference⁻¹, 1) && __builtin_count (xʳʰˢ) == 1;
switch (__builtin_check⁻¹ (xₙ, 2))
{
case 0b110000:
__builtin_roll (5);
__builtin_roll (5);
__builtin_stdout¹ (OP_2ROT);
return translate² (x₀, x₁, s);
case 0b1100:
if (dupˡʰˢ && dupʳʰˢ)
{
__builtin_pick (3);
__builtin_pick (3);
__builtin_stdout¹ (OP_2OVER);
}
else
{
__builtin_roll (3);
__builtin_roll (3);
__builtin_stdout¹ (OP_2SWAP);
}
return translate² (x₀, x₁, s);
case 0b110:
if (dupˡʰˢ != dupʳʰˢ)
{
translate¹ (x₀);
translate¹ (x₁);
return;
}
if (x₀ == xˡʰˢ)
{
__builtin_roll (2);
__builtin_stdout¹ (OP_ROT);
}
else // x₀ == xʳʰˢ
{
__builtin_swap;
__builtin_stdout¹ (OP_SWAP);
}
return translate² (x₀, x₁, s);
case 0b101:
if (dupˡʰˢ != dupʳʰˢ)
{
translate¹ (x₀);
translate¹ (x₁);
return;
}
__builtin_roll (2);
__builtin_stdout¹ (OP_ROT);
return translate² (x₀, x₁, s);
case 0b11:
if (dupˡʰˢ && dupʳʰˢ)
{
__builtin_over;
__builtin_over;
__builtin_stdout¹ (OP_2DUP);
}
if (dupˡʰˢ != dupʳʰˢ)
{
if (dupˡʰˢ)
{
__builtin_over;
__builtin_stdout¹ (OP_OVER);
}
if (dupʳʰˢ)
{
__builtin_swap;
__builtin_over;
__builtin_stdout¹ (OP_TUCK);
}
}
if (s == STRICT && __builtin_stack [__builtin_ap - 1] == x₀)
{
__builtin_swap;
__builtin_stdout¹ (OP_SWAP);
}
return;
default:
if (s == STRICT)
{
translate¹ (x₀);
translate¹ (x₁);
}
else
{
translate¹ (xˡʰˢ);
translate² (x₀, x₁, s);
}
return;
}
}
// Permute stack elements
void translate³ (uint16_t x₀, uint16_t x₁, uint16_t x₂, workflow s)
{
uint32_t ap₀ = __builtin_find (x₀); assert (ap₀ != 0x80000000);
uint32_t ap₁ = __builtin_find (x₁); assert (ap₁ != 0x80000000);
uint32_t ap₂ = __builtin_find (x₂); assert (ap₂ != 0x80000000);
uint16_t xˡʰˢ = __builtin_stack [min (min (ap₀, ap₁), ap₂)];
uint16_t xᵐⁱᵈ;
uint16_t xʳʰˢ = __builtin_stack [max (max (ap₀, ap₁), ap₂)];
if (x₀ != xˡʰˢ && x₀ != xʳʰˢ) xᵐⁱᵈ = x₀;
if (x₁ != xˡʰˢ && x₁ != xʳʰˢ) xᵐⁱᵈ = x₁;
if (x₂ != xˡʰˢ && x₂ != xʳʰˢ) xᵐⁱᵈ = x₂;
uint32_t xₙ [ ] = {xˡʰˢ, xᵐⁱᵈ, xʳʰˢ};
uint16_t dupˡʰˢ = trace [xˡʰˢ].reference > count⁺ (xˡʰˢ, reference⁻¹, 1) && __builtin_count (xˡʰˢ) == 1;
uint16_t dupᵐⁱᵈ = trace [xᵐⁱᵈ].reference > count⁺ (xᵐⁱᵈ, reference⁻¹, 1) && __builtin_count (xᵐⁱᵈ) == 1;
uint16_t dupʳʰˢ = trace [xʳʰˢ].reference > count⁺ (xʳʰˢ, reference⁻¹, 1) && __builtin_count (xʳʰˢ) == 1;
switch (__builtin_check⁻¹ (xₙ, 3) & 0b111)
{
case 0b111:
if (dupˡʰˢ && dupᵐⁱᵈ && dupʳʰˢ) // xᵢ xⱼ xₖ → xᵢ xⱼ xₖ
{
__builtin_pick (2);
__builtin_pick (2);
__builtin_pick (2);
__builtin_stdout¹ (OP_3DUP);
dupˡʰˢ = dupᵐⁱᵈ = dupʳʰˢ = 0;
}
if (s == STRICT)
{
if (x₁ == xˡʰˢ || x₂ == xˡʰˢ && ! dupˡʰˢ) // xᵢ x₀ xⱼ → x₀ xⱼ xᵢ
{
__builtin_roll (2);
__builtin_stdout¹ (OP_ROT);
translate³ (x₀, x₁, x₂, STRICT);
return;
}
}
case 0b11:
if (dupᵐⁱᵈ && dupʳʰˢ) // xᵢ xⱼ → xᵢ xⱼ xᵢ xⱼ
{
__builtin_over;
__builtin_over;
__builtin_stdout¹ (OP_2DUP);
translate³ (x₀, x₁, x₂, s);
return;
}
if (s == STRICT)
{
if (x₂ == xᵐⁱᵈ && x₁ == xʳʰˢ) // x₀ x₂ x₁ → x₀ x₁ x₂
{
__builtin_swap;
__builtin_stdout¹ (OP_SWAP);
translate³ (x₀, x₁, x₂, STRICT);
return;
}
}
if (dupᵐⁱᵈ) // xᵢ xⱼ → xᵢ xⱼ xᵢ
{
__builtin_over;
__builtin_stdout¹ (OP_OVER);
translate³ (x₀, x₁, x₂, s);
return;
}
if (dupʳʰˢ) // xᵢ xⱼ → xⱼ xᵢ xⱼ
{
__builtin_swap;
__builtin_over;
__builtin_stdout¹ (OP_TUCK);
translate³ (x₀, x₁, x₂, s);
return;
}
if (dupˡʰˢ || __builtin_ap - __builtin_find (xˡʰˢ) - 1 >= 3) // xᵢ ⋯ xⱼ xₖ → xᵢ ⋯ xⱼ xₖ xᵢ
{
translate¹ (xˡʰˢ);
if (s == STRICT) translate³ (x₀, x₁, x₂, STRICT);
return;
}
if (x₁ == xᵐⁱᵈ && x₀ == xʳʰˢ) // x₂ ⋯ x₁ x₀ → x₂ ⋯ x₀ x₁
{
__builtin_swap;
__builtin_stdout¹ (OP_SWAP);
translate³ (x₀, x₁, x₂, s);
return;
}
if (s == STRICT)
{
assert (__builtin_stack [__builtin_ap - 3] == x₀);
assert (__builtin_stack [__builtin_ap - 2] == x₁);
assert (__builtin_stack [__builtin_ap - 1] == x₂);
}
return;
case 0b110:
case 0b101:
translate² (xᵐⁱᵈ, xʳʰˢ, LEEWAY);
translate³ (x₀, x₁, x₂, s);
return;
default:
if (s == LEEWAY)
{
translate¹ (xʳʰˢ);
translate¹ (xᵐⁱᵈ);
translate¹ (xˡʰˢ);
return;
}
if (s == STRICT)
{
translate¹ (x₀);
translate¹ (x₁);
translate¹ (x₂);
assert (__builtin_stack [__builtin_ap - 3] == x₀);
assert (__builtin_stack [__builtin_ap - 2] == x₁);
assert (__builtin_stack [__builtin_ap - 1] == x₂);
return;
}
}
}
// Dispatch x₀ ⋯ xₙ₋₁
uint32_t translate (const uint32_t xₙ [ ], uint8_t n, workflow s)
{
uint32_t xᵢ;
uint32_t x₀, x₁, x₂;
switch (n)
{
case 0:
return 0x80000000; // WARNING
case 1:
x₀ = xₙ [0];
if (trace [x₀].reference != 0) -- trace [x₀].reference;
translate¹ (x₀);
assert (__builtin_find (x₀) == __builtin_ap - 1), __builtin_drop;
return x₀;
case 2:
x₀ = xₙ [0];
x₁ = xₙ [1];
if (trace [x₀].reference != 0) -- trace [x₀].reference;
if (trace [x₁].reference != 0) -- trace [x₁].reference;
translate² (x₀, x₁, s);
if (s == STRICT)
{
assert (__builtin_find (x₁) == __builtin_ap - 1), __builtin_drop;
assert (__builtin_find (x₀) == __builtin_ap - 1), __builtin_drop;
return x₁;
}
assert (__builtin_check (xₙ, 2) == 0b11);
xᵢ = __builtin_stack [__builtin_ap - 1];
__builtin_drop;
__builtin_drop;
return xᵢ;
case 3:
x₀ = xₙ [0];
x₁ = xₙ [1];
x₂ = xₙ [2];
if (trace [x₀].reference != 0) -- trace [x₀].reference;
if (trace [x₁].reference != 0) -- trace [x₁].reference;
if (trace [x₂].reference != 0) -- trace [x₂].reference;
translate³ (x₀, x₁, x₂, s);
if (s == STRICT)
{
assert (__builtin_find (x₂) == __builtin_ap - 1), __builtin_drop;
assert (__builtin_find (x₁) == __builtin_ap - 1), __builtin_drop;
assert (__builtin_find (x₀) == __builtin_ap - 1), __builtin_drop;
return x₂;
}
assert (__builtin_check (xₙ, 3) == 0b111);
xᵢ = __builtin_stack [__builtin_ap - 1];
__builtin_drop;
__builtin_drop;
__builtin_drop;
return xᵢ;
default: // WARNING
assert (s == LEEWAY);
for (uint8_t i = 0; i < n; ++ i) assert (__builtin_find (xₙ [i]) != 0x80000000);
xᵢ = __builtin_drop;
for (uint8_t i = 1; i < n; ++ i) __builtin_drop;
return xᵢ;
}
}
// Discard expression
void serialize⁻ (uint16_t x)
{
uint32_t apₓ = __builtin_find (x);
if (apₓ == 0x80000000) switch (trace [x].code)
{
case OP_0:
return;
case 1:
case 2:
case 3:
case 4:
return;
// ⋯
// OP_PUSHDATA1
// OP_PUSHDATA2
// OP_PUSHDATA4
case OP_1NEGATE:
return;
case OP_RESERVED:
return;
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13: