def edwardsadd(P1,P2): x1,y1 = P1 x2,y2 = P2 x3 = (x1*y2+y1*x2)/(one+d*x1*y1*x2*y2) y3 = (y1*y2-x1*x2)/(one-d*x1*y1*x2*y2) return x3,y3 # example: one = 1 d = -300 P1 = (0.8,-0.043188945044921672046) P2 = (-0.4,0.130930734141595428759) print edwardsadd(P1,P2) # output: (0.26691902067523626, 0.20374230558087345) print edwardsadd(P2,P1) # output: (0.26691902067523626, 0.20374230558087345) def F(p): # caveat: caller must ensure that p is prime class F: def __init__(self,x): self.int = x % p def __str__(self): return str(self.int) __repr__ = __str__ def __eq__(a,b): return a.int == b.int def __ne__(a,b): return a.int != b.int def __add__(a,b): return F(a.int + b.int) def __sub__(a,b): return F(a.int - b.int) def __mul__(a,b): return F(a.int * b.int) def __div__(a,b): # caveat: caller must ensure that b is nonzero return a*F(pow(b.int,p-2,p)) return F # example: F1009 = F(1009) d = F1009(-11) one = F1009(1) P1 = (F1009(7),F1009(415)) P2 = (F1009(23),F1009(487)) print edwardsadd(P1,P2) # output: (944, 175) print edwardsadd(P2,P1) # output: (944, 175)