RSA Factorization from Known (N, e, d)
- •
What it is
- •
If an attacker learns a valid (public modulus , public exponent , private exponent ) triple for an RSA key — even without directly knowing or — the modulus can be factored efficiently, in randomized polynomial time. The key fact: is a multiple of , so for (almost) any base , repeatedly halving the exponent of (writing with odd, squaring up from ) will, with probability at least per random , land on a non-trivial square root of unity modulo partway through — a value with — at which point splits off a genuine factor, exactly the same mechanism as RSA Fixed-Point Leakage (Unconcealed Messages) → Factorization and Miller-Rabin's compositeness witnesses.
- •
- •
When to apply
- •
You've obtained a private exponent for a key without its factorization directly (e.g. it's your own real key, provided as , and you want to recover from it programmatically).
- •
- •
Math
- •
for some integer . Write , odd. For random , compute ; if , repeatedly square it up to times, checking whether the result becomes 1 — if so, the previous value is (with good probability) a non-trivial square root of unity, and reveals a factor. Retry with a fresh random if an attempt doesn't produce one.
- •
- •
Worked example
- •
Toy 256-bit RSA key with : computing normally, then running only the factor-from- procedure (as if were unknown) correctly recovered the exact original pair.
- •
- •
Python
- •
def factor_from_d(N, e, d): k = e*d - 1 t = k r = 0 while t % 2 == 0: t //= 2 r += 1 while True: g = random.randrange(2, N - 2) y = pow(g, t, N) if y in (1, N - 1): continue for _ in range(r): x = pow(y, 2, N) if x == 1: fac = math.gcd(y - 1, N) if 1 < fac < N: return fac, N // fac break y = x
- •
- •
SageMath
- •
Not particularly relevant — this is a direct probabilistic algorithm, equally simple in either language.
- •
- •
- •
Cards
- •
What single fact about ed-1 makes this factorization algorithm work?
- •
ed - 1 is a multiple of φ(N), which lets you replicate the "find a non-trivial square root of unity" trick used by Miller-Rabin, without knowing φ(N)'s value directly.
- •
- •
Why might a single random base g fail to produce a factor, and what's the fix?
- •
With roughly 50% probability per random g, the squaring sequence hits -1 before ever revealing a non-trivial root; simply retrying with a fresh random g resolves it.
- •
- •