Repository navigation
Regenerate curve25519 asm: sc_reduce and sc_muladd range fixes - #12277
Conversation
|
@claude review |
|
There was a problem hiding this comment.
🟡 Changes recommended
The range fix lacks checked-in regression vectors for boundary and negative-fold cases against the assembly variants, allowing future regeneration to reintroduce failures undetected by existing Ed25519 tests.
1 open issue
What changed in this PR
Regenerates wolfCrypt’s Ed25519 scalar assembly to correct edge-case reduction results, extending the C implementation fix from #12076.
Changes:
- Adds branchless order corrections for negative intermediate values in
sc_reduceandsc_muladd. - Preserves required high bits and corrects carry handling on 32-bit ARM.
- Updates corresponding standalone and inline assembly variants across x86-64, ARM64, ARM32, and Thumb2.
| File | Description |
|---|---|
wolfcrypt/src/port/arm/thumb2-curve25519.S |
Fixes Thumb2 scalar reduction in both architecture variants. |
wolfcrypt/src/port/arm/thumb2-curve25519_c.c |
Applies matching fixes to Thumb2 inline assembly. |
wolfcrypt/src/port/arm/armv8-curve25519.S |
Corrects ARM64 negative folds and final masks. |
wolfcrypt/src/port/arm/armv8-curve25519.asm |
Applies matching ARM64 assembler fixes. |
wolfcrypt/src/port/arm/armv8-curve25519_c.c |
Updates ARM64 inline assembly. |
wolfcrypt/src/port/arm/armv8-32-curve25519.S |
Fixes ARM32 folds, carries, and masks across architecture variants. |
wolfcrypt/src/port/arm/armv8-32-curve25519_c.c |
Applies matching fixes to ARM32 inline assembly. |
wolfcrypt/src/fe_x25519_asm.S |
Corrects x64 and AVX2 scalar reduction paths. |
wolfcrypt/src/fe_x25519_asm.asm |
Applies matching x64 and AVX2 fixes for MASM. |
Review effort: Balanced
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. It regenerates Ed25519 scalar-reduction assembly on four architectures, about 1.2k lines, so a human should still look it over.
What was reviewed:
- The new "add order if negative" step in sc_reduce/sc_muladd on x64, AVX2 and AArch64. It builds a sign mask with sar/asr and adds the masked limbs of L with no branches. It only uses scratch registers that the next "Sub product" step loads again.
- The wider top-limb masks: x86 0x1fff..., and AArch64 BIC count 14 instead of 15. The ARM32/Thumb2 BFC ranges and the ADC carry change were checked against the description, not limb by limb.
- I did not assemble or run the PR's vector harness, and I did not check the ARM64 .asm, which the PR says was never assembled.
Extended reasoning...
The change regenerates sc_reduce and sc_muladd (Ed25519 scalar reduction mod l) in the x86_64, AArch64, ARM32 and Thumb2 assembly, including the inline-asm _c.c twins: 9 files, about 1.2k lines. This is security-sensitive crypto code, and the correction has to stay constant-time. The x64 and AArch64 corrections look correct and branch-free. Arithmetic correctness across every carry chain can't be settled by reading alone. That, plus the size and the crypto surface, is why I deferred instead of approving.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12277
Scan targets checked: wolfssl-pr-general
Review type: general (no scan target covers the changed files)
Coverage: 8 of 9 in-scope changed file(s) opened by the reviewer; not opened: wolfcrypt/src/port/arm/armv8-curve25519_c.c
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
- fe_x25519_asm.S/.asm, armv8-curve25519.S/.asm/_c.c, armv8-32-curve25519.S/_c.c and thumb2-curve25519.S/_c.c are regenerated from wolfSSL/scripts.
f075940 to
63c0c5b
Compare

Problem
The asm
sc_reduce/sc_muladd(Ed25519 scalar reduction modl) return a wrong scalar for some edge inputs on every port. The C version was fixed in #12076.Fix (
wolfcrypt/src/fe_x25519_asm.S)Regenerated from wolfSSL/scripts#729; only
sc_reduce*/sc_muladd*change.fe_x25519_asm.S,.asmsc_reduce_x64,sc_muladd_x64,sc_reduce_avx2,sc_muladd_avx2port/arm/armv8-curve25519.S,.asm,_c.csc_reduce,sc_muladdport/arm/armv8-32-curve25519.S,_c.csc_reduce,sc_muladd(all ARCH variants)port/arm/thumb2-curve25519.S,_c.csc_reduce,sc_muladd(default and ARCH_7M)Verification
x mod l(902k vectors, 11 port builds): 0 wrong. Before: 56,549 / 57,975 wrongsc_reduceresults (x86_64 / 32-bit ARM).testwolfcryptpasses on macOS armasm, x86_64 intelasm (x64 and AVX2 paths), ARM32 and Thumb2 (.Sand inline) under qemu.WolfSSLIntelAsm=true: testsuite passes, and the harness on the MASM objects gives 0 wrong. The ARM64.asmwas not assembled.