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

Generated by: LCOV version 1.11