Discrete math for cryptography
Note: I wrote this for UBC’s CPSC 436S (cybersecurity) course when I got to TA it in 2024. It was put here to test the MathML-generating abilities of the script that compiles this site. Since it also works as a general page about the basics of cryptography-related math, it lives here now.
Unfortunately, the UBC computer science curriculum doesn’t include much discrete
math, apart from the intro in CPSC 121. This page will try to fill in the
gap—
Computational hardness (as motivation)
Modern cryptography depends on the existence of one-way functions. Informally speaking, these are functions where finding the output from an input is easy (can be done in polynomial time), but finding the input given an output is hard.
You might find it concerning that no one knows if one-way functions actually exist. If someone did prove they exist, then we would know P != NP. We do the next best thing by using things that look like one-way functions and praying. The two hopefully-one-way functions we cover in class are:
- Factoring: If a number has large prime factors, there is no quick way to find them. But given a list of factors, we can find their product in polynomial time.
- The discrete logarithm problem: Given and in a finite group, finding an exponent such that can be hard, where finding given and is easy.
The above terminology will need some explaining. Along with relevant definitions and notation, this page offers:
- An explanation of these hard problems
- Properties of abstract mathematical objects used in cryptography
- Properties of the not-abstract integers and modular arithmetic, also used in cryptography
Primes and factoring
Recall that a prime number is an integer greater than 1 which can’t be factored further; its only divisors are one and itself.
For any number, we can get its list of prime factors by repeated division. Let be a function that does this, which takes a positive integer and produces an orderless array of its prime factors, so .
It is clear that every list of prime numbers corresponds to exactly one product (multiply them all together and you’ll get the same result every time). Notably, the reverse is also true: every number corresponds to exactly one unordered list of factors. This uniqueness of a number’s prime factorization is known as the fundamental theorem of arithmetic. If you are familiar with the term, we can say our function is a bijection between the positive integers and the set of unordered lists of primes.
Every whole positive number* is either prime (i.e. has length 1) or composite ( has length ). Two numbers are coprime or relatively prime if they share no factors at all.
*When we’re talking about multiplication, 1 is a more of a no-op than a number, so it doesn’t count as a number for this sentence. It is neither composite nor prime; you can think of to have length 0. More on 1 being the identity of multiplication later.
To do anything interesting with our numbers, we need to define the rules they should follow.
Fields
Roughly speaking, a field is a set where math () works in the usual way. Precisely, a field is a set along with two functions ("addition") and ("multiplication") where the following rules hold:
- Closure: The given operations on elements of should never "escape" the set. is in and is in for any and in .
- Distributive property: distributes over , so
- Addition is associative: for every choice of in .
- Addition is commutative: for every in .
- Additive identity: There is some element (call it ) in where adding it is a no-op. That is, for any in . You may recognize to be 0 in familiar fields.
- Additive inverse: For any in , there is an "inverse" element such that adding it brings you back to the identity: ( )
- Multiplication is also associative
- Multiplication is also commutative
- Multiplicative identity: There is some in such that for any in . You may recognize this to be 1 in familiar fields.
- Multiplicative inverse: For any in with the exception of , there is an "inverse" element in such that
Usually these rules are combined for brevity, but we’ll keep the list long so we can choose ones to cross out later. If all of these are true for your choices of , then you have yourself a field!
Exercises
Is
(with normal addition and multiplication) a field?
No.
Which rule does it break from above?
Number 9. There is no integer
such that
, so 3 has no multiplicative inverse in
. Is
(the rational numbers) under normal addition and multiplication a field?
Yes.
Finite fields and modular arithmetic
By defining all the rules we needed to do arithmetic, we can find another field to do arithmetic in. The field we define next will also get us closer to a real explanation of the discrete log problem mentioned above.
Sort the set of all integers into 5 buckets (congruence classes) depending on the result of taking the integer modulo 5. We can denote to mean the congruence class 1 goes in, so and
Then by defining addition as and multiplication as , all rules hold! (proof of 0-8 left as an exercise to the reader).
The least intuitive thing here is finding the multiplicative inverse of each congruence class. If we were dealing with the rational numbers, finding this inverse is as easy as swapping the numerator and denominator. In , the multiplicative identity is , and we can see , , and , so we know , , and . So everything but is invertible. This means along with modular addition and multiplication is indeed a field.
Side note
Finding multiplicative inverses in larger fields can be done in polynomial time using the extended Euclidean algorithm. If you’re using a newer version of Python,
pow(x, -1, p)
will find the inverse of
x
mod p
.
Exercises
What are all the multiplicative inverses in
?
and
and
since (
) What are all the additive inverses in
?
and
and
and
(rhetorical question) The elements of
seem to pair up very nicely; every element has a unique inverse. Are the additive and multiplicative inverses always like this?
Yes. This is a property we get as a reward for defining rules.
Another fun property of fields: given a natural number , there is either exactly one or zero finite fields with elements (having order ). This is not true of other mathematical objects; for example, there can be many dissimilar groups of the same order. The finite field of size exists if and only if is a prime power: for some prime and positive integer . Since there is only one finite field of a given order, we call it "the Galois field of order ," or . Simple modular math allows us to understand fields where .
When we do math in with non-prime , we run into issues with invertibility. Take and attempt to find the multiplicative inverse of : no matter what, makes the product even, so getting to is impossible. So isn’t a field, but the remaining properties are worth noting.
Rings
A ring is a set and two operations ( and ) where the following rules hold:
- Closure: The given operations on elements of should never "escape" the set. is in and is in for any and in .
- Distributive property: distributes over , so
- Addition is associative: for every choice of in .
- Addition is commutative: for every in
- Additive identity: There is some element (call it ) in where adding it is a no-op. That is, for any in .
- Additive inverse: For any in , there is an "inverse" element such that adding it brings you back to the identity: ()
- Multiplication is associative
Multiplication is commutative (click to see note)
Since we’re talking about modular arithmetic, this will be true anyway, but we don’t need commutative multiplication in a ring (for example, the set of 2x2 matrices with integer entries form a non-commutative ring).- Multiplicative identity: There is some in such that for any in .
- Multiplicative inverse: For any in with the exception of , there is an "inverse" element in such that
Exercises
Is (with normal addition and multiplication) a ring?
Yes.Which elements of have no multiplicative inverse?
(of course), , , , , .If we choose a non-prime for , we don’t get a field, but we still get a ring as a participation medal.
More modular arithmetic: Euler’s Totient
You might notice something in common between the troublesome elements of with no multiplicative inverse: they share common factors with 10. The invertible elements, on the other hand, are coprime to 10. This count of coprime-to- numbers that are smaller than is denoted by , where is Euler’s totient function. This function is quick to calculate, but only if we know the factors of .
Exercises
What is
for some prime
?
The only numbers that share factors with
are multiples of
, which are too big to be counted by the totient. Hence
is
. What is
for some prime
and
? (Hint: how many multiples of
are less than
?)
. What is
where
and
are different primes?
There are
numbers less than
.
of these are multiples of
(so
,
, ...,
), and similarly,
multiples of
. They are coprime, so no overlap between their multiples until
. So
.
By removing the non-invertible numbers (including 0) from , we can reinstate rule 9, but we need to ignore addition, or else we could produce a sum that is one of our removed elements (breaking rule 0). Our result has elements and is something called a group.
Groups
A group is a set with one operation, , ("multiplication") where the following rules hold:
- Closure: The given operations on elements of should never "escape" the set. is in and is in for any and in .
- Distributive property: distributes over , so
- Addition is associative: for every choice of in .
- Addition is commutative: for every in
- Additive identity: There is some element (call it ) in where adding it is a no-op. That is, for any in .
- Additive inverse: For any in , there is an "inverse" element such that adding it brings you back to the identity: ( )
- Multiplication is associative
Multiplication is commutative (click to see note)
As with rings, we don’t need commutativity for a group, but since we’re talking about integers modulo , our multiplication is commutative. Commutative groups are usually called abelian. Usually when you see an introduction to groups, they’ll provide a non-abelian example like the group of rotations and reflections of a polygon (), the 2x2 matrices with real-number entries and nonzero determinant (), the group of moves on the rubik’s cube, etc.- Multiplicative identity: There is some in such that for any in .
- Multiplicative inverse: For any in with the exception of , there is an "inverse" element in such that
We call "the multiplicative group of integers modulo ." It is a handy piece of notation that specifies both the operation and the set we’re working with. Our example would contain elements: , and .
Exercises
We named our operations arbitrarily and didn’t specify what they did,
other than requiring an identity and an inverse. Would using rules 0, 2, 4,
and 5 (deleting the rest) also give us a group?
Yes! The field rules give us two copies of the group rules, one using addition, and one using multiplication over the nonzero elements. A short definition of a field is a set that is an abelian group under +, where nonzero elements are an abelian group under
, and the distributive law holds, which would be a pretty unhelpful definition to put at the top of this page.
The discrete log problem
Fix and in some group. What integer makes ? In the group , there is no easy way to find (assuming was chosen well).
Diffie-Hellman key exchange works in any group where finding the discrete log is hard. In 436S, we only discuss it using the group , but elliptic curve Diffie-Hellman uses a group obtained from points on an elliptic curve in a finite field. RSA operates in the group of integers mod under multiplication (so ).
Exercises
Let
be prime and call the additive group of integers modulo
. Let
and
be elements of it. Why is finding an integer
such that
NOT difficult (where exponentiation is repeated addition in this case)?
is also a field. We can just find the multiplicative inverse of
in the field, which is quick to do, and then pick
.
Generating a group
A cyclic group has an additional rule: There must be some element in the set (usually given the letter ) such that We say generates the whole group. Angle brackets denote the set of every possible combination (using ) of the items inside, where is read "as the group generated by , , and ." If a group is cyclic, then = .
Groups in general are not necessarily cyclic. For example, we can make a group of 2-bit binary numbers with XOR as the "multiplication". This is a group, but no 2-bit binary number generates all of them through repeated XORs: , , ,
Back to our multiplicative groups over the integers, we may find ourselves wanting a generator of , (e.g. for the Diffie-Hellman parameter ). In this particular kind of group, a generator is called a primitive root modulo (but we’ll stick with "generator" for now). Interestingly:
- What we’re looking for might not exist, meaning the group is not always cyclic. Note that we’re safe with prime . If you’re curious, the group is cyclic if and only if is one of of , or where is prime and .
- There is no general formula to find a generator given , but if we know is cyclic, there happens to be many possible generators, so guessing has a good chance of success, and checking that it generates everything can be done quickly using theorems described below.
Exercises
In the group
, what is
?
{
,
,
,
}. So
, meaning
generates the group.
Does
also generate
?
No.
, which is not equal to
Say we have a group
with generator
(so
). If we list all of out
,
,
, ... will we loop back to
at some point?
Yes.
is in the group (by rule 8), so if it’s not in
, then
doesn’t properly generate the group.
(You could also argue that the inverse
is in the group somewhere, so
for some
. Then we loop back when we hit
.)
In an earlier exercise, we found all inverses in
by guessing and checking and we found that inverses formed neat pairs. Given the additional information that
generates the group and
in that order, can you find all inverses again without guessing?
Since
, we can take any
and pick
such that
some multiple of 6. Then
is the inverse of
.
, so
and
are inverses
, so
and
are inverses
, so
is its own inverse
and, of course,
is its own inverse.
Here is an example of finding generators of by brute force. Can you spot any patterns?
# [[pow(g, i, 17) for i in range(1, 17)] for g in range(1, 17)] i=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | generator? g= ----------------------------------------------------------------+----------- 1 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | 2 [2, 4, 8, 16, 15, 13, 9, 1, 2, 4, 8, 16, 15, 13, 9, 1] | 3 [3, 9, 10, 13, 5, 15, 11, 16, 14, 8, 7, 4, 12, 2, 6, 1] | yes 4 [4, 16, 13, 1, 4, 16, 13, 1, 4, 16, 13, 1, 4, 16, 13, 1] | 5 [5, 8, 6, 13, 14, 2, 10, 16, 12, 9, 11, 4, 3, 15, 7, 1] | yes 6 [6, 2, 12, 4, 7, 8, 14, 16, 11, 15, 5, 13, 10, 9, 3, 1] | yes 7 [7, 15, 3, 4, 11, 9, 12, 16, 10, 2, 14, 13, 6, 8, 5, 1] | yes 8 [8, 13, 2, 16, 9, 4, 15, 1, 8, 13, 2, 16, 9, 4, 15, 1] | 9 [9, 13, 15, 16, 8, 4, 2, 1, 9, 13, 15, 16, 8, 4, 2, 1] | 10 [10, 15, 14, 4, 6, 9, 5, 16, 7, 2, 3, 13, 11, 8, 12, 1] | yes 11 [11, 2, 5, 4, 10, 8, 3, 16, 6, 15, 12, 13, 7, 9, 14, 1] | yes 12 [12, 8, 11, 13, 3, 2, 7, 16, 5, 9, 6, 4, 14, 15, 10, 1] | yes 13 [13, 16, 4, 1, 13, 16, 4, 1, 13, 16, 4, 1, 13, 16, 4, 1] | 14 [14, 9, 7, 13, 12, 15, 6, 16, 3, 8, 10, 4, 5, 2, 11, 1] | yes 15 [15, 4, 9, 16, 2, 13, 8, 1, 15, 4, 9, 16, 2, 13, 8, 1] | 16 [16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1] |
Euler’s theorem, Fermat’s little theorem
It would be very useful if the pattern of ones in the right-hand column held true for any element of any ; that would allow huge exponents to be reduced (modulo 16 in this case).
Not only is it true (for coprime and ), but we now have enough tools to prove it! Let be cyclic with generator , so the whole group can be written as a list (call it ) We know all the group’s elements are in , just in an unknown order. (recall the elements are minus any terms not coprime to ). Let be any element of the group. Notice that the list is some permutation of : multiplication is invertible, so we can’t multiply two different things by and get the same thing out. Then, since the lists contain the same elements, and we are in a commutative group, the products of all elements in the list remains the same: Factoring all the terms out, we get: Finally, multiplying both sides by the inverse of (which is just some member of the group, and with your knowledge of how inverses work, you might know which one): which is exactly Euler’s theorem.
Fermat’s little theorem follows straight from Euler’s by substituting prime for : So we know that, no matter what element we pick from the multiplicative group modulo , raising it to the (the size of the group) will give us 1.
Lagrange’s theorem
The proof above is handy because it doesn’t require any definitions beyond what we already have. Admittedly, it doesn’t provide much intuition as to why.
- A subgroup is a subset of a group where the group laws are satisfied. For example, in the group of the integers under addition, the even numbers are a subgroup. A group is always a subgroup of itself.
- Lagrange’s theorem states that, if you find a subgroup in a group, the order of a subgroup divides the order of the group. (in other words, the order of the group is a multiple of the order of the subgroup.)
Then (with some details intentionally left out) Euler’s theorem is Lagrange’s theorem applied to the group and the subgroup .
Notation notes
- In this page we used square brackets to be very clear that the "numbers" we’re using are congruence classes of integers. Usually you’ll see shortened to and written as (the triple bar meaning "is equivalent/congruent to").
- The common (mod
) notation can be confusing when mistaking "mod" for the
%
operator, which is a different thing (and even this operator can refer to different things; try calculating(-1 % 5)
in python, and again into your browser console.) (mod ) is a statement about the world we’re living in when reading something like (mod ).
Conclusion
If you would like to learn more, The Joy of Cryptography is a wonderful and well-written undergraduate-level textbook. It teaches cryptography along with the underlying math, has sane notation, and is freely available online. See chapters 0.1, 0.2, 0.4, 13.1, and 14.1 for things relevant to this webpage.