added libtompoly-0.03
[libtompoly.git] / pb_mul.c
blob48785c20dc265ec3b119190a71ed513b2d469785
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_mul(pb_poly *a, pb_poly *b, pb_poly *c)
16 int err, x, y;
17 pb_poly tmp;
18 mp_int tmp2;
20 /* make temp */
21 if ((err = pb_init_size(&tmp, &(c->characteristic), a->used + b->used)) != MP_OKAY) {
22 return err;
25 if ((err = mp_init(&tmp2)) != MP_OKAY) {
26 goto __TMP;
29 /* multiply the terms */
30 for (x = 0; x < a->used; x++) {
31 for (y = 0; y < b->used; y++) {
32 if ((err = mp_mul(&(a->terms[x]), &(b->terms[y]), &tmp2)) != MP_OKAY) {
33 goto __TMP2;
35 if ((err = mp_add(&tmp2, &(tmp.terms[x+y]), &(tmp.terms[x+y]))) != MP_OKAY) {
36 goto __TMP2;
41 /* now reduce the terms as required */
42 if (mp_iszero(&(c->characteristic)) == MP_NO) {
43 for (x = 0; x < (a->used + b->used); x++) {
44 if ((err = mp_mod(&(tmp.terms[x]), &(c->characteristic), &(tmp.terms[x]))) != MP_OKAY) {
45 goto __TMP2;
50 /* set digit count */
51 tmp.used = a->used + b->used;
52 pb_clamp(&tmp);
54 /* exchange tmp and c */
55 pb_exch(&tmp, c);
57 err = MP_OKAY;
59 __TMP2: mp_clear(&tmp2);
60 __TMP : pb_clear(&tmp);
61 return err;