poly.sage 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. q = 0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001
  2. K = GF(q)
  3. a = K(0x00)
  4. b = K(0x05)
  5. E = EllipticCurve(K, (a, b))
  6. G = E(0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000, 0x02)
  7. p = 0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001
  8. assert E.order() == p
  9. F = GF(p)
  10. Poly.<X> = F[]
  11. k = 3
  12. n = 2^k
  13. x = F(88)
  14. px = (F(110) + F(56) * X + F(89) * X^2 + F(6543) * X^3
  15. + F(2) * X^4 + F(110) * X^5 + F(44) * X^6 + F(78) * X^7)
  16. assert px.degree() <= n
  17. base_G = [E.random_element(), E.random_element(), E.random_element(),
  18. E.random_element(), E.random_element(), E.random_element(),
  19. E.random_element(), E.random_element()]
  20. base_H = E.random_element()
  21. base_U = E.random_element()
  22. # Make the initial commitment to px
  23. blind = F.random_element()
  24. C = int(blind) * base_H + sum(int(k) * G for k, G in zip(px, base_G))
  25. # Dot product
  26. def dot(x, y):
  27. result = None
  28. for x_i, y_i in zip(x, y):
  29. if result is None:
  30. result = int(x_i) * y_i
  31. else:
  32. result += int(x_i) * y_i
  33. return result
  34. ## Step 2
  35. # Sample a random polynomial of degree n - 1
  36. s_poly = Poly([F.random_element() for _ in range(n)])
  37. # Polynomial should evaluate to 0 at x
  38. s_poly -= s_poly(x)
  39. assert s_poly(x) == 0
  40. ## Step 3
  41. # Commitment randomness
  42. s_poly_blind = F.random_element()
  43. ## Step 4
  44. s_poly_commitment = (int(s_poly_blind) * base_H
  45. + sum(int(k) * G for k, G in zip(s_poly, base_G)))
  46. ## Step 5
  47. iota = F.random_element()
  48. ## Step 8 (following Halo2 not BCSM20 order)
  49. z = F.random_element()
  50. ## Step 6
  51. final_poly = s_poly * iota + px
  52. ##############################
  53. # This code is not in BCSM20 #
  54. ##############################
  55. final_poly -= final_poly(x)
  56. assert final_poly(x) == 0
  57. ##############################
  58. ## Step 7
  59. blind = s_poly_blind * iota + blind
  60. # Step 8 creation of C' does not happen in Halo2 (see the notes
  61. # from "Comparison to other work")
  62. # Initialize the vectors in step 8
  63. a = list(final_poly)
  64. assert len(a) == n
  65. b = [x^i for i in range(n)]
  66. assert len(b) == len(a)
  67. assert dot(a, b) == final_poly(x)
  68. # Now loop from 3, 2, 1
  69. half_3 = 2^2
  70. assert half_3 * 2 == len(a) == len(b) == len(base_G)
  71. a_lo_4, a_hi_4 = a[:half_3], a[half_3:]
  72. b_lo_4, b_hi_4 = b[:half_3], b[half_3:]
  73. G_lo_4, G_hi_4 = base_G[:half_3], base_G[half_3:]
  74. l_3 = dot(a_hi_4, G_lo_4)
  75. r_3 = dot(a_lo_4, G_hi_4)
  76. value_l_3 = dot(a_hi_4, b_lo_4)
  77. value_r_3 = dot(a_lo_4, b_hi_4)
  78. l_randomness_3 = F.random_element()
  79. r_randomness_3 = F.random_element()
  80. l_3 += (int(value_l_3 * z) * base_U
  81. + int(l_randomness_3) * base_H)
  82. r_3 += (int(value_r_3 * z) * base_U
  83. + int(r_randomness_3) * base_H)
  84. challenge_3 = F.random_element()
  85. a_3 = [a_lo_4_i + challenge_3^-1 * a_hi_4_i
  86. for a_lo_4_i, a_hi_4_i in zip(a_lo_4, a_hi_4)]
  87. b_3 = [b_lo_4_i + challenge_3 * b_hi_4_i
  88. for b_lo_4_i, b_hi_4_i in zip(b_lo_4, b_hi_4)]
  89. G_3 = [G_lo_4_i + int(challenge_3) * G_hi_4_i
  90. for G_lo_4_i, G_hi_4_i in zip(G_lo_4, G_hi_4)]
  91. # Not in the paper
  92. blind += l_randomness_3 * challenge_3^-1
  93. blind += r_randomness_3 * challenge_3
  94. # k = 2
  95. half_2 = 2^1
  96. assert half_2 * 2 == len(a_3) == len(b_3) == len(G_3)
  97. a_lo_3, a_hi_3 = a_3[:half_2], a_3[half_2:]
  98. b_lo_3, b_hi_3 = b_3[:half_2], b_3[half_2:]
  99. G_lo_3, G_hi_3 = G_3[:half_2], G_3[half_2:]
  100. l_2 = dot(a_hi_3, G_lo_3)
  101. r_2 = dot(a_lo_3, G_hi_3)
  102. value_l_2 = dot(a_hi_3, b_lo_3)
  103. value_r_2 = dot(a_lo_3, b_hi_3)
  104. l_randomness_2 = F.random_element()
  105. r_randomness_2 = F.random_element()
  106. l_2 += (int(value_l_2 * z) * base_U
  107. + int(l_randomness_2) * base_H)
  108. r_2 += (int(value_r_2 * z) * base_U
  109. + int(r_randomness_2) * base_H)
  110. challenge_2 = F.random_element()
  111. a_2 = [a_lo_3_i + challenge_2^-1 * a_hi_3_i
  112. for a_lo_3_i, a_hi_3_i in zip(a_lo_3, a_hi_3)]
  113. b_2 = [b_lo_3_i + challenge_2 * b_hi_3_i
  114. for b_lo_3_i, b_hi_3_i in zip(b_lo_3, b_hi_3)]
  115. G_2 = [G_lo_3_i + int(challenge_2) * G_hi_3_i
  116. for G_lo_3_i, G_hi_3_i in zip(G_lo_3, G_hi_3)]
  117. blind += l_randomness_2 * challenge_2^-1
  118. blind += r_randomness_2 * challenge_2
  119. # k = 1
  120. half_1 = 2^0
  121. assert half_1 * 2 == len(a_2) == len(b_2) == len(G_2)
  122. a_lo_2, a_hi_2 = a_2[:half_1], a_2[half_1:]
  123. b_lo_2, b_hi_2 = b_2[:half_1], b_2[half_1:]
  124. G_lo_2, G_hi_2 = G_2[:half_1], G_2[half_1:]
  125. l_1 = dot(a_hi_2, G_lo_2)
  126. r_1 = dot(a_lo_2, G_hi_2)
  127. value_l_1 = dot(a_hi_2, b_lo_2)
  128. value_r_1 = dot(a_lo_2, b_hi_2)
  129. l_randomness_1 = F.random_element()
  130. r_randomness_1 = F.random_element()
  131. l_1 += (int(value_l_1 * z) * base_U
  132. + int(l_randomness_1) * base_H)
  133. r_1 += (int(value_r_1 * z) * base_U
  134. + int(r_randomness_1) * base_H)
  135. challenge_1 = F.random_element()
  136. a_1 = [a_lo_2_i + challenge_1^-1 * a_hi_2_i
  137. for a_lo_2_i, a_hi_2_i in zip(a_lo_2, a_hi_2)]
  138. b_1 = [b_lo_2_i + challenge_1 * b_hi_2_i
  139. for b_lo_2_i, b_hi_2_i in zip(b_lo_2, b_hi_2)]
  140. G_1 = [G_lo_2_i + int(challenge_1) * G_hi_2_i
  141. for G_lo_2_i, G_hi_2_i in zip(G_lo_2, G_hi_2)]
  142. blind += l_randomness_1 * challenge_1^-1
  143. blind += r_randomness_1 * challenge_1
  144. # Finished looping
  145. assert len(a_1) == 1
  146. a = a_1[0]