mirror of
https://github.com/Mbed-TLS/mbedtls-framework.git
synced 2026-11-02 05:22:47 +00:00
Use math.gcd() instead of custom function
Signed-off-by: Felix Conway <[email protected]>
This commit is contained in:
@@ -43,6 +43,7 @@ of BaseTarget in test_data_generation.py.
|
|||||||
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import math
|
||||||
|
|
||||||
from abc import ABCMeta
|
from abc import ABCMeta
|
||||||
from typing import List
|
from typing import List
|
||||||
@@ -188,9 +189,9 @@ class BignumInvMod(BignumOperation):
|
|||||||
|
|
||||||
def __init__(self, val_a: str, val_b: str) -> None:
|
def __init__(self, val_a: str, val_b: str) -> None:
|
||||||
super().__init__(val_a, val_b)
|
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)
|
self._result = bignum_common.invmod_positive(self.int_a, self.int_b)
|
||||||
except ValueError:
|
else:
|
||||||
self._result = -1 # No modular inverse.
|
self._result = -1 # No modular inverse.
|
||||||
|
|
||||||
def description_suffix(self) -> str:
|
def description_suffix(self) -> str:
|
||||||
@@ -264,7 +265,7 @@ class BignumGCD(BignumOperation):
|
|||||||
def __init__(self, val_a: str, val_b: str) -> None:
|
def __init__(self, val_a: str, val_b: str) -> None:
|
||||||
super().__init__(val_a, val_b)
|
super().__init__(val_a, val_b)
|
||||||
# We always expect a positive result.
|
# 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:
|
def description_suffix(self) -> str:
|
||||||
suffix = ": "
|
suffix = ": "
|
||||||
|
|||||||
@@ -30,13 +30,6 @@ def invmod(a: int, n: int) -> int:
|
|||||||
return b
|
return b
|
||||||
raise ValueError("Not invertible")
|
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:
|
def invmod_positive(a: int, n: int) -> int:
|
||||||
"""Return a non-negative inverse of a to modulo n.
|
"""Return a non-negative inverse of a to modulo n.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user