From d185c1d7bd951f350a942becbad8819692d2bdbf Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 19 May 2019 14:56:04 +0200 Subject: [PATCH] refactor without inner scope --- bn_mp_lshd.c | 42 ++++++++++++++++++++---------------------- bn_mp_rshd.c | 45 +++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c index b8da2b6..ba36576 100644 --- a/bn_mp_lshd.c +++ b/bn_mp_lshd.c @@ -8,6 +8,7 @@ mp_err mp_lshd(mp_int *a, int b) { int x; mp_err res; + mp_digit *top, *bottom; /* if its less than zero return */ if (b <= 0) { @@ -25,32 +26,29 @@ mp_err mp_lshd(mp_int *a, int b) } } - { - mp_digit *top, *bottom; + /* increment the used by the shift amount then copy upwards */ + a->used += b; - /* increment the used by the shift amount then copy upwards */ - a->used += b; + /* top */ + top = a->dp + a->used - 1; - /* top */ - top = a->dp + a->used - 1; + /* base */ + bottom = (a->dp + a->used - 1) - b; - /* base */ - bottom = (a->dp + a->used - 1) - b; - - /* much like mp_rshd this is implemented using a sliding window - * except the window goes the otherway around. Copying from - * the bottom to the top. see bn_mp_rshd.c for more info. - */ - for (x = a->used - 1; x >= b; x--) { - *top-- = *bottom--; - } - - /* zero the lower digits */ - top = a->dp; - for (x = 0; x < b; x++) { - *top++ = 0; - } + /* much like mp_rshd this is implemented using a sliding window + * except the window goes the otherway around. Copying from + * the bottom to the top. see bn_mp_rshd.c for more info. + */ + for (x = a->used - 1; x >= b; x--) { + *top-- = *bottom--; } + + /* zero the lower digits */ + top = a->dp; + for (x = 0; x < b; x++) { + *top++ = 0; + } + return MP_OKAY; } #endif diff --git a/bn_mp_rshd.c b/bn_mp_rshd.c index 2bbf597..1ab9ba4 100644 --- a/bn_mp_rshd.c +++ b/bn_mp_rshd.c @@ -7,6 +7,7 @@ void mp_rshd(mp_int *a, int b) { int x; + mp_digit *bottom, *top; /* if b <= 0 then ignore it */ if (b <= 0) { @@ -19,35 +20,31 @@ void mp_rshd(mp_int *a, int b) return; } - { - mp_digit *bottom, *top; + /* shift the digits down */ - /* shift the digits down */ + /* bottom */ + bottom = a->dp; - /* bottom */ - bottom = a->dp; + /* top [offset into digits] */ + top = a->dp + b; - /* top [offset into digits] */ - top = a->dp + b; + /* this is implemented as a sliding window where + * the window is b-digits long and digits from + * the top of the window are copied to the bottom + * + * e.g. - /* this is implemented as a sliding window where - * the window is b-digits long and digits from - * the top of the window are copied to the bottom - * - * e.g. + b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> + /\ | ----> + \-------------------/ ----> + */ + for (x = 0; x < (a->used - b); x++) { + *bottom++ = *top++; + } - b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> - /\ | ----> - \-------------------/ ----> - */ - for (x = 0; x < (a->used - b); x++) { - *bottom++ = *top++; - } - - /* zero the top digits */ - for (; x < a->used; x++) { - *bottom++ = 0; - } + /* zero the top digits */ + for (; x < a->used; x++) { + *bottom++ = 0; } /* remove excess digits */