Skip to content

SP int: fixes from review by Claude#10235

Merged
dgarske merged 1 commit intowolfSSL:masterfrom
SparkiDev:sp_int_fixes_6
Apr 16, 2026
Merged

SP int: fixes from review by Claude#10235
dgarske merged 1 commit intowolfSSL:masterfrom
SparkiDev:sp_int_fixes_6

Conversation

@SparkiDev
Copy link
Copy Markdown
Contributor

@SparkiDev SparkiDev commented Apr 16, 2026

Description

  1. sp_cond_swap_ct_ex (line ~5524) — XOR typo: b->sign ^= b->sign always
    zeroed the sign. Fixed to b->sign ^= t->sign to correctly swap signs.
  2. sp_mod_d (line ~7271) — Negative modulo correction was applied even
    when the remainder was 0. Added (*r != 0) guard to avoid producing d
    instead of 0.
  3. sp_lshb (line ~8444) — Left-shift size check was off. Refactored to
    correctly distinguish between pure-digit shifts and bit-within-digit
    shifts when checking if the result fits, using separate overflow checks
    for each case.
  4. _sp_mulmod_tmp (line ~12160) — Zero inputs caused an allocation of
    size 0, which is problematic. Added an early path: if either operand is
    zero, set result to zero and skip the allocation/multiply entirely.
  5. sp_mod_2d — copy path (line ~14762) — XMEMCPY copied digits *
    SP_WORD_SIZEOF bytes but a may have fewer than digits used digits. Fixed
    to copy min(a->used, digits) digits to avoid reading uninitialized
    memory.
  6. sp_mod_2d — negation loop (line ~14782) — Negation loop iterated
    over r->used, which could exceed digits. Fixed to loop over min(r->used,
    digits).
  7. _sp_sqrmod (line ~17314) — Same zero-input issue as _sp_mulmod_tmp.
    Added early zero path to skip the allocation/squaring when input is
    zero.
  8. sp_lcm (line ~19838) — Typo in sign check: b->sign >= MP_NEG
    (comparing against a value that is 1, so >= 1 would also match MP_ZPOS)
    changed to b->sign == MP_NEG.

Testing

Standard

@SparkiDev SparkiDev self-assigned this Apr 16, 2026
1. sp_cond_swap_ct_ex (line ~5524) — XOR typo: b->sign ^= b->sign always
zeroed the sign. Fixed to b->sign ^= t->sign to correctly swap signs.
2. sp_mod_d (line ~7271) — Negative modulo correction was applied even
when the remainder was 0. Added (*r != 0) guard to avoid producing d
instead of 0.
3. sp_lshb (line ~8444) — Left-shift size check was off. Refactored to
correctly distinguish between pure-digit shifts and bit-within-digit
shifts when checking if the result fits, using separate overflow checks
for each case.
4. _sp_mulmod_tmp (line ~12160) — Zero inputs caused an allocation of
size 0, which is problematic. Added an early path: if either operand is
zero, set result to zero and skip the allocation/multiply entirely.
5. sp_mod_2d — copy path (line ~14762) — XMEMCPY copied digits *
SP_WORD_SIZEOF bytes but a may have fewer than digits used digits. Fixed
to copy min(a->used, digits) digits to avoid reading uninitialized
memory.
6. sp_mod_2d — negation loop (line ~14782) — Negation loop iterated
over r->used, which could exceed digits. Fixed to loop over min(r->used,
digits).
7. _sp_sqrmod (line ~17314) — Same zero-input issue as _sp_mulmod_tmp.
Added early zero path to skip the allocation/squaring when input is
zero.
8. sp_lcm (line ~19838) — Typo in sign check: b->sign >= MP_NEG
(comparing against a value that is 1, so >= 1 would also match MP_ZPOS)
changed to b->sign == MP_NEG.
@github-actions
Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Apr 16, 2026
@SparkiDev SparkiDev requested a review from wolfSSL-Bot April 16, 2026 09:50
@dgarske dgarske merged commit 3d548f4 into wolfSSL:master Apr 16, 2026
447 of 452 checks passed
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.

3 participants