Although this has been extensively discussed around here, I'm curious whether my approach makes sense, or I should just stick to 'the standard version'.I'm implementing some homomorphic encryption primitives (Paillier, Okamoto–Uchiyama etc) and, at the moment, I'm using GMP as the big number library (this may change in the future). GMP has a function called mpznextprime and I generate random numbers, after which I call this function on them, to get prime numbers.
Cryptography’s not-so-secret sauce. For modern cryptography, prime numbers' randomness is handy. The ubiquitous RSA encryption algorithm multiplies two very large random numbers in the knowledge.
Is this a good approach? The other alternative would be to just generate random numbers in a loop, until mpzprobabprimep(number, 10) says that they are prime. This latter approach seems rather wasteful though, from a programmer's point of view.How do you generate prime numbers with GMP or OpenSSL (or other crypto libraries like cryptlib, crypto, etc)? I am unsure how the mpznextprime function works or if other libraries provide such functionality. Ideally, I should be able to just swap various big number libraries in my implementation, without changing the prime generator wrapper.
$begingroup$ Here is an approach to select a random prime nearly free of bias. Say that for some $a,b$ with $2≤a≪b$ we want a random prime $p$ with $a≤p.
Two mathematicians have uncovered a simple, previously unnoticed property of prime numbers — those numbers that are divisible only by 1 and themselves. Prime numbers, it seems, have decided preferences about the final digits of the primes that immediately follow them.Among the first billion prime numbers, for instance, a prime ending in 9 is almost 65 percent more likely to be followed by a prime ending in 1 than another prime ending in 9. In a, and of Stanford University present both numerical and theoretical evidence that prime numbers repel other would-be primes that end in the same digit, and have varied predilections for being followed by primes ending in the other possible final digits. “We’ve been studying primes for a long time, and no one spotted this before,” said, a number theorist at the University of Montreal and University College London. “It’s crazy.”The discovery is the exact opposite of what most mathematicians would have predicted, said, a number theorist at Emory University in Atlanta. When he first heard the news, he said, “I was floored. I thought, ‘For sure, your program’s not working.’”This conspiracy among prime numbers seems, at first glance, to violate a longstanding assumption in number theory: that prime numbers behave much like random numbers.
Most mathematicians would have assumed, Granville and Ono agreed, that a prime should have an equal chance of being followed by a prime ending in 1, 3, 7 or 9 (the four possible endings for all prime numbers except 2 and 5).“I can’t believe anyone in the world would have guessed this,” Granville said. Even after having seen Lemke Oliver and Soundararajan’s analysis of their phenomenon, he said, “it still seems like a strange thing.”Yet the pair’s work doesn’t upend the notion that primes behave randomly so much as point to how subtle their particular mix of randomness and order is. “Can we redefine what ‘random’ means in this context so that once again, this phenomenon looks like it might be random?” Soundararajan said. “That’s what we think we’ve done.” Prime PreferencesSoundararajan was drawn to study consecutive primes after hearing a lecture at Stanford by the mathematician, of the University of Cambridge, in which he mentioned a counterintuitive property of coin-tossing: If Alice tosses a coin until she sees a head followed by a tail, and Bob tosses a coin until he sees two heads in a row, then on average, Alice will require four tosses while Bob will require six tosses (try this at home!), even though head-tail and head-head have an equal chance of appearing after two coin tosses.