Skip to content

Commit 29b7ebe

Browse files
committed
don't generate biased prime numbers
since the current algorithm selects a number and then goes up, primes for which the previous prime is far away are more likely to be selected, similarly the bigger of the twin primes is less likely to be selected make the prime selection a little bit more uniform by just modifying the randomly selecting number to be odd, if an even is selected
1 parent d59ac46 commit 29b7ebe

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

tlslite/utils/cryptomath.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,15 @@ def getRandomPrime(bits, display=False):
387387
#29 % 30 and keep them there
388388
low = ((2 ** (bits-1)) * 3) // 2
389389
high = 2 ** bits - 30
390-
p = getRandomNumber(low, high)
391-
p += 29 - (p % 30)
392-
while 1:
393-
if display: print(".", end=' ')
394-
p += 30
395-
if p >= high:
396-
p = getRandomNumber(low, high)
397-
p += 29 - (p % 30)
398-
if isPrime(p, display=display):
399-
return p
390+
while True:
391+
if display:
392+
print(".", end=' ')
393+
cand_p = getRandomNumber(low, high)
394+
# make odd
395+
if cand_p % 2 == 0:
396+
cand_p += 1
397+
if isPrime(cand_p, display=display):
398+
return cand_p
400399

401400

402401
#Unused at the moment...

0 commit comments

Comments
 (0)