Repository navigation
Fix used digit handling on fast math - #12278
yosuke-wolfssl wants to merge 3 commits into
Conversation
- The negation loop in fp_mod_2d() stops at bmax. - test.c gains mp_test_no_digits_above_used(). mp_test_mod_2d() runs on fast math, uses two-digit negative operands, and checks each result with it. Issue: F-18451
There was a problem hiding this comment.
🟡 Changes recommended
The zero check can miss upper-half corruption in 64-bit digits, and the CI configuration does not enable the intended Koblitz curves, leaving their arithmetic untested.
2 open issues
What changed in this PR
This PR restores fast math’s requirement that digits at and above used remain zero, preventing stale digits from corrupting subsequent arithmetic in wolfCrypt.
Changes:
- Clear stale destination digits in constant-time helpers and random-number generation.
- Limit negative
fp_mod_2d()processing to the relevant digits. - Expand regression checks and add a constant-time Montgomery-reduction CI configuration.
| File | Description |
|---|---|
wolfcrypt/test/test.c |
Adds digit-invariant checks and extends arithmetic tests to fast math. |
wolfcrypt/src/wolfmath.c |
Clears the destination before setting its random-number length. |
wolfcrypt/src/tfm.c |
Fixes stale digits in modular arithmetic helpers. |
tests/api/test_wolfmath.c |
Checks unused digits after mp_rand(). |
.github/configs/os-check-linux.json |
Adds fast-math CI coverage with constant-time Montgomery reduction. |
Review effort: Balanced
|
7e51ee1 to
30dd258
Compare
|
@claude review |
There was a problem hiding this comment.
I reviewed this PR and found no bugs. It changes constant-time helpers in TomsFastMath that ECC and the Montgomery reduction depend on, so a maintainer familiar with tfm.c should still look at it.
What was reviewed:
- In
fp_div_2_mod_ct,fp_submod_ctandfp_addmod_ct, the new loops that clear the high digits stop at the destination's oldused. That is a public length, so they add no branch or loop that depends on secret data. Readingusedbefore the write is also safe when the destination is the same object asaorb. - In
fp_mod_2d, limiting negation tox < bmaxmatches the existingbmaxfill loop and the mask on the top digit that follow it. - The guard on
mp_test_mod_2dwas removed, butmp_testitself is only built underWOLFSSL_SP_MATH_ALLorUSE_FAST_MATH, so no build now reaches it withoutmp_mod_2d. I did not build or run the new tests or the CI leg.
Extended reasoning...
The change edits four arithmetic routines in wolfcrypt/src/tfm.c and mp_rand in wolfmath.c, adds fast-math checks to test.c and test_wolfmath.c, and adds one CI leg. The security-sensitive surface is constant-time modular arithmetic used by ECC and the Montgomery reduction. Reading the code, the fixes look correct and keep the constant-time property. I deferred instead of approving because this is core crypto math. Also, the two Copilot inline threads were resolved by the PR author, though later commits may have addressed them.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12278
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): .github/configs/os-check-linux.json
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Max
| } | ||
| c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1)); | ||
| c->used = i + 1; | ||
| for (i = c->used; i < oldused; i++) { |
There was a problem hiding this comment.
This function and others changed below must be constant time.
This changes make them not constant time.
There was a problem hiding this comment.
You are correct.
It now clears only dp[modulus->used], a single store at a public index.
- fp_addmod_ct(), fp_submod_ct() and fp_div_2_mod_ct() zero the destination's digit at the modulus length after writing the result. - mp_test_addsubmod_ct() runs on fast math and starts each destination one digit wider than the modulus; it and mp_test_mont() check the result with mp_test_no_digits_above_used(). - os-check-linux.json gains a fastmath-mont-red-ct leg. Issue: F-18450, F-18447
- mp_rand() calls mp_zero() before setting used on fast math. - test_mp_rand checks the digits above used on fast math. Issue: F-18454
30dd258 to
62da1f6
Compare

Problem
Fast math (
USE_FAST_MATH) requires every digit ofdp[]at and aboveusedto be zero.s_fp_add(), the borrow read infp_submod_ct()and the comba multipliers read pastusedand rely on it. Four writers leave non-zero digits there, so the result prints correctly but the next operation on it is wrong.Fix (
wolfcrypt/src/tfm.c)fp_submod_ct(), this also clears the Montgomery carry digit in bothfp_montgomery_reduce_ex()andfp_montgomery_reduce_mulx().fp_mod_2d()negates only the digits belowbmax.mp_rand()(wolfmath.c) callsmp_zero()before settingusedon fast math.Closes f-18447, f-18450, f-18451, f-18454.
Tests
mp_test_no_digits_above_used()intest.c.mp_test_addsubmod_ct()andmp_test_mod_2d()now also runs on fast math.test_mp_randchecks the digits aboveusedon fast math.fastmath-mont-red-ctleg inos-check-linux.json.Verification
testwolfcryptand the wolfmath API group pass on fast math (±WOLFSSL_MONT_RED_CT,ALT_ECC_SIZE), sp_int and heap math;make checkpasses on fast math withWOLFSSL_MONT_RED_CT.-Werror(Ubuntu 24.04) clean; 32-bit digits (i386) pass.Not covered in CI:
fp_montgomery_reduce_mulx()withWOLFSSL_MONT_RED_CT(needs--enable-intelasm). It shares the fixedfp_submod_ct()tail with the generic reduction.