-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Added powm (power modulo) to the std::num module. #11735
Conversation
/// assert_eq!(num::powm(2, 4, 3), 1); | ||
/// ``` | ||
#[inline] | ||
pub fn powm<T: One + Mul<T, T> + Rem<T, T>>(a: T, mut p: uint, d: T) -> T { |
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.
It would be good to have an explicit check for d == 0
, to make the error messages nicer.
How common is this function in standard libraries? It seems fairly specialized and google doesn't tell me much. |
@brson: https://en.wikipedia.org/wiki/Modular_exponentiation#In_programming_languages I'm leaning against including it because the primary use case is with big integers, and the implementation here will be very slow as it's allocating a lot in the loop. GMP functions (and libraries implementing a similar API) take an output parameter and one or two inputs allowed to alias the output in order to cut down on allocations. Third party big integer libraries will also provide their own efficient implementation of this function. |
IMO, if this is included it needs to be a default implementation on a trait for a library to override. However, I don't think numeric traits belong in the standard library before exploring them outside of it and assembling a great API. |
I'm also leaning towards not adding it for now or at least moving it to |
As libextra isn't going to stay around, I don't think we should put anything else there. @bjz has a numerics library where this stuff can be incubated until it matures. |
@thestinger oh, didn't know about libextra. Then yeah, I agree. I don't think this belongs here for now. @lilac this is the numeric library we're talking about https://github.com/bjz/num-rs |
@thestinger What do you mean by "the implementation here will be very slow as it's allocating a lot in the loop"? There is no allocation in the loop. |
For bigints, |
With the GMP C API and similar big integer APIs, you're able to hoist every temporary in the computation outside of the loop and avoid allocating memory except when the bit size of the temporary needs to be expanded. Big integers share a lot in common with vectors, as that's usually what they are under the hood. |
@huonw @thestinger I see. Thanks! |
@thestinger The intension of this pull request is to add modular exponentiation function for integers (i32 or i64). When I wrote it, I didn't consider big integer. I just want a simple function that is like pow, but with a modulo. If you think it's not worth to be included, I'll close it. But I hope we can find out somewhere it belongs. |
Having a "modular exponentiation" function seems a bad idea. Instead, there should be a "modular integer" type, and a generic pow() function (already merged from pull #11503). Obviously, applying pow() to modular integers results in modular exponentiation, just like applying addition and multiplication will result in modular addition and modular multiplication. In general, pow() works for any base in a https://en.wikipedia.org/wiki/Ring_mathematics and any integer exponent, so it makes no sense to duplicate it for every possible ring. For the record, other rings where pow is useful are integer-valued matrices, for instance, to quickly the compute the nth value of a recursively defined sequence such as the Fibonacci numbers. |
Modular bigints and a generic pow in style of #11503 suffer the same allocation problem as this implementation (it may even be worse, if every number owns their modulus, so that needs to be cloned too). |
ignore lower-camel-case words in `doc_markdown` This fixes rust-lang#11568 by ignoring camelCase words starting with a lower case letter. r? `@blyxyas` --- changelog: none
Added a function to compute modular exponentiation.
This function is useful in the field of cryptography.