Project Nayuki


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 \(F\) – such as real numbers (\(\mathbb{R}\)), rational numbers (\(\mathbb{Q}\)), or integers modulo a prime (\(\mathbb{Z}_p\)). 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 \(a\) and \(b\), an elliptic curve is defined by the set of all points \((x, y)\) that satisfy the equation \(y^2 = x^3 + ax + b\), plus a special “point at infinity” named \(O\).

Point doubling

To compute \(2P\) (or \(P + P\)), there are three cases:

  • If \(P = O\), then \(2P = O\).

  • Else \(P = (x, y)\):

    • If \(y = 0\), then \(2P = O\).

    • Else \(y ≠ 0\), then let \(s = (3x^2 + a)/(2y)\), let \(x_2 = s^2 - 2x\), let \(y_2 = s(x - x_2) - y\), and \(2P = (x_2, y_2)\).

As we can see, the complicated case involves approximately 6 multiplications, 4 additions/subtractions, and 1 division.

Point addition

To compute \(P + Q\) where \(P ≠ Q\), there are four cases:

  • If \(P = O\), then \(P + Q = Q\). Likewise if \(Q = O\), then \(P + Q = P\).

  • Else \(P = (x_0, y_0)\) and \(Q = (x_1, y_1)\):

    • If \(x_0 = x_1\) (and necessarily \(y_0 ≠ y_1\)), then \(P + Q = O\).

    • Else \(x_0 ≠ x_1\), then let \(s = (y_0 - y_1) / (x_0 - x_1)\), let \(x_2 = s^2 - x_0 - x_1\), let \(y_2 = s(x_0 - x_2) - y_0\), and \(P + Q = (x_2, y_2)\).

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)\). In the new projective form, each point will have 3 coordinates, like \((X, Y, Z)\), with the restriction that \(Z\) is never zero. The forward mapping is given by \((x, y) → (xz, yz, z)\), for any non-zero \(z\) (usually chosen to be \(1\) for convenience). The reverse mapping is given by \((X, Y, Z) → (X/Z, Y/Z)\), as long as \(Z\) is non-zero.

Point doubling

The affine form test \(y = 0\) corresponds to the projective form test \(Y/Z = 0\). This is equivalent to \(Y = 0\), since \(Z ≠ 0\).

For the interesting case where \(P = (X, Y, Z)\) and \(Y ≠ 0\), let’s convert the affine arithmetic to projective arithmetic.

Expand and simplify:

\(\begin{align} s &= \frac{3x^2 + a}{2y} \\ &= \frac{3(X/Z)^2 + a}{2(Y/Z)} \\ &= \frac{3(X/Z)^2 + a}{2(Y/Z)} ⋅ \frac{Z^2}{Z^2} \\ &= \frac{3X^2 + aZ^2}{2YZ}. \end{align}\)

Let \(T = 3X^2 + aZ^2\).

Let \(U = 2YZ\).

Substitute \(s = \displaystyle\frac{T}{U}\).

Expand and simplify:

\(\begin{align} x_2 &= s^2 - 2x \\ &= \left( \frac{T}{U} \right)^2 - 2\frac{X}{Z} \\ &= \frac{T^2}{U^2} - \frac{2X}{Z} ⋅ \frac{4Y^2 Z}{4Y^2 Z} \\ &= \frac{T^2}{U^2} - \frac{8X Y^2 Z}{4Y^2 Z^2} \\ &= \frac{T^2}{U^2} - \frac{4UXY}{U^2} \\ &= \frac{T^2 - 4UXY}{U^2}. \end{align}\)

Let \(V = 2UXY\).

Let \(W = T^2 - 2V\).

Substitute \(x_2 = \displaystyle\frac{W}{U^2}\).

Expand and simplify:

\(\begin{align} y_2 &= s(x - x_2) - y \\ &= \frac{T}{U} \left( \frac{X}{Z} - \frac{W}{U^2} \right) - \frac{Y}{Z} \\ &= \frac{T}{U} \left( \frac{X}{Z} ⋅ \frac{4Y^2 Z}{4Y^2 Z} - \frac{W}{U^2} \right) - \frac{Y}{Z} \\ &= \frac{T}{U} \left( \frac{4X Y^2 Z}{4Y^2 Z^2} - \frac{W}{U^2} \right) - \frac{Y}{Z} \\ &= \frac{T}{U} \left( \frac{V}{U^2} - \frac{W}{U^2} \right) - \frac{Y}{Z} \\ &= \frac{T}{U} \left( \frac{V - W}{U^2} \right) - \frac{Y}{Z} \\ &= \frac{T(V - W)}{U^3} - \frac{Y}{Z} \\ &= \frac{T(V - W)}{U^3} - \frac{Y}{Z} ⋅ \frac{8Y^3 Z^2}{8Y^3 Z^2} \\ &= \frac{T(V - W)}{U^3} - \frac{8Y^4 Z^2}{8Y^3 Z^3} \\ &= \frac{T(V - W)}{U^3} - \frac{2U^2 Y^2}{U^3} \\ &= \frac{T(V - W) - 2U^2 Y^2}{U^3} \\ &= \frac{T(V - W) - 2(UY)^2}{U^3}. \end{align}\)

Adjust denominator: \(x_2 = \displaystyle\frac{W}{U^2} ⋅ \frac{U}{U} = \frac{UW}{U^3}.\)

Now that \(x_2\) and \(y_2\) have the same denominator, we can write:

\(\begin{align} X_2 &= UW. \\ Y_2 &= T(V - W) - 2(UY)^2. \\ Z_2 &= U^3. \end{align}\)

As we can see, the complicated case involves approximately 18 multiplications, 4 additions/subtractions, and 0 divisions.

Point addition

The affine form test \(x_0 = x_1\) corresponds to the projective form test \(X_0/Z_0 = X_1/Z_1\). This is equivalent to \(X_0 Z_1 = X_1 Z_0\), via cross-multiplication.

For the interesting case where \(P = (X_0, Y_0, Z_0)\), \(Q = (X_1, Y_1, Z_1)\), and \(X_0 Z_1 ≠ X_1 Z_0\), let’s convert the affine arithmetic to projective arithmetic.

Expand and simplify:

\(\begin{align} s &= \frac{y_0 - y_1}{x_0 - x_1} \\ &= \frac{Y_0/Z_0 - Y_1/Z_1}{X_0/Z_0 - X_1/Z_1} \\ &= \frac{Y_0/Z_0 - Y_1/Z_1}{X_0/Z_0 - X_1/Z_1} ⋅ \frac{Z_0 Z_1}{Z_0 Z_1} \\ &= \frac{Y_0 Z_1 - Y_1 Z_0}{X_0 Z_1 - X_1 Z_0}. \end{align}\)

Let \(T_0 = Y_0 Z_1\).

Let \(T_1 = Y_1 Z_0\).

Let \(T = T_0 - T_1\).

Let \(U_0 = X_0 Z_1\).

Let \(U_1 = X_1 Z_0\).

Let \(U = U_0 - U_1\).

Substitute \(s = \displaystyle\frac{T}{U}\).

Expand and simplify:

\(\begin{align} x_2 &= s^2 - x_0 - x_1 \\ &= \left( \frac{T}{U} \right)^2 - \frac{X_0}{Z_0} - \frac{X_1}{Z_1} \\ &= \frac{T^2}{U^2} ⋅ \frac{Z_0 Z_1}{Z_0 Z_1} - \frac{X_0}{Z_0} ⋅ \frac{U^2 Z_1}{U^2 Z_1} - \frac{X_1}{Z_1} ⋅ \frac{U^2 Z_0}{U^2 Z_0} \\ &= \frac{T^2 Z_0 Z_1 - U^2 X_0 Z_1 - U^2 X_1 Z_0}{U^2 Z_0 Z_1} \\ &= \frac{T^2 Z_0 Z_1 - U^2 (U_0 + U_1)}{U^2 Z_0 Z_1}. \end{align}\)

Let \(U_2 = U^2\).

Let \(V = Z_0 Z_1\).

Let \(W = T^2 V - U_2 (U_0 + U_1)\).

Substitute \(x_2 = \displaystyle\frac{W}{U_2 V}\).

Expand and simplify:

\(\begin{align} y_2 &= s(x_0 - x_2) - y_0 \\ &= \frac{T}{U} \left[ \frac{X_0}{Z_0} - \frac{W}{U_2 V} \right] - \frac{Y_0}{Z_0} \\ &= \frac{T}{U} \left[ \frac{X_0}{Z_0} ⋅ \frac{U_2 Z_1}{U_2 Z_1} - \frac{W}{U_2 V} \right] - \frac{Y_0}{Z_0} \\ &= \frac{T}{U} \left[ \frac{U_0 U_2}{U_2 V} - \frac{W}{U_2 V} \right] - \frac{Y_0}{Z_0} \\ &= \frac{T(U_0 U_2 - W)}{U U_2 V} - \frac{Y_0}{Z_0} \\ &= \frac{T(U_0 U_2 - W)}{U U_2 V} - \frac{Y_0}{Z_0} ⋅ \frac{U U_2 Z_1}{U U_2 Z_1} \\ &= \frac{T(U_0 U_2 - W)}{U U_2 V} - \frac{U U_2 Y_0 Z_1}{U U_2 V} \\ &= \frac{T(U_0 U_2 - W) - T_0 U U_2}{U U_2 V}. \end{align}\)

Let \(U_3 = U U_2\).

Substitute: \(y_2 = \displaystyle\frac{T(U_0 U_2 - W) - T_0 U_3}{U_3 V}\).

Adjust denominator: \(x_2 = \displaystyle\frac{W}{U_2 V} ⋅ \frac{U}{U} = \frac{UW}{U U_2 V} = \frac{UW}{U_3 V}\).

Now that \(x_2\) and \(y_2\) have the same denominator, we can write:

\(\begin{align} X_2 &= UW. \\ Y_2 &= T(U_0 U_2 - W) - T_0 U_3. \\ Z_2 &= U_3 V. \end{align}\)

As we can see, the complicated case involves approximately 15 multiplications, 6 additions/subtractions, and 0 divisions.

Source code

More info