1 /*
2 Copyright (C) 2011 Sebastian Pancratz
3 Copyright (C) 2010 William Hart
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 <https://www.gnu.org/licenses/>.
11 */
12
13 #include <gmp.h>
14 #include "flint.h"
15 #include "fmpz.h"
16 #include "fmpz_poly.h"
17 #include "fmpz_mod_poly.h"
18
_fmpz_mod_poly_sqr(fmpz * res,const fmpz * poly,slong len,const fmpz_t p)19 void _fmpz_mod_poly_sqr(fmpz *res, const fmpz *poly, slong len, const fmpz_t p)
20 {
21 _fmpz_poly_sqr(res, poly, len);
22 _fmpz_vec_scalar_mod_fmpz(res, res, 2 * len - 1, p);
23 }
24
fmpz_mod_poly_sqr(fmpz_mod_poly_t res,const fmpz_mod_poly_t poly,const fmpz_mod_ctx_t ctx)25 void fmpz_mod_poly_sqr(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly,
26 const fmpz_mod_ctx_t ctx)
27 {
28 const slong len = poly->length;
29
30 if (len == 0)
31 {
32 fmpz_mod_poly_zero(res, ctx);
33 return;
34 }
35
36 if (res == poly)
37 {
38 fmpz *t = flint_calloc(2 * len - 1, sizeof(fmpz));
39
40 _fmpz_mod_poly_sqr(t, poly->coeffs, len, fmpz_mod_ctx_modulus(ctx));
41
42 _fmpz_vec_clear(res->coeffs, res->alloc);
43 res->alloc = 2 * len - 1;
44 res->length = 2 * len - 1;
45 res->coeffs = t;
46 _fmpz_mod_poly_normalise(res);
47 }
48 else
49 {
50 fmpz_mod_poly_fit_length(res, 2*len - 1, ctx);
51
52 _fmpz_mod_poly_sqr(res->coeffs, poly->coeffs, len,
53 fmpz_mod_ctx_modulus(ctx));
54
55 _fmpz_mod_poly_set_length(res, 2 * len - 1);
56 _fmpz_mod_poly_normalise(res);
57 }
58 }
59
60