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;
188 uint32_t u = 1, v = 0, q = 0, r = 1;
189 volatile uint32_t c1, c2;
190 uint32_t mask1, mask2, f = f0,
g = g0, x, y, z;
193 for (i = 0; i < 30; ++i) {
203 x = (f ^ mask1) - mask1;
204 y = (u ^ mask1) - mask1;
205 z = (v ^ mask1) - mask1;
213 zeta = (zeta ^ mask1) - 1;
234 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30);
240 0xFF, 0x55, 0x33, 0x49, 0xC7, 0x5D, 0x3B, 0x11, 0x0F, 0xE5, 0xC3, 0x59,
241 0xD7, 0xED, 0xCB, 0x21, 0x1F, 0x75, 0x53, 0x69, 0xE7, 0x7D, 0x5B, 0x31,
242 0x2F, 0x05, 0xE3, 0x79, 0xF7, 0x0D, 0xEB, 0x41, 0x3F, 0x95, 0x73, 0x89,
243 0x07, 0x9D, 0x7B, 0x51, 0x4F, 0x25, 0x03, 0x99, 0x17, 0x2D, 0x0B, 0x61,
244 0x5F, 0xB5, 0x93, 0xA9, 0x27, 0xBD, 0x9B, 0x71, 0x6F, 0x45, 0x23, 0xB9,
245 0x37, 0x4D, 0x2B, 0x81, 0x7F, 0xD5, 0xB3, 0xC9, 0x47, 0xDD, 0xBB, 0x91,
246 0x8F, 0x65, 0x43, 0xD9, 0x57, 0x6D, 0x4B, 0xA1, 0x9F, 0xF5, 0xD3, 0xE9,
247 0x67, 0xFD, 0xDB, 0xB1, 0xAF, 0x85, 0x63, 0xF9, 0x77, 0x8D, 0x6B, 0xC1,
248 0xBF, 0x15, 0xF3, 0x09, 0x87, 0x1D, 0xFB, 0xD1, 0xCF, 0xA5, 0x83, 0x19,
249 0x97, 0xAD, 0x8B, 0xE1, 0xDF, 0x35, 0x13, 0x29, 0xA7, 0x3D, 0x1B, 0xF1,
250 0xEF, 0xC5, 0xA3, 0x39, 0xB7, 0xCD, 0xAB, 0x01
265 uint32_t u = 1, v = 0, q = 0, r = 1;
266 uint32_t f = f0,
g = g0,
m;
268 int i = 30, limit, zeros;
291 tmp = f; f =
g;
g = -tmp;
292 tmp = u; u = q; q = -tmp;
293 tmp = v; v = r; r = -tmp;
298 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
301 m = (UINT32_MAX >> (32 - limit)) & 255U;
319 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30);
338 uint32_t u = 1, v = 0, q = 0, r = 1;
339 uint32_t f = f0,
g = g0,
m;
341 int i = 30, limit, zeros;
355 jac ^= (zeros & ((f >> 1) ^ (f >> 2)));
368 jac ^= ((f &
g) >> 1);
369 tmp = f; f =
g;
g = tmp;
370 tmp = u; u = q; q = tmp;
371 tmp = v; v = r; r = tmp;
376 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
379 m = (UINT32_MAX >> (32 - limit)) & 255U;
397 VERIFY_CHECK((int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == ((int64_t)1) << 30 ||
398 (int64_t)
t->u *
t->r - (int64_t)
t->v *
t->q == -(((int64_t)1) << 30));
411 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
412 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
413 int32_t di, ei, md, me, sd, se;
427 md = (u & sd) + (v & se);
428 me = (q & sd) + (r & se);
432 cd = (int64_t)u * di + (int64_t)v * ei;
433 ce = (int64_t)q * di + (int64_t)r * ei;
438 cd += (int64_t)modinfo->
modulus.
v[0] * md;
439 ce += (int64_t)modinfo->
modulus.
v[0] * me;
445 for (i = 1; i < 9; ++i) {
448 cd += (int64_t)u * di + (int64_t)v * ei;
449 ce += (int64_t)q * di + (int64_t)r * ei;
450 cd += (int64_t)modinfo->
modulus.
v[i] * md;
451 ce += (int64_t)modinfo->
modulus.
v[i] * me;
452 d->
v[i - 1] = (int32_t)cd & M30; cd >>= 30;
453 e->
v[i - 1] = (int32_t)ce & M30; ce >>= 30;
456 d->
v[8] = (int32_t)cd;
457 e->
v[8] = (int32_t)ce;
471 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
472 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
479 cf = (int64_t)u * fi + (int64_t)v * gi;
480 cg = (int64_t)q * fi + (int64_t)r * gi;
486 for (i = 1; i < 9; ++i) {
489 cf += (int64_t)u * fi + (int64_t)v * gi;
490 cg += (int64_t)q * fi + (int64_t)r * gi;
491 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
492 g->v[i - 1] = (int32_t)cg & M30; cg >>= 30;
495 f->
v[8] = (int32_t)cf;
496 g->v[8] = (int32_t)cg;
506 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
507 const int32_t u =
t->u, v =
t->v, q =
t->q, r =
t->r;
515 cf = (int64_t)u * fi + (int64_t)v * gi;
516 cg = (int64_t)q * fi + (int64_t)r * gi;
522 for (i = 1; i < len; ++i) {
525 cf += (int64_t)u * fi + (int64_t)v * gi;
526 cg += (int64_t)q * fi + (int64_t)r * gi;
527 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
528 g->v[i - 1] = (int32_t)cg & M30; cg >>= 30;
531 f->
v[len - 1] = (int32_t)cf;
532 g->v[len - 1] = (int32_t)cg;
546 for (i = 0; i < 20; ++i) {
573 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&
g, 9, &SECP256K1_SIGNED30_ONE, 0) == 0);
575 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
576 secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
577 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
578 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
579 (secp256k1_modinv32_mul_cmp_30(&f, 9, &modinfo->
modulus, 1) == 0 ||
580 secp256k1_modinv32_mul_cmp_30(&f, 9, &modinfo->
modulus, -1) == 0)));
600 int32_t cond, fn, gn;
621 for (j = 1; j < len; ++j) {
625 if (cond == 0)
break;
631 cond = ((int32_t)len - 2) >> 31;
632 cond |= fn ^ (fn >> 31);
633 cond |= gn ^ (gn >> 31);
636 f.
v[len - 2] |= (uint32_t)fn << 30;
637 g.v[len - 2] |= (uint32_t)gn << 30;
653 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&
g, len, &SECP256K1_SIGNED30_ONE, 0) == 0);
655 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
656 secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
657 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
658 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
659 (secp256k1_modinv32_mul_cmp_30(&f, len, &modinfo->
modulus, 1) == 0 ||
660 secp256k1_modinv32_mul_cmp_30(&f, len, &modinfo->
modulus, -1) == 0)));
671 #define JACOBI32_ITERATIONS 25 673 #define JACOBI32_ITERATIONS 50 683 int32_t cond, fn, gn;
688 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);
711 for (j = 1; j < len; ++j) {
715 if (cond == 0)
return 1 - 2*(jac & 1);
721 cond = ((int32_t)len - 2) >> 31;
725 if (cond == 0) --len;
#define VERIFY_CHECK(cond)
static void secp256k1_modinv32(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 int secp256k1_jacobi32_maybe_var(const secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static SECP256K1_INLINE int secp256k1_ctz32_var(uint32_t x)
static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int32_t sign, const secp256k1_modinv32_modinfo *modinfo)
#define JACOBI32_ITERATIONS
secp256k1_modinv32_signed30 modulus
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 void secp256k1_modinv32_var(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 int32_t secp256k1_modinv32_posdivsteps_30_var(int32_t eta, uint32_t f0, uint32_t g0, secp256k1_modinv32_trans2x2 *t, int *jacp)
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_update_fg_30_var(int len, secp256k1_modinv32_signed30 *f, secp256k1_modinv32_signed30 *g, const secp256k1_modinv32_trans2x2 *t)