Post · 14 min read· pp. 361–367

Need for speed: Elliptic curves chapter

Introduction

Elliptic curves (EC) have gained widespread acceptance as tools for cryptography. They offer several advantages over other methods, such as RSA, providing equal levels of security with shorter keys (for example, 228-bit keys in EC cryptography are as good as 2300-bit RSA keys). This represents an advantage, since more and more cryptography is done on smart-phones, which are less powerful than computers. These are curves defined by the equation y2=x3+ax+by^2 = x^3 + ax + b over some field (for example, the real numbers). Their shape depends on aa and bb, but they look more or less like the following picture: An elliptic curve over the real numbers

In cryptography, we are not interested in curves defined over the real numbers. We work with them over some finite field Fp\mathcal{F_p} (that is, a set with a finite number of elements, such as $53$, $101$ o $2^{255}-19$), because that gives us a mathematical structure (a finite group) which is very convenient. The curve looks like scattered points with no clear pattern over a finite field:

Elliptic curves play a role in key exchange when connecting via SSH to a server or to prove ownership in bitcoin. They also appear when performing digital signatures, generating random numbers (though there have been some problems) and they are useful even to factor numbers (Lenstra's algorithm). For example, in the elliptic curve digital signature algorithm (ECDSA) you have these steps (don't worry if you do not understand all the terms now, we will cover them one by one afterwards):

  1. Calculate E=hash(message), where hash is a secure hash function.
  2. Take Z equal to the nn leftmost bits of E, where nn is the order of the group (that is, the number of elements making the group).
  3. Select a cryptographically secure random number kk (never use the same kk twice or you'll be revealing your key).
  4. Evaluate (x1,y1)=kg(x_1,y_1)=kg, where gg is the generator of the group.
  5. Let r=x1r=x_1.
  6. Evaluate s=k−1(Z+rsk)(modn)s=k^{-1}(Z+rs_k) \pmod{n}, where sks_k is the secret key.
  7. The signature is the pair (r,s)(r,s).

In this example, we have to evaluate in step 4 an addition on the curve to arrive at point (x1,y1)(x_1,y_1), which gives us rr. In general, kk is a large number (having 256 bits, for example), so that operation can be quite expensive. Besides, if the implementation is not done properly, elliptic curve cryptography could be targeted by side-channel attacks, such as timing and cache attacks. Some elliptic curves have properties that allow for a constant-time implementation, which makes them resistant to these strategies.

Elliptic curves also appear in zk-SNARKs (zero-knowledge succint non-interactive arguments of knowledge; we'll go hunting for the SNARK on another post) to provide homomorphic hiding. The word sounds important, but the idea behind is simple. Suppose that there are two variables, xx and yy and you want (or need) to know x+yx+y. The problem is, you don't know them directly, but you have their encrypted form E(x)E(x) and E(y)E(y). If you have homomorphic hiding, you can compute E(x+y)=E(x)×E(y)E(x+y)=E(x)\times E(y), where ×\times is the operation over the encrypted variables. So, even if you don't know the variables themselves, you can perform mathematical operations on them (and luckily, that's just what you need). This is achieved in practice by means of two elliptic curves (known as a pairing; not all elliptic curves are that sociable or get along quite well with others). To be a good match, we need that the operations can be performed as quickly as possible (among other things). A simple example is the exponential function, f:R→R+/f(x)=exp⁡(x)f: \mathbb{R} \rightarrow \mathbb{R}^+/ f(x)=\exp(x). If you have x=2.303x=2.303, exp⁡(2.303)≈10\exp(2.303)\approx 10, y=3y=3, exp⁡(3)≈20.09\exp(3)\approx 20.09, then exp⁡(x+y)=exp⁡(x)exp⁡(y)=10×20.09=200.9\exp(x+y)=\exp(x)\exp(y)=10\times 20.09=200.9, which is equal to exp⁡(5.303)\exp(5.303) and x+y=5.303x+y=5.303. Of course, in this case it is very easy to go back and know the exact numbers xx, yy and x+yx+y; in the case of elliptic curves, this is very hard, owing to the particular group structure.

To be able to work with elliptic curves, we need to define an operation involving the points on the curve. We can do this using the chord-and-tangent construction: given two points on the curve, we can draw a line connecting them; the line intersects the curve at a third point and we reflect it around the xx-axis to obtain the sum (remember the picture of the curve defined over real numbers). The formulae are s=y2−y1x2−x1s=\frac{y_2-y_1}{x_2-x_1} x3=s2−x1−x2x_3=s^2-x_1-x_2 y3=s(x1−x3)−y1y_3=s(x_1-x_3)-y_1

There are some special cases, such as when we want to add a point to itself (we call that "doubling"). To make things work, we need to add a special point O\mathcal{O}, the point at infinity. The curve, together with the operation, form a finite cyclic group. In simple words, every time we add two points we get a third one which belongs to the curve (it is closed under the operation). We also have an identity point (the point at infinity, P+OP+\mathcal{O}) and each point PP has an inverse P′P^\prime, such that P+P′=OP+P^\prime=\mathcal{O}. Moreover, the elements of the group can be generared by repeteadly adding a point gg (the generator) to itself. In other words, for PP in the group, there is some kk such that kg=Pkg=P. If we are given kk, we can quickly calculate PP, but doing the operation the other way around (that is, given PP, find kk) can be very difficult (this is known as the discrete logarithm problem) and we used this idea in a previous paragraph.

All these calculations are done with the operations of the finite field Fp\mathcal{F}_p. We see that, at each addition step, we have to calculate the slope of the line, which involves a division on elements of the finite field. This can be rewritten as s=(x2−x1)−1(y2−y1)s=(x_2-x_1)^{-1}(y_2-y_1), where (x2−x1)−1=b(x_2-x_1)^{-1}=b is the multiplicative inverse of x2−x1x_2-x_1. In simpler form, b(x2−x1)≡1(modp)b(x_2-x_1)\equiv 1 \pmod{p} (When we write a≡b(modp)a \equiv b \pmod{p}, we say that there is some integer qq such that a=pq+b. It is read aa is congruent to bb modulo pp). Computing inverses is possible, but quite more expensive than multiplications. There is a result from number theory called Fermat's little theorem, which tells us that ap−1≡1(modp)a^{p-1}\equiv 1 \pmod{p} if aa and pp have no common divisors other than 1 (we say aa and pp are coprime). We can write this in a different fashion,

ap−2a≡1(modp)a^{p-2}a\equiv 1 \pmod{p}

and we see that b=ap−2b=a^{p-2} (we can make things simple and then reduce bb to ap−2(modp)a^{p-2} \pmod{p}). So, to get the multiplicative inverse, we have to perform many multiplications. (Sometimes it is much easier. Let's take p=5p=5 and we try to find $4^{-1}$. We can see that if we do $4\times 4=16 \equiv 1 \pmod{5}$, so $4^{-1}=4$. This is rather strange, but we have to remember that operations on the finite field have a different behavior). As a matter of fact, p−1p-1 gives an upper bound to the power nn we have to apply to a field element aa to get its inverse, that is an≡1(modp)a^n \equiv 1 \pmod{p}. We call the lowest (positive) exponent nn such that an≡1(modp)a^n \equiv 1 \pmod{p} the order of the element. Lagrange's Theorem says that the order nn divides p−1p-1. For example, take p=7p=7, so p−1=6p-1=6. We see that $4^3=64\equiv 1 \pmod{7}$, so $4^2\equiv 2 \pmod{7}$ is the inverse of $4$ ($2\times 4=8 \equiv 1 \pmod{7}$). In the same way, $2^3\equiv 1 \pmod{7}$. In the case of $3$, $3^6 \equiv 1 \pmod{7}$ and $3^5 \equiv 5 \pmod{7}$ and we also have $5^6 \equiv 1 \pmod{7}$. So, we see that the orders nn are among the divisors of p−1=6p-1=6.

So, even if the equations for point addition over elliptic curves look really simple, they involve many calculations and these may be expensive. If every time we want to add two points, we have to find the multiplicative inverse modulo a large prime, we see that we are paying a high price. There are a couple of tricks we can perform, such as transforming the curve, to gain a lot of speed or avoid some other issues, such as side-channel attacks.

If you are one of those not willing to pay the cost of finding inverses and saving some time or just love speed for the sake of it, then the next section is for you.

Projective coordinates

We can save ourselves from costly inversions if we move from our nice 2 dimensional space to a 3 dimensional space. This was introduced by Moebius and helps us also to represent the point at infinity properly. We can map our points from our elliptic curve (x,y)(x,y) to points in projective space (X,Y,Z)(X,Y,Z) as (x,y)→(X=x,Y=y,Z=1)(x,y) \rightarrow (X=x,Y=y,Z=1) and O→(0,1,0)\mathcal{O} \rightarrow (0,1,0). We can go back using the transformation (X,Y,Z)→(x=X/Z,y=Y/Z)(X,Y,Z) \rightarrow (x=X/Z,y=Y/Z), except for the point at infinity, where it is ill-defined. We can visualize this process with the following picture, where we take three points from an elliptic curve and transform them to 3-d.

We can think of this as transforming our 2-d points to lines passing through the origin in 3-d space. For example, the point (x1,y1)(x_1,y_1) in 2-d transforms to the line (μx1,μy1,μ)(\mu x_1,\mu y_1, \mu) with μ\mu an element in the field. Thus, two points P1=(X1,Y1,Z1)P_1=(X_1,Y_1,Z_1) and P2=(X2,Y2,Z2)P_2=(X_2,Y_2,Z_2) are the same in 2-d (more precisely, are congruent) if we can find η\eta such that (ηX1,ηY1,ηZ1)=(X2,Y2,Z2)(\eta X_1,\eta Y_1,\eta Z_1)=(X_2,Y_2,Z_2). These lines do not contain the origin (0,0,0)(0,0,0). It is usual to write points is projective space as (X:Y:Z)(X:Y:Z), instead of (X,Y,Z)(X,Y,Z). In our picture, the point A (yellow) gets mapped to the point D (red above it). All the points that lie on the same straight line passing through the origin and D (pink dashed) are considered equivalent to D. Similarly, point B (blue) is mapped to point F (light blue) and all the ponts over the light green dotted line (except the origin) are equivalent to F. When we add points in this space, the components (X,Y,Z)(X,Y,Z) will change, but we can go back to the point belonging to the curve by just retracing our steps to Z=1Z=1 along the line that passes through the origin. Why go all this length? We will shortly see that we avoid inversions at each addition step and do just one at the time of finding the point in 2-d (for example, when we need to find r=x1r=x_1 in ECDSA). Of course, if we have to do P=2gP=2g we didn't gain anything, but if we have to perform P=kgP=kg with kk in the order of 256 bits, we saved many costly inversions.

Making the substitutions into the elliptic curve equation

(YZ)2=(XZ)3+a(XZ)+b\left(\frac{Y}{Z}\right)^2 = \left(\frac{X}{Z}\right)^3 + a\left(\frac{X}{Z}\right) + b
We can multiply by Z3Z^3 and get the equation
ZY2=X3+aZ2+bZ3ZY^2 = X^3 + aZ^2 + bZ^3
If we want to sum PP and QQ to yield R=P+QR = P + Q in projective space, we can use the formulae:

ZR=ZPZQ(XPZQ−XQZP)3Z_R = Z_PZ_Q(X_PZ_Q-X_QZ_P)^3 XR=(XPZQ−XQZP)(ZQZP(YPZQ−YQZP)2−(XPZQ−XQZP)2(XPZQ+XQZP))X_R = (X_PZ_Q-X_QZ_P)(Z_QZ_P(Y_PZ_Q-Y_QZ_P)^2 - (X_PZ_Q-X_QZ_P)^2(X_PZ_Q+X_QZP)) YR=ZPZQ(XQYP−XPYQ)(XPZQ−XQZP)2−(YPZQ−YQZP)AY_R = Z_PZ_Q(X_QY_P-X_PY_Q)(X_PZ_Q-X_QZ_P)^2 - (Y_PZ_Q-Y_QZ_P)A A=ZPZQ(YPZQ−YQZP)2−(XPZQ+XQZP)(XPZQ−XQZP)2A = Z_PZ_Q(Y_PZ_Q-Y_QZ_P)^2 - (X_PZ_Q+X_QZ_P)(X_PZ_Q-X_QZ_P)^2.

This looks more complicated and difficult than the simple formulae for 2 dimensional (2-d) space. However, we do not have to calculate any inverses! To get the sum, we have to perform 12 multiplications and 2 squarings. In 2-d, we have 2 multiplications, one squaring and one inversion. Inversions can be 20 times or more expensive than multiplications, so we've saved at least 10 multiplications (some authors say inversions are about 80 times more expensive than multiplications).

Some curves can go even faster. If x3+ax+bx^3 + ax + b has a solution in Fp\mathcal{F_p}, we can work with an equivalent Jacobi quartic v2=a′u4+du2+1v^2 = a^\prime u^4 + du^2 + 1, where a′a^\prime and dd depend on the root. We can transform the curve (u,v)(u,v) to 3-d space (U,V,W)(U,V,W) using u=U/Wu = U/W and v=V/W2v = V / W^2 and get the equation

V2=a′U4+dU2W2+W4V^2 = a^\prime U^4 + dU^2 W^2 + W^4

If we want to sum P3=P1+P2P_3 = P_1+P_2, in these coordinates we have:

U3=U1W1V2+U2W2V1U_3 = U_1W_1V_2 + U_2W_2V_1 V3=((W1W2)2+a′(U1U2)2)(V1V2+dU1U2W1W2)+2a′U1U2W1W2(U12W22+U22W12)V_3 = ((W_1 W_2)^2 + a^\prime (U_1 U_2)^2)(V_1 V_2 + dU_1 U_2 W_1 W_2) + 2a^\prime U_1 U_2 W_1 W_2 ({U_1}^2 {W_2}^2 + {U_2}^2 {W_1}^2) W3=(W1W2)2−a′(U1U2)2W_3 = (W_1 W_2)^2 - a^\prime (U_1 U_2)^2

These allow us to further reduce the costs for adding to 6 multiplications and 4 squarings. Other models with fast implementations are Edwards curves and Montgomery curves, which have some of the fastest implementations.

Montgomery curves satisfy the following equation

By2=x3+Ax2+xBy^2 = x^3 + Ax^2 + x
where B(A2−4)≠0B(A^2-4)\neq 0. This expression can be cast in the Weierstrass form by making some transformation. If we take (x,y)(x,y) and map it to (x′,y′)(x^\prime , y^\prime) given by (x,y)→(x/B+A/3B,y/B)(x,y) \rightarrow (x/B+A/3B,y/B), we get
y2=x3+(3−A23B2)x+2A3−9A27B3y^2 = x^3 + \left(\frac{3 - A^2}{ 3B^2 }\right)x + \frac{2A^3 - 9A}{27B^3}
Transforming a Weierstrass curve into a Montgomery curve is not always possible, though. The order of the group must be divisible by $4$ and x3+ax+b=0x^3 + ax + b = 0 must have a solution.

Montgomery curves can also be related to twisted Edwards curves, which obey the following equation

ax2+y2=1+dx2y2ax^2 + y^2 = 1+dx^2 y^2
The parameters are related via A=2(a+d)/(a−d)A=2(a+d)/(a-d) and B=4/(a−d)B=4/(a-d). We say these two curves are birrationally equivalent. For example, the well-known Edwards curve 25519, with p=2255−19p = 2^{255}-19 is (birrationally) equivalent to the Montgomery curve t2=u3+486662u2+ut^2 = u^3 + 486662u^2 + u. The mappings are (x,y)=(−486664u/t,(u−1)/(u+1))(x,y) = (\sqrt{-486664}u/t,(u-1)/(u+1)) (u,t)=((1+y)/(1−y),−486664(1+y)/(x(1−y)))(u,t) = ((1+y)/(1-y),\sqrt{-486664}(1+y)/(x(1-y)))

Montgomery curves have some interesting properties that lend themselves to constant time implementation. We can work in projective coordinates just using the xx component, with the transformation x=X/Zx=X/Z. Doubling a point takes the simple form: 4R=(X1+Z1)2−(X1−Z1)24R = (X_1+Z_1)^2 - (X_1-Z_1)^2 X2=(X1+Z1)2(X1−Z1)2X_2 = (X_1+Z_1)^2 (X_1-Z_1)^2 Z2=R((X1−Z1)2+((A+2)/4)R)Z_2 = R((X_1-Z_1)^2 + ((A+2)/4)R)

Twisted Edwards curves have there advantages, too. The expressions for point addition and doubling are the same. Given P1=(x1,y1)P_1 = (x_1 , y_1), P2=(x2,y2)P_2 = (x_2,y_2) we get x3=x1y2+x2y11+dx1x2y1y2x_3 = \frac{x_1y_2 + x_2y_1}{1 + dx_1x_2y_1y_2} y3=y1y2−ax1x21−dx1x2y1y2y_3 = \frac{y_1y_2 - ax_1x_2}{1 - dx_1x_2y_1y_2} If we let x1=x2x_1 = x_2 and y1=y2y_1 = y_2 we get the expressions for point doubling. There are several alternatives to speeding up the calculations, such as projective, inverted or extended coordinates.

There are some other tricks to add and multiply points over elliptic curves, such as the technique by Gallant, Lambert and Vanstone (GLV) and generalized by Galbraith, Lin and Scott (GLS).

Summary

Elliptic curves have gained acceptance in cryptography because they offer good levels of security with short key lengths and allow for faster implementations than other methods such as RSA. This allows smartphones and other less powerful devices to perform cryptographic operations in a fast and reliable way.

Using the chord-and-tangent method, we can generate finite cyclic groups; in applications, we are generally interested in calculating kgkg, where kk is an integer and gg is a point in the elliptic curve. The main drawback is that we need to find multiplicative inverses of field elements, which involve many multiplications.

We can improve the speed of these computations by performing transformations between curves (for example, taking a Weierstrass curve to Montgomery form) and using projective coordinates. This way, we avoid calculating multiplicative inverses at each step, at the expense of a few extra multiplications (this extra cost is usually negligible to the overall cost of the inversion). There are also more advanced techniques allowing us to jump from one point to a very distant one, such as with GLS.