Skip to content

x86-64: verified vg_rsa_private_precompute (base and ADX) - #4931

Closed
alex wants to merge 6 commits into
mainfrom
claude/x86_64-rsa-private-precompute
Closed

alex wants to merge 6 commits into
mainfrom
claude/x86_64-rsa-private-precompute

Conversation

@alex

@alex alex commented Nov 10, 2026

Copy link
Copy Markdown
Member

Part of #4141. This adds the x86-64 implementation of vg_rsa_private_precompute, verified against the spec's privatePrecomputeContract. It is an implementation only: no spec change, no TCB change.

The code (Impl/Rsa/X86_64/PrivPre.lean)

It is one function, and most of it is vg_rsa_private_crt's own code (Crt.lean):

  1. entry saves the callee-saved registers and the arguments in the working space's header. pre goes in three slots: the output, the input and qInv. p and q also go in the exponents' slots, which nothing reads.
  2. zeroPre zeroes pre's 8 pre_len bytes (the loop is the same as fail's). The input and qInv the CRT code reads are therefore 0.
  3. The modulus check (invalid) branches on n, which is public.
    • Invalid modulus: fail runs and returns 0. pre is all zeros.
    • Valid modulus: Crt.nSetup, Crt.primesSetup and Crt.checks run. They give n, R² mod n, the primes' workspaces and the mask. The mask is the mask of p q = n, because 0 < n and 0 < p follow from it. Where the mask is clear, p and q are replaced by 3.
  4. r2At: in each prime's workspace, aY := 1, then doubles runs 128 w_X times. That gives 2^(128 w_X) mod X'. The count depends only on public lengths.
  5. out copies n, R² mod n, R_p² mod p and R_q² mod q to pre. Every word is and-ed with the mask, and the mask's low bit is returned.

The function is registered with CallMont.base and with CallMont.adx, as vg_rsa_public_precompute is. So there is a _adx variant, which the dispatcher chooses with ADX.

Proofs (Proof/Bignum/X86_64/PrivPre/)

  • Contract: ppContract, the contract on the registers, and pp_implies, which shows it implies the shared contract with 8 bytes of stack.
  • Head: entry and zeroPre. From there, ppPre_of builds vg_rsa_private_crt's CrtPre: the input is pre's zeros, qInv is zeros, and the exponents are p and q. This lets the CRT's front_ok be reused unchanged.
  • R2: r2X_ok, which applies doubles_ok in a prime's workspace, then r2P_ok and r2Q_ok.
  • Out: copyMasked_ok, outArr_ok, outAt_ok and out_ok.
  • Correct: code_correct.
    • When n is invalid, pre is zeros.
    • Otherwise, pre's words are those of privatePrecompute, or zeros if p q ≠ n (wordsAt_out, main_post).
  • Constant time:
    • CTR2 has the double/doubles relational lemmas for a workspace whose -m⁻¹ is secret (GoodW), then r2X_ct and r2At_ct.
    • CTOut has outArr_ct and out_ct.
    • CTMain reuses the CRT's stages (nSetup_ct, setupS_ct, checksS_ct), extended with pre's facts (SX).
    • CTCode covers entry, zero, the check, and fail/main (code_constantTime).
  • Verified: pp_call_verified (via Verified.of_inline_sig), plus pp_patch and pp_postOut. pp_postOut means the registration goes through X86_64.Scrub.withScrubAll: the function returns without secret residue, and nothing is added to ci/residue_allowlist.txt.

No Rust caller yet

The Rust code is unchanged. PrivateKey::precompute still calls vg_rsa_private_precompute only on PPC64LE. The planned single Rust switch PR will make every architecture compute pre, and its benchmark run (from_crt/from_primes) belongs there. This PR therefore has no benchmark. The dispatcher has no caller, so it does not count toward coverage.

Checks

  • Static checks: every step of the Static checks job passes. check_lean_single_importer.py was run with --base origin/main.
  • Lean build and emit:
    • The full lake build passes, as do Emit.lean and Emit.lean --check.
    • The only crate changes are the x86-64 rsa files: vg_rsa_private_precompute, vg_rsa_private_precompute_adx and their dispatcher.
    • check_lean_public_imports.py passes on the new modules.
  • Rust: cargo fmt --check passes, as does cargo clippy --all-targets -- -D warnings, as do the RSA tests (Wycheproof included).
  • Native runs of the new function (temporarily called from PrivateKey::precompute on x86-64; that change is not in this PR):
    • Generated 1024-, 2048- and 3072-bit keys.
    • Both the ADX variant and the baseline (VG_CPU_FEATURES=none) returned 1.
    • pre matched n, 2^(128w) mod n, 2^(128 w_p) mod p and 2^(128 w_q) mod q, computed in Python.

Generated by Claude Code

alex commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Split by entrypoint at alex's request into #4943 (stacked on #4940).


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants