7#ifndef SECP256K1_MODINV32_IMPL_H
8#define SECP256K1_MODINV32_IMPL_H
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;
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];
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;
189 uint32_t c1, c2,
f = f0, g = g0, x, y, z;
192 for (i = 0; i < 30; ++i) {
210 zeta = (zeta ^ c1) - 1;
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
265 int i = 30, limit, zeros;
269 zeros = secp256k1_ctz32_var(g | (
UINT32_MAX << i));
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);
300 w = (g * inv256[(
f >> 1) & 127]) & m;
346 md = (u & sd) + (v & se);
347 me = (q & sd) + (
r & se);
364 for (i = 1; i < 9; ++i) {
371 d->v[i - 1] = (
int32_t)cd & M30; cd >>= 30;
372 e->
v[i - 1] = (
int32_t)ce & M30; ce >>= 30;
405 for (i = 1; i < 9; ++i) {
410 f->v[i - 1] = (
int32_t)cf & M30; cf >>= 30;
411 g->
v[i - 1] = (
int32_t)cg & M30; cg >>= 30;
441 for (i = 1; i <
len; ++i) {
446 f->v[i - 1] = (
int32_t)cf & M30; cf >>= 30;
447 g->
v[i - 1] = (
int32_t)cg & M30; cg >>= 30;
465 for (i = 0; i < 20; ++i) {
468 zeta = secp256k1_modinv32_divsteps_30(zeta,
f.v[0], g.
v[0], &t);
470 secp256k1_modinv32_update_de_30(&d, &e, &t, modinfo);
478 secp256k1_modinv32_update_fg_30(&
f, &g, &t);
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)));
503 secp256k1_modinv32_normalize_30(&d,
f.v[8], modinfo);
525 eta = secp256k1_modinv32_divsteps_30_var(eta,
f.v[0], g.
v[0], &t);
527 secp256k1_modinv32_update_de_30(&d, &e, &t, modinfo);
535 secp256k1_modinv32_update_fg_30_var(
len, &
f, &g, &t);
540 for (
j = 1;
j <
len; ++
j) {
544 if (cond == 0)
break;
551 cond |=
fn ^ (
fn >> 31);
552 cond |= gn ^ (gn >> 31);
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)));
583 secp256k1_modinv32_normalize_30(&d,
f.v[
len - 1], modinfo);
#define VERIFY_CHECK(cond)
static const Reg16 di(Operand::DI)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
secp256k1_modinv32_signed30 modulus