1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-11-30 19:28:10 +00:00
simplesshd/dropbear/libtommath/bn_mp_div_2.c

50 lines
1.0 KiB
C
Raw Normal View History

2020-12-28 21:40:37 +00:00
#include "tommath_private.h"
2014-12-10 21:56:49 +00:00
#ifdef BN_MP_DIV_2_C
2020-12-28 21:40:37 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2014-12-10 21:56:49 +00:00
/* b = a/2 */
2020-12-28 21:40:37 +00:00
mp_err mp_div_2(const mp_int *a, mp_int *b)
2014-12-10 21:56:49 +00:00
{
2020-12-28 21:40:37 +00:00
int x, oldused;
mp_digit r, rr, *tmpa, *tmpb;
mp_err err;
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
/* copy */
if (b->alloc < a->used) {
if ((err = mp_grow(b, a->used)) != MP_OKAY) {
return err;
}
}
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
oldused = b->used;
b->used = a->used;
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
/* source alias */
tmpa = a->dp + b->used - 1;
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
/* dest alias */
tmpb = b->dp + b->used - 1;
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
2014-12-10 21:56:49 +00:00
/* get the carry for the next iteration */
2020-12-28 21:40:37 +00:00
rr = *tmpa & 1u;
2014-12-10 21:56:49 +00:00
/* shift the current digit, add in carry and store */
2020-12-28 21:40:37 +00:00
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
2014-12-10 21:56:49 +00:00
/* forward carry to next iteration */
r = rr;
2020-12-28 21:40:37 +00:00
}
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
/* zero excess digits */
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
b->sign = a->sign;
mp_clamp(b);
return MP_OKAY;
2014-12-10 21:56:49 +00:00
}
#endif