elliptic curve projective
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·8Y3Z28Y3