Separate out to_montgomery and from_montgomery for bignum tests

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2022-12-06 12:20:43 +00:00
parent ecda186893
commit c240600f24
3 changed files with 11 additions and 6 deletions

View file

@ -251,6 +251,12 @@ class ModOperationCommon(OperationCommon):
# provides earlier/more robust input validation. # provides earlier/more robust input validation.
self.int_n = hex_to_int(val_n) self.int_n = hex_to_int(val_n)
def to_montgomery(self, val) -> int:
return (val * self.r) % self.int_n
def from_montgomery(self, val) -> int:
return (val * self.r_inv) % self.int_n
@property @property
def boundary(self) -> int: def boundary(self) -> int:
return self.int_n return self.int_n

View file

@ -764,7 +764,7 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon):
def arguments(self) -> List[str]: def arguments(self) -> List[str]:
# Input 'a' has to be given in Montgomery form # Input 'a' has to be given in Montgomery form
mont_a = (self.int_a * self.r) % self.int_n mont_a = self.to_montgomery(self.int_a)
arg_mont_a = self.format_arg('{:x}'.format(mont_a)) arg_mont_a = self.format_arg('{:x}'.format(mont_a))
return [bignum_common.quote_str(n) for n in [self.arg_n, return [bignum_common.quote_str(n) for n in [self.arg_n,
arg_mont_a, arg_mont_a,
@ -772,9 +772,9 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon):
] + self.result() ] + self.result()
def result(self) -> List[str]: def result(self) -> List[str]:
# Result has to be given in Montgomery form # Result has to be given in Montgomery form too
result = pow(self.int_a, self.int_b, self.int_n) result = pow(self.int_a, self.int_b, self.int_n)
mont_result = (result * self.r) % self.int_n mont_result = self.to_montgomery(result)
return [self.format_result(mont_result)] return [self.format_result(mont_result)]
@property @property

View file

@ -92,10 +92,9 @@ class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
arity = 1 arity = 1
def result(self) -> List[str]: def result(self) -> List[str]:
result = (self.int_a * self.r) % self.int_n result = self.to_montgomery(self.int_a)
return [self.format_result(result)] return [self.format_result(result)]
class BignumModRawConvertFromMont(bignum_common.ModOperationCommon, class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
BignumModRawTarget): BignumModRawTarget):
""" Test cases for mpi_mod_raw_from_mont_rep(). """ """ Test cases for mpi_mod_raw_from_mont_rep(). """
@ -106,7 +105,7 @@ class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
arity = 1 arity = 1
def result(self) -> List[str]: def result(self) -> List[str]:
result = (self.int_a * self.r_inv) % self.int_n result = self.from_montgomery(self.int_a)
return [self.format_result(result)] return [self.format_result(result)]