-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add modular exponentiation and modular inverse, closes #45 #65
Conversation
I finally made a rebase as the changes seemed too minor for a commit. I hope this will not break something, as I had to force push into my branch. |
I have added modular inverse for negative exponents in modular exponentiation. I fear that modular inverse is too slow as for now. It might be improved with a faster multiplication. Fixes #45. |
src/bigints.nim
Outdated
if modulus == 1: | ||
return zero | ||
if exponent < 0: | ||
if base == zero: |
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.
should we have base == zero return zero also for positive exponent? (and maybe we also check for exponent == 0 before and return 1 in that case)?
code would then be:
if exponent == zero:
return one
if base == zero:
return zero
if exponent < 0:
let baseInv = invmod(base, modulus)
return powmod(baseInv, -exponent, modulus)
also: not sure if all those ifs would be better with an if..elif..else
(even if they raise or return)
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.
Your propositiion removes a lot of unuseful computation for zero base and large positive exponent indeed.
I have not written it this way as zero does not appear as a specific case of the entire algorithm. The square and multiply algorithm would work with 0 as base too.
It is an edge case for modular inverse though and 0 has strictly speaking no modular inverse. As @konsumlamm has proposed to me to remove an if clause in previous PR, I think it is better to keep only edge cases in the first ifs (it improves readability).
Concerning multiple ifs rather than if .. elif .. else clause, I personnaly prefer to avoid else clause as it adds unuseful indentation for the rest of the algorithm. Maybe the compiler is able to do more compilation's optimization with if .. elif .. else clause. I do not think it improves a lot.
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.
I'm not fully done with reviewing, will finish that later (hopefully today).
I have changed the modular exponentiation so that it uses an if..elif..else construct rather than only ifs. |
Fix PR's history. The 5 different commits might be too much, wich ones should I keep ? |
Don't worry, PRs get squashed anyway, it doesn't really matter how many commits your PR has. |
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.
Now I'm done with reviewing. It took a little longer than expected.
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
I believe there are some cases where invmod returns negative integers depending on the sign of the input. This needs more testing. |
I see no opposition to these changes. Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
Merge with latests changes
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
I see no opposition to these changes. Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
Tried to rebase, then merge with current master branch. History is a mess. Please squash my commits before merging. |
I removed an example that used a custom modular exponentiation similar to my powmod function. I could have replaced with it with just one call to the function, it would not have been interesting. |
@dlesnoff can you rebase this, to resolve the conflicts? |
Fix conflict
merged the changes to resolve the conflicts. |
Add powmod with all the comments made on the previous pull request.