Python Program for Integer Elliptic Curves

This section provides simple Python program, IntegerEllipticCurve.py, that searches integer points on any given elliptic curve with integer coefficients.

If you are interested to find the element set of elliptic curve points in 2-dimensional integer space, here is my simple Python program, IntegerEllipticCurve.py, that searches integer points on any given elliptic curve with integer coefficients:

# IntegerEllipticCurve.py
# Copyright (c) HerongYang.com. All Rights Reserved.
#
import sys

if (len(sys.argv) < 3):
   print("Usage: python IntegerEllipticCurve.py a b")
   exit()

aa = sys.argv[1]
bb = sys.argv[2]
a = int(aa)
b = int(bb)

r = 4*a**3 + 27*b**2
if (r == 0):
   print("Error: (a, b) = ("+aa+", "+bb+") and 4a^3 + 27b^2 == 0")
   exit()

print("Elliptic equation: y^2 = x^3 + ax + b with (a, b) = ("
   +aa+", "+bb+")")

# Find a x such that x^3 + ax + b < 0
x = 0
r = x**3 + a*x + b
while (r >= 0):
  x = x - 1
  r = x**3 + a*x + b
print("Starting with: x = "+str(x))

# Find a x such that x^3 + ax + b >= 0
while ( r<0 ):
  x = x + 1
  r = x**3 + a*x + b
print("First candidate: x = "+str(x))

# Check every x < 100000
y = 0
while (x < 100000):
   r = x**3 + a*x + b
   l = y**2

   while (l < r):
      y = y + 1
      l = y**2

   # print("(x, y) = ("+str(x)+", "+str(y)+")")
   # print("(r, l) = ("+str(r)+", "+str(l)+")")
   if (l == r):
      print("Point on curve: (x, y) = ("+str(x)+", "+str(y)+")")

   x = x + 1

print("Ended with (x, y, r, l) = "
   +str(x)+", "+str(y)+", "+str(r)+", "+str(l)+")")
exit()

If you run it with (a,b) = (1,4), you will get:

herong> python IntegerEllipticCurve 1 4

Elliptic equation: y^2 = x^3 + ax + b with (a, b) = (1, 4)
Starting with: x = -2
First candidate: x = -1
Point on curve: (x, y) = (0, 2)
Point on curve: (x, y) = (0, -2)
Point on curve: (x, y) = (4128, 265222)
Point on curve: (x, y) = (4128, -265222)
Ended with (x, y, r, l)
   = 100000, 31622303, 999970000400002, 999970047023809)

The program works and found 4 points in the range of (x,y) < (1000000, 31622303):

(x, y) = (0, 2)
(x, y) = (0, -2)
(x, y) = (4128, 265222)
(x, y) = (4128, -265222)

Of course, you can increase the search range to a higher number. But the program may crash with integer overflow exceptions or run for a long long time.

Table of Contents

 About This Book

 Geometric Introduction to Elliptic Curves

 Algebraic Introduction to Elliptic Curves

 Abelian Group and Elliptic Curves

 Discrete Logarithm Problem (DLP)

 Finite Fields

 Generators and Cyclic Subgroups

Reduced Elliptic Curve Groups

 Converting Elliptic Curve Groups

 Elliptic Curves in Integer Space

Python Program for Integer Elliptic Curves

 Elliptic Curves Reduced by Modular Arithmetic

 Python Program for Reduced Elliptic Curves

 Point Pattern of Reduced Elliptic Curves

 Integer Points of First Region as Element Set

 Reduced Point Additive Operation

 Modular Arithmetic Reduction on Rational Numbers

 Reduced Point Additive Operation Improved

 What Is Reduced Elliptic Curve Group

 Reduced Elliptic Curve Group - E23(1,4)

 Reduced Elliptic Curve Group - E97(-1,1)

 Reduced Elliptic Curve Group - E127(-1,3)

 Reduced Elliptic Curve Group - E1931(443,1045)

 What Is Hasse's Theorem

 Finite Elliptic Curve Group, Eq(a,b), q = p^n

 Elliptic Curve Subgroups

 tinyec - Python Library for ECC

 EC (Elliptic Curve) Key Pair

 ECDH (Elliptic Curve Diffie-Hellman) Key Exchange

 ECDSA (Elliptic Curve Digital Signature Algorithm)

 ECES (Elliptic Curve Encryption Scheme)

 EC Cryptography in Java

 Standard Elliptic Curves

 Terminology

 References

 Full Version in PDF/EPUB