cryptotools/examples/elliptic/curve_scalar.py

45 lines
953 B
Python

#!/usr/bin/env python3
import matplotlib.pyplot as plt
from Cryptotools.Groups.curve import Curve
from Cryptotools.Groups.point import Point
a = 3
b = 8
curve = Curve(a, b, Curve.WEIERSTRASS)
x = curve.x
curve.generatePoints()
y = curve.y
yn = curve.yn
points = curve.getPoints()
P = points[10]
nP = curve.scalar(P, 5)
print(f"{nP.x} {nP.y}")
# For testing, we may add n times with the addition operation for the Scalar Multiplication
tmp = Point(0, 0)
for i in range(5):
tmp = curve.add(P, tmp)
# Unfortunately, the result is approximatively the same
# the multiplication need to be more accurate
# if tmp == nP:
# print(True)
plt.figure(figsize=(10, 6))
plt.plot(x, y, color='b', label=f'$y^2 = x^3 + {a}x + {b}$')
plt.plot(x, yn, color='b', )
plt.plot(P.x, P.y, marker='o', color="red")
plt.annotate('P', (P.x, P.y + 0.5))
plt.plot(nP.x, nP.y, marker='o', color="red")
plt.annotate('nP', (nP.x, nP.y + 0.5))
plt.legend()
plt.show()