-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_fraud.py
277 lines (216 loc) · 8.75 KB
/
test_fraud.py
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
from examples.game256.game256_contracts import G256_S0, G256_S1, G256_S2, Compute2x
from matt.btctools.common import sha256
from matt.btctools.messages import CTxOut
from matt.contracts import P2TR
from matt.hub.fraud import Bisect_1, Bisect_2, Leaf
from matt.manager import ContractManager, SchnorrSigner
from matt.merkle import is_power_of_2
from matt.btctools import key
from matt.utils import encode_wit_element, format_tx_markdown
AMOUNT = 20_000
alice_key = key.ExtendedKey.deserialize(
"tprv8ZgxMBicQKsPdpwA4vW8DcSdXzPn7GkS2RdziGXUX8k86bgDQLKhyXtB3HMbJhPFd2vKRpChWxgPe787WWVqEtjy8hGbZHqZKeRrEwMm3SN")
bob_key = key.ExtendedKey.deserialize(
"tprv8ZgxMBicQKsPeDvaW4xxmiMXxqakLgvukT8A5GR6mRwBwjsDJV1jcZab8mxSerNcj22YPrusm2Pz5oR8LTw9GqpWT51VexTNBzxxm49jCZZ")
def test_leaf_reveal_alice(manager: ContractManager):
L = Leaf(alice_key.pubkey[1:], bob_key.pubkey[1:], Compute2x)
x_start = 347
x_end_alice = 2 * x_start
x_end_bob = 2 * x_start - 1 # some wrong value
h_start = sha256(encode_wit_element(x_start))
h_end_alice = sha256(encode_wit_element(x_end_alice))
h_end_bob = sha256(encode_wit_element(x_end_bob))
L_inst = manager.fund_instance(L, AMOUNT, data=L.State(
h_start=h_start, h_end_alice=h_end_alice, h_end_bob=h_end_bob))
outputs = [
CTxOut(
nValue=AMOUNT,
scriptPubKey=P2TR(alice_key.pubkey[1:], []).get_tr_info().scriptPubKey
)
]
out_instances = L_inst("alice_reveal", SchnorrSigner(alice_key), outputs)(
x=x_start,
h_y_b=h_end_bob
)
assert len(out_instances) == 0
def test_leaf_reveal_bob(manager: ContractManager):
L = Leaf(alice_key.pubkey[1:], bob_key.pubkey[1:], Compute2x)
x_start = 347
x_end_alice = 2 * x_start - 1 # some wrong value
x_end_bob = 2 * x_start
h_start = sha256(encode_wit_element(x_start))
h_end_alice = sha256(encode_wit_element(x_end_alice))
h_end_bob = sha256(encode_wit_element(x_end_bob))
L_inst = manager.fund_instance(L, AMOUNT, data=L.State(
h_start=h_start, h_end_alice=h_end_alice, h_end_bob=h_end_bob))
outputs = [
CTxOut(
nValue=AMOUNT,
scriptPubKey=P2TR(bob_key.pubkey[1:], []).get_tr_info().scriptPubKey
)
]
out_instances = L_inst("bob_reveal", SchnorrSigner(bob_key), outputs)(
x=x_start,
h_y_a=h_end_alice
)
assert len(out_instances) == 0
def test_fraud_proof_full(manager: ContractManager, report):
alice_trace = [2, 4, 8, 16, 32, 64, 127, 254, 508]
bob_trace = [2, 4, 8, 16, 32, 64, 128, 256, 512]
assert alice_trace[0] == bob_trace[0] and len(alice_trace) == len(bob_trace)
n = len(alice_trace) - 1 # the trace has n + 1 entries
assert is_power_of_2(n)
h_a = [sha256(encode_wit_element(x)) for x in alice_trace]
h_b = [sha256(encode_wit_element(x)) for x in bob_trace]
def t_from_trace(trace, i, j):
assert len(trace) > j
assert 0 <= i < n
assert i <= j < n
assert j >= i and is_power_of_2(j - i + 1)
m = (j - i + 1) // 2
if i == j:
return sha256(trace[i] + trace[i + 1])
else:
return sha256(trace[i] + trace[j + 1] + t_from_trace(trace, i, i + m - 1) + t_from_trace(trace, i + m, j))
def t_node_a(i, j) -> bytes:
return t_from_trace(h_a, i, j)
def t_node_b(i, j) -> bytes:
return t_from_trace(h_b, i, j)
x = 2
y = alice_trace[-1]
z = bob_trace[-1]
assert z == 2 * 256 # Bob is saying the truth
alice_signer = SchnorrSigner(alice_key)
bob_signer = SchnorrSigner(bob_key)
# Game starts, the UTXO is funded
G = G256_S0(alice_key.pubkey[1:], bob_key.pubkey[1:])
inst = manager.fund_instance(G, AMOUNT)
# Bob chooses its input
[inst] = inst('choose', bob_signer)(x=x)
assert isinstance(inst.contract, G256_S1)
assert isinstance(inst.data_expanded, G256_S1.State) and inst.data_expanded.x == x
t_a = t_node_a(0, n - 1) # trace root according to Alice
t_b = t_node_b(0, n - 1) # trace root according to Bob
# Alice reveals her answer
[inst] = inst('reveal', alice_signer)(x=x, y=y, t_a=t_a)
assert isinstance(inst.contract, G256_S2)
assert inst.data_expanded == G256_S2.State(t_a=t_a, x=x, y=y)
# Bob disagrees and starts the challenge
[inst] = inst('start_challenge', bob_signer)(
t_a=t_a,
x=x,
y=y,
z=z,
t_b=t_b
)
# inst now represents a step in the bisection protocol corresponding to the root of the computation
assert isinstance(inst.contract, Bisect_1)
assert inst.contract.i == 0 and inst.contract.j == 7
i, j = inst.contract.i, inst.contract.j
m = (j - i + 1) // 2
[inst] = inst('alice_reveal', alice_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j)
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Alice)"))
assert isinstance(inst.contract, Bisect_2)
assert inst.contract.i == 0 and inst.contract.j == 7
[inst] = inst('bob_reveal_right', bob_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j),
h_mid_b=h_b[i + m],
trace_left_b=t_node_b(i, i + m - 1),
trace_right_b=t_node_b(i + m, j),
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Bob, right child)"))
assert isinstance(inst.contract, Bisect_1)
i, j = inst.contract.i, inst.contract.j
m = (j - i + 1) // 2
assert i == 4 and j == 7
# Bisection repeats on the node covering from index 4 to index 7
[inst] = inst('alice_reveal', alice_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j)
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Alice)"))
assert isinstance(inst.contract, Bisect_2)
assert inst.contract.i == 4 and inst.contract.j == 7
[inst] = inst('bob_reveal_left', bob_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j),
h_mid_b=h_b[i + m],
trace_left_b=t_node_b(i, i + m - 1),
trace_right_b=t_node_b(i + m, j),
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Bob, left child)"))
assert isinstance(inst.contract, Bisect_1)
i, j = inst.contract.i, inst.contract.j
m = (j - i + 1) // 2
assert i == 4 and j == 5
# Bisection repeats on the node covering from index 4 to index 5 (last bisection step)
[inst] = inst('alice_reveal', alice_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j)
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Alice)"))
assert isinstance(inst.contract, Bisect_2)
assert inst.contract.i == 4 and inst.contract.j == 5
[inst] = inst('bob_reveal_right', bob_signer)(
h_start=h_a[i],
h_end_a=h_a[j + 1],
h_end_b=h_b[j + 1],
trace_a=t_node_a(i, j),
trace_b=t_node_b(i, j),
h_mid_a=h_a[i + m],
trace_left_a=t_node_a(i, i + m - 1),
trace_right_a=t_node_a(i + m, j),
h_mid_b=h_b[i + m],
trace_left_b=t_node_b(i, i + m - 1),
trace_right_b=t_node_b(i + m, j),
)
report.write("Fraud proof", format_tx_markdown(inst.funding_tx, "Bisection (Bob, right child)"))
# We reached a leaf. Only who was doubling correctly can withdraw
assert isinstance(inst.contract, Leaf)
assert alice_trace[5] == bob_trace[5] and alice_trace[6] != bob_trace[6]
outputs = [
CTxOut(
nValue=AMOUNT,
scriptPubKey=P2TR(bob_key.pubkey[1:], []).get_tr_info().scriptPubKey
)
]
out_instances = inst("bob_reveal", bob_signer, outputs)(
x=bob_trace[5],
h_y_a=h_a[6]
)
assert len(out_instances) == 0
report.write("Fraud proof", format_tx_markdown(inst.spending_tx, "Leaf reveal"))