Add generated tests for mpi_gcd()

Signed-off-by: Felix Conway <[email protected]>
This commit is contained in:
Felix Conway
2025-07-30 13:12:47 +01:00
parent cd3f8dad04
commit e200647618
2 changed files with 73 additions and 0 deletions
+66
View File
@@ -212,6 +212,72 @@ class BignumInvMod(BignumOperation):
return [bignum_common.quote_str("{:x}".format(self._result)), "0"]
class BignumGCD(BignumOperation):
"""Test cases for greatest common divisor."""
count = 0
symbol = "GCD"
test_function = "mpi_gcd"
test_name = "GCD"
# The default values are not very useful here, so clear them.
input_values = [] # type: List[str]
input_cases = bignum_common.combination_two_lists(
# Input values for A
[
"3c094fd6b36ee4902c8ba84d13a401def90a2130116dad3361",
"-3c094fd6b36ee4902c8ba84d13a401def90a2130116dad3361",
"b2b06ebe14a185a83d5d2d7bddd1dd0e05e800d6b914fbed4e",
"-b2b06ebe14a185a83d5d2d7bddd1dd0e05e800d6b914fbed4e",
"203265b387",
"-203265b387",
"9bc8e63852",
"-9bc8e63852",
"123456789abcdef",
"-123456789abcdef",
"50000",
"-50000",
],
# Input values for B
[
"63522ef00b3fa4c6773f1116dca45c121ee51b722644d96b63",
"-63522ef00b3fa4c6773f1116dca45c121ee51b722644d96b63",
"20902ee9da48664a3b9d5d68542405ac0ad0dd90ce79d633c0",
"-20902ee9da48664a3b9d5d68542405ac0ad0dd90ce79d633c0",
"7e9de225bb",
"-7e9de225bb",
"4796a42828",
"-4796a42828",
"123456789abcdef",
"-123456789abcdef",
"50000",
"-50000",
],
)
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))
def description_suffix(self) -> str:
suffix = ": "
if abs(self.int_a) > abs(self.int_b):
suffix += "|A|>|B|"
elif abs(self.int_a) < abs(self.int_b):
suffix += "|A|<|B|"
else:
suffix += "|A|=|B|"
if self.int_a < 0:
suffix += ", A<0"
if self.int_b < 0:
suffix += ", B<0"
suffix += ", A even" if (self.int_a % 2 == 0) else ", A odd"
suffix += ", B even" if (self.int_b % 2 == 0) else ", B odd"
return suffix
def result(self) -> List[str]:
return [bignum_common.quote_str("{:x}".format(self._result))]
class BignumAdd(BignumOperation):
"""Test cases for bignum value addition."""
count = 0
@@ -30,6 +30,13 @@ 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.