Skip to content

Instantly share code, notes, and snippets.

@junquera
Created January 28, 2019 15:37
Show Gist options
  • Save junquera/231a82e878b473f2868bd251e37cdbea to your computer and use it in GitHub Desktop.
Save junquera/231a82e878b473f2868bd251e37cdbea to your computer and use it in GitHub Desktop.
Suma de puntos de curvas elípticas
import math
class Punto():
def __init__(self, x, y):
self.x = x
self.y = y
def __cmp__(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
def __eq__(self, other):
return (self.x == other.x) and (self.y == other.y)
def __str__(self):
return "x = %d; y = %d;" % (self.x, self.y)
#** EJEMPLO TEORÍA **
# a y b parámetros de la curva
# en ecuación general de Weirstrass
# y^2 = x^3  + ax + b
a = 2
b = 5
p = 19
def inv(a, mod):
for i in range(mod):
if ((i*a)%mod) == 1:
return i
raise Exception("No tiene inverso")
def lamb(p1, p2, mod):
if p1 != p2:
return (int((p2.y - p1.y)*inv(p2.x-p1.x, mod)) % mod)
else:
return (int((3*(p1.x**2) + a) * inv(2*p1.y, mod)) % mod)
def suma(p1, p2, mod):
l = lamb(p1, p2, mod)
print("lambda", l)
xs = ((l**2) - p1.x - p2.x) % mod
ys = (l * (p1.x - xs) - p1.y) % mod
return Punto(xs, ys)
p1 = Punto(0, 0)
p2 = Punto(1, 1)
p3 = Punto(0, 0)
print(p1 == p2)
print(p1 == p3)
pa = Punto(6, 10)
pb = Punto(9, 12)
print(suma(pa, pb, p))
# Ejercicio Grupal
a = 1
b = 1
p = 17
# Apartado 1
p1 = Punto(4, 1)
p2 = Punto(9, 5)
print(suma(p1, p2, p))
# Apartado 2
p1 = Punto(6, 6)
print(suma(p1, p1, p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment