Bitcoin Core  22.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 "field.h"
11 #include "group.h"
12 
13 #define SECP256K1_G_ORDER_13 SECP256K1_GE_CONST(\
14  0xc3459c3d, 0x35326167, 0xcd86cce8, 0x07a2417f,\
15  0x5b8bd567, 0xde8538ee, 0x0d507b0c, 0xd128f5bb,\
16  0x8e467fec, 0xcd30000a, 0x6cc1184e, 0x25d382c2,\
17  0xa2f4494e, 0x2fbe9abc, 0x8b64abac, 0xd005fb24\
18 )
19 #define SECP256K1_G_ORDER_199 SECP256K1_GE_CONST(\
20  0x226e653f, 0xc8df7744, 0x9bacbf12, 0x7d1dcbf9,\
21  0x87f05b2a, 0xe7edbd28, 0x1f564575, 0xc48dcf18,\
22  0xa13872c2, 0xe933bb17, 0x5d9ffd5b, 0xb5b6e10c,\
23  0x57fe3c00, 0xbaaaa15a, 0xe003ec3e, 0x9c269bae\
24 )
25 
28 #define SECP256K1_G SECP256K1_GE_CONST(\
29  0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,\
30  0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,\
31  0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,\
32  0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL\
33 )
34 /* These exhaustive group test orders and generators are chosen such that:
35  * - The field size is equal to that of secp256k1, so field code is the same.
36  * - The curve equation is of the form y^2=x^3+B for some constant B.
37  * - The subgroup has a generator 2*P, where P.x=1.
38  * - The subgroup has size less than 1000 to permit exhaustive testing.
39  * - The subgroup admits an endomorphism of the form lambda*(x,y) == (beta*x,y).
40  *
41  * These parameters are generated using sage/gen_exhaustive_groups.sage.
42  */
43 #if defined(EXHAUSTIVE_TEST_ORDER)
44 # if EXHAUSTIVE_TEST_ORDER == 13
46 
48  0x3d3486b2, 0x159a9ca5, 0xc75638be, 0xb23a69bc,
49  0x946a45ab, 0x24801247, 0xb4ed2b8e, 0x26b6a417
50 );
51 # elif EXHAUSTIVE_TEST_ORDER == 199
53 
55  0x2cca28fa, 0xfc614b80, 0x2a3db42b, 0x00ba00b1,
56  0xbea8d943, 0xdace9ab2, 0x9536daea, 0x0074defb
57 );
58 # else
59 # error No known generator for the specified exhaustive test group order.
60 # endif
61 #else
63 
64 static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 7);
65 #endif
66 
67 static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
68  secp256k1_fe zi2;
69  secp256k1_fe zi3;
71  secp256k1_fe_sqr(&zi2, zi);
72  secp256k1_fe_mul(&zi3, &zi2, zi);
73  secp256k1_fe_mul(&r->x, &a->x, &zi2);
74  secp256k1_fe_mul(&r->y, &a->y, &zi3);
75  r->infinity = a->infinity;
76 }
77 
78 static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
79  r->infinity = 0;
80  r->x = *x;
81  r->y = *y;
82 }
83 
84 static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
85  return a->infinity;
86 }
87 
88 static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
89  *r = *a;
91  secp256k1_fe_negate(&r->y, &r->y, 1);
92 }
93 
95  secp256k1_fe z2, z3;
96  r->infinity = a->infinity;
97  secp256k1_fe_inv(&a->z, &a->z);
98  secp256k1_fe_sqr(&z2, &a->z);
99  secp256k1_fe_mul(&z3, &a->z, &z2);
100  secp256k1_fe_mul(&a->x, &a->x, &z2);
101  secp256k1_fe_mul(&a->y, &a->y, &z3);
102  secp256k1_fe_set_int(&a->z, 1);
103  r->x = a->x;
104  r->y = a->y;
105 }
106 
108  secp256k1_fe z2, z3;
109  if (a->infinity) {
111  return;
112  }
113  secp256k1_fe_inv_var(&a->z, &a->z);
114  secp256k1_fe_sqr(&z2, &a->z);
115  secp256k1_fe_mul(&z3, &a->z, &z2);
116  secp256k1_fe_mul(&a->x, &a->x, &z2);
117  secp256k1_fe_mul(&a->y, &a->y, &z3);
118  secp256k1_fe_set_int(&a->z, 1);
119  secp256k1_ge_set_xy(r, &a->x, &a->y);
120 }
121 
122 static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len) {
123  secp256k1_fe u;
124  size_t i;
125  size_t last_i = SIZE_MAX;
126 
127  for (i = 0; i < len; i++) {
128  if (a[i].infinity) {
130  } else {
131  /* Use destination's x coordinates as scratch space */
132  if (last_i == SIZE_MAX) {
133  r[i].x = a[i].z;
134  } else {
135  secp256k1_fe_mul(&r[i].x, &r[last_i].x, &a[i].z);
136  }
137  last_i = i;
138  }
139  }
140  if (last_i == SIZE_MAX) {
141  return;
142  }
143  secp256k1_fe_inv_var(&u, &r[last_i].x);
144 
145  i = last_i;
146  while (i > 0) {
147  i--;
148  if (!a[i].infinity) {
149  secp256k1_fe_mul(&r[last_i].x, &r[i].x, &u);
150  secp256k1_fe_mul(&u, &u, &a[last_i].z);
151  last_i = i;
152  }
153  }
154  VERIFY_CHECK(!a[last_i].infinity);
155  r[last_i].x = u;
156 
157  for (i = 0; i < len; i++) {
158  if (!a[i].infinity) {
159  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &r[i].x);
160  }
161  }
162 }
163 
164 static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr) {
165  size_t i = len - 1;
166  secp256k1_fe zs;
167 
168  if (len > 0) {
169  /* The z of the final point gives us the "global Z" for the table. */
170  r[i].x = a[i].x;
171  r[i].y = a[i].y;
172  /* Ensure all y values are in weak normal form for fast negation of points */
174  *globalz = a[i].z;
175  r[i].infinity = 0;
176  zs = zr[i];
177 
178  /* Work our way backwards, using the z-ratios to scale the x/y values. */
179  while (i > 0) {
180  if (i != len - 1) {
181  secp256k1_fe_mul(&zs, &zs, &zr[i]);
182  }
183  i--;
184  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
185  }
186  }
187 }
188 
190  r->infinity = 1;
191  secp256k1_fe_clear(&r->x);
192  secp256k1_fe_clear(&r->y);
193  secp256k1_fe_clear(&r->z);
194 }
195 
197  r->infinity = 1;
198  secp256k1_fe_clear(&r->x);
199  secp256k1_fe_clear(&r->y);
200 }
201 
203  r->infinity = 0;
204  secp256k1_fe_clear(&r->x);
205  secp256k1_fe_clear(&r->y);
206  secp256k1_fe_clear(&r->z);
207 }
208 
210  r->infinity = 0;
211  secp256k1_fe_clear(&r->x);
212  secp256k1_fe_clear(&r->y);
213 }
214 
215 static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
216  secp256k1_fe x2, x3;
217  r->x = *x;
218  secp256k1_fe_sqr(&x2, x);
219  secp256k1_fe_mul(&x3, x, &x2);
220  r->infinity = 0;
222  if (!secp256k1_fe_sqrt(&r->y, &x3)) {
223  return 0;
224  }
226  if (secp256k1_fe_is_odd(&r->y) != odd) {
227  secp256k1_fe_negate(&r->y, &r->y, 1);
228  }
229  return 1;
230 
231 }
232 
234  r->infinity = a->infinity;
235  r->x = a->x;
236  r->y = a->y;
237  secp256k1_fe_set_int(&r->z, 1);
238 }
239 
240 static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
241  secp256k1_fe r, r2;
242  VERIFY_CHECK(!a->infinity);
243  secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
244  r2 = a->x; secp256k1_fe_normalize_weak(&r2);
245  return secp256k1_fe_equal_var(&r, &r2);
246 }
247 
248 static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
249  r->infinity = a->infinity;
250  r->x = a->x;
251  r->y = a->y;
252  r->z = a->z;
254  secp256k1_fe_negate(&r->y, &r->y, 1);
255 }
256 
258  return a->infinity;
259 }
260 
262  secp256k1_fe y2, x3;
263  if (a->infinity) {
264  return 0;
265  }
266  /* y^2 = x^3 + 7 */
267  secp256k1_fe_sqr(&y2, &a->y);
268  secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
271  return secp256k1_fe_equal_var(&y2, &x3);
272 }
273 
275  /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
276  *
277  * Note that there is an implementation described at
278  * https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
279  * which trades a multiply for a square, but in practice this is actually slower,
280  * mainly because it requires more normalizations.
281  */
282  secp256k1_fe t1,t2,t3,t4;
283 
284  r->infinity = a->infinity;
285 
286  secp256k1_fe_mul(&r->z, &a->z, &a->y);
287  secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
288  secp256k1_fe_sqr(&t1, &a->x);
289  secp256k1_fe_mul_int(&t1, 3); /* T1 = 3*X^2 (3) */
290  secp256k1_fe_sqr(&t2, &t1); /* T2 = 9*X^4 (1) */
291  secp256k1_fe_sqr(&t3, &a->y);
292  secp256k1_fe_mul_int(&t3, 2); /* T3 = 2*Y^2 (2) */
293  secp256k1_fe_sqr(&t4, &t3);
294  secp256k1_fe_mul_int(&t4, 2); /* T4 = 8*Y^4 (2) */
295  secp256k1_fe_mul(&t3, &t3, &a->x); /* T3 = 2*X*Y^2 (1) */
296  r->x = t3;
297  secp256k1_fe_mul_int(&r->x, 4); /* X' = 8*X*Y^2 (4) */
298  secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
299  secp256k1_fe_add(&r->x, &t2); /* X' = 9*X^4 - 8*X*Y^2 (6) */
300  secp256k1_fe_negate(&t2, &t2, 1); /* T2 = -9*X^4 (2) */
301  secp256k1_fe_mul_int(&t3, 6); /* T3 = 12*X*Y^2 (6) */
302  secp256k1_fe_add(&t3, &t2); /* T3 = 12*X*Y^2 - 9*X^4 (8) */
303  secp256k1_fe_mul(&r->y, &t1, &t3); /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
304  secp256k1_fe_negate(&t2, &t4, 2); /* T2 = -8*Y^4 (3) */
305  secp256k1_fe_add(&r->y, &t2); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
306 }
307 
319  if (a->infinity) {
321  if (rzr != NULL) {
322  secp256k1_fe_set_int(rzr, 1);
323  }
324  return;
325  }
326 
327  if (rzr != NULL) {
328  *rzr = a->y;
330  secp256k1_fe_mul_int(rzr, 2);
331  }
332 
333  secp256k1_gej_double(r, a);
334 }
335 
337  /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
338  secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
339 
340  if (a->infinity) {
341  VERIFY_CHECK(rzr == NULL);
342  *r = *b;
343  return;
344  }
345 
346  if (b->infinity) {
347  if (rzr != NULL) {
348  secp256k1_fe_set_int(rzr, 1);
349  }
350  *r = *a;
351  return;
352  }
353 
354  r->infinity = 0;
355  secp256k1_fe_sqr(&z22, &b->z);
356  secp256k1_fe_sqr(&z12, &a->z);
357  secp256k1_fe_mul(&u1, &a->x, &z22);
358  secp256k1_fe_mul(&u2, &b->x, &z12);
359  secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
360  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
361  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
362  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
365  secp256k1_gej_double_var(r, a, rzr);
366  } else {
367  if (rzr != NULL) {
368  secp256k1_fe_set_int(rzr, 0);
369  }
371  }
372  return;
373  }
374  secp256k1_fe_sqr(&i2, &i);
375  secp256k1_fe_sqr(&h2, &h);
376  secp256k1_fe_mul(&h3, &h, &h2);
377  secp256k1_fe_mul(&h, &h, &b->z);
378  if (rzr != NULL) {
379  *rzr = h;
380  }
381  secp256k1_fe_mul(&r->z, &a->z, &h);
382  secp256k1_fe_mul(&t, &u1, &h2);
383  r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
384  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
385  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
386  secp256k1_fe_add(&r->y, &h3);
387 }
388 
390  /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
391  secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
392  if (a->infinity) {
393  VERIFY_CHECK(rzr == NULL);
394  secp256k1_gej_set_ge(r, b);
395  return;
396  }
397  if (b->infinity) {
398  if (rzr != NULL) {
399  secp256k1_fe_set_int(rzr, 1);
400  }
401  *r = *a;
402  return;
403  }
404  r->infinity = 0;
405 
406  secp256k1_fe_sqr(&z12, &a->z);
407  u1 = a->x; secp256k1_fe_normalize_weak(&u1);
408  secp256k1_fe_mul(&u2, &b->x, &z12);
409  s1 = a->y; secp256k1_fe_normalize_weak(&s1);
410  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
411  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
412  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
415  secp256k1_gej_double_var(r, a, rzr);
416  } else {
417  if (rzr != NULL) {
418  secp256k1_fe_set_int(rzr, 0);
419  }
421  }
422  return;
423  }
424  secp256k1_fe_sqr(&i2, &i);
425  secp256k1_fe_sqr(&h2, &h);
426  secp256k1_fe_mul(&h3, &h, &h2);
427  if (rzr != NULL) {
428  *rzr = h;
429  }
430  secp256k1_fe_mul(&r->z, &a->z, &h);
431  secp256k1_fe_mul(&t, &u1, &h2);
432  r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
433  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
434  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
435  secp256k1_fe_add(&r->y, &h3);
436 }
437 
438 static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
439  /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
440  secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
441 
442  if (b->infinity) {
443  *r = *a;
444  return;
445  }
446  if (a->infinity) {
447  secp256k1_fe bzinv2, bzinv3;
448  r->infinity = b->infinity;
449  secp256k1_fe_sqr(&bzinv2, bzinv);
450  secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
451  secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
452  secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
453  secp256k1_fe_set_int(&r->z, 1);
454  return;
455  }
456  r->infinity = 0;
457 
466  secp256k1_fe_mul(&az, &a->z, bzinv);
467 
468  secp256k1_fe_sqr(&z12, &az);
469  u1 = a->x; secp256k1_fe_normalize_weak(&u1);
470  secp256k1_fe_mul(&u2, &b->x, &z12);
471  s1 = a->y; secp256k1_fe_normalize_weak(&s1);
472  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
473  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
474  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
477  secp256k1_gej_double_var(r, a, NULL);
478  } else {
480  }
481  return;
482  }
483  secp256k1_fe_sqr(&i2, &i);
484  secp256k1_fe_sqr(&h2, &h);
485  secp256k1_fe_mul(&h3, &h, &h2);
486  r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
487  secp256k1_fe_mul(&t, &u1, &h2);
488  r->x = t; secp256k1_fe_mul_int(&r->x, 2); secp256k1_fe_add(&r->x, &h3); secp256k1_fe_negate(&r->x, &r->x, 3); secp256k1_fe_add(&r->x, &i2);
489  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
490  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
491  secp256k1_fe_add(&r->y, &h3);
492 }
493 
494 
495 static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
496  /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
497  static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
498  secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
499  secp256k1_fe m_alt, rr_alt;
500  int infinity, degenerate;
501  VERIFY_CHECK(!b->infinity);
502  VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
503 
554  secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
555  u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
556  secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
557  s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
558  secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
559  secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
560  t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
561  m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
562  secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
563  secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
564  secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
565  secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
568  degenerate = secp256k1_fe_normalizes_to_zero(&m) &
570  /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
571  * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
572  * a nontrivial cube root of one. In either case, an alternate
573  * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
574  * so we set R/M equal to this. */
575  rr_alt = s1;
576  secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
577  secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
578 
579  secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
580  secp256k1_fe_cmov(&m_alt, &m, !degenerate);
581  /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
582  * From here on out Ralt and Malt represent the numerator
583  * and denominator of lambda; R and M represent the explicit
584  * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
585  secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
586  secp256k1_fe_mul(&q, &n, &t); /* q = Q = T*Malt^2 (1) */
587  /* These two lines use the observation that either M == Malt or M == 0,
588  * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
589  * zero (which is "computed" by cmov). So the cost is one squaring
590  * versus two multiplications. */
591  secp256k1_fe_sqr(&n, &n);
592  secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
593  secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
594  secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Malt*Z (1) */
595  infinity = secp256k1_fe_normalizes_to_zero(&r->z) & ~a->infinity;
596  secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
597  secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
598  secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
600  r->x = t; /* r->x = Ralt^2-Q (1) */
601  secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
602  secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
603  secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
604  secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
605  secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
607  secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
608  secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
609 
611  secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
612  secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
613  secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
614  r->infinity = infinity;
615 }
616 
618  /* Operations: 4 mul, 1 sqr */
619  secp256k1_fe zz;
621  secp256k1_fe_sqr(&zz, s);
622  secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
623  secp256k1_fe_mul(&r->y, &r->y, &zz);
624  secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
625  secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
626 }
627 
629  secp256k1_fe x, y;
630  VERIFY_CHECK(!a->infinity);
631  x = a->x;
633  y = a->y;
635  secp256k1_fe_to_storage(&r->x, &x);
636  secp256k1_fe_to_storage(&r->y, &y);
637 }
638 
640  secp256k1_fe_from_storage(&r->x, &a->x);
641  secp256k1_fe_from_storage(&r->y, &a->y);
642  r->infinity = 0;
643 }
644 
646  secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
647  secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
648 }
649 
651  static const secp256k1_fe beta = SECP256K1_FE_CONST(
652  0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
653  0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
654  );
655  *r = *a;
656  secp256k1_fe_mul(&r->x, &r->x, &beta);
657 }
658 
660 #ifdef EXHAUSTIVE_TEST_ORDER
661  secp256k1_gej out;
662  int i;
663 
664  /* A very simple EC multiplication ladder that avoids a dependency on ecmult. */
666  for (i = 0; i < 32; ++i) {
667  secp256k1_gej_double_var(&out, &out, NULL);
668  if ((((uint32_t)EXHAUSTIVE_TEST_ORDER) >> (31 - i)) & 1) {
669  secp256k1_gej_add_ge_var(&out, &out, ge, NULL);
670  }
671  }
672  return secp256k1_gej_is_infinity(&out);
673 #else
674  (void)ge;
675  /* The real secp256k1 group has cofactor 1, so the subgroup is the entire curve. */
676  return 1;
677 #endif
678 }
679 
680 #endif /* SECP256K1_GROUP_IMPL_H */
secp256k1_gej::infinity
int infinity
Definition: group.h:27
VERIFY_CHECK
#define VERIFY_CHECK(cond)
Definition: util.h:95
secp256k1_fe_inv
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the (modular) inverse of another.
secp256k1_ge::y
secp256k1_fe y
Definition: group.h:19
secp256k1_ge_to_storage
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:628
EXHAUSTIVE_TEST_ORDER
#define EXHAUSTIVE_TEST_ORDER
Definition: tests_exhaustive.c:17
secp256k1_ge_is_infinity
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:84
secp256k1_fe_normalize_var
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
Normalize a field element, without constant-time guarantee.
secp256k1_ge_set_gej_var
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:107
secp256k1_fe_normalize
static void secp256k1_fe_normalize(secp256k1_fe *r)
Field element module.
SECP256K1_FE_CONST
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
Definition: field_10x26.h:40
secp256k1_gej::x
secp256k1_fe x
Definition: group.h:24
group.h
secp256k1_ge_set_all_gej_var
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Definition: group_impl.h:122
SECP256K1_G_ORDER_13
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:13
secp256k1_gej_is_infinity
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:257
secp256k1_gej::z
secp256k1_fe z
Definition: group.h:26
secp256k1_gej_add_var
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:336
secp256k1_fe_normalizes_to_zero
static int secp256k1_fe_normalizes_to_zero(const secp256k1_fe *r)
Verify whether a field element represents zero i.e.
secp256k1_ge_storage::y
secp256k1_fe_storage y
Definition: group.h:35
secp256k1_gej_double
static SECP256K1_INLINE void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:274
secp256k1_gej_set_infinity
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:189
secp256k1_gej_add_ge
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:495
secp256k1_ge_globalz_set_table_gej
static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr)
Definition: group_impl.h:164
secp256k1_gej_set_ge
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:233
secp256k1_ge_set_gej
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:94
secp256k1_ge_const_g
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:62
secp256k1_gej
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
secp256k1_fe_sqrt
static int secp256k1_fe_sqrt(secp256k1_fe *r, const secp256k1_fe *a)
If a has a square root, it is computed in r and 1 is returned.
secp256k1_fe_equal_var
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
Same as secp256k1_fe_equal, but may be variable time.
secp256k1_fe_is_odd
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
Check the "oddness" of a field element.
secp256k1_fe_mul
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
Sets a field element to be the product of two others.
secp256k1_ge_storage::x
secp256k1_fe_storage x
Definition: group.h:34
SECP256K1_G
#define SECP256K1_G
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition: group_impl.h:28
secp256k1_fe_clear
static void secp256k1_fe_clear(secp256k1_fe *a)
Sets a field element equal to zero, initializing all fields.
secp256k1_fe
Definition: field_10x26.h:12
secp256k1_ge_storage
Definition: group.h:33
secp256k1_fe_storage_cmov
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.
secp256k1_ge_set_xo_var
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:215
secp256k1_fe_from_storage
static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
Convert a field element back from the storage type.
secp256k1_gej::y
secp256k1_fe y
Definition: group.h:25
secp256k1_ge_set_xy
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:78
field.h
secp256k1_fe_const_b
static const secp256k1_fe secp256k1_fe_const_b
Definition: group_impl.h:64
secp256k1_fe_add
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
Adds a field element to another.
secp256k1_gej_eq_x_var
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:240
secp256k1_fe_to_storage
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
Convert a field element to the storage type.
secp256k1_fe_set_int
static void secp256k1_fe_set_int(secp256k1_fe *r, int a)
Set a field element equal to a small (not greater than 0x7FFF), non-negative integer.
secp256k1_fe_cmov
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
secp256k1_fe_sqr
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the square of another.
secp256k1_ge::infinity
int infinity
Definition: group.h:20
secp256k1_fe_is_zero
static int secp256k1_fe_is_zero(const secp256k1_fe *a)
Verify whether a field element is zero.
secp256k1_ge_set_infinity
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Definition: group_impl.h:196
secp256k1_gej_add_zinv_var
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:438
secp256k1_ge_set_gej_zinv
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:67
secp256k1_ge_storage_cmov
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:645
secp256k1_gej_add_ge_var
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:389
secp256k1_fe_negate
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
Set a field element equal to the additive inverse of another.
secp256k1_ge_mul_lambda
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:650
SECP256K1_G_ORDER_199
#define SECP256K1_G_ORDER_199
Definition: group_impl.h:19
SECP256K1_INLINE
#define SECP256K1_INLINE
Definition: secp256k1.h:127
secp256k1_ge_is_valid_var
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:261
secp256k1_fe_normalize_weak
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
Weakly normalize a field element: reduce its magnitude to 1, but don't fully normalize.
secp256k1_fe_mul_int
static void secp256k1_fe_mul_int(secp256k1_fe *r, int a)
Multiplies the passed field element with a small integer constant.
secp256k1_ge::x
secp256k1_fe x
Definition: group.h:18
secp256k1_ge_clear
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:209
ByteUnit::m
@ m
secp256k1_gej_rescale
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:617
secp256k1_ge_neg
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:88
secp256k1_gej_clear
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:202
secp256k1_ge
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
secp256k1_ge_is_in_correct_subgroup
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Definition: group_impl.h:659
secp256k1_fe_normalizes_to_zero_var
static int secp256k1_fe_normalizes_to_zero_var(const secp256k1_fe *r)
Verify whether a field element represents zero i.e.
secp256k1_fe_inv_var
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a)
Potentially faster version of secp256k1_fe_inv, without constant-time guarantee.
ByteUnit::t
@ t
secp256k1_gej_double_var
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:308
secp256k1_gej_neg
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:248
secp256k1_ge_from_storage
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:639