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/.
37 #include "ecc-internal.h"
39 /* Double a point on an Edwards curve, in homogeneous coordinates */
41 ecc_dup_eh (const struct ecc_curve *ecc,
42 mp_limb_t *r, const mp_limb_t *p,
45 /* Formulas (from djb,
46 http://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#doubling-dbl-2007-bl):
48 Computation Operation Live variables
54 h = z^2 sqr b, c, d, e, h
55 j = e-2*h b, c, d, e, j
56 x' = (b-e)*j mul c, d, e, j
60 But for the twisted curve, we need some sign changes.
66 h = z^2 sqr b, c, d, e, h
67 ! j = -e+2*h b, c, d, e, j
68 ! x' = (b-c-d)*j mul c, d, e, j
69 ! y' = e*(c+d) mul e, j
73 #define c (scratch + ecc->p.size)
74 #define d (scratch + 2*ecc->p.size)
75 #define e (scratch + 3*ecc->p.size)
76 #define j (scratch + 4*ecc->p.size)
79 ecc_modp_add (ecc, e, p, p + ecc->p.size);
80 ecc_modp_sqr (ecc, b, e);
83 ecc_modp_sqr (ecc, c, p);
85 ecc_modp_sqr (ecc, d, p + ecc->p.size);
86 /* h, can use r as scratch, even for in-place operation. */
87 ecc_modp_sqr (ecc, r, p + 2*ecc->p.size);
89 ecc_modp_sub (ecc, e, d, c);
91 ecc_modp_sub (ecc, b, b, c);
92 ecc_modp_sub (ecc, b, b, d);
94 ecc_modp_add (ecc, r, r, r);
95 ecc_modp_sub (ecc, j, r, e);
98 ecc_modp_mul (ecc, r, b, j);
100 ecc_modp_add (ecc, c, c, d); /* Redundant */
101 ecc_modp_mul (ecc, r + ecc->p.size, e, c);
103 ecc_modp_mul (ecc, b, e, j);
104 mpn_copyi (r + 2*ecc->p.size, b, ecc->p.size);