1 /* LibTomPoly, Polynomial Basis Math -- Tom St Denis
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
14 /* returns the monic GCD only for GF(p^k)[x] */
15 int pb_gcd(pb_poly
*a
, pb_poly
*b
, pb_poly
*c
)
20 if (mp_iszero(&(c
->characteristic
)) == MP_YES
) {
24 /* special cases (one or both are zero) */
25 if (a
->used
== 0 && b
->used
== 0) {
26 /* both zero, set to 1 */
29 mp_set(&(c
->terms
[0]), 1);
31 } else if (a
->used
== 0) {
33 } else if (b
->used
== 0) {
37 if ((err
= pb_init(&tmp
, &(c
->characteristic
))) != MP_OKAY
) {
40 if ((err
= pb_init_copy(&A
, a
)) != MP_OKAY
) {
43 if ((err
= pb_init_copy(&B
, b
)) != MP_OKAY
) {
48 if ((err
= pb_mod(&A
, &B
, &tmp
)) != MP_OKAY
) {
52 if ((err
= pb_copy(&B
, &A
)) != MP_OKAY
) {
55 if ((err
= pb_copy(&tmp
, &B
)) != MP_OKAY
) {
60 /* ensure it's monic */
61 err
= pb_monic(&A
, c
);
65 __TMP
: pb_clear(&tmp
);