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 20123-c22c2cd) Lines: 1244 1332 93.4 %
Date: 2017-01-24 05:49:10 Functions: 148 153 96.7 %
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    64612286 : get_FpX_red(GEN T, GEN *B)
      21             : {
      22    64612286 :   if (typ(T)!=t_VEC) { *B=NULL; return T; }
      23      121761 :   *B = gel(T,1); return gel(T,2);
      24             : }
      25             : 
      26             : GEN
      27      385874 : get_FpX_mod(GEN T) { return typ(T)==t_VEC? gel(T,2): T; }
      28             : 
      29             : long
      30     1382999 : get_FpX_var(GEN T) { return typ(T)==t_VEC? varn(gel(T,2)): varn(T); }
      31             : 
      32             : long
      33     1585873 : 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    40051787 : to_Flx(GEN *P, GEN *Q, GEN p)
      54             : {
      55    40051787 :   ulong pp = uel(p,2);
      56    40051787 :   *P = ZX_to_Flx(*P, pp);
      57    40051787 :   *Q = ZX_to_Flx(*Q, pp); return pp;
      58             : }
      59             : 
      60             : static ulong
      61      755234 : to_Flxq(GEN *P, GEN *T, GEN p)
      62             : {
      63      755234 :   ulong pp = uel(p,2);
      64      755234 :   if (P) *P = ZX_to_Flx(*P, pp);
      65      755234 :   *T = ZXT_to_FlxT(*T, pp); return pp;
      66             : }
      67             : 
      68             : GEN
      69        1710 : Z_to_FpX(GEN a, GEN p, long v)
      70             : {
      71        1710 :   pari_sp av = avma;
      72        1710 :   GEN z = cgetg(3, t_POL);
      73        1710 :   GEN x = modii(a, p);
      74        1710 :   if (!signe(x)) { avma =av; return pol_0(v); }
      75        1710 :   z[1] = evalsigne(1) | evalvarn(v);
      76        1710 :   gel(z,2) = x; return z;
      77             : }
      78             : 
      79             : /* z in Z[X], return lift(z * Mod(1,p)), normalized*/
      80             : GEN
      81    66134445 : FpX_red(GEN z, GEN p)
      82             : {
      83    66134445 :   long i, l = lg(z);
      84    66134445 :   GEN x = cgetg(l, t_POL);
      85    66156786 :   for (i=2; i<l; i++) gel(x,i) = modii(gel(z,i),p);
      86    66134410 :   x[1] = z[1]; return FpX_renormalize(x,l);
      87             : }
      88             : GEN
      89      290105 : FpXV_red(GEN z, GEN p)
      90             : {
      91      290105 :   long i,l = lg(z);
      92      290105 :   GEN x = cgetg(l, t_VEC);
      93      290105 :   for (i=1; i<l; i++) gel(x,i) = FpX_red(gel(z,i), p);
      94      290105 :   return x;
      95             : }
      96             : 
      97             : GEN
      98     1098346 : FpXT_red(GEN z, GEN p)
      99             : {
     100     1098346 :   if (typ(z) == t_POL)
     101     1014902 :     return FpX_red(z, p);
     102             :   else
     103             :   {
     104       83444 :     long i,l = lg(z);
     105       83444 :     GEN x = cgetg(l, t_VEC);
     106       83445 :     for (i=1; i<l; i++) gel(x,i) = FpXT_red(gel(z,i), p);
     107       83443 :     return x;
     108             :   }
     109             : }
     110             : 
     111             : GEN
     112      260249 : FpX_normalize(GEN z, GEN p)
     113             : {
     114      260249 :   GEN p1 = leading_coeff(z);
     115      260249 :   if (lg(z) == 2 || equali1(p1)) return z;
     116       45821 :   return FpX_Fp_mul_to_monic(z, Fp_inv(p1,p), p);
     117             : }
     118             : 
     119             : GEN
     120      677258 : FpX_center(GEN T, GEN p, GEN pov2)
     121             : {
     122      677258 :   long i, l = lg(T);
     123      677258 :   GEN P = cgetg(l,t_POL);
     124      677258 :   for(i=2; i<l; i++) gel(P,i) = Fp_center(gel(T,i), p, pov2);
     125      677258 :   P[1] = T[1]; return P;
     126             : }
     127             : 
     128             : GEN
     129     8072836 : FpX_add(GEN x,GEN y,GEN p)
     130             : {
     131     8072836 :   long lx = lg(x), ly = lg(y), i;
     132             :   GEN z;
     133     8072836 :   if (lx < ly) swapspec(x,y, lx,ly);
     134     8072836 :   z = cgetg(lx,t_POL); z[1] = x[1];
     135     8072836 :   for (i=2; i<ly; i++) gel(z,i) = Fp_add(gel(x,i),gel(y,i), p);
     136     8072836 :   for (   ; i<lx; i++) gel(z,i) = modii(gel(x,i), p);
     137     8072836 :   z = ZX_renormalize(z, lx);
     138     8072836 :   if (!lgpol(z)) { avma = (pari_sp)(z + lx); return pol_0(varn(x)); }
     139     7794862 :   return z;
     140             : }
     141             : 
     142             : static GEN
     143        6598 : Fp_red_FpX(GEN x, GEN p, long v)
     144             : {
     145             :   GEN z;
     146        6598 :   if (!signe(x)) return pol_0(v);
     147         449 :   z = cgetg(3, t_POL);
     148         449 :   gel(z,2) = Fp_red(x,p);
     149         449 :   z[1] = evalvarn(v);
     150         449 :   return FpX_renormalize(z, 3);
     151             : }
     152             : 
     153             : static GEN
     154           8 : Fp_neg_FpX(GEN x, GEN p, long v)
     155             : {
     156             :   GEN z;
     157           8 :   if (!signe(x)) return pol_0(v);
     158           1 :   z = cgetg(3, t_POL);
     159           1 :   gel(z,2) = Fp_neg(x,p);
     160           1 :   z[1] = evalvarn(v);
     161           1 :   return FpX_renormalize(z, 3);
     162             : }
     163             : 
     164             : GEN
     165      550299 : FpX_Fp_add(GEN y,GEN x,GEN p)
     166             : {
     167      550299 :   long i, lz = lg(y);
     168             :   GEN z;
     169      550299 :   if (lz == 2) return Fp_red_FpX(x,p,varn(y));
     170      543701 :   z = cgetg(lz,t_POL); z[1] = y[1];
     171      543701 :   gel(z,2) = Fp_add(gel(y,2),x, p);
     172      543701 :   if (lz == 3) z = FpX_renormalize(z,lz);
     173             :   else
     174      496255 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     175      543701 :   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      299833 : FpX_Fp_sub(GEN y,GEN x,GEN p)
     192             : {
     193      299833 :   long i, lz = lg(y);
     194             :   GEN z;
     195      299833 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     196      299825 :   z = cgetg(lz,t_POL); z[1] = y[1];
     197      299825 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     198      299824 :   if (lz == 3) z = FpX_renormalize(z,lz);
     199             :   else
     200      122518 :     for(i=3;i<lz;i++) gel(z,i) = icopy(gel(y,i));
     201      299825 :   return z;
     202             : }
     203             : GEN
     204        1559 : FpX_Fp_sub_shallow(GEN y,GEN x,GEN p)
     205             : {
     206        1559 :   long i, lz = lg(y);
     207             :   GEN z;
     208        1559 :   if (lz == 2) return Fp_neg_FpX(x,p,varn(y));
     209        1559 :   z = cgetg(lz,t_POL); z[1] = y[1];
     210        1559 :   gel(z,2) = Fp_sub(gel(y,2),x, p);
     211        1559 :   if (lz == 3) z = FpX_renormalize(z,lz);
     212             :   else
     213        1405 :     for(i=3;i<lz;i++) gel(z,i) = gel(y,i);
     214        1559 :   return z;
     215             : }
     216             : 
     217             : GEN
     218       73822 : FpX_neg(GEN x,GEN p)
     219             : {
     220       73822 :   long i, lx = lg(x);
     221       73822 :   GEN y = cgetg(lx,t_POL);
     222       73822 :   y[1] = x[1];
     223       73822 :   for(i=2; i<lx; i++) gel(y,i) = Fp_neg(gel(x,i), p);
     224       73822 :   return ZX_renormalize(y, lx);
     225             : }
     226             : 
     227             : static GEN
     228     6577838 : FpX_subspec(GEN x,GEN y,GEN p, long nx, long ny)
     229             : {
     230             :   long i, lz;
     231             :   GEN z;
     232     6577838 :   if (nx >= ny)
     233             :   {
     234     4835194 :     lz = nx+2;
     235     4835194 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     236     4836593 :     for (i=0; i<ny; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     237     4835197 :     for (   ; i<nx; i++) gel(z,i) = modii(gel(x,i), p);
     238             :   }
     239             :   else
     240             :   {
     241     1742644 :     lz = ny+2;
     242     1742644 :     z = cgetg(lz,t_POL); z[1] = 0; z += 2;
     243     1742640 :     for (i=0; i<nx; i++) gel(z,i) = Fp_sub(gel(x,i),gel(y,i), p);
     244     1742644 :     for (   ; i<ny; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     245             :   }
     246     6577839 :   z = FpX_renormalize(z-2, lz);
     247     6577840 :   if (!lgpol(z)) { avma = (pari_sp)(z + lz); return pol_0(0); }
     248     6461684 :   return z;
     249             : }
     250             : 
     251             : GEN
     252     6479991 : FpX_sub(GEN x,GEN y,GEN p)
     253             : {
     254     6479991 :   GEN z = FpX_subspec(x+2,y+2,p,lgpol(x),lgpol(y));
     255     6479992 :   setvarn(z, varn(x));
     256     6479992 :   return z;
     257             : }
     258             : 
     259             : GEN
     260        8427 : Fp_FpX_sub(GEN x, GEN y, GEN p)
     261             : {
     262        8427 :   long ly = lg(y), i;
     263             :   GEN z;
     264        8427 :   if (ly <= 3) {
     265         219 :     z = cgetg(3, t_POL);
     266         219 :     x = (ly == 3)? Fp_sub(x, gel(y,2), p): modii(x, p);
     267         219 :     if (!signe(x)) { avma = (pari_sp)(z + 3); return pol_0(varn(y)); }
     268         187 :     z[1] = evalsigne(1)|y[1]; gel(z,2) = x; return z;
     269             :   }
     270        8208 :   z = cgetg(ly,t_POL);
     271        8208 :   gel(z,2) = Fp_sub(x, gel(y,2), p);
     272        8208 :   for (i = 3; i < ly; i++) gel(z,i) = Fp_neg(gel(y,i), p);
     273        8208 :   z = ZX_renormalize(z, ly);
     274        8208 :   if (!lgpol(z)) { avma = (pari_sp)(z + ly); return pol_0(varn(x)); }
     275        8208 :   z[1] = y[1]; return z;
     276             : }
     277             : 
     278             : GEN
     279    46036000 : FpX_mul(GEN x,GEN y,GEN p) { return FpX_red(ZX_mul(x, y), p); }
     280             : 
     281             : GEN
     282     1653931 : FpX_mulspec(GEN a, GEN b, GEN p, long na, long nb)
     283     1653931 : { return FpX_red(ZX_mulspec(a, b, na, nb), p); }
     284             : 
     285             : GEN
     286     3148348 : FpX_sqr(GEN x,GEN p) { return FpX_red(ZX_sqr(x), p); }
     287             : 
     288             : GEN
     289     1181193 : FpX_mulu(GEN y, ulong x,GEN p)
     290             : {
     291             :   GEN z;
     292             :   long i, l;
     293     1181193 :   x = umodui(x, p);
     294     1181193 :   if (!x) return zeropol(varn(y));
     295     1180927 :   z = cgetg_copy(y, &l); z[1] = y[1];
     296     1180927 :   for(i=2; i<l; i++) gel(z,i) = Fp_mulu(gel(y,i), x, p);
     297     1180927 :   return z;
     298             : }
     299             : 
     300             : GEN
     301     3502708 : FpX_Fp_mulspec(GEN y,GEN x,GEN p,long ly)
     302             : {
     303             :   GEN z;
     304             :   long i;
     305     3502708 :   if (!signe(x)) return pol_0(0);
     306     3248167 :   z = cgetg(ly+2,t_POL); z[1] = evalsigne(1);
     307     3248167 :   for(i=0; i<ly; i++) gel(z,i+2) = Fp_mul(gel(y,i), x, p);
     308     3248167 :   return ZX_renormalize(z, ly+2);
     309             : }
     310             : 
     311             : GEN
     312     3497024 : FpX_Fp_mul(GEN y,GEN x,GEN p)
     313             : {
     314     3497024 :   GEN z = FpX_Fp_mulspec(y+2,x,p,lgpol(y));
     315     3497024 :   setvarn(z, varn(y)); return z;
     316             : }
     317             : 
     318             : GEN
     319       45821 : FpX_Fp_mul_to_monic(GEN y,GEN x,GEN p)
     320             : {
     321             :   GEN z;
     322             :   long i, l;
     323       45821 :   z = cgetg_copy(y, &l); z[1] = y[1];
     324       45821 :   for(i=2; i<l-1; i++) gel(z,i) = Fp_mul(gel(y,i), x, p);
     325       45821 :   gel(z,l-1) = gen_1; return z;
     326             : }
     327             : 
     328             : struct _FpXQ {
     329             :   GEN T, p, aut;
     330             : };
     331             : 
     332             : static GEN
     333       55979 : _FpX_sqr(void *data, GEN x)
     334             : {
     335       55979 :   struct _FpXQ *D = (struct _FpXQ*)data;
     336       55979 :   return FpX_sqr(x, D->p);
     337             : }
     338             : static GEN
     339       69597 : _FpX_mul(void *data, GEN x, GEN y)
     340             : {
     341       69597 :   struct _FpXQ *D = (struct _FpXQ*)data;
     342       69597 :   return FpX_mul(x,y, D->p);
     343             : }
     344             : 
     345             : GEN
     346      267610 : FpX_powu(GEN x, ulong n, GEN p)
     347             : {
     348             :   struct _FpXQ D;
     349      267610 :   if (n==0) return pol_1(varn(x));
     350       33978 :   D.p = p;
     351       33978 :   return gen_powu(x, n, (void *)&D, _FpX_sqr, _FpX_mul);
     352             : }
     353             : 
     354             : GEN
     355        1090 : FpX_halve(GEN y, GEN p)
     356             : {
     357             :   GEN z;
     358             :   long i, l;
     359        1090 :   z = cgetg_copy(y, &l); z[1] = y[1];
     360        1090 :   for(i=2; i<l; i++) gel(z,i) = Fp_halve(gel(y,i), p);
     361        1090 :   return z;
     362             : }
     363             : 
     364             : static GEN
     365    61645870 : 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    61645870 :   if (!signe(y)) pari_err_INV("FpX_divrem",y);
     372    61645870 :   vx = varn(x);
     373    61645870 :   dy = degpol(y);
     374    61645870 :   dx = degpol(x);
     375    61645870 :   if (dx < dy)
     376             :   {
     377       64674 :     if (pr)
     378             :     {
     379       64659 :       av0 = avma; x = FpX_red(x, p);
     380       64659 :       if (pr == ONLY_DIVIDES) { avma=av0; return signe(x)? NULL: pol_0(vx); }
     381       64617 :       if (pr == ONLY_REM) return x;
     382       64617 :       *pr = x;
     383             :     }
     384       64632 :     return pol_0(vx);
     385             :   }
     386    61581196 :   lead = leading_coeff(y);
     387    61581196 :   if (!dy) /* y is constant */
     388             :   {
     389      290151 :     if (pr && pr != ONLY_DIVIDES)
     390             :     {
     391      277773 :       if (pr == ONLY_REM) return pol_0(vx);
     392      245786 :       *pr = pol_0(vx);
     393             :     }
     394      258164 :     av0 = avma;
     395      258164 :     if (equali1(lead)) return FpX_red(x, p);
     396      255393 :     else return gerepileupto(av0, FpX_Fp_mul(x, Fp_inv(lead,p), p));
     397             :   }
     398    61291045 :   av0 = avma; dz = dx-dy;
     399    61291045 :   if (lgefint(p) == 3)
     400             :   { /* assume ab != 0 mod p */
     401    39548512 :     ulong pp = to_Flx(&x, &y, p);
     402    39548512 :     z = Flx_divrem(x, y, pp, pr);
     403    39548512 :     avma = av0; /* HACK: assume pr last on stack, then z */
     404    39548512 :     if (!z) return NULL;
     405    39548477 :     z = leafcopy(z);
     406    39548477 :     if (pr && pr != ONLY_DIVIDES && pr != ONLY_REM)
     407             :     {
     408     2058762 :       *pr = leafcopy(*pr);
     409     2058762 :       *pr = Flx_to_ZX_inplace(*pr);
     410             :     }
     411    39548477 :     return Flx_to_ZX_inplace(z);
     412             :   }
     413    21742533 :   lead = equali1(lead)? NULL: gclone(Fp_inv(lead,p));
     414    21742470 :   avma = av0;
     415    21742470 :   z=cgetg(dz+3,t_POL); z[1] = x[1];
     416    21742470 :   x += 2; y += 2; z += 2;
     417    21742470 :   for (dy1=dy-1; dy1>=0 && !signe(gel(y, dy1)); dy1--);
     418             : 
     419    21742470 :   p1 = gel(x,dx); av = avma;
     420    21742470 :   gel(z,dz) = lead? gerepileuptoint(av, Fp_mul(p1,lead, p)): icopy(p1);
     421    51017278 :   for (i=dx-1; i>=dy; i--)
     422             :   {
     423    29274808 :     av=avma; p1=gel(x,i);
     424   315297288 :     for (j=i-dy1; j<=i && j<=dz; j++)
     425   286022480 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     426    29274808 :     if (lead) p1 = mulii(p1,lead);
     427    29274808 :     gel(z,i-dy) = gerepileuptoint(av,modii(p1, p));
     428             :   }
     429    21742470 :   if (!pr) { if (lead) gunclone(lead); return z-2; }
     430             : 
     431    21724591 :   rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
     432    22484033 :   for (sx=0; ; i--)
     433             :   {
     434    22484033 :     p1 = gel(x,i);
     435    73708541 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     436    51224508 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     437    22484033 :     p1 = modii(p1,p); if (signe(p1)) { sx = 1; break; }
     438      827309 :     if (!i) break;
     439      759442 :     avma=av;
     440      759442 :   }
     441    21724591 :   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    21724591 :   lr=i+3; rem -= lr;
     448    21724591 :   rem[0] = evaltyp(t_POL) | evallg(lr);
     449    21724591 :   rem[1] = z[-1];
     450    21724591 :   p1 = gerepileuptoint((pari_sp)rem, p1);
     451    21724591 :   rem += 2; gel(rem,i) = p1;
     452    81853800 :   for (i--; i>=0; i--)
     453             :   {
     454    60129209 :     av=avma; p1 = gel(x,i);
     455   422901776 :     for (j=maxss(0,i-dy1); j<=i && j<=dz; j++)
     456   362772567 :       p1 = subii(p1, mulii(gel(z,j),gel(y,i-j)));
     457    60129209 :     gel(rem,i) = gerepileuptoint(av, modii(p1,p));
     458             :   }
     459    21724591 :   rem -= 2;
     460    21724591 :   if (lead) gunclone(lead);
     461    21724591 :   if (!sx) (void)FpX_renormalize(rem, lr);
     462    21724591 :   if (pr == ONLY_REM) return gerepileupto(av0,rem);
     463      527314 :   *pr = rem; return z-2;
     464             : }
     465             : 
     466             : GEN
     467       19593 : FpX_div_by_X_x(GEN a, GEN x, GEN p, GEN *r)
     468             : {
     469       19593 :   long l = lg(a)-1, i;
     470       19593 :   GEN z = cgetg(l, t_POL);
     471       19593 :   z[1] = evalsigne(1) | evalvarn(0);
     472       19593 :   gel(z, l-1) = gel(a,l);
     473      632443 :   for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
     474      612850 :     gel(z, i) = Fp_addmul(gel(a,i+1), x, gel(z,i+1), p);
     475       19593 :   if (r) *r = Fp_addmul(gel(a,2), x, gel(z,2), p);
     476       19593 :   return z;
     477             : }
     478             : 
     479             : static GEN
     480       69706 : _FpX_divrem(void * E, GEN x, GEN y, GEN *r)
     481             : {
     482       69706 :   struct _FpXQ *D = (struct _FpXQ*) E;
     483       69706 :   return FpX_divrem(x, y, D->p, r);
     484             : }
     485             : static GEN
     486       10241 : _FpX_add(void * E, GEN x, GEN y) {
     487       10241 :   struct _FpXQ *D = (struct _FpXQ*) E;
     488       10241 :   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        5642 : FpX_digits(GEN x, GEN T, GEN p)
     495             : {
     496        5642 :   pari_sp av = avma;
     497             :   struct _FpXQ D;
     498        5642 :   long d = degpol(T), n = (lgpol(x)+d-1)/d;
     499             :   GEN z;
     500        5642 :   D.p = p;
     501        5642 :   z = gen_digits(x,T,n,(void *)&D, &FpX_ring, _FpX_divrem);
     502        5642 :   return gerepileupto(av, z);
     503             : }
     504             : 
     505             : GEN
     506        2296 : FpXV_FpX_fromdigits(GEN x, GEN T, GEN p)
     507             : {
     508        2296 :   pari_sp av = avma;
     509             :   struct _FpXQ D;
     510             :   GEN z;
     511        2296 :   D.p = p;
     512        2296 :   z = gen_fromdigits(x,T,(void *)&D, &FpX_ring);
     513        2296 :   return gerepileupto(av, z);
     514             : }
     515             : 
     516             : long
     517       23779 : FpX_valrem(GEN x, GEN t, GEN p, GEN *py)
     518             : {
     519       23779 :   pari_sp av=avma;
     520             :   long k;
     521             :   GEN r, y;
     522             : 
     523       68957 :   for (k=0; ; k++)
     524             :   {
     525       68957 :     y = FpX_divrem(x, t, p, &r);
     526       68957 :     if (signe(r)) break;
     527       45178 :     x = y;
     528       45178 :   }
     529       23779 :   *py = gerepilecopy(av,x);
     530       23779 :   return k;
     531             : }
     532             : 
     533             : static GEN
     534         191 : FpX_halfgcd_basecase(GEN a, GEN b, GEN p)
     535             : {
     536         191 :   pari_sp av=avma;
     537             :   GEN u,u1,v,v1;
     538         191 :   long vx = varn(a);
     539         191 :   long n = lgpol(a)>>1;
     540         191 :   u1 = v = pol_0(vx);
     541         191 :   u = v1 = pol_1(vx);
     542        3577 :   while (lgpol(b)>n)
     543             :   {
     544        3195 :     GEN r, q = FpX_divrem(a,b,p, &r);
     545        3195 :     a = b; b = r; swap(u,u1); swap(v,v1);
     546        3195 :     u1 = FpX_sub(u1, FpX_mul(u, q, p), p);
     547        3195 :     v1 = FpX_sub(v1, FpX_mul(v, q ,p), p);
     548        3195 :     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         191 :   return gerepilecopy(av, mkmat2(mkcol2(u,u1), mkcol2(v,v1)));
     555             : }
     556             : static GEN
     557         346 : FpX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN p)
     558             : {
     559         346 :   return FpX_add(FpX_mul(u, x, p),FpX_mul(v, y, p), p);
     560             : }
     561             : 
     562             : static GEN
     563         169 : FpXM_FpX_mul2(GEN M, GEN x, GEN y, GEN p)
     564             : {
     565         169 :   GEN res = cgetg(3, t_COL);
     566         169 :   gel(res, 1) = FpX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, p);
     567         169 :   gel(res, 2) = FpX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, p);
     568         169 :   return res;
     569             : }
     570             : 
     571             : static GEN
     572         169 : FpXM_mul2(GEN A, GEN B, GEN p)
     573             : {
     574         169 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
     575         169 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
     576         169 :   GEN M1 = FpX_mul(FpX_add(A11,A22, p), FpX_add(B11,B22, p), p);
     577         169 :   GEN M2 = FpX_mul(FpX_add(A21,A22, p), B11, p);
     578         169 :   GEN M3 = FpX_mul(A11, FpX_sub(B12,B22, p), p);
     579         169 :   GEN M4 = FpX_mul(A22, FpX_sub(B21,B11, p), p);
     580         169 :   GEN M5 = FpX_mul(FpX_add(A11,A12, p), B22, p);
     581         169 :   GEN M6 = FpX_mul(FpX_sub(A21,A11, p), FpX_add(B11,B12, p), p);
     582         169 :   GEN M7 = FpX_mul(FpX_sub(A12,A22, p), FpX_add(B21,B22, p), p);
     583         169 :   GEN T1 = FpX_add(M1,M4, p), T2 = FpX_sub(M7,M5, p);
     584         169 :   GEN T3 = FpX_sub(M1,M2, p), T4 = FpX_add(M3,M6, p);
     585         169 :   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         165 : FpX_FpXM_qmul(GEN q, GEN M, GEN p)
     592             : {
     593         165 :   GEN u, v, res = cgetg(3, t_MAT);
     594         165 :   u = FpX_sub(gcoeff(M,1,1), FpX_mul(gcoeff(M,2,1), q, p), p);
     595         165 :   gel(res,1) = mkcol2(gcoeff(M,2,1), u);
     596         165 :   v = FpX_sub(gcoeff(M,1,2), FpX_mul(gcoeff(M,2,2), q, p), p);
     597         165 :   gel(res,2) = mkcol2(gcoeff(M,2,2), v);
     598         165 :   return res;
     599             : }
     600             : 
     601             : static GEN
     602           4 : matid2_FpXM(long v)
     603             : {
     604           4 :   retmkmat2(mkcol2(pol_1(v),pol_0(v)),
     605             :             mkcol2(pol_0(v),pol_1(v)));
     606             : }
     607             : 
     608             : static GEN
     609         165 : FpX_halfgcd_split(GEN x, GEN y, GEN p)
     610             : {
     611         165 :   pari_sp av=avma;
     612             :   GEN R, S, V;
     613             :   GEN y1, r, q;
     614         165 :   long l = lgpol(x), n = l>>1, k;
     615         165 :   if (lgpol(y)<=n) return matid2_FpXM(varn(x));
     616         165 :   R = FpX_halfgcd(RgX_shift_shallow(x,-n),RgX_shift_shallow(y,-n),p);
     617         165 :   V = FpXM_FpX_mul2(R,x,y,p); y1 = gel(V,2);
     618         165 :   if (lgpol(y1)<=n) return gerepilecopy(av, R);
     619         165 :   q = FpX_divrem(gel(V,1), y1, p, &r);
     620         165 :   k = 2*n-degpol(y1);
     621         165 :   S = FpX_halfgcd(RgX_shift_shallow(y1,-k), RgX_shift_shallow(r,-k),p);
     622         165 :   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         356 : FpX_halfgcd_i(GEN x, GEN y, GEN p)
     631             : {
     632         356 :   if (lg(x)<=FpX_HALFGCD_LIMIT) return FpX_halfgcd_basecase(x,y,p);
     633         165 :   return FpX_halfgcd_split(x,y,p);
     634             : }
     635             : 
     636             : GEN
     637         465 : FpX_halfgcd(GEN x, GEN y, GEN p)
     638             : {
     639         465 :   pari_sp av = avma;
     640             :   GEN M,q,r;
     641         465 :   if (lgefint(p)==3)
     642             :   {
     643         109 :     ulong pp = to_Flx(&x, &y, p);
     644         109 :     M = FlxM_to_ZXM(Flx_halfgcd(x, y, pp));
     645             :   }
     646             :   else
     647             :   {
     648         356 :     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         356 :     if (degpol(y)<degpol(x)) return FpX_halfgcd_i(x,y,p);
     655          11 :     q = FpX_divrem(y,x,p,&r);
     656          11 :     M = FpX_halfgcd_i(x,r,p);
     657          11 :     gcoeff(M,1,1) = FpX_sub(gcoeff(M,1,1), FpX_mul(q, gcoeff(M,1,2), p), p);
     658          11 :     gcoeff(M,2,1) = FpX_sub(gcoeff(M,2,1), FpX_mul(q, gcoeff(M,2,2), p), p);
     659             :   }
     660         120 :   return gerepilecopy(av, M);
     661             : }
     662             : 
     663             : static GEN
     664       52006 : FpX_gcd_basecase(GEN a, GEN b, GEN p)
     665             : {
     666       52006 :   pari_sp av = avma, av0=avma;
     667      696605 :   while (signe(b))
     668             :   {
     669             :     GEN c;
     670      592656 :     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      592656 :     av = avma; c = FpX_rem(a,b,p); a=b; b=c;
     676             :   }
     677       51943 :   avma = av; return a;
     678             : }
     679             : 
     680             : GEN
     681      440720 : FpX_gcd(GEN x, GEN y, GEN p)
     682             : {
     683      440720 :   pari_sp av = avma;
     684      440720 :   if (lgefint(p)==3)
     685             :   {
     686             :     ulong pp;
     687      388533 :     (void)new_chunk((lg(x) + lg(y)) << 2); /* scratch space */
     688      388533 :     pp = to_Flx(&x, &y, p);
     689      388533 :     x = Flx_gcd(x, y, pp);
     690      388533 :     avma = av; return Flx_to_ZX(x);
     691             :   }
     692       52187 :   x = FpX_red(x, p);
     693       52187 :   y = FpX_red(y, p);
     694       52187 :   if (!signe(x)) return gerepileupto(av, y);
     695      104012 :   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       52006 :   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          77 : FpX_gcd_check(GEN x, GEN y, GEN p)
     713             : {
     714          77 :   pari_sp av = avma;
     715             :   GEN a,b,c;
     716             : 
     717          77 :   a = FpX_red(x, p);
     718          77 :   b = FpX_red(y, p);
     719         952 :   while (signe(b))
     720             :   {
     721         826 :     GEN g = gcdii(p, leading_coeff(b));
     722         826 :     if (!equali1(g)) return gerepileuptoint(av,g);
     723         798 :     c = FpX_rem(a,b,p); a = b; b = c;
     724             :   }
     725          49 :   avma = av; return NULL;
     726             : }
     727             : 
     728             : static GEN
     729      245786 : FpX_extgcd_basecase(GEN a, GEN b, GEN p, GEN *ptu, GEN *ptv)
     730             : {
     731      245786 :   pari_sp av=avma;
     732             :   GEN u,v,d,d1,v1;
     733      245786 :   long vx = varn(a);
     734      245786 :   d = a; d1 = b;
     735      245786 :   v = pol_0(vx); v1 = pol_1(vx);
     736     1078703 :   while (signe(d1))
     737             :   {
     738      587131 :     GEN r, q = FpX_divrem(d,d1,p, &r);
     739      587131 :     v = FpX_sub(v,FpX_mul(q,v1,p),p);
     740      587131 :     u=v; v=v1; v1=u;
     741      587131 :     u=r; d=d1; d1=u;
     742      587131 :     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      245786 :   if (ptu) *ptu = FpX_div(FpX_sub(d,FpX_mul(b,v,p),p),a,p);
     749      245786 :   *ptv = v; return d;
     750             : }
     751             : 
     752             : static GEN
     753           4 : FpX_extgcd_halfgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     754             : {
     755           4 :   pari_sp av=avma;
     756           4 :   GEN u,v,R = matid2_FpXM(varn(x));
     757          12 :   while (lg(y)>FpX_EXTGCD_LIMIT)
     758             :   {
     759             :     GEN M, c;
     760           4 :     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           4 :     M = FpX_halfgcd(x,y, p);
     767           4 :     c = FpXM_FpX_mul2(M, x,y, p);
     768           4 :     R = FpXM_mul2(M, R, p);
     769           4 :     x = gel(c,1); y = gel(c,2);
     770           4 :     gerepileall(av,3,&x,&y,&R);
     771             :   }
     772           4 :   y = FpX_extgcd_basecase(x,y,p,&u,&v);
     773           4 :   if (ptu) *ptu = FpX_addmulmul(u,v,gcoeff(R,1,1),gcoeff(R,2,1),p);
     774           4 :   *ptv = FpX_addmulmul(u,v,gcoeff(R,1,2),gcoeff(R,2,2),p);
     775           4 :   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      360419 : FpX_extgcd(GEN x, GEN y, GEN p, GEN *ptu, GEN *ptv)
     782             : {
     783             :   GEN d;
     784      360419 :   pari_sp ltop=avma;
     785      360419 :   if (lgefint(p)==3)
     786             :   {
     787      114633 :     ulong pp = to_Flx(&x, &y, p);
     788      114633 :     d = Flx_extgcd(x,y, pp, ptu,ptv);
     789      114633 :     d = Flx_to_ZX(d);
     790      114633 :     if (ptu) *ptu=Flx_to_ZX(*ptu);
     791      114633 :     *ptv=Flx_to_ZX(*ptv);
     792             :   }
     793             :   else
     794             :   {
     795      245786 :     x = FpX_red(x, p);
     796      245786 :     y = FpX_red(y, p);
     797      245786 :     if (lg(y)>FpX_EXTGCD_LIMIT)
     798           4 :       d = FpX_extgcd_halfgcd(x, y, p, ptu, ptv);
     799             :     else
     800      245782 :       d = FpX_extgcd_basecase(x, y, p, ptu, ptv);
     801             :   }
     802      360419 :   gerepileall(ltop,ptu?3:2,&d,ptv,ptu);
     803      360419 :   return d;
     804             : }
     805             : 
     806             : GEN
     807       13377 : FpX_rescale(GEN P, GEN h, GEN p)
     808             : {
     809       13377 :   long i, l = lg(P);
     810       13377 :   GEN Q = cgetg(l,t_POL), hi = h;
     811       13377 :   Q[l-1] = P[l-1];
     812       61054 :   for (i=l-2; i>=2; i--)
     813             :   {
     814       61054 :     gel(Q,i) = Fp_mul(gel(P,i), hi, p);
     815       61054 :     if (i == 2) break;
     816       47677 :     hi = Fp_mul(hi,h, p);
     817             :   }
     818       13377 :   Q[1] = P[1]; return Q;
     819             : }
     820             : 
     821             : GEN
     822      635776 : FpX_deriv(GEN x, GEN p) { return FpX_red(ZX_deriv(x), p); }
     823             : 
     824             : int
     825        2107 : FpX_is_squarefree(GEN f, GEN p)
     826             : {
     827        2107 :   pari_sp av = avma;
     828        2107 :   GEN z = FpX_gcd(f,FpX_deriv(f,p),p);
     829        2107 :   avma = av;
     830        2107 :   return degpol(z)==0;
     831             : }
     832             : 
     833             : GEN
     834       19161 : random_FpX(long d1, long v, GEN p)
     835             : {
     836       19161 :   long i, d = d1+2;
     837       19161 :   GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
     838       19161 :   for (i=2; i<d; i++) gel(y,i) = randomi(p);
     839       19161 :   return FpX_renormalize(y,d);
     840             : }
     841             : 
     842             : GEN
     843         566 : FpX_dotproduct(GEN x, GEN y, GEN p)
     844             : {
     845         566 :   long i, l = minss(lg(x), lg(y));
     846             :   pari_sp av;
     847             :   GEN c;
     848         566 :   if (l == 2) return gen_0;
     849         566 :   av = avma; c = mulii(gel(x,2),gel(y,2));
     850         566 :   for (i=3; i<l; i++) c = addii(c, mulii(gel(x,i),gel(y,i)));
     851         566 :   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      371459 : FpX_eval(GEN x,GEN y,GEN p)
     861             : {
     862             :   pari_sp av;
     863             :   GEN p1,r,res;
     864      371459 :   long j, i=lg(x)-1;
     865      371459 :   if (i<=2 || !signe(y))
     866      154238 :     return (i==1)? gen_0: modii(gel(x,2),p);
     867      217221 :   res=cgeti(lgefint(p));
     868      217221 :   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     1041856 :   for (i--; i>=2; i=j-1)
     872             :   {
     873     1288900 :     for (j=i; !signe(gel(x,j)); j--)
     874      464265 :       if (j==2)
     875             :       {
     876       25365 :         if (i!=j) y = Fp_powu(y,i-j+1,p);
     877       25365 :         p1=mulii(p1,y);
     878       25365 :         goto fppoleval;/*sorry break(2) no implemented*/
     879             :       }
     880      824635 :     r = (i==j)? y: Fp_powu(y,i-j+1,p);
     881      824635 :     p1 = Fp_addmul(gel(x,j), p1, r, p);
     882      824635 :     if ((i & 7) == 0) { affii(p1, res); p1 = res; avma = av; }
     883             :   }
     884             :  fppoleval:
     885      217221 :   modiiz(p1,p,res);
     886      217221 :   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         980 : FpX_chinese_coprime(GEN x,GEN y,GEN Tx,GEN Ty,GEN Tz,GEN p)
     897             : {
     898         980 :   pari_sp av = avma;
     899             :   GEN ax,p1;
     900         980 :   ax = FpX_mul(FpXQ_inv(Tx,Ty,p), Tx,p);
     901         980 :   p1 = FpX_mul(ax, FpX_sub(y,x,p),p);
     902         980 :   p1 = FpX_add(x,p1,p);
     903         980 :   if (!Tz) Tz=FpX_mul(Tx,Ty,p);
     904         980 :   p1 = FpX_rem(p1,Tz,p);
     905         980 :   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        5853 : FpX_resultant(GEN a, GEN b, GEN p)
     911             : {
     912             :   long da,db,dc;
     913             :   pari_sp av;
     914        5853 :   GEN c,lb, res = gen_1;
     915             : 
     916        5853 :   if (!signe(a) || !signe(b)) return gen_0;
     917        5853 :   da = degpol(a);
     918        5853 :   db = degpol(b);
     919        5853 :   if (db > da)
     920             :   {
     921           0 :     swapspec(a,b, da,db);
     922           0 :     if (both_odd(da,db)) res = subii(p, res);
     923             :   }
     924        5853 :   if (!da) return gen_1; /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
     925        5853 :   av = avma;
     926       33724 :   while (db)
     927             :   {
     928       22025 :     lb = gel(b,db+2);
     929       22025 :     c = FpX_rem(a,b, p);
     930       22025 :     a = b; b = c; dc = degpol(c);
     931       22025 :     if (dc < 0) { avma = av; return NULL; }
     932             : 
     933       22018 :     if (both_odd(da,db)) res = subii(p, res);
     934       22018 :     if (!equali1(lb)) res = Fp_mul(res, Fp_powu(lb, da - dc, p), p);
     935       22018 :     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       22018 :     da = db; /* = degpol(a) */
     941       22018 :     db = dc; /* = degpol(b) */
     942             :   }
     943        5846 :   res = Fp_mul(res, Fp_powu(gel(b,2), da, p), p);
     944        5846 :   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          28 : FpX_disc(GEN P, GEN p)
     950             : {
     951          28 :   pari_sp av = avma;
     952          28 :   GEN L, dP = FpX_deriv(P,p), D = FpX_resultant(P, dP, p);
     953             :   long dd;
     954          28 :   if (!D || !signe(D)) return gen_0;
     955          21 :   dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
     956          21 :   L = leading_coeff(P);
     957          21 :   if (dd && !equali1(L))
     958           7 :     D = (dd == -1)? Fp_div(D,L,p): Fp_mul(D, Fp_powu(L, dd, p), p);
     959          21 :   if (degpol(P) & 2) D = Fp_neg(D ,p);
     960          21 :   return gerepileuptoint(av, D);
     961             : }
     962             : 
     963             : GEN
     964       18692 : FpXV_prod(GEN V, GEN p)
     965             : {
     966             :   struct _FpXQ D;
     967       18692 :   D.p = p;
     968       18692 :   return gen_product(V, (void *)&D, &_FpX_mul);
     969             : }
     970             : 
     971             : GEN
     972        5341 : FpV_roots_to_pol(GEN V, GEN p, long v)
     973             : {
     974        5341 :   pari_sp ltop=avma;
     975             :   long i;
     976        5341 :   GEN g=cgetg(lg(V),t_VEC);
     977       40978 :   for(i=1;i<lg(V);i++)
     978       35637 :     gel(g,i) = deg1pol_shallow(gen_1,modii(negi(gel(V,i)),p),v);
     979        5341 :   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        4195 : FpV_inv(GEN x, GEN p)
     986             : {
     987        4195 :   long i, lx = lg(x);
     988        4195 :   GEN u, y = cgetg(lx, t_VEC);
     989             : 
     990        4195 :   gel(y,1) = gel(x,1);
     991        4195 :   for (i=2; i<lx; i++) gel(y,i) = Fp_mul(gel(y,i-1), gel(x,i), p);
     992             : 
     993        4195 :   u = Fp_inv(gel(y,--i), p);
     994      328365 :   for ( ; i > 1; i--)
     995             :   {
     996      324170 :     gel(y,i) = Fp_mul(u, gel(y,i-1), p);
     997      324170 :     u = Fp_mul(u, gel(x,i), p); /* u = 1 / (x[1] ... x[i-1]) */
     998             :   }
     999        4195 :   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        1498 : FpX_invBarrett_basecase(GEN T, GEN p)
    1027             : {
    1028        1498 :   long i, l=lg(T)-1, lr = l-1, k;
    1029        1498 :   GEN r=cgetg(lr, t_POL); r[1]=T[1];
    1030        1498 :   gel(r,2) = gen_1;
    1031       86132 :   for (i=3; i<lr; i++)
    1032             :   {
    1033       84634 :     pari_sp av = avma;
    1034       84634 :     GEN u = gel(T,l-i+2);
    1035     2684325 :     for (k=3; k<i; k++)
    1036     2599691 :       u = addii(u, mulii(gel(T,l-i+k), gel(r,k)));
    1037       84634 :     gel(r,i) = gerepileupto(av, modii(negi(u), p));
    1038             :   }
    1039        1498 :   return FpX_renormalize(r,lr);
    1040             : }
    1041             : 
    1042             : /* Return new lgpol */
    1043             : static long
    1044      206497 : ZX_lgrenormalizespec(GEN x, long lx)
    1045             : {
    1046             :   long i;
    1047      253414 :   for (i = lx-1; i>=0; i--)
    1048      253412 :     if (signe(gel(x,i))) break;
    1049      206497 :   return i+1;
    1050             : }
    1051             : 
    1052             : INLINE GEN
    1053      196316 : FpX_recipspec(GEN x, long l, long n)
    1054             : {
    1055      196316 :   return RgX_recipspec_shallow(x, l, n);
    1056             : }
    1057             : 
    1058             : static GEN
    1059         528 : FpX_invBarrett_Newton(GEN T, GEN p)
    1060             : {
    1061         528 :   pari_sp av = avma;
    1062         528 :   long nold, lx, lz, lq, l = degpol(T), i, lQ;
    1063         528 :   GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
    1064         528 :   ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
    1065         528 :   for (i=0;i<l;i++) gel(x,i) = gen_0;
    1066         528 :   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         528 :   gel(x,0) = Fp_inv(gel(q,0), p);
    1071         528 :   if (lQ>1) gel(q,1) = Fp_red(gel(q,1), p);
    1072         528 :   if (lQ>1 && signe(gel(q,1)))
    1073         482 :   {
    1074         477 :     GEN u = gel(q, 1);
    1075         477 :     if (!equali1(gel(x,0))) u = Fp_mul(u, Fp_sqr(gel(x,0), p), p);
    1076         477 :     gel(x,1) = Fp_neg(u, p); lx = 2;
    1077             :   }
    1078             :   else
    1079          51 :     lx = 1;
    1080         533 :   nold = 1;
    1081        4826 :   for (; mask > 1; )
    1082             :   { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
    1083        3765 :     long i, lnew, nnew = nold << 1;
    1084             : 
    1085        3765 :     if (mask & 1) nnew--;
    1086        3765 :     mask >>= 1;
    1087             : 
    1088        3765 :     lnew = nnew + 1;
    1089        3765 :     lq = ZX_lgrenormalizespec(q, minss(lQ,lnew));
    1090        3766 :     z = FpX_mulspec(x, q, p, lx, lq); /* FIXME: high product */
    1091        3766 :     lz = lgpol(z); if (lz > lnew) lz = lnew;
    1092        3766 :     z += 2;
    1093             :     /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
    1094        3766 :     for (i = nold; i < lz; i++) if (signe(gel(z,i))) break;
    1095        3766 :     nold = nnew;
    1096        3766 :     if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
    1097             : 
    1098             :     /* z + i represents (x*q - 1) / t^i */
    1099        3543 :     lz = ZX_lgrenormalizespec (z+i, lz-i);
    1100        3543 :     z = FpX_mulspec(x, z+i, p, lx, lz); /* FIXME: low product */
    1101        3543 :     lz = lgpol(z); z += 2;
    1102        3543 :     if (lz > lnew-i) lz = ZX_lgrenormalizespec(z, lnew-i);
    1103             : 
    1104        3543 :     lx = lz+ i;
    1105        3543 :     y  = x + i; /* x -= z * t^i, in place */
    1106        3543 :     for (i = 0; i < lz; i++) gel(y,i) = Fp_neg(gel(z,i), p);
    1107             :   }
    1108         528 :   x -= 2; setlg(x, lx + 2); x[1] = T[1];
    1109         528 :   return gerepilecopy(av, x);
    1110             : }
    1111             : 
    1112             : /* 1/polrecip(T)+O(x^(deg(T)-1)) */
    1113             : GEN
    1114        2058 : FpX_invBarrett(GEN T, GEN p)
    1115             : {
    1116        2058 :   pari_sp ltop = avma;
    1117        2058 :   long l = lg(T);
    1118             :   GEN r;
    1119        2058 :   if (l<5) return pol_0(varn(T));
    1120        2026 :   if (l<=FpX_INVBARRETT_LIMIT)
    1121             :   {
    1122        1498 :     GEN c = gel(T,l-1), ci=gen_1;
    1123        1498 :     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        1498 :       r = FpX_invBarrett_basecase(T, p);
    1131             :   }
    1132             :   else
    1133         528 :     r = FpX_invBarrett_Newton(T, p);
    1134        2026 :   return gerepileupto(ltop, r);
    1135             : }
    1136             : 
    1137             : GEN
    1138      421526 : FpX_get_red(GEN T, GEN p)
    1139             : {
    1140      421526 :   if (typ(T)==t_POL && lg(T)>FpX_BARRETT_LIMIT)
    1141        1737 :     retmkvec2(FpX_invBarrett(T,p),T);
    1142      419789 :   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       97848 : FpX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN T, GEN p, GEN *pr)
    1149             : {
    1150             :   GEN q, r;
    1151       97848 :   long lt = degpol(T); /*We discard the leading term*/
    1152             :   long ld, lm, lT, lmg;
    1153       97848 :   ld = l-lt;
    1154       97848 :   lm = minss(ld, lgpol(mg));
    1155       97849 :   lT  = ZX_lgrenormalizespec(T+2,lt);
    1156       97849 :   lmg = ZX_lgrenormalizespec(mg+2,lm);
    1157       97849 :   q = FpX_recipspec(x+lt,ld,ld);              /* q = rec(x)     lq<=ld*/
    1158       97846 :   q = FpX_mulspec(q+2,mg+2,p,lgpol(q),lmg);    /* q = rec(x) * mg lq<=ld+lm*/
    1159       97849 :   q = FpX_recipspec(q+2,minss(ld,lgpol(q)),ld);/* q = rec (rec(x) * mg) lq<=ld*/
    1160       97849 :   if (!pr) return q;
    1161       97849 :   r = FpX_mulspec(q+2,T+2,p,lgpol(q),lT);      /* r = q*pol        lr<=ld+lt*/
    1162       97848 :   r = FpX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* r = x - r   lr<=lt */
    1163       97847 :   if (pr == ONLY_REM) return r;
    1164         592 :   *pr = r; return q;
    1165             : }
    1166             : 
    1167             : static GEN
    1168       97450 : FpX_divrem_Barrett_noGC(GEN x, GEN mg, GEN T, GEN p, GEN *pr)
    1169             : {
    1170       97450 :   GEN q = NULL, r = FpX_red(x, p);
    1171       97450 :   long l = lgpol(r), lt = degpol(T), lm = 2*lt-1;
    1172             :   long i;
    1173       97450 :   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       97450 :   if (lt <= 1)
    1181          32 :     return FpX_divrem_basecase(r,T,p,pr);
    1182       97418 :   if (pr != ONLY_REM && l>lm)
    1183             :   {
    1184         128 :     q = cgetg(l-lt+2, t_POL);
    1185         128 :     for (i=0;i<l-lt;i++) gel(q+2,i) = gen_0;
    1186             :   }
    1187      195266 :   while (l>lm)
    1188             :   {
    1189         430 :     GEN zr, zq = FpX_divrem_Barrettspec(r+2+l-lm,lm,mg,T,p,&zr);
    1190         430 :     long lz = lgpol(zr);
    1191         430 :     if (pr != ONLY_REM)
    1192             :     {
    1193         268 :       long lq = lgpol(zq);
    1194         268 :       for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
    1195             :     }
    1196         430 :     for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
    1197         430 :     l = l-lm+lz;
    1198             :   }
    1199       97418 :   if (pr != ONLY_REM)
    1200             :   {
    1201         162 :     if (l > lt)
    1202             :     {
    1203         162 :       GEN zq = FpX_divrem_Barrettspec(r+2,l,mg,T,p,&r);
    1204         162 :       if (!q) q = zq;
    1205             :       else
    1206             :       {
    1207         128 :         long lq = lgpol(zq);
    1208         128 :         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       97256 :     if (l > lt)
    1217       97256 :       r = FpX_divrem_Barrettspec(r+2, l, mg, T, p, ONLY_REM);
    1218             :     else
    1219           0 :       r = FpX_renormalize(r, l+2);
    1220       97255 :     r[1] = x[1]; return FpX_renormalize(r, lg(r));
    1221             :   }
    1222         162 :   if (pr) { r[1] = x[1]; r = FpX_renormalize(r, lg(r)); }
    1223         162 :   q[1] = x[1]; q = FpX_renormalize(q, lg(q));
    1224         162 :   if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
    1225         162 :   if (pr) *pr = r;
    1226         162 :   return q;
    1227             : }
    1228             : 
    1229             : GEN
    1230     3650590 : FpX_divrem(GEN x, GEN T, GEN p, GEN *pr)
    1231             : {
    1232     3650590 :   GEN B, y = get_FpX_red(T, &B);
    1233     3650590 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1234     3650590 :   if (pr==ONLY_REM) return FpX_rem(x, y, p);
    1235     3650590 :   if (!B && d+3 < FpX_DIVREM_BARRETT_LIMIT)
    1236     3649593 :     return FpX_divrem_basecase(x,y,p,pr);
    1237         997 :   else if (lgefint(p)==3)
    1238             :   {
    1239         807 :     pari_sp av = avma;
    1240         807 :     ulong pp = to_Flxq(&x, &T, p);
    1241         807 :     GEN z = Flx_divrem(x, T, pp, pr);
    1242         807 :     if (!z) return NULL;
    1243         807 :     if (!pr || pr == ONLY_DIVIDES)
    1244           8 :       return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1245         799 :     z = Flx_to_ZX(z);
    1246         799 :     *pr = Flx_to_ZX(*pr);
    1247         799 :     gerepileall(av, 2, &z, pr);
    1248         799 :     return z;
    1249             :   } else
    1250             :   {
    1251         190 :     pari_sp av=avma;
    1252         190 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1253         190 :     GEN q1 = FpX_divrem_Barrett_noGC(x,mg,y,p,pr);
    1254         190 :     if (!q1) {avma=av; return NULL;}
    1255         190 :     if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q1);
    1256         190 :     gerepileall(av,2,&q1,pr);
    1257         190 :     return q1;
    1258             :   }
    1259             : }
    1260             : 
    1261             : GEN
    1262    60961668 : FpX_rem(GEN x, GEN T, GEN p)
    1263             : {
    1264    60961668 :   GEN B, y = get_FpX_red(T, &B);
    1265    60961667 :   long dy = degpol(y), dx = degpol(x), d = dx-dy;
    1266    60961669 :   if (d < 0) return FpX_red(x,p);
    1267    58105540 :   if (!B && d+3 < FpX_REM_BARRETT_LIMIT)
    1268    57996245 :     return FpX_divrem_basecase(x,y,p,ONLY_REM);
    1269      109295 :   else if (lgefint(p)==3)
    1270             :   {
    1271       12035 :     pari_sp av = avma;
    1272       12035 :     ulong pp = to_Flxq(&x, &T, p);
    1273       12035 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, Flx_rem(x, T, pp)));
    1274             :   } else
    1275             :   {
    1276       97260 :     pari_sp av = avma;
    1277       97260 :     GEN mg = B? B: FpX_invBarrett(y, p);
    1278       97260 :     return gerepileupto(av, FpX_divrem_Barrett_noGC(x, mg, y, p, ONLY_REM));
    1279             :   }
    1280             : }
    1281             : 
    1282             : static GEN
    1283        8171 : deg2pol_shallow(GEN x2, GEN x1, GEN x0, long v)
    1284             : {
    1285        8171 :   GEN x = cgetg(5,t_POL);
    1286        8171 :   x[1] = evalsigne(1) | evalvarn(v);
    1287        8171 :   gel(x,2) = x0;
    1288        8171 :   gel(x,3) = x1;
    1289        8171 :   gel(x,4) = x2;
    1290        8171 :   return normalizepol_lg(x,5);
    1291             : }
    1292             : 
    1293             : static GEN
    1294        1772 : FpV_producttree(GEN xa, GEN s, GEN p, long vs)
    1295             : {
    1296        1772 :   long n = lg(xa)-1;
    1297        1772 :   long m = n==1 ? 1: expu(n-1)+1;
    1298        1772 :   long i, j, k, ls = lg(s);
    1299        1772 :   GEN T = cgetg(m+1, t_VEC);
    1300        1772 :   GEN t = cgetg(ls, t_VEC);
    1301       12918 :   for (j=1, k=1; j<ls; k+=s[j++])
    1302       22292 :     gel(t, j) = s[j] == 1 ?
    1303       19317 :              deg1pol(gen_1, Fp_neg(gel(xa,k), p), vs):
    1304       32684 :              deg2pol_shallow(gen_1,
    1305       16342 :                Fp_neg(Fp_add(gel(xa,k), gel(xa,k+1), p), p),
    1306       16342 :                Fp_mul(gel(xa,k), gel(xa,k+1), p), vs);
    1307        1772 :   gel(T,1) = t;
    1308        4888 :   for (i=2; i<=m; i++)
    1309             :   {
    1310        3116 :     GEN u = gel(T, i-1);
    1311        3116 :     long n = lg(u)-1;
    1312        3116 :     GEN t = cgetg(((n+1)>>1)+1, t_VEC);
    1313       12490 :     for (j=1, k=1; k<n; j++, k+=2)
    1314        9374 :       gel(t, j) = FpX_mul(gel(u, k), gel(u, k+1), p);
    1315        3116 :     gel(T, i) = t;
    1316             :   }
    1317        1772 :   return T;
    1318             : }
    1319             : 
    1320             : static GEN
    1321        1772 : FpX_FpV_multieval_tree(GEN P, GEN xa, GEN T, GEN p)
    1322             : {
    1323        1772 :   pari_sp av = avma;
    1324             :   long i,j,k;
    1325        1772 :   long m = lg(T)-1;
    1326             :   GEN t;
    1327        1772 :   GEN Tp = cgetg(m+1, t_VEC);
    1328        1772 :   gel(Tp, m) = mkvec(P);
    1329        4888 :   for (i=m-1; i>=1; i--)
    1330             :   {
    1331        3116 :     GEN u = gel(T, i);
    1332        3116 :     GEN v = gel(Tp, i+1);
    1333        3116 :     long n = lg(u)-1;
    1334        3116 :     t = cgetg(n+1, t_VEC);
    1335       12490 :     for (j=1, k=1; k<n; j++, k+=2)
    1336             :     {
    1337        9374 :       gel(t, k)   = FpX_rem(gel(v, j), gel(u, k), p);
    1338        9374 :       gel(t, k+1) = FpX_rem(gel(v, j), gel(u, k+1), p);
    1339             :     }
    1340        3116 :     gel(Tp, i) = t;
    1341             :   }
    1342             :   {
    1343        1772 :     GEN R = cgetg(lg(xa), t_VEC);
    1344        1772 :     GEN u = gel(T, i+1);
    1345        1772 :     GEN v = gel(Tp, i+1);
    1346        1772 :     long n = lg(u)-1;
    1347       12918 :     for (j=1, k=1; j<=n; j++)
    1348             :     {
    1349       11146 :       long c, d = degpol(gel(u,j));
    1350       30463 :       for (c=1; c<=d; c++, k++)
    1351       19317 :         gel(R,k) = FpX_eval(gel(v, j), gel(xa,k), p);
    1352             :     }
    1353        1772 :     return gerepileupto(av, R);
    1354             :   }
    1355             : }
    1356             : 
    1357             : static GEN
    1358           1 : FpVV_polint_tree(GEN T, GEN R, GEN s, GEN xa, GEN ya, GEN p, long vs)
    1359             : {
    1360           1 :   pari_sp av = avma;
    1361           1 :   long m = lg(T)-1;
    1362           1 :   long i, j, k, ls = lg(s);
    1363           1 :   GEN Tp = cgetg(m+1, t_VEC);
    1364           1 :   GEN t = cgetg(ls, t_VEC);
    1365           3 :   for (j=1, k=1; j<ls; k+=s[j++])
    1366           2 :     if (s[j]==2)
    1367             :     {
    1368           2 :       GEN a = Fp_mul(gel(ya,k), gel(R,k), p);
    1369           2 :       GEN b = Fp_mul(gel(ya,k+1), gel(R,k+1), p);
    1370           6 :       gel(t, j) = deg1pol(Fp_add(a, b, p),
    1371           2 :               Fp_neg(Fp_add(Fp_mul(gel(xa,k), b, p ),
    1372           2 :               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           1 :   gel(Tp, 1) = t;
    1377           2 :   for (i=2; i<=m; i++)
    1378             :   {
    1379           1 :     GEN u = gel(T, i-1);
    1380           1 :     GEN t = cgetg(lg(gel(T,i)), t_VEC);
    1381           1 :     GEN v = gel(Tp, i-1);
    1382           1 :     long n = lg(v)-1;
    1383           2 :     for (j=1, k=1; k<n; j++, k+=2)
    1384           3 :       gel(t, j) = FpX_add(ZX_mul(gel(u, k), gel(v, k+1)),
    1385           2 :                           ZX_mul(gel(u, k+1), gel(v, k)), p);
    1386           1 :     gel(Tp, i) = t;
    1387             :   }
    1388           1 :   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           1 : FpV_polint(GEN xa, GEN ya, GEN p, long vs)
    1402             : {
    1403           1 :   pari_sp av = avma;
    1404           1 :   GEN s = producttree_scheme(lg(xa)-1);
    1405           1 :   GEN T = FpV_producttree(xa, s, p, vs);
    1406           1 :   long m = lg(T)-1;
    1407           1 :   GEN P = FpX_deriv(gmael(T, m, 1), p);
    1408           1 :   GEN R = FpV_inv(FpX_FpV_multieval_tree(P, xa, T, p), p);
    1409           1 :   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        1771 : FpV_invVandermonde(GEN L, GEN den, GEN p)
    1429             : {
    1430        1771 :   pari_sp av = avma;
    1431        1771 :   long i, n = lg(L);
    1432             :   GEN M, R;
    1433        1771 :   GEN s = producttree_scheme(n-1);
    1434        1771 :   GEN tree = FpV_producttree(L, s, p, 0);
    1435        1771 :   long m = lg(tree)-1;
    1436        1771 :   GEN T = gmael(tree, m, 1);
    1437        1771 :   R = FpV_inv(FpX_FpV_multieval_tree(FpX_deriv(T, p), L, tree, p), p);
    1438        1771 :   if (den) R = FpC_Fp_mul(R, den, p);
    1439        1771 :   M = cgetg(n, t_MAT);
    1440       21084 :   for (i = 1; i < n; i++)
    1441             :   {
    1442       19313 :     GEN P = FpX_Fp_mul(FpX_div_by_X_x(T, gel(L,i), p, NULL), gel(R,i), p);
    1443       19313 :     gel(M,i) = RgX_to_RgC(P, n-1);
    1444             :   }
    1445        1771 :   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     2656646 : FpXQ_red(GEN x, GEN T, GEN p)
    1458             : {
    1459     2656646 :   GEN z = FpX_red(x,p);
    1460     2656646 :   return FpX_rem(z, T,p);
    1461             : }
    1462             : 
    1463             : GEN
    1464    39909742 : FpXQ_mul(GEN x,GEN y,GEN T,GEN p)
    1465             : {
    1466    39909742 :   GEN z = FpX_mul(x,y,p);
    1467    39909743 :   return FpX_rem(z, T, p);
    1468             : }
    1469             : 
    1470             : GEN
    1471     3058095 : FpXQ_sqr(GEN x, GEN T, GEN p)
    1472             : {
    1473     3058095 :   GEN z = FpX_sqr(x,p);
    1474     3058095 :   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      288473 : FpXQ_invsafe(GEN x, GEN y, GEN p)
    1481             : {
    1482      288473 :   GEN V, z = FpX_extgcd(get_FpX_mod(y), x, p, NULL, &V);
    1483      288473 :   if (degpol(z)) return NULL;
    1484      288473 :   z = Fp_invsafe(gel(z,2), p);
    1485      288473 :   if (!z) return NULL;
    1486      288473 :   return FpX_Fp_mul(V, z, p);
    1487             : }
    1488             : 
    1489             : GEN
    1490      288452 : FpXQ_inv(GEN x,GEN T,GEN p)
    1491             : {
    1492      288452 :   pari_sp av = avma;
    1493      288452 :   GEN U = FpXQ_invsafe(x, T, p);
    1494      288452 :   if (!U) pari_err_INV("FpXQ_inv",x);
    1495      288452 :   return gerepileupto(av, U);
    1496             : }
    1497             : 
    1498             : GEN
    1499      227394 : FpXQ_div(GEN x,GEN y,GEN T,GEN p)
    1500             : {
    1501      227394 :   pari_sp av = avma;
    1502      227394 :   return gerepileupto(av, FpXQ_mul(x,FpXQ_inv(y,T,p),T,p));
    1503             : }
    1504             : 
    1505             : static GEN
    1506     1069855 : _FpXQ_add(void *data, GEN x, GEN y)
    1507             : {
    1508             :   (void) data;
    1509     1069855 :   return ZX_add(x, y);
    1510             : }
    1511             : static GEN
    1512       57554 : _FpXQ_sub(void *data, GEN x, GEN y)
    1513             : {
    1514             :   (void) data;
    1515       57554 :   return ZX_sub(x, y);
    1516             : }
    1517             : static GEN
    1518     1198824 : _FpXQ_cmul(void *data, GEN P, long a, GEN x)
    1519             : {
    1520             :   (void) data;
    1521     1198824 :   return ZX_Z_mul(x, gel(P,a+2));
    1522             : }
    1523             : static GEN
    1524     2592011 : _FpXQ_sqr(void *data, GEN x)
    1525             : {
    1526     2592011 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1527     2592011 :   return FpXQ_sqr(x, D->T, D->p);
    1528             : }
    1529             : static GEN
    1530      871802 : _FpXQ_mul(void *data, GEN x, GEN y)
    1531             : {
    1532      871802 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1533      871802 :   return FpXQ_mul(x,y, D->T, D->p);
    1534             : }
    1535             : static GEN
    1536        3537 : _FpXQ_zero(void *data)
    1537             : {
    1538        3537 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1539        3537 :   return pol_0(get_FpX_var(D->T));
    1540             : }
    1541             : static GEN
    1542      311664 : _FpXQ_one(void *data)
    1543             : {
    1544      311664 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1545      311664 :   return pol_1(get_FpX_var(D->T));
    1546             : }
    1547             : static GEN
    1548      356237 : _FpXQ_red(void *data, GEN x)
    1549             : {
    1550      356237 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1551      356237 :   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       13559 : get_FpXQ_algebra(void **E, GEN T, GEN p)
    1559             : {
    1560       13559 :   GEN z = new_chunk(sizeof(struct _FpXQ));
    1561       13559 :   struct _FpXQ *e = (struct _FpXQ *) z;
    1562       13559 :   e->T = FpX_get_red(T, p);
    1563       13559 :   e->p  = p; *E = (void*)e;
    1564       13559 :   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      515287 : FpXQ_pow(GEN x, GEN n, GEN T, GEN p)
    1583             : {
    1584             :   struct _FpXQ D;
    1585             :   pari_sp av;
    1586      515287 :   long s = signe(n);
    1587             :   GEN y;
    1588      515287 :   if (!s) return pol_1(varn(x));
    1589      513582 :   if (is_pm1(n)) /* +/- 1 */
    1590        9053 :     return (s < 0)? FpXQ_inv(x,T,p): FpXQ_red(x,T,p);
    1591      504529 :   av = avma;
    1592      504529 :   if (!is_bigint(p))
    1593             :   {
    1594      342686 :     ulong pp = to_Flxq(&x, &T, p);
    1595      342686 :     y = Flxq_pow(x, n, T, pp);
    1596      342686 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1597             :   }
    1598      161843 :   if (s < 0) x = FpXQ_inv(x,T,p);
    1599      161843 :   D.p = p; D.T = FpX_get_red(T,p);
    1600      161843 :   y = gen_pow(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1601      161843 :   return gerepileupto(av, y);
    1602             : }
    1603             : 
    1604             : GEN /*Assume n is very small*/
    1605       62916 : FpXQ_powu(GEN x, ulong n, GEN T, GEN p)
    1606             : {
    1607             :   struct _FpXQ D;
    1608             :   pari_sp av;
    1609             :   GEN y;
    1610       62916 :   if (!n) return pol_1(varn(x));
    1611       62916 :   if (n==1) return FpXQ_red(x,T,p);
    1612       32718 :   av = avma;
    1613       32718 :   if (!is_bigint(p))
    1614             :   {
    1615       32397 :     ulong pp = to_Flxq(&x, &T, p);
    1616       32397 :     y = Flxq_powu(x, n, T, pp);
    1617       32397 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, y));
    1618             :   }
    1619         321 :   D.T = FpX_get_red(T, p); D.p = p;
    1620         321 :   y = gen_powu(x, n, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul);
    1621         321 :   return gerepileupto(av, y);
    1622             : }
    1623             : 
    1624             : /* generates the list of powers of x of degree 0,1,2,...,l*/
    1625             : GEN
    1626      185505 : FpXQ_powers(GEN x, long l, GEN T, GEN p)
    1627             : {
    1628             :   struct _FpXQ D;
    1629             :   int use_sqr;
    1630      185505 :   if (l>2 && lgefint(p) == 3) {
    1631      153251 :     pari_sp av = avma;
    1632      153251 :     ulong pp = to_Flxq(&x, &T, p);
    1633      153251 :     GEN z = FlxV_to_ZXV(Flxq_powers(x, l, T, pp));
    1634      153251 :     return gerepileupto(av, z);
    1635             :   }
    1636       32254 :   use_sqr = 2*degpol(x)>=get_FpX_degree(T);
    1637       32254 :   D.T = FpX_get_red(T,p); D.p = p;
    1638       32254 :   return gen_powers(x, l, use_sqr, (void*)&D, &_FpXQ_sqr, &_FpXQ_mul,&_FpXQ_one);
    1639             : }
    1640             : 
    1641             : GEN
    1642        4072 : FpXQ_matrix_pow(GEN y, long n, long m, GEN P, GEN l)
    1643             : {
    1644        4072 :   return RgXV_to_RgM(FpXQ_powers(y,m-1,P,l),n);
    1645             : }
    1646             : 
    1647             : GEN
    1648      156491 : FpX_Frobenius(GEN T, GEN p)
    1649             : {
    1650      156491 :   return FpXQ_pow(pol_x(get_FpX_var(T)), p, T, p);
    1651             : }
    1652             : 
    1653             : GEN
    1654        2693 : FpX_matFrobenius(GEN T, GEN p)
    1655             : {
    1656        2693 :   long n = get_FpX_degree(T);
    1657        2693 :   return FpXQ_matrix_pow(FpX_Frobenius(T, p), n, n, T, p);
    1658             : }
    1659             : 
    1660             : GEN
    1661      126121 : FpX_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1662             : {
    1663             :   struct _FpXQ D;
    1664      126121 :   D.T = FpX_get_red(T,p); D.p = p;
    1665      126121 :   return gen_bkeval_powers(Q,degpol(Q),x,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1666             : }
    1667             : 
    1668             : GEN
    1669      170420 : FpX_FpXQ_eval(GEN Q, GEN x, GEN T, GEN p)
    1670             : {
    1671             :   struct _FpXQ D;
    1672             :   int use_sqr;
    1673      170420 :   if (lgefint(p) == 3)
    1674             :   {
    1675      167164 :     pari_sp av = avma;
    1676      167164 :     ulong pp = to_Flxq(&x, &T, p);
    1677      167164 :     GEN z = Flx_Flxq_eval(ZX_to_Flx(Q, pp), x, T, pp);
    1678      167164 :     return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    1679             :   }
    1680        3256 :   use_sqr = 2*degpol(x) >= get_FpX_degree(T);
    1681        3256 :   D.T = FpX_get_red(T,p); D.p = p;
    1682        3256 :   return gen_bkeval(Q,degpol(Q),x,use_sqr,(void*)&D,&FpXQ_algebra,_FpXQ_cmul);
    1683             : }
    1684             : 
    1685             : GEN
    1686         854 : FpXC_FpXQV_eval(GEN P, GEN x, GEN T, GEN p)
    1687             : {
    1688         854 :   long i, l = lg(P);
    1689         854 :   GEN res = cgetg(l, t_COL);
    1690        3682 :   for (i=1; i<l; i++)
    1691        2828 :     gel(res,i) = FpX_FpXQV_eval(gel(P,i), x, T, p);
    1692         854 :   return res;
    1693             : }
    1694             : 
    1695             : GEN
    1696         308 : FpXM_FpXQV_eval(GEN Q, GEN x, GEN T, GEN p)
    1697             : {
    1698         308 :   long i, l = lg(Q);
    1699         308 :   GEN y = cgetg(l, t_MAT);
    1700        1162 :   for (i=1; i<l; i++)
    1701         854 :     gel(y,i) = FpXC_FpXQV_eval(gel(Q,i), x, T, p);
    1702         308 :   return y;
    1703             : }
    1704             : 
    1705             : GEN
    1706         616 : FpXQ_autpowers(GEN aut, long f, GEN T, GEN p)
    1707             : {
    1708         616 :   pari_sp av = avma;
    1709         616 :   long n = get_FpX_degree(T);
    1710         616 :   long i, nautpow = brent_kung_optpow(n-1,f-2,1);
    1711         616 :   long v = get_FpX_var(T);
    1712             :   GEN autpow, V;
    1713         616 :   T = FpX_get_red(T, p);
    1714         616 :   autpow = FpXQ_powers(aut, nautpow,T,p);
    1715         616 :   V = cgetg(f + 2, t_VEC);
    1716         616 :   gel(V,1) = pol_x(v); if (f==0) return gerepileupto(av, V);
    1717         616 :   gel(V,2) = gcopy(aut);
    1718        3150 :   for (i = 3; i <= f+1; i++)
    1719        2534 :     gel(V,i) = FpX_FpXQV_eval(gel(V,i-1),autpow,T,p);
    1720         616 :   return gerepileupto(av, V);
    1721             : }
    1722             : 
    1723             : static GEN
    1724         542 : FpXQ_autpow_sqr(void *E, GEN x)
    1725             : {
    1726         542 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1727         542 :   return FpX_FpXQ_eval(x, x, D->T, D->p);
    1728             : }
    1729             : 
    1730             : static GEN
    1731           7 : FpXQ_autpow_mul(void *E, GEN x, GEN y)
    1732             : {
    1733           7 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1734           7 :   return FpX_FpXQ_eval(x, y, D->T, D->p);
    1735             : }
    1736             : 
    1737             : GEN
    1738         514 : FpXQ_autpow(GEN x, ulong n, GEN T, GEN p)
    1739             : {
    1740             :   struct _FpXQ D;
    1741         514 :   D.T = FpX_get_red(T, p); D.p = p;
    1742         514 :   if (n==0) return pol_x(varn(x));
    1743         514 :   if (n==1) return ZX_copy(x);
    1744         514 :   return gen_powu(x,n,(void*)&D,FpXQ_autpow_sqr,FpXQ_autpow_mul);
    1745             : }
    1746             : 
    1747             : static GEN
    1748           7 : FpXQ_auttrace_mul(void *E, GEN x, GEN y)
    1749             : {
    1750           7 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1751           7 :   GEN T = D->T, p = D->p;
    1752           7 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1753           7 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1754           7 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1755           7 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1756           7 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1757           7 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1758           7 :   GEN a3 = FpX_add(a1, aphi, p);
    1759           7 :   return mkvec2(phi3, a3);
    1760             : }
    1761             : 
    1762             : static GEN
    1763           7 : FpXQ_auttrace_sqr(void *E, GEN x)
    1764           7 : { return FpXQ_auttrace_mul(E, x, x); }
    1765             : 
    1766             : GEN
    1767          14 : FpXQ_auttrace(GEN x, ulong n, GEN T, GEN p)
    1768             : {
    1769             :   struct _FpXQ D;
    1770          14 :   D.T = FpX_get_red(T, p); D.p = p;
    1771          14 :   return gen_powu(x,n,(void*)&D,FpXQ_auttrace_sqr,FpXQ_auttrace_mul);
    1772             : }
    1773             : 
    1774             : static GEN
    1775        1551 : FpXQ_autsum_mul(void *E, GEN x, GEN y)
    1776             : {
    1777        1551 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1778        1551 :   GEN T = D->T, p = D->p;
    1779        1551 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1780        1551 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1781        1551 :   ulong d = brent_kung_optpow(maxss(degpol(phi2),degpol(a2)),2,1);
    1782        1551 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1783        1551 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1784        1551 :   GEN aphi = FpX_FpXQV_eval(a2, V1, T, p);
    1785        1551 :   GEN a3 = FpXQ_mul(a1, aphi, T, p);
    1786        1551 :   return mkvec2(phi3, a3);
    1787             : }
    1788             : static GEN
    1789         809 : FpXQ_autsum_sqr(void *E, GEN x)
    1790         809 : { return FpXQ_autsum_mul(E, x, x); }
    1791             : 
    1792             : GEN
    1793         795 : FpXQ_autsum(GEN x, ulong n, GEN T, GEN p)
    1794             : {
    1795             :   struct _FpXQ D;
    1796         795 :   D.T = FpX_get_red(T, p); D.p = p;
    1797         795 :   return gen_powu(x,n,(void*)&D,FpXQ_autsum_sqr,FpXQ_autsum_mul);
    1798             : }
    1799             : 
    1800             : static GEN
    1801         308 : FpXQM_autsum_mul(void *E, GEN x, GEN y)
    1802             : {
    1803         308 :   struct _FpXQ *D = (struct _FpXQ*)E;
    1804         308 :   GEN T = D->T, p = D->p;
    1805         308 :   GEN phi1 = gel(x,1), a1 = gel(x,2);
    1806         308 :   GEN phi2 = gel(y,1), a2 = gel(y,2);
    1807         308 :   long g = lg(a2)-1, dT = get_FpX_degree(T);
    1808         308 :   ulong d = brent_kung_optpow(dT-1, g*g+1, 1);
    1809         308 :   GEN V1 = FpXQ_powers(phi1, d, T, p);
    1810         308 :   GEN phi3 = FpX_FpXQV_eval(phi2, V1, T, p);
    1811         308 :   GEN aphi = FpXM_FpXQV_eval(a2, V1, T, p);
    1812         308 :   GEN a3 = FqM_mul(a1, aphi, T, p);
    1813         308 :   return mkvec2(phi3, a3);
    1814             : }
    1815             : static GEN
    1816         210 : FpXQM_autsum_sqr(void *E, GEN x)
    1817         210 : { return FpXQM_autsum_mul(E, x, x); }
    1818             : 
    1819             : GEN
    1820         140 : FpXQM_autsum(GEN x, ulong n, GEN T, GEN p)
    1821             : {
    1822             :   struct _FpXQ D;
    1823         140 :   D.T = FpX_get_red(T, p); D.p = p;
    1824         140 :   return gen_powu(x, n, (void*)&D, FpXQM_autsum_sqr, FpXQM_autsum_mul);
    1825             : }
    1826             : 
    1827             : static long
    1828        5396 : bounded_order(GEN p, GEN b, long k)
    1829             : {
    1830             :   long i;
    1831        5396 :   GEN a=modii(p,b);
    1832       10876 :   for(i=1;i<k;i++)
    1833             :   {
    1834        6712 :     if (equali1(a))
    1835        1232 :       return i;
    1836        5480 :     a = Fp_mul(a,p,b);
    1837             :   }
    1838        4164 :   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      150915 : FpXQ_pow_Frobenius(GEN x, GEN n, GEN aut, GEN T, GEN p)
    1856             : {
    1857      150915 :   pari_sp av=avma;
    1858      150915 :   long d = get_FpX_degree(T);
    1859      150915 :   GEN an = absi(n), z, q;
    1860      150915 :   if (cmpii(an,p)<0 || cmpis(an,d)<=0)
    1861      145519 :     return FpXQ_pow(x, n, T, p);
    1862        5396 :   q = powiu(p, d);
    1863        5396 :   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        5396 :     GEN b = diviiround(q, an), a = subii(q, mulii(an,b));
    1871             :     GEN bb, u, v, autk;
    1872        5396 :     long vb = Z_pvalrem(b,p,&bb);
    1873        5396 :     long m, r, k = is_pm1(bb) ? 1 : bounded_order(p,bb,d);
    1874        5396 :     if (!k || d-vb<k) return FpXQ_pow(x,n, T, p);
    1875        1232 :     m = (d-vb)/k; r = (d-vb)%k;
    1876        1232 :     u = diviiexact(subis(powiu(p,k),1),bb);
    1877        1232 :     v = diviiexact(subii(powiu(p,r+vb),a),b);
    1878        1232 :     autk = k==1 ? aut: FpXQ_autpow(aut,k,T,p);
    1879        1232 :     if (r)
    1880             :     {
    1881         444 :       GEN autr = r==1 ? aut: FpXQ_autpow(aut,r,T,p);
    1882         444 :       z = FpX_FpXQ_eval(x,autr,T,p);
    1883         788 :     } else z = x;
    1884        1232 :     if (m > 1) z = gel(FpXQ_autsum(mkvec2(autk, z), m, T, p), 2);
    1885        1232 :     if (!is_pm1(u)) z = FpXQ_pow(z, u, T, p);
    1886        1232 :     if (signe(v)) z = FpXQ_mul(z, FpXQ_pow(x, v, T, p), T, p);
    1887             :   }
    1888        1232 :   return gerepileupto(av,signe(n)>0 ? z : FpXQ_inv(z,T,p));
    1889             : }
    1890             : 
    1891             : /* assume T irreducible mod p */
    1892             : int
    1893        3227 : FpXQ_issquare(GEN x, GEN T, GEN p)
    1894             : {
    1895             :   pari_sp av;
    1896             :   long res;
    1897        3227 :   if (lg(x) == 2 || absequalui(2, p)) return 1;
    1898        3227 :   if (lg(x) == 3) return Fq_issquare(gel(x,2), T, p);
    1899             :   /* Ng = g^((q-1)/(p-1)) */
    1900        3178 :   av = avma; res = kronecker(FpXQ_norm(x,T,p), p) == 1;
    1901        3178 :   avma = av; return res;
    1902             : }
    1903             : int
    1904       90811 : Fp_issquare(GEN x, GEN p)
    1905             : {
    1906       90811 :   if (absequalui(2, p)) return 1;
    1907       90811 :   return kronecker(x, p) == 1;
    1908             : }
    1909             : /* assume T irreducible mod p */
    1910             : int
    1911       90664 : Fq_issquare(GEN x, GEN T, GEN p)
    1912             : {
    1913       90664 :   if (typ(x) != t_INT) return FpXQ_issquare(x, T, p);
    1914       90629 :   return (T && ! odd(get_FpX_degree(T))) || Fp_issquare(x, p);
    1915             : }
    1916             : 
    1917             : long
    1918         133 : Fq_ispower(GEN x, GEN K, GEN T, GEN p)
    1919             : {
    1920         133 :   pari_sp av = avma;
    1921             :   long d;
    1922             :   GEN Q;
    1923         133 :   if (!T) return Fp_ispower(x,K,p);
    1924         112 :   d = get_FpX_degree(T);
    1925         112 :   if (!umodui(d, K)) return 1;
    1926          63 :   Q = subiu(powiu(p,d), 1);
    1927          63 :   Q = diviiexact(Q, gcdii(Q, K));
    1928          63 :   d = gequal1(Fq_pow(x, Q, T,p));
    1929          63 :   avma = av; return d;
    1930             : }
    1931             : 
    1932             : /* discrete log in FpXQ for a in Fp^*, g in FpXQ^* of order ord */
    1933             : GEN
    1934        5620 : Fp_FpXQ_log(GEN a, GEN g, GEN o, GEN T, GEN p)
    1935             : {
    1936        5620 :   pari_sp av = avma;
    1937             :   GEN q,n_q,ord,ordp, op;
    1938             : 
    1939        5620 :   if (equali1(a)) return gen_0;
    1940             :   /* p > 2 */
    1941             : 
    1942        3637 :   ordp = subis(p, 1); /* even */
    1943        3637 :   ord  = get_arith_Z(o);
    1944        3609 :   if (!ord) ord = T? subis(powiu(p, get_FpX_degree(T)), 1): ordp;
    1945        3609 :   if (equalii(a, ordp)) /* -1 */
    1946        2171 :     return gerepileuptoint(av, shifti(ord,-1));
    1947        1438 :   ordp = gcdii(ordp,ord);
    1948        1438 :   op = typ(o)==t_MAT ? famat_Z_gcd(o,ordp) : ordp;
    1949             : 
    1950        1438 :   q = NULL;
    1951        1438 :   if (T)
    1952             :   { /* we want < g > = Fp^* */
    1953        1438 :     if (!equalii(ord,ordp)) {
    1954        1428 :       q = diviiexact(ord,ordp);
    1955        1428 :       g = FpXQ_pow(g,q,T,p);
    1956             :     }
    1957        1438 :     g = constant_coeff(g);
    1958             :   }
    1959        1438 :   n_q = Fp_log(a,g,op,p);
    1960        1438 :   if (lg(n_q)==1) return gerepileuptoleaf(av, n_q);
    1961        1438 :   if (q) n_q = mulii(q, n_q);
    1962        1438 :   return gerepileuptoint(av, n_q);
    1963             : }
    1964             : 
    1965             : static GEN
    1966      150203 : _FpXQ_pow(void *data, GEN x, GEN n)
    1967             : {
    1968      150203 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1969      150203 :   return FpXQ_pow_Frobenius(x,n, D->aut, D->T, D->p);
    1970             : }
    1971             : 
    1972             : static GEN
    1973        3405 : _FpXQ_rand(void *data)
    1974             : {
    1975        3405 :   pari_sp av=avma;
    1976        3405 :   struct _FpXQ *D = (struct _FpXQ*)data;
    1977             :   GEN z;
    1978             :   do
    1979             :   {
    1980        3405 :     avma=av;
    1981        3405 :     z=random_FpX(get_FpX_degree(D->T),get_FpX_var(D->T),D->p);
    1982        3405 :   } while (!signe(z));
    1983        3405 :   return z;
    1984             : }
    1985             : 
    1986             : static GEN
    1987        1437 : _FpXQ_easylog(void *E, GEN a, GEN g, GEN ord)
    1988             : {
    1989        1437 :   struct _FpXQ *s=(struct _FpXQ*) E;
    1990        1437 :   if (degpol(a)) return NULL;
    1991        1420 :   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        1776 : get_FpXQ_star(void **E, GEN T, GEN p)
    1998             : {
    1999        1776 :   struct _FpXQ *e = (struct _FpXQ *) stack_malloc(sizeof(struct _FpXQ));
    2000        1776 :   e->T = T; e->p  = p; e->aut =  FpX_Frobenius(T, p);
    2001        1776 :   *E = (void*)e; return &FpXQ_star;
    2002             : }
    2003             : 
    2004             : GEN
    2005          30 : FpXQ_order(GEN a, GEN ord, GEN T, GEN p)
    2006             : {
    2007          30 :   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          30 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2018          30 :     return gen_order(a,ord,E,S);
    2019             :   }
    2020             : }
    2021             : 
    2022             : GEN
    2023       71767 : FpXQ_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2024             : {
    2025       71767 :   pari_sp av=avma;
    2026       71767 :   if (lgefint(p)==3)
    2027             :   {
    2028       71723 :     if (uel(p,2) == 2)
    2029             :     {
    2030       40817 :       GEN z = F2xq_log(ZX_to_F2x(a), ZX_to_F2x(g), ord,
    2031             :                                      ZX_to_F2x(get_FpX_mod(T)));
    2032       40817 :       return gerepileuptoleaf(av, z);
    2033             :     }
    2034             :     else
    2035             :     {
    2036       30906 :       ulong pp = to_Flxq(&a, &T, p);
    2037       30906 :       GEN z = Flxq_log(a, ZX_to_Flx(g, pp), ord, T, pp);
    2038       30906 :       return gerepileuptoleaf(av, z);
    2039             :     }
    2040             :   }
    2041             :   else
    2042             :   {
    2043             :     void *E;
    2044          44 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2045          44 :     GEN z = gen_PH_log(a,g,ord,E,S);
    2046          16 :     return gerepileuptoleaf(av, z);
    2047             :   }
    2048             : }
    2049             : 
    2050             : GEN
    2051      271845 : Fq_log(GEN a, GEN g, GEN ord, GEN T, GEN p)
    2052             : {
    2053      271845 :   if (!T) return Fp_log(a,g,ord,p);
    2054       75923 :   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       75923 :   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        8485 : FpXQ_sqrtn(GEN a, GEN n, GEN T, GEN p, GEN *zeta)
    2068             : {
    2069        8485 :   pari_sp av = avma;
    2070             :   GEN z;
    2071        8485 :   if (!signe(a))
    2072             :   {
    2073          70 :     long v=varn(a);
    2074          70 :     if (signe(n) < 0) pari_err_INV("FpXQ_sqrtn",a);
    2075          63 :     if (zeta) *zeta=pol_1(v);
    2076          63 :     return pol_0(v);
    2077             :   }
    2078        8415 :   if (lgefint(p)==3)
    2079             :   {
    2080        6713 :     if (uel(p,2) == 2)
    2081             :     {
    2082         798 :       z = F2xq_sqrtn(ZX_to_F2x(a), n, ZX_to_F2x(get_Flx_mod(T)), zeta);
    2083         798 :       if (!z) return NULL;
    2084         798 :       z = F2x_to_ZX(z);
    2085         798 :       if (!zeta) return gerepileuptoleaf(av, z);
    2086           7 :       *zeta=F2x_to_ZX(*zeta);
    2087             :     } else
    2088             :     {
    2089        5915 :       ulong pp = to_Flxq(&a, &T, p);
    2090        5915 :       z = Flxq_sqrtn(a, n, T, pp, zeta);
    2091        5915 :       if (!z) return NULL;
    2092        3598 :       if (!zeta) return Flx_to_ZX_inplace(gerepileuptoleaf(av, z));
    2093          70 :       z = Flx_to_ZX(z);
    2094          70 :       *zeta=Flx_to_ZX(*zeta);
    2095             :     }
    2096             :   }
    2097             :   else
    2098             :   {
    2099             :     void *E;
    2100        1702 :     const struct bb_group *S = get_FpXQ_star(&E,T,p);
    2101        1702 :     GEN o = addis(powiu(p,get_FpX_degree(T)),-1);
    2102        1702 :     z = gen_Shanks_sqrtn(a,n,o,zeta,E,S);
    2103        3403 :     if (!z) return NULL;
    2104        1660 :     if (!zeta) return gerepileupto(av, z);
    2105             :   }
    2106          78 :   gerepileall(av, 2, &z,zeta);
    2107          78 :   return z;
    2108             : }
    2109             : 
    2110             : GEN
    2111        7340 : FpXQ_sqrt(GEN a, GEN T, GEN p)
    2112             : {
    2113        7340 :   return FpXQ_sqrtn(a, gen_2, T, p, NULL);
    2114             : }
    2115             : 
    2116             : GEN
    2117        3179 : FpXQ_norm(GEN x, GEN TB, GEN p)
    2118             : {
    2119        3179 :   pari_sp av = avma;
    2120        3179 :   GEN T = get_FpX_mod(TB);
    2121        3179 :   GEN y = FpX_resultant(T, x, p);
    2122        3179 :   GEN L = leading_coeff(T);
    2123        3179 :   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       20804 : FpXQ_trace(GEN x, GEN TB, GEN p)
    2129             : {
    2130       20804 :   pari_sp av = avma;
    2131       20804 :   GEN T = get_FpX_mod(TB);
    2132       20804 :   GEN dT = FpX_deriv(T,p);
    2133       20804 :   long n = degpol(dT);
    2134       20804 :   GEN z = FpXQ_mul(x, dT, TB, p);
    2135       20804 :   if (degpol(z)<n) { avma = av; return gen_0; }
    2136       19628 :   return gerepileuptoint(av, Fp_div(gel(z,2+n), gel(T,3+n),p));
    2137             : }
    2138             : 
    2139             : GEN
    2140           1 : FpXQ_charpoly(GEN x, GEN T, GEN p)
    2141             : {
    2142           1 :   pari_sp ltop=avma;
    2143           1 :   long vT, v = fetch_var();
    2144             :   GEN R;
    2145           1 :   T = leafcopy(get_FpX_mod(T));
    2146           1 :   vT = varn(T); setvarn(T, v);
    2147           1 :   x = leafcopy(x); setvarn(x, v);
    2148           1 :   R = FpX_FpXY_resultant(T, deg1pol_shallow(gen_1,FpX_neg(x,p),vT),p);
    2149           1 :   (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         108 : FpXn_mul(GEN a, GEN b, long n, GEN p)
    2158             : {
    2159         108 :   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          30 : FpXQ_transmul_init(GEN tau, GEN T, GEN p)
    2167             : {
    2168             :   GEN bht;
    2169          30 :   GEN h, Tp = get_FpX_red(T, &h);
    2170          30 :   long n = degpol(Tp), vT = varn(Tp);
    2171          30 :   GEN ft = FpX_recipspec(Tp+2, n+1, n+1);
    2172          30 :   GEN bt = FpX_recipspec(tau+2, lgpol(tau), n);
    2173          30 :   setvarn(ft, vT); setvarn(bt, vT);
    2174          30 :   if (h)
    2175           0 :     bht = FpXn_mul(bt, h, n-1, p);
    2176             :   else
    2177             :   {
    2178          30 :     GEN bh = FpX_div(RgX_shift_shallow(tau, n-1), T, p);
    2179          30 :     bht = FpX_recipspec(bh+2, lgpol(bh), n-1);
    2180          30 :     setvarn(bht, vT);
    2181             :   }
    2182          30 :   return mkvec3(bt, bht, ft);
    2183             : }
    2184             : 
    2185             : static GEN
    2186         123 : FpXQ_transmul(GEN tau, GEN a, long n, GEN p)
    2187             : {
    2188         123 :   pari_sp ltop = avma;
    2189             :   GEN t1, t2, t3, vec;
    2190         123 :   GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
    2191         123 :   if (signe(a)==0) return pol_0(varn(a));
    2192         123 :   t2 = RgX_shift_shallow(FpX_mul(bt, a, p),1-n);
    2193         123 :   if (signe(bht)==0) return gerepilecopy(ltop, t2);
    2194         108 :   t1 = RgX_shift_shallow(FpX_mul(ft, a, p),-n);
    2195         108 :   t3 = FpXn_mul(t1, bht, n-1, p);
    2196         108 :   vec = FpX_sub(t2, RgX_shift_shallow(t3, 1), p);
    2197         108 :   return gerepileupto(ltop, vec);
    2198             : }
    2199             : 
    2200             : GEN
    2201        3277 : FpXQ_minpoly(GEN x, GEN T, GEN p)
    2202             : {
    2203        3277 :   pari_sp ltop = avma;
    2204             :   long vT, n;
    2205             :   GEN v_x, g, tau;
    2206        3277 :   if (lgefint(p)==3)
    2207             :   {
    2208        3262 :     ulong pp = to_Flxq(&x, &T, p);
    2209        3262 :     GEN g = Flxq_minpoly(x, T, pp);
    2210        3262 :     return gerepileupto(ltop, Flx_to_ZX(g));
    2211             :   }
    2212          15 :   vT = get_FpX_var(T);
    2213          15 :   n = get_FpX_degree(T);
    2214          15 :   g = pol_1(vT);
    2215          15 :   tau = pol_1(vT);
    2216          15 :   T = FpX_get_red(T, p);
    2217          15 :   x = FpXQ_red(x, T, p);
    2218          15 :   v_x = FpXQ_powers(x, usqrt(2*n), T, p);
    2219          45 :   while(signe(tau) != 0)
    2220             :   {
    2221             :     long i, j, m, k1;
    2222             :     GEN M, v, tr;
    2223             :     GEN g_prime, c;
    2224          15 :     if (degpol(g) == n) { tau = pol_1(vT); g = pol_1(vT); }
    2225          15 :     v = random_FpX(n, vT, p);
    2226          15 :     tr = FpXQ_transmul_init(tau, T, p);
    2227          15 :     v = FpXQ_transmul(tr, v, n, p);
    2228          15 :     m = 2*(n-degpol(g));
    2229          15 :     k1 = usqrt(m);
    2230          15 :     tr = FpXQ_transmul_init(gel(v_x,k1+1), T, p);
    2231          15 :     c = cgetg(m+2,t_POL);
    2232          15 :     c[1] = evalsigne(1)|evalvarn(vT);
    2233         123 :     for (i=0; i<m; i+=k1)
    2234             :     {
    2235         108 :       long mj = minss(m-i, k1);
    2236         674 :       for (j=0; j<mj; j++)
    2237         566 :         gel(c,m+1-(i+j)) = FpX_dotproduct(v, gel(v_x,j+1), p);
    2238         108 :       v = FpXQ_transmul(tr, v, n, p);
    2239             :     }
    2240          15 :     c = FpX_renormalize(c, m+2);
    2241             :     /* now c contains <v,x^i> , i = 0..m-1  */
    2242          15 :     M = FpX_halfgcd(pol_xn(m, vT), c, p);
    2243          15 :     g_prime = gmael(M, 2, 2);
    2244          15 :     if (degpol(g_prime) < 1) continue;
    2245          15 :     g = FpX_mul(g, g_prime, p);
    2246          15 :     tau = FpXQ_mul(tau, FpX_FpXQV_eval(g_prime, v_x, T, p), T, p);
    2247             :   }
    2248          15 :   g = FpX_normalize(g,p);
    2249          15 :   return gerepilecopy(ltop,g);
    2250             : }
    2251             : 
    2252             : GEN
    2253           8 : FpXQ_conjvec(GEN x, GEN T, GEN p)
    2254             : {
    2255           8 :   pari_sp av=avma;
    2256             :   long i;
    2257           8 :   long n = get_FpX_degree(T), v = varn(x);
    2258           8 :   GEN M = FpX_matFrobenius(T, p);
    2259           8 :   GEN z = cgetg(n+1,t_COL);
    2260           8 :   gel(z,1) = RgX_to_RgC(x,n);
    2261           8 :   for (i=2; i<=n; i++) gel(z,i) = FpM_FpC_mul(M,gel(z,i-1),p);
    2262           8 :   gel(z,1) = x;
    2263           8 :   for (i=2; i<=n; i++) gel(z,i) = RgV_to_RgX(gel(z,i),v);
    2264           8 :   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        1465 : gener_FpXQ_i(GEN T, GEN p, GEN p_1, GEN Lp, GEN Lq)
    2274             : {
    2275             :   pari_sp av;
    2276        1465 :   long vT = varn(T), f = degpol(T), l = lg(Lq);
    2277        1465 :   GEN F = FpX_Frobenius(T, p);
    2278        1465 :   int p_is_2 = is_pm1(p_1);
    2279        3146 :   for (av = avma;; avma = av)
    2280             :   {
    2281        3146 :     GEN t, g = random_FpX(f, vT, p);
    2282             :     long i;
    2283        3146 :     if (degpol(g) < 1) continue;
    2284        3034 :     if (p_is_2)
    2285         392 :       t = g;
    2286             :     else
    2287             :     {
    2288        2642 :       t = FpX_resultant(T, g, p); /* Ng = g^((q-1)/(p-1)), assuming T monic */
    2289        2642 :       if (kronecker(t, p) == 1) continue;
    2290        1307 :       if (lg(Lp) > 1 && !is_gener_Fp(t, p, p_1, Lp)) continue;
    2291        1301 :       t = FpXQ_pow(g, shifti(p_1,-1), T, p);
    2292             :     }
    2293        2177 :     for (i = 1; i < l; i++)
    2294             :     {
    2295         712 :       GEN a = FpXQ_pow_Frobenius(t, gel(Lq,i), F, T, p);
    2296         712 :       if (!degpol(a) && equalii(gel(a,2), p_1)) break;
    2297             :     }
    2298        3158 :     if (i == l) return g;
    2299        1681 :   }
    2300             : }
    2301             : 
    2302             : GEN
    2303        6841 : gener_FpXQ(GEN T, GEN p, GEN *po)
    2304             : {
    2305        6841 :   long i, j, f = get_FpX_degree(T);
    2306             :   GEN g, Lp, Lq, p_1, q_1, N, o;
    2307        6841 :   pari_sp av = avma;
    2308             : 
    2309        6841 :   p_1 = subiu(p,1);
    2310        6841 :   if (f == 1) {
    2311             :     GEN Lp, fa;
    2312           7 :     o = p_1;
    2313           7 :     fa = Z_factor(o);
    2314           7 :     Lp = gel(fa,1);
    2315           7 :     Lp = vecslice(Lp, 2, lg(Lp)-1); /* remove 2 for efficiency */
    2316             : 
    2317           7 :     g = cgetg(3, t_POL);
    2318           7 :     g[1] = evalsigne(1) | evalvarn(get_FpX_var(T));
    2319           7 :     gel(g,2) = pgener_Fp_local(p, Lp);
    2320           7 :     if (po) *po = mkvec2(o, fa);
    2321           7 :     return g;
    2322             :   }
    2323        6834 :   if (lgefint(p) == 3)
    2324             :   {
    2325        6811 :     ulong pp = to_Flxq(NULL, &T, p);
    2326        6811 :     g = gener_Flxq(T, pp, po);
    2327        6811 :     if (!po) return Flx_to_ZX_inplace(gerepileuptoleaf(av, g));
    2328        6811 :     g = Flx_to_ZX(g);
    2329        6811 :     gerepileall(av, 2, &g, po);
    2330        6811 :     return g;
    2331             :   }
    2332             :   /* p now odd */
    2333          23 :   q_1 = subiu(powiu(p,f), 1);
    2334          23 :   N = diviiexact(q_1, p_1);
    2335          23 :   Lp = odd_prime_divisors(p_1);
    2336          23 :   for (i=lg(Lp)-1; i; i--) gel(Lp,i) = diviiexact(p_1, gel(Lp,i));
    2337          23 :   o = factor_pn_1(p,f);
    2338          23 :   Lq = leafcopy( gel(o, 1) );
    2339         199 :   for (i = j = 1; i < lg(Lq); i++)
    2340             :   {
    2341         176 :     if (remii(p_1, gel(Lq,i)) == gen_0) continue;
    2342         106 :     gel(Lq,j++) = diviiexact(N, gel(Lq,i));
    2343             :   }
    2344          23 :   setlg(Lq, j);
    2345          23 :   g = gener_FpXQ_i(get_FpX_mod(T), p, p_1, Lp, Lq);
    2346          23 :   if (!po) g = gerepilecopy(av, g);
    2347             :   else {
    2348           7 :     *po = mkvec2(q_1, o);
    2349           7 :     gerepileall(av, 2, &g, po);
    2350             :   }
    2351          23 :   return g;
    2352             : }
    2353             : 
    2354             : GEN
    2355        1442 : gener_FpXQ_local(GEN T, GEN p, GEN L)
    2356             : {
    2357        1442 :   GEN Lp, Lq, p_1 = subiu(p,1), q_1, N, Q;
    2358        1442 :   long f, i, ip, iq, l = lg(L);
    2359        1442 :   T = get_FpX_mod(T);
    2360        1442 :   f = degpol(T);
    2361        1442 :   q_1 = subiu(powiu(p,f), 1);
    2362        1442 :   N = diviiexact(q_1, p_1);
    2363             : 
    2364        1442 :   Q = is_pm1(p_1)? gen_1: shifti(p_1,-1);
    2365        1442 :   Lp = cgetg(l, t_VEC); ip = 1;
    2366        1442 :   Lq = cgetg(l, t_VEC); iq = 1;
    2367        2114 :   for (i=1; i < l; i++)
    2368             :   {
    2369         672 :     GEN a, b, ell = gel(L,i);
    2370         672 :     if (absequaliu(ell,2)) continue;
    2371         392 :     a = dvmdii(Q, ell, &b);
    2372         392 :     if (b == gen_0)
    2373          21 :       gel(Lp,ip++) = a;
    2374             :     else
    2375         371 :       gel(Lq,iq++) = diviiexact(N,ell);
    2376             :   }
    2377        1442 :   setlg(Lp, ip);
    2378        1442 :   setlg(Lq, iq);
    2379        1442 :   return gener_FpXQ_i(T, p, p_1, Lp, Lq);
    2380             : }

Generated by: LCOV version 1.11