x25519.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. #include <string.h> // memcpy
  2. #include <stdint.h>
  3. typedef unsigned __int128 uint128_t;
  4. typedef uint64_t fe25519[5];
  5. typedef struct {
  6. fe25519 X;
  7. fe25519 Y;
  8. fe25519 Z;
  9. } ge25519_p2;
  10. typedef struct {
  11. fe25519 X;
  12. fe25519 Y;
  13. fe25519 Z;
  14. fe25519 T;
  15. } ge25519_p3;
  16. typedef struct {
  17. fe25519 X;
  18. fe25519 Y;
  19. fe25519 Z;
  20. fe25519 T;
  21. } ge25519_p1p1;
  22. typedef struct {
  23. fe25519 yplusx;
  24. fe25519 yminusx;
  25. fe25519 xy2d;
  26. } ge25519_precomp;
  27. /*
  28. h = 0
  29. */
  30. static inline void
  31. fe25519_0(fe25519 h)
  32. {
  33. memset(&h[0], 0, 5 * sizeof h[0]);
  34. }
  35. /*
  36. h = 1
  37. */
  38. static inline void
  39. fe25519_1(fe25519 h)
  40. {
  41. h[0] = 1;
  42. memset(&h[1], 0, 4 * sizeof h[0]);
  43. }
  44. /*
  45. h = f + g
  46. Can overlap h with f or g.
  47. */
  48. static inline void
  49. fe25519_add(fe25519 h, const fe25519 f, const fe25519 g)
  50. {
  51. uint64_t h0 = f[0] + g[0];
  52. uint64_t h1 = f[1] + g[1];
  53. uint64_t h2 = f[2] + g[2];
  54. uint64_t h3 = f[3] + g[3];
  55. uint64_t h4 = f[4] + g[4];
  56. h[0] = h0;
  57. h[1] = h1;
  58. h[2] = h2;
  59. h[3] = h3;
  60. h[4] = h4;
  61. }
  62. /*
  63. h = f - g
  64. */
  65. static void
  66. fe25519_sub(fe25519 h, const fe25519 f, const fe25519 g)
  67. {
  68. const uint64_t mask = 0x7ffffffffffffULL;
  69. uint64_t h0, h1, h2, h3, h4;
  70. h0 = g[0];
  71. h1 = g[1];
  72. h2 = g[2];
  73. h3 = g[3];
  74. h4 = g[4];
  75. h1 += h0 >> 51;
  76. h0 &= mask;
  77. h2 += h1 >> 51;
  78. h1 &= mask;
  79. h3 += h2 >> 51;
  80. h2 &= mask;
  81. h4 += h3 >> 51;
  82. h3 &= mask;
  83. h0 += 19ULL * (h4 >> 51);
  84. h4 &= mask;
  85. h0 = (f[0] + 0xfffffffffffdaULL) - h0;
  86. h1 = (f[1] + 0xffffffffffffeULL) - h1;
  87. h2 = (f[2] + 0xffffffffffffeULL) - h2;
  88. h3 = (f[3] + 0xffffffffffffeULL) - h3;
  89. h4 = (f[4] + 0xffffffffffffeULL) - h4;
  90. h[0] = h0;
  91. h[1] = h1;
  92. h[2] = h2;
  93. h[3] = h3;
  94. h[4] = h4;
  95. }
  96. /*
  97. h = -f
  98. */
  99. static inline void
  100. fe25519_neg(fe25519 h, const fe25519 f)
  101. {
  102. fe25519 zero;
  103. fe25519_0(zero);
  104. fe25519_sub(h, zero, f);
  105. }
  106. /*
  107. h = f * g
  108. Can overlap h with f or g.
  109. */
  110. extern "C" void x25519_fe51_mul(fe25519 h, const fe25519 f, const fe25519 g);
  111. #define fe25519_mul x25519_fe51_mul
  112. /*
  113. static void
  114. fe25519_mul(fe25519 h, const fe25519 f, const fe25519 g)
  115. {
  116. const uint64_t mask = 0x7ffffffffffffULL;
  117. uint128_t r0, r1, r2, r3, r4, carry;
  118. uint64_t f0, f1, f2, f3, f4;
  119. uint64_t f1_19, f2_19, f3_19, f4_19;
  120. uint64_t g0, g1, g2, g3, g4;
  121. uint64_t r00, r01, r02, r03, r04;
  122. f0 = f[0];
  123. f1 = f[1];
  124. f2 = f[2];
  125. f3 = f[3];
  126. f4 = f[4];
  127. g0 = g[0];
  128. g1 = g[1];
  129. g2 = g[2];
  130. g3 = g[3];
  131. g4 = g[4];
  132. f1_19 = 19ULL * f1;
  133. f2_19 = 19ULL * f2;
  134. f3_19 = 19ULL * f3;
  135. f4_19 = 19ULL * f4;
  136. r0 = ((uint128_t)f0) * ((uint128_t)g0);
  137. r0 += ((uint128_t)f1_19) * ((uint128_t)g4);
  138. r0 += ((uint128_t)f2_19) * ((uint128_t)g3);
  139. r0 += ((uint128_t)f3_19) * ((uint128_t)g2);
  140. r0 += ((uint128_t)f4_19) * ((uint128_t)g1);
  141. r1 = ((uint128_t)f0) * ((uint128_t)g1);
  142. r1 += ((uint128_t)f1) * ((uint128_t)g0);
  143. r1 += ((uint128_t)f2_19) * ((uint128_t)g4);
  144. r1 += ((uint128_t)f3_19) * ((uint128_t)g3);
  145. r1 += ((uint128_t)f4_19) * ((uint128_t)g2);
  146. r2 = ((uint128_t)f0) * ((uint128_t)g2);
  147. r2 += ((uint128_t)f1) * ((uint128_t)g1);
  148. r2 += ((uint128_t)f2) * ((uint128_t)g0);
  149. r2 += ((uint128_t)f3_19) * ((uint128_t)g4);
  150. r2 += ((uint128_t)f4_19) * ((uint128_t)g3);
  151. r3 = ((uint128_t)f0) * ((uint128_t)g3);
  152. r3 += ((uint128_t)f1) * ((uint128_t)g2);
  153. r3 += ((uint128_t)f2) * ((uint128_t)g1);
  154. r3 += ((uint128_t)f3) * ((uint128_t)g0);
  155. r3 += ((uint128_t)f4_19) * ((uint128_t)g4);
  156. r4 = ((uint128_t)f0) * ((uint128_t)g4);
  157. r4 += ((uint128_t)f1) * ((uint128_t)g3);
  158. r4 += ((uint128_t)f2) * ((uint128_t)g2);
  159. r4 += ((uint128_t)f3) * ((uint128_t)g1);
  160. r4 += ((uint128_t)f4) * ((uint128_t)g0);
  161. r00 = ((uint64_t)r0) & mask;
  162. carry = r0 >> 51;
  163. r1 += carry;
  164. r01 = ((uint64_t)r1) & mask;
  165. carry = r1 >> 51;
  166. r2 += carry;
  167. r02 = ((uint64_t)r2) & mask;
  168. carry = r2 >> 51;
  169. r3 += carry;
  170. r03 = ((uint64_t)r3) & mask;
  171. carry = r3 >> 51;
  172. r4 += carry;
  173. r04 = ((uint64_t)r4) & mask;
  174. carry = r4 >> 51;
  175. r00 += 19ULL * (uint64_t)carry;
  176. carry = r00 >> 51;
  177. r00 &= mask;
  178. r01 += (uint64_t)carry;
  179. carry = r01 >> 51;
  180. r01 &= mask;
  181. r02 += (uint64_t)carry;
  182. h[0] = r00;
  183. h[1] = r01;
  184. h[2] = r02;
  185. h[3] = r03;
  186. h[4] = r04;
  187. }
  188. */
  189. /*
  190. h = f * f
  191. Can overlap h with f.
  192. */
  193. extern "C" void x25519_fe51_sqr(fe25519 h, const fe25519 f);
  194. #define fe25519_sq x25519_fe51_sqr
  195. /*
  196. static void
  197. fe25519_sq(fe25519 h, const fe25519 f)
  198. {
  199. const uint64_t mask = 0x7ffffffffffffULL;
  200. uint128_t r0, r1, r2, r3, r4, carry;
  201. uint64_t f0, f1, f2, f3, f4;
  202. uint64_t f0_2, f1_2, f1_38, f2_38, f3_38, f3_19, f4_19;
  203. uint64_t r00, r01, r02, r03, r04;
  204. f0 = f[0];
  205. f1 = f[1];
  206. f2 = f[2];
  207. f3 = f[3];
  208. f4 = f[4];
  209. f0_2 = f0 << 1;
  210. f1_2 = f1 << 1;
  211. f1_38 = 38ULL * f1;
  212. f2_38 = 38ULL * f2;
  213. f3_38 = 38ULL * f3;
  214. f3_19 = 19ULL * f3;
  215. f4_19 = 19ULL * f4;
  216. r0 = ((uint128_t)f0) * ((uint128_t)f0);
  217. r0 += ((uint128_t)f1_38) * ((uint128_t)f4);
  218. r0 += ((uint128_t)f2_38) * ((uint128_t)f3);
  219. r1 = ((uint128_t)f0_2) * ((uint128_t)f1);
  220. r1 += ((uint128_t)f2_38) * ((uint128_t)f4);
  221. r1 += ((uint128_t)f3_19) * ((uint128_t)f3);
  222. r2 = ((uint128_t)f0_2) * ((uint128_t)f2);
  223. r2 += ((uint128_t)f1) * ((uint128_t)f1);
  224. r2 += ((uint128_t)f3_38) * ((uint128_t)f4);
  225. r3 = ((uint128_t)f0_2) * ((uint128_t)f3);
  226. r3 += ((uint128_t)f1_2) * ((uint128_t)f2);
  227. r3 += ((uint128_t)f4_19) * ((uint128_t)f4);
  228. r4 = ((uint128_t)f0_2) * ((uint128_t)f4);
  229. r4 += ((uint128_t)f1_2) * ((uint128_t)f3);
  230. r4 += ((uint128_t)f2) * ((uint128_t)f2);
  231. r00 = ((uint64_t)r0) & mask;
  232. carry = r0 >> 51;
  233. r1 += carry;
  234. r01 = ((uint64_t)r1) & mask;
  235. carry = r1 >> 51;
  236. r2 += carry;
  237. r02 = ((uint64_t)r2) & mask;
  238. carry = r2 >> 51;
  239. r3 += carry;
  240. r03 = ((uint64_t)r3) & mask;
  241. carry = r3 >> 51;
  242. r4 += carry;
  243. r04 = ((uint64_t)r4) & mask;
  244. carry = r4 >> 51;
  245. r00 += 19ULL * (uint64_t)carry;
  246. carry = r00 >> 51;
  247. r00 &= mask;
  248. r01 += (uint64_t)carry;
  249. carry = r01 >> 51;
  250. r01 &= mask;
  251. r02 += (uint64_t)carry;
  252. h[0] = r00;
  253. h[1] = r01;
  254. h[2] = r02;
  255. h[3] = r03;
  256. h[4] = r04;
  257. }
  258. */
  259. static void
  260. fe25519_invert(fe25519 out, const fe25519 z)
  261. {
  262. fe25519 t0;
  263. fe25519 t1;
  264. fe25519 t2;
  265. fe25519 t3;
  266. int i;
  267. fe25519_sq(t0, z);
  268. fe25519_sq(t1, t0);
  269. fe25519_sq(t1, t1);
  270. fe25519_mul(t1, z, t1);
  271. fe25519_mul(t0, t0, t1);
  272. fe25519_sq(t2, t0);
  273. fe25519_mul(t1, t1, t2);
  274. fe25519_sq(t2, t1);
  275. for (i = 1; i < 5; ++i) {
  276. fe25519_sq(t2, t2);
  277. }
  278. fe25519_mul(t1, t2, t1);
  279. fe25519_sq(t2, t1);
  280. for (i = 1; i < 10; ++i) {
  281. fe25519_sq(t2, t2);
  282. }
  283. fe25519_mul(t2, t2, t1);
  284. fe25519_sq(t3, t2);
  285. for (i = 1; i < 20; ++i) {
  286. fe25519_sq(t3, t3);
  287. }
  288. fe25519_mul(t2, t3, t2);
  289. for (i = 1; i < 11; ++i) {
  290. fe25519_sq(t2, t2);
  291. }
  292. fe25519_mul(t1, t2, t1);
  293. fe25519_sq(t2, t1);
  294. for (i = 1; i < 50; ++i) {
  295. fe25519_sq(t2, t2);
  296. }
  297. fe25519_mul(t2, t2, t1);
  298. fe25519_sq(t3, t2);
  299. for (i = 1; i < 100; ++i) {
  300. fe25519_sq(t3, t3);
  301. }
  302. fe25519_mul(t2, t3, t2);
  303. for (i = 1; i < 51; ++i) {
  304. fe25519_sq(t2, t2);
  305. }
  306. fe25519_mul(t1, t2, t1);
  307. for (i = 1; i < 6; ++i) {
  308. fe25519_sq(t1, t1);
  309. }
  310. fe25519_mul(out, t1, t0);
  311. }
  312. static void
  313. fe25519_reduce(fe25519 h, const fe25519 f)
  314. {
  315. const uint64_t mask = 0x7ffffffffffffULL;
  316. uint128_t t[5];
  317. t[0] = f[0];
  318. t[1] = f[1];
  319. t[2] = f[2];
  320. t[3] = f[3];
  321. t[4] = f[4];
  322. t[1] += t[0] >> 51;
  323. t[0] &= mask;
  324. t[2] += t[1] >> 51;
  325. t[1] &= mask;
  326. t[3] += t[2] >> 51;
  327. t[2] &= mask;
  328. t[4] += t[3] >> 51;
  329. t[3] &= mask;
  330. t[0] += 19 * (t[4] >> 51);
  331. t[4] &= mask;
  332. t[1] += t[0] >> 51;
  333. t[0] &= mask;
  334. t[2] += t[1] >> 51;
  335. t[1] &= mask;
  336. t[3] += t[2] >> 51;
  337. t[2] &= mask;
  338. t[4] += t[3] >> 51;
  339. t[3] &= mask;
  340. t[0] += 19 * (t[4] >> 51);
  341. t[4] &= mask;
  342. /* now t is between 0 and 2^255-1, properly carried. */
  343. /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
  344. t[0] += 19ULL;
  345. t[1] += t[0] >> 51;
  346. t[0] &= mask;
  347. t[2] += t[1] >> 51;
  348. t[1] &= mask;
  349. t[3] += t[2] >> 51;
  350. t[2] &= mask;
  351. t[4] += t[3] >> 51;
  352. t[3] &= mask;
  353. t[0] += 19ULL * (t[4] >> 51);
  354. t[4] &= mask;
  355. /* now between 19 and 2^255-1 in both cases, and offset by 19. */
  356. t[0] += 0x8000000000000 - 19ULL;
  357. t[1] += 0x8000000000000 - 1ULL;
  358. t[2] += 0x8000000000000 - 1ULL;
  359. t[3] += 0x8000000000000 - 1ULL;
  360. t[4] += 0x8000000000000 - 1ULL;
  361. /* now between 2^255 and 2^256-20, and offset by 2^255. */
  362. t[1] += t[0] >> 51;
  363. t[0] &= mask;
  364. t[2] += t[1] >> 51;
  365. t[1] &= mask;
  366. t[3] += t[2] >> 51;
  367. t[2] &= mask;
  368. t[4] += t[3] >> 51;
  369. t[3] &= mask;
  370. t[4] &= mask;
  371. h[0] = t[0];
  372. h[1] = t[1];
  373. h[2] = t[2];
  374. h[3] = t[3];
  375. h[4] = t[4];
  376. }
  377. /*
  378. h = 2 * f * f
  379. Can overlap h with f.
  380. */
  381. static void
  382. fe25519_sq2(fe25519 h, const fe25519 f)
  383. {
  384. const uint64_t mask = 0x7ffffffffffffULL;
  385. uint128_t r0, r1, r2, r3, r4, carry;
  386. uint64_t f0, f1, f2, f3, f4;
  387. uint64_t f0_2, f1_2, f1_38, f2_38, f3_38, f3_19, f4_19;
  388. uint64_t r00, r01, r02, r03, r04;
  389. f0 = f[0];
  390. f1 = f[1];
  391. f2 = f[2];
  392. f3 = f[3];
  393. f4 = f[4];
  394. f0_2 = f0 << 1;
  395. f1_2 = f1 << 1;
  396. f1_38 = 38ULL * f1;
  397. f2_38 = 38ULL * f2;
  398. f3_38 = 38ULL * f3;
  399. f3_19 = 19ULL * f3;
  400. f4_19 = 19ULL * f4;
  401. r0 = ((uint128_t)f0) * ((uint128_t)f0);
  402. r0 += ((uint128_t)f1_38) * ((uint128_t)f4);
  403. r0 += ((uint128_t)f2_38) * ((uint128_t)f3);
  404. r1 = ((uint128_t)f0_2) * ((uint128_t)f1);
  405. r1 += ((uint128_t)f2_38) * ((uint128_t)f4);
  406. r1 += ((uint128_t)f3_19) * ((uint128_t)f3);
  407. r2 = ((uint128_t)f0_2) * ((uint128_t)f2);
  408. r2 += ((uint128_t)f1) * ((uint128_t)f1);
  409. r2 += ((uint128_t)f3_38) * ((uint128_t)f4);
  410. r3 = ((uint128_t)f0_2) * ((uint128_t)f3);
  411. r3 += ((uint128_t)f1_2) * ((uint128_t)f2);
  412. r3 += ((uint128_t)f4_19) * ((uint128_t)f4);
  413. r4 = ((uint128_t)f0_2) * ((uint128_t)f4);
  414. r4 += ((uint128_t)f1_2) * ((uint128_t)f3);
  415. r4 += ((uint128_t)f2) * ((uint128_t)f2);
  416. r0 <<= 1;
  417. r1 <<= 1;
  418. r2 <<= 1;
  419. r3 <<= 1;
  420. r4 <<= 1;
  421. r00 = ((uint64_t)r0) & mask;
  422. carry = r0 >> 51;
  423. r1 += carry;
  424. r01 = ((uint64_t)r1) & mask;
  425. carry = r1 >> 51;
  426. r2 += carry;
  427. r02 = ((uint64_t)r2) & mask;
  428. carry = r2 >> 51;
  429. r3 += carry;
  430. r03 = ((uint64_t)r3) & mask;
  431. carry = r3 >> 51;
  432. r4 += carry;
  433. r04 = ((uint64_t)r4) & mask;
  434. carry = r4 >> 51;
  435. r00 += 19ULL * (uint64_t)carry;
  436. carry = r00 >> 51;
  437. r00 &= mask;
  438. r01 += (uint64_t)carry;
  439. carry = r01 >> 51;
  440. r01 &= mask;
  441. r02 += (uint64_t)carry;
  442. h[0] = r00;
  443. h[1] = r01;
  444. h[2] = r02;
  445. h[3] = r03;
  446. h[4] = r04;
  447. }
  448. /*
  449. h = f
  450. */
  451. static inline void
  452. fe25519_copy(fe25519 h, const fe25519 f)
  453. {
  454. uint64_t f0 = f[0];
  455. uint64_t f1 = f[1];
  456. uint64_t f2 = f[2];
  457. uint64_t f3 = f[3];
  458. uint64_t f4 = f[4];
  459. h[0] = f0;
  460. h[1] = f1;
  461. h[2] = f2;
  462. h[3] = f3;
  463. h[4] = f4;
  464. }
  465. static void
  466. fe25519_tobytes(unsigned char *s, const fe25519 h)
  467. {
  468. fe25519 t;
  469. uint64_t t0, t1, t2, t3;
  470. fe25519_reduce(t, h);
  471. t0 = t[0] | (t[1] << 51);
  472. t1 = (t[1] >> 13) | (t[2] << 38);
  473. t2 = (t[2] >> 26) | (t[3] << 25);
  474. t3 = (t[3] >> 39) | (t[4] << 12);
  475. memcpy(s + 0, &t0, sizeof t0);
  476. memcpy(s + 8, &t1, sizeof t1);
  477. memcpy(s + 16, &t2, sizeof t2);
  478. memcpy(s + 24, &t3, sizeof t3);
  479. }
  480. static void
  481. ge25519_p3_0(ge25519_p3 *h)
  482. {
  483. fe25519_0(h->X);
  484. fe25519_1(h->Y);
  485. fe25519_1(h->Z);
  486. fe25519_0(h->T);
  487. }
  488. /*
  489. Replace (t,u) with (u,u) if b == 1;
  490. replace (t,u) with (t,u) if b == 0.
  491. *
  492. Preconditions: b in {0,1}.
  493. */
  494. static void
  495. ge25519_cmov(ge25519_precomp *t, const ge25519_precomp *u, unsigned char b)
  496. {
  497. if (b)
  498. memcpy(t, u, sizeof *t);
  499. }
  500. static void
  501. ge25519_copy(ge25519_precomp *t, const ge25519_precomp *u)
  502. {
  503. memcpy(t, u, sizeof *t);
  504. }
  505. /*
  506. r = p
  507. */
  508. static void
  509. ge25519_p3_to_p2(ge25519_p2 *r, const ge25519_p3 *p)
  510. {
  511. fe25519_copy(r->X, p->X);
  512. fe25519_copy(r->Y, p->Y);
  513. fe25519_copy(r->Z, p->Z);
  514. }
  515. /*
  516. r = 2 * p
  517. */
  518. static void
  519. ge25519_p2_dbl(ge25519_p1p1 *r, const ge25519_p2 *p)
  520. {
  521. fe25519 t0;
  522. fe25519_sq(r->X, p->X);
  523. fe25519_sq(r->Z, p->Y);
  524. fe25519_sq2(r->T, p->Z);
  525. fe25519_add(r->Y, p->X, p->Y);
  526. fe25519_sq(t0, r->Y);
  527. fe25519_add(r->Y, r->Z, r->X);
  528. fe25519_sub(r->Z, r->Z, r->X);
  529. fe25519_sub(r->X, t0, r->Y);
  530. fe25519_sub(r->T, r->T, r->Z);
  531. }
  532. /*
  533. r = 2 * p
  534. */
  535. static void
  536. ge25519_p3_dbl(ge25519_p1p1 *r, const ge25519_p3 *p)
  537. {
  538. ge25519_p2 q;
  539. ge25519_p3_to_p2(&q, p);
  540. ge25519_p2_dbl(r, &q);
  541. }
  542. static void
  543. ge25519_precomp_0(ge25519_precomp *h)
  544. {
  545. fe25519_1(h->yplusx);
  546. fe25519_1(h->yminusx);
  547. fe25519_0(h->xy2d);
  548. }
  549. static void
  550. ge25519_cmov8_base(ge25519_precomp *t, const int pos, const signed char b)
  551. {
  552. static const ge25519_precomp base[32][8] = { /* base[i][j] = (j+1)*256^i*B */
  553. #include "fe_51_base.h"
  554. };
  555. ge25519_precomp_0(t);
  556. const unsigned char babs = b < 0 ? -b : b;
  557. if (babs == 0)
  558. return;
  559. ge25519_copy(t, &base[pos][babs - 1]);
  560. if (b < 0)
  561. {
  562. ge25519_precomp minust;
  563. fe25519_copy(minust.yplusx, t->yminusx);
  564. fe25519_copy(minust.yminusx, t->yplusx);
  565. fe25519_neg(minust.xy2d, t->xy2d);
  566. ge25519_copy(t, &minust);
  567. }
  568. }
  569. /*
  570. r = p + q
  571. */
  572. static void
  573. ge25519_madd(ge25519_p1p1 *r, const ge25519_p3 *p, const ge25519_precomp *q)
  574. {
  575. fe25519 t0;
  576. fe25519_add(r->X, p->Y, p->X);
  577. fe25519_sub(r->Y, p->Y, p->X);
  578. fe25519_mul(r->Z, r->X, q->yplusx);
  579. fe25519_mul(r->Y, r->Y, q->yminusx);
  580. fe25519_mul(r->T, q->xy2d, p->T);
  581. fe25519_add(t0, p->Z, p->Z);
  582. fe25519_sub(r->X, r->Z, r->Y);
  583. fe25519_add(r->Y, r->Z, r->Y);
  584. fe25519_add(r->Z, t0, r->T);
  585. fe25519_sub(r->T, t0, r->T);
  586. }
  587. /*
  588. r = p
  589. */
  590. static void
  591. ge25519_p1p1_to_p2(ge25519_p2 *r, const ge25519_p1p1 *p)
  592. {
  593. fe25519_mul(r->X, p->X, p->T);
  594. fe25519_mul(r->Y, p->Y, p->Z);
  595. fe25519_mul(r->Z, p->Z, p->T);
  596. }
  597. /*
  598. r = p
  599. */
  600. static void
  601. ge25519_p1p1_to_p3(ge25519_p3 *r, const ge25519_p1p1 *p)
  602. {
  603. fe25519_mul(r->X, p->X, p->T);
  604. fe25519_mul(r->Y, p->Y, p->Z);
  605. fe25519_mul(r->Z, p->Z, p->T);
  606. fe25519_mul(r->T, p->X, p->Y);
  607. }
  608. /*
  609. h = a * B (with precomputation)
  610. where a = a[0]+256*a[1]+...+256^31 a[31]
  611. B is the Ed25519 base point (x,4/5) with x positive
  612. (as bytes: 0x5866666666666666666666666666666666666666666666666666666666666666)
  613. Preconditions:
  614. a[31] <= 127
  615. */
  616. static void
  617. ge25519_scalarmult_base(ge25519_p3 *h, const unsigned char *a)
  618. {
  619. signed char e[64];
  620. signed char carry;
  621. ge25519_p1p1 r;
  622. ge25519_p2 s;
  623. ge25519_precomp t;
  624. int i;
  625. for (i = 0; i < 32; ++i) {
  626. e[2 * i + 0] = (a[i] >> 0) & 15;
  627. e[2 * i + 1] = (a[i] >> 4) & 15;
  628. }
  629. /* each e[i] is between 0 and 15 */
  630. /* e[63] is between 0 and 7 */
  631. carry = 0;
  632. for (i = 0; i < 63; ++i) {
  633. e[i] += carry;
  634. carry = e[i] + 8;
  635. carry >>= 4;
  636. e[i] -= carry * ((signed char)1 << 4);
  637. }
  638. e[63] += carry;
  639. /* each e[i] is between -8 and 8 */
  640. ge25519_p3_0(h);
  641. for (i = 1; i < 64; i += 2) {
  642. ge25519_cmov8_base(&t, i / 2, e[i]);
  643. ge25519_madd(&r, h, &t);
  644. ge25519_p1p1_to_p3(h, &r);
  645. }
  646. ge25519_p3_dbl(&r, h);
  647. ge25519_p1p1_to_p2(&s, &r);
  648. ge25519_p2_dbl(&r, &s);
  649. ge25519_p1p1_to_p2(&s, &r);
  650. ge25519_p2_dbl(&r, &s);
  651. ge25519_p1p1_to_p2(&s, &r);
  652. ge25519_p2_dbl(&r, &s);
  653. ge25519_p1p1_to_p3(h, &r);
  654. for (i = 0; i < 64; i += 2) {
  655. ge25519_cmov8_base(&t, i / 2, e[i]);
  656. ge25519_madd(&r, h, &t);
  657. ge25519_p1p1_to_p3(h, &r);
  658. }
  659. }
  660. static void
  661. edwards_to_montgomery(fe25519 montgomeryX, const fe25519 edwardsY, const fe25519 edwardsZ)
  662. {
  663. fe25519 tempX;
  664. fe25519 tempZ;
  665. fe25519_add(tempX, edwardsZ, edwardsY);
  666. fe25519_sub(tempZ, edwardsZ, edwardsY);
  667. fe25519_invert(tempZ, tempZ);
  668. fe25519_mul(montgomeryX, tempX, tempZ);
  669. }
  670. int
  671. crypto_scalarmult_curve25519_base_internal(
  672. unsigned char *q, const unsigned char *n)
  673. {
  674. unsigned char *t = q;
  675. ge25519_p3 A;
  676. fe25519 pk;
  677. unsigned int i;
  678. for (i = 0; i < 32; i++) {
  679. t[i] = n[i];
  680. }
  681. t[0] &= 248;
  682. t[31] &= 127;
  683. t[31] |= 64;
  684. ge25519_scalarmult_base(&A, t);
  685. edwards_to_montgomery(pk, A.Y, A.Z);
  686. fe25519_tobytes(q, pk);
  687. return 0;
  688. }