From 4cafd9fa6223ec9e99b0753be0d0906aa21a577f Mon Sep 17 00:00:00 2001 From: Christoph Zurnieden Date: Wed, 28 Oct 2026 17:08:14 +0100 Subject: [PATCH] bugfix #616 --- demo/test.c | 25 +++++++++++++++---------- mp_exptmod.c | 6 ++++++ mp_invmod.c | 4 ++-- tommath_class.h | 1 + 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/demo/test.c b/demo/test.c index 2fa6e08db..cc060193c 100644 --- a/demo/test.c +++ b/demo/test.c @@ -148,6 +148,11 @@ static int test_trivial_stuff(void) mp_set(&c, 7u); /* same here */ EXPECT(mp_exptmod(&a, &b, &c, &d) != MP_OKAY); + /* Check if exptmod(n, 0, 1) == 1 */ + mp_zero(&b); + DO(mp_exptmod(&a, &b, &c, &d)); + EXPECT(mp_isone(&d)); + EXPECT(mp_iseven(&a) != mp_isodd(&a)); @@ -584,16 +589,16 @@ static int test_mp_invmod(void) j = -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 */ { - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1, 2, -1, 8, -1 }, /* i = -10 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 3, 1, -1, 3, 7, -1, 1 }, /* -9 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 3, -1, 6, -1, 1, -1 }, /* -8 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 1, 2, 5, -1, 1, 5, 7 }, /* -7 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, -1, 1, -1, -1, -1 }, /* -6 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 3, -1, 1, 4, 3, 7, -1 }, /* -5 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, 1, -1, 5, -1, 2, -1 }, /* -4 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 3, -1, 2, 5, -1, 3 }, /* -3 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 2, -1, 3, -1, 4, -1 }, /* -2 */ - {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, /* -1 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 2, -1, -1, -1, 2, -1, 8, -1 }, /* i = -10 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 3, 1, -1, 3, 7, -1, 1 }, /* -9 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 3, -1, 6, -1, 1, -1 }, /* -8 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 5, -1, 1, 5, 7 }, /* -7 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 4, -1, 1, -1, -1, -1 }, /* -6 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 3, -1, 1, 4, 3, 7, -1 }, /* -5 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 2, -1, 1, -1, 5, -1, 2, -1 }, /* -4 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 3, -1, 2, 5, -1, 3 }, /* -3 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1 }, /* -2 */ + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, /* -1 */ {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* 0 */ {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* 1 */ {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 2, -1, 3, -1, 4, -1, 5, -1 }, /* 2 */ diff --git a/mp_exptmod.c b/mp_exptmod.c index eaab861eb..97ea515b0 100644 --- a/mp_exptmod.c +++ b/mp_exptmod.c @@ -17,6 +17,12 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) return MP_VAL; } + /* n = 0 mod 1 for all n in Z */ + if (mp_isone(P)) { + mp_zero(Y); + return MP_OKAY; + } + /* if exponent X is negative we have to recurse */ if (mp_isneg(X)) { mp_int tmpG, tmpX; diff --git a/mp_invmod.c b/mp_invmod.c index 2494acbf6..40ccb0c6e 100644 --- a/mp_invmod.c +++ b/mp_invmod.c @@ -6,8 +6,8 @@ /* hac 14.61, pp608 */ mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) { - /* for all n in N and n > 0, n = 0 mod 1 */ - if (!mp_isneg(a) && mp_cmp_d(b, 1uL) == MP_EQ) { + /* for all n in Z, n = 0 mod 1 */ + if (mp_isone(b)) { mp_zero(c); return MP_OKAY; } diff --git a/tommath_class.h b/tommath_class.h index 40ddfd4b4..135eb54a1 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -314,6 +314,7 @@ # define MP_INVMOD_C # define MP_REDUCE_IS_2K_C # define MP_REDUCE_IS_2K_L_C +# define MP_ZERO_C # define S_MP_EXPTMOD_C # define S_MP_EXPTMOD_FAST_C #endif