-
-
Notifications
You must be signed in to change notification settings - Fork 35
large int factorization #173
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
Draft
s-celles
wants to merge
9
commits into
JuliaMath:main
Choose a base branch
from
s-celles:002-large-int-factorization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ba628d
feat: implement ECM and MPQS polyalgorithm for large integer factoriz…
s-celles a70428b
cleanup: remove useless optim unsafe_store! / pointer...
s-celles cf41020
cleanup: use IntegerMathUtils.ispower
s-celles dd73d80
cleanup: using pollardfactor instead of _pollard_rho_small
s-celles 33d809a
refactor(polyalgorithm): use bit-length instead of decimal digits for…
s-celles f0edbae
cleanup: remove useless optim unsafe_store! / pointer... (2)
s-celles e3cf32d
cleanup: move _find_factor
s-celles 7626ae5
cleanup: move _find_factor for Julia 1.6
s-celles f86b0b6
test: improve coverage
s-celles 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,54 @@ | ||
| * | ||
|
|
||
| ### Allowed files and directories ### | ||
|
|
||
| !.gitignore | ||
| !.github/ | ||
| !.github/workflows/ | ||
| !.github/workflows/* | ||
| !.github/dependabot.yml | ||
|
|
||
| !LICENSE.md | ||
| !README.md | ||
| !Project.toml | ||
|
|
||
| !src/ | ||
| !src/*.jl | ||
| !test/ | ||
| !test/*.jl | ||
| !docs/ | ||
| !docs/src/ | ||
| !docs/src/*.md | ||
| !docs/make.jl | ||
| !docs/Project.toml | ||
|
|
||
| ### Denied even if allowed above ### | ||
|
|
||
| # Files generated by invoking Julia with --code-coverage | ||
| *.jl.cov | ||
| *.jl.*.cov | ||
|
|
||
| # Files generated by invoking Julia with --track-allocation | ||
| *.jl.mem | ||
| Manifest.toml | ||
| docs/build | ||
| docs/site | ||
| docs/Manifest.toml | ||
|
|
||
| # System-specific files and directories generated by the BinaryProvider and BinDeps packages | ||
| # They contain absolute paths specific to the host computer, and so should not be committed | ||
| deps/deps.jl | ||
| deps/build.log | ||
| deps/downloads/ | ||
| deps/usr/ | ||
| deps/src/ | ||
|
|
||
| # Build artifacts for creating documentation generated by the Documenter package | ||
| docs/build/ | ||
| docs/site/ | ||
|
|
||
| # File generated by Pkg, the package manager, based on a corresponding Project.toml | ||
| # It records a fixed state of all packages used by the project. As such, it should not be | ||
| # committed for packages, but should be committed for applications that require a static | ||
| # environment. | ||
| Manifest*.toml | ||
|
|
||
| # File generated by the Preferences package to store local preferences | ||
| LocalPreferences.toml | ||
| JuliaLocalPreferences.toml |
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,233 @@ | ||
| # Elliptic Curve Method (ECM) for integer factorization | ||
| # Ref: Lenstra (1987) "Factoring integers with elliptic curves" | ||
| # Ref: Montgomery (1987) "Speeding the Pollard and Elliptic Curve Methods of Factorization" | ||
|
|
||
| """ | ||
| Point on a Montgomery curve in projective coordinates (X:Z). | ||
| The point at infinity is represented by Z == 0. | ||
| """ | ||
| struct MontgomeryCurvePoint | ||
|
Comment on lines
+8
to
+9
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that this is GMP only is somewhat unfortunate. Ideally this code would work for BitIntegers.jl also... I'm willing to accept it though since BigInt is probably what most users are using in practice. |
||
| X::BigInt | ||
| Z::BigInt | ||
| end | ||
|
|
||
| """ | ||
| In-place modular reduction: sets r = n mod d (non-negative remainder). | ||
| """ | ||
| function _mpz_fdiv_r!(r::BigInt, n::BigInt, d::BigInt) | ||
| ccall((:__gmpz_fdiv_r, :libgmp), Cvoid, (Ref{BigInt}, Ref{BigInt}, Ref{BigInt}), r, n, d) | ||
| end | ||
|
|
||
| """ | ||
| Preallocated scratch space for ECM point arithmetic. | ||
| Avoids BigInt allocation in the hot Montgomery ladder loop. | ||
| t1-t6: scratch for add!/double!; R0/R1/tmp: scratch for scalar_mul! | ||
| """ | ||
| struct ECMBuffers | ||
| t1::BigInt | ||
| t2::BigInt | ||
| t3::BigInt | ||
| t4::BigInt | ||
| t5::BigInt | ||
| t6::BigInt | ||
| R0_X::BigInt | ||
| R0_Z::BigInt | ||
| R1_X::BigInt | ||
| R1_Z::BigInt | ||
| tmp_X::BigInt | ||
| tmp_Z::BigInt | ||
| end | ||
|
|
||
| ECMBuffers() = ECMBuffers(BigInt(), BigInt(), BigInt(), BigInt(), BigInt(), BigInt(), | ||
| BigInt(), BigInt(), BigInt(), BigInt(), BigInt(), BigInt()) | ||
|
|
||
| """ | ||
| In-place: mulmod!(dst, a, b, n, tmp) sets dst = (a * b) mod n using tmp as scratch. | ||
| """ | ||
| @inline function _mulmod!(dst::BigInt, a::BigInt, b::BigInt, n::BigInt, tmp::BigInt) | ||
| Base.GMP.MPZ.mul!(tmp, a, b) | ||
| _mpz_fdiv_r!(dst, tmp, n) | ||
| end | ||
|
|
||
| """ | ||
| Differential addition on Montgomery curve: given P, Q and P-Q, compute P+Q. | ||
| Uses projective coordinates and in-place arithmetic to avoid allocations. | ||
| """ | ||
| function _ecm_add!(res_X::BigInt, res_Z::BigInt, | ||
| P_X::BigInt, P_Z::BigInt, Q_X::BigInt, Q_Z::BigInt, | ||
| diff_X::BigInt, diff_Z::BigInt, n::BigInt, buf::ECMBuffers) | ||
| t1, t2, t3, t4, t5, t6 = buf.t1, buf.t2, buf.t3, buf.t4, buf.t5, buf.t6 | ||
| # u = (P.X - P.Z) * (Q.X + Q.Z) mod n | ||
| Base.GMP.MPZ.sub!(t1, P_X, P_Z) # t1 = P.X - P.Z | ||
| Base.GMP.MPZ.add!(t2, Q_X, Q_Z) # t2 = Q.X + Q.Z | ||
| _mulmod!(t5, t1, t2, n, t3) # t5 = u | ||
|
|
||
| # v = (P.X + P.Z) * (Q.X - Q.Z) mod n | ||
| Base.GMP.MPZ.add!(t1, P_X, P_Z) # t1 = P.X + P.Z | ||
| Base.GMP.MPZ.sub!(t2, Q_X, Q_Z) # t2 = Q.X - Q.Z | ||
| _mulmod!(t6, t1, t2, n, t3) # t6 = v | ||
|
|
||
| # add = u + v, sub = u - v | ||
| Base.GMP.MPZ.add!(t1, t5, t6) # t1 = add = u + v | ||
| Base.GMP.MPZ.sub!(t2, t5, t6) # t2 = sub = u - v | ||
|
|
||
| # X = diff.Z * add^2 mod n | ||
| _mulmod!(t3, t1, t1, n, t4) # t3 = add^2 mod n | ||
| _mulmod!(res_X, diff_Z, t3, n, t4) # res_X = diff.Z * add^2 mod n | ||
|
|
||
| # Z = diff.X * sub^2 mod n | ||
| _mulmod!(t3, t2, t2, n, t4) # t3 = sub^2 mod n | ||
| _mulmod!(res_Z, diff_X, t3, n, t4) # res_Z = diff.X * sub^2 mod n | ||
| end | ||
|
|
||
| """ | ||
| In-place point doubling on Montgomery curve with parameter a24 = (a+2)/4. | ||
| """ | ||
| function _ecm_double!(res_X::BigInt, res_Z::BigInt, | ||
| P_X::BigInt, P_Z::BigInt, | ||
| n::BigInt, a24::BigInt, buf::ECMBuffers) | ||
| t1, t2, t3, t4, t5, t6 = buf.t1, buf.t2, buf.t3, buf.t4, buf.t5, buf.t6 | ||
| # u = (P.X + P.Z)^2 mod n | ||
| Base.GMP.MPZ.add!(t1, P_X, P_Z) # t1 = P.X + P.Z | ||
| _mulmod!(t5, t1, t1, n, t3) # t5 = u = (P.X+P.Z)^2 mod n | ||
|
|
||
| # v = (P.X - P.Z)^2 mod n | ||
| Base.GMP.MPZ.sub!(t1, P_X, P_Z) # t1 = P.X - P.Z | ||
| _mulmod!(t6, t1, t1, n, t3) # t6 = v = (P.X-P.Z)^2 mod n | ||
|
|
||
| # diff = u - v | ||
| Base.GMP.MPZ.sub!(t1, t5, t6) # t1 = diff = u - v | ||
|
|
||
| # X = u * v mod n | ||
| _mulmod!(res_X, t5, t6, n, t3) # res_X = u * v mod n | ||
|
|
||
| # Z = diff * (v + a24 * diff) mod n | ||
| _mulmod!(t2, a24, t1, n, t3) # t2 = a24 * diff mod n | ||
| Base.GMP.MPZ.add!(t2, t6) # t2 = v + a24 * diff | ||
| _mulmod!(res_Z, t1, t2, n, t3) # res_Z = diff * (v + a24*diff) mod n | ||
| end | ||
|
|
||
| """ | ||
| Montgomery ladder scalar multiplication: compute [k]P on Montgomery curve. | ||
| Uses preallocated buffers to avoid allocation in the inner loop. | ||
| Returns the point [k]P as (res_X, res_Z). | ||
| """ | ||
| function _ecm_scalar_mul!(res_X::BigInt, res_Z::BigInt, | ||
| k::BigInt, P_X::BigInt, P_Z::BigInt, | ||
| n::BigInt, a24::BigInt, buf::ECMBuffers) | ||
| R0_X, R0_Z = buf.R0_X, buf.R0_Z | ||
| R1_X, R1_Z = buf.R1_X, buf.R1_Z | ||
| tmp_X, tmp_Z = buf.tmp_X, buf.tmp_Z | ||
|
|
||
| # R0 = P, R1 = 2P | ||
| Base.GMP.MPZ.set!(R0_X, P_X) | ||
| Base.GMP.MPZ.set!(R0_Z, P_Z) | ||
| _ecm_double!(R1_X, R1_Z, P_X, P_Z, n, a24, buf) | ||
|
|
||
| bits = ndigits(k, base=2) | ||
| for i in (bits - 2):-1:0 | ||
| if isodd(k >> i) | ||
| _ecm_add!(tmp_X, tmp_Z, R0_X, R0_Z, R1_X, R1_Z, P_X, P_Z, n, buf) | ||
| Base.GMP.MPZ.set!(R0_X, tmp_X) | ||
| Base.GMP.MPZ.set!(R0_Z, tmp_Z) | ||
| _ecm_double!(tmp_X, tmp_Z, R1_X, R1_Z, n, a24, buf) | ||
| Base.GMP.MPZ.set!(R1_X, tmp_X) | ||
| Base.GMP.MPZ.set!(R1_Z, tmp_Z) | ||
| else | ||
| _ecm_add!(tmp_X, tmp_Z, R0_X, R0_Z, R1_X, R1_Z, P_X, P_Z, n, buf) | ||
| Base.GMP.MPZ.set!(R1_X, tmp_X) | ||
| Base.GMP.MPZ.set!(R1_Z, tmp_Z) | ||
| _ecm_double!(tmp_X, tmp_Z, R0_X, R0_Z, n, a24, buf) | ||
| Base.GMP.MPZ.set!(R0_X, tmp_X) | ||
| Base.GMP.MPZ.set!(R0_Z, tmp_Z) | ||
| end | ||
| end | ||
| Base.GMP.MPZ.set!(res_X, R0_X) | ||
| Base.GMP.MPZ.set!(res_Z, R0_Z) | ||
| end | ||
|
|
||
| """ | ||
| ecm_factor(n::BigInt, B1::Int, num_curves::Int) -> Union{BigInt, Nothing} | ||
|
|
||
| Attempt to find a non-trivial factor of `n` using the Elliptic Curve Method. | ||
| Computes [m]P where m = lcm(1..B1) = prod(p^floor(log_p(B1)) for p prime ≤ B1). | ||
| Uses batched gcd (accumulate Z coordinates, check periodically) to reduce gcd calls. | ||
| Returns a factor or `nothing` if none found within the curve budget. | ||
| """ | ||
| function ecm_factor(n::BigInt, B1::Int, num_curves::Int)::Union{BigInt, Nothing} | ||
| # Precompute prime powers for Stage 1 | ||
| prime_powers = BigInt[] | ||
| for p in primes(B1) | ||
| pk = BigInt(p) | ||
| while pk * p <= B1 | ||
| pk *= p | ||
| end | ||
| push!(prime_powers, pk) | ||
| end | ||
|
|
||
| buf = ECMBuffers() | ||
| Q_X = BigInt() | ||
| Q_Z = BigInt() | ||
| tmp_mul = BigInt() # scratch for acc * Q.Z | ||
|
|
||
| for _ in 1:num_curves | ||
| # Generate random curve via σ parameter (Suyama's parametrization) | ||
| σ = BigInt(rand(6:10^9)) | ||
| u = mod(σ * σ - 5, n) | ||
| v = mod(4 * σ, n) | ||
| x0 = mod(u * u * u, n) | ||
| z0 = mod(v * v * v, n) | ||
|
|
||
| vu_diff = mod(v - u, n) | ||
| a24_num = mod(vu_diff^3 * mod(3 * u + v, n), n) | ||
| a24_den = mod(16 * x0 * v, n) | ||
|
|
||
| g = gcd(a24_den, n) | ||
| if g > 1 && g < n | ||
| return g | ||
| end | ||
| if g == n | ||
| continue | ||
| end | ||
|
|
||
| a24_den_inv = invmod(a24_den, n) | ||
| a24 = mod(a24_num * a24_den_inv, n) | ||
|
|
||
| Base.GMP.MPZ.set!(Q_X, x0) | ||
| Base.GMP.MPZ.set!(Q_Z, z0) | ||
|
|
||
| # Stage 1: multiply Q by each prime power, with batched gcd | ||
| degenerate = false | ||
| acc = BigInt(1) | ||
| batch_count = 0 | ||
| for pk in prime_powers | ||
| _ecm_scalar_mul!(Q_X, Q_Z, pk, Q_X, Q_Z, n, a24, buf) | ||
| Base.GMP.MPZ.mul!(tmp_mul, acc, Q_Z) | ||
| _mpz_fdiv_r!(acc, tmp_mul, n) | ||
| batch_count += 1 | ||
|
|
||
| if batch_count >= 100 | ||
| g = gcd(acc, n) | ||
| if g > 1 && g < n | ||
| return g | ||
| end | ||
| if g == n | ||
| degenerate = true | ||
| break | ||
| end | ||
| Base.GMP.MPZ.set_si!(acc, 1) | ||
| batch_count = 0 | ||
| end | ||
| end | ||
|
|
||
| degenerate && continue | ||
|
|
||
| if batch_count > 0 | ||
| g = gcd(acc, n) | ||
| if g > 1 && g < n | ||
| return g | ||
| end | ||
| end | ||
| end | ||
| return nothing | ||
| end | ||
Oops, something went wrong.
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.
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.
is ECM slower than pollard for smaller numbers? That seems unexpected. Also, can you delete the polyalgorithm.jl file and move that code into here?
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.
Oh this is likely related to the ECM impl being BigInt only.