comparison libtommath/bn_mp_init_size.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_INIT_SIZE_C 2 #ifdef BN_MP_INIT_SIZE_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 /* init an mp_init for a given size */ 6 /* init an mp_init for a given size */
16 int mp_init_size(mp_int *a, int size) 7 mp_err mp_init_size(mp_int *a, int size)
17 { 8 {
18 int x; 9 size = MP_MAX(MP_MIN_PREC, size);
19
20 /* pad size so there are always extra digits */
21 size += (MP_PREC * 2) - (size % MP_PREC);
22 10
23 /* alloc mem */ 11 /* alloc mem */
24 a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)size); 12 a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
25 if (a->dp == NULL) { 13 if (a->dp == NULL) {
26 return MP_MEM; 14 return MP_MEM;
27 } 15 }
28 16
29 /* set the members */ 17 /* set the members */
30 a->used = 0; 18 a->used = 0;
31 a->alloc = size; 19 a->alloc = size;
32 a->sign = MP_ZPOS; 20 a->sign = MP_ZPOS;
33 21
34 /* zero the digits */
35 for (x = 0; x < size; x++) {
36 a->dp[x] = 0;
37 }
38
39 return MP_OKAY; 22 return MP_OKAY;
40 } 23 }
41 #endif 24 #endif
42
43 /* ref: HEAD -> master, tag: v1.1.0 */
44 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
45 /* commit time: 2019-01-28 20:32:32 +0100 */