-
Notifications
You must be signed in to change notification settings - Fork 8
/
ipa_bulletproof_pcs.py
387 lines (304 loc) · 13.2 KB
/
ipa_bulletproof_pcs.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
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
#!/usr/bin/env python3
# WARNING: This implementation may contain bugs and has not been audited.
# It is only for educational purposes. DO NOT use it in production.
from pypcs.curve import Fp, Fr, ec_mul, G1Point
from merlin.merlin_transcript import MerlinTranscript
from pedersen import PedersenCommitment
# WARNING:
# 1. For demonstration, we deliberately use an insecure random number
# generator, random.Random. To generate secure randomness, use
# the standard library `secrets` instead.
#
# See more here: https://docs.python.org/3/library/secrets.html
#
# 2. Challenges are only 1 byte long for simplicity, which is not secure.
import random
# Implementation of the BulletproofIPA PCS from the following paper:
# Bulletproofs: https://eprint.iacr.org/2017/1066.pdf
#
# The implementation of ZK is slightly different from the original
# paper. The original paper adds a Vector Schnorr protocol on the
# outer layer, while this implementation uses the method from the
# Hyrax paper, adding two blinders in each recursive round,
# and then using a Schnorr protocol (in the inner layer) to open
# in the final open phase.
#
# Hyrax: https://eprint.iacr.org/2017/1132.pdf
# TODO:
# - add options for the blinders
# - add batch proving and verifying
IPA_Argument = tuple[int,G1Point, G1Point, G1Point, list[Fr]]
class IPA_PCS:
pcs: PedersenCommitment
def __init__(self, pcs: PedersenCommitment):
"""
Args:
pcs: the PedersenCommitment instance to use for the proof
"""
self.pcs = pcs
self.rnd_gen = random.Random("ipa-pcs")
def commit(self, vec_c: list[Fr]) -> tuple[G1Point, Fr]:
"""
Commit to a vector of coefficients.
"""
blinder = Fr.rand()
return self.pcs.commit(vec_c, blinder), blinder
def inner_product_prove(self, \
a_cm: G1Point, vec_b: list[Fr], c: Fr, vec_a: list[Fr], rho_a: Fr, \
tr: MerlinTranscript,
debug=False) \
-> IPA_Argument:
"""
Prove an inner product of two vectors is correct.
<(a0, a1,...,a_{n-1}), (b0, b1,...,b_{n-1})> = c
Args:
a_cm: the commitment to the vector a
vec_b: the vector b
c: the challenge scalar
vec_a: the vector a
rho_a: the blinding factor for the commitment to vec_a
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
Returns:
an IPA_Argument tuple
"""
n = len(vec_a)
assert len(self.pcs.pp[0]) >= 2 * n + 1, f"EROR: len(pcs.pp) = {len(self.pcs.pp[0])}, while len(vec_a) = {len(vec_a)}"
assert len(vec_b) == n, f"EROR: len(vec_b) = {len(vec_b)}, while len(vec_a) = {len(vec_a)}"
if debug:
print(f"prove> n: {n}")
rng = random.Random(b"schnorr-1folding-commit")
G = self.pcs.pp[0][:n]
U = self.pcs.pp[0][-1]
H = self.pcs.pp[1]
tr.append_message(b"a_cm", str(a_cm).encode())
tr.append_message(b"vec_b", str(vec_b).encode())
tr.append_message(b"c", str(c).encode())
# Round 1: gamma <~ Fr
# WARN: challenge should be 32 bytes long, here we use 1 byte for debugging
gamma = Fr.from_bytes(tr.challenge_bytes(b"gamma", 1))
Ugamma = ec_mul(U, gamma)
P = a_cm + ec_mul(Ugamma, c)
PLR = []
rho = rho_a
# Round 2: PL, PR, ->
round = 0
half = n // 2
while half > 0:
if debug:
print(f"prove> round: {round}")
round += 1
G1 = G[:half]
G2 = G[half:]
as1 = vec_a[:half]
as2 = vec_a[half:]
bs1 = vec_b[:half]
bs2 = vec_b[half:]
rho_L, rho_R = Fr.rands(rng, 2)
PL = self.pcs.commit_with_pp(G1, as2) + ec_mul(Ugamma, ipa(as2, bs1)) + ec_mul(H, rho_L)
PR = self.pcs.commit_with_pp(G2, as1) + ec_mul(Ugamma, ipa(as1, bs2)) + ec_mul(H, rho_R)
PLR.insert(0, (PL, PR))
tr.append_message(b"PL", str(PL).encode())
tr.append_message(b"PR", str(PR).encode())
# Round 3: mu <~ Fr
mu = Fr.from_bytes(tr.challenge_bytes(b"mu", 1))
if debug:
print(f"prove> mu: {mu}")
vec_a = [as1[i] + as2[i] * mu for i in range(half)]
vec_b = [bs1[i] + bs2[i] * mu.inv() for i in range(half)]
rho += rho_L * mu + rho_R * mu.inv()
G = [G1[i] + ec_mul(G2[i], mu.inv()) for i in range(half)]
# Debug
if debug:
lhs = self.pcs.commit_with_pp(G, vec_a) + ec_mul(Ugamma, ipa(vec_a, vec_b)) + ec_mul(H, rho)
P += ec_mul(PL, mu) + ec_mul(PR, mu.inv())
if lhs == P:
print(f"prove> [vec_a]_(G) + [<vec_a, vec_b>]_(H) == P + mu*PL + mu^(-1)*PR ok ")
else:
print(f"prove> [vec_a]_(G) + [<vec_a, vec_b>]_(H) == P + mu*PL + mu^(-1)*PR failed ")
half = half // 2
assert len(vec_a) == len(vec_b) == len(G) == 1, "EROR: len(vec_a) and len(vec_b) should be 1"
a0 = vec_a[0]
b0 = vec_b[0]
G0 = G[0]
# Round 4: R ->
G_new = G0 + ec_mul(Ugamma, b0)
r, rho_r = Fr.rands(rng, 2)
R = ec_mul(G_new, r) + ec_mul(H, rho_r)
tr.append_message(b"R", str(R).encode())
# Round 5: zeta <~ Fr
zeta = Fr.from_bytes(tr.challenge_bytes(b"zeta", 1))
if debug:
print(f"prove> zeta: {zeta}")
# Round 6: z ->
z = r + zeta * a0
z_r = rho_r + zeta * rho
# Debug
if debug:
lhs = self.pcs.commit_with_pp([G_new], [z])
rhs = P + ec_mul(G_new, r) + ec_mul(P, zeta)
if lhs == rhs:
print(f"prove> [z]_(G_new) == pcs.commit_with_pp(G, vec_z) ok ")
else:
print(f"prove> Z == pcs.commit_with_pp(G, vec_z) failed ")
return (n, PLR, R, z, z_r)
def inner_product_verify(self, a_cm: G1Point, vec_b: Fr, c: Fr, arg: IPA_Argument, tr: MerlinTranscript, debug=False) -> bool:
"""
Verify an inner product argument.
<(a0, a1,...,a_{n-1}), (b0, b1,...,b_{n-1})> = c
Args:
a_cm: the commitment to the vector a
vec_b: the vector b
c: the challenge scalar
arg: the IPA_UNI_Argument (proof transcript)
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
"""
n, PLR, R, z, z_r = arg
tr.append_message(b"a_cm", str(a_cm).encode())
tr.append_message(b"vec_b", str(vec_b).encode())
tr.append_message(b"c", str(c).encode())
G = self.pcs.pp[0][:n]
U = self.pcs.pp[0][-1]
H = self.pcs.pp[1]
# Round 1: gamma <~ Fr
# WARN: challenge should be 32 bytes long, here we use 1 byte for debugging
gamma = Fr.from_bytes(tr.challenge_bytes(b"gamma", 1))
if debug:
print(f"verify> gamma: {gamma}")
Ugamma = ec_mul(U, gamma)
P = a_cm + ec_mul(Ugamma, c)
# Round 2: PL, PR, ->
round = 0
half = n // 2
while half > 0:
PL, PR = PLR.pop()
tr.append_message(b"PL", str(PL).encode())
tr.append_message(b"PR", str(PR).encode())
# Round 3: mu <~ Fr
mu = Fr.from_bytes(tr.challenge_bytes(b"mu", 1))
if debug:
print(f"verify> mu: {mu}")
G1 = G[:half]
G2 = G[half:]
bs1 = vec_b[:half]
bs2 = vec_b[half:]
G = [G1[i] + ec_mul(G2[i], mu.inv()) for i in range(half)]
vec_b = [bs1[i] + mu.inv() * bs2[i] for i in range(half)]
# print(f"verify> G[{round}]: {G}")
# Z_1 ?= Z + x * AL + x^{-1} * AR
P += ec_mul(PL, mu) + ec_mul(PR, mu.inv())
half = half // 2
round += 1
assert len(G) == len(vec_b) == 1, "EROR: len(vec_a) and len(vec_b) should be 1"
G0 = G[0]
b0 = vec_b[0]
# Round 4: R ->
tr.append_message(b"R", str(R).encode())
# Round 5: zeta <~ Fr
zeta = Fr.from_bytes(tr.challenge_bytes(b"zeta", 1))
if debug:
print(f"verify> zeta: {zeta}")
# Round 6: z ->
G_new = G0 + ec_mul(Ugamma, b0)
rhs = R + ec_mul(P, zeta)
lhs = self.pcs.commit_with_pp([G_new, H], [z, z_r])
return lhs == rhs
def univariate_poly_eval_prove(self, \
f_cm: G1Point, x: Fr, y: Fr, coeffs: list[Fr], rho: Fr, tr: MerlinTranscript, debug=False) \
-> IPA_Argument:
"""
Prove that a polynomial f(x) = y.
f(X) = c0 + c1 * X + c2 * X^2 + ... + cn * X^n
Args:
f_cm: the commitment to the polynomial f(X)
x: the challenge point
y: the evaluation of f(x)
vec_c: the coeffcient vector of the polynomial f(X)
rho_c: the blinding factor for the commitment to vec_c
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
Returns:
an IPA_PCS_Argument tuple
"""
n = len(coeffs)
vec_x = [x**i for i in range(n)]
arg = self.inner_product_prove(f_cm, vec_x, y, coeffs, rho, tr, debug)
return arg
def univariate_poly_eval_verify(self, f_cm: G1Point, x: Fr, y: Fr, arg: IPA_Argument, tr: MerlinTranscript, debug=False) -> bool:
"""
Verify an evaluation argument for a polynomial f, st. f(x) = y.
f(X) = c0 + c1 * X + c2 * X^2 + ... + cn * X^n
Args:
f_cm: the commitment to the polynomial f(X)
x: the challenge point
y: the evaluation of f(x)
arg: the IPA_PCS_Argument (proof transcript)
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
"""
n, PLR, R, z, z_r = arg
vec_x = [x**i for i in range(n)]
return self.inner_product_verify(f_cm, vec_x, y, arg, tr, debug)
def mle_poly_eval_prove(self, f_cm: G1Point, us: list[Fr], v: Fr, evals: list[Fr], rho: Fr, tr: MerlinTranscript, debug=False) -> IPA_Argument:
"""
Prove that an MLE polynomial f(u0, u1, ..., u_{n-1}) = v.
f(X0, X1, ..., X_{n-1}) = a0 * eq(0, Xs) + a1 * eq(1, Xs) + ... + a_{n-1} * eq(n-1, Xs)
Args:
f_cm: the commitment to the MLE polynomial f(X0, X1, ..., X_{n-1})
us: the evaluation point (u0, u1, ..., u_{n-1})
v: the evaluation of f(u0, u1, ..., u_{n-1})
evals: the evaluation of f(X0, X1, ..., X_{n-1}) over all 2^n points over the hypercube {0, 1}^n
rho_c: the blinding factor for the commitment to vec_c
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
Returns:
an IPA_PCS_Argument tuple
"""
n = len(evals)
arg = self.inner_product_prove(f_cm, us, v, evals, rho, tr, debug)
return arg
def mle_poly_eval_verify(self, \
f_cm: G1Point, us: list[Fr], v: Fr, arg: IPA_Argument, tr: MerlinTranscript, debug=False) \
-> bool:
"""
Verify an evaluation argument for a polynomial f, st. f(x) = y.
f(X0, X1, ..., X_{n-1}) = a0 * eq(0, Xs) + a1 * eq(1, Xs) + ... + a_{n-1} * eq(n-1, Xs)
Args:
f_cm: the commitment to the polynomial f(X)
us: the evaluation point (u0, u1, ..., u_{n-1})
v: the evaluation of f(u0, u1, ..., u_{n-1})
arg: the IPA_PCS_Argument (proof transcript)
tr: the Merlin transcript to use for the proof
debug: whether to print debug information
"""
n, PLR, R, z, z_r = arg
return self.inner_product_verify(f_cm, us, v, arg, tr, debug)
def ipa(vec_a: list[Fr], vec_b: list[Fr]) -> Fr:
n = len(vec_a)
assert len(vec_b) == n
return sum(a * b for a, b in zip(vec_a, vec_b))
def test_ipa_pcs():
# initialize the PedersenCommitment and the IPA_PCS
pcs = PedersenCommitment.setup(20)
ipa_pcs = IPA_PCS(pcs)
tr = MerlinTranscript(b"ipa-pcs")
# A simple instance f(x) = y
coeffs = [Fr(2), Fr(3), Fr(4), Fr(5), Fr(6), Fr(7), Fr(8), Fr(9)]
x = Fr(4)
vec_x = [x**i for i in range(len(coeffs))]
y = ipa(coeffs, vec_x)
# commit to the polynomial
rho = Fr.rand()
f_cm = pcs.commit_with_blinder(coeffs, rho)
# fork the transcript for both prover and verifier
tr_prover = tr.fork(b"prover")
tr_verifier = tr.fork(b"verifier")
# prover proves f(x) = y and sends an argument to the verifier
arg = ipa_pcs.univariate_poly_eval_prove(f_cm, x, y, coeffs, rho, tr_prover, debug=True)
print(f"arg: {arg}")
# verifier verifies the argument
verified = ipa_pcs.univariate_poly_eval_verify(f_cm, x, y, arg, tr_verifier, debug=True)
print(f"verified: {verified}")
if __name__ == "__main__":
test_ipa_pcs()