p256-64.c 55 KB

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