diff options
author | Tom Cosgrove <tom.cosgrove@arm.com> | 2022-12-16 03:53:17 +0000 |
---|---|---|
committer | Tom Cosgrove <tom.cosgrove@arm.com> | 2022-12-16 03:53:17 +0000 |
commit | 1133d2325b9bd3a00fcdbf647de27991562ae35c (patch) | |
tree | 44ff6db0dce9b3cedc854fac2d46143365e02b19 | |
parent | dc197593273864636bb8cb49b73ed38ea81a7886 (diff) | |
download | mbedtls-1133d2325b9bd3a00fcdbf647de27991562ae35c.zip mbedtls-1133d2325b9bd3a00fcdbf647de27991562ae35c.tar.gz mbedtls-1133d2325b9bd3a00fcdbf647de27991562ae35c.tar.bz2 |
Attempt to pacify pylint in bignum tests
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
-rw-r--r-- | scripts/mbedtls_dev/bignum_common.py | 17 | ||||
-rw-r--r-- | scripts/mbedtls_dev/bignum_core.py | 10 | ||||
-rw-r--r-- | scripts/mbedtls_dev/bignum_mod.py | 24 | ||||
-rw-r--r-- | scripts/mbedtls_dev/bignum_mod_raw.py | 16 |
4 files changed, 26 insertions, 41 deletions
diff --git a/scripts/mbedtls_dev/bignum_common.py b/scripts/mbedtls_dev/bignum_common.py index 0339b1a..dd4fc36 100644 --- a/scripts/mbedtls_dev/bignum_common.py +++ b/scripts/mbedtls_dev/bignum_common.py @@ -39,6 +39,11 @@ def invmod(a: int, n: int) -> int: return b raise ValueError("Not invertible") +def invmod_positive(a: int, n: int) -> int: + """Return a non-negative inverse of a to modulo n.""" + inv = invmod(a, n) + return inv if inv >= 0 else inv + n + def hex_to_int(val: str) -> int: """Implement the syntax accepted by mbedtls_test_read_mpi(). @@ -244,6 +249,8 @@ class ModOperationCommon(OperationCommon): #pylint: disable=abstract-method """Target for bignum mod_raw test case generation.""" moduli = MODULI_DEFAULT # type: List[str] + mongtomgery_form_a = False + disallow_zero_a = False def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None: @@ -264,6 +271,14 @@ class ModOperationCommon(OperationCommon): return self.int_n @property + def arg_a(self) -> str: + if self.mongtomgery_form_a: + value_a = self.to_montgomery(self.int_a) + else: + value_a = self.int_a + return self.format_arg('{:x}'.format(value_a)) + + @property def arg_n(self) -> str: return self.format_arg(self.val_n) @@ -287,6 +302,8 @@ class ModOperationCommon(OperationCommon): def is_valid(self) -> bool: if self.int_a >= self.int_n: return False + if self.disallow_zero_a and self.int_a == 0: + return False if self.arity == 2 and self.int_b >= self.int_n: return False return True diff --git a/scripts/mbedtls_dev/bignum_core.py b/scripts/mbedtls_dev/bignum_core.py index 1a8c22b..3bd7f11 100644 --- a/scripts/mbedtls_dev/bignum_core.py +++ b/scripts/mbedtls_dev/bignum_core.py @@ -757,15 +757,7 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon): test_function = "mpi_core_exp_mod" test_name = "Core modular exponentiation (Mongtomery form only)" input_style = "fixed" - - def arguments(self) -> List[str]: - # Input 'a' has to be given in Montgomery form - mont_a = self.to_montgomery(self.int_a) - arg_mont_a = self.format_arg('{:x}'.format(mont_a)) - return [bignum_common.quote_str(n) for n in [self.arg_n, - arg_mont_a, - self.arg_b] - ] + self.result() + mongtomgery_form_a = True def result(self) -> List[str]: # Result has to be given in Montgomery form too diff --git a/scripts/mbedtls_dev/bignum_mod.py b/scripts/mbedtls_dev/bignum_mod.py index 9f98131..6428873 100644 --- a/scripts/mbedtls_dev/bignum_mod.py +++ b/scripts/mbedtls_dev/bignum_mod.py @@ -58,15 +58,10 @@ class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget): input_style = "fixed" arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n + disallow_zero_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) # To make negative tests easier, append 0 for success to the # generated cases return [self.format_result(result), "0"] @@ -80,20 +75,11 @@ class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget): input_style = "arch_split" # Mont. form requires arch_split arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n - - @property - def arg_a(self) -> str: - mont_a = self.to_montgomery(self.int_a) - return self.format_arg('{:x}'.format(mont_a)) + disallow_zero_a = True + mongtomgery_form_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) mont_result = self.to_montgomery(result) # To make negative tests easier, append 0 for success to the # generated cases diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 0084898..461b1f2 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -64,21 +64,11 @@ class BignumModRawInvPrime(bignum_common.ModOperationCommon, input_style = "arch_split" arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n - - @property - def arg_a(self) -> str: - # Input has to be given in Montgomery form - mont_a = self.to_montgomery(self.int_a) - return self.format_arg('{:x}'.format(mont_a)) + mongtomgery_form_a = True + disallow_zero_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) mont_result = self.to_montgomery(result) return [self.format_result(mont_result)] |