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
1001
1002
1003
1004
1005
1006
1007
1008
1009
|
Return-Path: <roconnor@blockstream.com>
Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133])
by lists.linuxfoundation.org (Postfix) with ESMTP id B84ABC000B
for <bitcoin-dev@lists.linuxfoundation.org>;
Tue, 22 Mar 2022 15:08:49 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1])
by smtp2.osuosl.org (Postfix) with ESMTP id 9676840B15
for <bitcoin-dev@lists.linuxfoundation.org>;
Tue, 22 Mar 2022 15:08:49 +0000 (UTC)
X-Virus-Scanned: amavisd-new at osuosl.org
X-Spam-Flag: NO
X-Spam-Score: -1.899
X-Spam-Level:
X-Spam-Status: No, score=-1.899 tagged_above=-999 required=5
tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
Authentication-Results: smtp2.osuosl.org (amavisd-new);
dkim=pass (2048-bit key)
header.d=blockstream-com.20210112.gappssmtp.com
Received: from smtp2.osuosl.org ([127.0.0.1])
by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 2yTJ5Efyv8Xy
for <bitcoin-dev@lists.linuxfoundation.org>;
Tue, 22 Mar 2022 15:08:46 +0000 (UTC)
X-Greylist: whitelisted by SQLgrey-1.8.0
Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com
[IPv6:2607:f8b0:4864:20::835])
by smtp2.osuosl.org (Postfix) with ESMTPS id 7543F40B23
for <bitcoin-dev@lists.linuxfoundation.org>;
Tue, 22 Mar 2022 15:08:46 +0000 (UTC)
Received: by mail-qt1-x835.google.com with SMTP id v2so14647936qtc.5
for <bitcoin-dev@lists.linuxfoundation.org>;
Tue, 22 Mar 2022 08:08:46 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=blockstream-com.20210112.gappssmtp.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=9weqdycT6e8BCTSIJ4khfTnDofB0GPQzvxKRU79jmuw=;
b=HlG5YXZE7TRuZEYNPTBORpYahvLWNaU9UVHRT0eeHvdoJO+RecpNpWtGVgkAeRbkql
ch3azTrCL0ZXwEAEln8uwyjb5LvC8JkOVQxrfCX4KhK74DAheLNb2yFCZEOPVloeL5rq
CX2P4u8fIk7DbsJ9Do2nmKG5/hhwIDpi0nR8nPQth3L5vgrrfjibUZD1hv5rlmIVfrWB
o5w2ZEa4d/YOxTzYRV7xKEUapjMFlIA9d8K0O9aHv8O24CUTdcDTk84zAzMs2JBnrDa/
JAtHYUshxRFDVcM9I/ebFt1bMBW0Ezt5DTsAcRFFdZ6Q09opF00lUin04kQHDJVLqPRl
G2rQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=9weqdycT6e8BCTSIJ4khfTnDofB0GPQzvxKRU79jmuw=;
b=MlDKuru0A3G/nNyxRE6HSwi1QWxy37B5TlWeBSPau26QotUqhkjZ5Z9ZYYBR3a6Wqw
h8kx5hys9D7fJLRRwPdxRfhGbOSmZGg+cZqZaRy2lLYDAO3IzUtS5U74JnFjgiY1c8do
nGRns5LuO5SBvXarM7nRdwJg4Q+XxoOwTdGC3qYu7GKxZ1jjUSrqrR4KahMnnUhKLrDX
CW/NN56l32MjAkRADcKK3LnGqUEHBXvf8W1tk7t5KVyh12NGAMYoZN2gm5BUUcrflzL7
jdZXSobydDZHtnyGz7fB5ZzmI17xBKB1MzGy1MZky3x+iWnf3QZEzVe9zbOoUw4VBmuN
UB+g==
X-Gm-Message-State: AOAM531GObVisqS7nWIHa7Ggi025FfnY2KGx1BFKJQvYKwcVM7YWC9Ur
QC3V9b9AyD6DElWHes9sZ2ESBiCJ2z6YS1MCa2JLliZ3Dak=
X-Google-Smtp-Source: ABdhPJxzxNe9972r1HtR9pPNdYjyd1wV/+lppfwLotfKPLqllfSej5lSpqBgh/W3+EEZCjm+jKwmj8FIDQWnU1e24vU=
X-Received: by 2002:ac8:580d:0:b0:2e1:c641:8c21 with SMTP id
g13-20020ac8580d000000b002e1c6418c21mr20591538qtg.677.1647961725029; Tue, 22
Mar 2022 08:08:45 -0700 (PDT)
MIME-Version: 1.0
References: <NGFW5p2Gl4t6AqL2E29THMT5DbppMJlB6bdUE6nxAdMajxeFcoRNdt5axNLql08EoyIMsBgZHHHYt_MiITZwzyGZIz0iFX4vaKIYrVV2QhU=@protonmail.com>
In-Reply-To: <NGFW5p2Gl4t6AqL2E29THMT5DbppMJlB6bdUE6nxAdMajxeFcoRNdt5axNLql08EoyIMsBgZHHHYt_MiITZwzyGZIz0iFX4vaKIYrVV2QhU=@protonmail.com>
From: "Russell O'Connor" <roconnor@blockstream.com>
Date: Tue, 22 Mar 2022 11:08:33 -0400
Message-ID: <CAMZUoK=TzOFfMFwNw6gjHtu2EeEPhyL9AjqLS-T=wphc905_JA@mail.gmail.com>
To: ZmnSCPxj <ZmnSCPxj@protonmail.com>,
Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
Content-Type: multipart/alternative; boundary="00000000000039600705dacffd80"
Subject: Re: [bitcoin-dev] Beyond Jets: Microcode: Consensus-Critical Jets
Without Softforks
X-BeenThere: bitcoin-dev@lists.linuxfoundation.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Bitcoin Protocol Discussion <bitcoin-dev.lists.linuxfoundation.org>
List-Unsubscribe: <https://lists.linuxfoundation.org/mailman/options/bitcoin-dev>,
<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=unsubscribe>
List-Archive: <http://lists.linuxfoundation.org/pipermail/bitcoin-dev/>
List-Post: <mailto:bitcoin-dev@lists.linuxfoundation.org>
List-Help: <mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=help>
List-Subscribe: <https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev>,
<mailto:bitcoin-dev-request@lists.linuxfoundation.org?subject=subscribe>
X-List-Received-Date: Tue, 22 Mar 2022 15:08:49 -0000
--00000000000039600705dacffd80
Content-Type: text/plain; charset="UTF-8"
Setting aside my thoughts that something like Simplicity would make a
better platform than Bitcoin Script (due to expression operating on a more
narrow interface than the entire stack (I'm looking at you OP_DEPTH)) there
is an issue with namespace management.
If I understand correctly, your implication was that once opcodes are
redefined by an OP_RETURN transaction, subsequent transactions of that
opcode refer to the new microtransaction. But then we have a race
condition between people submitting transactions expecting the outputs to
refer to the old code and having their code redefined by the time they do
get confirmed (or worse having them reorged).
I've partially addressed this issue in my Simplicity design where the
commitment of a Simplicity program in a scriptpubkey covers the hash of the
specification of the jets used, which makes commits unambiguously to the
semantics (rightly or wrongly). But the issue resurfaces at redemption
time where I (currently) have a consensus critical map of codes to jets
that is used to decode the witness data into a Simplicity program. If one
were to allow this map of codes to jets to be replaced (rather than just
extended) then it would cause redemption to fail, because the hash of the
new jets would no longer match the hash of the jets appearing the the
input's scriptpubkey commitment. While this is still not good and I don't
recommend it, it is probably better than letting the semantics of your
programs be changed out from under you.
This comment is not meant as an endorsement of ths idea, which is a little
bit out there, at least as far as Bitcoin is concerned. :)
My long term plans are to move this consensus critical map of codes out of
the consensus layer and into the p2p layer where peers can negotiate their
own encodings between each other. But that plan is also a little bit out
there, and it still doesn't solve the issue of how to weight reused jets,
where weight is still consensus critical.
On Tue, Mar 22, 2022 at 1:37 AM ZmnSCPxj via bitcoin-dev <
bitcoin-dev@lists.linuxfoundation.org> wrote:
> Good morning list,
>
> It is entirely possible that I have gotten into the deep end and am now
> drowning in insanity, but here goes....
>
> Subject: Beyond Jets: Microcode: Consensus-Critical Jets Without Softforks
>
> Introduction
> ============
>
> Recent (Early 2022) discussions on the bitcoin-dev mailing
> list have largely focused on new constructs that enable new
> functionality.
>
> One general idea can be summarized this way:
>
> * We should provide a very general language.
> * Then later, once we have learned how to use this language,
> we can softfork in new opcodes that compress sections of
> programs written in this general language.
>
> There are two arguments against this style:
>
> 1. One of the most powerful arguments the "general" side of
> the "general v specific" debate is that softforks are
> painful because people are going to keep reiterating the
> activation parameters debate in a memoryless process, so
> we want to keep the number of softforks low.
> * So, we should just provide a very general language and
> never softfork in any other change ever again.
> 2. One of the most powerful arguments the "general" side of
> the "general v specific" debate is that softforks are
> painful because people are going to keep reiterating the
> activation parameters debate in a memoryless process, so
> we want to keep the number of softforks low.
> * So, we should just skip over the initial very general
> language and individually activate small, specific
> constructs, reducing the needed softforks by one.
>
> By taking a page from microprocessor design, it seems to me
> that we can use the same above general idea (a general base
> language where we later "bless" some sequence of operations)
> while avoiding some of the arguments against it.
>
> Digression: Microcodes In CISC Microprocessors
> ----------------------------------------------
>
> In the 1980s and 1990s, two competing microprocessor design
> paradigms arose:
>
> * Complex Instruction Set Computing (CISC)
> - Few registers, many addressing/indexing modes, variable
> instruction length, many obscure instructions.
> * Reduced Instruction Set Computing (RISC)
> - Many registers, usually only immediate and indexed
> addressing modes, fixed instruction length, few
> instructions.
>
> In CISC, the microprocessor provides very application-specific
> instructions, often with a small number of registers with
> specific uses.
> The instruction set was complicated, and often required
> multiple specific circuits for each application-specific
> instruction.
> Instructions had varying sizes and varying number of cycles.
>
> In RISC, the micrprocessor provides fewer instructions, and
> programmers (or compilers) are supposed to generate the code
> for all application-specific needs.
> The processor provided large register banks which could be
> used very generically and interchangeably.
> Instructions had the same size and every instruction took a
> fixed number of cycles.
>
> In CISC you usually had shorter code which could be written
> by human programmers in assembly language or machine language.
> In RISC, you generally had longer code, often difficult for
> human programmers to write, and you *needed* a compiler to
> generate it (unless you were very careful, or insane enough
> you could scroll over multiple pages of instructions without
> becoming more insane), or else you might forget about stuff
> like jump slots.
>
> For the most part, RISC lost, since most modern processors
> today are x86 or x86-64, an instruction set with varying
> instruction sizes, varying number of cycles per instruction,
> and complex instructions with application-specific uses.
>
> Or at least, it *looks like* RISC lost.
> In the 90s, Intel was struggling since their big beefy CISC
> designs were becoming too complicated.
> Bugs got past testing and into mass-produced silicon.
> RISC processors were beating the pants off 386s in terms of
> raw number of computations per second.
>
> RISC processors had the major advantage that they were
> inherently simpler, due to having fewer specific circuits
> and filling up their silicon with general-purpose registers
> (which are large but very simple circuits) to compensate.
> This meant that processor designers could fit more of the
> design in their merely human meat brains, and were less
> likely to make mistakes.
> The fixed number of cycles per instruction made it trivial
> to create a fixed-length pipeline for instruction processing,
> and practical RISC processors could deliver one instruction
> per clock cycle.
> Worse, the simplicity of RISC meant that smaller and less
> experienced teams could produce viable competitors to the
> Intel x86s.
>
> So what Intel did was to use a RISC processor, and add a
> special Instruction Decoder unit.
> The Instruction Decoder would take the CISC instruction
> stream accepted by classic Intel x86 processors, and emit
> RISC instructions for the internal RISC processor.
> CISC instructions might be variable length and have variable
> number of cycles, but the emitted RISC instructions were
> individually fixed length and fixed number of cycles.
> A CISC instruction might be equivalent to a single RISC
> instruction, or several.
>
> With this technique, Intel could deliver performance
> approaching their RISC-only competition, while retaining
> back-compatibility with existing software written for their
> classic CISC processors.
>
> At its core, the Instruction Decoder was a table-driven
> parser.
> This lookup table could be stored into on-chip flash memory.
> This had the advantage that the on-chip flash memory could be
> updated in case of bugs in the implementation of CISC
> instructions.
> This on-chip flash memory was then termed "microcode".
>
> Important advantages of this "microcode" technique were:
>
> * Back-compatibility with existing instruction sets.
> * Easier and more scalable underlying design due to ability
> to use RISC techniques while still supporting CISC instruction
> sets.
> * Possible to fix bugs in implementations of complex CISC
> instructions by uploading new microcode.
>
> (Obviously I have elided a bunch of stuff, but the above
> rough sketch should be sufficient as introduction.)
>
> Bitcoin Consensus Layer As Hardware
> -----------------------------------
>
> While Bitcoin fullnode implementations are software, because
> of the need for consensus, this software is not actually very
> "soft".
> One can consider that, just as it would take a long time for
> new hardware to be designed with a changed instruction set,
> it is similarly taking a long time to change Bitcoin to
> support changed feature sets.
>
> Thus, we should really consider the Bitcoin consensus layer,
> and its SCRIPT, as hardware that other Bitcoin software and
> layers run on top of.
>
> This thus opens up the thought of using techniques that were
> useful in hardware design.
> Such as microcode: a translation layer from "old" instruction
> sets to "new" instruction sets, with the ability to modify this
> mapping.
>
> Microcode For Bitcoin SCRIPT
> ============================
>
> I propose:
>
> * Define a generic, low-level language (the "RISC language").
> * Define a mapping from a specific, high-level language to
> the above language (the microcode).
> * Allow users to sacrifice Bitcoins to define a new microcode.
> * Have users indicate the microcode they wish to use to
> interpret their Tapscripts.
>
> As a concrete example, let us consider the current Bitcoin
> SCRIPT as the "CISC" language.
>
> We can then support a "RISC" language that is composed of
> general instructions, such as arithmetic, SECP256K1 scalar
> and point math, bytevector concatenation, sha256 midstates,
> bytevector bit manipulation, transaction introspection, and
> so on.
> This "RISC" language would also be stack-based.
> As the "RISC" language would have more possible opcodes,
> we may need to use 2-byte opcodes for the "RISC" language
> instead of 1-byte opcodes.
> Let us call this "RISC" language the micro-opcode language.
>
> Then, the "microcode" simply maps the existing Bitcoin
> SCRIPT `OP_` codes to one or more `UOP_` micro-opcodes.
>
> An interesting fact is that stack-based languages have
> automatic referential transparency; that is, if I define
> some new word in a stack-based language and use that word,
> I can replace verbatim the text of the new word in that
> place without issue.
> Compare this to a language like C, where macro authors
> have to be very careful about inadvertent variable
> capture, wrapping `do { ... } while(0)` to avoid problems
> with `if` and multiple statements, multiple execution, and
> so on.
>
> Thus, a sequence of `OP_` opcodes can be mapped to a
> sequence of equivalent `UOP_` micro-opcodes without
> changing the interpretation of the source language, an
> important property when considering such a "compiled"
> language.
>
> We start with a default microcode which is equivalent
> to the current Bitcoin language.
> When users want to define a new microcode to implement
> new `OP_` codes or change existing `OP_` codes, they
> can refer to a "base" microcode, and only have to
> provide the new mappings.
>
> A microcode is fundamentally just a mapping from an
> `OP_` code to a variable-length sequence of `UOP_`
> micro-opcodes.
>
> ```Haskell
> import Data.Map
> -- type Opcode
> -- type UOpcode
> newtype Microcode = Microcode (Map.Map Opcode [UOpcode])
> ```
>
> Semantically, the SCRIPT interpreter processes `UOP_`
> micro-opcodes.
>
> ```Haskell
> -- instance Monad Interpreter -- can `fail`.
> interpreter :: Transaction -> TxInput -> [UOpcode] -> Interpreter ()
> ```
>
> Example
> -------
>
> Suppose a user wants to re-enable `OP_CAT`, and nothing
> else.
>
> That user creates a microcode, referring to the current
> default Bitcoin SCRIPT microcode as the "base".
> The base microcode defines `OP_CAT` as equal to the
> sequence `UOP_FAIL` i.e. a micro-opcode that always fails.
> However, the new microcode will instead redefine the
> `OP_CAT` as the micro-opcode sequence `UOP_CAT`.
>
> Microcodes then have a standard way of being represented
> as a byte sequence.
> The user serializes their new microcode as a byte
> sequence.
>
> Then, the user creates a new transaction where one of
> the outputs contains, say, 1.0 Bitcoins (exact required
> value TBD), and has the `scriptPubKey` of
> `OP_TRUE OP_RETURN <serialized_microcode>`.
> This output is a "microcode introduction output", which
> is provably unspendable, thus burning the Bitcoins.
>
> (It need not be a single user, multiple users can
> coordinate by signing a single transaction that commits
> their funds to the microcode introduction.)
>
> Once the above transaction has been deeply confirmed,
> the user can then take the hash of the microcode
> serialization.
> Then the user can use a SCRIPT with `OP_CAT` enabled,
> by using a Tapscript with, say, version `0xce`, and
> with the SCRIPT having the microcode hash as its first
> bytes, followed by the `OP_` codes.
>
> Fullnodes will then process recognized microcode
> introduction outputs and store mappings from their
> hashes to the microcodes in a new microcodes index.
> Fullnodes can then process version-`0xce` Tapscripts
> by checking if the microcodes index has the indicated
> microcode hash.
>
> Semantically, fullnodes take the SCRIPT, and for each
> `OP_` code in it, expands it to a sequence of `UOP_`
> micro-opcodes, then concatenates each such sequence.
> Then, the SCRIPT interpreter operates over a sequence
> of `UOP_` micro-opcodes.
>
> Optimizing Microcodes
> ---------------------
>
> Suppose there is some new microcode that users have
> published onchain.
>
> We want to be able to execute the defined microcode
> faster than expanding an `OP_`-code SCRIPT to a
> `UOP_`-code SCRIPT and having an interpreter loop
> over the `UOP_`-code SCRIPT.
>
> We can use LLVM.
>
> WARNING: LLVM might not be appropriate for
> network-facing security-sensitive applications.
> In particular, LLVM bugs. especially nondeterminism
> bugs, can lead to consensus divergence and disastrous
> chainsplits!
> On the other hand, LLVM bugs are compiler bugs and
> the same bugs can hit the static compiler `cc`, too,
> since the same LLVM code runs in both JIT and static
> compilation, so this risk already exists for Bitcoin.
> (i.e. we already rely on LLVM not being buggy enough
> to trigger Bitcoin consensus divergence, else we would
> have written Bitcoin Core SCRIPT interpreter in
> assembly.)
>
> Each `UOP_`-code has an equivalent tree of LLVM code.
> For each `Opcode` in the microcode, we take its
> sequence of `UOpcode`s and expand them to this tree,
> concatenating the equivalent trees for each `UOpcode`
> in the sequence.
> Then we ask LLVM to JIT-compile this code to a new
> function, running LLVM-provided optimizers.
> Then we put a pointer to this compiled function to a
> 256-long array of functions, where the array index is
> the `OP_` code.
>
> The SCRIPT interpreter then simply iterates over the
> `OP_` code SCRIPT and calls each of the JIT-compiled
> functions.
> This reduces much of the overhead of the `UOP_` layer
> and makes it approach the current performance of the
> existing `OP_` interpreter.
>
> For the default Bitcoin SCRIPT, the opcodes array
> contains pointers to statically-compiled functions.
> A microcode that is based on the default Bitcoin
> SCRIPT copies this opcodes array, then overwrites
> the entries.
>
> Future versions of Bitcoin Core can "bless"
> particular microcodes by providing statically-compiled
> functions for those microcodes.
> This leads to even better performance (there is
> no need to recompile ancient onchain microcodes each
> time Bitcoin Core starts) without any consensus
> divergence.
> It is a pure optimization and does not imply a
> tightening of rules, and is thus not a softfork.
>
> (To reduce the chance of network faults being used
> to poke into `W|X` memory (since `W|X` memory is
> needed in order to actually JIT compile) we can
> isolate the SCRIPT interpreter into its own process
> separate from the network-facing code.
> This does imply additional overhead in serializing
> transactions we want to ask the SCRIPT interpreter
> to validate.)
>
> Comparison To Jets
> ------------------
>
> This technique allows users to define "jets", i.e.
> sequences of low-level general operations that users
> have determined are common enough they should just
> be implemented as faster code that is executed
> directly by the underlying hardware processor rather
> than via a software interpreter.
> Basically, each redefined `OP_` code is a jet of a
> sequence of `UOP_` micro-opcodes.
>
> We implement this by dynamically JIT-compiling the
> proposed jets, as described above.
> SCRIPTs using jetted code remain smaller, as the
> jet definition is done in a previous transaction and
> does not require copy-pasta (Do Not Repeat Yourself!).
> At the same time, jettification is not tied to
> developers, thus removing the need to keep softforking
> new features --- we only need define a sufficiently
> general language and then we can implement pretty much
> anything worth implementing (and a bunch of other things
> that should not be implemented, but hey, users gonna
> use...).
>
> Bugs in existing microcodes can be fixed by basing a
> new microcode from the existing microcode, and
> redefining the buggy implementation.
> Existing Tapscripts need to be re-spent to point to
> the new bugfixed microcode, but if you used the
> point-spend branch as an N-of-N of all participants
> you have an upgrade mechanism for free.
>
> In order to ensure that the JIT-compilation of new
> microcodes is not triggered trivially, we require
> that users petitioning for the jettification of some
> operations (i.e. introducing a new microcode) must
> sacrifice Bitcoins.
>
> Burning Bitcoins is better than increasing the weight
> of microcode introduction outputs; all fullnodes are
> affected by the need to JIT-compile the new microcode,
> so they benefit from the reduction in supply, thus
> getting compensated for the work of JIT-compiling the
> new microcode.
> Ohter mechanisms for making microcode introduction
> outputs expensive are also possible.
>
> Nothing really requires that we use a stack-based
> language for this; any sufficiently FP language
> should allow referential transparency.
> _______________________________________________
> bitcoin-dev mailing list
> bitcoin-dev@lists.linuxfoundation.org
> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
>
--00000000000039600705dacffd80
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Setting aside my thoughts that something like Simplic=
ity would make a better platform than Bitcoin Script (due to expression ope=
rating on a more narrow interface than the entire stack (I'm looking at=
you OP_DEPTH)) there is an issue with namespace management.</div><div><br>=
</div><div>If I understand correctly, your implication was that once opcode=
s are redefined by an OP_RETURN transaction, subsequent transactions of tha=
t opcode refer to the new microtransaction.=C2=A0 But then we have a race c=
ondition between people submitting transactions expecting the outputs to re=
fer to the old code and having their code redefined by the time they do get=
confirmed=C2=A0 (or worse having them reorged).<br></div><div><br></div><d=
iv>I've partially addressed this issue in my Simplicity design where th=
e commitment of a Simplicity program in a scriptpubkey covers the hash of t=
he specification of the jets used, which makes commits unambiguously to the=
semantics (rightly or wrongly).=C2=A0 But the issue resurfaces at redempti=
on time where I (currently) have a consensus critical map of codes to jets =
that is used to decode the witness data into a Simplicity program.=C2=A0 If=
one were to allow this map of codes to jets to be replaced (rather than ju=
st extended) then it would cause redemption to fail, because the hash of th=
e new jets would no longer match the hash of the jets appearing the the inp=
ut's scriptpubkey commitment.=C2=A0 While this is still not good and I =
don't recommend it, it is probably better than letting the semantics of=
your programs be changed out from under you.</div><div><br></div><div>This=
comment is not meant as an endorsement of ths idea, which is a little bit =
out there, at least as far as Bitcoin is concerned. :)<br></div><div><br></=
div><div>My long term plans are to move this consensus critical map of code=
s out of the consensus layer and into the p2p layer where peers can negotia=
te their own encodings between each other.=C2=A0 But that plan is also a li=
ttle bit out there, and it still doesn't solve the issue of how to weig=
ht reused jets, where weight is still consensus critical.<br></div></div><b=
r><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, =
Mar 22, 2022 at 1:37 AM ZmnSCPxj via bitcoin-dev <<a href=3D"mailto:bitc=
oin-dev@lists.linuxfoundation.org">bitcoin-dev@lists.linuxfoundation.org</a=
>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px=
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Goo=
d morning list,<br>
<br>
It is entirely possible that I have gotten into the deep end and am now dro=
wning in insanity, but here goes....<br>
<br>
Subject: Beyond Jets: Microcode: Consensus-Critical Jets Without Softforks<=
br>
<br>
Introduction<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<br>
<br>
Recent (Early 2022) discussions on the bitcoin-dev mailing<br>
list have largely focused on new constructs that enable new<br>
functionality.<br>
<br>
One general idea can be summarized this way:<br>
<br>
* We should provide a very general language.<br>
=C2=A0 * Then later, once we have learned how to use this language,<br>
=C2=A0 =C2=A0 we can softfork in new opcodes that compress sections of<br>
=C2=A0 =C2=A0 programs written in this general language.<br>
<br>
There are two arguments against this style:<br>
<br>
1.=C2=A0 One of the most powerful arguments the "general" side of=
<br>
=C2=A0 =C2=A0 the "general v specific" debate is that softforks a=
re<br>
=C2=A0 =C2=A0 painful because people are going to keep reiterating the<br>
=C2=A0 =C2=A0 activation parameters debate in a memoryless process, so<br>
=C2=A0 =C2=A0 we want to keep the number of softforks low.<br>
=C2=A0 =C2=A0 * So, we should just provide a very general language and<br>
=C2=A0 =C2=A0 =C2=A0 never softfork in any other change ever again.<br>
2.=C2=A0 One of the most powerful arguments the "general" side of=
<br>
=C2=A0 =C2=A0 the "general v specific" debate is that softforks a=
re<br>
=C2=A0 =C2=A0 painful because people are going to keep reiterating the<br>
=C2=A0 =C2=A0 activation parameters debate in a memoryless process, so<br>
=C2=A0 =C2=A0 we want to keep the number of softforks low.<br>
=C2=A0 =C2=A0 * So, we should just skip over the initial very general<br>
=C2=A0 =C2=A0 =C2=A0 language and individually activate small, specific<br>
=C2=A0 =C2=A0 =C2=A0 constructs, reducing the needed softforks by one.<br>
<br>
By taking a page from microprocessor design, it seems to me<br>
that we can use the same above general idea (a general base<br>
language where we later "bless" some sequence of operations)<br>
while avoiding some of the arguments against it.<br>
<br>
Digression: Microcodes In CISC Microprocessors<br>
----------------------------------------------<br>
<br>
In the 1980s and 1990s, two competing microprocessor design<br>
paradigms arose:<br>
<br>
* Complex Instruction Set Computing (CISC)<br>
=C2=A0 - Few registers, many addressing/indexing modes, variable<br>
=C2=A0 =C2=A0 instruction length, many obscure instructions.<br>
* Reduced Instruction Set Computing (RISC)<br>
=C2=A0 - Many registers, usually only immediate and indexed<br>
=C2=A0 =C2=A0 addressing modes, fixed instruction length, few<br>
=C2=A0 =C2=A0 instructions.<br>
<br>
In CISC, the microprocessor provides very application-specific<br>
instructions, often with a small number of registers with<br>
specific uses.<br>
The instruction set was complicated, and often required<br>
multiple specific circuits for each application-specific<br>
instruction.<br>
Instructions had varying sizes and varying number of cycles.<br>
<br>
In RISC, the micrprocessor provides fewer instructions, and<br>
programmers (or compilers) are supposed to generate the code<br>
for all application-specific needs.<br>
The processor provided large register banks which could be<br>
used very generically and interchangeably.<br>
Instructions had the same size and every instruction took a<br>
fixed number of cycles.<br>
<br>
In CISC you usually had shorter code which could be written<br>
by human programmers in assembly language or machine language.<br>
In RISC, you generally had longer code, often difficult for<br>
human programmers to write, and you *needed* a compiler to<br>
generate it (unless you were very careful, or insane enough<br>
you could scroll over multiple pages of instructions without<br>
becoming more insane), or else you might forget about stuff<br>
like jump slots.<br>
<br>
For the most part, RISC lost, since most modern processors<br>
today are x86 or x86-64, an instruction set with varying<br>
instruction sizes, varying number of cycles per instruction,<br>
and complex instructions with application-specific uses.<br>
<br>
Or at least, it *looks like* RISC lost.<br>
In the 90s, Intel was struggling since their big beefy CISC<br>
designs were becoming too complicated.<br>
Bugs got past testing and into mass-produced silicon.<br>
RISC processors were beating the pants off 386s in terms of<br>
raw number of computations per second.<br>
<br>
RISC processors had the major advantage that they were<br>
inherently simpler, due to having fewer specific circuits<br>
and filling up their silicon with general-purpose registers<br>
(which are large but very simple circuits) to compensate.<br>
This meant that processor designers could fit more of the<br>
design in their merely human meat brains, and were less<br>
likely to make mistakes.<br>
The fixed number of cycles per instruction made it trivial<br>
to create a fixed-length pipeline for instruction processing,<br>
and practical RISC processors could deliver one instruction<br>
per clock cycle.<br>
Worse, the simplicity of RISC meant that smaller and less<br>
experienced teams could produce viable competitors to the<br>
Intel x86s.<br>
<br>
So what Intel did was to use a RISC processor, and add a<br>
special Instruction Decoder unit.<br>
The Instruction Decoder would take the CISC instruction<br>
stream accepted by classic Intel x86 processors, and emit<br>
RISC instructions for the internal RISC processor.<br>
CISC instructions might be variable length and have variable<br>
number of cycles, but the emitted RISC instructions were<br>
individually fixed length and fixed number of cycles.<br>
A CISC instruction might be equivalent to a single RISC<br>
instruction, or several.<br>
<br>
With this technique, Intel could deliver performance<br>
approaching their RISC-only competition, while retaining<br>
back-compatibility with existing software written for their<br>
classic CISC processors.<br>
<br>
At its core, the Instruction Decoder was a table-driven<br>
parser.<br>
This lookup table could be stored into on-chip flash memory.<br>
This had the advantage that the on-chip flash memory could be<br>
updated in case of bugs in the implementation of CISC<br>
instructions.<br>
This on-chip flash memory was then termed "microcode".<br>
<br>
Important advantages of this "microcode" technique were:<br>
<br>
* Back-compatibility with existing instruction sets.<br>
* Easier and more scalable underlying design due to ability<br>
=C2=A0 to use RISC techniques while still supporting CISC instruction<br>
=C2=A0 sets.<br>
* Possible to fix bugs in implementations of complex CISC<br>
=C2=A0 instructions by uploading new microcode.<br>
<br>
(Obviously I have elided a bunch of stuff, but the above<br>
rough sketch should be sufficient as introduction.)<br>
<br>
Bitcoin Consensus Layer As Hardware<br>
-----------------------------------<br>
<br>
While Bitcoin fullnode implementations are software, because<br>
of the need for consensus, this software is not actually very<br>
"soft".<br>
One can consider that, just as it would take a long time for<br>
new hardware to be designed with a changed instruction set,<br>
it is similarly taking a long time to change Bitcoin to<br>
support changed feature sets.<br>
<br>
Thus, we should really consider the Bitcoin consensus layer,<br>
and its SCRIPT, as hardware that other Bitcoin software and<br>
layers run on top of.<br>
<br>
This thus opens up the thought of using techniques that were<br>
useful in hardware design.<br>
Such as microcode: a translation layer from "old" instruction<br>
sets to "new" instruction sets, with the ability to modify this<b=
r>
mapping.<br>
<br>
Microcode For Bitcoin SCRIPT<br>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D<br>
<br>
I propose:<br>
<br>
* Define a generic, low-level language (the "RISC language").<br>
* Define a mapping from a specific, high-level language to<br>
=C2=A0 the above language (the microcode).<br>
* Allow users to sacrifice Bitcoins to define a new microcode.<br>
* Have users indicate the microcode they wish to use to<br>
=C2=A0 interpret their Tapscripts.<br>
<br>
As a concrete example, let us consider the current Bitcoin<br>
SCRIPT as the "CISC" language.<br>
<br>
We can then support a "RISC" language that is composed of<br>
general instructions, such as arithmetic, SECP256K1 scalar<br>
and point math, bytevector concatenation, sha256 midstates,<br>
bytevector bit manipulation, transaction introspection, and<br>
so on.<br>
This "RISC" language would also be stack-based.<br>
As the "RISC" language would have more possible opcodes,<br>
we may need to use 2-byte opcodes for the "RISC" language<br>
instead of 1-byte opcodes.<br>
Let us call this "RISC" language the micro-opcode language.<br>
<br>
Then, the "microcode" simply maps the existing Bitcoin<br>
SCRIPT `OP_` codes to one or more `UOP_` micro-opcodes.<br>
<br>
An interesting fact is that stack-based languages have<br>
automatic referential transparency; that is, if I define<br>
some new word in a stack-based language and use that word,<br>
I can replace verbatim the text of the new word in that<br>
place without issue.<br>
Compare this to a language like C, where macro authors<br>
have to be very careful about inadvertent variable<br>
capture, wrapping `do { ... } while(0)` to avoid problems<br>
with `if` and multiple statements, multiple execution, and<br>
so on.<br>
<br>
Thus, a sequence of `OP_` opcodes can be mapped to a<br>
sequence of equivalent `UOP_` micro-opcodes without<br>
changing the interpretation of the source language, an<br>
important property when considering such a "compiled"<br>
language.<br>
<br>
We start with a default microcode which is equivalent<br>
to the current Bitcoin language.<br>
When users want to define a new microcode to implement<br>
new `OP_` codes or change existing `OP_` codes, they<br>
can refer to a "base" microcode, and only have to<br>
provide the new mappings.<br>
<br>
A microcode is fundamentally just a mapping from an<br>
`OP_` code to a variable-length sequence of `UOP_`<br>
micro-opcodes.<br>
<br>
```Haskell<br>
import Data.Map<br>
-- type Opcode<br>
-- type UOpcode<br>
newtype Microcode =3D Microcode (Map.Map Opcode [UOpcode])<br>
```<br>
<br>
Semantically, the SCRIPT interpreter processes `UOP_`<br>
micro-opcodes.<br>
<br>
```Haskell<br>
-- instance Monad Interpreter -- can `fail`.<br>
interpreter :: Transaction -> TxInput -> [UOpcode] -> Interpreter =
()<br>
```<br>
<br>
Example<br>
-------<br>
<br>
Suppose a user wants to re-enable `OP_CAT`, and nothing<br>
else.<br>
<br>
That user creates a microcode, referring to the current<br>
default Bitcoin SCRIPT microcode as the "base".<br>
The base microcode defines `OP_CAT` as equal to the<br>
sequence `UOP_FAIL` i.e. a micro-opcode that always fails.<br>
However, the new microcode will instead redefine the<br>
`OP_CAT` as the micro-opcode sequence `UOP_CAT`.<br>
<br>
Microcodes then have a standard way of being represented<br>
as a byte sequence.<br>
The user serializes their new microcode as a byte<br>
sequence.<br>
<br>
Then, the user creates a new transaction where one of<br>
the outputs contains, say, 1.0 Bitcoins (exact required<br>
value TBD), and has the `scriptPubKey` of<br>
`OP_TRUE OP_RETURN <serialized_microcode>`.<br>
This output is a "microcode introduction output", which<br>
is provably unspendable, thus burning the Bitcoins.<br>
<br>
(It need not be a single user, multiple users can<br>
coordinate by signing a single transaction that commits<br>
their funds to the microcode introduction.)<br>
<br>
Once the above transaction has been deeply confirmed,<br>
the user can then take the hash of the microcode<br>
serialization.<br>
Then the user can use a SCRIPT with `OP_CAT` enabled,<br>
by using a Tapscript with, say, version `0xce`, and<br>
with the SCRIPT having the microcode hash as its first<br>
bytes, followed by the `OP_` codes.<br>
<br>
Fullnodes will then process recognized microcode<br>
introduction outputs and store mappings from their<br>
hashes to the microcodes in a new microcodes index.<br>
Fullnodes can then process version-`0xce` Tapscripts<br>
by checking if the microcodes index has the indicated<br>
microcode hash.<br>
<br>
Semantically, fullnodes take the SCRIPT, and for each<br>
`OP_` code in it, expands it to a sequence of `UOP_`<br>
micro-opcodes, then concatenates each such sequence.<br>
Then, the SCRIPT interpreter operates over a sequence<br>
of `UOP_` micro-opcodes.<br>
<br>
Optimizing Microcodes<br>
---------------------<br>
<br>
Suppose there is some new microcode that users have<br>
published onchain.<br>
<br>
We want to be able to execute the defined microcode<br>
faster than expanding an `OP_`-code SCRIPT to a<br>
`UOP_`-code SCRIPT and having an interpreter loop<br>
over the `UOP_`-code SCRIPT.<br>
<br>
We can use LLVM.<br>
<br>
WARNING: LLVM might not be appropriate for<br>
network-facing security-sensitive applications.<br>
In particular, LLVM bugs. especially nondeterminism<br>
bugs, can lead to consensus divergence and disastrous<br>
chainsplits!<br>
On the other hand, LLVM bugs are compiler bugs and<br>
the same bugs can hit the static compiler `cc`, too,<br>
since the same LLVM code runs in both JIT and static<br>
compilation, so this risk already exists for Bitcoin.<br>
(i.e. we already rely on LLVM not being buggy enough<br>
to trigger Bitcoin consensus divergence, else we would<br>
have written Bitcoin Core SCRIPT interpreter in<br>
assembly.)<br>
<br>
Each `UOP_`-code has an equivalent tree of LLVM code.<br>
For each `Opcode` in the microcode, we take its<br>
sequence of `UOpcode`s and expand them to this tree,<br>
concatenating the equivalent trees for each `UOpcode`<br>
in the sequence.<br>
Then we ask LLVM to JIT-compile this code to a new<br>
function, running LLVM-provided optimizers.<br>
Then we put a pointer to this compiled function to a<br>
256-long array of functions, where the array index is<br>
the `OP_` code.<br>
<br>
The SCRIPT interpreter then simply iterates over the<br>
`OP_` code SCRIPT and calls each of the JIT-compiled<br>
functions.<br>
This reduces much of the overhead of the `UOP_` layer<br>
and makes it approach the current performance of the<br>
existing `OP_` interpreter.<br>
<br>
For the default Bitcoin SCRIPT, the opcodes array<br>
contains pointers to statically-compiled functions.<br>
A microcode that is based on the default Bitcoin<br>
SCRIPT copies this opcodes array, then overwrites<br>
the entries.<br>
<br>
Future versions of Bitcoin Core can "bless"<br>
particular microcodes by providing statically-compiled<br>
functions for those microcodes.<br>
This leads to even better performance (there is<br>
no need to recompile ancient onchain microcodes each<br>
time Bitcoin Core starts) without any consensus<br>
divergence.<br>
It is a pure optimization and does not imply a<br>
tightening of rules, and is thus not a softfork.<br>
<br>
(To reduce the chance of network faults being used<br>
to poke into `W|X` memory (since `W|X` memory is<br>
needed in order to actually JIT compile) we can<br>
isolate the SCRIPT interpreter into its own process<br>
separate from the network-facing code.<br>
This does imply additional overhead in serializing<br>
transactions we want to ask the SCRIPT interpreter<br>
to validate.)<br>
<br>
Comparison To Jets<br>
------------------<br>
<br>
This technique allows users to define "jets", i.e.<br>
sequences of low-level general operations that users<br>
have determined are common enough they should just<br>
be implemented as faster code that is executed<br>
directly by the underlying hardware processor rather<br>
than via a software interpreter.<br>
Basically, each redefined `OP_` code is a jet of a<br>
sequence of `UOP_` micro-opcodes.<br>
<br>
We implement this by dynamically JIT-compiling the<br>
proposed jets, as described above.<br>
SCRIPTs using jetted code remain smaller, as the<br>
jet definition is done in a previous transaction and<br>
does not require copy-pasta (Do Not Repeat Yourself!).<br>
At the same time, jettification is not tied to<br>
developers, thus removing the need to keep softforking<br>
new features --- we only need define a sufficiently<br>
general language and then we can implement pretty much<br>
anything worth implementing (and a bunch of other things<br>
that should not be implemented, but hey, users gonna<br>
use...).<br>
<br>
Bugs in existing microcodes can be fixed by basing a<br>
new microcode from the existing microcode, and<br>
redefining the buggy implementation.<br>
Existing Tapscripts need to be re-spent to point to<br>
the new bugfixed microcode, but if you used the<br>
point-spend branch as an N-of-N of all participants<br>
you have an upgrade mechanism for free.<br>
<br>
In order to ensure that the JIT-compilation of new<br>
microcodes is not triggered trivially, we require<br>
that users petitioning for the jettification of some<br>
operations (i.e. introducing a new microcode) must<br>
sacrifice Bitcoins.<br>
<br>
Burning Bitcoins is better than increasing the weight<br>
of microcode introduction outputs; all fullnodes are<br>
affected by the need to JIT-compile the new microcode,<br>
so they benefit from the reduction in supply, thus<br>
getting compensated for the work of JIT-compiling the<br>
new microcode.<br>
Ohter mechanisms for making microcode introduction<br>
outputs expensive are also possible.<br>
<br>
Nothing really requires that we use a stack-based<br>
language for this; any sufficiently FP language<br>
should allow referential transparency.<br>
_______________________________________________<br>
bitcoin-dev mailing list<br>
<a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D"_blank">=
bitcoin-dev@lists.linuxfoundation.org</a><br>
<a href=3D"https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev" =
rel=3D"noreferrer" target=3D"_blank">https://lists.linuxfoundation.org/mail=
man/listinfo/bitcoin-dev</a><br>
</blockquote></div>
--00000000000039600705dacffd80--
|