Last active
September 8, 2018 20:17
-
-
Save kejsiStruga/63ecdee968daf268401634342fa0a7dc to your computer and use it in GitHub Desktop.
Addition of Elliptic Curves in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import Fraction | |
""" | |
General form of the Curves: | |
y**2 = x**3 + a*x**2 + b*x + c | |
So the curve is defined by the tuple (a,b,c). | |
""" | |
def on_curve(x, y): | |
"""Check if (x,y) is on the curve y**2 == x**3+3*x""" | |
return y ** 2 == x ** 3 + 3 * x | |
def sum1(cur, p1, p2): | |
"""compute p1+p2 on the elliptic curve cur | |
:type p1: tuple | |
""" | |
if p1 == p2: | |
lam = (3*p1[0]**2 + 2 * cur[0] * p1[0] + cur[1])/Fraction(2*p1[1]) | |
elif p1[0] == p2[0]: | |
return "null_element" | |
else: | |
lam = (p2[1] - p1[1]) / Fraction((p2[0] - p1[0])) | |
nu = p1[1] - lam * p1[0] | |
x3 = lam ** 2 - cur[0] - p1[0] - p2[0] | |
y3 = -(lam * x3 + nu) | |
""" | |
Because we have used Fraction on the denominator in order to turn it into a rational number, | |
python is forced to perform a rational quotient so that __lam__ gets the proper value; | |
Hence, getting this outcome __Fraction(-8, 9)__ merely means -8/9 | |
""" | |
return x3, y3 | |
if __name__ == '__main__': | |
curve1 = (0,0,17) | |
p1_1=(-1,4) | |
p2_1=(2,5) | |
print(sum1(curve1, p1_1, p2_1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment