Skip to content

Instantly share code, notes, and snippets.

@p3nj
Last active May 15, 2024 14:15
Show Gist options
  • Save p3nj/3d1e8f327300b10cd8563d3234147b45 to your computer and use it in GitHub Desktop.
Save p3nj/3d1e8f327300b10cd8563d3234147b45 to your computer and use it in GitHub Desktop.
A Python script generate the Elliptic-curve chart.
import numpy as np
import matplotlib.pyplot as plt
def modular_sqrt(a, p):
"""
Compute the modular square root of a modulo p using the Tonelli-Shanks algorithm.
Returns the two square roots if they exist, or None if no square root exists.
"""
a = int(a) # Convert a to a regular integer
if pow(a, (p-1)//2, p) != 1:
return None
q = p - 1
s = 0
while q % 2 == 0:
q //= 2
s += 1
if s == 1:
return pow(a, (p+1)//4, p)
for z in range(2, p):
if p - 1 == pow(z, (p-1)//2, p):
break
c = pow(z, q, p)
r = pow(a, (q+1)//2, p)
t = pow(a, q, p)
m = s
while (t - 1) % p != 0:
t2 = (t * t) % p
for i in range(1, m):
if (t2 - 1) % p == 0:
break
t2 = (t2 * t2) % p
b = pow(c, 1 << (m - i - 1), p)
r = (r * b) % p
c = (b * b) % p
t = (t * c) % p
m = i
return r, p-r
# Define the elliptic curve parameters
p = 13 # Modulus
a = 5 # Coefficient of x
b = 9 # Constant term
# Generate x values
x = np.arange(0, p)
# Calculate y values
y = []
x_coords = []
for xi in x:
rhs = (int(xi)**3 + a*int(xi) + b) % p
sqrt_rhs = modular_sqrt(rhs, p)
if sqrt_rhs:
y.append(sqrt_rhs[0])
y.append(sqrt_rhs[1])
x_coords.append(xi)
x_coords.append(xi)
sorted_points = sorted(zip(x_coords, y), key=lambda x: x[0])
print("The points on the curve are:")
for i, point in enumerate(sorted_points):
if i % 2 == 0:
print("")
print(point, end=", ")
# Define the points G and H
G = (7, 7)
H = (12, 9)
# Plot the elliptic curve
plt.figure(figsize=(6, 6))
plt.scatter(x_coords, y, label='Elliptic Curve', color='blue', marker='.')
# Plot the points G and H
plt.scatter(G[0], G[1], label='G (7, 7)', color='red', marker='o', s=100)
plt.scatter(H[0], H[1], label='H (12, 9)', color='green', marker='o', s=100)
plt.xlabel('X')
plt.ylabel('Y')
plt.title(f'Elliptic Curve over F_{p}: y^2 = x^3 + {a}x + {b} (mod {p})')
plt.legend()
plt.grid(True)
plt.xticks(range(0, p))
plt.yticks(range(0, p))
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment