def add(self, summand): """add elements of elliptic curves""" if (self.x == "Infinity"): # Add to zero (i.e. point at infinity) if (self.ec.verbose): print "Add: 0 + ({0},{1}) = ({0},{1})".format( summand.x, summand.y) return summand elif (summand.x == "Infinity"): # Add zero (i.e. point at infinity) if (self.ec.verbose): print "Add: ({0},{1}) + 0 = ({0},{1})".format(self.x, self.y) return self elif ((summand.x == self.x) and ((summand.y + self.y) % self.ec.prime == 0)): # P + (-P) = infty if (self.ec.verbose): print "Add: ({0},{1}) + ({2},{3}) = 0".format( self.x, self.y, summand.x, summand.y) return EllipticCurveElt(self.ec, ["Infinity", "Infinity"]) else: # Usual addition and doubling (what a nuisance: lambda is a keyword - shorten to lamb) if (self.x == summand.x): # Point doubling if (self.ec.verbose): print "Add (Double): ", lamb = (3 * (self.x**2) + self.ec.a) * numbthy.xgcd( 2 * self.y, self.ec.prime)[1] % self.ec.prime else: # Point addition if (self.ec.verbose): print "Add: ", lamb = (self.y - summand.y) * numbthy.xgcd( (self.x - summand.x), self.ec.prime)[1] % self.ec.prime if (self.ec.verbose): print "lambda = {0}; ".format(lamb), x3 = (lamb * lamb - self.x - summand.x) % self.ec.prime y3 = (lamb * (self.x - x3) - self.y) % self.ec.prime if (self.ec.verbose): print "({0},{1}) + ({2},{3}) = ({4},{5})".format( self.x, self.y, summand.x, summand.y, x3, y3) return EllipticCurveElt(self.ec, [x3, y3])
def add(self,summand): """add elements of elliptic curves""" if (self.x == "Infinity"): # Add to zero (i.e. point at infinity) return summand elif (summand.x == "Infinity"): # Add zero (i.e. point at infinity) return self elif ((summand.x == self.x) and ((summand.y + self.y) % self.ec.prime == 0)): # P + (-P) = infty return EllipticCurveElt(self.ec, ("Infinity","Infinity")) else: # Usual addition and doubling (what a nuisance: lambda is a keyword - shorten to lamb) if (self.x == summand.x): # Point doubling lamb = (3*(self.x**2)+self.ec.a)*numbthy.xgcd(2*self.y,self.ec.prime)[1] % self.ec.prime else: # Point addition lamb = (self.y - summand.y) * numbthy.xgcd((self.x - summand.x), self.ec.prime)[1] % self.ec.prime x3 = (lamb*lamb - self.x - summand.x) % self.ec.prime y3 = (lamb*(self.x-x3) - self.y) % self.ec.prime return EllipticCurveElt(self.ec, (x3,y3))
def leftHand(x): aux = numbthy.powmod(g, x, p); [m, aux, y] = numbthy.xgcd(aux, p); aux = (aux * h) % p; aux = aux % p; if(aux < 0): raise NameError('Aux less than zero.'); return aux;
def add(self, summand): """add elements of elliptic curves""" if (self.x == "Infinity"): # Add to zero (i.e. point at infinity) return summand elif (summand.x == "Infinity"): # Add zero (i.e. point at infinity) return self elif ((summand.x == self.x) and ((summand.y + self.y) % self.ec.prime == 0)): # P + (-P) = infty return EllipticCurveElt(self.ec, ("Infinity", "Infinity")) else: # Usual addition and doubling (what a nuisance: lambda is a keyword - shorten to lamb) if (self.x == summand.x): # Point doubling lamb = (3 * (self.x**2) + self.ec.a) * numbthy.xgcd( 2 * self.y, self.ec.prime)[1] % self.ec.prime else: # Point addition lamb = (self.y - summand.y) * numbthy.xgcd( (self.x - summand.x), self.ec.prime)[1] % self.ec.prime x3 = (lamb * lamb - self.x - summand.x) % self.ec.prime y3 = (lamb * (self.x - x3) - self.y) % self.ec.prime return EllipticCurveElt(self.ec, (x3, y3))
def test_xgcd_random(self): for testnum in range(10): a = random.randint(0, 10**20) b = random.randint(0, 10**20) (g, x, y) = numbthy.xgcd(a, b) try: self.assertEqual(g, a * x + b * y) break except AssertionError: raise AssertionError( "***** Error in xgcd *****: {2} != ({0})*({3}) + ({1})*({4})" .format(a, b, g, x, y))
def xgcd_function(): print("Testing -- xgcd function--") print("Using numpy.random generating {0} numbers".format(testnumber)) for n in range(testnumber): a = rd.randint(minimum, maximum) b = rd.randint(minimum, maximum) tf1 = nmtf.xgcd(tf.constant(a), tf.constant(b)) nm1 = nm.xgcd(a, b) print("Test {0}------------------------------".format(n + 1)) print("nmtf.xgcd({0}, {1}) = ({2}, {3}, {4})".format( a, b, tf1[0].eval(), tf1[1].eval(), tf1[2].eval())) print(" nm.xgcd({0}, {1}) = ({2}, {3}, {4})".format( a, b, nm1[0], nm1[1], nm1[2])) print("")
def add(self,summand): """add elements of elliptic curves""" if (self.x == "Infinity"): # Add to zero (i.e. point at infinity) if(self.ec.verbose): print "Add: 0 + ({0},{1}) = ({0},{1})".format(summand.x,summand.y) return summand elif (summand.x == "Infinity"): # Add zero (i.e. point at infinity) if(self.ec.verbose): print "Add: ({0},{1}) + 0 = ({0},{1})".format(self.x,self.y) return self elif ((summand.x == self.x) and ((summand.y + self.y) % self.ec.prime == 0)): # P + (-P) = infty if(self.ec.verbose): print "Add: ({0},{1}) + ({2},{3}) = 0".format(self.x,self.y,summand.x,summand.y) return EllipticCurveElt(self.ec, ["Infinity","Infinity"]) else: # Usual addition and doubling (what a nuisance: lambda is a keyword - shorten to lamb) if (self.x == summand.x): # Point doubling if(self.ec.verbose): print "Add (Double): ", lamb = (3*(self.x**2)+self.ec.a)*numbthy.xgcd(2*self.y,self.ec.prime)[1] % self.ec.prime else: # Point addition if(self.ec.verbose): print "Add: ", lamb = (self.y - summand.y) * numbthy.xgcd((self.x - summand.x), self.ec.prime)[1] % self.ec.prime if(self.ec.verbose): print "lambda = {0}; ".format(lamb), x3 = (lamb*lamb - self.x - summand.x) % self.ec.prime y3 = (lamb*(self.x-x3) - self.y) % self.ec.prime if(self.ec.verbose): print "({0},{1}) + ({2},{3}) = ({4},{5})".format(self.x,self.y,summand.x,summand.y,x3,y3) return EllipticCurveElt(self.ec, [x3,y3])
def inv(self): """inverse of element in a finite field""" return FiniteFieldElt(self.field, [ numbthy.xgcd(self.coeffs[0], self.field.char)[1] % self.field.char ])
def test_xgcd(self): for testcase in ((1, -1, 1, 0, -1), (6, 8, 2, -1, 1), (12345, 2345, 5, 87, -458), (98760, 76540, 20, 1898, -2449)): self.assertEqual(numbthy.xgcd(testcase[0], testcase[1]), (testcase[2], testcase[3], testcase[4]))
def multiplicativeInverse(a, n): """Efficiently calculate the multiplicative inverse using extended GCD algorithm""" (d,X,Y) = numbthy.xgcd(a,n) if d != 1: return None else: return X % n
def inv(self): """inverse of element in a finite field""" return FiniteFieldElt(self.field,[numbthy.xgcd(self.coeffs[0],self.field.char)[1] % self.field.char])