From ed5b14ee233de198ddd7e0c1952a7869e1ddbb9f Mon Sep 17 00:00:00 2001 From: Felix Conway Date: Tue, 29 Jul 2025 12:09:16 +0100 Subject: [PATCH] Use math.gcd() instead of custom function Signed-off-by: Felix Conway --- scripts/generate_bignum_tests.py | 7 ++++--- scripts/mbedtls_framework/bignum_common.py | 7 ------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/scripts/generate_bignum_tests.py b/scripts/generate_bignum_tests.py index 17ee1bfde..e363c2968 100755 --- a/scripts/generate_bignum_tests.py +++ b/scripts/generate_bignum_tests.py @@ -43,6 +43,7 @@ of BaseTarget in test_data_generation.py. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later import sys +import math from abc import ABCMeta from typing import List @@ -188,9 +189,9 @@ class BignumInvMod(BignumOperation): def __init__(self, val_a: str, val_b: str) -> None: super().__init__(val_a, val_b) - try: + if math.gcd(self.int_a, self.int_b) == 1: self._result = bignum_common.invmod_positive(self.int_a, self.int_b) - except ValueError: + else: self._result = -1 # No modular inverse. def description_suffix(self) -> str: @@ -264,7 +265,7 @@ class BignumGCD(BignumOperation): def __init__(self, val_a: str, val_b: str) -> None: super().__init__(val_a, val_b) # We always expect a positive result. - self._result = abs(bignum_common.gcd(self.int_a, self.int_b)) + self._result = math.gcd(self.int_a, self.int_b) def description_suffix(self) -> str: suffix = ": " diff --git a/scripts/mbedtls_framework/bignum_common.py b/scripts/mbedtls_framework/bignum_common.py index 8bc6a9dd3..58a76a171 100644 --- a/scripts/mbedtls_framework/bignum_common.py +++ b/scripts/mbedtls_framework/bignum_common.py @@ -30,13 +30,6 @@ def invmod(a: int, n: int) -> int: return b raise ValueError("Not invertible") -def gcd(a: int, b: int) -> int: - """Return the greatest common divisor of two numbers. - The result will match the sign of b.""" - while b: - a, b = b, a % b - return a - def invmod_positive(a: int, n: int) -> int: """Return a non-negative inverse of a to modulo n.