Bitcoin ABC 0.33.6
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) {
81 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
82 (void)a;
83}
84
85static void secp256k1_gej_verify(const secp256k1_gej *a) {
92 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
93 (void)a;
94}
95
96/* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
98 secp256k1_fe zi2;
99 secp256k1_fe zi3;
103
104 secp256k1_fe_sqr(&zi2, zi);
105 secp256k1_fe_mul(&zi3, &zi2, zi);
106 secp256k1_fe_mul(&r->x, &a->x, &zi2);
107 secp256k1_fe_mul(&r->y, &a->y, &zi3);
108 r->infinity = a->infinity;
109
111}
112
113/* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
115 secp256k1_fe zi2;
116 secp256k1_fe zi3;
120
121 secp256k1_fe_sqr(&zi2, zi);
122 secp256k1_fe_mul(&zi3, &zi2, zi);
123 secp256k1_fe_mul(&r->x, &a->x, &zi2);
124 secp256k1_fe_mul(&r->y, &a->y, &zi3);
125 r->infinity = a->infinity;
126
128}
129
130static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
133
134 r->infinity = 0;
135 r->x = *x;
136 r->y = *y;
137
139}
140
143
144 return a->infinity;
145}
146
147static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
149
150 *r = *a;
152 secp256k1_fe_negate(&r->y, &r->y, 1);
153
155}
156
158 secp256k1_fe z2, z3;
160
161 r->infinity = a->infinity;
162 secp256k1_fe_inv(&a->z, &a->z);
163 secp256k1_fe_sqr(&z2, &a->z);
164 secp256k1_fe_mul(&z3, &a->z, &z2);
165 secp256k1_fe_mul(&a->x, &a->x, &z2);
166 secp256k1_fe_mul(&a->y, &a->y, &z3);
167 secp256k1_fe_set_int(&a->z, 1);
168 r->x = a->x;
169 r->y = a->y;
170
173}
174
176 secp256k1_fe z2, z3;
178
181 return;
182 }
183 r->infinity = 0;
184 secp256k1_fe_inv_var(&a->z, &a->z);
185 secp256k1_fe_sqr(&z2, &a->z);
186 secp256k1_fe_mul(&z3, &a->z, &z2);
187 secp256k1_fe_mul(&a->x, &a->x, &z2);
188 secp256k1_fe_mul(&a->y, &a->y, &z3);
189 secp256k1_fe_set_int(&a->z, 1);
190 secp256k1_ge_set_xy(r, &a->x, &a->y);
191
194}
195
196static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
197 secp256k1_fe u;
198 size_t i;
199 size_t last_i = SIZE_MAX;
200#ifdef VERIFY
201 for (i = 0; i < len; i++) {
203 }
204#endif
205
206 for (i = 0; i < len; i++) {
207 if (a[i].infinity) {
209 } else {
210 /* Use destination's x coordinates as scratch space */
211 if (last_i == SIZE_MAX) {
212 r[i].x = a[i].z;
213 } else {
214 secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
215 }
216 last_i = i;
217 }
218 }
219 if (last_i == SIZE_MAX) {
220 return;
221 }
222 secp256k1_fe_inv_var(&u, &r[last_i].x);
223
224 i = last_i;
225 while (i > 0) {
226 i--;
227 if (!a[i].infinity) {
228 secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
229 secp256k1_fe_mul(&u, &u, &a[last_i].z);
230 last_i = i;
231 }
232 }
233 VERIFY_CHECK(!a[last_i].infinity);
234 r[last_i].x = u;
235
236 for (i = 0; i < len; i++) {
237 if (!a[i].infinity) {
238 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
239 }
240 }
241
242#ifdef VERIFY
243 for (i = 0; i < len; i++) {
244 SECP256K1_GE_VERIFY(&r[i]);
245 }
246#endif
247}
248
249static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) {
250 size_t i;
251 secp256k1_fe zs;
252#ifdef VERIFY
253 for (i = 0; i < len; i++) {
254 SECP256K1_GE_VERIFY(&a[i]);
255 SECP256K1_FE_VERIFY(&zr[i]);
256 }
257#endif
258
259 if (len > 0) {
260 i = len - 1;
261 /* Ensure all y values are in weak normal form for fast negation of points */
263 zs = zr[i];
264
265 /* Work our way backwards, using the z-ratios to scale the x/y values. */
266 while (i > 0) {
267 if (i != len - 1) {
268 secp256k1_fe_mul(&zs, &zs, &zr[i]);
269 }
270 i--;
271 secp256k1_ge_set_ge_zinv(&a[i], &a[i], &zs);
272 }
273 }
274
275#ifdef VERIFY
276 for (i = 0; i < len; i++) {
277 SECP256K1_GE_VERIFY(&a[i]);
278 }
279#endif
280}
281
283 r->infinity = 1;
287
289}
290
292 r->infinity = 1;
295
297}
298
300 r->infinity = 0;
304
306}
307
309 r->infinity = 0;
312
314}
315
317 secp256k1_fe x2, x3;
319
320 r->x = *x;
321 secp256k1_fe_sqr(&x2, x);
322 secp256k1_fe_mul(&x3, x, &x2);
323 r->infinity = 0;
325 return secp256k1_fe_sqrt(&r->y, &x3);
326}
327
328static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
329 int ret = secp256k1_ge_set_xquad(r, x);
331 if (secp256k1_fe_is_odd(&r->y) != odd) {
332 secp256k1_fe_negate(&r->y, &r->y, 1);
333 }
334
336 return ret;
337}
338
341
342 r->infinity = a->infinity;
343 r->x = a->x;
344 r->y = a->y;
345 secp256k1_fe_set_int(&r->z, 1);
346
348}
349
350static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b) {
351 secp256k1_gej tmp;
354
355 secp256k1_gej_neg(&tmp, a);
356 secp256k1_gej_add_var(&tmp, &tmp, b, NULL);
357 return secp256k1_gej_is_infinity(&tmp);
358}
359
360static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
361 secp256k1_fe r;
365
366 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
367 return secp256k1_fe_equal(&r, &a->x);
368}
369
372
373 r->infinity = a->infinity;
374 r->x = a->x;
375 r->y = a->y;
376 r->z = a->z;
378 secp256k1_fe_negate(&r->y, &r->y, 1);
379
381}
382
385
386 return a->infinity;
387}
388
390 secp256k1_fe y2, x3;
392
393 if (a->infinity) {
394 return 0;
395 }
396 /* y^2 = x^3 + 7 */
397 secp256k1_fe_sqr(&y2, &a->y);
398 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
400 return secp256k1_fe_equal(&y2, &x3);
401}
402
404 /* Operations: 3 mul, 4 sqr, 8 add/half/mul_int/negate */
405 secp256k1_fe l, s, t;
407
408 r->infinity = a->infinity;
409
410 /* Formula used:
411 * L = (3/2) * X1^2
412 * S = Y1^2
413 * T = -X1*S
414 * X3 = L^2 + 2*T
415 * Y3 = -(L*(X3 + T) + S^2)
416 * Z3 = Y1*Z1
417 */
418
419 secp256k1_fe_mul(&r->z, &a->z, &a->y); /* Z3 = Y1*Z1 (1) */
420 secp256k1_fe_sqr(&s, &a->y); /* S = Y1^2 (1) */
421 secp256k1_fe_sqr(&l, &a->x); /* L = X1^2 (1) */
422 secp256k1_fe_mul_int(&l, 3); /* L = 3*X1^2 (3) */
423 secp256k1_fe_half(&l); /* L = 3/2*X1^2 (2) */
424 secp256k1_fe_negate(&t, &s, 1); /* T = -S (2) */
425 secp256k1_fe_mul(&t, &t, &a->x); /* T = -X1*S (1) */
426 secp256k1_fe_sqr(&r->x, &l); /* X3 = L^2 (1) */
427 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + T (2) */
428 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + 2*T (3) */
429 secp256k1_fe_sqr(&s, &s); /* S' = S^2 (1) */
430 secp256k1_fe_add(&t, &r->x); /* T' = X3 + T (4) */
431 secp256k1_fe_mul(&r->y, &t, &l); /* Y3 = L*(X3 + T) (1) */
432 secp256k1_fe_add(&r->y, &s); /* Y3 = L*(X3 + T) + S^2 (2) */
433 secp256k1_fe_negate(&r->y, &r->y, 2); /* Y3 = -(L*(X3 + T) + S^2) (3) */
434
436}
437
440
451 if (a->infinity) {
453 if (rzr != NULL) {
454 secp256k1_fe_set_int(rzr, 1);
455 }
456 return;
457 }
458
459 if (rzr != NULL) {
460 *rzr = a->y;
462 }
463
465
467}
468
470 /* 12 mul, 4 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
471 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, h2, h3, t;
474
475 if (a->infinity) {
476 VERIFY_CHECK(rzr == NULL);
477 *r = *b;
478 return;
479 }
480 if (b->infinity) {
481 if (rzr != NULL) {
482 secp256k1_fe_set_int(rzr, 1);
483 }
484 *r = *a;
485 return;
486 }
487
488 secp256k1_fe_sqr(&z22, &b->z);
489 secp256k1_fe_sqr(&z12, &a->z);
490 secp256k1_fe_mul(&u1, &a->x, &z22);
491 secp256k1_fe_mul(&u2, &b->x, &z12);
492 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
493 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
494 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
495 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
498 secp256k1_gej_double_var(r, a, rzr);
499 } else {
500 if (rzr != NULL) {
501 secp256k1_fe_set_int(rzr, 0);
502 }
504 }
505 return;
506 }
507
508 r->infinity = 0;
509 secp256k1_fe_mul(&t, &h, &b->z);
510 if (rzr != NULL) {
511 *rzr = t;
512 }
513 secp256k1_fe_mul(&r->z, &a->z, &t);
514
515 secp256k1_fe_sqr(&h2, &h);
516 secp256k1_fe_negate(&h2, &h2, 1);
517 secp256k1_fe_mul(&h3, &h2, &h);
518 secp256k1_fe_mul(&t, &u1, &h2);
519
520 secp256k1_fe_sqr(&r->x, &i);
521 secp256k1_fe_add(&r->x, &h3);
522 secp256k1_fe_add(&r->x, &t);
523 secp256k1_fe_add(&r->x, &t);
524
525 secp256k1_fe_add(&t, &r->x);
526 secp256k1_fe_mul(&r->y, &t, &i);
527 secp256k1_fe_mul(&h3, &h3, &s1);
528 secp256k1_fe_add(&r->y, &h3);
529
531}
532
534 /* Operations: 8 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
535 secp256k1_fe z12, u1, u2, s1, s2, h, i, h2, h3, t;
538
539 if (a->infinity) {
540 VERIFY_CHECK(rzr == NULL);
542 return;
543 }
544 if (b->infinity) {
545 if (rzr != NULL) {
546 secp256k1_fe_set_int(rzr, 1);
547 }
548 *r = *a;
549 return;
550 }
551
552 secp256k1_fe_sqr(&z12, &a->z);
553 u1 = a->x;
554 secp256k1_fe_mul(&u2, &b->x, &z12);
555 s1 = a->y;
556 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
558 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
561 secp256k1_gej_double_var(r, a, rzr);
562 } else {
563 if (rzr != NULL) {
564 secp256k1_fe_set_int(rzr, 0);
565 }
567 }
568 return;
569 }
570
571 r->infinity = 0;
572 if (rzr != NULL) {
573 *rzr = h;
574 }
575 secp256k1_fe_mul(&r->z, &a->z, &h);
576
577 secp256k1_fe_sqr(&h2, &h);
578 secp256k1_fe_negate(&h2, &h2, 1);
579 secp256k1_fe_mul(&h3, &h2, &h);
580 secp256k1_fe_mul(&t, &u1, &h2);
581
582 secp256k1_fe_sqr(&r->x, &i);
583 secp256k1_fe_add(&r->x, &h3);
584 secp256k1_fe_add(&r->x, &t);
585 secp256k1_fe_add(&r->x, &t);
586
587 secp256k1_fe_add(&t, &r->x);
588 secp256k1_fe_mul(&r->y, &t, &i);
589 secp256k1_fe_mul(&h3, &h3, &s1);
590 secp256k1_fe_add(&r->y, &h3);
591
593 if (rzr != NULL) SECP256K1_FE_VERIFY(rzr);
594}
595
596static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
597 /* Operations: 9 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
598 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
601 SECP256K1_FE_VERIFY(bzinv);
602
603 if (a->infinity) {
604 secp256k1_fe bzinv2, bzinv3;
605 r->infinity = b->infinity;
606 secp256k1_fe_sqr(&bzinv2, bzinv);
607 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
608 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
609 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
610 secp256k1_fe_set_int(&r->z, 1);
612 return;
613 }
614 if (b->infinity) {
615 *r = *a;
616 return;
617 }
618
627 secp256k1_fe_mul(&az, &a->z, bzinv);
628
629 secp256k1_fe_sqr(&z12, &az);
630 u1 = a->x;
631 secp256k1_fe_mul(&u2, &b->x, &z12);
632 s1 = a->y;
633 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
635 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
638 secp256k1_gej_double_var(r, a, NULL);
639 } else {
641 }
642 return;
643 }
644
645 r->infinity = 0;
646 secp256k1_fe_mul(&r->z, &a->z, &h);
647
648 secp256k1_fe_sqr(&h2, &h);
649 secp256k1_fe_negate(&h2, &h2, 1);
650 secp256k1_fe_mul(&h3, &h2, &h);
651 secp256k1_fe_mul(&t, &u1, &h2);
652
653 secp256k1_fe_sqr(&r->x, &i);
654 secp256k1_fe_add(&r->x, &h3);
655 secp256k1_fe_add(&r->x, &t);
656 secp256k1_fe_add(&r->x, &t);
657
658 secp256k1_fe_add(&t, &r->x);
659 secp256k1_fe_mul(&r->y, &t, &i);
660 secp256k1_fe_mul(&h3, &h3, &s1);
661 secp256k1_fe_add(&r->y, &h3);
662
664}
665
666
668 /* Operations: 7 mul, 5 sqr, 21 add/cmov/half/mul_int/negate/normalizes_to_zero */
669 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
670 secp256k1_fe m_alt, rr_alt;
671 int degenerate;
675
676 /* In:
677 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
678 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
679 * we find as solution for a unified addition/doubling formula:
680 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
681 * x3 = lambda^2 - (x1 + x2)
682 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
683 *
684 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
685 * U1 = X1*Z2^2, U2 = X2*Z1^2
686 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
687 * Z = Z1*Z2
688 * T = U1+U2
689 * M = S1+S2
690 * Q = -T*M^2
691 * R = T^2-U1*U2
692 * X3 = R^2+Q
693 * Y3 = -(R*(2*X3+Q)+M^4)/2
694 * Z3 = M*Z
695 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
696 *
697 * This formula has the benefit of being the same for both addition
698 * of distinct points and doubling. However, it breaks down in the
699 * case that either point is infinity, or that y1 = -y2. We handle
700 * these cases in the following ways:
701 *
702 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
703 *
704 * - If a is infinity, we detect this, and at the end of the
705 * computation replace the result (which will be meaningless,
706 * but we compute to be constant-time) with b.x : b.y : 1.
707 *
708 * - If a = -b, we have y1 = -y2, which is a degenerate case.
709 * But here the answer is infinity, so we simply set the
710 * infinity flag of the result, overriding the computed values
711 * without even needing to cmov.
712 *
713 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
714 * properties of our curve (specifically, 1 has nontrivial cube
715 * roots in our field, and the curve equation has no x coefficient)
716 * then the answer is not infinity but also not given by the above
717 * equation. In this case, we cmov in place an alternate expression
718 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
719 * expressions for lambda are defined, they are equal, and can be
720 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
721 * then substitution of x^3 + 7 for y^2 (using the curve equation).
722 * For all pairs of nonzero points (a, b) at least one is defined,
723 * so this covers everything.
724 */
725
726 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
727 u1 = a->x; /* u1 = U1 = X1*Z2^2 (GEJ_X_M) */
728 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
729 s1 = a->y; /* s1 = S1 = Y1*Z2^3 (GEJ_Y_M) */
730 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
731 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
732 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (GEJ_X_M+1) */
733 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (GEJ_Y_M+1) */
734 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
735 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 (2) */
736 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (1) */
737 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (2) */
738 /* If lambda = R/M = R/0 we have a problem (except in the "trivial"
739 * case that Z = z1z2 = 0, and this is special-cased later on). */
740 degenerate = secp256k1_fe_normalizes_to_zero(&m);
741 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
742 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
743 * a nontrivial cube root of one. In either case, an alternate
744 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
745 * so we set R/M equal to this. */
746 rr_alt = s1;
747 secp256k1_fe_mul_int(&rr_alt, 2); /* rr_alt = Y1*Z2^3 - Y2*Z1^3 (GEJ_Y_M*2) */
748 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 (GEJ_X_M+2) */
749
750 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate); /* rr_alt (GEJ_Y_M*2) */
751 secp256k1_fe_cmov(&m_alt, &m, !degenerate); /* m_alt (GEJ_X_M+2) */
752 /* Now Ralt / Malt = lambda and is guaranteed not to be Ralt / 0.
753 * From here on out Ralt and Malt represent the numerator
754 * and denominator of lambda; R and M represent the explicit
755 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
756 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
757 secp256k1_fe_negate(&q, &t,
758 SECP256K1_GEJ_X_MAGNITUDE_MAX + 1); /* q = -T (GEJ_X_M+2) */
759 secp256k1_fe_mul(&q, &q, &n); /* q = Q = -T*Malt^2 (1) */
760 /* These two lines use the observation that either M == Malt or M == 0,
761 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
762 * zero (which is "computed" by cmov). So the cost is one squaring
763 * versus two multiplications. */
764 secp256k1_fe_sqr(&n, &n); /* n = Malt^4 (1) */
765 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (GEJ_Y_M+1) */
766 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
767 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Z3 = Malt*Z (1) */
768 secp256k1_fe_add(&t, &q); /* t = Ralt^2 + Q (2) */
769 r->x = t; /* r->x = X3 = Ralt^2 + Q (2) */
770 secp256k1_fe_mul_int(&t, 2); /* t = 2*X3 (4) */
771 secp256k1_fe_add(&t, &q); /* t = 2*X3 + Q (5) */
772 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*X3 + Q) (1) */
773 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*X3 + Q) + M^3*Malt (GEJ_Y_M+2) */
774 secp256k1_fe_negate(&r->y, &t,
775 SECP256K1_GEJ_Y_MAGNITUDE_MAX + 2); /* r->y = -(Ralt*(2*X3 + Q) + M^3*Malt) (GEJ_Y_M+3) */
776 secp256k1_fe_half(&r->y); /* r->y = Y3 = -(Ralt*(2*X3 + Q) + M^3*Malt)/2 ((GEJ_Y_M+3)/2 + 1) */
777
778 /* In case a->infinity == 1, replace r with (b->x, b->y, 1). */
779 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
780 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
782
783 /* Set r->infinity if r->z is 0.
784 *
785 * If a->infinity is set, then r->infinity = (r->z == 0) = (1 == 0) = false,
786 * which is correct because the function assumes that b is not infinity.
787 *
788 * Now assume !a->infinity. This implies Z = Z1 != 0.
789 *
790 * Case y1 = -y2:
791 * In this case we could have a = -b, namely if x1 = x2.
792 * We have degenerate = true, r->z = (x1 - x2) * Z.
793 * Then r->infinity = ((x1 - x2)Z == 0) = (x1 == x2) = (a == -b).
794 *
795 * Case y1 != -y2:
796 * In this case, we can't have a = -b.
797 * We have degenerate = false, r->z = (y1 + y2) * Z.
798 * Then r->infinity = ((y1 + y2)Z == 0) = (y1 == -y2) = false. */
800
802}
803
805 /* Operations: 4 mul, 1 sqr */
806 secp256k1_fe zz;
810
811 secp256k1_fe_sqr(&zz, s);
812 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
813 secp256k1_fe_mul(&r->y, &r->y, &zz);
814 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
815 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
816
818}
819
821 secp256k1_fe x, y;
824
825 x = a->x;
827 y = a->y;
829 secp256k1_fe_to_storage(&r->x, &x);
830 secp256k1_fe_to_storage(&r->y, &y);
831}
832
834 secp256k1_fe_from_storage(&r->x, &a->x);
835 secp256k1_fe_from_storage(&r->y, &a->y);
836 r->infinity = 0;
837
839}
840
844
845 secp256k1_fe_cmov(&r->x, &a->x, flag);
846 secp256k1_fe_cmov(&r->y, &a->y, flag);
847 secp256k1_fe_cmov(&r->z, &a->z, flag);
848 r->infinity ^= (r->infinity ^ a->infinity) & flag;
849
851}
852
854 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
855 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
856}
857
860
861 *r = *a;
863
865}
866
868 secp256k1_fe yz;
869
870 if (a->infinity) {
871 return 0;
872 }
873
874 /* We rely on the fact that the Jacobi symbol of 1 / a->z^3 is the same as
875 * that of a->z. Thus a->y / a->z^3 is a quadratic residue iff a->y * a->z
876 is */
877 secp256k1_fe_mul(&yz, &a->y, &a->z);
878 return secp256k1_fe_is_quad_var(&yz);
879}
880
882#ifdef EXHAUSTIVE_TEST_ORDER
884 int i;
886
887 /* A very simple EC multiplication ladder that avoids a dependency on ecmult. */
889 for (i = 0; i < 32; ++i) {
891 if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
892 secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
893 }
894 }
896#else
898
899 (void)ge;
900 /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
901 return 1;
902#endif
903}
904
906 secp256k1_fe c;
907 secp256k1_fe_sqr(&c, x);
908 secp256k1_fe_mul(&c, &c, x);
911}
912
914 /* We want to determine whether (xn/xd) is on the curve.
915 *
916 * (xn/xd)^3 + 7 is square <=> xd*xn^3 + 7*xd^4 is square (multiplying by xd^4, a square).
917 */
918 secp256k1_fe r, t;
920
921 secp256k1_fe_mul(&r, xd, xn); /* r = xd*xn */
922 secp256k1_fe_sqr(&t, xn); /* t = xn^2 */
923 secp256k1_fe_mul(&r, &r, &t); /* r = xd*xn^3 */
924 secp256k1_fe_sqr(&t, xd); /* t = xd^2 */
925 secp256k1_fe_sqr(&t, &t); /* t = xd^4 */
927 secp256k1_fe_mul_int(&t, SECP256K1_B); /* t = 7*xd^4 */
928 secp256k1_fe_add(&r, &t); /* r = xd*xn^3 + 7*xd^4 */
930}
931
932#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.
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:215
#define secp256k1_fe_mul_int(r, a)
Multiply a field element with a small integer.
Definition: field.h:237
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
#define secp256k1_fe_normalize_weak
Definition: field.h:79
static const secp256k1_fe secp256k1_const_beta
Definition: field.h:69
#define secp256k1_fe_is_odd
Definition: field.h:86
#define SECP256K1_FE_VERIFY_MAGNITUDE(a, m)
Definition: field.h:355
#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_VERIFY(a)
Definition: field.h:351
#define secp256k1_fe_is_square_var
Definition: field.h:104
#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 int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
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
#define SECP256K1_GE_X_MAGNITUDE_MAX
Maximum allowed magnitudes for group element coordinates in affine (x, y) and jacobian (x,...
Definition: group.h:49
#define SECP256K1_GEJ_VERIFY(a)
Definition: group.h:197
#define SECP256K1_GEJ_Y_MAGNITUDE_MAX
Definition: group.h:52
#define SECP256K1_GE_Y_MAGNITUDE_MAX
Definition: group.h:50
#define SECP256K1_GEJ_Z_MAGNITUDE_MAX
Definition: group.h:53
#define SECP256K1_GE_VERIFY(a)
Definition: group.h:193
#define SECP256K1_GEJ_X_MAGNITUDE_MAX
Definition: group.h:51
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Definition: group_impl.h:350
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:438
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:596
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:21
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:299
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:858
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:282
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:383
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:308
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:130
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:328
static void secp256k1_ge_verify(const secp256k1_ge *a)
Definition: group_impl.h:76
static int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x)
Definition: group_impl.h:905
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:533
static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
Definition: group_impl.h:841
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:667
#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:97
#define SECP256K1_B
Definition: group_impl.h:71
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:389
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:833
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:469
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd)
Definition: group_impl.h:913
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:804
static int secp256k1_ge_set_xquad(secp256k1_ge *r, const secp256k1_fe *x)
Definition: group_impl.h:316
static void secp256k1_ge_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi)
Definition: group_impl.h:114
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:360
#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:157
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:881
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Definition: group_impl.h:249
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:147
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:141
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Definition: group_impl.h:291
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:196
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:339
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:820
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:853
#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:175
static int secp256k1_gej_has_quad_y_var(const secp256k1_gej *a)
Definition: group_impl.h:867
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:370
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:403
#define SECP256K1_INLINE
Definition: util.h:48
#define VERIFY_CHECK(cond)
Definition: util.h:139
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