The security of the RSA algorithm is based on the difficulty of factorizing very large numbers. The setup of an RSA cryptosystem involves the generation of two large primes, say p and q, from which, the RSA modulus is calculated as n = p * q. The greater the modulus size, the higher is the security level of the RSA system. The recommended RSA modulus size for most settings is 2048 bits to 4096 bits. Thus, the primes to be generated need to be 1024 bit to 2048 bit long. For the synthesis of such large primes, instead of depending on deterministic methods, we rely on finding numbers that are prime with a satisfactorily high level of probability.
Large Prime Generation Procedure:
The goal is to efficiently compute very large random prime numbers with a specified bit-size. The standard method of manually implementing a random prime number generator which can generate prime values with a satisfactory level of accuracy is given as follows:
- Preselect a random number with the desired bit-size
- Ensure the chosen number is not divisible by the first few hundred primes (these are pre-generated)
- Apply a certain number of Rabin Miller Primality Test iterations, based on acceptable error rate, to get a number which is probably a prime
Below are the steps to implement the above procedure:
- Picking a Random Prime Candidate
The generation of a random number with n-bits means the random number is in the range 0 and . Some considerations when generating the random number are:
- Picking of small primes, such as 3, 5, 7…, must be avoided as the factorization of RSA modulus would become trivial. Thus, care must be taken to not have too many leading zeroes. This may be done by always making the highest order bit = 1
- Since all primes (> 2) are odd, for better performace, just odd number may be picked
- The generation of a random number with n-bits means the random number is in the range 0 and . Some considerations when generating the random number are:
- This step is a a low level primality test which requires the pre-calculation of the first few hundred primes (using Sieve of Eratosthenes).
- The prime candidate is divided by the pre-generated primes to check for divisibility. If the prime candidate is perfectly divisible by any of these pre-generated primes, the test fails and a new prime candidate must be picked and tested. This is repeated as long as a value which is coprime to all the primes in our generated primes list is found
- A prime candidate passing the low-level test is then tested again using the Rabin Miller Primality Test.
- For extremely large numbers, such as ones used in RSA, deterministic testing of whether the chosen value is prime or not is highly impractical as it requires an unreasonable amount of computing resources.
- A probabilistic approach is preferred as such. If an inputted value passes a single iteration of the Rabin Miller test, the probability of the number being prime is 75%.
- Thus, a candidate passing the test, an adequate number of times, can be considered to be a prime with a satisfactory level of probability.
- Usually, in commercial applications, we require error probabilities to be less than .
Finally, we can combine the above functions to create a three-step process to generate large primes. The steps would be
- Random number generation by calling nBitRandom(bitsize)
- Basic division test by calling getLowLevelPrime(prime_candidate)
- Rabin Miller Test by calling isMillerRabinPassed(prime_candidate)
- If the chosen random value passes all primality tests, it is returned as the n-bit prime number. Otherwise, in the case of test-failure, a new random value is picked and tested for primality. The process is repeated until the desired prime is found.
Below is the complete implementation of the above approach
1024 bit prime is:
Note: Library Generation of Large Primes in Python
The pycrypto library is a comprehensive collection of secure hash functions and various encryption algorithms. It also includes basic functions commonly required in encryption/decryption setups such as random number generation and random prime number generation. The goal of generating a random prime number with a specified bit-size can be achieved using the pycrypto getPrime module.
The syntax for generating a random n-bit prime number is:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.