1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright (C) 2014 William Hart
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpz_mod_poly.h"
void
fmpz_mod_poly_frobenius_power(fmpz_mod_poly_t res,
fmpz_mod_poly_frobenius_powers_2exp_t pow,
const fmpz_mod_poly_t f, ulong m, const fmpz_mod_ctx_t ctx)
{
slong i = 0;
ulong bit;
fmpz_mod_poly_struct * r;
fmpz_mod_poly_t tr;
if (res == f)
{
fmpz_mod_poly_init(tr, ctx);
r = tr;
}
else
{
r = res;
}
/* res = x^(p^0) = x */
if (m == 0)
{
fmpz_mod_poly_set_coeff_ui(r, 1, 1, ctx);
fmpz_mod_poly_set_coeff_ui(r, 0, 0, ctx);
_fmpz_mod_poly_set_length(r, 2);
/*
This is safe wrt impossible inverses, because any zero divisors
in the leading coefficient of f will have been found in the
precomp stage.
*/
if (f->length <= 2)
fmpz_mod_poly_rem(r, r, f, ctx);
}
else
{
/* first nonzero bit */
while ((m & (WORD(1) << i)) == 0)
i++;
/* res = f^(p^(2^i)) */
fmpz_mod_poly_set(r, pow->pow + i, ctx);
m ^= (WORD(1) << i);
while (m != 0)
{
i++;
bit = (WORD(1) << i);
if ((bit & m) != 0)
{
fmpz_mod_poly_compose_mod(r, pow->pow + i, r, f, ctx);
m ^= bit;
}
}
}
if (res == f)
{
fmpz_mod_poly_swap(res, r, ctx);
fmpz_mod_poly_clear(tr, ctx);
}
}