Skip to content

Regenerate curve25519 asm: sc_reduce and sc_muladd range fixes - #12277

Merged
SparkiDev merged 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/x25519-gen
Nov 26, 2026
Merged

SparkiDev merged 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/x25519-gen

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor

Problem

The asm sc_reduce / sc_muladd (Ed25519 scalar reduction mod l) 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.

File Functions
fe_x25519_asm.S, .asm sc_reduce_x64, sc_muladd_x64, sc_reduce_avx2, sc_muladd_avx2
port/arm/armv8-curve25519.S, .asm, _c.c sc_reduce, sc_muladd
port/arm/armv8-32-curve25519.S, _c.c sc_reduce, sc_muladd (all ARCH variants)
port/arm/thumb2-curve25519.S, _c.c sc_reduce, sc_muladd (default and ARCH_7M)

Verification

  • Harness against exact x mod l (902k vectors, 11 port builds): 0 wrong. Before: 56,549 / 57,975 wrong sc_reduce results (x86_64 / 32-bit ARM).
  • testwolfcrypt passes on macOS armasm, x86_64 intelasm (x64 and AVX2 paths), ARM32 and Thumb2 (.S and inline) under qemu.
  • Windows MSVC x64 with WolfSSLIntelAsm=true: testsuite passes, and the harness on the MASM objects gives 0 wrong. The ARM64 .asm was not assembled.
  • Ed25519 sign/verify within noise on arm64.

@yosuke-wolfssl yosuke-wolfssl self-assigned this Nov 26, 2026
Copilot AI balanced review requested due to automatic review settings November 26, 2026 01:05
@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,864 B (+4.9%, 296,444 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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_reduce and sc_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

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

SparkiDev
SparkiDev previously approved these changes Nov 26, 2026
- 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.
@SparkiDev
SparkiDev merged commit bd431d2 into wolfSSL:master Nov 26, 2026
449 checks passed
@yosuke-wolfssl
yosuke-wolfssl deleted the fix/x25519-gen branch November 26, 2026 23:39
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