1 /*
2     Copyright (C) 2012 Sebastian Pancratz
3     Copyright (C) 2013 Mike Hansen
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
11 */
12 
13 #include "ulong_extras.h"
14 #include "fq_nmod.h"
15 
16 /*
17     Sets (rop, 2d-1) to the image of (op, len) under the Frobenius operator
18     raised to the e-th power, assuming that neither op nor e are zero.
19  */
20 
_fq_nmod_frobenius(mp_limb_t * rop,const mp_limb_t * op,slong len,slong e,const fq_nmod_ctx_t ctx)21 void _fq_nmod_frobenius(mp_limb_t *rop, const mp_limb_t *op, slong len, slong e,
22                         const fq_nmod_ctx_t ctx)
23 {
24     const slong d = ctx->j[ctx->len - 1];
25 
26     if (len == 1)  /* op is in Fp, not just Fq */
27     {
28         _nmod_vec_set(rop, op, len);
29         _nmod_vec_zero(rop + len, (2*d - 1)  - len);
30     }
31     else
32     {
33         fmpz_t t;
34 
35         fmpz_init(t);
36         fmpz_pow_ui(t, fq_nmod_ctx_prime(ctx), e);
37         _fq_nmod_pow(rop, op, len, t, ctx);
38         fmpz_clear(t);
39     }
40 }
41 
fq_nmod_frobenius(fq_nmod_t rop,const fq_nmod_t op,slong e,const fq_nmod_ctx_t ctx)42 void fq_nmod_frobenius(fq_nmod_t rop, const fq_nmod_t op, slong e, const fq_nmod_ctx_t ctx)
43 {
44     const slong d = fq_nmod_ctx_degree(ctx);
45 
46     e = e % d;
47     if (e < 0)
48         e += d;
49 
50     if (fq_nmod_is_zero(op, ctx))
51     {
52         fq_nmod_zero(rop, ctx);
53     }
54     else if (e == 0)
55     {
56         fq_nmod_set(rop, op, ctx);
57     }
58     else
59     {
60         mp_limb_t *t;
61 
62         if (rop == op)
63         {
64             t = _nmod_vec_init(2 * d - 1);
65         }
66         else
67         {
68             nmod_poly_fit_length(rop, 2 * d - 1);
69             t = rop->coeffs;
70         }
71 
72         _fq_nmod_frobenius(t, op->coeffs, op->length, e, ctx);
73 
74         if (rop == op)
75         {
76             _nmod_vec_clear(rop->coeffs);
77             rop->coeffs = t;
78             rop->alloc  = 2 * d - 1;
79             rop->length = d;
80         }
81         else
82         {
83             _nmod_poly_set_length(rop, d);
84         }
85         _nmod_poly_normalise(rop);
86     }
87 }
88 
89