Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_shrink.c
blob345232401a489d7d2489307ff984c08c9f7078f1
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>
15 int pb_shrink(pb_poly *a)
17 int err, x;
18 mp_int *tmp;
20 /* first resize the array of terms */
21 if (a->used != a->alloc && a->used > 0) {
22 /* free the mp_ints */
23 for (x = a->used; x < a->alloc; x++) {
24 mp_clear(&(a->terms[x]));
27 /* resize the array of pointers */
28 tmp = realloc(a->terms, sizeof(mp_int) * a->used);
29 if (tmp == NULL) {
30 return MP_MEM;
32 a->terms = tmp;
33 a->alloc = a->used;
36 /* now shrink each mp_int */
37 for (x = 0; x < a->used; x++) {
38 if ((err = mp_shrink(&(a->terms[x]))) != MP_OKAY) {
39 return err;
42 return MP_OKAY;