-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add efficient linear combination for Montgomery forms #666
Conversation
Signed-off-by: Andrew Whitehead <[email protected]>
Signed-off-by: Andrew Whitehead <[email protected]>
Signed-off-by: Andrew Whitehead <[email protected]>
Signed-off-by: Andrew Whitehead <[email protected]>
/// 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 |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vartime only!
There was a problem hiding this comment.
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
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
usingConstMontyForm
, wherem
is aU256
with at least one leading zero, is reduced from 50 to 32ns in my tests.