-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Performance improvements for factor #1525
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d9095a2
factor: Refactor (eheh) around a `Factors` datatype
nicoonoclaste a1b2522
factor: Move each factorisation method to its own module
nicoonoclaste bc11e57
factor::factor: Use u64::trailing_zero instead of iterated division
nicoonoclaste 418fd61
factor::factor: Short-circuit the fallback to Pollard's rho
nicoonoclaste 1697406
factor::table: Remove extraneous calls to the primality test
nicoonoclaste e1a6dbe
factor::table: Remove obsolete, commented code
nicoonoclaste 74054fe
factor::factor: Remove extraneous call to the primality test
nicoonoclaste e3ecc81
factor: Move the Miller-Rabin primality test to its own module.
nicoonoclaste 6b9585b
factor::miller_rabbin: Refactor before extracting dividers
nicoonoclaste 8241037
factor::miller_rabin: Extract dividers from the primality test
nicoonoclaste 29eb8fd
format: Make clippy happy
nicoonoclaste 30fd6a0
factor::numeric: Replace lose functions with an Arithmetic trait
nicoonoclaste 543c7b9
factor::rho: Small refactor
nicoonoclaste 36a2948
factor::miller_rabin: Avoid unecessary exponentiation
nicoonoclaste 4c3682a
factor::Factors::add: Split up to work without NLL
nicoonoclaste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use crate::numeric::*; | ||
|
|
||
| // Small set of bases for the Miller-Rabin prime test, valid for all 64b integers; | ||
| // discovered by Jim Sinclair on 2011-04-20, see miller-rabin.appspot.com | ||
| #[allow(clippy::unreadable_literal)] | ||
| const BASIS: [u64; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]; | ||
|
|
||
| #[derive(Eq, PartialEq)] | ||
| pub(crate) enum Result { | ||
| Prime, | ||
| Pseudoprime, | ||
| Composite(u64), | ||
| } | ||
|
|
||
| impl Result { | ||
| pub(crate) fn is_prime(&self) -> bool { | ||
| *self == Result::Prime | ||
| } | ||
| } | ||
|
|
||
| // Deterministic Miller-Rabin primality-checking algorithm, adapted to extract | ||
| // (some) dividers; it will fail to factor strong pseudoprimes. | ||
| #[allow(clippy::many_single_char_names)] | ||
| pub(crate) fn test<A: Arithmetic>(n: u64) -> Result { | ||
| use self::Result::*; | ||
|
|
||
| if n < 2 { | ||
| return Pseudoprime; | ||
| } | ||
| if n % 2 == 0 { | ||
| return if n == 2 { Prime } else { Composite(2) }; | ||
| } | ||
|
|
||
| // n-1 = r 2ⁱ | ||
| let i = (n - 1).trailing_zeros(); | ||
| let r = (n - 1) >> i; | ||
|
|
||
| for a in BASIS.iter() { | ||
| let a = a % n; | ||
| if a == 0 { | ||
| break; | ||
| } | ||
|
|
||
| // x = a^r mod n | ||
| let mut x = A::pow(a, r, n); | ||
|
|
||
| { | ||
| // y = ((x²)²...)² i times = x ^ (2ⁱ) = a ^ (r 2ⁱ) = x ^ (n - 1) | ||
| let mut y = x; | ||
| for _ in 0..i { | ||
| y = A::mul(y, y, n) | ||
| } | ||
| if y != 1 { | ||
| return Pseudoprime; | ||
| }; | ||
| } | ||
|
|
||
| if x == 1 || x == n - 1 { | ||
| break; | ||
| } | ||
|
|
||
| loop { | ||
| let y = A::mul(x, x, n); | ||
| if y == 1 { | ||
| return Composite(gcd(x - 1, n)); | ||
| } | ||
| if y == n - 1 { | ||
| // This basis element is not a witness of `n` being composite. | ||
| // Keep looking. | ||
| break; | ||
| } | ||
| x = y; | ||
| } | ||
| } | ||
|
|
||
| Prime | ||
| } | ||
|
|
||
| // Used by build.rs' tests | ||
| #[allow(dead_code)] | ||
| pub(crate) fn is_prime(n: u64) -> bool { | ||
| if n < 1 << 63 { | ||
| test::<Small>(n) | ||
| } else { | ||
| test::<Big>(n) | ||
| } | ||
| .is_prime() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.