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