Skip to content

Commit 908e098

Browse files
committed
Fix: removed sign operation in s_mp_invmod_odd
Changed a check for <0 with mp_isneg() in s_mp_invmod Additional tests for mp_invmod() in demo/test.c
1 parent 96f9edf commit 908e098

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

demo/test.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,54 @@ static int test_mp_and(void)
549549
static int test_mp_invmod(void)
550550
{
551551
mp_int a, b, c, d;
552+
int i, j, k;
553+
int e;
554+
555+
int results[21][21] =
556+
/* Table generated with Pari/GP
557+
558+
for(i=-10,10,
559+
k=0;
560+
d=0;
561+
printf(" {");
562+
for(j=-10,10,
563+
iferr(
564+
printf(lift(Mod(1/i, j)) ", "),
565+
k,
566+
printf("-1, "))
567+
);
568+
print("},")
569+
)
570+
571+
Changes to the output: replaced j < 1 with -1 for now and added the result of 0^(-1) mod (1)
572+
573+
j = -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 */
574+
575+
{
576+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1, 2, -1, 8, -1 }, /* i = -10 */
577+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 3, 1, -1, 3, 7, -1, 1 }, /* -9 */
578+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 3, -1, 6, -1, 1, -1 }, /* -8 */
579+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 1, 2, 5, -1, 1, 5, 7 }, /* -7 */
580+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, -1, 1, -1, -1, -1 }, /* -6 */
581+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 3, -1, 1, 4, 3, 7, -1 }, /* -5 */
582+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, 1, -1, 5, -1, 2, -1 }, /* -4 */
583+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 3, -1, 2, 5, -1, 3 }, /* -3 */
584+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 2, -1, 3, -1, 4, -1 }, /* -2 */
585+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, /* -1 */
586+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* 0 */
587+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* 1 */
588+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 2, -1, 3, -1, 4, -1, 5, -1 }, /* 2 */
589+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 3, 2, -1, 5, 3, -1, 7 }, /* 3 */
590+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 4, -1, 2, -1, 7, -1 }, /* 4 */
591+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, -1, 5, 3, 5, 2, -1 }, /* 5 */
592+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 1, -1, 6, -1, -1, -1 }, /* 6 */
593+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 3, 3, 1, -1, 7, 4, 3 }, /* 7 */
594+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 2, -1, 2, -1, 1, -1, 8, -1 }, /* 8 */
595+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 4, -1, 4, 1, -1, 9 }, /* 9 */
596+
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, -1, -1, 5, -1, 1, -1 } /* 10 */
597+
};
598+
599+
552600
DOR(mp_init_multi(&a, &b, &c, &d, NULL));
553601

554602
/* mp_invmod corner-case of https://github.com/libtom/libtommath/issues/118 */
@@ -564,6 +612,30 @@ static int test_mp_invmod(void)
564612
EXPECT(mp_cmp(&c, &d) == MP_EQ);
565613
}
566614

615+
/* Some small general tests https://github.com/libtom/libtommath/issues/534 */
616+
for (i = -10; i < 11; i++) {
617+
for (j = -10; j < 11; j++) {
618+
mp_set_i32(&a, i);
619+
mp_set_i32(&b, j);
620+
e = mp_invmod(&a, &b, &c);
621+
if (e != MP_OKAY) {
622+
if (results[i+10][j+10] != -1) {
623+
printf("error = %s from ", mp_error_to_string(e));
624+
printf("error at i = %d, j =%d should be an error but gave ",i,j);
625+
e = mp_fwrite(&c,10,stdout);
626+
printf("\n");
627+
goto LBL_ERR;
628+
}
629+
} else {
630+
k = mp_get_i32(&c);
631+
if (k != results[i+10][j+10]) {
632+
printf("result at i = %d, j =%d is %d but should be %d \n", i,j,k,results[i+10][j+10]);
633+
goto LBL_ERR;
634+
}
635+
}
636+
}
637+
}
638+
567639
mp_clear_multi(&a, &b, &c, &d, NULL);
568640
return EXIT_SUCCESS;
569641
LBL_ERR:

s_mp_invmod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
9898
}
9999

100100
/* if its too low */
101-
while (mp_cmp_d(&C, 0uL) == MP_LT) {
101+
while (mp_isneg(&C)) {
102102
if ((err = mp_add(&C, b, &C)) != MP_OKAY) goto LBL_ERR;
103103
}
104104

s_mp_invmod_odd.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
1313
{
1414
mp_int x, y, u, v, B, D;
15-
mp_sign sign;
1615
mp_err err;
1716

1817
/* 2. [modified] b must be odd */
@@ -28,7 +27,7 @@ mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
2827
/* x == modulus, y == value to invert */
2928
if ((err = mp_copy(b, &x)) != MP_OKAY) goto LBL_ERR;
3029

31-
/* we need y = |a| */
30+
/* y needs to be positive but the remainder d of mp_div(a,b,c,d) might be negative */
3231
if ((err = mp_mod(a, b, &y)) != MP_OKAY) goto LBL_ERR;
3332

3433
/* if one of x,y is zero return an error! */
@@ -95,7 +94,6 @@ mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
9594
}
9695

9796
/* b is now the inverse */
98-
sign = a->sign;
9997
while (mp_isneg(&D)) {
10098
if ((err = mp_add(&D, b, &D)) != MP_OKAY) goto LBL_ERR;
10199
}
@@ -106,7 +104,6 @@ mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c)
106104
}
107105

108106
mp_exch(&D, c);
109-
c->sign = sign;
110107
err = MP_OKAY;
111108

112109
LBL_ERR:

0 commit comments

Comments
 (0)