diff options
-rw-r--r-- | rsa/common.py | 2 | ||||
-rw-r--r-- | tests/test_common.py | 12 |
2 files changed, 12 insertions, 2 deletions
diff --git a/rsa/common.py b/rsa/common.py index e074334..b573252 100644 --- a/rsa/common.py +++ b/rsa/common.py @@ -131,7 +131,7 @@ def extended_gcd(a, b): def inverse(x, n): - """Returns x^-1 (mod n) + """Returns the inverse of x % n under multiplication, a.k.a x^-1 (mod n) >>> inverse(7, 4) 3 diff --git a/tests/test_common.py b/tests/test_common.py index 453dcc8..ef32f61 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -18,7 +18,7 @@ import unittest import struct from rsa._compat import byte, b -from rsa.common import byte_size, bit_size, _bit_size +from rsa.common import byte_size, bit_size, _bit_size, inverse class TestByte(unittest.TestCase): @@ -75,3 +75,13 @@ class TestBitSize(unittest.TestCase): self.assertEqual(_bit_size(1 << 1024), 1025) self.assertEqual(_bit_size((1 << 1024) + 1), 1025) self.assertEqual(_bit_size((1 << 1024) - 1), 1024) + + +class TestInverse(unittest.TestCase): + def test_normal(self): + self.assertEqual(3, inverse(7, 4)) + self.assertEqual(9, inverse(5, 11)) + + def test_not_relprime(self): + self.assertRaises(ValueError, inverse, 4, 8) + self.assertRaises(ValueError, inverse, 25, 5) |