3 Copyright (C) 2014 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
38 #include "curve25519.h"
41 #include "ecc-internal.h"
43 /* Transform a point on the twisted Edwards curve to the curve25519
44 Montgomery curve, and return the x coordinate. */
46 curve25519_eh_to_x (mp_limb_t *xp, const mp_limb_t *p,
49 #define vp (p + ecc->p.size)
50 #define wp (p + 2*ecc->p.size)
52 #define t1 (scratch + ecc->p.size)
53 #define t2 (scratch + 2*ecc->p.size)
55 const struct ecc_curve *ecc = &_nettle_curve25519;
58 /* If u = U/W and v = V/W are the coordiantes of the point on the
59 Edwards curve we get the curve25519 x coordinate as
61 x = (1+v) / (1-v) = (W + V) / (W - V)
63 /* NOTE: For the infinity point, this subtraction gives zero (mod
64 p), which isn't invertible. For curve25519, the desired output is
65 x = 0, and we should be fine, since ecc_modp_inv returns 0
67 ecc_modp_sub (ecc, t0, wp, vp);
68 /* Needs a total of 5*size storage. */
69 ecc->p.invert (&ecc->p, t1, t0, t2 + ecc->p.size);
71 ecc_modp_add (ecc, t0, wp, vp);
72 ecc_modp_mul (ecc, t2, t0, t1);
74 cy = mpn_sub_n (xp, t2, ecc->p.m, ecc->p.size);
75 cnd_copy (cy, xp, t2, ecc->p.size);