after multiple objections of libtom users [1], we decided to change licensing
[libtompoly.git] / pb_init_size.c
blobbf44027a83de8d4f4ae1935e7d8f9f17119984e9
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 /* initialize a */
15 int pb_init_size(pb_poly *a, mp_int *characteristic, int size)
17 int x, y, err;
19 /* enforce a minimum size */
20 if (size < 1) {
21 size = 1;
24 /* pad size upwards */
25 size += (PB_TERMS - (size % PB_TERMS));
28 /* init characteristic */
29 if ((err = mp_init_copy(&(a->characteristic), characteristic)) != MP_OKAY) {
30 return err;
33 /* now allocate an array of mp_ints */
34 if ((a->terms = calloc(size, sizeof(mp_int))) == NULL) {
35 mp_clear(&(a->characteristic));
36 return MP_MEM;
39 /* now initialize them all */
40 for (x = 0; x < size; x++) {
41 if ((err = mp_init(&(a->terms[x]))) != MP_OKAY) {
42 /* free stuff */
43 for (y = 0; y < x; y++) {
44 mp_clear(&(a->terms[y]));
46 free(a->terms);
47 mp_clear(&(a->characteristic));
48 return err;
52 /* set our parameters */
53 a->used = 0;
54 a->alloc = size;
55 return MP_OKAY;