Skip to content

Commit

Permalink
replace a few fq_poly methods with gr wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Jan 26, 2025
1 parent 71107a8 commit 2d68d18
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 682 deletions.
83 changes: 14 additions & 69 deletions src/fq_poly_templates/compose_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@

#ifdef T

#include "gr.h"
#include "gr_mat.h"
#include "templates.h"

#include "ulong_extras.h"
void
_TEMPLATE(T, poly_compose_mod) (TEMPLATE(T, struct) * res,
const TEMPLATE(T, struct) * f, slong lenf,
const TEMPLATE(T, struct) * g,
const TEMPLATE(T, struct) * h, slong lenh,
const TEMPLATE(T, struct) * poly1, slong len1,
const TEMPLATE(T, struct) * poly2,
const TEMPLATE(T, struct) * poly3, slong len3,
const TEMPLATE(T, ctx_t) ctx)
{
if (lenh < TEMPLATE(CAP_T, COMPOSE_MOD_LENH_CUTOFF) || lenf >= lenh)
_TEMPLATE(T, poly_compose_mod_horner) (res, f, lenf, g, h, lenh, ctx);
else
_TEMPLATE(T, poly_compose_mod_brent_kung) (res, f, lenf, g, h, lenh,
ctx);
gr_ctx_t gr_ctx;
TEMPLATE3(_gr_ctx_init, T, from_ref)(gr_ctx, ctx);
GR_MUST_SUCCEED(_gr_poly_compose_mod(res, poly1, len1, poly2, poly3, len3, gr_ctx));
}

void
Expand All @@ -37,66 +36,12 @@ TEMPLATE(T, poly_compose_mod) (TEMPLATE(T, poly_t) res,
const TEMPLATE(T, poly_t) poly3,
const TEMPLATE(T, ctx_t) ctx)
{
TEMPLATE(T, t) inv3;
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
slong vec_len = FLINT_MAX(len3 - 1, len2);

TEMPLATE(T, struct) * ptr2;

if (len3 == 0)
{
flint_throw(FLINT_ERROR, "(%s): Division by zero\n", __func__);
}

if (len1 == 0 || len3 == 1)
{
TEMPLATE(T, poly_zero) (res, ctx);
return;
}

if (len1 == 1)
{
TEMPLATE(T, poly_set) (res, poly1, ctx);
return;
}

if (res == poly3 || res == poly1)
{
TEMPLATE(T, poly_t) tmp;
TEMPLATE(T, poly_init) (tmp, ctx);
TEMPLATE(T, poly_compose_mod) (tmp, poly1, poly2, poly3, ctx);
TEMPLATE(T, poly_swap) (tmp, res, ctx);
TEMPLATE(T, poly_clear) (tmp, ctx);
return;
}

ptr2 = _TEMPLATE(T, vec_init) (vec_len, ctx);

if (len2 <= len)
{
_TEMPLATE(T, vec_set) (ptr2, poly2->coeffs, len2, ctx);
_TEMPLATE(T, vec_zero) (ptr2 + len2, len - len2, ctx);
}
else
{
TEMPLATE(T, init) (inv3, ctx);
TEMPLATE(T, inv) (inv3, poly3->coeffs + len, ctx);
_TEMPLATE(T, poly_rem) (ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, ctx);
TEMPLATE(T, clear) (inv3, ctx);
}

TEMPLATE(T, poly_fit_length) (res, len, ctx);
_TEMPLATE(T, poly_compose_mod) (res->coeffs,
poly1->coeffs, len1, ptr2, poly3->coeffs,
len3, ctx);
_TEMPLATE(T, poly_set_length) (res, len, ctx);
_TEMPLATE(T, poly_normalise) (res, ctx);

_TEMPLATE(T, vec_clear) (ptr2, vec_len, ctx);
gr_ctx_t gr_ctx;
TEMPLATE3(_gr_ctx_init, T, from_ref)(gr_ctx, ctx);
GR_MUST_SUCCEED(gr_poly_compose_mod((gr_poly_struct *) res,
(const gr_poly_struct *) poly1,
(const gr_poly_struct *) poly2,
(const gr_poly_struct *) poly3, gr_ctx));
}


Expand Down
166 changes: 20 additions & 146 deletions src/fq_poly_templates/compose_mod_brent_kung.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,161 +13,35 @@

#ifdef T

#include "gr.h"
#include "gr_mat.h"
#include "templates.h"

void
_TEMPLATE(T, poly_compose_mod_brent_kung) (
TEMPLATE(T, struct) * res,
const TEMPLATE(T, struct) * poly1, slong len1,
const TEMPLATE(T, struct) * poly2,
const TEMPLATE(T, struct) * poly3, slong len3,
const TEMPLATE(T, ctx_t) ctx)
_TEMPLATE(T, poly_compose_mod_brent_kung) (TEMPLATE(T, struct) * res,
const TEMPLATE(T, struct) * poly1, slong len1,
const TEMPLATE(T, struct) * poly2,
const TEMPLATE(T, struct) * poly3, slong len3,
const TEMPLATE(T, ctx_t) ctx)
{
TEMPLATE(T, mat_t) A, B, C;
TEMPLATE(T, struct) * t, *h, *tmp;
slong i, n, m;

n = len3 - 1;

if (len3 == 1)
return;

if (len1 == 1)
{
TEMPLATE(T, set) (res, poly1, ctx);
return;
}

if (len3 == 2)
{
_TEMPLATE(T, TEMPLATE(poly_evaluate, T)) (res, poly1, len1, poly2,
ctx);
return;
}

m = n_sqrt(n) + 1;

TEMPLATE(T, mat_init) (A, m, n, ctx);
TEMPLATE(T, mat_init) (B, m, m, ctx);
TEMPLATE(T, mat_init) (C, m, n, ctx);

h = _TEMPLATE(T, vec_init) (2 * n - 1, ctx);
t = _TEMPLATE(T, vec_init) (2 * n - 1, ctx);

/* Set rows of B to the segments of poly1 */
for (i = 0; i < len1 / m; i++)
_TEMPLATE(T, vec_set) (TEMPLATE(T, mat_entry) (B, i, 0), poly1 + i * m, m, ctx);

_TEMPLATE(T, vec_set) (TEMPLATE(T, mat_entry) (B, i, 0), poly1 + i * m, len1 % m, ctx);

/* Set rows of A to powers of poly2 */
TEMPLATE(T, one) (TEMPLATE(T, mat_entry) (A, 0, 0), ctx);
_TEMPLATE(T, vec_set) (TEMPLATE(T, mat_entry) (A, 1, 0), poly2, n, ctx);
tmp = _TEMPLATE(T, vec_init) (2 * n - 1, ctx);
for (i = 2; i < m; i++)
{
_TEMPLATE(T, poly_mulmod) (tmp, TEMPLATE(T, mat_entry) (A, i - 1, 0), n, poly2, n, poly3,
len3, ctx);
_TEMPLATE(T, vec_set) (TEMPLATE(T, mat_entry) (A, i, 0), tmp, n, ctx);
}
_TEMPLATE(T, vec_clear) (tmp, 2 * n - 1, ctx);

TEMPLATE(T, mat_mul) (C, B, A, ctx);

/* Evaluate block composition using the Horner scheme */
_TEMPLATE(T, vec_set) (res, TEMPLATE(T, mat_entry) (C, m - 1, 0), n, ctx);
_TEMPLATE(T, poly_mulmod) (h, TEMPLATE(T, mat_entry) (A, m - 1, 0), n, poly2, n, poly3, len3,
ctx);

for (i = m - 2; i >= 0; i--)
{
_TEMPLATE(T, poly_mulmod) (t, res, n, h, n, poly3, len3, ctx);
_TEMPLATE(T, poly_add) (res, t, n, TEMPLATE(T, mat_entry) (C, i, 0), n, ctx);
}

_TEMPLATE(T, vec_clear) (h, 2 * n - 1, ctx);
_TEMPLATE(T, vec_clear) (t, 2 * n - 1, ctx);

TEMPLATE(T, mat_clear) (A, ctx);
TEMPLATE(T, mat_clear) (B, ctx);
TEMPLATE(T, mat_clear) (C, ctx);
gr_ctx_t gr_ctx;
TEMPLATE3(_gr_ctx_init, T, from_ref)(gr_ctx, ctx);
GR_MUST_SUCCEED(_gr_poly_compose_mod_brent_kung(res, poly1, len1, poly2, poly3, len3, gr_ctx));
}

void
TEMPLATE(T, poly_compose_mod_brent_kung) (TEMPLATE(T, poly_t) res,
const TEMPLATE(T, poly_t) poly1,
const TEMPLATE(T, poly_t) poly2,
const TEMPLATE(T, poly_t) poly3,
const TEMPLATE(T, ctx_t) ctx)
const TEMPLATE(T, poly_t) poly1,
const TEMPLATE(T, poly_t) poly2,
const TEMPLATE(T, poly_t) poly3,
const TEMPLATE(T, ctx_t) ctx)
{
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
slong vec_len = FLINT_MAX(len3 - 1, len2);

TEMPLATE(T, struct) * ptr2;
TEMPLATE(T, t) inv3;

if (len3 == 0)
{
flint_throw(FLINT_ERROR, "(%s): Division by zero\n", __func__);
}

if (len1 >= len3)
{
flint_throw(FLINT_ERROR, "(%s): The degree of the first polynomial must "
"be smaller than that of the modulus\n", __func__);
}

if (len1 == 0 || len3 == 1)
{
TEMPLATE(T, poly_zero) (res, ctx);
return;
}

if (len1 == 1)
{
TEMPLATE(T, poly_set) (res, poly1, ctx);
return;
}

if (res == poly3 || res == poly1)
{
TEMPLATE(T, poly_t) tmp;
TEMPLATE(T, poly_init) (tmp, ctx);
TEMPLATE(T, poly_compose_mod_brent_kung) (tmp, poly1, poly2, poly3,
ctx);
TEMPLATE(T, poly_swap) (tmp, res, ctx);
TEMPLATE(T, poly_clear) (tmp, ctx);
return;
}

ptr2 = _TEMPLATE(T, vec_init) (vec_len, ctx);

if (len2 <= len)
{
_TEMPLATE(T, vec_set) (ptr2, poly2->coeffs, len2, ctx);
_TEMPLATE(T, vec_zero) (ptr2 + len2, vec_len - len2, ctx);
}
else
{
TEMPLATE(T, init) (inv3, ctx);
TEMPLATE(T, inv) (inv3, poly3->coeffs + len, ctx);
_TEMPLATE(T, poly_rem) (ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, ctx);
TEMPLATE(T, clear) (inv3, ctx);
}

TEMPLATE(T, poly_fit_length) (res, len, ctx);
_TEMPLATE(T, poly_compose_mod_brent_kung) (res->coeffs, poly1->coeffs,
len1, ptr2, poly3->coeffs, len3,
ctx);
_TEMPLATE(T, poly_set_length) (res, len, ctx);
_TEMPLATE(T, poly_normalise) (res, ctx);

_TEMPLATE(T, vec_clear) (ptr2, vec_len, ctx);
gr_ctx_t gr_ctx;
TEMPLATE3(_gr_ctx_init, T, from_ref)(gr_ctx, ctx);
GR_MUST_SUCCEED(gr_poly_compose_mod_brent_kung((gr_poly_struct *) res,
(const gr_poly_struct *) poly1,
(const gr_poly_struct *) poly2,
(const gr_poly_struct *) poly3, gr_ctx));
}


#endif
Loading

0 comments on commit 2d68d18

Please sign in to comment.