/src/nettle-with-libgmp/ecc-dup-eh.c
Line  | Count  | Source  | 
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  | 836  | { | 
45  | 1.67k  | #define x1 p  | 
46  | 1.67k  | #define y1 (p + ecc->p.size)  | 
47  | 836  | #define z1 (p + 2*ecc->p.size)  | 
48  |  |  | 
49  | 3.34k  | #define x2 r  | 
50  | 2.50k  | #define y2 (r + ecc->p.size)  | 
51  | 7.52k  | #define z2 (r + 2*ecc->p.size)  | 
52  |  |  | 
53  |  |   /* Formulas (from djb,  | 
54  |  |      http://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html#doubling-dbl-2007-bl):  | 
55  |  |  | 
56  |  |      Computation  Operation Live variables  | 
57  |  |  | 
58  |  |      b = (x+y)^2  sqr   b  | 
59  |  |      c = x^2    sqr   b, c  | 
60  |  |      d = y^2    sqr   b, c, d  | 
61  |  |      e = c+d        b, c, d, e  | 
62  |  |      h = z^2    sqr   b, c, d, e, h  | 
63  |  |      j = e-2*h        b, c, d, e, j  | 
64  |  |      x' = (b-e)*j mul   c, d, e, j  | 
65  |  |      y' = e*(c-d) mul   e, j  | 
66  |  |      z' = e*j   mul  | 
67  |  |   */  | 
68  | 7.52k  | #define C scratch  | 
69  | 6.68k  | #define D (scratch + 1*ecc->p.size)  | 
70  | 3.34k  | #define B (scratch + 2*ecc->p.size)  | 
71  |  |  | 
72  | 4.18k  | #define E C  | 
73  |  |  | 
74  | 836  |   ecc_mod_sqr (&ecc->p, C, x1, C);  /* C */  | 
75  | 836  |   ecc_mod_sqr (&ecc->p, D, y1, D);  /* C, D */  | 
76  | 836  |   ecc_mod_add (&ecc->p, B, x1, y1);  | 
77  | 836  |   ecc_mod_sqr (&ecc->p, B, B, x2);  /* C, D, B */  | 
78  |  |  | 
79  |  |   /* c-d stored at y' */  | 
80  | 836  |   ecc_mod_sub (&ecc->p, y2, C, D);  | 
81  | 836  |   ecc_mod_add (&ecc->p, E, C, D);  /* B, E */  | 
82  |  |   /* b-e stored at x' */  | 
83  | 836  |   ecc_mod_sub (&ecc->p, x2, B, E);  /* E */  | 
84  |  |  | 
85  |  |   /* Use D as scratch for the following multiplies. */  | 
86  | 836  |   ecc_mod_mul (&ecc->p, y2, y2, E, D);  | 
87  |  |  | 
88  |  |   /* h and j stored at z' */  | 
89  | 836  |   ecc_mod_sqr (&ecc->p, z2, z1, D);  | 
90  | 836  |   ecc_mod_add (&ecc->p, z2, z2, z2);  | 
91  | 836  |   ecc_mod_sub (&ecc->p, z2, E, z2);  | 
92  | 836  |   ecc_mod_mul (&ecc->p, x2, x2, z2, D);  | 
93  | 836  |   ecc_mod_mul (&ecc->p, z2, z2, E, D);  | 
94  | 836  | }  |