mpn_mod: add batched AVX2 Montgomery 4x4 matrix power - #2823
Draft
Brian-Heckel wants to merge 1 commit into
Draft
Conversation
Add an AVX2 batched-Montgomery kernel for 4x4 matrices over Z/nZ and use it for matrix powering. Numbers are stored as 31-bit limbs with the four output columns in the four AVX2 lanes; dot products sum their four products into a wide accumulator and reduce with a single Montgomery REDC (lazy reduction), valid for n < 2^246. The kernel is compile-time specialized on the limb count s = ceil((bits+2)/31), so work scales with the modulus size.
Brian-Heckel
marked this pull request as draft
September 2, 2026 04:32
Collaborator
|
Interesting, but this is a lot of ad hoc code for one specific operation at one specific size. Some context is missing here: is there a particularly important application relying critically on 4x4 matrix powering with these bit sizes? This looks like it would be more useful if:
Minor: |
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.
Adds an AVX2 batched-Montgomery kernel for 4×4 matrices over
Z/nZand uses it to accelerate matrix powering (A^exp) calledmpn_mod_mat_mul_batched_mont. On a 4×4 power over an odd modulus it is faster (1.60x) than the current generic path (gr_mat_pow_ui, which uses classical multiplies for 4×4), with the largest gains at smaller moduli. It falls back to the generic path whenever it does not apply. The matrix multiplication shouldn't be used for a generic multiply since conversion to Montgomery form has been profiled to end up taking more time than classic matrix multiplication. Thempn_mod_mat_mul_batched_montis provided as a function but doesn't provide a speedup in itself. Tests and profiles are provided.I'm leaving this as a draft in case details or specifics need to be addressed before merging.
Claude was used in the development and implementation of this PR.