Skip to content

Commit 4845e8f

Browse files
committed
better is_prime()
1 parent 09f0d10 commit 4845e8f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ecdsa/numbertheory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import math
4242
import warnings
43+
import random
4344
from .util import bit_length
4445

4546

@@ -573,6 +574,7 @@ def is_prime(n):
573574

574575
t = 40
575576
n_bits = 1 + bit_length(n)
577+
assert 11 <= n_bits <= 16384
576578
for k, tt in (
577579
(100, 27),
578580
(150, 18),
@@ -599,7 +601,7 @@ def is_prime(n):
599601
s = s + 1
600602
r = r // 2
601603
for i in xrange(t):
602-
a = smallprimes[i]
604+
a = random.choice(smallprimes)
603605
y = pow(a, r, n)
604606
if y != 1 and y != n - 1:
605607
j = 1

src/ecdsa/test_numbertheory.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,29 @@ def test_large_prime(self):
305305
# nextPrime[2^2048]
306306
assert is_prime(mpz(2) ** 2048 + 0x3D5)
307307

308+
def test_pseudoprime_base_19(self):
309+
assert not is_prime(1543267864443420616877677640751301)
310+
311+
def test_pseudoprime_base_300(self):
312+
# F. Arnault "Constructing Carmichael Numbers Which Are Strong
313+
# Pseudoprimes to Several Bases". Journal of Symbolic
314+
# Computation. 20 (2): 151-161. doi:10.1006/jsco.1995.1042.
315+
# Section 4.4 Large Example (a pseudoprime to all bases up to
316+
# 300)
317+
p = int(
318+
"29 674 495 668 685 510 550 154 174 642 905 332 730 "
319+
"771 991 799 853 043 350 995 075 531 276 838 753 171 "
320+
"770 199 594 238 596 428 121 188 033 664 754 218 345 "
321+
"562 493 168 782 883".replace(" ", "")
322+
)
323+
324+
assert is_prime(p)
325+
for _ in range(10):
326+
if not is_prime(p * (313 * (p - 1) + 1) * (353 * (p - 1) + 1)):
327+
break
328+
else:
329+
assert False, "composite not detected"
330+
308331

309332
class TestNumbertheory(unittest.TestCase):
310333
def test_gcd(self):

0 commit comments

Comments
 (0)