You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
14 KiB

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