Factorize
On this page, we’ll cover all the different methods that exist to factorize a modulus \(N\) into its prime factors \(p\) and \(q\), given information on those primes. This includes known or shared bits, special structure, or known bits (or the whole value) of other parameters of RSA.
It also covers more general methods that are more or less efficient depending on the primes, how they are generated, or their size.
Special Structure
Given \(N\), \(p\), \(q\), if
\[ \left\lceil N^{1/2}-\left\lfloor p^{1/2}\right\rfloor\cdot\left\lfloor q^{1/2}\right\rfloor\right\rfloor \]is a sufficiently small integer, then one can efficiently recover the prime factors from the modulus \(N\) using the Ghafar-Ariffin-Asbullah attack.
| |
Shor’s algorithm
The algorithm is as follows:
- Choose a base \(a\) coprime with \(N\).
- Find an integer \(r\) such that \(a^r \equiv 1 \mod N\) (this is the hard part).
- Now, if \(r\) is even, you can write
- You can calculate either \(p\) or \(q\) as either \(a^{r/2} + 1\) or \(a^{r/2} - 1\).
- If \(r\) is odd, use the same formula but with the decreasing factors of \(r\).
You can read more about how the algorithm works on the Wikipedia page, especially about the quantum order sub-routine from which the speed on quantum computers comes from.
| |
Base conversion
In the case of specially formed primes, it is easy to recover the prime factors \(p\) and \(q\) from a modulus \(N\) by converting it to different bases. This can notably be used when the modulus bit pattern is not random/structured or when the Hamming weight of the modulus is low or high.
Low hamming weight
Tip
See this note for more details.
Unbalanced Moduli
Known Bits
TODO
Bits in common
TODO
Known bits of primes
In this section, we’ll see how in some cases it is possible to recover the prime factors \(p\) and \(q\) of \(N = pq\) if we know some contiguous high or low bits of one of the primes.
The following attacks are based on or are variants of the Coppersmith’s method
Known MSB bits
Tip
You can read a better written and more rigorous explanation in this paper’s section 4.2.2.
Suppose you have a \(n\)-bit modulus \(N\) being the product of two primes \(p\) and \(q\) and that you know the \(k\) high bits of \(p\), noted \(b\). Then, you can write
\[ p = 2^{n-k} \cdot b + u \quad \Rightarrow \quad 2^{n-k} \cdot b = p - u \]with \(u\) the unknown bits.
With this value of \(b\), you can create the polynomial
\[ \begin{aligned} f(X) &= X + 2^{n-k} \cdot b &\mod N \\ &= X + (p - u) &\mod N \\ &= X - u &\mod p \\ \end{aligned} \]which has \(p - u\) as a root mod \(N\), but more importantly a root \(u\) mod \(p\).
If you know enough bits of \(p\) (i.e. \(k\) is large enough compared to \(n\)), then \(u\) is small enought to be a small root of \(f(X)\) mod \(p\) and you can thus efficiently calculate it using Coppersmith’s method.
Now that you know \(u\), you can simply calculate \(p = 2^{n-k} \cdot b + u\) to recover one of the prime factors of \(N\).
| |
Note
You can find in this repo a simpler and Sage-oriented implementation making use of Sage’s built-in function for small_roots.
Known LSB bits
The reasoning is exactly the same as presented above, except for the fact that in this case you write
\[ p = 2^{k} \cdot u + b \quad \Rightarrow \quad b = p - 2^{k} \cdot u \]with \(k\) low bits known of \(p\), and
\[ \begin{aligned} f(X) &= X + b &\mod N \\ &= X + (p - 2^{k} \cdot u) &\mod N \\ &= X - 2^{k} \cdot u &\mod p \\ \end{aligned} \]Known middle bits
Tip
As before, you can find a better written and more rigorous explanation in this paper’s section 4.2.4.
Known bits chunks
Tip
Again, you check this this paper’s section 4.2.5.
Many random bits
In the case where the attacker has knowledge of many non-contiguous bits of both \(p\) and \(q\) randomly spread out, they can use the Branch and Prune method to iteratively find the missing bits of information from the factors \(p\) and \(q\).
You can check this code for a good implementation of this attack.