Note: This isn't a blog post; I wrote it 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. It will present much more math than is required for CPSC 436S, both for your understanding of asymmetric cryptography, and hopefully to also show that the math itself is interesting.
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:
The above terminology will need some explaining. Along with relevant definitions and notation, this page offers:
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.
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:
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!
No.
Number 9. There is no integer such that , so 3 has no multiplicative inverse in .
Yes.
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
.
and
and
since ()
and
and
and
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.
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).
Yes.
(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.
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 .
The only numbers that share factors with are multiples of , which are too big to be counted by the totient. Hence is .
.
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.
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.
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 .
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.
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 ).
is also a field. We can just find the multiplicative inverse of in the field, which is quick to do, and then pick .
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:
{, , , }. So , meaning generates the group.
No. , which is not equal to
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 .)
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] |
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.
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.
Then (with some details intentionally left out) Euler's theorem is Lagrange's theorem applied to the group and the subgroup .
%
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 ).
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.