Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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 */
Expand Down
6 changes: 6 additions & 0 deletions mp_exptmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions mp_invmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tommath_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading