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.

The theory behind point addition

To add two points PPand 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.

ā€‹

Implementation in Python

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)

Resources

ā€‹