summaryrefslogtreecommitdiff
path: root/27/3c6b58611630f07f76c24b38e5207dfe8432d5
blob: 445a48d6c6cd803a2aadb2993bae6e55478e1211 (plain)
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
Return-Path: <salvatore.ingala@gmail.com>
Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136])
 by lists.linuxfoundation.org (Postfix) with ESMTP id E42E5C002A
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Mon,  1 May 2023 13:11:22 +0000 (UTC)
Received: from localhost (localhost [127.0.0.1])
 by smtp3.osuosl.org (Postfix) with ESMTP id C629660E78
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Mon,  1 May 2023 13:11:22 +0000 (UTC)
DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org C629660E78
Authentication-Results: smtp3.osuosl.org;
 dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com
 header.a=rsa-sha256 header.s=20221208 header.b=G3AblKoS
X-Virus-Scanned: amavisd-new at osuosl.org
X-Spam-Flag: NO
X-Spam-Score: -2.098
X-Spam-Level: 
X-Spam-Status: No, score=-2.098 tagged_above=-999 required=5
 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
 DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001] autolearn=ham autolearn_force=no
Received: from smtp3.osuosl.org ([127.0.0.1])
 by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id im-GECjQeZMW
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Mon,  1 May 2023 13:11:20 +0000 (UTC)
X-Greylist: whitelisted by SQLgrey-1.8.0
DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org AC9A060B71
Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com
 [IPv6:2607:f8b0:4864:20::c35])
 by smtp3.osuosl.org (Postfix) with ESMTPS id AC9A060B71
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Mon,  1 May 2023 13:11:20 +0000 (UTC)
Received: by mail-oo1-xc35.google.com with SMTP id
 006d021491bc7-547705d6197so1470230eaf.3
 for <bitcoin-dev@lists.linuxfoundation.org>;
 Mon, 01 May 2023 06:11:20 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1682946679; x=1685538679;
 h=to:subject:message-id:date:from:in-reply-to:references:mime-version
 :from:to:cc:subject:date:message-id:reply-to;
 bh=NohhQtkS3D776C6r/FcmunrfUlybdvC9qQr3xpqeUvY=;
 b=G3AblKoSyebtmNOPRfeUuivpuTbEV11uLc/mInnW24wB9M0HtSXzK2MlM8DrP76Ssu
 aVGD1t/VrJi+JMPwZ1VImAmT04k/++M2iZuYjRTAd1ZM85Y0DCMH2A/TlVbK0wrZGeJU
 00H2GyWgCVsApIcve0pAUMWqMo0tN5CziZsAH57S1cg9vUv9mgmuA6b62SydKYf+gaaP
 moxT1/i0oIAWCOxOTvyf1CnXXSmMGia+2bLurSroqPiqyHq0fuWAjSVEhT+zrW4wm9qt
 yJYsqJ+bQlbaECAUmDKiQAMBip0084BWfm6ahhqOTWnG879vzYTFaxdTcmUxLI8gmZ04
 zSpA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1682946679; x=1685538679;
 h=to:subject:message-id:date:from:in-reply-to:references:mime-version
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=NohhQtkS3D776C6r/FcmunrfUlybdvC9qQr3xpqeUvY=;
 b=TAvV4Tc9fJ4qg0Pn9zP80Du60lYTzvxZajbIJU4Vi1H7V0pnY1PUYZkSDeYLmn2RFn
 kHnCOWarRro6sq2F2rcm/xWt12dx8ek0Tk8BcWczKNmg5hJYmcfS6Rl3gfOzOnPmUaaj
 W+jySZV7kNMXeCxpS2UWlCvfHxo6lpLCPR2JQKV4nUNzIbi+h4GwkXCkzrv69Kha2Do4
 pKCUQjrC5v2UM3vFaiGeLTHjGaoxhouaOulcVt0G4Kg7nI3dytkrZrAa1NRb00D8J/4m
 VPgCBjIrG9dO22Eq8zDexr/DeamGxZeVlAqWakV9ij833MYZfxeCLa5r09rx/+yUlvxi
 zVvw==
X-Gm-Message-State: AC+VfDwqjkOGyvPlkCS9NZ5yOej3q6UuZkQ267Ob66wP/MxBgl7q8Xs2
 cBesudKItCSKAgEVobwynxeBcHMDF67vTIZH78RsW2BNPhAMbA==
X-Google-Smtp-Source: ACHHUZ6biVTS3L3lb881cWDx/iL9zL/MLjLptAoGGIeCh8m5pyudWNXZ51fHSzMRtppYkdl4wObxwvjTBLI39eAoHTw=
X-Received: by 2002:a4a:d688:0:b0:546:3aa4:3db8 with SMTP id
 i8-20020a4ad688000000b005463aa43db8mr6570485oot.5.1682946679366; Mon, 01 May
 2023 06:11:19 -0700 (PDT)
MIME-Version: 1.0
References: <CAMhCMoH9uZPeAE_2tWH6rf0RndqV+ypjbNzazpFwFnLUpPsZ7g@mail.gmail.com>
 <CALZpt+GVe0XTdWqV=LAcOj=nq+k2DEFqc+sKyxAujLDBR7bYbQ@mail.gmail.com>
 <CAMhCMoEONv3jriBU3qwm0pt75iF_sgra1H2Z4rOF8u+e7hs_cw@mail.gmail.com>
 <0f352f70-c93a-614f-e443-67d198ec2c26@protonmail.com>
 <7f3674d1-c1ad-9a82-e30f-7cf24d697faf@protonmail.com>
 <CAMhCMoGabEASO9CGc1hAMpYZn4nWH5D8XFs3eFcSSFAitSFUGA@mail.gmail.com>
 <CAGpPWDZkUYW=Qb763TPzUa6yUf217nh0Bo+O9Qyf=WS2pUQUYA@mail.gmail.com>
 <CAD3i26AXZKDCH3odhCjpMwzOTGQKSFqH9S+5N9UXNTb7CJHONA@mail.gmail.com>
In-Reply-To: <CAD3i26AXZKDCH3odhCjpMwzOTGQKSFqH9S+5N9UXNTb7CJHONA@mail.gmail.com>
From: Salvatore Ingala <salvatore.ingala@gmail.com>
Date: Mon, 1 May 2023 15:11:08 +0200
Message-ID: <CAMhCMoFgto3Bu5+yEoqn1Jf8fNd+EQK-t_H3TKR2=3RXe8FdcQ@mail.gmail.com>
To: =?UTF-8?Q?Johan_Tor=C3=A5s_Halseth?= <johanth@gmail.com>, 
 Bitcoin Protocol Discussion <bitcoin-dev@lists.linuxfoundation.org>
Content-Type: multipart/alternative; boundary="000000000000ffe94505faa18eb7"
X-Mailman-Approved-At: Mon, 01 May 2023 14:37:03 +0000
Subject: Re: [bitcoin-dev] Merkleize All The Things
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: Mon, 01 May 2023 13:11:23 -0000

--000000000000ffe94505faa18eb7
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Johan,

Thanks for your message.

I think games where all the possible futures can be enumerated are
not ideal to showcase MATT, as one could just fully represent them
with just CTV or COCV, and not use the "data embedding" at all.

Perhaps rock-paper-scissors could be a better academic example. [1]

I'm not sure this will fully address your question; however I think
it's quite an instructive example, and I wanted to work it out for
quite some time.

It would be interesting to explore some contracts where the size
of the embedded data is substantially larger, and that could be
a natural next step to think about.


### Rock paper scissors

We want a protocol between Alice and Bob, where they bet 1 coin each:

1. Alice chooses and publishes her move;
2. Bob chooses his move, and the pot is adjudicated as per the rules.

Of course, if implemented naively, this wouldn't be a very fun game:
Bob would just wait to see Alice's move and play accordingly.

That's easy to fix, though:

1. Alice publishes a commitment to her move
2. Bob publishes his move in clear
3. Alice reveals her move, and the pot is adjudicated.

We can encode Rock =3D 0, Paper =3D 1, Scissors =3D 2. Let m_A, m_B be
Alice's and Bob's move, respectively. Then, it's easy to verify that:
=E2=88=92 m_B - m_A =3D=3D 0 (mod 3) =3D=3D> it's a tie
=E2=88=92 m_B - m_A =3D=3D 1 (mod 3) =3D=3D> Bob wins
=E2=88=92 m_B - m_A =3D=3D 2 (mod 3) =3D=3D> Alice wins

In order to create a hiding commitment for Alice, she can choose a
256-bit random number r_A, and compute:

  c_A =3D SHA256(m_A || r_A)

With that in mind, the full protocol can go like this:

1. Alice chooses her move m_A and a large random number r_A;
   she posts c_A computed as above;
2. Bob chooses m_B and publishes it;
3. Alice publishes m_A and r_A, then the winner is adjudicated.


### MATT playing RPS

To implement this with CICV/COCV, we can use just 3 transactions: in
fact, Alice can already compute c_A and share it with Bob before they
both commit their coins into an encumbered UTXO. That also means that
c_A can actually be hardcoded in the Scripts, rather than taking
space in the UTXO's embedded data.

Therefore, they both put one coin each, and they send to an output
whose script is the state S0 described below.

We assume that the keypath in the P2TR defined below is either a NUMS
point, or perhaps a Musig2 aggregate key that can be used to settle
the game collaboratively.

Note that there are 3 possible payout options that are fully known
when the game starts: either Alice takes all the money, or they split
evenly, or Bob takes all the money.
Similarly to the vault implementation [2], this seems to be another
case where CTV fits very well, as it allows to very efficiently
describe the three possible outcomes by their CTV hashes. Let them
be <ctv-alice-wins>, <ctv-split>, <ctv-bob-wins>, respectively.

Therefore, this avoids the need for 64-bit maths, and explicit amount
introspection =E2=88=92 at least for these contracts.


[State S0] (Start of the game, Alice moved; Bob's turn)
Spending conditions:
 - after <forfait-delay>, Alice takes the money    // (Bob forfaits)
 - Bob posts m_B (0, 1 or 2); the next output is [S1] with data m_B

The first script is:
  // witness: []
  <forfait-delay>
  OP_CHECKSEQUENCEVERIFY
  OP_DROP
  <ctv-alice-wins>
  OP_CHECKTEMPLATEVERIFY

The second is
  // witness: [<bob_sig> <m_B>]
  OP_DUP 0 3 OP_WITHIN     // check that m_B is 0, 1 or 2

  <internal_pubkey> OP_SWAP
  <S1's taptree>
  OP_CHECKOUTPUTCONTRACTVERIFY // check that the output is correct

  <bob_pubkey>
  OP_CHECKSIG


[State S1] (Alice reveals m_A and adjudicates)
 - after <forfait-timeout>, Bob takes the money    // (Alice forfaits)
 - Alice posts correct m_A and r_A compatible with c_A;


The first script is symmetric to Bob's forfait script above.

The second condition can be split into three leaf scripts, one for
each possible value of m_B - m_A (mod 3):

  // witness: [<m_B> <m_A> <r_A>]

  OP_OVER OP_DUP OP_TOALTSTACK  // save m_A
  0 3 OP_WITHIN OP_VERIFY       // check that m_A is 0, 1 or 2

  // check that SHA256(m_A || r_A) equals c_A
  OP_2DUP
  OP_CAT OP_SHA256
  <c_A>
  OP_EQUALVERIFY

  OP_DUP
  <internal_pubkey>, OP_SWAP
  OP_CHECKINPUTCONTRACTVERIFY

  OP_FROMALTSTACK
  OP_SUB           // stack now contains m_B - m_A

  OP_DUP           // if the result is negative, add 3
  0 OP_LESSTHAN
  OP_IF
    3
    OP_ADD
  OP_ENDIF

  {0, 1, 2}       // draw / Bob wins / Alice wins, respectively
  OP_EQUALVERIFY

  {<ctv-split>, <ctv-bob-wins>, <ctv-alice-wins>}  // respectively
  OP_CHECKTEMPLATEVERIFY


### Comments

In general, we would have to worry about the possible
malleability of the witness elements, when they are not signatures
or preimages themselves. Here, in particular, it might seem that's
an issue when <m_B> is provided while spending the state [S0].
However, here the value of <m_B> is also committed to in the output
thanks to COCV; therefore, Bob's signature prevents malleability
also for m_B.

In general, it seems to be the case in MATT contracts that one would
want the signature of the authorized party performing a transition to
some other state of the smart contract with contains embedded data;
this makes the malleability issue less of a problem in practice than
I initially thought.

If the internal_pubkey is a musig-aggregated key of Alice and Bob,
the game can be settled entirely offline after the first transaction.
Simply, Bob communicates his move to Alice, Alice reveals her move to
Bob, and they can settle the bet. The game would be played without
any script being executed, therefore all transactions could look like
any other P2TR, with the only possible fingerprinting being due to the
input amounts.

It should be possible to generalize the protocol so that many rounds
can be played off-chain within the same UTXO, but I didn't try to
figure out the details.

Best,
Salvatore Ingala


[1] - https://en.wikipedia.org/wiki/Rock_paper_scissors
[2] -
https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2023-April/021588.h=
tml

On Fri, 28 Apr 2023 at 10:48, Johan Tor=C3=A5s Halseth <johanth@gmail.com> =
wrote:

> Hi, Salvatore.
>
> I find this proposal very interesting. Especially since you seemingly
> can achieve such powerful capabilities by such simple opcodes.
>
> I'm still trying to grok how this would look like on-chain (forget
> about the off-chain part for now), if we were to play out such a
> computation.
>
> Let's say you have a simple game like "one player tic-tac-toe" with
> only two tiles: [ _ | _ ]. The player wins if he can get two in a row
> (pretty easy game tbh).
>
> Could you give a complete example how you would encode one such state
> transition (going from [ X, _ ] -> [ X, X ] for instance) in Bitcoin
> script?
>
> Feel free to choose a different game or program if you prefer :)
>
> Thanks!
> Johan
>
>
>
> On Tue, Dec 13, 2022 at 2:08=E2=80=AFPM Billy Tetrud via bitcoin-dev
> <bitcoin-dev@lists.linuxfoundation.org> wrote:
> >
> > Re Verkle trees, that's a very interesting construction that would be
> super useful as a tool for something like Utreexo. A potentially
> substantial downside is that it seems the cryptography used to get those
> nice properties of Verkle trees isn't quantum safe. While a lot of things
> in Bitcoin seems to be going down the path of quantum-unsafe (I'm looking
> at you, taproot), there are still a lot of people who think quantum safet=
y
> is important in a lot of contexts.
> >
> > On Thu, Dec 1, 2022 at 5:52 AM Salvatore Ingala via bitcoin-dev <
> bitcoin-dev@lists.linuxfoundation.org> wrote:
> >>
> >> Hello Rijndael,
> >>
> >>
> >>
> >> On Wed, 30 Nov 2022 at 23:09, Rijndael <rot13maxi@protonmail.com>
> wrote:
> >>>
> >>> Hello Salvatore,
> >>>
> >>> I found my answer re-reading your original post:
> >>> > During the arbitration phase (say at the i-th leaf node of M_T), an=
y
> party can win the challenge by providing correct values for tr_i =3D (st_=
i,
> op_i, st_{i + 1}). Crucially, only one party is able to provide correct
> values, and Script can verify that indeed the state moves from st_i to
> st_{i + 1} by executing op_i. The challenge is over.
> >>
> >> You are correct, the computation step encoded in a leaf needs to be
> simple enough for Script to verify it.
> >>
> >> For the academic purpose of proving completeness (that is, any
> computation can be successfully "proved" by the availability of the
> corresponding fraud proof), one can imagine reducing the computation all
> the way down to a circuit, where each step (leaf) is as simple as what ca=
n
> be checked with {OP_NOT, OP_BOOLAND, OP_BOOLOR, OP_EQUAL}.
> >>
> >> In practice, you would want to utilize Script to its fullest, so for
> example you wouldn't compile a SHA256 computation to something else =E2=
=80=93 you'd
> rather use OP_SHA256 directly.
> >>
> >>>
> >>> That raises leads to a different question: Alice initially posts a
> commitment to an execution trace of `f(x) =3D y`, `x`, and `y`. Bob Disag=
rees
> with `y` so starts the challenge protocol. Is there a commitment to `f`? =
In
> other words, the dispute protocol (as I read it) finds the leftmost step =
in
> Alice and Bob's execution traces that differ, and then rewards the coins =
to
> the participant who's "after-value" is computed by the step's operation
> applied to the "before value". But if the participants each present valid
> steps but with different operations, who wins? In other words, Alice coul=
d
> present [64, DECREMENT, 63] and Bob could present [64, INCREMENT, 65].
> Those steps don't match, but both are valid. Is there something to ensure
> that before the challenge protocol starts, that the execution trace that
> Alice posts is for the right computation and not a different computation
> that yields a favorable result for her (and for which she can generate a
> valid merkle tree)?
> >>
> >>
> >> The function f is already hard-coded in the contract itself, by means
> of the tree of scripts =E2=88=92 that already commits to the possible fut=
ures.
> Therefore, once you are at state S14, you know that you are verifying the
> 6th step of the computation; and the operation in the 6th step of the
> computation depends solely on f, not its inputs. In fact, you made me
> realize that I could drop op_i from the i-th leaf commitment, and just
> embed the information in the Script of that corresponding state.
> >>
> >> Note that the states S0 to S14 of the 256x game are not _all_ the
> possible states, but only the ones that occurred in that execution of the
> contract (corresponding to a path from the root to the leaf of the Merkle
> tree of the computation trace), and therefore the ones that materialized =
in
> a UTXO. Different choices made by the parties (by providing different dat=
a,
> and therefore choosing different branches) would lead to a different leaf=
,
> and therefore to different (but in a certain sense "symmetric") states.
> >>
> >> =3D=3D=3D=3D=3D=3D=3D=3D
> >>
> >> Since we are talking about the fact that f is committed to in the
> contract, I'll take the chance to extend on this a bit with a fun
> construction on top.
> >> It is well-known in the academic literature of state channels that you
> can create contracts where even the function ("program", or "contract") i=
s
> not decided when the channel is created.
> >>
> >> Since f is generic, we can choose f itself to be a universal Turing
> machine. That is, we can imagine a function f(code, data) that executes a
> program ("code") on the "data" given to it as input.
> >> Since we can do fraud proofs on statements "f(code, data) =3D=3D outpu=
t",
> we could build contracts where the "code" itself is chosen later.
> >>
> >> For example, one could build a universal state channel, where parties
> can enter any contract among themselves (e.g.: start playing a chess game=
)
> entirely inside the channel. The state of this universal channel would
> contain all the states of the individual contracts that are currently ope=
n
> in the channel, and even starting/closing contracts can happen entirely
> off-chain.
> >>
> >> I believe these constructions are practical (the code of universal
> Turing machines is not really complicated), so it might be worth explorin=
g
> further to figure out useful applications of this approach (supercharging
> lightning?).
> >>
> >> We should probably start by implementing testnet rock-paper-scissors i=
n
> MATT, though :)
> >>
> >> Best,
> >> Salvatore Ingala
> >> _______________________________________________
> >> bitcoin-dev mailing list
> >> bitcoin-dev@lists.linuxfoundation.org
> >> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
> >
> > _______________________________________________
> > bitcoin-dev mailing list
> > bitcoin-dev@lists.linuxfoundation.org
> > https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
>

--000000000000ffe94505faa18eb7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">Hi Johan,<div><br></div><div>Thanks for y=
our message.<br><br>I think games where all the possible futures can be enu=
merated are<br>not ideal to showcase MATT, as one could just fully represen=
t them</div><div>with just CTV or COCV, and not use the &quot;data embeddin=
g&quot; at all.<br><br>Perhaps rock-paper-scissors could be a better academ=
ic example. [1]<br><br>I&#39;m not sure this will fully address your questi=
on; however I think</div><div>it&#39;s quite an instructive example, and I =
wanted to work it out for</div><div>quite some time.</div><div><br></div><d=
iv>It would be interesting to explore some contracts where the size</div><d=
iv>of the embedded data is substantially larger, and that could be</div><di=
v>a natural next step to=C2=A0think=C2=A0about.</div><div><br></div><div><b=
r>### Rock paper scissors<br><br>We want a protocol between Alice and Bob, =
where they bet 1 coin each:<br><br>1. Alice chooses and publishes her move;=
<br>2. Bob chooses his move, and the pot is adjudicated as per the rules.<b=
r><br>Of course, if implemented naively, this wouldn&#39;t be a very fun ga=
me:<br>Bob would just wait to see Alice&#39;s move and play accordingly.<br=
><br>That&#39;s easy to fix, though:<br><br>1. Alice publishes a commitment=
 to her move<br>2. Bob publishes his move in clear<br>3. Alice reveals her =
move, and the pot is adjudicated.<br><br>We can encode Rock =3D 0, Paper =
=3D 1, Scissors =3D 2. Let m_A, m_B be<br>Alice&#39;s and Bob&#39;s move, r=
espectively. Then, it&#39;s easy to verify that:<br>=E2=88=92 m_B - m_A =3D=
=3D 0 (mod 3) =3D=3D&gt; it&#39;s a tie<br>=E2=88=92 m_B - m_A =3D=3D 1 (mo=
d 3) =3D=3D&gt; Bob wins<br>=E2=88=92 m_B - m_A =3D=3D 2 (mod 3) =3D=3D&gt;=
 Alice wins<br><br>In order to create a hiding commitment for Alice, she ca=
n choose a<br>256-bit random number r_A, and compute:<br><br>=C2=A0 c_A =3D=
 SHA256(m_A || r_A)<br><br>With that in mind, the full protocol can go like=
 this:<br><br>1. Alice chooses her move m_A and a large random number r_A;<=
br>=C2=A0 =C2=A0she posts c_A computed as above;<br>2. Bob chooses m_B and =
publishes it;<br>3. Alice publishes m_A and r_A, then the winner is adjudic=
ated.<br><br></div><div><br>### MATT playing RPS<br><br>To implement this w=
ith CICV/COCV, we can use just 3 transactions: in<br>fact, Alice can alread=
y compute c_A and share it with Bob before they<br>both commit their coins =
into an encumbered UTXO. That also means that<br>c_A can actually be hardco=
ded in the Scripts, rather than taking<br>space in the UTXO&#39;s embedded =
data.<br><br>Therefore, they both put one coin each, and they send to an ou=
tput<br>whose script is the state S0 described below.<br><br></div><div>We =
assume that the keypath in the P2TR defined below is either a NUMS<br>point=
, or perhaps a Musig2 aggregate key that can be used to settle<br>the game =
collaboratively.<br></div><div><br>Note that there are 3 possible payout op=
tions that are fully known<br>when the game starts: either Alice takes all =
the money, or they split<br>evenly, or Bob takes all the money.<br>Similarl=
y to the vault implementation [2], this seems to be another<br>case where C=
TV fits very well, as it allows to very efficiently<br>describe the three p=
ossible outcomes by their CTV hashes. Let them<br>be &lt;ctv-alice-wins&gt;=
, &lt;ctv-split&gt;, &lt;ctv-bob-wins&gt;, respectively.<br><br></div><div>=
Therefore, this avoids the need for 64-bit maths, and explicit amount</div>=
<div>introspection =E2=88=92 at least for these contracts.<br><br><br>[Stat=
e S0] (Start of the game, Alice moved; Bob&#39;s turn)<br>Spending conditio=
ns:<br>=C2=A0- after &lt;forfait-delay&gt;, Alice takes the money =C2=A0 =
=C2=A0// (Bob forfaits)<br>=C2=A0- Bob posts m_B (0, 1 or 2); the next outp=
ut is [S1] with data m_B<br><br>The first script is:<br>=C2=A0 // witness: =
[]<br>=C2=A0 &lt;forfait-delay&gt;<br>=C2=A0 OP_CHECKSEQUENCEVERIFY<br>=C2=
=A0 OP_DROP<br>=C2=A0 &lt;ctv-alice-wins&gt;<br>=C2=A0 OP_CHECKTEMPLATEVERI=
FY<br><br>The second is<br>=C2=A0 // witness: [&lt;bob_sig&gt; &lt;m_B&gt;]=
<br>=C2=A0 OP_DUP 0 3 OP_WITHIN =C2=A0 =C2=A0 // check that m_B is 0, 1 or =
2<br><br>=C2=A0 &lt;internal_pubkey&gt; OP_SWAP<br>=C2=A0 &lt;S1&#39;s tapt=
ree&gt;<br>=C2=A0 OP_CHECKOUTPUTCONTRACTVERIFY // check that the output is =
correct<br><br>=C2=A0 &lt;bob_pubkey&gt;<br>=C2=A0 OP_CHECKSIG</div><div><b=
r><br>[State S1] (Alice reveals m_A and adjudicates)<br>=C2=A0- after &lt;f=
orfait-timeout&gt;, Bob takes the money =C2=A0 =C2=A0// (Alice forfaits)<br=
>=C2=A0- Alice posts correct m_A and r_A compatible with c_A; =C2=A0<br><br=
><br>The first script is symmetric to Bob&#39;s forfait script above.<br><b=
r>The second condition can be split into three leaf scripts, one for<br>eac=
h possible value of m_B - m_A (mod 3):<br><br>=C2=A0 // witness: [&lt;m_B&g=
t; &lt;m_A&gt; &lt;r_A&gt;]<br><br>=C2=A0 OP_OVER OP_DUP OP_TOALTSTACK =C2=
=A0// save m_A<br>=C2=A0 0 3 OP_WITHIN OP_VERIFY =C2=A0 =C2=A0 =C2=A0 // ch=
eck that m_A is 0, 1 or 2<br><br>=C2=A0 // check that SHA256(m_A || r_A) eq=
uals c_A<br>=C2=A0 OP_2DUP<br>=C2=A0 OP_CAT OP_SHA256<br>=C2=A0 &lt;c_A&gt;=
<br>=C2=A0 OP_EQUALVERIFY<br><br>=C2=A0 OP_DUP<br>=C2=A0 &lt;internal_pubke=
y&gt;, OP_SWAP<br>=C2=A0 OP_CHECKINPUTCONTRACTVERIFY<br><br>=C2=A0 OP_FROMA=
LTSTACK<br>=C2=A0 OP_SUB =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // stack now co=
ntains m_B - m_A<br><br>=C2=A0 OP_DUP =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 //=
 if the result is negative, add 3<br>=C2=A0 0 OP_LESSTHAN<br>=C2=A0 OP_IF<b=
r>=C2=A0 =C2=A0 3<br>=C2=A0 =C2=A0 OP_ADD<br>=C2=A0 OP_ENDIF<br><br>=C2=A0 =
{0, 1, 2} =C2=A0 =C2=A0 =C2=A0 // draw / Bob wins / Alice wins, respectivel=
y<br>=C2=A0 OP_EQUALVERIFY<br><br>=C2=A0 {&lt;ctv-split&gt;, &lt;ctv-bob-wi=
ns&gt;, &lt;ctv-alice-wins&gt;} =C2=A0// respectively<br>=C2=A0 OP_CHECKTEM=
PLATEVERIFY<br><br><br>### Comments<br><br>In general, we would have to wor=
ry about the possible<br>malleability of the witness elements, when they ar=
e not signatures<br>or preimages themselves. Here, in particular, it might =
seem that&#39;s</div><div>an issue when &lt;m_B&gt; is provided while spend=
ing the state [S0].</div><div>However, here the value of &lt;m_B&gt; is als=
o committed to in the output<br>thanks to COCV; therefore, Bob&#39;s signat=
ure prevents malleability</div><div>also for m_B.<br><br>In general, it see=
ms to be the case in MATT contracts that one would<br>want the signature of=
 the authorized party performing a transition to<br>some other state of the=
 smart contract with contains embedded data;<br>this makes the malleability=
 issue less of a problem in practice than</div><div>I initially thought.<br=
><br>If the internal_pubkey is a musig-aggregated key of Alice and Bob,<br>=
the game can be settled entirely offline after the first transaction.<br>Si=
mply, Bob communicates his move to Alice, Alice reveals her move to<br>Bob,=
 and they can settle the bet. The game would be played without<br>any scrip=
t being executed, therefore all transactions could look like</div><div>any =
other P2TR,=C2=A0with the only possible fingerprinting being due to the</di=
v><div>input amounts.<br><br>It should be possible to generalize the protoc=
ol so that many rounds<br>can be played off-chain within the same UTXO, but=
 I didn&#39;t try to<br>figure out the details.<br><br>Best,<br>Salvatore I=
ngala<br><br><br>[1] - <a href=3D"https://en.wikipedia.org/wiki/Rock_paper_=
scissors">https://en.wikipedia.org/wiki/Rock_paper_scissors</a><br>[2] - <a=
 href=3D"https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2023-April=
/021588.html">https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2023-=
April/021588.html</a><br></div></div><br><div class=3D"gmail_quote"><div di=
r=3D"ltr" class=3D"gmail_attr">On Fri, 28 Apr 2023 at 10:48, Johan Tor=C3=
=A5s Halseth &lt;<a href=3D"mailto:johanth@gmail.com">johanth@gmail.com</a>=
&gt; 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">Hi, =
Salvatore.<br>
<br>
I find this proposal very interesting. Especially since you seemingly<br>
can achieve such powerful capabilities by such simple opcodes.<br>
<br>
I&#39;m still trying to grok how this would look like on-chain (forget<br>
about the off-chain part for now), if we were to play out such a<br>
computation.<br>
<br>
Let&#39;s say you have a simple game like &quot;one player tic-tac-toe&quot=
; with<br>
only two tiles: [ _ | _ ]. The player wins if he can get two in a row<br>
(pretty easy game tbh).<br>
<br>
Could you give a complete example how you would encode one such state<br>
transition (going from [ X, _ ] -&gt; [ X, X ] for instance) in Bitcoin<br>
script?<br>
<br>
Feel free to choose a different game or program if you prefer :)<br>
<br>
Thanks!<br>
Johan<br>
<br>
<br>
<br>
On Tue, Dec 13, 2022 at 2:08=E2=80=AFPM Billy Tetrud via bitcoin-dev<br>
&lt;<a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D"_bla=
nk">bitcoin-dev@lists.linuxfoundation.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Re Verkle trees, that&#39;s a very interesting construction that would=
 be super useful as a tool for something like Utreexo. A potentially substa=
ntial downside is that it seems the cryptography used to get those nice pro=
perties of Verkle trees isn&#39;t quantum safe. While a lot of things in Bi=
tcoin seems to be going down the path of quantum-unsafe (I&#39;m looking at=
 you, taproot), there are still a lot of people who think quantum safety is=
 important in a lot of contexts.<br>
&gt;<br>
&gt; On Thu, Dec 1, 2022 at 5:52 AM Salvatore Ingala via bitcoin-dev &lt;<a=
 href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D"_blank">bi=
tcoin-dev@lists.linuxfoundation.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hello Rijndael,<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On Wed, 30 Nov 2022 at 23:09, Rijndael &lt;<a href=3D"mailto:rot13=
maxi@protonmail.com" target=3D"_blank">rot13maxi@protonmail.com</a>&gt; wro=
te:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Hello Salvatore,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I found my answer re-reading your original post:<br>
&gt;&gt;&gt; &gt; During the arbitration phase (say at the i-th leaf node o=
f M_T), any party can win the challenge by providing correct values for tr_=
i =3D (st_i, op_i, st_{i + 1}). Crucially, only one party is able to provid=
e correct values, and Script can verify that indeed the state moves from st=
_i to st_{i + 1} by executing op_i. The challenge is over.<br>
&gt;&gt;<br>
&gt;&gt; You are correct, the computation step encoded in a leaf needs to b=
e simple enough for Script to verify it.<br>
&gt;&gt;<br>
&gt;&gt; For the academic purpose of proving completeness (that is, any com=
putation can be successfully &quot;proved&quot; by the availability of the =
corresponding fraud proof), one can imagine reducing the computation all th=
e way down to a circuit, where each step (leaf) is as simple as what can be=
 checked with {OP_NOT, OP_BOOLAND, OP_BOOLOR, OP_EQUAL}.<br>
&gt;&gt;<br>
&gt;&gt; In practice, you would want to utilize Script to its fullest, so f=
or example you wouldn&#39;t compile a SHA256 computation to something else =
=E2=80=93 you&#39;d rather use OP_SHA256 directly.<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; That raises leads to a different question: Alice initially pos=
ts a commitment to an execution trace of `f(x) =3D y`, `x`, and `y`. Bob Di=
sagrees with `y` so starts the challenge protocol. Is there a commitment to=
 `f`? In other words, the dispute protocol (as I read it) finds the leftmos=
t step in Alice and Bob&#39;s execution traces that differ, and then reward=
s the coins to the participant who&#39;s &quot;after-value&quot; is compute=
d by the step&#39;s operation applied to the &quot;before value&quot;. But =
if the participants each present valid steps but with different operations,=
 who wins? In other words, Alice could present [64, DECREMENT, 63] and Bob =
could present [64, INCREMENT, 65]. Those steps don&#39;t match, but both ar=
e valid. Is there something to ensure that before the challenge protocol st=
arts, that the execution trace that Alice posts is for the right computatio=
n and not a different computation that yields a favorable result for her (a=
nd for which she can generate a valid merkle tree)?<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; The function f is already hard-coded in the contract itself, by me=
ans of the tree of scripts =E2=88=92 that already commits to the possible f=
utures. Therefore, once you are at state S14, you know that you are verifyi=
ng the 6th step of the computation; and the operation in the 6th step of th=
e computation depends solely on f, not its inputs. In fact, you made me rea=
lize that I could drop op_i from the i-th leaf commitment, and just embed t=
he information in the Script of that corresponding state.<br>
&gt;&gt;<br>
&gt;&gt; Note that the states S0 to S14 of the 256x game are not _all_ the =
possible states, but only the ones that occurred in that execution of the c=
ontract (corresponding to a path from the root to the leaf of the Merkle tr=
ee of the computation trace), and therefore the ones that materialized in a=
 UTXO. Different choices made by the parties (by providing different data, =
and therefore choosing different branches) would lead to a different leaf, =
and therefore to different (but in a certain sense &quot;symmetric&quot;) s=
tates.<br>
&gt;&gt;<br>
&gt;&gt; =3D=3D=3D=3D=3D=3D=3D=3D<br>
&gt;&gt;<br>
&gt;&gt; Since we are talking about the fact that f is committed to in the =
contract, I&#39;ll take the chance to extend on this a bit with a fun const=
ruction on top.<br>
&gt;&gt; It is well-known in the academic literature of state channels that=
 you can create contracts where even the function (&quot;program&quot;, or =
&quot;contract&quot;) is not decided when the channel is created.<br>
&gt;&gt;<br>
&gt;&gt; Since f is generic, we can choose f itself to be a universal Turin=
g machine. That is, we can imagine a function f(code, data) that executes a=
 program (&quot;code&quot;) on the &quot;data&quot; given to it as input.<b=
r>
&gt;&gt; Since we can do fraud proofs on statements &quot;f(code, data) =3D=
=3D output&quot;, we could build contracts where the &quot;code&quot; itsel=
f is chosen later.<br>
&gt;&gt;<br>
&gt;&gt; For example, one could build a universal state channel, where part=
ies can enter any contract among themselves (e.g.: start playing a chess ga=
me) entirely inside the channel. The state of this universal channel would =
contain all the states of the individual contracts that are currently open =
in the channel, and even starting/closing contracts can happen entirely off=
-chain.<br>
&gt;&gt;<br>
&gt;&gt; I believe these constructions are practical (the code of universal=
 Turing machines is not really complicated), so it might be worth exploring=
 further to figure out useful applications of this approach (supercharging =
lightning?).<br>
&gt;&gt;<br>
&gt;&gt; We should probably start by implementing testnet rock-paper-scisso=
rs in MATT, though :)<br>
&gt;&gt;<br>
&gt;&gt; Best,<br>
&gt;&gt; Salvatore Ingala<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; bitcoin-dev mailing list<br>
&gt;&gt; <a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D=
"_blank">bitcoin-dev@lists.linuxfoundation.org</a><br>
&gt;&gt; <a href=3D"https://lists.linuxfoundation.org/mailman/listinfo/bitc=
oin-dev" rel=3D"noreferrer" target=3D"_blank">https://lists.linuxfoundation=
.org/mailman/listinfo/bitcoin-dev</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; bitcoin-dev mailing list<br>
&gt; <a href=3D"mailto:bitcoin-dev@lists.linuxfoundation.org" target=3D"_bl=
ank">bitcoin-dev@lists.linuxfoundation.org</a><br>
&gt; <a href=3D"https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-=
dev" rel=3D"noreferrer" target=3D"_blank">https://lists.linuxfoundation.org=
/mailman/listinfo/bitcoin-dev</a><br>
</blockquote></div></div>

--000000000000ffe94505faa18eb7--