ecc

Elliptic Curve Cryptograpy library from scratch in python.
Log | Files | Refs | README | LICENSE

commit c129e51d4f27e1cd68d893ca8e5dc5b68339b7d0
parent 56bd301ce3333fbe514ce1ee9851ff9b6b14966b
Author: Shivakumar <[email protected]>
Date:   Sat, 28 Jun 2025 00:20:26 +0530

update .gitignore and better format the project

Diffstat:
M.gitignore | 10++++++++++
Mecc/__init__.py | 4+++-
Mecc/curve.py | 75+++++++++++++++++++++++++++++++++++----------------------------------------
Mecc/curves/__init__.py | 2++
Mecc/field.py | 35+++++++++++------------------------
Mecc/point.py | 58+++++++++++++++++++++++++---------------------------------
Mtests/test_field.py | 2+-
Mtests/test_point.py | 4+---
Mtests/test_secp256k1.py | 2+-
9 files changed, 89 insertions(+), 103 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,5 +1,15 @@ # python __pycache__/ +*.py[oc] +build/ +dist/ +wheels/ +*.egg-info +.python-version + +# Virtual environments +.venv +.env # macos .DS_Store diff --git a/ecc/__init__.py b/ecc/__init__.py @@ -2,4 +2,6 @@ from .field import FieldElement from .point import Point from .curve import Curve -from .curves.secp256k1 import secp256k1, G as secp256k1_G +from .curves.secp256k1 import secp256k1 + +__all__ = ["FieldElement", "Point", "Curve", "secp256k1"] diff --git a/ecc/curve.py b/ecc/curve.py @@ -1,74 +1,69 @@ # Definition of the Elliptic Curve. -# -# First, we check if the curve is singular, meaning if the -# discriminant of the curve is 0. We discard such curves +# +# First, we check if the curve is singular, meaning if the +# discriminant of the curve is 0. We discard such curves # because if the discriminant is 0, it means the curve has -# cusps or self-intersection. We do not use such curves in +# cusps or self-intersection. We do not use such curves in # cryptography. -# -# For any polynomial, the discriminant gives the criterion +# +# For any polynomial, the discriminant gives the criterion # for whether the polynomial has repeated roots or not. -# +# # For a cubic polynomial: # f(x) = (x - α)(x - β)(x - γ) -# +# # Therefore, the discriminant is given by, # ∆ = a^4 (α−β)^2 (β−γ)^2 (γ−α)^2 -# -# So, for a Elliptic curve in defined by the short Weierstrass -# form: +# +# So, for a Elliptic curve in defined by the short Weierstrass +# form: # y^2 = x^3 + ax + b -# -# Full discriminant formula: +# +# Full discriminant formula: # ∆ = -16(4a^3 + 27b^2) -# -# +# +# # Elliptic Curve Discreate Logarithmic Problem: -# -# Scalar Muiltiplication -> one way function +# +# Scalar Muiltiplication -> one way function # over the Field E(Z/pZ) -# -# Let there be a point G such that G belongs to E(Z/pZ) and -# G generates all the points in the Field E(Z/pZ). -# +# +# Let there be a point G such that G belongs to E(Z/pZ) and +# G generates all the points in the Field E(Z/pZ). +# # So, we can say that the Co-factor, -# +# # h = |E(Z/pZ)|/n where n = ord(G) -# -# Usually we select G such that h = 1. -# -# So, Therefore, the Domain parameters are {P, a, b, G, n, h}. -# The domain parameters are public. +# +# Usually we select G such that h = 1. +# +# So, Therefore, the Domain parameters are {P, a, b, G, n, h}. +# The domain parameters are public. class Curve: - - def __init__(self, a, b, P, G, n, h=1): - """ - parameters: + parameters: a, b : Curve parameters (y^2 = x^3 + ax + b). P : Prime modulo for finite Field. G : Generator point. n : Order of Generator point. h : Co-factor (usually 1). """ - + self.a = a self.b = b self.P = P self.G = G self.n = n - - # Check if the curve is singular - if 4*(a**2) + 27*(b**2) == 0: - raise ValueError("This is a singular curve (discriminant is zero). Choose different parameters for the curve.") - - def __repr__(self): + # Check if the curve is singular + if 4 * (a**2) + 27 * (b**2) == 0: + raise ValueError( + "This is a singular curve (discriminant is zero). Choose different parameters for the curve." + ) + def __repr__(self): return f"Curve: y^2 = x^3 + {self.a}x + {self.b} over the Field(Z/{self.P}Z)." - - diff --git a/ecc/curves/__init__.py b/ecc/curves/__init__.py @@ -1 +1,3 @@ from .secp256k1 import secp256k1 + +__all__ = ["secp256k1"] diff --git a/ecc/field.py b/ecc/field.py @@ -1,19 +1,19 @@ # Definition of Field E(Z/pZ) for the elliptic curve : y^2 = x^3 + ax + b # -# The Group operation for the curve are under the Field E(Z/pZ). +# The Group operation for the curve are under the Field E(Z/pZ). # The operations like : - Addition (+) # - Multiplication (*) # - Subtraction (-) -# +# # For the operation : - Power (^) -# -# We use the Fermat's Little Theorem. As we have to compute a^k for all +# +# We use the Fermat's Little Theorem. As we have to compute a^k for all # a and k are integers. We know that a^(p-1) = 1 (mod p) for all p = prime. -# Then : +# Then : # a^m(p-1) = 1 (mod p) -# +# # Let k be some integer such that k = q(p-1) + r. -# +# # a^k = a^(q(p-1) + r) (mod p) # a^k = (a^(p-1))^q * a^r (mod p) # a^k = a^r (mod p) @@ -21,18 +21,15 @@ class FieldElement: - def __init__(self, num: int, prime: int): - if num >= prime or num < 0: - raise ValueError(f"{num} is not in Filed range 0 to {prime-1}") + raise ValueError(f"{num} is not in Filed range 0 to {prime - 1}") self.num = num self.prime = prime - + def __add__(self, other): - if self.prime != other.prime: raise TypeError("Can not add the elements of different Field.") @@ -40,37 +37,31 @@ class FieldElement: def __sub__(self, other): - if self.prime != other.prime: raise TypeError("Can not subtract the elements of different Field.") return FieldElement((self.num - other.num) % self.prime, self.prime) - - def __mul__(self, other): + def __mul__(self, other): if self.prime != other.prime: raise TypeError("Can not multiply the elements of different Field.") return FieldElement((self.num * other.num) % self.prime, self.prime) - + def __eq__(self, other): - if other is None: return False return self.num == other.num and self.prime == other.prime - def __rmul__(self, coefficient): - res = (coefficient * self.num) % self.prime return FieldElement(res, self.prime) def __truediv__(self, other): - if self.prime != other.prime: raise TypeError("Can not divide the elements of different Field.") @@ -81,13 +72,9 @@ class FieldElement: def __pow__(self, exponent: int): - exponent = exponent % (self.prime - 1) return FieldElement(pow(self.num, exponent, self.prime), self.prime) def __repr__(self): - return f"F_{self.prime}({self.num})" - - diff --git a/ecc/point.py b/ecc/point.py @@ -1,45 +1,45 @@ # Definition of Point on the curve : y^2 = x^3 + ax + b # # Group operation for the elliptic curve. -# The operations are: -# - Addition +# The operations are: +# - Addition # - Scalar Multiplication -# -# For Addition: # -# Let's say we have two distinct point P and Q, we want -# to do P + Q, so we find the line passing through both -# the points P and Q, and this would intersect the curve -# at a point -R, we find the reflection of -R to get R. -# Such that : +# For Addition: +# +# Let's say we have two distinct point P and Q, we want +# to do P + Q, so we find the line passing through both +# the points P and Q, and this would intersect the curve +# at a point -R, we find the reflection of -R to get R. +# Such that : # P + Q = R -# +# # So, we find the slope of the line passing through P & Q. -# +# # s = (yp - yq)/(xp - xq) # xr = s^2 - (xp + xq) # yr = s(xp - xr) - yp -# +# # But, if P and Q are the same point, then we have to find # 2P. To find 2P, we have to find the slope of the tangent -# at the point P, we can do that by finding the derivative -# of the curve. +# at the point P, we can do that by finding the derivative +# of the curve. # 2P = P + P # -# So, the derivative of the curve would be -# +# So, the derivative of the curve would be +# # s = (3xp + a)/2yp # xr = s^2 - 2xp # yr = s(xp - xr) - yp -# -# -# For Scalar Multiplication: -# -# Let's say for a Point P in the curve and let there be a +# +# +# For Scalar Multiplication: +# +# Let's say for a Point P in the curve and let there be a # scalar k that belongs to Z. Then k * P is just adding P # k times. We do this by Double and Add Algorithm. -# -# Q = k * P +# +# Q = k * P # Q = P + P + ... + P (k times) @@ -47,9 +47,7 @@ from .field import FieldElement class Point: - def __init__(self, x, y, curve): - self.curve = curve self.prime = curve.P @@ -68,7 +66,6 @@ class Point: def __add__(self, other): - if self.curve != other.curve: raise TypeError("Points are not on the same curve.") @@ -85,7 +82,7 @@ class Point: # Distinct Points if self.x != other.x: s = (other.y - self.y) / (other.x - self.x) - xr = s ** 2 - (self.x + other.x) + xr = s**2 - (self.x + other.x) yr = s * (self.x - xr) - self.y return Point(xr.num, yr.num, self.curve) @@ -93,7 +90,7 @@ class Point: # Point doubling if self == other: s = (3 * self.x**2 + self.a) / (2 * self.y) - xr = s ** 2 - 2 * self.x + xr = s**2 - 2 * self.x yr = s * (self.x - xr) - self.y return Point(xr.num, yr.num, self.curve) @@ -102,7 +99,6 @@ class Point: def __eq__(self, other): - if not isinstance(other, Point): return False @@ -110,7 +106,6 @@ class Point: def __rmul__(self, coefficient): - result = Point(None, None, self.curve) addend = self @@ -125,10 +120,7 @@ class Point: def __repr__(self): - if self.x is None: return "Point(infinity)" return f"Point(\n\t{self.x.num}, \n\t{self.y.num}\n)" - - diff --git a/tests/test_field.py b/tests/test_field.py @@ -9,7 +9,7 @@ def test_addition(): def test_exponentiation(): a = FieldElement(3, 13) - assert a ** 12 == FieldElement(1, 13) + assert a**12 == FieldElement(1, 13) def test_multiplication(): diff --git a/tests/test_point.py b/tests/test_point.py @@ -1,6 +1,5 @@ from ecc import Point from ecc import Curve -from ecc import FieldElement G = (5, 1) @@ -9,6 +8,7 @@ n = 19 curve = Curve(2, 2, P, G, n) + def test_point_on_curve(): p = Point(3, 1, curve) assert p.y**2 == p.x**3 + p.a * p.x + p.b @@ -36,5 +36,3 @@ def test_scalar_multiplication(): def test_infinity(): p = Point(5, 1, curve) assert Point(None, None, curve) == n * p - - diff --git a/tests/test_secp256k1.py b/tests/test_secp256k1.py @@ -27,4 +27,4 @@ def test_point_order(): G = Point(Gx, Gy, secp256k1) infinity = secp256k1.n * G - assert (infinity.x, infinity.y) == (None, None) + assert (infinity.x, infinity.y) == (None, None)