Generate large prime numbers using boost library

Recently I am working on a RSA like algorithm project, which needs to generate large secret prime numbers, e.g. 512 bits, or 1024 bits. Luckily we are usingĀ boost library, which is inbuilt lots of functions to assist this generation.

#include <boost/multiprecision/miller_rabin.hpp>
#include <boost/random/mersenne_twister.hpp>

using namespace boost::multiprecision;
using namespace boost::random;

...
//Generate a secret RSA-like 512 bits primes p
cpp_int p = GetPrime();
...
cpp_int GetPrime()
{
    mt11213b base_gen(clock());
    independent_bits_engine<mt11213b, 512, cpp_int> gen(base_gen);

    // Generate some large random primes
    // Note 25 trials of Miller-Rabin 
    // likelihood that number is prime
    cpp_int n;
    do
    {
        n = gen();
    }while (!miller_rabin_test(n, 25));
    return n;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.