Avoid temporary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/bn_mp_div_2d.c b/bn_mp_div_2d.c
index 5b9fb48..6e53c37 100644
--- a/bn_mp_div_2d.c
+++ b/bn_mp_div_2d.c
@@ -20,8 +20,6 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
{
mp_digit D, r, rr;
int x, res;
- mp_int t;
-
/* if the shift count is <= 0 then we do no work */
if (b <= 0) {
@@ -32,24 +30,19 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
return res;
}
- if ((res = mp_init (&t)) != MP_OKAY) {
+ /* copy */
+ if ((res = mp_copy (a, c)) != MP_OKAY) {
return res;
}
+ /* 'a' should not be used after here - it might be the same as d */
/* get the remainder */
if (d != NULL) {
- if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
- mp_clear (&t);
+ if ((res = mp_mod_2d (a, b, d)) != MP_OKAY) {
return res;
}
}
- /* copy */
- if ((res = mp_copy (a, c)) != MP_OKAY) {
- mp_clear (&t);
- return res;
- }
-
/* shift by as many digits in the bit count */
if (b >= (int)DIGIT_BIT) {
mp_rshd (c, b / DIGIT_BIT);
@@ -84,10 +77,6 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
}
}
mp_clamp (c);
- if (d != NULL) {
- mp_exch (&t, d);
- }
- mp_clear (&t);
return MP_OKAY;
}
#endif