ecc

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

commit 49c6f375f826faadeb232b5229a80aed7014d1c7
parent 627be72160bd3c09fd6b671bbcd34f4458c266f8
Author: Shivakumar <[email protected]>
Date:   Thu, 26 Jun 2025 22:04:32 +0530

add docs for point.py and curve.py

Diffstat:
Mecc/curve.py | 41+++++++++++++++++++++++++++++++++++++++++
Mecc/point.py | 47+++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/ecc/curve.py b/ecc/curve.py @@ -1,3 +1,44 @@ +# 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 +# because if the discriminant is 0, it means the curve has +# cusps or self-intersection. We do not use such curves in +# cryptography. +# +# 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: +# y^2 = x^3 + ax + b +# +# Full discriminant formula: +# ∆ = -16(4a^3 + 27b^2) +# +# +# Elliptic Curve Discreate Logarithmic Problem: +# +# 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). +# +# 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. class Curve: diff --git a/ecc/point.py b/ecc/point.py @@ -1,3 +1,48 @@ +# Definition of Point on the curve : y^2 = x^3 + ax + b +# +# Group operation for the elliptic curve. +# 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 : +# 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. +# 2P = P + P +# +# 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 +# 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 = P + P + ... + P (k times) + + from .field import FieldElement @@ -85,3 +130,5 @@ class Point: return "Point(infinity)" return f"Point(\n\t{self.x.num}, \n\t{self.y.num}\n)" + +