Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

consts.go 9.8 KiB

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