Bitcoin Core  26.1.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 "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 )
33 
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 
76 static void secp256k1_ge_verify(const secp256k1_ge *a) {
77 #ifdef VERIFY
82  VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
83 #endif
84  (void)a;
85 }
86 
87 static void secp256k1_gej_verify(const secp256k1_gej *a) {
88 #ifdef VERIFY
95  VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
96 #endif
97  (void)a;
98 }
99 
100 /* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
101 static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
102  secp256k1_fe zi2;
103  secp256k1_fe zi3;
106  VERIFY_CHECK(!a->infinity);
107 
108  secp256k1_fe_sqr(&zi2, zi);
109  secp256k1_fe_mul(&zi3, &zi2, zi);
110  secp256k1_fe_mul(&r->x, &a->x, &zi2);
111  secp256k1_fe_mul(&r->y, &a->y, &zi3);
112  r->infinity = a->infinity;
113 
115 }
116 
117 /* Set r to the affine coordinates of Jacobian point (a.x, a.y, 1/zi). */
118 static void secp256k1_ge_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi) {
119  secp256k1_fe zi2;
120  secp256k1_fe zi3;
123  VERIFY_CHECK(!a->infinity);
124 
125  secp256k1_fe_sqr(&zi2, zi);
126  secp256k1_fe_mul(&zi3, &zi2, zi);
127  secp256k1_fe_mul(&r->x, &a->x, &zi2);
128  secp256k1_fe_mul(&r->y, &a->y, &zi3);
129  r->infinity = a->infinity;
130 
132 }
133 
134 static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
137 
138  r->infinity = 0;
139  r->x = *x;
140  r->y = *y;
141 
143 }
144 
147 
148  return a->infinity;
149 }
150 
151 static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
153 
154  *r = *a;
156  secp256k1_fe_negate(&r->y, &r->y, 1);
157 
159 }
160 
162  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 
177 }
178 
180  secp256k1_fe z2, z3;
182 
183  if (secp256k1_gej_is_infinity(a)) {
185  return;
186  }
187  r->infinity = 0;
188  secp256k1_fe_inv_var(&a->z, &a->z);
189  secp256k1_fe_sqr(&z2, &a->z);
190  secp256k1_fe_mul(&z3, &a->z, &z2);
191  secp256k1_fe_mul(&a->x, &a->x, &z2);
192  secp256k1_fe_mul(&a->y, &a->y, &z3);
193  secp256k1_fe_set_int(&a->z, 1);
194  secp256k1_ge_set_xy(r, &a->x, &a->y);
195 
198 }
199 
200 static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
201  secp256k1_fe u;
202  size_t i;
203  size_t last_i = SIZE_MAX;
204 #ifdef VERIFY
205  for (i = 0; i < len; i++) {
206  secp256k1_gej_verify(&a[i]);
207  }
208 #endif
209 
210  for (i = 0; i < len; i++) {
211  if (a[i].infinity) {
213  } else {
214  /* Use destination's x coordinates as scratch space */
215  if (last_i == SIZE_MAX) {
216  r[i].x = a[i].z;
217  } else {
218  secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
219  }
220  last_i = i;
221  }
222  }
223  if (last_i == SIZE_MAX) {
224  return;
225  }
226  secp256k1_fe_inv_var(&u, &r[last_i].x);
227 
228  i = last_i;
229  while (i > 0) {
230  i--;
231  if (!a[i].infinity) {
232  secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
233  secp256k1_fe_mul(&u, &u, &a[last_i].z);
234  last_i = i;
235  }
236  }
237  VERIFY_CHECK(!a[last_i].infinity);
238  r[last_i].x = u;
239 
240  for (i = 0; i < len; i++) {
241  if (!a[i].infinity) {
242  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
243  }
244  }
245 
246 #ifdef VERIFY
247  for (i = 0; i < len; i++) {
248  secp256k1_ge_verify(&r[i]);
249  }
250 #endif
251 }
252 
253 static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) {
254  size_t i;
255  secp256k1_fe zs;
256 #ifdef VERIFY
257  for (i = 0; i < len; i++) {
258  secp256k1_ge_verify(&a[i]);
259  secp256k1_fe_verify(&zr[i]);
260  }
261 #endif
262 
263  if (len > 0) {
264  i = len - 1;
265  /* Ensure all y values are in weak normal form for fast negation of points */
267  zs = zr[i];
268 
269  /* Work our way backwards, using the z-ratios to scale the x/y values. */
270  while (i > 0) {
271  if (i != len - 1) {
272  secp256k1_fe_mul(&zs, &zs, &zr[i]);
273  }
274  i--;
275  secp256k1_ge_set_ge_zinv(&a[i], &a[i], &zs);
276  }
277  }
278 
279 #ifdef VERIFY
280  for (i = 0; i < len; i++) {
281  secp256k1_ge_verify(&a[i]);
282  }
283 #endif
284 }
285 
287  r->infinity = 1;
288  secp256k1_fe_clear(&r->x);
289  secp256k1_fe_clear(&r->y);
290  secp256k1_fe_clear(&r->z);
291 
293 }
294 
296  r->infinity = 1;
297  secp256k1_fe_clear(&r->x);
298  secp256k1_fe_clear(&r->y);
299 
301 }
302 
304  r->infinity = 0;
305  secp256k1_fe_clear(&r->x);
306  secp256k1_fe_clear(&r->y);
307  secp256k1_fe_clear(&r->z);
308 
310 }
311 
313  r->infinity = 0;
314  secp256k1_fe_clear(&r->x);
315  secp256k1_fe_clear(&r->y);
316 
318 }
319 
320 static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
321  secp256k1_fe x2, x3;
322  int ret;
324 
325  r->x = *x;
326  secp256k1_fe_sqr(&x2, x);
327  secp256k1_fe_mul(&x3, x, &x2);
328  r->infinity = 0;
330  ret = secp256k1_fe_sqrt(&r->y, &x3);
332  if (secp256k1_fe_is_odd(&r->y) != odd) {
333  secp256k1_fe_negate(&r->y, &r->y, 1);
334  }
335 
337  return ret;
338 }
339 
342 
343  r->infinity = a->infinity;
344  r->x = a->x;
345  r->y = a->y;
346  secp256k1_fe_set_int(&r->z, 1);
347 
349 }
350 
351 static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b) {
352  secp256k1_gej tmp;
355 
356  secp256k1_gej_neg(&tmp, a);
357  secp256k1_gej_add_var(&tmp, &tmp, b, NULL);
358  return secp256k1_gej_is_infinity(&tmp);
359 }
360 
361 static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
362  secp256k1_fe r;
365 #ifdef VERIFY
366  VERIFY_CHECK(!a->infinity);
367 #endif
368 
369  secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
370  return secp256k1_fe_equal(&r, &a->x);
371 }
372 
373 static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
375 
376  r->infinity = a->infinity;
377  r->x = a->x;
378  r->y = a->y;
379  r->z = a->z;
381  secp256k1_fe_negate(&r->y, &r->y, 1);
382 
384 }
385 
388 
389  return a->infinity;
390 }
391 
393  secp256k1_fe y2, x3;
395 
396  if (a->infinity) {
397  return 0;
398  }
399  /* y^2 = x^3 + 7 */
400  secp256k1_fe_sqr(&y2, &a->y);
401  secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
403  return secp256k1_fe_equal(&y2, &x3);
404 }
405 
407  /* Operations: 3 mul, 4 sqr, 8 add/half/mul_int/negate */
408  secp256k1_fe l, s, t;
410 
411  r->infinity = a->infinity;
412 
413  /* Formula used:
414  * L = (3/2) * X1^2
415  * S = Y1^2
416  * T = -X1*S
417  * X3 = L^2 + 2*T
418  * Y3 = -(L*(X3 + T) + S^2)
419  * Z3 = Y1*Z1
420  */
421 
422  secp256k1_fe_mul(&r->z, &a->z, &a->y); /* Z3 = Y1*Z1 (1) */
423  secp256k1_fe_sqr(&s, &a->y); /* S = Y1^2 (1) */
424  secp256k1_fe_sqr(&l, &a->x); /* L = X1^2 (1) */
425  secp256k1_fe_mul_int(&l, 3); /* L = 3*X1^2 (3) */
426  secp256k1_fe_half(&l); /* L = 3/2*X1^2 (2) */
427  secp256k1_fe_negate(&t, &s, 1); /* T = -S (2) */
428  secp256k1_fe_mul(&t, &t, &a->x); /* T = -X1*S (1) */
429  secp256k1_fe_sqr(&r->x, &l); /* X3 = L^2 (1) */
430  secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + T (2) */
431  secp256k1_fe_add(&r->x, &t); /* X3 = L^2 + 2*T (3) */
432  secp256k1_fe_sqr(&s, &s); /* S' = S^2 (1) */
433  secp256k1_fe_add(&t, &r->x); /* T' = X3 + T (4) */
434  secp256k1_fe_mul(&r->y, &t, &l); /* Y3 = L*(X3 + T) (1) */
435  secp256k1_fe_add(&r->y, &s); /* Y3 = L*(X3 + T) + S^2 (2) */
436  secp256k1_fe_negate(&r->y, &r->y, 2); /* Y3 = -(L*(X3 + T) + S^2) (3) */
437 
439 }
440 
443 
454  if (a->infinity) {
456  if (rzr != NULL) {
457  secp256k1_fe_set_int(rzr, 1);
458  }
459  return;
460  }
461 
462  if (rzr != NULL) {
463  *rzr = a->y;
465  }
466 
467  secp256k1_gej_double(r, a);
468 
470 }
471 
473  /* 12 mul, 4 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
474  secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, h2, h3, t;
477 
478  if (a->infinity) {
479  VERIFY_CHECK(rzr == NULL);
480  *r = *b;
481  return;
482  }
483  if (b->infinity) {
484  if (rzr != NULL) {
485  secp256k1_fe_set_int(rzr, 1);
486  }
487  *r = *a;
488  return;
489  }
490 
491  secp256k1_fe_sqr(&z22, &b->z);
492  secp256k1_fe_sqr(&z12, &a->z);
493  secp256k1_fe_mul(&u1, &a->x, &z22);
494  secp256k1_fe_mul(&u2, &b->x, &z12);
495  secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
496  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
497  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
498  secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
501  secp256k1_gej_double_var(r, a, rzr);
502  } else {
503  if (rzr != NULL) {
504  secp256k1_fe_set_int(rzr, 0);
505  }
507  }
508  return;
509  }
510 
511  r->infinity = 0;
512  secp256k1_fe_mul(&t, &h, &b->z);
513  if (rzr != NULL) {
514  *rzr = t;
515  }
516  secp256k1_fe_mul(&r->z, &a->z, &t);
517 
518  secp256k1_fe_sqr(&h2, &h);
519  secp256k1_fe_negate(&h2, &h2, 1);
520  secp256k1_fe_mul(&h3, &h2, &h);
521  secp256k1_fe_mul(&t, &u1, &h2);
522 
523  secp256k1_fe_sqr(&r->x, &i);
524  secp256k1_fe_add(&r->x, &h3);
525  secp256k1_fe_add(&r->x, &t);
526  secp256k1_fe_add(&r->x, &t);
527 
528  secp256k1_fe_add(&t, &r->x);
529  secp256k1_fe_mul(&r->y, &t, &i);
530  secp256k1_fe_mul(&h3, &h3, &s1);
531  secp256k1_fe_add(&r->y, &h3);
532 
534 }
535 
537  /* Operations: 8 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
538  secp256k1_fe z12, u1, u2, s1, s2, h, i, h2, h3, t;
541 
542  if (a->infinity) {
543  VERIFY_CHECK(rzr == NULL);
544  secp256k1_gej_set_ge(r, b);
545  return;
546  }
547  if (b->infinity) {
548  if (rzr != NULL) {
549  secp256k1_fe_set_int(rzr, 1);
550  }
551  *r = *a;
552  return;
553  }
554 
555  secp256k1_fe_sqr(&z12, &a->z);
556  u1 = a->x;
557  secp256k1_fe_mul(&u2, &b->x, &z12);
558  s1 = a->y;
559  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
561  secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
564  secp256k1_gej_double_var(r, a, rzr);
565  } else {
566  if (rzr != NULL) {
567  secp256k1_fe_set_int(rzr, 0);
568  }
570  }
571  return;
572  }
573 
574  r->infinity = 0;
575  if (rzr != NULL) {
576  *rzr = h;
577  }
578  secp256k1_fe_mul(&r->z, &a->z, &h);
579 
580  secp256k1_fe_sqr(&h2, &h);
581  secp256k1_fe_negate(&h2, &h2, 1);
582  secp256k1_fe_mul(&h3, &h2, &h);
583  secp256k1_fe_mul(&t, &u1, &h2);
584 
585  secp256k1_fe_sqr(&r->x, &i);
586  secp256k1_fe_add(&r->x, &h3);
587  secp256k1_fe_add(&r->x, &t);
588  secp256k1_fe_add(&r->x, &t);
589 
590  secp256k1_fe_add(&t, &r->x);
591  secp256k1_fe_mul(&r->y, &t, &i);
592  secp256k1_fe_mul(&h3, &h3, &s1);
593  secp256k1_fe_add(&r->y, &h3);
594 
596  if (rzr != NULL) secp256k1_fe_verify(rzr);
597 }
598 
599 static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
600  /* Operations: 9 mul, 3 sqr, 11 add/negate/normalizes_to_zero (ignoring special cases) */
601  secp256k1_fe az, z12, u1, u2, s1, s2, h, i, h2, h3, t;
604  secp256k1_fe_verify(bzinv);
605 
606  if (a->infinity) {
607  secp256k1_fe bzinv2, bzinv3;
608  r->infinity = b->infinity;
609  secp256k1_fe_sqr(&bzinv2, bzinv);
610  secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
611  secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
612  secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
613  secp256k1_fe_set_int(&r->z, 1);
615  return;
616  }
617  if (b->infinity) {
618  *r = *a;
619  return;
620  }
621 
630  secp256k1_fe_mul(&az, &a->z, bzinv);
631 
632  secp256k1_fe_sqr(&z12, &az);
633  u1 = a->x;
634  secp256k1_fe_mul(&u2, &b->x, &z12);
635  s1 = a->y;
636  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
638  secp256k1_fe_negate(&i, &s2, 1); secp256k1_fe_add(&i, &s1);
641  secp256k1_gej_double_var(r, a, NULL);
642  } else {
644  }
645  return;
646  }
647 
648  r->infinity = 0;
649  secp256k1_fe_mul(&r->z, &a->z, &h);
650 
651  secp256k1_fe_sqr(&h2, &h);
652  secp256k1_fe_negate(&h2, &h2, 1);
653  secp256k1_fe_mul(&h3, &h2, &h);
654  secp256k1_fe_mul(&t, &u1, &h2);
655 
656  secp256k1_fe_sqr(&r->x, &i);
657  secp256k1_fe_add(&r->x, &h3);
658  secp256k1_fe_add(&r->x, &t);
659  secp256k1_fe_add(&r->x, &t);
660 
661  secp256k1_fe_add(&t, &r->x);
662  secp256k1_fe_mul(&r->y, &t, &i);
663  secp256k1_fe_mul(&h3, &h3, &s1);
664  secp256k1_fe_add(&r->y, &h3);
665 
667 }
668 
669 
670 static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
671  /* Operations: 7 mul, 5 sqr, 21 add/cmov/half/mul_int/negate/normalizes_to_zero */
672  secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
673  secp256k1_fe m_alt, rr_alt;
674  int degenerate;
677  VERIFY_CHECK(!b->infinity);
678 
679  /* In:
680  * Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
681  * In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
682  * we find as solution for a unified addition/doubling formula:
683  * lambda = ((x1 + x2)^2 - x1 * x2 + a) / (y1 + y2), with a = 0 for secp256k1's curve equation.
684  * x3 = lambda^2 - (x1 + x2)
685  * 2*y3 = lambda * (x1 + x2 - 2 * x3) - (y1 + y2).
686  *
687  * Substituting x_i = Xi / Zi^2 and yi = Yi / Zi^3, for i=1,2,3, gives:
688  * U1 = X1*Z2^2, U2 = X2*Z1^2
689  * S1 = Y1*Z2^3, S2 = Y2*Z1^3
690  * Z = Z1*Z2
691  * T = U1+U2
692  * M = S1+S2
693  * Q = -T*M^2
694  * R = T^2-U1*U2
695  * X3 = R^2+Q
696  * Y3 = -(R*(2*X3+Q)+M^4)/2
697  * Z3 = M*Z
698  * (Note that the paper uses xi = Xi / Zi and yi = Yi / Zi instead.)
699  *
700  * This formula has the benefit of being the same for both addition
701  * of distinct points and doubling. However, it breaks down in the
702  * case that either point is infinity, or that y1 = -y2. We handle
703  * these cases in the following ways:
704  *
705  * - If b is infinity we simply bail by means of a VERIFY_CHECK.
706  *
707  * - If a is infinity, we detect this, and at the end of the
708  * computation replace the result (which will be meaningless,
709  * but we compute to be constant-time) with b.x : b.y : 1.
710  *
711  * - If a = -b, we have y1 = -y2, which is a degenerate case.
712  * But here the answer is infinity, so we simply set the
713  * infinity flag of the result, overriding the computed values
714  * without even needing to cmov.
715  *
716  * - If y1 = -y2 but x1 != x2, which does occur thanks to certain
717  * properties of our curve (specifically, 1 has nontrivial cube
718  * roots in our field, and the curve equation has no x coefficient)
719  * then the answer is not infinity but also not given by the above
720  * equation. In this case, we cmov in place an alternate expression
721  * for lambda. Specifically (y1 - y2)/(x1 - x2). Where both these
722  * expressions for lambda are defined, they are equal, and can be
723  * obtained from each other by multiplication by (y1 + y2)/(y1 + y2)
724  * then substitution of x^3 + 7 for y^2 (using the curve equation).
725  * For all pairs of nonzero points (a, b) at least one is defined,
726  * so this covers everything.
727  */
728 
729  secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
730  u1 = a->x; /* u1 = U1 = X1*Z2^2 (GEJ_X_M) */
731  secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
732  s1 = a->y; /* s1 = S1 = Y1*Z2^3 (GEJ_Y_M) */
733  secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
734  secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
735  t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (GEJ_X_M+1) */
736  m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (GEJ_Y_M+1) */
737  secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
738  secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 (2) */
739  secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (1) */
740  secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (2) */
741  /* If lambda = R/M = R/0 we have a problem (except in the "trivial"
742  * case that Z = z1z2 = 0, and this is special-cased later on). */
743  degenerate = secp256k1_fe_normalizes_to_zero(&m);
744  /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
745  * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
746  * a nontrivial cube root of one. In either case, an alternate
747  * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
748  * so we set R/M equal to this. */
749  rr_alt = s1;
750  secp256k1_fe_mul_int(&rr_alt, 2); /* rr_alt = Y1*Z2^3 - Y2*Z1^3 (GEJ_Y_M*2) */
751  secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 (GEJ_X_M+2) */
752 
753  secp256k1_fe_cmov(&rr_alt, &rr, !degenerate); /* rr_alt (GEJ_Y_M*2) */
754  secp256k1_fe_cmov(&m_alt, &m, !degenerate); /* m_alt (GEJ_X_M+2) */
755  /* Now Ralt / Malt = lambda and is guaranteed not to be Ralt / 0.
756  * From here on out Ralt and Malt represent the numerator
757  * and denominator of lambda; R and M represent the explicit
758  * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
759  secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
760  secp256k1_fe_negate(&q, &t,
761  SECP256K1_GEJ_X_MAGNITUDE_MAX + 1); /* q = -T (GEJ_X_M+2) */
762  secp256k1_fe_mul(&q, &q, &n); /* q = Q = -T*Malt^2 (1) */
763  /* These two lines use the observation that either M == Malt or M == 0,
764  * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
765  * zero (which is "computed" by cmov). So the cost is one squaring
766  * versus two multiplications. */
767  secp256k1_fe_sqr(&n, &n); /* n = Malt^4 (1) */
768  secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (GEJ_Y_M+1) */
769  secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
770  secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Z3 = Malt*Z (1) */
771  secp256k1_fe_add(&t, &q); /* t = Ralt^2 + Q (2) */
772  r->x = t; /* r->x = X3 = Ralt^2 + Q (2) */
773  secp256k1_fe_mul_int(&t, 2); /* t = 2*X3 (4) */
774  secp256k1_fe_add(&t, &q); /* t = 2*X3 + Q (5) */
775  secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*X3 + Q) (1) */
776  secp256k1_fe_add(&t, &n); /* t = Ralt*(2*X3 + Q) + M^3*Malt (GEJ_Y_M+2) */
777  secp256k1_fe_negate(&r->y, &t,
778  SECP256K1_GEJ_Y_MAGNITUDE_MAX + 2); /* r->y = -(Ralt*(2*X3 + Q) + M^3*Malt) (GEJ_Y_M+3) */
779  secp256k1_fe_half(&r->y); /* r->y = Y3 = -(Ralt*(2*X3 + Q) + M^3*Malt)/2 ((GEJ_Y_M+3)/2 + 1) */
780 
781  /* In case a->infinity == 1, replace r with (b->x, b->y, 1). */
782  secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
783  secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
785 
786  /* Set r->infinity if r->z is 0.
787  *
788  * If a->infinity is set, then r->infinity = (r->z == 0) = (1 == 0) = false,
789  * which is correct because the function assumes that b is not infinity.
790  *
791  * Now assume !a->infinity. This implies Z = Z1 != 0.
792  *
793  * Case y1 = -y2:
794  * In this case we could have a = -b, namely if x1 = x2.
795  * We have degenerate = true, r->z = (x1 - x2) * Z.
796  * Then r->infinity = ((x1 - x2)Z == 0) = (x1 == x2) = (a == -b).
797  *
798  * Case y1 != -y2:
799  * In this case, we can't have a = -b.
800  * We have degenerate = false, r->z = (y1 + y2) * Z.
801  * Then r->infinity = ((y1 + y2)Z == 0) = (y1 == -y2) = false. */
803 
805 }
806 
808  /* Operations: 4 mul, 1 sqr */
809  secp256k1_fe zz;
812 #ifdef VERIFY
814 #endif
815 
816  secp256k1_fe_sqr(&zz, s);
817  secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
818  secp256k1_fe_mul(&r->y, &r->y, &zz);
819  secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
820  secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
821 
823 }
824 
826  secp256k1_fe x, y;
828  VERIFY_CHECK(!a->infinity);
829 
830  x = a->x;
832  y = a->y;
834  secp256k1_fe_to_storage(&r->x, &x);
835  secp256k1_fe_to_storage(&r->y, &y);
836 }
837 
839  secp256k1_fe_from_storage(&r->x, &a->x);
840  secp256k1_fe_from_storage(&r->y, &a->y);
841  r->infinity = 0;
842 
844 }
845 
846 static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag) {
849 
850  secp256k1_fe_cmov(&r->x, &a->x, flag);
851  secp256k1_fe_cmov(&r->y, &a->y, flag);
852  secp256k1_fe_cmov(&r->z, &a->z, flag);
853  r->infinity ^= (r->infinity ^ a->infinity) & flag;
854 
856 }
857 
859  secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
860  secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
861 }
862 
865 
866  *r = *a;
868 
870 }
871 
873 #ifdef EXHAUSTIVE_TEST_ORDER
875  int i;
877 
878  /* A very simple EC multiplication ladder that avoids a dependency on ecmult. */
880  for (i = 0; i < 32; ++i) {
881  secp256k1_gej_double_var(&out, &out, NULL);
882  if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
883  secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
884  }
885  }
887 #else
889 
890  (void)ge;
891  /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
892  return 1;
893 #endif
894 }
895 
897  secp256k1_fe c;
898  secp256k1_fe_sqr(&c, x);
899  secp256k1_fe_mul(&c, &c, x);
901  return secp256k1_fe_is_square_var(&c);
902 }
903 
905  /* We want to determine whether (xn/xd) is on the curve.
906  *
907  * (xn/xd)^3 + 7 is square <=> xd*xn^3 + 7*xd^4 is square (multiplying by xd^4, a square).
908  */
909  secp256k1_fe r, t;
910 #ifdef VERIFY
912 #endif
913  secp256k1_fe_mul(&r, xd, xn); /* r = xd*xn */
914  secp256k1_fe_sqr(&t, xn); /* t = xn^2 */
915  secp256k1_fe_mul(&r, &r, &t); /* r = xd*xn^3 */
916  secp256k1_fe_sqr(&t, xd); /* t = xd^2 */
917  secp256k1_fe_sqr(&t, &t); /* t = xd^4 */
918  VERIFY_CHECK(SECP256K1_B <= 31);
919  secp256k1_fe_mul_int(&t, SECP256K1_B); /* t = 7*xd^4 */
920  secp256k1_fe_add(&r, &t); /* r = xd*xn^3 + 7*xd^4 */
921  return secp256k1_fe_is_square_var(&r);
922 }
923 
924 #endif /* SECP256K1_GROUP_IMPL_H */
#define VERIFY_CHECK(cond)
Definition: util.h:143
This field implementation represents the value as 10 uint32_t limbs in base 2^26. ...
Definition: field_10x26.h:14
int ret
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Definition: group_impl.h:253
#define SECP256K1_G_ORDER_199
Definition: group_impl.h:27
#define secp256k1_fe_add_int
Definition: field.h:103
static int secp256k1_fe_sqrt(secp256k1_fe *SECP256K1_RESTRICT r, const secp256k1_fe *SECP256K1_RESTRICT a)
Compute a square root of a field element.
secp256k1_fe x
Definition: group.h:29
#define secp256k1_fe_inv_var
Definition: field.h:100
#define secp256k1_fe_mul
Definition: field.h:94
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:807
#define secp256k1_fe_clear
Definition: field.h:84
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:179
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:151
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:441
#define SECP256K1_GEJ_Z_MAGNITUDE_MAX
Definition: group.h:53
#define secp256k1_fe_mul_int(r, a)
Multiply a field element with a small integer.
Definition: field.h:237
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:536
#define secp256k1_fe_is_square_var
Definition: field.h:104
#define secp256k1_fe_half
Definition: field.h:102
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize_weak
Definition: field.h:79
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:303
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
Definition: field.h:93
#define SECP256K1_G
Generator for secp256k1, value &#39;g&#39; defined in "Standards for Efficient Cryptography" (SEC2) 2...
Definition: group_impl.h:36
secp256k1_fe_storage y
Definition: group.h:40
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:161
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
#define secp256k1_fe_cmov
Definition: field.h:96
#define SECP256K1_B
Definition: group_impl.h:71
#define SECP256K1_INLINE
Definition: util.h:48
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:670
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:145
#define secp256k1_fe_normalizes_to_zero_var
Definition: field.h:82
#define SECP256K1_GEJ_X_MAGNITUDE_MAX
Definition: group.h:51
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:340
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:361
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:872
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:70
static void secp256k1_gej_verify(const secp256k1_gej *a)
Definition: group_impl.h:87
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:838
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:134
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:286
static int secp256k1_gej_eq_var(const secp256k1_gej *a, const secp256k1_gej *b)
Definition: group_impl.h:351
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:373
static void secp256k1_fe_verify(const secp256k1_fe *a)
Check invariants on a field element (no-op unless VERIFY is enabled).
static const secp256k1_fe secp256k1_fe_one
Definition: field.h:68
int infinity
Definition: group.h:32
#define secp256k1_fe_inv
Definition: field.h:99
secp256k1_fe_storage x
Definition: group.h:39
#define secp256k1_fe_is_odd
Definition: field.h:86
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:863
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:200
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:320
#define secp256k1_fe_set_int
Definition: field.h:83
static int secp256k1_ge_x_frac_on_curve_var(const secp256k1_fe *xn, const secp256k1_fe *xd)
Definition: group_impl.h:904
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
#define SECP256K1_G_ORDER_7
Definition: group_impl.h:15
static void secp256k1_fe_verify_magnitude(const secp256k1_fe *a, int m)
Check that magnitude of a is at most m (no-op unless VERIFY is enabled).
#define secp256k1_fe_to_storage
Definition: field.h:97
static void secp256k1_ge_verify(const secp256k1_ge *a)
Definition: group_impl.h:76
int infinity
Definition: group.h:19
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Definition: group_impl.h:295
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:21
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:858
#define SECP256K1_GE_X_MAGNITUDE_MAX
Maximum allowed magnitudes for group element coordinates in affine (x, y) and jacobian (x...
Definition: group.h:49
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:599
static const secp256k1_fe secp256k1_const_beta
Definition: field.h:69
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:825
#define SECP256K1_GEJ_Y_MAGNITUDE_MAX
Definition: group.h:52
#define EXHAUSTIVE_TEST_ORDER
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:386
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:101
secp256k1_fe z
Definition: group.h:31
#define secp256k1_fe_normalize
Definition: field.h:78
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:406
#define SECP256K1_GE_Y_MAGNITUDE_MAX
Definition: group.h:50
#define secp256k1_fe_normalize_var
Definition: field.h:80
static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
Definition: group_impl.h:846
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:392
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
Definition: field.h:215
secp256k1_fe y
Definition: group.h:30
static void secp256k1_ge_set_ge_zinv(secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zi)
Definition: group_impl.h:118
static int secp256k1_ge_x_on_curve_var(const secp256k1_fe *x)
Definition: group_impl.h:896
secp256k1_fe y
Definition: group.h:18
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:472
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:312
#define secp256k1_fe_from_storage
Definition: field.h:98