Fix accidental use of C99 for loop.
[gd/nettle] / ecc-dup-eh.c
1 /* ecc-dup-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 /* Double a point on an Edwards curve, in homogeneous coordinates */
40 void
41 ecc_dup_eh (const struct ecc_curve *ecc,
42             mp_limb_t *r, const mp_limb_t *p,
43             mp_limb_t *scratch)
44 {
45   /* Formulas (from djb,
46      http://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#doubling-dbl-2007-bl):
47
48      Computation        Operation       Live variables
49      
50      b = (x+y)^2        sqr             b
51      c = x^2            sqr             b, c
52      d = y^2            sqr             b, c, d
53      e = c+d                            b, c, d, e
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
57      y' = e*(c-d)       mul             e, j
58      z' = e*j           mul
59
60      But for the twisted curve, we need some sign changes.
61
62      b = (x+y)^2        sqr             b
63      c = x^2            sqr             b, c
64      d = y^2            sqr             b, c, d
65    ! e = -c+d                           b, c, d, e
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
70      z' = e*j           mul
71   */
72 #define b scratch 
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)
77
78   /* b */
79   ecc_modp_add (ecc, e, p, p + ecc->p.size);
80   ecc_modp_sqr (ecc, b, e);
81
82   /* c */
83   ecc_modp_sqr (ecc, c, p);
84   /* d */
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);
88   /* e, */
89   ecc_modp_sub (ecc, e, d, c);
90   /* b - c - d */
91   ecc_modp_sub (ecc, b, b, c);
92   ecc_modp_sub (ecc, b, b, d);
93   /* j */
94   ecc_modp_add (ecc, r, r, r);
95   ecc_modp_sub (ecc, j, r, e);
96
97   /* x' */
98   ecc_modp_mul (ecc, r, b, j);
99   /* y' */
100   ecc_modp_add (ecc, c, c, d); /* Redundant */
101   ecc_modp_mul (ecc, r + ecc->p.size, e, c);
102   /* z' */
103   ecc_modp_mul (ecc, b, e, j);
104   mpn_copyi (r + 2*ecc->p.size, b, ecc->p.size);
105 }