Line | Count | Source (jump to first uncovered line) |
1 | | /* ecc-dup-jj.c |
2 | | |
3 | | Copyright (C) 2013 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 | | /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */ |
33 | | |
34 | | #if HAVE_CONFIG_H |
35 | | # include "config.h" |
36 | | #endif |
37 | | |
38 | | #include "ecc.h" |
39 | | #include "ecc-internal.h" |
40 | | |
41 | | /* NOTE: Behaviour for corner cases: |
42 | | |
43 | | + p = 0 ==> r = 0, correct! |
44 | | */ |
45 | | void |
46 | | ecc_dup_jj (const struct ecc_curve *ecc, |
47 | | mp_limb_t *r, const mp_limb_t *p, |
48 | | mp_limb_t *scratch) |
49 | 0 | { |
50 | 0 | #define x1 p |
51 | 0 | #define y1 (p + ecc->p.size) |
52 | 0 | #define z1 (p + 2*ecc->p.size) |
53 | |
|
54 | 0 | #define x2 r |
55 | 0 | #define y2 (r + ecc->p.size) |
56 | 0 | #define z2 (r + 2*ecc->p.size) |
57 | | |
58 | | /* Formulas (from djb, |
59 | | http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b): |
60 | | |
61 | | Computation Operation Live variables |
62 | | delta = z^2 sqr delta |
63 | | gamma = y^2 sqr delta, gamma |
64 | | z' = (y+z)^2-gamma-delta sqr delta, gamma |
65 | | alpha = 3*(x-delta)*(x+delta) mul gamma, beta, alpha |
66 | | beta = x*gamma mul gamma, beta, alpha |
67 | | x' = alpha^2-8*beta sqr gamma, beta, alpha |
68 | | y' = alpha*(4*beta-x')-8*gamma^2 mul, sqr |
69 | | */ |
70 | |
|
71 | 0 | #define gamma scratch |
72 | 0 | #define delta (scratch + ecc->p.size) |
73 | 0 | #define alpha delta |
74 | |
|
75 | 0 | #define beta (scratch + 2*ecc->p.size) |
76 | 0 | #define sum (scratch + 3*ecc->p.size) |
77 | |
|
78 | 0 | ecc_mod_sqr (&ecc->p, gamma, y1, gamma); /* x, y, z, gamma */ |
79 | 0 | ecc_mod_sqr (&ecc->p, delta, z1, delta); /* x, y, z, gamma, delta */ |
80 | |
|
81 | 0 | ecc_mod_add (&ecc->p, sum, z1, y1); /* x, gamma, delta, s */ |
82 | 0 | ecc_mod_sqr (&ecc->p, sum, sum, y2); /* Can use y-z as scratch */ |
83 | 0 | ecc_mod_sub (&ecc->p, z2, sum, delta); /* x, z, gamma, delta */ |
84 | 0 | ecc_mod_sub (&ecc->p, z2, z2, gamma); |
85 | |
|
86 | 0 | ecc_mod_mul (&ecc->p, beta, x1, gamma, beta); /* x, z, gamma, delta, beta */ |
87 | |
|
88 | 0 | ecc_mod_add (&ecc->p, sum, x1, delta); /* x, sum, z', gamma, delta, beta */ |
89 | 0 | ecc_mod_sub (&ecc->p, delta, x1, delta); /* sum, z', gamma, delta, beta */ |
90 | | /* This multiplication peaks the storage need; can use x-y for scratch. */ |
91 | 0 | ecc_mod_mul (&ecc->p, alpha, sum, delta, x2); /* z', gamma, alpha, beta */ |
92 | 0 | ecc_mod_mul_1 (&ecc->p, alpha, alpha, 3); |
93 | |
|
94 | 0 | ecc_mod_mul_1 (&ecc->p, y2, beta, 4); |
95 | | |
96 | | /* From now on, can use beta as scratch. */ |
97 | 0 | ecc_mod_sqr (&ecc->p, x2, alpha, beta); /* alpha^2 */ |
98 | 0 | ecc_mod_submul_1 (&ecc->p, x2, y2, 2); /* alpha^2 - 8 beta */ |
99 | |
|
100 | 0 | ecc_mod_sub (&ecc->p, y2, y2, x2); /* 4 beta - x' */ |
101 | 0 | ecc_mod_mul (&ecc->p, y2, y2, alpha, beta); |
102 | 0 | ecc_mod_sqr (&ecc->p, gamma, gamma, beta); |
103 | 0 | ecc_mod_submul_1 (&ecc->p, y2, gamma, 8); |
104 | 0 | } |