def sec_inverse(self): gcd, x, y = extended_euclidean_algorithm(self.value, self.P) assert (self.value * x + self.P * y) == gcd if gcd != 1: # Either n is 0, or p is not prime number raise ValueError('{} has no multiplicate inverse ' 'modulo {}'.format(self.value, self.P)) return self.__class__(x % self.P)
def mod_inverse(a, m): """ gcd = as + mt ab = 1 (mod m) a and m are relatively prime """ g, s, t = extended_euclidean_algorithm(a, m) if g != 1: raise Exception('modular inverse does not exist') else: return s % m
def sec_inverse(self): if isinstance(self.value, complex): return complex_truediv_algorithm(complex(1), self.value, self.__class__) gcd, x, y = extended_euclidean_algorithm(self.value, self.P) assert (self.value * x + self.P * y) == gcd # if gcd != 1: # # Either n is 0, or p is not prime number # raise ValueError( # '{} has no multiplicate inverse ' # 'modulo {}'.format(self.value, self.P) # ) return self.__class__(x % self.P)