summaryrefslogtreecommitdiff
path: root/rsa/common.py
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2019-08-04 16:41:01 +0200
committerSybren A. Stüvel <sybren@stuvel.eu>2019-08-04 17:05:58 +0200
commitb6cebd53fcafd3088fc8361f6d3466166f75410b (patch)
treea1a3912fb9e91e249e433df0a9b79572f46340f3 /rsa/common.py
parent6760eb76e665dc81863a82110164c4b3b38e7ee9 (diff)
downloadrsa-git-b6cebd53fcafd3088fc8361f6d3466166f75410b.tar.gz
Added type annotations + some fixes to get them correct
One functional change: `CryptoOperation.read_infile()` now reads bytes from `sys.stdin` instead of text. This is necessary to be consistent with the rest of the code, which all deals with bytes.
Diffstat (limited to 'rsa/common.py')
-rw-r--r--rsa/common.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/rsa/common.py b/rsa/common.py
index a4337f6..b983b98 100644
--- a/rsa/common.py
+++ b/rsa/common.py
@@ -16,17 +16,18 @@
"""Common functionality shared by several modules."""
+import typing
+
class NotRelativePrimeError(ValueError):
- def __init__(self, a, b, d, msg=None):
- super(NotRelativePrimeError, self).__init__(
- msg or "%d and %d are not relatively prime, divider=%i" % (a, b, d))
+ def __init__(self, a, b, d, msg=''):
+ super().__init__(msg or "%d and %d are not relatively prime, divider=%i" % (a, b, d))
self.a = a
self.b = b
self.d = d
-def bit_size(num):
+def bit_size(num: int) -> int:
"""
Number of bits needed to represent a integer excluding any prefix
0 bits.
@@ -54,7 +55,7 @@ def bit_size(num):
raise TypeError('bit_size(num) only supports integers, not %r' % type(num))
-def byte_size(number):
+def byte_size(number: int) -> int:
"""
Returns the number of bytes required to hold a specific long number.
@@ -79,7 +80,7 @@ def byte_size(number):
return ceil_div(bit_size(number), 8)
-def ceil_div(num, div):
+def ceil_div(num: int, div: int) -> int:
"""
Returns the ceiling function of a division between `num` and `div`.
@@ -103,7 +104,7 @@ def ceil_div(num, div):
return quanta
-def extended_gcd(a, b):
+def extended_gcd(a: int, b: int) -> typing.Tuple[int, int, int]:
"""Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb
"""
# r = gcd(a,b) i = multiplicitive inverse of a mod b
@@ -128,7 +129,7 @@ def extended_gcd(a, b):
return a, lx, ly # Return only positive values
-def inverse(x, n):
+def inverse(x: int, n: int) -> int:
"""Returns the inverse of x % n under multiplication, a.k.a x^-1 (mod n)
>>> inverse(7, 4)
@@ -145,7 +146,7 @@ def inverse(x, n):
return inv
-def crt(a_values, modulo_values):
+def crt(a_values: typing.Iterable[int], modulo_values: typing.Iterable[int]) -> int:
"""Chinese Remainder Theorem.
Calculates x such that x = a[i] (mod m[i]) for each i.