115 lines
2.7 KiB
Python
115 lines
2.7 KiB
Python
from elliptic_curve import *
|
|
import matplotlib as mpl
|
|
from itertools import count
|
|
|
|
plots = []
|
|
|
|
def is_too_far_away(x, y):
|
|
return x > 100 or x < -100 or y > 100 or y < -100
|
|
|
|
def display_elliptic_curve(ax, curve):
|
|
global plots
|
|
plots += ax.plot(*curve.points(), color='red')
|
|
|
|
def display_elliptic_curve_over_finite_field(ax, curve):
|
|
global plots
|
|
xs, ys = curve.points()
|
|
plots += [
|
|
ax.axhline(curve.mod, color='black', linewidth=1.2),
|
|
ax.axvline(curve.mod, color='black', linewidth=1.2),
|
|
]
|
|
plots += [ax.scatter(
|
|
xs,
|
|
ys,
|
|
color='red', s=20, marker='o'
|
|
)]
|
|
|
|
def double_elliptic_curve(ax, curve, x, y):
|
|
global plots
|
|
p = curve.double(x, y)
|
|
plots += ax.plot([x], [y], 'o')
|
|
plots += [ax.axline(
|
|
(x, y),
|
|
(p[0], -p[1]),
|
|
linestyle='--'
|
|
)]
|
|
if not is_too_far_away(*p):
|
|
plots += [ax.axline(
|
|
p,
|
|
(p[0], p[1] + 1),
|
|
linestyle='--'
|
|
)]
|
|
plots += ax.plot([p[0]], [p[1]], 'o')
|
|
|
|
def double_elliptic_curve_over_finite_field(ax, curve, x, y):
|
|
pass
|
|
|
|
def clear():
|
|
global plots
|
|
for i in plots:
|
|
i.remove()
|
|
plots = []
|
|
|
|
def addition_elliptic_curve(ax, curve, x1, y1, x2, y2):
|
|
global plots
|
|
p = curve.add(x1, y1, x2, y2)
|
|
plots += [ax.axline(
|
|
(x1, y1),
|
|
(x2, y2),
|
|
linestyle='--'
|
|
)]
|
|
xa = [x1, x2]
|
|
ya = [y1, y2]
|
|
if not is_too_far_away(*p):
|
|
xa.append(p[0])
|
|
ya.append(p[1])
|
|
plots += [ax.axline(
|
|
(p[0], p[1]),
|
|
(p[0], p[1] + 1),
|
|
linestyle='--'
|
|
)]
|
|
plots += ax.plot(xa, ya, 'o')
|
|
|
|
def addition_elliptic_curve_over_finite_field(ax, curve, x1, y1, x2, y2):
|
|
global plots
|
|
p = curve.add(x1, y1, x2, y2)
|
|
plots += ax.plot([x1, x2], [y1, y2], 'x', markersize=12, color='red')
|
|
plots += ax.plot(p[0], p[1], 'x', markersize=12, color='blue')
|
|
ohdiff = x2 - x1
|
|
ovdiff = y2 - y1
|
|
for i in range(4):
|
|
x1, y1, x2, y2 = curve.bound_intersections(x1, y1, x2, y2)
|
|
plots += ax.plot(
|
|
(x1, x2),
|
|
(y1, y2),
|
|
linestyle='--',
|
|
color = 'yellow',
|
|
)
|
|
if y2 == 0:
|
|
new_x1 = x2
|
|
new_y1 = curve.mod
|
|
else:
|
|
new_x1 = 0
|
|
new_y1 = y2
|
|
new_x2 = new_x1 + ohdiff
|
|
new_y2 = new_y1 + ovdiff
|
|
x1, y1, x2, y2 = new_x1, new_y1, new_x2, new_y2
|
|
|
|
def addition(ax, curve, x1, y1, x2, y2):
|
|
if isinstance(curve, EllipticCurve):
|
|
addition_elliptic_curve(ax, curve, x1, y1, x2, y2)
|
|
elif isinstance(curve, EllipticCurveOverFiniteField):
|
|
addition_elliptic_curve_over_finite_field(ax, curve, x1, y1, x2, y2)
|
|
|
|
def double(ax, curve, x, y):
|
|
if isinstance(curve, EllipticCurve):
|
|
double_elliptic_curve(ax, curve, x, y)
|
|
elif isinstance(curve, EllipticCurveOverFiniteField):
|
|
double_elliptic_curve_over_finite_field(ax, curve, x, y)
|
|
|
|
def display(ax, curve):
|
|
if isinstance(curve, EllipticCurve):
|
|
display_elliptic_curve(ax, curve)
|
|
elif isinstance(curve, EllipticCurveOverFiniteField):
|
|
display_elliptic_curve_over_finite_field(ax, curve)
|