groth_poly_commit.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # This file was *autogenerated* from the file groth_poly_commit.sage
  2. from sage.all_cmdline import * # import sage library
  3. _sage_const_0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001 = Integer(0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001); _sage_const_0x00 = Integer(0x00); _sage_const_0x05 = Integer(0x05); _sage_const_0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000 = Integer(0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000); _sage_const_0x02 = Integer(0x02); _sage_const_0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001 = Integer(0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001); _sage_const_1000 = Integer(1000); _sage_const_110 = Integer(110); _sage_const_2 = Integer(2); _sage_const_56 = Integer(56); _sage_const_89 = Integer(89); _sage_const_6543 = Integer(6543); _sage_const_77 = Integer(77)
  4. import numpy as np
  5. from collections import namedtuple
  6. PolyProof = namedtuple("PolyProof", [
  7. "poly_commit",
  8. "poly_blind_commit",
  9. "poly_response",
  10. "poly_blind_respond",
  11. "x_blind_factors",
  12. "evaluation_commits",
  13. "evaluation_response",
  14. "value"
  15. ])
  16. # Implementation of Groth09 inner product proof
  17. q = _sage_const_0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
  18. K = GF(q)
  19. a = K(_sage_const_0x00 )
  20. b = K(_sage_const_0x05 )
  21. E = EllipticCurve(K, (a, b))
  22. G = E(_sage_const_0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000 , _sage_const_0x02 )
  23. p = _sage_const_0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
  24. assert E.order() == p
  25. Scalar = GF(p)
  26. # Create some generator points. Normally we would use hash to curve.
  27. # All these points will be generators since the curve is a cyclic group
  28. H = E.random_element()
  29. G_vec = [E.random_element() for _ in range(_sage_const_1000 )]
  30. def dot_product(x, y):
  31. result = None
  32. for x_i, y_i in zip(x, y):
  33. if result is None:
  34. result = int(x_i) * y_i
  35. else:
  36. result += int(x_i) * y_i
  37. return result
  38. def create_proof(a, x):
  39. x = np.array([x**i for i in range(len(a))])
  40. # Evaluate the polynomial
  41. z = a.dot(x)
  42. assert len(a) == len(x)
  43. # We will now construct a proof
  44. # Commitments
  45. t = Scalar.random_element()
  46. r = Scalar.random_element()
  47. s = Scalar.random_element()
  48. C_z = int(t) * H + int(z) * G
  49. C_x = int(r) * H + dot_product(a, G_vec)
  50. C_y = int(s) * H + dot_product(x, G_vec)
  51. d_x = np.array([Scalar.random_element() for _ in range(len(x))])
  52. d_y = np.array([Scalar.random_element() for _ in range(len(x))])
  53. r_d = Scalar.random_element()
  54. s_d = Scalar.random_element()
  55. A_d = int(r_d) * H + dot_product(d_x, G_vec)
  56. B_d = int(s_d) * H + dot_product(d_y, G_vec)
  57. # (cx + d_x)(cy + d_y) = d_x d_y + c(x d_y + y d_x) + c^2 xy
  58. t_0 = Scalar.random_element()
  59. t_1 = Scalar.random_element()
  60. C_0 = int(t_0) * H + int(d_x.dot(d_y)) * G
  61. C_1 = int(t_1) * H + int(a.dot(d_y) + x.dot(d_x)) * G
  62. # Challenge
  63. # Using the Fiat-Shamir transform, we would hash the transcript
  64. #c = Scalar.random_element()
  65. c = _sage_const_110
  66. # Responses
  67. f_x = c * a + d_x
  68. f_y = c * x + d_y
  69. r_x = c * r + r_d
  70. s_y = c * s + s_d
  71. t_z = c**_sage_const_2 * t + c * t_1 + t_0
  72. # Verify
  73. #B_d = int(s_d) * H + dot_product(d_y, G_vec)
  74. #C_y = int(s) * H + dot_product(x, G_vec)
  75. assert int(c) * C_x + A_d == int(r_x) * H + dot_product(f_x, G_vec)
  76. assert int(c) * C_y + B_d == int(s_y) * H + dot_product(f_y, G_vec)
  77. # Actual inner product check
  78. # Comm(f_x f_y) == e^2 C_z + c Comm(x d_y + y d_x) + Comm(d_x d_y)
  79. assert int(t_z) * H + int(f_x.dot(f_y)) * G == int(c**_sage_const_2 ) * C_z + int(c) * C_1 + C_0
  80. return PolyProof(
  81. poly_commit=C_x,
  82. poly_blind_commit=A_d,
  83. poly_response=f_x,
  84. poly_blind_respond=r_x,
  85. x_blind_factors=(s_d, d_y, s),
  86. evaluation_commits=(C_0, C_1, C_z),
  87. evaluation_response=t_z,
  88. value=z
  89. )
  90. def verify_proof(proof, x):
  91. C_x = proof.poly_commit
  92. A_d = proof.poly_blind_commit
  93. f_x = proof.poly_response
  94. r_x = proof.poly_blind_respond
  95. (s_d, d_y, s) = proof.x_blind_factors
  96. (C_0, C_1, C_z) = proof.evaluation_commits
  97. t_z = proof.evaluation_response
  98. z = proof.value
  99. x = np.array([x**i for i in range(len(a))])
  100. c = _sage_const_110
  101. f_y = c * x + d_y
  102. s_y = c * s + s_d
  103. B_d = int(s_d) * H + dot_product(d_y, G_vec)
  104. C_y = int(s) * H + dot_product(x, G_vec)
  105. if int(c) * C_x + A_d != int(r_x) * H + dot_product(f_x, G_vec):
  106. return False
  107. if int(c) * C_y + B_d != int(s_y) * H + dot_product(f_y, G_vec):
  108. return False
  109. # Actual inner product check
  110. # Comm(f_x f_y) == e^2 C_z + c Comm(x d_y + y d_x) + Comm(d_x d_y)
  111. if int(t_z) * H + int(f_x.dot(f_y)) * G != int(c**_sage_const_2 ) * C_z + int(c) * C_1 + C_0:
  112. return False
  113. return True
  114. a = np.array([
  115. Scalar(_sage_const_110 ), Scalar(_sage_const_56 ), Scalar(_sage_const_89 ), Scalar(_sage_const_6543 ), Scalar(_sage_const_2 )
  116. ])
  117. xx = Scalar(_sage_const_77 )
  118. proof = create_proof(a, xx)
  119. assert verify_proof(proof, xx)