Point Addition in Python
Recall from the discussion in Group Theory, we learnt how a generator point can be added to itself repeatedly to generate every element of the group. In this section, we'll understand how to perform this addition, and implement it in Python.

To add two points
PP
and
QQ
on an elliptic curve, find the third point
RR
where line joining
PP
and
QQ
intersects. This value of
RR
is equal to
โˆ’(P+Q)-(P+Q)
. Reflecting the point along the X-axis will give us
P+QP+Q
.
Addition of two points on an elliptic curve over a field of real numbers.
To find the coordinates of the third point of intersection, simply calculate the slope between P and Q, and extrapolate it using the general equation of elliptic curve.
Addition of two points on an elliptic curve over a finite field.

inf = float("inf")
โ€‹
โ€‹
class Point:
... # add these methods to the previously defined Point class
def __add__(self, other):
#################################################################
# Point Addition for Pโ‚ or Pโ‚‚ = I (identity) #
# #
# Formula: #
# P + I = P #
# I + P = P #
#################################################################
if self == I:
return other
โ€‹
if other == I:
return self
โ€‹
#################################################################
# Point Addition for Xโ‚ = Xโ‚‚ (additive inverse) #
# #
# Formula: #
# P + (-P) = I #
# (-P) + P = I #
#################################################################
if self.x == other.x and self.y == (-1 * other.y):
return I
โ€‹
#################################################################
# Point Addition for Xโ‚ โ‰  Xโ‚‚ (line with slope) #
# #
# Formula: #
# S = (Yโ‚‚ - Yโ‚) / (Xโ‚‚ - Xโ‚) #
# Xโ‚ƒ = Sยฒ - Xโ‚ - Xโ‚‚ #
# Yโ‚ƒ = S(Xโ‚ - Xโ‚ƒ) - Yโ‚ #
#################################################################
if self.x != other.x:
x1, x2 = self.x, other.x
y1, y2 = self.y, other.y
โ€‹
s = (y2 - y1) / (x2 - x1)
x3 = s ** 2 - x1 - x2
y3 = s * (x1 - x3) - y1
โ€‹
return self.__class__(
x=x3.value,
y=y3.value,
curve=secp256k1
)
โ€‹
#################################################################
# Point Addition for Pโ‚ = Pโ‚‚ (vertical tangent) #
# #
# Formula: #
# S = โˆž #
# (Xโ‚ƒ, Yโ‚ƒ) = I #
#################################################################
if self == other and self.y == inf:
return I
โ€‹
#################################################################
# Point Addition for Pโ‚ = Pโ‚‚ (tangent with slope) #
# #
# Formula: #
# S = (3Xโ‚ยฒ + a) / 2Yโ‚ .. โˆ‚(Yยฒ) = โˆ‚(Xยฒ + aX + b) #
# Xโ‚ƒ = Sยฒ - 2Xโ‚ #
# Yโ‚ƒ = S(Xโ‚ - Xโ‚ƒ) - Yโ‚ #
#################################################################
if self == other:
x1, y1, a = self.x, self.y, self.curve.a
โ€‹
s = (3 * x1 ** 2 + a) / (2 * y1)
x3 = s ** 2 - 2 * x1
y3 = s * (x1 - x3) - y1
โ€‹
return self.__class__(
x=x3.value,
y=y3.value,
curve=secp256k1
)
Point at Infinity
Also known as the identity point, it is the third point where P and Q meet, in the figure below.
โ€‹
P+(โˆ’P)=IP + (-P) = I
Point at infinity is the third point where the line joining P and Q meets the curve.
We can initialise the point at infinity like this:
I = Point(x=None, y=None, curve=secp256k1)

โ€‹
Copy link
On this page