Bitcoin ABC 0.33.3
P2P Digital Currency
group_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_GROUP_IMPL_H
8#define SECP256K1_GROUP_IMPL_H
9
10#include "field.h"
11#include "group.h"
12#include "util.h"
13
14/* Begin of section generated by sage/gen_exhaustive_groups.sage. */
15#define SECP256K1_G_ORDER_7 SECP256K1_GE_CONST(\
16 0x66625d13, 0x317ffe44, 0x63d32cff, 0x1ca02b9b,\
17 0xe5c6d070, 0x50b4b05e, 0x81cc30db, 0xf5166f0a,\
18 0x1e60e897, 0xa7c00c7c, 0x2df53eb6, 0x98274ff4,\
19 0x64252f42, 0x8ca44e17, 0x3b25418c, 0xff4ab0cf\
20)
21#define SECP256K1_G_ORDER_13 SECP256K1_GE_CONST(\
22 0xa2482ff8, 0x4bf34edf, 0xa51262fd, 0xe57921db,\
23 0xe0dd2cb7, 0xa5914790, 0xbc71631f, 0xc09704fb,\
24 0x942536cb, 0xa3e49492, 0x3a701cc3, 0xee3e443f,\
25 0xdf182aa9, 0x15b8aa6a, 0x166d3b19, 0xba84b045\
26)
27#define SECP256K1_G_ORDER_199 SECP256K1_GE_CONST(\
28 0x7fb07b5c, 0xd07c3bda, 0x553902e2, 0x7a87ea2c,\
29 0x35108a7f, 0x051f41e5, 0xb76abad5, 0x1f2703ad,\
30 0x0a251539, 0x5b4c4438, 0x952a634f, 0xac10dd4d,\
31 0x6d6f4745, 0x98990c27, 0x3a4f3116, 0xd32ff969\
32)
36#define SECP256K1_G SECP256K1_GE_CONST(\
37 0x79be667e, 0xf9dcbbac, 0x55a06295, 0xce870b07,\
38 0x029bfcdb, 0x2dce28d9, 0x59f2815b, 0x16f81798,\
39 0x483ada77, 0x26a3c465, 0x5da4fbfc, 0x0e1108a8,\
40 0xfd17b448, 0xa6855419, 0x9c47d08f, 0xfb10d4b8\
41)
42/* These exhaustive group test orders and generators are chosen such that:
43 * - The field size is equal to that of secp256k1, so field code is the same.
44 * - The curve equation is of the form y^2=x^3+B for some small constant B.
45 * - The subgroup has a generator 2*P, where P.x is as small as possible.
46 * - The subgroup has size less than 1000 to permit exhaustive testing.
47 * - The subgroup admits an endomorphism of the form lambda*(x,y) == (beta*x,y).
48 */
49#if defined(EXHAUSTIVE_TEST_ORDER)
50# if EXHAUSTIVE_TEST_ORDER == 7
51
53#define SECP256K1_B 6
54
55# elif EXHAUSTIVE_TEST_ORDER == 13
56
58#define SECP256K1_B 2
59
60# elif EXHAUSTIVE_TEST_ORDER == 199
61
63#define SECP256K1_B 4
64
65# else
66# error No known generator for the specified exhaustive test group order.
67# endif
68#else
69
71#define SECP256K1_B 7
72
73#endif
74/* End of section generated by sage/gen_exhaustive_groups.sage. */
75
76static void secp256k1_ge_verify(const secp256k1_ge *a) {
77#ifdef VERIFY
80 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
81#endif
82 (void)a;
83}
84
85static void secp256k1_gej_verify(const secp256k1_gej *a) {
86#ifdef VERIFY
90 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
91#endif
92 (void)a;
93}
94
95/* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
97 secp256k1_fe zi2;
98 secp256k1_fe zi3;
102 secp256k1_fe_sqr(&zi2, zi);
103 secp256k1_fe_mul(&zi3, &zi2, zi);
104 secp256k1_fe_mul(&r->x, &a->x, &zi2);
105 secp256k1_fe_mul(&r->y, &a->y, &zi3);
106 r->infinity = a->infinity;
108}
109
110/* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
112 secp256k1_fe zi2;
113 secp256k1_fe zi3;
117 secp256k1_fe_sqr(&zi2, zi);
118 secp256k1_fe_mul(&zi3, &zi2, zi);
119 secp256k1_fe_mul(&r->x, &a->x, &zi2);
120 secp256k1_fe_mul(&r->y, &a->y, &zi3);
121 r->infinity = a->infinity;
123}
124
125static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
128 r->infinity = 0;
129 r->x = *x;
130 r->y = *y;
132}
133
136 return a->infinity;
137}
138
139static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
141 *r = *a;
143 secp256k1_fe_negate(&r->y, &r->y, 1);
145}
146
148 secp256k1_fe z2, z3;
150 r->infinity = a->infinity;
151 secp256k1_fe_inv(&a->z, &a->z);
152 secp256k1_fe_sqr(&z2, &a->z);
153 secp256k1_fe_mul(&z3, &a->z, &z2);
154 secp256k1_fe_mul(&a->x, &a->x, &z2);
155 secp256k1_fe_mul(&a->y, &a->y, &z3);
156 secp256k1_fe_set_int(&a->z, 1);
157 r->x = a->x;
158 r->y = a->y;
160}
161
163 secp256k1_fe z2, z3;
167 return;
168 }
169 r->infinity = 0;
170 secp256k1_fe_inv_var(&a->z, &a->z);
171 secp256k1_fe_sqr(&z2, &a->z);
172 secp256k1_fe_mul(&z3, &a->z, &z2);
173 secp256k1_fe_mul(&a->x, &a->x, &z2);
174 secp256k1_fe_mul(&a->y, &a->y, &z3);
175 secp256k1_fe_set_int(&a->z, 1);
176 secp256k1_ge_set_xy(r, &a->x, &a->y);
178}
179
180static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
181 secp256k1_fe u;
182 size_t i;
183 size_t last_i = SIZE_MAX;
184
185 for (i = 0; i < len; i++) {
187 if (a[i].infinity) {
189 } else {
190 /* Use destination's x coordinates as scratch space */
191 if (last_i == SIZE_MAX) {
192 r[i].x = a[i].z;
193 } else {
194 secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
195 }
196 last_i = i;
197 }
198 }
199 if (last_i == SIZE_MAX) {
200 return;
201 }
202 secp256k1_fe_inv_var(&u, &r[last_i].x);
203
204 i = last_i;
205 while (i > 0) {
206 i--;
207 if (!a[i].infinity) {
208 secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
209 secp256k1_fe_mul(&u, &u, &a[last_i].z);
210 last_i = i;
211 }
212 }
213 VERIFY_CHECK(!a[last_i].infinity);
214 r[last_i].x = u;
215
216 for (i = 0; i < len; i++) {
217 if (!a[i].infinity) {
218 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
219 }
220 secp256k1_ge_verify(&r[i]);
221 }
222}
223
224static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) {
225 size_t i = len - 1;
226 secp256k1_fe zs;
227
228 if (len > 0) {
229 /* Verify inputs a[len-1] and zr[len-1]. */
230 secp256k1_ge_verify(&a[i]);
231 secp256k1_fe_verify(&zr[i]);
232 /* Ensure all y values are in weak normal form for fast negation of points */
234 zs = zr[i];
235
236 /* Work our way backwards, using the z-ratios to scale the x/y values. */
237 while (i > 0) {
238 /* Verify all inputs a[i] and zr[i]. */
239 secp256k1_fe_verify(&zr[i]);
240 secp256k1_ge_verify(&a[i]);
241 if (i != len - 1) {
242 secp256k1_fe_mul(&zs, &zs, &zr[i]);
243 }
244 i--;
245 secp256k1_ge_set_ge_zinv(&a[i], &a[i], &zs);
246 /* Verify the output a[i]. */
247 secp256k1_ge_verify(&a[i]);
248 }
249 }
250}
251
253 r->infinity = 1;
258}
259
261 r->infinity = 1;
265}
266
268 r->infinity = 0;
272}
273
275 r->infinity = 0;
278}
279
281 secp256k1_fe x2, x3;
283 r->x = *x;
284 secp256k1_fe_sqr(&x2, x);
285 secp256k1_fe_mul(&x3, x, &x2);
286 r->infinity = 0;
288 return secp256k1_fe_sqrt(&r->y, &x3);
289}
290
291static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
292 int ret = secp256k1_ge_set_xquad(r, x);
294 if (secp256k1_fe_is_odd(&r->y) != odd) {
295 secp256k1_fe_negate(&r->y, &r->y, 1);
296 }
298 return ret;
299}
300
303 r->infinity = a->infinity;
304 r->x = a->x;
305 r->y = a->y;
306 secp256k1_fe_set_int(&r->z, 1);
308}
309
310static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b) {
311 secp256k1_gej tmp;
314 secp256k1_gej_neg(&tmp, a);
315 secp256k1_gej_add_var(&tmp, &tmp, b, NULL);
316 return secp256k1_gej_is_infinity(&tmp);
317}
318
319static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
320 secp256k1_fe r;
321
322#ifdef VERIFY
324 VERIFY_CHECK(a->x.magnitude <= 31);
327#endif
328
329 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
330 return secp256k1_fe_equal_var(&r, &a->x);
331}
332
335 r->infinity = a->infinity;
336 r->x = a->x;
337 r->y = a->y;
338 r->z = a->z;
340 secp256k1_fe_negate(&r->y, &r->y, 1);
342}
343
346 return a->infinity;
347}
348
350 secp256k1_fe y2, x3;
352 if (a->infinity) {
353 return 0;
354 }
355 /* y^2 = x^3 + 7 */
356 secp256k1_fe_sqr(&y2, &a->y);
357 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
359 return secp256k1_fe_equal_var(&y2, &x3);
360}
361
363 /* Operations: 3 mul, 4 sqr, 8 add/half/mul_int/negate */
364 secp256k1_fe l, s, t;
365
367 r->infinity = a->infinity;
368
369 /* Formula used:
370 * L = (3/2) * X1^2
371 * S = Y1^2
372 * T = -X1*S
373 * X3 = L^2 + 2*T
374 * Y3 = -(L*(X3 + T) + S^2)
375 * Z3 = Y1*Z1
376 */
377
378 secp256k1_fe_mul(&r->z, &a->z, &a->y); /* Z3 = Y1*Z1 (1) */
379 secp256k1_fe_sqr(&s, &a->y); /* S = Y1^2 (1) */
380 secp256k1_fe_sqr(&l, &a->x); /* L = X1^2 (1) */
381 secp256k1_fe_mul_int(&l, 3); /* L = 3*X1^2 (3) */
382 secp256k1_fe_half(&l); /* L = 3/2*X1^2 (2) */
383 secp256k1_fe_negate(&t, &s, 1); /* T = -S (2) */
384 secp256k1_fe_mul(&t, &t, &a->x); /* T = -X1*S (1) */
385 secp256k1_fe_sqr(&r->x, &l); /* X3 = L^2 (1) */
386 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + T (2) */
387 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + 2*T (3) */
388 secp256k1_fe_sqr(&s, &s); /* S' = S^2 (1) */
389 secp256k1_fe_add(&t, &r->x); /* T' = X3 + T (4) */
390 secp256k1_fe_mul(&r->y, &t, &l); /* Y3 = L*(X3 + T) (1) */
391 secp256k1_fe_add(&r->y, &s); /* Y3 = L*(X3 + T) + S^2 (2) */
392 secp256k1_fe_negate(&r->y, &r->y, 2); /* Y3 = -(L*(X3 + T) + S^2) (3) */
394}
395
408 if (a->infinity) {
410 if (rzr != NULL) {
411 secp256k1_fe_set_int(rzr, 1);
412 }
413 return;
414 }
415
416 if (rzr != NULL) {
417 *rzr = a->y;
419 }
420
423}
424
426 /* 12 mul, 4 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
427 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, h2, h3, t;
428
431 if (a->infinity) {
432 VERIFY_CHECK(rzr == NULL);
433 *r = *b;
434 return;
435 }
436 if (b->infinity) {
437 if (rzr != NULL) {
438 secp256k1_fe_set_int(rzr, 1);
439 }
440 *r = *a;
441 return;
442 }
443
444 secp256k1_fe_sqr(&z22, &b->z);
445 secp256k1_fe_sqr(&z12, &a->z);
446 secp256k1_fe_mul(&u1, &a->x, &z22);
447 secp256k1_fe_mul(&u2, &b->x, &z12);
448 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
449 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
450 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
451 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
454 secp256k1_gej_double_var(r, a, rzr);
455 } else {
456 if (rzr != NULL) {
457 secp256k1_fe_set_int(rzr, 0);
458 }
460 }
461 return;
462 }
463
464 r->infinity = 0;
465 secp256k1_fe_mul(&t, &h, &b->z);
466 if (rzr != NULL) {
467 *rzr = t;
468 }
469 secp256k1_fe_mul(&r->z, &a->z, &t);
470
471 secp256k1_fe_sqr(&h2, &h);
472 secp256k1_fe_negate(&h2, &h2, 1);
473 secp256k1_fe_mul(&h3, &h2, &h);
474 secp256k1_fe_mul(&t, &u1, &h2);
475
476 secp256k1_fe_sqr(&r->x, &i);
477 secp256k1_fe_add(&r->x, &h3);
478 secp256k1_fe_add(&r->x, &t);
479 secp256k1_fe_add(&r->x, &t);
480
481 secp256k1_fe_add(&t, &r->x);
482 secp256k1_fe_mul(&r->y, &t, &i);
483 secp256k1_fe_mul(&h3, &h3, &s1);
484 secp256k1_fe_add(&r->y, &h3);
486}
487
489 /* 8 mul, 3 sqr, 13 add/negate/normalize_weak/normalizes_to_zero (ignoring special cases) */
490 secp256k1_fe z12, u1, u2, s1, s2, h, i, h2, h3, t;
493 if (a->infinity) {
494 VERIFY_CHECK(rzr == NULL);
496 return;
497 }
498 if (b->infinity) {
499 if (rzr != NULL) {
500 secp256k1_fe_set_int(rzr, 1);
501 }
502 *r = *a;
503 return;
504 }
505
506 secp256k1_fe_sqr(&z12, &a->z);
507 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
508 secp256k1_fe_mul(&u2, &b->x, &z12);
509 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
510 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
511 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
512 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
515 secp256k1_gej_double_var(r, a, rzr);
516 } else {
517 if (rzr != NULL) {
518 secp256k1_fe_set_int(rzr, 0);
519 }
521 }
522 return;
523 }
524
525 r->infinity = 0;
526 if (rzr != NULL) {
527 *rzr = h;
528 }
529 secp256k1_fe_mul(&r->z, &a->z, &h);
530
531 secp256k1_fe_sqr(&h2, &h);
532 secp256k1_fe_negate(&h2, &h2, 1);
533 secp256k1_fe_mul(&h3, &h2, &h);
534 secp256k1_fe_mul(&t, &u1, &h2);
535
536 secp256k1_fe_sqr(&r->x, &i);
537 secp256k1_fe_add(&r->x, &h3);
538 secp256k1_fe_add(&r->x, &t);
539 secp256k1_fe_add(&r->x, &t);
540
541 secp256k1_fe_add(&t, &r->x);
542 secp256k1_fe_mul(&r->y, &t, &i);
543 secp256k1_fe_mul(&h3, &h3, &s1);
544 secp256k1_fe_add(&r->y, &h3);
546 if (rzr != NULL) secp256k1_fe_verify(rzr);
547}
548
549static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
550 /* 9 mul, 3 sqr, 13 add/negate/normalize_weak/normalizes_to_zero (ignoring special cases) */
551 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
552
554 secp256k1_fe_verify(bzinv);
555 if (a->infinity) {
556 secp256k1_fe bzinv2, bzinv3;
557 r->infinity = b->infinity;
558 secp256k1_fe_sqr(&bzinv2, bzinv);
559 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
560 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
561 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
562 secp256k1_fe_set_int(&r->z, 1);
563 return;
564 }
565 if (b->infinity) {
566 *r = *a;
567 return;
568 }
569
578 secp256k1_fe_mul(&az, &a->z, bzinv);
579
580 secp256k1_fe_sqr(&z12, &az);
581 u1 = a->x; secp256k1_fe_normalize_weak(&u1);
582 secp256k1_fe_mul(&u2, &b->x, &z12);
583 s1 = a->y; secp256k1_fe_normalize_weak(&s1);
584 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
585 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
586 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
589 secp256k1_gej_double_var(r, a, NULL);
590 } else {
592 }
593 return;
594 }
595
596 r->infinity = 0;
597 secp256k1_fe_mul(&r->z, &a->z, &h);
598
599 secp256k1_fe_sqr(&h2, &h);
600 secp256k1_fe_negate(&h2, &h2, 1);
601 secp256k1_fe_mul(&h3, &h2, &h);
602 secp256k1_fe_mul(&t, &u1, &h2);
603
604 secp256k1_fe_sqr(&r->x, &i);
605 secp256k1_fe_add(&r->x, &h3);
606 secp256k1_fe_add(&r->x, &t);
607 secp256k1_fe_add(&r->x, &t);
608
609 secp256k1_fe_add(&t, &r->x);
610 secp256k1_fe_mul(&r->y, &t, &i);
611 secp256k1_fe_mul(&h3, &h3, &s1);
612 secp256k1_fe_add(&r->y, &h3);
614}
615
616
618 /* Operations: 7 mul, 5 sqr, 24 add/cmov/half/mul_int/negate/normalize_weak/normalizes_to_zero */
619 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
620 secp256k1_fe m_alt, rr_alt;
621 int degenerate;
625 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
626
627 /* In:
628 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
629 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
630 * we find as solution for a unified addition/doubling formula:
631 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
632 * x3 = lambda^2 - (x1 + x2)
633 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
634 *
635 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
636 * U1 = X1*Z2^2, U2 = X2*Z1^2
637 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
638 * Z = Z1*Z2
639 * T = U1+U2
640 * M = S1+S2
641 * Q = -T*M^2
642 * R = T^2-U1*U2
643 * X3 = R^2+Q
644 * Y3 = -(R*(2*X3+Q)+M^4)/2
645 * Z3 = M*Z
646 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
647 *
648 * This formula has the benefit of being the same for both addition
649 * of distinct points and doubling. However, it breaks down in the
650 * case that either point is infinity, or that y1 = -y2. We handle
651 * these cases in the following ways:
652 *
653 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
654 *
655 * - If a is infinity, we detect this, and at the end of the
656 * computation replace the result (which will be meaningless,
657 * but we compute to be constant-time) with b.x : b.y : 1.
658 *
659 * - If a = -b, we have y1 = -y2, which is a degenerate case.
660 * But here the answer is infinity, so we simply set the
661 * infinity flag of the result, overriding the computed values
662 * without even needing to cmov.
663 *
664 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
665 * properties of our curve (specifically, 1 has nontrivial cube
666 * roots in our field, and the curve equation has no x coefficient)
667 * then the answer is not infinity but also not given by the above
668 * equation. In this case, we cmov in place an alternate expression
669 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
670 * expressions for lambda are defined, they are equal, and can be
671 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
672 * then substitution of x^3 + 7 for y^2 (using the curve equation).
673 * For all pairs of nonzero points (a, b) at least one is defined,
674 * so this covers everything.
675 */
676
677 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
678 u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
679 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
680 s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
681 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
682 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
683 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
684 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
685 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
686 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
687 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
688 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
689 /* If lambda = R/M = R/0 we have a problem (except in the "trivial"
690 * case that Z = z1z2 = 0, and this is special-cased later on). */
691 degenerate = secp256k1_fe_normalizes_to_zero(&m);
692 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
693 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
694 * a nontrivial cube root of one. In either case, an alternate
695 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
696 * so we set R/M equal to this. */
697 rr_alt = s1;
698 secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
699 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
700
701 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
702 secp256k1_fe_cmov(&m_alt, &m, !degenerate);
703 /* Now Ralt / Malt = lambda and is guaranteed not to be Ralt / 0.
704 * From here on out Ralt and Malt represent the numerator
705 * and denominator of lambda; R and M represent the explicit
706 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
707 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
708 secp256k1_fe_negate(&q, &t, 2); /* q = -T (3) */
709 secp256k1_fe_mul(&q, &q, &n); /* q = Q = -T*Malt^2 (1) */
710 /* These two lines use the observation that either M == Malt or M == 0,
711 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
712 * zero (which is "computed" by cmov). So the cost is one squaring
713 * versus two multiplications. */
714 secp256k1_fe_sqr(&n, &n);
715 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
716 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
717 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Z3 = Malt*Z (1) */
718 secp256k1_fe_add(&t, &q); /* t = Ralt^2 + Q (2) */
719 r->x = t; /* r->x = X3 = Ralt^2 + Q (2) */
720 secp256k1_fe_mul_int(&t, 2); /* t = 2*X3 (4) */
721 secp256k1_fe_add(&t, &q); /* t = 2*X3 + Q (5) */
722 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*X3 + Q) (1) */
723 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*X3 + Q) + M^3*Malt (3) */
724 secp256k1_fe_negate(&r->y, &t, 3); /* r->y = -(Ralt*(2*X3 + Q) + M^3*Malt) (4) */
725 secp256k1_fe_half(&r->y); /* r->y = Y3 = -(Ralt*(2*X3 + Q) + M^3*Malt)/2 (3) */
726
727 /* In case a->infinity == 1, replace r with (b->x, b->y, 1). */
728 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
729 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
731
732 /* Set r->infinity if r->z is 0.
733 *
734 * If a->infinity is set, then r->infinity = (r->z == 0) = (1 == 0) = false,
735 * which is correct because the function assumes that b is not infinity.
736 *
737 * Now assume !a->infinity. This implies Z = Z1 != 0.
738 *
739 * Case y1 = -y2:
740 * In this case we could have a = -b, namely if x1 = x2.
741 * We have degenerate = true, r->z = (x1 - x2) * Z.
742 * Then r->infinity = ((x1 - x2)Z == 0) = (x1 == x2) = (a == -b).
743 *
744 * Case y1 != -y2:
745 * In this case, we can't have a = -b.
746 * We have degenerate = false, r->z = (y1 + y2) * Z.
747 * Then r->infinity = ((y1 + y2)Z == 0) = (y1 == -y2) = false. */
750}
751
753 /* Operations: 4 mul, 1 sqr */
754 secp256k1_fe zz;
757#ifdef VERIFY
759#endif
760 secp256k1_fe_sqr(&zz, s);
761 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
762 secp256k1_fe_mul(&r->y, &r->y, &zz);
763 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
764 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
766}
767
769 secp256k1_fe x, y;
772 x = a->x;
774 y = a->y;
776 secp256k1_fe_to_storage(&r->x, &x);
777 secp256k1_fe_to_storage(&r->y, &y);
778}
779
781 secp256k1_fe_from_storage(&r->x, &a->x);
782 secp256k1_fe_from_storage(&r->y, &a->y);
783 r->infinity = 0;
785}
786
790 secp256k1_fe_cmov(&r->x, &a->x, flag);
791 secp256k1_fe_cmov(&r->y, &a->y, flag);
792 secp256k1_fe_cmov(&r->z, &a->z, flag);
793
794 r->infinity ^= (r->infinity ^ a->infinity) & flag;
796}
797
799 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
800 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
801}
802
804 *r = *a;
808}
809
811 secp256k1_fe yz;
812
813 if (a->infinity) {
814 return 0;
815 }
816
817 /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
818 * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
819 is */
820 secp256k1_fe_mul(&yz, &a->y, &a->z);
821 return secp256k1_fe_is_quad_var(&yz);
822}
823
825#ifdef EXHAUSTIVE_TEST_ORDER
827 int i;
828
830 /* A very simple EC multiplication ladder that avoids a dependency on ecmult. */
832 for (i = 0; i < 32; ++i) {
834 if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
835 secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
836 }
837 }
839#else
840 (void)ge;
841 /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
842 return 1;
843#endif
844}
845
846#endif /* SECP256K1_GROUP_IMPL_H */
#define secp256k1_fe_cmov
Definition: field.h:96
static int secp256k1_fe_is_quad_var(const secp256k1_fe *a)
Checks whether a field element is a quadratic residue.
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal, without constant-time guarantee.
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
#define secp256k1_fe_mul_int
Definition: field.h:92
#define secp256k1_fe_negate
Definition: field.h:91
#define secp256k1_fe_normalize_weak
Definition: field.h:79
static const secp256k1_fe secp256k1_const_beta
Definition: field.h:69
static void secp256k1_fe_verify(const secp256k1_fe *a)
Check invariants on a field element (no-op unless VERIFY is enabled).
#define secp256k1_fe_is_odd
Definition: field.h:86
#define secp256k1_fe_mul
Definition: field.h:94
static const secp256k1_fe secp256k1_fe_one
Definition: field.h:68
static int secp256k1_fe_sqrt(secp256k1_fe *SECP256K1_RESTRICT r, const secp256k1_fe *SECP256K1_RESTRICT a)
Compute a square root of a field element.
#define secp256k1_fe_add
Definition: field.h:93
#define secp256k1_fe_clear
Definition: field.h:84
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_half
Definition: field.h:102
#define secp256k1_fe_to_storage
Definition: field.h:97
#define secp256k1_fe_inv_var
Definition: field.h:100
#define secp256k1_fe_from_storage
Definition: field.h:98
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
#define secp256k1_fe_inv
Definition: field.h:99
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize
Definition: field.h:78
static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
#define secp256k1_fe_add_int
Definition: field.h:103
#define secp256k1_fe_set_int
Definition: field.h:83
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Definition: group_impl.h:310
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:396
static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv)
Definition: group_impl.h:549
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:21
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:267
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:803
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:252
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:344
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:274
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:125
static void secp256k1_gej_verify(const secp256k1_gej *a)
Definition: group_impl.h:85
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:291
static void secp256k1_ge_verify(const secp256k1_ge *a)
Definition: group_impl.h:76
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Definition: group_impl.h:488
static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
Definition: group_impl.h:787
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:617
#define SECP256K1_G
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition: group_impl.h:36
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:96
#define SECP256K1_B
Definition: group_impl.h:71
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:349
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:780
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:425
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:752
static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x)
Definition: group_impl.h:280
static void secp256k1_ge_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi)
Definition: group_impl.h:111
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:319
#define SECP256K1_G_ORDER_7
Definition: group_impl.h:15
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:147
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:824
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Definition: group_impl.h:224
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:139
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:70
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:134
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Definition: group_impl.h:260
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:180
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:301
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:768
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:798
#define SECP256K1_G_ORDER_199
Definition: group_impl.h:27
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:162
static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a)
Definition: group_impl.h:810
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:333
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:362
#define SECP256K1_INLINE
Definition: util.h:48
#define VERIFY_CHECK(cond)
Definition: util.h:130
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
Definition: field_10x26.h:14
secp256k1_fe_storage x
Definition: group.h:39
secp256k1_fe_storage y
Definition: group.h:40
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
int infinity
Definition: group.h:19
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
secp256k1_fe y
Definition: group.h:30
secp256k1_fe x
Definition: group.h:29
int infinity
Definition: group.h:32
secp256k1_fe z
Definition: group.h:31
#define EXHAUSTIVE_TEST_ORDER