pairing.sage 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # see
  2. # https://github.com/zcash/librustzcash/blob/6e0364cd42a2b3d2b958a54771ef51a8db79dd29/pairing/src/bls12_381/README.md#generators
  3. xxx = -0xd201000000010000
  4. q = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
  5. assert q == (xxx - 1)^2 * ((xxx^4 - xxx^2 + 1) / 3) + xxx
  6. # 381 bits to represent q
  7. assert 380 < log(q, 2).n() < 381
  8. r = 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001
  9. assert r == (xxx^4 - xxx^2 + 1)
  10. assert 254 < log(r, 2).n() < 255
  11. # F₂ is constructed as F(u) / (u² + 1)
  12. # F₆ is constructed as F₂(v) / (v³- (u + 1))
  13. # F₁₂ is constructed as F₆(w) / (w²- v)
  14. # we can't do extension field towers in sage...
  15. # https://ask.sagemath.org/question/49663/efficiently-computing-tower-fields-for-pairings/
  16. F1 = GF(q)
  17. K2.<x> = PolynomialRing(F1)
  18. F2.<u> = F1.extension(x^2 + 1)
  19. # See file bls-init.sage for an explanation
  20. R.<y> = PolynomialRing(F2)
  21. K.<w> = F2.extension(y^6 - (u + 1))
  22. v = w^2
  23. print(f"u = {u}")
  24. print(f"v = {v}")
  25. print(f"w = {w}")
  26. assert u^2 + 1 == 0
  27. assert v^3 - (u + 1) == 0
  28. assert w^2 - v == 0
  29. E1 = EllipticCurve(K, (0, 4))
  30. E2 = EllipticCurve(K, (0, 4*(u + 1)))
  31. def find_random_point(E, F, A, B):
  32. while True:
  33. x = F.random_element()
  34. y = sqrt(x^3 + A*x + B)
  35. return E(x, y)
  36. E1.random_point = lambda: find_random_point(E1, F1, 0, 4)
  37. E2.random_point = lambda: find_random_point(E2, F2, 0, 4*(u + 1))
  38. x1 = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
  39. y1 = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
  40. G1 = E1(x1, y1)
  41. #assert G1.order() == r
  42. x2 = 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758*u + 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160
  43. y2 = 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582*u + 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
  44. G2 = E2(x2, y2)
  45. #assert G2.order() == r
  46. # Embedding degree
  47. k = GF(r)(q).multiplicative_order()
  48. assert k == 12
  49. # We need to map G1 -> G2
  50. # and then G2 -> G12
  51. #assert G1.tate_pairing(G2, r, k, q) == 1
  52. # G₁ ⊂ E(F)
  53. # G₂ ⊂ E'(F₂)
  54. # But the pairing function can only operate on curves from the same curve
  55. # These points are chosen with mapping to F₁₂ called a sextic twist
  56. # See https://hackmd.io/@benjaminion/bls12-381
  57. # https://github.com/zebra-lucky/python-bls/blob/master/bls_py/ec.py
  58. #value = GF(r)(110)
  59. #assert (G1.tate_pairing(int(value) * G2, r, k) ==
  60. # (int(value) * G1).tate_pairing(G2, r, k))