# The Mathematics of Secret Sharing

Here’s a simple puzzle with a neat story. A rich old woman is drafting her will and wants to distribute her expansive estate equally amongst her five children. But her children are very greedy, and the woman knows that if he leaves her will unprotected her children will resort to nefarious measures to try to get more than their fair share. In one fearful scenario, she worries that the older four children will team up to bully the youngest child entirely out of his claim! She desperately wants them to cooperate, so she decides to lock the will away, and the key is a secret integer $N$. The question is, how can she distribute this secret number to her children so that the only way they can open the safe is if they are all present and willing?

A mathematical way to say this is: how can she distribute some information to her children so that, given all of their separate pieces of information, they can reconstruct the key, but for every choice of fewer than 5 children, there is no way to reliably recover the key? This is called the secret sharing problem. More generally, say we have an integer $N$ called the secret, a number of participants $k$, and a number required for reconstruction $r$. Then a secret sharing protocol is the data of a method for distributing information and a method for reconstructing the secret. The distributing method is an algorithm $D$ that accepts as input $N, k, r$ and produces as output a list of $k$ numbers $D(N, k) = (x_1, x_2, \dots, x_k)$. These are the numbers distributed to the $k$ participants. Then the reconstruction method is a function $R$ which accepts as input $r$ numbers $(y_1, \dots, y_r)$ and outputs a number $M$. We want two properties to hold :

• The reconstruction function $R$ outputs $N$ when given any $r$ of the numbers output by $D$.
• One cannot reliably reconstruct $N$ with fewer than $r$ of the numbers output by $D$.

The question is: does an efficient secret sharing protocol exist for every possible choice of $r \leq k$? In fact it does, and the one we’ll describe in this post is far more secure than the word “reliable” suggests. It will be so hard as to be mathematically impossible to reconstruct the secret from fewer than the desired number of pieces. Independently discovered by Adi Shamir in 1979, the protocol we’ll see in this post is wonderfully simple, and as we describe it we’ll build up a program to implement it. This time we’ll work in the Haskell programming language, and you can download the program from this blog’s Github page. And finally, a shout out to my friend Karishma Chadha who worked together with me on this post. She knows Haskell a lot better than I do.

## Polynomial Interpolation

The key to the secret sharing protocol is a beautiful fact about polynomials. Specifically, if you give me $k+1$ points in the plane with distinct $x$ values, then there is a unique degree $k$ polynomial that passes through the points. Just as importantly (and as a byproduct of this fact), there are infinitely many degree $k+1$ polynomials that pass through the same points. For example, if I give you the points $(1,2), (2,4), (-2,2)$, the only quadratic (degree 2) polynomial that passes through all of them is $1 + \frac{1}{2}x + \frac{1}{2} x^2$. The proof that you can always find such a polynomial is pretty painless, so let’s take it slowly and write a program as we go. Suppose you give me some list of $k+1$ points $(x_0, y_0), \dots, (x_k, y_k)$ and no two $x$ values are the same. The proof has two parts. First we have to prove existence, that some degree $k$ polynomial passes through the points, and then we have to prove that the polynomial is unique. The uniqueness part is easier, so let’s do the existence part first. Let’s start with just one point $(x_0, y_0)$. What’s a degree zero polynomial that passes through it? Just the constant function $f(x) = y_0$. For two points $(x_0, y_0), (x_1, y_1)$ it’s similarly easy, since we all probably remember from basic geometry that there’s a unique line passing through any two points. But let’s write the line in a slightly different way:

$\displaystyle f(x) = \frac{(x-x_1)}{x_0-x_1}y_0 + \frac{(x-x_0)}{x_1-x_0} y_1$

Why write it this way? Because now it should be obvious that the polynomial passes through our two points: if I plug in $x_0$ then the second term is zero and the first term is just $y_0(x_0 – x_1) / (x_0 – x_1) = y_0$, and likewise for $x_1$.

For example, if we’re given $(1, 3), (2, 5)$ we get:

$\displaystyle f(x) = \frac{(x – 2)}{(1-2)} \cdot 3 + \frac{(x-1)}{(2-1)} \cdot 5$

Plugging in $x = 1$ cancels the second term out, leaving $f(1) = \frac{1-2}{1-2} \cdot 3 = 3$, and plugging in $x = 2$ cancels the first term, leaving $f(2) = \frac{(2-1)}{(2-1)} \cdot 5 = 5$.

Now the hard step is generalizing this to three points. But the suggestive form above gives us a hint on how to continue.

$\displaystyle f(x) = \frac{(x-x_1)(x-x_2)}{(x_0-x_1)(x_0-x_2)}y_0+\frac{(x-x_0)(x-x_2)}{(x_1-x_0)(x_1-x_2)}y_1+ \frac{(x-x_0)(x-x_1)}{(x_2-x_0)(x_2-x_1)}y_2$

Notice that the numerators of the terms take on the form $y_j \prod_{i \ne j} (x-x_i)$, that is, a product $(x-x_0)(x-x_1), \dots, (x-x_n) y_j$ excluding $(x – x_j)$. Thus, all terms will cancel out to 0 if we plug in $x_i$, except one term, which has the form

$\displaystyle y_i \cdot \frac{\prod_{j \neq i} (x-x_j)}{\prod_{j \neq i} (x_i – x_j)}$

Here, the fraction on the right side of the term cancels out to 1 when $x_i$ is plugged in, leaving only $y_i$, the desired result. Now that we’ve written the terms in this general product form, we can easily construct examples for any number of points. We just do a sum of terms that look like this, one for each $y$ value. Try writing this out as a summation, if you feel comfortable with notation.

Let’s go further and write an algorithm to construct the polynomial for us. Some preliminaries: we encode a polynomial as a list of coefficients in degree-increasing order, so that $1 + 3x + 5x^3$ is represented by [1,3,0,5].

type Point = (Rational, Rational)
type Polynomial = [Rational] --Polynomials are represented in ascending degree order


Then we can write some simple functions for adding and multiplying polynomials

addPoly :: Polynomial -&gt; Polynomial -&gt; Polynomial

multNShift :: Polynomial -&gt; (Rational, Int) -&gt; Polynomial
multNShift xs (y, shift) =
(replicate shift 0) ++ ( map ((*) y) xs)

multPoly :: Polynomial -&gt; Polynomial -&gt; Polynomial
multPoly [] [] = []
multPoly [] _ = []
multPoly _ [] = []
multPoly xs ys = foldr addPoly [] $map (multNShift ys)$ zip xs [0..]


In short, multNShift multiplies a polynomial by a monomial (like $3x^2 (1 + 7x + 2x^4)$), and multPoly does the usual distribution of terms, using multNShift to do most of the hard work. Then to construct the polynomial we need one more helper function to extract all elements of a list except a specific entry:

allBut :: Integer -&gt; [a] -&gt; [a]
allBut i list = snd $unzip$ filter (\ (index,_) -&gt; i /= index) $zip [0..] list  And now we can construct a polynomial from a list of points in the same way we did mathematically. findPolynomial :: [Point] -&gt; Polynomial findPolynomial points = let term (i, (xi,yi)) = let prodTerms = map (\ (xj, _) -&gt; [-xj/(xi - xj), 1/(xi - xj)])$ allBut i points
in multPoly [yi] $foldl multPoly [1] prodTerms in foldl addPoly []$ map term $zip [0..] points  Here the sub-function term constructs the$ i$-th term of the polynomial, and the remaining expression adds up all the terms. Remember that due to our choice of representation the awkward 1 sitting in the formula signifies the presence of$ x$. And that’s it! An example of it’s use to construct$ 3x – 1$: *Main&gt; findPolynomial [(1,2), (2,5)] [(-1) % 1,3 % 1]  Now the last thing we need to do is show that the polynomial we constructed in this way is unique. Here’s a proof. Suppose there are two degree$ n$polynomials$ f(x)$and$ g(x)$that pass through the$ n+1$given data points$ (x_0, y_0), (x_1, y_1), \dots , (x_n, y_n)$. Let$ h(x) = p(x) – q(x)$, and we want to show that$ h(x)$is the zero polynomial. This proves that$ f(x)$is unique because the only assumptions we made at the beginning were that$ f,g$both passed through the given points. Now since both$ f$and$ g$are degree$ n$polynomials,$ h$is a polynomial of degree at most$ n$. It is also true that$ h(x_i) = p(x_i) – q(x_i) = y_i – y_i = 0$where$ 0\leq i\leq n$. Thus, we have (at least)$ n+1$roots of this degree$ n$polynomial. But this can’t happen by the fundamental theorem of algebra! In more detail: if a nonzero degree$ \leq n$polynomial really could have$ n+1$distinct roots, then you could factor it into at least$ n+1$linear terms like$ h(x) = (x – x_0)(x – x_1) \dots (x – x_n)$. But since there are$ n+1$copies of$ x$,$ h$would need to be a degree$ n+1$polynomial! The only way to resolve this contradiction is if$ h$is actually the zero polynomial, and thus$ h(x) = f(x) – g(x) = 0$,$ f(x) = g(x)$. This completes the proof. Now that we know these polynomials exist and are unique, it makes sense to give them a name. So for a given set of$ k+1$points, call the unique degree$ k$polynomial that passes through them the interpolating polynomial for those points. ## Secret Sharing with Interpolating Polynomials Once you think to use interpolating polynomials, the connection to secret sharing seems almost obvious. If you want to distribute a secret to$ k$people so that$ r$of them can reconstruct it here’s what you do: 1. Pick a random polynomial$ p$of degree$ r-1$so that the secret is$ p(0)$. 2. Distribute the points$ (1, p(1)), (2, p(2)), \dots, (k, p(k))$. Then the reconstruction function is: take the points provided by at least$ r$participants, use them to reconstruct$ p$, and output$ p(0)$. That’s it! Step 1 might seem hard at first, but you can just notice that$ p(0)$is equivalent to the constant term of the polynomial, so you can pick$ r-1$random numbers for the other coefficients of$ p$and output them. In Haskell, makePolynomial :: Rational -&gt; Int -&gt; StdGen -&gt; Polynomial makePolynomial secret r generator = secret : map toRational (take (r-1)$ randomRs (1, (numerator(2*secret))) generator)

share :: Rational -&gt; Integer -&gt; Int -&gt; IO [Point]
share secret k r = do
generator &lt;- getStdGen
let poly = makePolynomial secret r generator
ys = map (eval poly) $map toRational [1..k] return$ zip [1..] ys


In words, we initialize the Haskell standard generator (which wraps the results inside an IO monad), then we construct a polynomial by letting the first coefficient be the secret and choosing random coefficients for the rest. And findPolynomial is the reconstruction function.

Finally, just to flush the program out a little more, we write a function that encodes or decodes a string as an integer.

encode :: String -&gt; Integer
encode str = let nums = zip [0..] $map (toInteger . ord) str integers = map (\(i, n) -&gt; shift n (i*8)) nums in foldl (+) 0 integers decode :: Integer -&gt; String decode 0 = &quot;&quot; decode num = if num &lt; 0 then error &quot;Can't decode a negative number&quot; else chr (fromInteger (num .&amp;. 127)) : (decode$ shift num (-8))


And then we have a function that shows the whole process in action.

example msg k r =
let secret = toRational $encode msg in do points (numerator x, numerator y)) points let subset = take r points encodedSecret = eval (findPolynomial subset) 0 putStrLn$ show $numerator encodedSecret putStrLn$ decode $numerator encodedSecret  And a function call: *Main&gt; example &quot;Hello world!&quot; 10 5 10334410032606748633331426632 [(1,34613972928232668944107982702),(2,142596447049264820443250256658),(3,406048862884360219576198642966),(4,916237517700482382735379150124),(5,1783927975542901326260203400662),(6,3139385067235193566437068631142),(7,5132372890379242119499357692158),(8,7932154809355236501627439048336),(9,11727493455321672728948666778334),(10,16726650726215353317537380574842)] 10334410032606748633331426632 Hello world!  ## Security The final question to really close this problem with a nice solution is, “How secure is this protocol?” That is, if you didn’t know the secret but you had$ r-1$numbers, could you find a way to recover the secret, oh, say, 0.01% of the time? Pleasingly, the answer is a solid no. This protocol has something way stronger, what’s called information-theoretic security. In layman’s terms, this means it cannot possibly be broken, period. That is, without taking advantage of some aspect of the random number generator, which we assume is a secure random number generator. But with that assumption the security proof is trivial. Here it goes. Pick a number$ M$that isn’t the secret$ N$. It’s any number you want. And say you only have$ r-1$of the correct numbers$ y_1, \dots, y_{r-1}$. Then there is a final number$ y_r$so that the protocol reconstructs$ M$instead of$ N$. This is no matter which of the unused$ x$-values you pick, no matter what$ M$and$ r-1$numbers you started with. This is simply because adding in$ (0, M)$defines a new polynomial$ q$, and you can use any point on$ q$as your$ r$-th number. Here’s what this means. A person trying to break the secret sharing protocol would have no way to tell if they did it correctly! If the secret is a message, then a bad reconstruction could produce any message. In information theory terms, knowing$ r-1$of the numbers provides no information about the actual message. In our story from the beginning of the post, no matter how much computing power one of the greedy children may have, the only algorithm they have to open the safe is to try every combination. The mother could make the combination have length in the millions of digits, or even better, the mother could encode the will as an integer and distribute that as the secret. I imagine there are some authenticity issues there, since one could claim to have reconstructed a false will, signatures and all, but there appear to be measures to account for this. One might wonder if this is the only known secret sharing protocol, and the answer is no. Essentially, any time you have an existence and uniqueness theorem in mathematics, and the objects you’re working with are efficiently constructible, then you have the potential for a secret sharing protocol. There are two more on Wikipedia. But people don’t really care to find new ones anymore because the known protocols are as good as it gets. On a broader level, the existence of efficient secret sharing protocols is an important fact used in the field of secure multiparty computation. Here the goal is for a group of individuals to compute a function depending on secret information from all of them, without revealing their secret information to anyone. A classic example of this is to compute the average of seven salaries without revealing any of the salaries. This was a puzzle featured on Car Talk, and it has a cute answer. See if you can figure it out. Until next time! # Elliptic Curve Diffie-Hellman So far in this series we’ve seen elliptic curves from many perspectives, including the elementary, algebraic, and programmatic ones. We implemented finite field arithmetic and connected it to our elliptic curve code. So we’re in a perfect position to feast on the main course: how do we use elliptic curves to actually do cryptography? ## History As the reader has heard countless times in this series, an elliptic curve is a geometric object whose points have a surprising and well-defined notion of addition. That you can add some points on some elliptic curves was a well-known technique since antiquity, discovered by Diophantus. It was not until the mid 19th century that the general question of whether addition always makes sense was answered by Karl Weierstrass. In 1908 Henri Poincaré asked about how one might go about classifying the structure of elliptic curves, and it was not until 1922 that Louis Mordell proved the fundamental theorem of elliptic curves, classifying their algebraic structure for most important fields. While mathematicians have always been interested in elliptic curves (there is currently a million dollar prize out for a solution to one problem about them), its use in cryptography was not suggested until 1985. Two prominent researchers independently proposed it: Neal Koblitz at the University of Washington, and Victor Miller who was at IBM Research at the time. Their proposal was solid from the start, but elliptic curves didn’t gain traction in practice until around 2005. More recently, the NSA was revealed to have planted vulnerable national standards for elliptic curve cryptography so they could have backdoor access. You can see a proof and implementation of the backdoor at Aris Adamantiadis’s blog. For now we’ll focus on the cryptographic protocols themselves. ## The Discrete Logarithm Problem Koblitz and Miller had insights aplenty, but the central observation in all of this is the following. Adding is easy on elliptic curves, but undoing addition seems hard. What I mean by this is usually called the discrete logarithm problem. Here’s a formal definition. Recall that an additive group is just a set of things that have a well-defined addition operation, and the that notation$ ny$means$ y + y + \dots + y$($ n$times). Definition: Let$ G$be an additive group, and let$ x, y$be elements of$ G$so that$ x = ny$for some integer$ n$. The discrete logarithm problem asks one to find$ n$when given$ x$and$ y$. I like to give super formal definitions first, so let’s do a comparison. For integers this problem is very easy. If you give me 12 and 4185072, I can take a few seconds and compute that$ 4185072 = (348756) 12$using the elementary-school division algorithm (in the above notation,$ y=12, x=4185072$, and$ n = 348756$). The division algorithm for integers is efficient, and so it gives us a nice solution to the discrete logarithm problem for the additive group of integers$ \mathbb{Z}$. The reason we use the word “logarithm” is because if your group operation is multiplication instead of addition, you’re tasked with solving the equation$ x = y^n$for$ n$. With real numbers you’d take a logarithm of both sides, hence the name. Just in case you were wondering, we can also solve the multiplicative logarithm problem efficiently for rational numbers (and hence for integers) using the square-and-multiply algorithm. Just square$ y$until doing so would make you bigger than$ x$, then multiply by$ y$until you hit$ x$. But integers are way nicer than they need to be. They are selflessly well-ordered. They give us division for free. It’s a computational charity! What happens when we move to settings where we don’t have a division algorithm? In mathematical lingo: we’re really interested in the case when$ G$is just a group, and doesn’t have additional structure. The less structure we have, the harder it should be to solve problems like the discrete logarithm. Elliptic curves are an excellent example of such a group. There is no sensible ordering for points on an elliptic curve, and we don’t know how to do division efficiently. The best we can do is add$ y$to itself over and over until we hit$ x$, and it could easily happen that$ n$(as a number) is exponentially larger than the number of bits in$ x$and$ y$. What we really want is a polynomial time algorithm for solving discrete logarithms. Since we can take multiples of a point very fast using the double-and-add algorithm from our previous post, if there is no polynomial time algorithm for the discrete logarithm problem then “taking multiples” fills the role of a theoretical one-way function, and as we’ll see this opens the door for secure communication. Here’s the formal statement of the discrete logarithm problem for elliptic curves. Problem: Let$ E$be an elliptic curve over a finite field$ k$. Let$ P, Q$be points on$ E$such that$ P = nQ$for some integer$ n$. Let$ |P|$denote the number of bits needed to describe the point$ P$. We wish to find an algorithm which determines$ n$and has runtime polynomial in$ |P| + |Q|$. If we want to allow randomness, we can require the algorithm to find the correct$ n$with probability at least 2/3. So this problem seems hard. And when mathematicians and computer scientists try to solve a problem for many years and they can’t, the cryptographers get excited. They start to wonder: under the assumption that the problem has no efficient solution, can we use that as the foundation for a secure communication protocol? ## The Diffie-Hellman Protocol and Problem Let’s spend the rest of this post on the simplest example of a cryptographic protocol based on elliptic curves: the Diffie-Hellman key exchange. A lot of cryptographic techniques are based on two individuals sharing a secret string, and using that string as the key to encrypt and decrypt their messages. In fact, if you have enough secret shared information, and you only use it once, you can have provably unbreakable encryption! We’ll cover this idea in a future series on the theory of cryptography (it’s called a one-time pad, and it’s not all that complicated). All we need now is motivation to get a shared secret. Because what if your two individuals have never met before and they want to generate such a shared secret? Worse, what if their only method of communication is being monitored by nefarious foes? Can they possibly exchange public information and use it to construct a shared piece of secret information? Miraculously, the answer is yes, and one way to do it is with the Diffie-Hellman protocol. Rather than explain it abstractly let’s just jump right in and implement it with elliptic curves. As hinted by the discrete logarithm problem, we only really have one tool here: taking multiples of a point. So say we’ve chosen a curve$ C$and a point on that curve$ Q$. Then we can take some secret integer$ n$, and publish$ Q$and$ nQ$for the world to see. If the discrete logarithm problem is truly hard, then we can rest assured that nobody will be able to discover$ n$. How can we use this to established a shared secret? This is where Diffie-Hellman comes in. Take our two would-be communicators, Alice and Bob. Alice and Bob each pick a binary string called a secret key, which in interpreted as a number in this protocol. Let’s call Alice’s secret key$ s_A$and Bob’s$ s_B$, and note that they don’t have to be the same. As the name “secret key” suggests, the secret keys are held secret. Moreover, we’ll assume that everything else in this protocol, including all data sent between the two parties, is public. So Alice and Bob agree ahead of time on a public elliptic curve$ C$and a public point$ Q$on$ C$. We’ll sometimes call this point the base point for the protocol. Bob can cunningly do the following trick: take his secret key$ s_B$and send$ s_B Q$to Alice. Equally slick Alice computes$ s_A Q$and sends that to Bob. Now Alice, having$ s_B Q $, computes$ s_A s_B Q$. And Bob, since he has$ s_A Q$, can compute$ s_B s_A Q$. But since addition is commutative in elliptic curve groups, we know$ s_A s_B Q = s_B s_A Q$. The secret piece of shared information can be anything derived from this new point, for example its$ x$-coordinate. If we want to talk about security, we have to describe what is public and what the attacker is trying to determine. In this case the public information consists of the points$ Q, s_AQ, s_BQ$. What is the attacker trying to figure out? Well she really wants to eavesdrop on their subsequent conversation, that is, the stuff that encrypt with their new shared secret$ s_As_BQ$. So the attacker wants find out$ s_As_BQ$. And we’ll call this the Diffie-Hellman problem. Diffie-Hellman Problem: Suppose you fix an elliptic curve$ E$over a finite field$ k$, and you’re given four points$ Q, aQ, bQ$and$ P$for some unknown integers$ a, b$. Determine if$ P = abQ$in polynomial time (in the lengths of$ Q, aQ, bQ, P$). On one hand, if we had an efficient solution to the discrete logarithm problem, we could easily use that to solve the Diffie-Hellman problem because we could compute$ a,b$and them quickly compute$ abQ$and check if it’s$ P$. In other words discrete log is at least as hard as this problem. On the other hand nobody knows if you can do this without solving the discrete logarithm problem. Moreover, we’re making this problem as easy as we reasonably can because we don’t require you to be able to compute$ abQ$. Even if some prankster gave you a candidate for$ abQ$, all you have to do is check if it’s correct. One could imagine some test that rules out all fakes but still doesn’t allow us to compute the true point, which would be one way to solve this problem without being able to solve discrete log. So this is our hardness assumption: assuming this problem has no efficient solution then no attacker, even with really lucky guesses, can feasibly determine Alice and Bob’s shared secret. ## Python Implementation The Diffie-Hellman protocol is just as easy to implement as you would expect. Here’s some Python code that does the trick. Note that all the code produced in the making of this post is available on this blog’s Github page. def sendDH(privateKey, generator, sendFunction): return sendFunction(privateKey * generator) def receiveDH(privateKey, receiveFunction): return privateKey * receiveFunction()  And using our code from the previous posts in this series we can run it on a small test. import os def generateSecretKey(numBits): return int.from_bytes(os.urandom(numBits // 8), byteorder='big') if __name__ == &quot;__main__&quot;: F = FiniteField(3851, 1) curve = EllipticCurve(a=F(324), b=F(1287)) basePoint = Point(curve, F(920), F(303)) aliceSecretKey = generateSecretKey(8) bobSecretKey = generateSecretKey(8) alicePublicKey = sendDH(aliceSecretKey, basePoint, lambda x:x) bobPublicKey = sendDH(bobSecretKey, basePoint, lambda x:x) sharedSecret1 = receiveDH(bobSecretKey, lambda: alicePublicKey) sharedSecret2 = receiveDH(aliceSecretKey, lambda: bobPublicKey) print('Shared secret is %s == %s' % (sharedSecret1, sharedSecret2))  Pythons os module allows us to access the operating system’s random number generator (which is supposed to be cryptographically secure) via the function urandom, which accepts as input the number of bytes you wish to generate, and produces as output a Python bytestring object that we then convert to an integer. Our simplistic (and totally insecure!) protocol uses the elliptic curve$ C$defined by$ y^2 = x^3 + 324 x + 1287$over the finite field$ \mathbb{Z}/3851$. We pick the base point$ Q = (920, 303)$, and call the relevant functions with placeholders for actual network transmission functions. There is one issue we have to note. Say we fix our base point$ Q$. Since an elliptic curve over a finite field can only have finitely many points (since the field only has finitely many possible pairs of numbers), it will eventually happen that$ nQ = 0$is the ideal point. Recall that the smallest value of$ n$for which$ nQ = 0$is called the order of$ Q$. And so when we’re generating secret keys, we have to pick them to be smaller than the order of the base point. Viewed from the other angle, we want to pick$ Q$to have large order, so that we can pick large and difficult-to-guess secret keys. In fact, no matter what integer you use for the secret key it will be equivalent to some secret key that’s less than the order of$ Q$. So if an attacker could guess the smaller secret key he wouldn’t need to know your larger key. The base point we picked in the example above happens to have order 1964, so an 8-bit key is well within the bounds. A real industry-strength elliptic curve (say, Curve25519 or the curves used in the NIST standards*) is designed to avoid these problems. The order of the base point used in the Diffie-Hellman protocol for Curve25519 has gargantuan order (like$ 2^{256}$). So 256-bit keys can easily be used. I’m brushing some important details under the rug, because the key as an actual string is derived from 256 pseudorandom bits in a highly nontrivial way. So there we have it: a simple cryptographic protocol based on elliptic curves. While we didn’t experiment with a truly secure elliptic curve in this example, we’ll eventually extend our work to include Curve25519. But before we do that we want to explore some of the other algorithms based on elliptic curves, including random number generation and factoring. ## Comments on Insecurity Why do we use elliptic curves for this? Why not do something like RSA and do multiplication (and exponentiation) modulo some large prime? Well, it turns out that algorithmic techniques are getting better and better at solving the discrete logarithm problem for integers mod$ p$, leading some to claim that RSA is dead. But even if we will never find a genuinely efficient algorithm (polynomial time is good, but might not be good enough), these techniques have made it clear that the key size required to maintain high security in RSA-type protocols needs to be really big. Like 4096 bits. But for elliptic curves we can get away with 256-bit keys. The reason for this is essentially mathematical: addition on elliptic curves is not as well understood as multiplication is for integers, and the more complex structure of the group makes it seem inherently more difficult. So until some powerful general attacks are found, it seems that we can get away with higher security on elliptic curves with smaller key sizes. I mentioned that the particular elliptic curve we chose was insecure, and this raises the natural question: what makes an elliptic curve/field/basepoint combination secure or insecure? There are a few mathematical pitfalls (including certain attacks we won’t address), but one major non-mathematical problem is called a side-channel attack. A side channel attack against a cryptographic protocol is one that gains additional information about users’ secret information by monitoring side-effects of the physical implementation of the algorithm. The problem is that different operations, doubling a point and adding two different points, have very different algorithms. As a result, they take different amounts of time to complete and they require differing amounts of power. Both of these can be used to reveal information about the secret keys. Despite the different algorithms for arithmetic on Weierstrass normal form curves, one can still implement them to be secure. Naively, one might pad the two subroutines with additional (useless) operations so that they have more similar time/power signatures, but I imagine there are better methods available. But much of what makes a curve’s domain parameters mathematically secure or insecure is still unknown. There are a handful of known attacks against very specific families of parameters, and so cryptography experts simply avoid these as they are discovered. Here is a short list of pitfalls, and links to overviews: 1. Make sure the order of your basepoint has a short facorization (e.g., is$ 2p, 3p,$or$ 4p$for some prime$ p$). Otherwise you risk attacks based on the Chinese Remainder Theorem, the most prominent of which is called Pohlig-Hellman. 2. Make sure your curve is not supersingular. If it is you can reduce the discrete logarithm problem to one in a different and much simpler group. 3. If your curve$ C$is defined over$ \mathbb{Z}/p$, make sure the number of points on$ C$is not equal to$ p$. Such a curve is called prime-field anomalous, and its discrete logarithm problem can be reduced to the (additive) version on integers. 4. Don’t pick a small underlying field like$ \mathbb{F}_{2^m}$for small$ m$. General-purpose attacks can be sped up significantly against such fields. 5. If you use the field$ \mathbb{F}_{2^m}$, ensure that$ m$is prime. Many believe that if$ m$has small divisors, attacks based on some very complicated algebraic geometry can be used to solve the discrete logarithm problem more efficiently than any general-purpose method. This gives evidence that$ m$being composite at all is dangerous, so we might as well make it prime. This is a sublist of the list provided on page 28 of this white paper. The interesting thing is that there is little about the algorithm and protocol that is vulnerable. Almost all of the vulnerabilities come from using bad curves, bad fields, or a bad basepoint. Since the known attacks work on a pretty small subset of parameters, one potentially secure technique is to just generate a random curve and a random point on that curve! But apparently all respected national agencies will refuse to call your algorithm “standards compliant” if you do this. Next time we’ll continue implementing cryptographic protocols, including the more general public-key message sending and signing protocols. Until then! # (Finite) Fields — A Primer So far on this blog we’ve given some introductory notes on a few kinds of algebraic structures in mathematics (most notably groups and rings, but also monoids). Fields are the next natural step in the progression. If the reader is comfortable with rings, then a field is extremely simple to describe: they’re just commutative rings with 0 and 1, where every nonzero element has a multiplicative inverse. We’ll give a list of all of the properties that go into this “simple” definition in a moment, but an even more simple way to describe a field is as a place where “arithmetic makes sense.” That is, you get operations for$ +,-, \cdot , /$which satisfy the expected properties of addition, subtraction, multiplication, and division. So whatever the objects in your field are (and sometimes they are quite weird objects), they behave like usual numbers in a very concrete sense. So here’s the official definition of a field. We call a set$ F$a field if it is endowed with two binary operations addition ($ +$) and multiplication ($ \cdot$, or just symbol juxtaposition) that have the following properties: • There is an element we call 0 which is the identity for addition. • Addition is commutative and associative. • Every element$ a \in F$has a corresponding additive inverse$ b$(which may equal$ a$) for which$ a + b = 0$. These three properties are just the axioms of a (commutative) group, so we continue: • There is an element we call 1 (distinct from 0) which is the identity for multiplication. • Multiplication is commutative and associative. • Every nonzero element$ a \in F$has a corresponding multiplicative inverse$ b$(which may equal$ a$) for which$ ab = 1$. • Addition and multiplication distribute across each other as we expect. If we exclude the existence of multiplicative inverses, these properties make$ F$a commutative ring, and so we have the following chain of inclusions that describes it all$ \displaystyle \textup{Fields} \subset \textup{Commutative Rings} \subset \textup{Rings} \subset \textup{Commutative Groups} \subset \textup{Groups}$The standard examples of fields are the real numbers$ \mathbb{R}$, the rationals$ \mathbb{Q}$, and the complex numbers$ \mathbb{C}$. But of course there are many many more. The first natural question to ask about fields is: what can they look like? For example, can there be any finite fields? A field$ F$which as a set has only finitely many elements? As we saw in our studies of groups and rings, the answer is yes! The simplest example is the set of integers modulo some prime$ p$. We call them$ \mathbb{Z} / p \mathbb{Z},$or sometimes just$ \mathbb{Z}/p$for short, and let’s rederive what we know about them now. As a set,$ \mathbb{Z}/p$consists of the integers$ \left \{ 0, 1, \dots, p-1 \right \}$. The addition and multiplication operations are easy to define, they’re just usual addition and multiplication followed by a modulus. That is, we add by$ a + b \mod p$and multiply with$ ab \mod p$. This thing is clearly a commutative ring (because the integers form a commutative ring), so to show this is a field we need to show that everything has a multiplicative inverse. There is a nice fact that allows us to do this: an element$ a$has an inverse if and only if the only way for it to divide zero is the trivial way$ 0a = 0$. Here’s a proof. For one direction, suppose$ a$divides zero nontrivially, that is there is some$ c \neq 0$with$ ac = 0$. Then if$ a$had an inverse$ b$, then$ 0 = b(ac) = (ba)c = c$, but that’s very embarrassing for$ c$because it claimed to be nonzero. Now suppose$ a$only divides zero in the trivial way. Then look at all possible ways to multiply$ a$by other nonzero elements of$ F$. No two can give you the same result because if$ ax = ay$then (without using multiplicative inverses)$ a(x-y) = 0$, but we know that$ a$can only divide zero in the trivial way so$ x=y$. In other words, the map “multiplication by$ a$” is injective. Because the set of nonzero elements of$ F$is finite you have to hit everything (the map is in fact a bijection), and some$ x$will give you$ ax = 1$. Now let’s use this fact on$ \mathbb{Z}/p$in the obvious way. Since$ p$is a prime, there are no two smaller numbers$ a, b < p$so that$ ab = p$. But in$ \mathbb{Z}/p$the number$ p$is equivalent to zero (mod$ p$)! So$ \mathbb{Z}/p$has no nontrivial zero divisors, and so every element has an inverse, and so it’s a finite field with$ p$elements. The next question is obvious: can we get finite fields of other sizes? The answer turns out to be yes, but you can’t get finite fields of any size. Let’s see why. ## Characteristics and Vector Spaces Say you have a finite field$ k$(lower-case k is the standard letter for a field, so let’s forget about$ F$). Beacuse the field is finite, if you take 1 and keep adding it to itself you’ll eventually run out of field elements. That is,$ n = 1 + 1 + \dots + 1 = 0$at some point. How do I know it’s zero and doesn’t keep cycling never hitting zero? Well if at two points$ n = m \neq 0$, then$ n-m = 0$is a time where you hit zero, contradicting the claim. Now we define$ \textup{char}(k)$, the characteristic of$ k$, to be the smallest$ n$(sums of 1 with itself) for which$ n = 0$. If there is no such$ n$(this can happen if$ k$is infinite, but doesn’t always happen for infinite fields), then we say the characteristic is zero. It would probably make more sense to say the characteristic is infinite, but that’s just the way it is. Of course, for finite fields the characteristic is always positive. So what can we say about this number? We have seen lots of example where it’s prime, but is it always prime? It turns out the answer is yes! For if$ ab = n = \textup{char}(k)$is composite, then by the minimality of$ n$we get$ a,b \neq 0$, but$ ab = n = 0$. This can’t happen by our above observation, because being a zero divisor means you have no inverse! Contradiction, sucker. But it might happen that there are elements of$ k$that can’t be written as$ 1 + 1 + \dots + 1$for any number of terms. We’ll construct examples in a minute (in fact, we’ll classify all finite fields), but we already have a lot of information about what those fields might look like. Indeed, since every field has 1 in it, we just showed that every finite field contains a smaller field (a subfield) of all the ways to add 1 to itself. Since the characteristic is prime, the subfield is a copy of$ \mathbb{Z}/p$for$ p = \textup{char}(k)$. We call this special subfield the prime subfield of$ k$. The relationship between the possible other elements of$ k$and the prime subfield is very neat. Because think about it: if$ k$is your field and$ F$is your prime subfield, then the elements of$ k$can interact with$ F$just like any other field elements. But if we separate$ k$from$ F$(make a separate copy of$ F$), and just think of$ k$as having addition, then the relationship with$ F$is that of a vector space! In fact, whenever you have two fields$ k \subset k’$, the latter has the structure of a vector space over the former. Back to finite fields,$ k$is a vector space over its prime subfield, and now we can impose all the power and might of linear algebra against it. What’s it’s dimension? Finite because$ k$is a finite set! Call the dimension$ m$, then we get a basis$ v_1, \dots, v_m$. Then the crucial part: every element of$ k$has a unique representation in terms of the basis. So they are expanded in the form$ \displaystyle f_1v_1 + \dots + f_mv_m$where the$ f_i$come from$ F$. But now, since these are all just field operations, every possible choice for the$ f_i$has to give you a different field element. And how many choices are there for the$ f_i$? Each one has exactly$ |F| = \textup{char}(k) = p$. And so by counting we get that$ k$has$ p^m$many elements. This is getting exciting quickly, but we have to pace ourselves! This is a constraint on the possible size of a finite field, but can we realize it for all choices of$ p, m$? The answer is again yes, and in the next section we’ll see how. But reader be warned: the formal way to do it requires a little bit of familiarity with ideals in rings to understand the construction. I’ll try to avoid too much technical stuff, but if you don’t know what an ideal is, you should expect to get lost (it’s okay, that’s the nature of learning new math!). ## Constructing All Finite Fields Let’s describe a construction. Take a finite field$ k$of characteristic$ p$, and say you want to make a field of size$ p^m$. What we need to do is construct a field extension, that is, find a bigger field containing$ k$so that the vector space dimension of our new field over$ k$is exactly$ m$. What you can do is first form the ring of polynomials with coefficients in$ k$. This ring is usually denoted$ k[x]$, and it’s easy to check it’s a ring (polynomial addition and multiplication are defined in the usual way). Now if I were speaking to a mathematician I would say, “From here you take an irreducible monic polynomial$ p(x)$of degree$ m$, and quotient your ring by the principal ideal generated by$ p$. The result is the field we want!” In less compact terms, the idea is exactly the same as modular arithmetic on integers. Instead of doing arithmetic with integers modulo some prime (an irreducible integer), we’re doing arithmetic with polynomials modulo some irreducible polynomial$ p(x)$. Now you see the reason I used$ p$for a polynomial, to highlight the parallel thought process. What I mean by “modulo a polynomial” is that you divide some element$ f$in your ring by$ p$as much as you can, until the degree of the remainder is smaller than the degree of$ p(x)$, and that’s the element of your quotient. The Euclidean algorithm guarantees that we can do this no matter what$ k$is (in the formal parlance,$ k[x]$is called a Euclidean domain for this very reason). In still other words, the “quotient structure” tells us that two polynomials$ f, g \in k[x]$are considered to be the same in$ k[x] / p$if and only if$ f – g$is divisible by$ p$. This is actually the same definition for$ \mathbb{Z}/p$, with polynomials replacing numbers, and if you haven’t already you can start to imagine why people decided to study rings in general. Let’s do a specific example to see what’s going on. Say we’re working with$ k = \mathbb{Z}/3$and we want to compute a field of size$ 27 = 3^3$. First we need to find a monic irreducible polynomial of degree$ 3$. For now, I just happen to know one:$ p(x) = x^3 – x + 1$. In fact, we can check it’s irreducible, because to be reducible it would have to have a linear factor and hence a root in$ \mathbb{Z}/3$. But it’s easy to see that if you compute$ p(0), p(1), p(2)$and take (mod 3) you never get zero. So I’m calling this new ring$ \displaystyle \frac{\mathbb{Z}/3[x]}{(x^3 – x + 1)}$It happens to be a field, and we can argue it with a whole lot of ring theory. First, we know an irreducible element of this ring is also prime (because the ring is a unique factorization domain), and prime elements generate maximal ideals (because it’s a principal ideal domain), and if you quotient by a maximal ideal you get a field (true of all rings). But if we want to avoid that kind of argument and just focus on this ring, we can explicitly construct inverses. Say you have a polynomial$ f(x)$, and for illustration purposes we’ll choose$ f(x) = x^4 + x^2 – 1$. Now in the quotient ring we could do polynomial long division to find remainders, but another trick is just to notice that the quotient is equivalent to the condition that$ x^3 = x – 1$. So we can reduce$ f(x)$by applying this rule to$ x^4 = x^3 x$to get$ \displaystyle f(x) = x^2 + x(x-1) – 1 = 2x^2 – x – 1$Now what’s the inverse of$ f(x)$? Well we need a polynomial$ g(x) = ax^2 + bx + c$whose product with$ f$gives us something which is equivalent to 1, after you reduce by$ x^3 – x + 1$. A few minutes of algebra later and you’ll discover that this is equivalent to the following polynomial being identically 1$ \displaystyle (a-b+2c)x^2 + (-3a+b-c)x + (a – 2b – 2c) = 1$In other words, we get a system of linear equations which we need to solve:$ \displaystyle \begin{aligned} a & – & b & + & 2c & = 0 \\ -3a & + & b & – & c &= 0 \\ a & – & 2b & – & 2c &= 1 \end{aligned}$And from here you can solve with your favorite linear algebra techniques. This is a good exercise for working in fields, because you get to abuse the prime subfield being characteristic 3 to say terrifying things like$ -1 = 2$and$ 6b = 0$. The end result is that the inverse polynomial is$ 2x^2 + x + 1$, and if you were really determined you could write a program to compute these linear systems for any input polynomial and ensure they’re all solvable. We prefer the ring theoretic proof. In any case, it’s clear that taking a polynomial ring like this and quotienting by a monic irreducible polynomial gives you a field. We just control the size of that field by choosing the degree of the irreducible polynomial to our satisfaction. And that’s how we get all finite fields! ## One Last Word on Irreducible Polynomials One thing we’ve avoided is the question of why irreducible monic polynomials exist of all possible degrees$ m$over any$ \mathbb{Z}/p$(and as a consequence we can actually construct finite fields of all possible sizes). The answer requires a bit of group theory to prove this, but it turns out that the polynomial$ x^{p^m} – x$has all degree$ m$monic irreducible polynomials as factors. But perhaps a better question (for computer scientists) is how do we work over a finite field in practice? One way is to work with polynomial arithmetic as we described above, but this has some downsides: it requires us to compute these irreducible monic polynomials (which doesn’t sound so hard, maybe), to do polynomial long division every time we add, subtract, or multiply, and to compute inverses by solving a linear system. But we can do better for some special finite fields, say where the characteristic is 2 (smells like binary) or we’re only looking at$ F_{p^2}$. The benefit there is that we aren’t forced to use polynomials. We can come up with some other kind of structure (say, matrices of a special form) which happens to have the same field structure and makes computing operations relatively painless. We’ll see how this is done in the future, and see it applied to cryptography when we continue with our series on elliptic curve cryptography. Until then! # Elliptic Curves as Elementary Equations Finding solutions to systems of polynomial equations is one of the oldest and deepest problems in all of mathematics. This is broadly the domain of algebraic geometry, and mathematicians wield some of the most sophisticated and abstract tools available to attack these problems. The elliptic curve straddles the elementary and advanced mathematical worlds in an interesting way. On one hand, it’s easy to describe in elementary terms: it’s the set of solutions to a cubic function of two variables. But despite how simple they seem deep theorems govern their behavior, and many natural questions about elliptic curves are still wide open. Since elliptic curves provide us with some of the strongest and most widely used encryption protocols, understanding elliptic curves more deeply would give insight into the security (or potential insecurity) of these protocols. Our first goal in this series is to treat elliptic curves as mathematical objects, and derive the elliptic curve group as the primary object of study. We’ll see what “group” means next time, and afterward we’ll survey some of the vast landscape of unanswered questions. But this post will be entirely elementary, and will gently lead into the natural definition of the group structure on an elliptic curve. ## Elliptic Curves as Equations The simplest way to describe an elliptic curve is as the set of all solutions to a specific kind of polynomial equation in two real variables,$ x,y$. Specifically, the equation has the form:$ \displaystyle y^2 = x^3 + ax + b$Where$ a,b$are real numbers such that$ \displaystyle -16(4a^3 + 27b^2) \neq 0$One would naturally ask, “Who the hell came up with that?” A thorough answer requires a convoluted trip through 19th and 20th-century mathematical history, but it turns out that this is a clever form of a very natural family of equations. We’ll elaborate on this in another post, but for now we can give an elementary motivation. Say you have a pyramid of spheres whose layers are squares, like the one below We might wonder when it’s the case that we can rearrange these spheres into a single square. Clearly you can do it for a pyramid of height 1 because a single ball is also a 1×1 square (and one of height zero if you allow a 0x0 square). But are there any others? This question turns out to be a question about an elliptic curve. First, recall that the number of spheres in such a pyramid is given by$ \displaystyle 1 + 4 + 9 + 16 + \dots + n^2 = \frac{n(n+1)(2n+1)}{6}$And so we’re asking if there are any positive integers$ y$such that$ \displaystyle y^2 = \frac{x(x+1)(2x+1)}{6}$Here is a graph of this equation in the plane. As you admire it, though, remember that we’re chiefly interested in integer solutions. The equation doesn’t quite have the special form we mentioned above, but the reader can rest assured (and we’ll prove it later) that one can transform our equation into that form without changing the set of solutions. In the meantime let’s focus on the question: are there any integer-valued points on this curve besides$ (0,0)$and$ (1,1)$? The method we use to answer this question comes from ancient Greece, and is due to Diophantus. The idea is that we can use the two points we already have to construct a third point. This method is important because it forms the basis for our entire study of elliptic curves. Take the line passing through$ (0,0)$and$ (1,1)$, given by the equation$ y = x$, and compute the intersection of this line and the original elliptic curve. The “intersection” simply means to solve both equations simultaneously. In this case it’s$ \begin{aligned} y^2 &= \frac{x(x+1)(2x+1)}{6} \\ y &= x \end{aligned}$It’s clear what to do: just substitute the latter in for the former. That is, solve$ \displaystyle x^2 = \frac{x(x+1)(2x+1)}{6}$Rearranging this into a single polynomial and multiplying through by 3 gives$ \displaystyle x^3 – \frac{3x^2}{2} + \frac{x}{2} = 0$Factoring cubics happens to be easy, but let’s instead use a different trick that will come up again later. Let’s use a fact that is taught in elementary algebra and precalculus courses and promptly forgotten, that the sum of the roots of any polynomial is$ \frac{-a_{n-1}}{a_n}$, where$ a_{n}$is the leading coefficient and$ a_{n-1}$is the next coefficient. Here$ a_n = 1$, so the sum of the roots is$ 3/2$. This is useful because we already know two roots, namely the solutions 0 and 1 we used to define the system of equations in the first place. So the third root satisfies$ \displaystyle r + 0 + 1 = \frac{3}{2}$And it’s$ r = 1/2$, giving the point$ (1/2, 1/2)$since the line was$ y=x$. Because of the symmetry of the curve, we also get the point$ (1/2, -1/2)$. Here’s a zoomed-in picture of what we just did to our elliptic curve. We used the two pink points (which gave us the dashed line) to find the purple point. The bad news is that these two new points don’t have integer coordinates. So it doesn’t answer our question. The good news is that now we have more points! So we can try this trick again to see if it will give us still more points, and hope to find some that are integer valued. (It sounds like a hopeless goal, but just hold out a bit longer). If we try this trick again using$ (1/2, -1/2)$and$ (1,1)$, we get the equation$ \displaystyle (3x – 2)^2 = \frac{x(x+1)(2x+1)}{6}$And redoing all the algebraic steps we did before gives us the solution$ x=24, y=70$. In other words, we just proved that$ \displaystyle 1^2 + 2^2 + \dots + 24^2 = 70^2$Great! Here’s another picture showing what we just did. In reality we don’t care about this little puzzle. Its solution might be a fun distraction (and even more distracting: try to prove there aren’t any other integer solutions), but it’s not the real treasure. The mathematical gem is the method of finding the solution. We can ask the natural question: if you have two points on an elliptic curve, and you take the line between those two points, will you always get a third point on the curve? Certainly the answer is no. See this example of two points whose line is vertical. But with some mathematical elbow grease, we can actually force it to work! That is, we can define things just right so that the line between any two points on an elliptic curve will always give you another point on the curve. This sounds like mysterious black magic, but it lights the way down a long mathematical corridor of new ideas, and is required to make sense of using elliptic curves for cryptography. ## Shapes of Elliptic Curves Before we continue, let’s take a little detour to get a good feel for the shapes of elliptic curves. We have defined elliptic curves by a special kind of equation (we’ll give it a name in a future post). During most of our study we won’t be able to make any geometric sense of these equations. But for now, we can pretend that we’re working over real numbers and graph these equations in the plane. Elliptic curves in the form$ y^2 = x^3 + ax + b$have a small handful of different shapes that we can see as$ a,b$vary: The problem is when we cross the point at which the rounded part pinches off in the first animation, and the circular component appears in the second. At those precise moments, the curve becomes “non-smooth” (or singular), and for reasons we’ll see later this is bad. The condition from the beginning of the article (that$ -16(4a^3 + 27b^2) \neq 0$) ensures that these two cases are excluded from consideration, and it’s one crucial part of our “elbow grease” to ensure that lines behave nicely. The “canonical” shape of the elliptic curve is given by the specific example$ y^2 = x^3 – x + 1\$. It’s the example that should pop up whenever you imagine an elliptic curve, and it’s the example we’ll use for all of our pictures.

So in the next post we’ll roll up our sleeves and see exactly how “drawing lines” can be turned into an algebraic structure on an elliptic curve.

Until then!