The Joy of Secs: Part 1

Posted by Mark Hornsby on 08/08/2019

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:

  1. Be able to calculate yy given xx for a particular curve (this will help us to understand the format of an Elliptic Curve public key)
  2. 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:

Eliptic curve catalog

The problem with this is that it is an elliptic curve defined over the real numbers, R\Bbb{R}. An implementation of ECC using real numbers would generally be inefficient and could further be insecure. This is where our finite field comes in:

Eliptic curve defined over Z61

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 Z61\Bbb{Z}_{61}.

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 00 to n1n - 1, and if you added too large a number and dropped off the right hand end then you simply looped back and started counting from 00 again? The same applies if you subtract too large a number and go past 00, you simply loop back around to the largest number on the line, n1n - 1, and carry on counting down. What I’ve just described is integer maths modulo some number, nn. In mathematical notation this is normally referred to as Zn\Bbb{Z}_n and is known as a commutative ring.

So just where does our finite field come in?

If we take the simplest commutative ring above, Z2\Bbb{Z}_2, we can draft a table of how the classic mathematical operations of addition and multiplication behave for each possible value in the set:

Z2+01Z2×01001000110101\begin{array}{ccccccccccccccccccccc} \Bbb{Z}_2^+ & & 0 & 1 & & & & & & & & & & & & & & & & &\Bbb{Z}_2^\times & & 0 & 1 \\ \\ 0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & & & & & & & & & & & & & & & & &0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} \\ 1 & & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{0} & & & & & & & & & & & & & & & & &1 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} \\ \end{array}

These tables represent the addition and multiplication operations, but how could we handle subtraction? Of the 4 subtraction operations, only 010-1 is non-trivial because it wraps around the left hand side of our number line (1-1 is not on the line) and becomes 11. 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 010-1 simply find the 00 in the left column and then work across the row to find 11; The label of the column, 11, is the result of the calculation. This technique works for any pair of values in Z2\Bbb{Z}_2.

Ok, so now on to something a little harder, division. Given we only have two values, there are only 4 possible division operations 0/00/0, 0/10/1, 1/01/0 and 1/11/1. Dividing by 11 is trivial, but dividing by 00 presents problems! You were probably taught that dividing by 00 isn't possible:

Divide by zero on TI68 calculator

If we choose to ignore the challenges of dividing by 0 then the multiplication table for Z2\Bbb{Z}_2 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 Z2\Bbb{Z}_2 there are only two possible divisions: 0/1=00/1=0 and 1/1=11/1=1. This is rather simple in Z2\Bbb{Z}_2, so lets move on to Z3\Bbb{Z}_3:

Z3+012Z3×012001200001120101222012021\begin{array}{ccccccccccccccccccccc} \Bbb{Z}_3^+ & & 0 & 1 & 2 & & & & & & & & & & & & & & & &\Bbb{Z}_3^\times & & 0 & 1 & 2 \\ \\ 0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & & & & & & & & & & & & & & & &0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} \\ 1 & & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{0} & & & & & & & & & & & & & & & &1 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} \\ 2 & & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & & & & & & & & & & & & & & & &2 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{1} \\ \end{array}

Some of the values here start to get a little harder to calculate. For example 1/21 / 2. How can we represent the answer to this as an integer? Normally we’d just say 0.50.5 and be done, but with our example we can only work with values that are in Z3\Bbb{Z}_3, i.e. 00, 11 and 22. 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:

0×2=01×2=22×2=4=1Z3\begin{aligned} 0 \times 2 &= \textcolor{red}0 \\ 1 \times 2 &= \textcolor{red}2 \\ 2 \times 2 = 4 &= \textcolor{green}1 \in \Bbb{Z}_3 \end{aligned}

And there you have your answer, 1/21/2 in Z3\Bbb{Z}_3 is 22. 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 Z4\Bbb{Z}_4:

Z4+0123Z4×01230012300000112301012322301202023301230321\begin{array}{cccccccccccccccccccc} \Bbb{Z}_4^+ & & 0 & 1 & 2 & 3 & & & & & & & & & & & & & &\Bbb{Z}_4^\times & & 0 & 1 & 2 & 3 \\ \\ 0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & & & & & & & & & & && & &0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} \\ 1 & & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{0} & & & & & & & & & & && & &1 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} \\ 2 & & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & & & & & & & & & & && & &2 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} \\ 3 & & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & & & & & & & & & & && & &3 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{1} \\ \end{array}

Trying to calculate 2/22/2 should be simple, right? Traditional maths would tell you this is 11, but if we again use the lookup technique and travel across the row you'll notice that both 11 and 33 are possible answers. This makes sense if we also flip the question around: what do you multiply by 22 to get 22 in Z4\Bbb{Z}_4:

1×2=22×2=4=0Z4\begin{aligned} 1 \times 2 &= 2 \\ 2 \times 2 = 4 &= 0 \in \Bbb{Z}_4 \end{aligned}

Now how about 1/21 / 2? If we use the lookup technique unfortunately we hit a blank, there is no 11 value in the 3rd row (neither is there a 33). This implies that multiplication is not invertible and so fails the Multiplicative Inverse element rule of a field.

From this we can summise that Z2\Bbb{Z}_2 and Z3\Bbb{Z}_3 are both finite fields, but Z4\Bbb{Z}_4 is not (merely just a commutative ring!).

Does this mean that no other values will work? Well looking at the multiplication table for Z5\Bbb{Z}_5:

Z5+01234Z5×01234001234000000112340101234223401202413334012303142440123404321\begin{array}{cccccccccccccccccccc} \Bbb{Z}_5^+ & & 0 & 1 & 2 & 3 & 4 & & & & & & & & & & & & &\Bbb{Z}_5^\times & & 0 & 1 & 2 & 3 & 4 \\ \\ 0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & & & & & & & & & & & & &0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} \\ 1 & & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{0} & & & & & & & & & & & & &1 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} \\ 2 & & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & & & & & & & & & & & & &2 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{3} \\ 3 & & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & & & & & & & & & & & & &3 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{2} \\ 4 & & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & & & & & & & & & & & & &4 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{1} \\ \end{array}

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 Z5\Bbb{Z}_5 is also a finite field.

Interesting, what about Z6\Bbb{Z}_6?

Z6+012345Z6×012345001234500000001123450101234522345012024024334501230303034450123404204255012345054321\begin{array}{ccccccccccccccccccc} \Bbb{Z}_6^+ & & 0 & 1 & 2 & 3 & 4 & 5 & & & & & & & & & & &\Bbb{Z}_6^\times & & 0 & 1 & 2 & 3 & 4 & 5 \\ \\ 0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} & & & & & & & & & & &0 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} &\textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{0} \\ 1 & & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{0} & & & & & & & & & & &1 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} &\textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} \\ 2 & & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & & & & & & & & & & &2 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{4} &\textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{4} \\ 3 & & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & & & & & & & & & & &3 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{0} &\textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{3} \\ 4 & & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & & & & & & & & & & &4 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{2} &\textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{4} & \textcolor{#bfbfbf}{2} \\ 5 & & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{1} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{4} & & & & & & & & & & &5 & & \textcolor{#bfbfbf}{0} & \textcolor{#bfbfbf}{5} & \textcolor{#bfbfbf}{4} &\textcolor{#bfbfbf}{3} & \textcolor{#bfbfbf}{2} & \textcolor{#bfbfbf}{1} \\ \end{array}

Unfortunately, like Z4\Bbb{Z}_4, Z6\Bbb{Z}_6 also has values that have no multiplicative inverse, so not a finite field.

Although we’ve only looked at a small handful of Zi\Bbb{Z}_is here you might have noticed a pattern; Zi\Bbb{Z}_i is a finite field when ii 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.