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.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 
21 void
_fq_frobenius(fmpz * rop,const fmpz * op,slong len,slong e,const fq_ctx_t ctx)22 _fq_frobenius(fmpz * rop, const fmpz * op, slong len, slong e, const fq_ctx_t ctx)
23 {
24     const slong d = fq_ctx_degree(ctx);
25 
26     if (len == 1)               /* op is in Fp, not just Fq */
27     {
28         _fmpz_vec_set(rop, op, len);
29         _fmpz_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_ctx_prime(ctx), e);
37         _fq_pow(rop, op, len, t, ctx);
38         fmpz_clear(t);
39     }
40 }
41 
42 void
fq_frobenius(fq_t rop,const fq_t op,slong e,const fq_ctx_t ctx)43 fq_frobenius(fq_t rop, const fq_t op, slong e, const fq_ctx_t ctx)
44 {
45     const slong d = fq_ctx_degree(ctx);
46 
47     e = e % d;
48     if (e < 0)
49         e += d;
50 
51     if (fq_is_zero(op, ctx))
52     {
53         fq_zero(rop, ctx);
54     }
55     else if (e == 0)
56     {
57         fq_set(rop, op, ctx);
58     }
59     else
60     {
61         fmpz *t;
62 
63         if (rop == op)
64         {
65             t = _fmpz_vec_init(2 * d - 1);
66         }
67         else
68         {
69             fmpz_poly_fit_length(rop, 2 * d - 1);
70             t = rop->coeffs;
71         }
72 
73         _fq_frobenius(t, op->coeffs, op->length, e, ctx);
74 
75         if (rop == op)
76         {
77             _fmpz_vec_clear(rop->coeffs, rop->alloc);
78             rop->coeffs = t;
79             rop->alloc = 2 * d - 1;
80             rop->length = d;
81         }
82         else
83         {
84             _fmpz_poly_set_length(rop, d);
85         }
86         _fmpz_poly_normalise(rop);
87     }
88 }
89