No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

143 líneas
4.2 KiB

  1. package internal
  2. const (
  3. FP_MAX_WORDS = 12 // Currently p751.NumWords
  4. )
  5. // I keep it bool in order to be able to apply logical NOT
  6. type KeyVariant uint
  7. type PrimeFieldId uint
  8. // Representation of an element of the base field F_p.
  9. //
  10. // No particular meaning is assigned to the representation -- it could represent
  11. // an element in Montgomery form, or not. Tracking the meaning of the field
  12. // element is left to higher types.
  13. type FpElement [FP_MAX_WORDS]uint64
  14. // Represents an intermediate product of two elements of the base field F_p.
  15. type FpElementX2 [2 * FP_MAX_WORDS]uint64
  16. // Represents an element of the extended field Fp^2 = Fp(x+i)
  17. type Fp2Element struct {
  18. A FpElement
  19. B FpElement
  20. }
  21. type DomainParams struct {
  22. // P, Q and R=P-Q base points
  23. Affine_P, Affine_Q, Affine_R Fp2Element
  24. // Size of a compuatation strategy for x-torsion group
  25. IsogenyStrategy []uint32
  26. // Max size of secret key for x-torsion group
  27. SecretBitLen uint
  28. // Max size of secret key for x-torsion group
  29. SecretByteLen uint
  30. }
  31. type SidhParams struct {
  32. Id PrimeFieldId
  33. // Bytelen of P
  34. Bytelen int
  35. // The public key size, in bytes.
  36. PublicKeySize int
  37. // The shared secret size, in bytes.
  38. SharedSecretSize uint
  39. // 2- and 3-torsion group parameter definitions
  40. A, B DomainParams
  41. // Precomputed identity element in the Fp2 in Montgomery domain
  42. OneFp2 Fp2Element
  43. // Precomputed 1/2 in the Fp2 in Montgomery domain
  44. HalfFp2 Fp2Element
  45. // Length of SIKE secret message. Must be one of {24,32,40},
  46. // depending on size of prime field used (see [SIKE], 1.4 and 5.1)
  47. MsgLen uint
  48. // Length of SIKE ephemeral KEM key (see [SIKE], 1.4 and 5.1)
  49. KemSize uint
  50. // Access to field arithmetic
  51. Op FieldOps
  52. }
  53. // Interface for working with isogenies.
  54. type Isogeny interface {
  55. // Given a torsion point on a curve computes isogenous curve.
  56. // Returns curve coefficients (A:C), so that E_(A/C) = E_(A/C)/<P>,
  57. // where P is a provided projective point. Sets also isogeny constants
  58. // that are needed for isogeny evaluation.
  59. GenerateCurve(*ProjectivePoint) CurveCoefficientsEquiv
  60. // Evaluates isogeny at caller provided point. Requires isogeny curve constants
  61. // to be earlier computed by GenerateCurve.
  62. EvaluatePoint(*ProjectivePoint) ProjectivePoint
  63. }
  64. // Stores curve projective parameters equivalent to A/C. Meaning of the
  65. // values depends on the context. When working with isogenies over
  66. // subgroup that are powers of:
  67. // * three then (A:C) ~ (A+2C:A-2C)
  68. // * four then (A:C) ~ (A+2C: 4C)
  69. // See Appendix A of SIKE for more details
  70. type CurveCoefficientsEquiv struct {
  71. A Fp2Element
  72. C Fp2Element
  73. }
  74. // A point on the projective line P^1(F_{p^2}).
  75. //
  76. // This represents a point on the Kummer line of a Montgomery curve. The
  77. // curve is specified by a ProjectiveCurveParameters struct.
  78. type ProjectivePoint struct {
  79. X Fp2Element
  80. Z Fp2Element
  81. }
  82. // A point on the projective line P^1(F_{p^2}).
  83. //
  84. // This is used to work projectively with the curve coefficients.
  85. type ProjectiveCurveParameters struct {
  86. A Fp2Element
  87. C Fp2Element
  88. }
  89. // Stores Isogeny 3 curve constants
  90. type isogeny3 struct {
  91. Field FieldOps
  92. K1 Fp2Element
  93. K2 Fp2Element
  94. }
  95. // Stores Isogeny 4 curve constants
  96. type isogeny4 struct {
  97. isogeny3
  98. K3 Fp2Element
  99. }
  100. type FieldOps interface {
  101. // Set res = lhs + rhs.
  102. //
  103. // Allowed to overlap lhs or rhs with res.
  104. Add(res, lhs, rhs *Fp2Element)
  105. // Set res = lhs - rhs.
  106. //
  107. // Allowed to overlap lhs or rhs with res.
  108. Sub(res, lhs, rhs *Fp2Element)
  109. // Set res = lhs * rhs.
  110. //
  111. // Allowed to overlap lhs or rhs with res.
  112. Mul(res, lhs, rhs *Fp2Element)
  113. // Set res = x * x
  114. //
  115. // Allowed to overlap res with x.
  116. Square(res, x *Fp2Element)
  117. // Set res = 1/x
  118. //
  119. // Allowed to overlap res with x.
  120. Inv(res, x *Fp2Element)
  121. // If choice = 1u8, set (x,y) = (y,x). If choice = 0u8, set (x,y) = (x,y).
  122. CondSwap(xPx, xPz, xQx, xQz *Fp2Element, choice uint8)
  123. // Converts Fp2Element to Montgomery domain (x*R mod p)
  124. ToMontgomery(x *Fp2Element)
  125. // Converts 'a' in montgomery domain to element from Fp2Element
  126. // and stores it in 'x'
  127. FromMontgomery(x *Fp2Element, a *Fp2Element)
  128. }