Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_init_size.c
blobad1692c4b181d8b148663a9ebc64a7dd51ab7023
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 /* initialize a */
16 int pb_init_size(pb_poly *a, mp_int *characteristic, int size)
18 int x, y, err;
20 /* enforce a minimum size */
21 if (size < 1) {
22 size = 1;
25 /* pad size upwards */
26 size += (PB_TERMS - (size % PB_TERMS));
29 /* init characteristic */
30 if ((err = mp_init_copy(&(a->characteristic), characteristic)) != MP_OKAY) {
31 return err;
34 /* now allocate an array of mp_ints */
35 if ((a->terms = calloc(size, sizeof(mp_int))) == NULL) {
36 mp_clear(&(a->characteristic));
37 return MP_MEM;
40 /* now initialize them all */
41 for (x = 0; x < size; x++) {
42 if ((err = mp_init(&(a->terms[x]))) != MP_OKAY) {
43 /* free stuff */
44 for (y = 0; y < x; y++) {
45 mp_clear(&(a->terms[y]));
47 free(a->terms);
48 mp_clear(&(a->characteristic));
49 return err;
53 /* set our parameters */
54 a->used = 0;
55 a->alloc = size;
56 return MP_OKAY;