Skip to content

Commit

Permalink
Port powmod from fq_poly to gr_poly, remove duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Jan 30, 2025
1 parent e6562ca commit bfa1d07
Show file tree
Hide file tree
Showing 20 changed files with 897 additions and 1,528 deletions.
16 changes: 16 additions & 0 deletions doc/source/gr_poly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,22 @@ Modular arithmetic and composition
and ``poly2`` upon polynomial division by ``f``. ``finv``
is the inverse of the reverse of ``f``.

.. function:: int _gr_poly_powmod_fmpz_binexp(gr_ptr res, gr_srcptr poly, const fmpz_t e, gr_srcptr f, slong lenf, gr_ctx_t ctx)
int gr_poly_powmod_fmpz_binexp(gr_poly_t res, const gr_poly_t poly, const fmpz_t e, const gr_poly_t f, gr_ctx_t ctx)
int _gr_poly_powmod_fmpz_binexp_preinv(gr_ptr res, gr_srcptr poly, const fmpz_t e, gr_srcptr f, slong lenf, gr_srcptr finv, slong lenfinv, gr_ctx_t ctx)
int gr_poly_powmod_fmpz_binexp_preinv(gr_poly_t res, const gr_poly_t poly, const fmpz_t e, const gr_poly_t f, const gr_poly_t finv, gr_ctx_t ctx)
int _gr_poly_powmod_x_fmpz_preinv(gr_ptr res, const fmpz_t e, gr_srcptr f, slong lenf, gr_srcptr finv, slong lenfinv, gr_ctx_t ctx)
int gr_poly_powmod_x_fmpz_preinv(gr_poly_t res, const fmpz_t e, const gr_poly_t f, const gr_poly_t finv, gr_ctx_t ctx)
int _gr_poly_powmod_ui_binexp(gr_ptr res, gr_srcptr poly, ulong e, gr_srcptr f, slong lenf, gr_ctx_t ctx)
int gr_poly_powmod_ui_binexp(gr_poly_t res, const gr_poly_t poly, ulong e, const gr_poly_t f, gr_ctx_t ctx)
int _gr_poly_powmod_ui_binexp_preinv(gr_ptr res, gr_srcptr poly, ulong e, gr_srcptr f, slong lenf, gr_srcptr finv, slong lenfinv, gr_ctx_t ctx)
int gr_poly_powmod_ui_binexp_preinv(gr_poly_t res, const gr_poly_t poly, ulong e, const gr_poly_t f, const gr_poly_t finv, gr_ctx_t ctx)
int _gr_poly_powmod_fmpz_sliding_preinv(gr_ptr res, gr_srcptr poly, const fmpz_t e, ulong k, gr_srcptr f, slong lenf, gr_srcptr finv, slong lenfinv, gr_ctx_t ctx)
int gr_poly_powmod_fmpz_sliding_preinv(gr_poly_t res, const gr_poly_t poly, const fmpz_t e, ulong k, const gr_poly_t f, const gr_poly_t finv, gr_ctx_t ctx)

Sets ``res`` to ``poly`` raised to the power ``e``
modulo ``f``.

.. function:: int _gr_poly_compose_mod_horner(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, gr_srcptr poly3, slong len3, gr_ctx_t ctx)
int gr_poly_compose_mod_horner(gr_poly_t res, const gr_poly_t poly1, const gr_poly_t poly2, const gr_poly_t poly3, gr_ctx_t ctx)
int _gr_poly_compose_mod_brent_kung(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, gr_srcptr poly3, slong len3, gr_ctx_t ctx)
Expand Down
147 changes: 10 additions & 137 deletions src/fmpz_mod_poly/powmod_fmpz_binexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,155 +12,28 @@
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/

#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_mod.h"
#include "gr.h"
#include "gr_poly.h"
#include "fmpz_mod_poly.h"

void
_fmpz_mod_poly_powmod_fmpz_binexp(fmpz * res, const fmpz * poly,
const fmpz_t e, const fmpz * f,
slong lenf, const fmpz_mod_ctx_t ctx)
{
fmpz * T, * Q;
fmpz_t invf;
slong lenT, lenQ;
slong i;

if (lenf == 2)
{
fmpz_mod_pow_fmpz(res, poly, e, ctx);
return;
}

lenT = 2 * lenf - 3;
lenQ = lenT - lenf + 1;

T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;

fmpz_init(invf);
fmpz_mod_inv(invf, f + lenf - 1, ctx);

_fmpz_vec_set(res, poly, lenf - 1);

for (i = fmpz_sizeinbase(e, 2) - 2; i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, ctx);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, ctx);

if (fmpz_tstbit(e, i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, ctx);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, ctx);
}
}

fmpz_clear(invf);
_fmpz_vec_clear(T, lenT + lenQ);
gr_ctx_t gr_ctx;
_gr_ctx_init_fmpz_mod_from_ref(gr_ctx, ctx);
GR_MUST_SUCCEED(_gr_poly_powmod_fmpz_binexp(res, poly, e, f, lenf, gr_ctx));
}


void
fmpz_mod_poly_powmod_fmpz_binexp(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, const fmpz_t e,
const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;

if (lenf == 0)
{
flint_throw(FLINT_ERROR, "Exception (fmpz_mod_poly_powmod_fmpz_binexp). Divide by zero\n");
}

if (lenf == 1)
{
fmpz_mod_poly_zero(res, ctx);
return;
}

if (fmpz_sgn(e) < 0)
{
flint_throw(FLINT_ERROR, "Exception (fmpz_mod_poly_powmod_fmpz_binexp). negative exp not implemented\n");
}

if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, ctx);
fmpz_mod_poly_init(r, ctx);
fmpz_mod_poly_divrem(t, r, poly, f, ctx);
fmpz_mod_poly_powmod_fmpz_binexp(res, r, e, f, ctx);
fmpz_mod_poly_clear(t, ctx);
fmpz_mod_poly_clear(r, ctx);
return;
}

if (fmpz_abs_fits_ui(e))
{
ulong exp = fmpz_get_ui(e);

if (exp <= 2)
{
if (exp == UWORD(0))
{
fmpz_mod_poly_fit_length(res, 1, ctx);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (exp == UWORD(1))
{
fmpz_mod_poly_set(res, poly, ctx);
}
else
{
fmpz_mod_poly_mulmod(res, poly, poly, f, ctx);
}
return;
}
}

if (len == 0)
{
fmpz_mod_poly_zero(res, ctx);
return;
}

if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
}
else
{
q = poly->coeffs;
}

if ((res == poly && !qcopy) || (res == f))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, 2*lenf - 3, ctx);
_fmpz_mod_poly_powmod_fmpz_binexp(t->coeffs,
q, e, f->coeffs, lenf, ctx);
fmpz_mod_poly_swap(res, t, ctx);
fmpz_mod_poly_clear(t, ctx);
}
else
{
fmpz_mod_poly_fit_length(res, 2*lenf - 3, ctx);
_fmpz_mod_poly_powmod_fmpz_binexp(res->coeffs,
q, e, f->coeffs, lenf, ctx);
}

if (qcopy)
_fmpz_vec_clear(q, trunc);

_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
gr_ctx_t gr_ctx;
_gr_ctx_init_fmpz_mod_from_ref(gr_ctx, ctx);
GR_MUST_SUCCEED(gr_poly_powmod_fmpz_binexp((gr_poly_struct *) res,
(const gr_poly_struct *) poly, e,
(const gr_poly_struct *) f, gr_ctx));
}
142 changes: 11 additions & 131 deletions src/fmpz_mod_poly/powmod_fmpz_binexp_preinv.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/

#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_mod.h"
#include "gr.h"
#include "gr_poly.h"
#include "fmpz_mod_poly.h"

void
Expand All @@ -24,140 +23,21 @@ _fmpz_mod_poly_powmod_fmpz_binexp_preinv(fmpz * res, const fmpz * poly,
slong lenf, const fmpz* finv, slong lenfinv,
const fmpz_mod_ctx_t ctx)
{
fmpz * T, * Q;
slong lenT, lenQ;
slong i;

if (lenf == 2)
{
fmpz_mod_pow_fmpz(res, poly, e, ctx);
return;
}

lenT = 2 * lenf - 3;
lenQ = lenT - lenf + 1;

T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;

_fmpz_vec_set(res, poly, lenf - 1);

for (i = fmpz_sizeinbase(e, 2) - 2; i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, ctx);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f, lenf, finv, lenfinv, ctx);

if (fmpz_tstbit(e, i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, ctx);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f, lenf, finv, lenfinv, ctx);
}
}

_fmpz_vec_clear(T, lenT + lenQ);
gr_ctx_t gr_ctx;
_gr_ctx_init_fmpz_mod_from_ref(gr_ctx, ctx);
GR_MUST_SUCCEED(_gr_poly_powmod_fmpz_binexp_preinv(res, poly, e, f, lenf, finv, lenfinv, gr_ctx));
}


void
fmpz_mod_poly_powmod_fmpz_binexp_preinv(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, const fmpz_t e,
const fmpz_mod_poly_t f, const fmpz_mod_poly_t finv,
const fmpz_mod_ctx_t ctx)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;

if (lenf == 0)
{
flint_throw(FLINT_ERROR, "(fmpz_mod_poly_powmod_fmpz_binexp_preinv): Divide by zero.\n");
}


if (lenf == 1)
{
fmpz_mod_poly_zero(res, ctx);
return;
}

if (fmpz_sgn(e) < 0)
{
flint_throw(FLINT_ERROR, "(fmpz_mod_poly_powmod_fmpz_binexp_preinv): "
"Negative exp not implemented\n");
}

if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, ctx);
fmpz_mod_poly_init(r, ctx);
fmpz_mod_poly_divrem(t, r, poly, f, ctx);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, r, e, f, finv, ctx);
fmpz_mod_poly_clear(t, ctx);
fmpz_mod_poly_clear(r, ctx);
return;
}

if (fmpz_abs_fits_ui(e))
{
ulong exp = fmpz_get_ui(e);

if (exp <= 2)
{
if (exp == UWORD (0))
{
fmpz_mod_poly_fit_length(res, 1, ctx);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (exp == UWORD (1))
{
fmpz_mod_poly_set(res, poly, ctx);
}
else
{
fmpz_mod_poly_mulmod_preinv(res, poly, poly, f, finv, ctx);
}
return;
}
}

if (len == 0)
{
fmpz_mod_poly_zero(res, ctx);
return;
}

if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
} else
q = poly->coeffs;

if ((res == poly && !qcopy) || (res == f) || (res == finv))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, 2*lenf - 3, ctx);
_fmpz_mod_poly_powmod_fmpz_binexp_preinv(t->coeffs, q, e, f->coeffs,
lenf, finv->coeffs, finv->length, ctx);
fmpz_mod_poly_swap(res, t, ctx);
fmpz_mod_poly_clear(t, ctx);
}
else
{
fmpz_mod_poly_fit_length(res, 2*lenf - 3, ctx);
_fmpz_mod_poly_powmod_fmpz_binexp_preinv(res->coeffs, q, e, f->coeffs,
lenf, finv->coeffs, finv->length, ctx);
}

if (qcopy)
_fmpz_vec_clear(q, trunc);

_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
gr_ctx_t gr_ctx;
_gr_ctx_init_fmpz_mod_from_ref(gr_ctx, ctx);
GR_MUST_SUCCEED(gr_poly_powmod_fmpz_binexp_preinv((gr_poly_struct *) res,
(const gr_poly_struct *) poly, e,
(const gr_poly_struct *) f,
(const gr_poly_struct *) finv, gr_ctx));
}
Loading

0 comments on commit bfa1d07

Please sign in to comment.