1
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* grow as required */ |
|
18 int mp_grow (mp_int * a, int size) |
|
19 { |
|
20 int i; |
|
21 mp_digit *tmp; |
|
22 |
|
23 /* if the alloc size is smaller alloc more ram */ |
|
24 if (a->alloc < size) { |
|
25 /* ensure there are always at least MP_PREC digits extra on top */ |
|
26 size += (MP_PREC * 2) - (size % MP_PREC); |
|
27 |
|
28 /* reallocate the array a->dp |
|
29 * |
|
30 * We store the return in a temporary variable |
|
31 * in case the operation failed we don't want |
|
32 * to overwrite the dp member of a. |
|
33 */ |
|
34 tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size); |
|
35 if (tmp == NULL) { |
|
36 /* reallocation failed but "a" is still valid [can be freed] */ |
|
37 return MP_MEM; |
|
38 } |
|
39 |
|
40 /* reallocation succeeded so set a->dp */ |
|
41 a->dp = tmp; |
|
42 |
|
43 /* zero excess digits */ |
|
44 i = a->alloc; |
|
45 a->alloc = size; |
|
46 for (; i < a->alloc; i++) { |
|
47 a->dp[i] = 0; |
|
48 } |
|
49 } |
|
50 return MP_OKAY; |
|
51 } |