Skip to content

Commit 04c6c1b

Browse files
committed
Make secp256k1_modinv64_det_check_pow2 support abs val
1 parent 5fffb2c commit 04c6c1b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/modinv64_impl.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ static int secp256k1_modinv64_mul_cmp_62(const secp256k1_modinv64_signed62 *a, i
7171
return 0;
7272
}
7373

74-
/* Check if the determinant of t is equal to 1 << n. */
75-
static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *t, unsigned int n) {
74+
/* Check if the determinant of t is equal to 1 << n. If abs, check if |det t| == 1 << n. */
75+
static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *t, unsigned int n, int abs) {
7676
secp256k1_int128 a;
7777
secp256k1_i128_det(&a, t->u, t->v, t->q, t->r);
78-
return secp256k1_i128_check_pow2(&a, n, 1);
78+
if (secp256k1_i128_check_pow2(&a, n, 1)) return 1;
79+
if (abs && secp256k1_i128_check_pow2(&a, n, -1)) return 1;
80+
return 0;
7981
}
8082
#endif
8183

@@ -218,7 +220,7 @@ static int64_t secp256k1_modinv64_divsteps_59(int64_t zeta, uint64_t f0, uint64_
218220
* aggregate of 59 of them will have determinant 2^59. Multiplying with the initial
219221
* 8*identity (which has determinant 2^6) means the overall outputs has determinant
220222
* 2^65. */
221-
VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 65));
223+
VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 65, 0));
222224
#endif
223225
return zeta;
224226
}
@@ -301,7 +303,7 @@ static int64_t secp256k1_modinv64_divsteps_62_var(int64_t eta, uint64_t f0, uint
301303
* does not change the gcd of f and g, apart from adding a power-of-2 factor to it (which
302304
* will be divided out again). As each divstep's individual matrix has determinant 2, the
303305
* aggregate of 62 of them will have determinant 2^62. */
304-
VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 62));
306+
VERIFY_CHECK(secp256k1_modinv64_det_check_pow2(t, 62, 0));
305307
#endif
306308
return eta;
307309
}

0 commit comments

Comments
 (0)