after multiple objections of libtom users [1], we decided to change licensing
[libtompoly.git] / pb_copy.c
blobd9ae3662e29f9c70e7b863b6ccf4d49dff3c5d42
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 /* dest = src */
15 int pb_copy(pb_poly *src, pb_poly *dest)
17 int err, x;
19 /* avoid trivial copies */
20 if (src == dest) {
21 return MP_OKAY;
24 /* grow dest as required */
25 if (dest->alloc < src->used) {
26 if ((err = pb_grow(dest, src->used)) != MP_OKAY) {
27 return err;
31 /* set the characteristic */
32 if ((err = mp_copy(&(src->characteristic), &(dest->characteristic))) != MP_OKAY) {
33 return err;
36 /* copy digits */
37 for (x = 0; x < src->used; x++) {
38 if ((err = mp_copy(&(src->terms[x]), &(dest->terms[x]))) != MP_OKAY) {
39 return err;
43 /* zero excess digits */
44 for (x = src->used; x < dest->used; x++) {
45 mp_zero(&(dest->terms[x]));
47 dest->used = src->used;
48 return MP_OKAY;