This post is part of COSIC’s Guide to Crypto, in which researchers from COSIC are informally writing about different concepts in cryptography. Our aim is to introduce the reader to these concepts with explanations, illustrations and examples. Today’s topic: factoring using factor bases!
In their 1978 paper introducing the RSA cryptosystem, Rivest, Shamir and Adleman recommended the use of public keys \( N=pq \) of about 200 decimal digits. They estimated that it would take billions of years to factor such numbers. Reality caught up with that recommendation, which has long been updated to 600 digits or larger. Indeed, the 200-digit RSA challenge got factored about 15 years ago, and very recently the same happened to the 250-digit challenge:

There are two main ingredients to this remarkable progress: (i) a dramatic increase in available computing power, and (ii) significant improvements to the factor-base method for factoring integers, on which the RSA people had based their estimate. Just to put things in perspective: the naive trial-division method from high school won’t let you break RSA keys of more than 40 digits, even with modern-day resources.
This blog post aims at giving a high-level introduction to this factor-base method. In a subsequent blog post, we will discuss the algebraic-group method. These are the two known approaches to factoring integers faster than exponential-time. Both methods rely on arithmetic in \( \mathbf{Z}_N \), the ring of integers modulo our public key \( N \). Through the Chinese remainder theorem, this ring can be viewed as a product of two fields:
\( \mathbf{Z}_N \cong \mathbf{F}_p \times \mathbf{F}_q. \)
(An integer \( a \bmod N \) on the left corresponds to the couple \( (a \bmod p, a \bmod q) \) on the right.) This may seem a useless statement because we do not know \( p \) and \( q \), yet it helps in understanding the properties of \( \mathbf{Z}_N \). For example, it tells us that the equation \( x^2=1 \) has four solutions in \( \mathbf{Z}_N \), for the simple reason that the corresponding equation \( (x_1^2, x_2^2) = (1, 1) \) has four solutions in \( \mathbf{F}_p \times \mathbf{F}_q \), namely \( (1, 1) \), \( (1, -1) \), \( (-1, 1) \) and \( (-1, -1) \).
The goal in both approaches is to produce an element of \( a \in \mathbf{Z}_N \) which has non-negligible odds of having exactly one zero component when viewed on the right. In that case \( a \) is divisible by exactly one of the factors \( p \) or \( q \). This factor can then be revealed through the computation of \( \gcd(a,N) \) using Euclid’s lightning fast algorithm.
Factor-base method
Here, this goal is achieved by producing a random solution \( b \in \mathbf{Z}_N \) to the equation \( x^2=1 \). As we mentioned, under the Chinese remainder theorem this corresponds to one of \( (1, 1) \), \( (1, -1) \), \( (-1, 1) \), \( (-1, -1) \), and therefore \( a := b-1 \) corresponds to one of
\( (0,0), (0, -2), (-2, 0), (-2, -2). \)
The middle two couples are of the desired shape, so we have a 50% chance that \( \gcd(a, N) \) will reveal a secret factor of \( N \).
This idea is not unique to the factor-base method, which is really about how to find such a random solution \( b \). The main tool — nomen est omen — is a “factor base”. This is just a smallish set \( \mathcal{F} = \{ p_1, p_2, \ldots, p_r \} \) over which integers are easy to factor, meaning the following: for any given integer it should be easy to see whether it can be written as a product of powers of \( p_j \)’s or not, and it should be easy to carry out this factorization in practice. In today’s exposition, factor bases always consist of all prime numbers up to a given bound \( B \), and factoring over that factor base is done by trial division. For example, we could choose \( B=16 \) so that
\( \mathcal{F} = \{ 2, 3, 5, 7, 11, 13 \}, \)
and then \( 6435 = 3^2\cdot 5 \cdot 11 \cdot 13 \) factors over \( \mathcal{F} \), but \( 6436 = 2^2 \cdot \mathbf{?} \) does not.
Now, instead of directly searching for a random \( b \in \mathbf{Z}_N \) that satisfies \( b^2 = 1 \), let us lower our expectations and look for a random \( b \in \mathbf{Z}_N \) for which \( b^2 \), say when viewed as an integer between \( 0 \) and \( N-1 \), factors over \( \mathcal{F} \). Unless the bound \( B \) was chosen unreasonably small, we expect that a non-negligible amount of such integers can indeed be written as a product of powers of the \( p_j \)’s. Therefore our strategy is just plain trial-and-error. The resulting identity
\( b^2 \equiv p_1^{e_1} \cdot p_2^{e_2} \cdots p_r^{e_r} \bmod N \)
is called a relation. In its own right it is useless, but as we will explain in a minute, finding enough relations allows to construct the desired random solution to \( x^2 = 1 \).
Let us first illustrate the method through an example: imagine we wish to crack the RSA-modulus \( N = 64321 \). We choose \( B = 16 \) as above and randomly pick \( b = 28475 \). Modulo \( N \) this squares to \( 59420 \), off which we can peel two \( 2 \)’s and one \( 5 \) as factors. But then we are stuck:
\( 28475^2 \equiv 2^2 \cdot 5 \cdot \mathbf{?} \bmod N. \)
Too bad, we discard and try again, now with \( b = 45131 \). This one squares to \( 18375 \) modulo \( N \), which fully factors as \( 3 \cdot 5^3 \cdot 7^2 \), leading to the relation
\( 45131^2 \equiv 3 \cdot 5^3 \cdot 7^2 \bmod N. \)
Another good example is \( b = 266 \), whose square \( 6435 \) we already encountered: it splits as \( 3^2\cdot 5 \cdot 11 \cdot 13 \). Say that after a few more wins and losses we end up with the following four relations:
\( \begin{array}{rcl} 45131^2 & \equiv & 2^0 \cdot 3^1 \cdot 5^3 \cdot 7^2 \cdot 11^0 \cdot 13^0, \\ 266^2 & \equiv & 2^0 \cdot 3^2\cdot 5^1 \cdot 7^0 \cdot 11^1 \cdot 13^1, \\ 16562^2 & \equiv & 2^2 \cdot 3^3 \cdot 5^2 \cdot 7^0 \cdot 11^0 \cdot 13^1, \\ 49962^2 & \equiv & 2^2 \cdot 3^6 \cdot 5^0 \cdot 7^0 \cdot 11^1 \cdot 13^0 \\ \end{array} \)
(again, congruences modulo \( N \)). Then here comes the main trick: while the individual relations are somewhat pointless, their product
\( 45131^2 \cdot 266^2 \cdot 16562^2 \cdot 49962^2 \equiv 2^4 \cdot 3^{12} \cdot 5^6 \cdot 7^2 \cdot 11^2 \cdot 13^2 \)
which can be rewritten as
\( (45131 \cdot 266 \cdot 16562 \cdot 49962)^2 \equiv (2^2 \cdot 3^6 \cdot 5^3 \cdot 7 \cdot 11 \cdot 13)^2 \)
is incredibly useful: indeed, through inversion modulo \( N \) we have found our desired solution
\( b = \frac{45131 \cdot 266 \cdot 16562 \cdot 49962}{2^2 \cdot 3^6 \cdot 5^3 \cdot 7 \cdot 11 \cdot 13} = 3929 \in \mathbf{Z}_N \)
to the equation \( x^2 = 1 \)! As explained at the beginning, by computing \( \gcd(a, N) \) with \( a := b – 1 = 3928 \), we have a 50% chance of revealing a non-trivial factor of \( N \). In this case we are lucky and find \( p = 491 \); the other factor is then \( q = N/p = 131 \).
This generalizes as follows. Given an RSA-modulus \( N \), we choose some bound \( B \) defining a factor base
\( \mathcal{F} = \{ \, \text{primes } p \leq B \, \} = \{ p_1, p_2, \ldots, p_r \}, \)
and by considering random squares in \( \mathbf{Z}_N \) we eventually find relations
\( \begin{array}{rcl} b_1^2 & \equiv & p_1^{e_{1,1}} \cdot p_2^{e_{1,2}} \cdots p_r^{e_{1,r}}, \\ b_2^2 & \equiv & p_2^{e_{2,1}} \cdot p_2^{e_{2,2}} \cdots p_r^{e_{2,r}}, \\ \phantom{x} & \vdots \\ b_s^2 & \equiv & p_1^{e_{s,1}} \cdot p_2^{e_{s,2}} \cdots p_r^{e_{s,r}}. \end{array} \)
In the example, when taking the product of all relations, we ended up with a right-hand side consisting of even powers of the \( p_j \)’s. It is too naive to hope that this is true in general. Luckily, there is an easy method for finding a subset of the set of relations whose product has that property. Indeed, if we define selector bits \( s_i = 0 \) resp. \( s_i = 1 \) according to whether we wish to include the \( i \)-th relation or not, we want them to satisfy
\( \begin{pmatrix} s_1 & s_2 & \ldots & s_s \end{pmatrix} \begin{pmatrix} e_{1, 1} & e_{1, 2} & \ldots & e_{1, r} \\ e_{2, 1} & e_{2,2} & \ldots & e_{2, r} \\ \vdots & \vdots & \ddots & \vdots \\ e_{s, 1} & e_{s,2} & \ldots & e_{s, r} \end{pmatrix} \equiv \begin{pmatrix} 0 & 0 & \ldots & 0 \end{pmatrix} \bmod 2, \)
and they should not all be equal to \( 0 \). In other words, we are looking for a non-trivial element in the left kernel of the matrix \( (e_{i,j}) \) when considered over \( \mathbf{F}_2 \). So it just amounts to linear algebra. Note that the desired element is guaranteed to exist if \( s > r \). Once we have found the selector bits, we proceed as in the example to find an identity \( \beta^2 \equiv \pi^2 \bmod N \), where \( \beta \) is the product of the selected \( b_i \)’s and \( \pi \) is a product of powers of our factor base elements \( p_j \). Through inversion mod \( N \) we find our desired solution \( b = \beta / \pi \) to the equation \( x^2 = 1 \) and we try to factor \( N \) by computing \( \gcd(b – 1, N) \). The connoisseurs will remark that this gcd can also be computed as \( \gcd(\beta – \pi, N) \). So the inversion step can actually be avoided.
Recall that the chance of success is about 50%. Luckily, one does not need to start all over in the event of a failure. Instead, one just repeats the last step using different selector bits, which exist as soon as our exponent matrix \( (e_{i,j})_{i,j} \) has a large enough left kernel modulo \( 2 \). If needed, we can search for a few more relations to slightly enlarge \( s \), thereby enlarging the kernel.
We are left with a crucial question: what value of \( B \) do we take? If it is chosen very small, then the chance of finding random squares whose reductions modulo \( N \) factor over \( \mathcal{F} \) becomes negligible. As an extreme example: if \( B = 1 \) then we are directly searching for a random solution to \( x^2 = 1 \) using trial-and-error, which is utterly hopeless. On the other hand, when choosing a very large \( B \), we need an awful lot of relations before we can find a non-trivial element in the left kernel of our exponent matrix. Apart from the work needed to find these relations and the amount of space needed to store them (which are easy to distribute across many devices), the linear algebra might become intractable. Furthermore, factoring over the factor base becomes harder.
Quite remarkably, the optimal balance is found for some \( B \) in the sub-exponential range. For those familiar with the \( L \)-notation: the optimal \( B \) lies in \( L_N(\frac{1}{2}; \frac{1}{2}) \), and the overall method then runs in time \( L_N(\frac{1}{2}; 2) \). See page 328 of Galbraith’s book for a precise analysis.
We have just come to describe Dixon’s random squares algorithm from 1981, which is the prototypical factor-base method (although historically, it was preceded by the continued fraction method, referred to in the RSA paper). There are two natural target points for making the method more efficient. One is speeding up the computation of the selector bits \( s_i \) by resorting to sparse linear algebra: indeed, in general, the vast majority of the exponents \( (e_{i,j}) \) will be zero. Another natural goal is to accelerate the search for relations, which in turn allows for a smaller factor base and therefore, again, faster linear algebra.
Speeding up the relation search
Here is a small example of such an acceleration: by including \( -1 \) in the factor base and reducing our random squares \( b_i^2 \) into the interval \( \{ -\lfloor N/2 \rfloor, \ldots, \lfloor N/2 \rfloor \} \), rather than \( \{ 0, \ldots, N-1 \} \), we end up with smaller numbers (in absolute value), which have a bigger chance of factoring over \( \mathcal{F} \). More effectively, instead of taking genuinely random squares, one can stick to \( b_i \)’s that lie very close to \( \sqrt{N} \), leading to even smaller outcomes. On top of that, this can be combined with a bookkeeping tool called sieving, allowing for a more dedicated search for \( b_i \)’s whose squares factor over \( \mathcal{F} \). These are, in a nutshell, the main ingredients of the quadratic sieve, which was designed by Pomerance in 1981. It runs in time \( L_N(\frac{1}{2}; 1) \) and was the first algorithm capable of breaking through the 100-digit barrier; this happened in 1991.
Since 1996, all record factorizations were obtained using its successor: the number field sieve, whose underlying ideas came to maturity at the turn of the 90’s. A detailed discussion goes beyond the scope of this blog post, but the main difference is as follows. In the quadratic sieve, as in Dixon’s random squares algorithm, the square on the left-hand side of the congruence \( \beta^2 \equiv \pi^2 \bmod N \) is obtained in a naive way, namely as a mere product of \( b_i^2 \)’s. In particular, the factor base only serves the construction of a square on the right-hand side. In the number field sieve, both sides are constructed using a factor base. Using ideas from algebraic number theory, this can be done at the benefit of having to check much smaller numbers for factorizability into small primes. With a heuristic runtime of
\( L_N(\frac{1}{3}, \sqrt[3]{\frac{64}{9}}), \)
the number field sieve is our best known factorization algorithm, although its power only kicks in from numbers of about 100 digits onward. For more background, we refer to Pomerance’s historical survey and to Stevenhagen’s notes.


