Random prime number

2019-02-04 11:56发布

How do I quickly generate a random prime number, that is for sure 1024 bit long?

6条回答
孤傲高冷的网名
2楼-- · 2019-02-04 12:07

You do not specify a context/language/platform.. if you'd like to use unix/linux-like system and shell, you might consider a solution involving OpenSSL version >= 1.0.0:

$ openssl prime -generate -bits 1024
140750877582727333214379261853877378646889234118675380673028200387281415297520423589261211081966230040412916644372766351028035798201654335110081318739796178745233127842988596480299276295476504358587725867882394416543075082108266054273016211760684113070285409887820598314292803190900634009988950624354964653677

If you got the same result, something is very wrong with the universe.

Add -hex option if you like hexadecimal system.

查看更多
霸刀☆藐视天下
3楼-- · 2019-02-04 12:09

In PARI/GP:

randomprime([2^1023,2^1024])

If you'd like to do this in 'library mode'

#include <pari/pari.h>
// ...
randomprime(mkvec2(int2u(1023), int2u(1024)))
查看更多
男人必须洒脱
4楼-- · 2019-02-04 12:16

1024 is a lot. Are you sure a probabilistic prime won't do? Probabilistic prime generator is part of JDK

查看更多
姐就是有狂的资本
5楼-- · 2019-02-04 12:17

To trade memory for speed you could just generate them and store them in a list and then randomly pick one.

Edit: Naturally you can't generate them all so the best you could achieve is pseudo randomness at a high memory cost. Also this isn't good if you want it for security.

查看更多
孤傲高冷的网名
6楼-- · 2019-02-04 12:21
  1. Generate 1024 random bits. Use a random source that is strong enough for your intended purpose.

  2. Set the highest and lowest bits to 1. This makes sure there are no leading zeros (the prime candidate is big enough) and it is not an even number (definitely not prime).

  3. Test for primality. If it's not a prime, go back to 1.

Alternatively, use a library function that generates primes for you.

查看更多
男人必须洒脱
7楼-- · 2019-02-04 12:23

Use a library function, such as OpenSSL. There's no need to write this yourself.

Example: http://ardoino.com/7-maths-openssl-primes-random/

查看更多
登录 后发表回答