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], l, r, L, R
  154. def verify(self, P, a, v, x_powers, r, u, U, lj, rj, L, R):
  155. print("method verify()")
  156. # compute P' = P + [v] U
  157. P = P + int(v) * U
  158. s = build_s_from_us(u, self.d)
  159. b = inner_product_field(s, x_powers)
  160. G = inner_product_point(s, self.gs)
  161. # synthetic blinding factor
  162. # r' = r + ∑ ( lⱼ uⱼ² + rⱼ uⱼ⁻²)
  163. print(" synthetic blinding factor r' = r + ∑ ( lⱼ uⱼ² + rⱼ uⱼ⁻²)")
  164. r_ = r
  165. print(" r_ =", r_)
  166. # Q_0 = P' ⋅ ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
  167. print(" Q_0 = P' ⋅ ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)")
  168. Q_0 = P
  169. print(" Q_0 =", Q_0)
  170. for j in range(len(u)):
  171. print(" j =", j)
  172. u_ = u[j] # uⱼ
  173. u_inv = u[j]^(-1) # uⱼ⁻²
  174. # ∑ ( [uⱼ²] Lⱼ + [uⱼ⁻²] Rⱼ)
  175. Q_0 = Q_0 + int(u[j]^2) * L[j] + int(u_inv^2) * R[j]
  176. print(" Q_0 =", Q_0)
  177. r_ = r_ + lj[j] * (u_^2) + rj[j] * (u_inv^2)
  178. print(" r_ =", r_)
  179. Q_1 = int(a) * G + int(r_) * self.h + int(a * b)*U
  180. print(" Q_1", Q_1)
  181. # Q_1_ = int(a) * (G + int(b)*U) + int(r_) * self.h
  182. return Q_0 == Q_1
  183. # s = (
  184. # u₁⁻¹ u₂⁻¹ … uₖ⁻¹,
  185. # u₁ u₂⁻¹ … uₖ⁻¹,
  186. # u₁⁻¹ u₂ … uₖ⁻¹,
  187. # u₁ u₂ … uₖ⁻¹,
  188. # ⋮ ⋮ ⋮
  189. # u₁ u₂ … uₖ
  190. # )
  191. def build_s_from_us(u, d):
  192. k = int(math.log(d, 2))
  193. s = [1]*d
  194. t = d
  195. for j in reversed(range(k)):
  196. t = t/2
  197. c = 0
  198. for i in range(d):
  199. if c<t:
  200. s[i] = s[i] * u[j]^(-1)
  201. else:
  202. s[i] = s[i] * u[j]
  203. c = c+1
  204. if c>=t*2:
  205. c=0
  206. return s
  207. def powers_of(g, d):
  208. r = [None] * d
  209. for i in range(d):
  210. r[i] = g^i
  211. return r
  212. def multiples_of(g, d):
  213. r = [None] * d
  214. for i in range(d):
  215. r[i] = g*i
  216. return r
  217. def random_values(G, d):
  218. r = [None] * d
  219. for i in range(d):
  220. r[i] = G.random_element()
  221. return r
  222. def inner_product_field(a, b):
  223. assert len(a) == len(b)
  224. c = 0
  225. for i in range(len(a)):
  226. c = c + a[i] * b[i]
  227. return c
  228. def inner_product_point(a, b):
  229. assert len(a) == len(b)
  230. c = 0
  231. for i in range(len(a)):
  232. c = c + int(a[i]) * b[i]
  233. return c
  234. def vec_add(a, b):
  235. assert len(a) == len(b)
  236. return [x + y for x, y in zip(a, b)]
  237. def vec_mul(a, b):
  238. assert len(a) == len(b)
  239. return [x * y for x, y in zip(a, b)]
  240. def vec_scalar_mul_field(a, n):
  241. r = [None]*len(a)
  242. for i in range(len(a)):
  243. r[i] = a[i]*n
  244. return r
  245. def vec_scalar_mul_point(a, n):
  246. r = [None]*len(a)
  247. for i in range(len(a)):
  248. r[i] = a[i]*int(n)
  249. return r
  250. # Tests
  251. import unittest, operator
  252. # Ethereum elliptic curve
  253. p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
  254. a = 0
  255. b = 7
  256. Fp = GF(p)
  257. E = EllipticCurve(Fp, [a,b])
  258. GX = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
  259. GY = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
  260. g = E(GX,GY)
  261. n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
  262. h = 1
  263. q = g.order()
  264. Fq = GF(q)
  265. # simpler curve values
  266. # p = 19
  267. # Fp = GF(p)
  268. # E = EllipticCurve(Fp,[0,3])
  269. # g = E(1, 2)
  270. # q = g.order()
  271. # Fq = GF(q)
  272. print(E)
  273. print(Fq)
  274. assert is_prime(p)
  275. assert is_prime(q)
  276. assert g * q == 0
  277. class TestUtils(unittest.TestCase):
  278. def test_vecs(self):
  279. a = [1, 2, 3, 4, 5]
  280. b = [1, 2, 3, 4, 5]
  281. c = vec_scalar_mul_field(a, 10)
  282. assert c == [10, 20, 30, 40, 50]
  283. c = inner_product_field(a, b)
  284. assert c == 55
  285. # check that <a, b> with b = (1, x, x^2, ..., x^{d-1}) is the same
  286. # than evaluating p(x) with coefficients a_i, at x
  287. a = [Fq(1), Fq(2), Fq(3), Fq(4), Fq(5), Fq(6), Fq(7), Fq(8)]
  288. z = Fq(3)
  289. b = powers_of(z, 8)
  290. c = inner_product_field(a, b)
  291. x = PolynomialRing(Fq, 'x').gen()
  292. px = 1 + 2*x + 3*x^2 + 4*x^3 + 5*x^4 + 6*x^5 + 7*x^6 + 8*x^7
  293. assert c == px(x=z)
  294. class TestIPA_bulletproofs(unittest.TestCase):
  295. def test_inner_product_argument(self):
  296. d = 8
  297. ipa = IPA_bulletproofs(Fq, E, g, d)
  298. # prover
  299. # p(x) = 1 + 2x + 3x² + 4x³ + 5x⁴ + 6x⁵ + 7x⁶ + 8x⁷
  300. 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)]
  301. x = ipa.F(3)
  302. b = powers_of(x, ipa.d) # = b
  303. # prover
  304. P = ipa.commit(a, b)
  305. print("commit", P)
  306. v = ipa.evaluate(a, b)
  307. print("v", v)
  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_argument(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. # blinding factor
  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. # This might be obtained from the hash of the transcript
  357. # (Fiat-Shamir heuristic for non-interactive version)
  358. U = ipa.E.random_element()
  359. k = int(math.log(ipa.d, 2))
  360. u = [None] * k
  361. for j in reversed(range(0, k)):
  362. u[j] = ipa.F.random_element()
  363. while (u[j] == 0): # prevent u[j] from being 0
  364. u[j] = ipa.F.random_element()
  365. # prover
  366. a_ipa, lj, rj, L, R = ipa.ipa(a, x_powers, u, U)
  367. # verifier
  368. print("P", P)
  369. print("a_ipa", a_ipa)
  370. print("\n Verify:")
  371. verif = ipa.verify(P, a_ipa, v, x_powers, r, u, U, lj, rj, L, R)
  372. assert verif == True
  373. if __name__ == '__main__':
  374. unittest.main()