選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

288 行
9.7 KiB

  1. package sike
  2. // I keep it bool in order to be able to apply logical NOT
  3. type KeyVariant uint
  4. // Representation of an element of the base field F_p.
  5. //
  6. // No particular meaning is assigned to the representation -- it could represent
  7. // an element in Montgomery form, or not. Tracking the meaning of the field
  8. // element is left to higher types.
  9. type Fp [FP_WORDS]uint64
  10. // Represents an intermediate product of two elements of the base field F_p.
  11. type FpX2 [2 * FP_WORDS]uint64
  12. // Represents an element of the extended field Fp^2 = Fp(x+i)
  13. type Fp2 struct {
  14. A Fp
  15. B Fp
  16. }
  17. type DomainParams struct {
  18. // P, Q and R=P-Q base points
  19. Affine_P, Affine_Q, Affine_R Fp2
  20. // Size of a compuatation strategy for x-torsion group
  21. IsogenyStrategy []uint32
  22. // Max size of secret key for x-torsion group
  23. SecretBitLen uint
  24. // Max size of secret key for x-torsion group
  25. SecretByteLen uint
  26. }
  27. type SidhParams struct {
  28. Id uint8
  29. // Bytelen of P
  30. Bytelen int
  31. // The public key size, in bytes.
  32. PublicKeySize int
  33. // The shared secret size, in bytes.
  34. SharedSecretSize int
  35. // 2- and 3-torsion group parameter definitions
  36. A, B DomainParams
  37. // Precomputed identity element in the Fp2 in Montgomery domain
  38. OneFp2 Fp2
  39. // Precomputed 1/2 in the Fp2 in Montgomery domain
  40. HalfFp2 Fp2
  41. // Length of SIKE secret message. Must be one of {24,32,40},
  42. // depending on size of prime field used (see [SIKE], 1.4 and 5.1)
  43. MsgLen int
  44. // Length of SIKE ephemeral KEM key (see [SIKE], 1.4 and 5.1)
  45. KemSize int
  46. }
  47. // Stores curve projective parameters equivalent to A/C. Meaning of the
  48. // values depends on the context. When working with isogenies over
  49. // subgroup that are powers of:
  50. // * three then (A:C) ~ (A+2C:A-2C)
  51. // * four then (A:C) ~ (A+2C: 4C)
  52. // See Appendix A of SIKE for more details
  53. type CurveCoefficientsEquiv struct {
  54. A Fp2
  55. C Fp2
  56. }
  57. // A point on the projective line P^1(F_{p^2}).
  58. //
  59. // This represents a point on the Kummer line of a Montgomery curve. The
  60. // curve is specified by a ProjectiveCurveParameters struct.
  61. type ProjectivePoint struct {
  62. X Fp2
  63. Z Fp2
  64. }
  65. // Base type for public and private key. Used mainly to carry domain
  66. // parameters.
  67. type key struct {
  68. // Domain parameters of the algorithm to be used with a key
  69. params *SidhParams
  70. // Flag indicates wether corresponds to 2-, 3-torsion group or SIKE
  71. keyVariant KeyVariant
  72. }
  73. // Defines operations on private key
  74. type PrivateKey struct {
  75. key
  76. // Secret key
  77. Scalar []byte
  78. // Used only by KEM
  79. S []byte
  80. }
  81. // Defines operations on public key
  82. type PublicKey struct {
  83. key
  84. affine_xP Fp2
  85. affine_xQ Fp2
  86. affine_xQmP Fp2
  87. }
  88. // A point on the projective line P^1(F_{p^2}).
  89. //
  90. // This is used to work projectively with the curve coefficients.
  91. type ProjectiveCurveParameters struct {
  92. A Fp2
  93. C Fp2
  94. }
  95. const (
  96. // First 2 bits identify SIDH variant third bit indicates
  97. // wether key is a SIKE variant (set) or SIDH (not set)
  98. // 001 - SIDH: corresponds to 2-torsion group
  99. KeyVariant_SIDH_A KeyVariant = 1 << 0
  100. // 010 - SIDH: corresponds to 3-torsion group
  101. KeyVariant_SIDH_B = 1 << 1
  102. // 110 - SIKE
  103. KeyVariant_SIKE = 1<<2 | KeyVariant_SIDH_B
  104. // Number of uint64 limbs used to store field element
  105. FP_WORDS = 8
  106. )
  107. // Used internally by this package
  108. // -------------------------------
  109. var p503 = Fp{
  110. 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xABFFFFFFFFFFFFFF,
  111. 0x13085BDA2211E7A0, 0x1B9BF6C87B7E7DAF, 0x6045C6BDDA77A4D0, 0x004066F541811E1E,
  112. }
  113. // 2*503
  114. var p503x2 = Fp{
  115. 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x57FFFFFFFFFFFFFF,
  116. 0x2610B7B44423CF41, 0x3737ED90F6FCFB5E, 0xC08B8D7BB4EF49A0, 0x0080CDEA83023C3C,
  117. }
  118. // p503 + 1
  119. var p503p1 = Fp{
  120. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0xAC00000000000000,
  121. 0x13085BDA2211E7A0, 0x1B9BF6C87B7E7DAF, 0x6045C6BDDA77A4D0, 0x004066F541811E1E,
  122. }
  123. // R^2=(2^512)^2 mod p
  124. var p503R2 = Fp{
  125. 0x5289A0CF641D011F, 0x9B88257189FED2B9, 0xA3B365D58DC8F17A, 0x5BC57AB6EFF168EC,
  126. 0x9E51998BD84D4423, 0xBF8999CBAC3B5695, 0x46E9127BCE14CDB6, 0x003F6CFCE8B81771,
  127. }
  128. // p503 + 1 left-shifted by 8, assuming little endianness
  129. var p503p1s8 = Fp{
  130. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  131. 0x085BDA2211E7A0AC, 0x9BF6C87B7E7DAF13, 0x45C6BDDA77A4D01B, 0x4066F541811E1E60,
  132. }
  133. // 1*R mod p
  134. var P503_OneFp2 = Fp2{
  135. A: Fp{
  136. 0x00000000000003F9, 0x0000000000000000, 0x0000000000000000, 0xB400000000000000,
  137. 0x63CB1A6EA6DED2B4, 0x51689D8D667EB37D, 0x8ACD77C71AB24142, 0x0026FBAEC60F5953},
  138. }
  139. // 1/2 * R mod p
  140. var P503_HalfFp2 = Fp2{
  141. A: Fp{
  142. 0x00000000000001FC, 0x0000000000000000, 0x0000000000000000, 0xB000000000000000,
  143. 0x3B69BB2464785D2A, 0x36824A2AF0FE9896, 0xF5899F427A94F309, 0x0033B15203C83BB8},
  144. }
  145. var Params SidhParams
  146. func init() {
  147. Params = SidhParams{
  148. // SIDH public key byte size.
  149. PublicKeySize: 378,
  150. // SIDH shared secret byte size.
  151. SharedSecretSize: 126,
  152. A: DomainParams{
  153. // The x-coordinate of PA
  154. Affine_P: Fp2{
  155. A: Fp{
  156. 0xE7EF4AA786D855AF, 0xED5758F03EB34D3B, 0x09AE172535A86AA9, 0x237B9CC07D622723,
  157. 0xE3A284CBA4E7932D, 0x27481D9176C5E63F, 0x6A323FF55C6E71BF, 0x002ECC31A6FB8773,
  158. },
  159. B: Fp{
  160. 0x64D02E4E90A620B8, 0xDAB8128537D4B9F1, 0x4BADF77B8A228F98, 0x0F5DBDF9D1FB7D1B,
  161. 0xBEC4DB288E1A0DCC, 0xE76A8665E80675DB, 0x6D6F252E12929463, 0x003188BD1463FACC,
  162. },
  163. },
  164. // The x-coordinate of QA
  165. Affine_Q: Fp2{
  166. A: Fp{
  167. 0xB79D41025DE85D56, 0x0B867DA9DF169686, 0x740E5368021C827D, 0x20615D72157BF25C,
  168. 0xFF1590013C9B9F5B, 0xC884DCADE8C16CEA, 0xEBD05E53BF724E01, 0x0032FEF8FDA5748C,
  169. },
  170. B: Fp{
  171. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  172. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  173. },
  174. },
  175. // The x-coordinate of RA = PA-QA
  176. Affine_R: Fp2{
  177. A: Fp{
  178. 0x12E2E849AA0A8006, 0x41CF47008635A1E8, 0x9CD720A70798AED7, 0x42A820B42FCF04CF,
  179. 0x7BF9BAD32AAE88B1, 0xF619127A54090BBE, 0x1CB10D8F56408EAA, 0x001D6B54C3C0EDEB,
  180. },
  181. B: Fp{
  182. 0x34DB54931CBAAC36, 0x420A18CB8DD5F0C4, 0x32008C1A48C0F44D, 0x3B3BA772B1CFD44D,
  183. 0xA74B058FDAF13515, 0x095FC9CA7EEC17B4, 0x448E829D28F120F8, 0x00261EC3ED16A489,
  184. },
  185. },
  186. // Max size of secret key for 2-torsion group, corresponds to 2^e2 - 1
  187. SecretBitLen: 250,
  188. // SecretBitLen in bytes.
  189. SecretByteLen: uint((250 + 7) / 8),
  190. // 2-torsion group computation strategy
  191. IsogenyStrategy: []uint32{
  192. 0x3D, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01,
  193. 0x01, 0x02, 0x01, 0x01, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02,
  194. 0x01, 0x01, 0x02, 0x01, 0x01, 0x10, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01,
  195. 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01,
  196. 0x01, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x1D, 0x10, 0x08, 0x04, 0x02, 0x01,
  197. 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x08, 0x04, 0x02,
  198. 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x0D, 0x08,
  199. 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01,
  200. 0x05, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01},
  201. },
  202. B: DomainParams{
  203. // The x-coordinate of PB
  204. Affine_P: Fp2{
  205. A: Fp{
  206. 0x7EDE37F4FA0BC727, 0xF7F8EC5C8598941C, 0xD15519B516B5F5C8, 0xF6D5AC9B87A36282,
  207. 0x7B19F105B30E952E, 0x13BD8B2025B4EBEE, 0x7B96D27F4EC579A2, 0x00140850CAB7E5DE,
  208. },
  209. B: Fp{
  210. 0x7764909DAE7B7B2D, 0x578ABB16284911AB, 0x76E2BFD146A6BF4D, 0x4824044B23AA02F0,
  211. 0x1105048912A321F3, 0xB8A2E482CF0F10C1, 0x42FF7D0BE2152085, 0x0018E599C5223352,
  212. },
  213. },
  214. // The x-coordinate of QB
  215. Affine_Q: Fp2{
  216. A: Fp{
  217. 0x4256C520FB388820, 0x744FD7C3BAAF0A13, 0x4B6A2DDDB12CBCB8, 0xE46826E27F427DF8,
  218. 0xFE4A663CD505A61B, 0xD6B3A1BAF025C695, 0x7C3BB62B8FCC00BD, 0x003AFDDE4A35746C,
  219. },
  220. B: Fp{
  221. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  222. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  223. },
  224. },
  225. // The x-coordinate of RB = PB - QB
  226. Affine_R: Fp2{
  227. A: Fp{
  228. 0x75601CD1E6C0DFCB, 0x1A9007239B58F93E, 0xC1F1BE80C62107AC, 0x7F513B898F29FF08,
  229. 0xEA0BEDFF43E1F7B2, 0x2C6D94018CBAE6D0, 0x3A430D31BCD84672, 0x000D26892ECCFE83,
  230. },
  231. B: Fp{
  232. 0x1119D62AEA3007A1, 0xE3702AA4E04BAE1B, 0x9AB96F7D59F990E7, 0xF58440E8B43319C0,
  233. 0xAF8134BEE1489775, 0xE7F7774E905192AA, 0xF54AE09308E98039, 0x001EF7A041A86112,
  234. },
  235. },
  236. // Size of secret key for 3-torsion group, corresponds to log_2(3^e3) - 1.
  237. SecretBitLen: 252,
  238. // SecretBitLen in bytes.
  239. SecretByteLen: uint((252 + 7) / 8),
  240. // 3-torsion group computation strategy
  241. IsogenyStrategy: []uint32{
  242. 0x47, 0x26, 0x15, 0x0D, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02,
  243. 0x01, 0x01, 0x02, 0x01, 0x01, 0x05, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02,
  244. 0x01, 0x01, 0x01, 0x09, 0x05, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01,
  245. 0x01, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x11, 0x09, 0x05, 0x03, 0x02,
  246. 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02,
  247. 0x01, 0x01, 0x08, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01,
  248. 0x01, 0x02, 0x01, 0x01, 0x21, 0x11, 0x09, 0x05, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01,
  249. 0x02, 0x01, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x08, 0x04,
  250. 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01,
  251. 0x10, 0x08, 0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01, 0x01,
  252. 0x02, 0x01, 0x01, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x04, 0x02, 0x01,
  253. 0x01, 0x02, 0x01, 0x01},
  254. },
  255. OneFp2: P503_OneFp2,
  256. HalfFp2: P503_HalfFp2,
  257. MsgLen: 24,
  258. // SIKEp503 provides 128 bit of classical security ([SIKE], 5.1)
  259. KemSize: 16,
  260. // ceil(503+7/8)
  261. Bytelen: 63,
  262. }
  263. }