Repository navigation
PPC64LE Montgomery arithmetic of up to four words: the modulus as immediates, add and sub in registers - #4783
Merged
Merged
Conversation
…mmediates, add and sub in registers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH4GefHP3pmPRSL4N4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH4GefHP3pmPRSL4N4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH4GefHP3pmPRSL4N4
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 #4141 (PPC64LE audit, finding 1). This follows #4774.
Before this change, every call of a curve's field function stored the modulus into its working space with
setConst, which takes 24 instructions for four words. Add and sub then ran the memory chains ofImpl/Mont/PPC64LE.lean. For moduli of at most four words (P-192, P-224, P-256, secp256k1, brainpoolP256r1, and theirn), nothing touches the modulus in memory any more.Impl/Mont/PPC64LE.lean:movImm64loads a 64-bit constant into a register, in one to five instructions.rmul4'sqloadputs the words ofmintomRegas immediates (mword m j) instead of loading them.add4andsub4keep the sum or difference inr7–r10.add4subtractsmwith immediates. When that borrows, the mask(carry + CA) - 1addsmback.sub4addsmback under the borrow's mask (subfe r11, r11, r11).Impl/Weierstrass/PPC64LE/Mont.lean:addFnandsubFnchooseadd4/sub4inside their block, asrmulSeldoes, so the code's shape andnoFramesdo not change.Proof/Mont/PPC64LE/Ops.lean:movImm64_ok,add4_okandsub4_ok.rmul4_oknow takesRegPre, which isOpPrewithout the modulus in memory, andm < 2^(64n).Proof/Weierstrass/PPC64LE/MontFn.lean:entry_okgives the modulus in memory only forn > 4.mulFn_ok,addFn_okandsubFn_okare unchanged.r14andr25–r31. The docs ofCall.leanandmulCall_oknow say that the multiplication saves and restores registers withinr15–r24(r15–r(15 + 2n)for at most four words).P-256's
addgoes from 95 to 65 instructions,subfrom 89 to 45, andmulfrom 146 to 131 (static counts).No TCB or Spec change.
Testing
lake build,Emit.leanandEmit.lean --checkpass. The crate changes are the ≤4-word moduli'sadd/sub/mulfunctions, their assembly, andecdh_p256.rs.check_lean_public_imports.pypass.Benchmark
POWER9, run 42538012356, scoped to
arches=powerpc64, base7cfde1c4:ec_p256_public_key/65ecdh_brainpoolp256r1/32ecdh_p192/24ecdh_p224/28ecdh_p256/32ecdh_secp256k1/32ecdsa_p256_sha256_sign/64ecdsa_p256_sha256_sign/1024ecdsa_p256_sha256_sign/16384ecdsa_p256_sha256_verify/64ecdsa_p256_sha256_verify/1024ecdsa_p256_sha256_verify/16384ecdsa_p256_sha384_sign/64ecdsa_p256_sha384_sign/1024ecdsa_p256_sha384_sign/16384ecdsa_p256_sha384_verify/64ecdsa_p256_sha384_verify/1024ecdsa_p256_sha384_verify/16384Generated by Claude Code