Skip to content

Commit 1d146ac

Browse files
Merge #1819: tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812)
f47bbc0 test: add unit tests for secp256k1_scalar_check_overflow (Rohit Yadav) Pull request description: This Pull Request improves the tests for `secp256k1_scalar_check_overflow` as requested in #1812. ### Changes: - Removed the redundant "all ones" check from `run_scalar_tests`. - Added a new dedicated test function `test_scalar_check_overflow`. - Added static checks for edge cases: `0`, `N-1`, `N`, `N+1`, and `MAX`. - Added random input tests that verify `check_overflow` against a manual byte comparison. Fixes #1812. ACKs for top commit: theStack: re-ACK f47bbc0 real-or-random: utACK f47bbc0 Tree-SHA512: dad3aa31ecf3f296843c907ac3d9aa5a9b9cb839b36aa3b59e49c853c60c58291412e70dff37dc15f8e14023a8f1e1aba87395065607612d5f6cfa92e14e73b5
2 parents d071aa5 + f47bbc0 commit 1d146ac

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

src/tests.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,8 +2193,58 @@ static void run_scalar_set_b32_seckey_tests(void) {
21932193
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
21942194
}
21952195

2196+
static void test_scalar_check_overflow(void) {
2197+
secp256k1_scalar s;
2198+
const secp256k1_scalar n_minus_1 = SECP256K1_SCALAR_CONST(
2199+
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
2200+
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364140UL
2201+
);
2202+
const secp256k1_scalar n = SECP256K1_SCALAR_CONST(
2203+
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
2204+
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
2205+
);
2206+
const secp256k1_scalar n_plus_1 = SECP256K1_SCALAR_CONST(
2207+
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
2208+
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364142UL
2209+
);
2210+
const secp256k1_scalar max = SECP256K1_SCALAR_CONST(
2211+
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
2212+
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
2213+
);
2214+
2215+
int i;
2216+
2217+
secp256k1_scalar_set_int(&s, 0);
2218+
CHECK(secp256k1_scalar_check_overflow(&s) == 0);
2219+
CHECK(secp256k1_scalar_check_overflow(&n_minus_1) == 0);
2220+
CHECK(secp256k1_scalar_check_overflow(&n) == 1);
2221+
CHECK(secp256k1_scalar_check_overflow(&n_plus_1) == 1);
2222+
CHECK(secp256k1_scalar_check_overflow(&max) == 1);
2223+
2224+
for (i = 0; i < 2 * COUNT; i++) {
2225+
int expected_overflow;
2226+
int overflow = 0;
2227+
unsigned char b32[32];
2228+
2229+
testrand256(b32);
2230+
2231+
/* Force top bits to be 0xFF sometimes to ensure we hit overflows */
2232+
if (i % 2 == 0) {
2233+
memset(b32, 0xFF, 16);
2234+
}
2235+
2236+
expected_overflow = (secp256k1_memcmp_var(b32, secp256k1_group_order_bytes, 32) >= 0);
2237+
2238+
secp256k1_scalar_set_b32(&s, b32, &overflow);
2239+
CHECK(overflow == expected_overflow);
2240+
}
2241+
}
2242+
21962243
static void run_scalar_tests(void) {
21972244
int i;
2245+
2246+
test_scalar_check_overflow();
2247+
21982248
for (i = 0; i < 128 * COUNT; i++) {
21992249
scalar_test();
22002250
}
@@ -2258,15 +2308,6 @@ static void run_scalar_tests(void) {
22582308
}
22592309
}
22602310

2261-
{
2262-
/* Does check_overflow check catch all ones? */
2263-
static const secp256k1_scalar overflowed = SECP256K1_SCALAR_CONST(
2264-
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
2265-
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
2266-
);
2267-
CHECK(secp256k1_scalar_check_overflow(&overflowed));
2268-
}
2269-
22702311
{
22712312
/* Static test vectors.
22722313
* These were reduced from ~10^12 random vectors based on comparison-decision

0 commit comments

Comments
 (0)