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

uint: Implement modulo operations for special moduli #108

Merged
merged 1 commit into from
Aug 15, 2022

Conversation

haslersn
Copy link
Contributor

@haslersn haslersn commented Aug 7, 2022

For a project of mine where the modulus can be chosen to be close to UInt::MAX, I created optimized implementations of modular operations. I thought maybe others can benefit from them, too, so I ported my implementation to the crypto-bigint crate.

This commit implements modulo operations (neg, add, sub, mul) for special moduli that are so close to MAX that the difference to overflow fits in a single Limb. For such moduli, these new implementations are much faster than the existing generic modulus implementations. (For mul there's no comparison since there's no corresponding generic modulus implementation, yet.)

For U256, I benchmarked the generic against the specialized implementations using criterion-rs on Intel Core i7-8565U @ 1.80GHz and obtained the following average times. Note that I used a const modulus known at compile-time, which enables some compiler optimizations after inlining. With a modulus known only at runtime, times might differ.

U256::add_mod U256::sub_mod U256::mul_mod
generic (after #109 got merged) 10.857 ns 9.6262 ns not implemented
_special 3.8276 ns 4.1339 ns 20.188 ns

@haslersn haslersn force-pushed the special-moduli-ops branch 3 times, most recently from ce2ea4a to 0dee5d2 Compare August 7, 2022 12:11
Implement modulo operations (`neg`, `add`, `sub`, `mul`) for
special moduli that are so close to `MAX` that the difference
to overflow fits in a single `Limb`. For such moduli, these
new implementations are much faster than the existing generic
modulus implementations. (For `mul` there's no comparison since
there's no corresponding generic modulus implementation, yet.)
@tarcieri
Copy link
Member

tarcieri commented Aug 8, 2022

This looks reasonable enough at first glance. Give me a little more time to more thoroughly review it.

@haslersn
Copy link
Contributor Author

haslersn commented Aug 8, 2022

I had an error in my benchmarks, namely I forgot to pass the inputs through black_box. I corrected the original post with updated benchmark results.

@tarcieri tarcieri merged commit f28cef1 into RustCrypto:master Aug 15, 2022
@tarcieri
Copy link
Member

Thank you!

@haslersn haslersn deleted the special-moduli-ops branch August 15, 2022 14:36
@tarcieri tarcieri mentioned this pull request Oct 11, 2022
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