3.6-trusted-setup.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from bls_py import bls12381
  2. from bls_py import pairing
  3. from bls_py import ec
  4. from bls_py.fields import Fq, Fq2, Fq6, Fq12, bls12381_q as Q
  5. import random
  6. import numpy as np
  7. # Section 3.6 from "Why and How zk-SNARK Works"
  8. def rand_scalar():
  9. return random.randrange(1, bls12381.q)
  10. #x = rand_scalar()
  11. #y = ec.y_for_x(x)
  12. g1 = ec.generator_Fq(bls12381)
  13. g2 = ec.generator_Fq2(bls12381)
  14. null = ec.AffinePoint(Fq(Q, 0), Fq(Q, 1), True, bls12381)
  15. assert g1 + null == g1
  16. null2 = ec.AffinePoint(Fq2.zero(Q), Fq2.zero(Q), True, bls12381)
  17. assert null2 + g2 == g2
  18. #################################
  19. # Verifier (trusted setup)
  20. #################################
  21. # samples a random value (a secret)
  22. s = rand_scalar()
  23. # calculate the shift
  24. a = rand_scalar()
  25. # calculates encryptions of s for all powers i in 0 to d
  26. # E(s^i) = g^s^i
  27. d = 10
  28. encrypted_powers = [
  29. g1 * (s**i) for i in range(d)
  30. ]
  31. encrypted_powers_g2 = [
  32. g2 * (s**i) for i in range(d)
  33. ]
  34. encrypted_shifted_powers = [
  35. g1 * (a * s**i) for i in range(d)
  36. ]
  37. # evaluates unencrypted target polynomial with s: t(s)
  38. target = (s - 1) * (s - 2)
  39. # CRS = common reference string = trusted setup parameters
  40. target_crs = g1 * target
  41. alpha_crs = g2 * a
  42. # Proving key = (encrypted_powers, encrypted_shifted_powers)
  43. # Verify key = (target_crs, alpha_crs)
  44. # encrypted values of s provided to the prover
  45. # Actual values of s are toxic waste and discarded
  46. #################################
  47. # Prover
  48. #################################
  49. # delta shift
  50. delta = rand_scalar()
  51. # E(p(s)) = p(s)G
  52. # = c_d s^d G + ... + c_1 s^1 G + c_0 s^0 G
  53. # = s^3 G - 3 s^2 G + 2 s G
  54. # E(h(s)) = sG
  55. # t(s) = s^2 - 3s + 2
  56. # E(h(s)) t(s) = s^3 G - 3 s^2 G + 2 s G
  57. # Lets test these manually:
  58. e_s = encrypted_powers
  59. e_p_s = e_s[3] - 3 * e_s[2] + 2 * e_s[1]
  60. e_h_s = e_s[1]
  61. t_s = s**2 - 3*s + 2
  62. # exponentiate with delta
  63. e_p_s *= delta
  64. e_h_s *= delta
  65. assert t_s == target
  66. assert e_p_s == e_h_s * t_s
  67. e_as = encrypted_shifted_powers
  68. e_p_as = e_as[3] - 3 * e_as[2] + 2 * e_as[1]
  69. # exponentiate with delta
  70. e_p_as *= delta
  71. assert e_p_s * a == e_p_as
  72. #############################
  73. # x^3 - 3x^2 + 2x
  74. main_poly = np.poly1d([1, -3, 2, 0])
  75. # (x - 1)(x - 2)
  76. target_poly = np.poly1d([1, -1]) * np.poly1d([1, -2])
  77. # Calculates polynomial h(x) = p(x) / t(x)
  78. cofactor, remainder = main_poly / target_poly
  79. assert remainder == np.poly1d([0])
  80. # Using encrypted powers and coefficients, evaluates
  81. # E(p(s)) and E(h(s))
  82. def evaluate(poly, encrypted_powers, identity):
  83. coeffs = list(poly.coef)[::-1]
  84. result = identity
  85. for power, coeff in zip(encrypted_powers, coeffs):
  86. #print(coeff, power)
  87. coeff = int(coeff)
  88. # I have to do this for some strange reason
  89. # Because if coeff is negative and I do += power * coeff
  90. # then it gives me a different result than what I expect
  91. if coeff < 0:
  92. result -= power * (-coeff)
  93. else:
  94. result += power * coeff
  95. # Add delta to the result
  96. # Free extra obfuscation to the polynomial
  97. return result * delta
  98. encrypted_poly = evaluate(main_poly, encrypted_powers, null)
  99. assert encrypted_poly == e_p_s
  100. encrypted_cofactor = evaluate(cofactor, encrypted_powers_g2, null2)
  101. # Alpha shifted powers
  102. encrypted_shift_poly = evaluate(main_poly, encrypted_shifted_powers, null)
  103. # resulting g^p and g^h are provided to the verifier
  104. # proof = (encrypted_poly, encrypted_cofactor, encrypted_shift_poly)
  105. #################################
  106. # Verifier
  107. #################################
  108. # Last check that p = t(s) h
  109. # Check polynomial cofactors:
  110. #assert encrypted_poly == encrypted_cofactor * target
  111. # e(g^p, g) == e(g^t, g^h)
  112. res1 = pairing.ate_pairing(encrypted_poly, g2)
  113. res2 = pairing.ate_pairing(target_crs, encrypted_cofactor)
  114. assert res1 == res2
  115. # Verify (g^p)^a == g^p'
  116. # Check polynomial restriction:
  117. res1 = pairing.ate_pairing(encrypted_shift_poly, g2)
  118. res2 = pairing.ate_pairing(encrypted_poly, alpha_crs)
  119. assert res1 == res2
  120. #assert encrypted_poly * a == encrypted_shift_poly