Skip to content

tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812)#1819

Merged
real-or-random merged 1 commit intobitcoin-core:masterfrom
therohityadav:test-overflow
Feb 4, 2026
Merged

tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812)#1819
real-or-random merged 1 commit intobitcoin-core:masterfrom
therohityadav:test-overflow

Conversation

@therohityadav
Copy link
Contributor

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.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces should be removed to pass the CI.

@therohityadav
Copy link
Contributor Author

Trailing spaces should be removed to pass the CI.

Thank you, I have removed the trailing whitespace and all the tests passed.

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@real-or-random
Copy link
Contributor

Thank you, I have removed the trailing whitespace and all the tests passed.

Thanks, and please squash your commits. :)

@therohityadav
Copy link
Contributor Author

Thank you, I have removed the trailing whitespace and all the tests passed.

Thanks, and please squash your commits. :)

Thanks for the Concept ACK and the review! I have addressed all the feedback:

  • Restored the global count variable and used it for the loop iteration.
  • Switched to testrand256 for randomness.
  • Used secp256k1_group_order_bytes to avoid duplicating constants.
  • Squashed the commits into one.

@real-or-random
Copy link
Contributor

I don't see any diff compared to your last push. (See https://github.com/bitcoin-core/secp256k1/compare/58be14be2de04cc39df0f286826f0b19b8809fe0..83568d863521ba25a2f3bc791f0776a898cdb2cb)

But I see you squashed. Did you push the wrong commit?

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, I have just one more comment

src/tests.c Outdated
CHECK(secp256k1_scalar_check_overflow(&n_plus_1) == 1);
CHECK(secp256k1_scalar_check_overflow(&max) == 1);

for (i = 0; i < count; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I literally meant COUNT, a global variable in the tests. This loop could run 2 * COUNT times, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I literally meant COUNT, a global variable in the tests. This loop could run 2 * COUNT times, I think.

Thank you for the catch. I have removed the local count variable and updated the loop to use the global COUNT with 2 * COUNT iterations.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept and code-review ACK 6b49efd

As a follow-up idea, maybe a deduplication of these scalar constants with other tests could make sense (see e.g. $ git grep -i 0xbaeedce6).


static void run_scalar_tests(void) {
int i;
test_scalar_check_overflow();
Copy link
Contributor

@theStack theStack Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit: could add a newline before to separate the declaration block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking nit: could add a newline before to separate the declaration block

I have added the newline to separate the declaration block.

Regarding the scalar constant deduplication: I am looking into doing that in a follow-up PR to keep this one focused.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK f47bbc0

Copy link
Contributor

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK f47bbc0

@real-or-random real-or-random merged commit 1d146ac into bitcoin-core:master Feb 4, 2026
122 checks passed
@real-or-random
Copy link
Contributor

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tests: Improve _scalar_check_overflow tests

4 participants