added libtompoly-0.03
[libtompoly.git] / pb_grow.c
blobc653297ed732a836509d687fa3f129e4c8db8396
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>
14 int pb_grow(pb_poly *a, int size)
16 mp_int *tmp;
17 int x, err;
19 if (a->alloc < size) {
20 /* pad size upwards */
21 size += (PB_TERMS - (size % PB_TERMS));
23 /* regrow terms */
24 tmp = realloc(a->terms, sizeof(mp_int) * size);
25 if (tmp == NULL) {
26 return MP_MEM;
28 a->terms = tmp;
30 /* zero the new stuff (prevents mp_clear from double freeing) */
31 memset(a->terms + a->alloc, 0, (size - a->alloc) * sizeof(mp_int));
33 /* now init the terms */
34 for (x = a->alloc; x < size; x++) {
35 if ((err = mp_init(&(a->terms[x]))) != MP_OKAY) {
36 return err;
40 /* update info */
41 a->alloc = size;
43 return MP_OKAY;