Exemplo n.º 1
0
def _prime_seed_generator(bit_count, secret_prime=True):
    while True:
        seed = mpz(secrets.randbits(bit_count))
        seed = bit_set(seed, bit_count -
                       1)  # ensure bit_count length, by setting highest bit
        if secret_prime:
            seed = bit_set(
                seed, bit_count -
                2)  # ensure that prime will be suitable for the RSA modulus
        seed = bit_set(seed, 0)  # create an uneven seed
        yield seed
Exemplo n.º 2
0
def new_prime(n_bits=1024):
    """
    is_prime: Miller-Rabin's test (default=25 times)
    """
    upper_bound = gmpy2.mpz(2**n_bits)

    state = gmpy2.random_state(int_time())
    rand_num = gmpy2.mpz_random(state, upper_bound)
    rand_num = gmpy2.bit_set(rand_num, n_bits - 1)
    while not gmpy2.is_prime(rand_num):
        state = gmpy2.random_state(int_time())
        rand_num = gmpy2.mpz_random(state, upper_bound)
        rand_num = gmpy2.bit_set(rand_num, n_bits - 1)
    return rand_num
Exemplo n.º 3
0
def getprimeover(n):
    """return a random n-bit prime number
    """
    r = gmpy2.mpz(random.SystemRandom().getrandbits(n))
    r = gmpy2.bit_set(r, n - 1)

    return int(gmpy2.next_prime(r))
Exemplo n.º 4
0
 def __getSafePrimes(n_len):
     rng = secrets.SystemRandom()
     prime_ = gmpy2.mpz(rng.getrandbits(n_len - 1))
     prime_ = gmpy2.bit_set(prime_, n_len - 2)
     while True:
         prime_ = gmpy2.next_prime(prime_)
         prime = 2 * prime_ + 1
         if gmpy2.is_prime(prime, 25):
             break
     return prime_, prime
Exemplo n.º 5
0
def gen_prime(n_bits: int) -> int:
    """Returns a prime number with `n_bits` bits.

    :param n_bits: The number of bits the prime number should contain.
    :return: A prime number with `n_bits` bits.
    """
    base = randbits(n_bits)
    base = bit_set(base, n_bits - 1)
    p = next_prime(base)

    return p
Exemplo n.º 6
0
    def add_token(self, column):
        if self.winner == NO_ONE:
            if self.row_counter[column] < 0:
                print('Invalid move')
                return False

            player = self.current_player
            row, bit_row = self.__first_empty_row(column)
            self.np_board[row, column] = player
            if player == PLAYER1:
                self.bit_board_1 = bit_set(self.bit_board_1, bit_row)
            else:
                self.bit_board_2 = bit_set(self.bit_board_2, bit_row)

            self.move_count += 1

            self.winner = self.check_winner()
            self.current_player = PLAYER1 if player != PLAYER1 else PLAYER2
            return True
        return False
Exemplo n.º 7
0
def getprimeover(n, seed=None):
    """return a random n-bit prime number
    """
    if not seed:
        r = gmpy2.mpz(random.SystemRandom().getrandbits(n))
    else:
        random.seed(seed)
        r = random.getrandbits(n)
    r = gmpy2.bit_set(r, n - 1)

    return int(gmpy2.next_prime(r))
Exemplo n.º 8
0
def getprimeover(N):
    if HAVE_GMP:
        randfunc = random.SystemRandom()
        r = gmpy2.mpz(randfunc.getrandbits(N))
        r = gmpy2.bit_set(r, N - 1)
        return int(gmpy2.next_prime(r))
    elif HAVE_CRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        randfunc = random.SystemRandom()
        n = randfunc.randrange(2**(N - 1), 2**N) | 1
        while not is_prime(n):
            n += 2
        return n
Exemplo n.º 9
0
def getprimeover(N):
    """Return a random N-bit prime number using the System's best
    Cryptographic random source.
    Use GMP if available, otherwise fallback to PyCrypto
    """
    if HAVE_GMP:
        randfunc = random.SystemRandom()
        r = gmpy2.mpz(randfunc.getrandbits(N))
        r = gmpy2.bit_set(r, N - 1)
        return int(gmpy2.next_prime(r))
    elif HAVE_CRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        raise NotImplementedError("No pure python implementation sorry")
Exemplo n.º 10
0
def getPrime(N):
    """
    Returns a random N-bit prime number usign either GMP or PyCrypto.
    """
    if GMPY:
        randomFunction = random.SystemRandom()
        rand = gmpy2.mpz(randomFunction.getrandbits(N))
        rand = gmpy2.bit_set(rand, N - 1)
        return int(gmpy2.next_prime(rand))
    elif PYCRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        raise NotImplementedError(
            "Couldn't find GMP or PyCrypto. No futher method implemented. Please install one of these two."
        )
Exemplo n.º 11
0
def getprimeover(N):
    """Return a random N-bit prime number using the System's best
    Cryptographic random source.

    Use GMP if available, otherwise fallback to PyCrypto
    """
    if HAVE_GMP:
        randfunc = random.SystemRandom()
        r = gmpy2.mpz(randfunc.getrandbits(N))
        r = gmpy2.bit_set(r, N - 1)
        return int(gmpy2.next_prime(r))
    elif HAVE_CRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        raise NotImplementedError("No pure python implementation sorry")
Exemplo n.º 12
0
def findAVulnerablePrime(bitSize):
    generator = 65537
    m = nt.primorial(prime_default(bitSize), False)

    max_order = nt.totient(m)
    max_order_factors = nt.factorint(max_order)

    order = element_order_general(generator, m, max_order, max_order_factors)
    order_factors = nt.factorint(order)

    power_range = [0, order - 1]
    min_prime = g.bit_set(
        g.bit_set(g.mpz(0), bitSize // 2 - 1), bitSize // 2 - 2
    )  # g.add(pow(g.mpz(2), (length / 2 - 1)), pow(g.mpz(2), (length / 2 - 2)))
    max_prime = g.bit_set(
        min_prime, bitSize // 2 - 4
    )  # g.sub(g.add(min_prime, pow(g.mpz(2), (length / 2 - 4))), g.mpz(1))
    multiple_range = [g.f_div(min_prime, m), g.c_div(max_prime, m)]

    random_state = g.random_state(random.SystemRandom().randint(0, 2**256))

    return random_prime(random_state,
                        nt.primorial(prime_default(bitSize), False), generator,
                        power_range, multiple_range)
Exemplo n.º 13
0
    def make_sign_params(self, messages):
        if len(messages) != len(self.a):
            raise ValueError("must have %d messages to sign, you provided %d",
                             len(self.a), len(messages))
        ms = [hash_message_as_int(m) for m in messages]

        ln = self._n.bit_length()
        lm = ms[0].bit_length()
        le = lm + 2
        ls = lm + ln + self.l

        e = gen_prime(le, secret_prime=False)
        s = secrets.randbits(ls)
        s = gm.bit_set(s, ls - 1)

        return e, ms, s
Exemplo n.º 14
0
def getprimeover(N):
    """Return a random N-bit prime number using the System's best
    Cryptographic random source.

    Use GMP if available, otherwise fallback to PyCrypto
    """
    if HAVE_GMP:
        randfunc = random.SystemRandom()
        r = gmpy2.mpz(randfunc.getrandbits(N))
        r = gmpy2.bit_set(r, N - 1)
        return int(gmpy2.next_prime(r))
    elif HAVE_CRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        randfunc = random.SystemRandom()
        n = randfunc.randrange(2**(N - 1), 2**N) | 1
        while not is_prime(n):
            n += 2
        return n
Exemplo n.º 15
0
def getprimeover(N):
    """Return a random N-bit prime number using the System's best
    Cryptographic random source.

    Use GMP if available, otherwise fallback to PyCrypto
    """
    if HAVE_GMP:
        randfunc = random.SystemRandom()
        r = gmpy2.mpz(randfunc.getrandbits(N))
        r = gmpy2.bit_set(r, N - 1)
        return int(gmpy2.next_prime(r))
    elif HAVE_CRYPTO:
        return number.getPrime(N, os.urandom)
    else:
        randfunc = random.SystemRandom()
        n = randfunc.randrange(2**(N-1), 2**N) | 1
        while not is_prime(n):
            n += 2
        return n
Exemplo n.º 16
0
    def make_sign_params(self, message):
        """
        On input m, choose a random prime number e of length :math:`l_e ≥ l_m + 2`,
        and a random number s of length :math:`l_s = l_n + l_m + l`, where l is a security parameter.

        :param message: the unhashed encoded message
        :return: e, int(message_hash), s
        """
        m = hash_message_as_int(message)

        ln = self.modulus.bit_length()
        lm = m.bit_length()
        le = lm + 2
        ls = lm + ln + self.l

        e = gen_prime(le, secret_prime=False)
        s = secrets.randbits(ls)
        s = gm.bit_set(s, ls - 1)

        return e, m, s
def get_primer(n):
    """Return a random n-bit prime number"""
    rand_fun = random.SystemRandom()
    r = gmpy2.mpz(rand_fun.getrandbits(n))
    r = gmpy2.bit_set(r, n - 1)
    return int(gmpy2.next_prime(r))
Exemplo n.º 18
0
def getprimeover(bits_length):
    randfunc = random.SystemRandom()
    r = gmpy2.mpz(randfunc.getrandbits(bits_length))
    r = gmpy2.bit_set(r, bits_length - 1)
    return int(gmpy2.next_prime(r))
Exemplo n.º 19
0
def generate_prime(n_bits):
    rf = random.SystemRandom()
    r = gmpy2.mpz(rf.getrandbits(n_bits))
    r = gmpy2.bit_set(r, n_bits - 1)
    return gmpy2.next_prime(r)
Exemplo n.º 20
0
def _random_prime(bits):
    rand_function = random.SystemRandom()
    r = gp.mpz(rand_function.getrandbits(bits))
    r = gp.bit_set(r, bits - 1)
    return gp.next_prime(r)
Exemplo n.º 21
0
def gen_prime(N):
    randfunc = SystemRandom()
    r = gmpy2.mpz(randfunc.getrandbits(N))
    r = gmpy2.bit_set(r, N - 1)
    return gmpy2.next_prime(r)