7#ifndef SECP256K1_MODINV32_IMPL_H
8#define SECP256K1_MODINV32_IMPL_H
28 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
31 for (i = 0; i < 8; ++i) {
32 if (i < alen) c += (int64_t)a->
v[i] * factor;
33 r->
v[i] = (int32_t)c & M30; c >>= 30;
35 if (8 < alen) c += (int64_t)a->
v[8] * factor;
44 secp256k1_modinv32_mul_30(&am, a, alen, 1);
45 secp256k1_modinv32_mul_30(&bm, b, 9, factor);
46 for (i = 0; i < 8; ++i) {
51 for (i = 8; i >= 0; --i) {
52 if (am.
v[i] < bm.
v[i])
return -1;
53 if (am.
v[i] > bm.
v[i])
return 1;
64 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
65 int32_t r0 = r->
v[0], r1 = r->
v[1], r2 = r->
v[2], r3 = r->
v[3], r4 = r->
v[4],
66 r5 = r->
v[5], r6 = r->
v[6], r7 = r->
v[7], r8 = r->
v[8];
67 volatile int32_t cond_add, cond_negate;
72 for (i = 0; i < 9; ++i) {
86 r0 += modinfo->
modulus.
v[0] & cond_add;
87 r1 += modinfo->
modulus.
v[1] & cond_add;
88 r2 += modinfo->
modulus.
v[2] & cond_add;
89 r3 += modinfo->
modulus.
v[3] & cond_add;
90 r4 += modinfo->
modulus.
v[4] & cond_add;
91 r5 += modinfo->
modulus.
v[5] & cond_add;
92 r6 += modinfo->
modulus.
v[6] & cond_add;
93 r7 += modinfo->
modulus.
v[7] & cond_add;
94 r8 += modinfo->
modulus.
v[8] & cond_add;
95 cond_negate =
sign >> 31;
96 r0 = (r0 ^ cond_negate) - cond_negate;
97 r1 = (r1 ^ cond_negate) - cond_negate;
98 r2 = (r2 ^ cond_negate) - cond_negate;
99 r3 = (r3 ^ cond_negate) - cond_negate;
100 r4 = (r4 ^ cond_negate) - cond_negate;
101 r5 = (r5 ^ cond_negate) - cond_negate;
102 r6 = (r6 ^ cond_negate) - cond_negate;
103 r7 = (r7 ^ cond_negate) - cond_negate;
104 r8 = (r8 ^ cond_negate) - cond_negate;
106 r1 += r0 >> 30; r0 &= M30;
107 r2 += r1 >> 30; r1 &= M30;
108 r3 += r2 >> 30; r2 &= M30;
109 r4 += r3 >> 30; r3 &= M30;
110 r5 += r4 >> 30; r4 &= M30;
111 r6 += r5 >> 30; r5 &= M30;
112 r7 += r6 >> 30; r6 &= M30;
113 r8 += r7 >> 30; r7 &= M30;
118 r0 += modinfo->
modulus.
v[0] & cond_add;
119 r1 += modinfo->
modulus.
v[1] & cond_add;
120 r2 += modinfo->
modulus.
v[2] & cond_add;
121 r3 += modinfo->
modulus.
v[3] & cond_add;
122 r4 += modinfo->
modulus.
v[4] & cond_add;
123 r5 += modinfo->
modulus.
v[5] & cond_add;
124 r6 += modinfo->
modulus.
v[6] & cond_add;
125 r7 += modinfo->
modulus.
v[7] & cond_add;
126 r8 += modinfo->
modulus.
v[8] & cond_add;
128 r1 += r0 >> 30; r0 &= M30;
129 r2 += r1 >> 30; r1 &= M30;
130 r3 += r2 >> 30; r2 &= M30;
131 r4 += r3 >> 30; r3 &= M30;
132 r5 += r4 >> 30; r4 &= M30;
133 r6 += r5 >> 30; r5 &= M30;
134 r7 += r6 >> 30; r6 &= M30;
135 r8 += r7 >> 30; r7 &= M30;
186 uint32_t u = 1, v = 0, q = 0, r = 1;
187 volatile uint32_t c1, c2;
188 uint32_t mask1, mask2, f = f0,
g = g0, x, y, z;
191 for (i = 0; i < 30; ++i) {
201 x = (f ^ mask1) - mask1;
202 y = (u ^ mask1) - mask1;
203 z = (v ^ mask1) - mask1;
211 zeta = (zeta ^ mask1) - 1;
232 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30);
238 0xFF, 0x55, 0x33, 0x49, 0xC7, 0x5D, 0x3B, 0x11, 0x0F, 0xE5, 0xC3, 0x59,
239 0xD7, 0xED, 0xCB, 0x21, 0x1F, 0x75, 0x53, 0x69, 0xE7, 0x7D, 0x5B, 0x31,
240 0x2F, 0x05, 0xE3, 0x79, 0xF7, 0x0D, 0xEB, 0x41, 0x3F, 0x95, 0x73, 0x89,
241 0x07, 0x9D, 0x7B, 0x51, 0x4F, 0x25, 0x03, 0x99, 0x17, 0x2D, 0x0B, 0x61,
242 0x5F, 0xB5, 0x93, 0xA9, 0x27, 0xBD, 0x9B, 0x71, 0x6F, 0x45, 0x23, 0xB9,
243 0x37, 0x4D, 0x2B, 0x81, 0x7F, 0xD5, 0xB3, 0xC9, 0x47, 0xDD, 0xBB, 0x91,
244 0x8F, 0x65, 0x43, 0xD9, 0x57, 0x6D, 0x4B, 0xA1, 0x9F, 0xF5, 0xD3, 0xE9,
245 0x67, 0xFD, 0xDB, 0xB1, 0xAF, 0x85, 0x63, 0xF9, 0x77, 0x8D, 0x6B, 0xC1,
246 0xBF, 0x15, 0xF3, 0x09, 0x87, 0x1D, 0xFB, 0xD1, 0xCF, 0xA5, 0x83, 0x19,
247 0x97, 0xAD, 0x8B, 0xE1, 0xDF, 0x35, 0x13, 0x29, 0xA7, 0x3D, 0x1B, 0xF1,
248 0xEF, 0xC5, 0xA3, 0x39, 0xB7, 0xCD, 0xAB, 0x01
263 uint32_t u = 1, v = 0, q = 0, r = 1;
264 uint32_t f = f0,
g = g0,
m;
266 int i = 30, limit, zeros;
289 tmp = f; f =
g;
g = -tmp;
290 tmp = u; u = q; q = -tmp;
291 tmp = v; v = r; r = -tmp;
296 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
299 m = (UINT32_MAX >> (32 - limit)) & 255U;
317 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30);
336 uint32_t u = 1, v = 0, q = 0, r = 1;
337 uint32_t f = f0,
g = g0,
m;
339 int i = 30, limit, zeros;
353 jac ^= (zeros & ((f >> 1) ^ (f >> 2)));
366 jac ^= ((f &
g) >> 1);
367 tmp = f; f =
g;
g = tmp;
368 tmp = u; u = q; q = tmp;
369 tmp = v; v = r; r = tmp;
374 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
377 m = (UINT32_MAX >> (32 - limit)) & 255U;
395 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30 ||
396 (int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == -(((int64_t)1) << 30));
409 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
410 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
411 int32_t di, ei, md, me, sd, se;
424 md = (u & sd) + (v & se);
425 me = (q & sd) + (r & se);
429 cd = (int64_t)u * di + (int64_t)v * ei;
430 ce = (int64_t)q * di + (int64_t)r * ei;
435 cd += (int64_t)modinfo->
modulus.
v[0] * md;
436 ce += (int64_t)modinfo->
modulus.
v[0] * me;
442 for (i = 1; i < 9; ++i) {
445 cd += (int64_t)u * di + (int64_t)v * ei;
446 ce += (int64_t)q * di + (int64_t)r * ei;
447 cd += (int64_t)modinfo->
modulus.
v[i] * md;
448 ce += (int64_t)modinfo->
modulus.
v[i] * me;
449 d->
v[i - 1] = (int32_t)cd & M30; cd >>= 30;
450 e->
v[i - 1] = (int32_t)ce & M30; ce >>= 30;
453 d->
v[8] = (int32_t)cd;
454 e->
v[8] = (int32_t)ce;
467 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
468 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
475 cf = (int64_t)u * fi + (int64_t)v * gi;
476 cg = (int64_t)q * fi + (int64_t)r * gi;
482 for (i = 1; i < 9; ++i) {
485 cf += (int64_t)u * fi + (int64_t)v * gi;
486 cg += (int64_t)q * fi + (int64_t)r * gi;
487 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
488 g->v[i - 1] = (int32_t)cg & M30; cg >>= 30;
491 f->
v[8] = (int32_t)cf;
492 g->v[8] = (int32_t)cg;
502 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
503 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
511 cf = (int64_t)u * fi + (int64_t)v * gi;
512 cg = (int64_t)q * fi + (int64_t)r * gi;
518 for (i = 1; i < len; ++i) {
521 cf += (int64_t)u * fi + (int64_t)v * gi;
522 cg += (int64_t)q * fi + (int64_t)r * gi;
523 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
524 g->v[i - 1] = (int32_t)cg & M30; cg >>= 30;
527 f->
v[len - 1] = (int32_t)cf;
528 g->v[len - 1] = (int32_t)cg;
542 for (i = 0; i < 20; ++i) {
567 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&
g, 9, &SECP256K1_SIGNED30_ONE, 0) == 0);
569 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
570 secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
571 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
572 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
573 secp256k1_modinv32_mul_cmp_30(&f, 9, &modinfo->
modulus, 1) == 0));
592 int32_t cond, fn, gn;
613 for (j = 1; j < len; ++j) {
617 if (cond == 0)
break;
623 cond = ((int32_t)len - 2) >> 31;
624 cond |= fn ^ (fn >> 31);
625 cond |= gn ^ (gn >> 31);
628 f.
v[len - 2] |= (uint32_t)fn << 30;
629 g.v[len - 2] |= (uint32_t)gn << 30;
644 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&
g, len, &SECP256K1_SIGNED30_ONE, 0) == 0);
646 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
647 secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
648 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
649 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
650 secp256k1_modinv32_mul_cmp_30(&f, len, &modinfo->
modulus, 1) == 0));
660#define JACOBI32_ITERATIONS 25
662#define JACOBI32_ITERATIONS 50
672 int32_t cond, fn, gn;
677 VERIFY_CHECK(
g.v[0] >= 0 &&
g.v[1] >= 0 &&
g.v[2] >= 0 &&
g.v[3] >= 0 &&
g.v[4] >= 0 &&
g.v[5] >= 0 &&
g.v[6] >= 0 &&
g.v[7] >= 0 &&
g.v[8] >= 0);
699 for (j = 1; j < len; ++j) {
703 if (cond == 0)
return 1 - 2*(jac & 1);
709 cond = ((int32_t)len - 2) >> 31;
713 if (cond == 0) --len;
static void secp256k1_modinv32_update_fg_30_var(int len, secp256k1_modinv32_signed30 *f, secp256k1_modinv32_signed30 *g, const secp256k1_modinv32_trans2x2 *t)
static void secp256k1_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static int32_t secp256k1_modinv32_divsteps_30_var(int32_t eta, uint32_t f0, uint32_t g0, secp256k1_modinv32_trans2x2 *t)
static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int32_t sign, const secp256k1_modinv32_modinfo *modinfo)
#define JACOBI32_ITERATIONS
static int32_t secp256k1_modinv32_posdivsteps_30_var(int32_t eta, uint32_t f0, uint32_t g0, secp256k1_modinv32_trans2x2 *t, int *jacp)
static void secp256k1_modinv32(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static int32_t secp256k1_modinv32_divsteps_30(int32_t zeta, uint32_t f0, uint32_t g0, secp256k1_modinv32_trans2x2 *t)
static int secp256k1_jacobi32_maybe_var(const secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static void secp256k1_modinv32_update_fg_30(secp256k1_modinv32_signed30 *f, secp256k1_modinv32_signed30 *g, const secp256k1_modinv32_trans2x2 *t)
static const uint8_t secp256k1_modinv32_inv256[128]
static void secp256k1_modinv32_update_de_30(secp256k1_modinv32_signed30 *d, secp256k1_modinv32_signed30 *e, const secp256k1_modinv32_trans2x2 *t, const secp256k1_modinv32_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_ctz32_var(uint32_t x)
#define VERIFY_CHECK(cond)
secp256k1_modinv32_signed30 modulus