Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_invmod.c
blob5607decc0e39d275d7c9adabcd9a38e2dc7af5c3
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_invmod(pb_poly *a, pb_poly *b, pb_poly *c)
16 int err;
17 pb_poly tmp;
19 if ((err = pb_init(&tmp, &(c->characteristic))) != MP_OKAY) {
20 return err;
23 if ((err = pb_exteuclid(a, b, c, NULL, &tmp)) != MP_OKAY) { goto _ERR; }
25 /* if deg(tmp(x)) > 0 then there is no invmod */
26 if (tmp.used > 1) { err = MP_VAL; goto _ERR; }
28 err = MP_OKAY;
29 _ERR: pb_clear(&tmp);
30 return err;