Exemplo n.º 1
0
def generate_prime(seed):
    random.seed(seed)
    while True:
        p = random.getrandbits(512) | (1 << 511)
        if p % E == 1: continue
        if not is_prime(p): continue
        return p
Exemplo n.º 2
0
    def __init__(self, modulus, root_order=2):
        """
        """
        modulus = modulus
        root_order = root_order

        if modulus <= 2 or not is_prime(modulus):
            err = "Provided modulus is not an odd prime"
            raise AlgebraError(err)

        if root_order <= 0 or root_order >= modulus:
            err = "Provided order of unit root is not in the allowed range"
            raise AlgebraError(err)

        order, s = divmod(modulus - 1, root_order)

        if s != 0:
            err = "Provided order of unit root does not divide the group\'s order"
            raise AlgebraError(err)

        self.__modulus = modulus
        self.__order = order

        self.__Element = ModPrimeElement
        self.__unit = mpz(1)
Exemplo n.º 3
0
 def _validate_system(cls, modulus, order, generator, root_order,
         prime_order, min_mod_size, min_gen_size, allow_weakness):
     """
     """
     if root_order==2 and modulus % 4 != 3:
         #
         # ~ Algebraic fact: the condition p = 3 mod 4 guarantees direct
         # ~ solvability of the congruence x ^ 2 = a (mod p), a E Z*_p,
         # ~ allowing for efficient verification of quadratic residues
         #
         err = "Provided modulus is not 3 mod 4"
         raise WrongCryptoError(err)
     if prime_order and not is_prime(order):
         err = "Order of the requested group is not prime"
         raise WrongCryptoError(err)
     if not allow_weakness:
         MIN_MOD_SIZE = min_mod_size or cls.MIN_MOD_SIZE
         if modulus.bit_length() < MIN_MOD_SIZE:
             err = f"Provided modulus is < {MIN_MOD_SIZE} bits long"
             raise WeakCryptoError(err)
         MIN_GEN_SIZE = min_gen_size or cls.MIN_GEN_SIZE
         if generator.bit_length < MIN_GEN_SIZE:
             err = f"Generator is < {MIN_GEN_SIZE} bits long"
             raise WeakCryptoError(err)
 def __init__(self):
     self.d = precompute_d()        
     self.primes = [is_prime(p) for p in range(2000)]
     self.cache = {}