Repository navigation
modexp: one-digit Montgomery products and 32-bit quotient digits - #12
Merged
Merged
Conversation
…otient digits without 64-bit division
Member
Author
|
Verified the MODEXP changes in 🤖 AI agent (Codex / GPT-6) on behalf of @LukaszRozmej |
main got a one-digit Montgomery product of its own, mont_mul_1 (12 delegations). This branch has the shared mont_mul for that case (8), so the merge keeps modexp.c of this branch.
Collaborator
|
#11 is in main now and it had a one-digit product of its own ( |
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.
Changes
reduce()(Knuth D3) estimates each quotient digit with two 32-bit divisions (Hacker's Delightdivlu) in place of a 64-bit division. On RV32IM the 64-bit division is a call to__udivdi3, about 680 instructions. The digit is now auint32_t, so the products in D3 and D4 are 32 x 32 bit.hi(a b) - hi(q N)withq = lo(a b) N^-1 mod R: eight delegations in place of about 21 in the general CIOS loop.modexp_montkeepsN^-1for it.MODEXP with a 256-bit odd modulus, for example a field inversion
x^(r-2) mod rin a SNARK verifier, goes from about 88,000 to about 24,000 cycles for each call.Measured on the Nethermind trusted-input Airbender guest (master
6b332240f76), Airbender CLI cycles, 27 mainnet blocks (26279462 to 26279540, every third block):No block is slower. The other 23 blocks change by 0 to -0.57%.
Testing
build/ci.sh: all 1554 vectors pass. They include odd moduli of 1 to 32 bytes (the one-digit path) and random even moduli (thereduce()path).