Bitcoin Core  26.1.0
P2P Digital Currency
modinv64_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2020 Peter Dettman *
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_MODINV64_IMPL_H
8 #define SECP256K1_MODINV64_IMPL_H
9 
10 #include "int128.h"
11 #include "modinv64.h"
12 
13 /* This file implements modular inversion based on the paper "Fast constant-time gcd computation and
14  * modular inversion" by Daniel J. Bernstein and Bo-Yin Yang.
15  *
16  * For an explanation of the algorithm, see doc/safegcd_implementation.md. This file contains an
17  * implementation for N=62, using 62-bit signed limbs represented as int64_t.
18  */
19 
20 /* Data type for transition matrices (see section 3 of explanation).
21  *
22  * t = [ u v ]
23  * [ q r ]
24  */
25 typedef struct {
26  int64_t u, v, q, r;
28 
29 #ifdef VERIFY
30 /* Helper function to compute the absolute value of an int64_t.
31  * (we don't use abs/labs/llabs as it depends on the int sizes). */
32 static int64_t secp256k1_modinv64_abs(int64_t v) {
33  VERIFY_CHECK(v > INT64_MIN);
34  if (v < 0) return -v;
35  return v;
36 }
37 
38 static const secp256k1_modinv64_signed62 SECP256K1_SIGNED62_ONE = {{1}};
39 
40 /* Compute a*factor and put it in r. All but the top limb in r will be in range [0,2^62). */
41 static void secp256k1_modinv64_mul_62(secp256k1_modinv64_signed62 *r, const secp256k1_modinv64_signed62 *a, int alen, int64_t factor) {
42  const uint64_t M62 = UINT64_MAX >> 2;
43  secp256k1_int128 c, d;
44  int i;
46  for (i = 0; i < 4; ++i) {
47  if (i < alen) secp256k1_i128_accum_mul(&c, a->v[i], factor);
48  r->v[i] = secp256k1_i128_to_u64(&c) & M62; secp256k1_i128_rshift(&c, 62);
49  }
50  if (4 < alen) secp256k1_i128_accum_mul(&c, a->v[4], factor);
53  r->v[4] = secp256k1_i128_to_i64(&c);
54 }
55 
56 /* Return -1 for a<b*factor, 0 for a==b*factor, 1 for a>b*factor. A has alen limbs; b has 5. */
57 static int secp256k1_modinv64_mul_cmp_62(const secp256k1_modinv64_signed62 *a, int alen, const secp256k1_modinv64_signed62 *b, int64_t factor) {
58  int i;
60  secp256k1_modinv64_mul_62(&am, a, alen, 1); /* Normalize all but the top limb of a. */
61  secp256k1_modinv64_mul_62(&bm, b, 5, factor);
62  for (i = 0; i < 4; ++i) {
63  /* Verify that all but the top limb of a and b are normalized. */
64  VERIFY_CHECK(am.v[i] >> 62 == 0);
65  VERIFY_CHECK(bm.v[i] >> 62 == 0);
66  }
67  for (i = 4; i >= 0; --i) {
68  if (am.v[i] < bm.v[i]) return -1;
69  if (am.v[i] > bm.v[i]) return 1;
70  }
71  return 0;
72 }
73 
74 /* Check if the determinant of t is equal to 1 << n. If abs, check if |det t| == 1 << n. */
75 static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *t, unsigned int n, int abs) {
77  secp256k1_i128_det(&a, t->u, t->v, t->q, t->r);
78  if (secp256k1_i128_check_pow2(&a, n, 1)) return 1;
79  if (abs && secp256k1_i128_check_pow2(&a, n, -1)) return 1;
80  return 0;
81 }
82 #endif
83 
84 /* Take as input a signed62 number in range (-2*modulus,modulus), and add a multiple of the modulus
85  * to it to bring it to range [0,modulus). If sign < 0, the input will also be negated in the
86  * process. The input must have limbs in range (-2^62,2^62). The output will have limbs in range
87  * [0,2^62). */
89  const int64_t M62 = (int64_t)(UINT64_MAX >> 2);
90  int64_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4];
91  volatile int64_t cond_add, cond_negate;
92 
93 #ifdef VERIFY
94  /* Verify that all limbs are in range (-2^62,2^62). */
95  int i;
96  for (i = 0; i < 5; ++i) {
97  VERIFY_CHECK(r->v[i] >= -M62);
98  VERIFY_CHECK(r->v[i] <= M62);
99  }
100  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(r, 5, &modinfo->modulus, -2) > 0); /* r > -2*modulus */
101  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(r, 5, &modinfo->modulus, 1) < 0); /* r < modulus */
102 #endif
103 
104  /* In a first step, add the modulus if the input is negative, and then negate if requested.
105  * This brings r from range (-2*modulus,modulus) to range (-modulus,modulus). As all input
106  * limbs are in range (-2^62,2^62), this cannot overflow an int64_t. Note that the right
107  * shifts below are signed sign-extending shifts (see assumptions.h for tests that that is
108  * indeed the behavior of the right shift operator). */
109  cond_add = r4 >> 63;
110  r0 += modinfo->modulus.v[0] & cond_add;
111  r1 += modinfo->modulus.v[1] & cond_add;
112  r2 += modinfo->modulus.v[2] & cond_add;
113  r3 += modinfo->modulus.v[3] & cond_add;
114  r4 += modinfo->modulus.v[4] & cond_add;
115  cond_negate = sign >> 63;
116  r0 = (r0 ^ cond_negate) - cond_negate;
117  r1 = (r1 ^ cond_negate) - cond_negate;
118  r2 = (r2 ^ cond_negate) - cond_negate;
119  r3 = (r3 ^ cond_negate) - cond_negate;
120  r4 = (r4 ^ cond_negate) - cond_negate;
121  /* Propagate the top bits, to bring limbs back to range (-2^62,2^62). */
122  r1 += r0 >> 62; r0 &= M62;
123  r2 += r1 >> 62; r1 &= M62;
124  r3 += r2 >> 62; r2 &= M62;
125  r4 += r3 >> 62; r3 &= M62;
126 
127  /* In a second step add the modulus again if the result is still negative, bringing
128  * r to range [0,modulus). */
129  cond_add = r4 >> 63;
130  r0 += modinfo->modulus.v[0] & cond_add;
131  r1 += modinfo->modulus.v[1] & cond_add;
132  r2 += modinfo->modulus.v[2] & cond_add;
133  r3 += modinfo->modulus.v[3] & cond_add;
134  r4 += modinfo->modulus.v[4] & cond_add;
135  /* And propagate again. */
136  r1 += r0 >> 62; r0 &= M62;
137  r2 += r1 >> 62; r1 &= M62;
138  r3 += r2 >> 62; r2 &= M62;
139  r4 += r3 >> 62; r3 &= M62;
140 
141  r->v[0] = r0;
142  r->v[1] = r1;
143  r->v[2] = r2;
144  r->v[3] = r3;
145  r->v[4] = r4;
146 
147 #ifdef VERIFY
148  VERIFY_CHECK(r0 >> 62 == 0);
149  VERIFY_CHECK(r1 >> 62 == 0);
150  VERIFY_CHECK(r2 >> 62 == 0);
151  VERIFY_CHECK(r3 >> 62 == 0);
152  VERIFY_CHECK(r4 >> 62 == 0);
153  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(r, 5, &modinfo->modulus, 0) >= 0); /* r >= 0 */
154  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(r, 5, &modinfo->modulus, 1) < 0); /* r < modulus */
155 #endif
156 }
157 
158 /* Compute the transition matrix and eta for 59 divsteps (where zeta=-(delta+1/2)).
159  * Note that the transformation matrix is scaled by 2^62 and not 2^59.
160  *
161  * Input: zeta: initial zeta
162  * f0: bottom limb of initial f
163  * g0: bottom limb of initial g
164  * Output: t: transition matrix
165  * Return: final zeta
166  *
167  * Implements the divsteps_n_matrix function from the explanation.
168  */
169 static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t) {
170  /* u,v,q,r are the elements of the transformation matrix being built up,
171  * starting with the identity matrix times 8 (because the caller expects
172  * a result scaled by 2^62). Semantically they are signed integers
173  * in range [-2^62,2^62], but here represented as unsigned mod 2^64. This
174  * permits left shifting (which is UB for negative numbers). The range
175  * being inside [-2^63,2^63) means that casting to signed works correctly.
176  */
177  uint64_t u = 8, v = 0, q = 0, r = 8;
178  volatile uint64_t c1, c2;
179  uint64_t mask1, mask2, f = f0, g = g0, x, y, z;
180  int i;
181 
182  for (i = 3; i < 62; ++i) {
183  VERIFY_CHECK((f & 1) == 1); /* f must always be odd */
184  VERIFY_CHECK((u * f0 + v * g0) == f << i);
185  VERIFY_CHECK((q * f0 + r * g0) == g << i);
186  /* Compute conditional masks for (zeta < 0) and for (g & 1). */
187  c1 = zeta >> 63;
188  mask1 = c1;
189  c2 = g & 1;
190  mask2 = -c2;
191  /* Compute x,y,z, conditionally negated versions of f,u,v. */
192  x = (f ^ mask1) - mask1;
193  y = (u ^ mask1) - mask1;
194  z = (v ^ mask1) - mask1;
195  /* Conditionally add x,y,z to g,q,r. */
196  g += x & mask2;
197  q += y & mask2;
198  r += z & mask2;
199  /* In what follows, c1 is a condition mask for (zeta < 0) and (g & 1). */
200  mask1 &= mask2;
201  /* Conditionally change zeta into -zeta-2 or zeta-1. */
202  zeta = (zeta ^ mask1) - 1;
203  /* Conditionally add g,q,r to f,u,v. */
204  f += g & mask1;
205  u += q & mask1;
206  v += r & mask1;
207  /* Shifts */
208  g >>= 1;
209  u <<= 1;
210  v <<= 1;
211  /* Bounds on zeta that follow from the bounds on iteration count (max 10*59 divsteps). */
212  VERIFY_CHECK(zeta >= -591 && zeta <= 591);
213  }
214  /* Return data in t and return value. */
215  t->u = (int64_t)u;
216  t->v = (int64_t)v;
217  t->q = (int64_t)q;
218  t->r = (int64_t)r;
219 #ifdef VERIFY
220  /* The determinant of t must be a power of two. This guarantees that multiplication with t
221  * does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
222  * will be divided out again). As each divstep's individual matrix has determinant 2, the
223  * aggregate of 59 of them will have determinant 2^59. Multiplying with the initial
224  * 8*identity (which has determinant 2^6) means the overall outputs has determinant
225  * 2^65. */
226  VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 65, 0));
227 #endif
228  return zeta;
229 }
230 
231 /* Compute the transition matrix and eta for 62 divsteps (variable time, eta=-delta).
232  *
233  * Input: eta: initial eta
234  * f0: bottom limb of initial f
235  * g0: bottom limb of initial g
236  * Output: t: transition matrix
237  * Return: final eta
238  *
239  * Implements the divsteps_n_matrix_var function from the explanation.
240  */
241 static int64_t secp256k1_modinv64_divsteps_62_var(int64_t eta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t) {
242  /* Transformation matrix; see comments in secp256k1_modinv64_divsteps_62. */
243  uint64_t u = 1, v = 0, q = 0, r = 1;
244  uint64_t f = f0, g = g0, m;
245  uint32_t w;
246  int i = 62, limit, zeros;
247 
248  for (;;) {
249  /* Use a sentinel bit to count zeros only up to i. */
250  zeros = secp256k1_ctz64_var(g | (UINT64_MAX << i));
251  /* Perform zeros divsteps at once; they all just divide g by two. */
252  g >>= zeros;
253  u <<= zeros;
254  v <<= zeros;
255  eta -= zeros;
256  i -= zeros;
257  /* We're done once we've done 62 divsteps. */
258  if (i == 0) break;
259  VERIFY_CHECK((f & 1) == 1);
260  VERIFY_CHECK((g & 1) == 1);
261  VERIFY_CHECK((u * f0 + v * g0) == f << (62 - i));
262  VERIFY_CHECK((q * f0 + r * g0) == g << (62 - i));
263  /* Bounds on eta that follow from the bounds on iteration count (max 12*62 divsteps). */
264  VERIFY_CHECK(eta >= -745 && eta <= 745);
265  /* If eta is negative, negate it and replace f,g with g,-f. */
266  if (eta < 0) {
267  uint64_t tmp;
268  eta = -eta;
269  tmp = f; f = g; g = -tmp;
270  tmp = u; u = q; q = -tmp;
271  tmp = v; v = r; r = -tmp;
272  /* Use a formula to cancel out up to 6 bits of g. Also, no more than i can be cancelled
273  * out (as we'd be done before that point), and no more than eta+1 can be done as its
274  * sign will flip again once that happens. */
275  limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
276  VERIFY_CHECK(limit > 0 && limit <= 62);
277  /* m is a mask for the bottom min(limit, 6) bits. */
278  m = (UINT64_MAX >> (64 - limit)) & 63U;
279  /* Find what multiple of f must be added to g to cancel its bottom min(limit, 6)
280  * bits. */
281  w = (f * g * (f * f - 2)) & m;
282  } else {
283  /* In this branch, use a simpler formula that only lets us cancel up to 4 bits of g, as
284  * eta tends to be smaller here. */
285  limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
286  VERIFY_CHECK(limit > 0 && limit <= 62);
287  /* m is a mask for the bottom min(limit, 4) bits. */
288  m = (UINT64_MAX >> (64 - limit)) & 15U;
289  /* Find what multiple of f must be added to g to cancel its bottom min(limit, 4)
290  * bits. */
291  w = f + (((f + 1) & 4) << 1);
292  w = (-w * g) & m;
293  }
294  g += f * w;
295  q += u * w;
296  r += v * w;
297  VERIFY_CHECK((g & m) == 0);
298  }
299  /* Return data in t and return value. */
300  t->u = (int64_t)u;
301  t->v = (int64_t)v;
302  t->q = (int64_t)q;
303  t->r = (int64_t)r;
304 #ifdef VERIFY
305  /* The determinant of t must be a power of two. This guarantees that multiplication with t
306  * does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
307  * will be divided out again). As each divstep's individual matrix has determinant 2, the
308  * aggregate of 62 of them will have determinant 2^62. */
309  VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 62, 0));
310 #endif
311  return eta;
312 }
313 
314 /* Compute the transition matrix and eta for 62 posdivsteps (variable time, eta=-delta), and keeps track
315  * of the Jacobi symbol along the way. f0 and g0 must be f and g mod 2^64 rather than 2^62, because
316  * Jacobi tracking requires knowing (f mod 8) rather than just (f mod 2).
317  *
318  * Input: eta: initial eta
319  * f0: bottom limb of initial f
320  * g0: bottom limb of initial g
321  * Output: t: transition matrix
322  * Input/Output: (*jacp & 1) is bitflipped if and only if the Jacobi symbol of (f | g) changes sign
323  * by applying the returned transformation matrix to it. The other bits of *jacp may
324  * change, but are meaningless.
325  * Return: final eta
326  */
327 static int64_t secp256k1_modinv64_posdivsteps_62_var(int64_t eta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t, int *jacp) {
328  /* Transformation matrix; see comments in secp256k1_modinv64_divsteps_62. */
329  uint64_t u = 1, v = 0, q = 0, r = 1;
330  uint64_t f = f0, g = g0, m;
331  uint32_t w;
332  int i = 62, limit, zeros;
333  int jac = *jacp;
334 
335  for (;;) {
336  /* Use a sentinel bit to count zeros only up to i. */
337  zeros = secp256k1_ctz64_var(g | (UINT64_MAX << i));
338  /* Perform zeros divsteps at once; they all just divide g by two. */
339  g >>= zeros;
340  u <<= zeros;
341  v <<= zeros;
342  eta -= zeros;
343  i -= zeros;
344  /* Update the bottom bit of jac: when dividing g by an odd power of 2,
345  * if (f mod 8) is 3 or 5, the Jacobi symbol changes sign. */
346  jac ^= (zeros & ((f >> 1) ^ (f >> 2)));
347  /* We're done once we've done 62 posdivsteps. */
348  if (i == 0) break;
349  VERIFY_CHECK((f & 1) == 1);
350  VERIFY_CHECK((g & 1) == 1);
351  VERIFY_CHECK((u * f0 + v * g0) == f << (62 - i));
352  VERIFY_CHECK((q * f0 + r * g0) == g << (62 - i));
353  /* If eta is negative, negate it and replace f,g with g,f. */
354  if (eta < 0) {
355  uint64_t tmp;
356  eta = -eta;
357  tmp = f; f = g; g = tmp;
358  tmp = u; u = q; q = tmp;
359  tmp = v; v = r; r = tmp;
360  /* Update bottom bit of jac: when swapping f and g, the Jacobi symbol changes sign
361  * if both f and g are 3 mod 4. */
362  jac ^= ((f & g) >> 1);
363  /* Use a formula to cancel out up to 6 bits of g. Also, no more than i can be cancelled
364  * out (as we'd be done before that point), and no more than eta+1 can be done as its
365  * sign will flip again once that happens. */
366  limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
367  VERIFY_CHECK(limit > 0 && limit <= 62);
368  /* m is a mask for the bottom min(limit, 6) bits. */
369  m = (UINT64_MAX >> (64 - limit)) & 63U;
370  /* Find what multiple of f must be added to g to cancel its bottom min(limit, 6)
371  * bits. */
372  w = (f * g * (f * f - 2)) & m;
373  } else {
374  /* In this branch, use a simpler formula that only lets us cancel up to 4 bits of g, as
375  * eta tends to be smaller here. */
376  limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
377  VERIFY_CHECK(limit > 0 && limit <= 62);
378  /* m is a mask for the bottom min(limit, 4) bits. */
379  m = (UINT64_MAX >> (64 - limit)) & 15U;
380  /* Find what multiple of f must be added to g to cancel its bottom min(limit, 4)
381  * bits. */
382  w = f + (((f + 1) & 4) << 1);
383  w = (-w * g) & m;
384  }
385  g += f * w;
386  q += u * w;
387  r += v * w;
388  VERIFY_CHECK((g & m) == 0);
389  }
390  /* Return data in t and return value. */
391  t->u = (int64_t)u;
392  t->v = (int64_t)v;
393  t->q = (int64_t)q;
394  t->r = (int64_t)r;
395 #ifdef VERIFY
396  /* The determinant of t must be a power of two. This guarantees that multiplication with t
397  * does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
398  * will be divided out again). As each divstep's individual matrix has determinant 2 or -2,
399  * the aggregate of 62 of them will have determinant 2^62 or -2^62. */
400  VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 62, 1));
401 #endif
402  *jacp = jac;
403  return eta;
404 }
405 
406 /* Compute (t/2^62) * [d, e] mod modulus, where t is a transition matrix scaled by 2^62.
407  *
408  * On input and output, d and e are in range (-2*modulus,modulus). All output limbs will be in range
409  * (-2^62,2^62).
410  *
411  * This implements the update_de function from the explanation.
412  */
414  const uint64_t M62 = UINT64_MAX >> 2;
415  const int64_t d0 = d->v[0], d1 = d->v[1], d2 = d->v[2], d3 = d->v[3], d4 = d->v[4];
416  const int64_t e0 = e->v[0], e1 = e->v[1], e2 = e->v[2], e3 = e->v[3], e4 = e->v[4];
417  const int64_t u = t->u, v = t->v, q = t->q, r = t->r;
418  int64_t md, me, sd, se;
419  secp256k1_int128 cd, ce;
420 #ifdef VERIFY
421  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(d, 5, &modinfo->modulus, -2) > 0); /* d > -2*modulus */
422  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(d, 5, &modinfo->modulus, 1) < 0); /* d < modulus */
423  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(e, 5, &modinfo->modulus, -2) > 0); /* e > -2*modulus */
424  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(e, 5, &modinfo->modulus, 1) < 0); /* e < modulus */
425  VERIFY_CHECK(secp256k1_modinv64_abs(u) <= (((int64_t)1 << 62) - secp256k1_modinv64_abs(v))); /* |u|+|v| <= 2^62 */
426  VERIFY_CHECK(secp256k1_modinv64_abs(q) <= (((int64_t)1 << 62) - secp256k1_modinv64_abs(r))); /* |q|+|r| <= 2^62 */
427 #endif
428  /* [md,me] start as zero; plus [u,q] if d is negative; plus [v,r] if e is negative. */
429  sd = d4 >> 63;
430  se = e4 >> 63;
431  md = (u & sd) + (v & se);
432  me = (q & sd) + (r & se);
433  /* Begin computing t*[d,e]. */
434  secp256k1_i128_mul(&cd, u, d0);
435  secp256k1_i128_accum_mul(&cd, v, e0);
436  secp256k1_i128_mul(&ce, q, d0);
437  secp256k1_i128_accum_mul(&ce, r, e0);
438  /* Correct md,me so that t*[d,e]+modulus*[md,me] has 62 zero bottom bits. */
439  md -= (modinfo->modulus_inv62 * secp256k1_i128_to_u64(&cd) + md) & M62;
440  me -= (modinfo->modulus_inv62 * secp256k1_i128_to_u64(&ce) + me) & M62;
441  /* Update the beginning of computation for t*[d,e]+modulus*[md,me] now md,me are known. */
442  secp256k1_i128_accum_mul(&cd, modinfo->modulus.v[0], md);
443  secp256k1_i128_accum_mul(&ce, modinfo->modulus.v[0], me);
444  /* Verify that the low 62 bits of the computation are indeed zero, and then throw them away. */
445  VERIFY_CHECK((secp256k1_i128_to_u64(&cd) & M62) == 0); secp256k1_i128_rshift(&cd, 62);
446  VERIFY_CHECK((secp256k1_i128_to_u64(&ce) & M62) == 0); secp256k1_i128_rshift(&ce, 62);
447  /* Compute limb 1 of t*[d,e]+modulus*[md,me], and store it as output limb 0 (= down shift). */
448  secp256k1_i128_accum_mul(&cd, u, d1);
449  secp256k1_i128_accum_mul(&cd, v, e1);
450  secp256k1_i128_accum_mul(&ce, q, d1);
451  secp256k1_i128_accum_mul(&ce, r, e1);
452  if (modinfo->modulus.v[1]) { /* Optimize for the case where limb of modulus is zero. */
453  secp256k1_i128_accum_mul(&cd, modinfo->modulus.v[1], md);
454  secp256k1_i128_accum_mul(&ce, modinfo->modulus.v[1], me);
455  }
456  d->v[0] = secp256k1_i128_to_u64(&cd) & M62; secp256k1_i128_rshift(&cd, 62);
457  e->v[0] = secp256k1_i128_to_u64(&ce) & M62; secp256k1_i128_rshift(&ce, 62);
458  /* Compute limb 2 of t*[d,e]+modulus*[md,me], and store it as output limb 1. */
459  secp256k1_i128_accum_mul(&cd, u, d2);
460  secp256k1_i128_accum_mul(&cd, v, e2);
461  secp256k1_i128_accum_mul(&ce, q, d2);
462  secp256k1_i128_accum_mul(&ce, r, e2);
463  if (modinfo->modulus.v[2]) { /* Optimize for the case where limb of modulus is zero. */
464  secp256k1_i128_accum_mul(&cd, modinfo->modulus.v[2], md);
465  secp256k1_i128_accum_mul(&ce, modinfo->modulus.v[2], me);
466  }
467  d->v[1] = secp256k1_i128_to_u64(&cd) & M62; secp256k1_i128_rshift(&cd, 62);
468  e->v[1] = secp256k1_i128_to_u64(&ce) & M62; secp256k1_i128_rshift(&ce, 62);
469  /* Compute limb 3 of t*[d,e]+modulus*[md,me], and store it as output limb 2. */
470  secp256k1_i128_accum_mul(&cd, u, d3);
471  secp256k1_i128_accum_mul(&cd, v, e3);
472  secp256k1_i128_accum_mul(&ce, q, d3);
473  secp256k1_i128_accum_mul(&ce, r, e3);
474  if (modinfo->modulus.v[3]) { /* Optimize for the case where limb of modulus is zero. */
475  secp256k1_i128_accum_mul(&cd, modinfo->modulus.v[3], md);
476  secp256k1_i128_accum_mul(&ce, modinfo->modulus.v[3], me);
477  }
478  d->v[2] = secp256k1_i128_to_u64(&cd) & M62; secp256k1_i128_rshift(&cd, 62);
479  e->v[2] = secp256k1_i128_to_u64(&ce) & M62; secp256k1_i128_rshift(&ce, 62);
480  /* Compute limb 4 of t*[d,e]+modulus*[md,me], and store it as output limb 3. */
481  secp256k1_i128_accum_mul(&cd, u, d4);
482  secp256k1_i128_accum_mul(&cd, v, e4);
483  secp256k1_i128_accum_mul(&ce, q, d4);
484  secp256k1_i128_accum_mul(&ce, r, e4);
485  secp256k1_i128_accum_mul(&cd, modinfo->modulus.v[4], md);
486  secp256k1_i128_accum_mul(&ce, modinfo->modulus.v[4], me);
487  d->v[3] = secp256k1_i128_to_u64(&cd) & M62; secp256k1_i128_rshift(&cd, 62);
488  e->v[3] = secp256k1_i128_to_u64(&ce) & M62; secp256k1_i128_rshift(&ce, 62);
489  /* What remains is limb 5 of t*[d,e]+modulus*[md,me]; store it as output limb 4. */
490  d->v[4] = secp256k1_i128_to_i64(&cd);
491  e->v[4] = secp256k1_i128_to_i64(&ce);
492 #ifdef VERIFY
493  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(d, 5, &modinfo->modulus, -2) > 0); /* d > -2*modulus */
494  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(d, 5, &modinfo->modulus, 1) < 0); /* d < modulus */
495  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(e, 5, &modinfo->modulus, -2) > 0); /* e > -2*modulus */
496  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(e, 5, &modinfo->modulus, 1) < 0); /* e < modulus */
497 #endif
498 }
499 
500 /* Compute (t/2^62) * [f, g], where t is a transition matrix scaled by 2^62.
501  *
502  * This implements the update_fg function from the explanation.
503  */
505  const uint64_t M62 = UINT64_MAX >> 2;
506  const int64_t f0 = f->v[0], f1 = f->v[1], f2 = f->v[2], f3 = f->v[3], f4 = f->v[4];
507  const int64_t g0 = g->v[0], g1 = g->v[1], g2 = g->v[2], g3 = g->v[3], g4 = g->v[4];
508  const int64_t u = t->u, v = t->v, q = t->q, r = t->r;
509  secp256k1_int128 cf, cg;
510  /* Start computing t*[f,g]. */
511  secp256k1_i128_mul(&cf, u, f0);
512  secp256k1_i128_accum_mul(&cf, v, g0);
513  secp256k1_i128_mul(&cg, q, f0);
514  secp256k1_i128_accum_mul(&cg, r, g0);
515  /* Verify that the bottom 62 bits of the result are zero, and then throw them away. */
516  VERIFY_CHECK((secp256k1_i128_to_u64(&cf) & M62) == 0); secp256k1_i128_rshift(&cf, 62);
517  VERIFY_CHECK((secp256k1_i128_to_u64(&cg) & M62) == 0); secp256k1_i128_rshift(&cg, 62);
518  /* Compute limb 1 of t*[f,g], and store it as output limb 0 (= down shift). */
519  secp256k1_i128_accum_mul(&cf, u, f1);
520  secp256k1_i128_accum_mul(&cf, v, g1);
521  secp256k1_i128_accum_mul(&cg, q, f1);
522  secp256k1_i128_accum_mul(&cg, r, g1);
523  f->v[0] = secp256k1_i128_to_u64(&cf) & M62; secp256k1_i128_rshift(&cf, 62);
524  g->v[0] = secp256k1_i128_to_u64(&cg) & M62; secp256k1_i128_rshift(&cg, 62);
525  /* Compute limb 2 of t*[f,g], and store it as output limb 1. */
526  secp256k1_i128_accum_mul(&cf, u, f2);
527  secp256k1_i128_accum_mul(&cf, v, g2);
528  secp256k1_i128_accum_mul(&cg, q, f2);
529  secp256k1_i128_accum_mul(&cg, r, g2);
530  f->v[1] = secp256k1_i128_to_u64(&cf) & M62; secp256k1_i128_rshift(&cf, 62);
531  g->v[1] = secp256k1_i128_to_u64(&cg) & M62; secp256k1_i128_rshift(&cg, 62);
532  /* Compute limb 3 of t*[f,g], and store it as output limb 2. */
533  secp256k1_i128_accum_mul(&cf, u, f3);
534  secp256k1_i128_accum_mul(&cf, v, g3);
535  secp256k1_i128_accum_mul(&cg, q, f3);
536  secp256k1_i128_accum_mul(&cg, r, g3);
537  f->v[2] = secp256k1_i128_to_u64(&cf) & M62; secp256k1_i128_rshift(&cf, 62);
538  g->v[2] = secp256k1_i128_to_u64(&cg) & M62; secp256k1_i128_rshift(&cg, 62);
539  /* Compute limb 4 of t*[f,g], and store it as output limb 3. */
540  secp256k1_i128_accum_mul(&cf, u, f4);
541  secp256k1_i128_accum_mul(&cf, v, g4);
542  secp256k1_i128_accum_mul(&cg, q, f4);
543  secp256k1_i128_accum_mul(&cg, r, g4);
544  f->v[3] = secp256k1_i128_to_u64(&cf) & M62; secp256k1_i128_rshift(&cf, 62);
545  g->v[3] = secp256k1_i128_to_u64(&cg) & M62; secp256k1_i128_rshift(&cg, 62);
546  /* What remains is limb 5 of t*[f,g]; store it as output limb 4. */
547  f->v[4] = secp256k1_i128_to_i64(&cf);
548  g->v[4] = secp256k1_i128_to_i64(&cg);
549 }
550 
551 /* Compute (t/2^62) * [f, g], where t is a transition matrix for 62 divsteps.
552  *
553  * Version that operates on a variable number of limbs in f and g.
554  *
555  * This implements the update_fg function from the explanation.
556  */
558  const uint64_t M62 = UINT64_MAX >> 2;
559  const int64_t u = t->u, v = t->v, q = t->q, r = t->r;
560  int64_t fi, gi;
561  secp256k1_int128 cf, cg;
562  int i;
563  VERIFY_CHECK(len > 0);
564  /* Start computing t*[f,g]. */
565  fi = f->v[0];
566  gi = g->v[0];
567  secp256k1_i128_mul(&cf, u, fi);
568  secp256k1_i128_accum_mul(&cf, v, gi);
569  secp256k1_i128_mul(&cg, q, fi);
570  secp256k1_i128_accum_mul(&cg, r, gi);
571  /* Verify that the bottom 62 bits of the result are zero, and then throw them away. */
572  VERIFY_CHECK((secp256k1_i128_to_u64(&cf) & M62) == 0); secp256k1_i128_rshift(&cf, 62);
573  VERIFY_CHECK((secp256k1_i128_to_u64(&cg) & M62) == 0); secp256k1_i128_rshift(&cg, 62);
574  /* Now iteratively compute limb i=1..len of t*[f,g], and store them in output limb i-1 (shifting
575  * down by 62 bits). */
576  for (i = 1; i < len; ++i) {
577  fi = f->v[i];
578  gi = g->v[i];
579  secp256k1_i128_accum_mul(&cf, u, fi);
580  secp256k1_i128_accum_mul(&cf, v, gi);
581  secp256k1_i128_accum_mul(&cg, q, fi);
582  secp256k1_i128_accum_mul(&cg, r, gi);
583  f->v[i - 1] = secp256k1_i128_to_u64(&cf) & M62; secp256k1_i128_rshift(&cf, 62);
584  g->v[i - 1] = secp256k1_i128_to_u64(&cg) & M62; secp256k1_i128_rshift(&cg, 62);
585  }
586  /* What remains is limb (len) of t*[f,g]; store it as output limb (len-1). */
587  f->v[len - 1] = secp256k1_i128_to_i64(&cf);
588  g->v[len - 1] = secp256k1_i128_to_i64(&cg);
589 }
590 
591 /* Compute the inverse of x modulo modinfo->modulus, and replace x with it (constant time in x). */
593  /* Start with d=0, e=1, f=modulus, g=x, zeta=-1. */
594  secp256k1_modinv64_signed62 d = {{0, 0, 0, 0, 0}};
595  secp256k1_modinv64_signed62 e = {{1, 0, 0, 0, 0}};
598  int i;
599  int64_t zeta = -1; /* zeta = -(delta+1/2); delta starts at 1/2. */
600 
601  /* Do 10 iterations of 59 divsteps each = 590 divsteps. This suffices for 256-bit inputs. */
602  for (i = 0; i < 10; ++i) {
603  /* Compute transition matrix and new zeta after 59 divsteps. */
605  zeta = secp256k1_modinv64_divsteps_59(zeta, f.v[0], g.v[0], &t);
606  /* Update d,e using that transition matrix. */
607  secp256k1_modinv64_update_de_62(&d, &e, &t, modinfo);
608  /* Update f,g using that transition matrix. */
609 #ifdef VERIFY
610  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, -1) > 0); /* f > -modulus */
611  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, 1) <= 0); /* f <= modulus */
612  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, 5, &modinfo->modulus, -1) > 0); /* g > -modulus */
613  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, 5, &modinfo->modulus, 1) < 0); /* g < modulus */
614 #endif
616 #ifdef VERIFY
617  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, -1) > 0); /* f > -modulus */
618  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, 1) <= 0); /* f <= modulus */
619  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, 5, &modinfo->modulus, -1) > 0); /* g > -modulus */
620  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, 5, &modinfo->modulus, 1) < 0); /* g < modulus */
621 #endif
622  }
623 
624  /* At this point sufficient iterations have been performed that g must have reached 0
625  * and (if g was not originally 0) f must now equal +/- GCD of the initial f, g
626  * values i.e. +/- 1, and d now contains +/- the modular inverse. */
627 #ifdef VERIFY
628  /* g == 0 */
629  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, 5, &SECP256K1_SIGNED62_ONE, 0) == 0);
630  /* |f| == 1, or (x == 0 and d == 0 and |f|=modulus) */
631  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &SECP256K1_SIGNED62_ONE, -1) == 0 ||
632  secp256k1_modinv64_mul_cmp_62(&f, 5, &SECP256K1_SIGNED62_ONE, 1) == 0 ||
633  (secp256k1_modinv64_mul_cmp_62(x, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
634  secp256k1_modinv64_mul_cmp_62(&d, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
635  (secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, 1) == 0 ||
636  secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->modulus, -1) == 0)));
637 #endif
638 
639  /* Optionally negate d, normalize to [0,modulus), and return it. */
640  secp256k1_modinv64_normalize_62(&d, f.v[4], modinfo);
641  *x = d;
642 }
643 
644 /* Compute the inverse of x modulo modinfo->modulus, and replace x with it (variable time). */
646  /* Start with d=0, e=1, f=modulus, g=x, eta=-1. */
647  secp256k1_modinv64_signed62 d = {{0, 0, 0, 0, 0}};
648  secp256k1_modinv64_signed62 e = {{1, 0, 0, 0, 0}};
651 #ifdef VERIFY
652  int i = 0;
653 #endif
654  int j, len = 5;
655  int64_t eta = -1; /* eta = -delta; delta is initially 1 */
656  int64_t cond, fn, gn;
657 
658  /* Do iterations of 62 divsteps each until g=0. */
659  while (1) {
660  /* Compute transition matrix and new eta after 62 divsteps. */
662  eta = secp256k1_modinv64_divsteps_62_var(eta, f.v[0], g.v[0], &t);
663  /* Update d,e using that transition matrix. */
664  secp256k1_modinv64_update_de_62(&d, &e, &t, modinfo);
665  /* Update f,g using that transition matrix. */
666 #ifdef VERIFY
667  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, -1) > 0); /* f > -modulus */
668  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 1) <= 0); /* f <= modulus */
669  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, -1) > 0); /* g > -modulus */
670  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 1) < 0); /* g < modulus */
671 #endif
673  /* If the bottom limb of g is zero, there is a chance that g=0. */
674  if (g.v[0] == 0) {
675  cond = 0;
676  /* Check if the other limbs are also 0. */
677  for (j = 1; j < len; ++j) {
678  cond |= g.v[j];
679  }
680  /* If so, we're done. */
681  if (cond == 0) break;
682  }
683 
684  /* Determine if len>1 and limb (len-1) of both f and g is 0 or -1. */
685  fn = f.v[len - 1];
686  gn = g.v[len - 1];
687  cond = ((int64_t)len - 2) >> 63;
688  cond |= fn ^ (fn >> 63);
689  cond |= gn ^ (gn >> 63);
690  /* If so, reduce length, propagating the sign of f and g's top limb into the one below. */
691  if (cond == 0) {
692  f.v[len - 2] |= (uint64_t)fn << 62;
693  g.v[len - 2] |= (uint64_t)gn << 62;
694  --len;
695  }
696 #ifdef VERIFY
697  VERIFY_CHECK(++i < 12); /* We should never need more than 12*62 = 744 divsteps */
698  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, -1) > 0); /* f > -modulus */
699  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 1) <= 0); /* f <= modulus */
700  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, -1) > 0); /* g > -modulus */
701  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 1) < 0); /* g < modulus */
702 #endif
703  }
704 
705  /* At this point g is 0 and (if g was not originally 0) f must now equal +/- GCD of
706  * the initial f, g values i.e. +/- 1, and d now contains +/- the modular inverse. */
707 #ifdef VERIFY
708  /* g == 0 */
709  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &SECP256K1_SIGNED62_ONE, 0) == 0);
710  /* |f| == 1, or (x == 0 and d == 0 and |f|=modulus) */
711  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &SECP256K1_SIGNED62_ONE, -1) == 0 ||
712  secp256k1_modinv64_mul_cmp_62(&f, len, &SECP256K1_SIGNED62_ONE, 1) == 0 ||
713  (secp256k1_modinv64_mul_cmp_62(x, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
714  secp256k1_modinv64_mul_cmp_62(&d, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
715  (secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 1) == 0 ||
716  secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, -1) == 0)));
717 #endif
718 
719  /* Optionally negate d, normalize to [0,modulus), and return it. */
720  secp256k1_modinv64_normalize_62(&d, f.v[len - 1], modinfo);
721  *x = d;
722 }
723 
724 /* Do up to 25 iterations of 62 posdivsteps (up to 1550 steps; more is extremely rare) each until f=1.
725  * In VERIFY mode use a lower number of iterations (744, close to the median 756), so failure actually occurs. */
726 #ifdef VERIFY
727 #define JACOBI64_ITERATIONS 12
728 #else
729 #define JACOBI64_ITERATIONS 25
730 #endif
731 
732 /* Compute the Jacobi symbol of x modulo modinfo->modulus (variable time). gcd(x,modulus) must be 1. */
734  /* Start with f=modulus, g=x, eta=-1. */
737  int j, len = 5;
738  int64_t eta = -1; /* eta = -delta; delta is initially 1 */
739  int64_t cond, fn, gn;
740  int jac = 0;
741  int count;
742 
743  /* The input limbs must all be non-negative. */
744  VERIFY_CHECK(g.v[0] >= 0 && g.v[1] >= 0 && g.v[2] >= 0 && g.v[3] >= 0 && g.v[4] >= 0);
745 
746  /* If x > 0, then if the loop below converges, it converges to f=g=gcd(x,modulus). Since we
747  * require that gcd(x,modulus)=1 and modulus>=3, x cannot be 0. Thus, we must reach f=1 (or
748  * time out). */
749  VERIFY_CHECK((g.v[0] | g.v[1] | g.v[2] | g.v[3] | g.v[4]) != 0);
750 
751  for (count = 0; count < JACOBI64_ITERATIONS; ++count) {
752  /* Compute transition matrix and new eta after 62 posdivsteps. */
754  eta = secp256k1_modinv64_posdivsteps_62_var(eta, f.v[0] | ((uint64_t)f.v[1] << 62), g.v[0] | ((uint64_t)g.v[1] << 62), &t, &jac);
755  /* Update f,g using that transition matrix. */
756 #ifdef VERIFY
757  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 0) > 0); /* f > 0 */
758  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 1) <= 0); /* f <= modulus */
759  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 0) > 0); /* g > 0 */
760  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 1) < 0); /* g < modulus */
761 #endif
763  /* If the bottom limb of f is 1, there is a chance that f=1. */
764  if (f.v[0] == 1) {
765  cond = 0;
766  /* Check if the other limbs are also 0. */
767  for (j = 1; j < len; ++j) {
768  cond |= f.v[j];
769  }
770  /* If so, we're done. When f=1, the Jacobi symbol (g | f)=1. */
771  if (cond == 0) return 1 - 2*(jac & 1);
772  }
773 
774  /* Determine if len>1 and limb (len-1) of both f and g is 0. */
775  fn = f.v[len - 1];
776  gn = g.v[len - 1];
777  cond = ((int64_t)len - 2) >> 63;
778  cond |= fn;
779  cond |= gn;
780  /* If so, reduce length. */
781  if (cond == 0) --len;
782 #ifdef VERIFY
783  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 0) > 0); /* f > 0 */
784  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, 1) <= 0); /* f <= modulus */
785  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 0) > 0); /* g > 0 */
786  VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&g, len, &modinfo->modulus, 1) < 0); /* g < modulus */
787 #endif
788  }
789 
790  /* The loop failed to converge to f=g after 1550 iterations. Return 0, indicating unknown result. */
791  return 0;
792 }
793 
794 #endif /* SECP256K1_MODINV64_IMPL_H */
#define VERIFY_CHECK(cond)
Definition: util.h:143
static int64_t secp256k1_modinv64_posdivsteps_62_var(int64_t eta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t, int *jacp)
static void secp256k1_modinv64(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static SECP256K1_INLINE void secp256k1_i128_mul(secp256k1_int128 *r, int64_t a, int64_t b)
static SECP256K1_INLINE void secp256k1_i128_from_i64(secp256k1_int128 *r, int64_t a)
static int64_t secp256k1_modinv64_divsteps_62_var(int64_t eta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t)
static void secp256k1_modinv64_update_fg_62_var(int len, secp256k1_modinv64_signed62 *f, secp256k1_modinv64_signed62 *g, const secp256k1_modinv64_trans2x2 *t)
static SECP256K1_INLINE void secp256k1_i128_rshift(secp256k1_int128 *r, unsigned int n)
static SECP256K1_INLINE void secp256k1_i128_accum_mul(secp256k1_int128 *r, int64_t a, int64_t b)
int128_t secp256k1_int128
Definition: int128_native.h:17
static void secp256k1_modinv64_update_de_62(secp256k1_modinv64_signed62 *d, secp256k1_modinv64_signed62 *e, const secp256k1_modinv64_trans2x2 *t, const secp256k1_modinv64_modinfo *modinfo)
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x)
Definition: util.h:328
static void secp256k1_modinv64_update_fg_62(secp256k1_modinv64_signed62 *f, secp256k1_modinv64_signed62 *g, const secp256k1_modinv64_trans2x2 *t)
static SECP256K1_INLINE void secp256k1_i128_det(secp256k1_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d)
static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t)
secp256k1_modinv64_signed62 modulus
Definition: modinv64.h:25
static SECP256K1_INLINE int64_t secp256k1_i128_to_i64(const secp256k1_int128 *a)
static int secp256k1_jacobi64_maybe_var(const secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static SECP256K1_INLINE int secp256k1_i128_eq_var(const secp256k1_int128 *a, const secp256k1_int128 *b)
static SECP256K1_INLINE int secp256k1_i128_check_pow2(const secp256k1_int128 *r, unsigned int n, int sign)
static void secp256k1_modinv64_var(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
#define JACOBI64_ITERATIONS
static int count
static SECP256K1_INLINE uint64_t secp256k1_i128_to_u64(const secp256k1_int128 *a)
static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int64_t sign, const secp256k1_modinv64_modinfo *modinfo)
Definition: modinv64_impl.h:88