3-encrypted-polynomial.py 2.6 KB

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