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
|
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 DBD9FC000E
for <bitcoin-dev@lists.linuxfoundation.org>;
Sat, 3 Jul 2021 20:13:06 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1])
by smtp2.osuosl.org (Postfix) with ESMTP id ABE28401F0
for <bitcoin-dev@lists.linuxfoundation.org>;
Sat, 3 Jul 2021 20:13:06 +0000 (UTC)
X-Virus-Scanned: amavisd-new at osuosl.org
X-Spam-Flag: NO
X-Spam-Score: -1.9
X-Spam-Level:
X-Spam-Status: No, score=-1.9 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_PASS=-0.001]
autolearn=ham autolearn_force=no
Authentication-Results: smtp2.osuosl.org (amavisd-new);
dkim=pass (2048-bit key)
header.d=blockstream-com.20150623.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 zH6l1UPHIZfT
for <bitcoin-dev@lists.linuxfoundation.org>;
Sat, 3 Jul 2021 20:13:04 +0000 (UTC)
X-Greylist: whitelisted by SQLgrey-1.8.0
Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com
[IPv6:2607:f8b0:4864:20::f2c])
by smtp2.osuosl.org (Postfix) with ESMTPS id 514F5401D5
for <bitcoin-dev@lists.linuxfoundation.org>;
Sat, 3 Jul 2021 20:13:04 +0000 (UTC)
Received: by mail-qv1-xf2c.google.com with SMTP id h18so6504414qve.1
for <bitcoin-dev@lists.linuxfoundation.org>;
Sat, 03 Jul 2021 13:13:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=blockstream-com.20150623.gappssmtp.com; s=20150623;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=sIadC1bPhOi+rK9nC8f5NbXuLZvAloVEVX4og9sssaE=;
b=HLpof99LiQWFlKP0+H0GapL9jweWabV2S5leUymjdoHu34zsyhTutDBdl1FPqJQOur
5i7w/2I1Vx8oMSEL88mgoPQyS/td9ABCUcY8GED6N6BdfFKxyDt7oRsdJld+9bibgp9H
hFXsZ5rqC3UAAlWFLsB6hCosXFNWL7mcYRr3lefsHxSKojMHDmDV8NP0PaAn7kPSIUgw
ajUZyNmUu+Yjc/clAUcyVWilfqFeYUPnIElODWf4+KjmndjSoQ0MbUf+XhfUMAB8kuLy
fR88fxTVXAhlYbCfspAe3QAEcDB5I7zHE9pPm/oIf57412bupxFp7ALD5XbMDmQ2qFlO
fV+A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=sIadC1bPhOi+rK9nC8f5NbXuLZvAloVEVX4og9sssaE=;
b=WasApouOlB/VaiZkH+SIwad6cs2vbygWKnQPo1Ah9U6Hmz1TVhdX1QM1RhoMHjU824
tK2gaz0f0sJUVN7od/DUcEqY4x5acmChvOHe31IQK95OHsrd20fXoK4gUHliJz1fsT+w
e6Vq/0EVTVwdV95aJ3WkdRDflOXH4isSk5MamNO70qC/VeCUJ8bYnrvOMDozOmqB1DlK
tIZMZ0toQlSCk6DklLO0i9jdaa/FRTny7mc8HjnxQ9XNVoVfA81iDmSyYhsTAywMbWeK
2FRrnz4Vqft04vjELU3rTJjRvSUrYjchJsPAsEQtgYlI9Be7xu/Ov64xxd9a3aGeMIwS
XoxQ==
X-Gm-Message-State: AOAM5329bGt1wNZ4I8avrp0NicaWtu+fdISKUblK7N6yqQwIRZExSYS9
Y4jy3VqiOeby9ZBNoRfTgxhUGBZRKuPbSVz2SgBvpA==
X-Google-Smtp-Source: ABdhPJxigqZrVLfXj0AOrgL6NFyrEyBbpBdUOt4kCwmoA3M5XpEWJ4fBypKY2+5/aujRORsgELQLWht2Dxi6EnltjDo=
X-Received: by 2002:a05:6214:e68:: with SMTP id
jz8mr5319916qvb.13.1625343183155;
Sat, 03 Jul 2021 13:13:03 -0700 (PDT)
MIME-Version: 1.0
References: <CAD5xwhjmu-Eee47Ho5eA6E6+aAdnchLU0OVZo=RTHaXnN17x8A@mail.gmail.com>
<CAMZUoK=-jrH+fr=tUTHmLojm2-Ff99KYm9H97yhd=7bcOVG=fg@mail.gmail.com>
<CAD5xwhg0N1byx-G2tk=jjmZSHSBirpaX6OHTnh_x9iDEVF8PrQ@mail.gmail.com>
In-Reply-To: <CAD5xwhg0N1byx-G2tk=jjmZSHSBirpaX6OHTnh_x9iDEVF8PrQ@mail.gmail.com>
From: "Russell O'Connor" <roconnor@blockstream.com>
Date: Sat, 3 Jul 2021 16:12:51 -0400
Message-ID: <CAMZUoKnYAKum63fRUNJD-zAZX_p3MoFULGWRE7J2QkO69nOe8g@mail.gmail.com>
To: Jeremy <jlrubin@mit.edu>
Content-Type: multipart/alternative; boundary="000000000000120b8405c63db347"
Cc: Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
Subject: Re: [bitcoin-dev] CHECKSIGFROMSTACK/{Verify} BIP for Bitcoin
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: Sat, 03 Jul 2021 20:13:07 -0000
--000000000000120b8405c63db347
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
There is one line written at
https://github.com/ElementsProject/elements/pull/949/files#r660130155. I
suppose we need to decide on which variants of *VERIFY and *ADD we want to
include (presumably all of them) and choose which opcodes they will be
assigned to. And I guess for CHECKSIGFROMSTACKADD will want to place the n
value between the signature and the message on the stack. ... So I suppose
we will need more than one sentence.
The semantics would be basically to call secp256k1_schnorrsig_verify <
https://github.com/bitcoin-core/secp256k1/blob/0440945fb5ce69d335fed32827b5=
166e84b02e05/include/secp256k1_schnorrsig.h#L158>,
treating pubkeys and signatures the same way the other CHECKSIG operations
do, and in passing the (variable length) message from the stack.
CHECKSIGFROMSTACK would also be subject to the same sigops budget that
CHECKSIG has in tapscript.
On Sat, Jul 3, 2021 at 2:30 PM Jeremy <jlrubin@mit.edu> wrote:
> Awesome to hear that!
>
> Actually I don't think I did know (or I forgot/didn't catch it) that ther=
e
> was an updated spec for elements, I searched around for what I could find
> and came up empty handed. Do you have any links for that? That sounds
> perfect to me.
>
>
> On Sat, Jul 3, 2021, 10:50 AM Russell O'Connor <roconnor@blockstream.com>
> wrote:
>
>> Hi Jermy,
>>
>> As you are aware, we, and by we I mean mostly Sanket, are developing an
>> updated OP_CHECKSIGFROMSTACK implementation for tapscript on elements. =
The
>> plan here would be to effectively support the an interface to the
>> variable-length extension of BIP-0340 schnorr signatures.
>>
>> BIP-0340 would dispense with DER encoding (good riddance).
>> BIP-0340 signatures are batch verifiable along with other BIP-0340
>> transaction signatures and taproot tweak verification.
>> Support for variable length messages in BIP-0340 has been discussed in <
>> https://github.com/sipa/bips/issues/207> and an implementation has
>> recently been merged in <
>> https://github.com/bitcoin-core/secp256k1/pull/844>. The BIP has not
>> yet been updated but the difference is that the message m does not have =
to
>> be 32-bytes (it is recommended that the message be a 32-bit tagged hash =
or
>> a message with a 64-bit application specific prefix). The CHECKSIGFROMST=
ACK
>> operation (in tapscript) would use a stack item for this m value to
>> BIP-0340 signature verification and would not necessarily have to be 32
>> bytes.
>>
>> I think this design we are aiming for would be perfectly suited for
>> Bitcoin as well.
>>
>> On Sat, Jul 3, 2021 at 12:32 PM Jeremy via bitcoin-dev <
>> bitcoin-dev@lists.linuxfoundation.org> wrote:
>>
>>> Reproduced below is the BIP text from Bitcoin Cash's (MIT-Licensed)
>>> specification for "CheckDataSig", more or less the same thing as
>>> CHECKSIGFROMSTACK
>>> https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/op_c=
heckdatasig.md.
>>> In contrast to Element's implementation, it does not have Element's bug=
s
>>> around verify semantics and uses the nullfail rule, and there is a
>>> specification document so it seemed like the easiest starting point for
>>> discussion v.s. drafting something from scratch.
>>>
>>> Does anyone have any issue with adapting this exact text and
>>> implementation to a BIP for Bitcoin using 2 OP_SUCCESSX opcodes?
>>>
>>> Note that with *just* CheckSigFromStack, while you can do some very
>>> valuable use cases, but without OP_CAT it does not enable sophisticated
>>> covenants (and as per
>>> https://www.wpsoftware.net/andrew/blog/cat-and-schnorr-tricks-i.html
>>> just CAT alone enables such uses).
>>>
>>> Design questions worth considering as modifications:
>>>
>>> 1. Should CSFS require some sort of tagged hash? Very likely answer is
>>> no =E2=80=93 tags interfere with certain use cases
>>> 2. Should CSFS split the signature=E2=80=99s R & S value stack items fo=
r some
>>> applications that otherwise may require OP_CAT? E.g. using a pinned R v=
alue
>>> allows you to extract a private key if ever double signed, using 2 R va=
lues
>>> allows pay-to-reveal-key contracts. Most likely answer is no, if that i=
s
>>> desired then OP_CAT can be introduced
>>> 3. Should CSFS support a cheap way to reference the taproot internal or
>>> external key? Perhaps, can be handled with undefined upgradeable keytyp=
es.
>>> One might want to use the internal key, if the signed data should be va=
lid
>>> independent of the tapscript tree. One might want to use the external k=
ey,
>>> if the data should only be valid for a single tapscript key + tree.
>>> 4. Should invalid public keys types be a NOP to support future extended
>>> pubkey types?
>>>
>>>
>>>
>>> Best,
>>>
>>>
>>> Jeremy
>>>
>>>
>>> ---
>>> layout: specification
>>> title: OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY Specification
>>> category: spec
>>> date: 2018-08-20
>>> activation: 1542300000
>>> version: 0.6
>>> ---
>>>
>>> OP_CHECKDATASIG
>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>>
>>> OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY check whether a signature is =
valid with respect to a message and a public key.
>>>
>>> OP_CHECKDATASIG permits data to be imported into a script, and have its=
validity checked against some signing authority such as an "Oracle".
>>>
>>> OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY are designed to be implemente=
d similarly to OP_CHECKSIG [1]. Conceptually, one could imagine OP_CHECKSIG=
functionality being replaced by OP_CHECKDATASIG, along with a separate Op =
Code to create a hash from the transaction based on the SigHash algorithm.
>>>
>>> OP_CHECKDATASIG Specification
>>> -----------------------------
>>>
>>> ### Semantics
>>>
>>> OP_CHECKDATASIG fails immediately if the stack is not well formed. To b=
e well formed, the stack must contain at least three elements [`<sig>`, `<m=
sg>`, `<pubKey>`] in this order where `<pubKey>` is the top element and
>>> * `<pubKey>` must be a validly encoded public key
>>> * `<msg>` can be any string
>>> * `<sig>` must follow the strict DER encoding as described in [2] and=
the S-value of `<sig>` must be at most the curve order divided by 2 as des=
cribed in [3]
>>>
>>> If the stack is well formed, then OP_CHECKDATASIG pops the top three el=
ements [`<sig>`, `<msg>`, `<pubKey>`] from the stack and pushes true onto t=
he stack if `<sig>` is valid with respect to the raw single-SHA256 hash of =
`<msg>` and `<pubKey>` using the secp256k1 elliptic curve. Otherwise, it po=
ps three elements and pushes false onto the stack in the case that `<sig>` =
is the empty string and fails in all other cases.
>>>
>>> Nullfail is enforced the same as for OP_CHECKSIG [3]. If the signature =
does not match the supplied public key and message hash, and the signature =
is not an empty byte array, the entire script fails.
>>>
>>> ### Opcode Number
>>>
>>> OP_CHECKDATASIG uses the previously unused opcode number 186 (0xba in h=
ex encoding)
>>>
>>> ### SigOps
>>>
>>> Signature operations accounting for OP_CHECKDATASIG shall be calculated=
the same as OP_CHECKSIG. This means that each OP_CHECKDATASIG shall be cou=
nted as one (1) SigOp.
>>>
>>> ### Activation
>>>
>>> Use of OP_CHECKDATASIG, unless occuring in an unexecuted OP_IF branch, =
will make the transaction invalid if it is included in a block where the me=
dian timestamp of the prior 11 blocks is less than 1542300000.
>>>
>>> ### Unit Tests
>>>
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if 15 November 2018 pro=
tocol upgrade is not yet activated.
>>> - `<sig> <msg> OP_CHECKDATASIG` fails if there are fewer than 3 items =
on stack.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if `<pubKey>` is not a =
validly encoded public key.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if `<sig>` is not a val=
idly encoded signature with strict DER encoding.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if signature `<sig>` is=
not empty and does not pass the Low S check.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if signature `<sig>` is=
not empty and does not pass signature validation of `<msg>` and `<pubKey>`=
.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` pops three elements and pushe=
s false onto the stack if `<sig>` is an empty byte array.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIG` pops three elements and pushe=
s true onto the stack if `<sig>` is a valid signature of `<msg>` with respe=
ct to `<pubKey>`.
>>>
>>> OP_CHECKDATASIGVERIFY Specification
>>> -----------------------------------
>>>
>>> ### Semantics
>>>
>>> OP_CHECKDATASIGVERIFY is equivalent to OP_CHECKDATASIG followed by OP_V=
ERIFY. It leaves nothing on the stack, and will cause the script to fail im=
mediately if the signature check does not pass.
>>>
>>> ### Opcode Number
>>>
>>> OP_CHECKDATASIGVERIFY uses the previously unused opcode number 187 (0xb=
b in hex encoding)
>>>
>>> ### SigOps
>>>
>>> Signature operations accounting for OP_CHECKDATASIGVERIFY shall be calc=
ulated the same as OP_CHECKSIGVERIFY. This means that each OP_CHECKDATASIGV=
ERIFY shall be counted as one (1) SigOp.
>>>
>>> ### Activation
>>>
>>> Use of OP_CHECKDATASIGVERIFY, unless occuring in an unexecuted OP_IF br=
anch, will make the transaction invalid if it is included in a block where =
the median timestamp of the prior 11 blocks is less than 1542300000.
>>>
>>> ### Unit Tests
>>>
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if 15 November 20=
18 protocol upgrade is not yet activated.
>>> - `<sig> <msg> OP_CHECKDATASIGVERIFY` fails if there are fewer than 3 =
item on stack.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY`fails if `<pubKey>` is n=
ot a validly encoded public key.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if `<sig>` is not=
a validly encoded signature with strict DER encoding.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if signature `<si=
g>` is not empty and does not pass the Low S check.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if `<sig>` is not=
a valid signature of `<msg>` with respect to `<pubKey>`.
>>> - `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` pops the top three stac=
k elements if `<sig>` is a valid signature of `<msg>` with respect to `<pub=
Key>`.
>>>
>>> Sample Implementation [4, 5]
>>> ----------------------------
>>>
>>> ```c++
>>> case OP_CHECKDATASIG:
>>> case OP_CHECKDATASIGVERIFY: {
>>> // Make sure this remains an error before activ=
ation.
>>> if ((flags & SCRIPT_ENABLE_CHECKDATASIG) =3D=3D=
0) {
>>> return set_error(serror, SCRIPT_ERR_BAD_OPC=
ODE);
>>> }
>>>
>>> // (sig message pubkey -- bool)
>>> if (stack.size() < 3) {
>>> return set_error(
>>> serror, SCRIPT_ERR_INVALID_STACK_OPERAT=
ION);
>>> }
>>>
>>> valtype &vchSig =3D stacktop(-3);
>>> valtype &vchMessage =3D stacktop(-2);
>>> valtype &vchPubKey =3D stacktop(-1);
>>>
>>> if (!CheckDataSignatureEncoding(vchSig, flags,
>>> serror) ||
>>> !CheckPubKeyEncoding(vchPubKey, flags, serr=
or)) {
>>> // serror is set
>>> return false;
>>> }
>>>
>>> bool fSuccess =3D false;
>>> if (vchSig.size()) {
>>> valtype vchHash(32);
>>> CSHA256()
>>> .Write(vchMessage.data(), vchMessage.si=
ze())
>>> .Finalize(vchHash.data());
>>> uint256 message(vchHash);
>>> CPubKey pubkey(vchPubKey);
>>> fSuccess =3D pubkey.Verify(message, vchSig)=
;
>>> }
>>>
>>> if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAI=
L) &&
>>> vchSig.size()) {
>>> return set_error(serror, SCRIPT_ERR_SIG_NUL=
LFAIL);
>>> }
>>>
>>> popstack(stack);
>>> popstack(stack);
>>> popstack(stack);
>>> stack.push_back(fSuccess ? vchTrue : vchFalse);
>>> if (opcode =3D=3D OP_CHECKDATASIGVERIFY) {
>>> if (fSuccess) {
>>> popstack(stack);
>>> } else {
>>> return set_error(serror,
>>> SCRIPT_ERR_CHECKDATASI=
GVERIFY);
>>> }
>>> }
>>> } break;
>>> ```
>>>
>>> Sample Usage
>>> ------------
>>>
>>> The following example shows a spend and redeem script for a basic use o=
f CHECKDATASIG. This example validates the signature of some data, provide=
s a placeholder where you would then process that data, and finally allows =
one of 2 signatures to spend based on the outcome of the data processing.
>>>
>>> ### spend script:
>>> ```
>>> push txsignature
>>> push txpubkey
>>> push msg
>>> push sig
>>> ```
>>> ### redeem script:
>>> ```
>>> (txsig, txpubkey msg, sig)
>>> OP_OVER (txsig, txpubkey, msg, sig, msg)
>>> push data pubkey (txsig, txpubkey, msg, sig, msg, pubkey=
)
>>> OP_CHECKDATASIGVERIFY (txsig, txpubkey, msg)
>>> ```
>>> Now that msg is on the stack top, the script can write predicates on it=
,
>>> resulting in the message being consumed and a true/false condition left=
on the stack: (txpubkey, txsig, boolean)
>>> ```
>>> OP_IF (txsig, txpubkey)
>>> OP_DUP (txsig, txpubkey, txpubkey)
>>> OP_HASH160 (txsig, txpubkey, address)
>>> push <p2pkh spend address> (txsig, txpubkey, address, p2pkh spend =
address)
>>> OP_EQUALVERIFY (txsig, txpubkey)
>>> OP_CHECKSIG
>>> OP_ELSE
>>> (same as if clause but a different <p2pkh spend address>)
>>> OP_ENDIF
>>> ```
>>>
>>> History
>>> -------
>>>
>>> This specification is based on Andrew Stone=E2=80=99s OP_DATASIGVERIFY =
proposal [6, 7]. It is modified from Stone's original proposal based on a s=
ynthesis of all the peer-review and feedback received [8].
>>>
>>> References
>>> ----------
>>>
>>> [1] [OP_CHECKSIG](https://en.bitcoin.it/wiki/OP_CHECKSIG)
>>>
>>> [2] [Strict DER Encoding](https://github.com/bitcoin/bips/blob/master/b=
ip-0066.mediawiki)
>>>
>>> [3] [Low-S and Nullfail Specification](https://github.com/bitcoin/bips/=
blob/master/bip-0146.mediawiki)
>>>
>>> [4] [Bitcoin ABC implementation](https://reviews.bitcoinabc.org/D1621)
>>>
>>> [5] [Bitcoin ABC implementation update](https://reviews.bitcoinabc.org/=
D1646)
>>>
>>> [6] [Andrew Stone=E2=80=99s OP_DATASIGVERIFY](https://github.com/Bitcoi=
nUnlimited/BitcoinUnlimited/blob/bucash1.3.0.0/doc/opdatasigverify.md)
>>>
>>> [7] [Andrew Stone's article on Scripting](https://medium.com/@g.andrew.=
stone/bitcoin-scripting-applications-decision-based-spending-8e7b93d7bdb9)
>>>
>>> [8] [Peer Review of Andrew Stone's Proposal](https://github.com/bitcoin=
cashorg/bitcoincash.org/pull/10)
>>>
>>>
>>> --
>>> @JeremyRubin <https://twitter.com/JeremyRubin>
>>> <https://twitter.com/JeremyRubin>
>>> _______________________________________________
>>> bitcoin-dev mailing list
>>> bitcoin-dev@lists.linuxfoundation.org
>>> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
>>>
>>
--000000000000120b8405c63db347
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>There is one line written at <a href=3D"https://githu=
b.com/ElementsProject/elements/pull/949/files#r660130155">https://github.co=
m/ElementsProject/elements/pull/949/files#r660130155</a>. I suppose we need=
to decide on which variants of *VERIFY and *ADD we want to include (presum=
ably all of them) and choose which opcodes they will be assigned to.=C2=A0 =
And I guess for CHECKSIGFROMSTACKADD will want to place the n value between=
the signature and the message on the stack.=C2=A0 ... So I suppose we will=
need more than one sentence.<br></div><div><br></div><div>The semantics wo=
uld be basically to call <span class=3D"gmail-pl-smi">secp256k1_schnorrsig_=
verify <</span><a href=3D"https://github.com/bitcoin-core/secp256k1/blob=
/0440945fb5ce69d335fed32827b5166e84b02e05/include/secp256k1_schnorrsig.h#L1=
58">https://github.com/bitcoin-core/secp256k1/blob/0440945fb5ce69d335fed328=
27b5166e84b02e05/include/secp256k1_schnorrsig.h#L158</a>>, treating pubk=
eys and signatures the same way the other CHECKSIG operations do, and in pa=
ssing the (variable length) message from the stack.=C2=A0 CHECKSIGFROMSTACK=
would also be subject to the same sigops budget that CHECKSIG has in tapsc=
ript.<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gma=
il_attr">On Sat, Jul 3, 2021 at 2:30 PM Jeremy <<a href=3D"mailto:jlrubi=
n@mit.edu">jlrubin@mit.edu</a>> wrote:<br></div><blockquote class=3D"gma=
il_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,2=
04,204);padding-left:1ex"><div dir=3D"auto"><div>Awesome to hear that!<div =
dir=3D"auto"><br></div><div dir=3D"auto">Actually I don't think I did k=
now (or I forgot/didn't catch it) that there was an updated spec for el=
ements, I searched around for what I could find and came up empty handed. D=
o you have any links for that? That sounds perfect=C2=A0to me.</div><br><br=
><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, J=
ul 3, 2021, 10:50 AM Russell O'Connor <<a href=3D"mailto:roconnor@bl=
ockstream.com" target=3D"_blank">roconnor@blockstream.com</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"><div dir=3D"ltr"><=
div>Hi Jermy,</div><div><br></div><div>As you are aware, we, and by we I me=
an mostly Sanket, are developing an updated OP_CHECKSIGFROMSTACK implementa=
tion for tapscript on elements.=C2=A0 The plan here would be to effectively=
support the an interface to the variable-length extension of BIP-0340 schn=
orr signatures.</div><div><br></div><div>BIP-0340 would dispense with DER e=
ncoding (good riddance).</div><div>BIP-0340 signatures are batch verifiable=
along with other BIP-0340 transaction signatures and taproot tweak verific=
ation.</div><div>Support for variable length messages in BIP-0340 has been =
discussed in <<a href=3D"https://github.com/sipa/bips/issues/207" rel=3D=
"noreferrer" target=3D"_blank">https://github.com/sipa/bips/issues/207</a>&=
gt; and an implementation has recently been merged in <<a href=3D"https:=
//github.com/bitcoin-core/secp256k1/pull/844" rel=3D"noreferrer" target=3D"=
_blank">https://github.com/bitcoin-core/secp256k1/pull/844</a>>.=C2=A0 T=
he BIP has not yet been updated but the difference is that the message m do=
es not have to be 32-bytes (it is recommended that the message be a 32-bit =
tagged hash or a message with a 64-bit application specific prefix). The CH=
ECKSIGFROMSTACK operation (in tapscript) would use a stack item for this m =
value to BIP-0340 signature verification and would not necessarily have to =
be 32 bytes.<br></div><div><br></div><div>I think this design we are aiming=
for would be perfectly suited for Bitcoin as well.<br></div></div><br><div=
class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Jul 3,=
2021 at 12:32 PM Jeremy via bitcoin-dev <<a href=3D"mailto:bitcoin-dev@=
lists.linuxfoundation.org" rel=3D"noreferrer" target=3D"_blank">bitcoin-dev=
@lists.linuxfoundation.org</a>> wrote:<br></div><blockquote class=3D"gma=
il_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,2=
04,204);padding-left:1ex"><div dir=3D"ltr"><div>Reproduced below is the BIP=
text from Bitcoin Cash's (MIT-Licensed) specification for "CheckD=
ataSig", more or less the same thing as CHECKSIGFROMSTACK <a href=3D"h=
ttps://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/op_checkd=
atasig.md" rel=3D"noreferrer" target=3D"_blank">https://github.com/bitcoinc=
ashorg/bitcoincash.org/blob/master/spec/op_checkdatasig.md</a>. In contrast=
to Element's implementation, it does not have Element's bugs aroun=
d verify semantics and uses the nullfail rule, and there is a specification=
document so it seemed like the easiest starting point for discussion v.s. =
drafting something from scratch.</div><div><br>Does anyone have any issue w=
ith adapting this exact text and implementation to a BIP for Bitcoin using =
2 OP_SUCCESSX opcodes?</div><div><br></div><div>Note that with *just* Check=
SigFromStack, while you can do some very valuable use cases, but without OP=
_CAT it does not enable sophisticated covenants (and as per=C2=A0<a href=3D=
"https://www.wpsoftware.net/andrew/blog/cat-and-schnorr-tricks-i.html" rel=
=3D"noreferrer" target=3D"_blank">https://www.wpsoftware.net/andrew/blog/ca=
t-and-schnorr-tricks-i.html</a> just CAT alone enables such uses).<br><br>D=
esign questions worth considering as modifications:</div><div><br>1. Should=
CSFS require some sort of tagged hash? Very likely answer is no =E2=80=93 =
tags interfere with certain use cases<br>2. Should CSFS split the signature=
=E2=80=99s R & S value stack items for some applications that otherwise=
may require OP_CAT? E.g. using a pinned R value allows you to extract a pr=
ivate key if ever double signed, using 2 R values allows pay-to-reveal-key =
contracts. Most likely answer is no, if that is desired then OP_CAT can be =
introduced<br>3. Should CSFS support a cheap way to reference the taproot i=
nternal or external key? Perhaps, can be handled with undefined upgradeable=
keytypes. One might want to use the internal key, if the signed data shoul=
d be valid independent of the tapscript tree. One might want to use the ext=
ernal key, if the data should only be valid for a single tapscript key + tr=
ee.<br>4. Should invalid public keys types be a NOP to support future exten=
ded pubkey types?</div><div><br></div><div><br><br></div><div>Best,<br><br>=
<br>Jeremy<pre style=3D"color:rgb(0,0,0);font-family:arial,helvetica,sans-s=
erif;font-size:small;white-space:pre-wrap"><br></pre><pre style=3D"color:rg=
b(0,0,0);font-family:arial,helvetica,sans-serif;font-size:small;white-space=
:pre-wrap">---
layout: specification
title: OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY Specification
category: spec
date: 2018-08-20
activation: 1542300000
version: 0.6
---
OP_CHECKDATASIG
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY check whether a signature is vali=
d with respect to a message and a public key.
OP_CHECKDATASIG permits data to be imported into a script, and have its val=
idity checked against some signing authority such as an "Oracle".
OP_CHECKDATASIG and OP_CHECKDATASIGVERIFY are designed to be implemented si=
milarly to OP_CHECKSIG [1]. Conceptually, one could imagine OP_CHECKSIG fun=
ctionality being replaced by OP_CHECKDATASIG, along with a separate Op Code=
to create a hash from the transaction based on the SigHash algorithm.
OP_CHECKDATASIG Specification
-----------------------------
### Semantics
OP_CHECKDATASIG fails immediately if the stack is not well formed. To be we=
ll formed, the stack must contain at least three elements [`<sig>`, `=
<msg>`, `<pubKey>`] in this order where `<pubKey>` is the=
top element and
* `<pubKey>` must be a validly encoded public key
* `<msg>` can be any string
* `<sig>` must follow the strict DER encoding as described in [2] a=
nd the S-value of `<sig>` must be at most the curve order divided by =
2 as described in [3]
If the stack is well formed, then OP_CHECKDATASIG pops the top three elemen=
ts [`<sig>`, `<msg>`, `<pubKey>`] from the stack and push=
es true onto the stack if `<sig>` is valid with respect to the raw si=
ngle-SHA256 hash of `<msg>` and `<pubKey>` using the secp256k1 =
elliptic curve. Otherwise, it pops three elements and pushes false onto the=
stack in the case that `<sig>` is the empty string and fails in all =
other cases.
Nullfail is enforced the same as for OP_CHECKSIG [3]. If the signature does=
not match the supplied public key and message hash, and the signature is n=
ot an empty byte array, the entire script fails.
### Opcode Number
OP_CHECKDATASIG uses the previously unused opcode number 186 (0xba in hex e=
ncoding)
### SigOps
Signature operations accounting for OP_CHECKDATASIG shall be calculated the=
same as OP_CHECKSIG. This means that each OP_CHECKDATASIG shall be counted=
as one (1) SigOp.
### Activation
Use of OP_CHECKDATASIG, unless occuring in an unexecuted OP_IF branch, will=
make the transaction invalid if it is included in a block where the median=
timestamp of the prior 11 blocks is less than 1542300000.
### Unit Tests
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if 15 Nov=
ember 2018 protocol upgrade is not yet activated.
- `<sig> <msg> OP_CHECKDATASIG` fails if there are fewer than =
3 items on stack.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if `<p=
ubKey>` is not a validly encoded public key.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if `<s=
ig>` is not a validly encoded signature with strict DER encoding.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if signat=
ure `<sig>` is not empty and does not pass the Low S check.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` fails if signat=
ure `<sig>` is not empty and does not pass signature validation of `&=
lt;msg>` and `<pubKey>`.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` pops three elem=
ents and pushes false onto the stack if `<sig>` is an empty byte arra=
y.
- `<sig> <msg> <pubKey> OP_CHECKDATASIG` pops three elem=
ents and pushes true onto the stack if `<sig>` is a valid signature o=
f `<msg>` with respect to `<pubKey>`.
OP_CHECKDATASIGVERIFY Specification
-----------------------------------
### Semantics
OP_CHECKDATASIGVERIFY is equivalent to OP_CHECKDATASIG followed by OP_VERIF=
Y. It leaves nothing on the stack, and will cause the script to fail immedi=
ately if the signature check does not pass.
### Opcode Number
OP_CHECKDATASIGVERIFY uses the previously unused opcode number 187 (0xbb in=
hex encoding)
### SigOps
Signature operations accounting for OP_CHECKDATASIGVERIFY shall be calculat=
ed the same as OP_CHECKSIGVERIFY. This means that each OP_CHECKDATASIGVERIF=
Y shall be counted as one (1) SigOp.
### Activation
Use of OP_CHECKDATASIGVERIFY, unless occuring in an unexecuted OP_IF branch=
, will make the transaction invalid if it is included in a block where the =
median timestamp of the prior 11 blocks is less than 1542300000.
### Unit Tests
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if =
15 November 2018 protocol upgrade is not yet activated.
- `<sig> <msg> OP_CHECKDATASIGVERIFY` fails if there are fewer=
than 3 item on stack.
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY`fails if `=
<pubKey>` is not a validly encoded public key.
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if =
`<sig>` is not a validly encoded signature with strict DER encoding.
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if =
signature `<sig>` is not empty and does not pass the Low S check.
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` fails if =
`<sig>` is not a valid signature of `<msg>` with respect to `&l=
t;pubKey>`.
- `<sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY` pops the =
top three stack elements if `<sig>` is a valid signature of `<msg&=
gt;` with respect to `<pubKey>`.
Sample Implementation [4, 5]
----------------------------
```c++
case OP_CHECKDATASIG:
case OP_CHECKDATASIGVERIFY: {
// Make sure this remains an error before activatio=
n.
if ((flags & SCRIPT_ENABLE_CHECKDATASIG) =3D=3D=
0) {
return set_error(serror, SCRIPT_ERR_BAD_OPCODE)=
;
}
// (sig message pubkey -- bool)
if (stack.size() < 3) {
return set_error(
serror, SCRIPT_ERR_INVALID_STACK_OPERATION)=
;
}
valtype &vchSig =3D stacktop(-3);
valtype &vchMessage =3D stacktop(-2);
valtype &vchPubKey =3D stacktop(-1);
if (!CheckDataSignatureEncoding(vchSig, flags,
serror) ||
!CheckPubKeyEncoding(vchPubKey, flags, serror))=
{
// serror is set
return false;
}
bool fSuccess =3D false;
if (vchSig.size()) {
valtype vchHash(32);
CSHA256()
.Write(vchMessage.data(), vchMessage.size()=
)
.Finalize(vchHash.data());
uint256 message(vchHash);
CPubKey pubkey(vchPubKey);
fSuccess =3D pubkey.Verify(message, vchSig);
}
if (!fSuccess && (flags & SCRIPT_VERIFY=
_NULLFAIL) &&
vchSig.size()) {
return set_error(serror, SCRIPT_ERR_SIG_NULLFAI=
L);
}
popstack(stack);
popstack(stack);
popstack(stack);
stack.push_back(fSuccess ? vchTrue : vchFalse);
if (opcode =3D=3D OP_CHECKDATASIGVERIFY) {
if (fSuccess) {
popstack(stack);
} else {
return set_error(serror,
SCRIPT_ERR_CHECKDATASIGVER=
IFY);
}
}
} break;
```
Sample Usage
------------
The following example shows a spend and redeem script for a basic use of CH=
ECKDATASIG. This example validates the signature of some data, provides a =
placeholder where you would then process that data, and finally allows one =
of 2 signatures to spend based on the outcome of the data processing.
### spend script:
```
push txsignature
push txpubkey
push msg
push sig
```
### redeem script:
```
(txsig, txpubkey msg, sig)
OP_OVER (txsig, txpubkey, msg, sig, msg)
push data pubkey (txsig, txpubkey, msg, sig, msg, pubkey)
OP_CHECKDATASIGVERIFY (txsig, txpubkey, msg)
```
Now that msg is on the stack top, the script can write predicates on it,
resulting in the message being consumed and a true/false condition left on =
the stack: (txpubkey, txsig, boolean)
```
OP_IF (txsig, txpubkey)
OP_DUP (txsig, txpubkey, txpubkey)
OP_HASH160 (txsig, txpubkey, address)
push <p2pkh spend address> (txsig, txpubkey, address, p2pkh spen=
d address)
OP_EQUALVERIFY (txsig, txpubkey)
OP_CHECKSIG
OP_ELSE
(same as if clause but a different <p2pkh spend address>)
OP_ENDIF
```
History
-------
This specification is based on Andrew Stone=E2=80=99s OP_DATASIGVERIFY prop=
osal [6, 7]. It is modified from Stone's original proposal based on a s=
ynthesis of all the peer-review and feedback received [8].
References
----------
[1] [OP_CHECKSIG](<a href=3D"https://en.bitcoin.it/wiki/OP_CHECKSIG" rel=3D=
"noreferrer" target=3D"_blank">https://en.bitcoin.it/wiki/OP_CHECKSIG</a>)
[2] [Strict DER Encoding](<a href=3D"https://github.com/bitcoin/bips/blob/m=
aster/bip-0066.mediawiki" rel=3D"noreferrer" target=3D"_blank">https://gith=
ub.com/bitcoin/bips/blob/master/bip-0066.mediawiki</a>)
[3] [Low-S and Nullfail Specification](<a href=3D"https://github.com/bitcoi=
n/bips/blob/master/bip-0146.mediawiki" rel=3D"noreferrer" target=3D"_blank"=
>https://github.com/bitcoin/bips/blob/master/bip-0146.mediawiki</a>)
[4] [Bitcoin ABC implementation](<a href=3D"https://reviews.bitcoinabc.org/=
D1621" rel=3D"noreferrer" target=3D"_blank">https://reviews.bitcoinabc.org/=
D1621</a>)
[5] [Bitcoin ABC implementation update](<a href=3D"https://reviews.bitcoina=
bc.org/D1646" rel=3D"noreferrer" target=3D"_blank">https://reviews.bitcoina=
bc.org/D1646</a>)
[6] [Andrew Stone=E2=80=99s OP_DATASIGVERIFY](<a href=3D"https://github.com=
/BitcoinUnlimited/BitcoinUnlimited/blob/bucash1.3.0.0/doc/opdatasigverify.m=
d" rel=3D"noreferrer" target=3D"_blank">https://github.com/BitcoinUnlimited=
/BitcoinUnlimited/blob/bucash1.3.0.0/doc/opdatasigverify.md</a>)
[7] [Andrew Stone's article on Scripting](<a href=3D"https://medium.com=
/@g.andrew.stone/bitcoin-scripting-applications-decision-based-spending-8e7=
b93d7bdb9" rel=3D"noreferrer" target=3D"_blank">https://medium.com/@g.andre=
w.stone/bitcoin-scripting-applications-decision-based-spending-8e7b93d7bdb9=
</a>)
[8] [Peer Review of Andrew Stone's Proposal](<a href=3D"https://github.=
com/bitcoincashorg/bitcoincash.org/pull/10" rel=3D"noreferrer" target=3D"_b=
lank">https://github.com/bitcoincashorg/bitcoincash.org/pull/10</a>)</pre><=
/div><br clear=3D"all"><div><div dir=3D"ltr"><div dir=3D"ltr">--<br><a href=
=3D"https://twitter.com/JeremyRubin" rel=3D"noreferrer" target=3D"_blank">@=
JeremyRubin</a><a href=3D"https://twitter.com/JeremyRubin" rel=3D"noreferre=
r" target=3D"_blank"></a></div></div></div></div>
_______________________________________________<br>
bitcoin-dev mailing list<br>
<a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" rel=3D"noreferrer"=
target=3D"_blank">bitcoin-dev@lists.linuxfoundation.org</a><br>
<a href=3D"https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev" =
rel=3D"noreferrer noreferrer" target=3D"_blank">https://lists.linuxfoundati=
on.org/mailman/listinfo/bitcoin-dev</a><br>
</blockquote></div>
</blockquote></div></div></div>
</blockquote></div></div>
--000000000000120b8405c63db347--
|