Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_init.c
blobccd06e8198b00913f7e69917b444e44bfe46b225
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(pb_poly *a, mp_int *characteristic)
18 int x, y, err;
20 /* init characteristic */
21 if ((err = mp_init_copy(&(a->characteristic), characteristic)) != MP_OKAY) {
22 return err;
25 /* now allocate an array of mp_ints */
26 if ((a->terms = calloc(PB_TERMS, sizeof(mp_int))) == NULL) {
27 mp_clear(&(a->characteristic));
28 return MP_MEM;
31 /* now initialize them all */
32 for (x = 0; x < PB_TERMS; x++) {
33 if ((err = mp_init(&(a->terms[x]))) != MP_OKAY) {
34 /* free stuff */
35 for (y = 0; y < x; y++) {
36 mp_clear(&(a->terms[y]));
38 free(a->terms);
39 mp_clear(&(a->characteristic));
40 return err;
44 /* set our parameters */
45 a->used = 0;
46 a->alloc = PB_TERMS;
47 return MP_OKAY;