Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - FpX.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20443-183d202) Lines: 1199 1332 90.0 %
Date: 2017-03-27 05:17:48 Functions: 145 153 94.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2007  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Not so fast arithmetic with polynomials over Fp */
      18             : 
      19             : static GEN
      20    45683219 : get_FpX_red(GEN T, GEN *B)
      21             : {
      22    45683219 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
      23       58635 :   *B = gel(T,1); return gel(T,2);
      24             : }
      25             : 
      26             : GEN
      27      275192 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
      28             : 
      29             : long
      30      981733 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
      31             : 
      32             : long
      33     1026230 : get_FpX_degree(GEN T) { return typ(T)==t_VEC? degpol(gel(T,2)): degpol(T); }
      34             : 
      35             : /***********************************************************************/
      36             : /**                                                                   **/
      37             : /**                              FpX                                  **/
      38             : /**                                                                   **/
      39             : /***********************************************************************/
      40             : 
      41             : /* FpX are polynomials over Z/pZ represented as t_POL with
      42             :  * t_INT coefficients.
      43             :  * 1) Coefficients should belong to {0,...,p-1}, though non-reduced
      44             :  * coefficients should work but be slower.
      45             :  *
      46             :  * 2) p is not assumed to be prime, but it is assumed that impossible divisions
      47             :  *    will not happen.
      48             :  * 3) Theses functions let some garbage on the stack, but are gerepileupto
      49             :  * compatible.
      50             :  */
      51             : 
      52             : static ulong
      53    32383614 : to_Flx(GEN *P, GEN *Q, GEN p)
      54             : {
      55    32383614 :   ulong pp = uel(p,2);
      56    32383614 :   *P = ZX_to_Flx(*P, pp);
      57    32383614 :   *Q = ZX_to_Flx(*Q, pp); return pp;
      58             : }
      59             : 
      60             : static ulong
      61      535981 : to_Flxq(GEN *P, GEN *T, GEN p)
      62             : {
      63      535981 :   ulong pp = uel(p,2);
      64      535981 :   if (P) *P = ZX_to_Flx(*P, pp);
      65      535981 :   *T = ZXT_to_FlxT(*T, pp); return pp;
      66             : }
      67             : 
      68             : GEN
      69        1220 : Z_to_FpX(GEN a, GEN p, long v)
      70             : {
      71        1220 :   pari_sp av = avma;
      72        1220 :   GEN z = cgetg(3, t_POL);
      73        1220 :   GEN x = modii(a, p);
      74        1220 :   if (!signe(x)) { avma =av; return pol_0(v); }
      75        1220 :   z[1] = evalsigne(1) | evalvarn(v);
      76        1220 :   gel(z,2) = x; return z;
      77             : }
      78             : 
      79             : /* z in Z[X], return lift(z * Mod(1,p)), normalized*/
      80             : GEN
      81    46834326 : FpX_red(GEN z, GEN p)
      82             : {
      83    46834326 :   long i, l = lg(z);
      84    46834326 :   GEN x = cgetg(l, t_POL);
      85    46834326 :   for (i=2; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      86    46834326 :   x[1] = z[1]; return FpX_renormalize(x,l);
      87             : }
      88             : GEN
      89      207575 : FpXV_red(GEN z, GEN p)
      90             : {
      91      207575 :   long i,l = lg(z);
      92      207575 :   GEN x = cgetg(l, t_VEC);
      93      207575 :   for (i=1; i<l; i++) gel(x,i) = FpX_red(gel(z,i), p);
      94      207575 :   return x;
      95             : }
      96             : 
      97             : GEN
      98      777669 : FpXT_red(GEN z, GEN p)
      99             : {
     100      777669 :   if (typ(z) == t_POL)
     101      720797 :     return FpX_red(z, p);
     102             :   else
     103             :   {
     104       56872 :     long i,l = lg(z);
     105       56872 :     GEN x = cgetg(l, t_VEC);
     106       56872 :     for (i=1; i<l; i++) gel(x,i) = FpXT_red(gel(z,i), p);
     107       56872 :     return x;
     108             :   }
     109             : }
     110             : 
     111             : GEN
     112      185815 : FpX_normalize(GEN z, GEN p)
     113             : {
     114      185815 :   GEN p1 = leading_coeff(z);
     115      185815 :   if (lg(z) == 2 || equali1(p1)) return z;
     116       31925 :   return FpX_Fp_mul_to_monic(z, Fp_inv(p1,p), p);
     117             : }
     118             : 
     119             : GEN
     120      470420 : FpX_center(GEN T, GEN p, GEN pov2)
     121             : {
     122      470420 :   long i, l = lg(T);
     123      470420 :   GEN P = cgetg(l,t_POL);
     124      470420 :   for(i=2; i<l; i++) gel(P,i) = Fp_center(gel(T,i), p, pov2);
     125      470420 :   P[1] = T[1]; return P;
     126             : }
     127             : 
     128             : GEN
     129     5747720 : FpX_add(GEN x,GEN y,GEN p)
     130             : {
     131     5747720 :   long lx = lg(x), ly = lg(y), i;
     132             :   GEN z;
     133     5747720 :   if (lx < ly) swapspec(x,y, lx,ly);
     134     5747720 :   z = cgetg(lx,t_POL); z[1] = x[1];
     135     5747720 :   for (i=2; i<ly; i++) gel(z,i) = Fp_add(gel(x,i),gel(y,i), p);
     136     5747720 :   for (   ; i<lx; i++) gel(z,i) = modii(gel(x,i), p);
     137     5747720 :   z = ZX_renormalize(z, lx);
     138     5747720 :   if (!lgpol(z)) { avma = (pari_sp)(z + lx); return pol_0(varn(x)); }
     139     5552150 :   return z;
     140             : }
     141             : 
     142             : static GEN
     143        4715 : Fp_red_FpX(GEN x, GEN p, long v)
     144             : {
     145             :   GEN z;
     146        4715 :   if (!signe(x)) return pol_0(v);
     147         325 :   z = cgetg(3, t_POL);
     148         325 :   gel(z,2) = Fp_red(x,p);
     149         325 :   z[1] = evalvarn(v);
     150         325 :   return FpX_renormalize(z, 3);
     151             : }
     152             : 
     153             : static GEN
     154           5 : Fp_neg_FpX(GEN x, GEN p, long v)
     155             : {
     156             :   GEN z;
     157           5 :   if (!signe(x)) return pol_0(v);
     158           0 :   z = cgetg(3, t_POL);
     159           0 :   gel(z,2) = Fp_neg(x,p);
     160           0 :   z[1] = evalvarn(v);
     161           0 :   return FpX_renormalize(z, 3);
     162             : }
     163             : 
     164             : GEN
     165      393025 : FpX_Fp_add(GEN y,GEN x,GEN p)
     166             : {
     167      393025 :   long i, lz = lg(y);
     168             :   GEN z;
     169      393025 :   if (lz == 2) return Fp_red_FpX(x,p,varn(y));
     170      388310 :   z = cgetg(lz,t_POL); z[1] = y[1];
     171      388310 :   gel(z,2) = Fp_add(gel(y,2),x, p);
     172      388310 :   if (lz == 3) z = FpX_renormalize(z,lz);
     173             :   else
     174      354420 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     175      388310 :   return z;
     176             : }
     177             : GEN
     178           0 : FpX_Fp_add_shallow(GEN y,GEN x,GEN p)
     179             : {
     180           0 :   long i, lz = lg(y);
     181             :   GEN z;
     182           0 :   if (lz == 2) return scalar_ZX_shallow(x,varn(y));
     183           0 :   z = cgetg(lz,t_POL); z[1] = y[1];
     184           0 :   gel(z,2) = Fp_add(gel(y,2),x, p);
     185           0 :   if (lz == 3) z = FpX_renormalize(z,lz);
     186             :   else
     187           0 :     for(i=3;i<lz;i++) gel(z,i) = gel(y,i);
     188           0 :   return z;
     189             : }
     190             : GEN
     191      213400 : FpX_Fp_sub(GEN y,GEN x,GEN p)
     192             : {
     193      213400 :   long i, lz = lg(y);
     194             :   GEN z;
     195      213400 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     196      213395 :   z = cgetg(lz,t_POL); z[1] = y[1];
     197      213395 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     198      213395 :   if (lz == 3) z = FpX_renormalize(z,lz);
     199             :   else
     200       87040 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     201      213395 :   return z;
     202             : }
     203             : GEN
     204        1095 : FpX_Fp_sub_shallow(GEN y,GEN x,GEN p)
     205             : {
     206        1095 :   long i, lz = lg(y);
     207             :   GEN z;
     208        1095 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     209        1095 :   z = cgetg(lz,t_POL); z[1] = y[1];
     210        1095 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     211        1095 :   if (lz == 3) z = FpX_renormalize(z,lz);
     212             :   else
     213         980 :     for(i=3;i<lz;i++) gel(z,i) = gel(y,i);
     214        1095 :   return z;
     215             : }
     216             : 
     217             : GEN
     218       51158 : FpX_neg(GEN x,GEN p)
     219             : {
     220       51158 :   long i, lx = lg(x);
     221       51158 :   GEN y = cgetg(lx,t_POL);
     222       51158 :   y[1] = x[1];
     223       51158 :   for(i=2; i<lx; i++) gel(y,i) = Fp_neg(gel(x,i), p);
     224       51158 :   return ZX_renormalize(y, lx);
     225             : }
     226             : 
     227             : static GEN
     228     4660093 : FpX_subspec(GEN x,GEN y,GEN p, long nx, long ny)
     229             : {
     230             :   long i, lz;
     231             :   GEN z;
     232     4660093 :   if (nx >= ny)
     233             :   {
     234     3416904 :     lz = nx+2;
     235     3416904 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     236     3416904 :     for (i=0; i<ny; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     237     3416904 :     for (   ; i<nx; i++) gel(z,i) = modii(gel(x,i), p);
     238             :   }
     239             :   else
     240             :   {
     241     1243189 :     lz = ny+2;
     242     1243189 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     243     1243189 :     for (i=0; i<nx; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     244     1243189 :     for (   ; i<ny; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     245             :   }
     246     4660093 :   z = FpX_renormalize(z-2, lz);
     247     4660093 :   if (!lgpol(z)) { avma = (pari_sp)(z + lz); return pol_0(0); }
     248     4578548 :   return z;
     249             : }
     250             : 
     251             : GEN
     252     4609549 : FpX_sub(GEN x,GEN y,GEN p)
     253             : {
     254     4609549 :   GEN z = FpX_subspec(x+2,y+2,p,lgpol(x),lgpol(y));
     255     4609549 :   setvarn(z, varn(x));
     256     4609549 :   return z;
     257             : }
     258             : 
     259             : GEN
     260        5799 : Fp_FpX_sub(GEN x, GEN y, GEN p)
     261             : {
     262        5799 :   long ly = lg(y), i;
     263             :   GEN z;
     264        5799 :   if (ly <= 3) {
     265         140 :     z = cgetg(3, t_POL);
     266         140 :     x = (ly == 3)? Fp_sub(x, gel(y,2), p): modii(x, p);
     267         140 :     if (!signe(x)) { avma = (pari_sp)(z + 3); return pol_0(varn(y)); }
     268         125 :     z[1] = evalsigne(1)|y[1]; gel(z,2) = x; return z;
     269             :   }
     270        5659 :   z = cgetg(ly,t_POL);
     271        5659 :   gel(z,2) = Fp_sub(x, gel(y,2), p);
     272        5659 :   for (i = 3; i < ly; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     273        5659 :   z = ZX_renormalize(z, ly);
     274        5659 :   if (!lgpol(z)) { avma = (pari_sp)(z + ly); return pol_0(varn(x)); }
     275        5659 :   z[1] = y[1]; return z;
     276             : }
     277             : 
     278             : GEN
     279    32700601 : FpX_mul(GEN x,GEN y,GEN p) { return FpX_red(ZX_mul(x, y), p); }
     280             : 
     281             : GEN
     282     1136190 : FpX_mulspec(GEN a, GEN b, GEN p, long na, long nb)
     283     1136190 : { return FpX_red(ZX_mulspec(a, b, na, nb), p); }
     284             : 
     285             : GEN
     286     2095410 : FpX_sqr(GEN x,GEN p) { return FpX_red(ZX_sqr(x), p); }
     287             : 
     288             : GEN
     289      842265 : FpX_mulu(GEN y, ulong x,GEN p)
     290             : {
     291             :   GEN z;
     292             :   long i, l;
     293      842265 :   x = umodui(x, p);
     294      842265 :   if (!x) return zeropol(varn(y));
     295      842075 :   z = cgetg_copy(y, &l); z[1] = y[1];
     296      842075 :   for(i=2; i<l; i++) gel(z,i) = Fp_mulu(gel(y,i), x, p);
     297      842075 :   return z;
     298             : }
     299             : 
     300             : GEN
     301     2497008 : FpX_Fp_mulspec(GEN y,GEN x,GEN p,long ly)
     302             : {
     303             :   GEN z;
     304             :   long i;
     305     2497008 :   if (!signe(x)) return pol_0(0);
     306     2315638 :   z = cgetg(ly+2,t_POL); z[1] = evalsigne(1);
     307     2315638 :   for(i=0; i<ly; i++) gel(z,i+2) = Fp_mul(gel(y,i), x, p);
     308     2315638 :   return ZX_renormalize(z, ly+2);
     309             : }
     310             : 
     311             : GEN
     312     2493018 : FpX_Fp_mul(GEN y,GEN x,GEN p)
     313             : {
     314     2493018 :   GEN z = FpX_Fp_mulspec(y+2,x,p,lgpol(y));
     315     2493018 :   setvarn(z, varn(y)); return z;
     316             : }
     317             : 
     318             : GEN
     319       31925 : FpX_Fp_mul_to_monic(GEN y,GEN x,GEN p)
     320             : {
     321             :   GEN z;
     322             :   long i, l;
     323       31925 :   z = cgetg_copy(y, &l); z[1] = y[1];
     324       31925 :   for(i=2; i<l-1; i++) gel(z,i) = Fp_mul(gel(y,i), x, p);
     325       31925 :   gel(z,l-1) = gen_1; return z;
     326             : }
     327             : 
     328             : struct _FpXQ {
     329             :   GEN T, p, aut;
     330             : };
     331             : 
     332             : static GEN
     333       40655 : _FpX_sqr(void *data, GEN x)
     334             : {
     335       40655 :   struct _FpXQ *D = (struct _FpXQ*)data;
     336       40655 :   return FpX_sqr(x, D->p);
     337             : }
     338             : static GEN
     339       50085 : _FpX_mul(void *data, GEN x, GEN y)
     340             : {
     341       50085 :   struct _FpXQ *D = (struct _FpXQ*)data;
     342       50085 :   return FpX_mul(x,y, D->p);
     343             : }
     344             : 
     345             : GEN
     346      195105 : FpX_powu(GEN x, ulong n, GEN p)
     347             : {
     348             :   struct _FpXQ D;
     349      195105 :   if (n==0) return pol_1(varn(x));
     350       28225 :   D.p = p;
     351       28225 :   return gen_powu(x, n, (void *)&D, _FpX_sqr, _FpX_mul);
     352             : }
     353             : 
     354             : GEN
     355         765 : FpX_halve(GEN y, GEN p)
     356             : {
     357             :   GEN z;
     358             :   long i, l;
     359         765 :   z = cgetg_copy(y, &l); z[1] = y[1];
     360         765 :   for(i=2; i<l; i++) gel(z,i) = Fp_halve(gel(y,i), p);
     361         765 :   return z;
     362             : }
     363             : 
     364             : static GEN
     365    43623866 : FpX_divrem_basecase(GEN x, GEN y, GEN p, GEN *pr)
     366             : {
     367             :   long vx, dx, dy, dy1, dz, i, j, sx, lr;
     368             :   pari_sp av0, av;
     369             :   GEN z,p1,rem,lead;
     370             : 
     371    43623866 :   if (!signe(y)) pari_err_INV("FpX_divrem",y);
     372    43623866 :   vx = varn(x);
     373    43623866 :   dy = degpol(y);
     374    43623866 :   dx = degpol(x);
     375    43623866 :   if (dx < dy)
     376             :   {
     377       45410 :     if (pr)
     378             :     {
     379       45400 :       av0 = avma; x = FpX_red(x, p);
     380       45400 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: pol_0(vx); }
     381       45370 :       if (pr == ONLY_REM) return x;
     382       45370 :       *pr = x;
     383             :     }
     384       45380 :     return pol_0(vx);
     385             :   }
     386    43578456 :   lead = leading_coeff(y);
     387    43578456 :   if (!dy) /* y is constant */
     388             :   {
     389      205512 :     if (pr && pr != ONLY_DIVIDES)
     390             :     {
     391      196672 :       if (pr == ONLY_REM) return pol_0(vx);
     392      174527 :       *pr = pol_0(vx);
     393             :     }
     394      183367 :     av0 = avma;
     395      183367 :     if (equali1(lead)) return FpX_red(x, p);
     396      181357 :     else return gerepileupto(av0, FpX_Fp_mul(x, Fp_inv(lead,p), p));
     397             :   }
     398    43372944 :   av0 = avma; dz = dx-dy;
     399    43372944 :   if (lgefint(p) == 3)
     400             :   { /* assume ab != 0 mod p */
     401    32019969 :     ulong pp = to_Flx(&x, &y, p);
     402    32019969 :     z = Flx_divrem(x, y, pp, pr);
     403    32019969 :     avma = av0; /* HACK: assume pr last on stack, then z */
     404    32019969 :     if (!z) return NULL;
     405    32019944 :     z = leafcopy(z);
     406    32019944 :     if (pr && pr != ONLY_DIVIDES && pr != ONLY_REM)
     407             :     {
     408     1558409 :       *pr = leafcopy(*pr);
     409     1558409 :       *pr = Flx_to_ZX_inplace(*pr);
     410             :     }
     411    32019944 :     return Flx_to_ZX_inplace(z);
     412             :   }
     413    11352975 :   lead = equali1(lead)? NULL: gclone(Fp_inv(lead,p));
     414    11352930 :   avma = av0;
     415    11352930 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
     416    11352930 :   x += 2; y += 2; z += 2;
     417    11352930 :   for (dy1=dy-1; dy1>=0 && !signe(gel(y, dy1)); dy1--);
     418             : 
     419    11352930 :   p1 = gel(x,dx); av = avma;
     420    11352930 :   gel(z,dz) = lead? gerepileuptoint(av, Fp_mul(p1,lead, p)): icopy(p1);
     421    28320230 :   for (i=dx-1; i>=dy; i--)
     422             :   {
     423    16967300 :     av=avma; p1=gel(x,i);
     424   233613188 :     for (j=i-dy1; j<=i && j<=dz; j++)
     425   216645888 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     426    16967300 :     if (lead) p1 = mulii(p1,lead);
     427    16967300 :     gel(z,i-dy) = gerepileuptoint(av,modii(p1, p));
     428             :   }
     429    11352930 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
     430             : 
     431    11341610 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     432    11524835 :   for (sx=0; ; i--)
     433             :   {
     434    11524835 :     p1 = gel(x,i);
     435    39932200 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     436    28407365 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     437    11524835 :     p1 = modii(p1,p); if (signe(p1)) { sx = 1; break; }
     438      230165 :     if (!i) break;
     439      183225 :     avma=av;
     440      183225 :   }
     441    11341610 :   if (pr == ONLY_DIVIDES)
     442             :   {
     443           0 :     if (lead) gunclone(lead);
     444           0 :     if (sx) { avma=av0; return NULL; }
     445           0 :     avma = (pari_sp)rem; return z-2;
     446             :   }
     447    11341610 :   lr=i+3; rem -= lr;
     448    11341610 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     449    11341610 :   rem[1] = z[-1];
     450    11341610 :   p1 = gerepileuptoint((pari_sp)rem, p1);
     451    11341610 :   rem += 2; gel(rem,i) = p1;
     452    46045573 :   for (i--; i>=0; i--)
     453             :   {
     454    34703963 :     av=avma; p1 = gel(x,i);
     455   297282907 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     456   262578944 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     457    34703963 :     gel(rem,i) = gerepileuptoint(av, modii(p1,p));
     458             :   }
     459    11341610 :   rem -= 2;
     460    11341610 :   if (lead) gunclone(lead);
     461    11341610 :   if (!sx) (void)FpX_renormalize(rem, lr);
     462    11341610 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     463      297274 :   *pr = rem; return z-2;
     464             : }
     465             : 
     466             : GEN
     467       16625 : FpX_div_by_X_x(GEN a, GEN x, GEN p, GEN *r)
     468             : {
     469       16625 :   long l = lg(a)-1, i;
     470       16625 :   GEN z = cgetg(l, t_POL);
     471       16625 :   z[1] = evalsigne(1) | evalvarn(0);
     472       16625 :   gel(z, l-1) = gel(a,l);
     473      480775 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
     474      464150 :     gel(z, i) = Fp_addmul(gel(a,i+1), x, gel(z,i+1), p);
     475       16625 :   if (r) *r = Fp_addmul(gel(a,2), x, gel(z,2), p);
     476       16625 :   return z;
     477             : }
     478             : 
     479             : static GEN
     480       49790 : _FpX_divrem(void * E, GEN x, GEN y, GEN *r)
     481             : {
     482       49790 :   struct _FpXQ *D = (struct _FpXQ*) E;
     483       49790 :   return FpX_divrem(x, y, D->p, r);
     484             : }
     485             : static GEN
     486        7315 : _FpX_add(void * E, GEN x, GEN y) {
     487        7315 :   struct _FpXQ *D = (struct _FpXQ*) E;
     488        7315 :   return FpX_add(x, y, D->p);
     489             : }
     490             : 
     491             : static struct bb_ring FpX_ring = { _FpX_add,_FpX_mul,_FpX_sqr };
     492             : 
     493             : GEN
     494        4030 : FpX_digits(GEN x, GEN T, GEN p)
     495             : {
     496        4030 :   pari_sp av = avma;
     497             :   struct _FpXQ D;
     498        4030 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     499             :   GEN z;
     500        4030 :   D.p = p;
     501        4030 :   z = gen_digits(x,T,n,(void *)&D, &FpX_ring, _FpX_divrem);
     502        4030 :   return gerepileupto(av, z);
     503             : }
     504             : 
     505             : GEN
     506        1640 : FpXV_FpX_fromdigits(GEN x, GEN T, GEN p)
     507             : {
     508        1640 :   pari_sp av = avma;
     509             :   struct _FpXQ D;
     510             :   GEN z;
     511        1640 :   D.p = p;
     512        1640 :   z = gen_fromdigits(x,T,(void *)&D, &FpX_ring);
     513        1640 :   return gerepileupto(av, z);
     514             : }
     515             : 
     516             : long
     517       16995 : FpX_valrem(GEN x, GEN t, GEN p, GEN *py)
     518             : {
     519       16995 :   pari_sp av=avma;
     520             :   long k;
     521             :   GEN r, y;
     522             : 
     523       49325 :   for (k=0; ; k++)
     524             :   {
     525       49325 :     y = FpX_divrem(x, t, p, &r);
     526       49325 :     if (signe(r)) break;
     527       32330 :     x = y;
     528       32330 :   }
     529       16995 :   *py = gerepilecopy(av,x);
     530       16995 :   return k;
     531             : }
     532             : 
     533             : static GEN
     534          14 : FpX_halfgcd_basecase(GEN a, GEN b, GEN p)
     535             : {
     536          14 :   pari_sp av=avma;
     537             :   GEN u,u1,v,v1;
     538          14 :   long vx = varn(a);
     539          14 :   long n = lgpol(a)>>1;
     540          14 :   u1 = v = pol_0(vx);
     541          14 :   u = v1 = pol_1(vx);
     542         290 :   while (lgpol(b)>n)
     543             :   {
     544         262 :     GEN r, q = FpX_divrem(a,b,p, &r);
     545         262 :     a = b; b = r; swap(u,u1); swap(v,v1);
     546         262 :     u1 = FpX_sub(u1, FpX_mul(u, q, p), p);
     547         262 :     v1 = FpX_sub(v1, FpX_mul(v, q ,p), p);
     548         262 :     if (gc_needed(av,2))
     549             :     {
     550           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_halfgcd (d = %ld)",degpol(b));
     551           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
     552             :     }
     553             :   }
     554          14 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
     555             : }
     556             : static GEN
     557          12 : FpX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN p)
     558             : {
     559          12 :   return FpX_add(FpX_mul(u, x, p),FpX_mul(v, y, p), p);
     560             : }
     561             : 
     562             : static GEN
     563           4 : FpXM_FpX_mul2(GEN M, GEN x, GEN y, GEN p)
     564             : {
     565           4 :   GEN res = cgetg(3, t_COL);
     566           4 :   gel(res, 1) = FpX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, p);
     567           4 :   gel(res, 2) = FpX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, p);
     568           4 :   return res;
     569             : }
     570             : 
     571             : static GEN
     572           4 : FpXM_mul2(GEN A, GEN B, GEN p)
     573             : {
     574           4 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
     575           4 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
     576           4 :   GEN M1 = FpX_mul(FpX_add(A11,A22, p), FpX_add(B11,B22, p), p);
     577           4 :   GEN M2 = FpX_mul(FpX_add(A21,A22, p), B11, p);
     578           4 :   GEN M3 = FpX_mul(A11, FpX_sub(B12,B22, p), p);
     579           4 :   GEN M4 = FpX_mul(A22, FpX_sub(B21,B11, p), p);
     580           4 :   GEN M5 = FpX_mul(FpX_add(A11,A12, p), B22, p);
     581           4 :   GEN M6 = FpX_mul(FpX_sub(A21,A11, p), FpX_add(B11,B12, p), p);
     582           4 :   GEN M7 = FpX_mul(FpX_sub(A12,A22, p), FpX_add(B21,B22, p), p);
     583           4 :   GEN T1 = FpX_add(M1,M4, p), T2 = FpX_sub(M7,M5, p);
     584           4 :   GEN T3 = FpX_sub(M1,M2, p), T4 = FpX_add(M3,M6, p);
     585           4 :   retmkmat2(mkcol2(FpX_add(T1,T2, p), FpX_add(M2,M4, p)),
     586             :             mkcol2(FpX_add(M3,M5, p), FpX_add(T3,T4, p)));
     587             : }
     588             : 
     589             : /* Return [0,1;1,-q]*M */
     590             : static GEN
     591           2 : FpX_FpXM_qmul(GEN q, GEN M, GEN p)
     592             : {
     593           2 :   GEN u, v, res = cgetg(3, t_MAT);
     594           2 :   u = FpX_sub(gcoeff(M,1,1), FpX_mul(gcoeff(M,2,1), q, p), p);
     595           2 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
     596           2 :   v = FpX_sub(gcoeff(M,1,2), FpX_mul(gcoeff(M,2,2), q, p), p);
     597           2 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
     598           2 :   return res;
     599             : }
     600             : 
     601             : static GEN
     602           2 : matid2_FpXM(long v)
     603             : {
     604           2 :   retmkmat2(mkcol2(pol_1(v),pol_0(v)),
     605             :             mkcol2(pol_0(v),pol_1(v)));
     606             : }
     607             : 
     608             : static GEN
     609           2 : FpX_halfgcd_split(GEN x, GEN y, GEN p)
     610             : {
     611           2 :   pari_sp av=avma;
     612             :   GEN R, S, V;
     613             :   GEN y1, r, q;
     614           2 :   long l = lgpol(x), n = l>>1, k;
     615           2 :   if (lgpol(y)<=n) return matid2_FpXM(varn(x));
     616           2 :   R = FpX_halfgcd(RgX_shift_shallow(x,-n),RgX_shift_shallow(y,-n),p);
     617           2 :   V = FpXM_FpX_mul2(R,x,y,p); y1 = gel(V,2);
     618           2 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
     619           2 :   q = FpX_divrem(gel(V,1), y1, p, &r);
     620           2 :   k = 2*n-degpol(y1);
     621           2 :   S = FpX_halfgcd(RgX_shift_shallow(y1,-k), RgX_shift_shallow(r,-k),p);
     622           2 :   return gerepileupto(av, FpXM_mul2(S,FpX_FpXM_qmul(q,R,p),p));
     623             : }
     624             : 
     625             : /* Return M in GL_2(Fp[X]) such that:
     626             : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
     627             : */
     628             : 
     629             : static GEN
     630          16 : FpX_halfgcd_i(GEN x, GEN y, GEN p)
     631             : {
     632          16 :   if (lg(x)<=FpX_HALFGCD_LIMIT) return FpX_halfgcd_basecase(x,y,p);
     633           2 :   return FpX_halfgcd_split(x,y,p);
     634             : }
     635             : 
     636             : GEN
     637         101 : FpX_halfgcd(GEN x, GEN y, GEN p)
     638             : {
     639         101 :   pari_sp av = avma;
     640             :   GEN M,q,r;
     641         101 :   if (lgefint(p)==3)
     642             :   {
     643          85 :     ulong pp = to_Flx(&x, &y, p);
     644          85 :     M = FlxM_to_ZXM(Flx_halfgcd(x, y, pp));
     645             :   }
     646             :   else
     647             :   {
     648          16 :     if (!signe(x))
     649             :     {
     650           0 :       long v = varn(x);
     651           0 :       retmkmat2(mkcol2(pol_0(v),pol_1(v)),
     652             :                 mkcol2(pol_1(v),pol_0(v)));
     653             :     }
     654          16 :     if (degpol(y)<degpol(x)) return FpX_halfgcd_i(x,y,p);
     655           2 :     q = FpX_divrem(y,x,p,&r);
     656           2 :     M = FpX_halfgcd_i(x,r,p);
     657           2 :     gcoeff(M,1,1) = FpX_sub(gcoeff(M,1,1), FpX_mul(q, gcoeff(M,1,2), p), p);
     658           2 :     gcoeff(M,2,1) = FpX_sub(gcoeff(M,2,1), FpX_mul(q, gcoeff(M,2,2), p), p);
     659             :   }
     660          87 :   return gerepilecopy(av, M);
     661             : }
     662             : 
     663             : static GEN
     664       36060 : FpX_gcd_basecase(GEN a, GEN b, GEN p)
     665             : {
     666       36060 :   pari_sp av = avma, av0=avma;
     667      484815 :   while (signe(b))
     668             :   {
     669             :     GEN c;
     670      412740 :     if (gc_needed(av0,2))
     671             :     {
     672           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_gcd (d = %ld)",degpol(b));
     673           0 :       gerepileall(av0,2, &a,&b);
     674             :     }
     675      412740 :     av = avma; c = FpX_rem(a,b,p); a=b; b=c;
     676             :   }
     677       36015 :   avma = av; return a;
     678             : }
     679             : 
     680             : GEN
     681      315170 : FpX_gcd(GEN x, GEN y, GEN p)
     682             : {
     683      315170 :   pari_sp av = avma;
     684      315170 :   if (lgefint(p)==3)
     685             :   {
     686             :     ulong pp;
     687      278990 :     (void)new_chunk((lg(x) + lg(y)) << 2); /* scratch space */
     688      278990 :     pp = to_Flx(&x, &y, p);
     689      278990 :     x = Flx_gcd(x, y, pp);
     690      278990 :     avma = av; return Flx_to_ZX(x);
     691             :   }
     692       36180 :   x = FpX_red(x, p);
     693       36180 :   y = FpX_red(y, p);
     694       36180 :   if (!signe(x)) return gerepileupto(av, y);
     695       72120 :   while (lg(y)>FpX_GCD_LIMIT)
     696             :   {
     697             :     GEN c;
     698           0 :     if (lgpol(y)<=(lgpol(x)>>1))
     699             :     {
     700           0 :       GEN r = FpX_rem(x, y, p);
     701           0 :       x = y; y = r;
     702             :     }
     703           0 :     c = FpXM_FpX_mul2(FpX_halfgcd(x,y, p), x, y, p);
     704           0 :     x = gel(c,1); y = gel(c,2);
     705           0 :     gerepileall(av,2,&x,&y);
     706             :   }
     707       36060 :   return gerepileupto(av, FpX_gcd_basecase(x,y,p));
     708             : }
     709             : 
     710             : /* Return NULL if gcd can be computed else return a factor of p */
     711             : GEN
     712          75 : FpX_gcd_check(GEN x, GEN y, GEN p)
     713             : {
     714          75 :   pari_sp av = avma;
     715             :   GEN a,b,c;
     716             : 
     717          75 :   a = FpX_red(x, p);
     718          75 :   b = FpX_red(y, p);
     719         825 :   while (signe(b))
     720             :   {
     721         710 :     GEN g = gcdii(p, leading_coeff(b));
     722         710 :     if (!equali1(g)) return gerepileuptoint(av,g);
     723         675 :     c = FpX_rem(a,b,p); a = b; b = c;
     724             :   }
     725          40 :   avma = av; return NULL;
     726             : }
     727             : 
     728             : static GEN
     729      174527 : FpX_extgcd_basecase(GEN a, GEN b, GEN p, GEN *ptu, GEN *ptv)
     730             : {
     731      174527 :   pari_sp av=avma;
     732             :   GEN u,v,d,d1,v1;
     733      174527 :   long vx = varn(a);
     734      174527 :   d = a; d1 = b;
     735      174527 :   v = pol_0(vx); v1 = pol_1(vx);
     736      766432 :   while (signe(d1))
     737             :   {
     738      417378 :     GEN r, q = FpX_divrem(d,d1,p, &r);
     739      417378 :     v = FpX_sub(v,FpX_mul(q,v1,p),p);
     740      417378 :     u=v; v=v1; v1=u;
     741      417378 :     u=r; d=d1; d1=u;
     742      417378 :     if (gc_needed(av,2))
     743             :     {
     744           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_extgcd (d = %ld)",degpol(d));
     745           0 :       gerepileall(av,5, &d,&d1,&u,&v,&v1);
     746             :     }
     747             :   }
     748      174527 :   if (ptu) *ptu = FpX_div(FpX_sub(d,FpX_mul(b,v,p),p),a,p);
     749      174527 :   *ptv = v; return d;
     750             : }
     751             : 
     752             : static GEN
     753           2 : FpX_extgcd_halfgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     754             : {
     755           2 :   pari_sp av=avma;
     756           2 :   GEN u,v,R = matid2_FpXM(varn(x));
     757           6 :   while (lg(y)>FpX_EXTGCD_LIMIT)
     758             :   {
     759             :     GEN M, c;
     760           2 :     if (lgpol(y)<=(lgpol(x)>>1))
     761             :     {
     762           0 :       GEN r, q = FpX_divrem(x, y, p, &r);
     763           0 :       x = y; y = r;
     764           0 :       R = FpX_FpXM_qmul(q, R, p);
     765             :     }
     766           2 :     M = FpX_halfgcd(x,y, p);
     767           2 :     c = FpXM_FpX_mul2(M, x,y, p);
     768           2 :     R = FpXM_mul2(M, R, p);
     769           2 :     x = gel(c,1); y = gel(c,2);
     770           2 :     gerepileall(av,3,&x,&y,&R);
     771             :   }
     772           2 :   y = FpX_extgcd_basecase(x,y,p,&u,&v);
     773           2 :   if (ptu) *ptu = FpX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1),p);
     774           2 :   *ptv = FpX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2),p);
     775           2 :   return y;
     776             : }
     777             : 
     778             : /* x and y in Z[X], return lift(gcd(x mod p, y mod p)). Set u and v st
     779             :  * ux + vy = gcd (mod p) */
     780             : GEN
     781      259097 : FpX_extgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     782             : {
     783             :   GEN d;
     784      259097 :   pari_sp ltop=avma;
     785      259097 :   if (lgefint(p)==3)
     786             :   {
     787       84570 :     ulong pp = to_Flx(&x, &y, p);
     788       84570 :     d = Flx_extgcd(x,y, pp, ptu,ptv);
     789       84570 :     d = Flx_to_ZX(d);
     790       84570 :     if (ptu) *ptu=Flx_to_ZX(*ptu);
     791       84570 :     *ptv=Flx_to_ZX(*ptv);
     792             :   }
     793             :   else
     794             :   {
     795      174527 :     x = FpX_red(x, p);
     796      174527 :     y = FpX_red(y, p);
     797      174527 :     if (lg(y)>FpX_EXTGCD_LIMIT)
     798           2 :       d = FpX_extgcd_halfgcd(x, y, p, ptu, ptv);
     799             :     else
     800      174525 :       d = FpX_extgcd_basecase(x, y, p, ptu, ptv);
     801             :   }
     802      259097 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
     803      259097 :   return d;
     804             : }
     805             : 
     806             : GEN
     807        9570 : FpX_rescale(GEN P, GEN h, GEN p)
     808             : {
     809        9570 :   long i, l = lg(P);
     810        9570 :   GEN Q = cgetg(l,t_POL), hi = h;
     811        9570 :   Q[l-1] = P[l-1];
     812       43715 :   for (i=l-2; i>=2; i--)
     813             :   {
     814       43715 :     gel(Q,i) = Fp_mul(gel(P,i), hi, p);
     815       43715 :     if (i == 2) break;
     816       34145 :     hi = Fp_mul(hi,h, p);
     817             :   }
     818        9570 :   Q[1] = P[1]; return Q;
     819             : }
     820             : 
     821             : GEN
     822      454950 : FpX_deriv(GEN x, GEN p) { return FpX_red(ZX_deriv(x), p); }
     823             : 
     824             : int
     825        1675 : FpX_is_squarefree(GEN f, GEN p)
     826             : {
     827        1675 :   pari_sp av = avma;
     828        1675 :   GEN z = FpX_gcd(f,FpX_deriv(f,p),p);
     829        1675 :   avma = av;
     830        1675 :   return degpol(z)==0;
     831             : }
     832             : 
     833             : GEN
     834       13340 : random_FpX(long d1, long v, GEN p)
     835             : {
     836       13340 :   long i, d = d1+2;
     837       13340 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     838       13340 :   for (i=2; i<d; i++) gel(y,i) = randomi(p);
     839       13340 :   return FpX_renormalize(y,d);
     840             : }
     841             : 
     842             : GEN
     843         400 : FpX_dotproduct(GEN x, GEN y, GEN p)
     844             : {
     845         400 :   long i, l = minss(lg(x), lg(y));
     846             :   pari_sp av;
     847             :   GEN c;
     848         400 :   if (l == 2) return gen_0;
     849         400 :   av = avma; c = mulii(gel(x,2),gel(y,2));
     850         400 :   for (i=3; i<l; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     851         400 :   return gerepileuptoint(av, modii(c,p));
     852             : }
     853             : 
     854             : /* Evaluation in Fp
     855             :  * x a ZX and y an Fp, return x(y) mod p
     856             :  *
     857             :  * If p is very large (several longs) and x has small coefficients(<<p),
     858             :  * then Brent & Kung algorithm is faster. */
     859             : GEN
     860      268755 : FpX_eval(GEN x,GEN y,GEN p)
     861             : {
     862             :   pari_sp av;
     863             :   GEN p1,r,res;
     864      268755 :   long j, i=lg(x)-1;
     865      268755 :   if (i<=2 || !signe(y))
     866      107835 :     return (i==1)? gen_0: modii(gel(x,2),p);
     867      160920 :   res=cgeti(lgefint(p));
     868      160920 :   av=avma; p1=gel(x,i);
     869             :   /* specific attention to sparse polynomials (see poleval)*/
     870             :   /*You've guessed it! It's a copy-paste(tm)*/
     871      774695 :   for (i--; i>=2; i=j-1)
     872             :   {
     873      949065 :     for (j=i; !signe(gel(x,j)); j--)
     874      335290 :       if (j==2)
     875             :       {
     876       20520 :         if (i!=j) y = Fp_powu(y,i-j+1,p);
     877       20520 :         p1=mulii(p1,y);
     878       20520 :         goto fppoleval;/*sorry break(2) no implemented*/
     879             :       }
     880      613775 :     r = (i==j)? y: Fp_powu(y,i-j+1,p);
     881      613775 :     p1 = Fp_addmul(gel(x,j), p1, r, p);
     882      613775 :     if ((i & 7) == 0) { affii(p1, res); p1 = res; avma = av; }
     883             :   }
     884             :  fppoleval:
     885      160920 :   modiiz(p1,p,res);
     886      160920 :   avma = av; return res;
     887             : }
     888             : 
     889             : /* Tz=Tx*Ty where Tx and Ty coprime
     890             :  * return lift(chinese(Mod(x*Mod(1,p),Tx*Mod(1,p)),Mod(y*Mod(1,p),Ty*Mod(1,p))))
     891             :  * if Tz is NULL it is computed
     892             :  * As we do not return it, and the caller will frequently need it,
     893             :  * it must compute it and pass it.
     894             :  */
     895             : GEN
     896         700 : FpX_chinese_coprime(GEN x,GEN y,GEN Tx,GEN Ty,GEN Tz,GEN p)
     897             : {
     898         700 :   pari_sp av = avma;
     899             :   GEN ax,p1;
     900         700 :   ax = FpX_mul(FpXQ_inv(Tx,Ty,p), Tx,p);
     901         700 :   p1 = FpX_mul(ax, FpX_sub(y,x,p),p);
     902         700 :   p1 = FpX_add(x,p1,p);
     903         700 :   if (!Tz) Tz=FpX_mul(Tx,Ty,p);
     904         700 :   p1 = FpX_rem(p1,Tz,p);
     905         700 :   return gerepileupto(av,p1);
     906             : }
     907             : 
     908             : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
     909             : GEN
     910        4075 : FpX_resultant(GEN a, GEN b, GEN p)
     911             : {
     912             :   long da,db,dc;
     913             :   pari_sp av;
     914        4075 :   GEN c,lb, res = gen_1;
     915             : 
     916        4075 :   if (!signe(a) || !signe(b)) return gen_0;
     917        4075 :   da = degpol(a);
     918        4075 :   db = degpol(b);
     919        4075 :   if (db > da)
     920             :   {
     921           0 :     swapspec(a,b, da,db);
     922           0 :     if (both_odd(da,db)) res = subii(p, res);
     923             :   }
     924        4075 :   if (!da) return gen_1; /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
     925        4075 :   av = avma;
     926       23765 :   while (db)
     927             :   {
     928       15620 :     lb = gel(b,db+2);
     929       15620 :     c = FpX_rem(a,b, p);
     930       15620 :     a = b; b = c; dc = degpol(c);
     931       15620 :     if (dc < 0) { avma = av; return NULL; }
     932             : 
     933       15615 :     if (both_odd(da,db)) res = subii(p, res);
     934       15615 :     if (!equali1(lb)) res = Fp_mul(res, Fp_powu(lb, da - dc, p), p);
     935       15615 :     if (gc_needed(av,2))
     936             :     {
     937           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"FpX_resultant (da = %ld)",da);
     938           0 :       gerepileall(av,3, &a,&b,&res);
     939             :     }
     940       15615 :     da = db; /* = degpol(a) */
     941       15615 :     db = dc; /* = degpol(b) */
     942             :   }
     943        4070 :   res = Fp_mul(res, Fp_powu(gel(b,2), da, p), p);
     944        4070 :   return gerepileuptoint(av, res);
     945             : }
     946             : 
     947             : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
     948             : GEN
     949          20 : FpX_disc(GEN P, GEN p)
     950             : {
     951          20 :   pari_sp av = avma;
     952          20 :   GEN L, dP = FpX_deriv(P,p), D = FpX_resultant(P, dP, p);
     953             :   long dd;
     954          20 :   if (!D || !signe(D)) return gen_0;
     955          15 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
     956          15 :   L = leading_coeff(P);
     957          15 :   if (dd && !equali1(L))
     958           5 :     D = (dd == -1)? Fp_div(D,L,p): Fp_mul(D, Fp_powu(L, dd, p), p);
     959          15 :   if (degpol(P) & 2) D = Fp_neg(D ,p);
     960          15 :   return gerepileuptoint(av, D);
     961             : }
     962             : 
     963             : GEN
     964       13400 : FpXV_prod(GEN V, GEN p)
     965             : {
     966             :   struct _FpXQ D;
     967       13400 :   D.p = p;
     968       13400 :   return gen_product(V, (void *)&D, &_FpX_mul);
     969             : }
     970             : 
     971             : GEN
     972        4160 : FpV_roots_to_pol(GEN V, GEN p, long v)
     973             : {
     974        4160 :   pari_sp ltop=avma;
     975             :   long i;
     976        4160 :   GEN g=cgetg(lg(V),t_VEC);
     977       31590 :   for(i=1;i<lg(V);i++)
     978       27430 :     gel(g,i) = deg1pol_shallow(gen_1,modii(negi(gel(V,i)),p),v);
     979        4160 :   return gerepileupto(ltop,FpXV_prod(g,p));
     980             : }
     981             : 
     982             : /* invert all elements of x mod p using Montgomery's multi-inverse trick.
     983             :  * Not stack-clean. */
     984             : GEN
     985        1540 : FpV_inv(GEN x, GEN p)
     986             : {
     987        1540 :   long i, lx = lg(x);
     988        1540 :   GEN u, y = cgetg(lx, t_VEC);
     989             : 
     990        1540 :   gel(y,1) = gel(x,1);
     991        1540 :   for (i=2; i<lx; i++) gel(y,i) = Fp_mul(gel(y,i-1), gel(x,i), p);
     992             : 
     993        1540 :   u = Fp_inv(gel(y,--i), p);
     994       16425 :   for ( ; i > 1; i--)
     995             :   {
     996       14885 :     gel(y,i) = Fp_mul(u, gel(y,i-1), p);
     997       14885 :     u = Fp_mul(u, gel(x,i), p); /* u = 1 / (x[1] ... x[i-1]) */
     998             :   }
     999        1540 :   gel(y,1) = u; return y;
    1000             : }
    1001             : GEN
    1002           0 : FqV_inv(GEN x, GEN T, GEN p)
    1003             : {
    1004           0 :   long i, lx = lg(x);
    1005           0 :   GEN u, y = cgetg(lx, t_VEC);
    1006             : 
    1007           0 :   gel(y,1) = gel(x,1);
    1008           0 :   for (i=2; i<lx; i++) gel(y,i) = Fq_mul(gel(y,i-1), gel(x,i), T,p);
    1009             : 
    1010           0 :   u = Fq_inv(gel(y,--i), T,p);
    1011           0 :   for ( ; i > 1; i--)
    1012             :   {
    1013           0 :     gel(y,i) = Fq_mul(u, gel(y,i-1), T,p);
    1014           0 :     u = Fq_mul(u, gel(x,i), T,p); /* u = 1 / (x[1] ... x[i-1]) */
    1015             :   }
    1016           0 :   gel(y,1) = u; return y;
    1017             : }
    1018             : 
    1019             : /***********************************************************************/
    1020             : /**                                                                   **/
    1021             : /**                      Barrett reduction                            **/
    1022             : /**                                                                   **/
    1023             : /***********************************************************************/
    1024             : 
    1025             : static GEN
    1026         759 : FpX_invBarrett_basecase(GEN T, GEN p)
    1027             : {
    1028         759 :   long i, l=lg(T)-1, lr = l-1, k;
    1029         759 :   GEN r=cgetg(lr, t_POL); r[1]=T[1];
    1030         759 :   gel(r,2) = gen_1;
    1031       45376 :   for (i=3; i<lr; i++)
    1032             :   {
    1033       44617 :     pari_sp av = avma;
    1034       44617 :     GEN u = gel(T,l-i+2);
    1035     1583812 :     for (k=3; k<i; k++)
    1036     1539195 :       u = addii(u, mulii(gel(T,l-i+k), gel(r,k)));
    1037       44617 :     gel(r,i) = gerepileupto(av, modii(negi(u), p));
    1038             :   }
    1039         759 :   return FpX_renormalize(r,lr);
    1040             : }
    1041             : 
    1042             : /* Return new lgpol */
    1043             : static long
    1044      101408 : ZX_lgrenormalizespec(GEN x, long lx)
    1045             : {
    1046             :   long i;
    1047      112074 :   for (i = lx-1; i>=0; i--)
    1048      112074 :     if (signe(gel(x,i))) break;
    1049      101408 :   return i+1;
    1050             : }
    1051             : 
    1052             : INLINE GEN
    1053      101168 : FpX_recipspec(GEN x, long l, long n)
    1054             : {
    1055      101168 :   return RgX_recipspec_shallow(x, l, n);
    1056             : }
    1057             : 
    1058             : static GEN
    1059          20 : FpX_invBarrett_Newton(GEN T, GEN p)
    1060             : {
    1061          20 :   pari_sp av = avma;
    1062          20 :   long nold, lx, lz, lq, l = degpol(T), i, lQ;
    1063          20 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
    1064          20 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
    1065          20 :   for (i=0;i<l;i++) gel(x,i) = gen_0;
    1066          20 :   q = FpX_recipspec(T+2,l+1,l+1); lQ = lgpol(q); q+=2;
    1067             :   /* We work on _spec_ FpX's, all the l[xzq] below are lgpol's */
    1068             : 
    1069             :   /* initialize */
    1070          20 :   gel(x,0) = Fp_inv(gel(q,0), p);
    1071          20 :   if (lQ>1) gel(q,1) = Fp_red(gel(q,1), p);
    1072          20 :   if (lQ>1 && signe(gel(q,1)))
    1073          20 :   {
    1074          20 :     GEN u = gel(q, 1);
    1075          20 :     if (!equali1(gel(x,0))) u = Fp_mul(u, Fp_sqr(gel(x,0), p), p);
    1076          20 :     gel(x,1) = Fp_neg(u, p); lx = 2;
    1077             :   }
    1078             :   else
    1079           0 :     lx = 1;
    1080          20 :   nold = 1;
    1081         200 :   for (; mask > 1; )
    1082             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
    1083         160 :     long i, lnew, nnew = nold << 1;
    1084             : 
    1085         160 :     if (mask & 1) nnew--;
    1086         160 :     mask >>= 1;
    1087             : 
    1088         160 :     lnew = nnew + 1;
    1089         160 :     lq = ZX_lgrenormalizespec(q, minss(lQ,lnew));
    1090         160 :     z = FpX_mulspec(x, q, p, lx, lq); /* FIXME: high product */
    1091         160 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
    1092         160 :     z += 2;
    1093             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
    1094         160 :     for (i = nold; i < lz; i++) if (signe(gel(z,i))) break;
    1095         160 :     nold = nnew;
    1096         160 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
    1097             : 
    1098             :     /* z + i represents (x*q - 1) / t^i */
    1099          80 :     lz = ZX_lgrenormalizespec (z+i, lz-i);
    1100          80 :     z = FpX_mulspec(x, z+i, p, lx, lz); /* FIXME: low product */
    1101          80 :     lz = lgpol(z); z += 2;
    1102          80 :     if (lz > lnew-i) lz = ZX_lgrenormalizespec(z, lnew-i);
    1103             : 
    1104          80 :     lx = lz+ i;
    1105          80 :     y  = x + i; /* x -= z * t^i, in place */
    1106          80 :     for (i = 0; i < lz; i++) gel(y,i) = Fp_neg(gel(z,i), p);
    1107             :   }
    1108          20 :   x -= 2; setlg(x, lx + 2); x[1] = T[1];
    1109          20 :   return gerepilecopy(av, x);
    1110             : }
    1111             : 
    1112             : /* 1/polrecip(T)+O(x^(deg(T)-1)) */
    1113             : GEN
    1114         795 : FpX_invBarrett(GEN T, GEN p)
    1115             : {
    1116         795 :   pari_sp ltop = avma;
    1117         795 :   long l = lg(T);
    1118             :   GEN r;
    1119         795 :   if (l<5) return pol_0(varn(T));
    1120         779 :   if (l<=FpX_INVBARRETT_LIMIT)
    1121             :   {
    1122         759 :     GEN c = gel(T,l-1), ci=gen_1;
    1123         759 :     if (!equali1(c))
    1124             :     {
    1125           0 :       ci = Fp_inv(c, p);
    1126           0 :       T = FpX_Fp_mul(T, ci, p);
    1127           0 :       r = FpX_invBarrett_basecase(T, p);
    1128           0 :       r = FpX_Fp_mul(r, ci, p);
    1129             :     } else
    1130         759 :       r = FpX_invBarrett_basecase(T, p);
    1131             :   }
    1132             :   else
    1133          20 :     r = FpX_invBarrett_Newton(T, p);
    1134         779 :   return gerepileupto(ltop, r);
    1135             : }
    1136             : 
    1137             : GEN
    1138      207165 : FpX_get_red(GEN T, GEN p)
    1139             : {
    1140      207165 :   if (typ(T)==t_POL && lg(T)>FpX_BARRETT_LIMIT)
    1141         657 :     retmkvec2(FpX_invBarrett(T,p),T);
    1142      206508 :   return T;
    1143             : }
    1144             : 
    1145             : /* Compute x mod T where 2 <= degpol(T) <= l+1 <= 2*(degpol(T)-1)
    1146             :  * and mg is the Barrett inverse of T. */
    1147             : static GEN
    1148       50544 : FpX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN T, GEN p, GEN *pr)
    1149             : {
    1150             :   GEN q, r;
    1151       50544 :   long lt = degpol(T); /*We discard the leading term*/
    1152             :   long ld, lm, lT, lmg;
    1153       50544 :   ld = l-lt;
    1154       50544 :   lm = minss(ld, lgpol(mg));
    1155       50544 :   lT  = ZX_lgrenormalizespec(T+2,lt);
    1156       50544 :   lmg = ZX_lgrenormalizespec(mg+2,lm);
    1157       50544 :   q = FpX_recipspec(x+lt,ld,ld);              /* q = rec(x)     lq<=ld*/
    1158       50544 :   q = FpX_mulspec(q+2,mg+2,p,lgpol(q),lmg);    /* q = rec(x) * mg lq<=ld+lm*/
    1159       50544 :   q = FpX_recipspec(q+2,minss(ld,lgpol(q)),ld);/* q = rec (rec(x) * mg) lq<=ld*/
    1160       50544 :   if (!pr) return q;
    1161       50544 :   r = FpX_mulspec(q+2,T+2,p,lgpol(q),lT);      /* r = q*pol        lr<=ld+lt*/
    1162       50544 :   r = FpX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - r   lr<=lt */
    1163       50544 :   if (pr == ONLY_REM) return r;
    1164         250 :   *pr = r; return q;
    1165             : }
    1166             : 
    1167             : static GEN
    1168       50358 : FpX_divrem_Barrett_noGC(GEN x, GEN mg, GEN T, GEN p, GEN *pr)
    1169             : {
    1170       50358 :   GEN q = NULL, r = FpX_red(x, p);
    1171       50358 :   long l = lgpol(r), lt = degpol(T), lm = 2*lt-1;
    1172             :   long i;
    1173       50358 :   if (l <= lt)
    1174             :   {
    1175           0 :     if (pr == ONLY_REM) return r;
    1176           0 :     if (pr == ONLY_DIVIDES) return signe(x)? NULL: pol_0(varn(x));
    1177           0 :     if (pr) *pr = r;
    1178           0 :     return pol_0(varn(T));
    1179             :   }
    1180       50358 :   if (lt <= 1)
    1181          16 :     return FpX_divrem_basecase(r,T,p,pr);
    1182       50342 :   if (pr != ONLY_REM && l>lm)
    1183             :   {
    1184          48 :     q = cgetg(l-lt+2, t_POL);
    1185          48 :     for (i=0;i<l-lt;i++) gel(q+2,i) = gen_0;
    1186             :   }
    1187      100886 :   while (l>lm)
    1188             :   {
    1189         202 :     GEN zr, zq = FpX_divrem_Barrettspec(r+2+l-lm,lm,mg,T,p,&zr);
    1190         202 :     long lz = lgpol(zr);
    1191         202 :     if (pr != ONLY_REM)
    1192             :     {
    1193         110 :       long lq = lgpol(zq);
    1194         110 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1195             :     }
    1196         202 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1197         202 :     l = l-lm+lz;
    1198             :   }
    1199       50342 :   if (pr != ONLY_REM)
    1200             :   {
    1201          48 :     if (l > lt)
    1202             :     {
    1203          48 :       GEN zq = FpX_divrem_Barrettspec(r+2,l,mg,T,p,&r);
    1204          48 :       if (!q) q = zq;
    1205             :       else
    1206             :       {
    1207          48 :         long lq = lgpol(zq);
    1208          48 :         for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
    1209             :       }
    1210             :     }
    1211             :     else
    1212           0 :       r = FpX_renormalize(r, l+2);
    1213             :   }
    1214             :   else
    1215             :   {
    1216       50294 :     if (l > lt)
    1217       50294 :       r = FpX_divrem_Barrettspec(r+2, l, mg, T, p, ONLY_REM);
    1218             :     else
    1219           0 :       r = FpX_renormalize(r, l+2);
    1220       50294 :     r[1] = x[1]; return FpX_renormalize(r, lg(r));
    1221             :   }
    1222          48 :   if (pr) { r[1] = x[1]; r = FpX_renormalize(r, lg(r)); }
    1223          48 :   q[1] = x[1]; q = FpX_renormalize(q, lg(q));
    1224          48 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1225          48 :   if (pr) *pr = r;
    1226          48 :   return q;
    1227             : }
    1228             : 
    1229             : GEN
    1230     2600724 : FpX_divrem(GEN x, GEN T, GEN p, GEN *pr)
    1231             : {
    1232     2600724 :   GEN B, y = get_FpX_red(T, &B);
    1233     2600724 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1234     2600724 :   if (pr==ONLY_REM) return FpX_rem(x, y, p);
    1235     2600724 :   if (!B && d+3 < FpX_DIVREM_BARRETT_LIMIT)
    1236     2600194 :     return FpX_divrem_basecase(x,y,p,pr);
    1237         530 :   else if (lgefint(p)==3)
    1238             :   {
    1239         470 :     pari_sp av = avma;
    1240         470 :     ulong pp = to_Flxq(&x, &T, p);
    1241         470 :     GEN z = Flx_divrem(x, T, pp, pr);
    1242         470 :     if (!z) return NULL;
    1243         470 :     if (!pr || pr == ONLY_DIVIDES)
    1244           4 :       return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1245         466 :     z = Flx_to_ZX(z);
    1246         466 :     *pr = Flx_to_ZX(*pr);
    1247         466 :     gerepileall(av, 2, &z, pr);
    1248         466 :     return z;
    1249             :   } else
    1250             :   {
    1251          60 :     pari_sp av=avma;
    1252          60 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1253          60 :     GEN q1 = FpX_divrem_Barrett_noGC(x,mg,y,p,pr);
    1254          60 :     if (!q1) {avma=av; return NULL;}
    1255          60 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q1);
    1256          60 :     gerepileall(av,2,&q1,pr);
    1257          60 :     return q1;
    1258             :   }
    1259             : }
    1260             : 
    1261             : GEN
    1262    43082475 : FpX_rem(GEN x, GEN T, GEN p)
    1263             : {
    1264    43082475 :   GEN B, y = get_FpX_red(T, &B);
    1265    43082475 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1266    43082475 :   if (d < 0) return FpX_red(x,p);
    1267    41077420 :   if (!B && d+3 < FpX_REM_BARRETT_LIMIT)
    1268    41023656 :     return FpX_divrem_basecase(x,y,p,ONLY_REM);
    1269       53764 :   else if (lgefint(p)==3)
    1270             :   {
    1271        3466 :     pari_sp av = avma;
    1272        3466 :     ulong pp = to_Flxq(&x, &T, p);
    1273        3466 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, Flx_rem(x, T, pp)));
    1274             :   } else
    1275             :   {
    1276       50298 :     pari_sp av = avma;
    1277       50298 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1278       50298 :     return gerepileupto(av, FpX_divrem_Barrett_noGC(x, mg, y, p, ONLY_REM));
    1279             :   }
    1280             : }
    1281             : 
    1282             : static GEN
    1283        6945 : deg2pol_shallow(GEN x2, GEN x1, GEN x0, long v)
    1284             : {
    1285        6945 :   GEN x = cgetg(5,t_POL);
    1286        6945 :   x[1] = evalsigne(1) | evalvarn(v);
    1287        6945 :   gel(x,2) = x0;
    1288        6945 :   gel(x,3) = x1;
    1289        6945 :   gel(x,4) = x2;
    1290        6945 :   return normalizepol_lg(x,5);
    1291             : }
    1292             : 
    1293             : static GEN
    1294        1540 : FpV_producttree(GEN xa, GEN s, GEN p, long vs)
    1295             : {
    1296        1540 :   long n = lg(xa)-1;
    1297        1540 :   long m = n==1 ? 1: expu(n-1)+1;
    1298        1540 :   long i, j, k, ls = lg(s);
    1299        1540 :   GEN T = cgetg(m+1, t_VEC);
    1300        1540 :   GEN t = cgetg(ls, t_VEC);
    1301       11020 :   for (j=1, k=1; j<ls; k+=s[j++])
    1302       18960 :     gel(t, j) = s[j] == 1 ?
    1303       16425 :              deg1pol(gen_1, Fp_neg(gel(xa,k), p), vs):
    1304       27780 :              deg2pol_shallow(gen_1,
    1305       13890 :                Fp_neg(Fp_add(gel(xa,k), gel(xa,k+1), p), p),
    1306       13890 :                Fp_mul(gel(xa,k), gel(xa,k+1), p), vs);
    1307        1540 :   gel(T,1) = t;
    1308        4375 :   for (i=2; i<=m; i++)
    1309             :   {
    1310        2835 :     GEN u = gel(T, i-1);
    1311        2835 :     long n = lg(u)-1;
    1312        2835 :     GEN t = cgetg(((n+1)>>1)+1, t_VEC);
    1313       10775 :     for (j=1, k=1; k<n; j++, k+=2)
    1314        7940 :       gel(t, j) = FpX_mul(gel(u, k), gel(u, k+1), p);
    1315        2835 :     gel(T, i) = t;
    1316             :   }
    1317        1540 :   return T;
    1318             : }
    1319             : 
    1320             : static GEN
    1321        1540 : FpX_FpV_multieval_tree(GEN P, GEN xa, GEN T, GEN p)
    1322             : {
    1323        1540 :   pari_sp av = avma;
    1324             :   long i,j,k;
    1325        1540 :   long m = lg(T)-1;
    1326             :   GEN t;
    1327        1540 :   GEN Tp = cgetg(m+1, t_VEC);
    1328        1540 :   gel(Tp, m) = mkvec(P);
    1329        4375 :   for (i=m-1; i>=1; i--)
    1330             :   {
    1331        2835 :     GEN u = gel(T, i);
    1332        2835 :     GEN v = gel(Tp, i+1);
    1333        2835 :     long n = lg(u)-1;
    1334        2835 :     t = cgetg(n+1, t_VEC);
    1335       10775 :     for (j=1, k=1; k<n; j++, k+=2)
    1336             :     {
    1337        7940 :       gel(t, k)   = FpX_rem(gel(v, j), gel(u, k), p);
    1338        7940 :       gel(t, k+1) = FpX_rem(gel(v, j), gel(u, k+1), p);
    1339             :     }
    1340        2835 :     gel(Tp, i) = t;
    1341             :   }
    1342             :   {
    1343        1540 :     GEN R = cgetg(lg(xa), t_VEC);
    1344        1540 :     GEN u = gel(T, i+1);
    1345        1540 :     GEN v = gel(Tp, i+1);
    1346        1540 :     long n = lg(u)-1;
    1347       11020 :     for (j=1, k=1; j<=n; j++)
    1348             :     {
    1349        9480 :       long c, d = degpol(gel(u,j));
    1350       25905 :       for (c=1; c<=d; c++, k++)
    1351       16425 :         gel(R,k) = FpX_eval(gel(v, j), gel(xa,k), p);
    1352             :     }
    1353        1540 :     return gerepileupto(av, R);
    1354             :   }
    1355             : }
    1356             : 
    1357             : static GEN
    1358           0 : FpVV_polint_tree(GEN T, GEN R, GEN s, GEN xa, GEN ya, GEN p, long vs)
    1359             : {
    1360           0 :   pari_sp av = avma;
    1361           0 :   long m = lg(T)-1;
    1362           0 :   long i, j, k, ls = lg(s);
    1363           0 :   GEN Tp = cgetg(m+1, t_VEC);
    1364           0 :   GEN t = cgetg(ls, t_VEC);
    1365           0 :   for (j=1, k=1; j<ls; k+=s[j++])
    1366           0 :     if (s[j]==2)
    1367             :     {
    1368           0 :       GEN a = Fp_mul(gel(ya,k), gel(R,k), p);
    1369           0 :       GEN b = Fp_mul(gel(ya,k+1), gel(R,k+1), p);
    1370           0 :       gel(t, j) = deg1pol(Fp_add(a, b, p),
    1371           0 :               Fp_neg(Fp_add(Fp_mul(gel(xa,k), b, p ),
    1372           0 :               Fp_mul(gel(xa,k+1), a, p), p), p), vs);
    1373             :     }
    1374             :     else
    1375           0 :       gel(t, j) = scalarpol(Fp_mul(gel(ya,k), gel(R,k), p), vs);
    1376           0 :   gel(Tp, 1) = t;
    1377           0 :   for (i=2; i<=m; i++)
    1378             :   {
    1379           0 :     GEN u = gel(T, i-1);
    1380           0 :     GEN t = cgetg(lg(gel(T,i)), t_VEC);
    1381           0 :     GEN v = gel(Tp, i-1);
    1382           0 :     long n = lg(v)-1;
    1383           0 :     for (j=1, k=1; k<n; j++, k+=2)
    1384           0 :       gel(t, j) = FpX_add(ZX_mul(gel(u, k), gel(v, k+1)),
    1385           0 :                           ZX_mul(gel(u, k+1), gel(v, k)), p);
    1386           0 :     gel(Tp, i) = t;
    1387             :   }
    1388           0 :   return gerepilecopy(av, gmael(Tp,m,1));
    1389             : }
    1390             : 
    1391             : GEN
    1392           0 : FpX_FpV_multieval(GEN P, GEN xa, GEN p)
    1393             : {
    1394           0 :   pari_sp av = avma;
    1395           0 :   GEN s = producttree_scheme(lg(xa)-1);
    1396           0 :   GEN T = FpV_producttree(xa, s, p, varn(P));
    1397           0 :   return gerepileupto(av, FpX_FpV_multieval_tree(P, xa, T, p));
    1398             : }
    1399             : 
    1400             : GEN
    1401           0 : FpV_polint(GEN xa, GEN ya, GEN p, long vs)
    1402             : {
    1403           0 :   pari_sp av = avma;
    1404           0 :   GEN s = producttree_scheme(lg(xa)-1);
    1405           0 :   GEN T = FpV_producttree(xa, s, p, vs);
    1406           0 :   long m = lg(T)-1;
    1407           0 :   GEN P = FpX_deriv(gmael(T, m, 1), p);
    1408           0 :   GEN R = FpV_inv(FpX_FpV_multieval_tree(P, xa, T, p), p);
    1409           0 :   return gerepileupto(av, FpVV_polint_tree(T, R, s, xa, ya, p, vs));
    1410             : }
    1411             : 
    1412             : GEN
    1413           0 : FpV_FpM_polint(GEN xa, GEN ya, GEN p, long vs)
    1414             : {
    1415           0 :   pari_sp av = avma;
    1416           0 :   GEN s = producttree_scheme(lg(xa)-1);
    1417           0 :   GEN T = FpV_producttree(xa, s, p, vs);
    1418           0 :   long i, m = lg(T)-1, l = lg(ya)-1;
    1419           0 :   GEN P = FpX_deriv(gmael(T, m, 1), p);
    1420           0 :   GEN R = FpV_inv(FpX_FpV_multieval_tree(P, xa, T, p), p);
    1421           0 :   GEN M = cgetg(l+1, t_VEC);
    1422           0 :   for (i=1; i<=l; i++)
    1423           0 :     gel(M,i) = FpVV_polint_tree(T, R, s, xa, gel(ya,i), p, vs);
    1424           0 :   return gerepileupto(av, M);
    1425             : }
    1426             : 
    1427             : GEN
    1428        1540 : FpV_invVandermonde(GEN L, GEN den, GEN p)
    1429             : {
    1430        1540 :   pari_sp av = avma;
    1431        1540 :   long i, n = lg(L);
    1432             :   GEN M, R;
    1433        1540 :   GEN s = producttree_scheme(n-1);
    1434        1540 :   GEN tree = FpV_producttree(L, s, p, 0);
    1435        1540 :   long m = lg(tree)-1;
    1436        1540 :   GEN T = gmael(tree, m, 1);
    1437        1540 :   R = FpV_inv(FpX_FpV_multieval_tree(FpX_deriv(T, p), L, tree, p), p);
    1438        1540 :   if (den) R = FpC_Fp_mul(R, den, p);
    1439        1540 :   M = cgetg(n, t_MAT);
    1440       17965 :   for (i = 1; i < n; i++)
    1441             :   {
    1442       16425 :     GEN P = FpX_Fp_mul(FpX_div_by_X_x(T, gel(L,i), p, NULL), gel(R,i), p);
    1443       16425 :     gel(M,i) = RgX_to_RgC(P, n-1);
    1444             :   }
    1445        1540 :   return gerepilecopy(av, M);
    1446             : }
    1447             : 
    1448             : /***********************************************************************/
    1449             : /**                                                                   **/
    1450             : /**                              FpXQ                                 **/
    1451             : /**                                                                   **/
    1452             : /***********************************************************************/
    1453             : 
    1454             : /* FpXQ are elements of Fp[X]/(T), represented by FpX*/
    1455             : 
    1456             : GEN
    1457     1918999 : FpXQ_red(GEN x, GEN T, GEN p)
    1458             : {
    1459     1918999 :   GEN z = FpX_red(x,p);
    1460     1918999 :   return FpX_rem(z, T,p);
    1461             : }
    1462             : 
    1463             : GEN
    1464    28309062 : FpXQ_mul(GEN x,GEN y,GEN T,GEN p)
    1465             : {
    1466    28309062 :   GEN z = FpX_mul(x,y,p);
    1467    28309062 :   return FpX_rem(z, T, p);
    1468             : }
    1469             : 
    1470             : GEN
    1471     2032185 : FpXQ_sqr(GEN x, GEN T, GEN p)
    1472             : {
    1473     2032185 :   GEN z = FpX_sqr(x,p);
    1474     2032185 :   return FpX_rem(z, T, p);
    1475             : }
    1476             : 
    1477             : /* Inverse of x in Z/pZ[X]/(pol) or NULL if inverse doesn't exist
    1478             :  * return lift(1 / (x mod (p,pol))) */
    1479             : GEN
    1480      206002 : FpXQ_invsafe(GEN x, GEN y, GEN p)
    1481             : {
    1482      206002 :   GEN V, z = FpX_extgcd(get_FpX_mod(y), x, p, NULL, &V);
    1483      206002 :   if (degpol(z)) return NULL;
    1484      206002 :   z = Fp_invsafe(gel(z,2), p);
    1485      206002 :   if (!z) return NULL;
    1486      206002 :   return FpX_Fp_mul(V, z, p);
    1487             : }
    1488             : 
    1489             : GEN
    1490      205987 : FpXQ_inv(GEN x,GEN T,GEN p)
    1491             : {
    1492      205987 :   pari_sp av = avma;
    1493      205987 :   GEN U = FpXQ_invsafe(x, T, p);
    1494      205987 :   if (!U) pari_err_INV("FpXQ_inv",x);
    1495      205987 :   return gerepileupto(av, U);
    1496             : }
    1497             : 
    1498             : GEN
    1499      161360 : FpXQ_div(GEN x,GEN y,GEN T,GEN p)
    1500             : {
    1501      161360 :   pari_sp av = avma;
    1502      161360 :   return gerepileupto(av, FpXQ_mul(x,FpXQ_inv(y,T,p),T,p));
    1503             : }
    1504             : 
    1505             : static GEN
    1506      760595 : _FpXQ_add(void *data, GEN x, GEN y)
    1507             : {
    1508             :   (void) data;
    1509      760595 :   return ZX_add(x, y);
    1510             : }
    1511             : static GEN
    1512       41110 : _FpXQ_sub(void *data, GEN x, GEN y)
    1513             : {
    1514             :   (void) data;
    1515       41110 :   return ZX_sub(x, y);
    1516             : }
    1517             : static GEN
    1518      852980 : _FpXQ_cmul(void *data, GEN P, long a, GEN x)
    1519             : {
    1520             :   (void) data;
    1521      852980 :   return ZX_Z_mul(x, gel(P,a+2));
    1522             : }
    1523             : static GEN
    1524     1700460 : _FpXQ_sqr(void *data, GEN x)
    1525             : {
    1526     1700460 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1527     1700460 :   return FpXQ_sqr(x, D->T, D->p);
    1528             : }
    1529             : static GEN
    1530      433160 : _FpXQ_mul(void *data, GEN x, GEN y)
    1531             : {
    1532      433160 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1533      433160 :   return FpXQ_mul(x,y, D->T, D->p);
    1534             : }
    1535             : static GEN
    1536        2515 : _FpXQ_zero(void *data)
    1537             : {
    1538        2515 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1539        2515 :   return pol_0(get_FpX_var(D->T));
    1540             : }
    1541             : static GEN
    1542      219920 : _FpXQ_one(void *data)
    1543             : {
    1544      219920 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1545      219920 :   return pol_1(get_FpX_var(D->T));
    1546             : }
    1547             : static GEN
    1548      255060 : _FpXQ_red(void *data, GEN x)
    1549             : {
    1550      255060 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1551      255060 :   return FpX_red(x,D->p);
    1552             : }
    1553             : 
    1554             : static struct bb_algebra FpXQ_algebra = { _FpXQ_red, _FpXQ_add, _FpXQ_sub,
    1555             :        _FpXQ_mul, _FpXQ_sqr, _FpXQ_one, _FpXQ_zero };
    1556             : 
    1557             : const struct bb_algebra *
    1558        9685 : get_FpXQ_algebra(void **E, GEN T, GEN p)
    1559             : {
    1560        9685 :   GEN z = new_chunk(sizeof(struct _FpXQ));
    1561        9685 :   struct _FpXQ *e = (struct _FpXQ *) z;
    1562        9685 :   e->T = FpX_get_red(T, p);
    1563        9685 :   e->p  = p; *E = (void*)e;
    1564        9685 :   return &FpXQ_algebra;
    1565             : }
    1566             : 
    1567             : static struct bb_algebra FpX_algebra = { _FpXQ_red, _FpXQ_add, _FpXQ_sub,
    1568             :        _FpX_mul, _FpX_sqr, _FpXQ_one, _FpXQ_zero };
    1569             : 
    1570             : const struct bb_algebra *
    1571           0 : get_FpX_algebra(void **E, GEN p, long v)
    1572             : {
    1573           0 :   GEN z = new_chunk(sizeof(struct _FpXQ));
    1574           0 :   struct _FpXQ *e = (struct _FpXQ *) z;
    1575           0 :   e->T = pol_x(v);
    1576           0 :   e->p  = p; *E = (void*)e;
    1577           0 :   return &FpX_algebra;
    1578             : }
    1579             : 
    1580             : /* x,pol in Z[X], p in Z, n in Z, compute lift(x^n mod (p, pol)) */
    1581             : GEN
    1582      277190 : FpXQ_pow(GEN x, GEN n, GEN T, GEN p)
    1583             : {
    1584             :   struct _FpXQ D;
    1585             :   pari_sp av;
    1586      277190 :   long s = signe(n);
    1587             :   GEN y;
    1588      277190 :   if (!s) return pol_1(varn(x));
    1589      275995 :   if (is_pm1(n)) /* +/- 1 */
    1590        6485 :     return (s < 0)? FpXQ_inv(x,T,p): FpXQ_red(x,T,p);
    1591      269510 :   av = avma;
    1592      269510 :   if (!is_bigint(p))
    1593             :   {
    1594      245865 :     ulong pp = to_Flxq(&x, &T, p);
    1595      245865 :     y = Flxq_pow(x, n, T, pp);
    1596      245865 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1597             :   }
    1598       23645 :   if (s < 0) x = FpXQ_inv(x,T,p);
    1599       23645 :   D.p = p; D.T = FpX_get_red(T,p);
    1600       23645 :   y = gen_pow(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1601       23645 :   return gerepileupto(av, y);
    1602             : }
    1603             : 
    1604             : GEN /*Assume n is very small*/
    1605       45020 : FpXQ_powu(GEN x, ulong n, GEN T, GEN p)
    1606             : {
    1607             :   struct _FpXQ D;
    1608             :   pari_sp av;
    1609             :   GEN y;
    1610       45020 :   if (!n) return pol_1(varn(x));
    1611       45020 :   if (n==1) return FpXQ_red(x,T,p);
    1612       23415 :   av = avma;
    1613       23415 :   if (!is_bigint(p))
    1614             :   {
    1615       23210 :     ulong pp = to_Flxq(&x, &T, p);
    1616       23210 :     y = Flxq_powu(x, n, T, pp);
    1617       23210 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1618             :   }
    1619         205 :   D.T = FpX_get_red(T, p); D.p = p;
    1620         205 :   y = gen_powu(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1621         205 :   return gerepileupto(av, y);
    1622             : }
    1623             : 
    1624             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    1625             : GEN
    1626      130510 : FpXQ_powers(GEN x, long l, GEN T, GEN p)
    1627             : {
    1628             :   struct _FpXQ D;
    1629             :   int use_sqr;
    1630      130510 :   if (l>2 && lgefint(p) == 3) {
    1631      109465 :     pari_sp av = avma;
    1632      109465 :     ulong pp = to_Flxq(&x, &T, p);
    1633      109465 :     GEN z = FlxV_to_ZXV(Flxq_powers(x, l, T, pp));
    1634      109465 :     return gerepileupto(av, z);
    1635             :   }
    1636       21045 :   use_sqr = 2*degpol(x)>=get_FpX_degree(T);
    1637       21045 :   D.T = FpX_get_red(T,p); D.p = p;
    1638       21045 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul,&_FpXQ_one);
    1639             : }
    1640             : 
    1641             : GEN
    1642         395 : FpXQ_matrix_pow(GEN y, long n, long m, GEN P, GEN l)
    1643             : {
    1644         395 :   return RgXV_to_RgM(FpXQ_powers(y,m-1,P,l),n);
    1645             : }
    1646             : 
    1647             : GEN
    1648      114455 : FpX_Frobenius(GEN T, GEN p)
    1649             : {
    1650      114455 :   return FpXQ_pow(pol_x(get_FpX_var(T)), p, T, p);
    1651             : }
    1652             : 
    1653             : GEN
    1654         245 : FpX_matFrobenius(GEN T, GEN p)
    1655             : {
    1656         245 :   long n = get_FpX_degree(T);
    1657         245 :   return FpXQ_matrix_pow(FpX_Frobenius(T, p), n, n, T, p);
    1658             : }
    1659             : 
    1660             : GEN
    1661       91050 : FpX_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1662             : {
    1663             :   struct _FpXQ D;
    1664       91050 :   D.T = FpX_get_red(T,p); D.p = p;
    1665       91050 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1666             : }
    1667             : 
    1668             : GEN
    1669      121445 : FpX_FpXQ_eval(GEN Q, GEN x, GEN T, GEN p)
    1670             : {
    1671             :   struct _FpXQ D;
    1672             :   int use_sqr;
    1673      121445 :   if (lgefint(p) == 3)
    1674             :   {
    1675      119830 :     pari_sp av = avma;
    1676      119830 :     ulong pp = to_Flxq(&x, &T, p);
    1677      119830 :     GEN z = Flx_Flxq_eval(ZX_to_Flx(Q, pp), x, T, pp);
    1678      119830 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1679             :   }
    1680        1615 :   use_sqr = 2*degpol(x) >= get_FpX_degree(T);
    1681        1615 :   D.T = FpX_get_red(T,p); D.p = p;
    1682        1615 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1683             : }
    1684             : 
    1685             : GEN
    1686         610 : FpXC_FpXQV_eval(GEN P, GEN x, GEN T, GEN p)
    1687             : {
    1688         610 :   long i, l = lg(P);
    1689         610 :   GEN res = cgetg(l, t_COL);
    1690        2630 :   for (i=1; i<l; i++)
    1691        2020 :     gel(res,i) = FpX_FpXQV_eval(gel(P,i), x, T, p);
    1692         610 :   return res;
    1693             : }
    1694             : 
    1695             : GEN
    1696         220 : FpXM_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1697             : {
    1698         220 :   long i, l = lg(Q);
    1699         220 :   GEN y = cgetg(l, t_MAT);
    1700         830 :   for (i=1; i<l; i++)
    1701         610 :     gel(y,i) = FpXC_FpXQV_eval(gel(Q,i), x, T, p);
    1702         220 :   return y;
    1703             : }
    1704             : 
    1705             : GEN
    1706         505 : FpXQ_autpowers(GEN aut, long f, GEN T, GEN p)
    1707             : {
    1708         505 :   pari_sp av = avma;
    1709         505 :   long n = get_FpX_degree(T);
    1710         505 :   long i, nautpow = brent_kung_optpow(n-1,f-2,1);
    1711         505 :   long v = get_FpX_var(T);
    1712             :   GEN autpow, V;
    1713         505 :   T = FpX_get_red(T, p);
    1714         505 :   autpow = FpXQ_powers(aut, nautpow,T,p);
    1715         505 :   V = cgetg(f + 2, t_VEC);
    1716         505 :   gel(V,1) = pol_x(v); if (f==0) return gerepileupto(av, V);
    1717         505 :   gel(V,2) = gcopy(aut);
    1718        2525 :   for (i = 3; i <= f+1; i++)
    1719        2020 :     gel(V,i) = FpX_FpXQV_eval(gel(V,i-1),autpow,T,p);
    1720         505 :   return gerepileupto(av, V);
    1721             : }
    1722             : 
    1723             : static GEN
    1724         385 : FpXQ_autpow_sqr(void *E, GEN x)
    1725             : {
    1726         385 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1727         385 :   return FpX_FpXQ_eval(x, x, D->T, D->p);
    1728             : }
    1729             : 
    1730             : static GEN
    1731           5 : FpXQ_autpow_mul(void *E, GEN x, GEN y)
    1732             : {
    1733           5 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1734           5 :   return FpX_FpXQ_eval(x, y, D->T, D->p);
    1735             : }
    1736             : 
    1737             : GEN
    1738         365 : FpXQ_autpow(GEN x, ulong n, GEN T, GEN p)
    1739             : {
    1740             :   struct _FpXQ D;
    1741         365 :   D.T = FpX_get_red(T, p); D.p = p;
    1742         365 :   if (n==0) return pol_x(varn(x));
    1743         365 :   if (n==1) return ZX_copy(x);
    1744         365 :   return gen_powu(x,n,(void*)&D,FpXQ_autpow_sqr,FpXQ_autpow_mul);
    1745             : }
    1746             : 
    1747             : static GEN
    1748           5 : FpXQ_auttrace_mul(void *E, GEN x, GEN y)
    1749             : {
    1750           5 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1751           5 :   GEN T = D->T, p = D->p;
    1752           5 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1753           5 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1754           5 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1755           5 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1756           5 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1757           5 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1758           5 :   GEN a3 = FpX_add(a1, aphi, p);
    1759           5 :   return mkvec2(phi3, a3);
    1760             : }
    1761             : 
    1762             : static GEN
    1763           5 : FpXQ_auttrace_sqr(void *E, GEN x)
    1764           5 : { return FpXQ_auttrace_mul(E, x, x); }
    1765             : 
    1766             : GEN
    1767          10 : FpXQ_auttrace(GEN x, ulong n, GEN T, GEN p)
    1768             : {
    1769             :   struct _FpXQ D;
    1770          10 :   D.T = FpX_get_red(T, p); D.p = p;
    1771          10 :   return gen_powu(x,n,(void*)&D,FpXQ_auttrace_sqr,FpXQ_auttrace_mul);
    1772             : }
    1773             : 
    1774             : static GEN
    1775        1075 : FpXQ_autsum_mul(void *E, GEN x, GEN y)
    1776             : {
    1777        1075 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1778        1075 :   GEN T = D->T, p = D->p;
    1779        1075 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1780        1075 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1781        1075 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1782        1075 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1783        1075 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1784        1075 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1785        1075 :   GEN a3 = FpXQ_mul(a1, aphi, T, p);
    1786        1075 :   return mkvec2(phi3, a3);
    1787             : }
    1788             : static GEN
    1789         560 : FpXQ_autsum_sqr(void *E, GEN x)
    1790         560 : { return FpXQ_autsum_mul(E, x, x); }
    1791             : 
    1792             : GEN
    1793         550 : FpXQ_autsum(GEN x, ulong n, GEN T, GEN p)
    1794             : {
    1795             :   struct _FpXQ D;
    1796         550 :   D.T = FpX_get_red(T, p); D.p = p;
    1797         550 :   return gen_powu(x,n,(void*)&D,FpXQ_autsum_sqr,FpXQ_autsum_mul);
    1798             : }
    1799             : 
    1800             : static GEN
    1801         220 : FpXQM_autsum_mul(void *E, GEN x, GEN y)
    1802             : {
    1803         220 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1804         220 :   GEN T = D->T, p = D->p;
    1805         220 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1806         220 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1807         220 :   long g = lg(a2)-1, dT = get_FpX_degree(T);
    1808         220 :   ulong d = brent_kung_optpow(dT-1, g*g+1, 1);
    1809         220 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1810         220 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1811         220 :   GEN aphi = FpXM_FpXQV_eval(a2, V1, T, p);
    1812         220 :   GEN a3 = FqM_mul(a1, aphi, T, p);
    1813         220 :   return mkvec2(phi3, a3);
    1814             : }
    1815             : static GEN
    1816         150 : FpXQM_autsum_sqr(void *E, GEN x)
    1817         150 : { return FpXQM_autsum_mul(E, x, x); }
    1818             : 
    1819             : GEN
    1820         100 : FpXQM_autsum(GEN x, ulong n, GEN T, GEN p)
    1821             : {
    1822             :   struct _FpXQ D;
    1823         100 :   D.T = FpX_get_red(T, p); D.p = p;
    1824         100 :   return gen_powu(x, n, (void*)&D, FpXQM_autsum_sqr, FpXQM_autsum_mul);
    1825             : }
    1826             : 
    1827             : static long
    1828        3630 : bounded_order(GEN p, GEN b, long k)
    1829             : {
    1830             :   long i;
    1831        3630 :   GEN a=modii(p,b);
    1832        7305 :   for(i=1;i<k;i++)
    1833             :   {
    1834        4535 :     if (equali1(a))
    1835         860 :       return i;
    1836        3675 :     a = Fp_mul(a,p,b);
    1837             :   }
    1838        2770 :   return 0;
    1839             : }
    1840             : 
    1841             : /*
    1842             :   n = (p^d-a)\b
    1843             :   b = bb*p^vb
    1844             :   p^k = 1 [bb]
    1845             :   d = m*k+r+vb
    1846             :   u = (p^k-1)/bb;
    1847             :   v = (p^(r+vb)-a)/b;
    1848             :   w = (p^(m*k)-1)/(p^k-1)
    1849             :   n = p^r*w*u+v
    1850             :   w*u = p^vb*(p^(m*k)-1)/b
    1851             :   n = p^(r+vb)*(p^(m*k)-1)/b+(p^(r+vb)-a)/b
    1852             : */
    1853             : 
    1854             : static GEN
    1855       16245 : FpXQ_pow_Frobenius(GEN x, GEN n, GEN aut, GEN T, GEN p)
    1856             : {
    1857       16245 :   pari_sp av=avma;
    1858       16245 :   long d = get_FpX_degree(T);
    1859       16245 :   GEN an = absi(n), z, q;
    1860       16245 :   if (cmpii(an,p)<0 || cmpis(an,d)<=0)
    1861       12615 :     return FpXQ_pow(x, n, T, p);
    1862        3630 :   q = powiu(p, d);
    1863        3630 :   if (dvdii(q, n))
    1864             :   {
    1865           0 :     long vn = logint(an,p);
    1866           0 :     GEN autvn = vn==1 ? aut: FpXQ_autpow(aut,vn,T,p);
    1867           0 :     z = FpX_FpXQ_eval(x,autvn,T,p);
    1868             :   } else
    1869             :   {
    1870        3630 :     GEN b = diviiround(q, an), a = subii(q, mulii(an,b));
    1871             :     GEN bb, u, v, autk;
    1872        3630 :     long vb = Z_pvalrem(b,p,&bb);
    1873        3630 :     long m, r, k = is_pm1(bb) ? 1 : bounded_order(p,bb,d);
    1874        3630 :     if (!k || d-vb<k) return FpXQ_pow(x,n, T, p);
    1875         860 :     m = (d-vb)/k; r = (d-vb)%k;
    1876         860 :     u = diviiexact(subiu(powiu(p,k),1),bb);
    1877         860 :     v = diviiexact(subii(powiu(p,r+vb),a),b);
    1878         860 :     autk = k==1 ? aut: FpXQ_autpow(aut,k,T,p);
    1879         860 :     if (r)
    1880             :     {
    1881         315 :       GEN autr = r==1 ? aut: FpXQ_autpow(aut,r,T,p);
    1882         315 :       z = FpX_FpXQ_eval(x,autr,T,p);
    1883         545 :     } else z = x;
    1884         860 :     if (m > 1) z = gel(FpXQ_autsum(mkvec2(autk, z), m, T, p), 2);
    1885         860 :     if (!is_pm1(u)) z = FpXQ_pow(z, u, T, p);
    1886         860 :     if (signe(v)) z = FpXQ_mul(z, FpXQ_pow(x, v, T, p), T, p);
    1887             :   }
    1888         860 :   return gerepileupto(av,signe(n)>0 ? z : FpXQ_inv(z,T,p));
    1889             : }
    1890             : 
    1891             : /* assume T irreducible mod p */
    1892             : int
    1893        2300 : FpXQ_issquare(GEN x, GEN T, GEN p)
    1894             : {
    1895             :   pari_sp av;
    1896             :   long res;
    1897        2300 :   if (lg(x) == 2 || absequalui(2, p)) return 1;
    1898        2300 :   if (lg(x) == 3) return Fq_issquare(gel(x,2), T, p);
    1899             :   /* Ng = g^((q-1)/(p-1)) */
    1900        2265 :   av = avma; res = kronecker(FpXQ_norm(x,T,p), p) == 1;
    1901        2265 :   avma = av; return res;
    1902             : }
    1903             : int
    1904       64865 : Fp_issquare(GEN x, GEN p)
    1905             : {
    1906       64865 :   if (absequalui(2, p)) return 1;
    1907       64865 :   return kronecker(x, p) == 1;
    1908             : }
    1909             : /* assume T irreducible mod p */
    1910             : int
    1911       64760 : Fq_issquare(GEN x, GEN T, GEN p)
    1912             : {
    1913       64760 :   if (typ(x) != t_INT) return FpXQ_issquare(x, T, p);
    1914       64735 :   return (T && ! odd(get_FpX_degree(T))) || Fp_issquare(x, p);
    1915             : }
    1916             : 
    1917             : long
    1918          95 : Fq_ispower(GEN x, GEN K, GEN T, GEN p)
    1919             : {
    1920          95 :   pari_sp av = avma;
    1921             :   long d;
    1922             :   GEN Q;
    1923          95 :   if (!T) return Fp_ispower(x,K,p);
    1924          80 :   d = get_FpX_degree(T);
    1925          80 :   if (!umodui(d, K)) return 1;
    1926          45 :   Q = subiu(powiu(p,d), 1);
    1927          45 :   Q = diviiexact(Q, gcdii(Q, K));
    1928          45 :   d = gequal1(Fq_pow(x, Q, T,p));
    1929          45 :   avma = av; return d;
    1930             : }
    1931             : 
    1932             : /* discrete log in FpXQ for a in Fp^*, g in FpXQ^* of order ord */
    1933             : GEN
    1934        3970 : Fp_FpXQ_log(GEN a, GEN g, GEN o, GEN T, GEN p)
    1935             : {
    1936        3970 :   pari_sp av = avma;
    1937             :   GEN q,n_q,ord,ordp, op;
    1938             : 
    1939        3970 :   if (equali1(a)) return gen_0;
    1940             :   /* p > 2 */
    1941             : 
    1942        2565 :   ordp = subiu(p, 1); /* even */
    1943        2565 :   ord  = get_arith_Z(o);
    1944        2545 :   if (!ord) ord = T? subiu(powiu(p, get_FpX_degree(T)), 1): ordp;
    1945        2545 :   if (equalii(a, ordp)) /* -1 */
    1946        1515 :     return gerepileuptoint(av, shifti(ord,-1));
    1947        1030 :   ordp = gcdii(ordp,ord);
    1948        1030 :   op = typ(o)==t_MAT ? famat_Z_gcd(o,ordp) : ordp;
    1949             : 
    1950        1030 :   q = NULL;
    1951        1030 :   if (T)
    1952             :   { /* we want < g > = Fp^* */
    1953        1030 :     if (!equalii(ord,ordp)) {
    1954        1025 :       q = diviiexact(ord,ordp);
    1955        1025 :       g = FpXQ_pow(g,q,T,p);
    1956             :     }
    1957        1030 :     g = constant_coeff(g);
    1958             :   }
    1959        1030 :   n_q = Fp_log(a,g,op,p);
    1960        1030 :   if (lg(n_q)==1) return gerepileuptoleaf(av, n_q);
    1961        1030 :   if (q) n_q = mulii(q, n_q);
    1962        1030 :   return gerepileuptoint(av, n_q);
    1963             : }
    1964             : 
    1965             : static GEN
    1966       15780 : _FpXQ_pow(void *data, GEN x, GEN n)
    1967             : {
    1968       15780 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1969       15780 :   return FpXQ_pow_Frobenius(x,n, D->aut, D->T, D->p);
    1970             : }
    1971             : 
    1972             : static GEN
    1973        2275 : _FpXQ_rand(void *data)
    1974             : {
    1975        2275 :   pari_sp av=avma;
    1976        2275 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1977             :   GEN z;
    1978             :   do
    1979             :   {
    1980        2275 :     avma=av;
    1981        2275 :     z=random_FpX(get_FpX_degree(D->T),get_FpX_var(D->T),D->p);
    1982        2275 :   } while (!signe(z));
    1983        2275 :   return z;
    1984             : }
    1985             : 
    1986             : static GEN
    1987         980 : _FpXQ_easylog(void *E, GEN a, GEN g, GEN ord)
    1988             : {
    1989         980 :   struct _FpXQ *s=(struct _FpXQ*) E;
    1990         980 :   if (degpol(a)) return NULL;
    1991         970 :   return Fp_FpXQ_log(constant_coeff(a),g,ord,s->T,s->p);
    1992             : }
    1993             : 
    1994             : static const struct bb_group FpXQ_star={_FpXQ_mul,_FpXQ_pow,_FpXQ_rand,hash_GEN,ZX_equal,ZX_equal1,_FpXQ_easylog};
    1995             : 
    1996             : const struct bb_group *
    1997        1245 : get_FpXQ_star(void **E, GEN T, GEN p)
    1998             : {
    1999        1245 :   struct _FpXQ *e = (struct _FpXQ *) stack_malloc(sizeof(struct _FpXQ));
    2000        1245 :   e->T = T; e->p  = p; e->aut =  FpX_Frobenius(T, p);
    2001        1245 :   *E = (void*)e; return &FpXQ_star;
    2002             : }
    2003             : 
    2004             : GEN
    2005          20 : FpXQ_order(GEN a, GEN ord, GEN T, GEN p)
    2006             : {
    2007          20 :   if (lgefint(p)==3)
    2008             :   {
    2009           0 :     pari_sp av=avma;
    2010           0 :     ulong pp = to_Flxq(&a, &T, p);
    2011           0 :     GEN z = Flxq_order(a, ord, T, pp);
    2012           0 :     return gerepileuptoint(av,z);
    2013             :   }
    2014             :   else
    2015             :   {
    2016             :     void *E;
    2017          20 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2018          20 :     return gen_order(a,ord,E,S);
    2019             :   }
    2020             : }
    2021             : 
    2022             : GEN
    2023       51125 : FpXQ_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2024             : {
    2025       51125 :   pari_sp av=avma;
    2026       51125 :   if (lgefint(p)==3)
    2027             :   {
    2028       51095 :     if (uel(p,2) == 2)
    2029             :     {
    2030       29105 :       GEN z = F2xq_log(ZX_to_F2x(a), ZX_to_F2x(g), ord,
    2031             :                                      ZX_to_F2x(get_FpX_mod(T)));
    2032       29105 :       return gerepileuptoleaf(av, z);
    2033             :     }
    2034             :     else
    2035             :     {
    2036       21990 :       ulong pp = to_Flxq(&a, &T, p);
    2037       21990 :       GEN z = Flxq_log(a, ZX_to_Flx(g, pp), ord, T, pp);
    2038       21990 :       return gerepileuptoleaf(av, z);
    2039             :     }
    2040             :   }
    2041             :   else
    2042             :   {
    2043             :     void *E;
    2044          30 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2045          30 :     GEN z = gen_PH_log(a,g,ord,E,S);
    2046          10 :     return gerepileuptoleaf(av, z);
    2047             :   }
    2048             : }
    2049             : 
    2050             : GEN
    2051      183725 : Fq_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2052             : {
    2053      183725 :   if (!T) return Fp_log(a,g,ord,p);
    2054       54095 :   if (typ(g) == t_INT)
    2055             :   {
    2056           0 :     if (typ(a) == t_POL)
    2057             :     {
    2058           0 :       if (degpol(a)) return cgetg(1,t_VEC);
    2059           0 :       a = gel(a,2);
    2060             :     }
    2061           0 :     return Fp_log(a,g,ord,p);
    2062             :   }
    2063       54095 :   return typ(a) == t_INT? Fp_FpXQ_log(a,g,ord,T,p): FpXQ_log(a,g,ord,T,p);
    2064             : }
    2065             : 
    2066             : GEN
    2067        5530 : FpXQ_sqrtn(GEN a, GEN n, GEN T, GEN p, GEN *zeta)
    2068             : {
    2069        5530 :   pari_sp av = avma;
    2070             :   GEN z;
    2071        5530 :   if (!signe(a))
    2072             :   {
    2073          50 :     long v=varn(a);
    2074          50 :     if (signe(n) < 0) pari_err_INV("FpXQ_sqrtn",a);
    2075          45 :     if (zeta) *zeta=pol_1(v);
    2076          45 :     return pol_0(v);
    2077             :   }
    2078        5480 :   if (lgefint(p)==3)
    2079             :   {
    2080        4285 :     if (uel(p,2) == 2)
    2081             :     {
    2082         150 :       z = F2xq_sqrtn(ZX_to_F2x(a), n, ZX_to_F2x(get_Flx_mod(T)), zeta);
    2083         150 :       if (!z) return NULL;
    2084         150 :       z = F2x_to_ZX(z);
    2085         150 :       if (!zeta) return gerepileuptoleaf(av, z);
    2086           5 :       *zeta=F2x_to_ZX(*zeta);
    2087             :     } else
    2088             :     {
    2089        4135 :       ulong pp = to_Flxq(&a, &T, p);
    2090        4135 :       z = Flxq_sqrtn(a, n, T, pp, zeta);
    2091        4135 :       if (!z) return NULL;
    2092        2480 :       if (!zeta) return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    2093          50 :       z = Flx_to_ZX(z);
    2094          50 :       *zeta=Flx_to_ZX(*zeta);
    2095             :     }
    2096             :   }
    2097             :   else
    2098             :   {
    2099             :     void *E;
    2100        1195 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2101        1195 :     GEN o = subiu(powiu(p,get_FpX_degree(T)),1);
    2102        1195 :     z = gen_Shanks_sqrtn(a,n,o,zeta,E,S);
    2103        2390 :     if (!z) return NULL;
    2104        1165 :     if (!zeta) return gerepileupto(av, z);
    2105             :   }
    2106          55 :   gerepileall(av, 2, &z,zeta);
    2107          55 :   return z;
    2108             : }
    2109             : 
    2110             : GEN
    2111        5225 : FpXQ_sqrt(GEN a, GEN T, GEN p)
    2112             : {
    2113        5225 :   return FpXQ_sqrtn(a, gen_2, T, p, NULL);
    2114             : }
    2115             : 
    2116             : GEN
    2117        2265 : FpXQ_norm(GEN x, GEN TB, GEN p)
    2118             : {
    2119        2265 :   pari_sp av = avma;
    2120        2265 :   GEN T = get_FpX_mod(TB);
    2121        2265 :   GEN y = FpX_resultant(T, x, p);
    2122        2265 :   GEN L = leading_coeff(T);
    2123        2265 :   if (gequal1(L) || signe(x)==0) return y;
    2124           0 :   return gerepileupto(av, Fp_div(y, Fp_pows(L, degpol(x), p), p));
    2125             : }
    2126             : 
    2127             : GEN
    2128       14700 : FpXQ_trace(GEN x, GEN TB, GEN p)
    2129             : {
    2130       14700 :   pari_sp av = avma;
    2131       14700 :   GEN T = get_FpX_mod(TB);
    2132       14700 :   GEN dT = FpX_deriv(T,p);
    2133       14700 :   long n = degpol(dT);
    2134       14700 :   GEN z = FpXQ_mul(x, dT, TB, p);
    2135       14700 :   if (degpol(z)<n) { avma = av; return gen_0; }
    2136       13860 :   return gerepileuptoint(av, Fp_div(gel(z,2+n), gel(T,3+n),p));
    2137             : }
    2138             : 
    2139             : GEN
    2140           0 : FpXQ_charpoly(GEN x, GEN T, GEN p)
    2141             : {
    2142           0 :   pari_sp ltop=avma;
    2143           0 :   long vT, v = fetch_var();
    2144             :   GEN R;
    2145           0 :   T = leafcopy(get_FpX_mod(T));
    2146           0 :   vT = varn(T); setvarn(T, v);
    2147           0 :   x = leafcopy(x); setvarn(x, v);
    2148           0 :   R = FpX_FpXY_resultant(T, deg1pol_shallow(gen_1,FpX_neg(x,p),vT),p);
    2149           0 :   (void)delete_var(); return gerepileupto(ltop,R);
    2150             : }
    2151             : 
    2152             : /* Computing minimal polynomial :                         */
    2153             : /* cf Shoup 'Efficient Computation of Minimal Polynomials */
    2154             : /*          in Algebraic Extensions of Finite Fields'     */
    2155             : 
    2156             : static GEN
    2157          75 : FpXn_mul(GEN a, GEN b, long n, GEN p)
    2158             : {
    2159          75 :   return FpX_red(RgXn_red_shallow(ZX_mul(a, b), n), p);
    2160             : }
    2161             : 
    2162             : /* Let v a linear form, return the linear form z->v(tau*z)
    2163             :    that is, v*(M_tau) */
    2164             : 
    2165             : static GEN
    2166          20 : FpXQ_transmul_init(GEN tau, GEN T, GEN p)
    2167             : {
    2168             :   GEN bht;
    2169          20 :   GEN h, Tp = get_FpX_red(T, &h);
    2170          20 :   long n = degpol(Tp), vT = varn(Tp);
    2171          20 :   GEN ft = FpX_recipspec(Tp+2, n+1, n+1);
    2172          20 :   GEN bt = FpX_recipspec(tau+2, lgpol(tau), n);
    2173          20 :   setvarn(ft, vT); setvarn(bt, vT);
    2174          20 :   if (h)
    2175           0 :     bht = FpXn_mul(bt, h, n-1, p);
    2176             :   else
    2177             :   {
    2178          20 :     GEN bh = FpX_div(RgX_shift_shallow(tau, n-1), T, p);
    2179          20 :     bht = FpX_recipspec(bh+2, lgpol(bh), n-1);
    2180          20 :     setvarn(bht, vT);
    2181             :   }
    2182          20 :   return mkvec3(bt, bht, ft);
    2183             : }
    2184             : 
    2185             : static GEN
    2186          85 : FpXQ_transmul(GEN tau, GEN a, long n, GEN p)
    2187             : {
    2188          85 :   pari_sp ltop = avma;
    2189             :   GEN t1, t2, t3, vec;
    2190          85 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    2191          85 :   if (signe(a)==0) return pol_0(varn(a));
    2192          85 :   t2 = RgX_shift_shallow(FpX_mul(bt, a, p),1-n);
    2193          85 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    2194          75 :   t1 = RgX_shift_shallow(FpX_mul(ft, a, p),-n);
    2195          75 :   t3 = FpXn_mul(t1, bht, n-1, p);
    2196          75 :   vec = FpX_sub(t2, RgX_shift_shallow(t3, 1), p);
    2197          75 :   return gerepileupto(ltop, vec);
    2198             : }
    2199             : 
    2200             : GEN
    2201        2695 : FpXQ_minpoly(GEN x, GEN T, GEN p)
    2202             : {
    2203        2695 :   pari_sp ltop = avma;
    2204             :   long vT, n;
    2205             :   GEN v_x, g, tau;
    2206        2695 :   if (lgefint(p)==3)
    2207             :   {
    2208        2685 :     ulong pp = to_Flxq(&x, &T, p);
    2209        2685 :     GEN g = Flxq_minpoly(x, T, pp);
    2210        2685 :     return gerepileupto(ltop, Flx_to_ZX(g));
    2211             :   }
    2212          10 :   vT = get_FpX_var(T);
    2213          10 :   n = get_FpX_degree(T);
    2214          10 :   g = pol_1(vT);
    2215          10 :   tau = pol_1(vT);
    2216          10 :   T = FpX_get_red(T, p);
    2217          10 :   x = FpXQ_red(x, T, p);
    2218          10 :   v_x = FpXQ_powers(x, usqrt(2*n), T, p);
    2219          30 :   while(signe(tau) != 0)
    2220             :   {
    2221             :     long i, j, m, k1;
    2222             :     GEN M, v, tr;
    2223             :     GEN g_prime, c;
    2224          10 :     if (degpol(g) == n) { tau = pol_1(vT); g = pol_1(vT); }
    2225          10 :     v = random_FpX(n, vT, p);
    2226          10 :     tr = FpXQ_transmul_init(tau, T, p);
    2227          10 :     v = FpXQ_transmul(tr, v, n, p);
    2228          10 :     m = 2*(n-degpol(g));
    2229          10 :     k1 = usqrt(m);
    2230          10 :     tr = FpXQ_transmul_init(gel(v_x,k1+1), T, p);
    2231          10 :     c = cgetg(m+2,t_POL);
    2232          10 :     c[1] = evalsigne(1)|evalvarn(vT);
    2233          85 :     for (i=0; i<m; i+=k1)
    2234             :     {
    2235          75 :       long mj = minss(m-i, k1);
    2236         475 :       for (j=0; j<mj; j++)
    2237         400 :         gel(c,m+1-(i+j)) = FpX_dotproduct(v, gel(v_x,j+1), p);
    2238          75 :       v = FpXQ_transmul(tr, v, n, p);
    2239             :     }
    2240          10 :     c = FpX_renormalize(c, m+2);
    2241             :     /* now c contains <v,x^i> , i = 0..m-1  */
    2242          10 :     M = FpX_halfgcd(pol_xn(m, vT), c, p);
    2243          10 :     g_prime = gmael(M, 2, 2);
    2244          10 :     if (degpol(g_prime) < 1) continue;
    2245          10 :     g = FpX_mul(g, g_prime, p);
    2246          10 :     tau = FpXQ_mul(tau, FpX_FpXQV_eval(g_prime, v_x, T, p), T, p);
    2247             :   }
    2248          10 :   g = FpX_normalize(g,p);
    2249          10 :   return gerepilecopy(ltop,g);
    2250             : }
    2251             : 
    2252             : GEN
    2253           5 : FpXQ_conjvec(GEN x, GEN T, GEN p)
    2254             : {
    2255           5 :   pari_sp av=avma;
    2256             :   long i;
    2257           5 :   long n = get_FpX_degree(T), v = varn(x);
    2258           5 :   GEN M = FpX_matFrobenius(T, p);
    2259           5 :   GEN z = cgetg(n+1,t_COL);
    2260           5 :   gel(z,1) = RgX_to_RgC(x,n);
    2261           5 :   for (i=2; i<=n; i++) gel(z,i) = FpM_FpC_mul(M,gel(z,i-1),p);
    2262           5 :   gel(z,1) = x;
    2263           5 :   for (i=2; i<=n; i++) gel(z,i) = RgV_to_RgX(gel(z,i),v);
    2264           5 :   return gerepilecopy(av,z);
    2265             : }
    2266             : 
    2267             : /* p prime, p_1 = p-1, q = p^deg T, Lp = cofactors of some prime divisors
    2268             :  * l_p of p-1, Lq = cofactors of some prime divisors l_q of q-1, return a
    2269             :  * g in Fq such that
    2270             :  * - Ng generates all l_p-Sylows of Fp^*
    2271             :  * - g generates all l_q-Sylows of Fq^* */
    2272             : static GEN
    2273        1040 : gener_FpXQ_i(GEN T, GEN p, GEN p_1, GEN Lp, GEN Lq)
    2274             : {
    2275             :   pari_sp av;
    2276        1040 :   long vT = varn(T), f = degpol(T), l = lg(Lq);
    2277        1040 :   GEN F = FpX_Frobenius(T, p);
    2278        1040 :   int p_is_2 = is_pm1(p_1);
    2279        2150 :   for (av = avma;; avma = av)
    2280             :   {
    2281        2150 :     GEN t, g = random_FpX(f, vT, p);
    2282             :     long i;
    2283        2150 :     if (degpol(g) < 1) continue;
    2284        2070 :     if (p_is_2)
    2285         280 :       t = g;
    2286             :     else
    2287             :     {
    2288        1790 :       t = FpX_resultant(T, g, p); /* Ng = g^((q-1)/(p-1)), assuming T monic */
    2289        1790 :       if (kronecker(t, p) == 1) continue;
    2290         895 :       if (lg(Lp) > 1 && !is_gener_Fp(t, p, p_1, Lp)) continue;
    2291         895 :       t = FpXQ_pow(g, shifti(p_1,-1), T, p);
    2292             :     }
    2293        1505 :     for (i = 1; i < l; i++)
    2294             :     {
    2295         465 :       GEN a = FpXQ_pow_Frobenius(t, gel(Lq,i), F, T, p);
    2296         465 :       if (!degpol(a) && equalii(gel(a,2), p_1)) break;
    2297             :     }
    2298        2215 :     if (i == l) return g;
    2299        1110 :   }
    2300             : }
    2301             : 
    2302             : GEN
    2303        4885 : gener_FpXQ(GEN T, GEN p, GEN *po)
    2304             : {
    2305        4885 :   long i, j, f = get_FpX_degree(T);
    2306             :   GEN g, Lp, Lq, p_1, q_1, N, o;
    2307        4885 :   pari_sp av = avma;
    2308             : 
    2309        4885 :   p_1 = subiu(p,1);
    2310        4885 :   if (f == 1) {
    2311             :     GEN Lp, fa;
    2312           5 :     o = p_1;
    2313           5 :     fa = Z_factor(o);
    2314           5 :     Lp = gel(fa,1);
    2315           5 :     Lp = vecslice(Lp, 2, lg(Lp)-1); /* remove 2 for efficiency */
    2316             : 
    2317           5 :     g = cgetg(3, t_POL);
    2318           5 :     g[1] = evalsigne(1) | evalvarn(get_FpX_var(T));
    2319           5 :     gel(g,2) = pgener_Fp_local(p, Lp);
    2320           5 :     if (po) *po = mkvec2(o, fa);
    2321           5 :     return g;
    2322             :   }
    2323        4880 :   if (lgefint(p) == 3)
    2324             :   {
    2325        4865 :     ulong pp = to_Flxq(NULL, &T, p);
    2326        4865 :     g = gener_Flxq(T, pp, po);
    2327        4865 :     if (!po) return Flx_to_ZX_inplace(gerepileuptoleaf(av, g));
    2328        4865 :     g = Flx_to_ZX(g);
    2329        4865 :     gerepileall(av, 2, &g, po);
    2330        4865 :     return g;
    2331             :   }
    2332             :   /* p now odd */
    2333          15 :   q_1 = subiu(powiu(p,f), 1);
    2334          15 :   N = diviiexact(q_1, p_1);
    2335          15 :   Lp = odd_prime_divisors(p_1);
    2336          15 :   for (i=lg(Lp)-1; i; i--) gel(Lp,i) = diviiexact(p_1, gel(Lp,i));
    2337          15 :   o = factor_pn_1(p,f);
    2338          15 :   Lq = leafcopy( gel(o, 1) );
    2339         130 :   for (i = j = 1; i < lg(Lq); i++)
    2340             :   {
    2341         115 :     if (remii(p_1, gel(Lq,i)) == gen_0) continue;
    2342          70 :     gel(Lq,j++) = diviiexact(N, gel(Lq,i));
    2343             :   }
    2344          15 :   setlg(Lq, j);
    2345          15 :   g = gener_FpXQ_i(get_FpX_mod(T), p, p_1, Lp, Lq);
    2346          15 :   if (!po) g = gerepilecopy(av, g);
    2347             :   else {
    2348           5 :     *po = mkvec2(q_1, o);
    2349           5 :     gerepileall(av, 2, &g, po);
    2350             :   }
    2351          15 :   return g;
    2352             : }
    2353             : 
    2354             : GEN
    2355        1025 : gener_FpXQ_local(GEN T, GEN p, GEN L)
    2356             : {
    2357        1025 :   GEN Lp, Lq, p_1 = subiu(p,1), q_1, N, Q;
    2358        1025 :   long f, i, ip, iq, l = lg(L);
    2359        1025 :   T = get_FpX_mod(T);
    2360        1025 :   f = degpol(T);
    2361        1025 :   q_1 = subiu(powiu(p,f), 1);
    2362        1025 :   N = diviiexact(q_1, p_1);
    2363             : 
    2364        1025 :   Q = is_pm1(p_1)? gen_1: shifti(p_1,-1);
    2365        1025 :   Lp = cgetg(l, t_VEC); ip = 1;
    2366        1025 :   Lq = cgetg(l, t_VEC); iq = 1;
    2367        1500 :   for (i=1; i < l; i++)
    2368             :   {
    2369         475 :     GEN a, b, ell = gel(L,i);
    2370         475 :     if (absequaliu(ell,2)) continue;
    2371         275 :     a = dvmdii(Q, ell, &b);
    2372         275 :     if (b == gen_0)
    2373          15 :       gel(Lp,ip++) = a;
    2374             :     else
    2375         260 :       gel(Lq,iq++) = diviiexact(N,ell);
    2376             :   }
    2377        1025 :   setlg(Lp, ip);
    2378        1025 :   setlg(Lq, iq);
    2379        1025 :   return gener_FpXQ_i(T, p, p_1, Lp, Lq);
    2380             : }

Generated by: LCOV version 1.11