Chapters 1 through 6 used math you already had: XOR, counting, modular arithmetic for nonces. Starting with key exchange, every protocol depends on a specific kind of arithmetic with specific properties. This chapter builds that arithmetic from scratch.
Symmetric cryptography hides data behind a shared key. Public-key cryptography does something stranger: it creates mathematical relationships where one direction is easy and the reverse is practically impossible. That trick requires a very particular kind of arithmetic.
Think about what cryptography actually requires. You need to combine numbers in a way that is predictable (the same inputs always produce the same output). You need the result to stay within a fixed range (keys have fixed sizes). And you need to be able to undo every operation (if you encrypt, you must be able to decrypt).
These are not arbitrary requirements. They are constraints that the math itself must satisfy. This chapter identifies the minimal structures that meet those constraints. Once you see why each structure exists, the protocols in the next chapters will feel inevitable rather than mysterious.
XOR for encryption, modular addition for nonces, comparison for verification. All operations you could do in your head.
Modular exponentiation, elliptic curve point multiplication, polynomial arithmetic. All operations that require the structures this chapter builds.
Cryptography needs operations that can be undone. You encrypt, you must be able to decrypt. You sign, someone must be able to verify. What is the absolute minimum structure an operation needs to be reliably reversible?
Imagine you have a set of values and one operation that combines any two of them. For this to be useful in cryptography, you need four things. First, the result of combining two values must stay in the set (you can not produce a key that is outside your key space). Second, the order of grouping does not matter: . Third, there must be a neutral element that does nothing (an identity). Fourth, every element must have a partner that undoes it (an inverse).
A set with an operation satisfying these four properties is called a group. These are not arbitrary axioms. They are the consequences of needing reversibility.
Combining two elements always produces another element in the set.
There exists an element such that .
For every , there exists such that .
Take the numbers 0 through 11 and add them mod 12. You already use this group every time you read a clock: 9 + 5 = 2 (because 14 mod 12 = 2).
Closure: any sum mod 12 stays in {0,...,11}.
Identity: 0 is the neutral element.
Inverse: the inverse of 5 is 7, because 5 + 7 = 12 = 0 mod 12.
The group that matters for cryptography is not clock addition. It is the set under multiplication mod a prime . This is written . Its identity element is 1, and every element has a multiplicative inverse (we will see why shortly). The order of this group (the number of elements) is .
In Diffie-Hellman, Alice and Bob start from a single public number and generate their keys by raising it to secret powers. How can one number reach every possible key? And what happens if it can not?
Take a number and repeatedly multiply it by itself mod : Some starting values will eventually visit every element in the group before cycling back. These are called generators. Others only reach a subset and loop back early.
If Diffie-Hellman uses a generator, the shared secret could be any element in the group, and an attacker must search the entire space. If it uses a non-generator, the secret is confined to a smaller subgroup, and the attacker's job gets easier. A group where a single element generates everything is called a cyclic group. The order of an element is the number of distinct values it visits before returning to 1.
Lagrange's theorem: the order of any element always divides the order of the group. In , the group order is , so every element's order divides . A generator has order exactly . Explore this yourself in the demo below.
Colored = generators
in
Order: 6 (visits 6 of 6 elements)
A group gives you one reversible operation. But RSA needs multiplication to encrypt and addition on exponents to construct the decryption key. Elliptic curves need both point addition and scalar multiplication. One operation is not enough.
RSA encryption computes . To decrypt, you need a such that . Finding requires solving , which uses addition, subtraction, multiplication, and division on exponents. You need a set where two operations both work and play nicely together.
A field is exactly that: a set with addition and multiplication where both form groups (except that 0 has no multiplicative inverse, since for everything) and multiplication distributes over addition: . The rationals and the reals are fields. But computers need something finite.
Computers cannot do arithmetic on infinite sets. Keys have fixed sizes. Computation must terminate. Cryptography needs fields with a finite number of elements.
Take the integers and do arithmetic mod . Addition works for any modulus. But multiplication needs every nonzero element to have an inverse, and that only works when is prime.
Try mod 6: what is ? You need a number such that . Check every possibility: , , , , , . None of them give 1. The element 2 has no inverse. The field breaks.
With a prime , this never happens. Every nonzero element has an inverse. The set with addition and multiplication mod is the finite field , also written .
In GF(7) you can try all seven elements to find an inverse. In GF(p) with a 256-bit prime, that would take longer than the age of the universe. You need a fast algorithm.
Fermat's little theorem says that in , every nonzero element satisfies . Multiply both sides by :
So computing gives you the inverse. This is elegant and easy to implement using fast exponentiation (repeated squaring).
The Extended Euclidean Algorithm takes a different approach. It finds integers and such that . Reducing mod , that becomes , so is the inverse. This is what most cryptographic libraries actually use because it avoids the cost of exponentiation.
Both methods produce the same result. Fermat's little theorem is conceptually simpler. The Extended Euclidean Algorithm is faster in practice. You can try both in the inverse-finder mode of the demo above.
RSA encryption raises a message to a power: m^e mod n. To decrypt, you need another exponent d that undoes the first one. Finding d requires counting how many numbers are invertible mod n.
RSA encryption computes where is the product of two large primes. To decrypt, you compute . For this to work, you need for every message . How do you find ?
Euler's totient function counts the integers less than that share no common factor with . These are exactly the elements that have multiplicative inverses mod .
Every number from 1 to is coprime to . This is why has invertible elements.
The only numbers not coprime to are multiples of or . Computing this requires knowing the factorization.
For any coprime to :
This generalizes Fermat's little theorem (which is the special case where is prime). Now the RSA connection becomes clear. If , then:
Decryption works because is the modular inverse of mod . And the trapdoor is this: computing requires knowing and . Factoring to find them is the hard problem that makes RSA secure.
The next chapter shows elliptic curves as beautiful smooth shapes. But real numbers have infinite precision, which computers cannot handle. Real implementations use curves over finite fields, and the picture changes completely.
An elliptic curve over the real numbers is defined by . It produces a smooth curve, and you can define point addition geometrically: draw a line through two points, find the third intersection, reflect over the x-axis. Beautiful, but impractical for computers.
Over , the same equation defines a finite set of points where both and are integers mod . The smooth curve becomes a scattered cloud of discrete points. The geometry vanishes, but the algebra (the point addition formulas) works exactly the same way.
This is what P-256 actually computes. The prime is a 256-bit number, the curve has roughly points (by Hasse's theorem), and the Elliptic Curve Discrete Logarithm Problem (given , find ) is hard because there is no geometric shortcut in a finite field.
Smooth curve with infinitely many points
23 discrete points (including ∞). Click two to add them.
Click a point on the finite field curve to select it.
This is what P-256 actually computes. The same equation, but with a 256-bit prime. The scattered dots you see here are the same structure that secures every TLS connection, just with roughly points instead of 23.
GF(p) uses integer arithmetic mod a prime. AES needs something different: field operations on bytes where addition is XOR, fast, carry-free, and constant-time.
Instead of integers mod a prime, you can build a field from polynomials with coefficients in . An element of is a polynomial of degree at most 7 with binary coefficients, which is just a byte. For example, is (binary ).
Addition is XOR. in , so adding coefficients is the same as XOR-ing the bytes. No carries, no branches, constant time.
Multiplication is polynomial multiplication modulo an irreducible polynomial. AES uses (which is ). The irreducible polynomial plays the same role as the prime in : it ensures every nonzero element has a multiplicative inverse. This is what the MixColumns step of AES actually computes.
Polynomials and interpolation: a polynomial of degree is uniquely determined by points (Lagrange interpolation). This fact powers Shamir's secret sharing in Chapter 16: split a secret into shares (points on a polynomial) such that any of them reconstruct the secret, but shares reveal nothing.
Every concept in this chapter exists because a cryptographic protocol demanded it. None of these are arbitrary mathematical curiosities.
The key exchange protocol picks a generator of so that the shared secret could be any group element. A non-generator would confine it to a smaller, breakable subgroup.
RSA decryption works because is the modular inverse of mod . The trapdoor: computing requires knowing the prime factors.
Elliptic curves over replace infinite precision with modular arithmetic. The algebra works the same, but now everything fits in 256 bits and the DLP is hard.
gives AES constant-time byte operations. Polynomial interpolation gives Shamir's secret sharing information-theoretic security.
You now have the vocabulary. Groups give reversible operations. Fields give two of them. Finite fields make them computable. Generators, totients, and curves are not arbitrary design choices. Each exists because the algebra demands it. The next chapter puts it all to work.