diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/python/botan2.py | 26 | ||||
-rw-r--r-- | src/scripts/test_python.py | 14 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/python/botan2.py b/src/python/botan2.py index 311b00871..dc8376f65 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -1143,6 +1143,18 @@ class MPI(object): # For int or long (or whatever else), try converting to string: _DLL.botan_mp_set_from_str(self.__obj, _ctype_str(str(initial_value))) + @classmethod + def random(cls, rng_obj, bits): + bn = MPI() + _DLL.botan_mp_rand_bits(bn.handle_(), rng_obj.handle_(), c_size_t(bits)) + return bn + + @classmethod + def random_range(cls, rng_obj, lower, upper): + bn = MPI() + _DLL.botan_mp_rand_range(bn.handle_(), rng_obj.handle_(), lower.handle_(), upper.handle_()) + return bn + def __del__(self): _DLL.botan_mp_destroy(self.__obj) @@ -1178,6 +1190,20 @@ class MPI(object): rc = _DLL.botan_mp_is_negative(self.__obj) return rc == 1 + def is_positive(self): + rc = _DLL.botan_mp_is_positive(self.__obj) + return rc == 1 + + def is_zero(self): + rc = _DLL.botan_mp_is_zero(self.__obj) + return rc == 1 + + def is_odd(self): + return self.get_bit(0) == 1 + + def is_even(self): + return self.get_bit(0) == 0 + def flip_sign(self): _DLL.botan_mp_flip_sign(self.__obj) diff --git a/src/scripts/test_python.py b/src/scripts/test_python.py index 94da4dc24..1ba21f570 100644 --- a/src/scripts/test_python.py +++ b/src/scripts/test_python.py @@ -402,6 +402,20 @@ ofvkP1EDmpx50fHLawIDAQAB p = inv.pow_mod(botan2.MPI(46), mod) self.assertEqual(int(p), 42) + def test_mpi_random(self): + rng = botan2.RandomNumberGenerator() + + u = botan2.MPI.random(rng, 512) + self.assertEqual(u.bit_count(), 512) + + l = u >> 32 + self.assertEqual(l.bit_count(), 512-32) + + for _i in range(10): + x = botan2.MPI.random_range(rng, l, u) + self.assertTrue(x < u) + self.assertTrue(x > l) + def test_fpe(self): modulus = botan2.MPI('1000000000') |