comparison libtommath/bn_mp_grow.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h" 1 #include "tommath_private.h"
2 #ifdef BN_MP_GROW_C 2 #ifdef BN_MP_GROW_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* grow as required */ 6 /* grow as required */
16 int mp_grow(mp_int *a, int size) 7 mp_err mp_grow(mp_int *a, int size)
17 { 8 {
18 int i; 9 int i;
19 mp_digit *tmp; 10 mp_digit *tmp;
20 11
21 /* if the alloc size is smaller alloc more ram */ 12 /* if the alloc size is smaller alloc more ram */
22 if (a->alloc < size) { 13 if (a->alloc < size) {
23 /* ensure there are always at least MP_PREC digits extra on top */
24 size += (MP_PREC * 2) - (size % MP_PREC);
25
26 /* reallocate the array a->dp 14 /* reallocate the array a->dp
27 * 15 *
28 * We store the return in a temporary variable 16 * We store the return in a temporary variable
29 * in case the operation failed we don't want 17 * in case the operation failed we don't want
30 * to overwrite the dp member of a. 18 * to overwrite the dp member of a.
31 */ 19 */
32 tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size); 20 tmp = (mp_digit *) MP_REALLOC(a->dp,
21 (size_t)a->alloc * sizeof(mp_digit),
22 (size_t)size * sizeof(mp_digit));
33 if (tmp == NULL) { 23 if (tmp == NULL) {
34 /* reallocation failed but "a" is still valid [can be freed] */ 24 /* reallocation failed but "a" is still valid [can be freed] */
35 return MP_MEM; 25 return MP_MEM;
36 } 26 }
37 27
39 a->dp = tmp; 29 a->dp = tmp;
40 30
41 /* zero excess digits */ 31 /* zero excess digits */
42 i = a->alloc; 32 i = a->alloc;
43 a->alloc = size; 33 a->alloc = size;
44 for (; i < a->alloc; i++) { 34 MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
45 a->dp[i] = 0;
46 }
47 } 35 }
48 return MP_OKAY; 36 return MP_OKAY;
49 } 37 }
50 #endif 38 #endif
51
52 /* ref: HEAD -> master, tag: v1.1.0 */
53 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
54 /* commit time: 2019-01-28 20:32:32 +0100 */