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

Modular inversion improvements #263

Merged
merged 3 commits into from
Sep 2, 2023
Merged

Conversation

fjarri
Copy link
Contributor

@fjarri fjarri commented Aug 23, 2023

  • fix a typo in a proptest for rem() which rendered it useless
  • make inv_mod2k() constant-time both in self and k
  • rename the old inv_mod2k() to inv_mod2k_vartime()
  • make the code in inv_mod2k_vartime() clearer and fix the attribution
  • add a constant-time inv_mod() that supports both even and odd moduli (works via RNS and inv_mod2k()/inv_odd_mod(); another approach is binary GCD (see Constant-time GCD #227 for more info) which may or may not be faster, but is definitely more complicated)

Note that this PR does not introduce any backwards-incompatible changes (except for the performance regression for the users of inv_mod2k()).

Possible additions:

  • We can add inv_mod2k_bounded(), for the cases where a bound on k is known.
  • We can add inv_mod_vartime() (that calls inv_mod2k_vartime() internally, which would lead to a significant performance gain)

(questions involving breaking changes are moved to #268)

@fjarri fjarri changed the title More modular ops Modular inversion improvements Aug 24, 2023
@tarcieri
Copy link
Member

@fjarri needs a rebase

@fjarri
Copy link
Contributor Author

fjarri commented Aug 28, 2023

#267 should go in first, then I'll rebase on top of that.

@fjarri fjarri force-pushed the more-modular branch 2 times, most recently from 1a13201 to af94b47 Compare August 28, 2023 21:03
@fjarri
Copy link
Contributor Author

fjarri commented Aug 28, 2023

Rebased.

@tarcieri tarcieri merged commit e0320a1 into RustCrypto:master Sep 2, 2023
15 checks passed
/// Computes the multiplicative inverse of `self` mod `modulus`.
/// Returns `(inverse, CtChoice::TRUE)` if an inverse exists,
/// otherwise `(undefined, CtChoice::FALSE)`.
pub fn inv_mod(&self, modulus: &Self) -> (Self, CtChoice) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just realized, it could have been made const.

@fjarri fjarri deleted the more-modular branch September 3, 2023 20:57
@tarcieri tarcieri mentioned this pull request Sep 4, 2023
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