Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_toraw.c
blob8c0c63fa3dc9c8d168efc44f71962abae27d86f0
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_toraw(pb_poly *a, unsigned char *dst)
16 int err, x, y, z;
18 /* store the # of terms */
19 dst[0] = a->used & 255;
20 dst[1] = (a->used >> 8) & 255;
21 y = 2;
23 /* store the characteristic */
24 z = mp_signed_bin_size(&(a->characteristic));
25 dst[y++] = z&255;
26 dst[y++] = (z>>8)&255;
27 if ((err = mp_to_signed_bin(&(a->characteristic), dst+y)) != MP_OKAY) { return err; }
28 y += z;
30 for (x = 0; x < a->used; x++) {
31 z = mp_signed_bin_size(&(a->terms[x]));
32 dst[y++] = z&255;
33 dst[y++] = (z>>8)&255;
34 if ((err = mp_to_signed_bin(&(a->terms[x]), dst+y)) != MP_OKAY) { return err; }
35 y += z;
38 return MP_OKAY;