galois.random_prime(bits: int, seed: int | None = None) int

Returns a random prime \(p\) with \(b\) bits, such that \(2^b \le p < 2^{b+1}\).

Parameters:
bits: int

The number of bits in the prime \(p\).

seed: int | None = None

Non-negative integer used to initialize the PRNG. The default is None which means that unpredictable entropy will be pulled from the OS to be used as the seed.

Returns:

A random prime in \(2^b \le p < 2^{b+1}\).

Notes

This function randomly generates integers with \(b\) bits and uses the primality tests in is_prime() to determine if \(p\) is prime.

References

Examples

Generate a random 1024-bit prime.

In [1]: p = galois.random_prime(1024, seed=1); p
Out[1]: 327845897586213436751081882871255331286648902836386839087617368608439574698192016043769533823474001379935585889197488144338014865193967937011638431094821943416361149113909692569658970713864593781874423564706915495970135894084612689487074397782022398597547611189482697523681694691585678818112329605903872356773

In [2]: galois.is_prime(p)
Out[2]: True

Verify that \(p\) is prime using the OpenSSL library.

$ openssl prime 327845897586213436751081882871255331286648902836386839087617368608439574698192016043769533823474001379935585889197488144338014865193967937011638431094821943416361149113909692569658970713864593781874423564706915495970135894084612689487074397782022398597547611189482697523681694691585678818112329605903872356773
1D2DE38DE88C67E1EAFDEEAE77C40B8709ED9C275522C6D5578976B1ABCBE7E0F8C6DE1271EEC6EB3827649164189788F9F3A622AEA5F4039761EC708B5841DE88566D9B5BAF49BA92DCE5A300297A9E0E890E4103ED2AD4B5E0553CE56E8C34758CD45900125DBA1553AE73AA0CBD6018A2A8713D46E475BF058D1AAA52EF1A5 (327845897586213436751081882871255331286648902836386839087617368608439574698192016043769533823474001379935585889197488144338014865193967937011638431094821943416361149113909692569658970713864593781874423564706915495970135894084612689487074397782022398597547611189482697523681694691585678818112329605903872356773) is prime