/src/nettle-with-mini-gmp/ecc-add-th.c
Line  | Count  | Source  | 
1  |  | /* ecc-add-th.c  | 
2  |  |  | 
3  |  |    Copyright (C) 2014, 2017 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 a twisted Edwards curve, with result and first point in  | 
40  |  |    homogeneous coordinates. */  | 
41  |  | void  | 
42  |  | ecc_add_th (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  | 9.19k  | { | 
46  | 18.3k  | #define x1 p  | 
47  | 18.3k  | #define y1 (p + ecc->p.size)  | 
48  | 27.5k  | #define z1 (p + 2*ecc->p.size)  | 
49  |  |  | 
50  | 18.3k  | #define x2 q  | 
51  | 18.3k  | #define y2 (q + ecc->p.size)  | 
52  |  |  | 
53  | 101k  | #define x3 r  | 
54  | 36.7k  | #define y3 (r + ecc->p.size)  | 
55  | 9.19k  | #define z3 (r + 2*ecc->p.size)  | 
56  |  |  | 
57  |  |   /* Formulas (from djb,  | 
58  |  |      http://www.hyperelliptic.org/EFD/g1p/auto-twisted-projective.html#addition-madd-2008-bbjlp  | 
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) mul    C, D, T  | 
65  |  |          - C - D  | 
66  |  |      E = b*C*D          2 mul   C, E, T  (Replace C <-- D+C)  | 
67  |  |      B = z1^2   sqr   B, C, E, T  | 
68  |  |      F = B - E        B, C, E, F, T  | 
69  |  |      G = B + E          C, F, G, T  | 
70  |  |      x3 = z1 * F * T    2 mul   C, F, G, T  | 
71  |  |      y3 = z1*G*(D+C)  2 mul   F, G  | 
72  |  |      z3 = F*G   mul  | 
73  |  |  | 
74  |  |      10M + 1S  | 
75  |  |  | 
76  |  |      We have different sign for E, hence swapping F and G, because our  | 
77  |  |      ecc->b corresponds to -b above.  | 
78  |  |   */  | 
79  | 101k  | #define T scratch  | 
80  | 73.5k  | #define E (scratch + 1*ecc->p.size)  | 
81  | 27.5k  | #define F E  | 
82  | 91.9k  | #define C (scratch + 2*ecc->p.size)  | 
83  | 64.3k  | #define D (scratch + 3*ecc->p.size)  | 
84  | 27.5k  | #define B D  | 
85  |  |  | 
86  |  |   /* Use T as scratch, clobber E */  | 
87  | 9.19k  |   ecc_mod_mul (&ecc->p, C, x1, x2, T);  /* C */  | 
88  | 9.19k  |   ecc_mod_mul (&ecc->p, D, y1, y2, T);  /* C, D */  | 
89  | 9.19k  |   ecc_mod_add (&ecc->p, x3, x1, y1);  | 
90  | 9.19k  |   ecc_mod_add (&ecc->p, y3, x2, y2);  | 
91  | 9.19k  |   ecc_mod_mul (&ecc->p, T, x3, y3, T);  /* C, D, T */  | 
92  | 9.19k  |   ecc_mod_sub (&ecc->p, T, T, C);  | 
93  | 9.19k  |   ecc_mod_sub (&ecc->p, T, T, D);  | 
94  |  |   /* Can now use x3 as scratch, without breaking in-place operation. */  | 
95  | 9.19k  |   ecc_mod_mul (&ecc->p, T, T, z1, x3);  | 
96  |  |  | 
97  | 9.19k  |   ecc_mod_mul (&ecc->p, E, C, D, x3);  /* C, D, T, E */  | 
98  | 9.19k  |   ecc_mod_mul (&ecc->p, E, E, ecc->b, x3);  | 
99  |  |  | 
100  | 9.19k  |   ecc_mod_add (&ecc->p, C, D, C);  /* C, T, E */  | 
101  | 9.19k  |   ecc_mod_mul (&ecc->p, C, C, z1, x3);  | 
102  |  |  | 
103  | 9.19k  |   ecc_mod_sqr (&ecc->p, B, z1, x3);  /* C, T, E, B */  | 
104  | 9.19k  |   ecc_mod_add (&ecc->p, x3, B, E);  | 
105  | 9.19k  |   ecc_mod_sub (&ecc->p, F, B, E);  /* C, T, F */  | 
106  |  |  | 
107  |  |   /* Can now use y3 as scratch, without breaking in-place operation. */  | 
108  | 9.19k  |   ecc_mod_mul (&ecc->p, y3, C, F, y3);  /* T G */  | 
109  |  |  | 
110  |  |   /* Can use C--D as scratch */  | 
111  | 9.19k  |   ecc_mod_mul (&ecc->p, z3, x3, F, C);  /* T */  | 
112  | 9.19k  |   ecc_mod_mul (&ecc->p, x3, x3, T, C);  | 
113  | 9.19k  | }  |