Use math.gcd() instead of custom function

Signed-off-by: Felix Conway <[email protected]>
This commit is contained in:
Felix Conway
2025-07-30 13:12:47 +01:00
parent 2b4e41d37f
commit ed5b14ee23
2 changed files with 4 additions and 10 deletions
+4 -3
View File
@@ -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 = ": "
@@ -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.