def pointAddition(P, Q): gradient: int # check whether addition is possible if P == Q: print( "you can't do addition with the same point, you need to perform point duplication" ) else: isPointOfInfinity = False if P[0] == Q[0]: isPointOfInfinity = True else: # perform the addition divident = turnPositive(curve.p, Q[1] - P[1] % curve.p) diviser = Q[0] - P[0] % curve.p inverse = extendedEuclidian.getInverse(curve.p, diviser) gradient = divident * inverse % curve.p x = (gradient**2 - P[0] - Q[0]) % curve.p y = turnPositive(curve.p, ((gradient * (P[0] - x) - P[1]) % curve.p)) # print out the new point if isPointOfInfinity: print(f"\n{P} + {P} is the point of infinity!") else: print(f"\n{P} + {Q} is ({x}, {y})!")
def pointDuplication(self, P: Point) -> Point: if P.isPointOfInfinity(): return P elif P.y == 0: return Point() else: divident = (3 * ((P.x**2) % self.p) + self.a) % self.p diviser = 2 * P.y % self.p inverse = getInverse(self.p, diviser) gradient = divident * inverse % self.p x = turnPositive(self.p, ((gradient**2 % self.p) - P.x - P.x) % self.p) y = turnPositive(self.p, ((gradient * (P.x - x) - P.y) % self.p)) return Point(x, y)
def pointDuplication(P): isPointOfInfinity = False if P[1] == 0: isPointOfInfinity = True else: gradient: int divident = (3 * (P[0]**2) + curve.a) % curve.p diviser = 2 * P[1] % curve.p inverse = extendedEuclidian.getInverse(curve.p, diviser) gradient = divident * inverse % curve.p x = turnPositive(curve.p, (gradient**2 - P[0] - P[0]) % curve.p) y = turnPositive(curve.p, ((gradient * (P[0] - x) - P[1]) % curve.p)) # print out the new point if isPointOfInfinity: print(f"\n{P} + {P} is the point of infinity!") else: print(f"\n{P} + {P} is ({x}, {y})!")
def calculate(self): isPointOfInfinity = False if self.P[1] == 0: isPointOfInfinity = True else: gradient: int divident = (3 * (self.P[0]**2) + curve.a) % curve.p diviser = 2 * self.P[1] % curve.p inverse = extendedEuclidian.getInverse(curve.p, diviser) gradient = divident * inverse % curve.p x = turnPositive(curve.p, (gradient**2 - self.P[0] - self.P[0]) % curve.p) y = turnPositive( curve.p, ((gradient * (self.P[0] - x) - self.P[1]) % curve.p)) # print out the new point if isPointOfInfinity: print(f"\n{self.P} + {self.P} is the point of infinity!") else: print(f"\n{self.P} + {self.P} is ({x}, {y})!")
def pointAddition(self, P: Point, Q: Point) -> Point: # is one of them the point of infinity? if P.isPointOfInfinity() or Q.isPointOfInfinity(): if P.isPointOfInfinity(): return Q else: return P # are they identical? perform duplication elif P == Q: return self.pointDuplication(P) elif P.x == Q.x: return Point() else: # perform the addition divident = Q.y - P.y % self.p diviser = Q.x - P.x % self.p inverse = getInverse(self.p, diviser) gradient = divident * inverse % self.p x = turnPositive(self.p, ((gradient**2 % self.p) - P.x - Q.x) % self.p) y = turnPositive(self.p, ((gradient * (P.x - x) - P.y) % self.p)) return Point(x, y)