forked from 0xPARC/plonkathon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
330 lines (282 loc) · 13.7 KB
/
test.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
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
import pickle
from TESTING_verifier_DO_NOT_OPEN import TestingVerificationKey
from compiler.program import Program
from curve import G1Point
from poly import Basis, Polynomial
from setup import Setup
from prover import Prover
from verifier import VerificationKey
import json
from test.mini_poseidon import rc, mds, poseidon_hash
from utils import *
def setup_test():
print("===setup_test===")
setup = Setup.from_file("test/powersOfTau28_hez_final_11.ptau")
dummy_values = Polynomial(
list(map(Scalar, [1, 2, 3, 4, 5, 6, 7, 8])), Basis.LAGRANGE #TODO(keep), 采用点值法表示,w =19540430494807482326159819597004422086093766032135589407132600596362845576832, 多项式点的坐标分别为 (w^0,1),(w^1,2),(w^2,3),....(w^7,8)
)
program = Program(["c <== a * b"], 8) # 电路的阶=8
commitment = setup.commit(dummy_values)
assert commitment == G1Point(
(
16120260411117808045030798560855586501988622612038310041007562782458075125622,
3125847109934958347271782137825877642397632921923926105820408033549219695465,
)
)
vk = setup.verification_key(program.common_preprocessed_input())
assert (
vk.w
== 19540430494807482326159819597004422086093766032135589407132600596362845576832
)
print("Successfully created dummy commitment and verification key")
def basic_test():
print("===basic_test===")
# Extract 2^28 powers of tau
setup = Setup.from_file("test/powersOfTau28_hez_final_11.ptau")
print("Extracted setup")
program = Program(["c <== a * b"], 8)
vk = setup.verification_key(program.common_preprocessed_input())
print("Generated verification key")
their_output = json.load(open("test/main.plonk.vkey.json"))
for key in ("Qm", "Ql", "Qr", "Qo", "Qc", "S1", "S2", "S3", "X_2"):
if interpret_json_point(their_output[key]) != getattr(vk, key):
raise Exception(
"Mismatch {}: ours {} theirs {}".format(
key, getattr(vk, key), their_output[key]
)
)
assert getattr(vk, "w") == int(their_output["w"])
print("Basic test success")
return setup
# Equivalent to this zkrepl code:
#
# template Example () {
# signal input a;
# signal input b;
# signal c;
# c <== a * b + a;
# }
def ab_plus_a_test(setup):
print("===ab_plus_a_test===")
program = Program(["ab === a - c", "-ab === a * b"], 8)
vk = setup.verification_key(program.common_preprocessed_input())
print("Generated verification key")
their_output = json.load(open("test/main.plonk.vkey-58.json"))
for key in ("Qm", "Ql", "Qr", "Qo", "Qc", "S1", "S2", "S3", "X_2"):
if interpret_json_point(their_output[key]) != getattr(vk, key):
raise Exception(
"Mismatch {}: ours {} theirs {}".format(
key, getattr(vk, key), their_output[key]
)
)
assert getattr(vk, "w") == int(their_output["w"])
print("ab+a test success")
def one_public_input_test(setup):
print("===one_public_input_test===")
program = Program(["c public", "c === a * b"], 8)
vk = setup.verification_key(program.common_preprocessed_input())
print("Generated verification key")
their_output = json.load(open("test/main.plonk.vkey-59.json"))
for key in ("Qm", "Ql", "Qr", "Qo", "Qc", "S1", "S2", "S3", "X_2"):
if interpret_json_point(their_output[key]) != getattr(vk, key):
raise Exception(
"Mismatch {}: ours {} theirs {}".format(
key, getattr(vk, key), their_output[key]
)
)
assert getattr(vk, "w") == int(their_output["w"])
print("One public input test success")
def prover_test_dummy_verifier(setup):
print("===prover_test_dummy_verifier===")
print("Beginning prover test with test verifier")
program = Program(["e public", "c <== a * b", "e <== c * d"], 8)
assignments = {"a": 3, "b": 4, "c": 12, "d": 5, "e": 60}
prover = Prover(setup, program)
proof = prover.prove(assignments)
print("Beginning test verification")
program = Program(["e public", "c <== a * b", "e <== c * d"], 8)
public = [60]
vk = setup.verification_key(program.common_preprocessed_input())
vk_test = TestingVerificationKey(
group_order=vk.group_order,
Qm=vk.Qm,
Ql=vk.Ql,
Qr=vk.Qr,
Qo=vk.Qo,
Qc=vk.Qc,
S1=vk.S1,
S2=vk.S2,
S3=vk.S3,
X_2=vk.X_2,
w=vk.w,
)
assert vk_test.verify_proof_unoptimized(8, proof, public)
assert vk_test.verify_proof(8, proof, public)
print("Prover test with dummy verifier success")
def prover_test(setup):
print("===prover_test===")
print("Beginning prover test")
program = Program(["e public", "c <== a * b", "e <== c * d"], 8)
assignments = {"a": 3, "b": 4, "c": 12, "d": 5, "e": 60}
prover = Prover(setup, program)
proof = prover.prove(assignments)
print("Prover test success")
return proof
def verifier_test_unoptimized(setup, proof):
print("===verifier_test_unoptimized===")
print("Beginning verifier test")
program = Program(["e public", "c <== a * b", "e <== c * d"], 8)
public = [60]
vk = setup.verification_key(program.common_preprocessed_input())
assert vk.verify_proof_unoptimized(8, proof, public)
print("Verifier test success")
def verifier_test_full(setup, proof):
print("===verifier_test_full===")
print("Beginning verifier test")
program = Program(["e public", "c <== a * b", "e <== c * d"], 8)
public = [60]
vk = setup.verification_key(program.common_preprocessed_input())
assert vk.verify_proof_unoptimized(8, proof, public)
assert vk.verify_proof(8, proof, public)
print("Verifier test success")
def factorization_test(setup):
print("===factorization_test===")
print("Beginning test: prove you know small integers that multiply to 91")
program = Program.from_str(
"""n public
pb0 === pb0 * pb0
pb1 === pb1 * pb1
pb2 === pb2 * pb2
pb3 === pb3 * pb3
qb0 === qb0 * qb0
qb1 === qb1 * qb1
qb2 === qb2 * qb2
qb3 === qb3 * qb3
pb01 <== pb0 + 2 * pb1
pb012 <== pb01 + 4 * pb2
p <== pb012 + 8 * pb3
qb01 <== qb0 + 2 * qb1
qb012 <== qb01 + 4 * qb2
q <== qb012 + 8 * qb3
n === p * q""",
16,
)
public = [91]
print("input",program.common_preprocessed_input())
vk = setup.verification_key(program.common_preprocessed_input())
print("vk",vk)
print("Generated verification key")
assignments = program.fill_variable_assignments(
{
"pb3": 1,
"pb2": 1,
"pb1": 0,
"pb0": 1,
"qb3": 0,
"qb2": 1,
"qb1": 1,
"qb0": 1,
}
)
prover = Prover(setup, program)
proof = prover.prove(assignments)
print("Generated proof")
assert vk.verify_proof(16, proof, public)
print("Factorization test success!")
def output_proof_lang() -> str:
o = []
o.append("L0 public")
o.append("M0 public")
o.append("M64 public")
o.append("R0 <== 0")
for i in range(64):
for j, pos in enumerate(("L", "M", "R")):
f = {"x": i, "r": rc[i][j], "p": pos}
if i < 4 or i >= 60 or pos == "L":
o.append("{p}adj{x} <== {p}{x} + {r}".format(**f))
o.append("{p}sq{x} <== {p}adj{x} * {p}adj{x}".format(**f))
o.append("{p}qd{x} <== {p}sq{x} * {p}sq{x}".format(**f))
o.append("{p}qn{x} <== {p}qd{x} * {p}adj{x}".format(**f))
else:
o.append("{p}qn{x} <== {p}{x} + {r}".format(**f))
for j, pos in enumerate(("L", "M", "R")):
f = {"x": i, "p": pos, "m": mds[j]}
o.append("{p}suma{x} <== Lqn{x} * {m}".format(**f))
f = {"x": i, "p": pos, "m": mds[j + 1]}
o.append("{p}sumb{x} <== {p}suma{x} + Mqn{x} * {m}".format(**f))
f = {"x": i, "xp1": i + 1, "p": pos, "m": mds[j + 2]}
o.append("{p}{xp1} <== {p}sumb{x} + Rqn{x} * {m}".format(**f))
return "\n".join(o)
def poseidon_test(setup):
print("===poseidon_test===")
# PLONK-prove the correctness of a Poseidon execution. Note that this is
# a very suboptimal way to do it: an optimized implementation would use
# a custom PLONK gate to do a round in a single gate
expected_value = poseidon_hash(1, 2)
# Generate code for proof
program = Program.from_str(output_proof_lang(), 1024)
print("Generated code for Poseidon test")
assignments = program.fill_variable_assignments({"L0": 1, "M0": 2})
vk = setup.verification_key(program.common_preprocessed_input())
print("Generated verification key")
prover = Prover(setup, program)
proof = prover.prove(assignments)
print("Generated proof")
assert vk.verify_proof(1024, proof, [1, 2, expected_value])
print("Verified proof!")
# 检查当阶数=2^2 ~ 2^28时,均满足w[1] * w[-1] = 1
def root_of_unity_test():
for i in range(2,28):
order = 2 ** i
roots = Scalar(1).roots_of_unity(order)
val = roots[1] * roots[-1]
# print(f"{i}'s root of unity:{roots}\n", i, roots)
print(f"{i}'s roots[1] * roots[-1]={val}")
def root_of_unity_test1():
roots8 = Scalar(1).roots_of_unity(8)
roots32 = Scalar(1).roots_of_unity(32)
# roots8:[1, 19540430494807482326159819597004422086093766032135589407132600596362845576832, 21888242871839275217838484774961031246007050428528088939761107053157389710902, 13274704216607947843011480449124596415239537050559949017414504948711435969894, 21888242871839275222246405745257275088548364400416034343698204186575808495616, 2347812377031792896086586148252853002454598368280444936565603590212962918785, 4407920970296243842541313971887945403937097133418418784715, 8613538655231327379234925296132678673308827349856085326283699237864372525723]
# roots32:[1, 4419234939496763621076330863786513495701855246241724391626358375488475697872, 14940766826517323942636479241147756311199852622225275649687664389641784935947, 8353089677377103612376907029239831201621163137110616679113215703556701300027, 19540430494807482326159819597004422086093766032135589407132600596362845576832, 21593175090660679728966189540082956087710442206243643852421469785983375007422, 7453743110195651009871841175551411207906567694170420694440975759997908783171, 398060900184764123111996659293386330445164342166284510961681463198684035472, 21888242871839275217838484774961031246007050428528088939761107053157389710902, 15480425210935858833842661136375613442295926160997485829640439761218028937032, 15634706786522089014999940912207647497621112715300598509090847765194894752723, 9222527969605388450625148037496647087331675164191659244434925070698893435503, 13274704216607947843011480449124596415239537050559949017414504948711435969894, 10550721784764313104495045260998680866741519845912303749987955721122349694799, 20580681596408674675161806693190042586237586932987042748222592033583012763427, 4183653929190742691274098379026487729755080010366834215927449156672627370084, 21888242871839275222246405745257275088548364400416034343698204186575808495616, 17469007932342511601170074881470761592846509154174309952071845811087332797745, 6947476045321951279609926504109518777348511778190758694010539796934023559670, 13535153194462171609869498716017443886927201263305417664584988483019107195590, 2347812377031792896086586148252853002454598368280444936565603590212962918785, 295067781178595493280216205174319000837922194172390491276734400592433488195, 14434499761643624212374564569705863880641796706245613649257228426577899712446, 21490181971654511099134409085963888758103200058249749832736522723377124460145, 4407920970296243842541313971887945403937097133418418784715, 6407817660903416388403744608881661646252438239418548514057764425357779558585, 6253536085317186207246464833049627590927251685115435834607356421380913742894, 12665714902233886771621257707760628001216689236224375099263279115876915060114, 8613538655231327379234925296132678673308827349856085326283699237864372525723, 11337521087074962117751360484258594221806844554503730593710248465453458800818, 1307561275430600547084599052067232502310777467428991595475612152992795732190, 17704588942648532530972307366230787358793284390049200127770755029903181125533]
# roots32[1]^4:19540430494807482326159819597004422086093766032135589407132600596362845576832
print(f"roots8:{roots8}")
print(f"roots32:{roots32}")
print(f"roots32[1]^4:{roots32[1] ** 4}")
def lincom_test():
numbers = [1,3]
factors = [1,2,4,8]
bitlens = [len(bin(f)) - 2 for f in factors]
maxbitlen = max(bitlens)
subsets = []
for j in range(maxbitlen + 1):
subset = set() # 创建一个空的集合{}
for i in range(len(numbers)):
if factors[i] & (1 << j):
subset.add(i)
subsets.append(subset)
print(f"subset:{subset}")
print(f"subsets:{subsets}")
if __name__ == "__main__":
# root_of_unity_test()
# root_of_unity_test1()
#lincom_test()
# Step 1: Pass setup test
setup_test()
#
setup = basic_test()
#
# # Step 2: Pass prover test using verifier we provide (DO NOT READ TEST VERIFIER CODE)
prover_test_dummy_verifier(setup)
#
# Step 3: Pass verifier test using your own verifier
# with open("test/proof.pickle", "rb") as f:
# proof = pickle.load(f)
# verifier_test_unoptimized(setup, proof)
# verifier_test_full(setup, proof)
#
# Step 4: Pass end-to-end tests for prover and verifier
ab_plus_a_test(setup)
one_public_input_test(setup)
proof = prover_test(setup)
verifier_test_full(setup, proof)
factorization_test(setup)
poseidon_test(setup)