Repository navigation
x86-64 RSA CRT: R² mod n by R2Words.choice - #1385
Merged
Merged
Conversation
`vg_rsa_private_crt` computed R² mod n from the top bit of n by doublings and six squarings; it now uses `R2Words.choice`, the same computation the public-key functions use, which is proven and constant time for the multiplications it makes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqW9NMssoiBx9i4PpksckL
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.
This change comes from the rustls provider analysis in #991, which found the RSA private-key operation without AVX512_IFMA slower than aws-lc's. This is the second change for it, after #1357, and removes more of the CRT exponentiation's setup cost.
What changes
vg_rsa_private_crt(x86-64, every variant) needs R² mod n to bring the input inton's Montgomery form. Until nownSetupcomputed it asvg_rsa_publicdoes:n, one bit at a time, fromn's top bit up to2^w R mod n(doubles, aboutwdoublings overwwords each);It now uses
R2Words.choice, whichvg_rsa_public_precomputealready uses for the same number. Whenn's top bit is set andwis a multiple of 4, it reaches2^(16 w) R mod ninw / 4steps of one word each (long division, Knuth's Algorithm D, with a precomputed reciprocal), then makes two Montgomery squarings. Other moduli take the old computation. Every branch depends only onnandw, both public.Proofs
Impl/Rsa/X86_64/Crt.lean:nSetupcallsR2Words.choice mulin place of the doubling-and-squaring steps.Proof/Bignum/X86_64/CrtFront.lean: the R² step goes throughR2w.choice_ok, where it used the localr2_ok.Proof/Bignum/X86_64/CrtCTMain.lean: likewise throughR2w.choice_ct, where it usedr2_ct.No change to
Spec/orTCB/, and no new multiplication shapes:MmUsealready covers every multiplicationR2Words.choicemakes.Benchmark
ci/bench_compare.pywas too noisy on this VM to resolve a change under 1%: unchanged benchmarks moved by ±20%. So the comparison is an interleaved A/B instead:mainand this change;private_opcalls between the two with the same OpenSSL-generated key;Setup: Sapphire Rapids,
VG_CPU_FEATURES=avx,avx2,bmi2,adx(the_adxbackend, IFMA excluded), 2000 pairs.Checks run locally:
lake buildandEmit.lean(onlysrc/asm/x86_64/rsa.rschanges);check_lean_imports,check_lean_speed,check_vectors,check_arch_gates,check_variants,check_mcdt,algorithms_table(unchanged);cargo fmtandclippy -D warnings;VG_CPU_FEATURES=noneandavx,avx2,bmi2,adx.🤖 Generated with Claude Code
https://claude.ai/code/session_01PqW9NMssoiBx9i4PpksckL
Generated by Claude Code