Skip to content

Commit 659eaa3

Browse files
committed
Optimize mul_cmp
1 parent d9a5267 commit 659eaa3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/modinv32_impl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ static int secp256k1_modinv32_mul_cmp_30(const secp256k1_modinv32_signed30 *a, i
4949
VERIFY_CHECK(bm.v[i] >> 30 == 0);
5050
}
5151
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;
52+
diff = (am.v[i] > bm.v[i]) - (am.v[i] < bm.v[i]);
53+
if (diff != 0) {
54+
return diff;
55+
}
5456
}
5557
return 0;
5658
}
@@ -77,6 +79,8 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
7779
VERIFY_CHECK(secp256k1_modinv32_mul_cmp_30(r, 9, &modinfo->modulus, 1) < 0); /* r < modulus */
7880
#endif
7981

82+
/* TODO: parallelize */
83+
8084
/* In a first step, add the modulus if the input is negative, and then negate if requested.
8185
* This brings r from range (-2*modulus,modulus) to range (-modulus,modulus). As all input
8286
* limbs are in range (-2^30,2^30), this cannot overflow an int32_t. Note that the right

src/modinv64_impl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ static int secp256k1_modinv64_mul_cmp_62(const secp256k1_modinv64_signed62 *a, i
6565
VERIFY_CHECK(bm.v[i] >> 62 == 0);
6666
}
6767
for (i = 4; i >= 0; --i) {
68-
if (am.v[i] < bm.v[i]) return -1;
69-
if (am.v[i] > bm.v[i]) return 1;
68+
int diff = (am.v[i] > bm.v[i]) - (am.v[i] < bm.v[i]);
69+
if (diff != 0) {
70+
return diff;
71+
}
7072
}
7173
return 0;
7274
}
@@ -101,6 +103,8 @@ static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int6
101103
VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(r, 5, &modinfo->modulus, 1) < 0); /* r < modulus */
102104
#endif
103105

106+
/* TODO: parallelize */
107+
104108
/* In a first step, add the modulus if the input is negative, and then negate if requested.
105109
* This brings r from range (-2*modulus,modulus) to range (-modulus,modulus). As all input
106110
* limbs are in range (-2^62,2^62), this cannot overflow an int64_t. Note that the right

0 commit comments

Comments
 (0)