Skip to content

Commit 1472274

Browse files
committed
numbertheory: deprecate modular_exp
the function is just a wrapper around pow() builtin, so don't use it, mark it deprecated, exclude from coverage (as we're not testing it)
1 parent 518cfae commit 1472274

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/ecdsa/numbertheory.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,15 @@ class NegativeExponentError(Error):
3535
pass
3636

3737

38-
def modular_exp(base, exponent, modulus):
39-
"Raise base to exponent, reducing by modulus"
40-
if exponent < 0:
41-
raise NegativeExponentError("Negative exponents (%d) not allowed" \
42-
% exponent)
43-
return pow(base, exponent, modulus)
44-
# result = 1L
45-
# x = exponent
46-
# b = base + 0L
47-
# while x > 0:
48-
# if x % 2 > 0: result = (result * b) % modulus
49-
# x = x // 2
50-
# b = (b * b) % modulus
51-
# return result
38+
def modular_exp(base, exponent, modulus): # pragma: no cover
39+
"""Raise base to exponent, reducing by modulus"""
40+
# deprecated in 0.14
41+
warnings.warn("Function is unused in library code. If you use this code, "
42+
"change to pow() builtin.", DeprecationWarning)
43+
if exponent < 0:
44+
raise NegativeExponentError("Negative exponents (%d) not allowed"
45+
% exponent)
46+
return pow(base, exponent, modulus)
5247

5348

5449
def polynomial_reduce_mod(poly, polymod, p):
@@ -182,14 +177,14 @@ def square_root_mod_prime(a, p):
182177
% (a, p))
183178

184179
if p % 4 == 3:
185-
return modular_exp(a, (p + 1) // 4, p)
180+
return pow(a, (p + 1) // 4, p)
186181

187182
if p % 8 == 5:
188-
d = modular_exp(a, (p - 1) // 4, p)
183+
d = pow(a, (p - 1) // 4, p)
189184
if d == 1:
190-
return modular_exp(a, (p + 3) // 8, p)
185+
return pow(a, (p + 3) // 8, p)
191186
if d == p - 1:
192-
return (2 * a * modular_exp(4 * a, (p - 5) // 8, p)) % p
187+
return (2 * a * pow(4 * a, (p - 5) // 8, p)) % p
193188
raise RuntimeError("Shouldn't get here.")
194189

195190
if PY3:
@@ -524,11 +519,11 @@ def is_prime(n):
524519
r = r // 2
525520
for i in xrange(t):
526521
a = smallprimes[i]
527-
y = modular_exp(a, r, n)
522+
y = pow(a, r, n)
528523
if y != 1 and y != n - 1:
529524
j = 1
530525
while j <= s - 1 and y != n - 1:
531-
y = modular_exp(y, 2, n)
526+
y = pow(y, 2, n)
532527
if y == 1:
533528
miller_rabin_test_count = i + 1
534529
return False

src/ecdsa/test_numbertheory.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121

2222
def test_numbertheory():
2323

24-
# Making sure locally defined exceptions work:
25-
# p = modular_exp(2, -2, 3)
26-
# p = square_root_mod_prime(2, 3)
27-
2824
print_("Testing lcm...")
2925
assert lcm(3, 5 * 3, 7 * 3) == 3 * 5 * 7
3026
assert lcm([3, 5 * 3, 7 * 3]) == 3 * 5 * 7

0 commit comments

Comments
 (0)