示例#1
0
文件: fields.py 项目: marchon/klefki
 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)
示例#2
0
文件: rsa.py 项目: RyanKung/klefki
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
示例#3
0
    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)