7#ifndef SECP256K1_MODINV64_IMPL_H
8#define SECP256K1_MODINV64_IMPL_H
32static int64_t secp256k1_modinv64_abs(int64_t v) {
42 const uint64_t M62 = UINT64_MAX >> 2;
46 for (i = 0; i < 4; ++i) {
60 secp256k1_modinv64_mul_62(&am, a, alen, 1);
61 secp256k1_modinv64_mul_62(&bm, b, 5, factor);
62 for (i = 0; i < 4; ++i) {
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;
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;
96 for (i = 0; i < 5; ++i) {
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;
122 r1 += r0 >> 62; r0 &= M62;
123 r2 += r1 >> 62; r1 &= M62;
124 r3 += r2 >> 62; r2 &= M62;
125 r4 += r3 >> 62; r3 &= M62;
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;
136 r1 += r0 >> 62; r0 &= M62;
137 r2 += r1 >> 62; r1 &= M62;
138 r3 += r2 >> 62; r2 &= M62;
139 r4 += r3 >> 62; r3 &= M62;
175 uint64_t u = 8, v = 0, q = 0, r = 8;
176 volatile uint64_t c1, c2;
177 uint64_t mask1, mask2, f = f0,
g = g0, x, y, z;
180 for (i = 3; i < 62; ++i) {
190 x = (f ^ mask1) - mask1;
191 y = (u ^ mask1) - mask1;
192 z = (v ^ mask1) - mask1;
200 zeta = (zeta ^ mask1) - 1;
241 uint64_t u = 1, v = 0, q = 0, r = 1;
242 uint64_t f = f0,
g = g0,
m;
244 int i = 62, limit, zeros;
267 tmp = f; f =
g;
g = -tmp;
268 tmp = u; u = q; q = -tmp;
269 tmp = v; v = r; r = -tmp;
273 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
276 m = (UINT64_MAX >> (64 - limit)) & 63U;
279 w = (f *
g * (f * f - 2)) &
m;
283 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
286 m = (UINT64_MAX >> (64 - limit)) & 15U;
289 w = f + (((f + 1) & 4) << 1);
327 uint64_t u = 1, v = 0, q = 0, r = 1;
328 uint64_t f = f0,
g = g0,
m;
330 int i = 62, limit, zeros;
344 jac ^= (zeros & ((f >> 1) ^ (f >> 2)));
355 tmp = f; f =
g;
g = tmp;
356 tmp = u; u = q; q = tmp;
357 tmp = v; v = r; r = tmp;
360 jac ^= ((f &
g) >> 1);
364 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
367 m = (UINT64_MAX >> (64 - limit)) & 63U;
370 w = (f *
g * (f * f - 2)) &
m;
374 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
377 m = (UINT64_MAX >> (64 - limit)) & 15U;
380 w = f + (((f + 1) & 4) << 1);
412 const uint64_t M62 = UINT64_MAX >> 2;
413 const int64_t d0 = d->
v[0], d1 = d->
v[1], d2 = d->
v[2], d3 = d->
v[3], d4 = d->
v[4];
414 const int64_t e0 = e->
v[0], e1 = e->
v[1], e2 = e->
v[2], e3 = e->
v[3], e4 = e->
v[4];
415 const int64_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
416 int64_t md, me, sd, se;
422 VERIFY_CHECK(secp256k1_modinv64_abs(u) <= (((int64_t)1 << 62) - secp256k1_modinv64_abs(v)));
423 VERIFY_CHECK(secp256k1_modinv64_abs(q) <= (((int64_t)1 << 62) - secp256k1_modinv64_abs(r)));
428 md = (u & sd) + (v & se);
429 me = (q & sd) + (r & se);
501 const uint64_t M62 = UINT64_MAX >> 2;
502 const int64_t f0 = f->
v[0], f1 = f->
v[1], f2 = f->
v[2], f3 = f->
v[3], f4 = f->
v[4];
503 const int64_t g0 =
g->v[0], g1 =
g->v[1], g2 =
g->v[2], g3 =
g->v[3], g4 =
g->v[4];
504 const int64_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
554 const uint64_t M62 = UINT64_MAX >> 2;
555 const int64_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
572 for (i = 1; i < len; ++i) {
598 for (i = 0; i < 10; ++i) {
623 VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&
g, 5, &SECP256K1_SIGNED62_ONE, 0) == 0);
625 VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, 5, &SECP256K1_SIGNED62_ONE, -1) == 0 ||
626 secp256k1_modinv64_mul_cmp_62(&f, 5, &SECP256K1_SIGNED62_ONE, 1) == 0 ||
627 (secp256k1_modinv64_mul_cmp_62(x, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
628 secp256k1_modinv64_mul_cmp_62(&d, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
629 secp256k1_modinv64_mul_cmp_62(&f, 5, &modinfo->
modulus, 1) == 0));
648 int64_t cond, fn, gn;
668 for (j = 1; j < len; ++j) {
672 if (cond == 0)
break;
678 cond = ((int64_t)len - 2) >> 63;
679 cond |= fn ^ (fn >> 63);
680 cond |= gn ^ (gn >> 63);
683 f.
v[len - 2] |= (uint64_t)fn << 62;
684 g.v[len - 2] |= (uint64_t)gn << 62;
699 VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&
g, len, &SECP256K1_SIGNED62_ONE, 0) == 0);
701 VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &SECP256K1_SIGNED62_ONE, -1) == 0 ||
702 secp256k1_modinv64_mul_cmp_62(&f, len, &SECP256K1_SIGNED62_ONE, 1) == 0 ||
703 (secp256k1_modinv64_mul_cmp_62(x, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
704 secp256k1_modinv64_mul_cmp_62(&d, 5, &SECP256K1_SIGNED62_ONE, 0) == 0 &&
705 secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->
modulus, 1) == 0));
715#define JACOBI64_ITERATIONS 12
717#define JACOBI64_ITERATIONS 25
727 int64_t cond, fn, gn;
732 VERIFY_CHECK(
g.v[0] >= 0 &&
g.v[1] >= 0 &&
g.v[2] >= 0 &&
g.v[3] >= 0 &&
g.v[4] >= 0);
754 for (j = 1; j < len; ++j) {
758 if (cond == 0)
return 1 - 2*(jac & 1);
764 cond = ((int64_t)len - 2) >> 63;
768 if (cond == 0) --len;
int128_t secp256k1_int128
static SECP256K1_INLINE void secp256k1_i128_det(secp256k1_int128 *r, int64_t a, int64_t b, int64_t c, int64_t d)
static SECP256K1_INLINE void secp256k1_i128_rshift(secp256k1_int128 *r, unsigned int n)
static SECP256K1_INLINE uint64_t secp256k1_i128_to_u64(const secp256k1_int128 *a)
static SECP256K1_INLINE void secp256k1_i128_from_i64(secp256k1_int128 *r, int64_t a)
static SECP256K1_INLINE int secp256k1_i128_eq_var(const secp256k1_int128 *a, const secp256k1_int128 *b)
static SECP256K1_INLINE int64_t secp256k1_i128_to_i64(const secp256k1_int128 *a)
static SECP256K1_INLINE void secp256k1_i128_mul(secp256k1_int128 *r, int64_t a, int64_t b)
static SECP256K1_INLINE int secp256k1_i128_check_pow2(const secp256k1_int128 *r, unsigned int n, int sign)
static SECP256K1_INLINE void secp256k1_i128_accum_mul(secp256k1_int128 *r, int64_t a, int64_t b)
static int64_t secp256k1_modinv64_posdivsteps_62_var(int64_t eta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t, int *jacp)
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_normalize_62(secp256k1_modinv64_signed62 *r, int64_t sign, const secp256k1_modinv64_modinfo *modinfo)
#define JACOBI64_ITERATIONS
static void secp256k1_modinv64(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static void secp256k1_modinv64_var(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static void secp256k1_modinv64_update_fg_62_var(int len, secp256k1_modinv64_signed62 *f, secp256k1_modinv64_signed62 *g, const secp256k1_modinv64_trans2x2 *t)
static int secp256k1_jacobi64_maybe_var(const secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_t g0, secp256k1_modinv64_trans2x2 *t)
static void secp256k1_modinv64_update_fg_62(secp256k1_modinv64_signed62 *f, secp256k1_modinv64_signed62 *g, const secp256k1_modinv64_trans2x2 *t)
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 int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
static SECP256K1_INLINE int secp256k1_ctz64_var(uint64_t x)
#define VERIFY_CHECK(cond)
secp256k1_modinv64_signed62 modulus