p256-64.c 50 KB

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