Cs · Computer Science

How to Compute the Modular Inverse in Elliptic-Curve Addition

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the denominator

    Use the difference in x-coordinates, or twice the y-coordinate when doubling a point.

  2. 2. Run extended Euclid

    Find integers that express the denominator and the prime modulus as a linear combination.

  3. 3. Reduce the coefficient

    The coefficient of the denominator, reduced modulo p, is its multiplicative inverse.

  4. 4. Finish the point formula

    Multiply the slope numerator by that inverse modulo p, then compute the resulting coordinates.

When you add elliptic-curve points over a finite field, the slope formula contains a division. Since field elements are calculated modulo a prime pp, that division means multiplication by a modular inverse. The extended Euclidean algorithm computes the inverse.

This is not the same as finding the inverse point of a curve point. The modular inverse is used to calculate the slope; the inverse point is the point that adds to the identity.

The short-Weierstrass formulas below assume an odd prime pp and a nonsingular curve, so 4a3+27b2≢0(modp)4a^3+27b^2\not\equiv0\pmod p.

What needs to be inverted?

For distinct points P=(x1,y1)P=(x_1,y_1) and Q=(x2,y2)Q=(x_2,y_2) on a short Weierstrass curve y2=x3+ax+by^2=x^3+ax+b over the prime field Fp\mathbb{F}_p, when x2≠x1x_2\ne x_1, the slope is

λ=(y2−y1)(x2−x1)−1(modp).\lambda = (y_2-y_1)(x_2-x_1)^{-1} \pmod p.

The notation (x2−x1)−1(x_2-x_1)^{-1} means the multiplicative inverse of x2−x1x_2-x_1 modulo pp. It is the residue uu satisfying

u(x2−x1)≡1(modp).u(x_2-x_1) \equiv 1 \pmod p.

When doubling a point P=(x1,y1)P=(x_1,y_1) with y1≠0y_1\ne0, use the tangent slope instead:

λ=(3x12+a)(2y1)−1(modp).\lambda = (3x_1^2+a)(2y_1)^{-1} \pmod p.

After finding the slope, the coordinates of the sum are

x3=λ2−x1−x2(modp),y3=λ(x1−x3)−y1(modp).x_3 = \lambda^2-x_1-x_2 \pmod p, \qquad y_3=\lambda(x_1-x_3)-y_1 \pmod p.

All arithmetic, including subtraction and multiplication, is reduced modulo pp.

Find the inverse with extended Euclid

For an integer dd not divisible by prime pp, the Euclidean algorithm finds the greatest common divisor of dd and pp. The extended version also finds integers uu and vv such that

ud+vp=gcd⁡(d,p).ud+vp=\gcd(d,p).

Because pp is prime and dd is nonzero modulo pp, the gcd is 11. Reducing the equation modulo pp gives ud≡1(modp)ud\equiv1\pmod p, so uu reduced modulo pp is the inverse of dd.

For example, to invert 55 modulo 1717:

17=3⋅5+2,5=2⋅2+1.\begin{aligned} 17 &= 3\cdot 5+2,\\ 5 &= 2\cdot 2+1. \end{aligned}

Back-substitute to express 11 using 55 and 1717:

1=5−2(17−3⋅5)=7⋅5−2⋅17.1=5-2(17-3\cdot5)=7\cdot5-2\cdot17.

Thus 7⋅5≡1(mod17)7\cdot5\equiv1\pmod{17}, and 5−1≡7(mod17)5^{-1}\equiv7\pmod{17}. You can verify it directly: 5⋅7=35≡1(mod17)5\cdot7=35\equiv1\pmod{17}.

Worked point-addition example

Consider the curve y2=x3+2x+2y^2=x^3+2x+2 over F17\mathbb{F}_{17} and the points P=(5,1)P=(5,1) and R=(10,6)R=(10,6). Both lie on the curve. For instance, at PP, the right side is 53+2⋅5+2=137≡1=12(mod17)5^3+2\cdot5+2=137\equiv1=1^2\pmod{17}.

The slope denominator is xR−xP=10−5=5x_R-x_P=10-5=5. From the Euclidean calculation above, its inverse modulo 1717 is 77. Therefore

λ=(6−1)⋅7=35≡1(mod17).\lambda=(6-1)\cdot7=35\equiv1\pmod{17}.

Now calculate the sum coordinates, reducing each result modulo 1717:

x3=12−5−10=−14≡3(mod17),y3=1(5−3)−1=1(mod17).\begin{aligned} x_3 &= 1^2-5-10=-14\equiv3\pmod{17},\\ y_3 &= 1(5-3)-1=1\pmod{17}. \end{aligned}

So P+R=(3,1)P+R=(3,1). The inverse was needed only to evaluate the slope; after that, the ordinary point-addition formulas give the result using modular arithmetic.

Common mistakes and special cases

  • Using an ordinary fraction: Dividing by 55 modulo 1717 does not mean using the real number 1/51/5. It means multiplying by 5−1≡7(mod17)5^{-1}\equiv7\pmod{17}.
  • Forgetting to reduce modulo pp: Reduce intermediate results as needed. For example, 3535 is 11 modulo 1717.
  • Confusing two kinds of inverse: The additive inverse of P=(x,y)P=(x,y) is −P=(x,−y mod p)-P=(x,-y\bmod p). On the example curve, −(5,1)=(5,16)-(5,1)=(5,16) over F17\mathbb{F}_{17}.
  • Trying to invert zero: If distinct-point addition has x2=x1x_2=x_1, the denominator is zero. For distinct points on the curve, this means they are negatives of each other, and their sum is the point at infinity, the identity. In doubling, if y1=0y_1=0, the tangent is vertical and 2P2P is the point at infinity.
  • Applying the prime-field rule to a composite modulus: A nonzero number modulo a composite nn may not have an inverse. In general, an inverse exists exactly when gcd⁡(d,n)=1\gcd(d,n)=1. Elliptic-curve cryptographic groups commonly use a prime field, where every nonzero denominator has an inverse.

In code, an extended-Euclidean routine can return the inverse as a signed integer; reduce that result into the range 0,…,p−10,\ldots,p-1 before using it. A denominator that is zero modulo pp is a point-addition special case, not an input for modular inversion.

References

  1. Guide to Elliptic Curve Cryptography, Darrel Hankerson, Alfred Menezes, and Scott Vanstone
  2. A Course in Computational Algebraic Number Theory, Henri Cohen

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.