curve25519-fiat32.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright (C) 2015-2016 The fiat-crypto Authors.
  4. * Copyright (C) 2018-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  5. *
  6. * This is a machine-generated formally verified implementation of Curve25519
  7. * ECDH from: <https://github.com/mit-plv/fiat-crypto>. Though originally
  8. * machine generated, it has been tweaked to be suitable for use in the kernel.
  9. * It is optimized for 32-bit machines and machines that cannot work efficiently
  10. * with 128-bit integer types.
  11. */
  12. #include <asm/unaligned.h>
  13. #include <crypto/curve25519.h>
  14. #include <linux/string.h>
  15. /* fe means field element. Here the field is \Z/(2^255-19). An element t,
  16. * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
  17. * t[3]+2^102 t[4]+...+2^230 t[9].
  18. * fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
  19. * Multiplication and carrying produce fe from fe_loose.
  20. */
  21. typedef struct fe { u32 v[10]; } fe;
  22. /* fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc
  23. * Addition and subtraction produce fe_loose from (fe, fe).
  24. */
  25. typedef struct fe_loose { u32 v[10]; } fe_loose;
  26. static __always_inline void fe_frombytes_impl(u32 h[10], const u8 *s)
  27. {
  28. /* Ignores top bit of s. */
  29. u32 a0 = get_unaligned_le32(s);
  30. u32 a1 = get_unaligned_le32(s+4);
  31. u32 a2 = get_unaligned_le32(s+8);
  32. u32 a3 = get_unaligned_le32(s+12);
  33. u32 a4 = get_unaligned_le32(s+16);
  34. u32 a5 = get_unaligned_le32(s+20);
  35. u32 a6 = get_unaligned_le32(s+24);
  36. u32 a7 = get_unaligned_le32(s+28);
  37. h[0] = a0&((1<<26)-1); /* 26 used, 32-26 left. 26 */
  38. h[1] = (a0>>26) | ((a1&((1<<19)-1))<< 6); /* (32-26) + 19 = 6+19 = 25 */
  39. h[2] = (a1>>19) | ((a2&((1<<13)-1))<<13); /* (32-19) + 13 = 13+13 = 26 */
  40. h[3] = (a2>>13) | ((a3&((1<< 6)-1))<<19); /* (32-13) + 6 = 19+ 6 = 25 */
  41. h[4] = (a3>> 6); /* (32- 6) = 26 */
  42. h[5] = a4&((1<<25)-1); /* 25 */
  43. h[6] = (a4>>25) | ((a5&((1<<19)-1))<< 7); /* (32-25) + 19 = 7+19 = 26 */
  44. h[7] = (a5>>19) | ((a6&((1<<12)-1))<<13); /* (32-19) + 12 = 13+12 = 25 */
  45. h[8] = (a6>>12) | ((a7&((1<< 6)-1))<<20); /* (32-12) + 6 = 20+ 6 = 26 */
  46. h[9] = (a7>> 6)&((1<<25)-1); /* 25 */
  47. }
  48. static __always_inline void fe_frombytes(fe *h, const u8 *s)
  49. {
  50. fe_frombytes_impl(h->v, s);
  51. }
  52. static __always_inline u8 /*bool*/
  53. addcarryx_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
  54. {
  55. /* This function extracts 25 bits of result and 1 bit of carry
  56. * (26 total), so a 32-bit intermediate is sufficient.
  57. */
  58. u32 x = a + b + c;
  59. *low = x & ((1 << 25) - 1);
  60. return (x >> 25) & 1;
  61. }
  62. static __always_inline u8 /*bool*/
  63. addcarryx_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
  64. {
  65. /* This function extracts 26 bits of result and 1 bit of carry
  66. * (27 total), so a 32-bit intermediate is sufficient.
  67. */
  68. u32 x = a + b + c;
  69. *low = x & ((1 << 26) - 1);
  70. return (x >> 26) & 1;
  71. }
  72. static __always_inline u8 /*bool*/
  73. subborrow_u25(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
  74. {
  75. /* This function extracts 25 bits of result and 1 bit of borrow
  76. * (26 total), so a 32-bit intermediate is sufficient.
  77. */
  78. u32 x = a - b - c;
  79. *low = x & ((1 << 25) - 1);
  80. return x >> 31;
  81. }
  82. static __always_inline u8 /*bool*/
  83. subborrow_u26(u8 /*bool*/ c, u32 a, u32 b, u32 *low)
  84. {
  85. /* This function extracts 26 bits of result and 1 bit of borrow
  86. *(27 total), so a 32-bit intermediate is sufficient.
  87. */
  88. u32 x = a - b - c;
  89. *low = x & ((1 << 26) - 1);
  90. return x >> 31;
  91. }
  92. static __always_inline u32 cmovznz32(u32 t, u32 z, u32 nz)
  93. {
  94. t = -!!t; /* all set if nonzero, 0 if 0 */
  95. return (t&nz) | ((~t)&z);
  96. }
  97. static __always_inline void fe_freeze(u32 out[10], const u32 in1[10])
  98. {
  99. { const u32 x17 = in1[9];
  100. { const u32 x18 = in1[8];
  101. { const u32 x16 = in1[7];
  102. { const u32 x14 = in1[6];
  103. { const u32 x12 = in1[5];
  104. { const u32 x10 = in1[4];
  105. { const u32 x8 = in1[3];
  106. { const u32 x6 = in1[2];
  107. { const u32 x4 = in1[1];
  108. { const u32 x2 = in1[0];
  109. { u32 x20; u8/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
  110. { u32 x23; u8/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
  111. { u32 x26; u8/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
  112. { u32 x29; u8/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
  113. { u32 x32; u8/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
  114. { u32 x35; u8/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
  115. { u32 x38; u8/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
  116. { u32 x41; u8/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
  117. { u32 x44; u8/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
  118. { u32 x47; u8/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
  119. { u32 x49 = cmovznz32(x48, 0x0, 0xffffffff);
  120. { u32 x50 = (x49 & 0x3ffffed);
  121. { u32 x52; u8/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
  122. { u32 x54 = (x49 & 0x1ffffff);
  123. { u32 x56; u8/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
  124. { u32 x58 = (x49 & 0x3ffffff);
  125. { u32 x60; u8/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
  126. { u32 x62 = (x49 & 0x1ffffff);
  127. { u32 x64; u8/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
  128. { u32 x66 = (x49 & 0x3ffffff);
  129. { u32 x68; u8/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
  130. { u32 x70 = (x49 & 0x1ffffff);
  131. { u32 x72; u8/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
  132. { u32 x74 = (x49 & 0x3ffffff);
  133. { u32 x76; u8/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
  134. { u32 x78 = (x49 & 0x1ffffff);
  135. { u32 x80; u8/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
  136. { u32 x82 = (x49 & 0x3ffffff);
  137. { u32 x84; u8/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
  138. { u32 x86 = (x49 & 0x1ffffff);
  139. { u32 x88; addcarryx_u25(x85, x47, x86, &x88);
  140. out[0] = x52;
  141. out[1] = x56;
  142. out[2] = x60;
  143. out[3] = x64;
  144. out[4] = x68;
  145. out[5] = x72;
  146. out[6] = x76;
  147. out[7] = x80;
  148. out[8] = x84;
  149. out[9] = x88;
  150. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  151. }
  152. static __always_inline void fe_tobytes(u8 s[32], const fe *f)
  153. {
  154. u32 h[10];
  155. fe_freeze(h, f->v);
  156. s[0] = h[0] >> 0;
  157. s[1] = h[0] >> 8;
  158. s[2] = h[0] >> 16;
  159. s[3] = (h[0] >> 24) | (h[1] << 2);
  160. s[4] = h[1] >> 6;
  161. s[5] = h[1] >> 14;
  162. s[6] = (h[1] >> 22) | (h[2] << 3);
  163. s[7] = h[2] >> 5;
  164. s[8] = h[2] >> 13;
  165. s[9] = (h[2] >> 21) | (h[3] << 5);
  166. s[10] = h[3] >> 3;
  167. s[11] = h[3] >> 11;
  168. s[12] = (h[3] >> 19) | (h[4] << 6);
  169. s[13] = h[4] >> 2;
  170. s[14] = h[4] >> 10;
  171. s[15] = h[4] >> 18;
  172. s[16] = h[5] >> 0;
  173. s[17] = h[5] >> 8;
  174. s[18] = h[5] >> 16;
  175. s[19] = (h[5] >> 24) | (h[6] << 1);
  176. s[20] = h[6] >> 7;
  177. s[21] = h[6] >> 15;
  178. s[22] = (h[6] >> 23) | (h[7] << 3);
  179. s[23] = h[7] >> 5;
  180. s[24] = h[7] >> 13;
  181. s[25] = (h[7] >> 21) | (h[8] << 4);
  182. s[26] = h[8] >> 4;
  183. s[27] = h[8] >> 12;
  184. s[28] = (h[8] >> 20) | (h[9] << 6);
  185. s[29] = h[9] >> 2;
  186. s[30] = h[9] >> 10;
  187. s[31] = h[9] >> 18;
  188. }
  189. /* h = f */
  190. static __always_inline void fe_copy(fe *h, const fe *f)
  191. {
  192. memmove(h, f, sizeof(u32) * 10);
  193. }
  194. static __always_inline void fe_copy_lt(fe_loose *h, const fe *f)
  195. {
  196. memmove(h, f, sizeof(u32) * 10);
  197. }
  198. /* h = 0 */
  199. static __always_inline void fe_0(fe *h)
  200. {
  201. memset(h, 0, sizeof(u32) * 10);
  202. }
  203. /* h = 1 */
  204. static __always_inline void fe_1(fe *h)
  205. {
  206. memset(h, 0, sizeof(u32) * 10);
  207. h->v[0] = 1;
  208. }
  209. static noinline void fe_add_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
  210. {
  211. { const u32 x20 = in1[9];
  212. { const u32 x21 = in1[8];
  213. { const u32 x19 = in1[7];
  214. { const u32 x17 = in1[6];
  215. { const u32 x15 = in1[5];
  216. { const u32 x13 = in1[4];
  217. { const u32 x11 = in1[3];
  218. { const u32 x9 = in1[2];
  219. { const u32 x7 = in1[1];
  220. { const u32 x5 = in1[0];
  221. { const u32 x38 = in2[9];
  222. { const u32 x39 = in2[8];
  223. { const u32 x37 = in2[7];
  224. { const u32 x35 = in2[6];
  225. { const u32 x33 = in2[5];
  226. { const u32 x31 = in2[4];
  227. { const u32 x29 = in2[3];
  228. { const u32 x27 = in2[2];
  229. { const u32 x25 = in2[1];
  230. { const u32 x23 = in2[0];
  231. out[0] = (x5 + x23);
  232. out[1] = (x7 + x25);
  233. out[2] = (x9 + x27);
  234. out[3] = (x11 + x29);
  235. out[4] = (x13 + x31);
  236. out[5] = (x15 + x33);
  237. out[6] = (x17 + x35);
  238. out[7] = (x19 + x37);
  239. out[8] = (x21 + x39);
  240. out[9] = (x20 + x38);
  241. }}}}}}}}}}}}}}}}}}}}
  242. }
  243. /* h = f + g
  244. * Can overlap h with f or g.
  245. */
  246. static __always_inline void fe_add(fe_loose *h, const fe *f, const fe *g)
  247. {
  248. fe_add_impl(h->v, f->v, g->v);
  249. }
  250. static noinline void fe_sub_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
  251. {
  252. { const u32 x20 = in1[9];
  253. { const u32 x21 = in1[8];
  254. { const u32 x19 = in1[7];
  255. { const u32 x17 = in1[6];
  256. { const u32 x15 = in1[5];
  257. { const u32 x13 = in1[4];
  258. { const u32 x11 = in1[3];
  259. { const u32 x9 = in1[2];
  260. { const u32 x7 = in1[1];
  261. { const u32 x5 = in1[0];
  262. { const u32 x38 = in2[9];
  263. { const u32 x39 = in2[8];
  264. { const u32 x37 = in2[7];
  265. { const u32 x35 = in2[6];
  266. { const u32 x33 = in2[5];
  267. { const u32 x31 = in2[4];
  268. { const u32 x29 = in2[3];
  269. { const u32 x27 = in2[2];
  270. { const u32 x25 = in2[1];
  271. { const u32 x23 = in2[0];
  272. out[0] = ((0x7ffffda + x5) - x23);
  273. out[1] = ((0x3fffffe + x7) - x25);
  274. out[2] = ((0x7fffffe + x9) - x27);
  275. out[3] = ((0x3fffffe + x11) - x29);
  276. out[4] = ((0x7fffffe + x13) - x31);
  277. out[5] = ((0x3fffffe + x15) - x33);
  278. out[6] = ((0x7fffffe + x17) - x35);
  279. out[7] = ((0x3fffffe + x19) - x37);
  280. out[8] = ((0x7fffffe + x21) - x39);
  281. out[9] = ((0x3fffffe + x20) - x38);
  282. }}}}}}}}}}}}}}}}}}}}
  283. }
  284. /* h = f - g
  285. * Can overlap h with f or g.
  286. */
  287. static __always_inline void fe_sub(fe_loose *h, const fe *f, const fe *g)
  288. {
  289. fe_sub_impl(h->v, f->v, g->v);
  290. }
  291. static noinline void fe_mul_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
  292. {
  293. { const u32 x20 = in1[9];
  294. { const u32 x21 = in1[8];
  295. { const u32 x19 = in1[7];
  296. { const u32 x17 = in1[6];
  297. { const u32 x15 = in1[5];
  298. { const u32 x13 = in1[4];
  299. { const u32 x11 = in1[3];
  300. { const u32 x9 = in1[2];
  301. { const u32 x7 = in1[1];
  302. { const u32 x5 = in1[0];
  303. { const u32 x38 = in2[9];
  304. { const u32 x39 = in2[8];
  305. { const u32 x37 = in2[7];
  306. { const u32 x35 = in2[6];
  307. { const u32 x33 = in2[5];
  308. { const u32 x31 = in2[4];
  309. { const u32 x29 = in2[3];
  310. { const u32 x27 = in2[2];
  311. { const u32 x25 = in2[1];
  312. { const u32 x23 = in2[0];
  313. { u64 x40 = ((u64)x23 * x5);
  314. { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
  315. { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
  316. { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
  317. { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
  318. { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
  319. { u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
  320. { u64 x47 = (((((((((u64)x29 * x13) + ((u64)x31 * x11)) + ((u64)x27 * x15)) + ((u64)x33 * x9)) + ((u64)x25 * x17)) + ((u64)x35 * x7)) + ((u64)x23 * x19)) + ((u64)x37 * x5));
  321. { u64 x48 = (((((((u64)x31 * x13) + (0x2 * (((((u64)x29 * x15) + ((u64)x33 * x11)) + ((u64)x25 * x19)) + ((u64)x37 * x7)))) + ((u64)x27 * x17)) + ((u64)x35 * x9)) + ((u64)x23 * x21)) + ((u64)x39 * x5));
  322. { u64 x49 = (((((((((((u64)x31 * x15) + ((u64)x33 * x13)) + ((u64)x29 * x17)) + ((u64)x35 * x11)) + ((u64)x27 * x19)) + ((u64)x37 * x9)) + ((u64)x25 * x21)) + ((u64)x39 * x7)) + ((u64)x23 * x20)) + ((u64)x38 * x5));
  323. { u64 x50 = (((((0x2 * ((((((u64)x33 * x15) + ((u64)x29 * x19)) + ((u64)x37 * x11)) + ((u64)x25 * x20)) + ((u64)x38 * x7))) + ((u64)x31 * x17)) + ((u64)x35 * x13)) + ((u64)x27 * x21)) + ((u64)x39 * x9));
  324. { u64 x51 = (((((((((u64)x33 * x17) + ((u64)x35 * x15)) + ((u64)x31 * x19)) + ((u64)x37 * x13)) + ((u64)x29 * x21)) + ((u64)x39 * x11)) + ((u64)x27 * x20)) + ((u64)x38 * x9));
  325. { u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
  326. { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
  327. { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
  328. { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
  329. { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
  330. { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
  331. { u64 x58 = ((u64)(0x2 * x38) * x20);
  332. { u64 x59 = (x48 + (x58 << 0x4));
  333. { u64 x60 = (x59 + (x58 << 0x1));
  334. { u64 x61 = (x60 + x58);
  335. { u64 x62 = (x47 + (x57 << 0x4));
  336. { u64 x63 = (x62 + (x57 << 0x1));
  337. { u64 x64 = (x63 + x57);
  338. { u64 x65 = (x46 + (x56 << 0x4));
  339. { u64 x66 = (x65 + (x56 << 0x1));
  340. { u64 x67 = (x66 + x56);
  341. { u64 x68 = (x45 + (x55 << 0x4));
  342. { u64 x69 = (x68 + (x55 << 0x1));
  343. { u64 x70 = (x69 + x55);
  344. { u64 x71 = (x44 + (x54 << 0x4));
  345. { u64 x72 = (x71 + (x54 << 0x1));
  346. { u64 x73 = (x72 + x54);
  347. { u64 x74 = (x43 + (x53 << 0x4));
  348. { u64 x75 = (x74 + (x53 << 0x1));
  349. { u64 x76 = (x75 + x53);
  350. { u64 x77 = (x42 + (x52 << 0x4));
  351. { u64 x78 = (x77 + (x52 << 0x1));
  352. { u64 x79 = (x78 + x52);
  353. { u64 x80 = (x41 + (x51 << 0x4));
  354. { u64 x81 = (x80 + (x51 << 0x1));
  355. { u64 x82 = (x81 + x51);
  356. { u64 x83 = (x40 + (x50 << 0x4));
  357. { u64 x84 = (x83 + (x50 << 0x1));
  358. { u64 x85 = (x84 + x50);
  359. { u64 x86 = (x85 >> 0x1a);
  360. { u32 x87 = ((u32)x85 & 0x3ffffff);
  361. { u64 x88 = (x86 + x82);
  362. { u64 x89 = (x88 >> 0x19);
  363. { u32 x90 = ((u32)x88 & 0x1ffffff);
  364. { u64 x91 = (x89 + x79);
  365. { u64 x92 = (x91 >> 0x1a);
  366. { u32 x93 = ((u32)x91 & 0x3ffffff);
  367. { u64 x94 = (x92 + x76);
  368. { u64 x95 = (x94 >> 0x19);
  369. { u32 x96 = ((u32)x94 & 0x1ffffff);
  370. { u64 x97 = (x95 + x73);
  371. { u64 x98 = (x97 >> 0x1a);
  372. { u32 x99 = ((u32)x97 & 0x3ffffff);
  373. { u64 x100 = (x98 + x70);
  374. { u64 x101 = (x100 >> 0x19);
  375. { u32 x102 = ((u32)x100 & 0x1ffffff);
  376. { u64 x103 = (x101 + x67);
  377. { u64 x104 = (x103 >> 0x1a);
  378. { u32 x105 = ((u32)x103 & 0x3ffffff);
  379. { u64 x106 = (x104 + x64);
  380. { u64 x107 = (x106 >> 0x19);
  381. { u32 x108 = ((u32)x106 & 0x1ffffff);
  382. { u64 x109 = (x107 + x61);
  383. { u64 x110 = (x109 >> 0x1a);
  384. { u32 x111 = ((u32)x109 & 0x3ffffff);
  385. { u64 x112 = (x110 + x49);
  386. { u64 x113 = (x112 >> 0x19);
  387. { u32 x114 = ((u32)x112 & 0x1ffffff);
  388. { u64 x115 = (x87 + (0x13 * x113));
  389. { u32 x116 = (u32) (x115 >> 0x1a);
  390. { u32 x117 = ((u32)x115 & 0x3ffffff);
  391. { u32 x118 = (x116 + x90);
  392. { u32 x119 = (x118 >> 0x19);
  393. { u32 x120 = (x118 & 0x1ffffff);
  394. out[0] = x117;
  395. out[1] = x120;
  396. out[2] = (x119 + x93);
  397. out[3] = x96;
  398. out[4] = x99;
  399. out[5] = x102;
  400. out[6] = x105;
  401. out[7] = x108;
  402. out[8] = x111;
  403. out[9] = x114;
  404. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  405. }
  406. static __always_inline void fe_mul_ttt(fe *h, const fe *f, const fe *g)
  407. {
  408. fe_mul_impl(h->v, f->v, g->v);
  409. }
  410. static __always_inline void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g)
  411. {
  412. fe_mul_impl(h->v, f->v, g->v);
  413. }
  414. static __always_inline void
  415. fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g)
  416. {
  417. fe_mul_impl(h->v, f->v, g->v);
  418. }
  419. static noinline void fe_sqr_impl(u32 out[10], const u32 in1[10])
  420. {
  421. { const u32 x17 = in1[9];
  422. { const u32 x18 = in1[8];
  423. { const u32 x16 = in1[7];
  424. { const u32 x14 = in1[6];
  425. { const u32 x12 = in1[5];
  426. { const u32 x10 = in1[4];
  427. { const u32 x8 = in1[3];
  428. { const u32 x6 = in1[2];
  429. { const u32 x4 = in1[1];
  430. { const u32 x2 = in1[0];
  431. { u64 x19 = ((u64)x2 * x2);
  432. { u64 x20 = ((u64)(0x2 * x2) * x4);
  433. { u64 x21 = (0x2 * (((u64)x4 * x4) + ((u64)x2 * x6)));
  434. { u64 x22 = (0x2 * (((u64)x4 * x6) + ((u64)x2 * x8)));
  435. { u64 x23 = ((((u64)x6 * x6) + ((u64)(0x4 * x4) * x8)) + ((u64)(0x2 * x2) * x10));
  436. { u64 x24 = (0x2 * ((((u64)x6 * x8) + ((u64)x4 * x10)) + ((u64)x2 * x12)));
  437. { u64 x25 = (0x2 * (((((u64)x8 * x8) + ((u64)x6 * x10)) + ((u64)x2 * x14)) + ((u64)(0x2 * x4) * x12)));
  438. { u64 x26 = (0x2 * (((((u64)x8 * x10) + ((u64)x6 * x12)) + ((u64)x4 * x14)) + ((u64)x2 * x16)));
  439. { u64 x27 = (((u64)x10 * x10) + (0x2 * ((((u64)x6 * x14) + ((u64)x2 * x18)) + (0x2 * (((u64)x4 * x16) + ((u64)x8 * x12))))));
  440. { u64 x28 = (0x2 * ((((((u64)x10 * x12) + ((u64)x8 * x14)) + ((u64)x6 * x16)) + ((u64)x4 * x18)) + ((u64)x2 * x17)));
  441. { u64 x29 = (0x2 * (((((u64)x12 * x12) + ((u64)x10 * x14)) + ((u64)x6 * x18)) + (0x2 * (((u64)x8 * x16) + ((u64)x4 * x17)))));
  442. { u64 x30 = (0x2 * (((((u64)x12 * x14) + ((u64)x10 * x16)) + ((u64)x8 * x18)) + ((u64)x6 * x17)));
  443. { u64 x31 = (((u64)x14 * x14) + (0x2 * (((u64)x10 * x18) + (0x2 * (((u64)x12 * x16) + ((u64)x8 * x17))))));
  444. { u64 x32 = (0x2 * ((((u64)x14 * x16) + ((u64)x12 * x18)) + ((u64)x10 * x17)));
  445. { u64 x33 = (0x2 * ((((u64)x16 * x16) + ((u64)x14 * x18)) + ((u64)(0x2 * x12) * x17)));
  446. { u64 x34 = (0x2 * (((u64)x16 * x18) + ((u64)x14 * x17)));
  447. { u64 x35 = (((u64)x18 * x18) + ((u64)(0x4 * x16) * x17));
  448. { u64 x36 = ((u64)(0x2 * x18) * x17);
  449. { u64 x37 = ((u64)(0x2 * x17) * x17);
  450. { u64 x38 = (x27 + (x37 << 0x4));
  451. { u64 x39 = (x38 + (x37 << 0x1));
  452. { u64 x40 = (x39 + x37);
  453. { u64 x41 = (x26 + (x36 << 0x4));
  454. { u64 x42 = (x41 + (x36 << 0x1));
  455. { u64 x43 = (x42 + x36);
  456. { u64 x44 = (x25 + (x35 << 0x4));
  457. { u64 x45 = (x44 + (x35 << 0x1));
  458. { u64 x46 = (x45 + x35);
  459. { u64 x47 = (x24 + (x34 << 0x4));
  460. { u64 x48 = (x47 + (x34 << 0x1));
  461. { u64 x49 = (x48 + x34);
  462. { u64 x50 = (x23 + (x33 << 0x4));
  463. { u64 x51 = (x50 + (x33 << 0x1));
  464. { u64 x52 = (x51 + x33);
  465. { u64 x53 = (x22 + (x32 << 0x4));
  466. { u64 x54 = (x53 + (x32 << 0x1));
  467. { u64 x55 = (x54 + x32);
  468. { u64 x56 = (x21 + (x31 << 0x4));
  469. { u64 x57 = (x56 + (x31 << 0x1));
  470. { u64 x58 = (x57 + x31);
  471. { u64 x59 = (x20 + (x30 << 0x4));
  472. { u64 x60 = (x59 + (x30 << 0x1));
  473. { u64 x61 = (x60 + x30);
  474. { u64 x62 = (x19 + (x29 << 0x4));
  475. { u64 x63 = (x62 + (x29 << 0x1));
  476. { u64 x64 = (x63 + x29);
  477. { u64 x65 = (x64 >> 0x1a);
  478. { u32 x66 = ((u32)x64 & 0x3ffffff);
  479. { u64 x67 = (x65 + x61);
  480. { u64 x68 = (x67 >> 0x19);
  481. { u32 x69 = ((u32)x67 & 0x1ffffff);
  482. { u64 x70 = (x68 + x58);
  483. { u64 x71 = (x70 >> 0x1a);
  484. { u32 x72 = ((u32)x70 & 0x3ffffff);
  485. { u64 x73 = (x71 + x55);
  486. { u64 x74 = (x73 >> 0x19);
  487. { u32 x75 = ((u32)x73 & 0x1ffffff);
  488. { u64 x76 = (x74 + x52);
  489. { u64 x77 = (x76 >> 0x1a);
  490. { u32 x78 = ((u32)x76 & 0x3ffffff);
  491. { u64 x79 = (x77 + x49);
  492. { u64 x80 = (x79 >> 0x19);
  493. { u32 x81 = ((u32)x79 & 0x1ffffff);
  494. { u64 x82 = (x80 + x46);
  495. { u64 x83 = (x82 >> 0x1a);
  496. { u32 x84 = ((u32)x82 & 0x3ffffff);
  497. { u64 x85 = (x83 + x43);
  498. { u64 x86 = (x85 >> 0x19);
  499. { u32 x87 = ((u32)x85 & 0x1ffffff);
  500. { u64 x88 = (x86 + x40);
  501. { u64 x89 = (x88 >> 0x1a);
  502. { u32 x90 = ((u32)x88 & 0x3ffffff);
  503. { u64 x91 = (x89 + x28);
  504. { u64 x92 = (x91 >> 0x19);
  505. { u32 x93 = ((u32)x91 & 0x1ffffff);
  506. { u64 x94 = (x66 + (0x13 * x92));
  507. { u32 x95 = (u32) (x94 >> 0x1a);
  508. { u32 x96 = ((u32)x94 & 0x3ffffff);
  509. { u32 x97 = (x95 + x69);
  510. { u32 x98 = (x97 >> 0x19);
  511. { u32 x99 = (x97 & 0x1ffffff);
  512. out[0] = x96;
  513. out[1] = x99;
  514. out[2] = (x98 + x72);
  515. out[3] = x75;
  516. out[4] = x78;
  517. out[5] = x81;
  518. out[6] = x84;
  519. out[7] = x87;
  520. out[8] = x90;
  521. out[9] = x93;
  522. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  523. }
  524. static __always_inline void fe_sq_tl(fe *h, const fe_loose *f)
  525. {
  526. fe_sqr_impl(h->v, f->v);
  527. }
  528. static __always_inline void fe_sq_tt(fe *h, const fe *f)
  529. {
  530. fe_sqr_impl(h->v, f->v);
  531. }
  532. static __always_inline void fe_loose_invert(fe *out, const fe_loose *z)
  533. {
  534. fe t0;
  535. fe t1;
  536. fe t2;
  537. fe t3;
  538. int i;
  539. fe_sq_tl(&t0, z);
  540. fe_sq_tt(&t1, &t0);
  541. for (i = 1; i < 2; ++i)
  542. fe_sq_tt(&t1, &t1);
  543. fe_mul_tlt(&t1, z, &t1);
  544. fe_mul_ttt(&t0, &t0, &t1);
  545. fe_sq_tt(&t2, &t0);
  546. fe_mul_ttt(&t1, &t1, &t2);
  547. fe_sq_tt(&t2, &t1);
  548. for (i = 1; i < 5; ++i)
  549. fe_sq_tt(&t2, &t2);
  550. fe_mul_ttt(&t1, &t2, &t1);
  551. fe_sq_tt(&t2, &t1);
  552. for (i = 1; i < 10; ++i)
  553. fe_sq_tt(&t2, &t2);
  554. fe_mul_ttt(&t2, &t2, &t1);
  555. fe_sq_tt(&t3, &t2);
  556. for (i = 1; i < 20; ++i)
  557. fe_sq_tt(&t3, &t3);
  558. fe_mul_ttt(&t2, &t3, &t2);
  559. fe_sq_tt(&t2, &t2);
  560. for (i = 1; i < 10; ++i)
  561. fe_sq_tt(&t2, &t2);
  562. fe_mul_ttt(&t1, &t2, &t1);
  563. fe_sq_tt(&t2, &t1);
  564. for (i = 1; i < 50; ++i)
  565. fe_sq_tt(&t2, &t2);
  566. fe_mul_ttt(&t2, &t2, &t1);
  567. fe_sq_tt(&t3, &t2);
  568. for (i = 1; i < 100; ++i)
  569. fe_sq_tt(&t3, &t3);
  570. fe_mul_ttt(&t2, &t3, &t2);
  571. fe_sq_tt(&t2, &t2);
  572. for (i = 1; i < 50; ++i)
  573. fe_sq_tt(&t2, &t2);
  574. fe_mul_ttt(&t1, &t2, &t1);
  575. fe_sq_tt(&t1, &t1);
  576. for (i = 1; i < 5; ++i)
  577. fe_sq_tt(&t1, &t1);
  578. fe_mul_ttt(out, &t1, &t0);
  579. }
  580. static __always_inline void fe_invert(fe *out, const fe *z)
  581. {
  582. fe_loose l;
  583. fe_copy_lt(&l, z);
  584. fe_loose_invert(out, &l);
  585. }
  586. /* Replace (f,g) with (g,f) if b == 1;
  587. * replace (f,g) with (f,g) if b == 0.
  588. *
  589. * Preconditions: b in {0,1}
  590. */
  591. static noinline void fe_cswap(fe *f, fe *g, unsigned int b)
  592. {
  593. unsigned i;
  594. b = 0 - b;
  595. for (i = 0; i < 10; i++) {
  596. u32 x = f->v[i] ^ g->v[i];
  597. x &= b;
  598. f->v[i] ^= x;
  599. g->v[i] ^= x;
  600. }
  601. }
  602. /* NOTE: based on fiat-crypto fe_mul, edited for in2=121666, 0, 0.*/
  603. static __always_inline void fe_mul_121666_impl(u32 out[10], const u32 in1[10])
  604. {
  605. { const u32 x20 = in1[9];
  606. { const u32 x21 = in1[8];
  607. { const u32 x19 = in1[7];
  608. { const u32 x17 = in1[6];
  609. { const u32 x15 = in1[5];
  610. { const u32 x13 = in1[4];
  611. { const u32 x11 = in1[3];
  612. { const u32 x9 = in1[2];
  613. { const u32 x7 = in1[1];
  614. { const u32 x5 = in1[0];
  615. { const u32 x38 = 0;
  616. { const u32 x39 = 0;
  617. { const u32 x37 = 0;
  618. { const u32 x35 = 0;
  619. { const u32 x33 = 0;
  620. { const u32 x31 = 0;
  621. { const u32 x29 = 0;
  622. { const u32 x27 = 0;
  623. { const u32 x25 = 0;
  624. { const u32 x23 = 121666;
  625. { u64 x40 = ((u64)x23 * x5);
  626. { u64 x41 = (((u64)x23 * x7) + ((u64)x25 * x5));
  627. { u64 x42 = ((((u64)(0x2 * x25) * x7) + ((u64)x23 * x9)) + ((u64)x27 * x5));
  628. { u64 x43 = (((((u64)x25 * x9) + ((u64)x27 * x7)) + ((u64)x23 * x11)) + ((u64)x29 * x5));
  629. { u64 x44 = (((((u64)x27 * x9) + (0x2 * (((u64)x25 * x11) + ((u64)x29 * x7)))) + ((u64)x23 * x13)) + ((u64)x31 * x5));
  630. { u64 x45 = (((((((u64)x27 * x11) + ((u64)x29 * x9)) + ((u64)x25 * x13)) + ((u64)x31 * x7)) + ((u64)x23 * x15)) + ((u64)x33 * x5));
  631. { u64 x46 = (((((0x2 * ((((u64)x29 * x11) + ((u64)x25 * x15)) + ((u64)x33 * x7))) + ((u64)x27 * x13)) + ((u64)x31 * x9)) + ((u64)x23 * x17)) + ((u64)x35 * x5));
  632. { u64 x47 = (((((((((u64)x29 * x13) + ((u64)x31 * x11)) + ((u64)x27 * x15)) + ((u64)x33 * x9)) + ((u64)x25 * x17)) + ((u64)x35 * x7)) + ((u64)x23 * x19)) + ((u64)x37 * x5));
  633. { u64 x48 = (((((((u64)x31 * x13) + (0x2 * (((((u64)x29 * x15) + ((u64)x33 * x11)) + ((u64)x25 * x19)) + ((u64)x37 * x7)))) + ((u64)x27 * x17)) + ((u64)x35 * x9)) + ((u64)x23 * x21)) + ((u64)x39 * x5));
  634. { u64 x49 = (((((((((((u64)x31 * x15) + ((u64)x33 * x13)) + ((u64)x29 * x17)) + ((u64)x35 * x11)) + ((u64)x27 * x19)) + ((u64)x37 * x9)) + ((u64)x25 * x21)) + ((u64)x39 * x7)) + ((u64)x23 * x20)) + ((u64)x38 * x5));
  635. { u64 x50 = (((((0x2 * ((((((u64)x33 * x15) + ((u64)x29 * x19)) + ((u64)x37 * x11)) + ((u64)x25 * x20)) + ((u64)x38 * x7))) + ((u64)x31 * x17)) + ((u64)x35 * x13)) + ((u64)x27 * x21)) + ((u64)x39 * x9));
  636. { u64 x51 = (((((((((u64)x33 * x17) + ((u64)x35 * x15)) + ((u64)x31 * x19)) + ((u64)x37 * x13)) + ((u64)x29 * x21)) + ((u64)x39 * x11)) + ((u64)x27 * x20)) + ((u64)x38 * x9));
  637. { u64 x52 = (((((u64)x35 * x17) + (0x2 * (((((u64)x33 * x19) + ((u64)x37 * x15)) + ((u64)x29 * x20)) + ((u64)x38 * x11)))) + ((u64)x31 * x21)) + ((u64)x39 * x13));
  638. { u64 x53 = (((((((u64)x35 * x19) + ((u64)x37 * x17)) + ((u64)x33 * x21)) + ((u64)x39 * x15)) + ((u64)x31 * x20)) + ((u64)x38 * x13));
  639. { u64 x54 = (((0x2 * ((((u64)x37 * x19) + ((u64)x33 * x20)) + ((u64)x38 * x15))) + ((u64)x35 * x21)) + ((u64)x39 * x17));
  640. { u64 x55 = (((((u64)x37 * x21) + ((u64)x39 * x19)) + ((u64)x35 * x20)) + ((u64)x38 * x17));
  641. { u64 x56 = (((u64)x39 * x21) + (0x2 * (((u64)x37 * x20) + ((u64)x38 * x19))));
  642. { u64 x57 = (((u64)x39 * x20) + ((u64)x38 * x21));
  643. { u64 x58 = ((u64)(0x2 * x38) * x20);
  644. { u64 x59 = (x48 + (x58 << 0x4));
  645. { u64 x60 = (x59 + (x58 << 0x1));
  646. { u64 x61 = (x60 + x58);
  647. { u64 x62 = (x47 + (x57 << 0x4));
  648. { u64 x63 = (x62 + (x57 << 0x1));
  649. { u64 x64 = (x63 + x57);
  650. { u64 x65 = (x46 + (x56 << 0x4));
  651. { u64 x66 = (x65 + (x56 << 0x1));
  652. { u64 x67 = (x66 + x56);
  653. { u64 x68 = (x45 + (x55 << 0x4));
  654. { u64 x69 = (x68 + (x55 << 0x1));
  655. { u64 x70 = (x69 + x55);
  656. { u64 x71 = (x44 + (x54 << 0x4));
  657. { u64 x72 = (x71 + (x54 << 0x1));
  658. { u64 x73 = (x72 + x54);
  659. { u64 x74 = (x43 + (x53 << 0x4));
  660. { u64 x75 = (x74 + (x53 << 0x1));
  661. { u64 x76 = (x75 + x53);
  662. { u64 x77 = (x42 + (x52 << 0x4));
  663. { u64 x78 = (x77 + (x52 << 0x1));
  664. { u64 x79 = (x78 + x52);
  665. { u64 x80 = (x41 + (x51 << 0x4));
  666. { u64 x81 = (x80 + (x51 << 0x1));
  667. { u64 x82 = (x81 + x51);
  668. { u64 x83 = (x40 + (x50 << 0x4));
  669. { u64 x84 = (x83 + (x50 << 0x1));
  670. { u64 x85 = (x84 + x50);
  671. { u64 x86 = (x85 >> 0x1a);
  672. { u32 x87 = ((u32)x85 & 0x3ffffff);
  673. { u64 x88 = (x86 + x82);
  674. { u64 x89 = (x88 >> 0x19);
  675. { u32 x90 = ((u32)x88 & 0x1ffffff);
  676. { u64 x91 = (x89 + x79);
  677. { u64 x92 = (x91 >> 0x1a);
  678. { u32 x93 = ((u32)x91 & 0x3ffffff);
  679. { u64 x94 = (x92 + x76);
  680. { u64 x95 = (x94 >> 0x19);
  681. { u32 x96 = ((u32)x94 & 0x1ffffff);
  682. { u64 x97 = (x95 + x73);
  683. { u64 x98 = (x97 >> 0x1a);
  684. { u32 x99 = ((u32)x97 & 0x3ffffff);
  685. { u64 x100 = (x98 + x70);
  686. { u64 x101 = (x100 >> 0x19);
  687. { u32 x102 = ((u32)x100 & 0x1ffffff);
  688. { u64 x103 = (x101 + x67);
  689. { u64 x104 = (x103 >> 0x1a);
  690. { u32 x105 = ((u32)x103 & 0x3ffffff);
  691. { u64 x106 = (x104 + x64);
  692. { u64 x107 = (x106 >> 0x19);
  693. { u32 x108 = ((u32)x106 & 0x1ffffff);
  694. { u64 x109 = (x107 + x61);
  695. { u64 x110 = (x109 >> 0x1a);
  696. { u32 x111 = ((u32)x109 & 0x3ffffff);
  697. { u64 x112 = (x110 + x49);
  698. { u64 x113 = (x112 >> 0x19);
  699. { u32 x114 = ((u32)x112 & 0x1ffffff);
  700. { u64 x115 = (x87 + (0x13 * x113));
  701. { u32 x116 = (u32) (x115 >> 0x1a);
  702. { u32 x117 = ((u32)x115 & 0x3ffffff);
  703. { u32 x118 = (x116 + x90);
  704. { u32 x119 = (x118 >> 0x19);
  705. { u32 x120 = (x118 & 0x1ffffff);
  706. out[0] = x117;
  707. out[1] = x120;
  708. out[2] = (x119 + x93);
  709. out[3] = x96;
  710. out[4] = x99;
  711. out[5] = x102;
  712. out[6] = x105;
  713. out[7] = x108;
  714. out[8] = x111;
  715. out[9] = x114;
  716. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  717. }
  718. static __always_inline void fe_mul121666(fe *h, const fe_loose *f)
  719. {
  720. fe_mul_121666_impl(h->v, f->v);
  721. }
  722. void curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
  723. const u8 scalar[CURVE25519_KEY_SIZE],
  724. const u8 point[CURVE25519_KEY_SIZE])
  725. {
  726. fe x1, x2, z2, x3, z3;
  727. fe_loose x2l, z2l, x3l;
  728. unsigned swap = 0;
  729. int pos;
  730. u8 e[32];
  731. memcpy(e, scalar, 32);
  732. curve25519_clamp_secret(e);
  733. /* The following implementation was transcribed to Coq and proven to
  734. * correspond to unary scalar multiplication in affine coordinates given
  735. * that x1 != 0 is the x coordinate of some point on the curve. It was
  736. * also checked in Coq that doing a ladderstep with x1 = x3 = 0 gives
  737. * z2' = z3' = 0, and z2 = z3 = 0 gives z2' = z3' = 0. The statement was
  738. * quantified over the underlying field, so it applies to Curve25519
  739. * itself and the quadratic twist of Curve25519. It was not proven in
  740. * Coq that prime-field arithmetic correctly simulates extension-field
  741. * arithmetic on prime-field values. The decoding of the byte array
  742. * representation of e was not considered.
  743. *
  744. * Specification of Montgomery curves in affine coordinates:
  745. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
  746. *
  747. * Proof that these form a group that is isomorphic to a Weierstrass
  748. * curve:
  749. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
  750. *
  751. * Coq transcription and correctness proof of the loop
  752. * (where scalarbits=255):
  753. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
  754. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
  755. * preconditions: 0 <= e < 2^255 (not necessarily e < order),
  756. * fe_invert(0) = 0
  757. */
  758. fe_frombytes(&x1, point);
  759. fe_1(&x2);
  760. fe_0(&z2);
  761. fe_copy(&x3, &x1);
  762. fe_1(&z3);
  763. for (pos = 254; pos >= 0; --pos) {
  764. fe tmp0, tmp1;
  765. fe_loose tmp0l, tmp1l;
  766. /* loop invariant as of right before the test, for the case
  767. * where x1 != 0:
  768. * pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3
  769. * is nonzero
  770. * let r := e >> (pos+1) in the following equalities of
  771. * projective points:
  772. * to_xz (r*P) === if swap then (x3, z3) else (x2, z2)
  773. * to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
  774. * x1 is the nonzero x coordinate of the nonzero
  775. * point (r*P-(r+1)*P)
  776. */
  777. unsigned b = 1 & (e[pos / 8] >> (pos & 7));
  778. swap ^= b;
  779. fe_cswap(&x2, &x3, swap);
  780. fe_cswap(&z2, &z3, swap);
  781. swap = b;
  782. /* Coq transcription of ladderstep formula (called from
  783. * transcribed loop):
  784. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
  785. * <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
  786. * x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
  787. * x1 = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
  788. */
  789. fe_sub(&tmp0l, &x3, &z3);
  790. fe_sub(&tmp1l, &x2, &z2);
  791. fe_add(&x2l, &x2, &z2);
  792. fe_add(&z2l, &x3, &z3);
  793. fe_mul_tll(&z3, &tmp0l, &x2l);
  794. fe_mul_tll(&z2, &z2l, &tmp1l);
  795. fe_sq_tl(&tmp0, &tmp1l);
  796. fe_sq_tl(&tmp1, &x2l);
  797. fe_add(&x3l, &z3, &z2);
  798. fe_sub(&z2l, &z3, &z2);
  799. fe_mul_ttt(&x2, &tmp1, &tmp0);
  800. fe_sub(&tmp1l, &tmp1, &tmp0);
  801. fe_sq_tl(&z2, &z2l);
  802. fe_mul121666(&z3, &tmp1l);
  803. fe_sq_tl(&x3, &x3l);
  804. fe_add(&tmp0l, &tmp0, &z3);
  805. fe_mul_ttt(&z3, &x1, &z2);
  806. fe_mul_tll(&z2, &tmp1l, &tmp0l);
  807. }
  808. /* here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3)
  809. * else (x2, z2)
  810. */
  811. fe_cswap(&x2, &x3, swap);
  812. fe_cswap(&z2, &z3, swap);
  813. fe_invert(&z2, &z2);
  814. fe_mul_ttt(&x2, &x2, &z2);
  815. fe_tobytes(out, &x2);
  816. memzero_explicit(&x1, sizeof(x1));
  817. memzero_explicit(&x2, sizeof(x2));
  818. memzero_explicit(&z2, sizeof(z2));
  819. memzero_explicit(&x3, sizeof(x3));
  820. memzero_explicit(&z3, sizeof(z3));
  821. memzero_explicit(&x2l, sizeof(x2l));
  822. memzero_explicit(&z2l, sizeof(z2l));
  823. memzero_explicit(&x3l, sizeof(x3l));
  824. memzero_explicit(&e, sizeof(e));
  825. }