test: add unit tests for secp256k1_scalar_check_overflow
What changed, and why it matters
This commit only adds new unit tests for an existing internal function that checks whether a number has overflowed the secp256k1 group order. It does not change any production code, cryptographic logic, or behavior that could affect users. The change improves test coverage but introduces no security issue.
No security action needed. This is a routine test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff in src/tests.c adds a dedicated test_scalar_check_overflow() function and calls it from run_scalar_tests(). It verifies boundary values around the curve order n (n-1, n, n+1, and UINT256_MAX) and compares scalar_set_b32’s overflow flag against a memcmp-based expected result. An older, weaker all-ones overflow check is removed. No implementation code is modified.
Changed components
src/tests.cInspect captured patch +50 / −9
diff --git a/src/tests.c b/src/tests.c
index e09f5c7..b2e43ed 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -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
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.