Skip to content

Commit 307fa3a

Browse files
committed
fast examples for numbertheory tests
1 parent 87ef2b2 commit 307fa3a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/ecdsa/numbertheory.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
except NameError:
2121
xrange = range
2222
try:
23-
from gmpy2 import powmod
23+
from gmpy2 import powmod, mpz
2424

2525
GMPY2 = True
2626
GMPY = False
27-
except ImportError:
27+
except ImportError: # pragma: no branch
2828
GMPY2 = False
2929
try:
3030
from gmpy import mpz
@@ -33,8 +33,14 @@
3333
except ImportError:
3434
GMPY = False
3535

36+
37+
if GMPY2 or GMPY: # pragma: no branch
38+
integer_types = tuple(integer_types + (type(mpz(1)),))
39+
40+
3641
import math
3742
import warnings
43+
from .util import bit_length
3844

3945

4046
class Error(Exception):
@@ -555,16 +561,16 @@ def is_prime(n):
555561
return True
556562
else:
557563
return False
558-
559-
if gcd(n, 2 * 3 * 5 * 7 * 11) != 1:
564+
# 2310 = 2 * 3 * 5 * 7 * 11
565+
if gcd(n, 2310) != 1:
560566
return False
561567

562568
# Choose a number of iterations sufficient to reduce the
563569
# probability of accepting a composite below 2**-80
564570
# (from Menezes et al. Table 4.4):
565571

566572
t = 40
567-
n_bits = 1 + int(math.log(n, 2))
573+
n_bits = 1 + bit_length(n)
568574
for k, tt in (
569575
(100, 27),
570576
(150, 18),

src/ecdsa/test_numbertheory.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
square_root_mod_prime,
3131
)
3232

33+
try:
34+
from gmpy2 import mpz
35+
except ImportError:
36+
try:
37+
from gmpy import mpz
38+
except ImportError:
39+
40+
def mpz(x):
41+
return x
42+
3343

3444
BIGPRIMES = (
3545
999671,
@@ -293,7 +303,7 @@ def test_medium_non_trivial_composite(self):
293303

294304
def test_large_prime(self):
295305
# nextPrime[2^2048]
296-
assert is_prime(2**2048 + 0x3D5)
306+
assert is_prime(mpz(2) ** 2048 + 0x3D5)
297307

298308

299309
class TestNumbertheory(unittest.TestCase):
@@ -309,6 +319,7 @@ def test_gcd(self):
309319
"case times-out on it",
310320
)
311321
@settings(**HYP_SLOW_SETTINGS)
322+
@example([877 * 1151, 877 * 1009])
312323
@given(st_comp_with_com_fac())
313324
def test_gcd_with_com_factor(self, numbers):
314325
n = gcd(numbers)
@@ -323,6 +334,7 @@ def test_gcd_with_com_factor(self, numbers):
323334
"case times-out on it",
324335
)
325336
@settings(**HYP_SLOW_SETTINGS)
337+
@example([1151, 1069, 1009])
326338
@given(st_comp_no_com_fac())
327339
def test_gcd_with_uncom_factor(self, numbers):
328340
n = gcd(numbers)
@@ -415,13 +427,16 @@ def test_jacobi_with_one(self):
415427
@settings(**HYP_SLOW_SETTINGS)
416428
@given(st.integers(min_value=3, max_value=1000).filter(lambda x: x % 2))
417429
def test_jacobi(self, mod):
430+
mod = mpz(mod)
418431
if is_prime(mod):
419432
squares = set()
420433
for root in range(1, mod):
434+
root = mpz(root)
421435
assert jacobi(root * root, mod) == 1
422436
squares.add(root * root % mod)
423437
for i in range(1, mod):
424438
if i not in squares:
439+
i = mpz(i)
425440
assert jacobi(i, mod) == -1
426441
else:
427442
factors = factorization(mod)

0 commit comments

Comments
 (0)