-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1350 lines (1339 loc) · 48 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Taproot</title>
<link rel="stylesheet" href="dist/custom.css">
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/solarized.css">
<link rel="stylesheet" href="lib/css/zenburn.css">
<script src="plugin/highlight/highlight.js"></script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
"HTML-CSS": {
scale: 150
}
});
</script>
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/zenburn.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section>
<h1>Learn Taproot</h1>
<h3>https://github.com/jimmysong/learntaproot</h3>
Follow the instructions in README.md
</section>
<section>
<h2>Session Objectives</h2>
<ul>
<li class="fragment"><strong>Learn Schnorr Signatures</strong>
<li class="fragment"><strong>Learn Taproot Key Path Spend</strong>
<li class="fragment"><strong>Learn Taproot Script Path Spend</strong>
</ul>
</section>
</section>
<section>
<section>
<h2>Schnorr Signatures</h2>
</section>
<section>
<h2>Motivation</h2>
<ul>
<li class="fragment">Conceptually Simpler
<li class="fragment">ECDSA uses DER, which is 72-73 bytes, Schnorr uses 64
<li class="fragment">Fewer Elliptic Curve Operations (hash instead)
<li class="fragment">Key Aggregation/Signature Aggregation/Batch Verification
</ul>
</section>
<section>
<h2>ECDSA Signing</h2>
<ul>
<li class="fragment">$eG=P$, $z$ is hash of what's being signed, choose a random $k$
<li class="fragment">Compute $kG=R=(x,y)$, let $r=x$
<li class="fragment">Compute $s=\frac{z+re}{k}$
<li class="fragment">Signature is the pair $(r,s)$
</ul>
</section>
<section>
<h2>ECDSA Verification</h2>
<ul>
<li class="fragment">$eG=P$, $z$ is hash of what's being signed, choose a random $k$
<li class="fragment">Signature is $(r,s)$ where $s=\frac{z+re}{k}$
<li class="fragment">Compute $u=\frac{z}{s}, v=\frac{r}{s}$
$$uG+vP=\frac{z}{s}G+\frac{r}{s}P=\frac{z}{s}G+\frac{re}{s}G \\ =\frac{z+re}{s}G =\frac{(z+re)k}{z+re}G \\ =kG=R=(r,y)$$
</ul>
</section>
<section>
<h2>ECDSA</h2>
<ul>
<li class="fragment">$u$ is used to commit to $z$, or the tx being attested to
<li class="fragment">$v$ is used to commit to $r$, or the target/challenge that we're trying to hit/respond to
<li class="fragment">Kludgy, uses field division, which is expensive computationally
<li class="fragment">Developed after Schnorr and used in Bitcoin due to Patent issues (expired 2008)
</ul>
</section>
<section>
<h2>Schnorr</h2>
<ul>
<li class="fragment">Uses a hash function instead of field division
<li class="fragment">That hash can commit to everything at once, instead of just one thing.
<li class="fragment">$H(a||b||c||...)$
<li class="fragment">Target is a point on the curve $R$, not just the $x$ coordinate
<li class="fragment">Aggregation of keys and signatures now possible!
<li class="fragment">Batch verification possible!
<li class="fragment">BIP340
</ul>
</section>
<section>
<h2>Tagged Hashes</h2>
<ul>
<li class="fragment">Each hash is different so that hashes cannot feasibly collide
<li class="fragment">There are 10 different contexts, each creating its own set of hashes
<li class="fragment">The hash is SHA256, but with 64 bytes before the actual bytes being hashed
<li class="fragment">The 64 bytes are another SHA256 of the tag (e.g. "BIP0340/aux") repeated twice
<li class="fragment">H_aux(x) = SHA256(SHA256("BIP0340/aux") + SHA256("BIP0340/aux") + x)
</ul>
</section>
<section>
<h2>Tagged Hashes</h2>
<pre><code data-trim data-noescape class="python">
# Example Tagged Hashes
from hash import sha256
challenge_tag = b"BIP0340/challenge"
msg = b"some message"
challenge_hash = sha256(challenge_tag)
hash_challenge = sha256(challenge_hash + challenge_hash + msg)
print(hash_challenge.hex())
</code></pre>
</section>
<section>
<h2><b>Exercises</b></h2>
<ol start="1">
<li>What is the tagged hash "BIP0340/aux" of "hello world"?
<li>Make this test pass: <code>hash:HashTest:test_tagged_hash</code>
</ol>
</section>
<section>
<h2>$x$-only keys</h2>
<ul>
<li class="fragment">Assume $y$ is even
<li class="fragment">Serialized as 32-bytes
<li class="fragment">The private key $e$ is flipped to $N-e$ if $y$ is odd
<li class="fragment">$eG=P=(x,y)$ means $(N-e)G=0-eG=-P=(x,-y)$
<li class="fragment">Lots of flipping!
</ul>
</section>
<section>
<h2>$x$-only Keys</h2>
<pre><code data-trim data-noescape class="python">
# Example X-only pubkey
from ecc import PrivateKey, S256Point
from helper import int_to_big_endian
pubkey = PrivateKey(12345).point
xonly = int_to_big_endian(pubkey.x.num, 32)
print(xonly.hex())
pubkey2 = S256Point.parse(xonly)
print(pubkey.xonly() == pubkey2.xonly())
</code></pre>
</section>
<section>
<h2><b>Exercises</b></h2>
<ol start="3">
<li>Find the $x$-only pubkey format for the private key with the secret 21,000,000
<li>Make this test pass: <code>ecc:XOnlyTest:test_xonly</code>
</ul>
</section>
<section>
<h2>Schnorr Signature</h2>
<ul>
<li class="fragment">$(R,s)$, where $R$ is the pubkey of the target and $s$ is 32 bytes
<li class="fragment">Serialization is $R$ as $x$-only followed by $s$ in big endian
<li class="fragment">$k$-generation has a separate, secure process
<li class="fragment">Uses tagged hashes throughout
</ul>
</section>
<section>
<h2>ECDSA Verification</h2>
<ul>
<li class="fragment">$eG=P$, $z$ message, $kG=(r,y)$
<li class="fragment">Signature is $(r,s)$ where $s=\frac{z+re}{k}$
<li class="fragment">Compute $u=\frac{z}{s}, v=\frac{r}{s}$
$$uG+vP=\frac{z}{s}G+\frac{r}{s}P=\frac{z}{s}G+\frac{re}{s}G \\ =\frac{z+re}{s}G =\frac{(z+re)k}{z+re}G \\ =kG=R=(r,y)$$
</ul>
</section>
<section>
<h2>Schnorr Verification</h2>
<ul>
<li class="fragment">$eG=P$, $m$ message, $kG=R$, $H$ is a hash function
<li class="fragment">Signature is $(R,s)$ where $s=k + e H(R||P||m)$
$$-H(R||P||m)P+sG \\ =-H(R||P||m)P+(k+e H(R||P||m))G \\ =-H(R||P||m)P+kG+H(R||P||m)(eG) \\ =R+H(R||P||m)P-H(R||P||m)P=R$$
</ul>
</section>
<section>
<h2>Example Schnorr Verification</h2>
<pre><code data-trim data-noescape class="python">
from ecc import S256Point, SchnorrSignature, G, N
from hash import sha256, hash_challenge
from helper import big_endian_to_int
msg = sha256(b"I attest to understanding Schnorr Signatures")
sig_raw = bytes.fromhex("f3626c99fe36167e5fef6b95e5ed6e5687caa4dc828986a7de8f9423c0f77f9bc73091ed86085ce43de0e255b3d0afafc7eee41ddc9970c3dc8472acfcdfd39a")
sig = SchnorrSignature.parse(sig_raw)
xonly = bytes.fromhex("f01d6b9018ab421dd410404cb869072065522bf85734008f105cf385a023a80f")
point = S256Point.parse(xonly)
commitment = sig.r.xonly() + point.xonly() + msg
challenge = big_endian_to_int(hash_challenge(commitment)) % N
target = -challenge * point + sig.s * G
print(target == sig.r)
</code></pre>
</section>
<section>
<h2><b>Exercises</b></h2>
<ol start="5">
<li>Verify a Schnorr Signature
<li>Make this test pass: <code>ecc:SchnorrTest:test_verify</code>
</ol>
</section>
<section>
<h2>ECDSA Signing</h2>
<ul>
<li class="fragment">$eG=P$, $z$ message, $k$ random
<li class="fragment">$kG=R=(x,y)$, let $r=x$
<li class="fragment">$s=\frac{z+re}{k}$
<li class="fragment">Signature is $(r,s)$
</ul>
</section>
<section>
<h2>Schnorr Signing</h2>
<ul>
<li class="fragment">$eG=P$, $m$ message, $k$ random
<li class="fragment">$kG=R$, $H$ is <code>hash_challenge</code>
<li class="fragment">$s=k+e H(R||P||m)$ where $R$ and $P$ are $x$-only
<li class="fragment">Signature is $(R,s)$
</ul>
</section>
<section>
<h2>Schnorr Signing</h2>
<pre><code data-trim data-noescape class="python">
# Example Signing
from ecc import PrivateKey, N, G
from hash import hash_challenge, sha256
from helper import big_endian_to_int
priv = PrivateKey(12345)
if priv.point.y.num % 2 == 1:
d = N - priv.secret
else:
d = priv.secret
msg = sha256(b"I attest to understanding Schnorr Signatures")
k = 21016020145315867006318399104346325815084469783631925097217883979013588851039
r = k * G
if r.y.num % 2 == 1:
k = N - k
r = k * G
commitment = r.xonly() + priv.point.xonly() + msg
e = big_endian_to_int(hash_challenge(commitment)) % N
s = (k + e * d) % N
sig = SchnorrSignature(r, s)
if not priv.point.verify_schnorr(msg, sig):
raise RuntimeError("Bad Signature")
print(sig.serialize().hex())
</code></pre>
</section>
<section>
<h2><b>Exercises</b></h2>
<ol start="7">
<li>Sign the message "I'm learning Taproot!" with the private key 21,000,000
<li>Make this test pass: <code>ecc:SchnorrTest:test_sign</code>
</ol>
</section>
<section>
<h2>$k$ Generation</h2>
<ul>
<li class="fragment">Revealing $k$ reveals the private key
<li class="fragment">Bad random number generator for $k$ will reveal the private key
<li class="fragment">BIP340 starts with a random number $a$ called the auxillary
<li class="fragment">Then xor $a$ with the secret to make it impossible to guess
<li class="fragment">Then we hash with the message to generate the $k$
<li class="fragment">This makes $k$ unique to both the secret and the message
<li class="fragment">32 0-bytes $a$ can be used to create a deterministic $k$
</ul>
</section>
<section>
<h2>Batch Verification</h2>
<ul>
<li class="fragment">$e_iG=P_i$, $m_i$ message, $H$
<li class="fragment">Signature is $(R_i,s_i)$, $h_i=H(R_i||P_i||m_i)$
<li class="fragment">$-h_1 P_1+s_1G=R_1$
<li class="fragment">$-h_2 P_2+s_2G=R_2$
<li class="fragment">$-h_1 P_1-h_2 P_1+(s_1+s_2)G=R_1+R_2$
<li class="fragment">$(s_1+s_2)G=R_1+R_2+h_1 P_1+h_2 P_2$
</ul>
</section>
<section>
<h2>Exercises</h2>
<ol start="9">
<li>Batch Verify two Schnorr Signatures
</ol>
</section>
</section>
<section>
<section>
<h2>Taproot Key Path Spend</h2>
</section>
<section>
<h2>Taproot Structure</h2>
<ul>
<li class="fragment">Defined in BIP341
<li class="fragment">Combines both single-key and arbitrary script type addresses
<li class="fragment">p2pkh and p2sh did those previously
<li class="fragment">p2wpkh and p2wsh did those in Segwit
</ul>
</section>
<section>
<h2>Taproot Structure</h2>
<div class="tree">
<ul>
<li>
<div class="blue">External PubKey $Q$</div>
<ul>
<li>
<div class="green"><strong style="color:red"> Internal PubKey $P$ </strong></div>
</li>
<li>
<div class="brown">Merkle Root</div>
<ul>
<li>
<div class="brown">TapBranch</div>
<ul>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</section>
<section>
<h2>Taproot Architecture</h2>
<ul>
<li class="fragment">KeyPath Spend (single-key like p2pkh and p2wpkh)
<li class="fragment">ScriptPath Spend (arbitrary script like p2sh and p2wsh)
<li class="fragment">ScriptPath is a Merkle Tree of TapScripts
<li class="fragment">TapScripts are like Script, but with slightly different OP codes (defined in BIP342)
</ul>
</section>
<section>
<h2>Taproot Implementation</h2>
<ul>
<li class="fragment">Segwit version 1
<li class="fragment">Requires <code>OP_1</code> and 32 bytes
<li class="fragment">The 32 bytes are an $x$-only public key $Q$ (external public key)
<li class="fragment">KeyPath spend's public key is $P$ (internal public key)
<li class="fragment">The Merkle Root of the ScriptPath Spend combined with $P$ generates the tweak ($t$)
<li class="fragment">That tweak is used to generate $Q=P+tG$
</ul>
</section>
<section>
<h2>Spending from the KeyPath</h2>
<ul>
<li class="fragment">$m$ is the Merkle Root $m$ of the ScriptPath
<li class="fragment">Tweak $t$ and $P$ create $Q$, the external pubkey
<li class="fragment">$t=H(P||m)$ where $H$ is <code>hash_taptweak</code>
<li class="fragment">$Q=P+tG$, and $eG=P$ which means $Q=eG+tG$ and $Q=(e+t)G$
<li class="fragment">$e+t$ is your private key, which can sign for the $Q$
<li class="fragment">Witness has a single element, the Schnorr Signature
<li class="fragment">If you don't want a script path, $m$ is the empty string
</ul>
</section>
<section>
<h2>Key Path UTXO Example</h2>
<pre><code data-trim data-noescape class="python">
# Example Q calculation for a single-key
from ecc import S256Point, G
from hash import hash_taptweak
from helper import big_endian_to_int
from script import P2TRScriptPubKey
internal_pubkey_raw = bytes.fromhex("cbaa648dbfe734646ce958e2f14a874149fae4010fdeabde4bae6a732537fd91")
internal_pubkey = S256Point.parse(internal_pubkey_raw)
tweak = big_endian_to_int(hash_taptweak(internal_pubkey_raw))
external_pubkey = internal_pubkey + tweak * G
script_pubkey = P2TRScriptPubKey(external_pubkey)
print(script_pubkey)
</code></pre>
</section>
<section>
<h2>Exercises</h2>
<ol start="10">
<li>Make a P2TR ScriptPubKey using the private key 9284736473
<li>Make this test pass: <code>ecc:TapRootTest:test_default_tweak</code>
<li>Make this test pass: <code>ecc:TapRootTest:test_tweaked_key</code>
<li>Make this test pass: <code>ecc:TapRootTest:test_p2tr_script</code>
</ol>
</section>
<section>
<h2>P2TR Addresses</h2>
<ul>
<li class="fragment">Segwit v0 uses Bech32
<li class="fragment">Taproot (Segwit v1) uses Bech32m
<li class="fragment">Bech32m is different than Bech32 (BIP350)
<li class="fragment">Has error correcting capability and uses 32 letters/numbers
<li class="fragment">Segwit v0 addresses start with <code>bc1q</code> and p2wpkh is shorter than p2wsh
<li class="fragment">Segwit v1 addresses start with <code>bc1p</code> and they're all one length
</ul>
</section>
<section>
<h2>P2TR Address Example</h2>
<pre><code data-trim data-noescape class="python">
# Example of getting a p2tr address
from ecc import S256Point
internal_pubkey_raw = bytes.fromhex("cbaa648dbfe734646ce958e2f14a874149fae4010fdeabde4bae6a732537fd91")
internal_pubkey = S256Point.parse(internal_pubkey_raw)
print(internal_pubkey.p2tr_address())
print(internal_pubkey.p2tr_address(network="signet"))
</pre></code>
</section>
<section>
<h2>Exercise 14: Make your own Signet P2TR Address</h2>
<p>Submit your address at <a href="">this link</a></p>
</section>
<section>
<h2>Spending P2TR KeyPath</h2>
<ul>
<li class="fragment">We need the tweak $t$ and the private key $e$ to sign the transaction
<li class="fragment">The external pubkey $Q$ is in the UTXO as an $x$-only key
<li class="fragment">We put the Schnorr Signature in the Witness field
<li class="fragment">We use the <code>sign_schnorr</code> method in the <code>PrivateKey</code> to do this.
</ul>
</section>
<section>
<h2>Spending Plan</h2>
<ul>
<li class="fragment">We have 20,000 sats in this output:
<ul>
<li class="fragment"><code>871864...66995:0</code>
</ul>
<li class="fragment">We want to spend all of it to:
<ul>
<li><code>tb1ptaqplrhn...quchufq</code>
</ul>
<li class="fragment">1 input/1 output transaction
</ul>
</section>
<section>
<h2>Spending Example</h2>
<pre><code data-trim data-noescape class="python">
# Spending from a p2tr
from ecc import PrivateKey, N
from hash import sha256
from helper import big_endian_to_int
from script import address_to_script_pubkey
from tx import Tx, TxIn, TxOut
my_email = b"[email protected]"
my_secret = big_endian_to_int(sha256(my_email))
priv = PrivateKey(my_secret)
prev_tx = bytes.fromhex("871864d7631024465fc210e553fa9f50e7f0f2359288ad121aa733d65e366995")
prev_index = 0
target_address = "tb1ptaqplrhnyh3kq85n7dtm5vcpgstt0ev80f4wd8ngeppch4fzu8mquchufq"
fee = 500
tx_in = TxIn(prev_tx, prev_index)
target_script_pubkey = address_to_script_pubkey(target_address)
target_amount = tx_in.value(network="signet") - fee
tx_out = TxOut(target_amount, target_script_pubkey)
tx_obj = Tx(1, [tx_in], [tx_out], network="signet", segwit=True)
tweaked_secret = (priv.secret + big_endian_to_int(priv.point.tweak())) % N
tweaked_key = PrivateKey(tweaked_secret)
tx_obj.sign_p2tr_keypath(0, tweaked_key)
print(tx_obj.serialize().hex())
</code></pre>
</section>
<section>
<h2>Exercises</h2>
<ol start="15">
<li> Make this test pass: <code>ecc:PrivateKeyTest:test_tweaked_key</code>
</ol>
</section>
<section>
<h2>Exercise 16: Spend from your P2TR Address</h2>
<p>You have been sent 100,000 sats to your address on Signet. Send 40,000 sats back to <code>tb1q7kn55vf3mmd40gyj46r245lw87dc6us5n50lrg</code>, the rest to yourself.</p>
<p>Use <a href="https://mempool.space/signet/tx/push" target="_mempool">Mempool Signet</a> to broadcast your transaction</p>
</section>
</section>
<section>
<section>
<h2>Taproot Script Path Spend</h2>
</section>
<section>
<h2>Taproot Structure</h2>
<div class="tree">
<ul>
<li>
<div class="blue">External PubKey $Q$</div>
<ul>
<li>
<div class="green">Internal PubKey $P$</div>
</li>
<li>
<div class="brown">Merkle Root</div>
<ul>
<li>
<div class="brown">TapBranch</div>
<ul>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown"><strong style="color:red"> TapScript </strong></div>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</section>
<section>
<h2>TapScript</h2>
<ul>
<li class="fragment">Defined in BIP342
<li class="fragment">Same as Script except for a few New/Changed OP Codes
<li class="fragment"><code>OP_CHECKSIG</code> and <code>OP_CHECKSIGVERIFY</code> use Schnorr Signatures
<li class="fragment"><code>OP_CHECKMULTISIG</code> and <code>OP_CHECKMULTISIGVERIFY</code> are disabled
<li class="fragment"><code>OP_CHECKSIGADD</code> is added to replace multisig
</ul>
</section>
<section>
<h2><code>OP_CHECKSIGADD</code></h2>
<ul>
<li class="fragment"> Consumes the top three elements: a pubkey, a number, and a signature.
<li class="fragment"> Valid sig, returns the number+1 to the stack
<li class="fragment"> Invalid sig, returns the number back to the stack
</ul>
<table class="medhex">
<tr class="fragment">
<td>
Valid
</td>
<td>
<span class="op brown">OP_CHECKSIGADD</span>
</td>
<td>
<div class="stack">
<div class="row">
<span class="elem blue">PubKey</span>
</div>
<div class="row">
<span class="elem red">n</span>
</div>
<div class="row">
<span class="elem brown">Signature</span>
</div>
</div>
</td>
<td>
$\Rightarrow$
</td>
<td>
<div class="stack">
<div class="row">
<span class="elem red">n+1</span>
</div>
</div>
</td>
</tr>
<tr class="fragment">
<td>
Invalid
</td>
<td>
<span class="op brown">OP_CHECKSIGADD</span>
</td>
<td>
<div class="stack">
<div class="row">
<span class="elem blue">PubKey</span>
</div>
<div class="row">
<span class="elem red">n</span>
</div>
<div class="row">
<span class="elem brown">Signature</span>
</div>
</div>
</td>
<td>
$\Rightarrow$
</td>
<td>
<div class="stack">
<div class="row">
<span class="elem red">n</span>
</div>
</div>
</td>
</tr>
</table>
</section>
<section>
<h3>Example Multisig using OP_CHECKSIGADD</h3>
<div class="container">
<div class="stack">
<div class="row">TapScript</div>
<span class="elem blue">PubKey A</span>
<span class="op brown">OP_CHECKSIG</span>
<span class="elem blue">PubKey B</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row">Witness</div>
<span class="elem brown">Signature for C</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for A</span>
</div>
<div class="stack">
<div class="row">Execution</div>
<span class="elem brown">Signature for C</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for A</span>
<span class="elem blue">PubKey A</span>
<span class="op brown">OP_CHECKSIG</span>
<span class="elem blue">PubKey B</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="elem brown">Signature for C</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for A</span>
<span class="elem blue">PubKey A</span>
<span class="op brown">OP_CHECKSIG</span>
<span class="elem blue">PubKey B</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
</div>
<div class="stack">
<div class="row">Stack </div>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="elem blue">PubKey B</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
<span class="op brown">OP_CHECKSIG</span>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem blue">PubKey A</span>
<span class="elem brown">Signature for A</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for C</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="elem blue">PubKey B</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem red">1</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for C</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
<span class="op brown">OP_CHECKSIGADD</span>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem blue">PubKey B</span>
<span class="elem red">1</span>
<span class="elem brown">''</span>
<span class="elem brown">Signature for C</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="elem blue">PubKey C</span>
<span class="op brown">OP_CHECKSIGADD</span>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem red">1</span>
<span class="elem brown">Signature for C</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
<span class="op brown">OP_CHECKSIGADD</span>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem blue">PubKey C</span>
<span class="elem red">1</span>
<span class="elem brown">Signature for C</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
<span class="op red">OP_2</span>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row"> Current OP</div>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem red">2</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
</div>
<div class="stack">
<div class="row"> Current OP</div>
<span class="op brown">OP_EQUAL</span>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem red">2</span>
<span class="elem red">2</span>
</div>
</div>
</section>
<section>
<div class="container">
<div class="stack">
<div class="row">Execution</div>
</div>
<div class="stack">
<div class="row"> Current OP</div>
</div>
<div class="stack">
<div class="row">Stack </div>
<span class="elem red">1</span>
</div>
</div>
</section>
<section>
<pre><code data-trim data-noescape class="python">
def op_checksigadd_schnorr(stack, tx_obj, input_index):
# check to see if there's at least 3 elements
if len(stack) < 3:
return False
# pop off the pubkey
pubkey = stack.pop()
# pop off the n and do decode_num on it
n = decode_num(stack.pop())
# pop off the signature
sig = stack.pop()
# parse the pubkey
point = S256Point.parse_xonly(pubkey)
# if the signature has 0 length, it is not valid
# so put encode_num(n) back on stack and return True
if len(sig) == 0:
stack.append(encode_num(n))
return True
# use the get_signature_and_hashtype function on the sig
schnorr, hash_type = get_signature_and_hashtype(sig)
# get the message from the tx_obj.sig_hash using input index and hash type
msg = tx_obj.sig_hash(input_index, hash_type)
# verify the Schnorr signature
if point.verify_schnorr(msg, schnorr):
# if valid, increment the n, encode_num it and push back on stack
stack.append(encode_num(n + 1))
else:
# if invalid, encode_num on n and push back on stack
stack.append(encode_num(n))
# return True for successful execution
return True
</code></pre>
</section>
<section>
<h2>Example TapScripts</h2>
<ul>
<li class="fragment">1-of-1 (pay-to-pubkey) [pubkey, <code>OP_CHECKSIG</code>]
<li class="fragment">2-of-2 [pubkey A, <code>OP_CHECKSIGVERIFY</code>, pubkey B, <code>OP_CHECKSIG</code>]
<li class="fragment">2-of-3 [pubkey A, <code>OP_CHECKSIG</code>, pubkey B, <code>OP_CHECKSIGADD</code>, pubkey C, <code>OP_CHECKSIGADD</code>, <code>OP_2</code>, <code>OP_EQUAL</code>]
<li class="fragment">halvening timelock 1-of-1 [840000, <code>OP_CHECKLOCKTIMEVERIFY</code>, <code>OP_DROP</code>, pubkey, <code>OP_CHECKSIG</code>]
</ul>
</section>
<section>
<h2>Example TapScript</h2>
<pre><code data-trim data-noescape class="python">
# Example TapScripts
from ecc import PrivateKey
from op import encode_minimal_num
from taproot import TapScript
pubkey_a = PrivateKey(11111111).point.xonly()
pubkey_b = PrivateKey(22222222).point.xonly()
pubkey_c = PrivateKey(33333333).point.xonly()
# 1-of-1 (0xAC is OP_CHECKSIG)
script_pubkey = TapScript([pubkey_a, 0xAC])
print(script_pubkey)
# 2-of-2 (0xAD is OP_CHECKSIGVERIFY)
script_pubkey = TapScript([pubkey_a, 0xAD, pubkey_b, 0xAC])
print(script_pubkey)
# 2-of-3 (0xBA is OP_CHECKSIGADD, 0x52 is OP_2, 0x87 is OP_EQUAL)
script_pubkey = TapScript([pubkey_a, 0xAD, pubkey_b, 0xBA, pubkey_c, 0xBA, 0x52, 0x87])
print(script_pubkey)
# halvening timelock 1-of-1 (0xB1 is OP_CLTV, 0x75 is OP_DROP)
script_pubkey = TapScript([encode_minimal_num(840000), 0xB1, 0x75, pubkey_a, 0xAC])
print(script_pubkey)
</code></pre>
</section>
<section>
<h2>Exercises</h2>
<ol start="17">
<li>Make a TapScript for a 4-of-4 using pubkeys from private keys which correspond to 10101, 20202, 30303, 40404
</ol>
</section>
<section>
<h2>TapLeaf</h2>
<ul>
<li class="fragment">These are the leaves of the Merkle Tree
<li class="fragment">Has a TapLeaf Version (<code>0xc0</code>) and TapScript
<li class="fragment">Any Leaf can be executed to satisfy the Taproot Script Path
<li class="fragment">Hash of a TapLeaf is a Tagged Hash (TapLeaf) of the version + TapScript
</ul>
</section>
<section>
<h2>Taproot Structure</h2>
<div class="tree">
<ul>
<li>
<div class="blue">External PubKey $Q$</div>
<ul>
<li>
<div class="green">Internal PubKey $P$</div>
</li>
<li>
<div class="brown">Merkle Root</div>
<ul>
<li>
<div class="brown">TapBranch</div>
<ul>
<li>
<div class="brown"><strong style="color:red"> TapLeaf </strong></div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
<li>
<div class="brown">TapLeaf</div>
<ul>
<li>
<div class="brown">TapScript</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</section>
<section>
<h2>Example TapLeaf Hash</h2>
<pre><code data-trim data-noescape class="python">
# Example of making a TapLeaf and calculating the hash
from ecc import PrivateKey
from hash import hash_tapleaf
from helper import int_to_byte
from taproot import TapScript, TapLeaf
pubkey_a = PrivateKey(11111111).point.xonly()
pubkey_b = PrivateKey(22222222).point.xonly()
tap_script = TapScript([pubkey_a, 0xAD, pubkey_b, 0xAC])
tap_leaf = TapLeaf(tap_script)
h = hash_tapleaf(int_to_byte(tap_leaf.tapleaf_version) + tap_leaf.tap_script.serialize())
print(h.hex())
</code></pre>
</section>
<section>
<h2>Exercises</h2>
<ol start="18">
<li>Calculate the TapLeaf hash whose TapScript is a 2-of-4 using pubkeys from private keys which correspond to 10101, 20202, 30303, 40404
<li>Make this test pass: <code>taproot:TapRootTest:test_tapleaf_hash</code>
</ol>