Repository navigation
x86-64 RSA CRT: 2^E mod n in one multiplication (up to 4.7% faster private-key operations) - #1357
Merged
Merged
Conversation
…(WIP: src/asm not yet regenerated) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqW9NMssoiBx9i4PpksckL
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.
From the rustls provider analysis in #991: the RSA private-key operation without AVX512_IFMA is slower than aws-lc's. This is the first change for it. It removes most of the CRT exponentiation's setup cost per prime.
What changes
vg_rsa_private_crt(x86-64, every variant) brings the input into each prime's Montgomery form throughG = 2^E mod n, whereE = 64 w_X (K + 1). Until nowgPowcomputedGas2^D RwithD = E - 64 w: it started fromR, then did one squaring atn's width, plus a doubling when the bit was set, for each bit ofD. That is about 11 squarings ofnper prime.Dis a multiple of 64. When2^D < n, one multiplication gives the same number:Y := 2^D(one word set), thenY := R² Y R⁻¹ = 2^D R.2^D < nholds whenn's top word is nonzero andD / 64 < w - 1. That covers every key with primes of halfn's length.nand the lengths (public data), and keeps the old loop as the fallback.Proofs
Impl/Rsa/X86_64/Crt.lean:gPowgains the test, withgFastandgSlow(the old loop, unchanged).Proof/Bignum/X86_64/CrtGPow.lean:gPow_ok's statement is unchanged.gLd_ok,gTest_ok,gCmp_ok, and eithergFast_okorgSlow_ok(the old proof).Proof/Bignum/CrtGPow.lean:gC(D = 64 gC),wv_top,gFast_lt(2^D < n).Proof/Bignum/X86_64/CrtCTGPow.lean:gPow_ct_ofis reproven over the new branches. Each branch condition is pinned to public data:n's top word andD / 64versusw - 1.gFast_ctandgSlow_ctcover the two paths.Proof/Bignum/X86_64/Mont.lean:MmUse, the list of multiplications the Montgomery implementations are proven constant time for, gainsY := R² Y(o = aY, a = aR2, b = aY). Each implementation'sctproof (Mont.base,fnBase,adx,adxSquare,fnAdx) covers it with the sametaint_decideas the others.No change to
Spec/orTCB/.Benchmark
Sapphire Rapids,
VG_CPU_FEATURES=avx,avx2,bmi2,adx,OPENSSL_ia32cap=":~0x200000",ci/bench_compare.py --modules rsa --rounds 3(fastest of 3 interleaved runs per side):rsa_private/256rsa_private/384rsa_private/512rsa_pss_sha256_mgf1_sha256_sign/256rsa_pss_sha256_mgf1_sha256_sign/384rsa_pss_sha256_mgf1_sha256_sign/512rsa_pkcs1_sign/256rsa_pkcs1_decrypt/256Every other RSA benchmark is within noise. The IFMA variant calls the same
gPow(throughvg_rsa_mont_mul_adx), so it gains the same setup time.Checks run locally: the full
lake buildandEmit.lean(onlysrc/asm/x86_64/rsa.rschanges),check_lean_imports,check_lean_speed,check_variants,check_mcdt,check_vectors,check_arch_gates,cargo fmt,clippy -D warnings, and the RSA tests with Wycheproof by default (IFMA on this machine) and withVG_CPU_FEATURES=noneandavx,avx2,bmi2,adx.algorithms_table.pyleaves the README unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_01PqW9NMssoiBx9i4PpksckL
Generated by Claude Code