https://www.nayuki.io/page/elliptic-curve-point-addition-in-projective-coordinates
Introduction
Elliptic curves are a mathematical concept that is useful for cryptography, such as in SSL/TLS and Bitcoin. Using the so-called “group law”, it is easy to “add” points together and to “multiply” a point by an integer, but very hard to work backwards to “divide” a point by a number; this asymmetry is the basis for elliptic curve cryptography.
The coordinates of an elliptic curve point are values drawn from a field FF – such as real numbers (ℝR), rational numbers (ℚQ), or integers modulo a prime (ℤpZp). Because the values are from a field, they can be divided. Unfortunately, scalar division is generally an expensive operation, especially for finite fields modulo a prime. Adding two elliptic curve points together will perform (at least) one field division, and multiplying a curve point by an integer can involve hundreds of divisions. This page describes an algebraic trick to trade off these division operations for many more multiplication and addition operations, and only perform one division at the very end of a computation. This fancy logic can result in a 10× speed-up in real-world code (e.g. 256-bit ECDSA).
Arithmetic in affine coordinates
Affine coordinates are the conventional way of expressing elliptic curve points, which uses 2 coordinates. The math is concise and easy to follow.
For a pair of constants aa and bb, an elliptic curve is defined by the set of all points (x,y)(x,y) that satisfy the equation y2=x3+ax+by2=x3+ax+b, plus a special “point at infinity” named OO.
Point doubling
To compute 2P2P (or P+PP+P), there are three cases:
-
If P=OP=O, then 2P=O2P=O.
-
Else P=(x,y)P=(x,y):
-
If y=0y=0, then 2P=O2P=O.
-
Else y≠0y≠0, then let s=(3x2+a)/(2y)s=(3x2+a)/(2y), let x2=s2−2xx2=s2−2x, let y2=s(x−x2)−yy2=s(x−x2)−y, and 2P=(x2,y2)2P=(x2,y2).
-
As we can see, the complicated case involves approximately 6 multiplications, 4 additions/subtractions, and 1 division.
Point addition
To compute P+QP+Q where P≠QP≠Q, there are four cases:
-
If P=OP=O, then P+Q=QP+Q=Q. Likewise if Q=OQ=O, then P+Q=PP+Q=P.
-
Else P=(x0,y0)P=(x0,y0) and Q=(x1,y1)Q=(x1,y1):
-
If x0=x1x0=x1 (and necessarily y0≠y1y0≠y1), then P+Q=OP+Q=O.
-
Else x0≠x1x0≠x1, then let s=(y0−y1)/(x0−x1)s=(y0−y1)/(x0−x1), let x2=s2−x0−x1x2=s2−x0−x1, let y2=s(x0−x2)−y0y2=s(x0−x2)−y0, and P+Q=(x2,y2)P+Q=(x2,y2).
-
As we can see, the complicated case involves approximately 2 multiplications, 6 additions/subtractions, and 1 division.
Arithmetic in projective coordinates
The key idea of projective coordinates is that instead of performing every division immediately, we defer the divisions by multiplying them into a denominator. The denominator is represented by a new coordinate. Only at the very end do we perform a single division to convert from projective coordinates back to affine coordinates.
In affine form, each elliptic curve point has 2 coordinates, like (x,y)(x,y). In the new projective form, each point will have 3 coordinates, like (X,Y,Z)(X,Y,Z), with the restriction that ZZ is never zero. The forward mapping is given by (x,y)→(xz,yz,z)(x,y)→(xz,yz,z), for any non-zero zz (usually chosen to be 11 for convenience). The reverse mapping is given by (X,Y,Z)→(X/Z,Y/Z)(X,Y,Z)→(X/Z,Y/Z), as long as ZZ is non-zero.
Point doubling
The affine form test y=0y=0 corresponds to the projective form test Y/Z=0Y/Z=0. This is equivalent to Y=0Y=0, since Z≠0Z≠0.
For the interesting case where P=(X,Y,Z)P=(X,Y,Z) and Y≠0Y≠0, let’s convert the affine arithmetic to projective arithmetic.
Expand and simplify:
s=3x2+a2y=3(X/Z)2+a2(Y/Z)=3(X/Z)2+a2(Y/Z)·Z2Z2=3X2+aZ22YZ.s=3x2+a2y=3(X/Z)2+a2(Y/Z)=3(X/Z)2+a2(Y/Z)·Z2Z2=3X2+aZ22YZ.
Let T=3X2+aZ2T=3X2+aZ2.
Let U=2YZU=2YZ.
Substitute s=TUs=TU.
Expand and simplify:
x2=s2−2x=(TU)2−2XZ=T2U2−2XZ·4Y2Z4Y2Z=T2U2−8XY2Z4Y2Z2=T2U2−4UXYU2=T2−4UXYU2.x2=s2−2x=(TU)2−2XZ=T2U2−2XZ·4Y2Z4Y2Z=T2U2−8XY2Z4Y2Z2=T2U2−4UXYU2=T2−4UXYU2.
Let V=2UXYV=2UXY.
Let W=T2−2VW=T2−2V.
Substitute x2=WU2x2=WU2.
Expand and simplify:
y2=s(x−x2)−y=TU(XZ−WU2)−YZ=TU(XZ·4Y2Z4Y2Z−WU2)−YZ=TU(4XY2Z4Y2Z2−WU2)−YZ=TU(VU2−WU2)−YZ=TU(V−WU2)−YZ=T(V−W)U3−YZ=T(V−W)U3−YZ·8Y3Z28Y3Z2=T(V−W)U3−8Y4Z28Y3Z3=T(V−W)U3−2U2Y2U3=T(V−W)−2U2Y2U3=T(V−W)−2(UY)2U3.y2=s(x−x2)−y=TU(XZ−WU2)−YZ=TU(XZ·4Y2Z4Y2Z−WU2)−YZ=TU(4XY2Z4Y2Z2−WU2)−YZ=TU(VU2−WU2)−YZ=TU(V−WU2)−YZ=T(V−W)U3−YZ=T(V−W)U3−YZ·8Y3Z28Y3Z2=T(V−W)U3−8Y4Z28Y3Z3=T(V−W)U3−2U2Y2U3=T(V−W)−2U2Y2U3=T(V−W)−2(UY)2U3.
Adjust denominator: x2=WU2·UU=UWU3.x2=WU2·UU=UWU3.
Now that x2x2 and y2y2 have the same denominator, we can write:
X2Y2Z2=UW.=T(V−W)−2(UY)2.=U3.X2=UW.Y2=T(V−W)−2(UY)2.Z2=U3.
As we can see, the complicated case involves approximately 18 multiplications, 4 additions/subtractions, and 0 divisions.
Point addition
The affine form test x0=x1x0=x1 corresponds to the projective form test X0/Z0=X1/Z1X0/Z0=X1/Z1. This is equivalent to X0Z1=X1Z0X0Z1=X1Z0, via cross-multiplication.
For the interesting case where P=(X0,Y0,Z0)P=(X0,Y0,Z0), Q=(X1,Y1,Z1)Q=(X1,Y1,Z1), and X0Z1≠X1Z0X0Z1≠X1Z0, let’s convert the affine arithmetic to projective arithmetic.
Expand and simplify:
s=y0−y1x0−x1=Y0/Z0−Y1/Z1X0/Z0−X1/Z1=Y0/Z0−Y1/Z1X0/Z0−X1/Z1·Z0Z1Z0Z1=Y0Z1−Y1Z0X0Z1−X1Z0.s=y0−y1x0−x1=Y0/Z0−Y1/Z1X0/Z0−X1/Z1=Y0/Z0−Y1/Z1X0/Z0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U3U3V.
Adjust denominator: x2=WU2V·UU=UWUU2V=UWU3Vx2=WU2V·UU=UWUU2V=UWU3V.
Now that x2x2 and y2y2 have the same denominator, we can write:
X2Y2Z2=UW.=T(U0U2−W)−T0U3.=U3V.X2=UW.Y2=T(U0U2−W)−T0U3.Z2=U3V.
As we can see, the complicated case involves approximately 15 multiplications, 6 additions/subtractions, and 0 divisions.
Source code
- ellipticcurve.py (library)
- ellipticcurve-test.py (unit tests)
More info
- Wikibooks: Cryptography/Prime Curve/Standard Projective Coordinates
- Wikipedia: Elliptic curve - The group law