From b3575b30f26699ef8ce39490aa53f623865b7478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 28 Jul 2025 23:42:14 +0200 Subject: [PATCH] Fix documentation of bignum_common.invmod() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python's built-in pow() always returns a positive result. Signed-off-by: Manuel Pégourié-Gonnard --- scripts/mbedtls_framework/bignum_common.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/mbedtls_framework/bignum_common.py b/scripts/mbedtls_framework/bignum_common.py index eebc858b2..28143ce0f 100644 --- a/scripts/mbedtls_framework/bignum_common.py +++ b/scripts/mbedtls_framework/bignum_common.py @@ -19,8 +19,7 @@ T = TypeVar('T') #pylint: disable=invalid-name def invmod(a: int, n: int) -> int: """Return inverse of a to modulo n. - Equivalent to pow(a, -1, n) in Python 3.8+. Implementation is equivalent - to long_invmod() in CPython. + Warning: the output might be negative! See invmod_positive(). """ b, c = 1, 0 while n: @@ -32,7 +31,10 @@ def invmod(a: int, n: int) -> int: raise ValueError("Not invertible") 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. + + Equivalent to pow(a, -1, n) in Python 3.8+. + """ inv = invmod(a, n) return inv if inv >= 0 else inv + n