galois.random_prime

galois.random_prime(bits: int) int

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

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

Parameters
bits: int

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

Returns

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

References

Examples

Generate a random 1024-bit prime.

In [1]: p = galois.random_prime(1024); p
Out[1]: 317264070047188358086916550884620514935843766184585016715855374065148596231855472529950202482051882971105386511126773975462463220850631148798271421987361840680564551597445249261163718794719297301239179672884402981547877643750344698395239991016145123885798373851620596367920805670209179595788009490065744357123

In [2]: galois.is_prime(p)
Out[2]: True
$ openssl prime 236861787926957382206996886087214592029752524078026392358936844479667423570833116126506927878773110287700754280996224768092589904231910149528080012692722763539766058401127758399272786475279348968866620857161889678512852050561604969208679095086283103827661300743342847921567132587459205365243815835763830067933
1514D68EDB7C650F1FF713531A1A43255A4BE6D66EE1FDBD96F4EB32757C1B1BAF16A5933E24D45FAD6C6A814F3C8C14F3CB98F24FEA74C43C349D6FA3AB76EB0156811A1FBAA64EB4AC525CCEF9278AF78886DC6DBF46C4463A34C0E53B0FA2F784BB2DC5FDF076BB6E145AA15AA6D616ACC1D5F95B8BE757670B9AAF53292DD (236861787926957382206996886087214592029752524078026392358936844479667423570833116126506927878773110287700754280996224768092589904231910149528080012692722763539766058401127758399272786475279348968866620857161889678512852050561604969208679095086283103827661300743342847921567132587459205365243815835763830067933) is prime

Last update: Apr 21, 2022