ecp_nistp256.c 72 KB

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