curve25519.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /* Copyright (c) 2020, 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. // Some of this code is taken from the ref10 version of Ed25519 in SUPERCOP
  15. // 20141124 (http://bench.cr.yp.to/supercop.html). That code is released as
  16. // public domain. Other parts have been replaced to call into code generated by
  17. // Fiat (https://github.com/mit-plv/fiat-crypto) in //third_party/fiat.
  18. //
  19. // The field functions are shared by Ed25519 and X25519 where possible.
  20. #include <GFp/mem.h>
  21. #include "internal.h"
  22. #include "../internal.h"
  23. #if defined(_MSC_VER) && !defined(__clang__)
  24. // '=': conversion from 'int64_t' to 'int32_t', possible loss of data
  25. #pragma warning(disable: 4242)
  26. // '=': conversion from 'int32_t' to 'uint8_t', possible loss of data
  27. #pragma warning(disable: 4244)
  28. #endif
  29. #if defined(__GNUC__)
  30. #pragma GCC diagnostic ignored "-Wconversion"
  31. #pragma GCC diagnostic ignored "-Wsign-conversion"
  32. #endif
  33. // Various pre-computed constants.
  34. #include "./curve25519_tables.h"
  35. #if defined(BORINGSSL_CURVE25519_64BIT)
  36. #if defined(__GNUC__)
  37. #pragma GCC diagnostic ignored "-Wpedantic"
  38. #endif
  39. #include "../../third_party/fiat/curve25519_64.h"
  40. #else
  41. #include "../../third_party/fiat/curve25519_32.h"
  42. #endif // BORINGSSL_CURVE25519_64BIT
  43. // Low-level intrinsic operations
  44. static uint64_t load_3(const uint8_t *in) {
  45. uint64_t result;
  46. result = (uint64_t)in[0];
  47. result |= ((uint64_t)in[1]) << 8;
  48. result |= ((uint64_t)in[2]) << 16;
  49. return result;
  50. }
  51. static uint64_t load_4(const uint8_t *in) {
  52. uint64_t result;
  53. result = (uint64_t)in[0];
  54. result |= ((uint64_t)in[1]) << 8;
  55. result |= ((uint64_t)in[2]) << 16;
  56. result |= ((uint64_t)in[3]) << 24;
  57. return result;
  58. }
  59. // Field operations.
  60. #if defined(BORINGSSL_CURVE25519_64BIT)
  61. // assert_fe asserts that |f| satisfies bounds:
  62. //
  63. // [[0x0 ~> 0x8cccccccccccc],
  64. // [0x0 ~> 0x8cccccccccccc],
  65. // [0x0 ~> 0x8cccccccccccc],
  66. // [0x0 ~> 0x8cccccccccccc],
  67. // [0x0 ~> 0x8cccccccccccc]]
  68. //
  69. // See comments in curve25519_64.h for which functions use these bounds for
  70. // inputs or outputs.
  71. #define assert_fe(f) \
  72. do { \
  73. for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \
  74. dev_assert_secret(f[_assert_fe_i] <= UINT64_C(0x8cccccccccccc)); \
  75. } \
  76. } while (0)
  77. // assert_fe_loose asserts that |f| satisfies bounds:
  78. //
  79. // [[0x0 ~> 0x1a666666666664],
  80. // [0x0 ~> 0x1a666666666664],
  81. // [0x0 ~> 0x1a666666666664],
  82. // [0x0 ~> 0x1a666666666664],
  83. // [0x0 ~> 0x1a666666666664]]
  84. //
  85. // See comments in curve25519_64.h for which functions use these bounds for
  86. // inputs or outputs.
  87. #define assert_fe_loose(f) \
  88. do { \
  89. for (unsigned _assert_fe_i = 0; _assert_fe_i < 5; _assert_fe_i++) { \
  90. dev_assert_secret(f[_assert_fe_i] <= UINT64_C(0x1a666666666664)); \
  91. } \
  92. } while (0)
  93. #else
  94. // assert_fe asserts that |f| satisfies bounds:
  95. //
  96. // [[0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
  97. // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
  98. // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
  99. // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
  100. // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333]]
  101. //
  102. // See comments in curve25519_32.h for which functions use these bounds for
  103. // inputs or outputs.
  104. #define assert_fe(f) \
  105. do { \
  106. for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
  107. dev_assert_secret(f[_assert_fe_i] <= \
  108. ((_assert_fe_i & 1) ? 0x2333333u : 0x4666666u)); \
  109. } \
  110. } while (0)
  111. // assert_fe_loose asserts that |f| satisfies bounds:
  112. //
  113. // [[0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
  114. // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
  115. // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
  116. // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
  117. // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999]]
  118. //
  119. // See comments in curve25519_32.h for which functions use these bounds for
  120. // inputs or outputs.
  121. #define assert_fe_loose(f) \
  122. do { \
  123. for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
  124. dev_assert_secret(f[_assert_fe_i] <= \
  125. ((_assert_fe_i & 1) ? 0x6999999u : 0xd333332u)); \
  126. } \
  127. } while (0)
  128. #endif // BORINGSSL_CURVE25519_64BIT
  129. OPENSSL_STATIC_ASSERT(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
  130. "fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
  131. static void fe_frombytes_strict(fe *h, const uint8_t s[32]) {
  132. // |fiat_25519_from_bytes| requires the top-most bit be clear.
  133. dev_assert_secret((s[31] & 0x80) == 0);
  134. fiat_25519_from_bytes(h->v, s);
  135. assert_fe(h->v);
  136. }
  137. static void fe_frombytes(fe *h, const uint8_t s[32]) {
  138. uint8_t s_copy[32];
  139. GFp_memcpy(s_copy, s, 32);
  140. s_copy[31] &= 0x7f;
  141. fe_frombytes_strict(h, s_copy);
  142. }
  143. static void fe_tobytes(uint8_t s[32], const fe *f) {
  144. assert_fe(f->v);
  145. fiat_25519_to_bytes(s, f->v);
  146. }
  147. // h = 0
  148. static void fe_0(fe *h) {
  149. GFp_memset(h, 0, sizeof(fe));
  150. }
  151. static void fe_loose_0(fe_loose *h) {
  152. GFp_memset(h, 0, sizeof(fe_loose));
  153. }
  154. // h = 1
  155. static void fe_1(fe *h) {
  156. GFp_memset(h, 0, sizeof(fe));
  157. h->v[0] = 1;
  158. }
  159. static void fe_loose_1(fe_loose *h) {
  160. GFp_memset(h, 0, sizeof(fe_loose));
  161. h->v[0] = 1;
  162. }
  163. // h = f + g
  164. // Can overlap h with f or g.
  165. static void fe_add(fe_loose *h, const fe *f, const fe *g) {
  166. assert_fe(f->v);
  167. assert_fe(g->v);
  168. fiat_25519_add(h->v, f->v, g->v);
  169. assert_fe_loose(h->v);
  170. }
  171. // h = f - g
  172. // Can overlap h with f or g.
  173. static void fe_sub(fe_loose *h, const fe *f, const fe *g) {
  174. assert_fe(f->v);
  175. assert_fe(g->v);
  176. fiat_25519_sub(h->v, f->v, g->v);
  177. assert_fe_loose(h->v);
  178. }
  179. static void fe_carry(fe *h, const fe_loose* f) {
  180. assert_fe_loose(f->v);
  181. fiat_25519_carry(h->v, f->v);
  182. assert_fe(h->v);
  183. }
  184. static void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS],
  185. const fe_limb_t in1[FE_NUM_LIMBS],
  186. const fe_limb_t in2[FE_NUM_LIMBS]) {
  187. assert_fe_loose(in1);
  188. assert_fe_loose(in2);
  189. fiat_25519_carry_mul(out, in1, in2);
  190. assert_fe(out);
  191. }
  192. static void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) {
  193. fe_mul_impl(h->v, f->v, g->v);
  194. }
  195. // static void fe_mul_llt(fe_loose *h, const fe_loose *f, const fe *g) was
  196. // removed. This comment is here to make diffs vs. BoringSSL easier to read.
  197. static void fe_mul_ttt(fe *h, const fe *f, const fe *g) {
  198. fe_mul_impl(h->v, f->v, g->v);
  199. }
  200. static void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) {
  201. fe_mul_impl(h->v, f->v, g->v);
  202. }
  203. static void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) {
  204. fe_mul_impl(h->v, f->v, g->v);
  205. }
  206. static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) {
  207. fe_mul_impl(h->v, f->v, g->v);
  208. }
  209. static void fe_sq_tl(fe *h, const fe_loose *f) {
  210. assert_fe_loose(f->v);
  211. fiat_25519_carry_square(h->v, f->v);
  212. assert_fe(h->v);
  213. }
  214. static void fe_sq_tt(fe *h, const fe *f) {
  215. assert_fe_loose(f->v);
  216. fiat_25519_carry_square(h->v, f->v);
  217. assert_fe(h->v);
  218. }
  219. // Replace (f,g) with (g,f) if b == 1;
  220. // replace (f,g) with (f,g) if b == 0.
  221. //
  222. // Preconditions: b in {0,1}.
  223. static void fe_cswap(fe *f, fe *g, fe_limb_t b) {
  224. b = 0-b;
  225. for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
  226. fe_limb_t x = f->v[i] ^ g->v[i];
  227. x &= b;
  228. f->v[i] ^= x;
  229. g->v[i] ^= x;
  230. }
  231. }
  232. static void fe_mul121666(fe *h, const fe_loose *f) {
  233. assert_fe_loose(f->v);
  234. fiat_25519_carry_scmul_121666(h->v, f->v);
  235. assert_fe(h->v);
  236. }
  237. // h = -f
  238. static void fe_neg(fe_loose *h, const fe *f) {
  239. assert_fe(f->v);
  240. fiat_25519_opp(h->v, f->v);
  241. assert_fe_loose(h->v);
  242. }
  243. // Replace (f,g) with (g,g) if b == 1;
  244. // replace (f,g) with (f,g) if b == 0.
  245. //
  246. // Preconditions: b in {0,1}.
  247. static void fe_cmov(fe_loose *f, const fe_loose *g, fe_limb_t b) {
  248. // Silence an unused function warning. |fiat_25519_selectznz| isn't quite the
  249. // calling convention the rest of this code wants, so implement it by hand.
  250. //
  251. // TODO(davidben): Switch to fiat's calling convention, or ask fiat to emit a
  252. // different one.
  253. (void)fiat_25519_selectznz;
  254. b = 0-b;
  255. for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
  256. fe_limb_t x = f->v[i] ^ g->v[i];
  257. x &= b;
  258. f->v[i] ^= x;
  259. }
  260. }
  261. // h = f
  262. static void fe_copy(fe *h, const fe *f) {
  263. fe_limbs_copy(h->v, f->v);
  264. }
  265. static void fe_copy_lt(fe_loose *h, const fe *f) {
  266. fe_limbs_copy(h->v, f->v);
  267. }
  268. #if !defined(OPENSSL_SMALL)
  269. static void fe_copy_ll(fe_loose *h, const fe_loose *f) {
  270. fe_limbs_copy(h->v, f->v);
  271. }
  272. #endif // !defined(OPENSSL_SMALL)
  273. static void fe_loose_invert(fe *out, const fe_loose *z) {
  274. fe t0;
  275. fe t1;
  276. fe t2;
  277. fe t3;
  278. int i;
  279. fe_sq_tl(&t0, z);
  280. fe_sq_tt(&t1, &t0);
  281. for (i = 1; i < 2; ++i) {
  282. fe_sq_tt(&t1, &t1);
  283. }
  284. fe_mul_tlt(&t1, z, &t1);
  285. fe_mul_ttt(&t0, &t0, &t1);
  286. fe_sq_tt(&t2, &t0);
  287. fe_mul_ttt(&t1, &t1, &t2);
  288. fe_sq_tt(&t2, &t1);
  289. for (i = 1; i < 5; ++i) {
  290. fe_sq_tt(&t2, &t2);
  291. }
  292. fe_mul_ttt(&t1, &t2, &t1);
  293. fe_sq_tt(&t2, &t1);
  294. for (i = 1; i < 10; ++i) {
  295. fe_sq_tt(&t2, &t2);
  296. }
  297. fe_mul_ttt(&t2, &t2, &t1);
  298. fe_sq_tt(&t3, &t2);
  299. for (i = 1; i < 20; ++i) {
  300. fe_sq_tt(&t3, &t3);
  301. }
  302. fe_mul_ttt(&t2, &t3, &t2);
  303. fe_sq_tt(&t2, &t2);
  304. for (i = 1; i < 10; ++i) {
  305. fe_sq_tt(&t2, &t2);
  306. }
  307. fe_mul_ttt(&t1, &t2, &t1);
  308. fe_sq_tt(&t2, &t1);
  309. for (i = 1; i < 50; ++i) {
  310. fe_sq_tt(&t2, &t2);
  311. }
  312. fe_mul_ttt(&t2, &t2, &t1);
  313. fe_sq_tt(&t3, &t2);
  314. for (i = 1; i < 100; ++i) {
  315. fe_sq_tt(&t3, &t3);
  316. }
  317. fe_mul_ttt(&t2, &t3, &t2);
  318. fe_sq_tt(&t2, &t2);
  319. for (i = 1; i < 50; ++i) {
  320. fe_sq_tt(&t2, &t2);
  321. }
  322. fe_mul_ttt(&t1, &t2, &t1);
  323. fe_sq_tt(&t1, &t1);
  324. for (i = 1; i < 5; ++i) {
  325. fe_sq_tt(&t1, &t1);
  326. }
  327. fe_mul_ttt(out, &t1, &t0);
  328. }
  329. static void fe_invert(fe *out, const fe *z) {
  330. fe_loose l;
  331. fe_copy_lt(&l, z);
  332. fe_loose_invert(out, &l);
  333. }
  334. // return 0 if f == 0
  335. // return 1 if f != 0
  336. static int fe_isnonzero(const fe_loose *f) {
  337. fe tight;
  338. fe_carry(&tight, f);
  339. uint8_t s[32];
  340. fe_tobytes(s, &tight);
  341. static const uint8_t zero[32] = {0};
  342. return GFp_memcmp(s, zero, sizeof(zero)) != 0;
  343. }
  344. // return 1 if f is in {1,3,5,...,q-2}
  345. // return 0 if f is in {0,2,4,...,q-1}
  346. static int fe_isnegative(const fe *f) {
  347. uint8_t s[32];
  348. fe_tobytes(s, f);
  349. return s[0] & 1;
  350. }
  351. static void fe_sq2_tt(fe *h, const fe *f) {
  352. // h = f^2
  353. fe_sq_tt(h, f);
  354. // h = h + h
  355. fe_loose tmp;
  356. fe_add(&tmp, h, h);
  357. fe_carry(h, &tmp);
  358. }
  359. static void fe_pow22523(fe *out, const fe *z) {
  360. fe t0;
  361. fe t1;
  362. fe t2;
  363. int i;
  364. fe_sq_tt(&t0, z);
  365. fe_sq_tt(&t1, &t0);
  366. for (i = 1; i < 2; ++i) {
  367. fe_sq_tt(&t1, &t1);
  368. }
  369. fe_mul_ttt(&t1, z, &t1);
  370. fe_mul_ttt(&t0, &t0, &t1);
  371. fe_sq_tt(&t0, &t0);
  372. fe_mul_ttt(&t0, &t1, &t0);
  373. fe_sq_tt(&t1, &t0);
  374. for (i = 1; i < 5; ++i) {
  375. fe_sq_tt(&t1, &t1);
  376. }
  377. fe_mul_ttt(&t0, &t1, &t0);
  378. fe_sq_tt(&t1, &t0);
  379. for (i = 1; i < 10; ++i) {
  380. fe_sq_tt(&t1, &t1);
  381. }
  382. fe_mul_ttt(&t1, &t1, &t0);
  383. fe_sq_tt(&t2, &t1);
  384. for (i = 1; i < 20; ++i) {
  385. fe_sq_tt(&t2, &t2);
  386. }
  387. fe_mul_ttt(&t1, &t2, &t1);
  388. fe_sq_tt(&t1, &t1);
  389. for (i = 1; i < 10; ++i) {
  390. fe_sq_tt(&t1, &t1);
  391. }
  392. fe_mul_ttt(&t0, &t1, &t0);
  393. fe_sq_tt(&t1, &t0);
  394. for (i = 1; i < 50; ++i) {
  395. fe_sq_tt(&t1, &t1);
  396. }
  397. fe_mul_ttt(&t1, &t1, &t0);
  398. fe_sq_tt(&t2, &t1);
  399. for (i = 1; i < 100; ++i) {
  400. fe_sq_tt(&t2, &t2);
  401. }
  402. fe_mul_ttt(&t1, &t2, &t1);
  403. fe_sq_tt(&t1, &t1);
  404. for (i = 1; i < 50; ++i) {
  405. fe_sq_tt(&t1, &t1);
  406. }
  407. fe_mul_ttt(&t0, &t1, &t0);
  408. fe_sq_tt(&t0, &t0);
  409. for (i = 1; i < 2; ++i) {
  410. fe_sq_tt(&t0, &t0);
  411. }
  412. fe_mul_ttt(out, &t0, z);
  413. }
  414. // Group operations.
  415. int GFp_x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) {
  416. fe u;
  417. fe_loose v;
  418. fe v3;
  419. fe vxx;
  420. fe_loose check;
  421. fe_frombytes(&h->Y, s);
  422. fe_1(&h->Z);
  423. fe_sq_tt(&v3, &h->Y);
  424. fe_mul_ttt(&vxx, &v3, &d);
  425. fe_sub(&v, &v3, &h->Z); // u = y^2-1
  426. fe_carry(&u, &v);
  427. fe_add(&v, &vxx, &h->Z); // v = dy^2+1
  428. fe_sq_tl(&v3, &v);
  429. fe_mul_ttl(&v3, &v3, &v); // v3 = v^3
  430. fe_sq_tt(&h->X, &v3);
  431. fe_mul_ttl(&h->X, &h->X, &v);
  432. fe_mul_ttt(&h->X, &h->X, &u); // x = uv^7
  433. fe_pow22523(&h->X, &h->X); // x = (uv^7)^((q-5)/8)
  434. fe_mul_ttt(&h->X, &h->X, &v3);
  435. fe_mul_ttt(&h->X, &h->X, &u); // x = uv^3(uv^7)^((q-5)/8)
  436. fe_sq_tt(&vxx, &h->X);
  437. fe_mul_ttl(&vxx, &vxx, &v);
  438. fe_sub(&check, &vxx, &u);
  439. if (fe_isnonzero(&check)) {
  440. fe_add(&check, &vxx, &u);
  441. if (fe_isnonzero(&check)) {
  442. return 0;
  443. }
  444. fe_mul_ttt(&h->X, &h->X, &sqrtm1);
  445. }
  446. if (fe_isnegative(&h->X) != (s[31] >> 7)) {
  447. fe_loose t;
  448. fe_neg(&t, &h->X);
  449. fe_carry(&h->X, &t);
  450. }
  451. fe_mul_ttt(&h->T, &h->X, &h->Y);
  452. return 1;
  453. }
  454. static void ge_p2_0(ge_p2 *h) {
  455. fe_0(&h->X);
  456. fe_1(&h->Y);
  457. fe_1(&h->Z);
  458. }
  459. static void ge_p3_0(ge_p3 *h) {
  460. fe_0(&h->X);
  461. fe_1(&h->Y);
  462. fe_1(&h->Z);
  463. fe_0(&h->T);
  464. }
  465. static void ge_precomp_0(ge_precomp *h) {
  466. fe_loose_1(&h->yplusx);
  467. fe_loose_1(&h->yminusx);
  468. fe_loose_0(&h->xy2d);
  469. }
  470. // r = p
  471. static void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) {
  472. fe_copy(&r->X, &p->X);
  473. fe_copy(&r->Y, &p->Y);
  474. fe_copy(&r->Z, &p->Z);
  475. }
  476. // r = p
  477. static void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p) {
  478. fe_add(&r->YplusX, &p->Y, &p->X);
  479. fe_sub(&r->YminusX, &p->Y, &p->X);
  480. fe_copy_lt(&r->Z, &p->Z);
  481. fe_mul_ltt(&r->T2d, &p->T, &d2);
  482. }
  483. // r = p
  484. static void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p) {
  485. fe_mul_tll(&r->X, &p->X, &p->T);
  486. fe_mul_tll(&r->Y, &p->Y, &p->Z);
  487. fe_mul_tll(&r->Z, &p->Z, &p->T);
  488. }
  489. // r = p
  490. static void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) {
  491. fe_mul_tll(&r->X, &p->X, &p->T);
  492. fe_mul_tll(&r->Y, &p->Y, &p->Z);
  493. fe_mul_tll(&r->Z, &p->Z, &p->T);
  494. fe_mul_tll(&r->T, &p->X, &p->Y);
  495. }
  496. // r = 2 * p
  497. static void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) {
  498. fe trX, trZ, trT;
  499. fe t0;
  500. fe_sq_tt(&trX, &p->X);
  501. fe_sq_tt(&trZ, &p->Y);
  502. fe_sq2_tt(&trT, &p->Z);
  503. fe_add(&r->Y, &p->X, &p->Y);
  504. fe_sq_tl(&t0, &r->Y);
  505. fe_add(&r->Y, &trZ, &trX);
  506. fe_sub(&r->Z, &trZ, &trX);
  507. fe_carry(&trZ, &r->Y);
  508. fe_sub(&r->X, &t0, &trZ);
  509. fe_carry(&trZ, &r->Z);
  510. fe_sub(&r->T, &trT, &trZ);
  511. }
  512. // r = 2 * p
  513. static void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) {
  514. ge_p2 q;
  515. ge_p3_to_p2(&q, p);
  516. ge_p2_dbl(r, &q);
  517. }
  518. // r = p + q
  519. static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
  520. fe trY, trZ, trT;
  521. fe_add(&r->X, &p->Y, &p->X);
  522. fe_sub(&r->Y, &p->Y, &p->X);
  523. fe_mul_tll(&trZ, &r->X, &q->yplusx);
  524. fe_mul_tll(&trY, &r->Y, &q->yminusx);
  525. fe_mul_tlt(&trT, &q->xy2d, &p->T);
  526. fe_add(&r->T, &p->Z, &p->Z);
  527. fe_sub(&r->X, &trZ, &trY);
  528. fe_add(&r->Y, &trZ, &trY);
  529. fe_carry(&trZ, &r->T);
  530. fe_add(&r->Z, &trZ, &trT);
  531. fe_sub(&r->T, &trZ, &trT);
  532. }
  533. // r = p - q
  534. static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
  535. fe trY, trZ, trT;
  536. fe_add(&r->X, &p->Y, &p->X);
  537. fe_sub(&r->Y, &p->Y, &p->X);
  538. fe_mul_tll(&trZ, &r->X, &q->yminusx);
  539. fe_mul_tll(&trY, &r->Y, &q->yplusx);
  540. fe_mul_tlt(&trT, &q->xy2d, &p->T);
  541. fe_add(&r->T, &p->Z, &p->Z);
  542. fe_sub(&r->X, &trZ, &trY);
  543. fe_add(&r->Y, &trZ, &trY);
  544. fe_carry(&trZ, &r->T);
  545. fe_sub(&r->Z, &trZ, &trT);
  546. fe_add(&r->T, &trZ, &trT);
  547. }
  548. // r = p + q
  549. static void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
  550. fe trX, trY, trZ, trT;
  551. fe_add(&r->X, &p->Y, &p->X);
  552. fe_sub(&r->Y, &p->Y, &p->X);
  553. fe_mul_tll(&trZ, &r->X, &q->YplusX);
  554. fe_mul_tll(&trY, &r->Y, &q->YminusX);
  555. fe_mul_tlt(&trT, &q->T2d, &p->T);
  556. fe_mul_ttl(&trX, &p->Z, &q->Z);
  557. fe_add(&r->T, &trX, &trX);
  558. fe_sub(&r->X, &trZ, &trY);
  559. fe_add(&r->Y, &trZ, &trY);
  560. fe_carry(&trZ, &r->T);
  561. fe_add(&r->Z, &trZ, &trT);
  562. fe_sub(&r->T, &trZ, &trT);
  563. }
  564. // r = p - q
  565. static void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
  566. fe trX, trY, trZ, trT;
  567. fe_add(&r->X, &p->Y, &p->X);
  568. fe_sub(&r->Y, &p->Y, &p->X);
  569. fe_mul_tll(&trZ, &r->X, &q->YminusX);
  570. fe_mul_tll(&trY, &r->Y, &q->YplusX);
  571. fe_mul_tlt(&trT, &q->T2d, &p->T);
  572. fe_mul_ttl(&trX, &p->Z, &q->Z);
  573. fe_add(&r->T, &trX, &trX);
  574. fe_sub(&r->X, &trZ, &trY);
  575. fe_add(&r->Y, &trZ, &trY);
  576. fe_carry(&trZ, &r->T);
  577. fe_sub(&r->Z, &trZ, &trT);
  578. fe_add(&r->T, &trZ, &trT);
  579. }
  580. static uint8_t equal(signed char b, signed char c) {
  581. uint8_t ub = b;
  582. uint8_t uc = c;
  583. uint8_t x = ub ^ uc; // 0: yes; 1..255: no
  584. uint32_t y = x; // 0: yes; 1..255: no
  585. y -= 1; // 4294967295: yes; 0..254: no
  586. y >>= 31; // 1: yes; 0: no
  587. return y;
  588. }
  589. static void cmov(ge_precomp *t, const ge_precomp *u, uint8_t b) {
  590. fe_cmov(&t->yplusx, &u->yplusx, b);
  591. fe_cmov(&t->yminusx, &u->yminusx, b);
  592. fe_cmov(&t->xy2d, &u->xy2d, b);
  593. }
  594. #if defined(OPENSSL_SMALL)
  595. static void x25519_ge_scalarmult_small_precomp(
  596. ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]) {
  597. // precomp_table is first expanded into matching |ge_precomp|
  598. // elements.
  599. ge_precomp multiples[15];
  600. unsigned i;
  601. for (i = 0; i < 15; i++) {
  602. // The precomputed table is assumed to already clear the top bit, so
  603. // |fe_frombytes_strict| may be used directly.
  604. const uint8_t *bytes = &precomp_table[i*(2 * 32)];
  605. fe x, y;
  606. fe_frombytes_strict(&x, bytes);
  607. fe_frombytes_strict(&y, bytes + 32);
  608. ge_precomp *out = &multiples[i];
  609. fe_add(&out->yplusx, &y, &x);
  610. fe_sub(&out->yminusx, &y, &x);
  611. fe_mul_ltt(&out->xy2d, &x, &y);
  612. fe_mul_llt(&out->xy2d, &out->xy2d, &d2);
  613. }
  614. // See the comment above |k25519SmallPrecomp| about the structure of the
  615. // precomputed elements. This loop does 64 additions and 64 doublings to
  616. // calculate the result.
  617. ge_p3_0(h);
  618. for (i = 63; i < 64; i--) {
  619. unsigned j;
  620. signed char index = 0;
  621. for (j = 0; j < 4; j++) {
  622. const uint8_t bit = 1 & (a[(8 * j) + (i / 8)] >> (i & 7));
  623. index |= (bit << j);
  624. }
  625. ge_precomp e;
  626. ge_precomp_0(&e);
  627. for (j = 1; j < 16; j++) {
  628. cmov(&e, &multiples[j-1], equal(index, j));
  629. }
  630. ge_cached cached;
  631. ge_p1p1 r;
  632. x25519_ge_p3_to_cached(&cached, h);
  633. x25519_ge_add(&r, h, &cached);
  634. x25519_ge_p1p1_to_p3(h, &r);
  635. ge_madd(&r, h, &e);
  636. x25519_ge_p1p1_to_p3(h, &r);
  637. }
  638. }
  639. void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) {
  640. x25519_ge_scalarmult_small_precomp(h, a, k25519SmallPrecomp);
  641. }
  642. #else
  643. static uint8_t negative(signed char b) {
  644. uint32_t x = b;
  645. x >>= 31; // 1: yes; 0: no
  646. return x;
  647. }
  648. static void table_select(ge_precomp *t, int pos, signed char b) {
  649. ge_precomp minust;
  650. uint8_t bnegative = negative(b);
  651. uint8_t babs = b - ((uint8_t)((-bnegative) & b) << 1);
  652. ge_precomp_0(t);
  653. cmov(t, &k25519Precomp[pos][0], equal(babs, 1));
  654. cmov(t, &k25519Precomp[pos][1], equal(babs, 2));
  655. cmov(t, &k25519Precomp[pos][2], equal(babs, 3));
  656. cmov(t, &k25519Precomp[pos][3], equal(babs, 4));
  657. cmov(t, &k25519Precomp[pos][4], equal(babs, 5));
  658. cmov(t, &k25519Precomp[pos][5], equal(babs, 6));
  659. cmov(t, &k25519Precomp[pos][6], equal(babs, 7));
  660. cmov(t, &k25519Precomp[pos][7], equal(babs, 8));
  661. fe_copy_ll(&minust.yplusx, &t->yminusx);
  662. fe_copy_ll(&minust.yminusx, &t->yplusx);
  663. // NOTE: the input table is canonical, but types don't encode it
  664. fe tmp;
  665. fe_carry(&tmp, &t->xy2d);
  666. fe_neg(&minust.xy2d, &tmp);
  667. cmov(t, &minust, bnegative);
  668. }
  669. // h = a * B
  670. // where a = a[0]+256*a[1]+...+256^31 a[31]
  671. // B is the Ed25519 base point (x,4/5) with x positive.
  672. //
  673. // Preconditions:
  674. // a[31] <= 127
  675. void GFp_x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t *a) {
  676. signed char e[64];
  677. signed char carry;
  678. ge_p1p1 r;
  679. ge_p2 s;
  680. ge_precomp t;
  681. int i;
  682. for (i = 0; i < 32; ++i) {
  683. e[2 * i + 0] = (a[i] >> 0) & 15;
  684. e[2 * i + 1] = (a[i] >> 4) & 15;
  685. }
  686. // each e[i] is between 0 and 15
  687. // e[63] is between 0 and 7
  688. carry = 0;
  689. for (i = 0; i < 63; ++i) {
  690. e[i] += carry;
  691. carry = e[i] + 8;
  692. carry >>= 4;
  693. e[i] -= carry << 4;
  694. }
  695. e[63] += carry;
  696. // each e[i] is between -8 and 8
  697. ge_p3_0(h);
  698. for (i = 1; i < 64; i += 2) {
  699. table_select(&t, i / 2, e[i]);
  700. ge_madd(&r, h, &t);
  701. x25519_ge_p1p1_to_p3(h, &r);
  702. }
  703. ge_p3_dbl(&r, h);
  704. x25519_ge_p1p1_to_p2(&s, &r);
  705. ge_p2_dbl(&r, &s);
  706. x25519_ge_p1p1_to_p2(&s, &r);
  707. ge_p2_dbl(&r, &s);
  708. x25519_ge_p1p1_to_p2(&s, &r);
  709. ge_p2_dbl(&r, &s);
  710. x25519_ge_p1p1_to_p3(h, &r);
  711. for (i = 0; i < 64; i += 2) {
  712. table_select(&t, i / 2, e[i]);
  713. ge_madd(&r, h, &t);
  714. x25519_ge_p1p1_to_p3(h, &r);
  715. }
  716. }
  717. #endif
  718. static void slide(signed char *r, const uint8_t *a) {
  719. int i;
  720. int b;
  721. int k;
  722. for (i = 0; i < 256; ++i) {
  723. r[i] = 1 & (a[i >> 3] >> (i & 7));
  724. }
  725. for (i = 0; i < 256; ++i) {
  726. if (r[i]) {
  727. for (b = 1; b <= 6 && i + b < 256; ++b) {
  728. if (r[i + b]) {
  729. if (r[i] + (r[i + b] << b) <= 15) {
  730. r[i] += r[i + b] << b;
  731. r[i + b] = 0;
  732. } else if (r[i] - (r[i + b] << b) >= -15) {
  733. r[i] -= r[i + b] << b;
  734. for (k = i + b; k < 256; ++k) {
  735. if (!r[k]) {
  736. r[k] = 1;
  737. break;
  738. }
  739. r[k] = 0;
  740. }
  741. } else {
  742. break;
  743. }
  744. }
  745. }
  746. }
  747. }
  748. }
  749. // r = a * A + b * B
  750. // where a = a[0]+256*a[1]+...+256^31 a[31].
  751. // and b = b[0]+256*b[1]+...+256^31 b[31].
  752. // B is the Ed25519 base point (x,4/5) with x positive.
  753. static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a,
  754. const ge_p3 *A, const uint8_t *b) {
  755. signed char aslide[256];
  756. signed char bslide[256];
  757. ge_cached Ai[8]; // A,3A,5A,7A,9A,11A,13A,15A
  758. ge_p1p1 t;
  759. ge_p3 u;
  760. ge_p3 A2;
  761. int i;
  762. slide(aslide, a);
  763. slide(bslide, b);
  764. x25519_ge_p3_to_cached(&Ai[0], A);
  765. ge_p3_dbl(&t, A);
  766. x25519_ge_p1p1_to_p3(&A2, &t);
  767. x25519_ge_add(&t, &A2, &Ai[0]);
  768. x25519_ge_p1p1_to_p3(&u, &t);
  769. x25519_ge_p3_to_cached(&Ai[1], &u);
  770. x25519_ge_add(&t, &A2, &Ai[1]);
  771. x25519_ge_p1p1_to_p3(&u, &t);
  772. x25519_ge_p3_to_cached(&Ai[2], &u);
  773. x25519_ge_add(&t, &A2, &Ai[2]);
  774. x25519_ge_p1p1_to_p3(&u, &t);
  775. x25519_ge_p3_to_cached(&Ai[3], &u);
  776. x25519_ge_add(&t, &A2, &Ai[3]);
  777. x25519_ge_p1p1_to_p3(&u, &t);
  778. x25519_ge_p3_to_cached(&Ai[4], &u);
  779. x25519_ge_add(&t, &A2, &Ai[4]);
  780. x25519_ge_p1p1_to_p3(&u, &t);
  781. x25519_ge_p3_to_cached(&Ai[5], &u);
  782. x25519_ge_add(&t, &A2, &Ai[5]);
  783. x25519_ge_p1p1_to_p3(&u, &t);
  784. x25519_ge_p3_to_cached(&Ai[6], &u);
  785. x25519_ge_add(&t, &A2, &Ai[6]);
  786. x25519_ge_p1p1_to_p3(&u, &t);
  787. x25519_ge_p3_to_cached(&Ai[7], &u);
  788. ge_p2_0(r);
  789. for (i = 255; i >= 0; --i) {
  790. if (aslide[i] || bslide[i]) {
  791. break;
  792. }
  793. }
  794. for (; i >= 0; --i) {
  795. ge_p2_dbl(&t, r);
  796. if (aslide[i] > 0) {
  797. x25519_ge_p1p1_to_p3(&u, &t);
  798. x25519_ge_add(&t, &u, &Ai[aslide[i] / 2]);
  799. } else if (aslide[i] < 0) {
  800. x25519_ge_p1p1_to_p3(&u, &t);
  801. x25519_ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]);
  802. }
  803. if (bslide[i] > 0) {
  804. x25519_ge_p1p1_to_p3(&u, &t);
  805. ge_madd(&t, &u, &Bi[bslide[i] / 2]);
  806. } else if (bslide[i] < 0) {
  807. x25519_ge_p1p1_to_p3(&u, &t);
  808. ge_msub(&t, &u, &Bi[(-bslide[i]) / 2]);
  809. }
  810. x25519_ge_p1p1_to_p2(r, &t);
  811. }
  812. }
  813. // int64_lshift21 returns |a << 21| but is defined when shifting bits into the
  814. // sign bit. This works around a language flaw in C.
  815. static inline int64_t int64_lshift21(int64_t a) {
  816. return (int64_t)((uint64_t)a << 21);
  817. }
  818. // The set of scalars is \Z/l
  819. // where l = 2^252 + 27742317777372353535851937790883648493.
  820. // Input:
  821. // s[0]+256*s[1]+...+256^63*s[63] = s
  822. //
  823. // Output:
  824. // s[0]+256*s[1]+...+256^31*s[31] = s mod l
  825. // where l = 2^252 + 27742317777372353535851937790883648493.
  826. // Overwrites s in place.
  827. void GFp_x25519_sc_reduce(uint8_t s[64]) {
  828. int64_t s0 = 2097151 & load_3(s);
  829. int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
  830. int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
  831. int64_t s3 = 2097151 & (load_4(s + 7) >> 7);
  832. int64_t s4 = 2097151 & (load_4(s + 10) >> 4);
  833. int64_t s5 = 2097151 & (load_3(s + 13) >> 1);
  834. int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
  835. int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
  836. int64_t s8 = 2097151 & load_3(s + 21);
  837. int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
  838. int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
  839. int64_t s11 = 2097151 & (load_4(s + 28) >> 7);
  840. int64_t s12 = 2097151 & (load_4(s + 31) >> 4);
  841. int64_t s13 = 2097151 & (load_3(s + 34) >> 1);
  842. int64_t s14 = 2097151 & (load_4(s + 36) >> 6);
  843. int64_t s15 = 2097151 & (load_3(s + 39) >> 3);
  844. int64_t s16 = 2097151 & load_3(s + 42);
  845. int64_t s17 = 2097151 & (load_4(s + 44) >> 5);
  846. int64_t s18 = 2097151 & (load_3(s + 47) >> 2);
  847. int64_t s19 = 2097151 & (load_4(s + 49) >> 7);
  848. int64_t s20 = 2097151 & (load_4(s + 52) >> 4);
  849. int64_t s21 = 2097151 & (load_3(s + 55) >> 1);
  850. int64_t s22 = 2097151 & (load_4(s + 57) >> 6);
  851. int64_t s23 = (load_4(s + 60) >> 3);
  852. int64_t carry0;
  853. int64_t carry1;
  854. int64_t carry2;
  855. int64_t carry3;
  856. int64_t carry4;
  857. int64_t carry5;
  858. int64_t carry6;
  859. int64_t carry7;
  860. int64_t carry8;
  861. int64_t carry9;
  862. int64_t carry10;
  863. int64_t carry11;
  864. int64_t carry12;
  865. int64_t carry13;
  866. int64_t carry14;
  867. int64_t carry15;
  868. int64_t carry16;
  869. s11 += s23 * 666643;
  870. s12 += s23 * 470296;
  871. s13 += s23 * 654183;
  872. s14 -= s23 * 997805;
  873. s15 += s23 * 136657;
  874. s16 -= s23 * 683901;
  875. s23 = 0;
  876. s10 += s22 * 666643;
  877. s11 += s22 * 470296;
  878. s12 += s22 * 654183;
  879. s13 -= s22 * 997805;
  880. s14 += s22 * 136657;
  881. s15 -= s22 * 683901;
  882. s22 = 0;
  883. s9 += s21 * 666643;
  884. s10 += s21 * 470296;
  885. s11 += s21 * 654183;
  886. s12 -= s21 * 997805;
  887. s13 += s21 * 136657;
  888. s14 -= s21 * 683901;
  889. s21 = 0;
  890. s8 += s20 * 666643;
  891. s9 += s20 * 470296;
  892. s10 += s20 * 654183;
  893. s11 -= s20 * 997805;
  894. s12 += s20 * 136657;
  895. s13 -= s20 * 683901;
  896. s20 = 0;
  897. s7 += s19 * 666643;
  898. s8 += s19 * 470296;
  899. s9 += s19 * 654183;
  900. s10 -= s19 * 997805;
  901. s11 += s19 * 136657;
  902. s12 -= s19 * 683901;
  903. s19 = 0;
  904. s6 += s18 * 666643;
  905. s7 += s18 * 470296;
  906. s8 += s18 * 654183;
  907. s9 -= s18 * 997805;
  908. s10 += s18 * 136657;
  909. s11 -= s18 * 683901;
  910. s18 = 0;
  911. carry6 = (s6 + (1 << 20)) >> 21;
  912. s7 += carry6;
  913. s6 -= int64_lshift21(carry6);
  914. carry8 = (s8 + (1 << 20)) >> 21;
  915. s9 += carry8;
  916. s8 -= int64_lshift21(carry8);
  917. carry10 = (s10 + (1 << 20)) >> 21;
  918. s11 += carry10;
  919. s10 -= int64_lshift21(carry10);
  920. carry12 = (s12 + (1 << 20)) >> 21;
  921. s13 += carry12;
  922. s12 -= int64_lshift21(carry12);
  923. carry14 = (s14 + (1 << 20)) >> 21;
  924. s15 += carry14;
  925. s14 -= int64_lshift21(carry14);
  926. carry16 = (s16 + (1 << 20)) >> 21;
  927. s17 += carry16;
  928. s16 -= int64_lshift21(carry16);
  929. carry7 = (s7 + (1 << 20)) >> 21;
  930. s8 += carry7;
  931. s7 -= int64_lshift21(carry7);
  932. carry9 = (s9 + (1 << 20)) >> 21;
  933. s10 += carry9;
  934. s9 -= int64_lshift21(carry9);
  935. carry11 = (s11 + (1 << 20)) >> 21;
  936. s12 += carry11;
  937. s11 -= int64_lshift21(carry11);
  938. carry13 = (s13 + (1 << 20)) >> 21;
  939. s14 += carry13;
  940. s13 -= int64_lshift21(carry13);
  941. carry15 = (s15 + (1 << 20)) >> 21;
  942. s16 += carry15;
  943. s15 -= int64_lshift21(carry15);
  944. s5 += s17 * 666643;
  945. s6 += s17 * 470296;
  946. s7 += s17 * 654183;
  947. s8 -= s17 * 997805;
  948. s9 += s17 * 136657;
  949. s10 -= s17 * 683901;
  950. s17 = 0;
  951. s4 += s16 * 666643;
  952. s5 += s16 * 470296;
  953. s6 += s16 * 654183;
  954. s7 -= s16 * 997805;
  955. s8 += s16 * 136657;
  956. s9 -= s16 * 683901;
  957. s16 = 0;
  958. s3 += s15 * 666643;
  959. s4 += s15 * 470296;
  960. s5 += s15 * 654183;
  961. s6 -= s15 * 997805;
  962. s7 += s15 * 136657;
  963. s8 -= s15 * 683901;
  964. s15 = 0;
  965. s2 += s14 * 666643;
  966. s3 += s14 * 470296;
  967. s4 += s14 * 654183;
  968. s5 -= s14 * 997805;
  969. s6 += s14 * 136657;
  970. s7 -= s14 * 683901;
  971. s14 = 0;
  972. s1 += s13 * 666643;
  973. s2 += s13 * 470296;
  974. s3 += s13 * 654183;
  975. s4 -= s13 * 997805;
  976. s5 += s13 * 136657;
  977. s6 -= s13 * 683901;
  978. s13 = 0;
  979. s0 += s12 * 666643;
  980. s1 += s12 * 470296;
  981. s2 += s12 * 654183;
  982. s3 -= s12 * 997805;
  983. s4 += s12 * 136657;
  984. s5 -= s12 * 683901;
  985. s12 = 0;
  986. carry0 = (s0 + (1 << 20)) >> 21;
  987. s1 += carry0;
  988. s0 -= int64_lshift21(carry0);
  989. carry2 = (s2 + (1 << 20)) >> 21;
  990. s3 += carry2;
  991. s2 -= int64_lshift21(carry2);
  992. carry4 = (s4 + (1 << 20)) >> 21;
  993. s5 += carry4;
  994. s4 -= int64_lshift21(carry4);
  995. carry6 = (s6 + (1 << 20)) >> 21;
  996. s7 += carry6;
  997. s6 -= int64_lshift21(carry6);
  998. carry8 = (s8 + (1 << 20)) >> 21;
  999. s9 += carry8;
  1000. s8 -= int64_lshift21(carry8);
  1001. carry10 = (s10 + (1 << 20)) >> 21;
  1002. s11 += carry10;
  1003. s10 -= int64_lshift21(carry10);
  1004. carry1 = (s1 + (1 << 20)) >> 21;
  1005. s2 += carry1;
  1006. s1 -= int64_lshift21(carry1);
  1007. carry3 = (s3 + (1 << 20)) >> 21;
  1008. s4 += carry3;
  1009. s3 -= int64_lshift21(carry3);
  1010. carry5 = (s5 + (1 << 20)) >> 21;
  1011. s6 += carry5;
  1012. s5 -= int64_lshift21(carry5);
  1013. carry7 = (s7 + (1 << 20)) >> 21;
  1014. s8 += carry7;
  1015. s7 -= int64_lshift21(carry7);
  1016. carry9 = (s9 + (1 << 20)) >> 21;
  1017. s10 += carry9;
  1018. s9 -= int64_lshift21(carry9);
  1019. carry11 = (s11 + (1 << 20)) >> 21;
  1020. s12 += carry11;
  1021. s11 -= int64_lshift21(carry11);
  1022. s0 += s12 * 666643;
  1023. s1 += s12 * 470296;
  1024. s2 += s12 * 654183;
  1025. s3 -= s12 * 997805;
  1026. s4 += s12 * 136657;
  1027. s5 -= s12 * 683901;
  1028. s12 = 0;
  1029. carry0 = s0 >> 21;
  1030. s1 += carry0;
  1031. s0 -= int64_lshift21(carry0);
  1032. carry1 = s1 >> 21;
  1033. s2 += carry1;
  1034. s1 -= int64_lshift21(carry1);
  1035. carry2 = s2 >> 21;
  1036. s3 += carry2;
  1037. s2 -= int64_lshift21(carry2);
  1038. carry3 = s3 >> 21;
  1039. s4 += carry3;
  1040. s3 -= int64_lshift21(carry3);
  1041. carry4 = s4 >> 21;
  1042. s5 += carry4;
  1043. s4 -= int64_lshift21(carry4);
  1044. carry5 = s5 >> 21;
  1045. s6 += carry5;
  1046. s5 -= int64_lshift21(carry5);
  1047. carry6 = s6 >> 21;
  1048. s7 += carry6;
  1049. s6 -= int64_lshift21(carry6);
  1050. carry7 = s7 >> 21;
  1051. s8 += carry7;
  1052. s7 -= int64_lshift21(carry7);
  1053. carry8 = s8 >> 21;
  1054. s9 += carry8;
  1055. s8 -= int64_lshift21(carry8);
  1056. carry9 = s9 >> 21;
  1057. s10 += carry9;
  1058. s9 -= int64_lshift21(carry9);
  1059. carry10 = s10 >> 21;
  1060. s11 += carry10;
  1061. s10 -= int64_lshift21(carry10);
  1062. carry11 = s11 >> 21;
  1063. s12 += carry11;
  1064. s11 -= int64_lshift21(carry11);
  1065. s0 += s12 * 666643;
  1066. s1 += s12 * 470296;
  1067. s2 += s12 * 654183;
  1068. s3 -= s12 * 997805;
  1069. s4 += s12 * 136657;
  1070. s5 -= s12 * 683901;
  1071. s12 = 0;
  1072. carry0 = s0 >> 21;
  1073. s1 += carry0;
  1074. s0 -= int64_lshift21(carry0);
  1075. carry1 = s1 >> 21;
  1076. s2 += carry1;
  1077. s1 -= int64_lshift21(carry1);
  1078. carry2 = s2 >> 21;
  1079. s3 += carry2;
  1080. s2 -= int64_lshift21(carry2);
  1081. carry3 = s3 >> 21;
  1082. s4 += carry3;
  1083. s3 -= int64_lshift21(carry3);
  1084. carry4 = s4 >> 21;
  1085. s5 += carry4;
  1086. s4 -= int64_lshift21(carry4);
  1087. carry5 = s5 >> 21;
  1088. s6 += carry5;
  1089. s5 -= int64_lshift21(carry5);
  1090. carry6 = s6 >> 21;
  1091. s7 += carry6;
  1092. s6 -= int64_lshift21(carry6);
  1093. carry7 = s7 >> 21;
  1094. s8 += carry7;
  1095. s7 -= int64_lshift21(carry7);
  1096. carry8 = s8 >> 21;
  1097. s9 += carry8;
  1098. s8 -= int64_lshift21(carry8);
  1099. carry9 = s9 >> 21;
  1100. s10 += carry9;
  1101. s9 -= int64_lshift21(carry9);
  1102. carry10 = s10 >> 21;
  1103. s11 += carry10;
  1104. s10 -= int64_lshift21(carry10);
  1105. s[0] = s0 >> 0;
  1106. s[1] = s0 >> 8;
  1107. s[2] = (s0 >> 16) | (s1 << 5);
  1108. s[3] = s1 >> 3;
  1109. s[4] = s1 >> 11;
  1110. s[5] = (s1 >> 19) | (s2 << 2);
  1111. s[6] = s2 >> 6;
  1112. s[7] = (s2 >> 14) | (s3 << 7);
  1113. s[8] = s3 >> 1;
  1114. s[9] = s3 >> 9;
  1115. s[10] = (s3 >> 17) | (s4 << 4);
  1116. s[11] = s4 >> 4;
  1117. s[12] = s4 >> 12;
  1118. s[13] = (s4 >> 20) | (s5 << 1);
  1119. s[14] = s5 >> 7;
  1120. s[15] = (s5 >> 15) | (s6 << 6);
  1121. s[16] = s6 >> 2;
  1122. s[17] = s6 >> 10;
  1123. s[18] = (s6 >> 18) | (s7 << 3);
  1124. s[19] = s7 >> 5;
  1125. s[20] = s7 >> 13;
  1126. s[21] = s8 >> 0;
  1127. s[22] = s8 >> 8;
  1128. s[23] = (s8 >> 16) | (s9 << 5);
  1129. s[24] = s9 >> 3;
  1130. s[25] = s9 >> 11;
  1131. s[26] = (s9 >> 19) | (s10 << 2);
  1132. s[27] = s10 >> 6;
  1133. s[28] = (s10 >> 14) | (s11 << 7);
  1134. s[29] = s11 >> 1;
  1135. s[30] = s11 >> 9;
  1136. s[31] = s11 >> 17;
  1137. }
  1138. // Input:
  1139. // a[0]+256*a[1]+...+256^31*a[31] = a
  1140. // b[0]+256*b[1]+...+256^31*b[31] = b
  1141. // c[0]+256*c[1]+...+256^31*c[31] = c
  1142. //
  1143. // Output:
  1144. // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
  1145. // where l = 2^252 + 27742317777372353535851937790883648493.
  1146. static void sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
  1147. const uint8_t *c) {
  1148. int64_t a0 = 2097151 & load_3(a);
  1149. int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
  1150. int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
  1151. int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
  1152. int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
  1153. int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
  1154. int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
  1155. int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
  1156. int64_t a8 = 2097151 & load_3(a + 21);
  1157. int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
  1158. int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
  1159. int64_t a11 = (load_4(a + 28) >> 7);
  1160. int64_t b0 = 2097151 & load_3(b);
  1161. int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
  1162. int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
  1163. int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
  1164. int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
  1165. int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
  1166. int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
  1167. int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
  1168. int64_t b8 = 2097151 & load_3(b + 21);
  1169. int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
  1170. int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
  1171. int64_t b11 = (load_4(b + 28) >> 7);
  1172. int64_t c0 = 2097151 & load_3(c);
  1173. int64_t c1 = 2097151 & (load_4(c + 2) >> 5);
  1174. int64_t c2 = 2097151 & (load_3(c + 5) >> 2);
  1175. int64_t c3 = 2097151 & (load_4(c + 7) >> 7);
  1176. int64_t c4 = 2097151 & (load_4(c + 10) >> 4);
  1177. int64_t c5 = 2097151 & (load_3(c + 13) >> 1);
  1178. int64_t c6 = 2097151 & (load_4(c + 15) >> 6);
  1179. int64_t c7 = 2097151 & (load_3(c + 18) >> 3);
  1180. int64_t c8 = 2097151 & load_3(c + 21);
  1181. int64_t c9 = 2097151 & (load_4(c + 23) >> 5);
  1182. int64_t c10 = 2097151 & (load_3(c + 26) >> 2);
  1183. int64_t c11 = (load_4(c + 28) >> 7);
  1184. int64_t s0;
  1185. int64_t s1;
  1186. int64_t s2;
  1187. int64_t s3;
  1188. int64_t s4;
  1189. int64_t s5;
  1190. int64_t s6;
  1191. int64_t s7;
  1192. int64_t s8;
  1193. int64_t s9;
  1194. int64_t s10;
  1195. int64_t s11;
  1196. int64_t s12;
  1197. int64_t s13;
  1198. int64_t s14;
  1199. int64_t s15;
  1200. int64_t s16;
  1201. int64_t s17;
  1202. int64_t s18;
  1203. int64_t s19;
  1204. int64_t s20;
  1205. int64_t s21;
  1206. int64_t s22;
  1207. int64_t s23;
  1208. int64_t carry0;
  1209. int64_t carry1;
  1210. int64_t carry2;
  1211. int64_t carry3;
  1212. int64_t carry4;
  1213. int64_t carry5;
  1214. int64_t carry6;
  1215. int64_t carry7;
  1216. int64_t carry8;
  1217. int64_t carry9;
  1218. int64_t carry10;
  1219. int64_t carry11;
  1220. int64_t carry12;
  1221. int64_t carry13;
  1222. int64_t carry14;
  1223. int64_t carry15;
  1224. int64_t carry16;
  1225. int64_t carry17;
  1226. int64_t carry18;
  1227. int64_t carry19;
  1228. int64_t carry20;
  1229. int64_t carry21;
  1230. int64_t carry22;
  1231. s0 = c0 + a0 * b0;
  1232. s1 = c1 + a0 * b1 + a1 * b0;
  1233. s2 = c2 + a0 * b2 + a1 * b1 + a2 * b0;
  1234. s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0;
  1235. s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0;
  1236. s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0;
  1237. s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0;
  1238. s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 +
  1239. a6 * b1 + a7 * b0;
  1240. s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 +
  1241. a6 * b2 + a7 * b1 + a8 * b0;
  1242. s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 +
  1243. a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0;
  1244. s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 +
  1245. a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0;
  1246. s11 = c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 +
  1247. a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0;
  1248. s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 +
  1249. a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1;
  1250. s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 +
  1251. a9 * b4 + a10 * b3 + a11 * b2;
  1252. s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 +
  1253. a10 * b4 + a11 * b3;
  1254. s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 +
  1255. a11 * b4;
  1256. s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5;
  1257. s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6;
  1258. s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7;
  1259. s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8;
  1260. s20 = a9 * b11 + a10 * b10 + a11 * b9;
  1261. s21 = a10 * b11 + a11 * b10;
  1262. s22 = a11 * b11;
  1263. s23 = 0;
  1264. carry0 = (s0 + (1 << 20)) >> 21;
  1265. s1 += carry0;
  1266. s0 -= int64_lshift21(carry0);
  1267. carry2 = (s2 + (1 << 20)) >> 21;
  1268. s3 += carry2;
  1269. s2 -= int64_lshift21(carry2);
  1270. carry4 = (s4 + (1 << 20)) >> 21;
  1271. s5 += carry4;
  1272. s4 -= int64_lshift21(carry4);
  1273. carry6 = (s6 + (1 << 20)) >> 21;
  1274. s7 += carry6;
  1275. s6 -= int64_lshift21(carry6);
  1276. carry8 = (s8 + (1 << 20)) >> 21;
  1277. s9 += carry8;
  1278. s8 -= int64_lshift21(carry8);
  1279. carry10 = (s10 + (1 << 20)) >> 21;
  1280. s11 += carry10;
  1281. s10 -= int64_lshift21(carry10);
  1282. carry12 = (s12 + (1 << 20)) >> 21;
  1283. s13 += carry12;
  1284. s12 -= int64_lshift21(carry12);
  1285. carry14 = (s14 + (1 << 20)) >> 21;
  1286. s15 += carry14;
  1287. s14 -= int64_lshift21(carry14);
  1288. carry16 = (s16 + (1 << 20)) >> 21;
  1289. s17 += carry16;
  1290. s16 -= int64_lshift21(carry16);
  1291. carry18 = (s18 + (1 << 20)) >> 21;
  1292. s19 += carry18;
  1293. s18 -= int64_lshift21(carry18);
  1294. carry20 = (s20 + (1 << 20)) >> 21;
  1295. s21 += carry20;
  1296. s20 -= int64_lshift21(carry20);
  1297. carry22 = (s22 + (1 << 20)) >> 21;
  1298. s23 += carry22;
  1299. s22 -= int64_lshift21(carry22);
  1300. carry1 = (s1 + (1 << 20)) >> 21;
  1301. s2 += carry1;
  1302. s1 -= int64_lshift21(carry1);
  1303. carry3 = (s3 + (1 << 20)) >> 21;
  1304. s4 += carry3;
  1305. s3 -= int64_lshift21(carry3);
  1306. carry5 = (s5 + (1 << 20)) >> 21;
  1307. s6 += carry5;
  1308. s5 -= int64_lshift21(carry5);
  1309. carry7 = (s7 + (1 << 20)) >> 21;
  1310. s8 += carry7;
  1311. s7 -= int64_lshift21(carry7);
  1312. carry9 = (s9 + (1 << 20)) >> 21;
  1313. s10 += carry9;
  1314. s9 -= int64_lshift21(carry9);
  1315. carry11 = (s11 + (1 << 20)) >> 21;
  1316. s12 += carry11;
  1317. s11 -= int64_lshift21(carry11);
  1318. carry13 = (s13 + (1 << 20)) >> 21;
  1319. s14 += carry13;
  1320. s13 -= int64_lshift21(carry13);
  1321. carry15 = (s15 + (1 << 20)) >> 21;
  1322. s16 += carry15;
  1323. s15 -= int64_lshift21(carry15);
  1324. carry17 = (s17 + (1 << 20)) >> 21;
  1325. s18 += carry17;
  1326. s17 -= int64_lshift21(carry17);
  1327. carry19 = (s19 + (1 << 20)) >> 21;
  1328. s20 += carry19;
  1329. s19 -= int64_lshift21(carry19);
  1330. carry21 = (s21 + (1 << 20)) >> 21;
  1331. s22 += carry21;
  1332. s21 -= int64_lshift21(carry21);
  1333. s11 += s23 * 666643;
  1334. s12 += s23 * 470296;
  1335. s13 += s23 * 654183;
  1336. s14 -= s23 * 997805;
  1337. s15 += s23 * 136657;
  1338. s16 -= s23 * 683901;
  1339. s23 = 0;
  1340. s10 += s22 * 666643;
  1341. s11 += s22 * 470296;
  1342. s12 += s22 * 654183;
  1343. s13 -= s22 * 997805;
  1344. s14 += s22 * 136657;
  1345. s15 -= s22 * 683901;
  1346. s22 = 0;
  1347. s9 += s21 * 666643;
  1348. s10 += s21 * 470296;
  1349. s11 += s21 * 654183;
  1350. s12 -= s21 * 997805;
  1351. s13 += s21 * 136657;
  1352. s14 -= s21 * 683901;
  1353. s21 = 0;
  1354. s8 += s20 * 666643;
  1355. s9 += s20 * 470296;
  1356. s10 += s20 * 654183;
  1357. s11 -= s20 * 997805;
  1358. s12 += s20 * 136657;
  1359. s13 -= s20 * 683901;
  1360. s20 = 0;
  1361. s7 += s19 * 666643;
  1362. s8 += s19 * 470296;
  1363. s9 += s19 * 654183;
  1364. s10 -= s19 * 997805;
  1365. s11 += s19 * 136657;
  1366. s12 -= s19 * 683901;
  1367. s19 = 0;
  1368. s6 += s18 * 666643;
  1369. s7 += s18 * 470296;
  1370. s8 += s18 * 654183;
  1371. s9 -= s18 * 997805;
  1372. s10 += s18 * 136657;
  1373. s11 -= s18 * 683901;
  1374. s18 = 0;
  1375. carry6 = (s6 + (1 << 20)) >> 21;
  1376. s7 += carry6;
  1377. s6 -= int64_lshift21(carry6);
  1378. carry8 = (s8 + (1 << 20)) >> 21;
  1379. s9 += carry8;
  1380. s8 -= int64_lshift21(carry8);
  1381. carry10 = (s10 + (1 << 20)) >> 21;
  1382. s11 += carry10;
  1383. s10 -= int64_lshift21(carry10);
  1384. carry12 = (s12 + (1 << 20)) >> 21;
  1385. s13 += carry12;
  1386. s12 -= int64_lshift21(carry12);
  1387. carry14 = (s14 + (1 << 20)) >> 21;
  1388. s15 += carry14;
  1389. s14 -= int64_lshift21(carry14);
  1390. carry16 = (s16 + (1 << 20)) >> 21;
  1391. s17 += carry16;
  1392. s16 -= int64_lshift21(carry16);
  1393. carry7 = (s7 + (1 << 20)) >> 21;
  1394. s8 += carry7;
  1395. s7 -= int64_lshift21(carry7);
  1396. carry9 = (s9 + (1 << 20)) >> 21;
  1397. s10 += carry9;
  1398. s9 -= int64_lshift21(carry9);
  1399. carry11 = (s11 + (1 << 20)) >> 21;
  1400. s12 += carry11;
  1401. s11 -= int64_lshift21(carry11);
  1402. carry13 = (s13 + (1 << 20)) >> 21;
  1403. s14 += carry13;
  1404. s13 -= int64_lshift21(carry13);
  1405. carry15 = (s15 + (1 << 20)) >> 21;
  1406. s16 += carry15;
  1407. s15 -= int64_lshift21(carry15);
  1408. s5 += s17 * 666643;
  1409. s6 += s17 * 470296;
  1410. s7 += s17 * 654183;
  1411. s8 -= s17 * 997805;
  1412. s9 += s17 * 136657;
  1413. s10 -= s17 * 683901;
  1414. s17 = 0;
  1415. s4 += s16 * 666643;
  1416. s5 += s16 * 470296;
  1417. s6 += s16 * 654183;
  1418. s7 -= s16 * 997805;
  1419. s8 += s16 * 136657;
  1420. s9 -= s16 * 683901;
  1421. s16 = 0;
  1422. s3 += s15 * 666643;
  1423. s4 += s15 * 470296;
  1424. s5 += s15 * 654183;
  1425. s6 -= s15 * 997805;
  1426. s7 += s15 * 136657;
  1427. s8 -= s15 * 683901;
  1428. s15 = 0;
  1429. s2 += s14 * 666643;
  1430. s3 += s14 * 470296;
  1431. s4 += s14 * 654183;
  1432. s5 -= s14 * 997805;
  1433. s6 += s14 * 136657;
  1434. s7 -= s14 * 683901;
  1435. s14 = 0;
  1436. s1 += s13 * 666643;
  1437. s2 += s13 * 470296;
  1438. s3 += s13 * 654183;
  1439. s4 -= s13 * 997805;
  1440. s5 += s13 * 136657;
  1441. s6 -= s13 * 683901;
  1442. s13 = 0;
  1443. s0 += s12 * 666643;
  1444. s1 += s12 * 470296;
  1445. s2 += s12 * 654183;
  1446. s3 -= s12 * 997805;
  1447. s4 += s12 * 136657;
  1448. s5 -= s12 * 683901;
  1449. s12 = 0;
  1450. carry0 = (s0 + (1 << 20)) >> 21;
  1451. s1 += carry0;
  1452. s0 -= int64_lshift21(carry0);
  1453. carry2 = (s2 + (1 << 20)) >> 21;
  1454. s3 += carry2;
  1455. s2 -= int64_lshift21(carry2);
  1456. carry4 = (s4 + (1 << 20)) >> 21;
  1457. s5 += carry4;
  1458. s4 -= int64_lshift21(carry4);
  1459. carry6 = (s6 + (1 << 20)) >> 21;
  1460. s7 += carry6;
  1461. s6 -= int64_lshift21(carry6);
  1462. carry8 = (s8 + (1 << 20)) >> 21;
  1463. s9 += carry8;
  1464. s8 -= int64_lshift21(carry8);
  1465. carry10 = (s10 + (1 << 20)) >> 21;
  1466. s11 += carry10;
  1467. s10 -= int64_lshift21(carry10);
  1468. carry1 = (s1 + (1 << 20)) >> 21;
  1469. s2 += carry1;
  1470. s1 -= int64_lshift21(carry1);
  1471. carry3 = (s3 + (1 << 20)) >> 21;
  1472. s4 += carry3;
  1473. s3 -= int64_lshift21(carry3);
  1474. carry5 = (s5 + (1 << 20)) >> 21;
  1475. s6 += carry5;
  1476. s5 -= int64_lshift21(carry5);
  1477. carry7 = (s7 + (1 << 20)) >> 21;
  1478. s8 += carry7;
  1479. s7 -= int64_lshift21(carry7);
  1480. carry9 = (s9 + (1 << 20)) >> 21;
  1481. s10 += carry9;
  1482. s9 -= int64_lshift21(carry9);
  1483. carry11 = (s11 + (1 << 20)) >> 21;
  1484. s12 += carry11;
  1485. s11 -= int64_lshift21(carry11);
  1486. s0 += s12 * 666643;
  1487. s1 += s12 * 470296;
  1488. s2 += s12 * 654183;
  1489. s3 -= s12 * 997805;
  1490. s4 += s12 * 136657;
  1491. s5 -= s12 * 683901;
  1492. s12 = 0;
  1493. carry0 = s0 >> 21;
  1494. s1 += carry0;
  1495. s0 -= int64_lshift21(carry0);
  1496. carry1 = s1 >> 21;
  1497. s2 += carry1;
  1498. s1 -= int64_lshift21(carry1);
  1499. carry2 = s2 >> 21;
  1500. s3 += carry2;
  1501. s2 -= int64_lshift21(carry2);
  1502. carry3 = s3 >> 21;
  1503. s4 += carry3;
  1504. s3 -= int64_lshift21(carry3);
  1505. carry4 = s4 >> 21;
  1506. s5 += carry4;
  1507. s4 -= int64_lshift21(carry4);
  1508. carry5 = s5 >> 21;
  1509. s6 += carry5;
  1510. s5 -= int64_lshift21(carry5);
  1511. carry6 = s6 >> 21;
  1512. s7 += carry6;
  1513. s6 -= int64_lshift21(carry6);
  1514. carry7 = s7 >> 21;
  1515. s8 += carry7;
  1516. s7 -= int64_lshift21(carry7);
  1517. carry8 = s8 >> 21;
  1518. s9 += carry8;
  1519. s8 -= int64_lshift21(carry8);
  1520. carry9 = s9 >> 21;
  1521. s10 += carry9;
  1522. s9 -= int64_lshift21(carry9);
  1523. carry10 = s10 >> 21;
  1524. s11 += carry10;
  1525. s10 -= int64_lshift21(carry10);
  1526. carry11 = s11 >> 21;
  1527. s12 += carry11;
  1528. s11 -= int64_lshift21(carry11);
  1529. s0 += s12 * 666643;
  1530. s1 += s12 * 470296;
  1531. s2 += s12 * 654183;
  1532. s3 -= s12 * 997805;
  1533. s4 += s12 * 136657;
  1534. s5 -= s12 * 683901;
  1535. s12 = 0;
  1536. carry0 = s0 >> 21;
  1537. s1 += carry0;
  1538. s0 -= int64_lshift21(carry0);
  1539. carry1 = s1 >> 21;
  1540. s2 += carry1;
  1541. s1 -= int64_lshift21(carry1);
  1542. carry2 = s2 >> 21;
  1543. s3 += carry2;
  1544. s2 -= int64_lshift21(carry2);
  1545. carry3 = s3 >> 21;
  1546. s4 += carry3;
  1547. s3 -= int64_lshift21(carry3);
  1548. carry4 = s4 >> 21;
  1549. s5 += carry4;
  1550. s4 -= int64_lshift21(carry4);
  1551. carry5 = s5 >> 21;
  1552. s6 += carry5;
  1553. s5 -= int64_lshift21(carry5);
  1554. carry6 = s6 >> 21;
  1555. s7 += carry6;
  1556. s6 -= int64_lshift21(carry6);
  1557. carry7 = s7 >> 21;
  1558. s8 += carry7;
  1559. s7 -= int64_lshift21(carry7);
  1560. carry8 = s8 >> 21;
  1561. s9 += carry8;
  1562. s8 -= int64_lshift21(carry8);
  1563. carry9 = s9 >> 21;
  1564. s10 += carry9;
  1565. s9 -= int64_lshift21(carry9);
  1566. carry10 = s10 >> 21;
  1567. s11 += carry10;
  1568. s10 -= int64_lshift21(carry10);
  1569. s[0] = s0 >> 0;
  1570. s[1] = s0 >> 8;
  1571. s[2] = (s0 >> 16) | (s1 << 5);
  1572. s[3] = s1 >> 3;
  1573. s[4] = s1 >> 11;
  1574. s[5] = (s1 >> 19) | (s2 << 2);
  1575. s[6] = s2 >> 6;
  1576. s[7] = (s2 >> 14) | (s3 << 7);
  1577. s[8] = s3 >> 1;
  1578. s[9] = s3 >> 9;
  1579. s[10] = (s3 >> 17) | (s4 << 4);
  1580. s[11] = s4 >> 4;
  1581. s[12] = s4 >> 12;
  1582. s[13] = (s4 >> 20) | (s5 << 1);
  1583. s[14] = s5 >> 7;
  1584. s[15] = (s5 >> 15) | (s6 << 6);
  1585. s[16] = s6 >> 2;
  1586. s[17] = s6 >> 10;
  1587. s[18] = (s6 >> 18) | (s7 << 3);
  1588. s[19] = s7 >> 5;
  1589. s[20] = s7 >> 13;
  1590. s[21] = s8 >> 0;
  1591. s[22] = s8 >> 8;
  1592. s[23] = (s8 >> 16) | (s9 << 5);
  1593. s[24] = s9 >> 3;
  1594. s[25] = s9 >> 11;
  1595. s[26] = (s9 >> 19) | (s10 << 2);
  1596. s[27] = s10 >> 6;
  1597. s[28] = (s10 >> 14) | (s11 << 7);
  1598. s[29] = s11 >> 1;
  1599. s[30] = s11 >> 9;
  1600. s[31] = s11 >> 17;
  1601. }
  1602. void GFp_x25519_scalar_mult_generic_masked(uint8_t out[32],
  1603. const uint8_t scalar_masked[32],
  1604. const uint8_t point[32]) {
  1605. fe x1, x2, z2, x3, z3, tmp0, tmp1;
  1606. fe_loose x2l, z2l, x3l, tmp0l, tmp1l;
  1607. uint8_t e[32];
  1608. GFp_memcpy(e, scalar_masked, 32);
  1609. // The following implementation was transcribed to Coq and proven to
  1610. // correspond to unary scalar multiplication in affine coordinates given that
  1611. // x1 != 0 is the x coordinate of some point on the curve. It was also checked
  1612. // in Coq that doing a ladderstep with x1 = x3 = 0 gives z2' = z3' = 0, and z2
  1613. // = z3 = 0 gives z2' = z3' = 0. The statement was quantified over the
  1614. // underlying field, so it applies to Curve25519 itself and the quadratic
  1615. // twist of Curve25519. It was not proven in Coq that prime-field arithmetic
  1616. // correctly simulates extension-field arithmetic on prime-field values.
  1617. // The decoding of the byte array representation of e was not considered.
  1618. // Specification of Montgomery curves in affine coordinates:
  1619. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
  1620. // Proof that these form a group that is isomorphic to a Weierstrass curve:
  1621. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
  1622. // Coq transcription and correctness proof of the loop (where scalarbits=255):
  1623. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
  1624. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
  1625. // preconditions: 0 <= e < 2^255 (not necessarily e < order), fe_invert(0) = 0
  1626. fe_frombytes(&x1, point);
  1627. fe_1(&x2);
  1628. fe_0(&z2);
  1629. fe_copy(&x3, &x1);
  1630. fe_1(&z3);
  1631. unsigned swap = 0;
  1632. int pos;
  1633. for (pos = 254; pos >= 0; --pos) {
  1634. // loop invariant as of right before the test, for the case where x1 != 0:
  1635. // pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3 is nonzero
  1636. // let r := e >> (pos+1) in the following equalities of projective points:
  1637. // to_xz (r*P) === if swap then (x3, z3) else (x2, z2)
  1638. // to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
  1639. // x1 is the nonzero x coordinate of the nonzero point (r*P-(r+1)*P)
  1640. unsigned b = 1 & (e[pos / 8] >> (pos & 7));
  1641. swap ^= b;
  1642. fe_cswap(&x2, &x3, swap);
  1643. fe_cswap(&z2, &z3, swap);
  1644. swap = b;
  1645. // Coq transcription of ladderstep formula (called from transcribed loop):
  1646. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
  1647. // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
  1648. // x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
  1649. // x1 = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
  1650. fe_sub(&tmp0l, &x3, &z3);
  1651. fe_sub(&tmp1l, &x2, &z2);
  1652. fe_add(&x2l, &x2, &z2);
  1653. fe_add(&z2l, &x3, &z3);
  1654. fe_mul_tll(&z3, &tmp0l, &x2l);
  1655. fe_mul_tll(&z2, &z2l, &tmp1l);
  1656. fe_sq_tl(&tmp0, &tmp1l);
  1657. fe_sq_tl(&tmp1, &x2l);
  1658. fe_add(&x3l, &z3, &z2);
  1659. fe_sub(&z2l, &z3, &z2);
  1660. fe_mul_ttt(&x2, &tmp1, &tmp0);
  1661. fe_sub(&tmp1l, &tmp1, &tmp0);
  1662. fe_sq_tl(&z2, &z2l);
  1663. fe_mul121666(&z3, &tmp1l);
  1664. fe_sq_tl(&x3, &x3l);
  1665. fe_add(&tmp0l, &tmp0, &z3);
  1666. fe_mul_ttt(&z3, &x1, &z2);
  1667. fe_mul_tll(&z2, &tmp1l, &tmp0l);
  1668. }
  1669. // here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3) else (x2, z2)
  1670. fe_cswap(&x2, &x3, swap);
  1671. fe_cswap(&z2, &z3, swap);
  1672. fe_invert(&z2, &z2);
  1673. fe_mul_ttt(&x2, &x2, &z2);
  1674. fe_tobytes(out, &x2);
  1675. }
  1676. void GFp_x25519_public_from_private_generic_masked(uint8_t out_public_value[32],
  1677. const uint8_t private_key_masked[32]) {
  1678. uint8_t e[32];
  1679. GFp_memcpy(e, private_key_masked, 32);
  1680. ge_p3 A;
  1681. GFp_x25519_ge_scalarmult_base(&A, e);
  1682. // We only need the u-coordinate of the curve25519 point. The map is
  1683. // u=(y+1)/(1-y). Since y=Y/Z, this gives u=(Z+Y)/(Z-Y).
  1684. fe_loose zplusy, zminusy;
  1685. fe zminusy_inv;
  1686. fe_add(&zplusy, &A.Z, &A.Y);
  1687. fe_sub(&zminusy, &A.Z, &A.Y);
  1688. fe_loose_invert(&zminusy_inv, &zminusy);
  1689. fe_mul_tlt(&zminusy_inv, &zplusy, &zminusy_inv);
  1690. fe_tobytes(out_public_value, &zminusy_inv);
  1691. }
  1692. void GFp_x25519_fe_invert(fe *out, const fe *z) {
  1693. fe_invert(out, z);
  1694. }
  1695. uint8_t GFp_x25519_fe_isnegative(const fe *f) {
  1696. return (uint8_t)fe_isnegative(f);
  1697. }
  1698. void GFp_x25519_fe_mul_ttt(fe *h, const fe *f, const fe *g) {
  1699. fe_mul_ttt(h, f, g);
  1700. }
  1701. void GFp_x25519_fe_neg(fe *f) {
  1702. fe_loose t;
  1703. fe_neg(&t, f);
  1704. fe_carry(f, &t);
  1705. }
  1706. void GFp_x25519_fe_tobytes(uint8_t s[32], const fe *h) {
  1707. fe_tobytes(s, h);
  1708. }
  1709. void GFp_x25519_ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a,
  1710. const ge_p3 *A, const uint8_t *b) {
  1711. ge_double_scalarmult_vartime(r, a, A, b);
  1712. }
  1713. void GFp_x25519_sc_mask(uint8_t a[32]) {
  1714. a[0] &= 248;
  1715. a[31] &= 127;
  1716. a[31] |= 64;
  1717. }
  1718. void GFp_x25519_sc_muladd(uint8_t *s, const uint8_t *a, const uint8_t *b,
  1719. const uint8_t *c) {
  1720. sc_muladd(s, a, b, c);
  1721. }