Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add efficient linear combination for Montgomery forms #666

Merged
merged 4 commits into from
Sep 19, 2024

Conversation

andrewwhitehead
Copy link
Contributor

This PR implements Algorithm 2 (for B=1) from Efficient Algorithms for Large Prime Characteristic Fields and Their Application to Bilinear Pairings by Patrick Longa: https://eprint.iacr.org/2022/367

This algorithm interleaves schoolbook multiplication and accumulation of multiple terms with the Montgomery reduction. For moduli with one or more leading zeros, this helps to reduce the number of reductions performed. In the BoxedUint case this also reduces the number of allocations required. For larger moduli it may be useful to explore the use of larger values of B along with the existing Karatsuba multiplication.

As a concrete example, the calculation of (a•b + c•d) mod m using ConstMontyForm, where m is a U256 with at least one leading zero, is reduced from 50 to 32ns in my tests.

/// This is implemented as a macro to abstract over `const fn` and boxed use cases, since the latter
/// needs mutable references and thus the unstable `const_mut_refs` feature (rust-lang/rust#57349).
///
// TODO: change this into a `const fn` when `const_mut_refs` is stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon! #667

I guess we can add one more macro in the meantime.

@@ -30,6 +31,8 @@ pub struct MontyParams<const LIMBS: usize> {
/// The lowest limbs of -(MODULUS^-1) mod R
/// We only need the LSB because during reduction this value is multiplied modulo 2**Limb::BITS.
mod_neg_inv: Limb,
/// Leading zeros in the modulus, used to choose optimized algorithms
mod_leading_zeros: u32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I get the use cases, but this does seem like a potential sharp edge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vartime only!

Copy link
Member

@tarcieri tarcieri Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm just worried someone down the road might use it in constant-time use cases

@tarcieri tarcieri merged commit 21cba95 into RustCrypto:master Sep 19, 2024
18 checks passed
@andrewwhitehead andrewwhitehead deleted the feat/monty-lincomb branch September 19, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants