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

Added powm (power modulo) to the std::num module. #11735

Closed
wants to merge 2 commits into from

Conversation

lilac
Copy link
Contributor

@lilac lilac commented Jan 22, 2014

Added a function to compute modular exponentiation.
This function is useful in the field of cryptography.

/// 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 {
Copy link
Member

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.

@brson
Copy link
Contributor

brson commented Jan 23, 2014

How common is this function in standard libraries? It seems fairly specialized and google doesn't tell me much.

@thestinger
Copy link
Contributor

@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.

@thestinger
Copy link
Contributor

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.

@flaper87
Copy link
Contributor

I'm also leaning towards not adding it for now or at least moving it to libextra. I think num needs some work before it can be extended any further. I know @bjz has been working on an idea for this.

@thestinger
Copy link
Contributor

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.

@flaper87
Copy link
Contributor

@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

@lilac
Copy link
Contributor Author

lilac commented Jan 23, 2014

@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.

@huonw
Copy link
Member

huonw commented Jan 23, 2014

For bigints, a + b, a * b, a % b etc allocate a new vector to store the digits of the integer.

@thestinger
Copy link
Contributor

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.

@lilac
Copy link
Contributor Author

lilac commented Jan 23, 2014

@huonw @thestinger I see. Thanks!

@lilac
Copy link
Contributor Author

lilac commented Jan 23, 2014

@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.

@bill-myers
Copy link
Contributor

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.

@huonw
Copy link
Member

huonw commented Jan 23, 2014

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).

@lilac lilac closed this Jan 25, 2014
flip1995 pushed a commit to flip1995/rust that referenced this pull request Nov 2, 2023
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
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.

6 participants