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

Montgomery multiplication improvements #203

Merged
merged 5 commits into from
Mar 29, 2023

Conversation

fjarri
Copy link
Contributor

@fjarri fjarri commented Mar 29, 2023

  • Added benchmarks for Montgomery multiplication and DynResidueParams creation
  • Replaced the final reduction in montgomery_reduction() with sub_mod_with_hi(). Speeds it up by ~20% (for U256), I guess due to better vectorization?
  • Extracted muladdcarry() from montgomery_reduction()
  • Simplified Uint::sub_mod(), sub_mod_special(), add_mod(), add_mod_special() by reusing existing methods.
  • Made DynResidueParams::new() a const fn
  • Optimized mod_neg_inv calculation in DynResidueParams::new(), speeds it up by ~10% (for U256).

Originally I wanted to implement Montgomery multiplication by simultaneous multiplication + reduction instead of mul_wide, but it showed exactly the same performance, with the exception of the last reduction part - that's how I discovered the performance improvement. Still not quite sure what causes it.

(By the way, I measured the performance on arm64 - would be interesting to see the results for x64)

@tarcieri
Copy link
Member

Seems like a nice simplification. Thanks!

@tarcieri tarcieri merged commit 74b481b into RustCrypto:master Mar 29, 2023
@fjarri fjarri deleted the montgomery-mul branch March 29, 2023 23:53
@tarcieri tarcieri mentioned this pull request Apr 26, 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