Two RSA keypairs and one generator script. RSA1 exists for exactly one
purpose: leaking the top bits of a prime p1 through a low-exponent cube
root. The second modulus is built from p2 = next_prime(cyclic_xor(p1, key)),
and since XOR is bytewise with no carries, the leaked top bits of p1
become the top bits of p2 as well. What's left is factoring n2 with
part of a factor already known - a textbook Coppersmith setup, and the
generator's own source comment (copper what???) is basically a wink at
the technique.
Hint
e = 3 on a message much shorter than the modulus usually means "the
cube root is exact, no modular reduction needed." What tiny message would
an author embed just to leak something?
Step 1: cube-root the leak
RSA1 uses e = 3 with a 512-bit and a 3200-bit prime, so n1 is 3712
bits. The message it carries is short:
1m1 = b"nice_cube_rooting:" + str(p1 >> 120)
At roughly 824 bits, m1^3 lands near 2470 bits - comfortably under
n1 - so c1 is exactly m1^3 with no modular reduction. A plain
integer cube root recovers it whole:
1from gmpy2 import mpz, iroot
2m1, exact = iroot(mpz(c1), 3)
3assert exact
4# b"nice_cube_rooting:16808715527065818972790937725219045818794534263031797..."
That gives t = p1 >> 120: the top 280 bits of p1. The low 120 bits
stay hidden.
Step 2: p1's top bits leak straight into p2
p2 = next_prime(cyclic_xor(p1, b"L3M0N")). p1 is 400 bits (50 bytes)
and the XOR key is 5 bytes, so the key repeats every 5 bytes, and -
because XOR has no carries - byte i of the output depends only on
byte i of the input.
Insight
The top 35 bytes of cyclic_xor(p1, key) therefore depend only on the
top 35 bytes of p1, which is exactly the t just recovered. The
generator only accepts a p1 where p2 and xor_candidate(p1) agree
above bit 120, so those same top 280 bits are also p2's top 280
bits. That's 70% of p2 known for free, from a completely different
keypair.
Step 3: Coppersmith on the rest
Write p2 = X_hi * 2^120 + z, with X_hi known and 0 <= z < 2^120.
Factoring n2 while the high bits of one factor are known is a
univariate Coppersmith problem: find the small root z of
f(x) = X_hi*2^120 + x (mod n2), monic, with bound X = 2^120.
Pitfall
My first attempt passed beta = 400/912, the obvious ratio of p2's 400
bits to n2's 912 bits. small_roots came back empty. I assumed the
model itself was wrong and rebuilt a synthetic instance with a known
answer to check - which confirmed the true z really is a small root, so
the model was fine and the call was the problem.
Reading small_roots's own filter: it only keeps a candidate when
gcd(N, f(root)) >= N^beta. With beta = 400/912 that threshold sits
just above the actual 400-bit factor, so the correct root gets thrown
away by the library's own confidence check, not by the math. Passing a
genuine lower bound, beta = 0.435, returns z on the first call.
1from sage.all import PolynomialRing, Zmod
2
3R = PolynomialRing(Zmod(n2), 'x')
4x = R.gen()
5f = (X_hi * (1 << 120) + x).monic()
6roots = f.small_roots(X=1 << 120, beta=0.435)
7z = int(roots[0])
8p2 = X_hi * (1 << 120) + z
9assert n2 % p2 == 0
10q2 = n2 // p2
Finishing the RSA
1from gmpy2 import invert
2
3phi = (p2 - 1) * (q2 - 1)
4d = int(invert(e2, phi))
5m = pow(c2, d, n2)
6flag = m.to_bytes((m.bit_length() + 7) // 8, "big")
7print(flag.decode())
L3m0n{67f8efa3160ed89e4eaf73dd1ea674bd}
Note
Double-checked two ways: p2 * q2 == n2 exactly, and re-encrypting the
recovered plaintext with (e2, n2) reproduces the original c2 byte for
byte.