1
0

ecc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. #include <assert.h>
  2. #include "ssh.h"
  3. #include "mpint.h"
  4. #include "ecc.h"
  5. /* ----------------------------------------------------------------------
  6. * Weierstrass curves.
  7. */
  8. struct WeierstrassPoint {
  9. /*
  10. * Internally, we represent a point using 'Jacobian coordinates',
  11. * which are three values X,Y,Z whose relation to the affine
  12. * coordinates x,y is that x = X/Z^2 and y = Y/Z^3.
  13. *
  14. * This allows us to do most of our calculations without having to
  15. * take an inverse mod p: every time the obvious affine formulae
  16. * would need you to divide by something, you instead multiply it
  17. * into the 'denominator' coordinate Z. You only have to actually
  18. * take the inverse of Z when you need to get the affine
  19. * coordinates back out, which means you do it once after your
  20. * entire computation instead of at every intermediate step.
  21. *
  22. * The point at infinity is represented by setting all three
  23. * coordinates to zero.
  24. *
  25. * These values are also stored in the Montgomery-multiplication
  26. * transformed representation.
  27. */
  28. mp_int *X, *Y, *Z;
  29. WeierstrassCurve *wc;
  30. };
  31. struct WeierstrassCurve {
  32. /* Prime modulus of the finite field. */
  33. mp_int *p;
  34. /* Persistent Montgomery context for doing arithmetic mod p. */
  35. MontyContext *mc;
  36. /* Modsqrt context for point decompression. NULL if this curve was
  37. * constructed without providing nonsquare_mod_p. */
  38. ModsqrtContext *sc;
  39. /* Parameters of the curve, in Montgomery-multiplication
  40. * transformed form. */
  41. mp_int *a, *b;
  42. };
  43. WeierstrassCurve *ecc_weierstrass_curve(
  44. mp_int *p, mp_int *a, mp_int *b, mp_int *nonsquare_mod_p)
  45. {
  46. WeierstrassCurve *wc = snew(WeierstrassCurve);
  47. wc->p = mp_copy(p);
  48. wc->mc = monty_new(p);
  49. wc->a = monty_import(wc->mc, a);
  50. wc->b = monty_import(wc->mc, b);
  51. if (nonsquare_mod_p)
  52. wc->sc = modsqrt_new(p, nonsquare_mod_p);
  53. else
  54. wc->sc = NULL;
  55. return wc;
  56. }
  57. void ecc_weierstrass_curve_free(WeierstrassCurve *wc)
  58. {
  59. mp_free(wc->p);
  60. mp_free(wc->a);
  61. mp_free(wc->b);
  62. monty_free(wc->mc);
  63. if (wc->sc)
  64. modsqrt_free(wc->sc);
  65. sfree(wc);
  66. }
  67. static WeierstrassPoint *ecc_weierstrass_point_new_empty(WeierstrassCurve *wc)
  68. {
  69. WeierstrassPoint *wp = snew(WeierstrassPoint);
  70. wp->wc = wc;
  71. wp->X = wp->Y = wp->Z = NULL;
  72. return wp;
  73. }
  74. static WeierstrassPoint *ecc_weierstrass_point_new_imported(
  75. WeierstrassCurve *wc, mp_int *monty_x, mp_int *monty_y)
  76. {
  77. WeierstrassPoint *wp = ecc_weierstrass_point_new_empty(wc);
  78. wp->X = monty_x;
  79. wp->Y = monty_y;
  80. wp->Z = mp_copy(monty_identity(wc->mc));
  81. return wp;
  82. }
  83. WeierstrassPoint *ecc_weierstrass_point_new(
  84. WeierstrassCurve *wc, mp_int *x, mp_int *y)
  85. {
  86. return ecc_weierstrass_point_new_imported(
  87. wc, monty_import(wc->mc, x), monty_import(wc->mc, y));
  88. }
  89. WeierstrassPoint *ecc_weierstrass_point_new_identity(WeierstrassCurve *wc)
  90. {
  91. WeierstrassPoint *wp = ecc_weierstrass_point_new_empty(wc);
  92. size_t bits = mp_max_bits(wc->p);
  93. wp->X = mp_new(bits);
  94. wp->Y = mp_new(bits);
  95. wp->Z = mp_new(bits);
  96. return wp;
  97. }
  98. void ecc_weierstrass_point_copy_into(
  99. WeierstrassPoint *dest, WeierstrassPoint *src)
  100. {
  101. mp_copy_into(dest->X, src->X);
  102. mp_copy_into(dest->Y, src->Y);
  103. mp_copy_into(dest->Z, src->Z);
  104. }
  105. WeierstrassPoint *ecc_weierstrass_point_copy(WeierstrassPoint *orig)
  106. {
  107. WeierstrassPoint *wp = ecc_weierstrass_point_new_empty(orig->wc);
  108. wp->X = mp_copy(orig->X);
  109. wp->Y = mp_copy(orig->Y);
  110. wp->Z = mp_copy(orig->Z);
  111. return wp;
  112. }
  113. void ecc_weierstrass_point_free(WeierstrassPoint *wp)
  114. {
  115. mp_free(wp->X);
  116. mp_free(wp->Y);
  117. mp_free(wp->Z);
  118. smemclr(wp, sizeof(*wp));
  119. sfree(wp);
  120. }
  121. WeierstrassPoint *ecc_weierstrass_point_new_from_x(
  122. WeierstrassCurve *wc, mp_int *xorig, unsigned desired_y_parity)
  123. {
  124. pinitassert(wc->sc);
  125. /*
  126. * The curve equation is y^2 = x^3 + ax + b, which is already
  127. * conveniently in a form where we can compute the RHS and take
  128. * the square root of it to get y.
  129. */
  130. unsigned success;
  131. mp_int *x = monty_import(wc->mc, xorig);
  132. /*
  133. * Compute the RHS of the curve equation. We don't need to take
  134. * account of z here, because we're constructing the point from
  135. * scratch. So it really is just x^3 + ax + b.
  136. */
  137. mp_int *x2 = monty_mul(wc->mc, x, x);
  138. mp_int *x2_plus_a = monty_add(wc->mc, x2, wc->a);
  139. mp_int *x3_plus_ax = monty_mul(wc->mc, x2_plus_a, x);
  140. mp_int *rhs = monty_add(wc->mc, x3_plus_ax, wc->b);
  141. mp_free(x2);
  142. mp_free(x2_plus_a);
  143. mp_free(x3_plus_ax);
  144. { // WINSCP
  145. mp_int *y = monty_modsqrt(wc->sc, rhs, &success);
  146. mp_free(rhs);
  147. if (!success) {
  148. /* Failure! x^3+ax+b worked out to be a number that has no
  149. * square root mod p. In this situation there's no point in
  150. * trying to be time-constant, since the protocol sequence is
  151. * going to diverge anyway when we complain to whoever gave us
  152. * this bogus value. */
  153. mp_free(x);
  154. mp_free(y);
  155. return NULL;
  156. }
  157. /*
  158. * Choose whichever of y and p-y has the specified parity (of its
  159. * lowest positive residue mod p).
  160. */
  161. { // WINSCP
  162. mp_int *tmp = monty_export(wc->mc, y);
  163. unsigned flip = (mp_get_bit(tmp, 0) ^ desired_y_parity) & 1;
  164. mp_sub_into(tmp, wc->p, y);
  165. mp_select_into(y, y, tmp, flip);
  166. mp_free(tmp);
  167. } // WINSCP
  168. return ecc_weierstrass_point_new_imported(wc, x, y);
  169. } // WINSCP
  170. }
  171. static void ecc_weierstrass_cond_overwrite(
  172. WeierstrassPoint *dest, WeierstrassPoint *src, unsigned overwrite)
  173. {
  174. mp_select_into(dest->X, dest->X, src->X, overwrite);
  175. mp_select_into(dest->Y, dest->Y, src->Y, overwrite);
  176. mp_select_into(dest->Z, dest->Z, src->Z, overwrite);
  177. }
  178. static void ecc_weierstrass_cond_swap(
  179. WeierstrassPoint *P, WeierstrassPoint *Q, unsigned swap)
  180. {
  181. mp_cond_swap(P->X, Q->X, swap);
  182. mp_cond_swap(P->Y, Q->Y, swap);
  183. mp_cond_swap(P->Z, Q->Z, swap);
  184. }
  185. /*
  186. * Shared code between all three of the basic arithmetic functions:
  187. * once we've determined the slope of the line that we're intersecting
  188. * the curve with, this takes care of finding the coordinates of the
  189. * third intersection point (given the two input x-coordinates and one
  190. * of the y-coords) and negating it to generate the output.
  191. */
  192. static inline void ecc_weierstrass_epilogue(
  193. mp_int *Px, mp_int *Qx, mp_int *Py, mp_int *common_Z,
  194. mp_int *lambda_n, mp_int *lambda_d, WeierstrassPoint *out)
  195. {
  196. WeierstrassCurve *wc = out->wc;
  197. /* Powers of the numerator and denominator of the slope lambda */
  198. mp_int *lambda_n2 = monty_mul(wc->mc, lambda_n, lambda_n);
  199. mp_int *lambda_d2 = monty_mul(wc->mc, lambda_d, lambda_d);
  200. mp_int *lambda_d3 = monty_mul(wc->mc, lambda_d, lambda_d2);
  201. /* Make the output x-coordinate */
  202. mp_int *xsum = monty_add(wc->mc, Px, Qx);
  203. mp_int *lambda_d2_xsum = monty_mul(wc->mc, lambda_d2, xsum);
  204. out->X = monty_sub(wc->mc, lambda_n2, lambda_d2_xsum);
  205. /* Make the output y-coordinate */
  206. { // WINSCP
  207. mp_int *lambda_d2_Px = monty_mul(wc->mc, lambda_d2, Px);
  208. mp_int *xdiff = monty_sub(wc->mc, lambda_d2_Px, out->X);
  209. mp_int *lambda_n_xdiff = monty_mul(wc->mc, lambda_n, xdiff);
  210. mp_int *lambda_d3_Py = monty_mul(wc->mc, lambda_d3, Py);
  211. out->Y = monty_sub(wc->mc, lambda_n_xdiff, lambda_d3_Py);
  212. /* Make the output z-coordinate */
  213. out->Z = monty_mul(wc->mc, common_Z, lambda_d);
  214. mp_free(lambda_n2);
  215. mp_free(lambda_d2);
  216. mp_free(lambda_d3);
  217. mp_free(xsum);
  218. mp_free(xdiff);
  219. mp_free(lambda_d2_xsum);
  220. mp_free(lambda_n_xdiff);
  221. mp_free(lambda_d2_Px);
  222. mp_free(lambda_d3_Py);
  223. } // WINSCP
  224. }
  225. /*
  226. * Shared code between add and add_general: put the two input points
  227. * over a common denominator, and determine the slope lambda of the
  228. * line through both of them. If the points have the same
  229. * x-coordinate, then the slope will be returned with a zero
  230. * denominator.
  231. */
  232. static inline void ecc_weierstrass_add_prologue(
  233. WeierstrassPoint *P, WeierstrassPoint *Q,
  234. mp_int **Px, mp_int **Py, mp_int **Qx, mp_int **denom,
  235. mp_int **lambda_n, mp_int **lambda_d)
  236. {
  237. WeierstrassCurve *wc = P->wc;
  238. /* Powers of the points' denominators */
  239. mp_int *Pz2 = monty_mul(wc->mc, P->Z, P->Z);
  240. mp_int *Pz3 = monty_mul(wc->mc, Pz2, P->Z);
  241. mp_int *Qz2 = monty_mul(wc->mc, Q->Z, Q->Z);
  242. mp_int *Qz3 = monty_mul(wc->mc, Qz2, Q->Z);
  243. /* Points' x,y coordinates scaled by the other one's denominator
  244. * (raised to the appropriate power) */
  245. *Px = monty_mul(wc->mc, P->X, Qz2);
  246. *Py = monty_mul(wc->mc, P->Y, Qz3);
  247. *Qx = monty_mul(wc->mc, Q->X, Pz2);
  248. { // WINSCP
  249. mp_int *Qy = monty_mul(wc->mc, Q->Y, Pz3);
  250. /* Common denominator */
  251. *denom = monty_mul(wc->mc, P->Z, Q->Z);
  252. /* Slope of the line through the two points, if P != Q */
  253. *lambda_n = monty_sub(wc->mc, Qy, *Py);
  254. *lambda_d = monty_sub(wc->mc, *Qx, *Px);
  255. mp_free(Pz2);
  256. mp_free(Pz3);
  257. mp_free(Qz2);
  258. mp_free(Qz3);
  259. mp_free(Qy);
  260. } // WINSCP
  261. }
  262. WeierstrassPoint *ecc_weierstrass_add(WeierstrassPoint *P, WeierstrassPoint *Q)
  263. {
  264. WeierstrassCurve *wc = P->wc;
  265. assert(Q->wc == wc);
  266. { // WINSCP
  267. WeierstrassPoint *S = ecc_weierstrass_point_new_empty(wc);
  268. mp_int *Px, *Py, *Qx, *denom, *lambda_n, *lambda_d;
  269. ecc_weierstrass_add_prologue(
  270. P, Q, &Px, &Py, &Qx, &denom, &lambda_n, &lambda_d);
  271. /* Never expect to have received two mutually inverse inputs, or
  272. * two identical ones (which would make this a doubling). In other
  273. * words, the two input x-coordinates (after putting over a common
  274. * denominator) should never have been equal. */
  275. assert(!mp_eq_integer(lambda_n, 0));
  276. /* Now go to the common epilogue code. */
  277. ecc_weierstrass_epilogue(Px, Qx, Py, denom, lambda_n, lambda_d, S);
  278. mp_free(Px);
  279. mp_free(Py);
  280. mp_free(Qx);
  281. mp_free(denom);
  282. mp_free(lambda_n);
  283. mp_free(lambda_d);
  284. return S;
  285. } // WINSCP
  286. }
  287. /*
  288. * Code to determine the slope of the line you need to intersect with
  289. * the curve in the case where you're adding a point to itself. In
  290. * this situation you can't just say "the line through both input
  291. * points" because that's under-determined; instead, you have to take
  292. * the _tangent_ to the curve at the given point, by differentiating
  293. * the curve equation y^2=x^3+ax+b to get 2y dy/dx = 3x^2+a.
  294. */
  295. static inline void ecc_weierstrass_tangent_slope(
  296. WeierstrassPoint *P, mp_int **lambda_n, mp_int **lambda_d)
  297. {
  298. WeierstrassCurve *wc = P->wc;
  299. mp_int *X2 = monty_mul(wc->mc, P->X, P->X);
  300. mp_int *twoX2 = monty_add(wc->mc, X2, X2);
  301. mp_int *threeX2 = monty_add(wc->mc, twoX2, X2);
  302. mp_int *Z2 = monty_mul(wc->mc, P->Z, P->Z);
  303. mp_int *Z4 = monty_mul(wc->mc, Z2, Z2);
  304. mp_int *aZ4 = monty_mul(wc->mc, wc->a, Z4);
  305. *lambda_n = monty_add(wc->mc, threeX2, aZ4);
  306. *lambda_d = monty_add(wc->mc, P->Y, P->Y);
  307. mp_free(X2);
  308. mp_free(twoX2);
  309. mp_free(threeX2);
  310. mp_free(Z2);
  311. mp_free(Z4);
  312. mp_free(aZ4);
  313. }
  314. WeierstrassPoint *ecc_weierstrass_double(WeierstrassPoint *P)
  315. {
  316. WeierstrassCurve *wc = P->wc;
  317. WeierstrassPoint *D = ecc_weierstrass_point_new_empty(wc);
  318. mp_int *lambda_n, *lambda_d;
  319. ecc_weierstrass_tangent_slope(P, &lambda_n, &lambda_d);
  320. ecc_weierstrass_epilogue(P->X, P->X, P->Y, P->Z, lambda_n, lambda_d, D);
  321. mp_free(lambda_n);
  322. mp_free(lambda_d);
  323. return D;
  324. }
  325. static inline void ecc_weierstrass_select_into(
  326. WeierstrassPoint *dest, WeierstrassPoint *P, WeierstrassPoint *Q,
  327. unsigned choose_Q)
  328. {
  329. mp_select_into(dest->X, P->X, Q->X, choose_Q);
  330. mp_select_into(dest->Y, P->Y, Q->Y, choose_Q);
  331. mp_select_into(dest->Z, P->Z, Q->Z, choose_Q);
  332. }
  333. WeierstrassPoint *ecc_weierstrass_add_general(
  334. WeierstrassPoint *P, WeierstrassPoint *Q)
  335. {
  336. WeierstrassCurve *wc = P->wc;
  337. assert(Q->wc == wc);
  338. { // WINSCP
  339. WeierstrassPoint *S = ecc_weierstrass_point_new_empty(wc);
  340. /* Parameters for the epilogue, and slope of the line if P != Q */
  341. mp_int *Px, *Py, *Qx, *denom, *lambda_n, *lambda_d;
  342. ecc_weierstrass_add_prologue(
  343. P, Q, &Px, &Py, &Qx, &denom, &lambda_n, &lambda_d);
  344. /* Slope if P == Q */
  345. { // WINSCP
  346. mp_int *lambda_n_tangent, *lambda_d_tangent;
  347. ecc_weierstrass_tangent_slope(P, &lambda_n_tangent, &lambda_d_tangent);
  348. /* Select between those slopes depending on whether P == Q */
  349. { // WINSCP
  350. unsigned same_x_coord = mp_eq_integer(lambda_d, 0);
  351. unsigned same_y_coord = mp_eq_integer(lambda_n, 0);
  352. unsigned equality = same_x_coord & same_y_coord;
  353. mp_select_into(lambda_n, lambda_n, lambda_n_tangent, equality);
  354. mp_select_into(lambda_d, lambda_d, lambda_d_tangent, equality);
  355. /* Now go to the common code between addition and doubling */
  356. ecc_weierstrass_epilogue(Px, Qx, Py, denom, lambda_n, lambda_d, S);
  357. /* Check for the input identity cases, and overwrite the output if
  358. * necessary. */
  359. ecc_weierstrass_select_into(S, S, Q, mp_eq_integer(P->Z, 0));
  360. ecc_weierstrass_select_into(S, S, P, mp_eq_integer(Q->Z, 0));
  361. /*
  362. * In the case where P == -Q and so the output is the identity,
  363. * we'll have calculated lambda_d = 0 and so the output will have
  364. * z==0 already. Detect that and use it to normalise the other two
  365. * coordinates to zero.
  366. */
  367. { // WINSCP
  368. unsigned output_id = mp_eq_integer(S->Z, 0);
  369. mp_cond_clear(S->X, output_id);
  370. mp_cond_clear(S->Y, output_id);
  371. mp_free(Px);
  372. mp_free(Py);
  373. mp_free(Qx);
  374. mp_free(denom);
  375. mp_free(lambda_n);
  376. mp_free(lambda_d);
  377. mp_free(lambda_n_tangent);
  378. mp_free(lambda_d_tangent);
  379. return S;
  380. } // WINSCP
  381. } // WINSCP
  382. } // WINSCP
  383. } // WINSCP
  384. }
  385. WeierstrassPoint *ecc_weierstrass_multiply(WeierstrassPoint *B, mp_int *n)
  386. {
  387. WeierstrassPoint *two_B = ecc_weierstrass_double(B);
  388. WeierstrassPoint *k_B = ecc_weierstrass_point_copy(B);
  389. WeierstrassPoint *kplus1_B = ecc_weierstrass_point_copy(two_B);
  390. /*
  391. * This multiply routine more or less follows the shape of the
  392. * 'Montgomery ladder' technique that you have to use under the
  393. * extra constraint on addition in Montgomery curves, because it
  394. * was fresh in my mind and easier to just do it the same way. See
  395. * the comment in ecc_montgomery_multiply.
  396. */
  397. unsigned not_started_yet = 1;
  398. size_t bitindex; // WINSCP
  399. for (bitindex = mp_max_bits(n); bitindex-- > 0 ;) {
  400. unsigned nbit = mp_get_bit(n, bitindex);
  401. WeierstrassPoint *sum = ecc_weierstrass_add(k_B, kplus1_B);
  402. ecc_weierstrass_cond_swap(k_B, kplus1_B, nbit);
  403. { // WINSCP
  404. WeierstrassPoint *other = ecc_weierstrass_double(k_B);
  405. ecc_weierstrass_point_free(k_B);
  406. ecc_weierstrass_point_free(kplus1_B);
  407. k_B = other;
  408. kplus1_B = sum;
  409. ecc_weierstrass_cond_swap(k_B, kplus1_B, nbit);
  410. ecc_weierstrass_cond_overwrite(k_B, B, not_started_yet);
  411. ecc_weierstrass_cond_overwrite(kplus1_B, two_B, not_started_yet);
  412. not_started_yet &= ~nbit;
  413. } // WINSCP
  414. }
  415. ecc_weierstrass_point_free(two_B);
  416. ecc_weierstrass_point_free(kplus1_B);
  417. return k_B;
  418. }
  419. unsigned ecc_weierstrass_is_identity(WeierstrassPoint *wp)
  420. {
  421. return mp_eq_integer(wp->Z, 0);
  422. }
  423. /*
  424. * Normalise a point by scaling its Jacobian coordinates so that Z=1.
  425. * This doesn't change what point is represented by the triple, but it
  426. * means the affine x,y can now be easily recovered from X and Y.
  427. */
  428. static void ecc_weierstrass_normalise(WeierstrassPoint *wp)
  429. {
  430. WeierstrassCurve *wc = wp->wc;
  431. mp_int *zinv = monty_invert(wc->mc, wp->Z);
  432. mp_int *zinv2 = monty_mul(wc->mc, zinv, zinv);
  433. mp_int *zinv3 = monty_mul(wc->mc, zinv2, zinv);
  434. monty_mul_into(wc->mc, wp->X, wp->X, zinv2);
  435. monty_mul_into(wc->mc, wp->Y, wp->Y, zinv3);
  436. mp_free(zinv);
  437. mp_free(zinv2);
  438. mp_free(zinv3);
  439. mp_copy_into(wp->Z, monty_identity(wc->mc));
  440. }
  441. void ecc_weierstrass_get_affine(
  442. WeierstrassPoint *wp, mp_int **x, mp_int **y)
  443. {
  444. WeierstrassCurve *wc = wp->wc;
  445. ecc_weierstrass_normalise(wp);
  446. if (x)
  447. *x = monty_export(wc->mc, wp->X);
  448. if (y)
  449. *y = monty_export(wc->mc, wp->Y);
  450. }
  451. unsigned ecc_weierstrass_point_valid(WeierstrassPoint *P)
  452. {
  453. WeierstrassCurve *wc = P->wc;
  454. /*
  455. * The projective version of the curve equation is
  456. * Y^2 = X^3 + a X Z^4 + b Z^6
  457. */
  458. mp_int *lhs = monty_mul(P->wc->mc, P->Y, P->Y);
  459. mp_int *x2 = monty_mul(wc->mc, P->X, P->X);
  460. mp_int *x3 = monty_mul(wc->mc, x2, P->X);
  461. mp_int *z2 = monty_mul(wc->mc, P->Z, P->Z);
  462. mp_int *z4 = monty_mul(wc->mc, z2, z2);
  463. mp_int *az4 = monty_mul(wc->mc, wc->a, z4);
  464. mp_int *axz4 = monty_mul(wc->mc, az4, P->X);
  465. mp_int *x3_plus_axz4 = monty_add(wc->mc, x3, axz4);
  466. mp_int *z6 = monty_mul(wc->mc, z2, z4);
  467. mp_int *bz6 = monty_mul(wc->mc, wc->b, z6);
  468. mp_int *rhs = monty_add(wc->mc, x3_plus_axz4, bz6);
  469. unsigned valid = mp_cmp_eq(lhs, rhs);
  470. mp_free(lhs);
  471. mp_free(x2);
  472. mp_free(x3);
  473. mp_free(z2);
  474. mp_free(z4);
  475. mp_free(az4);
  476. mp_free(axz4);
  477. mp_free(x3_plus_axz4);
  478. mp_free(z6);
  479. mp_free(bz6);
  480. mp_free(rhs);
  481. return valid;
  482. }
  483. /* ----------------------------------------------------------------------
  484. * Montgomery curves.
  485. */
  486. struct MontgomeryPoint {
  487. /* XZ coordinates. These represent the affine x coordinate by the
  488. * relationship x = X/Z. */
  489. mp_int *X, *Z;
  490. MontgomeryCurve *mc;
  491. };
  492. struct MontgomeryCurve {
  493. /* Prime modulus of the finite field. */
  494. mp_int *p;
  495. /* Montgomery context for arithmetic mod p. */
  496. MontyContext *mc;
  497. /* Parameters of the curve, in Montgomery-multiplication
  498. * transformed form. */
  499. mp_int *a, *b;
  500. /* (a+2)/4, also in Montgomery-multiplication form. */
  501. mp_int *aplus2over4;
  502. };
  503. MontgomeryCurve *ecc_montgomery_curve(
  504. mp_int *p, mp_int *a, mp_int *b)
  505. {
  506. MontgomeryCurve *mc = snew(MontgomeryCurve);
  507. mc->p = mp_copy(p);
  508. mc->mc = monty_new(p);
  509. mc->a = monty_import(mc->mc, a);
  510. mc->b = monty_import(mc->mc, b);
  511. { // WINSCP
  512. mp_int *four = mp_from_integer(4);
  513. mp_int *fourinverse = mp_invert(four, mc->p);
  514. mp_int *aplus2 = mp_copy(a);
  515. mp_add_integer_into(aplus2, aplus2, 2);
  516. { // WINSCP
  517. mp_int *aplus2over4 = mp_modmul(aplus2, fourinverse, mc->p);
  518. mc->aplus2over4 = monty_import(mc->mc, aplus2over4);
  519. mp_free(four);
  520. mp_free(fourinverse);
  521. mp_free(aplus2);
  522. mp_free(aplus2over4);
  523. } // WINSCP
  524. } // WINSCP
  525. return mc;
  526. }
  527. void ecc_montgomery_curve_free(MontgomeryCurve *mc)
  528. {
  529. mp_free(mc->p);
  530. mp_free(mc->a);
  531. mp_free(mc->b);
  532. mp_free(mc->aplus2over4);
  533. monty_free(mc->mc);
  534. sfree(mc);
  535. }
  536. static MontgomeryPoint *ecc_montgomery_point_new_empty(MontgomeryCurve *mc)
  537. {
  538. MontgomeryPoint *mp = snew(MontgomeryPoint);
  539. mp->mc = mc;
  540. mp->X = mp->Z = NULL;
  541. return mp;
  542. }
  543. MontgomeryPoint *ecc_montgomery_point_new(MontgomeryCurve *mc, mp_int *x)
  544. {
  545. MontgomeryPoint *mp = ecc_montgomery_point_new_empty(mc);
  546. mp->X = monty_import(mc->mc, x);
  547. mp->Z = mp_copy(monty_identity(mc->mc));
  548. return mp;
  549. }
  550. void ecc_montgomery_point_copy_into(
  551. MontgomeryPoint *dest, MontgomeryPoint *src)
  552. {
  553. mp_copy_into(dest->X, src->X);
  554. mp_copy_into(dest->Z, src->Z);
  555. }
  556. MontgomeryPoint *ecc_montgomery_point_copy(MontgomeryPoint *orig)
  557. {
  558. MontgomeryPoint *mp = ecc_montgomery_point_new_empty(orig->mc);
  559. mp->X = mp_copy(orig->X);
  560. mp->Z = mp_copy(orig->Z);
  561. return mp;
  562. }
  563. void ecc_montgomery_point_free(MontgomeryPoint *mp)
  564. {
  565. mp_free(mp->X);
  566. mp_free(mp->Z);
  567. smemclr(mp, sizeof(*mp));
  568. sfree(mp);
  569. }
  570. static void ecc_montgomery_cond_overwrite(
  571. MontgomeryPoint *dest, MontgomeryPoint *src, unsigned overwrite)
  572. {
  573. mp_select_into(dest->X, dest->X, src->X, overwrite);
  574. mp_select_into(dest->Z, dest->Z, src->Z, overwrite);
  575. }
  576. static void ecc_montgomery_cond_swap(
  577. MontgomeryPoint *P, MontgomeryPoint *Q, unsigned swap)
  578. {
  579. mp_cond_swap(P->X, Q->X, swap);
  580. mp_cond_swap(P->Z, Q->Z, swap);
  581. }
  582. MontgomeryPoint *ecc_montgomery_diff_add(
  583. MontgomeryPoint *P, MontgomeryPoint *Q, MontgomeryPoint *PminusQ)
  584. {
  585. MontgomeryCurve *mc = P->mc;
  586. assert(Q->mc == mc);
  587. assert(PminusQ->mc == mc);
  588. /*
  589. * Differential addition is achieved using the following formula
  590. * that relates the affine x-coordinates of P, Q, P+Q and P-Q:
  591. *
  592. * x(P+Q) x(P-Q) (x(Q)-x(P))^2 = (x(P)x(Q) - 1)^2
  593. *
  594. * As with the Weierstrass coordinates, the code below transforms
  595. * that affine relation into a projective one to avoid having to
  596. * do a division during the main arithmetic.
  597. */
  598. { // WINSCP
  599. MontgomeryPoint *S = ecc_montgomery_point_new_empty(mc);
  600. mp_int *Px_m_Pz = monty_sub(mc->mc, P->X, P->Z);
  601. mp_int *Px_p_Pz = monty_add(mc->mc, P->X, P->Z);
  602. mp_int *Qx_m_Qz = monty_sub(mc->mc, Q->X, Q->Z);
  603. mp_int *Qx_p_Qz = monty_add(mc->mc, Q->X, Q->Z);
  604. mp_int *PmQp = monty_mul(mc->mc, Px_m_Pz, Qx_p_Qz);
  605. mp_int *PpQm = monty_mul(mc->mc, Px_p_Pz, Qx_m_Qz);
  606. mp_int *Xpre = monty_add(mc->mc, PmQp, PpQm);
  607. mp_int *Zpre = monty_sub(mc->mc, PmQp, PpQm);
  608. mp_int *Xpre2 = monty_mul(mc->mc, Xpre, Xpre);
  609. mp_int *Zpre2 = monty_mul(mc->mc, Zpre, Zpre);
  610. S->X = monty_mul(mc->mc, Xpre2, PminusQ->Z);
  611. S->Z = monty_mul(mc->mc, Zpre2, PminusQ->X);
  612. mp_free(Px_m_Pz);
  613. mp_free(Px_p_Pz);
  614. mp_free(Qx_m_Qz);
  615. mp_free(Qx_p_Qz);
  616. mp_free(PmQp);
  617. mp_free(PpQm);
  618. mp_free(Xpre);
  619. mp_free(Zpre);
  620. mp_free(Xpre2);
  621. mp_free(Zpre2);
  622. return S;
  623. } // WINSCP
  624. }
  625. MontgomeryPoint *ecc_montgomery_double(MontgomeryPoint *P)
  626. {
  627. MontgomeryCurve *mc = P->mc;
  628. MontgomeryPoint *D = ecc_montgomery_point_new_empty(mc);
  629. /*
  630. * To double a point in affine coordinates, in principle you can
  631. * use the same technique as for Weierstrass: differentiate the
  632. * curve equation to get the tangent line at the input point, use
  633. * that to get an expression for y which you substitute back into
  634. * the curve equation, and subtract the known two roots (in this
  635. * case both the same) from the x^2 coefficient of the resulting
  636. * cubic.
  637. *
  638. * In this case, we don't have an input y-coordinate, so you have
  639. * to do a bit of extra transformation to find a formula that can
  640. * work without it. The tangent formula is (3x^2 + 2ax + 1)/(2y),
  641. * and when that appears in the final formula it will be squared -
  642. * so we can substitute the y^2 in the denominator for the RHS of
  643. * the curve equation. Put together, that gives
  644. *
  645. * x_out = (x+1)^2 (x-1)^2 / 4(x^3+ax^2+x)
  646. *
  647. * and, as usual, the code below transforms that into projective
  648. * form to avoid the division.
  649. */
  650. mp_int *Px_m_Pz = monty_sub(mc->mc, P->X, P->Z);
  651. mp_int *Px_p_Pz = monty_add(mc->mc, P->X, P->Z);
  652. mp_int *Px_m_Pz_2 = monty_mul(mc->mc, Px_m_Pz, Px_m_Pz);
  653. mp_int *Px_p_Pz_2 = monty_mul(mc->mc, Px_p_Pz, Px_p_Pz);
  654. D->X = monty_mul(mc->mc, Px_m_Pz_2, Px_p_Pz_2);
  655. { // WINSCP
  656. mp_int *XZ = monty_mul(mc->mc, P->X, P->Z);
  657. mp_int *twoXZ = monty_add(mc->mc, XZ, XZ);
  658. mp_int *fourXZ = monty_add(mc->mc, twoXZ, twoXZ);
  659. mp_int *fourXZ_scaled = monty_mul(mc->mc, fourXZ, mc->aplus2over4);
  660. mp_int *Zpre = monty_add(mc->mc, Px_m_Pz_2, fourXZ_scaled);
  661. D->Z = monty_mul(mc->mc, fourXZ, Zpre);
  662. mp_free(Px_m_Pz);
  663. mp_free(Px_p_Pz);
  664. mp_free(Px_m_Pz_2);
  665. mp_free(Px_p_Pz_2);
  666. mp_free(XZ);
  667. mp_free(twoXZ);
  668. mp_free(fourXZ);
  669. mp_free(fourXZ_scaled);
  670. mp_free(Zpre);
  671. } // WINSCP
  672. return D;
  673. }
  674. static void ecc_montgomery_normalise(MontgomeryPoint *mp)
  675. {
  676. MontgomeryCurve *mc = mp->mc;
  677. mp_int *zinv = monty_invert(mc->mc, mp->Z);
  678. monty_mul_into(mc->mc, mp->X, mp->X, zinv);
  679. mp_free(zinv);
  680. mp_copy_into(mp->Z, monty_identity(mc->mc));
  681. }
  682. MontgomeryPoint *ecc_montgomery_multiply(MontgomeryPoint *B, mp_int *n)
  683. {
  684. /*
  685. * 'Montgomery ladder' technique, to compute an arbitrary integer
  686. * multiple of B under the constraint that you can only add two
  687. * unequal points if you also know their difference.
  688. *
  689. * The setup is that you maintain two curve points one of which is
  690. * always the other one plus B. Call them kB and (k+1)B, where k
  691. * is some integer that evolves as we go along. We begin by
  692. * doubling the input B, to initialise those points to B and 2B,
  693. * so that k=1.
  694. *
  695. * At each stage, we add kB and (k+1)B together - which we can do
  696. * under the differential-addition constraint because we know
  697. * their difference is always just B - to give us (2k+1)B. Then we
  698. * double one of kB or (k+1)B, and depending on which one we
  699. * choose, we end up with (2k)B or (2k+2)B. Either way, that
  700. * differs by B from the other value we've just computed. So in
  701. * each iteration, we do one diff-add and one doubling, plus a
  702. * couple of conditional swaps to choose which value we double and
  703. * which way round we put the output points, and the effect is to
  704. * replace k with either 2k or 2k+1, which we choose based on the
  705. * appropriate bit of the desired exponent.
  706. *
  707. * This routine doesn't assume we know the exact location of the
  708. * topmost set bit of the exponent. So to maintain constant time
  709. * it does an iteration for every _potential_ bit, starting from
  710. * the top downwards; after each iteration in which we haven't
  711. * seen a set exponent bit yet, we just overwrite the two points
  712. * with B and 2B again,
  713. */
  714. MontgomeryPoint *two_B = ecc_montgomery_double(B);
  715. MontgomeryPoint *k_B = ecc_montgomery_point_copy(B);
  716. MontgomeryPoint *kplus1_B = ecc_montgomery_point_copy(two_B);
  717. unsigned not_started_yet = 1;
  718. size_t bitindex; // WINSCP
  719. for (bitindex = mp_max_bits(n); bitindex-- > 0 ;) {
  720. unsigned nbit = mp_get_bit(n, bitindex);
  721. MontgomeryPoint *sum = ecc_montgomery_diff_add(k_B, kplus1_B, B);
  722. ecc_montgomery_cond_swap(k_B, kplus1_B, nbit);
  723. { // WINSCP
  724. MontgomeryPoint *other = ecc_montgomery_double(k_B);
  725. ecc_montgomery_point_free(k_B);
  726. ecc_montgomery_point_free(kplus1_B);
  727. k_B = other;
  728. kplus1_B = sum;
  729. ecc_montgomery_cond_swap(k_B, kplus1_B, nbit);
  730. ecc_montgomery_cond_overwrite(k_B, B, not_started_yet);
  731. ecc_montgomery_cond_overwrite(kplus1_B, two_B, not_started_yet);
  732. not_started_yet &= ~nbit;
  733. } // WINSCP
  734. }
  735. ecc_montgomery_point_free(two_B);
  736. ecc_montgomery_point_free(kplus1_B);
  737. return k_B;
  738. }
  739. void ecc_montgomery_get_affine(MontgomeryPoint *mp, mp_int **x)
  740. {
  741. MontgomeryCurve *mc = mp->mc;
  742. ecc_montgomery_normalise(mp);
  743. if (x)
  744. *x = monty_export(mc->mc, mp->X);
  745. }
  746. /* ----------------------------------------------------------------------
  747. * Twisted Edwards curves.
  748. */
  749. struct EdwardsPoint {
  750. /*
  751. * We represent an Edwards curve point in 'extended coordinates'.
  752. * There's more than one coordinate system going by that name,
  753. * unfortunately. These ones have the semantics that X,Y,Z are
  754. * ordinary projective coordinates (so x=X/Z and y=Y/Z), but also,
  755. * we store the extra value T = xyZ = XY/Z.
  756. */
  757. mp_int *X, *Y, *Z, *T;
  758. EdwardsCurve *ec;
  759. };
  760. struct EdwardsCurve {
  761. /* Prime modulus of the finite field. */
  762. mp_int *p;
  763. /* Montgomery context for arithmetic mod p. */
  764. MontyContext *mc;
  765. /* Modsqrt context for point decompression. */
  766. ModsqrtContext *sc;
  767. /* Parameters of the curve, in Montgomery-multiplication
  768. * transformed form. */
  769. mp_int *d, *a;
  770. };
  771. EdwardsCurve *ecc_edwards_curve(mp_int *p, mp_int *d, mp_int *a,
  772. mp_int *nonsquare_mod_p)
  773. {
  774. EdwardsCurve *ec = snew(EdwardsCurve);
  775. ec->p = mp_copy(p);
  776. ec->mc = monty_new(p);
  777. ec->d = monty_import(ec->mc, d);
  778. ec->a = monty_import(ec->mc, a);
  779. if (nonsquare_mod_p)
  780. ec->sc = modsqrt_new(p, nonsquare_mod_p);
  781. else
  782. ec->sc = NULL;
  783. return ec;
  784. }
  785. void ecc_edwards_curve_free(EdwardsCurve *ec)
  786. {
  787. mp_free(ec->p);
  788. mp_free(ec->d);
  789. mp_free(ec->a);
  790. monty_free(ec->mc);
  791. if (ec->sc)
  792. modsqrt_free(ec->sc);
  793. sfree(ec);
  794. }
  795. static EdwardsPoint *ecc_edwards_point_new_empty(EdwardsCurve *ec)
  796. {
  797. EdwardsPoint *ep = snew(EdwardsPoint);
  798. ep->ec = ec;
  799. ep->X = ep->Y = ep->Z = ep->T = NULL;
  800. return ep;
  801. }
  802. static EdwardsPoint *ecc_edwards_point_new_imported(
  803. EdwardsCurve *ec, mp_int *monty_x, mp_int *monty_y)
  804. {
  805. EdwardsPoint *ep = ecc_edwards_point_new_empty(ec);
  806. ep->X = monty_x;
  807. ep->Y = monty_y;
  808. ep->T = monty_mul(ec->mc, ep->X, ep->Y);
  809. ep->Z = mp_copy(monty_identity(ec->mc));
  810. return ep;
  811. }
  812. EdwardsPoint *ecc_edwards_point_new(
  813. EdwardsCurve *ec, mp_int *x, mp_int *y)
  814. {
  815. return ecc_edwards_point_new_imported(
  816. ec, monty_import(ec->mc, x), monty_import(ec->mc, y));
  817. }
  818. void ecc_edwards_point_copy_into(EdwardsPoint *dest, EdwardsPoint *src)
  819. {
  820. mp_copy_into(dest->X, src->X);
  821. mp_copy_into(dest->Y, src->Y);
  822. mp_copy_into(dest->Z, src->Z);
  823. mp_copy_into(dest->T, src->T);
  824. }
  825. EdwardsPoint *ecc_edwards_point_copy(EdwardsPoint *orig)
  826. {
  827. EdwardsPoint *ep = ecc_edwards_point_new_empty(orig->ec);
  828. ep->X = mp_copy(orig->X);
  829. ep->Y = mp_copy(orig->Y);
  830. ep->Z = mp_copy(orig->Z);
  831. ep->T = mp_copy(orig->T);
  832. return ep;
  833. }
  834. void ecc_edwards_point_free(EdwardsPoint *ep)
  835. {
  836. mp_free(ep->X);
  837. mp_free(ep->Y);
  838. mp_free(ep->Z);
  839. mp_free(ep->T);
  840. smemclr(ep, sizeof(*ep));
  841. sfree(ep);
  842. }
  843. EdwardsPoint *ecc_edwards_point_new_from_y(
  844. EdwardsCurve *ec, mp_int *yorig, unsigned desired_x_parity)
  845. {
  846. pinitassert(ec->sc);
  847. /*
  848. * The curve equation is ax^2 + y^2 = 1 + dx^2y^2, which
  849. * rearranges to x^2(dy^2-a) = y^2-1. So we compute
  850. * (y^2-1)/(dy^2-a) and take its square root.
  851. */
  852. unsigned success;
  853. mp_int *y = monty_import(ec->mc, yorig);
  854. mp_int *y2 = monty_mul(ec->mc, y, y);
  855. mp_int *dy2 = monty_mul(ec->mc, ec->d, y2);
  856. mp_int *dy2ma = monty_sub(ec->mc, dy2, ec->a);
  857. mp_int *y2m1 = monty_sub(ec->mc, y2, monty_identity(ec->mc));
  858. mp_int *recip_denominator = monty_invert(ec->mc, dy2ma);
  859. mp_int *radicand = monty_mul(ec->mc, y2m1, recip_denominator);
  860. mp_int *x = monty_modsqrt(ec->sc, radicand, &success);
  861. mp_free(y2);
  862. mp_free(dy2);
  863. mp_free(dy2ma);
  864. mp_free(y2m1);
  865. mp_free(recip_denominator);
  866. mp_free(radicand);
  867. if (!success) {
  868. /* Failure! x^2 worked out to be a number that has no square
  869. * root mod p. In this situation there's no point in trying to
  870. * be time-constant, since the protocol sequence is going to
  871. * diverge anyway when we complain to whoever gave us this
  872. * bogus value. */
  873. mp_free(x);
  874. mp_free(y);
  875. return NULL;
  876. }
  877. /*
  878. * Choose whichever of x and p-x has the specified parity (of its
  879. * lowest positive residue mod p).
  880. */
  881. { // WINSCP
  882. mp_int *tmp = monty_export(ec->mc, x);
  883. unsigned flip = (mp_get_bit(tmp, 0) ^ desired_x_parity) & 1;
  884. mp_sub_into(tmp, ec->p, x);
  885. mp_select_into(x, x, tmp, flip);
  886. mp_free(tmp);
  887. } // WINSCP
  888. return ecc_edwards_point_new_imported(ec, x, y);
  889. }
  890. static void ecc_edwards_cond_overwrite(
  891. EdwardsPoint *dest, EdwardsPoint *src, unsigned overwrite)
  892. {
  893. mp_select_into(dest->X, dest->X, src->X, overwrite);
  894. mp_select_into(dest->Y, dest->Y, src->Y, overwrite);
  895. mp_select_into(dest->Z, dest->Z, src->Z, overwrite);
  896. mp_select_into(dest->T, dest->T, src->T, overwrite);
  897. }
  898. static void ecc_edwards_cond_swap(
  899. EdwardsPoint *P, EdwardsPoint *Q, unsigned swap)
  900. {
  901. mp_cond_swap(P->X, Q->X, swap);
  902. mp_cond_swap(P->Y, Q->Y, swap);
  903. mp_cond_swap(P->Z, Q->Z, swap);
  904. mp_cond_swap(P->T, Q->T, swap);
  905. }
  906. EdwardsPoint *ecc_edwards_add(EdwardsPoint *P, EdwardsPoint *Q)
  907. {
  908. EdwardsCurve *ec = P->ec;
  909. assert(Q->ec == ec);
  910. { // WINSCP
  911. EdwardsPoint *S = ecc_edwards_point_new_empty(ec);
  912. /*
  913. * The affine rule for Edwards addition of (x1,y1) and (x2,y2) is
  914. *
  915. * x_out = (x1 y2 + y1 x2) / (1 + d x1 x2 y1 y2)
  916. * y_out = (y1 y2 - a x1 x2) / (1 - d x1 x2 y1 y2)
  917. *
  918. * The formulae below are listed as 'add-2008-hwcd' in
  919. * https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html
  920. *
  921. * and if you undo the careful optimisation to find out what
  922. * they're actually computing, it comes out to
  923. *
  924. * X_out = (X1 Y2 + Y1 X2) (Z1 Z2 - d T1 T2)
  925. * Y_out = (Y1 Y2 - a X1 X2) (Z1 Z2 + d T1 T2)
  926. * Z_out = (Z1 Z2 - d T1 T2) (Z1 Z2 + d T1 T2)
  927. * T_out = (X1 Y2 + Y1 X2) (Y1 Y2 - a X1 X2)
  928. */
  929. mp_int *PxQx = monty_mul(ec->mc, P->X, Q->X);
  930. mp_int *PyQy = monty_mul(ec->mc, P->Y, Q->Y);
  931. mp_int *PtQt = monty_mul(ec->mc, P->T, Q->T);
  932. mp_int *PzQz = monty_mul(ec->mc, P->Z, Q->Z);
  933. mp_int *Psum = monty_add(ec->mc, P->X, P->Y);
  934. mp_int *Qsum = monty_add(ec->mc, Q->X, Q->Y);
  935. mp_int *aPxQx = monty_mul(ec->mc, ec->a, PxQx);
  936. mp_int *dPtQt = monty_mul(ec->mc, ec->d, PtQt);
  937. mp_int *sumprod = monty_mul(ec->mc, Psum, Qsum);
  938. mp_int *xx_p_yy = monty_add(ec->mc, PxQx, PyQy);
  939. mp_int *E = monty_sub(ec->mc, sumprod, xx_p_yy);
  940. mp_int *F = monty_sub(ec->mc, PzQz, dPtQt);
  941. mp_int *G = monty_add(ec->mc, PzQz, dPtQt);
  942. mp_int *H = monty_sub(ec->mc, PyQy, aPxQx);
  943. S->X = monty_mul(ec->mc, E, F);
  944. S->Z = monty_mul(ec->mc, F, G);
  945. S->Y = monty_mul(ec->mc, G, H);
  946. S->T = monty_mul(ec->mc, H, E);
  947. mp_free(PxQx);
  948. mp_free(PyQy);
  949. mp_free(PtQt);
  950. mp_free(PzQz);
  951. mp_free(Psum);
  952. mp_free(Qsum);
  953. mp_free(aPxQx);
  954. mp_free(dPtQt);
  955. mp_free(sumprod);
  956. mp_free(xx_p_yy);
  957. mp_free(E);
  958. mp_free(F);
  959. mp_free(G);
  960. mp_free(H);
  961. return S;
  962. } // WINSCP
  963. }
  964. static void ecc_edwards_normalise(EdwardsPoint *ep)
  965. {
  966. EdwardsCurve *ec = ep->ec;
  967. mp_int *zinv = monty_invert(ec->mc, ep->Z);
  968. monty_mul_into(ec->mc, ep->X, ep->X, zinv);
  969. monty_mul_into(ec->mc, ep->Y, ep->Y, zinv);
  970. mp_free(zinv);
  971. mp_copy_into(ep->Z, monty_identity(ec->mc));
  972. monty_mul_into(ec->mc, ep->T, ep->X, ep->Y);
  973. }
  974. EdwardsPoint *ecc_edwards_multiply(EdwardsPoint *B, mp_int *n)
  975. {
  976. EdwardsPoint *two_B = ecc_edwards_add(B, B);
  977. EdwardsPoint *k_B = ecc_edwards_point_copy(B);
  978. EdwardsPoint *kplus1_B = ecc_edwards_point_copy(two_B);
  979. /*
  980. * Another copy of the same exponentiation routine following the
  981. * pattern of the Montgomery ladder, because it works as well as
  982. * any other technique and this way I didn't have to debug two of
  983. * them.
  984. */
  985. unsigned not_started_yet = 1;
  986. size_t bitindex; // WINSCP
  987. for (bitindex = mp_max_bits(n); bitindex-- > 0 ;) {
  988. unsigned nbit = mp_get_bit(n, bitindex);
  989. EdwardsPoint *sum = ecc_edwards_add(k_B, kplus1_B);
  990. ecc_edwards_cond_swap(k_B, kplus1_B, nbit);
  991. { // WINSCP
  992. EdwardsPoint *other = ecc_edwards_add(k_B, k_B);
  993. ecc_edwards_point_free(k_B);
  994. ecc_edwards_point_free(kplus1_B);
  995. k_B = other;
  996. kplus1_B = sum;
  997. ecc_edwards_cond_swap(k_B, kplus1_B, nbit);
  998. ecc_edwards_cond_overwrite(k_B, B, not_started_yet);
  999. ecc_edwards_cond_overwrite(kplus1_B, two_B, not_started_yet);
  1000. not_started_yet &= ~nbit;
  1001. } // WINSCP
  1002. }
  1003. ecc_edwards_point_free(two_B);
  1004. ecc_edwards_point_free(kplus1_B);
  1005. return k_B;
  1006. }
  1007. /*
  1008. * Helper routine to determine whether two values each given as a pair
  1009. * of projective coordinates represent the same affine value.
  1010. */
  1011. static inline unsigned projective_eq(
  1012. MontyContext *mc, mp_int *An, mp_int *Ad,
  1013. mp_int *Bn, mp_int *Bd)
  1014. {
  1015. mp_int *AnBd = monty_mul(mc, An, Bd);
  1016. mp_int *BnAd = monty_mul(mc, Bn, Ad);
  1017. unsigned toret = mp_cmp_eq(AnBd, BnAd);
  1018. mp_free(AnBd);
  1019. mp_free(BnAd);
  1020. return toret;
  1021. }
  1022. unsigned ecc_edwards_eq(EdwardsPoint *P, EdwardsPoint *Q)
  1023. {
  1024. EdwardsCurve *ec = P->ec;
  1025. assert(Q->ec == ec);
  1026. return (projective_eq(ec->mc, P->X, P->Z, Q->X, Q->Z) &
  1027. projective_eq(ec->mc, P->Y, P->Z, Q->Y, Q->Z));
  1028. }
  1029. void ecc_edwards_get_affine(EdwardsPoint *ep, mp_int **x, mp_int **y)
  1030. {
  1031. EdwardsCurve *ec = ep->ec;
  1032. ecc_edwards_normalise(ep);
  1033. if (x)
  1034. *x = monty_export(ec->mc, ep->X);
  1035. if (y)
  1036. *y = monty_export(ec->mc, ep->Y);
  1037. }