Merge bitcoin-core/secp256k1#1819: tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812)

f47bbc07f0 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 f47bbc07f0
  real-or-random:
    utACK f47bbc07f0

Tree-SHA512: dad3aa31ecf3f296843c907ac3d9aa5a9b9cb839b36aa3b59e49c853c60c58291412e70dff37dc15f8e14023a8f1e1aba87395065607612d5f6cfa92e14e73b5
This commit is contained in:
merge-script
2026-02-04 20:24:44 +01:00

View File

@@ -2193,8 +2193,58 @@ static void run_scalar_set_b32_seckey_tests(void) {
CHECK(secp256k1_scalar_set_b32_seckey(&s2, b32) == 0);
}
static void test_scalar_check_overflow(void) {
secp256k1_scalar s;
const secp256k1_scalar n_minus_1 = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364140UL
);
const secp256k1_scalar n = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
);
const secp256k1_scalar n_plus_1 = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364142UL
);
const secp256k1_scalar max = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
);
int i;
secp256k1_scalar_set_int(&s, 0);
CHECK(secp256k1_scalar_check_overflow(&s) == 0);
CHECK(secp256k1_scalar_check_overflow(&n_minus_1) == 0);
CHECK(secp256k1_scalar_check_overflow(&n) == 1);
CHECK(secp256k1_scalar_check_overflow(&n_plus_1) == 1);
CHECK(secp256k1_scalar_check_overflow(&max) == 1);
for (i = 0; i < 2 * COUNT; i++) {
int expected_overflow;
int overflow = 0;
unsigned char b32[32];
testrand256(b32);
/* Force top bits to be 0xFF sometimes to ensure we hit overflows */
if (i % 2 == 0) {
memset(b32, 0xFF, 16);
}
expected_overflow = (secp256k1_memcmp_var(b32, secp256k1_group_order_bytes, 32) >= 0);
secp256k1_scalar_set_b32(&s, b32, &overflow);
CHECK(overflow == expected_overflow);
}
}
static void run_scalar_tests(void) {
int i;
test_scalar_check_overflow();
for (i = 0; i < 128 * COUNT; i++) {
scalar_test();
}
@@ -2258,15 +2308,6 @@ static void run_scalar_tests(void) {
}
}
{
/* Does check_overflow check catch all ones? */
static const secp256k1_scalar overflowed = SECP256K1_SCALAR_CONST(
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL,
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL
);
CHECK(secp256k1_scalar_check_overflow(&overflowed));
}
{
/* Static test vectors.
* These were reduced from ~10^12 random vectors based on comparison-decision