math/ipa.sage
ipa.sageRaw
1# This file contains two Inner Product Argument implementations:
2# - Bulletproofs version: https://eprint.iacr.org/2017/1066.pdf
3# - Halo version: https://eprint.iacr.org/2019/1021.pdf
4
5
6
7# IPA_bulletproofs implements the IPA version from the Bulletproofs paper: https://eprint.iacr.org/2017/1066.pdf
8# https://doc-internal.dalek.rs/bulletproofs/notes/inner_product_proof/index.html
9class IPA_bulletproofs:
10 def __init__(self, F, E, g, d):
11 self.g = g
12 self.F = F
13 self.E = E
14 self.d = d
15 # TODO:
16 # Setup:
17 self.h = E.random_element() # TMP
18 self.gs = random_values(E, d)
19 self.hs = random_values(E, d)
20
21 # a: aᵢ ∈ 𝔽 coefficients of p(X)
22 # r: blinding factor
23 def commit(self, a, b):
24 P = inner_product_point(a, self.gs) + inner_product_point(b, self.hs)
25 return P
26
27 def evaluate(self, a, x_powers):
28 return inner_product_field(a, x_powers)
29
30 def ipa(self, a_, b_, u, U):
31 G = self.gs
32 H = self.hs
33 a = a_
34 b = b_
35
36 k = int(math.log(self.d, 2))
37 L = [None] * k
38 R = [None] * k
39
40 for j in reversed(range(0, k)):
41 m = len(a)/2
42 a_lo = a[:m]
43 a_hi = a[m:]
44 b_lo = b[:m]
45 b_hi = b[m:]
46 H_lo = H[:m]
47 H_hi = H[m:]
48 G_lo = G[:m]
49 G_hi = G[m:]
50
51 # Lⱼ = <a'ₗₒ, G'ₕᵢ> + [lⱼ] H + [<a'ₗₒ, b'ₕᵢ>] U
52 L[j] = inner_product_point(a_lo, G_hi) + inner_product_point(b_hi, H_lo) + int(inner_product_field(a_lo, b_hi)) * U
53 # Rⱼ = <a'ₕᵢ, G'ₗₒ> + [rⱼ] H + [<a'ₕᵢ, b'ₗₒ>] U
54 R[j] = inner_product_point(a_hi, G_lo) + inner_product_point(b_lo, H_hi) + int(inner_product_field(a_hi, b_lo)) * U
55
56 # use the random challenge uⱼ ∈ 𝕀 generated by the verifier
57 u_ = u[j] # uⱼ
58 u_inv = u[j]^(-1) # uⱼ⁻¹
59
60 a = vec_add(vec_scalar_mul_field(a_lo, u_), vec_scalar_mul_field(a_hi, u_inv))
61 b = vec_add(vec_scalar_mul_field(b_lo, u_inv), vec_scalar_mul_field(b_hi, u_))
62 G = vec_add(vec_scalar_mul_point(G_lo, u_inv), vec_scalar_mul_point(G_hi, u_))
63 H = vec_add(vec_scalar_mul_point(H_lo, u_), vec_scalar_mul_point(H_hi, u_inv))
64
65 assert len(a)==1
66 assert len(b)==1
67 assert len(G)==1
68 assert len(H)==1
69 # a, b, G have length=1
70 # L, R are the "cross-terms" of the inner product
71 return a[0], b[0], G[0], H[0], L, R
72
73 def verify(self, P, a, v, x_powers, u, U, L, R, b_ipa, G_ipa, H_ipa):
74 b = b_ipa
75 G = G_ipa
76 H = H_ipa
77
78 # Q_0 = P' ⋅ ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
79 C = P
80 for j in range(len(L)):
81 u_ = u[j] # uⱼ
82 u_inv = u[j]^(-1) # uⱼ⁻²
83
84 # ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
85 C = C + int(u_^2) * L[j] + int(u_inv^2) * R[j]
86
87 D = int(a) * G + int(b) * H + int(a * b)*U
88
89 return C == D
90
91# IPA_halo implements the modified IPA from the Halo paper: https://eprint.iacr.org/2019/1021.pdf
92class IPA_halo:
93 def __init__(self, F, E, g, d):
94 self.g = g
95 self.F = F
96 self.E = E
97 self.d = d
98
99 self.h = E.random_element() # TMP
100 self.gs = random_values(E, d)
101 self.hs = random_values(E, d)
102 # print(" h=", self.h)
103 # print(" G=", self.gs)
104 # print(" H=", self.hs)
105
106 def commit(self, a, r):
107 P = inner_product_point(a, self.gs) + r * self.h
108 return P
109
110 def evaluate(self, a, x_powers):
111 return inner_product_field(a, x_powers)
112
113 def ipa(self, a_, x_powers, u, U): # prove
114 print(" method ipa():")
115 G = self.gs
116 a = a_
117 b = x_powers
118
119 k = int(math.log(self.d, 2))
120 l = [None] * k
121 r = [None] * k
122 L = [None] * k
123 R = [None] * k
124
125 for j in reversed(range(0, k)):
126 print(" j =", j)
127 print(" len(a) = n =", len(a))
128 print(" m = n/2 =", len(a)/2)
129 m = len(a)/2
130 a_lo = a[:m]
131 a_hi = a[m:]
132 b_lo = b[:m]
133 b_hi = b[m:]
134 G_lo = G[:m]
135 G_hi = G[m:]
136
137 print(" Split into a_lo,hi b_lo,hi, G_lo,hi:")
138 print(" a", a)
139 print(" a_lo", a_lo)
140 print(" a_hi", a_hi)
141 print(" b", b)
142 print(" b_lo", b_lo)
143 print(" b_hi", b_hi)
144 print(" G", G)
145 print(" G_lo", G_lo)
146 print(" G_hi", G_hi)
147
148 l[j] = self.F.random_element() # random blinding factor
149 r[j] = self.F.random_element() # random blinding factor
150 print(" random blinding factors:")
151 print(" l[j]", l[j])
152 print(" r[j]", r[j])
153
154 # Lⱼ = <a'ₗₒ, G'ₕᵢ> + [lⱼ] H + [<a'ₗₒ, b'ₕᵢ>] U
155 L[j] = inner_product_point(a_lo, G_hi) + int(l[j]) * self.h + int(inner_product_field(a_lo, b_hi)) * U
156 # Rⱼ = <a'ₕᵢ, G'ₗₒ> + [rⱼ] H + [<a'ₕᵢ, b'ₗₒ>] U
157 R[j] = inner_product_point(a_hi, G_lo) + int(r[j]) * self.h + int(inner_product_field(a_hi, b_lo)) * U
158
159 print(" Compute Lⱼ = <a'ₗₒ, G'ₕᵢ> + [lⱼ] H + [<a'ₗₒ, b'ₕᵢ>] U")
160 print(" L[j]", L[j])
161 print(" Compute Rⱼ = <a'ₕᵢ, G'ₗₒ> + [rⱼ] H + [<a'ₕᵢ, b'ₗₒ>] U")
162 print(" R[j]", R[j])
163
164 # use the random challenge uⱼ ∈ 𝕀 generated by the verifier
165 u_ = u[j] # uⱼ
166 u_inv = self.F(u[j])^(-1) # uⱼ⁻¹
167 print(" u_j", u_)
168 print(" u_j^-1", u_inv)
169
170 a = vec_add(vec_scalar_mul_field(a_lo, u_), vec_scalar_mul_field(a_hi, u_inv))
171 b = vec_add(vec_scalar_mul_field(b_lo, u_inv), vec_scalar_mul_field(b_hi, u_))
172 G = vec_add(vec_scalar_mul_point(G_lo, u_inv), vec_scalar_mul_point(G_hi, u_))
173 print(" new a, b, G")
174 print(" a =", a)
175 print(" b =", b)
176 print(" G =", G)
177
178 assert len(a)==1
179 assert len(b)==1
180 assert len(G)==1
181 # a, b, G have length=1
182 # l, r are random blinding factors
183 # L, R are the "cross-terms" of the inner product
184 return a[0], l, r, L, R
185
186 def verify(self, P, a, v, x_powers, r, u, U, lj, rj, L, R):
187 print("methid verify()")
188
189 s = build_s_from_us(u, self.d)
190 b = inner_product_field(s, x_powers)
191 G = inner_product_point(s, self.gs)
192
193 # synthetic blinding factor
194 # r' = r + ∑ ( lⱼ uⱼ² + rⱼ uⱼ⁻²)
195 print(" synthetic blinding factor r' = r + ∑ ( lⱼ uⱼ² + rⱼ uⱼ⁻²)")
196 r_ = r
197 print(" r_ =", r_)
198 # Q_0 = P' ⋅ ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
199 print(" Q_0 = P' ⋅ ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)")
200 Q_0 = P
201 print(" Q_0 =", Q_0)
202 for j in range(len(u)):
203 print(" j =", j)
204 u_ = u[j] # uⱼ
205 u_inv = u[j]^(-1) # uⱼ⁻²
206
207 # ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
208 Q_0 = Q_0 + int(u[j]^2) * L[j] + int(u_inv^2) * R[j]
209 print(" Q_0 =", Q_0)
210
211 r_ = r_ + lj[j] * (u_^2) + rj[j] * (u_inv^2)
212 print(" r_ =", r_)
213
214 Q_1 = int(a) * G + int(r_) * self.h + int(a * b)*U
215 print(" Q_1", Q_1)
216 # Q_1_ = int(a) * (G + int(b)*U) + int(r_) * self.h
217
218 return Q_0 == Q_1
219
220
221# s = (
222# u₁⁻¹ u₂⁻¹ … uₖ⁻¹,
223# u₁ u₂⁻¹ … uₖ⁻¹,
224# u₁⁻¹ u₂ … uₖ⁻¹,
225# u₁ u₂ … uₖ⁻¹,
226# ⋮ ⋮ ⋮
227# u₁ u₂ … uₖ
228# )
229def build_s_from_us(u, d):
230 k = int(math.log(d, 2))
231 s = [1]*d
232 t = d
233 for j in reversed(range(k)):
234 t = t/2
235 c = 0
236 for i in range(d):
237 if c<t:
238 s[i] = s[i] * u[j]^(-1)
239 else:
240 s[i] = s[i] * u[j]
241 c = c+1
242 if c>=t*2:
243 c=0
244
245 return s
246
247
248
249
250def powers_of(g, d):
251 r = [None] * d
252 for i in range(d):
253 r[i] = g^i
254 return r
255
256
257def multiples_of(g, d):
258 r = [None] * d
259 for i in range(d):
260 r[i] = g*i
261 return r
262
263def random_values(G, d):
264 r = [None] * d
265 for i in range(d):
266 r[i] = G.random_element()
267 return r
268
269def inner_product_field(a, b):
270 assert len(a) == len(b)
271 c = 0
272 for i in range(len(a)):
273 c = c + a[i] * b[i]
274
275 return c
276
277def inner_product_point(a, b):
278 assert len(a) == len(b)
279 c = 0
280 for i in range(len(a)):
281 c = c + int(a[i]) * b[i]
282
283 return c
284
285def vec_add(a, b):
286 assert len(a) == len(b)
287 return [x + y for x, y in zip(a, b)]
288
289def vec_mul(a, b):
290 assert len(a) == len(b)
291 return [x * y for x, y in zip(a, b)]
292
293def vec_scalar_mul_field(a, n):
294 r = [None]*len(a)
295 for i in range(len(a)):
296 r[i] = a[i]*n
297 return r
298
299def vec_scalar_mul_point(a, n):
300 r = [None]*len(a)
301 for i in range(len(a)):
302 r[i] = a[i]*int(n)
303 return r
304
305
306# Tests
307import unittest, operator
308
309# Ethereum elliptic curve
310p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
311a = 0
312b = 7
313Fp = GF(p)
314E = EllipticCurve(Fp, [a,b])
315GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
316GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
317g = E(GX,GY)
318n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
319h = 1
320q = g.order()
321Fq = GF(q)
322
323# simplier curve values
324# p = 19
325# Fp = GF(p)
326# E = EllipticCurve(Fp,[0,3])
327# g = E(1, 2)
328# q = g.order()
329# Fq = GF(q)
330
331print(E)
332print(Fq)
333assert is_prime(p)
334assert is_prime(q)
335assert g * q == 0
336
337class TestUtils(unittest.TestCase):
338 def test_vecs(self):
339 a = [1, 2, 3, 4, 5]
340 b = [1, 2, 3, 4, 5]
341
342 c = vec_scalar_mul_field(a, 10)
343 assert c == [10, 20, 30, 40, 50]
344
345 c = inner_product_field(a, b)
346 assert c == 55
347
348 # check that <a, b> with b = (1, x, x^2, ..., x^{d-1}) is the same
349 # than evaluating p(x) with coefficients a_i, at x
350 a = [Fq(1), Fq(2), Fq(3), Fq(4), Fq(5), Fq(6), Fq(7), Fq(8)]
351 z = Fq(3)
352 b = powers_of(z, 8)
353 c = inner_product_field(a, b)
354
355 x = PolynomialRing(Fq, 'x').gen()
356 px = 1 + 2*x + 3*x^2 + 4*x^3 + 5*x^4 + 6*x^5 + 7*x^6 + 8*x^7
357 assert c == px(x=z)
358
359
360class TestIPA_bulletproofs(unittest.TestCase):
361 def test_inner_product_argument(self):
362 d = 8
363 ipa = IPA_bulletproofs(Fq, E, g, d)
364
365 # prover
366 # p(x) = 1 + 2x + 3x² + 4x³ + 5x⁴ + 6x⁵ + 7x⁶ + 8x⁷
367 a = [ipa.F(1), ipa.F(2), ipa.F(3), ipa.F(4), ipa.F(5), ipa.F(6), ipa.F(7), ipa.F(8)]
368 x = ipa.F(3)
369 b = powers_of(x, ipa.d) # = b
370
371 # prover
372 P = ipa.commit(a, b)
373 print("commit", P)
374 v = ipa.evaluate(a, b)
375 print("v", v)
376
377 # verifier generate random challenges {uᵢ} ∈ 𝕀 and U ∈ 𝔾
378 U = ipa.E.random_element()
379 k = int(math.log(d, 2))
380 u = [None] * k
381 for j in reversed(range(0, k)):
382 u[j] = ipa.F.random_element()
383 while (u[j] == 0): # prevent u[j] from being 0
384 u[j] = ipa.F.random_element()
385
386 P = P + int(inner_product_field(a, b)) * U
387
388 # prover
389 a_ipa, b_ipa, G_ipa, H_ipa, L, R = ipa.ipa(a, b, u, U)
390
391 # verifier
392 print("P", P)
393 print("a_ipa", a_ipa)
394 verif = ipa.verify(P, a_ipa, v, b, u, U, L, R, b_ipa, G_ipa, H_ipa)
395 print("Verification:", verif)
396 assert verif == True
397
398
399class TestIPA_halo(unittest.TestCase):
400 def test_homomorphic_property(self):
401 ipa = IPA_halo(Fq, E, g, 5)
402
403 a = [1, 2, 3, 4, 5]
404 b = [1, 2, 3, 4, 5]
405 c = vec_add(a, b)
406 assert c == [2,4,6,8,10]
407
408 r = int(ipa.F.random_element())
409 s = int(ipa.F.random_element())
410 vc_a = ipa.commit(a, r)
411 vc_b = ipa.commit(b, s)
412
413 # com(a, r) + com(b, s) == com(a+b, r+s)
414 expected_vc_c = ipa.commit(vec_add(a, b), r+s)
415 vc_c = vc_a + vc_b
416 assert vc_c == expected_vc_c
417
418 def test_inner_product_argument(self):
419 d = 8
420 ipa = IPA_halo(Fq, E, g, d)
421
422 # prover
423 # p(x) = 1 + 2x + 3x² + 4x³ + 5x⁴ + 6x⁵ + 7x⁶ + 8x⁷
424 a = [ipa.F(1), ipa.F(2), ipa.F(3), ipa.F(4), ipa.F(5), ipa.F(6), ipa.F(7), ipa.F(8)]
425 x = ipa.F(3)
426 x_powers = powers_of(x, ipa.d) # = b
427
428 # blinding factor
429 r = int(ipa.F.random_element())
430
431 # prover
432 P = ipa.commit(a, r)
433 print("commit", P)
434 v = ipa.evaluate(a, x_powers)
435 print("v", v)
436
437 # verifier generate random challenges {uᵢ} ∈ 𝕀 and U ∈ 𝔾
438 # This might be obtained from the hash of the transcript
439 # (Fiat-Shamir heuristic for non-interactive version)
440 U = ipa.E.random_element()
441 k = int(math.log(ipa.d, 2))
442 u = [None] * k
443 for j in reversed(range(0, k)):
444 u[j] = ipa.F.random_element()
445 while (u[j] == 0): # prevent u[j] from being 0
446 u[j] = ipa.F.random_element()
447
448 P = P + int(v) * U
449
450 # prover
451 a_ipa, lj, rj, L, R = ipa.ipa(a, x_powers, u, U)
452
453 # verifier
454 print("P", P)
455 print("a_ipa", a_ipa)
456 print("\n Verify:")
457 verif = ipa.verify(P, a_ipa, v, x_powers, r, u, U, lj, rj, L, R)
458 assert verif == True
459
460
461if __name__ == '__main__':
462 unittest.main()