Search code examples
crsagmp

Im using gmp in C and next prime isnt working? Im trying to generate rsa keys


mpz_t rand1,rand2;
mpz_init (rand1);
mpz_init (rand2);

mpz_random (rand1,512);
//mpz_random (rand2,512);

mpz_nextprime ( rand2, rand1 );
gmp_printf("random %Zd\n", rand2);
//free the big ints
mpz_clear(rand1);
mpz_clear(rand2);

However is i print after random it works but the second i call next prime nothing prints out?


Solution

  • From the info page:

    -- Function: void mpz_random (mpz_t ROP, mp_size_t MAX_SIZE) Generate a random integer of at most MAX_SIZE limbs. The generated random number doesn't satisfy any particular requirements of randomness. Negative random numbers are generated when MAX_SIZE is negative.

    This function is obsolete. Use mpz_urandomb' ormpz_urandomm' instead.

    You're creating a random(ish) integer with 512 limbs, that's 16384 bits if a limb is 32 bits, or 32768 bits if a limb is 64 bits. Primes in that range are sparse, so GMP will take a while to find one.

    Presumably you intended to find a prime of 512 bits?