From 29ef033a0b5fd613a63a2d732cd35785196ded56 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 25 May 2019 20:54:49 +0200 Subject: [PATCH] refactor without inner scope --- bn_mp_copy.c | 27 ++++++++++++--------------- bn_mp_div_2.c | 37 ++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/bn_mp_copy.c b/bn_mp_copy.c index 141dd0e..e72fcf6 100644 --- a/bn_mp_copy.c +++ b/bn_mp_copy.c @@ -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; diff --git a/bn_mp_div_2.c b/bn_mp_div_2.c index 2561e5a..f56ea81 100644 --- a/bn_mp_div_2.c +++ b/bn_mp_div_2.c @@ -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;