Bitcoin Core 32.99.0
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 <string.h>
11
12#include "field.h"
13#include "group.h"
14#include "util.h"
15
16/* Begin of section generated by sage/gen_exhaustive_groups.sage. */
17#define SECP256K1_G_ORDER_7 SECP256K1_GE_CONST(\
18 0x66625d13, 0x317ffe44, 0x63d32cff, 0x1ca02b9b,\
19 0xe5c6d070, 0x50b4b05e, 0x81cc30db, 0xf5166f0a,\
20 0x1e60e897, 0xa7c00c7c, 0x2df53eb6, 0x98274ff4,\
21 0x64252f42, 0x8ca44e17, 0x3b25418c, 0xff4ab0cf\
22)
23#define SECP256K1_G_ORDER_13 SECP256K1_GE_CONST(\
24 0xa2482ff8, 0x4bf34edf, 0xa51262fd, 0xe57921db,\
25 0xe0dd2cb7, 0xa5914790, 0xbc71631f, 0xc09704fb,\
26 0x942536cb, 0xa3e49492, 0x3a701cc3, 0xee3e443f,\
27 0xdf182aa9, 0x15b8aa6a, 0x166d3b19, 0xba84b045\
28)
29#define SECP256K1_G_ORDER_199 SECP256K1_GE_CONST(\
30 0x7fb07b5c, 0xd07c3bda, 0x553902e2, 0x7a87ea2c,\
31 0x35108a7f, 0x051f41e5, 0xb76abad5, 0x1f2703ad,\
32 0x0a251539, 0x5b4c4438, 0x952a634f, 0xac10dd4d,\
33 0x6d6f4745, 0x98990c27, 0x3a4f3116, 0xd32ff969\
34)
38#define SECP256K1_G SECP256K1_GE_CONST(\
39 0x79be667e, 0xf9dcbbac, 0x55a06295, 0xce870b07,\
40 0x029bfcdb, 0x2dce28d9, 0x59f2815b, 0x16f81798,\
41 0x483ada77, 0x26a3c465, 0x5da4fbfc, 0x0e1108a8,\
42 0xfd17b448, 0xa6855419, 0x9c47d08f, 0xfb10d4b8\
43)
44/* These exhaustive group test orders and generators are chosen such that:
45 * - The field size is equal to that of secp256k1, so field code is the same.
46 * - The curve equation is of the form y^2=x^3+B for some small constant B.
47 * - The subgroup has a generator 2*P, where P.x is as small as possible.
48 * - The subgroup has size less than 1000 to permit exhaustive testing.
49 * - The subgroup admits an endomorphism of the form lambda*(x,y) == (beta*x,y).
50 */
51#if defined(EXHAUSTIVE_TEST_ORDER)
52# if EXHAUSTIVE_TEST_ORDER == 7
53
55#define SECP256K1_B 6
56
57# elif EXHAUSTIVE_TEST_ORDER == 13
58
60#define SECP256K1_B 2
61
62# elif EXHAUSTIVE_TEST_ORDER == 199
63
65#define SECP256K1_B 4
66
67# else
68# error No known generator for the specified exhaustive test group order.
69# endif
70#else
71
73#define SECP256K1_B 7
74
75#endif
76/* End of section generated by sage/gen_exhaustive_groups.sage. */
77
78static void secp256k1_ge_verify(const secp256k1_ge *a) {
83 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
84 (void)a;
85}
86
87static void secp256k1_gej_verify(const secp256k1_gej *a) {
94 VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
95 (void)a;
96}
97
99 secp256k1_fe zi2;
100 secp256k1_fe zi3;
102
103 secp256k1_fe_sqr(&zi2, zi);
104 secp256k1_fe_mul(&zi3, &zi2, zi);
105 secp256k1_fe_mul(&r->x, &a->x, &zi2);
106 secp256k1_fe_mul(&r->y, &a->y, &zi3);
107 r->infinity = 0;
108}
113}
114
116 secp256k1_fe zi2;
117 secp256k1_fe zi3;
119
120 secp256k1_fe_sqr(&zi2, zi);
121 secp256k1_fe_mul(&zi3, &zi2, zi);
122 secp256k1_fe_mul(&r->x, &a->x, &zi2);
123 secp256k1_fe_mul(&r->y, &a->y, &zi3);
124 r->infinity = 0;
125}
130}
131
133 r->infinity = 0;
134 r->x = *x;
135 r->y = *y;
136}
137static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
141}
142
144 return a->infinity;
145}
149}
150
152 *r = *a;
154 secp256k1_fe_negate(&r->y, &r->y, 1);
155}
156static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
160}
161
163 secp256k1_fe z2, z3;
164
165 r->infinity = a->infinity;
166 secp256k1_fe_inv(&a->z, &a->z);
167 secp256k1_fe_sqr(&z2, &a->z);
168 secp256k1_fe_mul(&z3, &a->z, &z2);
169 secp256k1_fe_mul(&a->x, &a->x, &z2);
170 secp256k1_fe_mul(&a->y, &a->y, &z3);
171 secp256k1_fe_set_int(&a->z, 1);
172 r->x = a->x;
173 r->y = a->y;
174}
179}
180
182 secp256k1_fe z2, z3;
183
186 return;
187 }
188 r->infinity = 0;
189 secp256k1_fe_inv_var(&a->z, &a->z);
190 secp256k1_fe_sqr(&z2, &a->z);
191 secp256k1_fe_mul(&z3, &a->z, &z2);
192 secp256k1_fe_mul(&a->x, &a->x, &z2);
193 secp256k1_fe_mul(&a->y, &a->y, &z3);
194 secp256k1_fe_set_int(&a->z, 1);
195 secp256k1_ge_set_xy(r, &a->x, &a->y);
196}
201}
202
204 secp256k1_fe u;
205 size_t i;
206
207 if (len == 0) {
208 return;
209 }
210
211 /* Use destination's x coordinates as scratch space */
212 r[0].x = a[0].z;
213 for (i = 1; i < len; i++) {
214 secp256k1_fe_mul(&r[i].x, &r[i - 1].x, &a[i].z);
215 }
216 secp256k1_fe_inv(&u, &r[len - 1].x);
217
218 for (i = len - 1; i > 0; i--) {
219 secp256k1_fe_mul(&r[i].x, &r[i - 1].x, &u);
220 secp256k1_fe_mul(&u, &u, &a[i].z);
221 }
222 r[0].x = u;
223
224 for (i = 0; i < len; i++) {
225 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
226 }
227}
228static void secp256k1_ge_set_all_gej(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
229#ifdef VERIFY
230 size_t i;
231 for (i = 0; i < len; i++) {
234 }
235#endif
237#ifdef VERIFY
238 for (i = 0; i < len; i++) {
239 SECP256K1_GE_VERIFY(&r[i]);
240 }
241#endif
242}
243
245 secp256k1_fe u;
246 size_t i;
247 size_t last_i = SIZE_MAX;
248
249 for (i = 0; i < len; i++) {
250 if (a[i].infinity) {
252 } else {
253 /* Use destination's x coordinates as scratch space */
254 if (last_i == SIZE_MAX) {
255 r[i].x = a[i].z;
256 } else {
257 secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
258 }
259 last_i = i;
260 }
261 }
262 if (last_i == SIZE_MAX) {
263 return;
264 }
265 secp256k1_fe_inv_var(&u, &r[last_i].x);
266
267 i = last_i;
268 while (i > 0) {
269 i--;
270 if (!a[i].infinity) {
271 secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
272 secp256k1_fe_mul(&u, &u, &a[last_i].z);
273 last_i = i;
274 }
275 }
276 VERIFY_CHECK(!a[last_i].infinity);
277 r[last_i].x = u;
278
279 for (i = 0; i < len; i++) {
280 if (!a[i].infinity) {
281 secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
282 }
283 }
284}
285static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
286#ifdef VERIFY
287 size_t i;
288 for (i = 0; i < len; i++) {
290 }
291#endif
293#ifdef VERIFY
294 for (i = 0; i < len; i++) {
295 SECP256K1_GE_VERIFY(&r[i]);
296 }
297#endif
298}
299
301 size_t i;
302 secp256k1_fe zs;
303
304 if (len > 0) {
305 i = len - 1;
306 /* Ensure all y values are in weak normal form for fast negation of points */
308 zs = zr[i];
309
310 /* Work our way backwards, using the z-ratios to scale the x/y values. */
311 while (i > 0) {
312 if (i != len - 1) {
313 secp256k1_fe_mul(&zs, &zs, &zr[i]);
314 }
315 i--;
316 secp256k1_ge_set_ge_zinv(&a[i], &a[i], &zs);
317 }
318 }
319}
320static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) {
321#ifdef VERIFY
322 size_t i;
323 for (i = 0; i < len; i++) {
324 SECP256K1_GE_VERIFY(&a[i]);
325 SECP256K1_FE_VERIFY(&zr[i]);
326 }
327#endif
329#ifdef VERIFY
330 for (i = 0; i < len; i++) {
331 SECP256K1_GE_VERIFY(&a[i]);
332 }
333#endif
334}
335
337 r->infinity = 1;
338 secp256k1_fe_set_int(&r->x, 0);
339 secp256k1_fe_set_int(&r->y, 0);
340 secp256k1_fe_set_int(&r->z, 0);
341
343}
344
346 r->infinity = 1;
347 secp256k1_fe_set_int(&r->x, 0);
348 secp256k1_fe_set_int(&r->y, 0);
349
351}
352
355}
356
359}
360
362 secp256k1_fe x2, x3;
363 int ret;
364
365 r->x = *x;
366 secp256k1_fe_sqr(&x2, x);
367 secp256k1_fe_mul(&x3, x, &x2);
368 r->infinity = 0;
370 ret = secp256k1_fe_sqrt(&r->y, &x3);
372 if (secp256k1_fe_is_odd(&r->y) != odd) {
373 secp256k1_fe_negate(&r->y, &r->y, 1);
374 }
375
376 return ret;
377}
378static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
379 int ret;
383 return ret;
384}
385
387 r->infinity = a->infinity;
388 r->x = a->x;
389 r->y = a->y;
390 secp256k1_fe_set_int(&r->z, 1);
391}
396}
397
399 secp256k1_gej tmp;
400
401 secp256k1_gej_neg(&tmp, a);
402 secp256k1_gej_add_var(&tmp, &tmp, b, NULL);
403 return secp256k1_gej_is_infinity(&tmp);
404}
405static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b) {
407 return secp256k1_gej_impl_eq_var(a, b);
408}
409
411 secp256k1_gej tmp;
412
413 secp256k1_gej_neg(&tmp, a);
414 secp256k1_gej_add_ge_var(&tmp, &tmp, b, NULL);
415 return secp256k1_gej_is_infinity(&tmp);
416}
417static int secp256k1_gej_eq_ge_var(const secp256k1_gej *a, const secp256k1_ge *b) {
419 return secp256k1_gej_impl_eq_ge_var(a, b);
420}
421
423 secp256k1_fe tmp;
424
425 if (a->infinity != b->infinity) return 0;
426 if (a->infinity) return 1;
427
428 tmp = a->x;
430 if (!secp256k1_fe_equal(&tmp, &b->x)) return 0;
431
432 tmp = a->y;
434 if (!secp256k1_fe_equal(&tmp, &b->y)) return 0;
435
436 return 1;
437}
438static int secp256k1_ge_eq_var(const secp256k1_ge *a, const secp256k1_ge *b) {
440 return secp256k1_ge_impl_eq_var(a, b);
441}
442
444 secp256k1_fe r;
446
447 secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
448 return secp256k1_fe_equal(&r, &a->x);
449}
450static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
452 return secp256k1_gej_impl_eq_x_var(x, a);
453}
454
456 r->infinity = a->infinity;
457 r->x = a->x;
458 r->y = a->y;
459 r->z = a->z;
461 secp256k1_fe_negate(&r->y, &r->y, 1);
462}
467}
468
470 return a->infinity;
471}
475}
476
478 secp256k1_fe y2, x3;
479
480 if (a->infinity) {
481 return 0;
482 }
483 /* y^2 = x^3 + 7 */
484 secp256k1_fe_sqr(&y2, &a->y);
485 secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
487 return secp256k1_fe_equal(&y2, &x3);
488}
492}
493
495 /* Operations: 3 mul, 4 sqr, 8 add/half/mul_int/negate */
496 secp256k1_fe l, s, t;
497
498 r->infinity = a->infinity;
499
500 /* Formula used:
501 * L = (3/2) * X1^2
502 * S = Y1^2
503 * T = -X1*S
504 * X3 = L^2 + 2*T
505 * Y3 = -(L*(X3 + T) + S^2)
506 * Z3 = Y1*Z1
507 */
508
509 secp256k1_fe_mul(&r->z, &a->z, &a->y); /* Z3 = Y1*Z1 (1) */
510 secp256k1_fe_sqr(&s, &a->y); /* S = Y1^2 (1) */
511 secp256k1_fe_sqr(&l, &a->x); /* L = X1^2 (1) */
512 secp256k1_fe_mul_int(&l, 3); /* L = 3*X1^2 (3) */
513 secp256k1_fe_half(&l); /* L = 3/2*X1^2 (2) */
514 secp256k1_fe_negate(&t, &s, 1); /* T = -S (2) */
515 secp256k1_fe_mul(&t, &t, &a->x); /* T = -X1*S (1) */
516 secp256k1_fe_sqr(&r->x, &l); /* X3 = L^2 (1) */
517 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + T (2) */
518 secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + 2*T (3) */
519 secp256k1_fe_sqr(&s, &s); /* S' = S^2 (1) */
520 secp256k1_fe_add(&t, &r->x); /* T' = X3 + T (4) */
521 secp256k1_fe_mul(&r->y, &t, &l); /* Y3 = L*(X3 + T) (1) */
522 secp256k1_fe_add(&r->y, &s); /* Y3 = L*(X3 + T) + S^2 (2) */
523 secp256k1_fe_negate(&r->y, &r->y, 2); /* Y3 = -(L*(X3 + T) + S^2) (3) */
524}
529}
530
542 if (a->infinity) {
544 if (rzr != NULL) {
545 secp256k1_fe_set_int(rzr, 1);
546 }
547 return;
548 }
549
550 if (rzr != NULL) {
551 *rzr = a->y;
553 }
554
556}
560 SECP256K1_GEJ_VERIFY(r); if (rzr != NULL) SECP256K1_FE_VERIFY(rzr);
561}
562
564 /* 12 mul, 4 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
565 secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, h2, h3, t;
566
567 if (a->infinity) {
568 VERIFY_CHECK(rzr == NULL);
569 *r = *b;
570 return;
571 }
572 if (b->infinity) {
573 if (rzr != NULL) {
574 secp256k1_fe_set_int(rzr, 1);
575 }
576 *r = *a;
577 return;
578 }
579
580 secp256k1_fe_sqr(&z22, &b->z);
581 secp256k1_fe_sqr(&z12, &a->z);
582 secp256k1_fe_mul(&u1, &a->x, &z22);
583 secp256k1_fe_mul(&u2, &b->x, &z12);
584 secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
585 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
586 secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
587 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
590 secp256k1_gej_double_var(r, a, rzr);
591 } else {
592 if (rzr != NULL) {
593 secp256k1_fe_set_int(rzr, 0);
594 }
596 }
597 return;
598 }
599
600 r->infinity = 0;
601 secp256k1_fe_mul(&t, &h, &b->z);
602 if (rzr != NULL) {
603 *rzr = t;
604 }
605 secp256k1_fe_mul(&r->z, &a->z, &t);
606
607 secp256k1_fe_sqr(&h2, &h);
608 secp256k1_fe_negate(&h2, &h2, 1);
609 secp256k1_fe_mul(&h3, &h2, &h);
610 secp256k1_fe_mul(&t, &u1, &h2);
611
612 secp256k1_fe_sqr(&r->x, &i);
613 secp256k1_fe_add(&r->x, &h3);
614 secp256k1_fe_add(&r->x, &t);
615 secp256k1_fe_add(&r->x, &t);
616
617 secp256k1_fe_add(&t, &r->x);
618 secp256k1_fe_mul(&r->y, &t, &i);
619 secp256k1_fe_mul(&h3, &h3, &s1);
620 secp256k1_fe_add(&r->y, &h3);
621}
624 secp256k1_gej_impl_add_var(r, a, b, rzr);
625 SECP256K1_GEJ_VERIFY(r); if (rzr != NULL) SECP256K1_FE_VERIFY(rzr);
626}
627
629 /* Operations: 8 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
630 secp256k1_fe z12, u1, u2, s1, s2, h, i, h2, h3, t;
631
632 if (a->infinity) {
633 VERIFY_CHECK(rzr == NULL);
635 return;
636 }
637 if (b->infinity) {
638 if (rzr != NULL) {
639 secp256k1_fe_set_int(rzr, 1);
640 }
641 *r = *a;
642 return;
643 }
644
645 secp256k1_fe_sqr(&z12, &a->z);
646 u1 = a->x;
647 secp256k1_fe_mul(&u2, &b->x, &z12);
648 s1 = a->y;
649 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
651 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
654 secp256k1_gej_double_var(r, a, rzr);
655 } else {
656 if (rzr != NULL) {
657 secp256k1_fe_set_int(rzr, 0);
658 }
660 }
661 return;
662 }
663
664 r->infinity = 0;
665 if (rzr != NULL) {
666 *rzr = h;
667 }
668 secp256k1_fe_mul(&r->z, &a->z, &h);
669
670 secp256k1_fe_sqr(&h2, &h);
671 secp256k1_fe_negate(&h2, &h2, 1);
672 secp256k1_fe_mul(&h3, &h2, &h);
673 secp256k1_fe_mul(&t, &u1, &h2);
674
675 secp256k1_fe_sqr(&r->x, &i);
676 secp256k1_fe_add(&r->x, &h3);
677 secp256k1_fe_add(&r->x, &t);
678 secp256k1_fe_add(&r->x, &t);
679
680 secp256k1_fe_add(&t, &r->x);
681 secp256k1_fe_mul(&r->y, &t, &i);
682 secp256k1_fe_mul(&h3, &h3, &s1);
683 secp256k1_fe_add(&r->y, &h3);
684}
687 secp256k1_gej_impl_add_ge_var(r, a, b, rzr);
688 SECP256K1_GEJ_VERIFY(r); if (rzr != NULL) SECP256K1_FE_VERIFY(rzr);
689}
690
692 /* Operations: 9 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
693 secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
694
695 if (a->infinity) {
696 secp256k1_fe bzinv2, bzinv3;
697 r->infinity = b->infinity;
698 secp256k1_fe_sqr(&bzinv2, bzinv);
699 secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
700 secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
701 secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
702 secp256k1_fe_set_int(&r->z, 1);
703 return;
704 }
705 if (b->infinity) {
706 *r = *a;
707 return;
708 }
709
718 secp256k1_fe_mul(&az, &a->z, bzinv);
719
720 secp256k1_fe_sqr(&z12, &az);
721 u1 = a->x;
722 secp256k1_fe_mul(&u2, &b->x, &z12);
723 s1 = a->y;
724 secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
726 secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
729 secp256k1_gej_double_var(r, a, NULL);
730 } else {
732 }
733 return;
734 }
735
736 r->infinity = 0;
737 secp256k1_fe_mul(&r->z, &a->z, &h);
738
739 secp256k1_fe_sqr(&h2, &h);
740 secp256k1_fe_negate(&h2, &h2, 1);
741 secp256k1_fe_mul(&h3, &h2, &h);
742 secp256k1_fe_mul(&t, &u1, &h2);
743
744 secp256k1_fe_sqr(&r->x, &i);
745 secp256k1_fe_add(&r->x, &h3);
746 secp256k1_fe_add(&r->x, &t);
747 secp256k1_fe_add(&r->x, &t);
748
749 secp256k1_fe_add(&t, &r->x);
750 secp256k1_fe_mul(&r->y, &t, &i);
751 secp256k1_fe_mul(&h3, &h3, &s1);
752 secp256k1_fe_add(&r->y, &h3);
753}
754static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
756 secp256k1_gej_impl_add_zinv_var(r, a, b, bzinv);
758}
759
760
762 /* Operations: 7 mul, 5 sqr, 21 add/cmov/half/mul_int/negate/normalizes_to_zero */
763 secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
764 secp256k1_fe m_alt, rr_alt;
765 int degenerate;
767
768 /* In:
769 * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
770 * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
771 * we find as solution for a unified addition/doubling formula:
772 * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
773 * x3 = lambda^2 - (x1 + x2)
774 * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
775 *
776 * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
777 * U1 = X1*Z2^2, U2 = X2*Z1^2
778 * S1 = Y1*Z2^3, S2 = Y2*Z1^3
779 * Z = Z1*Z2
780 * T = U1+U2
781 * M = S1+S2
782 * Q = -T*M^2
783 * R = T^2-U1*U2
784 * X3 = R^2+Q
785 * Y3 = -(R*(2*X3+Q)+M^4)/2
786 * Z3 = M*Z
787 * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
788 *
789 * This formula has the benefit of being the same for both addition
790 * of distinct points and doubling. However, it breaks down in the
791 * case that either point is infinity, or that y1 = -y2. We handle
792 * these cases in the following ways:
793 *
794 * - If b is infinity we simply bail by means of a VERIFY_CHECK.
795 *
796 * - If a is infinity, we detect this, and at the end of the
797 * computation replace the result (which will be meaningless,
798 * but we compute to be constant-time) with b.x : b.y : 1.
799 *
800 * - If a = -b, we have y1 = -y2, which is a degenerate case.
801 * But here the answer is infinity, so we simply set the
802 * infinity flag of the result, overriding the computed values
803 * without even needing to cmov.
804 *
805 * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
806 * properties of our curve (specifically, 1 has nontrivial cube
807 * roots in our field, and the curve equation has no x coefficient)
808 * then the answer is not infinity but also not given by the above
809 * equation. In this case, we cmov in place an alternate expression
810 * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
811 * expressions for lambda are defined, they are equal, and can be
812 * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
813 * then substitution of x^3 + 7 for y^2 (using the curve equation).
814 * For all pairs of nonzero points (a, b) at least one is defined,
815 * so this covers everything.
816 */
817
818 secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
819 u1 = a->x; /* u1 = U1 = X1*Z2^2 (GEJ_X_M) */
820 secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
821 s1 = a->y; /* s1 = S1 = Y1*Z2^3 (GEJ_Y_M) */
822 secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
823 secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
824 t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (GEJ_X_M+1) */
825 m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (GEJ_Y_M+1) */
826 secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
827 secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 (2) */
828 secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (1) */
829 secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (2) */
830 /* If lambda = R/M = R/0 we have a problem (except in the "trivial"
831 * case that Z = z1z2 = 0, and this is special-cased later on). */
832 degenerate = secp256k1_fe_normalizes_to_zero(&m);
833 /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
834 * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
835 * a nontrivial cube root of one. In either case, an alternate
836 * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
837 * so we set R/M equal to this. */
838 rr_alt = s1;
839 secp256k1_fe_mul_int(&rr_alt, 2); /* rr_alt = Y1*Z2^3 - Y2*Z1^3 (GEJ_Y_M*2) */
840 secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 (GEJ_X_M+2) */
841
842 secp256k1_fe_cmov(&rr_alt, &rr, !degenerate); /* rr_alt (GEJ_Y_M*2) */
843 secp256k1_fe_cmov(&m_alt, &m, !degenerate); /* m_alt (GEJ_X_M+2) */
844 /* Now Ralt / Malt = lambda and is guaranteed not to be Ralt / 0.
845 * From here on out Ralt and Malt represent the numerator
846 * and denominator of lambda; R and M represent the explicit
847 * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
848 secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
850 SECP256K1_GEJ_X_MAGNITUDE_MAX + 1); /* q = -T (GEJ_X_M+2) */
851 secp256k1_fe_mul(&q, &q, &n); /* q = Q = -T*Malt^2 (1) */
852 /* These two lines use the observation that either M == Malt or M == 0,
853 * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
854 * zero (which is "computed" by cmov). So the cost is one squaring
855 * versus two multiplications. */
856 secp256k1_fe_sqr(&n, &n); /* n = Malt^4 (1) */
857 secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (GEJ_Y_M+1) */
858 secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
859 secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Z3 = Malt*Z (1) */
860 secp256k1_fe_add(&t, &q); /* t = Ralt^2 + Q (2) */
861 r->x = t; /* r->x = X3 = Ralt^2 + Q (2) */
862 secp256k1_fe_mul_int(&t, 2); /* t = 2*X3 (4) */
863 secp256k1_fe_add(&t, &q); /* t = 2*X3 + Q (5) */
864 secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*X3 + Q) (1) */
865 secp256k1_fe_add(&t, &n); /* t = Ralt*(2*X3 + Q) + M^3*Malt (GEJ_Y_M+2) */
866 secp256k1_fe_negate(&r->y, &t,
867 SECP256K1_GEJ_Y_MAGNITUDE_MAX + 2); /* r->y = -(Ralt*(2*X3 + Q) + M^3*Malt) (GEJ_Y_M+3) */
868 secp256k1_fe_half(&r->y); /* r->y = Y3 = -(Ralt*(2*X3 + Q) + M^3*Malt)/2 ((GEJ_Y_M+3)/2 + 1) */
869
870 /* In case a->infinity == 1, replace r with (b->x, b->y, 1). */
871 secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
872 secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
874
875 /* Set r->infinity if r->z is 0.
876 *
877 * If a->infinity is set, then r->infinity = (r->z == 0) = (1 == 0) = false,
878 * which is correct because the function assumes that b is not infinity.
879 *
880 * Now assume !a->infinity. This implies Z = Z1 != 0.
881 *
882 * Case y1 = -y2:
883 * In this case we could have a = -b, namely if x1 = x2.
884 * We have degenerate = true, r->z = (x1 - x2) * Z.
885 * Then r->infinity = ((x1 - x2)Z == 0) = (x1 == x2) = (a == -b).
886 *
887 * Case y1 != -y2:
888 * In this case, we can't have a = -b.
889 * We have degenerate = false, r->z = (y1 + y2) * Z.
890 * Then r->infinity = ((y1 + y2)Z == 0) = (y1 == -y2) = false. */
892}
897}
898
900 /* Operations: 4 mul, 1 sqr */
901 secp256k1_fe zz;
903
904 secp256k1_fe_sqr(&zz, s);
905 secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
906 secp256k1_fe_mul(&r->y, &r->y, &zz);
907 secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
908 secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
909}
914}
915
917 secp256k1_fe x, y;
919
920 x = a->x;
922 y = a->y;
924 secp256k1_fe_to_storage(&r->x, &x);
925 secp256k1_fe_to_storage(&r->y, &y);
926}
930}
931
933 secp256k1_fe_from_storage(&r->x, &a->x);
934 secp256k1_fe_from_storage(&r->y, &a->y);
935 r->infinity = 0;
936
938}
939
941 VERIFY_CHECK(flag == 0 || flag == 1);
942 secp256k1_fe_cmov(&r->x, &a->x, flag);
943 secp256k1_fe_cmov(&r->y, &a->y, flag);
944 secp256k1_fe_cmov(&r->z, &a->z, flag);
945 r->infinity ^= (r->infinity ^ a->infinity) & flag;
946}
949 secp256k1_gej_impl_cmov(r, a, flag);
951}
952
954 VERIFY_CHECK(flag == 0 || flag == 1);
955 secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
956 secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
957}
958
960 *r = *a;
962}
967}
968
970#ifdef EXHAUSTIVE_TEST_ORDER
972 int i;
973
974 /* A very simple EC multiplication ladder that avoids a dependency on ecmult. */
976 for (i = 0; i < 32; ++i) {
978 if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
979 secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
980 }
981 }
983#else
984 (void)ge;
985 /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
986 return 1;
987#endif
988}
992}
993
995 secp256k1_fe c;
996
997 secp256k1_fe_sqr(&c, x);
998 secp256k1_fe_mul(&c, &c, x);
1000 return secp256k1_fe_is_square_var(&c);
1001}
1005}
1006
1008 /* We want to determine whether (xn/xd) is on the curve.
1009 *
1010 * (xn/xd)^3 + 7 is square <=> xd*xn^3 + 7*xd^4 is square (multiplying by xd^4, a square).
1011 */
1012 secp256k1_fe r, t;
1014
1015 secp256k1_fe_mul(&r, xd, xn); /* r = xd*xn */
1016 secp256k1_fe_sqr(&t, xn); /* t = xn^2 */
1017 secp256k1_fe_mul(&r, &r, &t); /* r = xd*xn^3 */
1018 secp256k1_fe_sqr(&t, xd); /* t = xd^2 */
1019 secp256k1_fe_sqr(&t, &t); /* t = xd^4 */
1021 secp256k1_fe_mul_int(&t, SECP256K1_B); /* t = 7*xd^4 */
1022 secp256k1_fe_add(&r, &t); /* r = xd*xn^3 + 7*xd^4 */
1023 return secp256k1_fe_is_square_var(&r);
1024}
1025
1029}
1030
1031SECP256K1_INLINE static void secp256k1_ge_impl_to_bytes(unsigned char *buf, const secp256k1_ge *a) {
1034
1035 /* We require that the secp256k1_ge_storage type is exactly 64 bytes.
1036 * This is formally not guaranteed by the C standard, but should hold on any
1037 * sane compiler in the real world. */
1038 STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
1040 memcpy(buf, &s, 64);
1041}
1042static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a) {
1045}
1046
1047SECP256K1_INLINE static void secp256k1_ge_impl_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
1049
1050 STATIC_ASSERT(sizeof(secp256k1_ge_storage) == 64);
1051 memcpy(&s, buf, 64);
1053}
1054static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf) {
1057}
1058
1060 if (ge->infinity) {
1061 memset(data, 0, 64);
1062 } else {
1064 }
1065}
1066static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge) {
1069}
1070
1072 static const unsigned char zeros[64] = { 0 };
1073 if (secp256k1_memcmp_var(data, zeros, sizeof(zeros)) == 0) {
1075 } else {
1077 }
1078}
1079static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data) {
1082}
1083
1084SECP256K1_INLINE static int secp256k1_ge_impl_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
1085 if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
1086 secp256k1_fe x;
1087 return secp256k1_fe_set_b32_limit(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
1088 } else if (size == 65 && (pub[0] == SECP256K1_TAG_PUBKEY_UNCOMPRESSED || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
1089 secp256k1_fe x, y;
1090 if (!secp256k1_fe_set_b32_limit(&x, pub+1) || !secp256k1_fe_set_b32_limit(&y, pub+33)) {
1091 return 0;
1092 }
1093 secp256k1_ge_set_xy(elem, &x, &y);
1096 return 0;
1097 }
1098 return secp256k1_ge_is_valid_var(elem);
1099 } else {
1100 return 0;
1101 }
1102}
1103static int secp256k1_ge_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
1104 int ret = secp256k1_ge_impl_parse(elem, pub, size);
1105 if (ret) {
1106 SECP256K1_GE_VERIFY(elem);
1107 }
1108 return ret;
1109}
1110
1111SECP256K1_INLINE static void secp256k1_ge_impl_serialize33(secp256k1_ge *elem, unsigned char *pub33) {
1112 VERIFY_CHECK(!elem->infinity);
1113
1117 secp256k1_fe_get_b32(&pub33[1], &elem->x);
1118}
1119static void secp256k1_ge_serialize33(secp256k1_ge *elem, unsigned char *pub33) {
1120 SECP256K1_GE_VERIFY(elem);
1121 secp256k1_ge_impl_serialize33(elem, pub33);
1122 SECP256K1_GE_VERIFY(elem);
1123}
1124
1125SECP256K1_INLINE static void secp256k1_ge_impl_serialize65(secp256k1_ge *elem, unsigned char *pub65) {
1126 VERIFY_CHECK(!elem->infinity);
1127
1131 secp256k1_fe_get_b32(&pub65[1], &elem->x);
1132 secp256k1_fe_get_b32(&pub65[33], &elem->y);
1133}
1134static void secp256k1_ge_serialize65(secp256k1_ge *elem, unsigned char *pub65) {
1135 SECP256K1_GE_VERIFY(elem);
1136 secp256k1_ge_impl_serialize65(elem, pub65);
1137 SECP256K1_GE_VERIFY(elem);
1138}
1139
1141 if (ge->infinity) {
1142 memset(out33, 0, 33);
1143 } else {
1144 /* Serialize must succeed because the point is not at infinity */
1145 secp256k1_ge_serialize33(ge, out33);
1146 }
1147}
1148static void secp256k1_ge_serialize_ext33(unsigned char *out33, secp256k1_ge *ge) {
1152}
1153
1154SECP256K1_INLINE static int secp256k1_ge_impl_parse_ext33(secp256k1_ge *ge, const unsigned char *in33) {
1155 unsigned char zeros[33] = { 0 };
1156
1157 if (secp256k1_memcmp_var(in33, zeros, sizeof(zeros)) == 0) {
1159 return 1;
1160 }
1161 if (!secp256k1_ge_parse(ge, in33, 33)) {
1162 return 0;
1163 }
1165}
1166static int secp256k1_ge_parse_ext33(secp256k1_ge *ge, const unsigned char *in33) {
1167 int ret = secp256k1_ge_impl_parse_ext33(ge, in33);
1168 if (ret) {
1170 }
1171 return ret;
1172}
1173
1174#endif /* SECP256K1_GROUP_IMPL_H */
int ret
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
#define secp256k1_fe_cmov
Definition: field.h:95
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:218
#define secp256k1_fe_mul_int(r, a)
Multiply a field element with a small integer.
Definition: field.h:240
#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:85
#define SECP256K1_FE_VERIFY_MAGNITUDE(a, m)
Definition: field.h:356
#define secp256k1_fe_mul
Definition: field.h:93
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:92
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_half
Definition: field.h:101
#define secp256k1_fe_to_storage
Definition: field.h:96
#define secp256k1_fe_inv_var
Definition: field.h:99
#define secp256k1_fe_set_b32_limit
Definition: field.h:88
#define SECP256K1_FE_VERIFY(a)
Definition: field.h:352
#define secp256k1_fe_is_square_var
Definition: field.h:103
#define secp256k1_fe_from_storage
Definition: field.h:97
#define secp256k1_fe_get_b32
Definition: field.h:89
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
#define secp256k1_fe_inv
Definition: field.h:98
#define secp256k1_fe_sqr
Definition: field.h:94
#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 1, set *r equal to *a; if flag is 0, leave it.
#define secp256k1_fe_add_int
Definition: field.h:102
#define secp256k1_fe_set_int(r, a)
Set a field element to an integer in range [0,0x7FFF].
Definition: field.h:145
#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:242
#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:238
#define SECP256K1_GEJ_X_MAGNITUDE_MAX
Definition: group.h:51
static SECP256K1_INLINE void secp256k1_ge_impl_serialize33(secp256k1_ge *elem, unsigned char *pub33)
Definition: group_impl.h:1111
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Definition: group_impl.h:405
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:557
static SECP256K1_INLINE int secp256k1_ge_impl_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:361
static SECP256K1_INLINE void secp256k1_ge_impl_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:98
static SECP256K1_INLINE void secp256k1_ge_impl_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:162
static SECP256K1_INLINE void secp256k1_gej_impl_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:531
static SECP256K1_INLINE void secp256k1_ge_impl_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:132
static SECP256K1_INLINE void secp256k1_gej_impl_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:455
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:754
static SECP256K1_INLINE void secp256k1_gej_impl_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv)
Definition: group_impl.h:691
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:23
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:353
static SECP256K1_INLINE int secp256k1_ge_impl_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
Definition: group_impl.h:1084
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:963
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:336
static void secp256k1_ge_serialize65(secp256k1_ge *elem, unsigned char *pub65)
Definition: group_impl.h:1134
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:472
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:357
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:137
static SECP256K1_INLINE void secp256k1_gej_impl_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:899
static void secp256k1_gej_verify(const secp256k1_gej *a)
Definition: group_impl.h:87
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:378
static SECP256K1_INLINE void secp256k1_gej_impl_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:386
static void secp256k1_ge_verify(const secp256k1_ge *a)
Definition: group_impl.h:78
static SECP256K1_INLINE void secp256k1_ge_impl_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:916
static int secp256k1_ge_eq_var(const secp256k1_ge *a, const secp256k1_ge *b)
Definition: group_impl.h:438
static int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x)
Definition: group_impl.h:1002
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:685
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge)
Definition: group_impl.h:1066
static SECP256K1_INLINE void secp256k1_ge_impl_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:151
static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
Definition: group_impl.h:947
static SECP256K1_INLINE int secp256k1_gej_impl_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Definition: group_impl.h:398
static SECP256K1_INLINE int secp256k1_ge_impl_eq_var(const secp256k1_ge *a, const secp256k1_ge *b)
Definition: group_impl.h:422
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:893
#define SECP256K1_G
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition: group_impl.h:38
static SECP256K1_INLINE void secp256k1_ge_impl_to_bytes(unsigned char *buf, const secp256k1_ge *a)
Definition: group_impl.h:1031
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:109
static SECP256K1_INLINE void secp256k1_ge_impl_from_bytes(secp256k1_ge *r, const unsigned char *buf)
Definition: group_impl.h:1047
#define SECP256K1_B
Definition: group_impl.h:73
static SECP256K1_INLINE void secp256k1_gej_impl_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Definition: group_impl.h:628
static int secp256k1_gej_eq_ge_var(const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:417
static SECP256K1_INLINE int secp256k1_ge_impl_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd)
Definition: group_impl.h:1007
static SECP256K1_INLINE void secp256k1_ge_impl_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:244
static SECP256K1_INLINE void secp256k1_ge_impl_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge)
Definition: group_impl.h:1059
static int secp256k1_ge_parse_ext33(secp256k1_ge *ge, const unsigned char *in33)
Definition: group_impl.h:1166
static SECP256K1_INLINE int secp256k1_gej_impl_eq_ge_var(const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:410
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:489
static void secp256k1_ge_serialize33(secp256k1_ge *elem, unsigned char *pub33)
Definition: group_impl.h:1119
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data)
Definition: group_impl.h:1079
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:932
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:622
static SECP256K1_INLINE void secp256k1_ge_impl_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:959
static SECP256K1_INLINE int secp256k1_ge_impl_parse_ext33(secp256k1_ge *ge, const unsigned char *in33)
Definition: group_impl.h:1154
static SECP256K1_INLINE int secp256k1_gej_impl_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:469
static SECP256K1_INLINE int secp256k1_ge_impl_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:969
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd)
Definition: group_impl.h:1026
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:910
static void secp256k1_ge_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi)
Definition: group_impl.h:126
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:450
#define SECP256K1_G_ORDER_7
Definition: group_impl.h:17
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:175
static void secp256k1_ge_serialize_ext33(unsigned char *out33, secp256k1_ge *ge)
Definition: group_impl.h:1148
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:989
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Definition: group_impl.h:320
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:156
static SECP256K1_INLINE void secp256k1_gej_impl_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:563
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:72
static SECP256K1_INLINE void secp256k1_ge_impl_set_all_gej(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:203
static void secp256k1_ge_set_all_gej(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:228
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:146
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Definition: group_impl.h:345
static SECP256K1_INLINE void secp256k1_ge_impl_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi)
Definition: group_impl.h:115
static int secp256k1_ge_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
Definition: group_impl.h:1103
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:285
static SECP256K1_INLINE void secp256k1_ge_impl_serialize65(secp256k1_ge *elem, unsigned char *pub65)
Definition: group_impl.h:1125
static SECP256K1_INLINE void secp256k1_gej_impl_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
Definition: group_impl.h:940
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:392
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:927
static SECP256K1_INLINE int secp256k1_gej_impl_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:443
static SECP256K1_INLINE void secp256k1_ge_impl_serialize_ext33(unsigned char *out33, secp256k1_ge *ge)
Definition: group_impl.h:1140
static SECP256K1_INLINE int secp256k1_ge_impl_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:143
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a)
Definition: group_impl.h:1042
static SECP256K1_INLINE void secp256k1_gej_impl_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:761
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:953
#define SECP256K1_G_ORDER_199
Definition: group_impl.h:29
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:197
static SECP256K1_INLINE void secp256k1_ge_impl_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data)
Definition: group_impl.h:1071
static SECP256K1_INLINE int secp256k1_ge_impl_x_on_curve_var(const secp256k1_fe *x)
Definition: group_impl.h:994
static SECP256K1_INLINE void secp256k1_ge_impl_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:181
static SECP256K1_INLINE void secp256k1_ge_impl_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Definition: group_impl.h:300
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:463
static SECP256K1_INLINE int secp256k1_ge_impl_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:477
static SECP256K1_INLINE void secp256k1_gej_impl_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:494
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf)
Definition: group_impl.h:1054
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:525
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
Definition: util.h:268
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:281
#define SECP256K1_INLINE
Definition: util.h:53
#define STATIC_ASSERT(expr)
Assert statically that expr is true.
Definition: util.h:74
#define VERIFY_CHECK(cond)
Definition: util.h:169
#define SECP256K1_TAG_PUBKEY_EVEN
Prefix byte used to tag various encoded curvepoints for specific purposes.
Definition: secp256k1.h:220
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD
Definition: secp256k1.h:224
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN
Definition: secp256k1.h:223
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED
Definition: secp256k1.h:222
#define SECP256K1_TAG_PUBKEY_ODD
Definition: secp256k1.h:221
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