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

Generated by: LCOV version 1.11