r/ComputerSecurity • • 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!

3 Upvotes

6 comments sorted by

View all comments

1

u/[deleted] May 15 '23 edited May 16 '23

Generating large prime numbers... What can i do??

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.

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.

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.

$ cat test.cxx
#include <iostream>
#include <iomanip>
#include "integer.h"
#include "algebra.h"
#include "osrng.h"

int main(int argc, char* argv[])
{
    using namespace CryptoPP;
    AutoSeededRandomPool prng;

    const Integer base = 10, exp = 20;
    const Integer e = EuclideanDomainOf<Integer>().Exponentiate(base, exp);

    // Random, 512-bit integers. N is selected as prime.
    // Finding N takes the most of the running time.
    Integer a, n;
    a.Randomize(prng, 512);

    AlgorithmParameters params = MakeParameters("BitLength", 512)
                                 ("RandomNumberType", Integer::PRIME);
    n.GenerateRandom(prng, params);

    const Integer r = a_exp_b_mod_c(a, e, n);

    std::cout << "a: " << a << std::endl;
    std::cout << "e: " << e << std::endl;
    std::cout << "n: " << n << std::endl;
    std::cout << "r: " << r << std::endl;

    return 0;
}

$ g++ -o test.exe -g2 -O3 test.cxx ./libcryptopp.a

$ time ./test.exe
a: 13345189399332087094055251318515643269787041420117812968959375865052706372424631450062811610282059768166915552162371299765609262605023010027560930488763843.
e: 100000000000000000000.
n: 3358399947180098529713482166566309946109655883104251996688586978515219844236279236231225580641007670323485572779296639210070071302875993395265574648140601.
r: 1610933095881955163302161965078269832486638057682120211008602283879353860629646180477648038517906042700625736925863980691351549985963625441524372596208898.

real    0m0.021s
user    0m0.013s
sys     0m0.008s

And here is a run using 10^150. It is faster because the prime n was found quickly.

$ time ./test.exe
a: 7921890453842279425694651137503775751068780215279568265431827144666156033236874607457215097615481843697575048466972712501978200487484380476448153667062853.
e: 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.
n: 5894970051518954595349570570018801790494962862596197425777274730663213802756090579610818714878560663704555318444702268456013947791901802390804302626964339.
r: 5163369945484637572031420239527230292472681096207337101017231899447579774750269503352694800280273094328848536111109534880466641956430051273644954488242141.

real    0m0.009s
user    0m0.008s
sys     0m0.001s

As you can see 10^150 isn't even 512-bits. It is not a big number by any stretch of the imagination.