Skip to content

Commit b1f9bff

Browse files
committed
simplifications: invmod
1 parent 56144ee commit b1f9bff

File tree

2 files changed

+80
-84
lines changed

2 files changed

+80
-84
lines changed

s_mp_invmod_fast.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,49 @@ mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c)
4242
if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR;
4343
mp_set(&D, 1uL);
4444

45-
top:
46-
/* 4. while u is even do */
47-
while (mp_iseven(&u)) {
48-
/* 4.1 u = u/2 */
49-
if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
50-
51-
/* 4.2 if B is odd then */
52-
if (mp_isodd(&B)) {
53-
if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
45+
do {
46+
/* 4. while u is even do */
47+
while (mp_iseven(&u)) {
48+
/* 4.1 u = u/2 */
49+
if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
50+
51+
/* 4.2 if B is odd then */
52+
if (mp_isodd(&B)) {
53+
if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
54+
}
55+
/* B = B/2 */
56+
if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
5457
}
55-
/* B = B/2 */
56-
if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
57-
}
58-
59-
/* 5. while v is even do */
60-
while (mp_iseven(&v)) {
61-
/* 5.1 v = v/2 */
62-
if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
6358

64-
/* 5.2 if D is odd then */
65-
if (mp_isodd(&D)) {
66-
/* D = (D-x)/2 */
67-
if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
59+
/* 5. while v is even do */
60+
while (mp_iseven(&v)) {
61+
/* 5.1 v = v/2 */
62+
if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
63+
64+
/* 5.2 if D is odd then */
65+
if (mp_isodd(&D)) {
66+
/* D = (D-x)/2 */
67+
if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
68+
}
69+
/* D = D/2 */
70+
if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
6871
}
69-
/* D = D/2 */
70-
if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
71-
}
7272

73-
/* 6. if u >= v then */
74-
if (mp_cmp(&u, &v) != MP_LT) {
75-
/* u = u - v, B = B - D */
76-
if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
73+
/* 6. if u >= v then */
74+
if (mp_cmp(&u, &v) != MP_LT) {
75+
/* u = u - v, B = B - D */
76+
if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
7777

78-
if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
79-
} else {
80-
/* v - v - u, D = D - B */
81-
if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
78+
if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
79+
} else {
80+
/* v - v - u, D = D - B */
81+
if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
8282

83-
if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
84-
}
83+
if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
84+
}
8585

86-
/* if not zero goto step 4 */
87-
if (!mp_iszero(&u)) {
88-
goto top;
89-
}
86+
/* if not zero goto step 4 */
87+
} while (!mp_iszero(&u));
9088

9189
/* now a = C, b = D, gcd == g*v */
9290

s_mp_invmod_slow.c

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,60 +36,58 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
3636
mp_set(&A, 1uL);
3737
mp_set(&D, 1uL);
3838

39-
top:
40-
/* 4. while u is even do */
41-
while (mp_iseven(&u)) {
42-
/* 4.1 u = u/2 */
43-
if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
44-
45-
/* 4.2 if A or B is odd then */
46-
if (mp_isodd(&A) || mp_isodd(&B)) {
47-
/* A = (A+y)/2, B = (B-x)/2 */
48-
if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR;
49-
if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
39+
do {
40+
/* 4. while u is even do */
41+
while (mp_iseven(&u)) {
42+
/* 4.1 u = u/2 */
43+
if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR;
44+
45+
/* 4.2 if A or B is odd then */
46+
if (mp_isodd(&A) || mp_isodd(&B)) {
47+
/* A = (A+y)/2, B = (B-x)/2 */
48+
if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR;
49+
if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR;
50+
}
51+
/* A = A/2, B = B/2 */
52+
if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR;
53+
if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
5054
}
51-
/* A = A/2, B = B/2 */
52-
if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR;
53-
if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR;
54-
}
55-
56-
/* 5. while v is even do */
57-
while (mp_iseven(&v)) {
58-
/* 5.1 v = v/2 */
59-
if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
6055

61-
/* 5.2 if C or D is odd then */
62-
if (mp_isodd(&C) || mp_isodd(&D)) {
63-
/* C = (C+y)/2, D = (D-x)/2 */
64-
if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR;
65-
if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
56+
/* 5. while v is even do */
57+
while (mp_iseven(&v)) {
58+
/* 5.1 v = v/2 */
59+
if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR;
60+
61+
/* 5.2 if C or D is odd then */
62+
if (mp_isodd(&C) || mp_isodd(&D)) {
63+
/* C = (C+y)/2, D = (D-x)/2 */
64+
if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR;
65+
if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR;
66+
}
67+
/* C = C/2, D = D/2 */
68+
if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR;
69+
if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
6670
}
67-
/* C = C/2, D = D/2 */
68-
if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR;
69-
if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR;
70-
}
7171

72-
/* 6. if u >= v then */
73-
if (mp_cmp(&u, &v) != MP_LT) {
74-
/* u = u - v, A = A - C, B = B - D */
75-
if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
72+
/* 6. if u >= v then */
73+
if (mp_cmp(&u, &v) != MP_LT) {
74+
/* u = u - v, A = A - C, B = B - D */
75+
if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR;
7676

77-
if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR;
77+
if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR;
7878

79-
if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
80-
} else {
81-
/* v - v - u, C = C - A, D = D - B */
82-
if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
79+
if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR;
80+
} else {
81+
/* v - v - u, C = C - A, D = D - B */
82+
if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR;
8383

84-
if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR;
84+
if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR;
8585

86-
if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
87-
}
86+
if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR;
87+
}
8888

89-
/* if not zero goto step 4 */
90-
if (!mp_iszero(&u)) {
91-
goto top;
92-
}
89+
/* if not zero goto step 4 */
90+
} while (!mp_iszero(&u));
9391

9492
/* now a = C, b = D, gcd == g*v */
9593

@@ -111,7 +109,7 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
111109

112110
/* C is now the inverse */
113111
mp_exch(&C, c);
114-
err = MP_OKAY;
112+
115113
LBL_ERR:
116114
mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
117115
return err;

0 commit comments

Comments
 (0)