Skip to content

Commit fe382af

Browse files
authored
Merge pull request #478 from MasterDuke17/make_exptmod_with_modulus_1_always_0
Give correct values for invmod with modulus of 1
2 parents ce4e6ae + ea65456 commit fe382af

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

demo/test.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ static int test_trivial_stuff(void)
135135
mp_set(&b, 1u);
136136
DO(mp_neg(&b, &b));
137137
mp_set(&c, 1u);
138-
/* I expected this works, but somehow the computer sez no
139-
* DO(mp_exptmod(&a, &b, &c, &d));
140-
*/
141-
EXPECT(mp_exptmod(&a, &b, &c, &d) != MP_OKAY);
138+
DO(mp_exptmod(&a, &b, &c, &d));
142139

143140
mp_set(&c, 7u);
144141
/* same here */

mp_invmod.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
/* hac 14.61, pp608 */
77
mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
88
{
9+
/* for all n in N and n > 0, n = 0 mod 1 */
10+
if (!mp_isneg(a) && mp_cmp_d(b, 1uL) == MP_EQ) {
11+
mp_zero(c);
12+
return MP_OKAY;
13+
}
14+
915
/* b cannot be negative and has to be >1 */
1016
if (mp_isneg(b) || (mp_cmp_d(b, 1uL) != MP_GT)) {
1117
return MP_VAL;

tommath_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@
440440

441441
#if defined(MP_INVMOD_C)
442442
# define MP_CMP_D_C
443+
# define MP_ZERO_C
443444
# define S_MP_INVMOD_C
444445
# define S_MP_INVMOD_ODD_C
445446
#endif

0 commit comments

Comments
 (0)