Number Theory Background
Public-key cryptography is built on arithmetic that is easy to compute in one direction and hard to reverse without special information. RSA uses modular exponentiation modulo a product of primes. Diffie-Hellman uses exponentiation in cyclic groups where discrete logarithms are believed hard. Elliptic-curve systems use the same group language over points on curves. Before those schemes make sense, the number-theory toolkit must be precise.
Katz and Lindell give a self-contained number-theory chapter for primes, modular arithmetic, groups, Euler's theorem, the Chinese remainder theorem, RSA and Diffie-Hellman assumptions. Smart covers similar ground early, with more explicit algorithms for Euclid, inverses, CRT, finite fields, and implementation issues. The common theme is computational: the arithmetic operations are efficient, while selected inverse problems are believed infeasible at cryptographic sizes.
Definitions
An integer divides , written , if for some integer . A prime is an integer whose only positive divisors are and .
The greatest common divisor is the largest positive integer dividing both and . If , then and are coprime.
Modular equivalence is:
when . The set of residues modulo is .
The multiplicative group of invertible residues modulo is:
Euler's phi function is:
If for distinct primes , then:
A modular inverse of modulo is a value satisfying:
It exists exactly when .
A cyclic group is a group generated by one element , meaning every element can be written as for some integer . The order of is the smallest positive such that .
Key results
The Euclidean algorithm computes efficiently by repeatedly replacing with until the remainder is zero. The extended Euclidean algorithm also computes integers such that:
When , this identity gives a modular inverse:
Fermat's little theorem says that if is prime and , then:
Euler's theorem generalizes it: if , then:
The Chinese remainder theorem says that if are pairwise coprime, then the system:
has a unique solution modulo . The map
is a ring isomorphism. In RSA, CRT speeds up private-key operations by computing modulo and modulo separately, then recombining.
Fast modular exponentiation computes in multiplications by repeated squaring. This is why RSA encryption and Diffie-Hellman exponentiation are efficient even when exponents are hundreds or thousands of bits.
Cryptographic hardness assumptions are not theorems that problems are impossible. They are carefully stated beliefs about the cost of algorithms. Factoring large semiprimes, computing RSA roots without the private exponent, and finding discrete logarithms in selected groups are believed hard for classical computers at appropriate parameters. Parameter choice depends on the best known algorithms.
The group viewpoint keeps the notation honest. Addition modulo always gives an abelian group, but multiplication modulo is a group only after restricting to invertible residues. When is prime, every nonzero residue is invertible, so . When , many nonzero residues are not invertible: every multiple of or has no inverse. RSA works inside this mixed arithmetic, and the trapdoor is precisely the factorization that reveals the group structure.
Finite fields extend the same ideas beyond prime moduli. AES, for example, uses arithmetic in a field with elements, represented by polynomials over modulo an irreducible polynomial. Elliptic-curve cryptography uses points whose coordinates lie in finite fields. The notes do not need the full field theory to introduce RSA and DH, but recognizing that "modular arithmetic" and "finite-field arithmetic" are structured algebra prevents treating them as arbitrary bit tricks.
Primality testing is also part of the public-key pipeline. RSA key generation needs large primes, and it is not feasible to prove primality by trial division. Probabilistic tests such as Miller-Rabin quickly reject composites and accept primes with high confidence when repeated with independent bases. The important distinction is between generating a random number that merely has no small factors and generating one that passes a strong primality test.
CRT is more than a hand-solving trick. In RSA decryption, computing directly is slower than computing and , where and , then recombining. This speedup is standard, but it introduces fault-attack concerns: if an attacker can induce an error in one branch and see the faulty result, the difference from a correct signature can reveal a factor of . Implementations add verification or countermeasures.
Finally, asymptotic efficiency matters because cryptographic numbers are large. The extended Euclidean algorithm, modular multiplication, modular exponentiation, and primality tests all run in time polynomial in the bit length. Factoring and discrete logarithm algorithms are much more expensive at recommended sizes. Public-key cryptography lives in that gap.
It is also important to distinguish a theorem from an algorithmic assumption. Fermat's little theorem, Euler's theorem, Lagrange's theorem, and CRT are proven mathematical facts. The claim that factoring a 2048-bit RSA modulus is infeasible for current classical adversaries is an assumption based on known algorithms and public analysis. Cryptographic systems combine both: the theorem proves correctness, while the assumption supports security.
Many attacks exploit parameters that technically satisfy a formula but violate the intended group setting. A generator may not have the claimed order. A modulus may be composite where a prime was expected. An RSA modulus may share a prime with another modulus if random generation fails. These are number-theory errors first and cryptographic disasters second, so implementations routinely validate parameters before using them.
Random sampling in number-theoretic sets must also be exact enough for the proof. Choosing a random exponent modulo , a random RSA prime, or a random group element with biased rejection logic can leak structure or reduce the search space. Textbooks often write in one symbol; implementations must turn that symbol into unbiased bytes, rejection sampling, and tests.
This is why cryptographic libraries expose high-level key-generation routines instead of asking every application to assemble primes, groups, exponents, inverses, and validation checks by hand.
The fewer raw number-theory choices an application makes, the smaller its chance of violating a hidden precondition.
Reviewable parameters are part of the security story.
Visual
| Tool | Computes or states | Cryptographic use |
|---|---|---|
| Euclidean algorithm | coprimality checks | |
| Extended Euclid | modular inverses | |
| Fermat/Euler theorem | exponent cycles | RSA correctness, primality tests |
| CRT | combine residues | faster RSA decryption/signing |
| Repeated squaring | RSA, DH, DSA, Schnorr |
Worked example 1: modular inverse with extended Euclid
Problem: compute .
Method:
- Run Euclid:
- Back-substitute:
- Substitute :
- Substitute :
- Reduce modulo 19:
Since , the inverse is .
Check:
Checked answer: .
Worked example 2: Chinese remainder theorem
Problem: solve:
Method:
- The modulus product is:
-
Write from the first congruence.
-
Substitute into the second:
- Reduce:
and , so:
- The inverse of modulo is , so:
- Take :
Check:
Checked answer: .
Code
def egcd(a: int, b: int):
if b == 0:
return (a, 1, 0)
g, x1, y1 = egcd(b, a % b)
return (g, y1, x1 - (a // b) * y1)
def invmod(a: int, n: int) -> int:
g, x, _ = egcd(a, n)
if g != 1:
raise ValueError("inverse does not exist")
return x % n
def crt_pair(a1, n1, a2, n2):
t = ((a2 - a1) * invmod(n1, n2)) % n2
return (a1 + n1 * t) % (n1 * n2)
print(invmod(7, 19))
print(crt_pair(4, 7, 3, 5))
Common pitfalls
- Assuming every nonzero residue has an inverse modulo . That is true modulo prime , not arbitrary .
- Using Fermat's little theorem with composite moduli.
- Forgetting that CRT uniqueness is modulo the product of pairwise coprime moduli.
- Confusing with when is not prime.
- Implementing modular exponentiation by computing the huge integer first.
- Treating hardness assumptions as proven lower bounds.