EC Points in Python

Today we’re going to talk about something that might seem like it belongs more in math class than in our beloved programming language EC points. But don’t freak out, because with just a little bit of knowledge and some clever code, you can easily calculate these elusive coordinates right from the comfort of your own terminal (or IDE).

To start: what exactly are EC points? Well, they’re essentially mathematical objects that exist in a specific type of curve called an elliptic curve. These curves have some pretty cool properties for example, they can be used to create secure encryption algorithms and even simulate the behavior of certain physical systems (like quantum mechanics).

But enough about the theory! Let’s get down to business and see how we can use Python to calculate EC points. Here’s a simple script that demonstrates one possible implementation:

# Import necessary libraries
import math
from random import randint

# Define some constants for our elliptic curve (you can adjust these values as needed)
a = 1729
b = 0
p = 3 * 5 * 17 * 2467 # Define a prime number p as the modulus for our elliptic curve
x_base = 8 # Define the x-coordinate of the base point
y_base = int(math.sqrt((p - 1) / 2)) # Calculate the y-coordinate of the base point using the equation y^2 = (x^3 + ax + b) mod p
order = (p - 1) // 2 # Calculate the order of the elliptic curve, which is the number of points on the curve

# Define a function to calculate the x-coordinate of an EC point given its y-coordinate and the base point
def get_x(y):
    # Calculate the difference between the squares of the two possible values for x
    diff = (y ** 2 - y_base ** 2) % p # Use the equation x^2 = (y^2 - y_base^2) mod p
    
    # Check if either value for x is a valid solution to our equation
    while True:
        x1 = randint(0, p-1) # Generate a random x-coordinate
        x2 = (diff + x1**3) % p # Calculate the second possible value for x using the equation x^3 = (diff + x1^3) mod p
        
        # If the difference between these two values is zero, we've found an EC point!
        if ((x2 - x1) * (x2 + x1) % p == 0): # Check if the difference between the two values is zero using the equation (x2 - x1)(x2 + x1) mod p
            return int((x2 + x1) / 2) # Return the x-coordinate of the EC point

# Define a function to calculate the y-coordinate of an EC point given its x-coordinate and the base point
def get_y(x):
    # Calculate the square root of (p - 1) / 4, which is needed for our equation
    sqrt = int((math.sqrt(p - 1)) // 2) # Use integer division to get a whole number
    
    # Check if either value for y is a valid solution to our equation
    while True:
        y1 = randint(-sqrt, sqrt + 1) # Generate a random y-coordinate within the range [-sqrt, sqrt+1]
        y2 = (x * y1 % p) ** 2 - x # Calculate the second possible value for y using the equation y^2 = (x^3 + ax + b) mod p
        
        # If the difference between these two values is zero, we've found an EC point!
        if ((y2 - y1) * (y2 + y1) % p == 0): # Check if the difference between the two values is zero using the equation (y2 - y1)(y2 + y1) mod p
            return int((y2 + y1) / 2) # Return the y-coordinate of the EC point

# Define a function to calculate the order of our elliptic curve (i.e., how many EC points there are in total)
def get_order():
    # Initialize a counter for the number of valid solutions we've found so far
    count = 0
    
    # Loop through all possible x-coordinates and check if they correspond to an EC point
    for i in range(p):
        y1 = get_y(i) # Calculate the y-coordinate of the EC point using the function defined above
        
        # If the difference between these two values is zero, we've found a valid solution!
        if (get_x(y1) == i % p): # Check if the x-coordinate of the EC point matches the current x-coordinate in the loop
            count += 1 # Increment the counter
    
    return order - count # Return the difference between the total number of points and the number of valid solutions

# Run our script and print out some results for fun!
print("Base point: ({}, {})".format(x_base, y_base))
print("Order of curve: {}".format(get_order()))

Of course, this is just one possible approach and there are many other ways to tackle this problem depending on your specific needs (and the size of your computer’s memory!). But hopefully this script gives you a good starting point for exploring the fascinating world of elliptic curves in Python.

Later!

SICORPS