libtommath/bn_mp_rshd.c

52 lines
1.1 KiB
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_MP_RSHD_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-02-28 16:08:34 +00:00
/* shift right a certain amount of digits */
2017-08-30 17:07:12 +00:00
void mp_rshd(mp_int *a, int b)
2003-02-28 16:08:34 +00:00
{
2017-08-30 17:13:53 +00:00
int x;
2019-05-19 12:56:04 +00:00
mp_digit *bottom, *top;
2003-02-28 16:08:34 +00:00
2017-08-30 17:13:53 +00:00
/* if b <= 0 then ignore it */
if (b <= 0) {
return;
}
2003-02-28 16:08:34 +00:00
2017-08-30 17:13:53 +00:00
/* if b > used then simply zero it and return */
if (a->used <= b) {
mp_zero(a);
return;
}
2003-02-28 16:08:34 +00:00
2019-05-19 12:56:04 +00:00
/* shift the digits down */
2003-03-13 02:11:11 +00:00
2019-05-19 12:56:04 +00:00
/* bottom */
bottom = a->dp;
2003-03-13 02:11:11 +00:00
2019-05-19 12:56:04 +00:00
/* top [offset into digits] */
top = a->dp + b;
2003-03-22 15:10:20 +00:00
2019-05-19 12:56:04 +00:00
/* 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.
2003-03-22 15:10:20 +00:00
2019-05-19 12:56:04 +00:00
b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
/\ | ---->
\-------------------/ ---->
*/
for (x = 0; x < (a->used - b); x++) {
*bottom++ = *top++;
}
2003-02-28 16:08:34 +00:00
2019-05-19 12:56:04 +00:00
/* zero the top digits */
MP_ZERO_DIGITS(bottom, a->used - x);
2017-08-30 03:51:11 +00:00
2017-08-30 17:13:53 +00:00
/* remove excess digits */
a->used -= b;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif