aboutsummaryrefslogtreecommitdiff
path: root/src/libtommath/mp_shrink.c
blob: 3d9b1626d2557ed55abf5fe6508d2bc9d35d73aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "tommath_private.h"
#ifdef MP_SHRINK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* shrink a bignum */
mp_err mp_shrink(mp_int *a)
{
   int alloc = MP_MAX(MP_MIN_DIGIT_COUNT, a->used);
   if (a->alloc != alloc) {
      mp_digit *dp = (mp_digit *) MP_REALLOC(a->dp,
                                             (size_t)a->alloc * sizeof(mp_digit),
                                             (size_t)alloc * sizeof(mp_digit));
      if (dp == NULL) {
         return MP_MEM;
      }
      a->dp    = dp;
      a->alloc = alloc;
   }
   return MP_OKAY;
}
#endif