r/ComputerSecurity • u/LordTachankaMain • Mar 28 '23
Generating large prime numbers
(EDIT: Solved! Found the answer, it's in the comments below, I was missing an algorithm.)
For RSA encryption two large primes are needed. On online sites, they can be generated in milliseconds up to 2048 bit sizes.
My problem is that finding these large primes is quite hard. According to this stack exchange question, the best way is using a combination of Fermat and Miller-Rabin tests, each done multiple times.
Fermat: an-1 mod n = 1
The problem is, using Fermat's test, the faster of the two, and using the simplest and smallest number a = 2, I can't come remotely close to testing a prime in the needed range, atleast 10^150.
My computer can't even calculate n=10^20, as you need to take a10^20 - 1, and I don't have enough memory for that.
What can i do?? Even the simplest version of the simplest test would take billions of times the memory I have, not even counting the run time.
It's obviously possible, but I can't find anything anywhere on how!
1
u/Soxcks13 Mar 28 '23
Maybe I’m misunderstanding the problem but why do you need to keep all numbers in memory? Or are you trying to evaluate a number so large that the single number does not fit in your systems memory? The latter seems very unlikely, but not impossible.
Are you trying to iterate through numbers sequentially and evaluate them against the test condition you’ve provided? When/where does the memory limitation become a constraint?
1
u/[deleted] May 15 '23 edited May 16 '23
Typically you use a cryptographic library. You don't roll your own implementation.
Crypto libraries up for the task include Botan, Cryptlib, Crypto++, libgcrypt (used by GnuPG) and OpenSSL. Also see https://en.wikipedia.org/wiki/Comparison_of_cryptography_libraries.
Modular exponentiation is fast. It takes O(log e); not O(a^e). See https://math.stackexchange.com/questions/689395/big-o-for-modular-exponentiation and https://en.wikipedia.org/wiki/Modular_exponentiation.
The program below performs modular exponentiation in the blink of an eye using 10^20 as the exponent. The program uses Crypto++, https://cryptopp.com.
And here is a run using 10^150. It is faster because the prime
nwas found quickly.As you can see 10^150 isn't even 512-bits. It is not a big number by any stretch of the imagination.