added libtompoly-0.04
[libtompoly.git] / pb_shrink.c
blob68a3a58e139069dcf3261bce9fe6caa4faacff88
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_shrink(pb_poly *a)
16 int err, x;
17 mp_int *tmp;
19 /* first resize the array of terms */
20 if (a->used != a->alloc && a->used > 0) {
21 /* free the mp_ints */
22 for (x = a->used; x < a->alloc; x++) {
23 mp_clear(&(a->terms[x]));
26 /* resize the array of pointers */
27 tmp = realloc(a->terms, sizeof(mp_int) * a->used);
28 if (tmp == NULL) {
29 return MP_MEM;
31 a->terms = tmp;
32 a->alloc = a->used;
35 /* now shrink each mp_int */
36 for (x = 0; x < a->used; x++) {
37 if ((err = mp_shrink(&(a->terms[x]))) != MP_OKAY) {
38 return err;
41 return MP_OKAY;