The Chamber of Primes: Fermat's Factorization & The Sieve of Ma'at
In 1643, French mathematician Pierre de Fermat introduced an elegant method to factor odd composite numbers by representing them as the difference of two perfect squares:
n = x² - y² = (x - y)(x + y)
While modern general-purpose factorization relies on the General Number Field Sieve (GNFS) or Pollard's ρ, Fermat's method remains uniquely compelling: it translates an algebraic factorization problem into a visual geometric search, and it cleanly exposes the fatal vulnerability of RSA moduli generated with close primes.
The Geometric Gnomon
Geometrically, if we construct a stone square of dimension x × x and carve out an inner square void of dimension y × y from its corner, the remaining shape is an L-tromino (ancient Greek: gnomon) whose area is exactly x² - y².
Fermat noticed that this gnomon can always be sliced into two arms and reassembled into a rectangle of width (x - y) and height (x + y). Therefore, whenever the carved void r = x² - n forms an exact square y², the sides of the reassembled rectangle yield the non-trivial factors:
p = x - y, q = x + y ⇒ p × q = n
Eliminating Multiplication: The Incremental Addition Loop
A naive implementation of Fermat's method would compute x² - n for each step x = ⌈√n⌉, ⌈√n⌉ + 1, … using full multiplications. However, the difference between consecutive squares follows an arithmetic progression of odd numbers:
(x + 1)² - n = (x² - n) + (2x + 1)
By maintaining a running remainder r and incrementing it by 2x + 1 at each iteration, the inner loop requires only 64-bit/128-bit additions, making candidate generation exceptionally fast.
The Sieve of Ma'at: Legendre Quadratic Residue Filter
The primary performance bottleneck in Fermat's method is checking whether the remainder r is a perfect square. Exact integer square roots (even binary digit-by-digit square roots) are computationally expensive compared to addition.
To resolve this, we employ the Legendre symbol (a/p). If an integer r is a square, it must be a quadratic residue modulo every prime p. If:
(r / p) = -1
then r is guaranteed to be a quadratic non-residue modulo p, meaning it is mathematically impossible for r to be a perfect square.
In our Rust engine (crypto-lab), we use precomputed lookup tables for 20 small primes (3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73). Modulo 3 alone rejects 33.3% of candidates; modulo 5 rejects 40% of the remainder. Across all 20 primes, the sieve eliminates more than 99.999% of non-squares in O(1) table lookups before any square root is ever evaluated.
The RSA Vulnerability: Why Primes Must Not Be Close
When constructing an RSA keypair, the public modulus is n = p × q. If prime factors p and q are chosen close to each other, their average:
x = (p + q) / 2 ≈ √n
lies extremely close to the starting value ⌈√n⌉. In our Ramses II preset (n = 10,969,629,527 = 104,723 × 104,749), this 34-bit RSA modulus is cracked by Fermat's method in step 1!
This visual demonstration highlights why modern cryptographic standards (such as FIPS 186-4 and RFC 8017) mandate that prime candidates must satisfy |p - q| > 2nlen/2 - 100.
Interactive Visualizer
To experience the geometric gnomon, real-time WebSocket telemetry, and ancient Egyptian hieroglyphic numeral conversion in action, visit the live interactive tool:
𓁨 Enter The Chamber of Primes ❯