Repository navigation
x86-64 P-521 ECDH: an affine window table and the mixed addition - #1261
Merged
Merged
Conversation
reaperhulk
added this pull request to stack #1265
October 8, 2026 02:35
alex
force-pushed
the
claude/eloquent-mccarthy-n9d52f-p521affine
branch
from
October 8, 2026 03:29
f2a5d89 to
ec52c8d
Compare
Base automatically changed from
claude/eloquent-mccarthy-n9d52f-p521dblsched
to
main
October 8, 2026 03:36
…mixed addition The window table is built in projective coordinates and normalized by Montgomery's trick (one divsteps inversion over the table of the bits of n - 2, which ECDH does not read); every iteration but the last adds its entry by the mixed Jacobian addition (8 products and 3 squares, against 16 products). The table in Jacobian coordinates and the 16-product addition, now unused, are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019KRjToTNRB2yxFCdNMux4P
alex
force-pushed
the
claude/eloquent-mccarthy-n9d52f-p521affine
branch
from
October 8, 2026 03:36
ec52c8d to
9f5d8b8
Compare
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.
Summary
P-521 ECDH on x86-64 now brings the window table into affine coordinates with one inversion. Every window but the last then adds its entry with the mixed Jacobian addition (
maddJ, madd-2004-hmv: 8 products and 3 squares) instead of the full Jacobian addition (16 products).The table. It is built in projective coordinates by
WinCfg.build, the table the other curves' window method uses, then normalized byWinCfg.normTbl(Montgomery's trick):c_m = Z_1 ⋯ Z_mgo into the addition's temporaries, withc_8inR.z.c_8^(p-2)inE.x.Z_m^(p-2) = c_m^(p-2) c_{m-1}andc_{m-1}^(p-2) = c_m^(p-2) Z_m, and each entry'sXandYare multiplied by itsZ^(p-2).Zis set to one.The inversion. It is the divsteps inversion ECDH already runs for
Z^(p-2)(InvCfg.inv). It is a second instance,Cfg.invWin, writingE.xand with its working area over the table of the bits ofn - 2, which only ECDSA reads.n - 2table ends below them for three words or more.InvSpecW:E.x = R.z^(p-2), and what it writes). ECDH discharges it withInvSoundand the inversion's existingInvOk(invSpecQ).The iterations. Each iteration but the last adds the entry by
maddJintoDand then selects as before (selSum):EwhereRisO;Rwhere the entry isO(digit 0,(0 : 1 : 0));The mixed addition fails only for equal points (it gives
Ofor opposite ones), whichwin_sepalready rules out (InvJ.sumSelM). The last iteration adds the affine entry, already a projective representative, by the complete formulas, sotoProjEis gone.Proofs.
Proof/Weierstrass/WindowJA.lean(target-independent): Fermat's little theorem inFe Cfrom the group law'sx_eq(Law.fermat), the step of Montgomery's trick (trick_step), andRepA(a representative withZ ∈ {0, 1}).Proof/Weierstrass/X86_64/WinJNorm.lean:normTbl_ok, from a table ofRepto a table ofRepA, for entries other thanO. That holds since P-521 has prime order (PrimeOrder). It goes one product at a time (slotMul_ok), the prefix products and back-substitution by invariants (PreInv,BackInv).windowJ_oknow takesInvSpecW, and changesinvClob,winWand the inversion's writes.mulQJ4_ok(Proof/Ecdh/X86_64/MulJ4.lean) takes the widerWinMulPostJand writesmulQJ4W(winXJ:winXand the inversion's working area), withmulQJ4_wfor the exchange.mulQ'sWinMulPost,winXandmulQW, which the other curves and ECDSA verification share, are unchanged.buildJ,RepJ,toProjE) and the 16-product addition (jacAddSfrom x86-64 P-521: order the Jacobian doubling's and addition's field operations #1258,InvJ.sumSel) are no longer used.Constant time. The taint summaries gain one for the normalization (
winNormJSum,winNormJXSum).No change to
TCB/orSpec/.Speed
ci/bench_compare.py --rounds 5 --modules ecdh_p521against #1258's head, on a Cascade Lake:ecdh_p521/66Each of the 130 iterations saves 4 products and a square. The table costs one inversion and 37 products more, and saves the 7 conversions into Jacobian coordinates.
Checks
lake buildandEmit.lean --checkpassed. After it, the P-521 modules build,EmitOne.lean --check P521.X86_64finds the same code, and the full build is running locally.VG_CPU_FEATURES=none.🤖 Generated with Claude Code
https://claude.ai/code/session_019KRjToTNRB2yxFCdNMux4P