25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p256-64.c 60 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. /* A 64-bit implementation of the NIST P-256 elliptic curve point
  15. * multiplication
  16. *
  17. * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
  18. * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
  19. * work which got its smarts from Daniel J. Bernstein's work on the same. */
  20. #include <openssl/base.h>
  21. #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
  22. #include <openssl/bn.h>
  23. #include <openssl/ec.h>
  24. #include <openssl/err.h>
  25. #include <openssl/mem.h>
  26. #include <openssl/obj.h>
  27. #include <string.h>
  28. #include "internal.h"
  29. typedef uint8_t u8;
  30. typedef uint64_t u64;
  31. typedef int64_t s64;
  32. typedef __uint128_t uint128_t;
  33. typedef __int128_t int128_t;
  34. /* The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
  35. * can serialise an element of this field into 32 bytes. We call this an
  36. * felem_bytearray. */
  37. typedef u8 felem_bytearray[32];
  38. /* These are the parameters of P256, taken from FIPS 186-3, page 86. These
  39. * values are big-endian. */
  40. static const felem_bytearray nistp256_curve_params[5] = {
  41. {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* p */
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  44. {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* a = -3 */
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  47. 0xfc}, /* b */
  48. {0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd, 0x55,
  49. 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
  50. 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b},
  51. {0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, /* x */
  52. 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81,
  53. 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96},
  54. {0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, /* y */
  55. 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
  56. 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5}};
  57. /* The representation of field elements.
  58. * ------------------------------------
  59. *
  60. * We represent field elements with either four 128-bit values, eight 128-bit
  61. * values, or four 64-bit values. The field element represented is:
  62. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
  63. * or:
  64. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
  65. *
  66. * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
  67. * apart, but are 128-bits wide, the most significant bits of each limb overlap
  68. * with the least significant bits of the next.
  69. *
  70. * A field element with four limbs is an 'felem'. One with eight limbs is a
  71. * 'longfelem'
  72. *
  73. * A field element with four, 64-bit values is called a 'smallfelem'. Small
  74. * values are used as intermediate values before multiplication. */
  75. #define NLIMBS 4
  76. typedef uint128_t limb;
  77. typedef limb felem[NLIMBS];
  78. typedef limb longfelem[NLIMBS * 2];
  79. typedef u64 smallfelem[NLIMBS];
  80. /* This is the value of the prime as four 64-bit words, little-endian. */
  81. static const u64 kPrime[4] = {0xfffffffffffffffful, 0xffffffff, 0,
  82. 0xffffffff00000001ul};
  83. static const u64 bottom63bits = 0x7ffffffffffffffful;
  84. /* bin32_to_felem takes a little-endian byte array and converts it into felem
  85. * form. This assumes that the CPU is little-endian. */
  86. static void bin32_to_felem(felem out, const u8 in[32]) {
  87. out[0] = *((u64 *)&in[0]);
  88. out[1] = *((u64 *)&in[8]);
  89. out[2] = *((u64 *)&in[16]);
  90. out[3] = *((u64 *)&in[24]);
  91. }
  92. /* smallfelem_to_bin32 takes a smallfelem and serialises into a little endian,
  93. * 32 byte array. This assumes that the CPU is little-endian. */
  94. static void smallfelem_to_bin32(u8 out[32], const smallfelem in) {
  95. *((u64 *)&out[0]) = in[0];
  96. *((u64 *)&out[8]) = in[1];
  97. *((u64 *)&out[16]) = in[2];
  98. *((u64 *)&out[24]) = in[3];
  99. }
  100. /* To preserve endianness when using BN_bn2bin and BN_bin2bn. */
  101. static void flip_endian(u8 *out, const u8 *in, unsigned len) {
  102. unsigned i;
  103. for (i = 0; i < len; ++i) {
  104. out[i] = in[len - 1 - i];
  105. }
  106. }
  107. /* BN_to_felem converts an OpenSSL BIGNUM into an felem. */
  108. static int BN_to_felem(felem out, const BIGNUM *bn) {
  109. if (BN_is_negative(bn)) {
  110. OPENSSL_PUT_ERROR(EC, BN_to_felem, EC_R_BIGNUM_OUT_OF_RANGE);
  111. return 0;
  112. }
  113. felem_bytearray b_out;
  114. /* BN_bn2bin eats leading zeroes */
  115. memset(b_out, 0, sizeof(b_out));
  116. unsigned num_bytes = BN_num_bytes(bn);
  117. if (num_bytes > sizeof(b_out)) {
  118. OPENSSL_PUT_ERROR(EC, BN_to_felem, EC_R_BIGNUM_OUT_OF_RANGE);
  119. return 0;
  120. }
  121. felem_bytearray b_in;
  122. num_bytes = BN_bn2bin(bn, b_in);
  123. flip_endian(b_out, b_in, num_bytes);
  124. bin32_to_felem(out, b_out);
  125. return 1;
  126. }
  127. /* felem_to_BN converts an felem into an OpenSSL BIGNUM. */
  128. static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in) {
  129. felem_bytearray b_in, b_out;
  130. smallfelem_to_bin32(b_in, in);
  131. flip_endian(b_out, b_in, sizeof(b_out));
  132. return BN_bin2bn(b_out, sizeof(b_out), out);
  133. }
  134. /* Field operations. */
  135. static void smallfelem_one(smallfelem out) {
  136. out[0] = 1;
  137. out[1] = 0;
  138. out[2] = 0;
  139. out[3] = 0;
  140. }
  141. static void smallfelem_assign(smallfelem out, const smallfelem in) {
  142. out[0] = in[0];
  143. out[1] = in[1];
  144. out[2] = in[2];
  145. out[3] = in[3];
  146. }
  147. static void felem_assign(felem out, const felem in) {
  148. out[0] = in[0];
  149. out[1] = in[1];
  150. out[2] = in[2];
  151. out[3] = in[3];
  152. }
  153. /* felem_sum sets out = out + in. */
  154. static void felem_sum(felem out, const felem in) {
  155. out[0] += in[0];
  156. out[1] += in[1];
  157. out[2] += in[2];
  158. out[3] += in[3];
  159. }
  160. /* felem_small_sum sets out = out + in. */
  161. static void felem_small_sum(felem out, const smallfelem in) {
  162. out[0] += in[0];
  163. out[1] += in[1];
  164. out[2] += in[2];
  165. out[3] += in[3];
  166. }
  167. /* felem_scalar sets out = out * scalar */
  168. static void felem_scalar(felem out, const u64 scalar) {
  169. out[0] *= scalar;
  170. out[1] *= scalar;
  171. out[2] *= scalar;
  172. out[3] *= scalar;
  173. }
  174. /* longfelem_scalar sets out = out * scalar */
  175. static void longfelem_scalar(longfelem out, const u64 scalar) {
  176. out[0] *= scalar;
  177. out[1] *= scalar;
  178. out[2] *= scalar;
  179. out[3] *= scalar;
  180. out[4] *= scalar;
  181. out[5] *= scalar;
  182. out[6] *= scalar;
  183. out[7] *= scalar;
  184. }
  185. #define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
  186. #define two105 (((limb)1) << 105)
  187. #define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
  188. /* zero105 is 0 mod p */
  189. static const felem zero105 = {two105m41m9, two105, two105m41p9, two105m41p9};
  190. /* smallfelem_neg sets |out| to |-small|
  191. * On exit:
  192. * out[i] < out[i] + 2^105 */
  193. static void smallfelem_neg(felem out, const smallfelem small) {
  194. /* In order to prevent underflow, we subtract from 0 mod p. */
  195. out[0] = zero105[0] - small[0];
  196. out[1] = zero105[1] - small[1];
  197. out[2] = zero105[2] - small[2];
  198. out[3] = zero105[3] - small[3];
  199. }
  200. /* felem_diff subtracts |in| from |out|
  201. * On entry:
  202. * in[i] < 2^104
  203. * On exit:
  204. * out[i] < out[i] + 2^105. */
  205. static void felem_diff(felem out, const felem in) {
  206. /* In order to prevent underflow, we add 0 mod p before subtracting. */
  207. out[0] += zero105[0];
  208. out[1] += zero105[1];
  209. out[2] += zero105[2];
  210. out[3] += zero105[3];
  211. out[0] -= in[0];
  212. out[1] -= in[1];
  213. out[2] -= in[2];
  214. out[3] -= in[3];
  215. }
  216. #define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
  217. #define two107 (((limb)1) << 107)
  218. #define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
  219. /* zero107 is 0 mod p */
  220. static const felem zero107 = {two107m43m11, two107, two107m43p11, two107m43p11};
  221. /* An alternative felem_diff for larger inputs |in|
  222. * felem_diff_zero107 subtracts |in| from |out|
  223. * On entry:
  224. * in[i] < 2^106
  225. * On exit:
  226. * out[i] < out[i] + 2^107. */
  227. static void felem_diff_zero107(felem out, const felem in) {
  228. /* In order to prevent underflow, we add 0 mod p before subtracting. */
  229. out[0] += zero107[0];
  230. out[1] += zero107[1];
  231. out[2] += zero107[2];
  232. out[3] += zero107[3];
  233. out[0] -= in[0];
  234. out[1] -= in[1];
  235. out[2] -= in[2];
  236. out[3] -= in[3];
  237. }
  238. /* longfelem_diff subtracts |in| from |out|
  239. * On entry:
  240. * in[i] < 7*2^67
  241. * On exit:
  242. * out[i] < out[i] + 2^70 + 2^40. */
  243. static void longfelem_diff(longfelem out, const longfelem in) {
  244. static const limb two70m8p6 =
  245. (((limb)1) << 70) - (((limb)1) << 8) + (((limb)1) << 6);
  246. static const limb two70p40 = (((limb)1) << 70) + (((limb)1) << 40);
  247. static const limb two70 = (((limb)1) << 70);
  248. static const limb two70m40m38p6 = (((limb)1) << 70) - (((limb)1) << 40) -
  249. (((limb)1) << 38) + (((limb)1) << 6);
  250. static const limb two70m6 = (((limb)1) << 70) - (((limb)1) << 6);
  251. /* add 0 mod p to avoid underflow */
  252. out[0] += two70m8p6;
  253. out[1] += two70p40;
  254. out[2] += two70;
  255. out[3] += two70m40m38p6;
  256. out[4] += two70m6;
  257. out[5] += two70m6;
  258. out[6] += two70m6;
  259. out[7] += two70m6;
  260. /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
  261. out[0] -= in[0];
  262. out[1] -= in[1];
  263. out[2] -= in[2];
  264. out[3] -= in[3];
  265. out[4] -= in[4];
  266. out[5] -= in[5];
  267. out[6] -= in[6];
  268. out[7] -= in[7];
  269. }
  270. #define two64m0 (((limb)1) << 64) - 1
  271. #define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
  272. #define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
  273. #define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
  274. /* zero110 is 0 mod p. */
  275. static const felem zero110 = {two64m0, two110p32m0, two64m46, two64m32};
  276. /* felem_shrink converts an felem into a smallfelem. The result isn't quite
  277. * minimal as the value may be greater than p.
  278. *
  279. * On entry:
  280. * in[i] < 2^109
  281. * On exit:
  282. * out[i] < 2^64. */
  283. static void felem_shrink(smallfelem out, const felem in) {
  284. felem tmp;
  285. u64 a, b, mask;
  286. s64 high, low;
  287. static const u64 kPrime3Test = 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
  288. /* Carry 2->3 */
  289. tmp[3] = zero110[3] + in[3] + ((u64)(in[2] >> 64));
  290. /* tmp[3] < 2^110 */
  291. tmp[2] = zero110[2] + (u64)in[2];
  292. tmp[0] = zero110[0] + in[0];
  293. tmp[1] = zero110[1] + in[1];
  294. /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
  295. /* We perform two partial reductions where we eliminate the high-word of
  296. * tmp[3]. We don't update the other words till the end. */
  297. a = tmp[3] >> 64; /* a < 2^46 */
  298. tmp[3] = (u64)tmp[3];
  299. tmp[3] -= a;
  300. tmp[3] += ((limb)a) << 32;
  301. /* tmp[3] < 2^79 */
  302. b = a;
  303. a = tmp[3] >> 64; /* a < 2^15 */
  304. b += a; /* b < 2^46 + 2^15 < 2^47 */
  305. tmp[3] = (u64)tmp[3];
  306. tmp[3] -= a;
  307. tmp[3] += ((limb)a) << 32;
  308. /* tmp[3] < 2^64 + 2^47 */
  309. /* This adjusts the other two words to complete the two partial
  310. * reductions. */
  311. tmp[0] += b;
  312. tmp[1] -= (((limb)b) << 32);
  313. /* In order to make space in tmp[3] for the carry from 2 -> 3, we
  314. * conditionally subtract kPrime if tmp[3] is large enough. */
  315. high = tmp[3] >> 64;
  316. /* As tmp[3] < 2^65, high is either 1 or 0 */
  317. high <<= 63;
  318. high >>= 63;
  319. /* high is:
  320. * all ones if the high word of tmp[3] is 1
  321. * all zeros if the high word of tmp[3] if 0 */
  322. low = tmp[3];
  323. mask = low >> 63;
  324. /* mask is:
  325. * all ones if the MSB of low is 1
  326. * all zeros if the MSB of low if 0 */
  327. low &= bottom63bits;
  328. low -= kPrime3Test;
  329. /* if low was greater than kPrime3Test then the MSB is zero */
  330. low = ~low;
  331. low >>= 63;
  332. /* low is:
  333. * all ones if low was > kPrime3Test
  334. * all zeros if low was <= kPrime3Test */
  335. mask = (mask & low) | high;
  336. tmp[0] -= mask & kPrime[0];
  337. tmp[1] -= mask & kPrime[1];
  338. /* kPrime[2] is zero, so omitted */
  339. tmp[3] -= mask & kPrime[3];
  340. /* tmp[3] < 2**64 - 2**32 + 1 */
  341. tmp[1] += ((u64)(tmp[0] >> 64));
  342. tmp[0] = (u64)tmp[0];
  343. tmp[2] += ((u64)(tmp[1] >> 64));
  344. tmp[1] = (u64)tmp[1];
  345. tmp[3] += ((u64)(tmp[2] >> 64));
  346. tmp[2] = (u64)tmp[2];
  347. /* tmp[i] < 2^64 */
  348. out[0] = tmp[0];
  349. out[1] = tmp[1];
  350. out[2] = tmp[2];
  351. out[3] = tmp[3];
  352. }
  353. /* smallfelem_expand converts a smallfelem to an felem */
  354. static void smallfelem_expand(felem out, const smallfelem in) {
  355. out[0] = in[0];
  356. out[1] = in[1];
  357. out[2] = in[2];
  358. out[3] = in[3];
  359. }
  360. /* smallfelem_square sets |out| = |small|^2
  361. * On entry:
  362. * small[i] < 2^64
  363. * On exit:
  364. * out[i] < 7 * 2^64 < 2^67 */
  365. static void smallfelem_square(longfelem out, const smallfelem small) {
  366. limb a;
  367. u64 high, low;
  368. a = ((uint128_t)small[0]) * small[0];
  369. low = a;
  370. high = a >> 64;
  371. out[0] = low;
  372. out[1] = high;
  373. a = ((uint128_t)small[0]) * small[1];
  374. low = a;
  375. high = a >> 64;
  376. out[1] += low;
  377. out[1] += low;
  378. out[2] = high;
  379. a = ((uint128_t)small[0]) * small[2];
  380. low = a;
  381. high = a >> 64;
  382. out[2] += low;
  383. out[2] *= 2;
  384. out[3] = high;
  385. a = ((uint128_t)small[0]) * small[3];
  386. low = a;
  387. high = a >> 64;
  388. out[3] += low;
  389. out[4] = high;
  390. a = ((uint128_t)small[1]) * small[2];
  391. low = a;
  392. high = a >> 64;
  393. out[3] += low;
  394. out[3] *= 2;
  395. out[4] += high;
  396. a = ((uint128_t)small[1]) * small[1];
  397. low = a;
  398. high = a >> 64;
  399. out[2] += low;
  400. out[3] += high;
  401. a = ((uint128_t)small[1]) * small[3];
  402. low = a;
  403. high = a >> 64;
  404. out[4] += low;
  405. out[4] *= 2;
  406. out[5] = high;
  407. a = ((uint128_t)small[2]) * small[3];
  408. low = a;
  409. high = a >> 64;
  410. out[5] += low;
  411. out[5] *= 2;
  412. out[6] = high;
  413. out[6] += high;
  414. a = ((uint128_t)small[2]) * small[2];
  415. low = a;
  416. high = a >> 64;
  417. out[4] += low;
  418. out[5] += high;
  419. a = ((uint128_t)small[3]) * small[3];
  420. low = a;
  421. high = a >> 64;
  422. out[6] += low;
  423. out[7] = high;
  424. }
  425. /*felem_square sets |out| = |in|^2
  426. * On entry:
  427. * in[i] < 2^109
  428. * On exit:
  429. * out[i] < 7 * 2^64 < 2^67. */
  430. static void felem_square(longfelem out, const felem in) {
  431. u64 small[4];
  432. felem_shrink(small, in);
  433. smallfelem_square(out, small);
  434. }
  435. /* smallfelem_mul sets |out| = |small1| * |small2|
  436. * On entry:
  437. * small1[i] < 2^64
  438. * small2[i] < 2^64
  439. * On exit:
  440. * out[i] < 7 * 2^64 < 2^67. */
  441. static void smallfelem_mul(longfelem out, const smallfelem small1,
  442. const smallfelem small2) {
  443. limb a;
  444. u64 high, low;
  445. a = ((uint128_t)small1[0]) * small2[0];
  446. low = a;
  447. high = a >> 64;
  448. out[0] = low;
  449. out[1] = high;
  450. a = ((uint128_t)small1[0]) * small2[1];
  451. low = a;
  452. high = a >> 64;
  453. out[1] += low;
  454. out[2] = high;
  455. a = ((uint128_t)small1[1]) * small2[0];
  456. low = a;
  457. high = a >> 64;
  458. out[1] += low;
  459. out[2] += high;
  460. a = ((uint128_t)small1[0]) * small2[2];
  461. low = a;
  462. high = a >> 64;
  463. out[2] += low;
  464. out[3] = high;
  465. a = ((uint128_t)small1[1]) * small2[1];
  466. low = a;
  467. high = a >> 64;
  468. out[2] += low;
  469. out[3] += high;
  470. a = ((uint128_t)small1[2]) * small2[0];
  471. low = a;
  472. high = a >> 64;
  473. out[2] += low;
  474. out[3] += high;
  475. a = ((uint128_t)small1[0]) * small2[3];
  476. low = a;
  477. high = a >> 64;
  478. out[3] += low;
  479. out[4] = high;
  480. a = ((uint128_t)small1[1]) * small2[2];
  481. low = a;
  482. high = a >> 64;
  483. out[3] += low;
  484. out[4] += high;
  485. a = ((uint128_t)small1[2]) * small2[1];
  486. low = a;
  487. high = a >> 64;
  488. out[3] += low;
  489. out[4] += high;
  490. a = ((uint128_t)small1[3]) * small2[0];
  491. low = a;
  492. high = a >> 64;
  493. out[3] += low;
  494. out[4] += high;
  495. a = ((uint128_t)small1[1]) * small2[3];
  496. low = a;
  497. high = a >> 64;
  498. out[4] += low;
  499. out[5] = high;
  500. a = ((uint128_t)small1[2]) * small2[2];
  501. low = a;
  502. high = a >> 64;
  503. out[4] += low;
  504. out[5] += high;
  505. a = ((uint128_t)small1[3]) * small2[1];
  506. low = a;
  507. high = a >> 64;
  508. out[4] += low;
  509. out[5] += high;
  510. a = ((uint128_t)small1[2]) * small2[3];
  511. low = a;
  512. high = a >> 64;
  513. out[5] += low;
  514. out[6] = high;
  515. a = ((uint128_t)small1[3]) * small2[2];
  516. low = a;
  517. high = a >> 64;
  518. out[5] += low;
  519. out[6] += high;
  520. a = ((uint128_t)small1[3]) * small2[3];
  521. low = a;
  522. high = a >> 64;
  523. out[6] += low;
  524. out[7] = high;
  525. }
  526. /* felem_mul sets |out| = |in1| * |in2|
  527. * On entry:
  528. * in1[i] < 2^109
  529. * in2[i] < 2^109
  530. * On exit:
  531. * out[i] < 7 * 2^64 < 2^67 */
  532. static void felem_mul(longfelem out, const felem in1, const felem in2) {
  533. smallfelem small1, small2;
  534. felem_shrink(small1, in1);
  535. felem_shrink(small2, in2);
  536. smallfelem_mul(out, small1, small2);
  537. }
  538. /* felem_small_mul sets |out| = |small1| * |in2|
  539. * On entry:
  540. * small1[i] < 2^64
  541. * in2[i] < 2^109
  542. * On exit:
  543. * out[i] < 7 * 2^64 < 2^67 */
  544. static void felem_small_mul(longfelem out, const smallfelem small1,
  545. const felem in2) {
  546. smallfelem small2;
  547. felem_shrink(small2, in2);
  548. smallfelem_mul(out, small1, small2);
  549. }
  550. #define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
  551. #define two100 (((limb)1) << 100)
  552. #define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
  553. /* zero100 is 0 mod p */
  554. static const felem zero100 = {two100m36m4, two100, two100m36p4, two100m36p4};
  555. /* Internal function for the different flavours of felem_reduce.
  556. * felem_reduce_ reduces the higher coefficients in[4]-in[7].
  557. * On entry:
  558. * out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
  559. * out[1] >= in[7] + 2^32*in[4]
  560. * out[2] >= in[5] + 2^32*in[5]
  561. * out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
  562. * On exit:
  563. * out[0] <= out[0] + in[4] + 2^32*in[5]
  564. * out[1] <= out[1] + in[5] + 2^33*in[6]
  565. * out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
  566. * out[3] <= out[3] + 2^32*in[4] + 3*in[7] */
  567. static void felem_reduce_(felem out, const longfelem in) {
  568. int128_t c;
  569. /* combine common terms from below */
  570. c = in[4] + (in[5] << 32);
  571. out[0] += c;
  572. out[3] -= c;
  573. c = in[5] - in[7];
  574. out[1] += c;
  575. out[2] -= c;
  576. /* the remaining terms */
  577. /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
  578. out[1] -= (in[4] << 32);
  579. out[3] += (in[4] << 32);
  580. /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
  581. out[2] -= (in[5] << 32);
  582. /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
  583. out[0] -= in[6];
  584. out[0] -= (in[6] << 32);
  585. out[1] += (in[6] << 33);
  586. out[2] += (in[6] * 2);
  587. out[3] -= (in[6] << 32);
  588. /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
  589. out[0] -= in[7];
  590. out[0] -= (in[7] << 32);
  591. out[2] += (in[7] << 33);
  592. out[3] += (in[7] * 3);
  593. }
  594. /* felem_reduce converts a longfelem into an felem.
  595. * To be called directly after felem_square or felem_mul.
  596. * On entry:
  597. * in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
  598. * in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
  599. * On exit:
  600. * out[i] < 2^101 */
  601. static void felem_reduce(felem out, const longfelem in) {
  602. out[0] = zero100[0] + in[0];
  603. out[1] = zero100[1] + in[1];
  604. out[2] = zero100[2] + in[2];
  605. out[3] = zero100[3] + in[3];
  606. felem_reduce_(out, in);
  607. /* out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
  608. * out[1] > 2^100 - 2^64 - 7*2^96 > 0
  609. * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
  610. * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
  611. *
  612. * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
  613. * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
  614. * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
  615. * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101 */
  616. }
  617. /* felem_reduce_zero105 converts a larger longfelem into an felem.
  618. * On entry:
  619. * in[0] < 2^71
  620. * On exit:
  621. * out[i] < 2^106 */
  622. static void felem_reduce_zero105(felem out, const longfelem in) {
  623. out[0] = zero105[0] + in[0];
  624. out[1] = zero105[1] + in[1];
  625. out[2] = zero105[2] + in[2];
  626. out[3] = zero105[3] + in[3];
  627. felem_reduce_(out, in);
  628. /* out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
  629. * out[1] > 2^105 - 2^71 - 2^103 > 0
  630. * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
  631. * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
  632. *
  633. * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  634. * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  635. * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
  636. * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106 */
  637. }
  638. /* subtract_u64 sets *result = *result - v and *carry to one if the
  639. * subtraction underflowed. */
  640. static void subtract_u64(u64 *result, u64 *carry, u64 v) {
  641. uint128_t r = *result;
  642. r -= v;
  643. *carry = (r >> 64) & 1;
  644. *result = (u64)r;
  645. }
  646. /* felem_contract converts |in| to its unique, minimal representation. On
  647. * entry: in[i] < 2^109. */
  648. static void felem_contract(smallfelem out, const felem in) {
  649. u64 all_equal_so_far = 0, result = 0;
  650. felem_shrink(out, in);
  651. /* small is minimal except that the value might be > p */
  652. all_equal_so_far--;
  653. /* We are doing a constant time test if out >= kPrime. We need to compare
  654. * each u64, from most-significant to least significant. For each one, if
  655. * all words so far have been equal (m is all ones) then a non-equal
  656. * result is the answer. Otherwise we continue. */
  657. unsigned i;
  658. for (i = 3; i < 4; i--) {
  659. u64 equal;
  660. uint128_t a = ((uint128_t)kPrime[i]) - out[i];
  661. /* if out[i] > kPrime[i] then a will underflow and the high 64-bits
  662. * will all be set. */
  663. result |= all_equal_so_far & ((u64)(a >> 64));
  664. /* if kPrime[i] == out[i] then |equal| will be all zeros and the
  665. * decrement will make it all ones. */
  666. equal = kPrime[i] ^ out[i];
  667. equal--;
  668. equal &= equal << 32;
  669. equal &= equal << 16;
  670. equal &= equal << 8;
  671. equal &= equal << 4;
  672. equal &= equal << 2;
  673. equal &= equal << 1;
  674. equal = ((s64)equal) >> 63;
  675. all_equal_so_far &= equal;
  676. }
  677. /* if all_equal_so_far is still all ones then the two values are equal
  678. * and so out >= kPrime is true. */
  679. result |= all_equal_so_far;
  680. /* if out >= kPrime then we subtract kPrime. */
  681. u64 carry;
  682. subtract_u64(&out[0], &carry, result & kPrime[0]);
  683. subtract_u64(&out[1], &carry, carry);
  684. subtract_u64(&out[2], &carry, carry);
  685. subtract_u64(&out[3], &carry, carry);
  686. subtract_u64(&out[1], &carry, result & kPrime[1]);
  687. subtract_u64(&out[2], &carry, carry);
  688. subtract_u64(&out[3], &carry, carry);
  689. subtract_u64(&out[2], &carry, result & kPrime[2]);
  690. subtract_u64(&out[3], &carry, carry);
  691. subtract_u64(&out[3], &carry, result & kPrime[3]);
  692. }
  693. static void smallfelem_square_contract(smallfelem out, const smallfelem in) {
  694. longfelem longtmp;
  695. felem tmp;
  696. smallfelem_square(longtmp, in);
  697. felem_reduce(tmp, longtmp);
  698. felem_contract(out, tmp);
  699. }
  700. static void smallfelem_mul_contract(smallfelem out, const smallfelem in1,
  701. const smallfelem in2) {
  702. longfelem longtmp;
  703. felem tmp;
  704. smallfelem_mul(longtmp, in1, in2);
  705. felem_reduce(tmp, longtmp);
  706. felem_contract(out, tmp);
  707. }
  708. /* felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  709. * otherwise.
  710. * On entry:
  711. * small[i] < 2^64 */
  712. static limb smallfelem_is_zero(const smallfelem small) {
  713. limb result;
  714. u64 is_p;
  715. u64 is_zero = small[0] | small[1] | small[2] | small[3];
  716. is_zero--;
  717. is_zero &= is_zero << 32;
  718. is_zero &= is_zero << 16;
  719. is_zero &= is_zero << 8;
  720. is_zero &= is_zero << 4;
  721. is_zero &= is_zero << 2;
  722. is_zero &= is_zero << 1;
  723. is_zero = ((s64)is_zero) >> 63;
  724. is_p = (small[0] ^ kPrime[0]) | (small[1] ^ kPrime[1]) |
  725. (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
  726. is_p--;
  727. is_p &= is_p << 32;
  728. is_p &= is_p << 16;
  729. is_p &= is_p << 8;
  730. is_p &= is_p << 4;
  731. is_p &= is_p << 2;
  732. is_p &= is_p << 1;
  733. is_p = ((s64)is_p) >> 63;
  734. is_zero |= is_p;
  735. result = is_zero;
  736. result |= ((limb)is_zero) << 64;
  737. return result;
  738. }
  739. static int smallfelem_is_zero_int(const smallfelem small) {
  740. return (int)(smallfelem_is_zero(small) & ((limb)1));
  741. }
  742. /* felem_inv calculates |out| = |in|^{-1}
  743. *
  744. * Based on Fermat's Little Theorem:
  745. * a^p = a (mod p)
  746. * a^{p-1} = 1 (mod p)
  747. * a^{p-2} = a^{-1} (mod p) */
  748. static void felem_inv(felem out, const felem in) {
  749. felem ftmp, ftmp2;
  750. /* each e_I will hold |in|^{2^I - 1} */
  751. felem e2, e4, e8, e16, e32, e64;
  752. longfelem tmp;
  753. unsigned i;
  754. felem_square(tmp, in);
  755. felem_reduce(ftmp, tmp); /* 2^1 */
  756. felem_mul(tmp, in, ftmp);
  757. felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
  758. felem_assign(e2, ftmp);
  759. felem_square(tmp, ftmp);
  760. felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
  761. felem_square(tmp, ftmp);
  762. felem_reduce(ftmp, tmp); /* 2^4 - 2^2 */
  763. felem_mul(tmp, ftmp, e2);
  764. felem_reduce(ftmp, tmp); /* 2^4 - 2^0 */
  765. felem_assign(e4, ftmp);
  766. felem_square(tmp, ftmp);
  767. felem_reduce(ftmp, tmp); /* 2^5 - 2^1 */
  768. felem_square(tmp, ftmp);
  769. felem_reduce(ftmp, tmp); /* 2^6 - 2^2 */
  770. felem_square(tmp, ftmp);
  771. felem_reduce(ftmp, tmp); /* 2^7 - 2^3 */
  772. felem_square(tmp, ftmp);
  773. felem_reduce(ftmp, tmp); /* 2^8 - 2^4 */
  774. felem_mul(tmp, ftmp, e4);
  775. felem_reduce(ftmp, tmp); /* 2^8 - 2^0 */
  776. felem_assign(e8, ftmp);
  777. for (i = 0; i < 8; i++) {
  778. felem_square(tmp, ftmp);
  779. felem_reduce(ftmp, tmp);
  780. } /* 2^16 - 2^8 */
  781. felem_mul(tmp, ftmp, e8);
  782. felem_reduce(ftmp, tmp); /* 2^16 - 2^0 */
  783. felem_assign(e16, ftmp);
  784. for (i = 0; i < 16; i++) {
  785. felem_square(tmp, ftmp);
  786. felem_reduce(ftmp, tmp);
  787. } /* 2^32 - 2^16 */
  788. felem_mul(tmp, ftmp, e16);
  789. felem_reduce(ftmp, tmp); /* 2^32 - 2^0 */
  790. felem_assign(e32, ftmp);
  791. for (i = 0; i < 32; i++) {
  792. felem_square(tmp, ftmp);
  793. felem_reduce(ftmp, tmp);
  794. } /* 2^64 - 2^32 */
  795. felem_assign(e64, ftmp);
  796. felem_mul(tmp, ftmp, in);
  797. felem_reduce(ftmp, tmp); /* 2^64 - 2^32 + 2^0 */
  798. for (i = 0; i < 192; i++) {
  799. felem_square(tmp, ftmp);
  800. felem_reduce(ftmp, tmp);
  801. } /* 2^256 - 2^224 + 2^192 */
  802. felem_mul(tmp, e64, e32);
  803. felem_reduce(ftmp2, tmp); /* 2^64 - 2^0 */
  804. for (i = 0; i < 16; i++) {
  805. felem_square(tmp, ftmp2);
  806. felem_reduce(ftmp2, tmp);
  807. } /* 2^80 - 2^16 */
  808. felem_mul(tmp, ftmp2, e16);
  809. felem_reduce(ftmp2, tmp); /* 2^80 - 2^0 */
  810. for (i = 0; i < 8; i++) {
  811. felem_square(tmp, ftmp2);
  812. felem_reduce(ftmp2, tmp);
  813. } /* 2^88 - 2^8 */
  814. felem_mul(tmp, ftmp2, e8);
  815. felem_reduce(ftmp2, tmp); /* 2^88 - 2^0 */
  816. for (i = 0; i < 4; i++) {
  817. felem_square(tmp, ftmp2);
  818. felem_reduce(ftmp2, tmp);
  819. } /* 2^92 - 2^4 */
  820. felem_mul(tmp, ftmp2, e4);
  821. felem_reduce(ftmp2, tmp); /* 2^92 - 2^0 */
  822. felem_square(tmp, ftmp2);
  823. felem_reduce(ftmp2, tmp); /* 2^93 - 2^1 */
  824. felem_square(tmp, ftmp2);
  825. felem_reduce(ftmp2, tmp); /* 2^94 - 2^2 */
  826. felem_mul(tmp, ftmp2, e2);
  827. felem_reduce(ftmp2, tmp); /* 2^94 - 2^0 */
  828. felem_square(tmp, ftmp2);
  829. felem_reduce(ftmp2, tmp); /* 2^95 - 2^1 */
  830. felem_square(tmp, ftmp2);
  831. felem_reduce(ftmp2, tmp); /* 2^96 - 2^2 */
  832. felem_mul(tmp, ftmp2, in);
  833. felem_reduce(ftmp2, tmp); /* 2^96 - 3 */
  834. felem_mul(tmp, ftmp2, ftmp);
  835. felem_reduce(out, tmp); /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
  836. }
  837. static void smallfelem_inv_contract(smallfelem out, const smallfelem in) {
  838. felem tmp;
  839. smallfelem_expand(tmp, in);
  840. felem_inv(tmp, tmp);
  841. felem_contract(out, tmp);
  842. }
  843. /* Group operations
  844. * ----------------
  845. *
  846. * Building on top of the field operations we have the operations on the
  847. * elliptic curve group itself. Points on the curve are represented in Jacobian
  848. * coordinates. */
  849. /* point_double calculates 2*(x_in, y_in, z_in)
  850. *
  851. * The method is taken from:
  852. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  853. *
  854. * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  855. * while x_out == y_in is not (maybe this works, but it's not tested). */
  856. static void point_double(felem x_out, felem y_out, felem z_out,
  857. const felem x_in, const felem y_in, const felem z_in) {
  858. longfelem tmp, tmp2;
  859. felem delta, gamma, beta, alpha, ftmp, ftmp2;
  860. smallfelem small1, small2;
  861. felem_assign(ftmp, x_in);
  862. /* ftmp[i] < 2^106 */
  863. felem_assign(ftmp2, x_in);
  864. /* ftmp2[i] < 2^106 */
  865. /* delta = z^2 */
  866. felem_square(tmp, z_in);
  867. felem_reduce(delta, tmp);
  868. /* delta[i] < 2^101 */
  869. /* gamma = y^2 */
  870. felem_square(tmp, y_in);
  871. felem_reduce(gamma, tmp);
  872. /* gamma[i] < 2^101 */
  873. felem_shrink(small1, gamma);
  874. /* beta = x*gamma */
  875. felem_small_mul(tmp, small1, x_in);
  876. felem_reduce(beta, tmp);
  877. /* beta[i] < 2^101 */
  878. /* alpha = 3*(x-delta)*(x+delta) */
  879. felem_diff(ftmp, delta);
  880. /* ftmp[i] < 2^105 + 2^106 < 2^107 */
  881. felem_sum(ftmp2, delta);
  882. /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
  883. felem_scalar(ftmp2, 3);
  884. /* ftmp2[i] < 3 * 2^107 < 2^109 */
  885. felem_mul(tmp, ftmp, ftmp2);
  886. felem_reduce(alpha, tmp);
  887. /* alpha[i] < 2^101 */
  888. felem_shrink(small2, alpha);
  889. /* x' = alpha^2 - 8*beta */
  890. smallfelem_square(tmp, small2);
  891. felem_reduce(x_out, tmp);
  892. felem_assign(ftmp, beta);
  893. felem_scalar(ftmp, 8);
  894. /* ftmp[i] < 8 * 2^101 = 2^104 */
  895. felem_diff(x_out, ftmp);
  896. /* x_out[i] < 2^105 + 2^101 < 2^106 */
  897. /* z' = (y + z)^2 - gamma - delta */
  898. felem_sum(delta, gamma);
  899. /* delta[i] < 2^101 + 2^101 = 2^102 */
  900. felem_assign(ftmp, y_in);
  901. felem_sum(ftmp, z_in);
  902. /* ftmp[i] < 2^106 + 2^106 = 2^107 */
  903. felem_square(tmp, ftmp);
  904. felem_reduce(z_out, tmp);
  905. felem_diff(z_out, delta);
  906. /* z_out[i] < 2^105 + 2^101 < 2^106 */
  907. /* y' = alpha*(4*beta - x') - 8*gamma^2 */
  908. felem_scalar(beta, 4);
  909. /* beta[i] < 4 * 2^101 = 2^103 */
  910. felem_diff_zero107(beta, x_out);
  911. /* beta[i] < 2^107 + 2^103 < 2^108 */
  912. felem_small_mul(tmp, small2, beta);
  913. /* tmp[i] < 7 * 2^64 < 2^67 */
  914. smallfelem_square(tmp2, small1);
  915. /* tmp2[i] < 7 * 2^64 */
  916. longfelem_scalar(tmp2, 8);
  917. /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
  918. longfelem_diff(tmp, tmp2);
  919. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  920. felem_reduce_zero105(y_out, tmp);
  921. /* y_out[i] < 2^106 */
  922. }
  923. /* point_double_small is the same as point_double, except that it operates on
  924. * smallfelems. */
  925. static void point_double_small(smallfelem x_out, smallfelem y_out,
  926. smallfelem z_out, const smallfelem x_in,
  927. const smallfelem y_in, const smallfelem z_in) {
  928. felem felem_x_out, felem_y_out, felem_z_out;
  929. felem felem_x_in, felem_y_in, felem_z_in;
  930. smallfelem_expand(felem_x_in, x_in);
  931. smallfelem_expand(felem_y_in, y_in);
  932. smallfelem_expand(felem_z_in, z_in);
  933. point_double(felem_x_out, felem_y_out, felem_z_out, felem_x_in, felem_y_in,
  934. felem_z_in);
  935. felem_shrink(x_out, felem_x_out);
  936. felem_shrink(y_out, felem_y_out);
  937. felem_shrink(z_out, felem_z_out);
  938. }
  939. /* copy_conditional copies in to out iff mask is all ones. */
  940. static void copy_conditional(felem out, const felem in, limb mask) {
  941. unsigned i;
  942. for (i = 0; i < NLIMBS; ++i) {
  943. const limb tmp = mask & (in[i] ^ out[i]);
  944. out[i] ^= tmp;
  945. }
  946. }
  947. /* copy_small_conditional copies in to out iff mask is all ones. */
  948. static void copy_small_conditional(felem out, const smallfelem in, limb mask) {
  949. unsigned i;
  950. const u64 mask64 = mask;
  951. for (i = 0; i < NLIMBS; ++i) {
  952. out[i] = ((limb)(in[i] & mask64)) | (out[i] & ~mask);
  953. }
  954. }
  955. /* point_add calcuates (x1, y1, z1) + (x2, y2, z2)
  956. *
  957. * The method is taken from:
  958. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  959. * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  960. *
  961. * This function includes a branch for checking whether the two input points
  962. * are equal, (while not equal to the point at infinity). This case never
  963. * happens during single point multiplication, so there is no timing leak for
  964. * ECDH or ECDSA signing. */
  965. static void point_add(felem x3, felem y3, felem z3, const felem x1,
  966. const felem y1, const felem z1, const int mixed,
  967. const smallfelem x2, const smallfelem y2,
  968. const smallfelem z2) {
  969. felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
  970. longfelem tmp, tmp2;
  971. smallfelem small1, small2, small3, small4, small5;
  972. limb x_equal, y_equal, z1_is_zero, z2_is_zero;
  973. felem_shrink(small3, z1);
  974. z1_is_zero = smallfelem_is_zero(small3);
  975. z2_is_zero = smallfelem_is_zero(z2);
  976. /* ftmp = z1z1 = z1**2 */
  977. smallfelem_square(tmp, small3);
  978. felem_reduce(ftmp, tmp);
  979. /* ftmp[i] < 2^101 */
  980. felem_shrink(small1, ftmp);
  981. if (!mixed) {
  982. /* ftmp2 = z2z2 = z2**2 */
  983. smallfelem_square(tmp, z2);
  984. felem_reduce(ftmp2, tmp);
  985. /* ftmp2[i] < 2^101 */
  986. felem_shrink(small2, ftmp2);
  987. felem_shrink(small5, x1);
  988. /* u1 = ftmp3 = x1*z2z2 */
  989. smallfelem_mul(tmp, small5, small2);
  990. felem_reduce(ftmp3, tmp);
  991. /* ftmp3[i] < 2^101 */
  992. /* ftmp5 = z1 + z2 */
  993. felem_assign(ftmp5, z1);
  994. felem_small_sum(ftmp5, z2);
  995. /* ftmp5[i] < 2^107 */
  996. /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
  997. felem_square(tmp, ftmp5);
  998. felem_reduce(ftmp5, tmp);
  999. /* ftmp2 = z2z2 + z1z1 */
  1000. felem_sum(ftmp2, ftmp);
  1001. /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
  1002. felem_diff(ftmp5, ftmp2);
  1003. /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
  1004. /* ftmp2 = z2 * z2z2 */
  1005. smallfelem_mul(tmp, small2, z2);
  1006. felem_reduce(ftmp2, tmp);
  1007. /* s1 = ftmp2 = y1 * z2**3 */
  1008. felem_mul(tmp, y1, ftmp2);
  1009. felem_reduce(ftmp6, tmp);
  1010. /* ftmp6[i] < 2^101 */
  1011. } else {
  1012. /* We'll assume z2 = 1 (special case z2 = 0 is handled later). */
  1013. /* u1 = ftmp3 = x1*z2z2 */
  1014. felem_assign(ftmp3, x1);
  1015. /* ftmp3[i] < 2^106 */
  1016. /* ftmp5 = 2z1z2 */
  1017. felem_assign(ftmp5, z1);
  1018. felem_scalar(ftmp5, 2);
  1019. /* ftmp5[i] < 2*2^106 = 2^107 */
  1020. /* s1 = ftmp2 = y1 * z2**3 */
  1021. felem_assign(ftmp6, y1);
  1022. /* ftmp6[i] < 2^106 */
  1023. }
  1024. /* u2 = x2*z1z1 */
  1025. smallfelem_mul(tmp, x2, small1);
  1026. felem_reduce(ftmp4, tmp);
  1027. /* h = ftmp4 = u2 - u1 */
  1028. felem_diff_zero107(ftmp4, ftmp3);
  1029. /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
  1030. felem_shrink(small4, ftmp4);
  1031. x_equal = smallfelem_is_zero(small4);
  1032. /* z_out = ftmp5 * h */
  1033. felem_small_mul(tmp, small4, ftmp5);
  1034. felem_reduce(z_out, tmp);
  1035. /* z_out[i] < 2^101 */
  1036. /* ftmp = z1 * z1z1 */
  1037. smallfelem_mul(tmp, small1, small3);
  1038. felem_reduce(ftmp, tmp);
  1039. /* s2 = tmp = y2 * z1**3 */
  1040. felem_small_mul(tmp, y2, ftmp);
  1041. felem_reduce(ftmp5, tmp);
  1042. /* r = ftmp5 = (s2 - s1)*2 */
  1043. felem_diff_zero107(ftmp5, ftmp6);
  1044. /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
  1045. felem_scalar(ftmp5, 2);
  1046. /* ftmp5[i] < 2^109 */
  1047. felem_shrink(small1, ftmp5);
  1048. y_equal = smallfelem_is_zero(small1);
  1049. if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
  1050. point_double(x3, y3, z3, x1, y1, z1);
  1051. return;
  1052. }
  1053. /* I = ftmp = (2h)**2 */
  1054. felem_assign(ftmp, ftmp4);
  1055. felem_scalar(ftmp, 2);
  1056. /* ftmp[i] < 2*2^108 = 2^109 */
  1057. felem_square(tmp, ftmp);
  1058. felem_reduce(ftmp, tmp);
  1059. /* J = ftmp2 = h * I */
  1060. felem_mul(tmp, ftmp4, ftmp);
  1061. felem_reduce(ftmp2, tmp);
  1062. /* V = ftmp4 = U1 * I */
  1063. felem_mul(tmp, ftmp3, ftmp);
  1064. felem_reduce(ftmp4, tmp);
  1065. /* x_out = r**2 - J - 2V */
  1066. smallfelem_square(tmp, small1);
  1067. felem_reduce(x_out, tmp);
  1068. felem_assign(ftmp3, ftmp4);
  1069. felem_scalar(ftmp4, 2);
  1070. felem_sum(ftmp4, ftmp2);
  1071. /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
  1072. felem_diff(x_out, ftmp4);
  1073. /* x_out[i] < 2^105 + 2^101 */
  1074. /* y_out = r(V-x_out) - 2 * s1 * J */
  1075. felem_diff_zero107(ftmp3, x_out);
  1076. /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
  1077. felem_small_mul(tmp, small1, ftmp3);
  1078. felem_mul(tmp2, ftmp6, ftmp2);
  1079. longfelem_scalar(tmp2, 2);
  1080. /* tmp2[i] < 2*2^67 = 2^68 */
  1081. longfelem_diff(tmp, tmp2);
  1082. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  1083. felem_reduce_zero105(y_out, tmp);
  1084. /* y_out[i] < 2^106 */
  1085. copy_small_conditional(x_out, x2, z1_is_zero);
  1086. copy_conditional(x_out, x1, z2_is_zero);
  1087. copy_small_conditional(y_out, y2, z1_is_zero);
  1088. copy_conditional(y_out, y1, z2_is_zero);
  1089. copy_small_conditional(z_out, z2, z1_is_zero);
  1090. copy_conditional(z_out, z1, z2_is_zero);
  1091. felem_assign(x3, x_out);
  1092. felem_assign(y3, y_out);
  1093. felem_assign(z3, z_out);
  1094. }
  1095. /* point_add_small is the same as point_add, except that it operates on
  1096. * smallfelems. */
  1097. static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
  1098. smallfelem x1, smallfelem y1, smallfelem z1,
  1099. smallfelem x2, smallfelem y2, smallfelem z2) {
  1100. felem felem_x3, felem_y3, felem_z3;
  1101. felem felem_x1, felem_y1, felem_z1;
  1102. smallfelem_expand(felem_x1, x1);
  1103. smallfelem_expand(felem_y1, y1);
  1104. smallfelem_expand(felem_z1, z1);
  1105. point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0, x2,
  1106. y2, z2);
  1107. felem_shrink(x3, felem_x3);
  1108. felem_shrink(y3, felem_y3);
  1109. felem_shrink(z3, felem_z3);
  1110. }
  1111. /* Base point pre computation
  1112. * --------------------------
  1113. *
  1114. * Two different sorts of precomputed tables are used in the following code.
  1115. * Each contain various points on the curve, where each point is three field
  1116. * elements (x, y, z).
  1117. *
  1118. * For the base point table, z is usually 1 (0 for the point at infinity).
  1119. * This table has 2 * 16 elements, starting with the following:
  1120. * index | bits | point
  1121. * ------+---------+------------------------------
  1122. * 0 | 0 0 0 0 | 0G
  1123. * 1 | 0 0 0 1 | 1G
  1124. * 2 | 0 0 1 0 | 2^64G
  1125. * 3 | 0 0 1 1 | (2^64 + 1)G
  1126. * 4 | 0 1 0 0 | 2^128G
  1127. * 5 | 0 1 0 1 | (2^128 + 1)G
  1128. * 6 | 0 1 1 0 | (2^128 + 2^64)G
  1129. * 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
  1130. * 8 | 1 0 0 0 | 2^192G
  1131. * 9 | 1 0 0 1 | (2^192 + 1)G
  1132. * 10 | 1 0 1 0 | (2^192 + 2^64)G
  1133. * 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
  1134. * 12 | 1 1 0 0 | (2^192 + 2^128)G
  1135. * 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
  1136. * 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
  1137. * 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
  1138. * followed by a copy of this with each element multiplied by 2^32.
  1139. *
  1140. * The reason for this is so that we can clock bits into four different
  1141. * locations when doing simple scalar multiplies against the base point,
  1142. * and then another four locations using the second 16 elements.
  1143. *
  1144. * Tables for other points have table[i] = iG for i in 0 .. 16. */
  1145. /* gmul is the table of precomputed base points */
  1146. static const smallfelem gmul[2][16][3] = {
  1147. {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
  1148. {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
  1149. 0x6b17d1f2e12c4247},
  1150. {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
  1151. 0x4fe342e2fe1a7f9b},
  1152. {1, 0, 0, 0}},
  1153. {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
  1154. 0x0fa822bc2811aaa5},
  1155. {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
  1156. 0xbff44ae8f5dba80d},
  1157. {1, 0, 0, 0}},
  1158. {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
  1159. 0x300a4bbc89d6726f},
  1160. {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
  1161. 0x72aac7e0d09b4644},
  1162. {1, 0, 0, 0}},
  1163. {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
  1164. 0x447d739beedb5e67},
  1165. {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
  1166. 0x2d4825ab834131ee},
  1167. {1, 0, 0, 0}},
  1168. {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
  1169. 0xef9519328a9c72ff},
  1170. {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
  1171. 0x611e9fc37dbb2c9b},
  1172. {1, 0, 0, 0}},
  1173. {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
  1174. 0x550663797b51f5d8},
  1175. {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
  1176. 0x157164848aecb851},
  1177. {1, 0, 0, 0}},
  1178. {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
  1179. 0xeb5d7745b21141ea},
  1180. {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
  1181. 0xeafd72ebdbecc17b},
  1182. {1, 0, 0, 0}},
  1183. {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
  1184. 0xa6d39677a7849276},
  1185. {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
  1186. 0x674f84749b0b8816},
  1187. {1, 0, 0, 0}},
  1188. {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
  1189. 0x4e769e7672c9ddad},
  1190. {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
  1191. 0x42b99082de830663},
  1192. {1, 0, 0, 0}},
  1193. {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
  1194. 0x78878ef61c6ce04d},
  1195. {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
  1196. 0xb6cb3f5d7b72c321},
  1197. {1, 0, 0, 0}},
  1198. {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
  1199. 0x0c88bc4d716b1287},
  1200. {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
  1201. 0xdd5ddea3f3901dc6},
  1202. {1, 0, 0, 0}},
  1203. {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
  1204. 0x68f344af6b317466},
  1205. {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
  1206. 0x31b9c405f8540a20},
  1207. {1, 0, 0, 0}},
  1208. {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
  1209. 0x4052bf4b6f461db9},
  1210. {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
  1211. 0xfecf4d5190b0fc61},
  1212. {1, 0, 0, 0}},
  1213. {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
  1214. 0x1eddbae2c802e41a},
  1215. {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
  1216. 0x43104d86560ebcfc},
  1217. {1, 0, 0, 0}},
  1218. {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
  1219. 0xb48e26b484f7a21c},
  1220. {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
  1221. 0xfac015404d4d3dab},
  1222. {1, 0, 0, 0}}},
  1223. {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
  1224. {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
  1225. 0x7fe36b40af22af89},
  1226. {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
  1227. 0xe697d45825b63624},
  1228. {1, 0, 0, 0}},
  1229. {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
  1230. 0x4a5b506612a677a6},
  1231. {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
  1232. 0xeb13461ceac089f1},
  1233. {1, 0, 0, 0}},
  1234. {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
  1235. 0x0781b8291c6a220a},
  1236. {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
  1237. 0x690cde8df0151593},
  1238. {1, 0, 0, 0}},
  1239. {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
  1240. 0x8a535f566ec73617},
  1241. {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
  1242. 0x0455c08468b08bd7},
  1243. {1, 0, 0, 0}},
  1244. {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
  1245. 0x06bada7ab77f8276},
  1246. {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
  1247. 0x5b476dfd0e6cb18a},
  1248. {1, 0, 0, 0}},
  1249. {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
  1250. 0x3e29864e8a2ec908},
  1251. {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
  1252. 0x239b90ea3dc31e7e},
  1253. {1, 0, 0, 0}},
  1254. {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
  1255. 0x820f4dd949f72ff7},
  1256. {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
  1257. 0x140406ec783a05ec},
  1258. {1, 0, 0, 0}},
  1259. {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
  1260. 0x68f6b8542783dfee},
  1261. {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
  1262. 0xcbe1feba92e40ce6},
  1263. {1, 0, 0, 0}},
  1264. {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
  1265. 0xd0b2f94d2f420109},
  1266. {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
  1267. 0x971459828b0719e5},
  1268. {1, 0, 0, 0}},
  1269. {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
  1270. 0x961610004a866aba},
  1271. {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
  1272. 0x7acb9fadcee75e44},
  1273. {1, 0, 0, 0}},
  1274. {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
  1275. 0x24eb9acca333bf5b},
  1276. {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
  1277. 0x69f891c5acd079cc},
  1278. {1, 0, 0, 0}},
  1279. {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
  1280. 0xe51f547c5972a107},
  1281. {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
  1282. 0x1c309a2b25bb1387},
  1283. {1, 0, 0, 0}},
  1284. {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
  1285. 0x20b87b8aa2c4e503},
  1286. {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
  1287. 0xf5c6fa49919776be},
  1288. {1, 0, 0, 0}},
  1289. {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
  1290. 0x1ed7d1b9332010b9},
  1291. {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
  1292. 0x3a2b03f03217257a},
  1293. {1, 0, 0, 0}},
  1294. {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
  1295. 0x15fee545c78dd9f6},
  1296. {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
  1297. 0x4ab5b6b2b8753f81},
  1298. {1, 0, 0, 0}}}};
  1299. /* select_point selects the |idx|th point from a precomputation table and
  1300. * copies it to out. */
  1301. static void select_point(const u64 idx, unsigned int size,
  1302. const smallfelem pre_comp[16][3], smallfelem out[3]) {
  1303. unsigned i, j;
  1304. u64 *outlimbs = &out[0][0];
  1305. memset(outlimbs, 0, 3 * sizeof(smallfelem));
  1306. for (i = 0; i < size; i++) {
  1307. const u64 *inlimbs = (u64 *)&pre_comp[i][0][0];
  1308. u64 mask = i ^ idx;
  1309. mask |= mask >> 4;
  1310. mask |= mask >> 2;
  1311. mask |= mask >> 1;
  1312. mask &= 1;
  1313. mask--;
  1314. for (j = 0; j < NLIMBS * 3; j++) {
  1315. outlimbs[j] |= inlimbs[j] & mask;
  1316. }
  1317. }
  1318. }
  1319. /* get_bit returns the |i|th bit in |in| */
  1320. static char get_bit(const felem_bytearray in, int i) {
  1321. if (i < 0 || i >= 256) {
  1322. return 0;
  1323. }
  1324. return (in[i >> 3] >> (i & 7)) & 1;
  1325. }
  1326. /* Interleaved point multiplication using precomputed point multiples: The
  1327. * small point multiples 0*P, 1*P, ..., 17*P are in pre_comp[], the scalars
  1328. * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  1329. * generator, using certain (large) precomputed multiples in g_pre_comp.
  1330. * Output point (X, Y, Z) is stored in x_out, y_out, z_out. */
  1331. static void batch_mul(felem x_out, felem y_out, felem z_out,
  1332. const felem_bytearray scalars[],
  1333. const unsigned num_points, const u8 *g_scalar,
  1334. const int mixed, const smallfelem pre_comp[][17][3],
  1335. const smallfelem g_pre_comp[2][16][3]) {
  1336. int i, skip;
  1337. unsigned num, gen_mul = (g_scalar != NULL);
  1338. felem nq[3], ftmp;
  1339. smallfelem tmp[3];
  1340. u64 bits;
  1341. u8 sign, digit;
  1342. /* set nq to the point at infinity */
  1343. memset(nq, 0, 3 * sizeof(felem));
  1344. /* Loop over all scalars msb-to-lsb, interleaving additions of multiples
  1345. * of the generator (two in each of the last 32 rounds) and additions of
  1346. * other points multiples (every 5th round). */
  1347. skip = 1; /* save two point operations in the first
  1348. * round */
  1349. for (i = (num_points ? 255 : 31); i >= 0; --i) {
  1350. /* double */
  1351. if (!skip) {
  1352. point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
  1353. }
  1354. /* add multiples of the generator */
  1355. if (gen_mul && i <= 31) {
  1356. /* first, look 32 bits upwards */
  1357. bits = get_bit(g_scalar, i + 224) << 3;
  1358. bits |= get_bit(g_scalar, i + 160) << 2;
  1359. bits |= get_bit(g_scalar, i + 96) << 1;
  1360. bits |= get_bit(g_scalar, i + 32);
  1361. /* select the point to add, in constant time */
  1362. select_point(bits, 16, g_pre_comp[1], tmp);
  1363. if (!skip) {
  1364. /* Arg 1 below is for "mixed" */
  1365. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1, tmp[0], tmp[1],
  1366. tmp[2]);
  1367. } else {
  1368. smallfelem_expand(nq[0], tmp[0]);
  1369. smallfelem_expand(nq[1], tmp[1]);
  1370. smallfelem_expand(nq[2], tmp[2]);
  1371. skip = 0;
  1372. }
  1373. /* second, look at the current position */
  1374. bits = get_bit(g_scalar, i + 192) << 3;
  1375. bits |= get_bit(g_scalar, i + 128) << 2;
  1376. bits |= get_bit(g_scalar, i + 64) << 1;
  1377. bits |= get_bit(g_scalar, i);
  1378. /* select the point to add, in constant time */
  1379. select_point(bits, 16, g_pre_comp[0], tmp);
  1380. /* Arg 1 below is for "mixed" */
  1381. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1, tmp[0], tmp[1],
  1382. tmp[2]);
  1383. }
  1384. /* do other additions every 5 doublings */
  1385. if (num_points && (i % 5 == 0)) {
  1386. /* loop over all scalars */
  1387. for (num = 0; num < num_points; ++num) {
  1388. bits = get_bit(scalars[num], i + 4) << 5;
  1389. bits |= get_bit(scalars[num], i + 3) << 4;
  1390. bits |= get_bit(scalars[num], i + 2) << 3;
  1391. bits |= get_bit(scalars[num], i + 1) << 2;
  1392. bits |= get_bit(scalars[num], i) << 1;
  1393. bits |= get_bit(scalars[num], i - 1);
  1394. ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
  1395. /* select the point to add or subtract, in constant time. */
  1396. select_point(digit, 17, pre_comp[num], tmp);
  1397. smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
  1398. * point */
  1399. copy_small_conditional(ftmp, tmp[1], (((limb)sign) - 1));
  1400. felem_contract(tmp[1], ftmp);
  1401. if (!skip) {
  1402. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], mixed, tmp[0],
  1403. tmp[1], tmp[2]);
  1404. } else {
  1405. smallfelem_expand(nq[0], tmp[0]);
  1406. smallfelem_expand(nq[1], tmp[1]);
  1407. smallfelem_expand(nq[2], tmp[2]);
  1408. skip = 0;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. felem_assign(x_out, nq[0]);
  1414. felem_assign(y_out, nq[1]);
  1415. felem_assign(z_out, nq[2]);
  1416. }
  1417. /* Precomputation for the group generator. */
  1418. typedef struct {
  1419. smallfelem g_pre_comp[2][16][3];
  1420. } NISTP256_PRE_COMP;
  1421. /******************************************************************************/
  1422. /*
  1423. * OPENSSL EC_METHOD FUNCTIONS
  1424. */
  1425. int ec_GFp_nistp256_group_init(EC_GROUP *group) {
  1426. int ret = ec_GFp_simple_group_init(group);
  1427. group->a_is_minus3 = 1;
  1428. return ret;
  1429. }
  1430. int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
  1431. const BIGNUM *a, const BIGNUM *b,
  1432. BN_CTX *ctx) {
  1433. int ret = 0;
  1434. BN_CTX *new_ctx = NULL;
  1435. BIGNUM *curve_p, *curve_a, *curve_b;
  1436. if (ctx == NULL) {
  1437. if ((ctx = new_ctx = BN_CTX_new()) == NULL) {
  1438. return 0;
  1439. }
  1440. }
  1441. BN_CTX_start(ctx);
  1442. if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
  1443. ((curve_a = BN_CTX_get(ctx)) == NULL) ||
  1444. ((curve_b = BN_CTX_get(ctx)) == NULL)) {
  1445. goto err;
  1446. }
  1447. BN_bin2bn(nistp256_curve_params[0], sizeof(felem_bytearray), curve_p);
  1448. BN_bin2bn(nistp256_curve_params[1], sizeof(felem_bytearray), curve_a);
  1449. BN_bin2bn(nistp256_curve_params[2], sizeof(felem_bytearray), curve_b);
  1450. if (BN_cmp(curve_p, p) ||
  1451. BN_cmp(curve_a, a) ||
  1452. BN_cmp(curve_b, b)) {
  1453. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_group_set_curve,
  1454. EC_R_WRONG_CURVE_PARAMETERS);
  1455. goto err;
  1456. }
  1457. ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  1458. err:
  1459. BN_CTX_end(ctx);
  1460. BN_CTX_free(new_ctx);
  1461. return ret;
  1462. }
  1463. /* Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  1464. * (X/Z^2, Y/Z^3). */
  1465. int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  1466. const EC_POINT *point,
  1467. BIGNUM *x, BIGNUM *y,
  1468. BN_CTX *ctx) {
  1469. felem z1, z2, x_in, y_in;
  1470. smallfelem x_out, y_out;
  1471. longfelem tmp;
  1472. if (EC_POINT_is_at_infinity(group, point)) {
  1473. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_point_get_affine_coordinates,
  1474. EC_R_POINT_AT_INFINITY);
  1475. return 0;
  1476. }
  1477. if (!BN_to_felem(x_in, &point->X) ||
  1478. !BN_to_felem(y_in, &point->Y) ||
  1479. !BN_to_felem(z1, &point->Z)) {
  1480. return 0;
  1481. }
  1482. felem_inv(z2, z1);
  1483. felem_square(tmp, z2);
  1484. felem_reduce(z1, tmp);
  1485. felem_mul(tmp, x_in, z1);
  1486. felem_reduce(x_in, tmp);
  1487. felem_contract(x_out, x_in);
  1488. if (x != NULL && !smallfelem_to_BN(x, x_out)) {
  1489. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_point_get_affine_coordinates,
  1490. ERR_R_BN_LIB);
  1491. return 0;
  1492. }
  1493. felem_mul(tmp, z1, z2);
  1494. felem_reduce(z1, tmp);
  1495. felem_mul(tmp, y_in, z1);
  1496. felem_reduce(y_in, tmp);
  1497. felem_contract(y_out, y_in);
  1498. if (y != NULL && !smallfelem_to_BN(y, y_out)) {
  1499. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_point_get_affine_coordinates,
  1500. ERR_R_BN_LIB);
  1501. return 0;
  1502. }
  1503. return 1;
  1504. }
  1505. /* points below is of size |num|, and tmp_smallfelems is of size |num+1| */
  1506. static void make_points_affine(size_t num, smallfelem points[][3],
  1507. smallfelem tmp_smallfelems[]) {
  1508. /* Runs in constant time, unless an input is the point at infinity (which
  1509. * normally shouldn't happen). */
  1510. ec_GFp_nistp_points_make_affine_internal(
  1511. num, points, sizeof(smallfelem), tmp_smallfelems,
  1512. (void (*)(void *))smallfelem_one,
  1513. (int (*)(const void *))smallfelem_is_zero_int,
  1514. (void (*)(void *, const void *))smallfelem_assign,
  1515. (void (*)(void *, const void *))smallfelem_square_contract,
  1516. (void (*)(void *, const void *, const void *))smallfelem_mul_contract,
  1517. (void (*)(void *, const void *))smallfelem_inv_contract,
  1518. /* nothing to contract */
  1519. (void (*)(void *, const void *))smallfelem_assign);
  1520. }
  1521. /* Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  1522. * values Result is stored in r (r can equal one of the inputs). */
  1523. int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
  1524. const BIGNUM *scalar, size_t num,
  1525. const EC_POINT *points[],
  1526. const BIGNUM *scalars[], BN_CTX *ctx) {
  1527. int ret = 0;
  1528. int j;
  1529. int mixed = 0;
  1530. BN_CTX *new_ctx = NULL;
  1531. BIGNUM *x, *y, *z, *tmp_scalar;
  1532. felem_bytearray g_secret;
  1533. felem_bytearray *secrets = NULL;
  1534. smallfelem(*pre_comp)[17][3] = NULL;
  1535. smallfelem *tmp_smallfelems = NULL;
  1536. felem_bytearray tmp;
  1537. unsigned i, num_bytes;
  1538. int have_pre_comp = 0;
  1539. size_t num_points = num;
  1540. smallfelem x_in, y_in, z_in;
  1541. felem x_out, y_out, z_out;
  1542. const smallfelem(*g_pre_comp)[16][3] = NULL;
  1543. EC_POINT *generator = NULL;
  1544. const EC_POINT *p = NULL;
  1545. const BIGNUM *p_scalar = NULL;
  1546. if (ctx == NULL) {
  1547. ctx = new_ctx = BN_CTX_new();
  1548. if (ctx == NULL) {
  1549. return 0;
  1550. }
  1551. }
  1552. BN_CTX_start(ctx);
  1553. if ((x = BN_CTX_get(ctx)) == NULL ||
  1554. (y = BN_CTX_get(ctx)) == NULL ||
  1555. (z = BN_CTX_get(ctx)) == NULL ||
  1556. (tmp_scalar = BN_CTX_get(ctx)) == NULL) {
  1557. goto err;
  1558. }
  1559. if (scalar != NULL) {
  1560. /* try to use the standard precomputation */
  1561. g_pre_comp = &gmul[0];
  1562. generator = EC_POINT_new(group);
  1563. if (generator == NULL) {
  1564. goto err;
  1565. }
  1566. /* get the generator from precomputation */
  1567. if (!smallfelem_to_BN(x, g_pre_comp[0][1][0]) ||
  1568. !smallfelem_to_BN(y, g_pre_comp[0][1][1]) ||
  1569. !smallfelem_to_BN(z, g_pre_comp[0][1][2])) {
  1570. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_points_mul, ERR_R_BN_LIB);
  1571. goto err;
  1572. }
  1573. if (!ec_point_set_Jprojective_coordinates_GFp(group, generator, x, y, z,
  1574. ctx)) {
  1575. goto err;
  1576. }
  1577. if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
  1578. /* precomputation matches generator */
  1579. have_pre_comp = 1;
  1580. } else {
  1581. /* we don't have valid precomputation: treat the generator as a
  1582. * random point. */
  1583. num_points++;
  1584. }
  1585. }
  1586. if (num_points > 0) {
  1587. if (num_points >= 3) {
  1588. /* unless we precompute multiples for just one or two points,
  1589. * converting those into affine form is time well spent */
  1590. mixed = 1;
  1591. }
  1592. secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
  1593. pre_comp = OPENSSL_malloc(num_points * 17 * 3 * sizeof(smallfelem));
  1594. if (mixed) {
  1595. tmp_smallfelems =
  1596. OPENSSL_malloc((num_points * 17 + 1) * sizeof(smallfelem));
  1597. }
  1598. if (secrets == NULL || pre_comp == NULL ||
  1599. (mixed && tmp_smallfelems == NULL)) {
  1600. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_points_mul, ERR_R_MALLOC_FAILURE);
  1601. goto err;
  1602. }
  1603. /* we treat NULL scalars as 0, and NULL points as points at infinity,
  1604. * i.e., they contribute nothing to the linear combination. */
  1605. memset(secrets, 0, num_points * sizeof(felem_bytearray));
  1606. memset(pre_comp, 0, num_points * 17 * 3 * sizeof(smallfelem));
  1607. for (i = 0; i < num_points; ++i) {
  1608. if (i == num) {
  1609. /* we didn't have a valid precomputation, so we pick the generator. */
  1610. p = EC_GROUP_get0_generator(group);
  1611. p_scalar = scalar;
  1612. } else {
  1613. /* the i^th point */
  1614. p = points[i];
  1615. p_scalar = scalars[i];
  1616. }
  1617. if (p_scalar != NULL && p != NULL) {
  1618. /* reduce scalar to 0 <= scalar < 2^256 */
  1619. if (BN_num_bits(p_scalar) > 256 || BN_is_negative(p_scalar)) {
  1620. /* this is an unusual input, and we don't guarantee
  1621. * constant-timeness. */
  1622. if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
  1623. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_points_mul, ERR_R_BN_LIB);
  1624. goto err;
  1625. }
  1626. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1627. } else {
  1628. num_bytes = BN_bn2bin(p_scalar, tmp);
  1629. }
  1630. flip_endian(secrets[i], tmp, num_bytes);
  1631. /* precompute multiples */
  1632. if (!BN_to_felem(x_out, &p->X) ||
  1633. !BN_to_felem(y_out, &p->Y) ||
  1634. !BN_to_felem(z_out, &p->Z)) {
  1635. goto err;
  1636. }
  1637. felem_shrink(pre_comp[i][1][0], x_out);
  1638. felem_shrink(pre_comp[i][1][1], y_out);
  1639. felem_shrink(pre_comp[i][1][2], z_out);
  1640. for (j = 2; j <= 16; ++j) {
  1641. if (j & 1) {
  1642. point_add_small(pre_comp[i][j][0], pre_comp[i][j][1],
  1643. pre_comp[i][j][2], pre_comp[i][1][0],
  1644. pre_comp[i][1][1], pre_comp[i][1][2],
  1645. pre_comp[i][j - 1][0], pre_comp[i][j - 1][1],
  1646. pre_comp[i][j - 1][2]);
  1647. } else {
  1648. point_double_small(pre_comp[i][j][0], pre_comp[i][j][1],
  1649. pre_comp[i][j][2], pre_comp[i][j / 2][0],
  1650. pre_comp[i][j / 2][1], pre_comp[i][j / 2][2]);
  1651. }
  1652. }
  1653. }
  1654. }
  1655. if (mixed) {
  1656. make_points_affine(num_points * 17, pre_comp[0], tmp_smallfelems);
  1657. }
  1658. }
  1659. /* the scalar for the generator */
  1660. if (scalar != NULL && have_pre_comp) {
  1661. memset(g_secret, 0, sizeof(g_secret));
  1662. /* reduce scalar to 0 <= scalar < 2^256 */
  1663. if (BN_num_bits(scalar) > 256 || BN_is_negative(scalar)) {
  1664. /* this is an unusual input, and we don't guarantee
  1665. * constant-timeness. */
  1666. if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
  1667. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_points_mul, ERR_R_BN_LIB);
  1668. goto err;
  1669. }
  1670. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1671. } else {
  1672. num_bytes = BN_bn2bin(scalar, tmp);
  1673. }
  1674. flip_endian(g_secret, tmp, num_bytes);
  1675. /* do the multiplication with generator precomputation */
  1676. batch_mul(x_out, y_out, z_out, (const felem_bytearray(*))secrets,
  1677. num_points, g_secret, mixed, (const smallfelem(*)[17][3])pre_comp,
  1678. g_pre_comp);
  1679. } else {
  1680. /* do the multiplication without generator precomputation */
  1681. batch_mul(x_out, y_out, z_out, (const felem_bytearray(*))secrets,
  1682. num_points, NULL, mixed, (const smallfelem(*)[17][3])pre_comp,
  1683. NULL);
  1684. }
  1685. /* reduce the output to its unique minimal representation */
  1686. felem_contract(x_in, x_out);
  1687. felem_contract(y_in, y_out);
  1688. felem_contract(z_in, z_out);
  1689. if (!smallfelem_to_BN(x, x_in) ||
  1690. !smallfelem_to_BN(y, y_in) ||
  1691. !smallfelem_to_BN(z, z_in)) {
  1692. OPENSSL_PUT_ERROR(EC, ec_GFp_nistp256_points_mul, ERR_R_BN_LIB);
  1693. goto err;
  1694. }
  1695. ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
  1696. err:
  1697. BN_CTX_end(ctx);
  1698. EC_POINT_free(generator);
  1699. BN_CTX_free(new_ctx);
  1700. OPENSSL_free(secrets);
  1701. OPENSSL_free(pre_comp);
  1702. OPENSSL_free(tmp_smallfelems);
  1703. return ret;
  1704. }
  1705. const EC_METHOD *EC_GFp_nistp256_method(void) {
  1706. static const EC_METHOD ret = {
  1707. EC_FLAGS_DEFAULT_OCT,
  1708. ec_GFp_nistp256_group_init,
  1709. ec_GFp_simple_group_finish,
  1710. ec_GFp_simple_group_clear_finish,
  1711. ec_GFp_simple_group_copy, ec_GFp_nistp256_group_set_curve,
  1712. ec_GFp_simple_group_get_curve, ec_GFp_simple_group_get_degree,
  1713. ec_GFp_simple_group_check_discriminant, ec_GFp_simple_point_init,
  1714. ec_GFp_simple_point_finish, ec_GFp_simple_point_clear_finish,
  1715. ec_GFp_simple_point_copy, ec_GFp_simple_point_set_to_infinity,
  1716. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  1717. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  1718. ec_GFp_simple_point_set_affine_coordinates,
  1719. ec_GFp_nistp256_point_get_affine_coordinates,
  1720. 0 /* point_set_compressed_coordinates */, 0 /* point2oct */,
  1721. 0 /* oct2point */, ec_GFp_simple_add, ec_GFp_simple_dbl,
  1722. ec_GFp_simple_invert, ec_GFp_simple_is_at_infinity,
  1723. ec_GFp_simple_is_on_curve, ec_GFp_simple_cmp, ec_GFp_simple_make_affine,
  1724. ec_GFp_simple_points_make_affine, ec_GFp_nistp256_points_mul,
  1725. 0 /* precompute_mult */, 0 /* have_precompute_mult */,
  1726. ec_GFp_simple_field_mul, ec_GFp_simple_field_sqr, 0 /* field_div */,
  1727. 0 /* field_encode */, 0 /* field_decode */, 0 /* field_set_to_one */
  1728. };
  1729. return &ret;
  1730. }
  1731. #endif /* 64_BIT && !WINDOWS */