Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a prime of a given length in Sage?

Tags:

primes

sage

How can I get a random prime of a given bit length in Sage?

For example, to get a 512-bit prime, I tried

p = random_prime(2^512)

but according to the documentation:

The command random_prime(a, True) will return a random prime between 2 and a

so I can't use it, since I need a prime of an exact length.

like image 986
Chin Avatar asked Dec 24 '22 11:12

Chin


1 Answers

sage: random_prime?
Signature:      random_prime(n, proof=None, lbound=2)
Docstring:
   Returns a random prime p between lbound and n (i.e. lbound <= p <=
   n). The returned prime is chosen uniformly at random from the set
   of prime numbers less than or equal to n.

   INPUT:

   * "n" - an integer >= 2.

   * "proof" - bool or None (default: None) If False, the function
     uses a pseudo-primality test, which is much faster for really big
     numbers but does not provide a proof of primality. If None, uses
     the global default (see "sage.structure.proof.proof")

   * "lbound" - an integer >= 2 lower bound for the chosen primes

So is this sufficient?

sage: random_prime(2^512-1,False,2^511)
7484165189517896027318192121767201416039872004910529422703501933303497309177247161202453673508851750059292999942026203470027056226694857512284815420448467
sage: is_prime(7484165189517896027318192121767201416039872004910529422703501933303497309177247161202453673508851750059292999942026203470027056226694857512284815420448467)
True
like image 167
kcrisman Avatar answered Jan 13 '23 17:01

kcrisman