refactor without inner scope

This commit is contained in:
Francois Perrad 2019-05-25 20:54:49 +02:00
parent 857b112ef2
commit 29ef033a0b
2 changed files with 30 additions and 34 deletions

View File

@ -7,6 +7,7 @@
mp_err mp_copy(const mp_int *a, mp_int *b)
{
int n;
mp_digit *tmpa, *tmpb;
mp_err err;
/* if dst == src do nothing */
@ -22,26 +23,22 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
}
/* zero b and copy the parameters over */
{
mp_digit *tmpa, *tmpb;
/* pointer aliases */
/* pointer aliases */
/* source */
tmpa = a->dp;
/* source */
tmpa = a->dp;
/* destination */
tmpb = b->dp;
/* destination */
tmpb = b->dp;
/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
}
/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);
/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
}
/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);
/* copy used count and sign */
b->used = a->used;
b->sign = a->sign;

View File

@ -7,6 +7,7 @@
mp_err mp_div_2(const mp_int *a, mp_int *b)
{
int x, oldused;
mp_digit r, rr, *tmpa, *tmpb;
mp_err err;
/* copy */
@ -18,31 +19,29 @@ mp_err mp_div_2(const mp_int *a, mp_int *b)
oldused = b->used;
b->used = a->used;
{
mp_digit r, rr, *tmpa, *tmpb;
/* source alias */
tmpa = a->dp + b->used - 1;
/* source alias */
tmpa = a->dp + b->used - 1;
/* dest alias */
tmpb = b->dp + b->used - 1;
/* dest alias */
tmpb = b->dp + b->used - 1;
/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1u;
/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1u;
/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
/* forward carry to next iteration */
r = rr;
}
/* zero excess digits */
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
/* forward carry to next iteration */
r = rr;
}
/* zero excess digits */
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
b->sign = a->sign;
mp_clamp(b);
return MP_OKAY;