The Joy of Secs: Part 1
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 post ended up being a lot longer (in length and “the making”) than I intended and so I’ve broken it in to parts. In this post we’ll cover the basics of Finite Fields. You can also read part 2 about more advanced operations and non-prime Finite Fields and part 3 which covers calculations on elliptic curves.
So why The Joy of Secs? Well today we’re going to focus on two particular curves that are defined by the Standards for Efficient Cryptography Group, namely secp256k1 and secp256r1. The former is widely used in blockchain platforms and the latter is the NIST prime 256 curve, commonly available in hardware on mobile platforms. For those with a penchant for details, both of these curves are considered to have weaknesses, the SafeCurves website has a wealth of information on why.
I set myself two goals:
- Be able to calculate given for a particular curve (this will help us to understand the format of an Elliptic Curve public key)
- Understand how point addition works (this will help us to understand what an Elliptic Curve private key represents and how to derive a public key for a given private key)
Before we dig in to the elliptic curve side of things we firstly need to understand what “defined over a finite field” means. Most explanations of elliptic curves show a diagram of a nice continuous flowing curve like those in the diagram below:
The problem with this is that it is an elliptic curve defined over the real numbers, . An implementation of ECC using real numbers would generally be inefficient and could further be insecure. This is where our finite field comes in:
This plot is a lot less aestheticcally pleasing than the former, but better represents how elliptic curves are used in cryptography. In this case the curve is defined over the Finite Field .
We could take a long detour here understanding just what Groups, Rings and Fields are (if you have the inclination then there are a number of good resources), but for our purposes I’ll just take you back to some good old fashioned maths.
Before studying the advanced topics of Trigonometry, Geometry and Algebra at school you may well have studied a number line. Most likely you would have used a line that was big enough to incorprate whatever calculations you were asked to complete. You would add (and multiply) by counting up to the right and subtract by counting down to the left. Did you ever fall off the number line? I expect not, but what if there were a fixed number of entries on the line, from to , and if you added too large a number and dropped off the right hand end then you simply looped back and started counting from again? The same applies if you subtract too large a number and go past , you simply loop back around to the largest number on the line, , and carry on counting down. What I’ve just described is integer maths modulo some number, . In mathematical notation this is normally referred to as and is known as a commutative ring.
So just where does our finite field come in?
If we take the simplest commutative ring above, , we can draft a table of how the classic mathematical operations of addition and multiplication behave for each possible value in the set:
These tables represent the addition and multiplication operations, but how could we handle subtraction? Of the 4 subtraction operations, only is non-trivial because it wraps around the left hand side of our number line ( is not on the line) and becomes . Subtraction is just the opposite of addition, so looking back at our addition table we can use it to calculate the subtraction, reversing the steps of addition. To calculate simply find the in the left column and then work across the row to find ; The label of the column, , is the result of the calculation. This technique works for any pair of values in .
Ok, so now on to something a little harder, division. Given we only have two values, there are only 4 possible division operations , , and . Dividing by is trivial, but dividing by presents problems! You were probably taught that dividing by isn’t possible:
If we choose to ignore the challenges of dividing by 0 then the multiplication table for does have an answer for division in the same way the addition table also provides answers for subtraction. We can use a similar technique to above. First find the row for the denominator and then go along the row until you find the value of the numerator and then go up the column to the column header and that value gives you your result. In there are only two possible divisions: and . This is rather simple in , so lets move on to :
Some of the values here start to get a little harder to calculate. For example . How can we represent the answer to this as an integer? Normally we’d just say and be done, but with our example we can only work with values that are in , i.e. , and . To solve this we have to restate the division problem. Rather than “What is one divided by two?” we can phrase our problem as “what value, when multiplied by two, equals one?”. Given our three options let’s try each in turn:
And there you have your answer, in is . Using our technique of finding the denominator row and then searching for the column with numerator in it also yields the correct answer. Good news!
Let’s move on to :
Trying to calculate should be simple, right? Traditional maths would tell you this is , but if we again use the lookup technique and travel across the row you’ll notice that both and are possible answers. This makes sense if we also flip the question around: what do you multiply by to get in :
Now how about ? If we use the lookup technique unfortunately we hit a blank, there is no value in the 3rd row (neither is there a ). This implies that multiplication is not invertible and so fails the Multiplicative Inverse element rule of a field.
From this we can summise that and are both finite fields, but is not (merely just a commutative ring!).
Does this mean that no other values will work? Well looking at the multiplication table for :
We see unique values in every row and column (apart from the first row), so based on what we’ve understood so far this means that division does work for every pair of value where the denominator is non-zero and so is also a finite field.
Interesting, what about ?
Unfortunately, like , also has values that have no multiplicative inverse, so not a finite field.
Although we’ve only looked at a small handful of s here you might have noticed a pattern; is a finite field when is prime number. Consider writing out further tables as an exercise for the reader, but this pattern continues for all (the infinite number of) primes.
In this post you have learnt how to do basic math operations in a Finite Field, carry on to part 2 to look at more complex operations and non-prime fields.