Skip to content

sp_int: fixes and added testing#10529

Merged
JacobBarthelmeh merged 1 commit into
wolfSSL:masterfrom
SparkiDev:sp_fixes_8
May 27, 2026
Merged

sp_int: fixes and added testing#10529
JacobBarthelmeh merged 1 commit into
wolfSSL:masterfrom
SparkiDev:sp_fixes_8

Conversation

@SparkiDev
Copy link
Copy Markdown
Contributor

Description

sp_set_bit(): check i is in range before use.
_sp_div_2(): Use a constant-time clamp as called by a constant-time function.
_sp_sqr(): static buffer needs to be one larger for when ECC with P-521 is the largest size.

Add tests:

  • Testing negative numbers with mp_read_raidx/mp_to_radix 10/16
  • Testing negative numbers with mp_add_d/mp_sub_d
  • Testing of mp_gcd without mp_lcm
  • More testing of mp_mod_d and when negative numbers are used
  • Check maximum values work for square. Check of _sp_sqr() bug
  • Add testing of mp_add/sub_mod_ct
  • Add testing of mp_cmp_mag
  • Add testing of mp_mulmod/mp_sqrmod
  • Add testing of mp_exch
  • Add testing of mp_to_unsigned_bin_len_ct
  • Add testing of mp_exptmod that uses base-2 windowing method.
  • Add testing of mp_invmod_mont_ct

Testing

./configure --enable-all

@SparkiDev SparkiDev self-assigned this May 26, 2026
@SparkiDev SparkiDev force-pushed the sp_fixes_8 branch 2 times, most recently from edff6df to 91ccb1d Compare May 26, 2026 03:47
sp_set_bit(): check i is in range before use.
_sp_div_2(): Use a constant-time clamp as called by a constant-time
function.
_sp_sqr(): static buffer needs to be one larger for when ECC with P-521
is the largest size.

Add tests:
 - Testing negative numbers with mp_read_raidx/mp_to_radix 10/16
 - Testing negative numbers with mp_add_d/mp_sub_d
 - Testing of mp_gcd without mp_lcm
 - More testing of mp_mod_d and when negative numbers are used
 - Check maximum values work for square. Check of _sp_sqr() bug
 - Add testing of mp_add/sub_mod_ct
 - Add testing of mp_cmp_mag
 - Add testing of mp_mulmod/mp_sqrmod
 - Add testing of mp_exch
 - Add testing of mp_to_unsigned_bin_len_ct
 - Add testing of mp_exptmod that uses base-2 windowing method.
 - Add testing of mp_invmod_mont_ct
@SparkiDev
Copy link
Copy Markdown
Contributor Author

_sp_sqr() code generated with PR:
https://github.com/wolfSSL/scripts/pull/578

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes three bugs in wolfcrypt/src/sp_int.c and substantially extends the multi-precision integer tests in wolfcrypt/test/test.c to exercise the affected paths and other previously-untested behaviour.

Changes:

  • Bug fixes in sp_set_bit (range check on i), _sp_div_2 (constant-time clamp to keep the helper constant-time for its CT callers), and _sp_sqr (static buffer enlarged by one digit so ECC P-521 squaring fits). Also small sign-cast/sp_size_t cast cleanups.
  • Many new test cases in mp_test: negative-value radix conversions, negative mp_add_d/mp_sub_d, mp_mod_d fast paths and negative input, multi-word mp_gcd (now also tested without mp_lcm), max-value mp_sqr, mp_addmod_ct/mp_submod_ct/mp_div_2_mod_ct, mp_cmp_mag, mp_mulmod/mp_sqrmod result-aliasing, mp_exch, mp_to_unsigned_bin_len_ct, base-2/windowed mp_exptmod, and mp_invmod_mont_ct.
  • Test plumbing: mp_test_add_sub_d gains a b operand, mp_test_exptmod gains a t scratch operand, and the mp_test_lcm_gcd guards are split via a new MP_TEST_HAVE_LCM macro so mp_gcd is exercised independently.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
wolfcrypt/src/sp_int.c Range check in sp_set_bit, constant-time clamp in _sp_div_2, +1 digit on _sp_sqr static buffer, minor signed/sp_size_t cast cleanups.
wolfcrypt/test/test.c Extensive new mp_test coverage and updated dispatcher/signatures to feed the new tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JacobBarthelmeh JacobBarthelmeh merged commit cd82d7e into wolfSSL:master May 27, 2026
451 checks passed
Copy link
Copy Markdown

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10529

Scan targets checked: wolfcrypt-bugs, wolfcrypt-src

No new issues found in the changed files. ✅

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants