sp_int: fixes and added testing#10529
Merged
Merged
Conversation
edff6df to
91ccb1d
Compare
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
Contributor
Author
|
_sp_sqr() code generated with PR: |
JacobBarthelmeh
approved these changes
May 27, 2026
Contributor
There was a problem hiding this comment.
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 oni),_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_tcast cleanups. - Many new test cases in
mp_test: negative-value radix conversions, negativemp_add_d/mp_sub_d,mp_mod_dfast paths and negative input, multi-wordmp_gcd(now also tested withoutmp_lcm), max-valuemp_sqr,mp_addmod_ct/mp_submod_ct/mp_div_2_mod_ct,mp_cmp_mag,mp_mulmod/mp_sqrmodresult-aliasing,mp_exch,mp_to_unsigned_bin_len_ct, base-2/windowedmp_exptmod, andmp_invmod_mont_ct. - Test plumbing:
mp_test_add_sub_dgains aboperand,mp_test_exptmodgains atscratch operand, and themp_test_lcm_gcdguards are split via a newMP_TEST_HAVE_LCMmacro somp_gcdis 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.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #10529
Scan targets checked: wolfcrypt-bugs, wolfcrypt-src
No new issues found in the changed files. ✅
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
./configure --enable-all