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 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 uint32_t c1, c2, f = f0, g = g0, x, y, z;
192 for (i = 0; i < 30; ++i) {
210 eta = (eta ^ c1) - (c1 + 1);
231 VERIFY_CHECK((int64_t)t->
u * t->
r - (int64_t)t->
v * t->
q == ((int64_t)1) << 30);
247 static const uint8_t inv256[128] = {
248 0xFF, 0x55, 0x33, 0x49, 0xC7, 0x5D, 0x3B, 0x11, 0x0F, 0xE5, 0xC3, 0x59,
249 0xD7, 0xED, 0xCB, 0x21, 0x1F, 0x75, 0x53, 0x69, 0xE7, 0x7D, 0x5B, 0x31,
250 0x2F, 0x05, 0xE3, 0x79, 0xF7, 0x0D, 0xEB, 0x41, 0x3F, 0x95, 0x73, 0x89,
251 0x07, 0x9D, 0x7B, 0x51, 0x4F, 0x25, 0x03, 0x99, 0x17, 0x2D, 0x0B, 0x61,
252 0x5F, 0xB5, 0x93, 0xA9, 0x27, 0xBD, 0x9B, 0x71, 0x6F, 0x45, 0x23, 0xB9,
253 0x37, 0x4D, 0x2B, 0x81, 0x7F, 0xD5, 0xB3, 0xC9, 0x47, 0xDD, 0xBB, 0x91,
254 0x8F, 0x65, 0x43, 0xD9, 0x57, 0x6D, 0x4B, 0xA1, 0x9F, 0xF5, 0xD3, 0xE9,
255 0x67, 0xFD, 0xDB, 0xB1, 0xAF, 0x85, 0x63, 0xF9, 0x77, 0x8D, 0x6B, 0xC1,
256 0xBF, 0x15, 0xF3, 0x09, 0x87, 0x1D, 0xFB, 0xD1, 0xCF, 0xA5, 0x83, 0x19,
257 0x97, 0xAD, 0x8B, 0xE1, 0xDF, 0x35, 0x13, 0x29, 0xA7, 0x3D, 0x1B, 0xF1,
258 0xEF, 0xC5, 0xA3, 0x39, 0xB7, 0xCD, 0xAB, 0x01
262 uint32_t u = 1, v = 0, q = 0, r = 1;
263 uint32_t f = f0, g = g0, m;
265 int i = 30, limit, zeros;
288 tmp = f; f = g; g = -tmp;
289 tmp = u; u = q; q = -tmp;
290 tmp = v; v = r; r = -tmp;
295 limit = ((int)eta + 1) > i ? i : ((int)eta + 1);
298 m = (UINT32_MAX >> (32 - limit)) & 255U;
300 w = (g * inv256[(f >> 1) & 127]) & m;
316 VERIFY_CHECK((int64_t)t->
u * t->
r - (int64_t)t->
v * t->
q == ((int64_t)1) << 30);
328 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
329 const int32_t u = t->
u, v = t->
v, q = t->
q, r = t->
r;
330 int32_t di, ei, md, me, sd, se;
346 md = (u & sd) + (v & se);
347 me = (q & sd) + (r & se);
351 cd = (int64_t)u * di + (int64_t)v * ei;
352 ce = (int64_t)q * di + (int64_t)r * ei;
357 cd += (int64_t)modinfo->
modulus.
v[0] * md;
358 ce += (int64_t)modinfo->
modulus.
v[0] * me;
364 for (i = 1; i < 9; ++i) {
367 cd += (int64_t)u * di + (int64_t)v * ei;
368 ce += (int64_t)q * di + (int64_t)r * ei;
369 cd += (int64_t)modinfo->
modulus.
v[i] * md;
370 ce += (int64_t)modinfo->
modulus.
v[i] * me;
371 d->
v[i - 1] = (int32_t)cd & M30; cd >>= 30;
372 e->
v[i - 1] = (int32_t)ce & M30; ce >>= 30;
375 d->
v[8] = (int32_t)cd;
376 e->
v[8] = (int32_t)ce;
390 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
391 const int32_t u = t->
u, v = t->
v, q = t->
q, r = t->
r;
398 cf = (int64_t)u * fi + (int64_t)v * gi;
399 cg = (int64_t)q * fi + (int64_t)r * gi;
405 for (i = 1; i < 9; ++i) {
408 cf += (int64_t)u * fi + (int64_t)v * gi;
409 cg += (int64_t)q * fi + (int64_t)r * gi;
410 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
411 g->
v[i - 1] = (int32_t)cg & M30; cg >>= 30;
414 f->
v[8] = (int32_t)cf;
415 g->
v[8] = (int32_t)cg;
425 const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
426 const int32_t u = t->
u, v = t->
v, q = t->
q, r = t->
r;
434 cf = (int64_t)u * fi + (int64_t)v * gi;
435 cg = (int64_t)q * fi + (int64_t)r * gi;
441 for (i = 1; i < len; ++i) {
444 cf += (int64_t)u * fi + (int64_t)v * gi;
445 cg += (int64_t)q * fi + (int64_t)r * gi;
446 f->
v[i - 1] = (int32_t)cf & M30; cf >>= 30;
447 g->
v[i - 1] = (int32_t)cg & M30; cg >>= 30;
450 f->
v[len - 1] = (int32_t)cf;
451 g->
v[len - 1] = (int32_t)cg;
465 for (i = 0; i < 25; ++i) {
492 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&g, 9, &SECP256K1_SIGNED30_ONE, 0) == 0);
494 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
495 secp256k1_modinv32_mul_cmp_30(&f, 9, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
496 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
497 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
498 (secp256k1_modinv32_mul_cmp_30(&f, 9, &modinfo->
modulus, 1) == 0 ||
499 secp256k1_modinv32_mul_cmp_30(&f, 9, &modinfo->
modulus, -1) == 0)));
519 int32_t cond, fn, gn;
540 for (j = 1; j < len; ++j) {
544 if (cond == 0)
break;
550 cond = ((int32_t)len - 2) >> 31;
551 cond |= fn ^ (fn >> 31);
552 cond |= gn ^ (gn >> 31);
555 f.
v[len - 2] |= (uint32_t)fn << 30;
556 g.
v[len - 2] |= (uint32_t)gn << 30;
572 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&g, len, &SECP256K1_SIGNED30_ONE, 0) == 0);
574 VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, -1) == 0 ||
575 secp256k1_modinv32_mul_cmp_30(&f, len, &SECP256K1_SIGNED30_ONE, 1) == 0 ||
576 (secp256k1_modinv32_mul_cmp_30(x, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
577 secp256k1_modinv32_mul_cmp_30(&d, 9, &SECP256K1_SIGNED30_ONE, 0) == 0 &&
578 (secp256k1_modinv32_mul_cmp_30(&f, len, &modinfo->
modulus, 1) == 0 ||
579 secp256k1_modinv32_mul_cmp_30(&f, len, &modinfo->
modulus, -1) == 0)));
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)
static int32_t secp256k1_modinv32_divsteps_30(int32_t eta, uint32_t f0, uint32_t g0, secp256k1_modinv32_trans2x2 *t)
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 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 SECP256K1_INLINE int secp256k1_ctz32_var(uint32_t x)
#define VERIFY_CHECK(cond)
secp256k1_modinv32_signed30 modulus