Finite fields: basics, computer implementations, visualizations
In this post I want to talk about finite fields! This is the result of some teaching I did at Canada/USA Mathcamp in 2025. I taught a class on finite fields which assumed fewer prerequisites than is typical for students learning about finite fields, particularly avoiding any prior knowledge of abstract algebra.
I'll assume a bit more background in this post, but what I want to focus on more than the theory of finite fields (which one can read about in any algebra textbook after all) is some ideas as to how they can be implemented on computers, and ways to think about visualizing their multiplication tables.
Finite fields
In the phrase finite field, finite is an adjective modifying the noun field, so let's start with noun. A field is a set whose elements we can add, subtract, multiply, and divide. If you want to be formal about it there's a long list of axioms that the addition and multiplication operations must satisfy;1 we think of subtraction and division as properties of addition and multiplication, respectively.
Many familiar sets of numbers are fields: the rational numbers (whole number fractions) form a field, the real numbers (anything on a number, typically represented by their decimal expansion), the complex numbers. Notice that these familiar fields are all infinite sets, and in fact one is contained in the next.
A finite field is simply a field whose underlying set is finite! The most common examples are given by arithmetic modulo a prime : take the set of integers , and equip them with the usual addition and multiplication operations but have them wrap around at whenever we get a result larger or larger. We call this field . I'm skipping the details of why this is a field; the important thing here is that being prime is what enables us to have inverses for multiplication (aka division). If is not a prime this division property won't hold for all numbers (in particular it will fail with those which share a common factor with the modulus ).
Other finite fields
While the examples may be familiar if you've seen modular arithmetic before, there are other finite fields out there! Short of walking through the whole standard construction here (again, consult an algebra textbook if you want to read in detail), let me summarize the salient points here:
- Every finite field has prime characteristic, i.e. there is a unique prime number such that it contains .
- The additive group of a finite field forms a finite dimensional vector space over . In particular that means that a finite field must have elements, where is the dimension as a vector space over .
- Every finite field can be constructed by adjoining a root of an irreducible monic polynomial over to . If this polynomial is and it has degree , we think about this construction of adjoining a root as the quotient , and the standard choice of representatives for the elements are the polynomials with degree . The element plays the role of a root of in this quotient ring.
- Let's say that for some of degree . Multiplication can be performed by multiplying the representative polynomials and repeatedly applying the relation to reduce the result back down to a polynomial of degree .
To be very concrete about it, let's talk about the smallest field which isn't one of the , the field with four elements. Our only choice for irreducible polynomial of degree over is . The four elements of our field are . Addition is done mod . The interesting piece of multiplication is , which is equal to : using our reduction rule we have that and since everything happens mod we also have that and so we get that . To do another example:
Implementing finite field arithmetic on a computer
Finite fields of size are particularly amenable to being represented on computers. Indeed, an interesting everyday application of finite fields are QR codes: every QR code uses arithmetic in the field with elements as the basis for the mathematics it uses to detect and correct errors. Specifically QR codes use Reed-Solomon codes over the field with elements! Let's work through some of the details about how one might exploit the mathematics of finite fields to implement them on a computer.
Representing elements
There are three useful points of view on the elements of a finite field of size :
- As a polynomial of degree , with coefficients in .
- As a string of bits .
- As an (unsigned) integer in the range , .
Regardless of which of these viewpoints we use at any given time, it is very easy to store elements of a field with elements a length strings of bits. Especially when , a finite field element is nothing other than a single byte!
Implementing addition
Addition is straightforward: it is just the XOR operation! When we add polynomials we're just adding mod in each degree separately; as bit-strings this is taking the XOR in each component; this is the XOR of unsigned integers. We don't need any special work to implement addition on a computer!
Implementing multiplication
Multiplication is more more involved. Notice that addition doesn't depend on the choice of defining polynomial , while multiplication does. In principle we can multiply by computing the polynomial product and then repeatedly applying the reduction rule to any terms of degree until we're left with one of our representatives of degree . This sounds fiddly, can we do better?
One approach we could take is to simply store the entire multiplication table: we'd have fast computations, at the prohibitive cost of having to store a table. There are a few optimizations we could make (storing only half the table since multiplication is commutative, skipping rows which can easily be computed with additions or shifts) but these don't change the basic quadratic scaling of the space required.
There is another strategy that works in practice for some values of which is still table-based. We make use of another fact about finite fields: their multiplicative group is cyclic, we can choose some element such that every non-zero element of the field is a power of .2 After picking such an , we precompute two tables:
- A table which takes the "logarithm", converting any non-zero element of the finite field into the such that . This table has size .
- An table which "exponentiates", taking an integer and returning the finite field element . In principle this table only needs to have size as well, but in order to save one operation in our eventual multiplication algorithm it is convenient to double the size of the table, so that it can take as inputs any integer )$ and return raised to that integer.
Using these tables there is an efficient algorithm for multiplying in the field of size for small values of (i.e. where storage is reasonable)! To multiply elements and we:
- Use our lookup to find so that , .
- Do a normal integer addition . If our table only went up to this value might overflow that and we'd need to conditionally subtract off ; if we take the double size table we don't need this extra step.
- Use our lookup table with to get .
So we can multiply in our finite field with a single integer addition and 3 table lookups!
Further directions
Here I've given a very concrete description of how one can implement finite field arithmetic purely in software. In practice, some CPUs come with specialized instructions for performing finite field multiplication in these fields with elements, and more clever thinking and use of the underlying math goes into the design of those!3
Some visualizations
Once we have finite field arithmetic up and running on a computer, we can start generating some visualizations. For any finite field the addition table is not very exciting: addition works like . The multiplication table is where the interesting business happens. This makes sense from the polynomial quotient construction perspective: if we think of the elements of our finite field as polynomials of degree over , then addition doesn't increase the degree past the maximum degree of the summands, so we never encounter the defining equation.
Let's look at the multiplication table of , since is quite possibly the most computer-amenable finite field.
Figure 1: multiplication table of the finite field with 256 elements.
Here we've used the irreducible polynomial to define our field. Remember our three ways of representing elements in a finite field of size :
- polynomials of degree with coefficients,
- bit strings of length (i.e. bytes!),
- integers in the range . The bit-string definition lets us translate easily between the other two definitions: a bit-string is simply a record of the coefficients of a polynomial, or it can be viewed as an unsigned integer.
In the multiplication table image above pixels are coloured according to the integer value of the field element, from dark to light, to . Hence the first row of pixels is all black () and the second row is a gradient ( through ).
This is a natural colouring of pixels to use from the integer perspective on our bytes. But from the polynomial perspective, there's less of a direct relationship with "size". Why should the coefficient contribute more to the colour of the pixel than the coefficient, or any other?
Here's that same multiplication table, but now we've split the colouring into two channels: the bottom half of the byte (the 4 bits which are coefficients of through ) contributes to the green channel (scaled up by a factor of 16 so they lie in the same range as the top half of the byte), and the top half of the byte (coefficients of through ) contribute to the "purple" channel (directly to blue, of the value to red, for aesthetic reasons).4
Figure 2: 2-coloured multiplication table of the finite field with 256 elements.
It looks different! The patterns formed by the bottom 4 bits have a seeming periodicity to them, with the image looking like it breaks into a grid. The reason we don't have a completely regular -fold pattern is that the wraparound of the defining equation introduces some "noise" in those lower order bits.
Even that visualization introduced an artificial distinction between the high and low coefficients. Here's that same idea, but smoothly interpolating through which set of 4 consecutive bits are in the green and purple channels.
Figure 3: Animated multiplication table of the finite field with 256 elements.
We can see lots of patterns, and it's really cool to see the implied periodicity move from (when the bottom 4 bits are the green channel) down through powers of until there's no repetition (when the top bits are the green channel), back to implied -fold periodicity (alternating black and green when the lowest order bit contributes the most to the green channel).
Next time
It is true that for any prime power size there is a finite field of size . The typical proofs are non-constructive, in that one shows that a degree irreducible polynomial over must exist, without actually writing one down explicitly. In the next post I want to talk about a couple of ways of directly constructing families of finite fields. Until next time!
Here are the field axioms. A field is a set equipped with two binary operations and which we call addition and multiplication. These operations satisfy all the "usual" properties. They are associative (order of parentheses doesn't matter when adding/multiplying 3 elements, and similarly for multiplication), commutative (we can flip the order of addition/multiplication, and similarly for multiplication), and multiplication distributes over addition (). There are identity elements for addition and multiplication, so that and for any . You can subtract any element, and you can divide by any non-zero element. Rather than thinking about subtraction and division as separate operations, we think of them as properties satisfied by addition and multiplication. So for any in there exists an element with the property that (adding is like "subtracting" ). If then there exists an element which satisfies (multiplying by is like "dividing" by ).↩
In practice on chooses a polynomial so that itself is a generator of the multiplicative group. While there is no truly canonical reason to pick one defining polynomial over another, we can get a somewhat canonical choice by choosing an ordering on the elements of and picking irreducibles polynomials compatibly as one increases degrees, and with the restriction that their roots are generators of the multiplicative group. These are called Conway polynomials, and our standard choice for the field of elements is the degree one over .↩
modern CPUs have dedicated instructions both for performing arithmetic with polynomials over and for working with various finite fields of size (with a fixed "standard" choice of defining polynomial). See CLMUL instruction set and Advanced Vector Extensions (specifically the "Galois Field New Instructions") on Wikipedia for more information.↩
I'm not an eye specialist, but here's what my understanding of what is going on from a vision perspective. Human eyes have different types of colour sensing cells ("cones"), in red, green, blue flavours. We typically have the most red, followed by green, with blue further behind. So if there's equal intensities of red, green, blue wavelengths coming into our eyes we end up perceiving red as the dominant colour, followed by green then blue, see Wikipedia's article on Cone cells. So when we're building an image directly from integer values like this using an RGB channel PNG we run up against this fact of how humans experience colour. I tried this image with just red and blue and the red seemingly dominates the image, even though the maximum intensities are in principle the same. Green and blue has a similar issue with green dominating. Green and purple are an aesthetic combination, but if we try to make purple by putting the intensity fully into both red and blue channels that "purple" channel washes out the green, I think since the combined red and blue cone contribution overwhelms the green contribution. I find this combination of full green for half of the bits and full blue + half red for the other half of the bits to be great for visualization. The image is clearly dominated by green, so you are able to focus on the patterns happening at the level of the 4 green bits, but there's enough purple contribution that you can find it once you know what you're looking for at any stage.↩