ecc.c 35 KB

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