The Joy of Secs: Part 2
Posted by Mark Hornsby on 2019-08-08
One of the most widely deployed asymmetric cryptographic algorithms used today (driven by the popularity of blockchain technology) is based on elliptic curves over finite fields. Like all good engineers I struggled to just accept how they work. I needed to dig deeper to better understand the mathematics and in doing so decided to share my experience …
This is part 2 in a series. You can also read part 1 about the basics of Finite Fields and part 3 which covers calculations on elliptic curves.
In part 1 we learnt the basics of Finite Field maths and showed how to calculate 4 basic operations. We can now start to look at how to handle more complex operations. For example, taking , how can we calculate the square root of ? By simply restating the problem as before, I.e. “what, when multiplied by itself, equals one”. The answer is trivial, , but don’t stop there. In normal arithmetic there is a second answer, . How do we represent this in ? Well, using the number line concept and counting down one from loops us back round to . Could possibly be another solution? Multiplying out, , does indeed produce ! also has two answers in , namely and . Trying and yields no valid answers, but comparing this with the square root operation on the integers, , or real values, , you’ll see that all negative numbers have no answer (to solve this you need to move to complex numbers, ), so they are all quite similar in that regard.
Moving on, we can calculate powers by simply applying repeated multiplication and modulo operations e.g:
Notice how as the powers increase we generate each non-zero element of the field. Once each value has been generated once, the sequence starts again and this repeats ad infinitum. This feature (in this case of the value ) is what is referred to as a primitive element. Not all values within a given field are primitive elements e.g :
The value doesn’t generate every non-zero value in the field and so is not a primitive element of .
The inverse operation of the power operator is the logarithm. As before, we can use the list of values of successive powers to calculate the logarithm:
The sequence of values here is quite unpredictable, it would be hard to solve for any given value unless we’d written out the table of values like this. It’s pretty easy to do this for , but how about if your field has nearly values e.g. ? (This is the field used by the secp256k1 curve as we’ll see later).
It is this challenge, calculating the discrete logarithm in a large finite field, that allows it to be used in cryptography. It is relatively easy to calculate the power, but given a value it is very hard to find the logarithm. This is what is known as a ‘trapdoor’ function and this particular problem is known as the Discrete Logarithm Problem.
Both the elliptic curves that we are looking at as part of this series have prime orders (detailed later on), so we could stop here (feel free to skip straight to part 3), but it is worth understanding that there is another type of finite field that is used by some elliptic curves (sect… curves, not secp…). The simplest example of this type of field has 4 elements, but we already deteremined that it’s not (integers modulo 4). For these type of fields we need to start using something a little more powerful than just the integers, here we will introduce polynomials as some of the values in our field:
You’ll need to apply modular arithmetic to the coefficients (that is why ) and this makes the table quite different in structure to that for addition in . Now the multiplication table:
12 of the values in this table should be somewhat obvious to you, but what about the 4 values in the lower right quadrant? , so why have we put the values , or here? In the same way we apply modular arithmetic to integers, we need to apply a similar principal to polynomials. When our result does not lie within the original field (, , and ) we need to divide by some value and take the remainder. You’ll need to understand polynomial division for this (or you can just reapeatedly subtract until the result is in the original finite field). The value we divide by here is :
As you can see the remainder in each equation lies within our original field. More importantly the values in the multiplication table (excluding the first row of zeros) are all unique in each row. This validates that this field is indeed a finite field.
What leads us to divide by ? This value is known as an irreducible polynomial and there is only one choice for (or as it sometimes known, GF standing for Galois field). Using polynomials to construct a Finite Field works for any , where is a prime number and is a positive integer. (In fact we’ve seen a number of samples where is 1 already e.g. . Fields with more than elements often have multiple choices for the irreducible polynomial.) In fields of this form the value is referred to as the characteristic. It is this value that is used for modular arithmetic on the polynomial coefficients. The maximum power used in the polynomial will be . To show how this works for higher values here is the multiplication table for , the field with 8 elements, using the irreducible polynomial :
And the table for , the field of 9 elements, using irreducible polynomial :
You can review the SEC 2: Recommended Elliptic Curve Domain Parameters document for other curves that are defined over finite fields of characteristic 2. Lets leave the topic of finite fields and return to our discussion of elliptic curves in part 3.