1143e79a0b6f45c7a2902315228388ca74917b02
[platform/upstream/nettle.git] / ecc-add-jjj.c
1 /* ecc-add-jjj.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 void
42 ecc_add_jjj (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   /* Formulas, from djb,
47      http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl:
48
49      Computation                Operation       Live variables
50
51       Z1Z1 = Z1^2               sqr             Z1Z1
52       Z2Z2 = Z2^2               sqr             Z1Z1, Z2Z2
53       U1 = X1*Z2Z2              mul             Z1Z1, Z2Z2, U1
54       U2 = X2*Z1Z1              mul             Z1Z1, Z2Z2, U1, U2
55       H = U2-U1                                 Z1Z1, Z2Z2, U1, H
56       Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2)*H sqr, mul     Z1Z1, Z2Z2, U1, H
57       S1 = Y1*Z2*Z2Z2           mul, mul        Z1Z1, U1, H, S1
58       S2 = Y2*Z1*Z1Z1           mul, mul        U1, H, S1, S2
59       W = 2*(S2-S1)     (djb: r)                U1, H, S1, W
60       I = (2*H)^2               sqr             U1, H, S1, W, I
61       J = H*I                   mul             U1, S1, W, J, V
62       V = U1*I                  mul             S1, W, J, V
63       X3 = W^2-J-2*V            sqr             S1, W, J, V
64       Y3 = W*(V-X3)-2*S1*J      mul, mul
65   */
66   mp_limb_t *z1z1 = scratch;
67   mp_limb_t *z2z2 = scratch + ecc->p.size;
68   mp_limb_t *u1   = scratch + 2*ecc->p.size;
69   mp_limb_t *u2   = scratch + 3*ecc->p.size;
70   mp_limb_t *s1   = scratch; /* overlap z1z1 */
71   mp_limb_t *s2   = scratch + ecc->p.size; /* overlap z2z2 */
72   mp_limb_t *i    = scratch + 4*ecc->p.size;
73   mp_limb_t *j    = scratch + 5*ecc->p.size;
74   mp_limb_t *v    = scratch + 6*ecc->p.size;
75
76   /* z1^2, z2^2, u1 = x1 x2^2, u2 = x2 z1^2 - u1 */
77   ecc_modp_sqr (ecc, z1z1, p + 2*ecc->p.size);
78   ecc_modp_sqr (ecc, z2z2, q + 2*ecc->p.size);
79   ecc_modp_mul (ecc, u1, p, z2z2);
80   ecc_modp_mul (ecc, u2, q, z1z1);
81   ecc_modp_sub (ecc, u2, u2, u1);  /* Store h in u2 */
82
83   /* z3, use i, j, v as scratch, result at i. */
84   ecc_modp_add (ecc, i, p + 2*ecc->p.size, q + 2*ecc->p.size);
85   ecc_modp_sqr (ecc, v, i);
86   ecc_modp_sub (ecc, v, v, z1z1);
87   ecc_modp_sub (ecc, v, v, z2z2);
88   ecc_modp_mul (ecc, i, v, u2);
89   /* Delayed write, to support in-place operation. */
90
91   /* s1 = y1 z2^3, s2 = y2 z1^3, scratch at j and v */
92   ecc_modp_mul (ecc, j, z1z1, p + 2*ecc->p.size); /* z1^3 */
93   ecc_modp_mul (ecc, v, z2z2, q + 2*ecc->p.size); /* z2^3 */
94   ecc_modp_mul (ecc, s1, p + ecc->p.size, v);
95   ecc_modp_mul (ecc, v, j, q + ecc->p.size);
96   ecc_modp_sub (ecc, s2, v, s1);
97   ecc_modp_mul_1 (ecc, s2, s2, 2);
98
99   /* Store z3 */
100   mpn_copyi (r + 2*ecc->p.size, i, ecc->p.size);
101
102   /* i, j, v */
103   ecc_modp_sqr (ecc, i, u2);
104   ecc_modp_mul_1 (ecc, i, i, 4);
105   ecc_modp_mul (ecc, j, u2, i);
106   ecc_modp_mul (ecc, v, u1, i);
107
108   /* now, u1, u2 and i are free for reuse .*/
109   /* x3, use u1, u2 as scratch */
110   ecc_modp_sqr (ecc, u1, s2);
111   ecc_modp_sub (ecc, r, u1, j);
112   ecc_modp_submul_1 (ecc, r, v, 2);
113
114   /* y3 */
115   ecc_modp_mul (ecc, u1, s1, j); /* Frees j */
116   ecc_modp_sub (ecc, u2, v, r);  /* Frees v */
117   ecc_modp_mul (ecc, i, s2, u2);
118   ecc_modp_submul_1 (ecc, i, u1, 2);
119   mpn_copyi (r + ecc->p.size, i, ecc->p.size);
120 }