p256-64.c 51 KB

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