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.
 
 
 

145 line
4.6 KiB

  1. package p503toolbox
  2. // Interface for working with isogenies.
  3. type Isogeny interface {
  4. // Given a torsion point on a curve computes isogenous curve.
  5. // Returns curve coefficients (A:C), so that E_(A/C) = E_(A/C)/<P>,
  6. // where P is a provided projective point. Sets also isogeny constants
  7. // that are needed for isogeny evaluation.
  8. GenerateCurve(*ProjectivePoint) CurveCoefficientsEquiv
  9. // Evaluates isogeny at caller provided point. Requires isogeny curve constants
  10. // to be earlier computed by GenerateCurve.
  11. EvaluatePoint(*ProjectivePoint) ProjectivePoint
  12. }
  13. // Stores Isogeny 4 curve constants
  14. type isogeny4 struct {
  15. isogeny3
  16. K3 ExtensionFieldElement
  17. }
  18. // Stores Isogeny 3 curve constants
  19. type isogeny3 struct {
  20. K1 ExtensionFieldElement
  21. K2 ExtensionFieldElement
  22. }
  23. // Constructs isogeny4 objects
  24. func NewIsogeny4() Isogeny {
  25. return new(isogeny4)
  26. }
  27. // Constructs isogeny3 objects
  28. func NewIsogeny3() Isogeny {
  29. return new(isogeny3)
  30. }
  31. // Given a three-torsion point p = x(PB) on the curve E_(A:C), construct the
  32. // three-isogeny phi : E_(A:C) -> E_(A:C)/<P_3> = E_(A':C').
  33. //
  34. // Input: (XP_3: ZP_3), where P_3 has exact order 3 on E_A/C
  35. // Output: * Curve coordinates (A' + 2C', A' - 2C') corresponding to E_A'/C' = A_E/C/<P3>
  36. // * Isogeny phi with constants in F_p^2
  37. func (phi *isogeny3) GenerateCurve(p *ProjectivePoint) CurveCoefficientsEquiv {
  38. var t0, t1, t2, t3, t4 ExtensionFieldElement
  39. var coefEq CurveCoefficientsEquiv
  40. var K1, K2 = &phi.K1, &phi.K2
  41. K1.Sub(&p.X, &p.Z) // K1 = XP3 - ZP3
  42. t0.Square(K1) // t0 = K1^2
  43. K2.Add(&p.X, &p.Z) // K2 = XP3 + ZP3
  44. t1.Square(K2) // t1 = K2^2
  45. t2.Add(&t0, &t1) // t2 = t0 + t1
  46. t3.Add(K1, K2) // t3 = K1 + K2
  47. t3.Square(&t3) // t3 = t3^2
  48. t3.Sub(&t3, &t2) // t3 = t3 - t2
  49. t2.Add(&t1, &t3) // t2 = t1 + t3
  50. t3.Add(&t3, &t0) // t3 = t3 + t0
  51. t4.Add(&t3, &t0) // t4 = t3 + t0
  52. t4.Add(&t4, &t4) // t4 = t4 + t4
  53. t4.Add(&t1, &t4) // t4 = t1 + t4
  54. coefEq.C.Mul(&t2, &t4) // A24m = t2 * t4
  55. t4.Add(&t1, &t2) // t4 = t1 + t2
  56. t4.Add(&t4, &t4) // t4 = t4 + t4
  57. t4.Add(&t0, &t4) // t4 = t0 + t4
  58. t4.Mul(&t3, &t4) // t4 = t3 * t4
  59. t0.Sub(&t4, &coefEq.C) // t0 = t4 - A24m
  60. coefEq.A.Add(&coefEq.C, &t0) // A24p = A24m + t0
  61. return coefEq
  62. }
  63. // Given a 3-isogeny phi and a point pB = x(PB), compute x(QB), the x-coordinate
  64. // of the image QB = phi(PB) of PB under phi : E_(A:C) -> E_(A':C').
  65. //
  66. // The output xQ = x(Q) is then a point on the curve E_(A':C'); the curve
  67. // parameters are returned by the GenerateCurve function used to construct phi.
  68. func (phi *isogeny3) EvaluatePoint(p *ProjectivePoint) ProjectivePoint {
  69. var t0, t1, t2 ExtensionFieldElement
  70. var q ProjectivePoint
  71. var K1, K2 = &phi.K1, &phi.K2
  72. var px, pz = &p.X, &p.Z
  73. t0.Add(px, pz) // t0 = XQ + ZQ
  74. t1.Sub(px, pz) // t1 = XQ - ZQ
  75. t0.Mul(K1, &t0) // t2 = K1 * t0
  76. t1.Mul(K2, &t1) // t1 = K2 * t1
  77. t2.Add(&t0, &t1) // t2 = t0 + t1
  78. t0.Sub(&t1, &t0) // t0 = t1 - t0
  79. t2.Square(&t2) // t2 = t2 ^ 2
  80. t0.Square(&t0) // t0 = t0 ^ 2
  81. q.X.Mul(px, &t2) // XQ'= XQ * t2
  82. q.Z.Mul(pz, &t0) // ZQ'= ZQ * t0
  83. return q
  84. }
  85. // Given a four-torsion point p = x(PB) on the curve E_(A:C), construct the
  86. // four-isogeny phi : E_(A:C) -> E_(A:C)/<P_4> = E_(A':C').
  87. //
  88. // Input: (XP_4: ZP_4), where P_4 has exact order 4 on E_A/C
  89. // Output: * Curve coordinates (A' + 2C', 4C') corresponding to E_A'/C' = A_E/C/<P4>
  90. // * Isogeny phi with constants in F_p^2
  91. func (phi *isogeny4) GenerateCurve(p *ProjectivePoint) CurveCoefficientsEquiv {
  92. var coefEq CurveCoefficientsEquiv
  93. var xp4, zp4 = &p.X, &p.Z
  94. var K1, K2, K3 = &phi.K1, &phi.K2, &phi.K3
  95. K2.Sub(xp4, zp4)
  96. K3.Add(xp4, zp4)
  97. K1.Square(zp4)
  98. K1.Add(K1, K1)
  99. coefEq.C.Square(K1)
  100. K1.Add(K1, K1)
  101. coefEq.A.Square(xp4)
  102. coefEq.A.Add(&coefEq.A, &coefEq.A)
  103. coefEq.A.Square(&coefEq.A)
  104. return coefEq
  105. }
  106. // Given a 4-isogeny phi and a point xP = x(P), compute x(Q), the x-coordinate
  107. // of the image Q = phi(P) of P under phi : E_(A:C) -> E_(A':C').
  108. //
  109. // Input: Isogeny returned by GenerateCurve and point q=(Qx,Qz) from E0_A/C
  110. // Output: Corresponding point q from E1_A'/C', where E1 is 4-isogenous to E0
  111. func (phi *isogeny4) EvaluatePoint(p *ProjectivePoint) ProjectivePoint {
  112. var t0, t1 ExtensionFieldElement
  113. var q = *p
  114. var xq, zq = &q.X, &q.Z
  115. var K1, K2, K3 = &phi.K1, &phi.K2, &phi.K3
  116. t0.Add(xq, zq)
  117. t1.Sub(xq, zq)
  118. xq.Mul(&t0, K2)
  119. zq.Mul(&t1, K3)
  120. t0.Mul(&t0, &t1)
  121. t0.Mul(&t0, K1)
  122. t1.Add(xq, zq)
  123. zq.Sub(xq, zq)
  124. t1.Square(&t1)
  125. zq.Square(zq)
  126. xq.Add(&t0, &t1)
  127. t0.Sub(zq, &t0)
  128. xq.Mul(xq, &t1)
  129. zq.Mul(zq, &t0)
  130. return q
  131. }