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/.
39 #include "ecc-internal.h"
41 /* Convert from homogeneous coordinates on the Edwards curve to affine
44 ecc_eh_to_a (const struct ecc_curve *ecc,
46 mp_limb_t *r, const mp_limb_t *p,
50 #define tp (scratch + ecc->p.size)
54 #define yp (p + ecc->p.size)
55 #define zp (p + 2*ecc->p.size)
59 /* Needs 2*size + scratch for the invert call. */
60 ecc->p.invert (&ecc->p, izp, zp, tp + ecc->p.size);
62 ecc_modp_mul (ecc, tp, xp, izp);
63 cy = mpn_sub_n (r, tp, ecc->p.m, ecc->p.size);
64 cnd_copy (cy, r, tp, ecc->p.size);
68 /* Skip y coordinate */
71 /* Reduce modulo q. FIXME: Hardcoded for curve25519,
72 duplicates end of ecc_25519_modq. FIXME: Is this needed
73 at all? Full reduction mod p is maybe sufficient. */
75 assert (ecc->p.bit_size == 255);
76 shift = 252 - GMP_NUMB_BITS * (ecc->p.size - 1);
77 cy = mpn_submul_1 (r, ecc->q.m, ecc->p.size,
78 r[ecc->p.size-1] >> shift);
80 cnd_add_n (cy, r, ecc->q.m, ecc->p.size);
84 ecc_modp_mul (ecc, tp, yp, izp);
85 cy = mpn_sub_n (r + ecc->p.size, tp, ecc->p.m, ecc->p.size);
86 cnd_copy (cy, r + ecc->p.size, tp, ecc->p.size);