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.

85 lines
2.6 KiB

  1. # Sage impl of the powers of tau,
  2. # a Go implementation can be found at: https://github.com/arnaucube/eth-kzg-ceremony-alt
  3. load("bls12-381.sage") # file from https://github.com/arnaucube/math/blob/master/bls12-381.sage
  4. e = Pairing()
  5. def new_empty_SRS(nG1, nG2):
  6. g1s = [None] * nG1
  7. g2s = [None] * nG2
  8. for i in range(0, nG1):
  9. g1s[i] = e.G1
  10. for i in range(0, nG2):
  11. g2s[i] = e.G2
  12. return [g1s, g2s]
  13. def new_tau(random):
  14. return e.F1(random)
  15. def compute_contribution(new_tau, prev_srs):
  16. g1s = [None] * len(prev_srs[0])
  17. g2s = [None] * len(prev_srs[1])
  18. srs = [g1s, g2s]
  19. Q = e.r
  20. # compute [τ'⁰]₁, [τ'¹]₁, [τ'²]₁, ..., [τ'ⁿ⁻¹]₁, where n = len(prev_srs.G1s)
  21. for i in range(0, len(prev_srs[0])):
  22. srs[0][i] = (new_tau^i) * prev_srs[0][i]
  23. # compute [τ'⁰]₂, [τ'¹]₂, [τ'²]₂, ..., [τ'ⁿ⁻¹]₂, where n = len(prev_srs.G2s)
  24. for i in range(0, len(prev_srs[1])):
  25. srs[1][i] = (new_tau^i) * prev_srs[1][i]
  26. return srs
  27. def generate_proof(tau, prev_srs, new_srs):
  28. # g_1^{tau'} = g_1^{p * tau} = SRS_G1s[1] * p
  29. g1_ptau = prev_srs[0][1] * tau
  30. # g_2^{p}
  31. g2_p = tau * e.G2
  32. return [g1_ptau, g2_p]
  33. def verify(prev_srs, new_srs, proof):
  34. # 1. check that elements of the newSRS are valid points
  35. for i in range(0, len(new_srs[0])-1):
  36. assert new_srs[0][i] != None
  37. assert new_srs[0][i] != e.E1(0)
  38. assert new_srs[0][i] in e.E1
  39. for i in range(0, len(new_srs[1])-1):
  40. assert new_srs[1][i] != None
  41. assert new_srs[1][i] != e.E2(0)
  42. assert new_srs[1][i] in e.E2
  43. # 2. check proof.G1PTau == newSRS.G1Powers[1]
  44. assert proof[0] == new_srs[0][1]
  45. # 3. check newSRS.G1s[1] (g₁^τ'), is correctly related to prev_srs.G1s[1] (g₁^τ)
  46. # e([τ]₁, [p]₂) == e([τ']₁, [1]₂)
  47. assert e.pair(prev_srs[0][1], proof[1]) == e.pair(new_srs[0][1], e.G2)
  48. # 4. check newSRS following the powers of tau structure
  49. # i) e([τ'ⁱ]₁, [τ']₂) == e([τ'ⁱ⁺¹]₁, [1]₂), for i ∈ [1, n−1]
  50. for i in range(0, len(new_srs[0])-1):
  51. assert e.pair(new_srs[0][i], new_srs[1][1]) == e.pair(new_srs[0][i+1], e.G2)
  52. # ii) e([τ']₁, [τ'ʲ]₂) == e([1]₁, [τ'ʲ⁺¹]₂), for j ∈ [1, m−1]
  53. for i in range(0, len(new_srs[1])-1):
  54. assert e.pair(new_srs[0][1], new_srs[1][i]) == e.pair(e.G1, new_srs[1][i+1])
  55. (prev_srs) = new_empty_SRS(5, 3)
  56. random = 12345
  57. tau = new_tau(random)
  58. new_srs = compute_contribution(tau, prev_srs)
  59. proof = generate_proof(tau, prev_srs, new_srs)
  60. verify(prev_srs, new_srs, proof)