Dash Core  0.12.2.1
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 http://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 "num.h"
13 #include "field.h"
14 #include "group.h"
15 
20  0x79BE667EUL, 0xF9DCBBACUL, 0x55A06295UL, 0xCE870B07UL,
21  0x029BFCDBUL, 0x2DCE28D9UL, 0x59F2815BUL, 0x16F81798UL,
22  0x483ADA77UL, 0x26A3C465UL, 0x5DA4FBFCUL, 0x0E1108A8UL,
23  0xFD17B448UL, 0xA6855419UL, 0x9C47D08FUL, 0xFB10D4B8UL
24 );
25 
26 static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi) {
27  secp256k1_fe zi2;
28  secp256k1_fe zi3;
29  secp256k1_fe_sqr(&zi2, zi);
30  secp256k1_fe_mul(&zi3, &zi2, zi);
31  secp256k1_fe_mul(&r->x, &a->x, &zi2);
32  secp256k1_fe_mul(&r->y, &a->y, &zi3);
33  r->infinity = a->infinity;
34 }
35 
36 static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y) {
37  r->infinity = 0;
38  r->x = *x;
39  r->y = *y;
40 }
41 
42 static int secp256k1_ge_is_infinity(const secp256k1_ge *a) {
43  return a->infinity;
44 }
45 
46 static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a) {
47  *r = *a;
49  secp256k1_fe_negate(&r->y, &r->y, 1);
50 }
51 
53  secp256k1_fe z2, z3;
54  r->infinity = a->infinity;
55  secp256k1_fe_inv(&a->z, &a->z);
56  secp256k1_fe_sqr(&z2, &a->z);
57  secp256k1_fe_mul(&z3, &a->z, &z2);
58  secp256k1_fe_mul(&a->x, &a->x, &z2);
59  secp256k1_fe_mul(&a->y, &a->y, &z3);
60  secp256k1_fe_set_int(&a->z, 1);
61  r->x = a->x;
62  r->y = a->y;
63 }
64 
66  secp256k1_fe z2, z3;
67  r->infinity = a->infinity;
68  if (a->infinity) {
69  return;
70  }
71  secp256k1_fe_inv_var(&a->z, &a->z);
72  secp256k1_fe_sqr(&z2, &a->z);
73  secp256k1_fe_mul(&z3, &a->z, &z2);
74  secp256k1_fe_mul(&a->x, &a->x, &z2);
75  secp256k1_fe_mul(&a->y, &a->y, &z3);
76  secp256k1_fe_set_int(&a->z, 1);
77  r->x = a->x;
78  r->y = a->y;
79 }
80 
81 static void secp256k1_ge_set_all_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_callback *cb) {
82  secp256k1_fe *az;
83  secp256k1_fe *azi;
84  size_t i;
85  size_t count = 0;
86  az = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * len);
87  for (i = 0; i < len; i++) {
88  if (!a[i].infinity) {
89  az[count++] = a[i].z;
90  }
91  }
92 
93  azi = (secp256k1_fe *)checked_malloc(cb, sizeof(secp256k1_fe) * count);
95  free(az);
96 
97  count = 0;
98  for (i = 0; i < len; i++) {
99  r[i].infinity = a[i].infinity;
100  if (!a[i].infinity) {
101  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &azi[count++]);
102  }
103  }
104  free(azi);
105 }
106 
107 static void secp256k1_ge_set_table_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr) {
108  size_t i = len - 1;
109  secp256k1_fe zi;
110 
111  if (len > 0) {
112  /* Compute the inverse of the last z coordinate, and use it to compute the last affine output. */
113  secp256k1_fe_inv(&zi, &a[i].z);
114  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
115 
116  /* Work out way backwards, using the z-ratios to scale the x/y values. */
117  while (i > 0) {
118  secp256k1_fe_mul(&zi, &zi, &zr[i]);
119  i--;
120  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zi);
121  }
122  }
123 }
124 
125 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) {
126  size_t i = len - 1;
127  secp256k1_fe zs;
128 
129  if (len > 0) {
130  /* The z of the final point gives us the "global Z" for the table. */
131  r[i].x = a[i].x;
132  r[i].y = a[i].y;
133  *globalz = a[i].z;
134  r[i].infinity = 0;
135  zs = zr[i];
136 
137  /* Work our way backwards, using the z-ratios to scale the x/y values. */
138  while (i > 0) {
139  if (i != len - 1) {
140  secp256k1_fe_mul(&zs, &zs, &zr[i]);
141  }
142  i--;
143  secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
144  }
145  }
146 }
147 
149  r->infinity = 1;
150  secp256k1_fe_set_int(&r->x, 0);
151  secp256k1_fe_set_int(&r->y, 0);
152  secp256k1_fe_set_int(&r->z, 0);
153 }
154 
156  r->infinity = 0;
157  secp256k1_fe_clear(&r->x);
158  secp256k1_fe_clear(&r->y);
159  secp256k1_fe_clear(&r->z);
160 }
161 
163  r->infinity = 0;
164  secp256k1_fe_clear(&r->x);
165  secp256k1_fe_clear(&r->y);
166 }
167 
169  secp256k1_fe x2, x3, c;
170  r->x = *x;
171  secp256k1_fe_sqr(&x2, x);
172  secp256k1_fe_mul(&x3, x, &x2);
173  r->infinity = 0;
174  secp256k1_fe_set_int(&c, 7);
175  secp256k1_fe_add(&c, &x3);
176  return secp256k1_fe_sqrt_var(&r->y, &c);
177 }
178 
179 static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd) {
180  if (!secp256k1_ge_set_xquad_var(r, x)) {
181  return 0;
182  }
184  if (secp256k1_fe_is_odd(&r->y) != odd) {
185  secp256k1_fe_negate(&r->y, &r->y, 1);
186  }
187  return 1;
188 
189 }
190 
192  r->infinity = a->infinity;
193  r->x = a->x;
194  r->y = a->y;
195  secp256k1_fe_set_int(&r->z, 1);
196 }
197 
198 static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a) {
199  secp256k1_fe r, r2;
200  VERIFY_CHECK(!a->infinity);
201  secp256k1_fe_sqr(&r, &a->z); secp256k1_fe_mul(&r, &r, x);
202  r2 = a->x; secp256k1_fe_normalize_weak(&r2);
203  return secp256k1_fe_equal_var(&r, &r2);
204 }
205 
206 static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a) {
207  r->infinity = a->infinity;
208  r->x = a->x;
209  r->y = a->y;
210  r->z = a->z;
212  secp256k1_fe_negate(&r->y, &r->y, 1);
213 }
214 
216  return a->infinity;
217 }
218 
220  secp256k1_fe y2, x3, z2, z6;
221  if (a->infinity) {
222  return 0;
223  }
229  secp256k1_fe_sqr(&y2, &a->y);
230  secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
231  secp256k1_fe_sqr(&z2, &a->z);
232  secp256k1_fe_sqr(&z6, &z2); secp256k1_fe_mul(&z6, &z6, &z2);
233  secp256k1_fe_mul_int(&z6, 7);
234  secp256k1_fe_add(&x3, &z6);
236  return secp256k1_fe_equal_var(&y2, &x3);
237 }
238 
240  secp256k1_fe y2, x3, c;
241  if (a->infinity) {
242  return 0;
243  }
244  /* y^2 = x^3 + 7 */
245  secp256k1_fe_sqr(&y2, &a->y);
246  secp256k1_fe_sqr(&x3, &a->x); secp256k1_fe_mul(&x3, &x3, &a->x);
247  secp256k1_fe_set_int(&c, 7);
248  secp256k1_fe_add(&x3, &c);
250  return secp256k1_fe_equal_var(&y2, &x3);
251 }
252 
254  /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate */
255  secp256k1_fe t1,t2,t3,t4;
260  r->infinity = a->infinity;
261  if (r->infinity) {
262  if (rzr != NULL) {
263  secp256k1_fe_set_int(rzr, 1);
264  }
265  return;
266  }
267 
268  if (rzr != NULL) {
269  *rzr = a->y;
271  secp256k1_fe_mul_int(rzr, 2);
272  }
273 
274  secp256k1_fe_mul(&r->z, &a->z, &a->y);
275  secp256k1_fe_mul_int(&r->z, 2); /* Z' = 2*Y*Z (2) */
276  secp256k1_fe_sqr(&t1, &a->x);
277  secp256k1_fe_mul_int(&t1, 3); /* T1 = 3*X^2 (3) */
278  secp256k1_fe_sqr(&t2, &t1); /* T2 = 9*X^4 (1) */
279  secp256k1_fe_sqr(&t3, &a->y);
280  secp256k1_fe_mul_int(&t3, 2); /* T3 = 2*Y^2 (2) */
281  secp256k1_fe_sqr(&t4, &t3);
282  secp256k1_fe_mul_int(&t4, 2); /* T4 = 8*Y^4 (2) */
283  secp256k1_fe_mul(&t3, &t3, &a->x); /* T3 = 2*X*Y^2 (1) */
284  r->x = t3;
285  secp256k1_fe_mul_int(&r->x, 4); /* X' = 8*X*Y^2 (4) */
286  secp256k1_fe_negate(&r->x, &r->x, 4); /* X' = -8*X*Y^2 (5) */
287  secp256k1_fe_add(&r->x, &t2); /* X' = 9*X^4 - 8*X*Y^2 (6) */
288  secp256k1_fe_negate(&t2, &t2, 1); /* T2 = -9*X^4 (2) */
289  secp256k1_fe_mul_int(&t3, 6); /* T3 = 12*X*Y^2 (6) */
290  secp256k1_fe_add(&t3, &t2); /* T3 = 12*X*Y^2 - 9*X^4 (8) */
291  secp256k1_fe_mul(&r->y, &t1, &t3); /* Y' = 36*X^3*Y^2 - 27*X^6 (1) */
292  secp256k1_fe_negate(&t2, &t4, 2); /* T2 = -8*Y^4 (3) */
293  secp256k1_fe_add(&r->y, &t2); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
294 }
295 
298  secp256k1_gej_double_var(r, a, rzr);
299 }
300 
302  /* Operations: 12 mul, 4 sqr, 2 normalize, 12 mul_int/add/negate */
303  secp256k1_fe z22, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
304 
305  if (a->infinity) {
306  VERIFY_CHECK(rzr == NULL);
307  *r = *b;
308  return;
309  }
310 
311  if (b->infinity) {
312  if (rzr != NULL) {
313  secp256k1_fe_set_int(rzr, 1);
314  }
315  *r = *a;
316  return;
317  }
318 
319  r->infinity = 0;
320  secp256k1_fe_sqr(&z22, &b->z);
321  secp256k1_fe_sqr(&z12, &a->z);
322  secp256k1_fe_mul(&u1, &a->x, &z22);
323  secp256k1_fe_mul(&u2, &b->x, &z12);
324  secp256k1_fe_mul(&s1, &a->y, &z22); secp256k1_fe_mul(&s1, &s1, &b->z);
325  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
326  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
327  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
330  secp256k1_gej_double_var(r, a, rzr);
331  } else {
332  if (rzr != NULL) {
333  secp256k1_fe_set_int(rzr, 0);
334  }
335  r->infinity = 1;
336  }
337  return;
338  }
339  secp256k1_fe_sqr(&i2, &i);
340  secp256k1_fe_sqr(&h2, &h);
341  secp256k1_fe_mul(&h3, &h, &h2);
342  secp256k1_fe_mul(&h, &h, &b->z);
343  if (rzr != NULL) {
344  *rzr = h;
345  }
346  secp256k1_fe_mul(&r->z, &a->z, &h);
347  secp256k1_fe_mul(&t, &u1, &h2);
348  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);
349  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
350  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
351  secp256k1_fe_add(&r->y, &h3);
352 }
353 
355  /* 8 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
356  secp256k1_fe z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
357  if (a->infinity) {
358  VERIFY_CHECK(rzr == NULL);
359  secp256k1_gej_set_ge(r, b);
360  return;
361  }
362  if (b->infinity) {
363  if (rzr != NULL) {
364  secp256k1_fe_set_int(rzr, 1);
365  }
366  *r = *a;
367  return;
368  }
369  r->infinity = 0;
370 
371  secp256k1_fe_sqr(&z12, &a->z);
372  u1 = a->x; secp256k1_fe_normalize_weak(&u1);
373  secp256k1_fe_mul(&u2, &b->x, &z12);
374  s1 = a->y; secp256k1_fe_normalize_weak(&s1);
375  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &a->z);
376  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
377  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
380  secp256k1_gej_double_var(r, a, rzr);
381  } else {
382  if (rzr != NULL) {
383  secp256k1_fe_set_int(rzr, 0);
384  }
385  r->infinity = 1;
386  }
387  return;
388  }
389  secp256k1_fe_sqr(&i2, &i);
390  secp256k1_fe_sqr(&h2, &h);
391  secp256k1_fe_mul(&h3, &h, &h2);
392  if (rzr != NULL) {
393  *rzr = h;
394  }
395  secp256k1_fe_mul(&r->z, &a->z, &h);
396  secp256k1_fe_mul(&t, &u1, &h2);
397  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);
398  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
399  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
400  secp256k1_fe_add(&r->y, &h3);
401 }
402 
403 static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv) {
404  /* 9 mul, 3 sqr, 4 normalize, 12 mul_int/add/negate */
405  secp256k1_fe az, z12, u1, u2, s1, s2, h, i, i2, h2, h3, t;
406 
407  if (b->infinity) {
408  *r = *a;
409  return;
410  }
411  if (a->infinity) {
412  secp256k1_fe bzinv2, bzinv3;
413  r->infinity = b->infinity;
414  secp256k1_fe_sqr(&bzinv2, bzinv);
415  secp256k1_fe_mul(&bzinv3, &bzinv2, bzinv);
416  secp256k1_fe_mul(&r->x, &b->x, &bzinv2);
417  secp256k1_fe_mul(&r->y, &b->y, &bzinv3);
418  secp256k1_fe_set_int(&r->z, 1);
419  return;
420  }
421  r->infinity = 0;
422 
431  secp256k1_fe_mul(&az, &a->z, bzinv);
432 
433  secp256k1_fe_sqr(&z12, &az);
434  u1 = a->x; secp256k1_fe_normalize_weak(&u1);
435  secp256k1_fe_mul(&u2, &b->x, &z12);
436  s1 = a->y; secp256k1_fe_normalize_weak(&s1);
437  secp256k1_fe_mul(&s2, &b->y, &z12); secp256k1_fe_mul(&s2, &s2, &az);
438  secp256k1_fe_negate(&h, &u1, 1); secp256k1_fe_add(&h, &u2);
439  secp256k1_fe_negate(&i, &s1, 1); secp256k1_fe_add(&i, &s2);
442  secp256k1_gej_double_var(r, a, NULL);
443  } else {
444  r->infinity = 1;
445  }
446  return;
447  }
448  secp256k1_fe_sqr(&i2, &i);
449  secp256k1_fe_sqr(&h2, &h);
450  secp256k1_fe_mul(&h3, &h, &h2);
451  r->z = a->z; secp256k1_fe_mul(&r->z, &r->z, &h);
452  secp256k1_fe_mul(&t, &u1, &h2);
453  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);
454  secp256k1_fe_negate(&r->y, &r->x, 5); secp256k1_fe_add(&r->y, &t); secp256k1_fe_mul(&r->y, &r->y, &i);
455  secp256k1_fe_mul(&h3, &h3, &s1); secp256k1_fe_negate(&h3, &h3, 1);
456  secp256k1_fe_add(&r->y, &h3);
457 }
458 
459 
460 static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b) {
461  /* Operations: 7 mul, 5 sqr, 4 normalize, 21 mul_int/add/negate/cmov */
462  static const secp256k1_fe fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
463  secp256k1_fe zz, u1, u2, s1, s2, t, tt, m, n, q, rr;
464  secp256k1_fe m_alt, rr_alt;
465  int infinity, degenerate;
466  VERIFY_CHECK(!b->infinity);
467  VERIFY_CHECK(a->infinity == 0 || a->infinity == 1);
468 
519  secp256k1_fe_sqr(&zz, &a->z); /* z = Z1^2 */
520  u1 = a->x; secp256k1_fe_normalize_weak(&u1); /* u1 = U1 = X1*Z2^2 (1) */
521  secp256k1_fe_mul(&u2, &b->x, &zz); /* u2 = U2 = X2*Z1^2 (1) */
522  s1 = a->y; secp256k1_fe_normalize_weak(&s1); /* s1 = S1 = Y1*Z2^3 (1) */
523  secp256k1_fe_mul(&s2, &b->y, &zz); /* s2 = Y2*Z1^2 (1) */
524  secp256k1_fe_mul(&s2, &s2, &a->z); /* s2 = S2 = Y2*Z1^3 (1) */
525  t = u1; secp256k1_fe_add(&t, &u2); /* t = T = U1+U2 (2) */
526  m = s1; secp256k1_fe_add(&m, &s2); /* m = M = S1+S2 (2) */
527  secp256k1_fe_sqr(&rr, &t); /* rr = T^2 (1) */
528  secp256k1_fe_negate(&m_alt, &u2, 1); /* Malt = -X2*Z1^2 */
529  secp256k1_fe_mul(&tt, &u1, &m_alt); /* tt = -U1*U2 (2) */
530  secp256k1_fe_add(&rr, &tt); /* rr = R = T^2-U1*U2 (3) */
533  degenerate = secp256k1_fe_normalizes_to_zero(&m) &
535  /* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
536  * This means either x1 == beta*x2 or beta*x1 == x2, where beta is
537  * a nontrivial cube root of one. In either case, an alternate
538  * non-indeterminate expression for lambda is (y1 - y2)/(x1 - x2),
539  * so we set R/M equal to this. */
540  rr_alt = s1;
541  secp256k1_fe_mul_int(&rr_alt, 2); /* rr = Y1*Z2^3 - Y2*Z1^3 (2) */
542  secp256k1_fe_add(&m_alt, &u1); /* Malt = X1*Z2^2 - X2*Z1^2 */
543 
544  secp256k1_fe_cmov(&rr_alt, &rr, !degenerate);
545  secp256k1_fe_cmov(&m_alt, &m, !degenerate);
546  /* Now Ralt / Malt = lambda and is guaranteed not to be 0/0.
547  * From here on out Ralt and Malt represent the numerator
548  * and denominator of lambda; R and M represent the explicit
549  * expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
550  secp256k1_fe_sqr(&n, &m_alt); /* n = Malt^2 (1) */
551  secp256k1_fe_mul(&q, &n, &t); /* q = Q = T*Malt^2 (1) */
552  /* These two lines use the observation that either M == Malt or M == 0,
553  * so M^3 * Malt is either Malt^4 (which is computed by squaring), or
554  * zero (which is "computed" by cmov). So the cost is one squaring
555  * versus two multiplications. */
556  secp256k1_fe_sqr(&n, &n);
557  secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
558  secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
559  secp256k1_fe_mul(&r->z, &a->z, &m_alt); /* r->z = Malt*Z (1) */
560  infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
561  secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
562  secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
563  secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
565  r->x = t; /* r->x = Ralt^2-Q (1) */
566  secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
567  secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
568  secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
569  secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
570  secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
572  secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
573  secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */
574 
576  secp256k1_fe_cmov(&r->x, &b->x, a->infinity);
577  secp256k1_fe_cmov(&r->y, &b->y, a->infinity);
578  secp256k1_fe_cmov(&r->z, &fe_1, a->infinity);
579  r->infinity = infinity;
580 }
581 
583  /* Operations: 4 mul, 1 sqr */
584  secp256k1_fe zz;
586  secp256k1_fe_sqr(&zz, s);
587  secp256k1_fe_mul(&r->x, &r->x, &zz); /* r->x *= s^2 */
588  secp256k1_fe_mul(&r->y, &r->y, &zz);
589  secp256k1_fe_mul(&r->y, &r->y, s); /* r->y *= s^3 */
590  secp256k1_fe_mul(&r->z, &r->z, s); /* r->z *= s */
591 }
592 
594  secp256k1_fe x, y;
595  VERIFY_CHECK(!a->infinity);
596  x = a->x;
598  y = a->y;
600  secp256k1_fe_to_storage(&r->x, &x);
601  secp256k1_fe_to_storage(&r->y, &y);
602 }
603 
605  secp256k1_fe_from_storage(&r->x, &a->x);
606  secp256k1_fe_from_storage(&r->y, &a->y);
607  r->infinity = 0;
608 }
609 
611  secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
612  secp256k1_fe_storage_cmov(&r->y, &a->y, flag);
613 }
614 
615 #ifdef USE_ENDOMORPHISM
616 static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a) {
617  static const secp256k1_fe beta = SECP256K1_FE_CONST(
618  0x7ae96a2bul, 0x657c0710ul, 0x6e64479eul, 0xac3434e9ul,
619  0x9cf04975ul, 0x12f58995ul, 0xc1396c28ul, 0x719501eeul
620  );
621  *r = *a;
622  secp256k1_fe_mul(&r->x, &r->x, &beta);
623 }
624 #endif
625 
626 #endif
#define VERIFY_CHECK(cond)
Definition: util.h:64
static SECP256K1_INLINE void secp256k1_fe_clear(secp256k1_fe *a)
static int secp256k1_fe_is_zero(const secp256k1_fe *a)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
secp256k1_fe x
Definition: group.h:25
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *s)
Definition: group_impl.h:582
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:65
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Definition: group_impl.h:46
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:253
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
static void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static SECP256K1_INLINE void secp256k1_gej_double_nonzero(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Definition: group_impl.h:296
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:354
static void secp256k1_gej_clear(secp256k1_gej *r)
Definition: group_impl.h:155
static void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
static void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static int secp256k1_ge_set_xquad_var(secp256k1_ge *r, const secp256k1_fe *x)
Definition: group_impl.h:168
secp256k1_fe_storage y
Definition: group.h:36
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Definition: group_impl.h:52
static void secp256k1_fe_set_int(secp256k1_fe *r, int a)
static void secp256k1_ge_set_table_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zr)
Definition: group_impl.h:107
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
Definition: field_10x26.h:38
static int secp256k1_gej_is_valid_var(const secp256k1_gej *a)
Definition: group_impl.h:219
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_fe_mul_int(secp256k1_fe *r, int a)
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Definition: group_impl.h:460
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Definition: group_impl.h:42
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: group_impl.h:191
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
Definition: group_impl.h:198
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:19
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
Definition: group_impl.h:604
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
Definition: group_impl.h:36
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Definition: group_impl.h:148
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Definition: group_impl.h:206
#define SECP256K1_INLINE
Definition: secp256k1.h:116
static void secp256k1_ge_set_all_gej_var(size_t len, secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_callback *cb)
Definition: group_impl.h:81
int infinity
Definition: group.h:28
secp256k1_fe_storage x
Definition: group.h:35
static int secp256k1_fe_sqrt_var(secp256k1_fe *r, const secp256k1_fe *a)
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
Definition: group_impl.h:179
secp256k1_fe x
Definition: group.h:15
static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
static int secp256k1_fe_normalizes_to_zero(secp256k1_fe *r)
int infinity
Definition: group.h:17
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
#define SECP256K1_GE_CONST(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
Definition: group.h:20
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag)
Definition: group_impl.h:610
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:403
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Definition: group_impl.h:593
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Definition: group_impl.h:215
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:26
secp256k1_fe z
Definition: group.h:27
static void secp256k1_fe_normalize(secp256k1_fe *r)
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
Definition: group_impl.h:239
static int count
Definition: tests.c:41
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:125
secp256k1_fe y
Definition: group.h:26
secp256k1_fe y
Definition: group.h:16
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:68
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Definition: group_impl.h:301
static void secp256k1_ge_clear(secp256k1_ge *r)
Definition: group_impl.h:162
static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe *r)