]> Git Repo - secp256k1.git/blame - src/group_impl.h
Fix secp256k1_ge_set_table_gej_var parameter order
[secp256k1.git] / src / group_impl.h
CommitLineData
71712b27
GM
1/**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
0a433ea2 6
7a4b7691
PW
7#ifndef _SECP256K1_GROUP_IMPL_H_
8#define _SECP256K1_GROUP_IMPL_H_
9
11ab5622
PW
10#include "num.h"
11#include "field.h"
12#include "group.h"
607884fc 13
6efd6e77
GM
14/** Generator for secp256k1, value 'g' defined in
15 * "Standards for Efficient Cryptography" (SEC2) 2.7.1.
16 */
dd891e0e 17static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(
443cd4b8
PW
18 0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,
19 0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,
20 0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,
21 0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL
22);
4732d260 23
dd891e0e 24static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
7d893f49 25 secp256k1_fe zi2;
dd891e0e 26 secp256k1_fe zi3;
4f9791ab
PD
27 secp256k1_fe_sqr(&zi2, zi);
28 secp256k1_fe_mul(&zi3, &zi2, zi);
29 secp256k1_fe_mul(&r->x, &a->x, &zi2);
30 secp256k1_fe_mul(&r->y, &a->y, &zi3);
31 r->infinity = a->infinity;
32}
33
dd891e0e 34static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
f11ff5be
PW
35 r->infinity = 0;
36 r->x = *x;
37 r->y = *y;
607884fc
PW
38}
39
dd891e0e 40static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
f11ff5be 41 return a->infinity;
607884fc
PW
42}
43
dd891e0e 44static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
39bd94d8 45 *r = *a;
0295f0a3 46 secp256k1_fe_normalize_weak(&r->y);
39bd94d8
PW
47 secp256k1_fe_negate(&r->y, &r->y, 1);
48}
49
dd891e0e
PW
50static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a) {
51 secp256k1_fe z2, z3;
da55986f
PW
52 r->infinity = a->infinity;
53 secp256k1_fe_inv(&a->z, &a->z);
f735446c
GM
54 secp256k1_fe_sqr(&z2, &a->z);
55 secp256k1_fe_mul(&z3, &a->z, &z2);
da55986f
PW
56 secp256k1_fe_mul(&a->x, &a->x, &z2);
57 secp256k1_fe_mul(&a->y, &a->y, &z3);
58 secp256k1_fe_set_int(&a->z, 1);
59 r->x = a->x;
60 r->y = a->y;
61}
62
dd891e0e
PW
63static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a) {
64 secp256k1_fe z2, z3;
1136bedb
PW
65 r->infinity = a->infinity;
66 if (a->infinity) {
67 return;
68 }
f11ff5be 69 secp256k1_fe_inv_var(&a->z, &a->z);
f735446c
GM
70 secp256k1_fe_sqr(&z2, &a->z);
71 secp256k1_fe_mul(&z3, &a->z, &z2);
f11ff5be
PW
72 secp256k1_fe_mul(&a->x, &a->x, &z2);
73 secp256k1_fe_mul(&a->y, &a->y, &z3);
74 secp256k1_fe_set_int(&a->z, 1);
f11ff5be
PW
75 r->x = a->x;
76 r->y = a->y;
607884fc
PW
77}
78
541b7839 79static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len, const secp256k1_callback *cb) {
dd891e0e
PW
80 secp256k1_fe *az;
81 secp256k1_fe *azi;
f735446c 82 size_t i;
65a14abb 83 size_t count = 0;
dd891e0e 84 az = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * len);
f735446c 85 for (i = 0; i < len; i++) {
f16be77f
PD
86 if (!a[i].infinity) {
87 az[count++] = a[i].z;
88 }
89 }
90
dd891e0e 91 azi = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * count);
7d893f49 92 secp256k1_fe_inv_all_var(azi, az, count);
f461b769 93 free(az);
f16be77f
PD
94
95 count = 0;
f735446c 96 for (i = 0; i < len; i++) {
f16be77f
PD
97 r[i].infinity = a[i].infinity;
98 if (!a[i].infinity) {
4f9791ab 99 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &azi[count++]);
f16be77f
PD
100 }
101 }
f461b769 102 free(azi);
f16be77f
PD
103}
104
353c1bf0 105static void secp256k1_ge_set_table_gej_var(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr, size_t len) {
4f9791ab 106 size_t i = len - 1;
dd891e0e 107 secp256k1_fe zi;
4f9791ab 108
912f203f
GM
109 if (len > 0) {
110 /* Compute the inverse of the last z coordinate, and use it to compute the last affine output. */
111 secp256k1_fe_inv(&zi, &a[i].z);
4f9791ab 112 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
912f203f
GM
113
114 /* Work out way backwards, using the z-ratios to scale the x/y values. */
115 while (i > 0) {
116 secp256k1_fe_mul(&zi, &zi, &zr[i]);
117 i--;
118 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
119 }
4f9791ab
PD
120 }
121}
122
dd891e0e 123static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr) {
4f9791ab 124 size_t i = len - 1;
dd891e0e 125 secp256k1_fe zs;
4f9791ab 126
912f203f
GM
127 if (len > 0) {
128 /* The z of the final point gives us the "global Z" for the table. */
129 r[i].x = a[i].x;
130 r[i].y = a[i].y;
131 *globalz = a[i].z;
132 r[i].infinity = 0;
133 zs = zr[i];
134
135 /* Work our way backwards, using the z-ratios to scale the x/y values. */
136 while (i > 0) {
137 if (i != len - 1) {
138 secp256k1_fe_mul(&zs, &zs, &zr[i]);
139 }
140 i--;
141 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
4f9791ab 142 }
4f9791ab
PD
143 }
144}
145
dd891e0e 146static void secp256k1_gej_set_infinity(secp256k1_gej *r) {
f11ff5be 147 r->infinity = 1;
9338dbf7
PW
148 secp256k1_fe_set_int(&r->x, 0);
149 secp256k1_fe_set_int(&r->y, 0);
150 secp256k1_fe_set_int(&r->z, 0);
607884fc
PW
151}
152
dd891e0e 153static void secp256k1_gej_clear(secp256k1_gej *r) {
2f6c8019
GM
154 r->infinity = 0;
155 secp256k1_fe_clear(&r->x);
156 secp256k1_fe_clear(&r->y);
157 secp256k1_fe_clear(&r->z);
158}
159
dd891e0e 160static void secp256k1_ge_clear(secp256k1_ge *r) {
2f6c8019
GM
161 r->infinity = 0;
162 secp256k1_fe_clear(&r->x);
163 secp256k1_fe_clear(&r->y);
164}
165
926836ad 166static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x) {
dd891e0e 167 secp256k1_fe x2, x3, c;
f11ff5be 168 r->x = *x;
f735446c
GM
169 secp256k1_fe_sqr(&x2, x);
170 secp256k1_fe_mul(&x3, x, &x2);
eb0be8ee 171 r->infinity = 0;
f735446c 172 secp256k1_fe_set_int(&c, 7);
f11ff5be 173 secp256k1_fe_add(&c, &x3);
926836ad 174 return secp256k1_fe_sqrt(&r->y, &c);
64666251
PW
175}
176
177static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
926836ad 178 if (!secp256k1_ge_set_xquad(r, x)) {
09ca4f32 179 return 0;
26320197 180 }
39bd94d8 181 secp256k1_fe_normalize_var(&r->y);
26320197 182 if (secp256k1_fe_is_odd(&r->y) != odd) {
f11ff5be 183 secp256k1_fe_negate(&r->y, &r->y, 1);
26320197 184 }
09ca4f32 185 return 1;
64666251 186
910d0de4 187}
607884fc 188
dd891e0e 189static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a) {
f11ff5be
PW
190 r->infinity = a->infinity;
191 r->x = a->x;
192 r->y = a->y;
193 secp256k1_fe_set_int(&r->z, 1);
910d0de4 194}
607884fc 195
dd891e0e
PW
196static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
197 secp256k1_fe r, r2;
ce7eb6fb 198 VERIFY_CHECK(!a->infinity);
f735446c
GM
199 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
200 r2 = a->x; secp256k1_fe_normalize_weak(&r2);
d7174edf 201 return secp256k1_fe_equal_var(&r, &r2);
910d0de4 202}
607884fc 203
dd891e0e 204static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
f11ff5be
PW
205 r->infinity = a->infinity;
206 r->x = a->x;
207 r->y = a->y;
208 r->z = a->z;
0295f0a3 209 secp256k1_fe_normalize_weak(&r->y);
f11ff5be 210 secp256k1_fe_negate(&r->y, &r->y, 1);
607884fc
PW
211}
212
dd891e0e 213static int secp256k1_gej_is_infinity(const secp256k1_gej *a) {
f11ff5be 214 return a->infinity;
0a07e62f
PW
215}
216
dd891e0e
PW
217static int secp256k1_gej_is_valid_var(const secp256k1_gej *a) {
218 secp256k1_fe y2, x3, z2, z6;
26320197 219 if (a->infinity) {
eb0be8ee 220 return 0;
26320197 221 }
71712b27
GM
222 /** y^2 = x^3 + 7
223 * (Y/Z^3)^2 = (X/Z^2)^3 + 7
224 * Y^2 / Z^6 = X^3 / Z^6 + 7
225 * Y^2 = X^3 + 7*Z^6
226 */
f735446c
GM
227 secp256k1_fe_sqr(&y2, &a->y);
228 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
229 secp256k1_fe_sqr(&z2, &a->z);
230 secp256k1_fe_sqr(&z6, &z2); secp256k1_fe_mul(&z6, &z6, &z2);
910d0de4
PW
231 secp256k1_fe_mul_int(&z6, 7);
232 secp256k1_fe_add(&x3, &z6);
d7174edf
PW
233 secp256k1_fe_normalize_weak(&x3);
234 return secp256k1_fe_equal_var(&y2, &x3);
607884fc
PW
235}
236
dd891e0e
PW
237static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
238 secp256k1_fe y2, x3, c;
26320197 239 if (a->infinity) {
764332d0 240 return 0;
26320197 241 }
71712b27 242 /* y^2 = x^3 + 7 */
f735446c
GM
243 secp256k1_fe_sqr(&y2, &a->y);
244 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
245 secp256k1_fe_set_int(&c, 7);
764332d0 246 secp256k1_fe_add(&x3, &c);
d7174edf
PW
247 secp256k1_fe_normalize_weak(&x3);
248 return secp256k1_fe_equal_var(&y2, &x3);
764332d0
PW
249}
250
dd891e0e 251static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
8ec49d8a
AP
252 /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
253 *
254 * Note that there is an implementation described at
255 * https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
256 * which trades a multiply for a square, but in practice this is actually slower,
257 * mainly because it requires more normalizations.
258 */
dd891e0e 259 secp256k1_fe t1,t2,t3,t4;
3627437d
GM
260 /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
261 * Q must equal -Q, or that Q.y == -(Q.y), or Q.y is 0. For a point on y^2 = x^3 + 7 to have
262 * y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
7d893f49 263 *
e72e93ad
AP
264 * Having said this, if this function receives a point on a sextic twist, e.g. by
265 * a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
266 * since -6 does have a cube root mod p. For this point, this function will not set
267 * the infinity flag even though the point doubles to infinity, and the result
268 * point will be gibberish (z = 0 but infinity = 0).
3627437d 269 */
f7dc1c65
PW
270 r->infinity = a->infinity;
271 if (r->infinity) {
2b199de8 272 if (rzr != NULL) {
4f9791ab
PD
273 secp256k1_fe_set_int(rzr, 1);
274 }
607884fc
PW
275 return;
276 }
277
2b199de8 278 if (rzr != NULL) {
4f9791ab
PD
279 *rzr = a->y;
280 secp256k1_fe_normalize_weak(rzr);
281 secp256k1_fe_mul_int(rzr, 2);
282 }
283
be82e92f 284 secp256k1_fe_mul(&r->z, &a->z, &a->y);
71712b27 285 secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
f11ff5be 286 secp256k1_fe_sqr(&t1, &a->x);
71712b27
GM
287 secp256k1_fe_mul_int(&t1, 3); /* T1 = 3*X^2 (3) */
288 secp256k1_fe_sqr(&t2, &t1); /* T2 = 9*X^4 (1) */
f7dc1c65 289 secp256k1_fe_sqr(&t3, &a->y);
71712b27 290 secp256k1_fe_mul_int(&t3, 2); /* T3 = 2*Y^2 (2) */
910d0de4 291 secp256k1_fe_sqr(&t4, &t3);
71712b27 292 secp256k1_fe_mul_int(&t4, 2); /* T4 = 8*Y^4 (2) */
be82e92f 293 secp256k1_fe_mul(&t3, &t3, &a->x); /* T3 = 2*X*Y^2 (1) */
f11ff5be 294 r->x = t3;
71712b27
GM
295 secp256k1_fe_mul_int(&r->x, 4); /* X' = 8*X*Y^2 (4) */
296 secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
297 secp256k1_fe_add(&r->x, &t2); /* X' = 9*X^4 - 8*X*Y^2 (6) */
298 secp256k1_fe_negate(&t2, &t2, 1); /* T2 = -9*X^4 (2) */
299 secp256k1_fe_mul_int(&t3, 6); /* T3 = 12*X*Y^2 (6) */
300 secp256k1_fe_add(&t3, &t2); /* T3 = 12*X*Y^2 - 9*X^4 (8) */
301 secp256k1_fe_mul(&r->y, &t1, &t3); /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
302 secp256k1_fe_negate(&t2, &t4, 2); /* T2 = -8*Y^4 (3) */
303 secp256k1_fe_add(&r->y, &t2); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
607884fc
PW
304}
305
dd891e0e 306static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr) {
44015000
AP
307 VERIFY_CHECK(!secp256k1_gej_is_infinity(a));
308 secp256k1_gej_double_var(r, a, rzr);
309}
310
dd891e0e 311static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr) {
d61e8995 312 /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
dd891e0e 313 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
4f9791ab 314
f11ff5be 315 if (a->infinity) {
4f9791ab 316 VERIFY_CHECK(rzr == NULL);
f11ff5be 317 *r = *b;
607884fc
PW
318 return;
319 }
4f9791ab 320
f11ff5be 321 if (b->infinity) {
2b199de8 322 if (rzr != NULL) {
4f9791ab
PD
323 secp256k1_fe_set_int(rzr, 1);
324 }
f11ff5be 325 *r = *a;
607884fc
PW
326 return;
327 }
4f9791ab 328
eb0be8ee 329 r->infinity = 0;
f735446c
GM
330 secp256k1_fe_sqr(&z22, &b->z);
331 secp256k1_fe_sqr(&z12, &a->z);
332 secp256k1_fe_mul(&u1, &a->x, &z22);
333 secp256k1_fe_mul(&u2, &b->x, &z12);
334 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
335 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
336 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
337 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
49ee0dbe
PD
338 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
339 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
4f9791ab 340 secp256k1_gej_double_var(r, a, rzr);
607884fc 341 } else {
2b199de8 342 if (rzr != NULL) {
4f9791ab
PD
343 secp256k1_fe_set_int(rzr, 0);
344 }
eb0be8ee 345 r->infinity = 1;
607884fc
PW
346 }
347 return;
348 }
f735446c
GM
349 secp256k1_fe_sqr(&i2, &i);
350 secp256k1_fe_sqr(&h2, &h);
351 secp256k1_fe_mul(&h3, &h, &h2);
4f9791ab 352 secp256k1_fe_mul(&h, &h, &b->z);
2b199de8 353 if (rzr != NULL) {
4f9791ab
PD
354 *rzr = h;
355 }
356 secp256k1_fe_mul(&r->z, &a->z, &h);
f735446c 357 secp256k1_fe_mul(&t, &u1, &h2);
f11ff5be
PW
358 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
359 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
910d0de4 360 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
f11ff5be 361 secp256k1_fe_add(&r->y, &h3);
607884fc
PW
362}
363
dd891e0e 364static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr) {
d61e8995 365 /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
dd891e0e 366 secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
f11ff5be 367 if (a->infinity) {
2d5a186c
PD
368 VERIFY_CHECK(rzr == NULL);
369 secp256k1_gej_set_ge(r, b);
607884fc
PW
370 return;
371 }
f11ff5be 372 if (b->infinity) {
2b199de8 373 if (rzr != NULL) {
2d5a186c
PD
374 secp256k1_fe_set_int(rzr, 1);
375 }
f11ff5be 376 *r = *a;
607884fc
PW
377 return;
378 }
eb0be8ee 379 r->infinity = 0;
4f9791ab 380
f735446c
GM
381 secp256k1_fe_sqr(&z12, &a->z);
382 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
383 secp256k1_fe_mul(&u2, &b->x, &z12);
384 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
385 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
386 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
387 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
49ee0dbe
PD
388 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
389 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
2d5a186c 390 secp256k1_gej_double_var(r, a, rzr);
4f9791ab 391 } else {
2b199de8 392 if (rzr != NULL) {
2d5a186c
PD
393 secp256k1_fe_set_int(rzr, 0);
394 }
4f9791ab
PD
395 r->infinity = 1;
396 }
397 return;
398 }
399 secp256k1_fe_sqr(&i2, &i);
400 secp256k1_fe_sqr(&h2, &h);
401 secp256k1_fe_mul(&h3, &h, &h2);
2b199de8 402 if (rzr != NULL) {
2d5a186c
PD
403 *rzr = h;
404 }
405 secp256k1_fe_mul(&r->z, &a->z, &h);
4f9791ab
PD
406 secp256k1_fe_mul(&t, &u1, &h2);
407 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
408 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
409 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
410 secp256k1_fe_add(&r->y, &h3);
411}
412
dd891e0e 413static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
4f9791ab 414 /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
dd891e0e 415 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
4f9791ab
PD
416
417 if (b->infinity) {
418 *r = *a;
419 return;
420 }
421 if (a->infinity) {
dd891e0e 422 secp256k1_fe bzinv2, bzinv3;
4f9791ab
PD
423 r->infinity = b->infinity;
424 secp256k1_fe_sqr(&bzinv2, bzinv);
425 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
426 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
427 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
428 secp256k1_fe_set_int(&r->z, 1);
429 return;
430 }
431 r->infinity = 0;
432
433 /** We need to calculate (rx,ry,rz) = (ax,ay,az) + (bx,by,1/bzinv). Due to
434 * secp256k1's isomorphism we can multiply the Z coordinates on both sides
435 * by bzinv, and get: (rx,ry,rz*bzinv) = (ax,ay,az*bzinv) + (bx,by,1).
436 * This means that (rx,ry,rz) can be calculated as
437 * (ax,ay,az*bzinv) + (bx,by,1), when not applying the bzinv factor to rz.
438 * The variable az below holds the modified Z coordinate for a, which is used
439 * for the computation of rx and ry, but not for rz.
440 */
441 secp256k1_fe_mul(&az, &a->z, bzinv);
442
443 secp256k1_fe_sqr(&z12, &az);
444 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
445 secp256k1_fe_mul(&u2, &b->x, &z12);
446 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
447 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
448 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
449 secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
450 if (secp256k1_fe_normalizes_to_zero_var(&h)) {
451 if (secp256k1_fe_normalizes_to_zero_var(&i)) {
452 secp256k1_gej_double_var(r, a, NULL);
607884fc 453 } else {
eb0be8ee 454 r->infinity = 1;
607884fc
PW
455 }
456 return;
457 }
f735446c
GM
458 secp256k1_fe_sqr(&i2, &i);
459 secp256k1_fe_sqr(&h2, &h);
460 secp256k1_fe_mul(&h3, &h, &h2);
f11ff5be 461 r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
f735446c 462 secp256k1_fe_mul(&t, &u1, &h2);
f11ff5be
PW
463 r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
464 secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
910d0de4 465 secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
f11ff5be 466 secp256k1_fe_add(&r->y, &h3);
607884fc
PW
467}
468
4f9791ab 469
dd891e0e 470static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
5a43124c 471 /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
dd891e0e
PW
472 static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
473 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
474 secp256k1_fe m_alt, rr_alt;
5de4c5df 475 int infinity, degenerate;
9338dbf7
PW
476 VERIFY_CHECK(!b->infinity);
477 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
478
71712b27
GM
479 /** In:
480 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
481 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
482 * we find as solution for a unified addition/doubling formula:
483 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
484 * x3 = lambda^2 - (x1 + x2)
485 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
486 *
487 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
488 * U1 = X1*Z2^2, U2 = X2*Z1^2
2a54f9bc 489 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
71712b27
GM
490 * Z = Z1*Z2
491 * T = U1+U2
492 * M = S1+S2
493 * Q = T*M^2
494 * R = T^2-U1*U2
495 * X3 = 4*(R^2-Q)
496 * Y3 = 4*(R*(3*Q-2*R^2)-M^4)
497 * Z3 = 2*M*Z
498 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
5de4c5df
AP
499 *
500 * This formula has the benefit of being the same for both addition
501 * of distinct points and doubling. However, it breaks down in the
502 * case that either point is infinity, or that y1 = -y2. We handle
503 * these cases in the following ways:
504 *
505 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
506 *
507 * - If a is infinity, we detect this, and at the end of the
508 * computation replace the result (which will be meaningless,
509 * but we compute to be constant-time) with b.x : b.y : 1.
510 *
511 * - If a = -b, we have y1 = -y2, which is a degenerate case.
512 * But here the answer is infinity, so we simply set the
513 * infinity flag of the result, overriding the computed values
514 * without even needing to cmov.
515 *
516 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
517 * properties of our curve (specifically, 1 has nontrivial cube
518 * roots in our field, and the curve equation has no x coefficient)
519 * then the answer is not infinity but also not given by the above
520 * equation. In this case, we cmov in place an alternate expression
521 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
522 * expressions for lambda are defined, they are equal, and can be
523 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
524 * then substitution of x^3 + 7 for y^2 (using the curve equation).
525 * For all pairs of nonzero points (a, b) at least one is defined,
526 * so this covers everything.
71712b27
GM
527 */
528
f735446c
GM
529 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
530 u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
531 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
532 s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
81e45ff9 533 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
f735446c 534 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
f735446c
GM
535 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
536 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
bcf2fcfd 537 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
a5d796e0 538 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
7d054cd0
PD
539 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
540 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
5de4c5df
AP
541 /** If lambda = R/M = 0/0 we have a problem (except in the "trivial"
542 * case that Z = z1z2 = 0, and this is special-cased later on). */
543 degenerate = secp256k1_fe_normalizes_to_zero(&m) &
544 secp256k1_fe_normalizes_to_zero(&rr);
545 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
546 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
547 * a nontrivial cube root of one. In either case, an alternate
548 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
549 * so we set R/M equal to this. */
5a43124c
PD
550 rr_alt = s1;
551 secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
a5d796e0 552 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
5de4c5df
AP
553
554 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
555 secp256k1_fe_cmov(&m_alt, &m, !degenerate);
556 /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
557 * From here on out Ralt and Malt represent the numerator
558 * and denominator of lambda; R and M represent the explicit
559 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
560 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
561 secp256k1_fe_mul(&q, &n, &t); /* q = Q = T*Malt^2 (1) */
562 /* These two lines use the observation that either M == Malt or M == 0,
563 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
564 * zero (which is "computed" by cmov). So the cost is one squaring
565 * versus two multiplications. */
55e7fc32
PD
566 secp256k1_fe_sqr(&n, &n);
567 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
5de4c5df 568 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
b28d02a5 569 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Malt*Z (1) */
f735446c 570 infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
5de4c5df 571 secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
71712b27 572 secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
55e7fc32
PD
573 secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
574 secp256k1_fe_normalize_weak(&t);
575 r->x = t; /* r->x = Ralt^2-Q (1) */
bcf2fcfd 576 secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
55e7fc32 577 secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
5de4c5df 578 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
55e7fc32
PD
579 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
580 secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
0295f0a3 581 secp256k1_fe_normalize_weak(&r->y);
5de4c5df
AP
582 secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
583 secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
9338dbf7 584
a1d5ae15 585 /** In case a->infinity == 1, replace r with (b->x, b->y, 1). */
bb0ea50d
GM
586 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
587 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
588 secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
9338dbf7
PW
589 r->infinity = infinity;
590}
591
dd891e0e 592static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s) {
d2275795 593 /* Operations: 4 mul, 1 sqr */
dd891e0e 594 secp256k1_fe zz;
d2275795
GM
595 VERIFY_CHECK(!secp256k1_fe_is_zero(s));
596 secp256k1_fe_sqr(&zz, s);
597 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
598 secp256k1_fe_mul(&r->y, &r->y, &zz);
599 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
600 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
601}
602
dd891e0e
PW
603static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a) {
604 secp256k1_fe x, y;
e68d7208
PW
605 VERIFY_CHECK(!a->infinity);
606 x = a->x;
607 secp256k1_fe_normalize(&x);
608 y = a->y;
609 secp256k1_fe_normalize(&y);
610 secp256k1_fe_to_storage(&r->x, &x);
611 secp256k1_fe_to_storage(&r->y, &y);
612}
613
dd891e0e 614static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a) {
e68d7208
PW
615 secp256k1_fe_from_storage(&r->x, &a->x);
616 secp256k1_fe_from_storage(&r->y, &a->y);
617 r->infinity = 0;
618}
619
dd891e0e 620static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag) {
55422b6a
PW
621 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
622 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
623}
624
399c03f2 625#ifdef USE_ENDOMORPHISM
dd891e0e
PW
626static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a) {
627 static const secp256k1_fe beta = SECP256K1_FE_CONST(
4732d260
PW
628 0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
629 0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
630 );
f11ff5be 631 *r = *a;
4732d260 632 secp256k1_fe_mul(&r->x, &r->x, &beta);
607884fc 633}
399c03f2 634#endif
607884fc 635
e6e9805f
PW
636static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a) {
637 secp256k1_fe yz;
638
639 if (a->infinity) {
640 return 0;
641 }
642
643 /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
644 * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
645 is */
646 secp256k1_fe_mul(&yz, &a->y, &a->z);
647 return secp256k1_fe_is_quad_var(&yz);
648}
649
7a4b7691 650#endif
This page took 0.144365 seconds and 4 git commands to generate.