Chapter 07 // Primitives

Mathematical
Foundations

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.

Scroll to explore
01 // The Why

Everything after this chapter runs on algebra

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.

What structure does arithmetic need?

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.

+

Chapters 1-6: simple math

XOR for encryption, modular addition for nonces, comparison for verification. All operations you could do in your head.

{}

Chapters 8+: structured math

Modular exponentiation, elliptic curve point multiplication, polynomial arithmetic. All operations that require the structures this chapter builds.

02 // Groups

A set with one rule

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?

Starting from the need

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: (ab)c=a(bc)(a \star b) \star c = a \star (b \star c). 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.

The four axioms
1. Closure

Combining two elements always produces another element in the set.

2. Associativity

(ab)c=a(bc)(a \star b) \star c = a \star (b \star c)

3. Identity

There exists an element ee such that ae=ea=aa \star e = e \star a = a.

4. Inverse

For every aa, there exists a1a^{-1} such that aa1=ea \star a^{-1} = e.

A group you already know

Clock arithmetic

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 {1,2,,p1}\{1, 2, \ldots, p{-}1\} under multiplication mod a prime pp. This is written Zp\mathbb{Z}_p^*. 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 p1p - 1.

03 // Generators

One element that reaches everywhere

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?

Why the starting point matters

Take a number gg and repeatedly multiply it by itself mod pp: g1,g2,g3,g^1, g^2, g^3, \ldots Some starting values gg 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 Zp\mathbb{Z}_p^*, the group order is p1p - 1, so every element's order divides p1p - 1. A generator has order exactly p1p - 1. Explore this yourself in the demo below.

Group Explorer
Prime p
Element g

Colored = generators

123456
Generator

g=3g = 3 in Z7\mathbb{Z}_{7}^*

31 = 3 32 = 2 33 = 6 34 = 4 35 = 5 36 = 1 1

Order: 6 (visits 6 of 6 elements)

04 // Fields

A set with two rules

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.

Why one operation is not enough

RSA encryption computes memodnm^e \bmod n. To decrypt, you need a dd such that medmm^{ed} \equiv m. Finding dd requires solving ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, 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 0×x=00 \times x = 0 for everything) and multiplication distributes over addition: a×(b+c)=a×b+a×ca \times (b + c) = a \times b + a \times c. The rationals and the reals are fields. But computers need something finite.

05 // Finite Fields

Arithmetic that wraps around

Computers cannot do arithmetic on infinite sets. Keys have fixed sizes. Computation must terminate. Cryptography needs fields with a finite number of elements.

Why the modulus must be prime

Take the integers {0,1,2,,p1}\{0, 1, 2, \ldots, p{-}1\} and do arithmetic mod pp. Addition works for any modulus. But multiplication needs every nonzero element to have an inverse, and that only works when pp is prime.

Try mod 6: what is 212^{-1}? You need a number xx such that 2x1(mod6)2x \equiv 1 \pmod{6}. Check every possibility: 2×0=02 \times 0 = 0, 2×1=22 \times 1 = 2, 2×2=42 \times 2 = 4, 2×3=02 \times 3 = 0, 2×4=22 \times 4 = 2, 2×5=42 \times 5 = 4. None of them give 1. The element 2 has no inverse. The field breaks.

With a prime pp, this never happens. Every nonzero element has an inverse. The set {0,1,,p1}\{0, 1, \ldots, p{-}1\} with addition and multiplication mod pp is the finite field GF(p)GF(p), also written Fp\mathbb{F}_p.

Field Arithmetic
Prime modulus
Composite (broken)
a
op
b
Result
3+51(mod7)3 + 5 \equiv {\color{#00e5a0}1} \pmod{7}
06 // Finding Inverses

Undoing multiplication

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.

Two paths to the same answer

Fermat's little theorem says that in GF(p)GF(p), every nonzero element satisfies ap11(modp)a^{p-1} \equiv 1 \pmod{p}. Multiply both sides by a1a^{-1}:

ap2a1(modp)a^{p-2} \equiv a^{-1} \pmod{p}

So computing ap2modpa^{p-2} \bmod p 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 xx and yy such that ax+py=gcd(a,p)=1ax + py = \gcd(a, p) = 1. Reducing mod pp, that becomes ax1(modp)ax \equiv 1 \pmod{p}, so xx 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.

07 // Euler's Totient

Why RSA decryption works

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.

The question behind RSA

RSA encryption computes c=memodnc = m^e \bmod n where n=pqn = pq is the product of two large primes. To decrypt, you compute m=cdmodnm = c^d \bmod n. For this to work, you need medm(modn)m^{ed} \equiv m \pmod{n} for every message mm. How do you find dd?

Euler's totient function ϕ(n)\phi(n) counts the integers less than nn that share no common factor with nn. These are exactly the elements that have multiplicative inverses mod nn.

For a prime p
ϕ(p)=p1\phi(p) = p - 1

Every number from 1 to p1p{-}1 is coprime to pp. This is why GF(p)GF(p) has p1p - 1 invertible elements.

For n = p x q
ϕ(n)=(p1)(q1)\phi(n) = (p - 1)(q - 1)

The only numbers not coprime to nn are multiples of pp or qq. Computing this requires knowing the factorization.

The Fermat-Euler theorem

For any aa coprime to nn:

aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}

This generalizes Fermat's little theorem (which is the special case where nn is prime). Now the RSA connection becomes clear. If ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, then:

med=m1+kϕ(n)=m(mϕ(n))km1k=m(modn)m^{ed} = m^{1 + k\phi(n)} = m \cdot (m^{\phi(n)})^k \equiv m \cdot 1^k = m \pmod{n}

Decryption works because dd is the modular inverse of ee mod ϕ(n)\phi(n). And the trapdoor is this: computing ϕ(n)\phi(n) requires knowing pp and qq. Factoring nn to find them is the hard problem that makes RSA secure.

08 // Curves Over Finite Fields

From smooth curves to scattered points

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.

Why move to finite fields?

An elliptic curve over the real numbers is defined by y2=x3+ax+by^2 = x^3 + ax + b. 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 GF(p)GF(p), the same equation y2x3+ax+b(modp)y^2 \equiv x^3 + ax + b \pmod{p} defines a finite set of points where both xx and yy are integers mod pp. 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 pp is a 256-bit number, the curve has roughly pppoints (by Hasse's theorem), and the Elliptic Curve Discrete Logarithm Problem (given Q=kGQ = kG, find kk) is hard because there is no geometric shortcut in a finite field.

Elliptic Curves Over Finite Fields
Over real numbers (smooth)

Smooth curve with infinitely many points

Over GF(23) (discrete)
0011112222

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 22562^{256} points instead of 23.

09 // Binary Fields

Why AES uses XOR for multiplication

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.

Fields built from polynomials

Instead of integers mod a prime, you can build a field from polynomials with coefficients in {0,1}\{0, 1\}. An element of GF(28)GF(2^8) is a polynomial of degree at most 7 with binary coefficients, which is just a byte. For example, x6+x4+x2+x+1x^6 + x^4 + x^2 + x + 1 is 0x57\texttt{0x57} (binary 01010111\texttt{01010111}).

Addition is XOR. 1+1=01 + 1 = 0 in GF(2)GF(2), 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 x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1 (which is 0x11B\texttt{0x11B}). The irreducible polynomial plays the same role as the prime in GF(p)GF(p): 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 t1t - 1 is uniquely determined by ttpoints (Lagrange interpolation). This fact powers Shamir's secret sharing in Chapter 16: split a secret into nn shares (points on a polynomial) such that any tt of them reconstruct the secret, but t1t - 1 shares reveal nothing.

10 // What You Learned

Why every protocol needs algebra

Every concept in this chapter exists because a cryptographic protocol demanded it. None of these are arbitrary mathematical curiosities.

Chapter 8

Generators power Diffie-Hellman

The key exchange protocol picks a generator of Zp\mathbb{Z}_p^* so that the shared secret could be any group element. A non-generator would confine it to a smaller, breakable subgroup.

Chapter 9

Euler's totient unlocks RSA

RSA decryption works because dd is the modular inverse of ee mod ϕ(n)\phi(n). The trapdoor: computing ϕ(n)\phi(n) requires knowing the prime factors.

Chapters 8 & 10

Finite fields make curves computable

Elliptic curves over GF(p)GF(p) replace infinite precision with modular arithmetic. The algebra works the same, but now everything fits in 256 bits and the DLP is hard.

Chapter 5 & 16

Binary fields and polynomials

GF(28)GF(2^8)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.

Up Next

Chapter 08 // Key Exchange

Continue Learning