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 /* Add two points on an Edwards curve, in homogeneous coordinates */
41 ecc_add_ehh (const struct ecc_curve *ecc,
42 mp_limb_t *r, const mp_limb_t *p, const mp_limb_t *q,
46 #define y1 (p + ecc->p.size)
47 #define z1 (p + 2*ecc->p.size)
50 #define y2 (q + ecc->p.size)
51 #define z2 (q + 2*ecc->p.size)
54 #define y3 (r + ecc->p.size)
55 #define z3 (r + 2*ecc->p.size)
57 /* Formulas (from djb,
58 http://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#addition-add-2007-bl):
60 Computation Operation Live variables
64 T = (x1+y1)(x2+y2) - C - D, mul C, D, T
65 E = b*C*D 2 mul C, E, T (Replace C <-- D - C)
66 A = z1*z2 mul A, C, E, T
67 B = A^2 sqr A, B, C, E, T
68 F = B - E A, B, C, E, F, T
69 G = B + E A, C, F, G, T
70 x3 = A*F*T 2 mul A, C, G
71 y3 = A*G*(D-C) 2 mul F, G
74 But when working with the twist curve, we have to negate the
75 factor C = x1*x2. We change subtract to add in the y3
76 expression, and swap F and G.
79 #define D (scratch + ecc->p.size)
80 #define T (scratch + 2*ecc->p.size)
81 #define E (scratch + 3*ecc->p.size)
82 #define A (scratch + 4*ecc->p.size)
83 #define B (scratch + 5*ecc->p.size)
87 ecc_modp_mul (ecc, C, x1, x2);
88 ecc_modp_mul (ecc, D, y1, y2);
89 ecc_modp_add (ecc, A, x1, y1);
90 ecc_modp_add (ecc, B, x2, y2);
91 ecc_modp_mul (ecc, T, A, B);
92 ecc_modp_sub (ecc, T, T, C);
93 ecc_modp_sub (ecc, T, T, D);
94 ecc_modp_mul (ecc, x3, C, D);
95 ecc_modp_mul (ecc, E, x3, ecc->b);
96 ecc_modp_add (ecc, C, D, C); /* ! */
98 ecc_modp_mul (ecc, A, z1, z2);
99 ecc_modp_sqr (ecc, B, A);
101 ecc_modp_sub (ecc, F, B, E);
102 ecc_modp_add (ecc, G, B, E);
105 ecc_modp_mul (ecc, B, G, T); /* ! */
106 ecc_modp_mul (ecc, x3, B, A);
109 ecc_modp_mul (ecc, B, F, C); /* ! */
110 ecc_modp_mul (ecc, y3, B, A);
113 ecc_modp_mul (ecc, B, F, G);
114 mpn_copyi (z3, B, ecc->p.size);