Number Theory for Cryptography
The number theory behind public-key cryptography, and the algorithms that limit it. Divisibility, gcd as the least positive combination (Bézout), modular arithmetic and inverses, groups (Lagrange-style g^{|G|} = 1, exponents mod the group order, e-th powers as permutations), Z*_N and Euler's φ, the Chinese remainder theorem and computing with it. Random primes via the prime number theorem, the Fermat test and Carmichael numbers, Miller–Rabin with strong witnesses (error 2⁻ᵗ). The factoring and RSA assumptions (RSA ≤ factoring; φ(N) or d reveal the factors). Cyclic groups, element orders, generators, discrete log, CDH and DDH, why prime-order groups (Pohlig–Hellman, generators, near-uniform DH values), quadratic-residue subgroups of Z*_p for safe primes, elliptic-curve groups with explicit addition laws. Applications: one-way functions from factoring, RSA one-way permutations, a discrete-log collision-resistant hash. Attacks: Pollard p−1, Pollard rho (N^{1/4}), quadratic sieve (2^{O(√(n log n))}), number field sieve; baby-step/giant-step (√q, generically optimal), Pohlig–Hellman, discrete log in Z_N is easy, index calculus; group-size comparison for Z*_p versus elliptic curves. Worked examples and exercises verified.
Corresponds to Katz & Lindell, Introduction to Modern Cryptography, Chapters 7–8.
Motivation: concrete hard problems
IntuitionWhy number theory now
- Completing the top-down story. Private-key cryptography came from PRFs and PRPs, which come from one-way functions. Here one-way functions finally get concrete candidates: hard mathematical problems that have been studied for centuries.
- Public-key cryptography needs structure. Private-key primitives can be built heuristically with no number theory. Public-key schemes have no unifying primitive and can’t (yet) be built “from scratch”: they rely on specific algebraic hardness, where some trapdoor makes a problem easy and its absence makes it hard.
Efficiency is measured in the bit length . An algorithm taking steps is exponential. Algorithmic number theory cares about both sides: what is efficiently computable (exponentiation, inverses, gcd) and what seems hard (factoring, discrete log).
| Assumption | Problem | Best known attack (classical) |
|---|---|---|
| Factoring | given , find | number field sieve, |
| RSA | given , find | factor |
| Discrete log in | given , find | index calculus / NFS, subexponential |
| Discrete log on elliptic curves | same, in | generic only, |
| CDH / DDH | compute / recognise | via discrete log |
Primes, divisibility and modular arithmetic
DefinitionDivisibility and primes
if for some integer . If and , then . A prime has only the divisors 1 and ; 1 is neither prime nor composite. Fundamental theorem of arithmetic: uniquely. Division with remainder: with , computable in polynomial time.
TheoremProposition 7.2 — Bézout
For positive , there exist integers with , and is the smallest positive such combination. Euclid’s algorithm computes , and extended Euclid computes , both in polynomial time.
ProofThe smallest positive combination divides everything
Let be the least positive element of . Any gives with and , so and . In particular and . Any common divisor divides , so is the greatest common divisor.
∎
TheoremConsequences (Propositions 7.3–7.4)
- If and , then . In particular, or . (Multiply by .)
- If , and , then .
DefinitionModular arithmetic
is the remainder, and iff . Congruence respects , , , so reduce first, then compute: . It does not respect division: , yet .
TheoremProposition 7.7 — Invertibility
is invertible modulo iff . The inverse is the in . Example: , so . Division by means multiplication by , and cancellation then works. Addition, multiplication, inversion and exponentiation (repeated squaring) mod are all polynomial time.
Groups
DefinitionGroup (Def. 7.9)
A set with a binary operation satisfying closure, an identity, inverses, and associativity; abelian if also commutative. The order is the number of elements. The identity and inverses are unique (Exercise 7.1). A subgroup is a subset that is a group under the same operation.
Notation: additive (, identity 0, inverse , repetition ) or multiplicative (, identity 1, inverse , repetition ). Results are stated multiplicatively; this is notation only.
ExampleGroups and non-groups
- under is a group; under it is not (2 has no inverse).
- under is a group.
- under addition mod : order , inverse .
- Cancellation (Lemma 7.13): .
TheoremTheorem 7.14 —
In a finite group of order , for every .
ProofMultiplying by permutes the group (abelian case)
With , the elements are distinct by cancellation, so they are again. Then , and cancelling gives .
∎
TheoremCorollaries 7.15 and 7.17 — Exponent arithmetic
- Work mod in the exponent: . Additively, .
- -th powers are permutations: if , then is a bijection with inverse , , since . This is RSA’s engine.
The group and Euler’s
Definition (Proposition 7.18)
is an abelian group under multiplication mod . (Closure: a prime dividing and would divide or .) Its order is Euler’s phi function .
FormulaEuler’s (Theorem 7.19)
For : from , remove the multiples of and the multiples of . Example: has , and .
TheoremCorollaries 7.21–7.22 — Euler and Fermat
for , and (Fermat). If , then is a permutation of with inverse , . This even holds on all of for squarefree (Exercise 7.8: apply CRT; modulo either , giving , or use Fermat with ).
The Chinese remainder theorem
DefinitionIsomorphisms and direct products
An isomorphism is a bijection with (): the same algebraic structure in a different representation. has the componentwise operation.
TheoremTheorem 7.24 — Chinese remainder theorem
If with , then
It extends to any pairwise-coprime factorisation.
ProofInjective plus equal sizes
Units map to units: . Injective: means , so (Proposition 7.4), and . Same size: , so is a bijection. Homomorphism: (Exercise 7.6, since ), and likewise for .
∎
Example (Example 7.25)
, , , , , , , : every pair appears exactly once. Then , and indeed .
AlgorithmConverting back from (when are known)
- Extended Euclid: .
- and (precompute these once).
- .
Example (): gives and , so . Check: and .
ExampleExponentiation through CRT (Examples 7.29–7.30)
- : , so .
- : . Reduce exponents mod the group orders, and , giving .
Splitting into small moduli and reducing exponents mod and makes RSA decryption about 4× faster (RSA-CRT). It needs the factorisation, which only the key owner has.
Primes, factoring and RSA
Generating random primes
WarningRandom products are easy to factor
In “weak factoring” (multiply two random -bit integers), is even with probability , and small prime factors are common. The hardest numbers to factor have only large prime factors, so the factoring assumption uses random -bit primes, which must be efficiently generatable.
AlgorithmRandom -bit prime (Algorithm 7.34)
Repeat up to times: with (so the length is exactly ); return if Miller–Rabin with parameter says “prime”.
- Density (prime number theorem, weak form): at least of the -bit numbers are prime, so each try succeeds with probability at least . After tries, failure is at most .
- Testing: one-sided randomised tests (a prime is always called prime). A deterministic polynomial test exists (AKS, 2002) but is slower, so randomised tests are used in practice.
- Special forms (, safe primes ): modified generators work in practice, though their analyses rely on density conjectures.
Primality testing: Miller–Rabin
AlgorithmFermat test (first attempt, Algorithm 7.35)
Pick . Output “composite” if or (a witness), and repeat times.
LemmaLemmas 7.36–7.37 — subgroups are small
In a finite group, a nonempty subset containing 1 and closed under the operation is a subgroup (inverses come from ). A strict subgroup has at most half the elements: a coset is disjoint from and the same size.
TheoremTheorem 7.38 — one witness means many
If any witness exists, the non-witnesses form a strict subgroup of , so at least half of are witnesses. Each round then catches with probability at least , giving error at most .
WarningCarmichael numbers
Infinitely many composites (for example 561) have no Fermat witnesses at all. The Fermat test is not enough.
DefinitionStrong witnesses
Write with odd and look at (each the square of the previous). is a strong witness if and for . A non-strong-witness’s sequence looks like or . Every Fermat witness is a strong witness.
TheoremPrimes have no strong witnesses (Lemma 7.39)
Modulo an odd prime, the only square roots of 1 are : forces . In the sequence ending at , the term just before the first 1 is a square root of 1 other than 1, hence .
TheoremTheorem 7.40 — Many strong witnesses for odd composites that are not prime powers
Let be the largest index for which some non-strong-witness has ( exists since ). Define .
- It contains all non-strong-witnesses.
- It is a subgroup (closed under products).
- It is strict: write with coprime factors. Take with and set . Then , which is neither nor .
So at least half of are strong witnesses.
AlgorithmMiller–Rabin (Algorithm 7.44; Theorem 7.33)
Even : composite. Perfect power: composite (check for by binary search; Exercise 7.9). Otherwise write and repeat times: random ; “composite” if or is a strong witness. A prime always passes; a composite passes with probability at most .
The factoring and RSA assumptions
DefinitionFactoring assumption (Def. 7.45)
outputs with and -bit primes. Factoring is hard relative to it if every ppt , given , outputs with only with negligible probability.
DefinitionRSA assumption (Def. 7.46)
outputs with and (Algorithm 7.47: generate a modulus, choose , set ). RSA is hard if, given and uniform , every ppt finds with only with negligible probability.
ImportantRSA is no harder than factoring, and maybe easier
- Trapdoor: knowing gives , then , then . So hard RSA implies hard factoring.
- Equivalences: computing is as hard as factoring (Exercise 7.11: and , so are roots of ). Knowing also reveals the factorisation (Exercise 7.12 for : with , so try both).
- Gap: RSA might be solvable without computing or , so “RSA ⟺ factoring” is unproven.
Pure factoring lacks a convenient trapdoor structure for building schemes; RSA’s asymmetry (easy with the trapdoor , hard without) is what public-key schemes need.
Cyclic groups and discrete-log assumptions
DefinitionOrder, generators, cyclic groups
is the subgroup generated by . The order of is the least with , and . is cyclic if some (a generator) has order . Cyclic groups usually have several generators.
TheoremPropositions 7.49–7.52 and Theorem 7.53 — Orders
- .
- divides (Lagrange for cyclic subgroups).
- Prime order ⇒ cyclic, and every non-identity element is a generator.
- is cyclic for prime (but has non-prime order ).
ExampleOrders in practice
- (additive): 1 and 2 generate; 3 has order 5 (); 10 has order 3. Both orders divide 15.
- : , order 4, which divides 8.
- : , but , so 3 is a generator.
WarningIsomorphic is not computationally equivalent
Every cyclic group of order is isomorphic to via . But evaluating that isomorphism backwards is the discrete-log problem. Discrete log is trivial in and believed hard in or on elliptic curves: hardness depends on the representation.
Discrete log, CDH and DDH
DefinitionDiscrete logarithm (Def. 7.59)
outputs a cyclic group of order () with generator and efficient operations. is the unique with ; it satisfies . Hard if, given for uniform , every ppt algorithm finds only with negligible probability.
DefinitionDiffie–Hellman problems
.
- CDH: compute from .
- DDH (Def. 7.60): distinguish from : .
Hierarchy: DL easy ⇒ CDH easy ⇒ DDH easy, so hard DDH ⇒ hard CDH ⇒ hard DL (Exercises 7.13–7.14). The converses are believed false: some groups have hard DL and CDH but easy DDH (for example itself, via quadratic residuosity).
ImportantWhy prime-order groups
- Discrete log is hardest there. Pohlig–Hellman reduces order to orders and .
- Generators are free. Every non-identity element generates.
- DH values are almost uniform (Proposition 7.61). For random , (since iff a factor is 0, as is prime), and for (unique ). That is within negligible distance of uniform.
This is heuristic support only: prime order is neither necessary nor sufficient for DDH hardness.
AlgorithmQuadratic residues modulo a safe prime (Algorithm 7.62)
Squaring mod is 2-to-1, so the quadratic residues are half of and form a subgroup. For a safe prime with prime, has prime order , so DDH is believed hard there. Generate , pick , and output , a non-identity residue and hence a generator.
Elliptic-curve groups
DefinitionElliptic curve over ()
with (no repeated roots). is the set of solutions together with the point at infinity . Picture the real curve, with at the top of every vertical line. A line meets the curve in exactly 3 points (counting tangency twice and for vertical lines).
ExamplePoints on (Example 7.63)
takes the values . The quadratic residues mod 7 are .
- gives the single point ;
- gives and ;
- gives and .
With , that is 6 points.
FormulaGroup law (Claims 7.64–7.65)
Rule: for collinear points, is the identity, and (reflect). For and , neither :
- : ;
- doubling, with : ;
then
(with in the doubling case). If with , or with , the sum is . This is an abelian group; associativity is tedious to verify directly and follows naturally from algebraic geometry.
NoteWhy elliptic curves
No subexponential discrete-log algorithm is known for well-chosen curves, only generic ones. In practice, parameters are chosen so that (or a subgroup) has prime order. Schemes in this book treat the group generically (“any cyclic group where DDH is hard”), so the curve only matters at implementation time.
Applications
TheoremTheorem 7.68 — Factoring ⇒ one-way function
Let Gen use random bits. runs , using as the random tape with such that , and outputs . A uniform gives exactly Gen’s distribution of moduli, and any preimage reveals the factors (rerun Gen on ). Hence hard factoring ⇒ one-way , and hard RSA ⇒ one-way too.
TheoremConstruction 7.71 — The RSA one-way permutation family
: from GenRSA; ; Samp is uniform on ; . One-way iff RSA is hard, and a trapdoor permutation given . Discrete log also gives a one-way permutation family: maps bijectively onto (Exercise 7.17).
TheoremTheorem 7.73 — A discrete-log collision-resistant hash
With prime-order and random as the public key,
is collision-resistant if discrete log is hard. (Evidence suggests collision resistance can’t be built from arbitrary OWFs or OWPs, so number theory is needed for provable CRHFs.)
ProofA collision reveals
gives . Here , otherwise too and the inputs would be equal. Since is prime, is invertible, so . (If , output 0.) The reduction’s view is exactly the real key distribution, so its success equals the collision probability.
∎
ExampleToy instance and compression (Example 7.74, Exercise 7.16)
, has order , , . An 8-bit input splits into two 4-bit numbers: . Then , so . (Checked by hand.) Inputs are bits and outputs bits (elements of with ), so the hash compresses once . Merkle–Damgård then extends it to arbitrary length (Theorem 4.12).
ExampleAn RSA-based CRHF (Exercise 7.18, Construction 7.75)
Key ; for , with and . Strip equal outer layers until the first differing bit, where . Then , so is the -th root of : a collision solves RSA. (Sketch.)
Algorithms for factoring (Chapter 8)
NoteWhy attacks matter
Parameter sizes come from the best known algorithms: a scheme meant to last 5 years needs large enough that the best factoring algorithm takes longer than that. Trial division takes , so anything interesting must beat that.
Pollard’s method
AlgorithmPollard (Algorithm 8.1)
Find with . Then but , so .
- Choose with but , for example over small primes.
- Set . Modulo : (Fermat). Modulo : whenever has order not dividing , for example when it is a generator, which happens with probability .
It works exactly when is smooth (all prime factors small) and is not. Defence: strong primes ( prime). Today’s consensus is that the cost isn’t worth it, since general-purpose algorithms are better anyway, though some schemes need strong primes for structural reasons.
Pollard’s rho method
AlgorithmPollard rho (Algorithm 8.3)
Look for a good pair: but . Then .
- Birthday: random elements contain a collision mod with constant probability (about 1/4), but almost surely none mod . Testing all pairs would cost , no better than trial division.
- Iterate , for example with . The sequence mod is eventually periodic (a “ρ” shape). Claim 8.2: if with , then some has , namely the first multiple of the period at least . So test only (Floyd cycle-finding, as in the low-memory birthday attack).
Heuristically , about : general-purpose but still exponential. The sequence isn’t random, so a rigorous analysis is open.
The quadratic sieve
IntuitionCongruence of squares
If with , then but divides neither factor, so is a prime factor. For , each square has four square roots, so a random “equal squares” pair is nontrivial with probability about .
AlgorithmQuadratic sieve (high level)
- Collect relations. Fix a factor base of small primes. Find values with -smooth: . (The sieving that finds them is omitted.)
- Linear algebra mod 2. The exponent-parity matrix (, ) has a subset of rows summing to 0 over . The product of those relations is then .
Running time : subexponential. Fastest until the early 1990s, still best up to about 300-bit . The general number field sieve is fastest today: heuristically .
ExampleFactoring (Example 8.4)
| factorisation | ||
|---|---|---|
| 620 | 6647 | |
| 621 | 7888 | |
| 645 | 38272 | |
| 655 | 51272 |
The product is , all exponents even. So , i.e. with . Then , and . (Squares and factorisation checked by hand.)
Algorithms for discrete log (Chapter 8)
NoteGeneric versus specific
Generic algorithms use only group operations (and the order ). Specific ones exploit the representation. Brute force is . Generic algorithms provably need (Nechaev; Shoup), and baby-step/giant-step meets this, but the bound says nothing about particular groups.
Baby-step/giant-step
AlgorithmShanks’ baby-step/giant-step (Algorithm 8.5)
With :
- Giant steps: compute and sort , which mark the “circle” at spacing .
- Baby steps: for , compute and look it up. Since lies in some gap, for some , giving .
Time and memory . Pollard’s rho for discrete log achieves the same time with constant memory.
Example in (Example 8.6)
, . Giant steps: . Baby steps: for gives . The match gives . Check: .
Pohlig–Hellman
AlgorithmPohlig–Hellman
With pairwise coprime and : since (Lemma 8.7),
in a group of order . Solve each subproblem (for example with baby-step/giant-step) and combine by CRT. Time . Prime powers reduce further, digit by digit in base (Exercises 8.1–8.2), to , where the are the primes dividing . Smooth group order ⇒ easy discrete log, hence prime-order groups.
Example in (Example 8.8)
.
- , so (16 has order 5);
- , so ;
- , so is odd.
CRT gives , and indeed .
Discrete log in is easy
ExampleRepresentation matters
In additive , "" means solving . With , . With any generator , , so . This uses multiplication, a second operation beyond the group law. For non-generators, divide by , solve mod , and lift to solutions (Exercise 8.3).
Index calculus
AlgorithmIndex calculus in
- Precompute (needs only ; reusable): pick a factor base and find exponents with smooth. Each gives a linear relation .
- Per target : find with smooth, giving . Solve the linear system (care is needed because is composite).
Time , the same as the quadratic sieve; the NFS variant reaches . Specific to (smoothness of integer representatives). There is no analogue for good elliptic curves.
Example in (Example 8.9)
, , , and . So , , and . That gives ; checking gives 29. Then . (Relations checked by hand.)
ImportantWhy elliptic curves are smaller
A 512-bit : NFS costs about , roughly –. A prime-order elliptic curve with costs about by the best (generic) attack. Comparable security with far smaller groups, hence faster honest operations. (Modern guidance: about 3072-bit RSA or versus about 256-bit curves for 128-bit security. 512-bit Diffie–Hellman was actually broken in practice by the 2015 Logjam attack. And all of factoring, discrete log and EC discrete log fall to Shor’s quantum algorithm.)
Exercises (selected)
ExampleShort answers
- 7.2: is closed, contains 1, but has no inverses. Proposition 7.36 needs finiteness.
- 7.3: is a subgroup in finite groups (inverses ). In infinite groups needn’t be: lacks .
- 7.6: , but needn’t equal : take and -free cases, e.g. , , gives versus .
- 7.9: if with , then . For each , binary-search , giving polynomial time overall.
- 7.11–7.12: see the RSA box above.
- 8.3: with forces and with . Invert, then the lifts are all solutions.
(Answers worked here.)
Quick reference
| Object | Statement | Note |
|---|---|---|
| Bézout | extended Euclid, polynomial time | |
| Inverse mod | exists iff | |
| exponents mod | -th power permutes if | |
| ; | Euler/Fermat | |
| CRT | ||
| Prime generation | random -bit + Miller–Rabin, tries | density |
| Miller–Rabin | strong witnesses ≥ half for non-prime-power composites | error |
| Factoring / RSA | find / find | RSA ≤ factoring; or ⇒ factors |
| Orders | divide ; prime order ⇒ every non-identity element generates | |
| DL, CDH, DDH | DDH hard ⇒ CDH hard ⇒ DL hard | prime-order groups preferred |
| Safe-prime subgroup | , , order | |
| Elliptic curves | chord-and-tangent law, identity | only generic attacks |
| Applications | factoring ⇒ OWF; RSA ⇒ OWP family; DL ⇒ CRHF | |
| Pollard | smooth | |
| Pollard rho | ||
| Quadratic sieve | smooth relations + GF(2) linear algebra | |
| NFS | factoring and DL in | |
| BSGS | giant steps , baby steps | , generically optimal |
| Pohlig–Hellman | CRT over the factors of the order | |
| Index calculus | smooth relations for of the factor base | subexponential, only |
Facts to keep to hand
- Modular arithmetic is efficient; structure gives trapdoors. gcd, inverses and exponentiation are polynomial time. Exponents live modulo the group order, so knowing (equivalently the factorisation) inverts . RSA’s trapdoor is exactly that asymmetry, and CRT makes computing with known factors fast.
- Primes are plentiful and testable. About of -bit numbers are prime, and Miller–Rabin’s strong witnesses defeat Carmichael numbers, giving error . So random moduli and safe-prime groups are easy to generate.
- Cyclic groups host discrete log, CDH and DDH. Prefer prime order (Pohlig–Hellman, easy generators, near-uniform DH values): safe-prime residue subgroups of , or elliptic curves where only generic attacks are known. Hardness depends on representation, not abstract structure.
- Known attacks set the key sizes. Factoring and discrete log in fall to subexponential sieves (smooth relations plus linear algebra), while elliptic-curve discrete log resists all but generic attacks. That is why curves achieve the same security with much smaller parameters.
References
- Katz & Lindell, Introduction to Modern Cryptography — Chapters 7–8
- Shoup, A Computational Introduction to Number Theory and Algebra (2005)
- Crandall & Pomerance, Prime Numbers: A Computational Perspective (2005)
- Washington, Elliptic Curves: Number Theory and Cryptography (2008)