Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_grow.c
blobddf4c92a45a579d556509a2d4b9b15a873ec84c2
1 /* LibTomPoly, Polynomial Basis Math -- Tom St Denis
2 *
3 * LibTomPoly is a public domain library that provides
4 * polynomial basis arithmetic support. It relies on
5 * LibTomMath for large integer support.
7 * This library is free for all purposes without any
8 * express guarantee that it works.
10 * Tom St Denis, tomstdenis@iahu.ca, http://poly.libtomcrypt.org
12 #include <tompoly.h>
13 #include <stdlib.h>
14 #include <string.h>
16 int pb_grow(pb_poly *a, int size)
18 mp_int *tmp;
19 int x, err;
21 if (a->alloc < size) {
22 /* pad size upwards */
23 size += (PB_TERMS - (size % PB_TERMS));
25 /* regrow terms */
26 tmp = realloc(a->terms, sizeof(mp_int) * size);
27 if (tmp == NULL) {
28 return MP_MEM;
30 a->terms = tmp;
32 /* zero the new stuff (prevents mp_clear from double freeing) */
33 memset(a->terms + a->alloc, 0, (size - a->alloc) * sizeof(mp_int));
35 /* now init the terms */
36 for (x = a->alloc; x < size; x++) {
37 if ((err = mp_init(&(a->terms[x]))) != MP_OKAY) {
38 return err;
42 /* update info */
43 a->alloc = size;
45 return MP_OKAY;