3.4-restricted-polynomial.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.4 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. #################################
  17. # Verifier (trusted setup)
  18. #################################
  19. # samples a random value (a secret)
  20. s = rand_scalar()
  21. # calculate the shift
  22. a = rand_scalar()
  23. # calculates encryptions of s for all powers i in 0 to d
  24. # E(s^i) = g^s^i
  25. d = 10
  26. encrypted_powers = [
  27. g1 * (s**i) for i in range(d)
  28. ]
  29. encrypted_shifted_powers = [
  30. g1 * (a * s**i) for i in range(d)
  31. ]
  32. # evaluates unencrypted target polynomial with s: t(s)
  33. target = (s - 1) * (s - 2)
  34. # encrypted values of s provided to the prover
  35. # Actual values of s are toxic waste and discarded
  36. #################################
  37. # Prover
  38. #################################
  39. # E(p(s)) = p(s)G
  40. # = c_d s^d G + ... + c_1 s^1 G + c_0 s^0 G
  41. # = s^3 G - 3 s^2 G + 2 s G
  42. # E(h(s)) = sG
  43. # t(s) = s^2 - 3s + 2
  44. # E(h(s)) t(s) = s^3 G - 3 s^2 G + 2 s G
  45. # Lets test these manually:
  46. e_s = encrypted_powers
  47. e_p_s = e_s[3] - 3 * e_s[2] + 2 * e_s[1]
  48. e_h_s = e_s[1]
  49. t_s = s**2 - 3*s + 2
  50. assert t_s == target
  51. assert e_p_s == e_h_s * t_s
  52. e_as = encrypted_shifted_powers
  53. e_p_as = e_as[3] - 3 * e_as[2] + 2 * e_as[1]
  54. assert e_p_s * a == e_p_as
  55. #############################
  56. # x^3 - 3x^2 + 2x
  57. main_poly = np.poly1d([1, -3, 2, 0])
  58. # (x - 1)(x - 2)
  59. target_poly = np.poly1d([1, -1]) * np.poly1d([1, -2])
  60. # Calculates polynomial h(x) = p(x) / t(x)
  61. cofactor, remainder = main_poly / target_poly
  62. assert remainder == np.poly1d([0])
  63. # Using encrypted powers and coefficients, evaluates
  64. # E(p(s)) and E(h(s))
  65. def evaluate(poly, encrypted_powers):
  66. coeffs = list(poly.coef)[::-1]
  67. result = null
  68. for power, coeff in zip(encrypted_powers, coeffs):
  69. #print(coeff, power)
  70. coeff = int(coeff)
  71. # I have to do this for some strange reason
  72. # Because if coeff is negative and I do += power * coeff
  73. # then it gives me a different result than what I expect
  74. if coeff < 0:
  75. result -= power * (-coeff)
  76. else:
  77. result += power * coeff
  78. return result
  79. encrypted_poly = evaluate(main_poly, encrypted_powers)
  80. assert encrypted_poly == e_p_s
  81. encrypted_cofactor = evaluate(cofactor, encrypted_powers)
  82. # Alpha shifted powers
  83. encrypted_shift_poly = evaluate(main_poly, encrypted_shifted_powers)
  84. # resulting g^p and g^h are provided to the verifier
  85. #################################
  86. # Verifier
  87. #################################
  88. # Last check that p = t(s) h
  89. assert encrypted_poly == encrypted_cofactor * target
  90. # Verify (g^p)^a == g^p'
  91. assert encrypted_poly * a == encrypted_shift_poly