mirror of
https://github.com/ARMmbed/mbedtls.git
synced 2025-05-11 09:22:05 +08:00
Attempt to pacify pylint in bignum tests
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
parent
dc19759327
commit
1133d2325b
@ -39,6 +39,11 @@ def invmod(a: int, n: int) -> int:
|
|||||||
return b
|
return b
|
||||||
raise ValueError("Not invertible")
|
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:
|
def hex_to_int(val: str) -> int:
|
||||||
"""Implement the syntax accepted by mbedtls_test_read_mpi().
|
"""Implement the syntax accepted by mbedtls_test_read_mpi().
|
||||||
|
|
||||||
@ -244,6 +249,8 @@ class ModOperationCommon(OperationCommon):
|
|||||||
#pylint: disable=abstract-method
|
#pylint: disable=abstract-method
|
||||||
"""Target for bignum mod_raw test case generation."""
|
"""Target for bignum mod_raw test case generation."""
|
||||||
moduli = MODULI_DEFAULT # type: List[str]
|
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",
|
def __init__(self, val_n: str, val_a: str, val_b: str = "0",
|
||||||
bits_in_limb: int = 64) -> None:
|
bits_in_limb: int = 64) -> None:
|
||||||
@ -263,6 +270,14 @@ class ModOperationCommon(OperationCommon):
|
|||||||
def boundary(self) -> int:
|
def boundary(self) -> int:
|
||||||
return self.int_n
|
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
|
@property
|
||||||
def arg_n(self) -> str:
|
def arg_n(self) -> str:
|
||||||
return self.format_arg(self.val_n)
|
return self.format_arg(self.val_n)
|
||||||
@ -287,6 +302,8 @@ class ModOperationCommon(OperationCommon):
|
|||||||
def is_valid(self) -> bool:
|
def is_valid(self) -> bool:
|
||||||
if self.int_a >= self.int_n:
|
if self.int_a >= self.int_n:
|
||||||
return False
|
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:
|
if self.arity == 2 and self.int_b >= self.int_n:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
@ -757,15 +757,7 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon):
|
|||||||
test_function = "mpi_core_exp_mod"
|
test_function = "mpi_core_exp_mod"
|
||||||
test_name = "Core modular exponentiation (Mongtomery form only)"
|
test_name = "Core modular exponentiation (Mongtomery form only)"
|
||||||
input_style = "fixed"
|
input_style = "fixed"
|
||||||
|
mongtomgery_form_a = True
|
||||||
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()
|
|
||||||
|
|
||||||
def result(self) -> List[str]:
|
def result(self) -> List[str]:
|
||||||
# Result has to be given in Montgomery form too
|
# Result has to be given in Montgomery form too
|
||||||
|
@ -58,15 +58,10 @@ class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
|
|||||||
input_style = "fixed"
|
input_style = "fixed"
|
||||||
arity = 1
|
arity = 1
|
||||||
suffix = True
|
suffix = True
|
||||||
|
disallow_zero_a = True
|
||||||
@property
|
|
||||||
def is_valid(self) -> bool:
|
|
||||||
return self.int_a > 0 and self.int_a < self.int_n
|
|
||||||
|
|
||||||
def result(self) -> List[str]:
|
def result(self) -> List[str]:
|
||||||
result = bignum_common.invmod(self.int_a, self.int_n)
|
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||||
if result < 0:
|
|
||||||
result += self.int_n
|
|
||||||
# To make negative tests easier, append 0 for success to the
|
# To make negative tests easier, append 0 for success to the
|
||||||
# generated cases
|
# generated cases
|
||||||
return [self.format_result(result), "0"]
|
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
|
input_style = "arch_split" # Mont. form requires arch_split
|
||||||
arity = 1
|
arity = 1
|
||||||
suffix = True
|
suffix = True
|
||||||
|
disallow_zero_a = True
|
||||||
@property
|
mongtomgery_form_a = True
|
||||||
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))
|
|
||||||
|
|
||||||
def result(self) -> List[str]:
|
def result(self) -> List[str]:
|
||||||
result = bignum_common.invmod(self.int_a, self.int_n)
|
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||||
if result < 0:
|
|
||||||
result += self.int_n
|
|
||||||
mont_result = self.to_montgomery(result)
|
mont_result = self.to_montgomery(result)
|
||||||
# To make negative tests easier, append 0 for success to the
|
# To make negative tests easier, append 0 for success to the
|
||||||
# generated cases
|
# generated cases
|
||||||
|
@ -64,21 +64,11 @@ class BignumModRawInvPrime(bignum_common.ModOperationCommon,
|
|||||||
input_style = "arch_split"
|
input_style = "arch_split"
|
||||||
arity = 1
|
arity = 1
|
||||||
suffix = True
|
suffix = True
|
||||||
|
mongtomgery_form_a = True
|
||||||
@property
|
disallow_zero_a = True
|
||||||
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))
|
|
||||||
|
|
||||||
def result(self) -> List[str]:
|
def result(self) -> List[str]:
|
||||||
result = bignum_common.invmod(self.int_a, self.int_n)
|
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||||
if result < 0:
|
|
||||||
result += self.int_n
|
|
||||||
mont_result = self.to_montgomery(result)
|
mont_result = self.to_montgomery(result)
|
||||||
return [self.format_result(mont_result)]
|
return [self.format_result(mont_result)]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user