Repository navigation
x86-64 X25519: s2n-bignum's ladder iteration, choosing the point to double with mux - #2666
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ma2fA9rUTPXEEQjYyCMCRw
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ma2fA9rUTPXEEQjYyCMCRw
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ma2fA9rUTPXEEQjYyCMCRw
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ma2fA9rUTPXEEQjYyCMCRw
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.
Part of #991.
On x86-64, the Montgomery ladder shared by
vg_x25519andvg_x25519_adxnow runs the iteration of s2n-bignum'scurve25519_x25519: no swap of the points in each iteration, andmux(cmove) choosing the point to double. Before, each iteration ran twocswaps of four words. It uses the same field operations as before (F.mul,F.sqr,F.a24add,addCmov,subCmov), in s2n-bignum's order and slot sharing, as the AArch64_altladder does (#2591).vg_x25519_ifmaand the fixed-base functions are unchanged.Impl/X25519/X86_64.lean:mux o x y:[o] = [y]if ZF is set, else[x]. It loads both,cmoves word by word and stores, and changes no flag.bitCmp:k_tfromBITS,cmpwithswap(ZF if equal), stored as the newswap.step: the sums and differences of both points,dmsn, the bit, the twomuxes, then the addition and the doubling interleaved.z_2's factor iss² + a24·p, RFC 7748'sAA + a24·E.lastSwapis unchanged.Proof/X25519/X86_64/Small.lean,Env.lean:mux_okandmuxEB, andstore4Z_ok, astore4that also keeps the flags.Proof/X25519/X86_64/Iter.lean:bitCmp_ok,ops1_ok,ops2_ok.stepEnv_eval: the slots after an iteration areladderStep's, by cases onswapandk_t. It holds whichever point is doubled, since the addition is symmetric.step_ok: the loop invariantLInvis unchanged, since slots 3–6 keep RFC 7748's(x_2, z_2, x_3, z_3).z_2, z_3 ≤ 2pstill carries the single folds of the sums and differences.src/asm/x86_64/x25519.rsregenerated.Constant time: the choice is
cmoveon ZF, with no branch on the bit; the artifacts'taint_decidecovers it. Not trust changes: nothing underTCB/orSpec/. Contracts and registrations unchanged.Benchmarks
Run 39066148374: base
main(fcdc37c), one run of each side, x86_64, modulex25519._adxavx,avx2,bmi1,bmi2,adxon Zen 4 (EPYC 9V74)noneon Zen 5 (EPYC 9V45)x25519/32x25519_raw/32x25519_generate/32x25519_public_key/32The fixed-base rows (
x25519_generate,x25519_public_key) runvg_x25519_base*, whose code is unchanged. On Zen 3, aws-lc-rs'sx25519was 27.99 µs in run 39060668498, so the ADX ladder now leads it.On this session's Intel Xeon (Ice Lake class), with the ADX path forced, an unproven prototype of the same loop was −1.9% (best of five) and −3.5% (median). llvm-mca on the loop:
skylake/icelake-server265 → 254 cycles an iteration.Checks
lake buildpasses, andEmit.leanregeneratessrc/asmto exactly this PR's file.check_lean_imports,check_lean_modules,check_registrations,check_lean_speed,check_vectors,check_arch_gates,check_variantsandcheck_mcdtpass, as doescargo fmt --check.VG_CPU_FEATURESunset (IFMA),avx,avx2,bmi1,bmi2,adxandnone.Generated by Claude Code