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 - base2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20783-cec4728) Lines: 2011 2335 86.1 %
Date: 2017-06-28 05:59:20 Functions: 161 172 93.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  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             : Check the License for details. You should have received a copy of it, along
      10             : with the package; see the file 'COPYING'. If not, write to the Free Software
      11             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      12             : 
      13             : /*******************************************************************/
      14             : /*                                                                 */
      15             : /*                       MAXIMAL ORDERS                            */
      16             : /*                                                                 */
      17             : /*******************************************************************/
      18             : #include "pari.h"
      19             : #include "paripriv.h"
      20             : 
      21             : /* allow p = -1 from factorizations, avoid oo loop on p = 1 */
      22             : static long
      23         203 : safe_Z_pvalrem(GEN x, GEN p, GEN *z)
      24             : {
      25         203 :   if (is_pm1(p))
      26             :   {
      27           7 :     if (signe(p) > 0) return gvaluation(x,p); /*error*/
      28           0 :     *z = absi(x); return 1;
      29             :   }
      30         196 :   return Z_pvalrem(x, p, z);
      31             : }
      32             : /* D an integer, P a ZV, return a factorization matrix for D over P, removing
      33             :  * entries with 0 exponent. */
      34             : static GEN
      35          77 : fact_from_factors(GEN D, GEN P, long flag)
      36             : {
      37          77 :   long i, l = lg(P), iq = 1;
      38          77 :   GEN Q = cgetg(l+1,t_COL);
      39          77 :   GEN E = cgetg(l+1,t_COL);
      40         273 :   for (i=1; i<l; i++)
      41             :   {
      42         203 :     GEN p = gel(P,i);
      43             :     long k;
      44         203 :     if (flag && !equalim1(p))
      45             :     {
      46          14 :       p = gcdii(p, D);
      47          14 :       if (is_pm1(p)) continue;
      48             :     }
      49         203 :     k = safe_Z_pvalrem(D, p, &D);
      50         196 :     if (k) { gel(Q,iq) = p; gel(E,iq) = utoipos(k); iq++; }
      51             :   }
      52          70 :   if (signe(D) < 0) D = absi(D);
      53          70 :   if (!is_pm1(D))
      54             :   {
      55          49 :     long k = Z_isanypower(D, &D);
      56          49 :     if (!k) k = 1;
      57          49 :     gel(Q,iq) = D; gel(E,iq) = utoipos(k); iq++;
      58             :   }
      59          70 :   setlg(Q,iq);
      60          70 :   setlg(E,iq); return mkmat2(Q,E);
      61             : }
      62             : 
      63             : /* d a t_INT; f a t_MAT factorisation of some t_INT sharing some divisors
      64             :  * with d, or a prime (t_INT). Return a factorization F of d: "primes"
      65             :  * entries in f _may_ be composite, and are included as is in d. */
      66             : static GEN
      67         462 : update_fact(GEN d, GEN f)
      68             : {
      69             :   GEN P;
      70         462 :   switch (typ(f))
      71             :   {
      72         455 :     case t_INT: case t_VEC: case t_COL: return f;
      73             :     case t_MAT:
      74           7 :       if (lg(f) == 3) { P = gel(f,1); break; }
      75             :     /*fall through*/
      76             :     default:
      77           0 :       pari_err_TYPE("nfbasis [factorization expected]",f);
      78           0 :       return NULL;
      79             :   }
      80           7 :   return fact_from_factors(d, P, 1);
      81             : }
      82             : 
      83             : /* T = C T0(X/L); C = L^d / lt(T0), d = deg(T)
      84             :  * disc T = C^2(d - 1) L^-(d(d-1)) disc T0 = (L^d / lt(T0)^2)^(d-1) disc T0 */
      85             : static GEN
      86       16100 : set_disc(nfmaxord_t *S)
      87             : {
      88             :   GEN l0, L, dT;
      89             :   long d;
      90       16100 :   if (S->T0 == S->T) return ZX_disc(S->T);
      91        4865 :   d = degpol(S->T0);
      92        4865 :   l0 = leading_coeff(S->T0);
      93        4865 :   L = S->unscale;
      94        4865 :   if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
      95         553 :     dT = ZX_disc(S->T); /* more efficient */
      96             :   else
      97             :   {
      98        4312 :     GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
      99        4312 :     dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
     100             :   }
     101        4865 :   return S->dT = dT;
     102             : }
     103             : 
     104             : static GEN
     105       11858 : poldiscfactors_i(GEN T, GEN dT, long flag)
     106             : {
     107       11858 :   GEN fa = absZ_factor_limit(dT, 0);
     108       11858 :   GEN Tp, E, P = gel(fa,1);
     109       11858 :   long i, l = lg(P);
     110       11858 :   GEN p = gel(P,l-1);
     111       11858 :   if (l == 1 || ((flag || lgefint(p)==3) && BPSW_psp(p))) return fa;
     112          77 :   settyp(P, t_VEC);
     113          77 :   Tp = ZX_deriv(T);
     114         301 :   for (i = l-1; i < lg(P); i++)
     115             :   {
     116         224 :     GEN p = gel(P,i), r, L;
     117         224 :     if (Z_isanypower(p, &p)) gel(P,i) = p;
     118         378 :     if ((flag || lgefint(p)==3) && BPSW_psp(p)) continue;
     119         106 :     r = FpX_gcd_check(T, Tp, p);
     120         106 :     if (r) L = Z_cba(r, diviiexact(p,r));
     121             :     else
     122             :     {
     123          57 :       if (!flag) continue;
     124          21 :       L = gel(Z_factor(p),1); settyp(L, t_VEC);
     125             :     }
     126          70 :     P = shallowconcat(vecsplice(P,i), L);
     127          70 :     i--;
     128             :   }
     129          77 :   settyp(P, t_COL);
     130          77 :   P = ZV_sort(P); l = lg(P);
     131          77 :   E = cgetg(l, t_COL);
     132         798 :   for (i = 1; i < l; i++)
     133         721 :     gel(E,i) = utoi(Z_pvalrem(dT, gel(P,i), &dT));
     134          77 :   return mkmat2(P,E);
     135             : }
     136             : GEN
     137          28 : poldiscfactors(GEN T, long flag)
     138             : {
     139          28 :   pari_sp av = avma;
     140             :   GEN dT;
     141          28 :   if (typ(T) != t_POL || !RgX_is_ZX(T)) pari_err_TYPE("poldiscfactors",T);
     142          28 :   if (flag < 0 || flag > 1) pari_err_FLAG("poldiscfactors");
     143          28 :   dT = ZX_disc(T);
     144          28 :   return gerepilecopy(av, mkvec2(dT, poldiscfactors_i(T, dT, flag)));
     145             : }
     146             : 
     147             : static void
     148       16100 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
     149             : {
     150       16100 :   GEN dT, L, E, P, fa = NULL;
     151             :   pari_timer t;
     152       16100 :   long l, ty = typ(T);
     153             : 
     154       16100 :   if (DEBUGLEVEL) timer_start(&t);
     155       16100 :   if (ty == t_VEC) {
     156        4270 :     if (lg(T) != 3) pari_err_TYPE("nfmaxord",T);
     157        4270 :     fa = gel(T,2); T = gel(T,1); ty = typ(T);
     158             :   }
     159       16100 :   if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
     160       16100 :   T = Q_primpart(T);
     161       16100 :   if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
     162       16100 :   RgX_check_ZX(T, "nfmaxord");
     163       16100 :   S->T0 = T;
     164       16100 :   T = ZX_Q_normalize(T, &L);
     165       16100 :   S->unscale = L;
     166       16100 :   S->T = T;
     167       16100 :   S->dT = dT = set_disc(S);
     168       16100 :   if (fa)
     169             :   {
     170        4270 :     if (!isint1(L)) fa = update_fact(dT, fa);
     171        4270 :     switch(typ(fa))
     172             :     {
     173             :       case t_VEC: case t_COL:
     174          70 :         fa = fact_from_factors(dT, fa, 0);
     175          63 :         break;
     176             :       case t_INT:
     177        4151 :         fa = absZ_factor_limit(dT, (signe(fa) <= 0)? 1: itou(fa));
     178        4151 :         break;
     179             :       case t_MAT:
     180          49 :         if (is_Z_factornon0(fa)) break;
     181             :         /*fall through*/
     182             :       default:
     183           0 :         pari_err_TYPE("nfmaxord",fa);
     184             :     }
     185        4263 :     if (!signe(dT)) pari_err_IRREDPOL("nfmaxord",mkvec2(T,fa));
     186             :   }
     187             :   else
     188       11830 :     fa = poldiscfactors_i(T, dT, !(flag & nf_PARTIALFACT));
     189       16093 :   P = gel(fa,1); l = lg(P);
     190       16093 :   E = gel(fa,2);
     191       16093 :   if (l > 1 && is_pm1(gel(P,1)))
     192             :   {
     193          21 :     l--;
     194          21 :     P = vecslice(P, 2, l);
     195          21 :     E = vecslice(E, 2, l);
     196             :   }
     197       16093 :   S->dTP = P;
     198       16093 :   S->dTE = vec_to_vecsmall(E);
     199       16093 :   if (DEBUGLEVEL) timer_printf(&t, "disc. factorisation");
     200       16093 : }
     201             : 
     202             : static int
     203       38297 : fnz(GEN x,long j)
     204             : {
     205             :   long i;
     206      192731 :   for (i=1; i<j; i++)
     207      158746 :     if (signe(gel(x,i))) return 0;
     208       33985 :   return 1;
     209             : }
     210             : /* return list u[i], 2 by 2 coprime with the same prime divisors as ab */
     211             : static GEN
     212          70 : get_coprimes(GEN a, GEN b)
     213             : {
     214          70 :   long i, k = 1;
     215          70 :   GEN u = cgetg(3, t_COL);
     216          70 :   gel(u,1) = a;
     217          70 :   gel(u,2) = b;
     218             :   /* u1,..., uk 2 by 2 coprime */
     219         378 :   while (k+1 < lg(u))
     220             :   {
     221         238 :     GEN d, c = gel(u,k+1);
     222         238 :     if (is_pm1(c)) { k++; continue; }
     223         735 :     for (i=1; i<=k; i++)
     224             :     {
     225         567 :       GEN ui = gel(u,i);
     226         567 :       if (is_pm1(ui)) continue;
     227         168 :       d = gcdii(c, ui);
     228         168 :       if (d == gen_1) continue;
     229         168 :       c = diviiexact(c, d);
     230         168 :       gel(u,i) = diviiexact(ui, d);
     231         168 :       u = shallowconcat(u, d);
     232             :     }
     233         168 :     gel(u,++k) = c;
     234             :   }
     235         378 :   for (i = k = 1; i < lg(u); i++)
     236         308 :     if (!is_pm1(gel(u,i))) gel(u,k++) = gel(u,i);
     237          70 :   setlg(u, k); return u;
     238             : }
     239             : 
     240             : /*******************************************************************/
     241             : /*                                                                 */
     242             : /*                            ROUND 2                              */
     243             : /*                                                                 */
     244             : /*******************************************************************/
     245             : /* transpose of companion matrix of unitary polynomial x, cf matcompanion */
     246             : static GEN
     247           0 : companion(GEN x)
     248             : {
     249           0 :   long j, l = degpol(x);
     250           0 :   GEN c, y = cgetg(l+1,t_MAT);
     251             : 
     252           0 :   c = zerocol(l); gel(c,l) = gneg(gel(x,2));
     253           0 :   gel(y,1) = c;
     254           0 :   for (j=2; j<=l; j++)
     255             :   {
     256           0 :     c = col_ei(l, j-1); gel(c,l) = gneg(gel(x,j+1));
     257           0 :     gel(y,j) = c;
     258             :   }
     259           0 :   return y;
     260             : }
     261             : 
     262             : /* return (v - qw) mod m (only compute entries k0,..,n)
     263             :  * v and w are expected to have entries smaller than m */
     264             : static GEN
     265           0 : mtran(GEN v, GEN w, GEN q, GEN m, GEN mo2, long k0)
     266             : {
     267             :   long k;
     268             :   GEN p1;
     269             : 
     270           0 :   if (signe(q))
     271           0 :     for (k=lg(v)-1; k >= k0; k--)
     272             :     {
     273           0 :       pari_sp av = avma;
     274           0 :       p1 = subii(gel(v,k), mulii(q,gel(w,k)));
     275           0 :       p1 = centermodii(p1, m, mo2);
     276           0 :       gel(v,k) = gerepileuptoint(av, p1);
     277             :     }
     278           0 :   return v;
     279             : }
     280             : 
     281             : /* entries of v and w are C small integers */
     282             : static GEN
     283           0 : mtran_long(GEN v, GEN w, long q, long m, long k0)
     284             : {
     285             :   long k, p1;
     286             : 
     287           0 :   if (q)
     288             :   {
     289           0 :     for (k=lg(v)-1; k>= k0; k--)
     290             :     {
     291           0 :       p1 = v[k] - q * w[k];
     292           0 :       v[k] = p1 % m;
     293             :     }
     294             :   }
     295           0 :   return v;
     296             : }
     297             : 
     298             : /* coeffs of a are C-long integers */
     299             : static void
     300           0 : rowred_long(GEN a, long rmod)
     301             : {
     302           0 :   long j,k, c = lg(a), r = lgcols(a);
     303             : 
     304           0 :   for (j=1; j<r; j++)
     305             :   {
     306           0 :     for (k=j+1; k<c; k++)
     307           0 :       while (coeff(a,j,k))
     308             :       {
     309           0 :         long q = coeff(a,j,j) / coeff(a,j,k);
     310           0 :         GEN pro = mtran_long(gel(a,j),gel(a,k),q,rmod, j);
     311           0 :         gel(a, j) = gel(a, k); gel(a, k)=pro;
     312             :       }
     313           0 :     if (coeff(a,j,j) < 0)
     314           0 :       for (k=j; k<r; k++) coeff(a,k,j)=-coeff(a,k,j);
     315           0 :     for (k=1; k<j; k++)
     316             :     {
     317           0 :       long q = coeff(a,j,k) / coeff(a,j,j);
     318           0 :       gel(a,k) = mtran_long(gel(a,k),gel(a,j),q,rmod, k);
     319             :     }
     320             :   }
     321             :   /* don't update the 0s in the last columns */
     322           0 :   for (j=1; j<r; j++)
     323           0 :     for (k=1; k<r; k++) gcoeff(a,j,k) = stoi(coeff(a,j,k));
     324           0 : }
     325             : 
     326             : static void
     327           0 : rowred(GEN a, GEN rmod, GEN rmodo2)
     328             : {
     329           0 :   long j,k, c = lg(a), r = lgcols(a);
     330           0 :   pari_sp av=avma;
     331             : 
     332           0 :   for (j=1; j<r; j++)
     333             :   {
     334           0 :     for (k=j+1; k<c; k++)
     335           0 :       while (signe(gcoeff(a,j,k)))
     336             :       {
     337           0 :         GEN q=diviiround(gcoeff(a,j,j),gcoeff(a,j,k));
     338           0 :         GEN pro=mtran(gel(a,j),gel(a,k),q,rmod,rmodo2, j);
     339           0 :         gel(a, j) = gel(a, k); gel(a, k)=pro;
     340             :       }
     341           0 :     if (signe(gcoeff(a,j,j)) < 0)
     342           0 :       for (k=j; k<r; k++) gcoeff(a,k,j) = negi(gcoeff(a,k,j));
     343           0 :     for (k=1; k<j; k++)
     344             :     {
     345           0 :       GEN q=diviiround(gcoeff(a,j,k),gcoeff(a,j,j));
     346           0 :       gel(a,k) = mtran(gel(a,k),gel(a,j),q,rmod,rmodo2, k);
     347             :     }
     348           0 :     if (gc_needed(av,1))
     349             :     {
     350             :       long j1,k1;
     351             :       GEN p1;
     352           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rowred j=%ld", j);
     353           0 :       p1 = gerepilecopy(av,a);
     354           0 :       for (j1=1; j1<r; j1++)
     355           0 :         for (k1=1; k1<c; k1++) gcoeff(a,j1,k1) = gcoeff(p1,j1,k1);
     356             :     }
     357             :   }
     358           0 : }
     359             : 
     360             : /* Compute d/x where d is t_INT, x lower triangular t_MAT with t_INT coeffs
     361             :  * whose diagonal coeffs divide d (lower triangular ZM result). */
     362             : static GEN
     363           0 : matinv(GEN x, GEN d)
     364             : {
     365             :   pari_sp av,av1;
     366           0 :   long i,j,k, n = lg(x);
     367             :   GEN y,h;
     368             : 
     369           0 :   y = matid(n-1);
     370           0 :   for (i=1; i<n; i++)
     371           0 :     gcoeff(y,i,i) = diviiexact(d,gcoeff(x,i,i));
     372           0 :   av=avma;
     373           0 :   for (i=2; i<n; i++)
     374           0 :     for (j=i-1; j; j--)
     375             :     {
     376           0 :       for (h=gen_0,k=j+1; k<=i; k++)
     377             :       {
     378           0 :         GEN p1 = mulii(gcoeff(y,i,k),gcoeff(x,k,j));
     379           0 :         if (p1 != gen_0) h=addii(h,p1);
     380             :       }
     381           0 :       togglesign(h); av1=avma;
     382           0 :       gcoeff(y,i,j) = gerepile(av,av1,diviiexact(h,gcoeff(x,j,j)));
     383           0 :       av = avma;
     384             :     }
     385           0 :   return y;
     386             : }
     387             : 
     388             : /* epsilon > 1 */
     389             : static GEN
     390           0 : maxord2(GEN cf, GEN p, long epsilon)
     391             : {
     392           0 :   long sp,i,n=lg(cf)-1;
     393           0 :   pari_sp av=avma, av2;
     394             :   GEN T,T2,Tn,m,v,delta,hard_case_exponent, *w;
     395           0 :   const GEN pp = sqri(p);
     396           0 :   const GEN ppo2 = shifti(pp,-1);
     397           0 :   const long pps = (2*expi(pp)+2 < (long)BITS_IN_LONG)? pp[2]: 0;
     398             : 
     399           0 :   if (abscmpiu(p,n) > 0)
     400             :   {
     401           0 :     hard_case_exponent = NULL;
     402           0 :     sp = 0; /* gcc -Wall */
     403             :   }
     404             :   else
     405             :   {
     406             :     long k;
     407           0 :     k = sp = itos(p);
     408           0 :     i=1; while (k < n) { k *= sp; i++; }
     409           0 :     hard_case_exponent = utoipos(i);
     410             :   }
     411           0 :   T=cgetg(n+1,t_MAT); for (i=1; i<=n; i++) gel(T,i) = cgetg(n+1,t_COL);
     412           0 :   T2=cgetg(2*n+1,t_MAT); for (i=1; i<=2*n; i++) gel(T2,i) = cgetg(n+1,t_COL);
     413           0 :   Tn=cgetg(n*n+1,t_MAT); for (i=1; i<=n*n; i++) gel(Tn,i) = cgetg(n+1,t_COL);
     414           0 :   v = new_chunk(n+1);
     415           0 :   w = (GEN*)new_chunk(n+1);
     416             : 
     417           0 :   av2 = avma;
     418           0 :   delta=gen_1; m=matid(n);
     419             : 
     420             :   for(;;)
     421             :   {
     422             :     long j, k, h;
     423           0 :     pari_sp av0 = avma;
     424           0 :     GEN t,b,jp,hh,index,p1, dd = sqri(delta), ppdd = mulii(dd,pp);
     425           0 :     GEN ppddo2 = shifti(ppdd,-1);
     426             : 
     427           0 :     if (DEBUGLEVEL > 3)
     428           0 :       err_printf("ROUND2: epsilon = %ld\tavma = %ld\n",epsilon,avma);
     429             : 
     430           0 :     b=matinv(m,delta);
     431           0 :     for (i=1; i<=n; i++)
     432             :     {
     433           0 :       for (j=1; j<=n; j++)
     434           0 :         for (k=1; k<=n; k++)
     435             :         {
     436           0 :           p1 = j==k? gcoeff(m,i,1): gen_0;
     437           0 :           for (h=2; h<=n; h++)
     438             :           {
     439           0 :             GEN p2 = mulii(gcoeff(m,i,h),gcoeff(gel(cf,h),j,k));
     440           0 :             if (p2!=gen_0) p1 = addii(p1,p2);
     441             :           }
     442           0 :           gcoeff(T,j,k) = centermodii(p1, ppdd, ppddo2);
     443             :         }
     444           0 :       p1 = ZM_mul(m, ZM_mul(T,b));
     445           0 :       for (j=1; j<=n; j++)
     446           0 :         for (k=1; k<=n; k++)
     447           0 :           gcoeff(p1,j,k) = centermodii(diviiexact(gcoeff(p1,j,k),dd),pp,ppo2);
     448           0 :       w[i] = p1;
     449             :     }
     450             : 
     451           0 :     if (hard_case_exponent)
     452             :     {
     453           0 :       for (j=1; j<=n; j++)
     454             :       {
     455           0 :         for (i=1; i<=n; i++) gcoeff(T,i,j) = gcoeff(w[j],1,i);
     456             :         /* ici la boucle en k calcule la puissance p mod p de w[j] */
     457           0 :         for (k=1; k<sp; k++)
     458             :         {
     459           0 :           for (i=1; i<=n; i++)
     460             :           {
     461           0 :             p1 = gen_0;
     462           0 :             for (h=1; h<=n; h++)
     463             :             {
     464           0 :               GEN p2=mulii(gcoeff(T,h,j),gcoeff(w[j],h,i));
     465           0 :               if (p2!=gen_0) p1 = addii(p1,p2);
     466             :             }
     467           0 :             gel(v,i) = modii(p1, p);
     468             :           }
     469           0 :           for (i=1; i<=n; i++) gcoeff(T,i,j) = gel(v,i);
     470             :         }
     471             :       }
     472           0 :       t = ZM_pow(T, hard_case_exponent);
     473             :     }
     474             :     else
     475             :     {
     476           0 :       for (i=1; i<=n; i++)
     477           0 :         for (j=1; j<=n; j++)
     478             :         {
     479           0 :           pari_sp av1 = avma;
     480           0 :           p1 = gen_0;
     481           0 :           for (k=1; k<=n; k++)
     482           0 :             for (h=1; h<=n; h++)
     483             :             {
     484           0 :               const GEN r=modii(gcoeff(w[i],k,h),p);
     485           0 :               const GEN s=modii(gcoeff(w[j],h,k),p);
     486           0 :               const GEN p2 = mulii(r,s);
     487           0 :               if (p2!=gen_0) p1 = addii(p1,p2);
     488             :             }
     489           0 :           gcoeff(T,i,j) = gerepileupto(av1,p1);
     490             :         }
     491           0 :       t = T;
     492             :     }
     493             : 
     494           0 :     setlg(T2, 2*n+1);
     495           0 :     if (pps)
     496             :     {
     497           0 :       long ps = p[2];
     498           0 :       for (i=1; i<=n; i++)
     499           0 :         for (j=1; j<=n; j++)
     500             :         {
     501           0 :           coeff(T2,j,i)=(i==j)? ps: 0;
     502           0 :           coeff(T2,j,n+i)=smodis(gcoeff(t,i,j),ps);
     503             :         }
     504           0 :       rowred_long(T2,pps);
     505             :     }
     506             :     else
     507             :     {
     508           0 :       for (i=1; i<=n; i++)
     509           0 :         for (j=1; j<=n; j++)
     510             :         {
     511           0 :           gcoeff(T2,j,i)=(i==j)? p: gen_0;
     512           0 :           gcoeff(T2,j,n+i) = modii(gcoeff(t,i,j),p);
     513             :         }
     514           0 :       rowred(T2,pp,ppo2);
     515             :     }
     516           0 :     setlg(T2, n+1);
     517           0 :     jp=matinv(T2,p);
     518           0 :     setlg(Tn, n*n+1);
     519           0 :     if (pps)
     520             :     {
     521           0 :       for (k=1; k<=n; k++)
     522             :       {
     523           0 :         pari_sp av1=avma;
     524           0 :         t = ZM_mul(ZM_mul(jp,w[k]), T2);
     525           0 :         for (h=i=1; i<=n; i++)
     526           0 :           for (j=1; j<=n; j++,h++)
     527           0 :             coeff(Tn,k,h) = itos(diviiexact(gcoeff(t,i,j), p)) % pps;
     528           0 :         avma=av1;
     529             :       }
     530           0 :       avma = av0;
     531           0 :       rowred_long(Tn,pps);
     532             :     }
     533             :     else
     534             :     {
     535           0 :       for (k=1; k<=n; k++)
     536             :       {
     537           0 :         t = ZM_mul(ZM_mul(jp,w[k]), T2);
     538           0 :         for (h=i=1; i<=n; i++)
     539           0 :           for (j=1; j<=n; j++,h++)
     540           0 :             gcoeff(Tn,k,h) = diviiexact(gcoeff(t,i,j), p);
     541             :       }
     542           0 :       rowred(Tn,pp,ppo2);
     543             :     }
     544           0 :     setlg(Tn, n+1);
     545           0 :     index = ZM_det_triangular(Tn);
     546           0 :     if (is_pm1(index)) break;
     547             : 
     548           0 :     m = ZM_mul(matinv(Tn,index), m);
     549           0 :     m = Q_primitive_part(m, &hh);
     550           0 :     delta = mulii(index,delta);
     551           0 :     if (hh) delta = diviiexact(delta,hh);
     552           0 :     epsilon -= 2 * Z_pval(index,p);
     553           0 :     if (epsilon < 2) break;
     554           0 :     if (gc_needed(av2,1))
     555             :     {
     556           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"maxord2");
     557           0 :       gerepileall(av2, 2, &m, &delta);
     558             :     }
     559           0 :   }
     560           0 :   m = shallowtrans(m);
     561           0 :   return gerepileupto(av, RgM_Rg_div(ZM_hnfmodid(m, delta), delta));
     562             : }
     563             : 
     564             : static GEN
     565           0 : allbase2(nfmaxord_t *S)
     566             : {
     567           0 :   GEN cf, O, P = S->dTP, E = S->dTE, f = S->T;
     568           0 :   long i, lP = lg(P), n = degpol(f);
     569             : 
     570           0 :   cf = cgetg(n+1,t_VEC); gel(cf,2) = companion(f);
     571           0 :   for (i=3; i<=n; i++) gel(cf,i) = ZM_mul(gel(cf,2), gel(cf,i-1));
     572           0 :   O = cgetg(lP, t_VEC);
     573           0 :   for (i=1; i<lP; i++)
     574             :   {
     575           0 :     GEN p = gel(P, i);
     576           0 :     long e = E[i];
     577           0 :     if (DEBUGLEVEL) err_printf("Treating p^k = %Ps^%ld\n", p, e);
     578           0 :     gel(O,i) = e == 1? gen_1: maxord2(cf, p, e);
     579             :   }
     580           0 :   return O;
     581             : }
     582             : 
     583             : /*******************************************************************/
     584             : /*                                                                 */
     585             : /*                            ROUND 4                              */
     586             : /*                                                                 */
     587             : /*******************************************************************/
     588             : static GEN maxord_i(GEN p, GEN f, long mf, GEN w, long flag);
     589             : static GEN dbasis(GEN p, GEN f, long mf, GEN alpha, GEN U);
     590             : static GEN maxord(GEN p,GEN f,long mf);
     591             : static GEN ZX_Dedekind(GEN F, GEN *pg, GEN p);
     592             : 
     593             : /* Warning: data computed for T = ZX_Q_normalize(T0). If S.unscale !=
     594             :  * gen_1, caller must take steps to correct the components if it wishes
     595             :  * to stick to the original T0. Return a vector of p-maximal orders, for
     596             :  * those p s.t p^2 | disc(T) [ = S->dTP ]*/
     597             : static GEN
     598       16100 : get_maxord(nfmaxord_t *S, GEN T0, long flag)
     599             : {
     600             :   VOLATILE GEN P, E, O;
     601             :   VOLATILE long lP, i, k;
     602             : 
     603       16100 :   nfmaxord_check_args(S, T0, flag);
     604       16093 :   if (flag & nf_ROUND2) return allbase2(S);
     605       16093 :   P = S->dTP; lP = lg(P);
     606       16093 :   E = S->dTE;
     607       16093 :   O = cgetg(1, t_VEC);
     608       80017 :   for (i=1; i<lP; i++)
     609             :   {
     610             :     VOLATILE pari_sp av;
     611             :     /* includes the silly case where P[i] = -1 */
     612       63924 :     if (E[i] <= 1) { O = shallowconcat(O, gen_1); continue; }
     613       52941 :     av = avma;
     614       52941 :     pari_CATCH(CATCH_ALL) {
     615          70 :       GEN N, u, err = pari_err_last();
     616             :       long l;
     617          70 :       switch(err_get_num(err))
     618             :       {
     619             :         case e_INV:
     620             :         {
     621          70 :           GEN p, x = err_get_compo(err, 2);
     622          70 :           if (typ(x) == t_INTMOD)
     623             :           { /* caught false prime, update factorization */
     624          70 :             p = gcdii(gel(x,1), gel(x,2));
     625          70 :             u = diviiexact(gel(x,1),p);
     626          70 :             if (DEBUGLEVEL) pari_warn(warner,"impossible inverse: %Ps", x);
     627          70 :             gerepileall(av, 2, &p, &u);
     628             : 
     629          70 :             u = get_coprimes(p, u); l = lg(u);
     630             :             /* no small factors, but often a prime power */
     631          70 :             for (k = 1; k < l; k++) (void)Z_isanypower(gel(u,k), &gel(u,k));
     632          70 :             break;
     633             :           }
     634             :           /* fall through */
     635             :         }
     636             :         case e_PRIME: case e_IRREDPOL:
     637             :         { /* we're here because we failed BPSW_isprime(), no point in
     638             :            * reporting a possible counter-example to the BPSW test */
     639           0 :           GEN p = gel(P,i);
     640           0 :           avma = av;
     641           0 :           if (DEBUGLEVEL)
     642           0 :             pari_warn(warner,"large composite in nfmaxord:loop(), %Ps", p);
     643           0 :           if (expi(p) < 100) /* factor should require ~20ms for this */
     644           0 :             u = gel(Z_factor(p), 1);
     645             :           else
     646             :           { /* give up, probably not maximal */
     647           0 :             GEN B, g, k = ZX_Dedekind(S->T, &g, p);
     648           0 :             k = FpX_normalize(k, p);
     649           0 :             B = dbasis(p, S->T, E[i], NULL, FpX_div(S->T,k,p));
     650           0 :             O = shallowconcat(O, mkvec(B));
     651           0 :             pari_CATCH_reset(); continue;
     652             :           }
     653           0 :           break;
     654             :         }
     655           0 :         default: pari_err(0, err);
     656           0 :           return NULL;
     657             :       }
     658          70 :       l = lg(u);
     659          70 :       gel(P,i) = gel(u,1);
     660          70 :       P = shallowconcat(P, vecslice(u, 2, l-1));
     661          70 :       av = avma;
     662          70 :       N = S->dT; E[i] = Z_pvalrem(N, gel(P,i), &N);
     663          70 :       for (k=lP, lP=lg(P); k < lP; k++) E[k] = Z_pvalrem(N, gel(P,k), &N);
     664       53011 :     } pari_RETRY {
     665       53011 :       if (DEBUGLEVEL) err_printf("Treating p^k = %Ps^%ld\n",P[i],E[i]);
     666       53011 :       O = shallowconcat(O, mkvec( maxord(gel(P,i),S->T,E[i]) ));
     667       52941 :     } pari_ENDCATCH;
     668             :   }
     669       16093 :   S->dTP = P; return O;
     670             : }
     671             : 
     672             : /* M a QM, return denominator of diagonal. All denominators are powers of
     673             :  * a given integer */
     674             : static GEN
     675        6818 : diag_denom(GEN M)
     676             : {
     677        6818 :   GEN d = gen_1;
     678        6818 :   long j, l = lg(M);
     679       77028 :   for (j=1; j<l; j++)
     680             :   {
     681       70210 :     GEN t = gcoeff(M,j,j);
     682       70210 :     if (typ(t) == t_INT) continue;
     683       17745 :     t = gel(t,2);
     684       17745 :     if (abscmpii(t,d) > 0) d = t;
     685             :   }
     686        6818 :   return d;
     687             : }
     688             : void
     689       12971 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
     690             : {
     691       12971 :   GEN O = get_maxord(S, T0, flag);
     692       12964 :   GEN f = S->T, P = S->dTP, a = NULL, da = NULL, P2, E2, D;
     693       12964 :   long n = degpol(f), lP = lg(P), i, j, k;
     694       12964 :   int centered = 0;
     695       12964 :   pari_sp av = avma;
     696             :   /* r1 & basden not initialized here */
     697       12964 :   S->r1 = -1;
     698       12964 :   S->basden = NULL;
     699       38017 :   for (i=1; i<lP; i++)
     700             :   {
     701       25053 :     GEN M, db, b = gel(O,i);
     702       25053 :     if (b == gen_1) continue;
     703        6818 :     db = diag_denom(b);
     704        6818 :     if (db == gen_1) continue;
     705             : 
     706             :     /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
     707        6818 :     b = Q_muli_to_int(b,db);
     708        6818 :     if (!da) { da = db; a = b; }
     709             :     else
     710             :     { /* optimization: easy as long as both matrix are diagonal */
     711        4312 :       j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
     712        4312 :       k = j-1; M = cgetg(2*n-k+1,t_MAT);
     713       25522 :       for (j=1; j<=k; j++)
     714             :       {
     715       21210 :         gel(M,j) = gel(a,j);
     716       21210 :         gcoeff(M,j,j) = mulii(gcoeff(a,j,j),gcoeff(b,j,j));
     717             :       }
     718             :       /* could reduce mod M(j,j) but not worth it: usually close to da*db */
     719        4312 :       for (  ; j<=n;     j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
     720        4312 :       for (  ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
     721        4312 :       da = mulii(da,db);
     722        4312 :       a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
     723        4312 :       gerepileall(av, 2, &a, &da);
     724        4312 :       centered = 1;
     725             :     }
     726             :   }
     727       12964 :   if (da)
     728             :   {
     729        2506 :     GEN index = diviiexact(da, gcoeff(a,1,1));
     730        2506 :     for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
     731        2506 :     if (!centered) a = ZM_hnfcenter(a);
     732        2506 :     a = RgM_Rg_div(a, da);
     733        2506 :     S->index = index;
     734        2506 :     S->dK = diviiexact(S->dT, sqri(index));
     735             :   }
     736             :   else
     737             :   {
     738       10458 :     S->index = gen_1;
     739       10458 :     S->dK = S->dT;
     740       10458 :     a = matid(n);
     741             :   }
     742             : 
     743       12964 :   D = S->dK;
     744       12964 :   P2 = cgetg(lP, t_COL);
     745       12964 :   E2 = cgetg(lP, t_VECSMALL);
     746       38017 :   for (k = j = 1; j < lP; j++)
     747             :   {
     748       25053 :     long v = Z_pvalrem(D, gel(P,j), &D);
     749       25053 :     if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
     750             :   }
     751       12964 :   setlg(P2, k); S->dKP = P2;
     752       12964 :   setlg(E2, k); S->dKE = E2;
     753       12964 :   S->basis = RgM_to_RgXV(a, varn(f));
     754       12964 : }
     755             : GEN
     756          56 : nfbasis(GEN x, GEN *pdK, GEN fa)
     757             : {
     758          56 :   pari_sp av = avma;
     759             :   nfmaxord_t S;
     760             :   GEN B;
     761          56 :   nfmaxord(&S, fa? mkvec2(x,fa): x, 0);
     762          56 :   B = RgXV_unscale(S.basis, S.unscale);
     763          56 :   if (pdK)  *pdK = S.dK;
     764          56 :   gerepileall(av, pdK? 2: 1, &B, pdK); return B;
     765             : }
     766             : GEN
     767        3129 : nfdisc(GEN x)
     768             : {
     769        3129 :   pari_sp av = avma;
     770             :   nfmaxord_t S;
     771        3129 :   GEN O = get_maxord(&S, x, 0);
     772        3129 :   long n = degpol(S.T), lP = lg(O), i, j;
     773        3129 :   GEN index = gen_1;
     774       42000 :   for (i=1; i<lP; i++)
     775             :   {
     776       38871 :     GEN b = gel(O,i);
     777       38871 :     if (b == gen_1) continue;
     778      398482 :     for (j = 1; j <= n; j++)
     779             :     {
     780      364777 :       GEN c = gcoeff(b,j,j);
     781      364777 :       if (typ(c) == t_FRAC) index = mulii(index, gel(c,2)) ;
     782             :     }
     783             :   }
     784        3129 :   return gerepileuptoint(av, diviiexact(S.dT, sqri(index)));
     785             : }
     786             : 
     787             : GEN
     788          56 : nfbasis_gp(GEN x) { return nfbasis(x,NULL,NULL); }
     789             : 
     790             : static ulong
     791      104704 : Flx_checkdeflate(GEN x)
     792             : {
     793      104704 :   ulong d = 0, i, lx = (ulong)lg(x);
     794      228623 :   for (i=3; i<lx; i++)
     795      198821 :     if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
     796      104704 :   return d;
     797             : }
     798             : 
     799             : /* product of (monic) irreducible factors of f over Fp[X]
     800             :  * Assume f reduced mod p, otherwise valuation at x may be wrong */
     801             : static GEN
     802      104704 : Flx_radical(GEN f, ulong p)
     803             : {
     804      104704 :   long v0 = Flx_valrem(f, &f);
     805             :   ulong du, d, e;
     806             :   GEN u;
     807             : 
     808      104704 :   d = Flx_checkdeflate(f);
     809      104704 :   if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
     810       89697 :   if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
     811       89697 :   u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
     812       89690 :   du = degpol(u);
     813       89690 :   if (du)
     814             :   {
     815       62111 :     if (du == (ulong)degpol(f))
     816           0 :       f = Flx_radical(Flx_deflate(f,p), p);
     817             :     else
     818             :     {
     819       62111 :       u = Flx_normalize(u, p);
     820       62111 :       f = Flx_div(f, u, p);
     821       62111 :       if (p <= du)
     822             :       {
     823        7140 :         GEN w = Flxq_powu(f, du, u, p);
     824        7140 :         w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
     825        7140 :         f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
     826             :       }
     827             :     }
     828             :   }
     829       89690 :   if (v0) f = Flx_shift(f, 1);
     830       89690 :   return f;
     831             : }
     832             : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
     833             : static GEN
     834        3113 : FpX_radical(GEN f, GEN p)
     835             : {
     836             :   GEN u;
     837             :   long v0;
     838        3113 :   if (lgefint(p) == 3)
     839             :   {
     840         472 :     ulong q = p[2];
     841         472 :     return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
     842             :   }
     843        2641 :   v0 = ZX_valrem(f, &f);
     844        2641 :   u = FpX_gcd(f,FpX_deriv(f, p), p);
     845        2578 :   if (degpol(u)) f = FpX_div(f, u, p);
     846        2578 :   if (v0) f = RgX_shift(f, 1);
     847        2578 :   return f;
     848             : }
     849             : /* f / a */
     850             : static GEN
     851       97086 : zx_z_div(GEN f, ulong a)
     852             : {
     853       97086 :   long i, l = lg(f);
     854       97086 :   GEN g = cgetg(l, t_VECSMALL);
     855       97086 :   g[1] = f[1];
     856       97086 :   for (i = 2; i < l; i++) g[i] = f[i] / a;
     857       97086 :   return g;
     858             : }
     859             : /* Dedekind criterion; return k = gcd(g,h, (f-gh)/p), where
     860             :  *   f = \prod f_i^e_i, g = \prod f_i, h = \prod f_i^{e_i-1}
     861             :  * k = 1 iff Z[X]/(f) is p-maximal */
     862             : static GEN
     863      100205 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
     864             : {
     865             :   GEN k, h, g, f, f2;
     866      100205 :   ulong q = p[2];
     867      100205 :   if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
     868       97086 :   {
     869       97092 :     ulong q = p[2], q2 = q*q;
     870       97092 :     f2 = ZX_to_Flx(F, q2);
     871       97092 :     f = Flx_red(f2, q);
     872       97092 :     g = Flx_radical(f, q);
     873       97086 :     h = Flx_div(f, g, q);
     874       97086 :     k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
     875       97086 :     k = Flx_gcd(k, Flx_gcd(g,h,q), q);
     876       97086 :     k = Flx_to_ZX(k);
     877       97086 :     g = Flx_to_ZX(g);
     878             :   }
     879             :   else
     880             :   {
     881        3113 :     f2 = FpX_red(F, sqri(p));
     882        3113 :     f = FpX_red(f2, p);
     883        3113 :     g = FpX_radical(f, p);
     884        3049 :     h = FpX_div(f, g, p);
     885        3049 :     k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
     886        3049 :     k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
     887             :   }
     888      100135 :   *pg = g; return k;
     889             : }
     890             : 
     891             : /* p-maximal order of Z[x]/f; mf = v_p(Disc(f)) or < 0 [unknown].
     892             :  * Return gen_1 if p-maximal */
     893             : static GEN
     894      100205 : maxord(GEN p, GEN f, long mf)
     895             : {
     896      100205 :   const pari_sp av = avma;
     897      100205 :   GEN res, g, k = ZX_Dedekind(f, &g, p);
     898      100135 :   long dk = degpol(k);
     899      100135 :   if (DEBUGLEVEL>2) err_printf("  ZX_dedekind: gcd has degree %ld\n", dk);
     900      100135 :   if (!dk) { avma = av; return gen_1; }
     901       67949 :   if (mf < 0) mf = ZpX_disc_val(f, p);
     902       67949 :   if (2*dk >= mf-1)
     903             :   {
     904       34874 :     k = FpX_normalize(k, p);
     905       34874 :     res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
     906             :   }
     907             :   else
     908             :   {
     909             :     GEN w, F1, F2;
     910       33075 :     F1 = FpX_factor(k,p);
     911       33075 :     F2 = FpX_factor(FpX_div(g,k,p),p);
     912       33075 :     w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
     913       33075 :     res = maxord_i(p, f, mf, w, 0);
     914             :   }
     915       67949 :   return gerepilecopy(av,res);
     916             : }
     917             : 
     918             : static GEN
     919      775461 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
     920             : {
     921      775461 :   long j, n = degpol(f1);
     922      775461 :   GEN h, a = cgetg(n+1,t_MAT);
     923      775461 :   f1 = Flx_get_red(f1, pm);
     924      775461 :   h = Flx_rem(f2,f1,pm);
     925     3275170 :   for (j=1;; j++)
     926             :   {
     927     3275170 :     gel(a,j) = Flx_to_Flv(h, n);
     928     3275170 :     if (j == n) break;
     929     2499709 :     h = Flx_rem(Flx_shift(h, 1), f1, pm);
     930     2499709 :   }
     931      775461 :   return zlm_echelon(a, early_abort, p, pm);
     932             : }
     933             : /* Sylvester's matrix, mod p^m (assumes f1 monic). If early_abort
     934             :  * is set, return NULL if one pivot is 0 mod p^m */
     935             : static GEN
     936       13861 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
     937             : {
     938       13861 :   long j, n = degpol(f1);
     939       13861 :   GEN h, a = cgetg(n+1,t_MAT);
     940       13861 :   h = FpXQ_red(f2,f1,pm);
     941      141946 :   for (j=1;; j++)
     942             :   {
     943      141946 :     gel(a,j) = RgX_to_RgC(h, n);
     944      141946 :     if (j == n) break;
     945      128085 :     h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
     946      128085 :   }
     947       13861 :   return ZpM_echelon(a, early_abort, p, pm);
     948             : }
     949             : 
     950             : /* polynomial gcd mod p^m (assumes f1 monic). Return a QpX ! */
     951             : static GEN
     952       18271 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
     953             : {
     954       18271 :   pari_sp av = avma;
     955       18271 :   GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
     956       18271 :   long c, l = lg(a), sv = f1[1];
     957      107106 :   for (c = 1; c < l; c++)
     958             :   {
     959      107106 :     ulong t = ucoeff(a,c,c);
     960      107106 :     if (t)
     961             :     {
     962       18271 :       a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
     963       18271 :       if (t == 1) return gerepilecopy(av, a);
     964        2527 :       return gerepileupto(av, RgX_Rg_div(a, utoipos(t)));
     965             :     }
     966             :   }
     967           0 :   avma = av;
     968           0 :   a = cgetg(2,t_POL); a[1] = sv; return a;
     969             : }
     970             : GEN
     971       23996 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
     972             : {
     973       23996 :   pari_sp av = avma;
     974             :   GEN a;
     975             :   long c, l, v;
     976       23996 :   if (lgefint(pm) == 3)
     977             :   {
     978       18271 :     ulong q = pm[2];
     979       18271 :     return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
     980             :   }
     981        5725 :   a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
     982        5725 :   l = lg(a); v = varn(f1);
     983       38676 :   for (c = 1; c < l; c++)
     984             :   {
     985       38676 :     GEN t = gcoeff(a,c,c);
     986       38676 :     if (signe(t))
     987             :     {
     988        5725 :       a = RgV_to_RgX(gel(a,c), v);
     989        5725 :       if (equali1(t)) return gerepilecopy(av, a);
     990        1603 :       return gerepileupto(av, RgX_Rg_div(a, t));
     991             :     }
     992             :   }
     993           0 :   avma = av; return pol_0(v);
     994             : }
     995             : 
     996             : /* Return m > 0, such that p^m ~ 2^16 for initial value of m; p > 1 */
     997             : static long
     998      712641 : init_m(GEN p)
     999             : {
    1000      712641 :   if (lgefint(p) > 3) return 1;
    1001      712561 :   return (long)(16 / log2(p[2]));
    1002             : }
    1003             : 
    1004             : /* reduced resultant mod p^m (assumes x monic) */
    1005             : GEN
    1006       80624 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
    1007             : {
    1008       80624 :   pari_sp av = avma;
    1009             :   GEN z;
    1010       80624 :   if (lgefint(pm) == 3)
    1011             :   {
    1012       76019 :     ulong q = pm[2];
    1013       76019 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
    1014       76019 :     if (lg(z) > 1)
    1015             :     {
    1016       76019 :       ulong c = ucoeff(z,1,1);
    1017       76019 :       if (c) { avma = av; return utoipos(c); }
    1018             :     }
    1019             :   }
    1020             :   else
    1021             :   {
    1022        4605 :     z = ZpX_sylvester_echelon(x,y,0,p,pm);
    1023        4605 :     if (lg(z) > 1)
    1024             :     {
    1025        4605 :       GEN c = gcoeff(z,1,1);
    1026        4605 :       if (signe(c)) return gerepileuptoint(av, c);
    1027             :     }
    1028             :   }
    1029       32158 :   avma = av; return gen_0;
    1030             : }
    1031             : /* Assume Res(f,g) divides p^M. Return Res(f, g), using dynamic p-adic
    1032             :  * precision (until result is non-zero or p^M). */
    1033             : GEN
    1034       54924 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
    1035             : {
    1036       54924 :   GEN R, q = NULL;
    1037             :   long m;
    1038       54924 :   m = init_m(p); if (m < 1) m = 1;
    1039       25700 :   for(;; m <<= 1) {
    1040       80624 :     if (M < 2*m) break;
    1041       39217 :     q = q? sqri(q): powiu(p, m); /* p^m */
    1042       39217 :     R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
    1043       25700 :   }
    1044       41407 :   q = powiu(p, M);
    1045       41407 :   R = ZpX_reduced_resultant(f,g, p, q); return signe(R)? R: q;
    1046             : }
    1047             : 
    1048             : /* v_p(Res(x,y) mod p^m), assumes (lc(x),p) = 1 */
    1049             : static long
    1050      684702 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
    1051             : {
    1052      684702 :   pari_sp av = avma;
    1053             :   GEN z;
    1054             :   long i, l, v;
    1055      684702 :   if (lgefint(pm) == 3)
    1056             :   {
    1057      681171 :     ulong q = pm[2], pp = p[2];
    1058      681171 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
    1059      681171 :     if (!z) { avma = av; return -1; } /* failure */
    1060      624855 :     v = 0; l = lg(z);
    1061      624855 :     for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
    1062             :   }
    1063             :   else
    1064             :   {
    1065        3531 :     z = ZpX_sylvester_echelon(x, y, 1, p, pm);
    1066        3531 :     if (!z) { avma = av; return -1; } /* failure */
    1067        3107 :     v = 0; l = lg(z);
    1068        3107 :     for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
    1069             :   }
    1070      627962 :   return v;
    1071             : }
    1072             : 
    1073             : /* assume (lc(f),p) = 1; no assumption on g */
    1074             : long
    1075      657717 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
    1076             : {
    1077      657717 :   pari_sp av = avma;
    1078      657717 :   GEN q = NULL;
    1079             :   long v, m;
    1080      657717 :   m = init_m(p); if (m < 2) m = 2;
    1081       26985 :   for(;; m <<= 1) {
    1082      684702 :     if (m > M) m = M;
    1083      684702 :     q = q? sqri(q): powiu(p, m); /* p^m */
    1084      684702 :     v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) break;
    1085       56740 :     if (m == M) return M;
    1086       26985 :   }
    1087      627962 :   avma = av; return v;
    1088             : }
    1089             : 
    1090             : /* assume f separable and (lc(f),p) = 1 */
    1091             : long
    1092       28287 : ZpX_disc_val(GEN f, GEN p)
    1093             : {
    1094       28287 :   pari_sp av = avma;
    1095             :   long v;
    1096       28287 :   if (degpol(f) == 1) return 0;
    1097       28287 :   v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
    1098       28287 :   avma = av; return v;
    1099             : }
    1100             : 
    1101             : /* *e a ZX, *d, *z in Z, *d = p^(*vd). Simplify e / d by cancelling a
    1102             :  * common factor p^v; if z!=NULL, update it by cancelling the same power of p */
    1103             : static void
    1104      534968 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
    1105             : {
    1106             :   GEN newe;
    1107      534968 :   long ve = ZX_pvalrem(*e, p, &newe);
    1108      534968 :   if (ve) {
    1109             :     GEN newd;
    1110      304430 :     long v = minss(*vd, ve);
    1111      304430 :     if (v) {
    1112      304430 :       if (v == *vd)
    1113             :       { /* rare, denominator cancelled */
    1114       35847 :         if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
    1115       35847 :         newd = gen_1;
    1116       35847 :         *vd = 0;
    1117       35847 :         if (z) *z =diviiexact(*z, powiu(p, v));
    1118             :       }
    1119             :       else
    1120             :       { /* v = ve < vd, generic case */
    1121      268583 :         GEN q = powiu(p, v);
    1122      268583 :         newd = diviiexact(*d, q);
    1123      268583 :         *vd -= v;
    1124      268583 :         if (z) *z = diviiexact(*z, q);
    1125             :       }
    1126      304430 :       *e = newe;
    1127      304430 :       *d = newd;
    1128             :     }
    1129             :   }
    1130      534968 : }
    1131             : 
    1132             : /* return denominator, a power of p */
    1133             : static GEN
    1134      337442 : QpX_denom(GEN x)
    1135             : {
    1136      337442 :   long i, l = lg(x);
    1137      337442 :   GEN maxd = gen_1;
    1138     1746080 :   for (i=2; i<l; i++)
    1139             :   {
    1140     1408638 :     GEN d = gel(x,i);
    1141     1408638 :     if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
    1142             :   }
    1143      337442 :   return maxd;
    1144             : }
    1145             : static GEN
    1146       47194 : QpXV_denom(GEN x)
    1147             : {
    1148       47194 :   long l = lg(x), i;
    1149       47194 :   GEN maxd = gen_1;
    1150      240975 :   for (i = 1; i < l; i++)
    1151             :   {
    1152      193781 :     GEN d = QpX_denom(gel(x,i));
    1153      193781 :     if (cmpii(d, maxd) > 0) maxd = d;
    1154             :   }
    1155       47194 :   return maxd;
    1156             : }
    1157             : 
    1158             : static GEN
    1159      143661 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
    1160             : {
    1161      143661 :   *pdx = QpX_denom(x);
    1162      143661 :   if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
    1163             :   else {
    1164      108710 :     x = Q_muli_to_int(x,*pdx);
    1165      108710 :     *pv = Z_pval(*pdx, p);
    1166             :   }
    1167      143661 :   return x;
    1168             : }
    1169             : 
    1170             : /* p^v * f o g mod (T,q). q = p^vq  */
    1171             : static GEN
    1172       20517 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
    1173             : {
    1174       20517 :   GEN D = NULL, z, df, dg, qD;
    1175       20517 :   long vD = 0, vdf, vdg;
    1176             : 
    1177       20517 :   f = QpX_remove_denom(f, p, &df, &vdf);
    1178       20517 :   if (typ(g) == t_VEC) /* [num,den,v_p(den)] */
    1179           0 :   { vdg = itos(gel(g,3)); dg = gel(g,2); g = gel(g,1); }
    1180             :   else
    1181       20517 :     g = QpX_remove_denom(g, p, &dg, &vdg);
    1182       20517 :   if (df) { D = df; vD = vdf; }
    1183       20517 :   if (dg) {
    1184        3920 :     long degf = degpol(f);
    1185        3920 :     D = mul_content(D, powiu(dg, degf));
    1186        3920 :     vD += degf * vdg;
    1187             :   }
    1188       20517 :   qD = D ? mulii(q, D): q;
    1189       20517 :   if (dg) f = FpX_rescale(f, dg, qD);
    1190       20517 :   z = FpX_FpXQ_eval(f, g, T, qD);
    1191       20517 :   if (!D) {
    1192           0 :     if (v) {
    1193           0 :       if (v > 0)
    1194           0 :         z = ZX_Z_mul(z, powiu(p, v));
    1195             :       else
    1196           0 :         z = RgX_Rg_div(z, powiu(p, -v));
    1197             :     }
    1198           0 :     return z;
    1199             :   }
    1200       20517 :   update_den(p, &z, &D, &vD, NULL);
    1201       20517 :   qD = mulii(D,q);
    1202       20517 :   if (v) vD -= v;
    1203       20517 :   z = FpX_center(z, qD, shifti(qD,-1));
    1204       20517 :   if (vD > 0)
    1205       20517 :     z = RgX_Rg_div(z, powiu(p, vD));
    1206           0 :   else if (vD < 0)
    1207           0 :     z = ZX_Z_mul(z, powiu(p, -vD));
    1208       20517 :   return z;
    1209             : }
    1210             : 
    1211             : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
    1212             : static GEN
    1213       33075 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
    1214             : {
    1215       33075 :   long i, l = lg(M);
    1216       33075 :   M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
    1217      269157 :   for (i = 1; i < l; i++)
    1218      236082 :     if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
    1219       33075 :   return M;
    1220             : }
    1221             : 
    1222             : /* Return Z-basis for Z[a] + U(a)/p Z[a] in Z[t]/(f), mf = v_p(disc f), U
    1223             :  * a ZX. Special cases: a = t is coded as NULL, U = 0 is coded as NULL */
    1224             : static GEN
    1225       44352 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
    1226             : {
    1227       44352 :   long n = degpol(f), i, dU;
    1228             :   GEN b, h;
    1229             : 
    1230       44352 :   if (n == 1) return matid(1);
    1231       44352 :   if (a && gequalX(a)) a = NULL;
    1232       44352 :   if (DEBUGLEVEL>5)
    1233             :   {
    1234           0 :     err_printf("  entering Dedekind Basis with parameters p=%Ps\n",p);
    1235           0 :     err_printf("  f = %Ps,\n  a = %Ps\n",f, a? a: pol_x(varn(f)));
    1236             :   }
    1237       44352 :   if (a)
    1238             :   {
    1239        9478 :     GEN pd = powiu(p, mf >> 1);
    1240        9478 :     GEN da, pdp = mulii(pd,p), D = pdp;
    1241             :     long vda;
    1242        9478 :     dU = U ? degpol(U): 0;
    1243        9478 :     b = cgetg(n+1, t_MAT);
    1244        9478 :     h = scalarpol(pd, varn(f));
    1245        9478 :     a = QpX_remove_denom(a, p, &da, &vda);
    1246        9478 :     if (da) D = mulii(D, da);
    1247        9478 :     gel(b,1) = scalarcol_shallow(pd, n);
    1248       42301 :     for (i=2; i<=n; i++)
    1249             :     {
    1250       32823 :       if (i == dU+1)
    1251           0 :         h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
    1252             :       else
    1253             :       {
    1254       32823 :         h = FpXQ_mul(h, a, f, D);
    1255       32823 :         if (da) h = ZX_Z_divexact(h, da);
    1256             :       }
    1257       32823 :       gel(b,i) = RgX_to_RgC(h,n);
    1258             :     }
    1259        9478 :     return ZpM_hnfmodid(b, p, pd);
    1260             :   }
    1261             :   else
    1262             :   {
    1263       34874 :     if (!U) return matid(n);
    1264       34874 :     dU = degpol(U);
    1265       34874 :     if (dU == n) return matid(n);
    1266       34874 :     U = FpX_normalize(U, p);
    1267       34874 :     b = cgetg(n+1, t_MAT);
    1268       34874 :     for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
    1269       34874 :     h = RgX_Rg_div(U, p);
    1270       45612 :     for ( ; i <= n; i++)
    1271             :     {
    1272       45612 :       gel(b, i) = RgX_to_RgC(h,n);
    1273       45612 :       if (i == n) break;
    1274       10738 :       h = RgX_shift_shallow(h,1);
    1275             :     }
    1276       34874 :     return b;
    1277             :   }
    1278             : }
    1279             : 
    1280             : static GEN
    1281       47194 : get_partial_order_as_pols(GEN p, GEN f)
    1282             : {
    1283       47194 :   GEN O = maxord(p, f, -1);
    1284       47194 :   long v = varn(f);
    1285       47194 :   return O == gen_1? pol_x_powers(degpol(f), v): RgM_to_RgXV(O, v);
    1286             : }
    1287             : 
    1288             : typedef struct {
    1289             :   /* constants */
    1290             :   long pisprime; /* -1: unknown, 1: prime,  0: composite */
    1291             :   GEN p, f; /* goal: factor f p-adically */
    1292             :   long df;
    1293             :   GEN pdf; /* p^df = reduced discriminant of f */
    1294             :   long mf; /* */
    1295             :   GEN psf, pmf; /* stability precision for f, wanted precision for f */
    1296             :   long vpsf; /* v_p(p_f) */
    1297             :   /* these are updated along the way */
    1298             :   GEN phi; /* a p-integer, in Q[X] */
    1299             :   GEN phi0; /* a p-integer, in Q[X] from testb2 / testc2, to be composed with
    1300             :              * phi when correct precision is known */
    1301             :   GEN chi; /* characteristic polynomial of phi (mod psc) in Z[X] */
    1302             :   GEN nu; /* irreducible divisor of chi mod p, in Z[X] */
    1303             :   GEN invnu; /* numerator ( 1/ Mod(nu, chi) mod pmr ) */
    1304             :   GEN Dinvnu;/* denominator ( ... ) */
    1305             :   long vDinvnu; /* v_p(Dinvnu) */
    1306             :   GEN prc, psc; /* reduced discriminant of chi, stability precision for chi */
    1307             :   long vpsc; /* v_p(p_c) */
    1308             :   GEN ns, nsf, precns; /* cached Newton sums for nsf and their precision */
    1309             : } decomp_t;
    1310             : 
    1311             : static long
    1312        1022 : p_is_prime(decomp_t *S)
    1313             : {
    1314        1022 :   if (S->pisprime < 0) S->pisprime = BPSW_psp(S->p);
    1315        1022 :   return S->pisprime;
    1316             : }
    1317             : 
    1318             : /* if flag = 0, maximal order, else factorization to precision r = flag */
    1319             : static GEN
    1320       23996 : Decomp(decomp_t *S, long flag)
    1321             : {
    1322       23996 :   pari_sp av = avma;
    1323             :   GEN fred, pr, pk, ph, b1, b2, a, e, de, f1, f2, dt, th;
    1324       23996 :   GEN p = S->p, chip;
    1325       23996 :   long k, r = flag? flag: 2*S->df + 1;
    1326             :   long vde, vdt;
    1327             : 
    1328       23996 :   if (DEBUGLEVEL>2)
    1329             :   {
    1330           0 :     err_printf("  entering Decomp");
    1331           0 :     if (DEBUGLEVEL>5) err_printf(", parameters: %Ps^%ld\n  f = %Ps",p, r, S->f);
    1332           0 :     err_printf("\n");
    1333             :   }
    1334       23996 :   chip = FpX_red(S->chi, p);
    1335       23996 :   if (!FpX_valrem(chip, S->nu, p, &b1))
    1336             :   {
    1337           0 :     if (!p_is_prime(S)) pari_err_PRIME("Decomp",p);
    1338           0 :     pari_err_BUG("Decomp (not a factor)");
    1339             :   }
    1340       23996 :   b2 = FpX_div(chip, b1, p);
    1341       23996 :   a = FpX_mul(FpXQ_inv(b2, b1, p), b2, p);
    1342             :   /* E = e / de, e in Z[X], de in Z,  E = a(phi) mod (f, p) */
    1343       23996 :   th = QpX_remove_denom(S->phi, p, &dt, &vdt);
    1344       23996 :   if (dt)
    1345             :   {
    1346        9583 :     long dega = degpol(a);
    1347        9583 :     vde = dega * vdt;
    1348        9583 :     de = powiu(dt, dega);
    1349        9583 :     pr = mulii(p, de);
    1350        9583 :     a = FpX_rescale(a, dt, pr);
    1351             :   }
    1352             :   else
    1353             :   {
    1354       14413 :     vde = 0;
    1355       14413 :     de = gen_1;
    1356       14413 :     pr = p;
    1357             :   }
    1358       23996 :   e = FpX_FpXQ_eval(a, th, S->f, pr);
    1359       23996 :   update_den(p, &e, &de, &vde, NULL);
    1360             : 
    1361       23996 :   pk = p; k = 1;
    1362             :   /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
    1363      153692 :   while (k < r + vde)
    1364             :   { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
    1365             :     GEN D;
    1366      105700 :     pk = sqri(pk); k <<= 1;
    1367      105700 :     e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
    1368      105700 :     de= mulii(de, sqri(de));
    1369      105700 :     vde *= 3;
    1370      105700 :     D = mulii(pk, de);
    1371      105700 :     e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
    1372      105700 :     update_den(p, &e, &de, &vde, NULL);
    1373             :   }
    1374       23996 :   pr = powiu(p, r); /* required precision of the factors */
    1375       23996 :   ph = mulii(de, pr);
    1376       23996 :   fred = centermod(S->f, ph);
    1377       23996 :   e    = centermod(e, ph);
    1378             : 
    1379       23996 :   f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
    1380       23996 :   fred = centermod(fred, pr);
    1381       23996 :   f1   = centermod(f1,   pr);
    1382       23996 :   f2 = FpX_div(fred,f1, pr);
    1383       23996 :   f2 = FpX_center(f2, pr, shifti(pr,-1));
    1384             : 
    1385       23996 :   if (DEBUGLEVEL>5)
    1386           0 :     err_printf("  leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
    1387             : 
    1388       23996 :   if (flag) {
    1389         399 :     gerepileall(av, 2, &f1, &f2);
    1390         399 :     return famat_mul_shallow(ZpX_monic_factor(f1, p, flag),
    1391             :                              ZpX_monic_factor(f2, p, flag));
    1392             :   } else {
    1393             :     GEN D, d1, d2, B1, B2, M;
    1394             :     long n, n1, n2, i;
    1395       23597 :     gerepileall(av, 4, &f1, &f2, &e, &de);
    1396       23597 :     D = de;
    1397       23597 :     B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
    1398       23597 :     B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
    1399       23597 :     d1 = QpXV_denom(B1);
    1400       23597 :     d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
    1401       23597 :     if (d1 != gen_1) {
    1402       20622 :       B1 = Q_muli_to_int(B1, d1);
    1403       20622 :       B2 = Q_muli_to_int(B2, d1);
    1404       20622 :       D = mulii(d1, D);
    1405             :     }
    1406       23597 :     fred = centermod_i(S->f, D, shifti(D,-1));
    1407       23597 :     M = cgetg(n+1, t_MAT);
    1408      144256 :     for (i=1; i<=n1; i++)
    1409      120659 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
    1410       23597 :     e = Z_ZX_sub(de, e); B2 -= n1;
    1411       96719 :     for (   ; i<=n; i++)
    1412       73122 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
    1413       23597 :     return ZpM_hnfmodid(M, p, D);
    1414             :   }
    1415             : }
    1416             : 
    1417             : /* minimum extension valuation: L/E */
    1418             : static void
    1419       49238 : vstar(GEN p,GEN h, long *L, long *E)
    1420             : {
    1421       49238 :   long first, j, k, v, w, m = degpol(h);
    1422             : 
    1423       49238 :   first = 1; k = 1; v = 0;
    1424      343287 :   for (j=1; j<=m; j++)
    1425             :   {
    1426      294049 :     GEN c = gel(h, m-j+2);
    1427      294049 :     if (signe(c))
    1428             :     {
    1429      283507 :       w = Z_pval(c,p);
    1430      283507 :       if (first || w*k < v*j) { v = w; k = j; }
    1431      283507 :       first = 0;
    1432             :     }
    1433             :   }
    1434             :   /* v/k = min_j ( v_p(h_{m-j}) / j ) */
    1435       49238 :   w = (long)ugcd(v,k);
    1436       49238 :   *L = v/w;
    1437       49238 :   *E = k/w;
    1438       49238 : }
    1439             : 
    1440             : static GEN
    1441       11326 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
    1442             : {
    1443             :   GEN z;
    1444       11326 :   a = Q_remove_denom(a, pda);
    1445       11326 :   *pvda = 0;
    1446       11326 :   if (*pda)
    1447             :   {
    1448       11326 :     long v = Z_pvalrem(*pda, p, &z);
    1449       11326 :     if (v) {
    1450       11326 :       *pda = powiu(p, v);
    1451       11326 :       *pvda = v;
    1452       11326 :       N  = mulii(*pda, N);
    1453             :     }
    1454             :     else
    1455           0 :       *pda = NULL;
    1456       11326 :     if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
    1457             :   }
    1458       11326 :   return centermod(a, N);
    1459             : }
    1460             : /* reduce the element a modulo N [ a power of p ], taking first care of the
    1461             :  * denominators */
    1462             : static GEN
    1463        7371 : redelt(GEN a, GEN N, GEN p)
    1464             : {
    1465             :   GEN da;
    1466             :   long vda;
    1467        7371 :   a = redelt_i(a, N, p, &da, &vda);
    1468        7371 :   if (da) a = RgX_Rg_div(a, da);
    1469        7371 :   return a;
    1470             : }
    1471             : 
    1472             : /* compute the Newton sums of g(x) mod p, assume deg g > 0 */
    1473             : GEN
    1474       39326 : polsymmodp(GEN g, GEN p)
    1475             : {
    1476             :   pari_sp av;
    1477       39326 :   long d = degpol(g), i, k;
    1478             :   GEN s, y, po2;
    1479             : 
    1480       39326 :   y = cgetg(d + 1, t_COL);
    1481       39326 :   gel(y,1) = utoipos(d);
    1482       39326 :   if (d == 1) return y;
    1483             :   /* k = 1, split off for efficiency */
    1484       39326 :   po2 = shifti(p,-1); /* to be left on stack */
    1485       39326 :   av = avma;
    1486       39326 :   s = gel(g,d-1+2);
    1487       39326 :   gel(y,2) = gerepileuptoint(av, centermodii(negi(s), p, po2));
    1488      152131 :   for (k = 2; k < d; k++)
    1489             :   {
    1490      112805 :     av = avma;
    1491      112805 :     s = mului(k, remii(gel(g,d-k+2), p));
    1492      112805 :     for (i = 1; i < k; i++) s = addii(s, mulii(gel(y,k-i+1), gel(g,d-i+2)));
    1493      112805 :     togglesign_safe(&s);
    1494      112805 :     gel(y,k+1) = gerepileuptoint(av, centermodii(s, p, po2));
    1495             :   }
    1496       39326 :   return y;
    1497             : }
    1498             : 
    1499             : /* compute the c first Newton sums modulo pp of the
    1500             :    characteristic polynomial of a/d mod chi, d > 0 power of p (NULL = gen_1),
    1501             :    a, chi in Zp[X], vda = v_p(da)
    1502             :    ns = Newton sums of chi */
    1503             : static GEN
    1504       66374 : newtonsums(GEN p, GEN a, GEN da, long vda, GEN chi, long c, GEN pp, GEN ns)
    1505             : {
    1506             :   GEN va, pa, dpa, s;
    1507             :   long j, k, vdpa;
    1508             :   pari_sp av;
    1509             : 
    1510       66374 :   a = centermod(a, pp); av = avma;
    1511       66374 :   dpa = pa = NULL; /* -Wall */
    1512       66374 :   vdpa = 0;
    1513       66374 :   va = zerovec(c);
    1514      450604 :   for (j = 1; j <= c; j++)
    1515             :   { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
    1516             :     long degpa;
    1517      385210 :     pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
    1518      385210 :     degpa = degpol(pa);
    1519      385210 :     if (degpa < 0) {
    1520           0 :       for (; j <= c; j++) gel(va,j) = gen_0;
    1521           0 :       return va;
    1522             :     }
    1523             : 
    1524      385210 :     if (da) {
    1525      377041 :       dpa = j == 1? da: mulii(dpa, da);
    1526      377041 :       vdpa += vda;
    1527      377041 :       update_den(p, &pa, &dpa, &vdpa, &pp);
    1528             :     }
    1529      385210 :     s = mulii(gel(pa,2), gel(ns,1)); /* k = 0 */
    1530      385210 :     for (k=1; k<=degpa; k++) s = addii(s, mulii(gel(pa,k+2), gel(ns,k+1)));
    1531      385210 :     if (da) {
    1532             :       GEN r;
    1533      377041 :       s = dvmdii(s, dpa, &r);
    1534      377041 :       if (r != gen_0) return NULL;
    1535             :     }
    1536      384230 :     gel(va,j) = centermodii(s, pp, shifti(pp,-1));
    1537             : 
    1538      384230 :     if (gc_needed(av, 1))
    1539             :     {
    1540           7 :       if(DEBUGMEM>1) pari_warn(warnmem, "newtonsums");
    1541           7 :       gerepileall(av, dpa?4:3, &pa, &va, &pp, &dpa);
    1542             :     }
    1543             :   }
    1544       65394 :   return va;
    1545             : }
    1546             : 
    1547             : /* compute the characteristic polynomial of a/da mod chi (a in Z[X]), given
    1548             :  * by its Newton sums to a precision of pp using Newton sums */
    1549             : static GEN
    1550       65394 : newtoncharpoly(GEN pp, GEN p, GEN NS)
    1551             : {
    1552       65394 :   long n = lg(NS)-1, j, k;
    1553       65394 :   GEN c = cgetg(n + 2, t_VEC);
    1554             : 
    1555       65394 :   gel(c,1) = (n & 1 ? gen_m1: gen_1);
    1556      447370 :   for (k = 2; k <= n+1; k++)
    1557             :   {
    1558      381997 :     pari_sp av2 = avma;
    1559      381997 :     GEN s = gen_0;
    1560             :     ulong z;
    1561      381997 :     long v = u_pvalrem(k - 1, p, &z);
    1562     3037531 :     for (j = 1; j < k; j++)
    1563             :     {
    1564     2655534 :       GEN t = mulii(gel(NS,j), gel(c,k-j));
    1565     2655534 :       if (!odd(j)) t = negi(t);
    1566     2655534 :       s = addii(s, t);
    1567             :     }
    1568      381997 :     if (v) {
    1569      134680 :       s = gdiv(s, powiu(p, v));
    1570      134680 :       if (typ(s) != t_INT) return NULL;
    1571             :     }
    1572      381976 :     s = mulii(s, Fp_inv(utoipos(z), pp));
    1573      381976 :     gel(c,k) = gerepileuptoint(av2, centermod(s, pp));
    1574             :   }
    1575       65373 :   for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
    1576       65373 :   return gtopoly(c, 0);
    1577             : }
    1578             : 
    1579             : static void
    1580       66374 : manage_cache(decomp_t *S, GEN f, GEN pp)
    1581             : {
    1582       66374 :   GEN t = S->precns;
    1583             : 
    1584       66374 :   if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
    1585       66374 :   if (cmpii(t, pp) < 0) t = pp;
    1586             : 
    1587       66374 :   if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
    1588             :   {
    1589       39326 :     if (DEBUGLEVEL>4)
    1590           0 :       err_printf("  Precision for cached Newton sums for %Ps: %Ps -> %Ps\n",
    1591           0 :                  f, S->precns? S->precns: gen_0, t);
    1592       39326 :     S->nsf = f;
    1593       39326 :     S->ns = polsymmodp(f, t);
    1594       39326 :     S->precns = t;
    1595             :   }
    1596       66374 : }
    1597             : 
    1598             : /* return NULL if a mod f is not an integer
    1599             :  * The denominator of any integer in Zp[X]/(f) divides pdr */
    1600             : static GEN
    1601       66374 : mycaract(decomp_t *S, GEN f, GEN a, GEN pp, GEN pdr)
    1602             : {
    1603             :   pari_sp av;
    1604             :   GEN d, chi, prec1, prec2, prec3, ns;
    1605       66374 :   long vd, n = degpol(f);
    1606             : 
    1607       66374 :   if (gequal0(a)) return pol_0(varn(f));
    1608             : 
    1609       66374 :   a = QpX_remove_denom(a, S->p, &d, &vd);
    1610       66374 :   prec1 = pp;
    1611       66374 :   if (lgefint(S->p) == 3)
    1612       66371 :     prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
    1613       66374 :   if (d)
    1614             :   {
    1615       64001 :     GEN p1 = powiu(d, n);
    1616       64001 :     prec2 = mulii(prec1, p1);
    1617       64001 :     prec3 = mulii(prec1, gmin(mulii(p1, d), pdr));
    1618             :   }
    1619             :   else
    1620        2373 :     prec2 = prec3 = prec1;
    1621       66374 :   manage_cache(S, f, prec3);
    1622             : 
    1623       66374 :   av = avma;
    1624       66374 :   ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
    1625       66374 :   if (!ns) return NULL;
    1626       65394 :   chi = newtoncharpoly(prec1, S->p, ns);
    1627       65394 :   if (!chi) return NULL;
    1628       65373 :   setvarn(chi, varn(f));
    1629       65373 :   return gerepileupto(av, centermod(chi, pp));
    1630             : }
    1631             : 
    1632             : static GEN
    1633       60900 : get_nu(GEN chi, GEN p, long *ptl)
    1634             : {
    1635       60900 :   GEN P = gel(FpX_factor(chi, p),1);
    1636       60900 :   *ptl = lg(P) - 1; return gel(P,*ptl);
    1637             : }
    1638             : 
    1639             : /* Factor characteristic polynomial chi of phi mod p. If it splits, update
    1640             :  * S->{phi, chi, nu} and return 1. In any case, set *nu to an irreducible
    1641             :  * factor mod p of chi */
    1642             : static int
    1643       51597 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
    1644             : {
    1645             :   long l;
    1646       51597 :   *nu  = get_nu(chi, S->p, &l);
    1647       51597 :   if (l == 1) return 0; /* single irreducible factor: doesn't split */
    1648             :   /* phi o phi0 mod (p, f) */
    1649        9583 :   S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
    1650        9583 :   S->chi = chi;
    1651        9583 :   S->nu = *nu; return 1;
    1652             : }
    1653             : 
    1654             : /* Return the prime element in Zp[phi], a t_INT (iff *Ep = 1) or QX;
    1655             :  * nup, chip are ZX. phi = NULL codes X
    1656             :  * If *Ep < oE or Ep divides Ediv (!=0) return NULL (uninteresting) */
    1657             : static GEN
    1658       47537 : getprime(decomp_t *S, GEN phi, GEN chip, GEN nup, long *Lp, long *Ep,
    1659             :          long oE, long Ediv)
    1660             : {
    1661             :   GEN z, chin, q, qp;
    1662             :   long r, s;
    1663             : 
    1664       47537 :   if (phi && dvdii(constant_coeff(chip), S->psc))
    1665             :   {
    1666         203 :     chip = mycaract(S, S->chi, phi, S->pmf, S->prc);
    1667         203 :     if (dvdii(constant_coeff(chip), S->pmf))
    1668          14 :       chip = ZXQ_charpoly(phi, S->chi, varn(chip));
    1669             :   }
    1670       47537 :   if (degpol(nup) == 1)
    1671             :   {
    1672       40040 :     GEN c = gel(nup,2); /* nup = X + c */
    1673       40040 :     chin = signe(c)? RgX_translate(chip, negi(c)): chip;
    1674             :   }
    1675             :   else
    1676        7497 :     chin = ZXQ_charpoly(nup, chip, varn(chip));
    1677             : 
    1678       47537 :   vstar(S->p, chin, Lp, Ep);
    1679       47537 :   if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
    1680             : 
    1681       26677 :   if (*Ep == 1) return S->p;
    1682       14882 :   (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
    1683       14882 :   if (r <= 0)
    1684             :   {
    1685        2205 :     long t = 1 + ((-r) / *Ep);
    1686        2205 :     r += t * *Ep;
    1687        2205 :     s += t * *Lp;
    1688             :   }
    1689             :   /* r > 0 minimal such that r L/E - s = 1/E
    1690             :    * pi = nu^r / p^s is an element of valuation 1/E,
    1691             :    * so is pi + O(p) since 1/E < 1. May compute nu^r mod p^(s+1) */
    1692       14882 :   q = powiu(S->p, s); qp = mulii(q, S->p);
    1693       14882 :   nup = FpXQ_powu(nup, r, S->chi, qp);
    1694       14882 :   if (!phi) return RgX_Rg_div(nup, q); /* phi = X : no composition */
    1695        1568 :   z = compmod(S->p, nup, phi, S->chi, qp, -s);
    1696        1568 :   return signe(z)? z: NULL;
    1697             : }
    1698             : 
    1699             : static int
    1700       14994 : update_phi(decomp_t *S)
    1701             : {
    1702       14994 :   GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
    1703             :   long k;
    1704       15071 :   for (k = 1;; k++)
    1705             :   {
    1706       15071 :     prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, S->vpsc);
    1707       15071 :     if (!equalii(prc, S->psc)) break;
    1708             : 
    1709             :     /* increase precision */
    1710          77 :     S->vpsc = maxss(S->vpsf, S->vpsc + 1);
    1711          77 :     S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
    1712             : 
    1713          77 :     PHI = S->phi;
    1714          77 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
    1715          77 :     PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
    1716          77 :     S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
    1717          77 :   }
    1718       14994 :   psc = mulii(sqri(prc), S->p);
    1719             : 
    1720       14994 :   if (!PHI) /* ok above for k = 1 */
    1721             :   {
    1722       14917 :     PHI = S->phi;
    1723       14917 :     if (S->phi0)
    1724             :     {
    1725        9289 :       PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
    1726        9289 :       S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
    1727             :     }
    1728             :   }
    1729       14994 :   S->phi = PHI;
    1730       14994 :   S->chi = FpX_red(S->chi, psc);
    1731             : 
    1732             :   /* may happen if p is unramified */
    1733       14994 :   if (is_pm1(prc)) return 0;
    1734       11214 :   S->psc = psc;
    1735       11214 :   S->vpsc = 2*Z_pval(prc, S->p) + 1;
    1736       11214 :   S->prc = mulii(prc, S->p); return 1;
    1737             : }
    1738             : 
    1739             : /* return 1 if at least 2 factors mod p ==> chi splits
    1740             :  * Replace S->phi such that F increases (to D) */
    1741             : static int
    1742        7805 : testb2(decomp_t *S, long D, GEN theta)
    1743             : {
    1744        7805 :   long v = varn(S->chi), dlim = degpol(S->chi)-1;
    1745        7805 :   GEN T0 = S->phi, chi, phi, nu;
    1746        7805 :   if (DEBUGLEVEL>4) err_printf("  Increasing Fa\n");
    1747             :   for (;;)
    1748             :   {
    1749        7805 :     phi = gadd(theta, random_FpX(dlim, v, S->p));
    1750        7805 :     chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1751             :     /* phi non-primary ? */
    1752        7805 :     if (split_char(S, chi, phi, T0, &nu)) return 1;
    1753        7798 :     if (degpol(nu) == D) break;
    1754           0 :   }
    1755             :   /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
    1756        7798 :   S->phi0 = T0;
    1757        7798 :   S->chi = chi;
    1758        7798 :   S->phi = phi;
    1759        7798 :   S->nu = nu; return 0;
    1760             : }
    1761             : 
    1762             : /* return 1 if at least 2 factors mod p ==> chi can be split.
    1763             :  * compute a new S->phi such that E = lcm(Ea, Et);
    1764             :  * A a ZX, T a t_INT (iff Et = 1, probably impossible ?) or QX */
    1765             : static int
    1766        1568 : testc2(decomp_t *S, GEN A, long Ea, GEN T, long Et)
    1767             : {
    1768        1568 :   GEN c, chi, phi, nu, T0 = S->phi;
    1769             : 
    1770        1568 :   if (DEBUGLEVEL>4) err_printf("  Increasing Ea\n");
    1771        1568 :   if (Et == 1) /* same as other branch, split for efficiency */
    1772           0 :     c = A; /* Et = 1 => s = 1, r = 0, t = 0 */
    1773             :   else
    1774             :   {
    1775             :     long r, s, t;
    1776        1568 :     (void)cbezout(Ea, Et, &r, &s); t = 0;
    1777        1568 :     while (r < 0) { r = r + Et; t++; }
    1778        1568 :     while (s < 0) { s = s + Ea; t++; }
    1779             : 
    1780             :     /* A^s T^r / p^t */
    1781        1568 :     c = RgXQ_mul(RgXQ_powu(A, s, S->chi), RgXQ_powu(T, r, S->chi), S->chi);
    1782        1568 :     c = RgX_Rg_div(c, powiu(S->p, t));
    1783        1568 :     c = redelt(c, S->psc, S->p);
    1784             :   }
    1785        1568 :   phi = RgX_add(c,  pol_x(varn(S->chi)));
    1786        1568 :   chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1787        1568 :   if (split_char(S, chi, phi, T0, &nu)) return 1;
    1788             :   /* E_phi = lcm(E_alpha,E_theta) */
    1789        1568 :   S->phi0 = T0;
    1790        1568 :   S->chi = chi;
    1791        1568 :   S->phi = phi;
    1792        1568 :   S->nu = nu; return 0;
    1793             : }
    1794             : 
    1795             : /* Return h^(-degpol(P)) P(x * h) if result is integral, NULL otherwise */
    1796             : static GEN
    1797        1421 : ZX_rescale_inv(GEN P, GEN h)
    1798             : {
    1799        1421 :   long i, l = lg(P);
    1800        1421 :   GEN Q = cgetg(l,t_POL), hi = h;
    1801        1421 :   gel(Q,l-1) = gel(P,l-1);
    1802        8218 :   for (i=l-2; i>=2; i--)
    1803             :   {
    1804             :     GEN r;
    1805        8218 :     gel(Q,i) = dvmdii(gel(P,i), hi, &r);
    1806        8218 :     if (signe(r)) return NULL;
    1807        8218 :     if (i == 2) break;
    1808        6797 :     hi = mulii(hi,h);
    1809             :   }
    1810        1421 :   Q[1] = P[1]; return Q;
    1811             : }
    1812             : 
    1813             : /* x p^-eq nu^-er mod p */
    1814             : static GEN
    1815       39025 : get_gamma(decomp_t *S, GEN x, long eq, long er)
    1816             : {
    1817       39025 :   GEN q, g = x, Dg = powiu(S->p, eq);
    1818       39025 :   long vDg = eq;
    1819       39025 :   if (er)
    1820             :   {
    1821        7714 :     if (!S->invnu)
    1822             :     {
    1823        3955 :       while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
    1824        3955 :       S->invnu = QXQ_inv(S->nu, S->chi);
    1825        3955 :       S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
    1826             :     }
    1827        7714 :     if (S->Dinvnu) {
    1828        7714 :       Dg = mulii(Dg, powiu(S->Dinvnu, er));
    1829        7714 :       vDg += er * S->vDinvnu;
    1830             :     }
    1831        7714 :     q = mulii(S->p, Dg);
    1832        7714 :     g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
    1833        7714 :     g = FpX_rem(g, S->chi, q);
    1834        7714 :     update_den(S->p, &g, &Dg, &vDg, NULL);
    1835        7714 :     g = centermod(g, mulii(S->p, Dg));
    1836             :   }
    1837       39025 :   if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
    1838       39025 :   return g;
    1839             : }
    1840             : static GEN
    1841       39445 : get_g(decomp_t *S, long Ea, long L, long E, GEN beta, GEN *pchig,
    1842             :       long *peq, long *per)
    1843             : {
    1844             :   long eq, er;
    1845       39445 :   GEN g, chig, chib = NULL;
    1846             :   for(;;) /* at most twice */
    1847             :   {
    1848       40446 :     if (L < 0)
    1849             :     {
    1850        1701 :       chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
    1851        1701 :       vstar(S->p, chib, &L, &E);
    1852             :     }
    1853       40446 :     eq = L / E; er = L*Ea / E - eq*Ea;
    1854             :     /* floor(L Ea/E) = eq Ea + er */
    1855       40446 :     if (er || !chib)
    1856             :     { /* g might not be an integer ==> chig = NULL */
    1857       39025 :       g = get_gamma(S, beta, eq, er);
    1858       39025 :       chig = mycaract(S, S->chi, g, S->psc, S->prc);
    1859             :     }
    1860             :     else
    1861             :     { /* g = beta/p^eq, special case of the above */
    1862        1421 :       GEN h = powiu(S->p, eq);
    1863        1421 :       g = RgX_Rg_div(beta, h);
    1864        1421 :       chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
    1865        1421 :       if (chig) chig = FpX_red(chig, S->pmf);
    1866             :     }
    1867             :     /* either success or second consecutive failure */
    1868       40446 :     if (chig || chib) break;
    1869             :     /* if g fails the v*-test, v(beta) was wrong. Retry once */
    1870        1001 :     L = -1;
    1871        1001 :   }
    1872       39445 :   *pchig = chig; *peq = eq; *per = er; return g;
    1873             : }
    1874             : 
    1875             : /* return 1 if at least 2 factors mod p ==> chi can be split */
    1876             : static int
    1877       18949 : loop(decomp_t *S, long Ea)
    1878             : {
    1879       18949 :   pari_sp av = avma;
    1880       18949 :   GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
    1881       18949 :   long N = degpol(S->f), v = varn(S->f);
    1882       18949 :   S->invnu = NULL;
    1883             :   for (;;)
    1884             :   { /* beta tends to a factor of chi */
    1885             :     long L, i, Fg, eq, er;
    1886       39445 :     GEN chig = NULL, d, g, nug;
    1887             : 
    1888       39445 :     if (DEBUGLEVEL>4) err_printf("  beta = %Ps\n", beta);
    1889       39445 :     L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
    1890       39445 :     if (L > S->mf) L = -1; /* from scratch */
    1891       39445 :     g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
    1892       39445 :     if (DEBUGLEVEL>4) err_printf("  (eq,er) = (%ld,%ld)\n", eq,er);
    1893             :     /* g = beta p^-eq  nu^-er (a unit), chig = charpoly(g) */
    1894       58394 :     if (split_char(S, chig, g,S->phi, &nug)) return 1;
    1895             : 
    1896       30268 :     Fg = degpol(nug);
    1897       30268 :     if (Fg == 1)
    1898             :     { /* frequent special case nug = x - d */
    1899             :       long Le, Ee;
    1900             :       GEN chie, nue, e, pie;
    1901       19684 :       d = negi(gel(nug,2));
    1902       19684 :       chie = RgX_translate(chig, d);
    1903       19684 :       nue = pol_x(v);
    1904       19684 :       e = RgX_Rg_sub(g, d);
    1905       19684 :       pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1906       19684 :       if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1907             :     }
    1908             :     else
    1909             :     {
    1910       10584 :       long Fa = degpol(S->nu), vdeng;
    1911             :       GEN deng, numg, nume;
    1912       18963 :       if (Fa % Fg) return testb2(S, clcm(Fa,Fg), g);
    1913             :       /* nu & nug irreducible mod p, deg nug | deg nu. To improve beta, look
    1914             :        * for a root d of nug in Fp[phi] such that v_p(g - d) > 0 */
    1915        2779 :       if (ZX_equal(nug, S->nu))
    1916        1757 :         d = pol_x(v);
    1917             :       else
    1918             :       {
    1919        1022 :         if (!p_is_prime(S)) pari_err_PRIME("FpX_ffisom",S->p);
    1920        1022 :         d = FpX_ffisom(nug, S->nu, S->p);
    1921             :       }
    1922             :       /* write g = numg / deng, e = nume / deng */
    1923        2779 :       numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
    1924        4753 :       for (i = 1; i <= Fg; i++)
    1925             :       {
    1926             :         GEN chie, nue, e;
    1927        4753 :         if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
    1928        4753 :         nume = ZX_sub(numg, ZX_Z_mul(d, deng));
    1929             :         /* test e = nume / deng */
    1930        4753 :         if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
    1931        1974 :           continue;
    1932        2779 :         e = RgX_Rg_div(nume, deng);
    1933        2779 :         chie = mycaract(S, S->chi, e, S->psc, S->prc);
    1934        3353 :         if (split_char(S, chie, e,S->phi, &nue)) return 1;
    1935        2380 :         if (RgX_is_monomial(nue))
    1936             :         { /* v_p(e) = v_p(g - d) > 0 */
    1937             :           long Le, Ee;
    1938             :           GEN pie;
    1939        2380 :           pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1940        2380 :           if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1941        2205 :           break;
    1942             :         }
    1943             :       }
    1944        2205 :       if (i > Fg)
    1945             :       {
    1946           0 :         if (!p_is_prime(S)) pari_err_PRIME("nilord",S->p);
    1947           0 :         pari_err_BUG("nilord (no root)");
    1948             :       }
    1949             :     }
    1950       20496 :     if (eq) d = gmul(d, powiu(S->p,  eq));
    1951       20496 :     if (er) d = gmul(d, gpowgs(S->nu, er));
    1952       20496 :     beta = gsub(beta, d);
    1953             : 
    1954       20496 :     if (gc_needed(av,1))
    1955             :     {
    1956           0 :       if (DEBUGMEM > 1) pari_warn(warnmem, "nilord");
    1957           0 :       gerepileall(av, S->invnu? 6: 4, &beta, &(S->precns), &(S->ns), &(S->nsf), &(S->invnu), &(S->Dinvnu));
    1958             :     }
    1959       20496 :   }
    1960             : }
    1961             : 
    1962             : static long
    1963       25109 : loop_init(decomp_t *S, GEN *popa, long *poE)
    1964             : {
    1965       25109 :   long oE = *poE;
    1966       25109 :   GEN opa = *popa;
    1967             :   for(;;)
    1968             :   {
    1969             :     long l, La, Ea; /* N.B If oE = 0, getprime cannot return NULL */
    1970       25473 :     GEN pia  = getprime(S, NULL, S->chi, S->nu, &La, &Ea, oE,0);
    1971       25473 :     if (pia) { /* success, we break out in THIS loop */
    1972       25109 :       opa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
    1973       25109 :       oE = Ea;
    1974       50218 :       if (La == 1) break; /* no need to change phi so that nu = pia */
    1975             :     }
    1976             :     /* phi += prime elt */
    1977       13678 :     S->phi = typ(opa) == t_INT? RgX_Rg_add_shallow(S->phi, opa)
    1978        8050 :                               : RgX_add(S->phi, opa);
    1979             :     /* recompute char. poly. chi from scratch */
    1980        5628 :     S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
    1981        5628 :     S->nu = get_nu(S->chi, S->p, &l);
    1982        5628 :     if (l > 1) return l; /* we can get a decomposition */
    1983        5628 :     if (!update_phi(S)) return 1; /* unramified / irreducible */
    1984        5628 :     if (pia) break;
    1985         364 :   }
    1986       25109 :   *poE = oE; *popa = opa; return 0;
    1987             : }
    1988             : /* flag != 0 iff we're looking for the p-adic factorization,
    1989             :    in which case it is the p-adic precision we want */
    1990             : static GEN
    1991       19523 : nilord(decomp_t *S, GEN dred, long flag)
    1992             : {
    1993       19523 :   GEN p = S->p;
    1994       19523 :   long oE, l, N  = degpol(S->f), v = varn(S->f);
    1995             :   GEN opa; /* t_INT or QX */
    1996             : 
    1997       19523 :   if (DEBUGLEVEL>2)
    1998             :   {
    1999           0 :     err_printf("  entering Nilord");
    2000           0 :     if (DEBUGLEVEL>4)
    2001             :     {
    2002           0 :       err_printf(" with parameters: %Ps^%ld\n", p, S->df);
    2003           0 :       err_printf("  fx = %Ps, gx = %Ps", S->f, S->nu);
    2004             :     }
    2005           0 :     err_printf("\n");
    2006             :   }
    2007             : 
    2008       19523 :   S->psc = mulii(sqri(dred), p);
    2009       19523 :   S->vpsc= 2*S->df + 1;
    2010       19523 :   S->prc = mulii(dred, p);
    2011       19523 :   S->psf = S->psc;
    2012       19523 :   S->vpsf = S->vpsc;
    2013       19523 :   S->chi = FpX_red(S->f, S->psc);
    2014       19523 :   S->phi = pol_x(v);
    2015       19523 :   S->pmf = powiu(p, S->mf+1);
    2016       19523 :   S->precns = NULL;
    2017       19523 :   oE = 0;
    2018       19523 :   opa = NULL; /* -Wall */
    2019             :   for(;;)
    2020             :   {
    2021       25109 :     long Fa = degpol(S->nu);
    2022       25109 :     S->phi0 = NULL; /* no delayed composition */
    2023       25109 :     l = loop_init(S, &opa, &oE);
    2024       25109 :     if (l > 1) return Decomp(S,flag);
    2025       25109 :     if (l == 1) break;
    2026       25109 :     if (DEBUGLEVEL>4) err_printf("  (Fa, oE) = (%ld,%ld)\n", Fa, oE);
    2027       25109 :     if (oE*Fa == N)
    2028             :     { /* O = Zp[phi] */
    2029        6160 :       if (flag) return NULL;
    2030        5803 :       return dbasis(p, S->f, S->mf, redelt(S->phi,sqri(p),p), NULL);
    2031             :     }
    2032       18949 :     if (loop(S, oE)) return Decomp(S,flag);
    2033        9366 :     if (!update_phi(S)) break; /* unramified / irreducible */
    2034        5586 :   }
    2035        3780 :   if (flag) return NULL;
    2036        3675 :   S->nu = get_nu(S->chi, S->p, &l);
    2037        3675 :   return l != 1? Decomp(S,flag): dbasis(p, S->f, S->mf, S->phi, S->chi);
    2038             : }
    2039             : 
    2040             : static GEN
    2041       33936 : maxord_i(GEN p, GEN f, long mf, GEN w, long flag)
    2042             : {
    2043       33936 :   long l = lg(w)-1;
    2044       33936 :   GEN h = gel(w,l); /* largest factor */
    2045       33936 :   GEN D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
    2046             :   decomp_t S;
    2047             : 
    2048       33936 :   S.f = f;
    2049       33936 :   S.pisprime = -1;
    2050       33936 :   S.p = p;
    2051       33936 :   S.mf = mf;
    2052       33936 :   S.nu = h;
    2053       33936 :   S.df = Z_pval(D, p);
    2054       33936 :   S.pdf = powiu(p, S.df);
    2055       33936 :   if (l == 1) return nilord(&S, D, flag);
    2056       14413 :   if (flag && flag <= mf) flag = mf + 1;
    2057       14413 :   S.phi = pol_x(varn(f));
    2058       14413 :   S.chi = f; return Decomp(&S, flag);
    2059             : }
    2060             : 
    2061             : static int
    2062        1029 : expo_is_squarefree(GEN e)
    2063             : {
    2064        1029 :   long i, l = lg(e);
    2065        1295 :   for (i=1; i<l; i++)
    2066        1127 :     if (e[i] != 1) return 0;
    2067         168 :   return 1;
    2068             : }
    2069             : 
    2070             : /* assume f a ZX with leading_coeff 1, degree > 0 */
    2071             : GEN
    2072        1232 : ZpX_monic_factor(GEN f, GEN p, long prec)
    2073             : {
    2074             :   GEN poly, ex, P, E;
    2075             :   long l, i;
    2076             : 
    2077        1232 :   if (degpol(f) == 1) return mkmat2(mkcol(f), mkcol(gen_1));
    2078             : 
    2079        1008 :   poly = ZX_squff(f,&ex); l = lg(poly);
    2080        1008 :   P = cgetg(l, t_VEC);
    2081        1008 :   E = cgetg(l, t_VEC);
    2082        2037 :   for (i = 1; i < l; i++)
    2083             :   {
    2084        1029 :     pari_sp av1 = avma;
    2085        1029 :     GEN fx = gel(poly,i), fa = FpX_factor(fx,p);
    2086        1029 :     GEN w = gel(fa,1), e = gel(fa,2);
    2087        1029 :     if (expo_is_squarefree(e))
    2088             :     { /* no repeated factors: Hensel lift */
    2089         168 :       GEN L = ZpX_liftfact(fx, w, powiu(p,prec), p, prec);
    2090         168 :       gel(P,i) = L; settyp(L, t_COL);
    2091         168 :       gel(E,i) = const_col(lg(L)-1, utoipos(ex[i]));
    2092             :     }
    2093             :     else
    2094             :     { /* use Round 4 */
    2095         861 :       GEN M = maxord_i(p, fx, ZpX_disc_val(fx,p), w, prec);
    2096         861 :       if (M)
    2097             :       {
    2098         399 :         M = gerepilecopy(av1, M);
    2099         399 :         gel(P,i) = gel(M,1);
    2100         399 :         gel(E,i) = ZC_z_mul(gel(M,2), ex[i]);
    2101             :       }
    2102             :       else
    2103             :       { /* irreducible */
    2104         462 :         avma = av1;
    2105         462 :         gel(P,i) = mkcol(fx);
    2106         462 :         gel(E,i) = mkcols(ex[i]);
    2107             :       }
    2108             :     }
    2109             :   }
    2110        1008 :   return mkmat2(shallowconcat1(P), shallowconcat1(E));
    2111             : }
    2112             : 
    2113             : /* DT = multiple of disc(T) or NULL
    2114             :  * Return a multiple of the denominator of an algebraic integer (in Q[X]/(T))
    2115             :  * when expressed in terms of the power basis */
    2116             : GEN
    2117        2570 : indexpartial(GEN T, GEN DT)
    2118             : {
    2119        2570 :   pari_sp av = avma;
    2120             :   long i, nb;
    2121        2570 :   GEN fa, E, P, res = gen_1, dT = ZX_deriv(T);
    2122             : 
    2123        2570 :   if (!DT) DT = ZX_disc(T);
    2124        2570 :   fa = absZ_factor_limit(DT, 0);
    2125        2570 :   P = gel(fa,1);
    2126        2570 :   E = gel(fa,2); nb = lg(P)-1;
    2127       14959 :   for (i = 1; i <= nb; i++)
    2128             :   {
    2129       12389 :     long e = itou(gel(E,i)), e2 = e >> 1;
    2130       12389 :     GEN p = gel(P,i), q = p;
    2131       12389 :     if (i == nb)
    2132        2563 :       q = powiu(p, (odd(e) && !BPSW_psp(p))? e2+1: e2);
    2133        9826 :     else if (e2 >= 2)
    2134        5917 :       q = ZpX_reduced_resultant_fast(T, dT, p, e2);
    2135       12389 :     res = mulii(res, q);
    2136             :   }
    2137        2570 :   return gerepileuptoint(av,res);
    2138             : }
    2139             : 
    2140             : /*******************************************************************/
    2141             : /*                                                                 */
    2142             : /*    2-ELT REPRESENTATION FOR PRIME IDEALS (dividing index)       */
    2143             : /*                                                                 */
    2144             : /*******************************************************************/
    2145             : /* to compute norm of elt in basis form */
    2146             : typedef struct {
    2147             :   long r1;
    2148             :   GEN M;  /* via embed_norm */
    2149             : 
    2150             :   GEN D, w, T; /* via resultant if M = NULL */
    2151             : } norm_S;
    2152             : 
    2153             : static GEN
    2154       58220 : get_norm(norm_S *S, GEN a)
    2155             : {
    2156       58220 :   if (S->M)
    2157             :   {
    2158             :     long e;
    2159       57575 :     GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
    2160       57575 :     if (e > -5) pari_err_PREC( "get_norm");
    2161       57575 :     return N;
    2162             :   }
    2163         645 :   if (S->w) a = RgV_RgC_mul(S->w, a);
    2164         645 :   return ZX_resultant_all(S->T, a, S->D, 0);
    2165             : }
    2166             : static void
    2167       15746 : init_norm(norm_S *S, GEN nf, GEN p)
    2168             : {
    2169       15746 :   GEN T = nf_get_pol(nf), M = nf_get_M(nf);
    2170       15746 :   long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
    2171             : 
    2172       15746 :   S->r1 = nf_get_r1(nf);
    2173       15746 :   if (N * ex <= prec2nbits(gprecision(M)) - 20)
    2174             :   { /* enough prec to use embed_norm */
    2175       15694 :     S->M = M;
    2176       15694 :     S->D = NULL;
    2177       15694 :     S->w = NULL;
    2178       15694 :     S->T = NULL;
    2179             :   }
    2180             :   else
    2181             :   {
    2182          52 :     GEN w = leafcopy(nf_get_zkprimpart(nf)), D = nf_get_zkden(nf), Dp = sqri(p);
    2183             :     long i;
    2184          52 :     if (!equali1(D))
    2185             :     {
    2186          52 :       GEN w1 = D;
    2187          52 :       long v = Z_pval(D, p);
    2188          52 :       D = powiu(p, v);
    2189          52 :       Dp = mulii(D, Dp);
    2190          52 :       gel(w, 1) = remii(w1, Dp);
    2191             :     }
    2192          52 :     for (i=2; i<=N; i++) gel(w,i) = FpX_red(gel(w,i), Dp);
    2193          52 :     S->M = NULL;
    2194          52 :     S->D = D;
    2195          52 :     S->w = w;
    2196          52 :     S->T = T;
    2197             :   }
    2198       15746 : }
    2199             : /* f = f(pr/p), q = p^(f+1), a in pr.
    2200             :  * Return 1 if v_pr(a) = 1, and 0 otherwise */
    2201             : static int
    2202       58220 : is_uniformizer(GEN a, GEN q, norm_S *S)
    2203       58220 : { return (remii(get_norm(S,a), q) != gen_0); }
    2204             : 
    2205             : /* Return x * y, x, y are t_MAT (Fp-basis of in O_K/p), assume (x,y)=1.
    2206             :  * Either x or y may be NULL (= O_K), not both */
    2207             : static GEN
    2208      126602 : mul_intersect(GEN x, GEN y, GEN p)
    2209             : {
    2210      126602 :   if (!x) return y;
    2211       87329 :   if (!y) return x;
    2212       74238 :   return FpM_intersect(x, y, p);
    2213             : }
    2214             : /* Fp-basis of (ZK/pr): applied to the primes found in primedec_aux()
    2215             :  * true nf */
    2216             : static GEN
    2217       50928 : Fp_basis(GEN nf, GEN pr)
    2218             : {
    2219             :   long i, j, l;
    2220             :   GEN x, y;
    2221             :   /* already in basis form (from Buchman-Lenstra) ? */
    2222       50928 :   if (typ(pr) == t_MAT) return pr;
    2223             :   /* ordinary prid (from Kummer) */
    2224       10425 :   x = pr_hnf(nf, pr);
    2225       10425 :   l = lg(x);
    2226       10425 :   y = cgetg(l, t_MAT);
    2227      127061 :   for (i=j=1; i<l; i++)
    2228      116636 :     if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
    2229       10425 :   setlg(y, j); return y;
    2230             : }
    2231             : /* Let Ip = prod_{ P | p } P be the p-radical. The list L contains the
    2232             :  * P (mod Ip) seen as sub-Fp-vector spaces of ZK/Ip.
    2233             :  * Return the list of (Ip / P) (mod Ip).
    2234             :  * N.B: All ideal multiplications are computed as intersections of Fp-vector
    2235             :  * spaces. true nf */
    2236             : static GEN
    2237       15746 : get_LV(GEN nf, GEN L, GEN p, long N)
    2238             : {
    2239       15746 :   long i, l = lg(L)-1;
    2240             :   GEN LV, LW, A, B;
    2241             : 
    2242       15746 :   LV = cgetg(l+1, t_VEC);
    2243       15746 :   if (l == 1) { gel(LV,1) = matid(N); return LV; }
    2244       13091 :   LW = cgetg(l+1, t_VEC);
    2245       13091 :   for (i=1; i<=l; i++) gel(LW,i) = Fp_basis(nf, gel(L,i));
    2246             : 
    2247             :   /* A[i] = L[1]...L[i-1], i = 2..l */
    2248       13091 :   A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
    2249       13091 :   for (i=1; i < l; i++) gel(A,i+1) = mul_intersect(gel(A,i), gel(LW,i), p);
    2250             :   /* B[i] = L[i+1]...L[l], i = 1..(l-1) */
    2251       13091 :   B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
    2252       13091 :   for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
    2253       13091 :   for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
    2254       13091 :   return LV;
    2255             : }
    2256             : 
    2257             : static void
    2258           0 : errprime(GEN p) { pari_err_PRIME("idealprimedec",p); }
    2259             : 
    2260             : /* P = Fp-basis (over O_K/p) for pr.
    2261             :  * V = Z-basis for I_p/pr. ramif != 0 iff some pr|p is ramified.
    2262             :  * Return a p-uniformizer for pr. Assume pr not inert, i.e. m > 0 */
    2263             : static GEN
    2264       41794 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
    2265             : {
    2266       41794 :   long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
    2267             :   GEN u, Mv, x, q;
    2268             : 
    2269       41794 :   f = N - m; /* we want v_p(Norm(x)) = p^f */
    2270       41794 :   q = powiu(p,f+1);
    2271             : 
    2272       41794 :   u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
    2273       41794 :   setlg(u, lg(P));
    2274       41794 :   u = centermod(ZM_ZC_mul(P, u), p);
    2275       41794 :   if (is_uniformizer(u, q, S)) return u;
    2276       12416 :   if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
    2277        9550 :     gel(u,1) = addii(gel(u,1), p); /* try u + p */
    2278             :   else
    2279        2866 :     gel(u,1) = subii(gel(u,1), p); /* try u - p */
    2280       12416 :   if (!ramif || is_uniformizer(u, q, S)) return u;
    2281             : 
    2282             :   /* P/p ramified, u in P^2, not in Q for all other Q|p */
    2283        4713 :   Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
    2284        4713 :   l = lg(P);
    2285       11678 :   for (i=1; i<l; i++)
    2286             :   {
    2287       11678 :     x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
    2288       11678 :     if (is_uniformizer(x, q, S)) return x;
    2289             :   }
    2290           0 :   errprime(p);
    2291             :   return NULL; /* LCOV_EXCL_LINE */
    2292             : }
    2293             : 
    2294             : /*******************************************************************/
    2295             : /*                                                                 */
    2296             : /*                   BUCHMANN-LENSTRA ALGORITHM                    */
    2297             : /*                                                                 */
    2298             : /*******************************************************************/
    2299             : static GEN
    2300      811605 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
    2301      811605 : { return mkvec5(p, u, utoipos(e), utoipos(f), t); }
    2302             : 
    2303             : /* nf a true nf; pr = (p,u) of ramification index e */
    2304             : GEN
    2305      756943 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
    2306             : {
    2307      756943 :   GEN t, T = nf_get_pol(nf);
    2308      756943 :   long f = degpol(u), N = degpol(T);
    2309             : 
    2310      756943 :   if (f == N) /* inert */
    2311             :   {
    2312      125664 :     u = scalarcol_shallow(p,N);
    2313      125664 :     t = gen_1;
    2314             :   }
    2315             :   else
    2316             :   { /* make sure v_pr(u) = 1 (automatic if e>1) */
    2317      631279 :     t = poltobasis(nf, FpX_div(T,u,p));
    2318      631279 :     t = centermod(t, p);
    2319      631279 :     u = FpX_center(u, p, shifti(p,-1));
    2320      631279 :     if (e == 1 && ZpX_resultant_val(T, u, p, f+1) > f)
    2321       36139 :       gel(u,2) = addii(gel(u,2), p);
    2322      631279 :     u = poltobasis(nf,u);
    2323      631279 :     t = zk_multable(nf, t); /* t never a scalar here since pr is not inert */
    2324             :   }
    2325      756943 :   return mk_pr(p,u,e,f,t);
    2326             : }
    2327             : 
    2328             : typedef struct {
    2329             :   GEN nf, p;
    2330             :   long I;
    2331             : } eltmod_muldata;
    2332             : 
    2333             : static GEN
    2334      177763 : sqr_mod(void *data, GEN x)
    2335             : {
    2336      177763 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2337      177763 :   return FpC_red(nfsqri(D->nf, x), D->p);
    2338             : }
    2339             : static GEN
    2340       84535 : ei_msqr_mod(void *data, GEN x)
    2341             : {
    2342       84535 :   GEN x2 = sqr_mod(data, x);
    2343       84535 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2344       84535 :   return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
    2345             : }
    2346             : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
    2347             : static GEN
    2348      108136 : pow_ei_mod_p(GEN nf, long I, GEN p)
    2349             : {
    2350      108136 :   pari_sp av = avma;
    2351             :   eltmod_muldata D;
    2352      108136 :   long N = nf_get_degree(nf);
    2353      108136 :   GEN y = col_ei(N,I);
    2354      108136 :   if (I == 1) return y;
    2355       92152 :   D.nf = nf;
    2356       92152 :   D.p = p;
    2357       92152 :   D.I = I;
    2358       92152 :   y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
    2359       92152 :   return gerepileupto(av,y);
    2360             : }
    2361             : 
    2362             : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
    2363             : static GEN
    2364       15746 : pradical(GEN nf, GEN p, GEN *phi)
    2365             : {
    2366       15746 :   long i, N = nf_get_degree(nf);
    2367             :   GEN q,m,frob,rad;
    2368             : 
    2369             :   /* matrix of Frob: x->x^p over Z_K/p */
    2370       15746 :   frob = cgetg(N+1,t_MAT);
    2371       15746 :   for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
    2372             : 
    2373       15746 :   m = frob; q = p;
    2374       15746 :   while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
    2375       15746 :   rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
    2376       15746 :   for (i=1; i<=N; i++) gcoeff(frob,i,i) = subiu(gcoeff(frob,i,i), 1);
    2377       15746 :   *phi = frob; return rad;
    2378             : }
    2379             : 
    2380             : /* return powers of a: a^0, ... , a^d,  d = dim A */
    2381             : static GEN
    2382       25449 : get_powers(GEN mul, GEN p)
    2383             : {
    2384       25449 :   long i, d = lgcols(mul);
    2385       25449 :   GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
    2386             : 
    2387       25449 :   gel(P,0) = scalarcol_shallow(gen_1, d-1);
    2388       25449 :   z = gel(mul,1);
    2389      142777 :   for (i=1; i<=d; i++)
    2390             :   {
    2391      117328 :     gel(P,i) = z; /* a^i */
    2392      117328 :     if (i!=d) z = FpM_FpC_mul(mul, z, p);
    2393             :   }
    2394       25449 :   return pow;
    2395             : }
    2396             : 
    2397             : /* minimal polynomial of a in A (dim A = d).
    2398             :  * mul = multiplication table by a in A */
    2399             : static GEN
    2400       22936 : pol_min(GEN mul, GEN p)
    2401             : {
    2402       22936 :   pari_sp av = avma;
    2403       22936 :   GEN z = FpM_deplin(get_powers(mul, p), p);
    2404       22936 :   return gerepilecopy(av, RgV_to_RgX(z,0));
    2405             : }
    2406             : 
    2407             : static GEN
    2408       53317 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N, long flim)
    2409             : {
    2410             :   GEN u, t;
    2411             :   long e, f;
    2412             : 
    2413       53317 :   if (typ(P) == t_VEC)
    2414             :   { /* already done (Kummer) */
    2415       10425 :     f = pr_get_f(P);
    2416       10425 :     if (flim > 0 && f > flim) return NULL;
    2417        9907 :     if (flim == -2) return (GEN)f;
    2418        9907 :     return P;
    2419             :   }
    2420       42892 :   f = N - (lg(P)-1);
    2421       42892 :   if (flim > 0 && f > flim) return NULL;
    2422       42473 :   if (flim == -2) return (GEN)f;
    2423             :   /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
    2424             :    * so that v_P(t) = e(P/p)-1 */
    2425       42263 :   if (f == N) {
    2426         469 :     u = scalarcol_shallow(p,N);
    2427         469 :     t = gen_1;
    2428         469 :     e = 1;
    2429             :   } else {
    2430             :     GEN mt;
    2431       41794 :     u = uniformizer(nf, S, P, V, p, ramif);
    2432       41794 :     t = FpM_deplin(zk_multable(nf,u), p);
    2433       41794 :     mt = zk_multable(nf, t);
    2434       41794 :     e = ramif? 1 + ZC_nfval(t,mk_pr(p,u,0,0,mt)): 1;
    2435       41794 :     t = mt;
    2436             :   }
    2437       42263 :   return mk_pr(p,u,e,f,t);
    2438             : }
    2439             : 
    2440             : /* true nf */
    2441             : static GEN
    2442       15746 : primedec_end(GEN nf, GEN L, GEN p, long flim)
    2443             : {
    2444       15746 :   long i, j, l = lg(L), N = nf_get_degree(nf);
    2445       15746 :   GEN LV = get_LV(nf, L,p,N);
    2446       15746 :   int ramif = dvdii(nf_get_disc(nf), p);
    2447       15746 :   norm_S S; init_norm(&S, nf, p);
    2448       68769 :   for (i = j = 1; i < l; i++)
    2449             :   {
    2450       53317 :     GEN P = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N, flim);
    2451       53317 :     if (!P) continue;
    2452       52380 :     gel(L,j++) = P;
    2453       52380 :     if (flim == -1) return P;
    2454             :   }
    2455       15452 :   setlg(L, j); return L;
    2456             : }
    2457             : 
    2458             : /* prime ideal decomposition of p; if flim>0, restrict to f(P,p) <= flim
    2459             :  * if flim = -1 return only the first P
    2460             :  * if flim = -2 return only the f(P/p) in a t_VECSMALL */
    2461             : static GEN
    2462      598554 : primedec_aux(GEN nf, GEN p, long flim)
    2463             : {
    2464      598554 :   const long TYP = (flim == -2)? t_VECSMALL: t_VEC;
    2465      598554 :   GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
    2466             :   long i, k, c, iL, N;
    2467             :   int kummer;
    2468             : 
    2469      598554 :   F = FpX_factor(T, p);
    2470      598554 :   E = gel(F,2);
    2471      598554 :   F = gel(F,1);
    2472             : 
    2473      598554 :   k = lg(F); if (k == 1) errprime(p);
    2474      598554 :   if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
    2475             :   {
    2476      581506 :     L = cgetg(k, TYP);
    2477     1322893 :     for (i=1; i<k; i++)
    2478             :     {
    2479      940350 :       GEN t = gel(F,i);
    2480      940350 :       long f = degpol(t);
    2481      940350 :       if (flim > 0 && f > flim) { setlg(L, i); break; }
    2482      743711 :       if (flim == -2)
    2483           0 :         L[i] = f;
    2484             :       else
    2485      743711 :         gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
    2486      743711 :       if (flim == -1) return gel(L,1);
    2487             :     }
    2488      579182 :     return L;
    2489             :   }
    2490             : 
    2491       17048 :   kummer = 0;
    2492       17048 :   g = FpXV_prod(F, p);
    2493       17048 :   h = FpX_div(T,g,p);
    2494       17048 :   f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
    2495             : 
    2496       17048 :   N = degpol(T);
    2497       17048 :   L = cgetg(N+1,TYP);
    2498       17048 :   iL = 1;
    2499       52503 :   for (i=1; i<k; i++)
    2500       36757 :     if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
    2501       10425 :     {
    2502       11727 :       GEN t = gel(F,i);
    2503       11727 :       kummer = 1;
    2504       11727 :       gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
    2505       11727 :       if (flim == -1) return gel(L,1);
    2506             :     }
    2507             :     else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
    2508       25030 :       E[i] = 0;
    2509             : 
    2510             :   /* phi matrix of x -> x^p - x in algebra Z_K/p */
    2511       15746 :   Ip = pradical(nf,p,&phi);
    2512             : 
    2513             :   /* split etale algebra Z_K / (p,Ip) */
    2514       15746 :   h = cgetg(N+1,t_VEC);
    2515       15746 :   if (kummer)
    2516             :   { /* split off Kummer factors */
    2517        4581 :     GEN mb, b = NULL;
    2518       22831 :     for (i=1; i<k; i++)
    2519       18250 :       if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
    2520        4581 :     if (!b) errprime(p);
    2521        4581 :     b = FpC_red(poltobasis(nf,b), p);
    2522        4581 :     mb = FpM_red(zk_multable(nf,b), p);
    2523             :     /* Fp-base of ideal (Ip, b) in ZK/p */
    2524        4581 :     gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
    2525             :   }
    2526             :   else
    2527       11165 :     gel(h,1) = Ip;
    2528             : 
    2529       15746 :   UN = col_ei(N, 1);
    2530       44409 :   for (c=1; c; c--)
    2531             :   { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
    2532             :        H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
    2533       28663 :     GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
    2534       28663 :     long dim, r = lg(H)-1;
    2535             : 
    2536       28663 :     M   = FpM_suppl(shallowconcat(H,UN), p);
    2537       28663 :     Mi  = FpM_inv(M, p);
    2538       28663 :     M2  = vecslice(M, r+1,N); /* M = (H|M2) invertible */
    2539       28663 :     Mi2 = rowslice(Mi,r+1,N);
    2540             :     /* FIXME: FpM_mul(,M2) could be done with vecpermute */
    2541       28663 :     phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
    2542       28663 :     mat1 = FpM_ker(phi2, p);
    2543       28663 :     dim = lg(mat1)-1; /* A2 product of 'dim' fields */
    2544       28663 :     if (dim > 1)
    2545             :     { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
    2546       22936 :       GEN R, a, mula, mul2, v = gel(mat1,2);
    2547             :       long n;
    2548             : 
    2549       22936 :       a = FpM_FpC_mul(M2,v, p); /* not a scalar */
    2550       22936 :       mula = FpM_red(zk_multable(nf,a), p);
    2551       22936 :       mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
    2552       22936 :       R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
    2553       22936 :       n = lg(R)-1;
    2554       73284 :       for (i=1; i<=n; i++)
    2555             :       {
    2556       50348 :         GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
    2557       50348 :         gel(h,c++) = FpM_image(shallowconcat(H, I), p);
    2558             :       }
    2559       22936 :       if (n == dim)
    2560       16957 :         for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
    2561             :     }
    2562             :     else /* A2 field ==> H maximal, f = N-r = dim(A2) */
    2563        5727 :       gel(L,iL++) = H;
    2564             :   }
    2565       15746 :   setlg(L, iL);
    2566       15746 :   return primedec_end(nf, L, p, flim);
    2567             : }
    2568             : 
    2569             : GEN
    2570      594431 : idealprimedec_limit_f(GEN nf, GEN p, long f)
    2571             : {
    2572      594431 :   pari_sp av = avma;
    2573             :   GEN v;
    2574      594431 :   if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
    2575      594431 :   if (f < 0) pari_err_DOMAIN("idealprimedec", "f", "<", gen_0, stoi(f));
    2576      594431 :   v = primedec_aux(checknf(nf), p, f);
    2577      594431 :   v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
    2578      594431 :   return gerepileupto(av,v);
    2579             : }
    2580             : GEN
    2581        3920 : idealprimedec_galois(GEN nf, GEN p)
    2582             : {
    2583        3920 :   pari_sp av = avma;
    2584        3920 :   GEN v = primedec_aux(checknf(nf), p, -1);
    2585        3920 :   return gerepilecopy(av,v);
    2586             : }
    2587             : GEN
    2588         203 : idealprimedec_degrees(GEN nf, GEN p)
    2589             : {
    2590         203 :   pari_sp av = avma;
    2591         203 :   GEN v = primedec_aux(checknf(nf), p, -2);
    2592         203 :   vecsmall_sort(v); return gerepileuptoleaf(av, v);
    2593             : }
    2594             : GEN
    2595      194334 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
    2596      194334 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
    2597             : GEN
    2598      141581 : idealprimedec(GEN nf, GEN p)
    2599      141581 : { return idealprimedec_limit_f(nf, p, 0); }
    2600             : GEN
    2601        1057 : nf_pV_to_prV(GEN nf, GEN P)
    2602             : {
    2603             :   long i, l;
    2604        1057 :   GEN Q = cgetg_copy(P,&l);
    2605        1057 :   if (l == 1) return Q;
    2606        1015 :   for (i = 1; i < l; i++) gel(Q,i) = idealprimedec(nf, gel(P,i));
    2607        1015 :   return shallowconcat1(Q);
    2608             : }
    2609             : 
    2610             : /* return [Fp[x]: Fp] */
    2611             : static long
    2612         357 : ffdegree(GEN x, GEN frob, GEN p)
    2613             : {
    2614         357 :   pari_sp av = avma;
    2615         357 :   long d, f = lg(frob)-1;
    2616         357 :   GEN y = x;
    2617             : 
    2618        1491 :   for (d=1; d < f; d++)
    2619             :   {
    2620        1253 :     y = FpM_FpC_mul(frob, y, p);
    2621        1253 :     if (ZV_equal(y, x)) break;
    2622             :   }
    2623         357 :   avma = av; return d;
    2624             : }
    2625             : 
    2626             : static GEN
    2627        6944 : lift_to_zk(GEN v, GEN c, long N)
    2628             : {
    2629        6944 :   GEN w = zerocol(N);
    2630        6944 :   long i, l = lg(c);
    2631        6944 :   for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
    2632        6944 :   return w;
    2633             : }
    2634             : 
    2635             : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
    2636             : static GEN
    2637      352863 : anti_uniformizer(GEN nf, GEN pr)
    2638             : {
    2639      352863 :   long N = nf_get_degree(nf), e = pr_get_e(pr);
    2640             :   GEN p, b, z;
    2641             : 
    2642      352863 :   if (e * pr_get_f(pr) == N) return gen_1;
    2643       89558 :   p = pr_get_p(pr);
    2644       89558 :   b = pr_get_tau(pr); /* ZM */
    2645       89558 :   if (e != 1)
    2646             :   {
    2647        2170 :     GEN q = powiu(pr_get_p(pr), e-1);
    2648        2170 :     b = ZM_Z_divexact(ZM_powu(b,e), q);
    2649             :   }
    2650             :   /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
    2651       89558 :   z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
    2652       89558 :   z = idealaddtoone_raw(nf, pr, z);
    2653       89558 :   return Z_ZC_sub(gen_1, FpC_center(FpC_red(z,p), p, shifti(p,-1)));
    2654             : }
    2655             : 
    2656             : #define mpr_TAU 1
    2657             : #define mpr_FFP 2
    2658             : #define mpr_NFP 5
    2659             : #define SMALLMODPR 4
    2660             : #define LARGEMODPR 6
    2661             : static GEN
    2662      749637 : modpr_TAU(GEN modpr)
    2663             : {
    2664      749637 :   GEN tau = gel(modpr,mpr_TAU);
    2665      749637 :   return isintzero(tau)? NULL: tau;
    2666             : }
    2667             : 
    2668             : /* prh = HNF matrix, which is identity but for the first line. Return a
    2669             :  * projector to ZK / prh ~ Z/prh[1,1] */
    2670             : GEN
    2671      351539 : dim1proj(GEN prh)
    2672             : {
    2673      351539 :   long i, N = lg(prh)-1;
    2674      351539 :   GEN ffproj = cgetg(N+1, t_VEC);
    2675      351539 :   GEN x, q = gcoeff(prh,1,1);
    2676      351539 :   gel(ffproj,1) = gen_1;
    2677      514098 :   for (i=2; i<=N; i++)
    2678             :   {
    2679      162559 :     x = gcoeff(prh,1,i);
    2680      162559 :     if (signe(x)) x = subii(q,x);
    2681      162559 :     gel(ffproj,i) = x;
    2682             :   }
    2683      351539 :   return ffproj;
    2684             : }
    2685             : 
    2686             : /* p not necessarily prime, but coprime to denom(basis) */
    2687             : GEN
    2688         119 : QXQV_to_FpM(GEN basis, GEN T, GEN p)
    2689             : {
    2690         119 :   long i, l = lg(basis), f = degpol(T);
    2691         119 :   GEN z = cgetg(l, t_MAT);
    2692        3031 :   for (i = 1; i < l; i++)
    2693             :   {
    2694        2912 :     GEN w = gel(basis,i);
    2695        2912 :     if (typ(w) == t_INT)
    2696           0 :       w = scalarcol_shallow(w, f);
    2697             :     else
    2698             :     {
    2699             :       GEN dx;
    2700        2912 :       w = Q_remove_denom(w, &dx);
    2701        2912 :       w = FpXQ_red(w, T, p);
    2702        2912 :       if (dx)
    2703             :       {
    2704           0 :         dx = Fp_inv(dx, p);
    2705           0 :         if (!equali1(dx)) w = FpX_Fp_mul(w, dx, p);
    2706             :       }
    2707        2912 :       w = RgX_to_RgC(w, f);
    2708             :     }
    2709        2912 :     gel(z,i) = w; /* w_i mod (T,p) */
    2710             :   }
    2711         119 :   return z;
    2712             : }
    2713             : 
    2714             : /* initialize reduction mod pr; if zk = 1, will only init data required to
    2715             :  * reduce *integral* element.  Realize (O_K/pr) as Fp[X] / (T), for a
    2716             :  * *monic* T */
    2717             : static GEN
    2718      366048 : modprinit(GEN nf, GEN pr, int zk)
    2719             : {
    2720      366048 :   pari_sp av = avma;
    2721             :   GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
    2722             :   long N, i, k, f;
    2723             : 
    2724      366048 :   nf = checknf(nf); checkprid(pr);
    2725      366041 :   f = pr_get_f(pr);
    2726      366041 :   N = nf_get_degree(nf);
    2727      366041 :   prh = pr_hnf(nf, pr);
    2728      366041 :   tau = zk? gen_0: anti_uniformizer(nf, pr);
    2729      366041 :   p = pr_get_p(pr);
    2730             : 
    2731      366041 :   if (f == 1)
    2732             :   {
    2733      349822 :     res = cgetg(SMALLMODPR, t_COL);
    2734      349822 :     gel(res,mpr_TAU) = tau;
    2735      349822 :     gel(res,mpr_FFP) = dim1proj(prh);
    2736      349822 :     gel(res,3) = pr; return gerepilecopy(av, res);
    2737             :   }
    2738             : 
    2739       16219 :   c = cgetg(f+1, t_VECSMALL);
    2740       16219 :   ffproj = cgetg(N+1, t_MAT);
    2741      115052 :   for (k=i=1; i<=N; i++)
    2742             :   {
    2743       98833 :     x = gcoeff(prh, i,i);
    2744       98833 :     if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
    2745             :     else
    2746       50554 :       gel(ffproj,i) = ZC_neg(gel(prh,i));
    2747             :   }
    2748       16219 :   ffproj = rowpermute(ffproj, c);
    2749       16219 :   if (! dvdii(nf_get_index(nf), p))
    2750             :   {
    2751       13706 :     GEN basis = nf_get_zkprimpart(nf), D = nf_get_zkden(nf);
    2752       13706 :     if (N == f)
    2753             :     { /* pr inert */
    2754        5873 :       T = nf_get_pol(nf);
    2755        5873 :       T = FpX_red(T,p);
    2756        5873 :       ffproj = RgV_to_RgM(basis, lg(basis)-1);
    2757             :     }
    2758             :     else
    2759             :     {
    2760        7833 :       T = RgV_RgC_mul(basis, pr_get_gen(pr));
    2761        7833 :       T = FpX_normalize(T,p);
    2762        7833 :       basis = FqV_red(vecpermute(basis,c), T, p);
    2763        7833 :       basis = RgV_to_RgM(basis, lg(basis)-1);
    2764        7833 :       ffproj = ZM_mul(basis, ffproj);
    2765             :     }
    2766       13706 :     ffproj = FpM_red(ffproj, p);
    2767       13706 :     if (!equali1(D))
    2768             :     {
    2769        1827 :       D = modii(D,p);
    2770        1827 :       if (!equali1(D)) ffproj = FpM_Fp_mul(ffproj, Fp_inv(D,p), p);
    2771             :     }
    2772             : 
    2773       13706 :     res = cgetg(SMALLMODPR+1, t_COL);
    2774       13706 :     gel(res,mpr_TAU) = tau;
    2775       13706 :     gel(res,mpr_FFP) = ffproj;
    2776       13706 :     gel(res,3) = pr;
    2777       13706 :     gel(res,4) = T; return gerepilecopy(av, res);
    2778             :   }
    2779             : 
    2780        2513 :   if (uisprime(f))
    2781             :   {
    2782        2275 :     mul = ei_multable(nf, c[2]);
    2783        2275 :     mul = vecpermute(mul, c);
    2784             :   }
    2785             :   else
    2786             :   {
    2787             :     GEN v, u, u2, frob;
    2788             :     long deg,deg1,deg2;
    2789             : 
    2790             :     /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
    2791         238 :     frob = cgetg(f+1, t_MAT);
    2792        1484 :     for (i=1; i<=f; i++)
    2793             :     {
    2794        1246 :       x = pow_ei_mod_p(nf,c[i],p);
    2795        1246 :       gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
    2796             :     }
    2797         238 :     u = col_ei(f,2); k = 2;
    2798         238 :     deg1 = ffdegree(u, frob, p);
    2799         588 :     while (deg1 < f)
    2800             :     {
    2801         112 :       k++; u2 = col_ei(f, k);
    2802         112 :       deg2 = ffdegree(u2, frob, p);
    2803         112 :       deg = clcm(deg1,deg2);
    2804         112 :       if (deg == deg1) continue;
    2805         112 :       if (deg == deg2) { deg1 = deg2; u = u2; continue; }
    2806           7 :       u = ZC_add(u, u2);
    2807           7 :       while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
    2808           7 :       deg1 = deg;
    2809             :     }
    2810         238 :     v = lift_to_zk(u,c,N);
    2811             : 
    2812         238 :     mul = cgetg(f+1,t_MAT);
    2813         238 :     gel(mul,1) = v; /* assume w_1 = 1 */
    2814         238 :     for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
    2815             :   }
    2816             : 
    2817             :   /* Z_K/pr = Fp(v), mul = mul by v */
    2818        2513 :   mul = FpM_red(mul, p);
    2819        2513 :   mul = FpM_mul(ffproj, mul, p);
    2820             : 
    2821        2513 :   pow = get_powers(mul, p);
    2822        2513 :   T = RgV_to_RgX(FpM_deplin(pow, p), nf_get_varn(nf));
    2823        2513 :   nfproj = cgetg(f+1, t_MAT);
    2824        2513 :   for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
    2825             : 
    2826        2513 :   setlg(pow, f+1);
    2827        2513 :   ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
    2828             : 
    2829        2513 :   res = cgetg(LARGEMODPR, t_COL);
    2830        2513 :   gel(res,mpr_TAU) = tau;
    2831        2513 :   gel(res,mpr_FFP) = ffproj;
    2832        2513 :   gel(res,3) = pr;
    2833        2513 :   gel(res,4) = T;
    2834        2513 :   gel(res,mpr_NFP) = nfproj; return gerepilecopy(av, res);
    2835             : }
    2836             : 
    2837             : GEN
    2838          49 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0); }
    2839             : GEN
    2840        6598 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1); }
    2841             : 
    2842             : /* x may be a modpr */
    2843             : static int
    2844      550515 : ok_modpr(GEN x)
    2845      550515 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
    2846             : void
    2847         182 : checkmodpr(GEN x)
    2848             : {
    2849         182 :   if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
    2850         182 :   checkprid(modpr_get_pr(x));
    2851         182 : }
    2852             : GEN
    2853        3024 : get_modpr(GEN x)
    2854        3024 : { return ok_modpr(x)? x: NULL; }
    2855             : 
    2856             : int
    2857     3009982 : checkprid_i(GEN x)
    2858             : {
    2859     8496511 :   return (typ(x) == t_VEC && lg(x) == 6
    2860     2440168 :           && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
    2861     5450101 :           && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
    2862             : }
    2863             : void
    2864     2331787 : checkprid(GEN x)
    2865     2331787 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
    2866             : GEN
    2867      652638 : get_prid(GEN x)
    2868             : {
    2869      652638 :   long lx = lg(x);
    2870      652638 :   if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
    2871      652638 :   if (checkprid_i(x)) return x;
    2872      547309 :   if (ok_modpr(x)) {
    2873        2674 :     x = modpr_get_pr(x);
    2874        2674 :     if (checkprid_i(x)) return x;
    2875             :   }
    2876      544635 :   return NULL;
    2877             : }
    2878             : 
    2879             : static GEN
    2880      751751 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
    2881             : {
    2882      751751 :   GEN modpr = (typ(*pr) == t_COL)? *pr: modprinit(nf, *pr, zk);
    2883      751744 :   *T = modpr_get_T(modpr);
    2884      751744 :   *pr = modpr_get_pr(modpr);
    2885      751744 :   *p = pr_get_p(*pr); return modpr;
    2886             : }
    2887             : 
    2888             : /* Return an element of O_K which is set to x Mod T */
    2889             : GEN
    2890        4144 : modpr_genFq(GEN modpr)
    2891             : {
    2892        4144 :   switch(lg(modpr))
    2893             :   {
    2894             :     case SMALLMODPR: /* Fp */
    2895         903 :       return gen_1;
    2896             :     case LARGEMODPR:  /* painful case, p \mid index */
    2897        1386 :       return gmael(modpr,mpr_NFP, 2);
    2898             :     default: /* trivial case : p \nmid index */
    2899             :     {
    2900        1855 :       long v = varn( modpr_get_T(modpr) );
    2901        1855 :       return pol_x(v);
    2902             :     }
    2903             :   }
    2904             : }
    2905             : 
    2906             : GEN
    2907      745171 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2908      745171 :   GEN modpr = to_ff_init(nf,pr,T,p,0);
    2909      745164 :   GEN tau = modpr_TAU(modpr);
    2910      745164 :   if (!tau) gel(modpr,mpr_TAU) = anti_uniformizer(nf, *pr);
    2911      745164 :   return modpr;
    2912             : }
    2913             : GEN
    2914        6580 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2915        6580 :   return to_ff_init(nf,pr,T,p,1);
    2916             : }
    2917             : 
    2918             : /* assume x in 'basis' form (t_COL) */
    2919             : GEN
    2920     1162125 : zk_to_Fq(GEN x, GEN modpr)
    2921             : {
    2922     1162125 :   GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2923     1162125 :   GEN ffproj = gel(modpr,mpr_FFP);
    2924     1162125 :   GEN T = modpr_get_T(modpr);
    2925     1162125 :   return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
    2926             : }
    2927             : 
    2928             : /* REDUCTION Modulo a prime ideal */
    2929             : 
    2930             : /* nf a true nf */
    2931             : static GEN
    2932     5465898 : Rg_to_ff(GEN nf, GEN x0, GEN modpr)
    2933             : {
    2934     5465898 :   GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2935     5465898 :   long tx = typ(x);
    2936             : 
    2937     5465898 :   if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
    2938     5465898 :   switch(tx)
    2939             :   {
    2940     4278297 :     case t_INT: return modii(x, p);
    2941        5796 :     case t_FRAC: return Rg_to_Fp(x, p);
    2942             :     case t_POL:
    2943      161970 :       switch(lg(x))
    2944             :       {
    2945         217 :         case 2: return gen_0;
    2946       24038 :         case 3: return Rg_to_Fp(gel(x,2), p);
    2947             :       }
    2948      137715 :       x = Q_remove_denom(x, &den);
    2949      137715 :       x = poltobasis(nf, x);
    2950             :       /* content(x) and den may not be coprime */
    2951      137659 :       break;
    2952             :     case t_COL:
    2953     1019835 :       x = Q_remove_denom(x, &den);
    2954             :       /* content(x) and den are coprime */
    2955     1019835 :       if (lg(x)-1 == nf_get_degree(nf)) break;
    2956          56 :     default: pari_err_TYPE("Rg_to_ff",x);
    2957           0 :       return NULL;
    2958             :   }
    2959     1157438 :   if (den)
    2960             :   {
    2961      100034 :     long v = Z_pvalrem(den, p, &den);
    2962      100034 :     if (v)
    2963             :     {
    2964        4830 :       if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
    2965             :       /* now v = valuation(true denominator of x) */
    2966        4830 :       if (v > 0)
    2967             :       {
    2968        4473 :         GEN tau = modpr_TAU(modpr);
    2969        4473 :         if (!tau) pari_err_TYPE("zk_to_ff", x0);
    2970        4473 :         x = nfmuli(nf,x, nfpow_u(nf, tau, v));
    2971        4473 :         v -= ZV_pvalrem(x, p, &x);
    2972             :       }
    2973        4830 :       if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2974        4802 :       if (v) return gen_0;
    2975        4781 :       if (is_pm1(den)) den = NULL;
    2976             :     }
    2977       99985 :     x = FpC_red(x, p);
    2978             :   }
    2979     1157389 :   x = zk_to_Fq(x, modpr);
    2980     1157389 :   if (den)
    2981             :   {
    2982       97073 :     GEN c = Fp_inv(den, p);
    2983       97073 :     x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
    2984             :   }
    2985     1157389 :   return x;
    2986             : }
    2987             : 
    2988             : GEN
    2989         182 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
    2990             : {
    2991         182 :   pari_sp av = avma;
    2992         182 :   nf = checknf(nf); checkmodpr(modpr);
    2993         182 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(Rg_to_ff(nf,x,modpr),modpr)));
    2994             : }
    2995             : 
    2996             : GEN
    2997         238 : nfmodpr(GEN nf, GEN x, GEN pr)
    2998             : {
    2999         238 :   pari_sp av = avma;
    3000             :   GEN T, p, modpr;
    3001         238 :   nf = checknf(nf);
    3002         238 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    3003         238 :   x = Rg_to_ff(nf, x, modpr);
    3004         154 :   x = Fq_to_FF(x, Tp_to_FF(T,p));
    3005         154 :   return gerepilecopy(av, x);
    3006             : }
    3007             : GEN
    3008          56 : nfmodprlift(GEN nf, GEN x, GEN pr)
    3009             : {
    3010          56 :   pari_sp av = avma;
    3011             :   GEN y, T, p, modpr;
    3012             :   long i, l, d;
    3013          56 :   nf = checknf(nf);
    3014          56 :   switch(typ(x))
    3015             :   {
    3016           0 :     case t_INT: return icopy(x);
    3017          28 :     case t_FFELT: break;
    3018             :     case t_VEC: case t_COL: case t_MAT:
    3019          28 :       y = cgetg_copy(x,&l);
    3020          28 :       for (i = 1; i < l; i++) gel(y,i) = nfmodprlift(nf,gel(x,i),pr);
    3021          28 :       return y;
    3022           0 :     default: pari_err_TYPE("nfmodprlit",x);
    3023             :   }
    3024          28 :   x = FF_to_FpXQ_i(x);
    3025          28 :   d = degpol(x);
    3026          28 :   if (d <= 0) { avma = av; return d? gen_0: icopy(gel(x,2)); }
    3027           0 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    3028           0 :   return gerepilecopy(av, Fq_to_nf(x, modpr));
    3029             : }
    3030             : 
    3031             : /* lift A from residue field to nf */
    3032             : GEN
    3033     1328369 : Fq_to_nf(GEN A, GEN modpr)
    3034             : {
    3035             :   long dA;
    3036     1328369 :   if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
    3037        5082 :   dA = degpol(A);
    3038        5082 :   if (dA <= 0) return dA ? gen_0: gel(A,2);
    3039        1897 :   return ZM_ZX_mul(gel(modpr,mpr_NFP), A);
    3040             : }
    3041             : GEN
    3042           0 : FqV_to_nfV(GEN A, GEN modpr)
    3043             : {
    3044           0 :   long i,l = lg(A);
    3045           0 :   GEN B = cgetg(l,typ(A));
    3046           0 :   for (i=1; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    3047           0 :   return B;
    3048             : }
    3049             : GEN
    3050        8022 : FqM_to_nfM(GEN A, GEN modpr)
    3051             : {
    3052        8022 :   long i,j,h,l = lg(A);
    3053        8022 :   GEN B = cgetg(l, t_MAT);
    3054             : 
    3055        8022 :   if (l == 1) return B;
    3056        7448 :   h = lgcols(A);
    3057       33817 :   for (j=1; j<l; j++)
    3058             :   {
    3059       26369 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    3060       26369 :     for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
    3061             :   }
    3062        7448 :   return B;
    3063             : }
    3064             : GEN
    3065        7273 : FqX_to_nfX(GEN A, GEN modpr)
    3066             : {
    3067             :   long i, l;
    3068             :   GEN B;
    3069             : 
    3070        7273 :   if (typ(A)!=t_POL) return icopy(A); /* scalar */
    3071        7273 :   B = cgetg_copy(A, &l); B[1] = A[1];
    3072        7273 :   for (i=2; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    3073        7273 :   return B;
    3074             : }
    3075             : 
    3076             : /* reduce A to residue field */
    3077             : GEN
    3078     5465478 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
    3079             : {
    3080     5465478 :   pari_sp av = avma;
    3081     5465478 :   return gerepileupto(av, Rg_to_ff(checknf(nf), A, modpr));
    3082             : }
    3083             : /* A t_VEC/t_COL */
    3084             : GEN
    3085        3763 : nfV_to_FqV(GEN A, GEN nf,GEN modpr)
    3086             : {
    3087        3763 :   long i,l = lg(A);
    3088        3763 :   GEN B = cgetg(l,typ(A));
    3089        3763 :   for (i=1; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i), modpr);
    3090        3763 :   return B;
    3091             : }
    3092             : /* A  t_MAT */
    3093             : GEN
    3094        4207 : nfM_to_FqM(GEN A, GEN nf,GEN modpr)
    3095             : {
    3096        4207 :   long i,j,h,l = lg(A);
    3097        4207 :   GEN B = cgetg(l,t_MAT);
    3098             : 
    3099        4207 :   if (l == 1) return B;
    3100        4207 :   h = lgcols(A);
    3101      121457 :   for (j=1; j<l; j++)
    3102             :   {
    3103      117250 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    3104      117250 :     for (i=1; i<h; i++) gel(Bj,i) = nf_to_Fq(nf, gel(Aj,i), modpr);
    3105             :   }
    3106        4207 :   return B;
    3107             : }
    3108             : /* A t_POL */
    3109             : GEN
    3110        8288 : nfX_to_FqX(GEN A, GEN nf,GEN modpr)
    3111             : {
    3112        8288 :   long i,l = lg(A);
    3113        8288 :   GEN B = cgetg(l,t_POL); B[1] = A[1];
    3114        8288 :   for (i=2; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i),modpr);
    3115        8288 :   return normalizepol_lg(B, l);
    3116             : }
    3117             : 
    3118             : /*******************************************************************/
    3119             : /*                                                                 */
    3120             : /*                       RELATIVE ROUND 2                          */
    3121             : /*                                                                 */
    3122             : /*******************************************************************/
    3123             : /* Shallow functions */
    3124             : /* FIXME: use a bb_field and export the nfX_* routines */
    3125             : static GEN
    3126        2730 : nfX_sub(GEN nf, GEN x, GEN y)
    3127             : {
    3128        2730 :   long i, lx = lg(x), ly = lg(y);
    3129             :   GEN z;
    3130        2730 :   if (ly <= lx) {
    3131        2730 :     z = cgetg(lx,t_POL); z[1] = x[1];
    3132        2730 :     for (i=2; i < ly; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    3133        2730 :     for (   ; i < lx; i++) gel(z,i) = gel(x,i);
    3134        2730 :     z = normalizepol_lg(z, lx);
    3135             :   } else {
    3136           0 :     z = cgetg(ly,t_POL); z[1] = y[1];
    3137           0 :     for (i=2; i < lx; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    3138           0 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    3139           0 :     z = normalizepol_lg(z, ly);
    3140             :   }
    3141        2730 :   return z;
    3142             : }
    3143             : /* FIXME: quadratic multiplication */
    3144             : static GEN
    3145       50792 : nfX_mul(GEN nf, GEN a, GEN b)
    3146             : {
    3147       50792 :   long da = degpol(a), db = degpol(b), dc, lc, k;
    3148             :   GEN c;
    3149       50792 :   if (da < 0 || db < 0) return gen_0;
    3150       50792 :   dc = da + db;
    3151       50792 :   if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
    3152       50792 :   lc = dc+3;
    3153       50792 :   c = cgetg(lc, t_POL); c[1] = a[1];
    3154      399252 :   for (k = 0; k <= dc; k++)
    3155             :   {
    3156      348460 :     long i, I = minss(k, da);
    3157      348460 :     GEN d = NULL;
    3158     1160166 :     for (i = maxss(k-db, 0); i <= I; i++)
    3159             :     {
    3160      811706 :       GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
    3161      811706 :       d = d? nfadd(nf, d, e): e;
    3162             :     }
    3163      348460 :     gel(c, k+2) = d;
    3164             :   }
    3165       50792 :   return normalizepol_lg(c, lc);
    3166             : }
    3167             : /* assume b monic */
    3168             : static GEN
    3169       48062 : nfX_rem(GEN nf, GEN a, GEN b)
    3170             : {
    3171       48062 :   long da = degpol(a), db = degpol(b);
    3172       48062 :   if (da < 0) return gen_0;
    3173       48062 :   a = leafcopy(a);
    3174      164416 :   while (da >= db)
    3175             :   {
    3176       68292 :     long i, k = da;
    3177       68292 :     GEN A = gel(a, k+2);
    3178      481355 :     for (i = db-1, k--; i >= 0; i--, k--)
    3179      413063 :       gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
    3180       68292 :     a = normalizepol_lg(a, lg(a)-1);
    3181       68292 :     da = degpol(a);
    3182             :   }
    3183       48062 :   return a;
    3184             : }
    3185             : static GEN
    3186       48062 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
    3187             : {
    3188       48062 :   GEN c = nfX_mul(nf, a, b);
    3189       48062 :   if (typ(c) != t_POL) return c;
    3190       48062 :   return nfX_rem(nf, c, T);
    3191             : }
    3192             : 
    3193             : static void
    3194       10206 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
    3195             : {
    3196             :   long i;
    3197       10206 :   if (typ(Ix) == t_VEC) /* standard */
    3198        6195 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
    3199             :   else /* constant ideal */
    3200        4011 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
    3201       10206 : }
    3202             : 
    3203             : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
    3204             :  * module generated by x and y. */
    3205             : static GEN
    3206        5103 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
    3207             : {
    3208        5103 :   long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
    3209        5103 :   GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
    3210        5103 :   fill(lx, H     , Hx, I     , Ix);
    3211        5103 :   fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
    3212             : }
    3213             : static GEN
    3214        2674 : rnfjoinmodules(GEN nf, GEN x, GEN y)
    3215             : {
    3216        2674 :   if (!x) return y;
    3217        1211 :   if (!y) return x;
    3218        1092 :   return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
    3219             : }
    3220             : 
    3221             : typedef struct {
    3222             :   GEN multab, T,p;
    3223             :   long h;
    3224             : } rnfeltmod_muldata;
    3225             : 
    3226             : static GEN
    3227       56819 : _sqr(void *data, GEN x)
    3228             : {
    3229       56819 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3230       96719 :   GEN z = x? tablesqr(D->multab,x)
    3231       96719 :            : tablemul_ei_ej(D->multab,D->h,D->h);
    3232       56819 :   return FqV_red(z,D->T,D->p);
    3233             : }
    3234             : static GEN
    3235       10269 : _msqr(void *data, GEN x)
    3236             : {
    3237       10269 :   GEN x2 = _sqr(data, x), z;
    3238       10269 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3239       10269 :   z = tablemul_ei(D->multab, x2, D->h);
    3240       10269 :   return FqV_red(z,D->T,D->p);
    3241             : }
    3242             : 
    3243             : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
    3244             : static GEN
    3245       16919 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
    3246             : {
    3247       16919 :   pari_sp av = avma;
    3248             :   GEN y;
    3249             :   rnfeltmod_muldata D;
    3250             : 
    3251       16919 :   if (!signe(n)) return gen_1;
    3252             : 
    3253       16919 :   D.multab = multab;
    3254       16919 :   D.h = h;
    3255       16919 :   D.T = T;
    3256       16919 :   D.p = p;
    3257       16919 :   y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
    3258       16919 :   return gerepilecopy(av, y);
    3259             : }
    3260             : 
    3261             : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
    3262             :  * a root, cf repres() */
    3263             : static GEN
    3264          21 : FqX_non_root(GEN P, GEN T, GEN p)
    3265             : {
    3266          21 :   long dP = degpol(P), f, vT;
    3267             :   long i, j, k, pi, pp;
    3268             :   GEN v;
    3269             : 
    3270          21 :   if (dP == 0) return gen_1;
    3271          21 :   pp = is_bigint(p) ? dP+1: itos(p);
    3272          21 :   v = cgetg(dP + 2, t_VEC);
    3273          21 :   gel(v,1) = gen_0;
    3274          21 :   if (T)
    3275           0 :   { f = degpol(T); vT = varn(T); }
    3276             :   else
    3277          21 :   { f = 1; vT = 0; }
    3278          42 :   for (i=pi=1; i<=f; i++,pi*=pp)
    3279             :   {
    3280          21 :     GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
    3281          42 :     for (j=1; j<pp; j++)
    3282             :     {
    3283          42 :       for (k=1; k<=pi; k++)
    3284             :       {
    3285          21 :         GEN z = Fq_add(gel(v,k), jgi, T,p);
    3286          21 :         if (!gequal0(FqX_eval(P, z, T,p))) return z;
    3287          21 :         gel(v, j*pi+k) = z;
    3288             :       }
    3289          21 :       if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
    3290             :     }
    3291             :   }
    3292          21 :   return NULL;
    3293             : }
    3294             : 
    3295             : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
    3296             :  * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
    3297             :  * vdisc = v_pr( disc(P) ).
    3298             :  * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
    3299             :  *   O = enlarged order, given by a pseudo-basis
    3300             :  *   flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
    3301             :  *   v = v_pr(disc(O)). */
    3302             : static GEN
    3303        2758 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
    3304             : {
    3305             :   GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
    3306             :   long m, vt, r, d, i, j, mpr;
    3307             : 
    3308        2758 :   if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
    3309        2751 :   if (vdisc == 1) return NULL; /* pr-maximal */
    3310        2751 :   if (!only_maximal && !gequal1(leading_coeff(P)))
    3311           0 :     pari_err_IMPL( "the full Dedekind criterion in the non-monic case");
    3312             :   /* either monic OR only_maximal = 1 */
    3313        2751 :   m = degpol(P);
    3314        2751 :   nfT = nf_get_pol(nf);
    3315        2751 :   modpr = nf_to_Fq_init(nf,&pr, &T, &p);
    3316        2751 :   Ppr = nfX_to_FqX(P, nf, modpr);
    3317        2751 :   mpr = degpol(Ppr);
    3318        2751 :   if (mpr < m) /* non-monic => only_maximal = 1 */
    3319             :   {
    3320          21 :     if (mpr < 0) return NULL;
    3321          21 :     if (! RgX_valrem(Ppr, &Ppr))
    3322             :     { /* non-zero constant coefficient */
    3323           0 :       Ppr = RgX_shift_shallow(RgX_recip_shallow(Ppr), m - mpr);
    3324           0 :       P = RgX_recip_shallow(P);
    3325             :     }
    3326             :     else
    3327             :     {
    3328          21 :       GEN z = FqX_non_root(Ppr, T, p);
    3329          21 :       if (!z) pari_err_IMPL( "Dedekind in the difficult case");
    3330           0 :       z = Fq_to_nf(z, modpr);
    3331           0 :       if (typ(z) == t_INT)
    3332           0 :         P = RgX_translate(P, z);
    3333             :       else
    3334           0 :         P = RgXQX_translate(P, z, T);
    3335           0 :       P = RgX_recip_shallow(P);
    3336           0 :       Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
    3337             :     }
    3338             :   }
    3339        2730 :   A = gel(FqX_factor(Ppr,T,p),1);
    3340        2730 :   r = lg(A); /* > 1 */
    3341        2730 :   g = gel(A,1);
    3342        2730 :   for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
    3343        2730 :   h = FqX_div(Ppr,g, T, p);
    3344        2730 :   gzk = FqX_to_nfX(g, modpr);
    3345        2730 :   hzk = FqX_to_nfX(h, modpr);
    3346        2730 :   k = nfX_sub(nf, P, nfX_mul(nf, gzk,hzk));
    3347        2730 :   tau = pr_get_tau(pr);
    3348        2730 :   switch(typ(tau))
    3349             :   {
    3350        1155 :     case t_INT: k = gdiv(k, p); break;
    3351        1575 :     case t_MAT: k = RgX_Rg_div(tablemulvec(NULL,tau, k), p); break;
    3352             :   }
    3353        2730 :   k = nfX_to_FqX(k, nf, modpr);
    3354        2730 :   k = FqX_normalize(FqX_gcd(FqX_gcd(g,h,  T,p), k, T,p), T,p);
    3355        2730 :   d = degpol(k);  /* <= m */
    3356        2730 :   if (!d) return NULL; /* pr-maximal */
    3357        1827 :   if (only_maximal) return gen_0; /* not maximal */
    3358             : 
    3359        1806 :   A = cgetg(m+d+1,t_MAT);
    3360        1806 :   I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
    3361             :  /* base[2] temporarily multiplied by p, for the final nfhnfmod,
    3362             :   * which requires integral ideals */
    3363        1806 :   prinvp = pr_inv_p(pr); /* again multiplied by p */
    3364       10577 :   for (j=1; j<=m; j++)
    3365             :   {
    3366        8771 :     gel(A,j) = col_ei(m, j);
    3367        8771 :     gel(I,j) = p;
    3368             :   }
    3369        1806 :   pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
    3370        3808 :   for (   ; j<=m+d; j++)
    3371             :   {
    3372        2002 :     gel(A,j) = RgX_to_RgC(pal,m);
    3373        2002 :     gel(I,j) = prinvp;
    3374        2002 :     if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
    3375             :   }
    3376             :   /* the modulus is integral */
    3377        1806 :   base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
    3378        1806 :   gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
    3379        1806 :   vt = vdisc - 2*d;
    3380        1806 :   return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
    3381             : }
    3382             : 
    3383             : /* [L:K] = n */
    3384             : static GEN
    3385         805 : triv_order(long n)
    3386             : {
    3387         805 :   GEN z = cgetg(3, t_VEC);
    3388         805 :   gel(z,1) = matid(n);
    3389         805 :   gel(z,2) = const_vec(n, gen_1); return z;
    3390             : }
    3391             : 
    3392             : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
    3393             :  * is pr-maximal (resp. not pr-maximal). */
    3394             : GEN
    3395          84 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
    3396             : {
    3397          84 :   pari_sp av = avma;
    3398             :   GEN z, dP;
    3399             :   long v;
    3400             : 
    3401          84 :   nf = checknf(nf);
    3402          84 :   P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 0);
    3403          84 :   dP = RgX_disc(P); P = lift_shallow(P);
    3404          84 :   if (!pr)
    3405             :   {
    3406          21 :     GEN fa = idealfactor(nf, dP);
    3407          21 :     GEN Q = gel(fa,1), E = gel(fa,2);
    3408          21 :     pari_sp av2 = avma;
    3409          21 :     long i, l = lg(Q);
    3410          21 :     for (i = 1; i < l; i++, avma = av2)
    3411             :     {
    3412          21 :       v = itos(gel(E,i));
    3413          21 :       if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { avma=av; return gen_0; }
    3414           0 :       avma = av2;
    3415             :     }
    3416           0 :     avma = av; return gen_1;
    3417             :   }
    3418          63 :   else if (typ(pr) == t_VEC)
    3419             :   { /* flag = 1 is implicit */
    3420          63 :     if (lg(pr) == 1) { avma = av; return gen_1; }
    3421          63 :     if (typ(gel(pr,1)) == t_VEC)
    3422             :     { /* list of primes */
    3423          14 :       GEN Q = pr;
    3424          14 :       pari_sp av2 = avma;
    3425          14 :       long i, l = lg(Q);
    3426          14 :       for (i = 1; i < l; i++, avma = av2)
    3427             :       {
    3428          14 :         v = nfval(nf, dP, gel(Q,i));
    3429          14 :         if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { avma=av; return gen_0; }
    3430             :       }
    3431           0 :       avma = av; return gen_1;
    3432             :     }
    3433             :   }
    3434             :   /* single prime */
    3435          49 :   v = nfval(nf, dP, pr);
    3436          49 :   z = rnfdedekind_i(nf, P, pr, v, flag);
    3437          42 :   if (z)
    3438             :   {
    3439          21 :     if (flag) { avma = av; return gen_0; }
    3440          14 :     z = gerepilecopy(av, z);
    3441             :   }
    3442             :   else
    3443             :   {
    3444          21 :     avma = av; if (flag) return gen_1;
    3445           7 :     z = cgetg(4, t_VEC);
    3446           7 :     gel(z,1) = gen_1;
    3447           7 :     gel(z,2) = triv_order(degpol(P));
    3448           7 :     gel(z,3) = stoi(v);
    3449             :   }
    3450          21 :   return z;
    3451             : }
    3452             : 
    3453             : static int
    3454       21469 : ideal_is1(GEN x) {
    3455       21469 :   switch(typ(x))
    3456             :   {
    3457        9233 :     case t_INT: return is_pm1(x);
    3458       11536 :     case t_MAT: return RgM_isidentity(x);
    3459             :   }
    3460         700 :   return 0;
    3461             : }
    3462             : 
    3463             : /* return a in ideal A such that v_pr(a) = v_pr(A) */
    3464             : static GEN
    3465       12082 : minval(GEN nf, GEN A, GEN pr)
    3466             : {
    3467       12082 :   GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
    3468       12082 :   if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
    3469       12082 :   return a;
    3470             : }
    3471             : 
    3472             : /* nf a true nf. Return NULL if power order if pr-maximal */
    3473             : static GEN
    3474        2674 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
    3475             : {
    3476        2674 :   pari_sp av = avma, av1;
    3477             :   long i, j, k, n, nn, vpol, cnt, sep;
    3478             :   GEN q, q1, p, T, modpr, W, I, p1;
    3479             :   GEN prhinv, mpi, Id;
    3480             : 
    3481        2674 :   if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
    3482        2674 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3483        2674 :   av1 = avma;
    3484        2674 :   p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
    3485        2674 :   if (!p1) { avma = av; return NULL; }
    3486        1792 :   if (is_pm1(gel(p1,1))) return gerepilecopy(av,gel(p1,2));
    3487         847 :   sep = itos(gel(p1,3));
    3488         847 :   W = gmael(p1,2,1);
    3489         847 :   I = gmael(p1,2,2);
    3490         847 :   gerepileall(av1, 2, &W, &I);
    3491             : 
    3492         847 :   mpi = zk_multable(nf, pr_get_gen(pr));
    3493         847 :   n = degpol(pol); nn = n*n;
    3494         847 :   vpol = varn(pol);
    3495         847 :   q1 = q = pr_norm(pr);
    3496         847 :   while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
    3497         847 :   Id = matid(n);
    3498         847 :   prhinv = pr_inv(pr);
    3499         847 :   av1 = avma;
    3500        4207 :   for(cnt=1;; cnt++)
    3501             :   {
    3502        4207 :     GEN I0 = leafcopy(I), W0 = leafcopy(W);
    3503             :     GEN Wa, Winv, Ip, A, MW, MWmod, F, pseudo, C, G;
    3504        4207 :     GEN Tauinv = cgetg(n+1, t_VEC), Tau = cgetg(n+1, t_VEC);
    3505             : 
    3506        4207 :     if (DEBUGLEVEL>1) err_printf("    pass no %ld\n",cnt);
    3507       25333 :     for (j=1; j<=n; j++)
    3508             :     {
    3509             :       GEN tau, tauinv;
    3510       21126 :       if (ideal_is1(gel(I,j)))
    3511             :       {
    3512        9044 :         gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
    3513        9044 :         continue;
    3514             :       }
    3515       12082 :       gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
    3516       12082 :       gel(Tauinv,j) = tauinv = nfinv(nf, tau);
    3517       12082 :       gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
    3518       12082 :       gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
    3519             :     }
    3520             :     /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
    3521             : 
    3522             :    /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
    3523        4207 :     Wa = RgM_to_RgXV(W,vpol);
    3524        4207 :     Winv = nfM_inv(nf, W);
    3525        4207 :     MW = cgetg(nn+1, t_MAT);
    3526             :     /* W_1 = 1 */
    3527        4207 :     for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
    3528       21126 :     for (i=2; i<=n; i++)
    3529       64981 :       for (j=i; j<=n; j++)
    3530             :       {
    3531       48062 :         GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
    3532       48062 :         if (typ(z) != t_POL)
    3533           0 :           z = nfC_nf_mul(nf, gel(Winv,1), z);
    3534             :         else
    3535             :         {
    3536       48062 :           z = RgX_to_RgC(z, lg(Winv)-1);
    3537       48062 :           z = nfM_nfC_mul(nf, Winv, z);
    3538             :         }
    3539       48062 :         gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
    3540             :       }
    3541             : 
    3542             :     /* compute Ip =  pr-radical [ could use Ker(trace) if q large ] */
    3543        4207 :     MWmod = nfM_to_FqM(MW,nf,modpr);
    3544        4207 :     F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
    3545        4207 :     for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
    3546        4207 :     Ip = FqM_ker(F,T,p);
    3547        4207 :     if (lg(Ip) == 1) { W = W0; I = I0; break; }
    3548             : 
    3549             :     /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
    3550        4011 :     A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
    3551        4011 :     for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
    3552        4011 :     MW = nfM_mul(nf, nfM_inv(nf,A), MW);
    3553        4011 :     C = cgetg(n+1, t_MAT);
    3554       24304 :     for (k=1; k<=n; k++)
    3555             :     {
    3556       20293 :       GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck;
    3557       20293 :       gel(C,k) = Ck = cgetg(nn+1, t_COL);
    3558      133154 :       for (j=1; j<=n; j++)
    3559             :       {
    3560      112861 :         GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
    3561      112861 :         for (i=1; i<=n; i++) gel(Ck, (j-1)*n+i) = nf_to_Fq(nf,gel(z,i),modpr);
    3562             :       }
    3563             :     }
    3564        4011 :     G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
    3565             : 
    3566        4011 :     pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
    3567             :     /* express W in terms of the power basis */
    3568        4011 :     W = nfM_mul(nf, W, gel(pseudo,1));
    3569        4011 :     I = gel(pseudo,2);
    3570             :     /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
    3571             :      * W[i,i] = Tau[i] */
    3572       24304 :     for (j=1; j<=n; j++)
    3573       20293 :       if (gel(Tau,j) != gen_1)
    3574             :       {
    3575       11522 :         gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
    3576       11522 :         gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
    3577             :       }
    3578        4011 :     if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
    3579        4011 :     if (sep <= 3 || gequal(I,I0)) break;
    3580             : 
    3581        3360 :     if (gc_needed(av1,2))
    3582             :     {
    3583           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
    3584           0 :       gerepileall(av1,2, &W,&I);
    3585             :     }
    3586        3360 :   }
    3587         847 :   return gerepilecopy(av, mkvec2(W, I));
    3588             : }
    3589             : 
    3590             : GEN
    3591      212484 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
    3592             : {
    3593      212484 :   switch(typ(c))
    3594             :   {
    3595      106847 :     case t_INT: case t_FRAC: return c;
    3596             :     case t_POL:
    3597        2835 :       if (lg(c) >= lg(T)) c = RgX_rem(c,T);
    3598        2835 :       break;
    3599             :     case t_POLMOD:
    3600      102795 :       if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
    3601      102473 :       c = gel(c,2);
    3602      102473 :       switch(typ(c))
    3603             :       {
    3604       89180 :         case t_POL: break;
    3605       13293 :         case t_INT: case t_FRAC: return c;
    3606           0 :         default: pari_err_TYPE(f, c);
    3607             :       }
    3608       89180 :       break;
    3609           7 :     default: pari_err_TYPE(f,c);
    3610             :   }
    3611             :   /* typ(c) = t_POL */
    3612       92015 :   if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
    3613       92008 :   switch(lg(c))
    3614             :   {
    3615        5411 :     case 2: return gen_0;
    3616             :     case 3:
    3617        2387 :       c = gel(c,2); if (is_rational_t(typ(c))) return c;
    3618           0 :       pari_err_TYPE(f,c);
    3619             :   }
    3620       84210 :   RgX_check_QX(c, f);
    3621       84196 :   return lift? c: mkpolmod(c, T);
    3622             : }
    3623             : /* check whether P is a polynomials with coeffs in number field Q[y]/(T) */
    3624             : GEN
    3625       56813 : RgX_nffix(const char *f, GEN T, GEN P, int lift)
    3626             : {
    3627       56813 :   long i, l, vT = varn(T);
    3628       56813 :   GEN Q = cgetg_copy(P, &l);
    3629       56813 :   if (typ(P) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), P);
    3630       56813 :   if (varncmp(varn(P), vT) >= 0) pari_err_PRIORITY(f, P, ">=", vT);
    3631       56792 :   Q[1] = P[1];
    3632       56792 :   for (i=2; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3633       56785 :   return normalizepol_lg(Q, l);
    3634             : }
    3635             : GEN
    3636          28 : RgV_nffix(const char *f, GEN T, GEN P, int lift)
    3637             : {
    3638             :   long i, l;
    3639          28 :   GEN Q = cgetg_copy(P, &l);
    3640          28 :   for (i=1; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3641          21 :   return Q;
    3642             : }
    3643             : 
    3644             : #if 0
    3645             : /* determinant of the trace pairing. FIXME: unused; for rnfmaxord ? */
    3646             : static GEN
    3647             : get_d(GEN nf, GEN pol, GEN A)
    3648             : {
    3649             :   long i, j, n = degpol(pol);
    3650             :   GEN W = RgM_to_RgXV(lift_shallow(matbasistoalg(nf,A)), varn(pol));
    3651             :   GEN T, nfT = nf_get_pol(nf), sym = polsym_gen(pol, NULL, n-1, nfT, NULL);
    3652             :   T = cgetg(n+1,t_MAT);
    3653             :   for (j=1; j<=n; j++) gel(T,j) = cgetg(n+1,t_COL);
    3654             :   for (j=1; j<=n; j++)
    3655             :     for (i=j; i<=n; i++)
    3656             :     {
    3657             :       GEN c = RgXQX_mul(gel(W,i),gel(W,j), nfT);
    3658             :       c = RgXQX_rem(c, pol, nfT);
    3659             :       c = simplify_shallow(quicktrace(c,sym));
    3660             :       gcoeff(T,j,i) = gcoeff(T,i,j) = c;
    3661             :     }
    3662             :   return nf_to_scalar_or_basis(nf, det(T));
    3663             : }
    3664             : #endif
    3665             : 
    3666             : /* nf = base field K
    3667             :  * pol= monic polynomial, coefficients in Z_K, defining a relative
    3668             :  *   extension L = K[X]/(pol). One MUST have varn(pol) << nf_get_varn(nf).
    3669             :  * Returns a pseudo-basis [A,I] of Z_L, set (D,d) to the relative
    3670             :  * discriminant, and f to the index-ideal */
    3671             : GEN
    3672        1505 : rnfallbase(GEN nf, GEN *ppol, GEN *pD, GEN *pd, GEN *pf)
    3673             : {
    3674             :   long i, n, l;
    3675        1505 :   GEN nfT, fa, E, P, z, D, disc, pol = *ppol;
    3676             : 
    3677        1505 :   nf = checknf(nf); nfT = nf_get_pol(nf);
    3678        1505 :   pol = RgX_nffix("rnfallbase", nfT,pol,0);
    3679        1505 :   if (!gequal1(leading_coeff(pol)))
    3680           0 :     pari_err_IMPL("non-monic relative polynomials");
    3681             : 
    3682        1505 :   n = degpol(pol);
    3683        1505 :   disc = nf_to_scalar_or_basis(nf, RgX_disc(pol));
    3684        1505 :   pol = lift_shallow(pol);
    3685        1505 :   fa = idealfactor(nf, disc);
    3686        1498 :   P = gel(fa,1); l = lg(P);
    3687        1498 :   E = gel(fa,2);
    3688        1498 :   z = NULL;
    3689        4760 :   for (i=1; i < l; i++)
    3690             :   {
    3691        3262 :     long e = itos(gel(E,i));
    3692        3262 :     if (e > 1) z = rnfjoinmodules(nf, z, rnfmaxord(nf, pol, gel(P,i), e));
    3693             :   }
    3694        1498 :   if (z) D = idealprod(nf, gel(z,2)); else { z = triv_order(n); D = gen_1; }
    3695        1498 :   if (isint1(D))
    3696             :   {
    3697         798 :     if (pf) *pf = gen_1;
    3698         798 :     D = disc;
    3699             :   }
    3700             :   else
    3701             :   {
    3702         700 :     if (pf) *pf = idealinv(nf, D);
    3703         700 :     D = idealmul(nf, disc, idealsqr(nf,D));
    3704             :   }
    3705        1498 :   if (pd)
    3706             :   {
    3707        1099 :     GEN f = core2partial(Q_content(disc), 0);
    3708        1099 :     *pd = gdiv(disc, sqri(gel(f,2)));
    3709             :   }
    3710        1498 :   *pD = D;
    3711        1498 :   *ppol = pol; return z;
    3712             : }
    3713             : 
    3714             : GEN
    3715          49 : rnfpseudobasis(GEN nf, GEN pol)
    3716             : {
    3717          49 :   pari_sp av = avma;
    3718          49 :   GEN D, d, z = rnfallbase(nf,&pol, &D, &d, NULL);
    3719          49 :   return gerepilecopy(av, mkvec4(gel(z,1), gel(z,2), D, d));
    3720             : }
    3721             : 
    3722             : GEN
    3723           7 : rnfdiscf(GEN nf, GEN pol)
    3724             : {
    3725           7 :   pari_sp av = avma;
    3726           7 :   GEN D, d; (void)rnfallbase(nf,&pol, &D, &d, NULL);
    3727           7 :   return gerepilecopy(av, mkvec2(D,d));
    3728             : }
    3729             : 
    3730             : GEN
    3731          35 : gen_if_principal(GEN bnf, GEN x)
    3732             : {
    3733          35 :   pari_sp av = avma;
    3734          35 :   GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
    3735          35 :   if (isintzero(z)) { avma = av; return NULL; }
    3736          28 :   return z;
    3737             : }
    3738             : 
    3739             : static int
    3740          63 : is_pseudo_matrix(GEN O)
    3741             : {
    3742         189 :   return (typ(O) ==t_VEC && lg(O) >= 3
    3743          63 :           && typ(gel(O,1)) == t_MAT
    3744          63 :           && typ(gel(O,2)) == t_VEC
    3745         126 :           && lgcols(O) == lg(gel(O,2)));
    3746             : }
    3747             : 
    3748             : /* given bnf and a pseudo-basis of an order in HNF [A,I], tries to simplify
    3749             :  * the HNF as much as possible. The resulting matrix will be upper triangular
    3750             :  * but the diagonal coefficients will not be equal to 1. The ideals are
    3751             :  * guaranteed to be integral and primitive. */
    3752             : GEN
    3753           0 : rnfsimplifybasis(GEN bnf, GEN x)
    3754             : {
    3755           0 :   pari_sp av = avma;
    3756             :   long i, l;
    3757             :   GEN y, Az, Iz, nf, A, I;
    3758             : 
    3759           0 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3760           0 :   if (!is_pseudo_matrix(x)) pari_err_TYPE("rnfsimplifybasis",x);
    3761           0 :   A = gel(x,1);
    3762           0 :   I = gel(x,2); l = lg(I);
    3763           0 :   y = cgetg(3, t_VEC);
    3764           0 :   Az = cgetg(l, t_MAT); gel(y,1) = Az;
    3765           0 :   Iz = cgetg(l, t_VEC); gel(y,2) = Iz;
    3766           0 :   for (i = 1; i < l; i++)
    3767             :   {
    3768             :     GEN c, d;
    3769           0 :     if (ideal_is1(gel(I,i))) {
    3770           0 :       gel(Iz,i) = gen_1;
    3771           0 :       gel(Az,i) = gel(A,i);
    3772           0 :       continue;
    3773             :     }
    3774             : 
    3775           0 :     gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
    3776           0 :     gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
    3777           0 :     if (c && ideal_is1(gel(Iz,i))) continue;
    3778             : 
    3779           0 :     d = gen_if_principal(bnf, gel(Iz,i));
    3780           0 :     if (d)
    3781             :     {
    3782           0 :       gel(Iz,i) = gen_1;
    3783           0 :       gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
    3784             :     }
    3785             :   }
    3786           0 :   return gerepilecopy(av, y);
    3787             : }
    3788             : 
    3789             : static GEN
    3790          70 : get_order(GEN nf, GEN O, const char *s)
    3791             : {
    3792          70 :   if (typ(O) == t_POL)
    3793           7 :     return rnfpseudobasis(nf, O);
    3794          63 :   if (!is_pseudo_matrix(O)) pari_err_TYPE(s, O);
    3795          63 :   return O;
    3796             : }
    3797             : 
    3798             : GEN
    3799          21 : rnfdet(GEN nf, GEN order)
    3800             : {
    3801          21 :   pari_sp av = avma;
    3802             :   GEN A, I, D;
    3803          21 :   nf = checknf(nf);
    3804          14 :   order = get_order(nf, order, "rnfdet");
    3805          14 :   A = gel(order,1);
    3806          14 :   I = gel(order,2);
    3807          14 :   D = idealmul(nf, nfM_det(nf,A), idealprod(nf,I));
    3808          14 :   return gerepileupto(av, D);
    3809             : }
    3810             : 
    3811             : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
    3812             :    t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
    3813             : static void
    3814          63 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
    3815             : {
    3816             :   GEN x, uv, y, da, db;
    3817             : 
    3818          63 :   a = idealinv(nf,a);
    3819          63 :   a = Q_remove_denom(a, &da);
    3820          63 :   b = Q_remove_denom(b, &db);
    3821          63 :   x = idealcoprime(nf,a,b);
    3822          63 :   uv = idealaddtoone(nf, idealmul(nf,x,a), b);
    3823          63 :   y = gel(uv,2);
    3824          63 :   if (da) x = gmul(x,da);
    3825          63 :   if (db) y = gdiv(y,db);
    3826          63 :   *px = x;
    3827          63 :   *py = y;
    3828          63 :   *pz = db ? negi(db): gen_m1;
    3829          63 :   *pt = nfdiv(nf, gel(uv,1), x);
    3830          63 : }
    3831             : 
    3832             : /* given a pseudo-basis of an order in HNF [A,I] (or [A,I,D,d]), gives an
    3833             :  * n x n matrix (not in HNF) of a pseudo-basis and an ideal vector
    3834             :  * [1,1,...,1,I] such that order = Z_K^(n-1) x I.
    3835             :  * Uses the approximation theorem ==> slow. */
    3836             : GEN
    3837          28 : rnfsteinitz(GEN nf, GEN order)
    3838             : {
    3839          28 :   pari_sp av = avma;
    3840             :   long i, n, l;
    3841             :   GEN A, I, p1;
    3842             : 
    3843          28 :   nf = checknf(nf);
    3844          28 :   order = get_order(nf, order, "rnfsteinitz");
    3845          28 :   A = RgM_to_nfM(nf, gel(order,1));
    3846          28 :   I = leafcopy(gel(order,2)); n=lg(A)-1;
    3847         189 :   for (i=1; i<n; i++)
    3848             :   {
    3849         161 :     GEN c1, c2, b, a = gel(I,i);
    3850         161 :     gel(I,i) = gen_1;
    3851         161 :     if (ideal_is1(a)) continue;
    3852             : 
    3853          63 :     c1 = gel(A,i);
    3854          63 :     c2 = gel(A,i+1);
    3855          63 :     b = gel(I,i+1);
    3856          63 :     if (ideal_is1(b))
    3857             :     {
    3858           0 :       gel(A,i) = c2;
    3859           0 :       gel(A,i+1) = gneg(c1);
    3860           0 :       gel(I,i+1) = a;
    3861             :     }
    3862             :     else
    3863             :     {
    3864          63 :       pari_sp av2 = avma;
    3865             :       GEN x, y, z, t;
    3866          63 :       nfidealdet1(nf,a,b, &x,&y,&z,&t);
    3867          63 :       x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
    3868          63 :       y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
    3869          63 :       gerepileall(av2, 2, &x,&y);
    3870          63 :       gel(A,i) = x;
    3871          63 :       gel(A,i+1) = y;
    3872          63 :       gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &p1);
    3873          63 :       if (p1) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), p1);
    3874             :     }
    3875             :   }
    3876          28 :   l = lg(order);
    3877          28 :   p1 = cgetg(l,t_VEC);
    3878          28 :   gel(p1,1) = A;
    3879          28 :   gel(p1,2) = I; for (i=3; i<l; i++) gel(p1,i) = gel(order,i);
    3880          28 :   return gerepilecopy(av, p1);
    3881             : }
    3882             : 
    3883             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3884             :  * and outputs a basis if it is free, an n+1-generating set if it is not */
    3885             : GEN
    3886          21 : rnfbasis(GEN bnf, GEN order)
    3887             : {
    3888          21 :   pari_sp av = avma;
    3889             :   long j, n;
    3890             :   GEN nf, A, I, cl, col, a;
    3891             : 
    3892          21 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3893          21 :   order = get_order(nf, order, "rnfbasis");
    3894          21 :   I = gel(order,2); n = lg(I)-1;
    3895          21 :   j=1; while (j<n && ideal_is1(gel(I,j))) j++;
    3896          21 :   if (j<n)
    3897             :   {
    3898           7 :     order = rnfsteinitz(nf,order);
    3899           7 :     I = gel(order,2);
    3900             :   }
    3901          21 :   A = gel(order,1);
    3902          21 :   col= gel(A,n); A = vecslice(A, 1, n-1);
    3903          21 :   cl = gel(I,n);
    3904          21 :   a = gen_if_principal(bnf, cl);
    3905          21 :   if (!a)
    3906             :   {
    3907           7 :     GEN v = idealtwoelt(nf, cl);
    3908           7 :     A = shallowconcat(A, gmul(gel(v,1), col));
    3909           7 :     a = gel(v,2);
    3910             :   }
    3911          21 :   A = shallowconcat(A, nfC_nf_mul(nf, col, a));
    3912          21 :   return gerepilecopy(av, A);
    3913             : }
    3914             : 
    3915             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3916             :  * and outputs a basis (not pseudo) in Hermite Normal Form if it exists, zero
    3917             :  * if not
    3918             :  */
    3919             : GEN
    3920           7 : rnfhnfbasis(GEN bnf, GEN order)
    3921             : {
    3922           7 :   pari_sp av = avma;
    3923             :   long j, n;
    3924             :   GEN nf, A, I, a;
    3925             : 
    3926           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3927           7 :   order = get_order(nf, order, "rnfbasis");
    3928           7 :   A = gel(order,1); A = RgM_shallowcopy(A);
    3929           7 :   I = gel(order,2); n = lg(A)-1;
    3930          42 :   for (j=1; j<=n; j++)
    3931             :   {
    3932          35 :     if (ideal_is1(gel(I,j))) continue;
    3933          14 :     a = gen_if_principal(bnf, gel(I,j));
    3934          14 :     if (!a) { avma = av; return gen_0; }
    3935          14 :     gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
    3936             :   }
    3937           7 :   return gerepilecopy(av,A);
    3938             : }
    3939             : 
    3940             : static long
    3941           7 : rnfisfree_aux(GEN bnf, GEN order)
    3942             : {
    3943             :   long n, j;
    3944             :   GEN nf, P, I;
    3945             : 
    3946           7 :   bnf = checkbnf(bnf);
    3947           7 :   if (is_pm1( bnf_get_no(bnf) )) return 1;
    3948           0 :   nf = bnf_get_nf(bnf);
    3949           0 :   order = get_order(nf, order, "rnfisfree");
    3950           0 :   I = gel(order,2); n = lg(I)-1;
    3951           0 :   j=1; while (j<=n && ideal_is1(gel(I,j))) j++;
    3952           0 :   if (j>n) return 1;
    3953             : 
    3954           0 :   P = gel(I,j);
    3955           0 :   for (j++; j<=n; j++)
    3956           0 :     if (!ideal_is1(gel(I,j))) P = idealmul(nf,P,gel(I,j));
    3957           0 :   return gequal0( isprincipal(bnf,P) );
    3958             : }
    3959             : 
    3960             : long
    3961           7 : rnfisfree(GEN bnf, GEN order)
    3962             : {
    3963           7 :   pari_sp av = avma;
    3964           7 :   long n = rnfisfree_aux(bnf, order);
    3965           7 :   avma = av; return n;
    3966             : }
    3967             : 
    3968             : /**********************************************************************/
    3969             : /**                                                                  **/
    3970             : /**                   COMPOSITUM OF TWO NUMBER FIELDS                **/
    3971             : /**                                                                  **/
    3972             : /**********************************************************************/
    3973             : static GEN
    3974        1169 : compositum_fix(GEN nf, GEN A)
    3975             : {
    3976             :   int ok;
    3977        1169 :   if (nf)
    3978             :   {
    3979         441 :     long i, l = lg(A);
    3980         441 :     A = shallowcopy(A);
    3981         441 :     for (i=2; i<l; i++) gel(A,i) = basistoalg(nf, gel(A,i));
    3982         441 :     ok = nfissquarefree(nf,A);
    3983             :   }
    3984             :   else
    3985             :   {
    3986         728 :     A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
    3987         728 :     ok = ZX_is_squarefree(A);
    3988             :   }
    3989        1169 :   if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
    3990        1162 :   return A;
    3991             : }
    3992             : INLINE long
    3993          14 : nextk(long k) { return k>0 ? -k : 1-k; }
    3994             : 
    3995             : /* modular version */
    3996             : GEN
    3997         630 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
    3998             : {
    3999         630 :   pari_sp av = avma;
    4000             :   int same;
    4001             :   long v, k;
    4002             :   GEN C, D, LPRS;
    4003             : 
    4004         630 :   if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
    4005         630 :   if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
    4006         630 :   if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
    4007         630 :   v = varn(A);
    4008         630 :   if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
    4009         630 :   if (nf)
    4010             :   {
    4011         245 :     nf = checknf(nf);
    4012         245 :     if (v == nf_get_varn(nf)) pari_err_PRIORITY("polcompositum", nf, "==",  v);
    4013             :   }
    4014         609 :   same = (A == B || RgX_equal(A,B));
    4015         609 :   A = compositum_fix(nf,A);
    4016         602 :   if (!same) B = compositum_fix(nf,B);
    4017             : 
    4018         602 :   D = LPRS = NULL; /* -Wall */
    4019         602 :   k = same? -1: 1;
    4020         602 :   if (nf)
    4021             :   {
    4022         224 :     long v0 = fetch_var();
    4023             :     GEN q;
    4024          14 :     for(;; k = nextk(k))
    4025             :     {
    4026         238 :       GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
    4027         238 :       GEN B1 = poleval(B,chgvar);
    4028         238 :       C = RgX_resultant_all(A,B1,&q);
    4029         238 :       C = gsubst(C,v0,pol_x(v));
    4030         238 :       if (nfissquarefree(nf,C)) break;
    4031          14 :     }
    4032         224 :     C = lift_if_rational(C);
    4033         224 :     if (flag&1)
    4034             :     {
    4035             :       GEN H0, H1;
    4036         182 :       H0 = gsubst(gel(q,2),v0,pol_x(v));
    4037         182 :       H1 = gsubst(gel(q,3),v0,pol_x(v));
    4038         182 :       if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
    4039         182 :       if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
    4040         182 :       H0 = lift_if_rational(H0);
    4041         182 :       H1 = lift_if_rational(H1);
    4042         182 :       LPRS = mkvec2(H0,H1);
    4043             :     }
    4044             :   }
    4045             :   else
    4046             :   {
    4047         378 :     B = leafcopy(B); setvarn(B,fetch_var_higher());
    4048         378 :     C = ZX_ZXY_resultant_all(A, B, &k, (flag&1)? &LPRS: NULL);
    4049         378 :     setvarn(C, v);
    4050             :   }
    4051             :   /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
    4052         602 :   if (same)
    4053             :   {
    4054          42 :     D = RgX_rescale(A, stoi(1 - k));
    4055          42 :     C = RgX_div(C, D);
    4056          42 :     if (degpol(C) <= 0)
    4057           0 :       C = mkvec(D);
    4058             :     else
    4059          42 :       C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
    4060             :   }
    4061         560 :   else if (flag & 2)
    4062         280 :     C = mkvec(C);
    4063             :   else
    4064         280 :     C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
    4065         595 :   gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
    4066         595 :   if (flag&1)
    4067             :   { /* a,b,c root of A,B,C = compositum, c = b - k a */
    4068         420 :     long i, l = lg(C);
    4069         420 :     GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
    4070         420 :     setvarn(mH0,v);
    4071         420 :     setvarn(H1,v);
    4072         861 :     for (i=1; i<l; i++)
    4073             :     {
    4074         441 :       GEN D = gel(C,i);
    4075         441 :       a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
    4076         441 :       b = gadd(pol_x(v), gmulsg(k,a));
    4077         441 :       gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
    4078             :     }
    4079             :   }
    4080         595 :   (void)delete_var();
    4081         595 :   settyp(C, t_VEC);
    4082         595 :   if (flag&2) C = gel(C,1);
    4083         595 :   return gerepilecopy(av, C);
    4084             : }
    4085             : GEN
    4086         385 : polcompositum0(GEN A, GEN B, long flag)
    4087         385 : { return nfcompositum(NULL,A,B,flag); }
    4088             : 
    4089             : GEN
    4090          35 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
    4091             : GEN
    4092         231 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }
    4093             : 
    4094             : /* Assume A,B irreducible (in particular squarefree) and define linearly
    4095             :  * disjoint extensions: no factorisation needed */
    4096             : GEN
    4097         385 : ZX_compositum_disjoint(GEN A, GEN B)
    4098             : {
    4099         385 :   long k = 1;
    4100         385 :   return ZX_ZXY_resultant_all(A, B, &k, NULL);
    4101             : }
    4102             : 
    4103             : GEN
    4104         119 : nfsplitting(GEN T, GEN D)
    4105             : {
    4106         119 :   pari_sp av = avma;
    4107             :   long d, v;
    4108             :   GEN F, K;
    4109         119 :   T = get_nfpol(T,&K);
    4110         112 :   if (!K)
    4111             :   {
    4112         105 :     if (typ(T) != t_POL) pari_err_TYPE("nfsplitting",T);
    4113         105 :     T = Q_primpart(T);
    4114         105 :     RgX_check_ZX(T,"nfsplitting");
    4115             :   }
    4116         112 :   d = degpol(T);
    4117         112 :   if (d<=1) return pol_x(0);
    4118          91 :   if (!K) {
    4119          84 :     if (!isint1(leading_coeff(T))) K = T = polredbest(T,0);
    4120          84 :     K = T;
    4121             :   }
    4122          91 :   if (D)
    4123             :   {
    4124          21 :     if (typ(D) != t_INT || signe(D) < 1) pari_err_TYPE("nfsplitting",D);
    4125             :   }
    4126             :   else
    4127             :   {
    4128          70 :     char *data = stack_strcat(pari_datadir, "/galdata");
    4129          70 :     long dmax = pari_is_dir(data)? 11: 7;
    4130          70 :     D = (d <= dmax)? gel(polgalois(T,DEFAULTPREC), 1): mpfact(d);
    4131             :   }
    4132          91 :   d = itos_or_0(D);
    4133          91 :   v = varn(T);
    4134          91 :   T = leafcopy(T); setvarn(T, fetch_var_higher());
    4135          91 :   for(F = T;;)
    4136             :   {
    4137         119 :     GEN P = gel(nffactor(K, F), 1), Q = gel(P,lg(P)-1);
    4138         119 :     if (degpol(gel(P,1)) == degpol(Q)) break;
    4139         105 :     F = rnfequation(K,Q);
    4140         105 :     if (degpol(F) == d) break;
    4141          28 :   }
    4142          91 :   if (umodiu(D,degpol(F)))
    4143             :   {
    4144           7 :     char *sD = itostr(D);
    4145           7 :     pari_warn(warner,stack_strcat("ignoring incorrect degree bound ",sD));
    4146             :   }
    4147          91 :   (void)delete_var();
    4148          91 :   setvarn(F,v);
    4149          91 :   return gerepilecopy(av, F);
    4150             : }

Generated by: LCOV version 1.11