From c240600f2490fb7fd01db78dfa8a0e2aec003633 Mon Sep 17 00:00:00 2001 From: Tom Cosgrove Date: Tue, 6 Dec 2022 12:20:43 +0000 Subject: Separate out to_montgomery and from_montgomery for bignum tests Signed-off-by: Tom Cosgrove --- scripts/mbedtls_dev/bignum_common.py | 6 ++++++ scripts/mbedtls_dev/bignum_core.py | 6 +++--- scripts/mbedtls_dev/bignum_mod_raw.py | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/mbedtls_dev/bignum_common.py b/scripts/mbedtls_dev/bignum_common.py index 67ea78db4..81bc28e19 100644 --- a/scripts/mbedtls_dev/bignum_common.py +++ b/scripts/mbedtls_dev/bignum_common.py @@ -251,6 +251,12 @@ class ModOperationCommon(OperationCommon): # provides earlier/more robust input validation. 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 def boundary(self) -> int: return self.int_n diff --git a/scripts/mbedtls_dev/bignum_core.py b/scripts/mbedtls_dev/bignum_core.py index a000bde07..118a659cf 100644 --- a/scripts/mbedtls_dev/bignum_core.py +++ b/scripts/mbedtls_dev/bignum_core.py @@ -764,7 +764,7 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon): def arguments(self) -> List[str]: # 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)) return [bignum_common.quote_str(n) for n in [self.arg_n, arg_mont_a, @@ -772,9 +772,9 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon): ] + self.result() 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) - mont_result = (result * self.r) % self.int_n + mont_result = self.to_montgomery(result) return [self.format_result(mont_result)] @property diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 0bbad5dd9..d05479a00 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -92,10 +92,9 @@ class BignumModRawConvertToMont(bignum_common.ModOperationCommon, arity = 1 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)] - class BignumModRawConvertFromMont(bignum_common.ModOperationCommon, BignumModRawTarget): """ Test cases for mpi_mod_raw_from_mont_rep(). """ @@ -106,7 +105,7 @@ class BignumModRawConvertFromMont(bignum_common.ModOperationCommon, arity = 1 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)] -- cgit v1.2.3