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.

305 lines
9.1 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