Skip to main content

tifa

Fast factorization of integers of up to about 280 bits (84 digits), with the TIFA C library: its quadratic sieve, elliptic curves and smaller methods, chosen by the size of the number.

>>> import tifa
>>> tifa.factor(360)
{2: 3, 3: 2, 5: 1}
>>> tifa.factor(2**64 + 1)
{274177: 1, 67280421310721: 1}
>>> tifa.factor(1)
{}

factor(n) returns the prime factorization of a positive integer as {prime: exponent}, primes in increasing order, like SymPy's factorint. n can be an int or any integer type with __index__ (NumPy's, gmpy2's).

Speed

One thread, balanced semiprimes (the hardest numbers of a size), on an AMD EPYC 9V74 (a GitHub runner), the same numbers for both:

bits 100 140 180 220
tifa.factor 0.84 ms 13 ms 0.25 s 4.3 s
PARI/GP 2.17 factorint 2.0 ms 46 ms 0.64 s 10.3 s

Numbers with small factors take much less. On an Apple M5 Pro, the same sizes take 0.39 ms, 5.7 ms, 0.10 s and 2.0 s; from 180 bits the time doubles about every 10 to 12 bits there: 9 s at 240 bits, 21 s at 256 and 1.6 minutes at 280.

Large numbers

Composites of more than 320 bits, where the quadratic sieve would take hours to years, are searched by elliptic curves alone, for factors of up to 85 bits: factor always returns, but a composite left raises FactorizationError, its factors holding what was found. Both sizes are options: a larger siqs_max_bits lets SIQS take longer, a smaller ecm_max_bits gives up sooner (each 5 bits less halve about its time; 85 is the most):

tifa.factor(n, siqs_max_bits=340, ecm_max_bits=70)

Hints

When something is known of n, a hint saves time:

tifa.factor(n, hint="balanced")    # two primes of about the same size
tifa.factor(n, hint="unbalanced")  # a prime factor under a third of its size

A hint never changes the result, but a wrong one can cost much more time than none.

Threads

factor releases the GIL, so that threads factor in parallel:

from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor() as pool:
    results = list(pool.map(tifa.factor, numbers))

To factor one large number sooner, factor(n, threads=6) lets SIQS collect its relations in up to 6 threads, at most one per 100 primes of its factor base (none below about 100 bits), and the ECM search before it share its curves among as many. On an Apple M5 Pro, 6 threads take 0.41 of the time of one at 140 bits, 0.27 at 180 and 0.22 at 240, for more CPU time in all (a quarter more at 140 bits, 9% at 200). For many numbers, one per thread as above is better.

Time limits

try:
    factors = tifa.factor(n, timeout=60)
except tifa.FactorizationTimeout as e:
    print("not done in a minute:", e.factors)

FactorizationTimeout is a TimeoutError, and a FactorizationError: its factors hold what was found, which multiply to n. prime_certificate(p, timeout=60) raises TimeoutError. The limit is approximate: the algorithms check the time now and then (SIQS every 16 chunks of its sieve, ECM after each curve), so that a call can end a little after it, by a millisecond from 240 to 280 bits on an Apple M5 Pro, a few tens at most.

In the main thread, Ctrl-C interrupts factor and prime_certificate within about a twentieth of a second, raising KeyboardInterrupt (or what another signal handler raises). Python runs no signal handlers in other threads: there, a time limit is the way to bound them.

Tuning

The parameters of the algorithms were tuned on an Apple M5 Pro and recent x86-64 processors; others may want their own, above 240 bits most. The package installs tifa-tune, which measures them on the machine and writes them to files that set_tuning_dir points the package at:

tifa-tune --algo siqs --sizes 232,248,264,280 --search descent --trials 2 \
    --fit 8 --out /path/to/tuning
tifa.set_tuning_dir("/path/to/tuning")

Each file applies to the sizes it was tuned for, give or take 8 bits. set_tuning_dir holds for the whole process; until it is called, the directory is that of the TIFA_TUNING_DIR environment variable, if set. set_tuning_dir("") keeps the built-in parameters alone. tifa-tune --help lists its options, which TIFA's README explains; the example above takes a few hours.

Errors

TypeError if n, threads, siqs_max_bits or ecm_max_bits is not an integer or timeout not a number, ValueError if one of them is not positive or the hint is unknown. When n can not be factored completely (composites too large, see Large numbers, or rarely a failure, or with proof=True a factor not proven prime), tifa.FactorizationError is raised, its factors attribute holding what was found (factors that multiply to n, some of them not prime).

Proofs

The primes are told from composites by a Baillie-PSW test, which no composite is known to pass, but which proves nothing above 2^64. proof=True also proves them prime:

tifa.factor(n, proof=True)

prime_certificate(p) returns the proof, which anyone can check: check_certificate does, in plain Python.

>>> cert = tifa.prime_certificate(2**89 - 1)
>>> cert
[(618970019642690137449562111, [(2, 3), (3, 3), (5, 3), (17, 3), (23, 3), (89, 2), (353, 3), (397, 3)])]
>>> tifa.check_certificate(2**89 - 1, cert)
True

Each step (p, [(q, a), ...]) proves p prime from primes q of p - 1 and integers a (Pocklington's theorem, and Brillhart, Lehmer and Selfridge's); a step (p, [(q, a), ...], D, [(r, P), ...]) also uses primes r of p + 1, with Lucas sequences of discriminant D and parameters P (Morrison's theorem). Each q and r is below 2^64 or proven by an earlier step; a prime below 2^64 needs no step. The certificate is plain data: json.dumps saves it.

On an Apple M5 Pro, a proof takes 0.5 ms on average at 128 bits, 5.5 ms at 192 bits, 53 ms at 224 bits and 66 ms at 256 bits (up to about a second when both p - 1 and p + 1 are hard to factor). On random numbers of 100 to 200 bits, factor(n, proof=True) takes 2% to 25% more time than factor(n). Without proof=True, nothing changes.

Shared primes

batch_gcd(moduli) returns the gcd of each integer with the product of the others: 1 when it shares no prime with them. Among RSA moduli, a gcd other than 1 and the modulus itself is a prime factor of it, found without factoring anything:

>>> tifa.batch_gcd([15, 21, 22, 143])
[3, 3, 11, 11]

Product and remainder trees (Heninger et al., "Mining Your Ps and Qs", 2012) take 30 s for 100,000 moduli of 2048 bits on an Apple M5 Pro, and 0.8 GB; timeout and Ctrl-C work as for factor.

Cofactorization

The cofactorization step of a sieve (the number field sieve, or a quadratic sieve with large primes) asks which of the integers it leaves split into primes below a bound, 2^lpb. A Cofactorizer answers, much faster than factor, as it stops as soon as the answer is known:

>>> cofactorize = tifa.Cofactorizer(31, fb_bound=2**22, max_large_primes=3)
>>> cofactorize(2147483647 * 2147483629)
{2147483629: 1, 2147483647: 1}
>>> print(cofactorize(4294967311 * 4294967357))  # primes above 2^31
None

cofactorize.many(numbers) returns the answers for a whole list, without the cost of a Python call for each. fb_bound says that the integers have no prime factor below it (the factor base bound); max_large_primes limits the number of prime factors from fb_bound on; with max_siqs_bits, a composite of more bits than that is given up (None) once ECM has searched it as long as SIQS would take, which saves 35% to 40% of the time and misses 0.25% to 0.30% of the smooth integers with three large primes and max_siqs_bits = 2 * lpb. Calls release the GIL, and threads may share a Cofactorizer; it pickles, for processes. On an Apple M5 Pro, on integers of 33 to 64 bits without prime factors below 2^23 (lpb 32, two large primes), many takes 1.56 us per integer, 5% more than the C function, and calls one by one 1.92 us.

Command line

python -m tifa prints the prime factors of the integers of its command line, or else of its standard input, as GNU factor does:

$ python -m tifa 360 0x10001
360: 2 2 2 3 3 5
65537: 65537
$ python -m tifa --exponents 360
360: 2^3 3^2 5

--proof, --hint, --timeout, --threads, --siqs-max-bits and --ecm-max-bits are those of factor; an integer that is invalid or not factored is reported on the standard error, and makes the exit status 1. The package also installs the programs of the C library tifa-factor, tifa-batch-gcd (see Shared primes) and tifa-tune (see Tuning).

Installing

From PyPI:

pip install tifa-factor

Wheels exist for Linux (x86-64 and arm64, glibc and musl) and macOS (arm64 and x86-64, 11.0 and later), for Python 3.10 and later; GMP comes in them. Elsewhere, pip builds the package from source.

From a checkout of TIFA, at the top of the tree:

pip install ".[test]"    # or "pip install ." without pytest
python -m pytest         # the tests

A build from source needs a C compiler, CMake 3.26 or later (pip fetches it if the system's is older) and GMP with its headers (libgmp-dev on Debian and Ubuntu, gmp-devel on Fedora, brew install gmp on macOS). A GMP installed elsewhere than in the usual places is found through GMP_ROOT, the directory holding its include and lib:

GMP_ROOT=/opt/gmp pip install .

Licence

LGPL 2.1 or later, as TIFA. The wheels contain GMP 6.3.0 (gmplib.org), under the LGPL 3 (readme/license_v3.txt), linked statically: pip install --no-binary tifa-factor tifa-factor builds the package against the GMP of the system instead.

Metadata

Release files for tifa-factor 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tifa-factor 0.5.0
File Size Uploaded Provenance Actions
tifa_factor-0.5.0.tar.gz 720.4 kB With provenance Details for tifa_factor-0.5.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for tifa-factor 0.5.0
File Actions
tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl
tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details for tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl
tifa_factor-0.5.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 Details for tifa_factor-0.5.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
tifa_factor-0.5.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details for tifa_factor-0.5.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
tifa_factor-0.5.0-cp310-abi3-macosx_11_0_x86_64.whl CPython 3.10 abi3 macOS 11.0+ x86-64 Details for tifa_factor-0.5.0-cp310-abi3-macosx_11_0_x86_64.whl
tifa_factor-0.5.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for tifa_factor-0.5.0-cp310-abi3-macosx_11_0_arm64.whl

Total release size: 16.7 MB

Release files / tifa_factor-0.5.0.tar.gz

Download URL tifa_factor-0.5.0.tar.gz
Size 720.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cacaee1a224a19734fc9bb04ea8483152de6a5fe5850fde1a3e4e468660e5961
BLAKE2b-256 checksum
How to use checksums
2c04ffc6287b4ba98e167d6c5ce893a543fa1e4d4b55155d9575edf308a5d335
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 3.0 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
3c6bbde3c4b69b739ab8c1c84ae4b4423e7229c1c795b298c662465498767e05
BLAKE2b-256 checksum
How to use checksums
9fe0dd1a6bbce56ca33a459df7078dc1e8fe8c4730be98721a956b3d30d6c866
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 2.6 MB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
dad246253fe0ffb66e297215cf6e9ec6b5092d7cd9bf27ee0146c93d87336752
BLAKE2b-256 checksum
How to use checksums
2f9b4d83281aa1476db9ff3f4e57c1bde9868a579747df4a425aad1b088a25ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 2.9 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
42ad9e386d77749b06f25f6aa17fc195d1b4ce5f771e7e8303834858cab77ce1
BLAKE2b-256 checksum
How to use checksums
cd6cafadf008b497f4082bb0474c919fea53123ff9ec7d1b3ac8ee5890447f11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 2.6 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
f0c469bf808804e0804cf47de85d29214a60c305062f9805566154bc18284166
BLAKE2b-256 checksum
How to use checksums
51bd4c9ca007265f3e5cff77cc1469b482db7d7c125b019eeeaaeb81a43ea7dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-macosx_11_0_x86_64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-macosx_11_0_x86_64.whl
Size 2.6 MB
Tags CPython 3.10 abi3 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
4347bcaad4728a4ea680d30906cb3366deb283e280e5fdcd8f680399284deaab
BLAKE2b-256 checksum
How to use checksums
48d10b67c26615c937530ffb9b0de0975dc5116f01fe6671422cf5c96c7df334
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / tifa_factor-0.5.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL tifa_factor-0.5.0-cp310-abi3-macosx_11_0_arm64.whl
Size 2.3 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
06931cb789eb6de25a488fc7cf1dc23a46f459c959eb352163d05e352e4f0761
BLAKE2b-256 checksum
How to use checksums
b4ea75a9727837aa2a9b0f2d3c4c5622b41cc067a126880394c265e78b89488a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Security for tifa-factor 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for tifa_factor-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page