You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.6 KiB

  1. # toy implementation of BLS signatures in Sage
  2. #
  3. # Scheme overview: https://arnaucube.com/blog/kzg-commitments.html
  4. # Go implementation: https://github.com/arnaucube/kzg-commitments-study
  5. load("bls12-381.sage")
  6. e = Pairing()
  7. def new_ts(l):
  8. Fr = GF(e.r)
  9. s = Fr.random_element()
  10. print("s", s)
  11. tauG1 = [None] * l
  12. tauG2 = [None] * l
  13. for i in range(0, l): # TODO probably duplicate G1 & G2 instead of first powering s^i and then * G_j
  14. sPow = Integer(s)^i
  15. tauG1[i] = sPow * e.G1
  16. tauG2[i] = sPow * e.G2
  17. return (tauG1, tauG2)
  18. def commit(taus, p):
  19. return evaluate_at_tau(p, taus)
  20. # evaluates p at tau
  21. def evaluate_at_tau(p, taus):
  22. e = 0
  23. for i in range(0, len(p.list())):
  24. e = e + p[i] * taus[i]
  25. return e
  26. def evaluation_proof(tau, p, z, y):
  27. # (p - y)
  28. n = p - y
  29. # (t - z)
  30. d = (t-z)
  31. # q, rem = n / d
  32. q = n / d
  33. print("q", q)
  34. q = q.numerator()
  35. den = q.denominator()
  36. print("q", q)
  37. print("den", den)
  38. # check that den = 1
  39. assert(den==1) # rem=0
  40. # proof: e = [q(t)]₁
  41. return evaluate_at_tau(q, tau)
  42. def verify(tau, c, proof, z, y):
  43. # [t]₂ - [z]₂
  44. sz = tau[1] - z*e.G2
  45. # c - [y]₁
  46. cy = c - y*e.G1
  47. print("proof", proof)
  48. print("sz", sz)
  49. print("cy", cy)
  50. lhs = e.pair(proof, sz)
  51. rhs = e.pair(cy, e.G2)
  52. print("lhs", lhs)
  53. print("rhs", rhs)
  54. return lhs == rhs
  55. (tauG1, tauG2) = new_ts(5)
  56. R.<t> = PolynomialRing(e.F1)
  57. p = t^3 + t + 5
  58. c = commit(tauG1, p)
  59. z = 3
  60. y = p(z) # = 35
  61. proof = evaluation_proof(tauG1, p, z, y)
  62. print("proof", proof)
  63. v = verify(tauG2, c, proof, z, y)
  64. print(v)
  65. assert(v)