The Elliptic Curve Method for Integer Factorization (ECM)#
Sage includes GMP-ECM, which is a highly optimized implementation of Lenstra’s elliptic curve factorization method. See https://gitlab.inria.fr/zimmerma/ecm for more about GMP-ECM. This file provides a Cython interface to the GMP-ECM library.
AUTHORS:
Robert L Miller (2008-01-21): library interface (clone of ecmfactor.c)
Jeroen Demeyer (2012-03-29): signal handling, documentation
Paul Zimmermann (2011-05-22) – added input/output of sigma
EXAMPLES:
sage: from sage.libs.libecm import ecmfactor
sage: result = ecmfactor(999, 0.00)
sage: result[0]
True
sage: result[1] in [3, 9, 27, 37, 111, 333, 999] or result[1]
True
sage: result = ecmfactor(999, 0.00, verbose=True)
Performing one curve with B1=0
Found factor in step 1: ...
sage: result[0]
True
sage: result[1] in [3, 9, 27, 37, 111, 333, 999] or result[1]
True
sage: ecmfactor(2^128+1,1000,sigma=227140902)
(True, 5704689200685129054721, 227140902)
>>> from sage.all import *
>>> from sage.libs.libecm import ecmfactor
>>> result = ecmfactor(Integer(999), RealNumber('0.00'))
>>> result[Integer(0)]
True
>>> result[Integer(1)] in [Integer(3), Integer(9), Integer(27), Integer(37), Integer(111), Integer(333), Integer(999)] or result[Integer(1)]
True
>>> result = ecmfactor(Integer(999), RealNumber('0.00'), verbose=True)
Performing one curve with B1=0
Found factor in step 1: ...
>>> result[Integer(0)]
True
>>> result[Integer(1)] in [Integer(3), Integer(9), Integer(27), Integer(37), Integer(111), Integer(333), Integer(999)] or result[Integer(1)]
True
>>> ecmfactor(Integer(2)**Integer(128)+Integer(1),Integer(1000),sigma=Integer(227140902))
(True, 5704689200685129054721, 227140902)
- sage.libs.libecm.ecmfactor(number, B1, verbose=False, sigma=0)[source]#
Try to find a factor of a positive integer using ECM (Elliptic Curve Method). This function tries one elliptic curve.
INPUT:
number
– positive integer to be factoredB1
– bound for step 1 of ECMverbose
(default:False
) – print some debugging information
OUTPUT:
Either
(False, None)
if no factor was found, or(True, f)
if the factorf
was found.EXAMPLES:
sage: from sage.libs.libecm import ecmfactor
>>> from sage.all import * >>> from sage.libs.libecm import ecmfactor
This number has a small factor which is easy to find for ECM:
sage: N = 2^167 - 1 sage: factor(N) 2349023 * 79638304766856507377778616296087448490695649 sage: ecmfactor(N, 2e5) (True, 2349023, ...)
>>> from sage.all import * >>> N = Integer(2)**Integer(167) - Integer(1) >>> factor(N) 2349023 * 79638304766856507377778616296087448490695649 >>> ecmfactor(N, RealNumber('2e5')) (True, 2349023, ...)
If a factor was found, we can reproduce the factorization with the same sigma value:
sage: N = 2^167 - 1 sage: ecmfactor(N, 2e5, sigma=1473308225) (True, 2349023, 1473308225)
>>> from sage.all import * >>> N = Integer(2)**Integer(167) - Integer(1) >>> ecmfactor(N, RealNumber('2e5'), sigma=Integer(1473308225)) (True, 2349023, 1473308225)
With a smaller B1 bound, we may or may not succeed:
sage: ecmfactor(N, 1e2) # random (False, None)
>>> from sage.all import * >>> ecmfactor(N, RealNumber('1e2')) # random (False, None)
The following number is a Mersenne prime, so we don’t expect to find any factors (there is an extremely small chance that we get the input number back as factorization):
sage: N = 2^127 - 1 sage: N.is_prime() True sage: ecmfactor(N, 1e3) (False, None)
>>> from sage.all import * >>> N = Integer(2)**Integer(127) - Integer(1) >>> N.is_prime() True >>> ecmfactor(N, RealNumber('1e3')) (False, None)
If we have several small prime factors, it is possible to find a product of primes as factor:
sage: N = 2^179 - 1 sage: factor(N) 359 * 1433 * 1489459109360039866456940197095433721664951999121 sage: ecmfactor(N, 1e3) # random (True, 514447, 3475102204)
>>> from sage.all import * >>> N = Integer(2)**Integer(179) - Integer(1) >>> factor(N) 359 * 1433 * 1489459109360039866456940197095433721664951999121 >>> ecmfactor(N, RealNumber('1e3')) # random (True, 514447, 3475102204)
We can ask for verbose output:
sage: N = 12^97 - 1 sage: factor(N) 11 * 43570062353753446053455610056679740005056966111842089407838902783209959981593077811330507328327968191581 sage: ecmfactor(N, 100, verbose=True) Performing one curve with B1=100 Found factor in step 1: 11 (True, 11, ...) sage: ecmfactor(N/11, 100, verbose=True) Performing one curve with B1=100 Found no factor. (False, None)
>>> from sage.all import * >>> N = Integer(12)**Integer(97) - Integer(1) >>> factor(N) 11 * 43570062353753446053455610056679740005056966111842089407838902783209959981593077811330507328327968191581 >>> ecmfactor(N, Integer(100), verbose=True) Performing one curve with B1=100 Found factor in step 1: 11 (True, 11, ...) >>> ecmfactor(N/Integer(11), Integer(100), verbose=True) Performing one curve with B1=100 Found no factor. (False, None)