Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / ecc-add-eh.c
1 /* ecc-add-eh.c
2
3    Copyright (C) 2014 Niels Möller
4
5    This file is part of GNU Nettle.
6
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9
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.
13
14    or
15
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.
19
20    or both in parallel, as here.
21
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.
26
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/.
30 */
31
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include "ecc.h"
37 #include "ecc-internal.h"
38
39 /* Add two points on an Edwards curve, with result and first point in
40    homogeneous coordinates. */
41 void
42 ecc_add_eh (const struct ecc_curve *ecc,
43             mp_limb_t *r, const mp_limb_t *p, const mp_limb_t *q,
44             mp_limb_t *scratch)
45 {
46 #define x1 p
47 #define y1 (p + ecc->p.size)
48 #define z1 (p + 2*ecc->p.size)
49
50 #define x2 q
51 #define y2 (q + ecc->p.size)
52
53 #define x3 r
54 #define y3 (r + ecc->p.size)
55 #define z3 (r + 2*ecc->p.size)
56
57   /* Formulas (from djb,
58      http://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#doubling-dbl-2007-bl):
59
60      Computation        Operation       Live variables
61
62      C = x1*x2          mul             C
63      D = y1*y2          mul             C, D
64      T = (x1+y1)(x2+y2) - C - D         C, D, T
65      E = b*C*D          2 mul           C, E, T  (Replace C <-- D - C)
66      B = z1^2           sqr             B, C, E, T
67      F = B - E                          B, C, E, F, T
68      G = B + E                          C, F, G, T
69      x3 = z1*F*T        3 mul           C, F, G, T
70      y3 = z1*G*(D-C)    2 mul           F, G
71      z3 = F*G           mul
72   */
73 #define C (scratch)
74 #define D (scratch + 1*ecc->p.size)
75 #define T (scratch + 2*ecc->p.size)
76 #define E (scratch + 3*ecc->p.size) 
77 #define B (scratch + 4*ecc->p.size)
78 #define F D
79 #define G E
80   
81   ecc_modp_mul (ecc, C, x1, x2);
82   ecc_modp_mul (ecc, D, y1, y2);
83   ecc_modp_add (ecc, x3, x1, y1);
84   ecc_modp_add (ecc, y3, x2, y2);
85   ecc_modp_mul (ecc, T, x3, y3);
86   ecc_modp_sub (ecc, T, T, C);
87   ecc_modp_sub (ecc, T, T, D);
88   ecc_modp_mul (ecc, x3, C, D);
89   ecc_modp_mul (ecc, E, x3, ecc->b);
90
91   ecc_modp_add (ecc, C, D, C); /* ! */
92   ecc_modp_sqr (ecc, B, z1);
93   ecc_modp_sub (ecc, F, B, E);
94   ecc_modp_add (ecc, G, B, E);  
95
96   /* x3 */
97   ecc_modp_mul (ecc, B, G, T); /* ! */
98   ecc_modp_mul (ecc, x3, B, z1);
99
100   /* y3 */
101   ecc_modp_mul (ecc, B, F, C); /* ! */
102   ecc_modp_mul (ecc, y3, B, z1);
103
104   /* z3 */
105   ecc_modp_mul (ecc, B, F, G);
106   mpn_copyi (z3, B, ecc->p.size);
107 }