Computer Science / RSA & Modular Arithmetic
FREE TO EXPLORE
LABORATORY 23 · COMPUTER SCIENCE

RSA & Modular Arithmetic theory

Understand rsa & modular arithmetic: recurrence relations, Big-O bounds, decision trees and hardware cache interactions.

Theoretical Foundations of RSA & Modular Arithmetic and Computational Complexity

Modern digital communication, global commerce, and national defense rely on public-key cryptography to establish secure channels over untrusted public networks. In 1977, Ron Rivest, Adi Shamir, and Leonard Adleman created the RSA cryptosystem, realizing Diffie and Hellman's vision of asymmetric trapdoor one-way functions. RSA leverages the profound computational asymmetry between modular exponentiation and prime factorization: multiplying two massive prime numbers p and q to form modulus n = p · q executes in microseconds, whereas factoring n back into p and q requires astronomical computational energy under the best known classical sub-exponential algorithms (the General Number Field Sieve, GNFS). Under RSA, public encryption C = M^e mod n is accessible to all, while decryption M = C^d mod n requires the private exponent d = e^(-1) mod φ(n), calculated via the Extended Euclidean Algorithm. To evaluate modular powers with 2048-bit exponents without performing 10^616 sequential multiplications, cryptographic hardware utilizes the Square-and-Multiply algorithm. By scanning the binary bitstring of the exponent, the algorithm executes exactly B modular squarings and an average of 0.5 B modular multiplications, establishing the deterministic linear scaling law M(B) = 1.5000 · B operations. In this laboratory investigation, students benchmark modular exponentiation across key lengths B ∈ [64, 4096], verify the linear scaling law M(B) = 1.5000 · B + 0.0000 via ordinary least-squares regression, execute the Extended Euclidean Algorithm, analyze Miller-Rabin primality testing bounds, and contrast classical factorization hardness with quantum Shor's algorithm threat models.

TOPIC 01 · FORMAL ANALYSIS

What is the foundational difference between Symmetric and Asymmetric (Public-Key) Cryptography?

Symmetric cryptography uses the exact same secret key for both encryption and decryption (e.g. AES). In an open network with N participants, pairwise communication requires sharing and storing N(N-1)/2 keys, creating a massive key distribution bottleneck. Asymmetric cryptography (Diffie-Hellman, RSA) uses a mathematically linked key pair: a public key (openly distributed) for encryption and a private key (kept secret) for decryption. Anyone can encrypt a message using the recipient's public key, but only the recipient holding the corresponding private key can decrypt it, solving the key distribution problem completely.

TOPIC 02 · FORMAL ANALYSIS

How does the RSA cryptosystem work, and what is the role of Euler's Totient Theorem?

RSA chooses two large primes p and q, computing modulus n = pq and Euler's totient φ(n) = (p-1)(q-1). A public exponent e is chosen coprime to φ(n), and the private exponent d is computed as the modular inverse: ed ≡ 1 (mod φ(n)). Encryption evaluates C = M^e mod n; decryption evaluates M' = C^d mod n. According to Euler's Totient Theorem, if gcd(M, n) = 1, then M^φ(n) ≡ 1 (mod n). Because ed = k·φ(n) + 1, we have C^d ≡ (M^e)^d = M^(k·φ(n)+1) = (M^φ(n))^k · M ≡ (1)^k · M ≡ M (mod n), guaranteeing flawless mathematical decryption.

TOPIC 03 · FORMAL ANALYSIS

How does the Extended Euclidean Algorithm compute the private key exponent d?

The condition ed ≡ 1 (mod φ(n)) can be rewritten as the linear Diophantine equation e·d + φ(n)·y = gcd(e, φ(n)) = 1. The Extended Euclidean Algorithm evaluates the sequence of quotients and remainders from the standard Euclidean algorithm backwards to determine the integer Bézout coefficients x and y satisfying a·x + b·y = gcd(a, b). For inputs e and φ(n), the resulting coefficient x represents the exact modular multiplicative inverse d = e^(-1) mod φ(n), computed in strictly logarithmic O(log(min(e, φ(n)))) operations.

TOPIC 04 · FORMAL ANALYSIS

Why is the Square-and-Multiply algorithm used for modular exponentiation, and what is its operational complexity?

Evaluating a^b mod n naively by multiplying a by itself b times requires b multiplications. For a 2048-bit exponent b, b ≈ 10^616, which would take billions of years. The Square-and-Multiply algorithm scans the binary bits of b from left to right. At each bit position, it squares the current accumulator (acc = acc² mod n); if the bit is 1, it additionally multiplies by the base (acc = acc·a mod n). For a B-bit exponent, this evaluates exactly B squarings and an average of B/2 multiplications (assuming 50% ones in the binary key), completing in strictly M(B) = 1.5 B modular operations (a few thousand operations for a 2048-bit key, executing in milliseconds).

TOPIC 05 · FORMAL ANALYSIS

How does the Miller-Rabin test verify whether massive integers are prime?

The Miller-Rabin test is a randomized polynomial-time algorithm based on Fermat's Little Theorem and the property that 1 has only two square roots modulo a prime: 1 and -1. For odd candidate n, it factors n-1 = 2^s · d. For a random witness base a, it evaluates x = a^d mod n. If x = 1 or x = n-1, n passes. Otherwise, it squares x repeatedly up to s-1 times: if x becomes n-1, it passes; if the loop terminates without seeing n-1, n is proven composite. If n is composite, at least 75% of all bases are witnesses. By running k = 40 independent rounds with random bases, the probability of falsely accepting a composite as prime is bounded by (1/4)^40 = 2^(-80) < 10^(-24).

TOPIC 06 · FORMAL ANALYSIS

What is the best known algorithm for breaking RSA, and why is 2048-bit RSA considered secure?

The security of RSA relies on the computational difficulty of factoring the composite modulus n into primes p and q. The fastest known classical factoring algorithm is the General Number Field Sieve (GNFS), which runs in sub-exponential time L_n[1/3, c] = exp((c + o(1)) (ln n)^(1/3) (ln ln n)^(2/3)). While 512-bit RSA was factored in 1999 and 768-bit in 2009, 2048-bit RSA has a modulus with 617 decimal digits. Factoring a 2048-bit modulus using GNFS would require an estimated 10^9 to 10^11 core-years of continuous computing, far exceeding global computational capabilities.

TOPIC 07 · FORMAL ANALYSIS

What threat do quantum computers pose to RSA, and how does Shor's algorithm work?

In 1994, Peter Shor demonstrated that a sufficiently large fault-tolerant quantum computer can factor composite integers in polynomial time O((log n)³). Shor's algorithm reduces factoring to finding the period r of the modular exponential function f(x) = a^x mod n, which can be computed in polynomial time using the Quantum Fourier Transform (QFT). Once the period r is found, gcd(a^(r/2) ± 1, n) yields the non-trivial prime factors of n with high probability. A quantum computer with approximately 4,000 logical qubits could break 2048-bit RSA in hours, driving the transition to post-quantum cryptography (PQC).

↳
Connect the theory to benchmark observations.

In the Square-and-Multiply modular exponentiation benchmark across private key exponent bit lengths B in [64, 4096] bits (where the exponent d is an IEEE large integer with uniform Hamming weight W ≈ 0.5 · B, representing realistic cryptographic keys), exponentiation proceeds via binary scanning from the most significant bit to the least significant bit. At each bit position i ∈ [0..B-1], the algorithm unconditionally executes exactly 1 modular squaring operation (acc = acc² mod n) to double the binary weight. Additionally, whenever the current bit b_i = 1, the algorithm executes exactly 1 modular multiplication (acc = acc · base mod n). Across an exponent of bit length B, the cumulative arithmetic operation count is strictly governed by the exact deterministic linear invariant M(B) = B + 0.5 · B = 1.5000 · B + 0.0000 modular operations. Plotting modular operations against exponent bit length B yields an ordinary least-squares regression slope k_mod = 1.5000 operations/bit and baseline intercept M_0 = 0.0000 operations with an ideal coefficient of determination R² = 1.000000.