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.8.0 lcov report (development 19614-52e089f) Lines: 1950 2274 85.8 %
Date: 2016-09-28 05:54:17 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         399 : update_fact(GEN d, GEN f)
      68             : {
      69             :   GEN P;
      70         399 :   switch (typ(f))
      71             :   {
      72         392 :     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        8967 : set_disc(nfmaxord_t *S)
      87             : {
      88             :   GEN l0, L, dT;
      89             :   long d;
      90        8967 :   if (S->T0 == S->T) return ZX_disc(S->T);
      91        2975 :   d = degpol(S->T0);
      92        2975 :   l0 = leading_coeff(S->T0);
      93        2975 :   L = S->unscale;
      94        2975 :   if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
      95         462 :     dT = ZX_disc(S->T); /* more efficient */
      96             :   else
      97             :   {
      98        2513 :     GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
      99        2513 :     dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
     100             :   }
     101        2975 :   return S->dT = dT;
     102             : }
     103             : static void
     104        8967 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
     105             : {
     106        8967 :   GEN dT, L, E, P, fa = NULL;
     107             :   pari_timer t;
     108        8967 :   long l, ty = typ(T);
     109             : 
     110        8967 :   if (DEBUGLEVEL) timer_start(&t);
     111        8967 :   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        8967 :   if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
     116        8967 :   T = Q_primpart(T);
     117        8967 :   if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
     118        8967 :   RgX_check_ZX(T, "nfmaxord");
     119        8967 :   S->T0 = T;
     120        8967 :   T = ZX_Q_normalize(T, &L);
     121        8967 :   S->unscale = L;
     122        8967 :   S->T = T;
     123        8967 :   S->dT = dT = set_disc(S);
     124        8967 :   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        4781 :     fa = (flag & nf_PARTIALFACT)? absZ_factor_limit(dT, 0): absZ_factor(dT);
     144        8960 :   P = gel(fa,1); l = lg(P);
     145        8960 :   E = gel(fa,2);
     146        8960 :   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        8960 :   S->dTP = P;
     153        8960 :   S->dTE = vec_to_vecsmall(E);
     154        8960 :   if (DEBUGLEVEL) timer_printf(&t, "disc. factorisation");
     155        8960 : }
     156             : 
     157             : static int
     158       37135 : fnz(GEN x,long j)
     159             : {
     160             :   long i;
     161      196210 :   for (i=1; i<j; i++)
     162      163079 :     if (signe(gel(x,i))) return 0;
     163       33131 :   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        8967 : get_maxord(nfmaxord_t *S, GEN T0, long flag)
     554             : {
     555             :   VOLATILE GEN P, E, O;
     556             :   VOLATILE long lP, i, k;
     557             : 
     558        8967 :   nfmaxord_check_args(S, T0, flag);
     559        8960 :   if (flag & nf_ROUND2) return allbase2(S);
     560        8960 :   P = S->dTP; lP = lg(P);
     561        8960 :   E = S->dTE;
     562        8960 :   O = cgetg(1, t_VEC);
     563       61607 :   for (i=1; i<lP; i++)
     564             :   {
     565             :     VOLATILE pari_sp av;
     566             :     /* includes the silly case where P[i] = -1 */
     567       52647 :     if (E[i] <= 1) { O = shallowconcat(O, gen_1); continue; }
     568       49756 :     av = avma;
     569       49756 :     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       49826 :     } pari_RETRY {
     620       49826 :       if (DEBUGLEVEL) err_printf("Treating p^k = %Ps^%ld\n",P[i],E[i]);
     621       49826 :       O = shallowconcat(O, mkvec( maxord(gel(P,i),S->T,E[i]) ));
     622       49756 :     } pari_ENDCATCH;
     623             :   }
     624        8960 :   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        6279 : diag_denom(GEN M)
     631             : {
     632        6279 :   GEN d = gen_1;
     633        6279 :   long j, l = lg(M);
     634       72373 :   for (j=1; j<l; j++)
     635             :   {
     636       66094 :     GEN t = gcoeff(M,j,j);
     637       66094 :     if (typ(t) == t_INT) continue;
     638       16478 :     t = gel(t,2);
     639       16478 :     if (abscmpii(t,d) > 0) d = t;
     640             :   }
     641        6279 :   return d;
     642             : }
     643             : void
     644        5838 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
     645             : {
     646        5838 :   GEN O = get_maxord(S, T0, flag);
     647        5831 :   GEN f = S->T, P = S->dTP, a = NULL, da = NULL, P2, E2, D;
     648        5831 :   long n = degpol(f), lP = lg(P), i, j, k;
     649        5831 :   int centered = 0;
     650        5831 :   pari_sp av = avma;
     651             :   /* r1 & basden not initialized here */
     652        5831 :   S->r1 = -1;
     653        5831 :   S->basden = NULL;
     654       19607 :   for (i=1; i<lP; i++)
     655             :   {
     656       13776 :     GEN M, db, b = gel(O,i);
     657       13776 :     if (b == gen_1) continue;
     658        6279 :     db = diag_denom(b);
     659        6279 :     if (db == gen_1) continue;
     660             : 
     661             :     /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
     662        6279 :     b = Q_muli_to_int(b,db);
     663        6279 :     if (!da) { da = db; a = b; }
     664             :     else
     665             :     { /* optimization: easy as long as both matrix are diagonal */
     666        4004 :       j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
     667        4004 :       k = j-1; M = cgetg(2*n-k+1,t_MAT);
     668       24479 :       for (j=1; j<=k; j++)
     669             :       {
     670       20475 :         gel(M,j) = gel(a,j);
     671       20475 :         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        4004 :       for (  ; j<=n;     j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
     675        4004 :       for (  ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
     676        4004 :       da = mulii(da,db);
     677        4004 :       a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
     678        4004 :       gerepileall(av, 2, &a, &da);
     679        4004 :       centered = 1;
     680             :     }
     681             :   }
     682        5831 :   if (da)
     683             :   {
     684        2275 :     GEN index = diviiexact(da, gcoeff(a,1,1));
     685        2275 :     for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
     686        2275 :     if (!centered) a = ZM_hnfcenter(a);
     687        2275 :     a = RgM_Rg_div(a, da);
     688        2275 :     S->index = index;
     689        2275 :     S->dK = diviiexact(S->dT, sqri(index));
     690             :   }
     691             :   else
     692             :   {
     693        3556 :     S->index = gen_1;
     694        3556 :     S->dK = S->dT;
     695        3556 :     a = matid(n);
     696             :   }
     697             : 
     698        5831 :   D = S->dK;
     699        5831 :   P2 = cgetg(lP, t_COL);
     700        5831 :   E2 = cgetg(lP, t_VECSMALL);
     701       19607 :   for (k = j = 1; j < lP; j++)
     702             :   {
     703       13776 :     long v = Z_pvalrem(D, gel(P,j), &D);
     704       13776 :     if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
     705             :   }
     706        5831 :   setlg(P2, k); S->dKP = P2;
     707        5831 :   setlg(E2, k); S->dKE = P2;
     708        5831 :   S->basis = RgM_to_RgXV(a, varn(f));
     709        5831 : }
     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      100694 : Flx_checkdeflate(GEN x)
     747             : {
     748      100694 :   ulong d = 0, i, lx = (ulong)lg(x);
     749      219202 :   for (i=3; i<lx; i++)
     750      192298 :     if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
     751      100694 :   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      100694 : Flx_radical(GEN f, ulong p)
     758             : {
     759      100694 :   long v0 = Flx_valrem(f, &f);
     760             :   ulong du, d, e;
     761             :   GEN u;
     762             : 
     763      100694 :   d = Flx_checkdeflate(f);
     764      100694 :   if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
     765       86961 :   if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
     766       86961 :   u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
     767       86954 :   du = degpol(u);
     768       86954 :   if (du)
     769             :   {
     770       61118 :     if (du == (ulong)degpol(f))
     771           0 :       f = Flx_radical(Flx_deflate(f,p), p);
     772             :     else
     773             :     {
     774       61118 :       u = Flx_normalize(u, p);
     775       61118 :       f = Flx_div(f, u, p);
     776       61118 :       if (p <= du)
     777             :       {
     778        6937 :         GEN w = Flxq_powu(f, du, u, p);
     779        6937 :         w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
     780        6937 :         f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
     781             :       }
     782             :     }
     783             :   }
     784       86954 :   if (v0) f = Flx_shift(f, 1);
     785       86954 :   return f;
     786             : }
     787             : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
     788             : static GEN
     789        3102 : FpX_radical(GEN f, GEN p)
     790             : {
     791             :   GEN u;
     792             :   long v0;
     793        3102 :   if (lgefint(p) == 3)
     794             :   {
     795         455 :     ulong q = p[2];
     796         455 :     return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
     797             :   }
     798        2647 :   v0 = ZX_valrem(f, &f);
     799        2647 :   u = FpX_gcd(f,FpX_deriv(f, p), p);
     800        2584 :   if (degpol(u)) f = FpX_div(f, u, p);
     801        2584 :   if (v0) f = RgX_shift(f, 1);
     802        2584 :   return f;
     803             : }
     804             : /* f / a */
     805             : static GEN
     806       93296 : zx_z_div(GEN f, ulong a)
     807             : {
     808       93296 :   long i, l = lg(f);
     809       93296 :   GEN g = cgetg(l, t_VECSMALL);
     810       93296 :   g[1] = f[1];
     811       93296 :   for (i = 2; i < l; i++) g[i] = f[i] / a;
     812       93296 :   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       96404 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
     819             : {
     820             :   GEN k, h, g, f, f2;
     821       96404 :   ulong q = p[2];
     822       96404 :   if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
     823       93296 :   {
     824       93302 :     ulong q = p[2], q2 = q*q;
     825       93302 :     f2 = ZX_to_Flx(F, q2);
     826       93302 :     f = Flx_red(f2, q);
     827       93302 :     g = Flx_radical(f, q);
     828       93296 :     h = Flx_div(f, g, q);
     829       93296 :     k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
     830       93296 :     k = Flx_gcd(k, Flx_gcd(g,h,q), q);
     831       93296 :     k = Flx_to_ZX(k);
     832       93296 :     g = Flx_to_ZX(g);
     833             :   }
     834             :   else
     835             :   {
     836        3102 :     f2 = FpX_red(F, sqri(p));
     837        3102 :     f = FpX_red(f2, p);
     838        3102 :     g = FpX_radical(f, p);
     839        3038 :     h = FpX_div(f, g, p);
     840        3038 :     k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
     841        3038 :     k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
     842             :   }
     843       96334 :   *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       96404 : maxord(GEN p, GEN f, long mf)
     850             : {
     851       96404 :   const pari_sp av = avma;
     852       96404 :   GEN res, g, k = ZX_Dedekind(f, &g, p);
     853       96334 :   long dk = degpol(k);
     854       96334 :   if (DEBUGLEVEL>2) err_printf("  ZX_dedekind: gcd has degree %ld\n", dk);
     855       96334 :   if (!dk) { avma = av; return gen_1; }
     856       67109 :   if (mf < 0) mf = ZpX_disc_val(f, p);
     857       67109 :   if (2*dk >= mf-1)
     858             :   {
     859       34468 :     k = FpX_normalize(k, p);
     860       34468 :     res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
     861             :   }
     862             :   else
     863             :   {
     864             :     GEN w, F1, F2;
     865       32641 :     F1 = FpX_factor(k,p);
     866       32641 :     F2 = FpX_factor(FpX_div(g,k,p),p);
     867       32641 :     w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
     868       32641 :     res = maxord_i(p, f, mf, w, 0);
     869             :   }
     870       67109 :   return gerepilecopy(av,res);
     871             : }
     872             : 
     873             : static GEN
     874      188916 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
     875             : {
     876      188916 :   long j, n = degpol(f1);
     877      188916 :   GEN h, a = cgetg(n+1,t_MAT);
     878      188916 :   f1 = Flx_get_red(f1, pm);
     879      188916 :   h = Flx_rem(f2,f1,pm);
     880     1459520 :   for (j=1;; j++)
     881             :   {
     882     1459520 :     gel(a,j) = Flx_to_Flv(h, n);
     883     1459520 :     if (j == n) break;
     884     1270604 :     h = Flx_rem(Flx_shift(h, 1), f1, pm);
     885     1270604 :   }
     886      188916 :   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       13121 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
     892             : {
     893       13121 :   long j, n = degpol(f1);
     894       13121 :   GEN h, a = cgetg(n+1,t_MAT);
     895       13121 :   h = FpXQ_red(f2,f1,pm);
     896      137736 :   for (j=1;; j++)
     897             :   {
     898      137736 :     gel(a,j) = RgX_to_RgC(h, n);
     899      137736 :     if (j == n) break;
     900      124615 :     h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
     901      124615 :   }
     902       13121 :   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       17995 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
     908             : {
     909       17995 :   pari_sp av = avma;
     910       17995 :   GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
     911       17995 :   long c, l = lg(a), sv = f1[1];
     912      105553 :   for (c = 1; c < l; c++)
     913             :   {
     914      105553 :     ulong t = ucoeff(a,c,c);
     915      105553 :     if (t)
     916             :     {
     917       17995 :       a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
     918       17995 :       if (t == 1) return gerepilecopy(av, a);
     919        2499 :       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       23667 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
     927             : {
     928       23667 :   pari_sp av = avma;
     929             :   GEN a;
     930             :   long c, l, v;
     931       23667 :   if (lgefint(pm) == 3)
     932             :   {
     933       17995 :     ulong q = pm[2];
     934       17995 :     return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
     935             :   }
     936        5672 :   a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
     937        5672 :   l = lg(a); v = varn(f1);
     938       38381 :   for (c = 1; c < l; c++)
     939             :   {
     940       38381 :     GEN t = gcoeff(a,c,c);
     941       38381 :     if (signe(t))
     942             :     {
     943        5672 :       a = RgV_to_RgX(gel(a,c), v);
     944        5672 :       if (equali1(t)) return gerepilecopy(av, a);
     945        1582 :       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      126042 : init_m(GEN p)
     954             : {
     955      126042 :   if (lgefint(p) > 3) return 1;
     956      126034 :   return (long)(16 / log2(p[2]));
     957             : }
     958             : 
     959             : /* reduced resultant mod p^m (assumes x monic) */
     960             : GEN
     961       80069 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
     962             : {
     963       80069 :   pari_sp av = avma;
     964             :   GEN z;
     965       80069 :   if (lgefint(pm) == 3)
     966             :   {
     967       75488 :     ulong q = pm[2];
     968       75488 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
     969       75488 :     if (lg(z) > 1)
     970             :     {
     971       75488 :       ulong c = ucoeff(z,1,1);
     972       75488 :       if (c) { avma = av; return utoipos(c); }
     973             :     }
     974             :   }
     975             :   else
     976             :   {
     977        4581 :     z = ZpX_sylvester_echelon(x,y,0,p,pm);
     978        4581 :     if (lg(z) > 1)
     979             :     {
     980        4581 :       GEN c = gcoeff(z,1,1);
     981        4581 :       if (signe(c)) return gerepileuptoint(av, c);
     982             :     }
     983             :   }
     984       31835 :   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       54425 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
     990             : {
     991       54425 :   GEN R, q = NULL;
     992             :   long m;
     993       54425 :   m = init_m(p); if (m < 1) m = 1;
     994       25644 :   for(;; m <<= 1) {
     995       80069 :     if (M < 2*m) break;
     996       39105 :     q = q? sqri(q): powiu(p, m); /* p^m */
     997       39105 :     R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
     998       25644 :   }
     999       40964 :   q = powiu(p, M);
    1000       40964 :   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       98301 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
    1006             : {
    1007       98301 :   pari_sp av = avma;
    1008             :   GEN z;
    1009             :   long i, l, v;
    1010       98301 :   if (lgefint(pm) == 3)
    1011             :   {
    1012       95433 :     ulong q = pm[2], pp = p[2];
    1013       95433 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
    1014       95433 :     if (!z) { avma = av; return -1; } /* failure */
    1015       68830 :     v = 0; l = lg(z);
    1016       68830 :     for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
    1017             :   }
    1018             :   else
    1019             :   {
    1020        2868 :     z = ZpX_sylvester_echelon(x, y, 1, p, pm);
    1021        2868 :     if (!z) { avma = av; return -1; } /* failure */
    1022        2444 :     v = 0; l = lg(z);
    1023        2444 :     for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
    1024             :   }
    1025       71274 :   return v;
    1026             : }
    1027             : 
    1028             : /* assume (lc(f),p) = 1; no assumption on g */
    1029             : long
    1030       71617 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
    1031             : {
    1032       71617 :   pari_sp av = avma;
    1033       71617 :   GEN q = NULL;
    1034             :   long v, m;
    1035       71617 :   m = init_m(p); if (m < 2) m = 2;
    1036       26684 :   for(;; m <<= 1) {
    1037       98301 :     if (m > M) m = M;
    1038       98301 :     q = q? sqri(q): powiu(p, m); /* p^m */
    1039       98301 :     v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) break;
    1040       27027 :     if (m == M) return M;
    1041       26684 :   }
    1042       71274 :   avma = av; return v;
    1043             : }
    1044             : 
    1045             : /* assume f separable and (lc(f),p) = 1 */
    1046             : long
    1047       27951 : ZpX_disc_val(GEN f, GEN p)
    1048             : {
    1049       27951 :   pari_sp av = avma;
    1050             :   long v;
    1051       27951 :   if (degpol(f) == 1) return 0;
    1052       27951 :   v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
    1053       27951 :   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      529000 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
    1060             : {
    1061             :   GEN newe;
    1062      529000 :   long ve = ZX_pvalrem(*e, p, &newe);
    1063      529000 :   if (ve) {
    1064             :     GEN newd;
    1065      300892 :     long v = minss(*vd, ve);
    1066      300892 :     if (v) {
    1067      300892 :       if (v == *vd)
    1068             :       { /* rare, denominator cancelled */
    1069       35462 :         if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
    1070       35462 :         newd = gen_1;
    1071       35462 :         *vd = 0;
    1072       35462 :         if (z) *z =diviiexact(*z, powiu(p, v));
    1073             :       }
    1074             :       else
    1075             :       { /* v = ve < vd, generic case */
    1076      265430 :         GEN q = powiu(p, v);
    1077      265430 :         newd = diviiexact(*d, q);
    1078      265430 :         *vd -= v;
    1079      265430 :         if (z) *z = diviiexact(*z, q);
    1080             :       }
    1081      300892 :       *e = newe;
    1082      300892 :       *d = newd;
    1083             :     }
    1084             :   }
    1085      529000 : }
    1086             : 
    1087             : /* return denominator, a power of p */
    1088             : static GEN
    1089      333206 : QpX_denom(GEN x)
    1090             : {
    1091      333206 :   long i, l = lg(x);
    1092      333206 :   GEN maxd = gen_1;
    1093     1726651 :   for (i=2; i<l; i++)
    1094             :   {
    1095     1393445 :     GEN d = gel(x,i);
    1096     1393445 :     if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
    1097             :   }
    1098      333206 :   return maxd;
    1099             : }
    1100             : static GEN
    1101       46578 : QpXV_denom(GEN x)
    1102             : {
    1103       46578 :   long l = lg(x), i;
    1104       46578 :   GEN maxd = gen_1;
    1105      237881 :   for (i = 1; i < l; i++)
    1106             :   {
    1107      191303 :     GEN d = QpX_denom(gel(x,i));
    1108      191303 :     if (cmpii(d, maxd) > 0) maxd = d;
    1109             :   }
    1110       46578 :   return maxd;
    1111             : }
    1112             : 
    1113             : static GEN
    1114      141903 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
    1115             : {
    1116      141903 :   *pdx = QpX_denom(x);
    1117      141903 :   if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
    1118             :   else {
    1119      107365 :     x = Q_muli_to_int(x,*pdx);
    1120      107365 :     *pv = Z_pval(*pdx, p);
    1121             :   }
    1122      141903 :   return x;
    1123             : }
    1124             : 
    1125             : /* p^v * f o g mod (T,q). q = p^vq  */
    1126             : static GEN
    1127       20286 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
    1128             : {
    1129       20286 :   GEN D = NULL, z, df, dg, qD;
    1130       20286 :   long vD = 0, vdf, vdg;
    1131             : 
    1132       20286 :   f = QpX_remove_denom(f, p, &df, &vdf);
    1133       20286 :   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       20286 :     g = QpX_remove_denom(g, p, &dg, &vdg);
    1137       20286 :   if (df) { D = df; vD = vdf; }
    1138       20286 :   if (dg) {
    1139        3885 :     long degf = degpol(f);
    1140        3885 :     D = mul_content(D, powiu(dg, degf));
    1141        3885 :     vD += degf * vdg;
    1142             :   }
    1143       20286 :   qD = D ? mulii(q, D): q;
    1144       20286 :   if (dg) f = FpX_rescale(f, dg, qD);
    1145       20286 :   z = FpX_FpXQ_eval(f, g, T, qD);
    1146       20286 :   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       20286 :   update_den(p, &z, &D, &vD, NULL);
    1156       20286 :   qD = mulii(D,q);
    1157       20286 :   if (v) vD -= v;
    1158       20286 :   z = FpX_center(z, qD, shifti(qD,-1));
    1159       20286 :   if (vD > 0)
    1160       20286 :     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       20286 :   return z;
    1164             : }
    1165             : 
    1166             : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
    1167             : static GEN
    1168       32641 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
    1169             : {
    1170       32641 :   long i, l = lg(M);
    1171       32641 :   M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
    1172      265720 :   for (i = 1; i < l; i++)
    1173      233079 :     if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
    1174       32641 :   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       43820 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
    1181             : {
    1182       43820 :   long n = degpol(f), i, dU;
    1183             :   GEN b, h;
    1184             : 
    1185       43820 :   if (n == 1) return matid(1);
    1186       43820 :   if (a && gequalX(a)) a = NULL;
    1187       43820 :   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       43820 :   if (a)
    1193             :   {
    1194        9352 :     GEN pd = powiu(p, mf >> 1);
    1195        9352 :     GEN da, pdp = mulii(pd,p), D = pdp;
    1196             :     long vda;
    1197        9352 :     dU = U ? degpol(U): 0;
    1198        9352 :     b = cgetg(n+1, t_MAT);
    1199        9352 :     h = scalarpol(pd, varn(f));
    1200        9352 :     a = QpX_remove_denom(a, p, &da, &vda);
    1201        9352 :     if (da) D = mulii(D, da);
    1202        9352 :     gel(b,1) = scalarcol_shallow(pd, n);
    1203       41776 :     for (i=2; i<=n; i++)
    1204             :     {
    1205       32424 :       if (i == dU+1)
    1206           0 :         h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
    1207             :       else
    1208             :       {
    1209       32424 :         h = FpXQ_mul(h, a, f, D);
    1210       32424 :         if (da) h = ZX_Z_divexact(h, da);
    1211             :       }
    1212       32424 :       gel(b,i) = RgX_to_RgC(h,n);
    1213             :     }
    1214        9352 :     return ZpM_hnfmodid(b, p, pd);
    1215             :   }
    1216             :   else
    1217             :   {
    1218       34468 :     if (!U) return matid(n);
    1219       34468 :     dU = degpol(U);
    1220       34468 :     if (dU == n) return matid(n);
    1221       34468 :     U = FpX_normalize(U, p);
    1222       34468 :     b = cgetg(n+1, t_MAT);
    1223       34468 :     for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
    1224       34468 :     h = RgX_Rg_div(U, p);
    1225       44912 :     for ( ; i <= n; i++)
    1226             :     {
    1227       44912 :       gel(b, i) = RgX_to_RgC(h,n);
    1228       44912 :       if (i == n) break;
    1229       10444 :       h = RgX_shift_shallow(h,1);
    1230             :     }
    1231       34468 :     return b;
    1232             :   }
    1233             : }
    1234             : 
    1235             : static GEN
    1236       46578 : get_partial_order_as_pols(GEN p, GEN f)
    1237             : {
    1238       46578 :   GEN O = maxord(p, f, -1);
    1239       46578 :   long v = varn(f);
    1240       46578 :   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       23667 : Decomp(decomp_t *S, long flag)
    1276             : {
    1277       23667 :   pari_sp av = avma;
    1278             :   GEN fred, pr, pk, ph, b1, b2, a, e, de, f1, f2, dt, th;
    1279       23667 :   GEN p = S->p, chip;
    1280       23667 :   long k, r = flag? flag: 2*S->df + 1;
    1281             :   long vde, vdt;
    1282             : 
    1283       23667 :   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       23667 :   chip = FpX_red(S->chi, p);
    1290       23667 :   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       23667 :   b2 = FpX_div(chip, b1, p);
    1296       23667 :   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       23667 :   th = QpX_remove_denom(S->phi, p, &dt, &vdt);
    1299       23667 :   if (dt)
    1300             :   {
    1301        9464 :     long dega = degpol(a);
    1302        9464 :     vde = dega * vdt;
    1303        9464 :     de = powiu(dt, dega);
    1304        9464 :     pr = mulii(p, de);
    1305        9464 :     a = FpX_rescale(a, dt, pr);
    1306             :   }
    1307             :   else
    1308             :   {
    1309       14203 :     vde = 0;
    1310       14203 :     de = gen_1;
    1311       14203 :     pr = p;
    1312             :   }
    1313       23667 :   e = FpX_FpXQ_eval(a, th, S->f, pr);
    1314       23667 :   update_den(p, &e, &de, &vde, NULL);
    1315             : 
    1316       23667 :   pk = p; k = 1;
    1317             :   /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
    1318      151676 :   while (k < r + vde)
    1319             :   { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
    1320             :     GEN D;
    1321      104342 :     pk = sqri(pk); k <<= 1;
    1322      104342 :     e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
    1323      104342 :     de= mulii(de, sqri(de));
    1324      104342 :     vde *= 3;
    1325      104342 :     D = mulii(pk, de);
    1326      104342 :     e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
    1327      104342 :     update_den(p, &e, &de, &vde, NULL);
    1328             :   }
    1329       23667 :   pr = powiu(p, r); /* required precision of the factors */
    1330       23667 :   ph = mulii(de, pr);
    1331       23667 :   fred = centermod(S->f, ph);
    1332       23667 :   e    = centermod(e, ph);
    1333             : 
    1334       23667 :   f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
    1335       23667 :   fred = centermod(fred, pr);
    1336       23667 :   f1   = centermod(f1,   pr);
    1337       23667 :   f2 = FpX_div(fred,f1, pr);
    1338       23667 :   f2 = FpX_center(f2, pr, shifti(pr,-1));
    1339             : 
    1340       23667 :   if (DEBUGLEVEL>5)
    1341           0 :     err_printf("  leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
    1342             : 
    1343       23667 :   if (flag) {
    1344         378 :     gerepileall(av, 2, &f1, &f2);
    1345         378 :     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       23289 :     gerepileall(av, 4, &f1, &f2, &e, &de);
    1351       23289 :     D = de;
    1352       23289 :     B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
    1353       23289 :     B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
    1354       23289 :     d1 = QpXV_denom(B1);
    1355       23289 :     d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
    1356       23289 :     if (d1 != gen_1) {
    1357       20356 :       B1 = Q_muli_to_int(B1, d1);
    1358       20356 :       B2 = Q_muli_to_int(B2, d1);
    1359       20356 :       D = mulii(d1, D);
    1360             :     }
    1361       23289 :     fred = centermod_i(S->f, D, shifti(D,-1));
    1362       23289 :     M = cgetg(n+1, t_MAT);
    1363      142464 :     for (i=1; i<=n1; i++)
    1364      119175 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
    1365       23289 :     e = Z_ZX_sub(de, e); B2 -= n1;
    1366       95417 :     for (   ; i<=n; i++)
    1367       72128 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
    1368       23289 :     return ZpM_hnfmodid(M, p, D);
    1369             :   }
    1370             : }
    1371             : 
    1372             : /* minimum extension valuation: L/E */
    1373             : static void
    1374       48621 : vstar(GEN p,GEN h, long *L, long *E)
    1375             : {
    1376       48621 :   long first, j, k, v, w, m = degpol(h);
    1377             : 
    1378       48621 :   first = 1; k = 1; v = 0;
    1379      339173 :   for (j=1; j<=m; j++)
    1380             :   {
    1381      290552 :     GEN c = gel(h, m-j+2);
    1382      290552 :     if (signe(c))
    1383             :     {
    1384      280283 :       w = Z_pval(c,p);
    1385      280283 :       if (first || w*k < v*j) { v = w; k = j; }
    1386      280283 :       first = 0;
    1387             :     }
    1388             :   }
    1389             :   /* v/k = min_j ( v_p(h_{m-j}) / j ) */
    1390       48621 :   w = (long)ugcd(v,k);
    1391       48621 :   *L = v/w;
    1392       48621 :   *E = k/w;
    1393       48621 : }
    1394             : 
    1395             : static GEN
    1396       11165 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
    1397             : {
    1398             :   GEN z;
    1399       11165 :   a = Q_remove_denom(a, pda);
    1400       11165 :   *pvda = 0;
    1401       11165 :   if (*pda)
    1402             :   {
    1403       11165 :     long v = Z_pvalrem(*pda, p, &z);
    1404       11165 :     if (v) {
    1405       11165 :       *pda = powiu(p, v);
    1406       11165 :       *pvda = v;
    1407       11165 :       N  = mulii(*pda, N);
    1408             :     }
    1409             :     else
    1410           0 :       *pda = NULL;
    1411       11165 :     if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
    1412             :   }
    1413       11165 :   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       38913 : polsymmodp(GEN g, GEN p)
    1430             : {
    1431             :   pari_sp av;
    1432       38913 :   long d = degpol(g), i, k;
    1433             :   GEN s, y, po2;
    1434             : 
    1435       38913 :   y = cgetg(d + 1, t_COL);
    1436       38913 :   gel(y,1) = utoipos(d);
    1437       38913 :   if (d == 1) return y;
    1438             :   /* k = 1, split off for efficiency */
    1439       38913 :   po2 = shifti(p,-1); /* to be left on stack */
    1440       38913 :   av = avma;
    1441       38913 :   s = gel(g,d-1+2);
    1442       38913 :   gel(y,2) = gerepileuptoint(av, centermodii(negi(s), p, po2));
    1443      150535 :   for (k = 2; k < d; k++)
    1444             :   {
    1445      111622 :     av = avma;
    1446      111622 :     s = mului(k, remii(gel(g,d-k+2), p));
    1447      111622 :     for (i = 1; i < k; i++) s = addii(s, mulii(gel(y,k-i+1), gel(g,d-i+2)));
    1448      111622 :     togglesign_safe(&s);
    1449      111622 :     gel(y,k+1) = gerepileuptoint(av, centermodii(s, p, po2));
    1450             :   }
    1451       38913 :   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       65589 : 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       65589 :   a = centermod(a, pp); av = avma;
    1466       65589 :   dpa = pa = NULL; /* -Wall */
    1467       65589 :   vdpa = 0;
    1468       65589 :   va = zerovec(c);
    1469      445902 :   for (j = 1; j <= c; j++)
    1470             :   { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
    1471             :     long degpa;
    1472      381265 :     pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
    1473      381265 :     degpa = degpol(pa);
    1474      381265 :     if (degpa < 0) {
    1475           0 :       for (; j <= c; j++) gel(va,j) = gen_0;
    1476           0 :       return va;
    1477             :     }
    1478             : 
    1479      381265 :     if (da) {
    1480      373068 :       dpa = j == 1? da: mulii(dpa, da);
    1481      373068 :       vdpa += vda;
    1482      373068 :       update_den(p, &pa, &dpa, &vdpa, &pp);
    1483             :     }
    1484      381265 :     s = mulii(gel(pa,2), gel(ns,1)); /* k = 0 */
    1485      381265 :     for (k=1; k<=degpa; k++) s = addii(s, mulii(gel(pa,k+2), gel(ns,k+1)));
    1486      381265 :     if (da) {
    1487             :       GEN r;
    1488      373068 :       s = dvmdii(s, dpa, &r);
    1489      373068 :       if (r != gen_0) return NULL;
    1490             :     }
    1491      380313 :     gel(va,j) = centermodii(s, pp, shifti(pp,-1));
    1492             : 
    1493      380313 :     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       64637 :   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       64637 : newtoncharpoly(GEN pp, GEN p, GEN NS)
    1506             : {
    1507       64637 :   long n = lg(NS)-1, j, k;
    1508       64637 :   GEN c = cgetg(n + 2, t_VEC);
    1509             : 
    1510       64637 :   gel(c,1) = (n & 1 ? gen_m1: gen_1);
    1511      442731 :   for (k = 2; k <= n+1; k++)
    1512             :   {
    1513      378115 :     pari_sp av2 = avma;
    1514      378115 :     GEN s = gen_0;
    1515             :     ulong z;
    1516      378115 :     long v = u_pvalrem(k - 1, p, &z);
    1517     3016727 :     for (j = 1; j < k; j++)
    1518             :     {
    1519     2638612 :       GEN t = mulii(gel(NS,j), gel(c,k-j));
    1520     2638612 :       if (!odd(j)) t = negi(t);
    1521     2638612 :       s = addii(s, t);
    1522             :     }
    1523      378115 :     if (v) {
    1524      133315 :       s = gdiv(s, powiu(p, v));
    1525      133315 :       if (typ(s) != t_INT) return NULL;
    1526             :     }
    1527      378094 :     s = mulii(s, Fp_inv(utoipos(z), pp));
    1528      378094 :     gel(c,k) = gerepileuptoint(av2, centermod(s, pp));
    1529             :   }
    1530       64616 :   for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
    1531       64616 :   return gtopoly(c, 0);
    1532             : }
    1533             : 
    1534             : static void
    1535       65589 : manage_cache(decomp_t *S, GEN f, GEN pp)
    1536             : {
    1537       65589 :   GEN t = S->precns;
    1538             : 
    1539       65589 :   if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
    1540       65589 :   if (cmpii(t, pp) < 0) t = pp;
    1541             : 
    1542       65589 :   if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
    1543             :   {
    1544       38913 :     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       38913 :     S->nsf = f;
    1548       38913 :     S->ns = polsymmodp(f, t);
    1549       38913 :     S->precns = t;
    1550             :   }
    1551       65589 : }
    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       65589 : 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       65589 :   long vd, n = degpol(f);
    1561             : 
    1562       65589 :   if (gequal0(a)) return pol_0(varn(f));
    1563             : 
    1564       65589 :   a = QpX_remove_denom(a, S->p, &d, &vd);
    1565       65589 :   prec1 = pp;
    1566       65589 :   if (lgefint(S->p) == 3)
    1567       65586 :     prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
    1568       65589 :   if (d)
    1569             :   {
    1570       63202 :     GEN p1 = powiu(d, n);
    1571       63202 :     prec2 = mulii(prec1, p1);
    1572       63202 :     prec3 = mulii(prec1, gmin(mulii(p1, d), pdr));
    1573             :   }
    1574             :   else
    1575        2387 :     prec2 = prec3 = prec1;
    1576       65589 :   manage_cache(S, f, prec3);
    1577             : 
    1578       65589 :   av = avma;
    1579       65589 :   ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
    1580       65589 :   if (!ns) return NULL;
    1581       64637 :   chi = newtoncharpoly(prec1, S->p, ns);
    1582       64637 :   if (!chi) return NULL;
    1583       64616 :   setvarn(chi, varn(f));
    1584       64616 :   return gerepileupto(av, centermod(chi, pp));
    1585             : }
    1586             : 
    1587             : static GEN
    1588       60192 : get_nu(GEN chi, GEN p, long *ptl)
    1589             : {
    1590       60192 :   GEN P = gel(FpX_factor(chi, p),1);
    1591       60192 :   *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       50988 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
    1599             : {
    1600             :   long l;
    1601       50988 :   *nu  = get_nu(chi, S->p, &l);
    1602       50988 :   if (l == 1) return 0; /* single irreducible factor: doesn't split */
    1603             :   /* phi o phi0 mod (p, f) */
    1604        9464 :   S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
    1605        9464 :   S->chi = chi;
    1606        9464 :   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       46948 : 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       46948 :   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       46948 :   if (degpol(nup) == 1)
    1626             :   {
    1627       39536 :     GEN c = gel(nup,2); /* nup = X + c */
    1628       39536 :     chin = signe(c)? RgX_translate(chip, negi(c)): chip;
    1629             :   }
    1630             :   else
    1631        7412 :     chin = ZXQ_charpoly(nup, chip, varn(chip));
    1632             : 
    1633       46948 :   vstar(S->p, chin, Lp, Ep);
    1634       46948 :   if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
    1635             : 
    1636       26341 :   if (*Ep == 1) return S->p;
    1637       14644 :   (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
    1638       14644 :   if (r <= 0)
    1639             :   {
    1640        2156 :     long t = 1 + ((-r) / *Ep);
    1641        2156 :     r += t * *Ep;
    1642        2156 :     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       14644 :   q = powiu(S->p, s); qp = mulii(q, S->p);
    1648       14644 :   nup = FpXQ_powu(nup, r, S->chi, qp);
    1649       14644 :   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       14839 : update_phi(decomp_t *S)
    1656             : {
    1657       14839 :   GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
    1658             :   long k;
    1659       14943 :   for (k = 1;; k++)
    1660             :   {
    1661       14943 :     prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, S->vpsc);
    1662       14943 :     if (!equalii(prc, S->psc)) break;
    1663             : 
    1664             :     /* increase precision */
    1665         104 :     S->vpsc = maxss(S->vpsf, S->vpsc + 1);
    1666         104 :     S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
    1667             : 
    1668         104 :     PHI = S->phi;
    1669         104 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
    1670         104 :     PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
    1671         104 :     S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
    1672         104 :   }
    1673       14839 :   psc = mulii(sqri(prc), S->p);
    1674             : 
    1675       14839 :   if (!PHI) /* ok above for k = 1 */
    1676             :   {
    1677       14735 :     PHI = S->phi;
    1678       14735 :     if (S->phi0)
    1679             :     {
    1680        9171 :       PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
    1681        9171 :       S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
    1682             :     }
    1683             :   }
    1684       14839 :   S->phi = PHI;
    1685       14839 :   S->chi = FpX_red(S->chi, psc);
    1686             : 
    1687             :   /* may happen if p is unramified */
    1688       14839 :   if (is_pm1(prc)) return 0;
    1689       11094 :   S->psc = psc;
    1690       11094 :   S->vpsc = 2*Z_pval(prc, S->p) + 1;
    1691       11094 :   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        7728 : testb2(decomp_t *S, long D, GEN theta)
    1698             : {
    1699        7728 :   long v = varn(S->chi), dlim = degpol(S->chi)-1;
    1700        7728 :   GEN T0 = S->phi, chi, phi, nu;
    1701        7728 :   if (DEBUGLEVEL>4) err_printf("  Increasing Fa\n");
    1702             :   for (;;)
    1703             :   {
    1704        7728 :     phi = gadd(theta, random_FpX(dlim, v, S->p));
    1705        7728 :     chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1706             :     /* phi non-primary ? */
    1707        7728 :     if (split_char(S, chi, phi, T0, &nu)) return 1;
    1708        7728 :     if (degpol(nu) == D) break;
    1709           0 :   }
    1710             :   /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
    1711        7728 :   S->phi0 = T0;
    1712        7728 :   S->chi = chi;
    1713        7728 :   S->phi = phi;
    1714        7728 :   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        1407 : ZX_rescale_inv(GEN P, GEN h)
    1753             : {
    1754        1407 :   long i, l = lg(P);
    1755        1407 :   GEN Q = cgetg(l,t_POL), hi = h;
    1756        1407 :   gel(Q,l-1) = gel(P,l-1);
    1757        8008 :   for (i=l-2; i>=2; i--)
    1758             :   {
    1759             :     GEN r;
    1760        8008 :     gel(Q,i) = dvmdii(gel(P,i), hi, &r);
    1761        8008 :     if (signe(r)) return NULL;
    1762        8008 :     if (i == 2) break;
    1763        6601 :     hi = mulii(hi,h);
    1764             :   }
    1765        1407 :   Q[1] = P[1]; return Q;
    1766             : }
    1767             : 
    1768             : /* x p^-eq nu^-er mod p */
    1769             : static GEN
    1770       38556 : get_gamma(decomp_t *S, GEN x, long eq, long er)
    1771             : {
    1772       38556 :   GEN q, g = x, Dg = powiu(S->p, eq);
    1773       38556 :   long vDg = eq;
    1774       38556 :   if (er)
    1775             :   {
    1776        7637 :     if (!S->invnu)
    1777             :     {
    1778        3906 :       while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
    1779        3906 :       S->invnu = QXQ_inv(S->nu, S->chi);
    1780        3906 :       S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
    1781             :     }
    1782        7637 :     if (S->Dinvnu) {
    1783        7637 :       Dg = mulii(Dg, powiu(S->Dinvnu, er));
    1784        7637 :       vDg += er * S->vDinvnu;
    1785             :     }
    1786        7637 :     q = mulii(S->p, Dg);
    1787        7637 :     g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
    1788        7637 :     g = FpX_rem(g, S->chi, q);
    1789        7637 :     update_den(S->p, &g, &Dg, &vDg, NULL);
    1790        7637 :     g = centermod(g, mulii(S->p, Dg));
    1791             :   }
    1792       38556 :   if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
    1793       38556 :   return g;
    1794             : }
    1795             : static GEN
    1796       38990 : 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       38990 :   GEN g, chig, chib = NULL;
    1801             :   for(;;) /* at most twice */
    1802             :   {
    1803       39963 :     if (L < 0)
    1804             :     {
    1805        1673 :       chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
    1806        1673 :       vstar(S->p, chib, &L, &E);
    1807             :     }
    1808       39963 :     eq = L / E; er = L*Ea / E - eq*Ea;
    1809             :     /* floor(L Ea/E) = eq Ea + er */
    1810       39963 :     if (er || !chib)
    1811             :     { /* g might not be an integer ==> chig = NULL */
    1812       38556 :       g = get_gamma(S, beta, eq, er);
    1813       38556 :       chig = mycaract(S, S->chi, g, S->psc, S->prc);
    1814             :     }
    1815             :     else
    1816             :     { /* g = beta/p^eq, special case of the above */
    1817        1407 :       GEN h = powiu(S->p, eq);
    1818        1407 :       g = RgX_Rg_div(beta, h);
    1819        1407 :       chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
    1820        1407 :       if (chig) chig = FpX_red(chig, S->pmf);
    1821             :     }
    1822             :     /* either success or second consecutive failure */
    1823       39963 :     if (chig || chib) break;
    1824             :     /* if g fails the v*-test, v(beta) was wrong. Retry once */
    1825         973 :     L = -1;
    1826         973 :   }
    1827       38990 :   *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       18739 : loop(decomp_t *S, long Ea)
    1833             : {
    1834       18739 :   pari_sp av = avma;
    1835       18739 :   GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
    1836       18739 :   long N = degpol(S->f), v = varn(S->f);
    1837       18739 :   S->invnu = NULL;
    1838             :   for (;;)
    1839             :   { /* beta tends to a factor of chi */
    1840             :     long L, i, Fg, eq, er;
    1841       38990 :     GEN chig = NULL, d, g, nug;
    1842             : 
    1843       38990 :     if (DEBUGLEVEL>4) err_printf("  beta = %Ps\n", beta);
    1844       38990 :     L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
    1845       38990 :     if (L > S->mf) L = -1; /* from scratch */
    1846       38990 :     g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
    1847       38990 :     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       57729 :     if (split_char(S, chig, g,S->phi, &nug)) return 1;
    1850             : 
    1851       29918 :     Fg = degpol(nug);
    1852       29918 :     if (Fg == 1)
    1853             :     { /* frequent special case nug = x - d */
    1854             :       long Le, Ee;
    1855             :       GEN chie, nue, e, pie;
    1856       19467 :       d = negi(gel(nug,2));
    1857       19467 :       chie = RgX_translate(chig, d);
    1858       19467 :       nue = pol_x(v);
    1859       19467 :       e = RgX_Rg_sub(g, d);
    1860       19467 :       pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1861       19467 :       if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1862             :     }
    1863             :     else
    1864             :     {
    1865       10451 :       long Fa = degpol(S->nu), vdeng;
    1866             :       GEN deng, numg, nume;
    1867       18746 :       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        2723 :       if (ZX_equal(nug, S->nu))
    1871        1722 :         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        2723 :       numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
    1879        4676 :       for (i = 1; i <= Fg; i++)
    1880             :       {
    1881             :         GEN chie, nue, e;
    1882        4676 :         if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
    1883        4676 :         nume = ZX_sub(numg, ZX_Z_mul(d, deng));
    1884             :         /* test e = nume / deng */
    1885        4676 :         if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
    1886        1953 :           continue;
    1887        2723 :         e = RgX_Rg_div(nume, deng);
    1888        2723 :         chie = mycaract(S, S->chi, e, S->psc, S->prc);
    1889        3290 :         if (split_char(S, chie, e,S->phi, &nue)) return 1;
    1890        2331 :         if (RgX_is_monomial(nue))
    1891             :         { /* v_p(e) = v_p(g - d) > 0 */
    1892             :           long Le, Ee;
    1893             :           GEN pie;
    1894        2331 :           pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1895        2331 :           if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1896        2156 :           break;
    1897             :         }
    1898             :       }
    1899        2156 :       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       20251 :     if (eq) d = gmul(d, powiu(S->p,  eq));
    1906       20251 :     if (er) d = gmul(d, gpowgs(S->nu, er));
    1907       20251 :     beta = gsub(beta, d);
    1908             : 
    1909       20251 :     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       20251 :   }
    1915             : }
    1916             : 
    1917             : static long
    1918       24794 : loop_init(decomp_t *S, GEN *popa, long *poE)
    1919             : {
    1920       24794 :   long oE = *poE;
    1921       24794 :   GEN opa = *popa;
    1922             :   for(;;)
    1923             :   {
    1924             :     long l, La, Ea; /* N.B If oE = 0, getprime cannot return NULL */
    1925       25150 :     GEN pia  = getprime(S, NULL, S->chi, S->nu, &La, &Ea, oE,0);
    1926       25150 :     if (pia) { /* success, we break out in THIS loop */
    1927       24794 :       opa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
    1928       24794 :       oE = Ea;
    1929       49588 :       if (La == 1) break; /* no need to change phi so that nu = pia */
    1930             :     }
    1931             :     /* phi += prime elt */
    1932       13564 :     S->phi = typ(opa) == t_INT? RgX_Rg_add_shallow(S->phi, opa)
    1933        8000 :                               : RgX_add(S->phi, opa);
    1934             :     /* recompute char. poly. chi from scratch */
    1935        5564 :     S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
    1936        5564 :     S->nu = get_nu(S->chi, S->p, &l);
    1937        5564 :     if (l > 1) return l; /* we can get a decomposition */
    1938        5564 :     if (!update_phi(S)) return 1; /* unramified / irreducible */
    1939        5564 :     if (pia) break;
    1940         356 :   }
    1941       24794 :   *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       19264 : nilord(decomp_t *S, GEN dred, long flag)
    1947             : {
    1948       19264 :   GEN p = S->p;
    1949       19264 :   long oE, l, N  = degpol(S->f), v = varn(S->f);
    1950             :   GEN opa; /* t_INT or QX */
    1951             : 
    1952       19264 :   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       19264 :   S->psc = mulii(sqri(dred), p);
    1964       19264 :   S->vpsc= 2*S->df + 1;
    1965       19264 :   S->prc = mulii(dred, p);
    1966       19264 :   S->psf = S->psc;
    1967       19264 :   S->vpsf = S->vpsc;
    1968       19264 :   S->chi = FpX_red(S->f, S->psc);
    1969       19264 :   S->phi = pol_x(v);
    1970       19264 :   S->pmf = powiu(p, S->mf+1);
    1971       19264 :   S->precns = NULL;
    1972       19264 :   oE = 0;
    1973       19264 :   opa = NULL; /* -Wall */
    1974             :   for(;;)
    1975             :   {
    1976       24794 :     long Fa = degpol(S->nu);
    1977       24794 :     S->phi0 = NULL; /* no delayed composition */
    1978       24794 :     l = loop_init(S, &opa, &oE);
    1979       24794 :     if (l > 1) return Decomp(S,flag);
    1980       24794 :     if (l == 1) break;
    1981       24794 :     if (DEBUGLEVEL>4) err_printf("  (Fa, oE) = (%ld,%ld)\n", Fa, oE);
    1982       24794 :     if (oE*Fa == N)
    1983             :     { /* O = Zp[phi] */
    1984        6055 :       if (flag) return NULL;
    1985        5712 :       return dbasis(p, S->f, S->mf, redelt(S->phi,sqri(p),p), NULL);
    1986             :     }
    1987       18739 :     if (loop(S, oE)) return Decomp(S,flag);
    1988        9275 :     if (!update_phi(S)) break; /* unramified / irreducible */
    1989        5530 :   }
    1990        3745 :   if (flag) return NULL;
    1991        3640 :   S->nu = get_nu(S->chi, S->p, &l);
    1992        3640 :   return l != 1? Decomp(S,flag): dbasis(p, S->f, S->mf, S->phi, S->chi);
    1993             : }
    1994             : 
    1995             : static GEN
    1996       33467 : maxord_i(GEN p, GEN f, long mf, GEN w, long flag)
    1997             : {
    1998       33467 :   long l = lg(w)-1;
    1999       33467 :   GEN h = gel(w,l); /* largest factor */
    2000       33467 :   GEN D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
    2001             :   decomp_t S;
    2002             : 
    2003       33467 :   S.f = f;
    2004       33467 :   S.pisprime = -1;
    2005       33467 :   S.p = p;
    2006       33467 :   S.mf = mf;
    2007       33467 :   S.nu = h;
    2008       33467 :   S.df = Z_pval(D, p);
    2009       33467 :   S.pdf = powiu(p, S.df);
    2010       33467 :   if (l == 1) return nilord(&S, D, flag);
    2011       14203 :   if (flag && flag <= mf) flag = mf + 1;
    2012       14203 :   S.phi = pol_x(varn(f));
    2013       14203 :   S.chi = f; return Decomp(&S, flag);
    2014             : }
    2015             : 
    2016             : static int
    2017         980 : expo_is_squarefree(GEN e)
    2018             : {
    2019         980 :   long i, l = lg(e);
    2020        1232 :   for (i=1; i<l; i++)
    2021        1078 :     if (e[i] != 1) return 0;
    2022         154 :   return 1;
    2023             : }
    2024             : 
    2025             : /* assume f a ZX with leading_coeff 1, degree > 0 */
    2026             : GEN
    2027        1162 : ZpX_monic_factor(GEN f, GEN p, long prec)
    2028             : {
    2029             :   GEN poly, ex, P, E;
    2030             :   long l, i;
    2031             : 
    2032        1162 :   if (degpol(f) == 1) return mkmat2(mkcol(f), mkcol(gen_1));
    2033             : 
    2034         959 :   poly = ZX_squff(f,&ex); l = lg(poly);
    2035         959 :   P = cgetg(l, t_VEC);
    2036         959 :   E = cgetg(l, t_VEC);
    2037        1939 :   for (i = 1; i < l; i++)
    2038             :   {
    2039         980 :     pari_sp av1 = avma;
    2040         980 :     GEN fx = gel(poly,i), fa = FpX_factor(fx,p);
    2041         980 :     GEN w = gel(fa,1), e = gel(fa,2);
    2042         980 :     if (expo_is_squarefree(e))
    2043             :     { /* no repeated factors: Hensel lift */
    2044         154 :       GEN L = ZpX_liftfact(fx, w, powiu(p,prec), p, prec);
    2045         154 :       gel(P,i) = L; settyp(L, t_COL);
    2046         154 :       gel(E,i) = const_vec(lg(L)-1, utoipos(ex[i]));
    2047             :     }
    2048             :     else
    2049             :     { /* use Round 4 */
    2050         826 :       GEN M = maxord_i(p, fx, ZpX_disc_val(fx,p), w, prec);
    2051         826 :       if (M)
    2052             :       {
    2053         378 :         M = gerepilecopy(av1, M);
    2054         378 :         gel(P,i) = gel(M,1);
    2055         378 :         gel(E,i) = ZC_z_mul(gel(M,2), ex[i]);
    2056             :       }
    2057             :       else
    2058             :       { /* irreducible */
    2059         448 :         avma = av1;
    2060         448 :         gel(P,i) = mkcol(fx);
    2061         448 :         gel(E,i) = mkcols(ex[i]);
    2062             :       }
    2063             :     }
    2064             :   }
    2065         959 :   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        2423 : indexpartial(GEN T, GEN DT)
    2073             : {
    2074        2423 :   pari_sp av = avma;
    2075             :   long i, nb;
    2076        2423 :   GEN fa, E, P, res = gen_1, dT = ZX_deriv(T);
    2077             : 
    2078        2423 :   if (!DT) DT = ZX_disc(T);
    2079        2423 :   fa = absZ_factor_limit(DT, 0);
    2080        2423 :   P = gel(fa,1);
    2081        2423 :   E = gel(fa,2); nb = lg(P)-1;
    2082       14539 :   for (i = 1; i <= nb; i++)
    2083             :   {
    2084       12116 :     long e = itou(gel(E,i)), e2 = e >> 1;
    2085       12116 :     GEN p = gel(P,i), q = p;
    2086       12116 :     if (i == nb)
    2087        2409 :       q = powiu(p, (odd(e) && !BPSW_psp(p))? e2+1: e2);
    2088        9707 :     else if (e2 >= 2)
    2089        6015 :       q = ZpX_reduced_resultant_fast(T, dT, p, e2);
    2090       12116 :     res = mulii(res, q);
    2091             :   }
    2092        2423 :   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      586398 : get_norm(norm_S *S, GEN a)
    2110             : {
    2111      586398 :   if (S->M)
    2112             :   {
    2113             :     long e;
    2114       54002 :     GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
    2115       54002 :     if (e > -5) pari_err_PREC( "get_norm");
    2116       54002 :     return N;
    2117             :   }
    2118      532396 :   if (S->w) a = RgV_RgC_mul(S->w, a);
    2119      532396 :   return ZX_resultant_all(S->T, a, S->D, 0);
    2120             : }
    2121             : static void
    2122       16312 : init_norm(norm_S *S, GEN nf, GEN p)
    2123             : {
    2124       16312 :   GEN T = nf_get_pol(nf), M = nf_get_M(nf);
    2125       16312 :   long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
    2126             : 
    2127       16312 :   S->r1 = nf_get_r1(nf);
    2128       16312 :   if (N * ex <= prec2nbits(gprecision(M)) - 20)
    2129             :   { /* enough prec to use embed_norm */
    2130       16260 :     S->M = M;
    2131       16260 :     S->D = NULL;
    2132       16260 :     S->w = NULL;
    2133       16260 :     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       16312 : }
    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      586398 : is_uniformizer(GEN a, GEN q, norm_S *S)
    2158      586398 : { 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      136150 : mul_intersect(GEN x, GEN y, GEN p)
    2164             : {
    2165      136150 :   if (!x) return y;
    2166       93982 :   if (!y) return x;
    2167       79926 :   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       54754 : 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       54754 :   if (typ(pr) == t_MAT) return pr;
    2177             :   /* ordinary prid (from Kummer) */
    2178       14309 :   x = idealhnf_two(nf, pr);
    2179       14309 :   l = lg(x);
    2180       14309 :   y = cgetg(l, t_MAT);
    2181      157012 :   for (i=j=1; i<l; i++)
    2182      142703 :     if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
    2183       14309 :   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       16312 : get_LV(GEN nf, GEN L, GEN p, long N)
    2192             : {
    2193       16312 :   long i, l = lg(L)-1;
    2194             :   GEN LV, LW, A, B;
    2195             : 
    2196       16312 :   LV = cgetg(l+1, t_VEC);
    2197       16312 :   if (l == 1) { gel(LV,1) = matid(N); return LV; }
    2198       14056 :   LW = cgetg(l+1, t_VEC);
    2199       14056 :   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       14056 :   A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
    2203       14056 :   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       14056 :   B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
    2206       14056 :   for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
    2207       14056 :   for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
    2208       14056 :   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       42288 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
    2219             : {
    2220       42288 :   long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
    2221             :   GEN u, Mv, x, q;
    2222             : 
    2223       42288 :   f = N - m; /* we want v_p(Norm(x)) = p^f */
    2224       42288 :   q = powiu(p,f+1);
    2225             : 
    2226       42288 :   u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
    2227       42288 :   setlg(u, lg(P));
    2228       42288 :   u = centermod(ZM_ZC_mul(P, u), p);
    2229       42288 :   if (is_uniformizer(u, q, S)) return u;
    2230       11499 :   if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
    2231        8325 :     gel(u,1) = addii(gel(u,1), p); /* try u + p */
    2232             :   else
    2233        3174 :     gel(u,1) = subii(gel(u,1), p); /* try u - p */
    2234       11499 :   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        3754 :   Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
    2238        3754 :   l = lg(P);
    2239        8570 :   for (i=1; i<l; i++)
    2240             :   {
    2241        8570 :     x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
    2242        8570 :     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      727038 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
    2255      727038 : { 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      675622 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
    2260             : {
    2261      675622 :   GEN t, T = nf_get_pol(nf);
    2262      675622 :   long f = degpol(u), N = degpol(T);
    2263             : 
    2264      675622 :   if (f == N) /* inert */
    2265             :   {
    2266      116690 :     u = scalarcol_shallow(p,N);
    2267      116690 :     t = gen_1;
    2268             :   }
    2269             :   else
    2270             :   { /* make sure v_pr(u) = 1 (automatic if e>1) */
    2271      558932 :     t = poltobasis(nf, FpX_div(T,u,p));
    2272      558932 :     t = centermod(t, p);
    2273      558932 :     u = FpX_center(u, p, shifti(p,-1));
    2274      558932 :     if (e == 1)
    2275             :     {
    2276             :       norm_S S;
    2277      531751 :       S.D = S.w = S.M = NULL; S.T = T;
    2278      531751 :       if (!is_uniformizer(u, powiu(p,f+1), &S)) gel(u,2) = addii(gel(u,2), p);
    2279             :     }
    2280      558932 :     u = poltobasis(nf,u);
    2281      558932 :     t = zk_multable(nf, t); /* t never a scalar here since pr is not inert */
    2282             :   }
    2283      675622 :   return mk_pr(p,u,e,f,t);
    2284             : }
    2285             : 
    2286             : typedef struct {
    2287             :   GEN nf, p;
    2288             :   long I;
    2289             : } eltmod_muldata;
    2290             : 
    2291             : static GEN
    2292      173206 : sqr_mod(void *data, GEN x)
    2293             : {
    2294      173206 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2295      173206 :   return FpC_red(nfsqri(D->nf, x), D->p);
    2296             : }
    2297             : static GEN
    2298       80489 : ei_msqr_mod(void *data, GEN x)
    2299             : {
    2300       80489 :   GEN x2 = sqr_mod(data, x);
    2301       80489 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2302       80489 :   return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
    2303             : }
    2304             : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
    2305             : static GEN
    2306      109220 : pow_ei_mod_p(GEN nf, long I, GEN p)
    2307             : {
    2308      109220 :   pari_sp av = avma;
    2309             :   eltmod_muldata D;
    2310      109220 :   long N = nf_get_degree(nf);
    2311      109220 :   GEN y = col_ei(N,I);
    2312      109220 :   if (I == 1) return y;
    2313       92481 :   D.nf = nf;
    2314       92481 :   D.p = p;
    2315       92481 :   D.I = I;
    2316       92481 :   y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
    2317       92481 :   return gerepileupto(av,y);
    2318             : }
    2319             : 
    2320             : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
    2321             : static GEN
    2322       16312 : pradical(GEN nf, GEN p, GEN *phi)
    2323             : {
    2324       16312 :   long i, N = nf_get_degree(nf);
    2325             :   GEN q,m,frob,rad;
    2326             : 
    2327             :   /* matrix of Frob: x->x^p over Z_K/p */
    2328       16312 :   frob = cgetg(N+1,t_MAT);
    2329       16312 :   for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
    2330             : 
    2331       16312 :   m = frob; q = p;
    2332       16312 :   while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
    2333       16312 :   rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
    2334       16312 :   for (i=1; i<=N; i++) gcoeff(frob,i,i) = subis(gcoeff(frob,i,i), 1);
    2335       16312 :   *phi = frob; return rad;
    2336             : }
    2337             : 
    2338             : /* return powers of a: a^0, ... , a^d,  d = dim A */
    2339             : static GEN
    2340       27023 : get_powers(GEN mul, GEN p)
    2341             : {
    2342       27023 :   long i, d = lgcols(mul);
    2343       27023 :   GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
    2344             : 
    2345       27023 :   gel(P,0) = scalarcol_shallow(gen_1, d-1);
    2346       27023 :   z = gel(mul,1);
    2347      148252 :   for (i=1; i<=d; i++)
    2348             :   {
    2349      121229 :     gel(P,i) = z; /* a^i */
    2350      121229 :     if (i!=d) z = FpM_FpC_mul(mul, z, p);
    2351             :   }
    2352       27023 :   return pow;
    2353             : }
    2354             : 
    2355             : /* minimal polynomial of a in A (dim A = d).
    2356             :  * mul = multiplication table by a in A */
    2357             : static GEN
    2358       22221 : pol_min(GEN mul, GEN p)
    2359             : {
    2360       22221 :   pari_sp av = avma;
    2361       22221 :   GEN z = FpM_deplin(get_powers(mul, p), p);
    2362       22221 :   return gerepilecopy(av, RgV_to_RgX(z,0));
    2363             : }
    2364             : 
    2365             : static GEN
    2366       57010 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N)
    2367             : {
    2368             :   GEN u, t;
    2369             :   long e, f;
    2370             : 
    2371       57010 :   if (typ(P) == t_VEC) return P; /* already done (Kummer) */
    2372       42701 :   f = N - (lg(P)-1);
    2373             :   /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
    2374             :    * so that v_P(t) = e(P/p)-1 */
    2375       42701 :   if (f == N) {
    2376         413 :     u = scalarcol_shallow(p,N);
    2377         413 :     t = gen_1;
    2378         413 :     e = 1;
    2379             :   } else {
    2380       42288 :     u = uniformizer(nf, S, P, V, p, ramif);
    2381       42288 :     t = FpM_deplin(zk_multable(nf,u), p);
    2382       42288 :     e = ramif? 1 + ZC_nfval(nf,t,mk_pr(p,u,0,0,t)): 1;
    2383       42288 :     t = zk_scalar_or_multable(nf, t);
    2384             :   }
    2385       42701 :   return mk_pr(p,u,e,f,t);
    2386             : }
    2387             : 
    2388             : static GEN
    2389       16312 : primedec_end(GEN nf, GEN L, GEN p)
    2390             : {
    2391       16312 :   long i, l = lg(L), N = nf_get_degree(nf);
    2392       16312 :   GEN Lpr = cgetg(l, t_VEC);
    2393       16312 :   GEN LV = get_LV(nf, L,p,N);
    2394       16312 :   int ramif = dvdii(nf_get_disc(nf), p);
    2395       16312 :   norm_S S; init_norm(&S, nf, p);
    2396       73322 :   for (i=1; i<l; i++)
    2397       57010 :     gel(Lpr,i) = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N);
    2398       16312 :   return Lpr;
    2399             : }
    2400             : 
    2401             : /* prime ideal decomposition of p; if flim!=0, restrict to f(P,p) <= flim */
    2402             : static GEN
    2403      555488 : primedec_aux(GEN nf, GEN p, long flim)
    2404             : {
    2405      555488 :   GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
    2406             :   long i, k, c, iL, N;
    2407             :   int kummer;
    2408             : 
    2409      555488 :   F = FpX_factor(T, p);
    2410      555488 :   E = gel(F,2);
    2411      555488 :   F = gel(F,1);
    2412             : 
    2413      555488 :   k = lg(F); if (k == 1) errprime(p);
    2414      555488 :   if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
    2415             :   {
    2416      539176 :     L = cgetg(k,t_VEC);
    2417     1186686 :     for (i=1; i<k; i++)
    2418             :     {
    2419      870158 :       GEN t = gel(F,i);
    2420      870158 :       if (flim && degpol(t) > flim) { setlg(L, i); break; }
    2421      647510 :       gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
    2422             :     }
    2423      539176 :     return L;
    2424             :   }
    2425             : 
    2426       16312 :   kummer = 0;
    2427       16312 :   g = FpXV_prod(F, p);
    2428       16312 :   h = FpX_div(T,g,p);
    2429       16312 :   f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
    2430             : 
    2431       16312 :   N = degpol(T);
    2432       16312 :   L = cgetg(N+1,t_VEC); iL = 1;
    2433       55216 :   for (i=1; i<k; i++)
    2434       38904 :     if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
    2435       14309 :     {
    2436       14309 :       GEN t = gel(F,i);
    2437       14309 :       kummer = 1;
    2438       14309 :       gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
    2439             :     }
    2440             :     else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
    2441       24595 :       E[i] = 0;
    2442             : 
    2443             :   /* phi matrix of x -> x^p - x in algebra Z_K/p */
    2444       16312 :   Ip = pradical(nf,p,&phi);
    2445             : 
    2446             :   /* split etale algebra Z_K / (p,Ip) */
    2447       16312 :   h = cgetg(N+1,t_VEC);
    2448       16312 :   if (kummer)
    2449             :   { /* split off Kummer factors */
    2450        6869 :     GEN mb, b = NULL;
    2451       32355 :     for (i=1; i<k; i++)
    2452       25486 :       if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
    2453        6869 :     if (!b) errprime(p);
    2454        6869 :     b = FpC_red(poltobasis(nf,b), p);
    2455        6869 :     mb = FpM_red(zk_multable(nf,b), p);
    2456             :     /* Fp-base of ideal (Ip, b) in ZK/p */
    2457        6869 :     gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
    2458             :   }
    2459             :   else
    2460        9443 :     gel(h,1) = Ip;
    2461             : 
    2462       16312 :   UN = col_ei(N, 1);
    2463       44281 :   for (c=1; c; c--)
    2464             :   { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
    2465             :        H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
    2466       27969 :     GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
    2467       27969 :     long dim, r = lg(H)-1;
    2468             : 
    2469       27969 :     M   = FpM_suppl(shallowconcat(H,UN), p);
    2470       27969 :     Mi  = FpM_inv(M, p);
    2471       27969 :     M2  = vecslice(M, r+1,N); /* M = (H|M2) invertible */
    2472       27969 :     Mi2 = rowslice(Mi,r+1,N);
    2473             :     /* FIXME: FpM_mul(,M2) could be done with vecpermute */
    2474       27969 :     phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
    2475       27969 :     mat1 = FpM_ker(phi2, p);
    2476       27969 :     dim = lg(mat1)-1; /* A2 product of 'dim' fields */
    2477       27969 :     if (dim > 1)
    2478             :     { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
    2479       22221 :       GEN R, a, mula, mul2, v = gel(mat1,2);
    2480             :       long n;
    2481             : 
    2482       22221 :       a = FpM_FpC_mul(M2,v, p); /* not a scalar */
    2483       22221 :       mula = FpM_red(zk_multable(nf,a), p);
    2484       22221 :       mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
    2485       22221 :       R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
    2486       22221 :       n = lg(R)-1;
    2487       70831 :       for (i=1; i<=n; i++)
    2488             :       {
    2489       48610 :         GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
    2490       48610 :         gel(h,c++) = FpM_image(shallowconcat(H, I), p);
    2491             :       }
    2492       22221 :       if (n == dim)
    2493       16851 :         for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
    2494             :     }
    2495             :     else /* A2 field ==> H maximal, f = N-r = dim(A2) */
    2496        5748 :       gel(L,iL++) = H;
    2497             :   }
    2498       16312 :   setlg(L, iL);
    2499       16312 :   L = primedec_end(nf, L, p);
    2500       16312 :   if (flim)
    2501             :   {
    2502        1641 :     long k = 1;
    2503        5540 :     for(i = 1; i < iL; i++)
    2504             :     {
    2505        3899 :       GEN P = gel(L,i);
    2506        3899 :       if (pr_get_f(P) <= flim) gel(L,k++) = P;
    2507             :     }
    2508        1641 :     setlg(L,k);
    2509             :   }
    2510       16312 :   return L;
    2511             : }
    2512             : 
    2513             : GEN
    2514      555488 : idealprimedec_limit_f(GEN nf, GEN p, long f)
    2515             : {
    2516      555488 :   pari_sp av = avma;
    2517             :   GEN v;
    2518      555488 :   if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
    2519      555488 :   v = primedec_aux(checknf(nf), p, f);
    2520      555488 :   v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
    2521      555488 :   return gerepileupto(av,v);
    2522             : }
    2523             : GEN
    2524      315700 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
    2525      315700 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
    2526             : GEN
    2527      112490 : idealprimedec(GEN nf, GEN p)
    2528      112490 : { return idealprimedec_limit_f(nf, p, 0); }
    2529             : GEN
    2530         343 : nf_pV_to_prV(GEN nf, GEN P)
    2531             : {
    2532             :   long i, l;
    2533         343 :   GEN Q = cgetg_copy(P,&l);
    2534         343 :   if (l == 1) return Q;
    2535         301 :   for (i = 1; i < l; i++) gel(Q,i) = idealprimedec(nf, gel(P,i));
    2536         301 :   return shallowconcat1(Q);
    2537             : }
    2538             : 
    2539             : /* return [Fp[x]: Fp] */
    2540             : static long
    2541         434 : ffdegree(GEN x, GEN frob, GEN p)
    2542             : {
    2543         434 :   pari_sp av = avma;
    2544         434 :   long d, f = lg(frob)-1;
    2545         434 :   GEN y = x;
    2546             : 
    2547        1750 :   for (d=1; d < f; d++)
    2548             :   {
    2549        1323 :     y = FpM_FpC_mul(frob, y, p);
    2550        1323 :     if (ZV_equal(y, x)) break;
    2551             :   }
    2552         434 :   avma = av; return d;
    2553             : }
    2554             : 
    2555             : static GEN
    2556       11648 : lift_to_zk(GEN v, GEN c, long N)
    2557             : {
    2558       11648 :   GEN w = zerocol(N);
    2559       11648 :   long i, l = lg(c);
    2560       11648 :   for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
    2561       11648 :   return w;
    2562             : }
    2563             : 
    2564             : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
    2565             : static GEN
    2566      683741 : anti_uniformizer(GEN nf, GEN pr)
    2567             : {
    2568      683741 :   long N = nf_get_degree(nf), e = pr_get_e(pr);
    2569             :   GEN p, b, z;
    2570             : 
    2571      683741 :   if (e * pr_get_f(pr) == N) return gen_1;
    2572      300598 :   p = pr_get_p(pr);
    2573      300598 :   b = pr_get_tau(pr); /* ZM */
    2574      300598 :   if (e != 1)
    2575             :   {
    2576       23786 :     GEN q = powiu(pr_get_p(pr), e-1);
    2577       23786 :     b = ZM_Z_divexact(ZM_powu(b,e), q);
    2578             :   }
    2579             :   /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
    2580      300598 :   z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
    2581      300598 :   z = idealaddtoone_i(nf, pr, z);
    2582      300598 :   return Z_ZC_sub(gen_1, z);
    2583             : }
    2584             : 
    2585             : #define mpr_TAU 1
    2586             : #define mpr_FFP 2
    2587             : #define mpr_NFP 5
    2588             : #define SMALLMODPR 4
    2589             : #define LARGEMODPR 6
    2590             : static GEN
    2591      685771 : modpr_TAU(GEN modpr)
    2592             : {
    2593      685771 :   GEN tau = gel(modpr,mpr_TAU);
    2594      685771 :   return isintzero(tau)? NULL: tau;
    2595             : }
    2596             : 
    2597             : /* prh = HNF matrix, which is identity but for the first line. Return a
    2598             :  * projector to ZK / prh ~ Z/prh[1,1] */
    2599             : GEN
    2600      575865 : dim1proj(GEN prh)
    2601             : {
    2602      575865 :   long i, N = lg(prh)-1;
    2603      575865 :   GEN ffproj = cgetg(N+1, t_VEC);
    2604      575865 :   GEN x, q = gcoeff(prh,1,1);
    2605      575865 :   gel(ffproj,1) = gen_1;
    2606     1080491 :   for (i=2; i<=N; i++)
    2607             :   {
    2608      504626 :     x = gcoeff(prh,1,i);
    2609      504626 :     if (signe(x)) x = subii(q,x);
    2610      504626 :     gel(ffproj,i) = x;
    2611             :   }
    2612      575865 :   return ffproj;
    2613             : }
    2614             : 
    2615             : /* p not necessarily prime, but coprime to denom(basis) */
    2616             : GEN
    2617      129400 : get_proj_modT(GEN basis, GEN T, GEN p)
    2618             : {
    2619      129400 :   long i, l = lg(basis), f = degpol(T);
    2620      129400 :   GEN z = cgetg(l, t_MAT);
    2621      560428 :   for (i = 1; i < l; i++)
    2622             :   {
    2623      431028 :     GEN cx, w = gel(basis,i);
    2624      431028 :     if (typ(w) == t_INT)
    2625           0 :       w = scalarcol_shallow(w, f);
    2626             :     else
    2627             :     {
    2628      431028 :       w = Q_primitive_part(w, &cx);
    2629      431028 :       w = FpXQ_red(w, T, p);
    2630      431028 :       if (cx) w = FpX_Fp_mul(w, Rg_to_Fp(cx, p), p);
    2631      431028 :       w = RgX_to_RgC(w, f);
    2632             :     }
    2633      431028 :     gel(z,i) = w; /* w_i mod (T,p) */
    2634             :   }
    2635      129400 :   return z;
    2636             : }
    2637             : 
    2638             : /* initialize reduction mod pr; if zk = 1, will only init data required to
    2639             :  * reduce *integral* element.  Realize (O_K/pr) as Fp[X] / (T), for a
    2640             :  * *monic* T */
    2641             : static GEN
    2642      708336 : modprinit(GEN nf, GEN pr, int zk)
    2643             : {
    2644      708336 :   pari_sp av = avma;
    2645             :   GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
    2646             :   long N, i, k, f;
    2647             : 
    2648      708336 :   nf = checknf(nf); checkprid(pr);
    2649      708329 :   f = pr_get_f(pr);
    2650      708329 :   N = nf_get_degree(nf);
    2651      708329 :   prh = idealhnf_two(nf, pr);
    2652      708329 :   tau = zk? gen_0: anti_uniformizer(nf, pr);
    2653      708329 :   p = pr_get_p(pr);
    2654             : 
    2655      708329 :   if (f == 1)
    2656             :   {
    2657      574302 :     res = cgetg(SMALLMODPR, t_COL);
    2658      574302 :     gel(res,mpr_TAU) = tau;
    2659      574302 :     gel(res,mpr_FFP) = dim1proj(prh);
    2660      574302 :     gel(res,3) = pr; return gerepilecopy(av, res);
    2661             :   }
    2662             : 
    2663      134027 :   c = cgetg(f+1, t_VECSMALL);
    2664      134027 :   ffproj = cgetg(N+1, t_MAT);
    2665      880307 :   for (k=i=1; i<=N; i++)
    2666             :   {
    2667      746280 :     x = gcoeff(prh, i,i);
    2668      746280 :     if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
    2669             :     else
    2670      308077 :       gel(ffproj,i) = ZC_neg(gel(prh,i));
    2671             :   }
    2672      134027 :   ffproj = rowpermute(ffproj, c);
    2673      134027 :   if (! dvdii(nf_get_index(nf), p))
    2674             :   {
    2675      129225 :     GEN basis = nf_get_zk(nf);
    2676      129225 :     if (N == f) T = nf_get_pol(nf); /* pr inert */
    2677             :     else
    2678             :     {
    2679       72800 :       T = RgV_RgC_mul(Q_primpart(basis), pr_get_gen(pr));
    2680       72800 :       T = FpX_normalize(T,p);
    2681       72800 :       basis = vecpermute(basis, c);
    2682             :     }
    2683      129225 :     T = FpX_red(T, p);
    2684      129225 :     ffproj = FpM_mul(get_proj_modT(basis, T, p), ffproj, p);
    2685             : 
    2686      129225 :     res = cgetg(SMALLMODPR+1, t_COL);
    2687      129225 :     gel(res,mpr_TAU) = tau;
    2688      129225 :     gel(res,mpr_FFP) = ffproj;
    2689      129225 :     gel(res,3) = pr;
    2690      129225 :     gel(res,4) = T; return gerepilecopy(av, res);
    2691             :   }
    2692             : 
    2693        4802 :   if (uisprime(f))
    2694             :   {
    2695        4375 :     mul = ei_multable(nf, c[2]);
    2696        4375 :     mul = vecpermute(mul, c);
    2697             :   }
    2698             :   else
    2699             :   {
    2700             :     GEN v, u, u2, frob;
    2701             :     long deg,deg1,deg2;
    2702             : 
    2703             :     /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
    2704         427 :     frob = cgetg(f+1, t_MAT);
    2705        2163 :     for (i=1; i<=f; i++)
    2706             :     {
    2707        1736 :       x = pow_ei_mod_p(nf,c[i],p);
    2708        1736 :       gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
    2709             :     }
    2710         427 :     u = col_ei(f,2); k = 2;
    2711         427 :     deg1 = ffdegree(u, frob, p);
    2712         861 :     while (deg1 < f)
    2713             :     {
    2714           7 :       k++; u2 = col_ei(f, k);
    2715           7 :       deg2 = ffdegree(u2, frob, p);
    2716           7 :       deg = clcm(deg1,deg2);
    2717           7 :       if (deg == deg1) continue;
    2718           7 :       if (deg == deg2) { deg1 = deg2; u = u2; continue; }
    2719           0 :       u = ZC_add(u, u2);
    2720           0 :       while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
    2721           0 :       deg1 = deg;
    2722             :     }
    2723         427 :     v = lift_to_zk(u,c,N);
    2724             : 
    2725         427 :     mul = cgetg(f+1,t_MAT);
    2726         427 :     gel(mul,1) = v; /* assume w_1 = 1 */
    2727         427 :     for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
    2728             :   }
    2729             : 
    2730             :   /* Z_K/pr = Fp(v), mul = mul by v */
    2731        4802 :   mul = FpM_red(mul, p);
    2732        4802 :   mul = FpM_mul(ffproj, mul, p);
    2733             : 
    2734        4802 :   pow = get_powers(mul, p);
    2735        4802 :   T = RgV_to_RgX(FpM_deplin(pow, p), nf_get_varn(nf));
    2736        4802 :   nfproj = cgetg(f+1, t_MAT);
    2737        4802 :   for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
    2738        4802 :   nfproj = coltoliftalg(nf, nfproj);
    2739             : 
    2740        4802 :   setlg(pow, f+1);
    2741        4802 :   ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
    2742             : 
    2743        4802 :   res = cgetg(LARGEMODPR, t_COL);
    2744        4802 :   gel(res,mpr_TAU) = tau;
    2745        4802 :   gel(res,mpr_FFP) = ffproj;
    2746        4802 :   gel(res,3) = pr;
    2747        4802 :   gel(res,4) = T;
    2748        4802 :   gel(res,mpr_NFP) = nfproj; return gerepilecopy(av, res);
    2749             : }
    2750             : 
    2751             : GEN
    2752          49 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0); }
    2753             : GEN
    2754        5260 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1); }
    2755             : 
    2756             : /* x may be a modpr */
    2757             : static int
    2758      550137 : ok_modpr(GEN x)
    2759      550137 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
    2760             : void
    2761         182 : checkmodpr(GEN x)
    2762             : {
    2763         182 :   if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
    2764         182 :   checkprid(modpr_get_pr(x));
    2765         182 : }
    2766             : GEN
    2767        2982 : get_modpr(GEN x)
    2768        2982 : { return ok_modpr(x)? x: NULL; }
    2769             : 
    2770             : int
    2771     3156135 : checkprid_i(GEN x)
    2772             : {
    2773     8954129 :   return (typ(x) == t_VEC && lg(x) == 6
    2774     2605648 :           && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
    2775     5761734 :           && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
    2776             : }
    2777             : void
    2778     2497904 : checkprid(GEN x)
    2779     2497904 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
    2780             : GEN
    2781      645862 : get_prid(GEN x)
    2782             : {
    2783      645862 :   long lx = lg(x);
    2784      645862 :   if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
    2785      645862 :   if (checkprid_i(x)) return x;
    2786      546973 :   if (ok_modpr(x)) {
    2787        2527 :     x = modpr_get_pr(x);
    2788        2527 :     if (checkprid_i(x)) return x;
    2789             :   }
    2790      544446 :   return NULL;
    2791             : }
    2792             : 
    2793             : static GEN
    2794      704868 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
    2795             : {
    2796      704868 :   GEN modpr = (typ(*pr) == t_COL)? *pr: modprinit(nf, *pr, zk);
    2797      704861 :   *T = modpr_get_T(modpr);
    2798      704861 :   *pr = modpr_get_pr(modpr);
    2799      704861 :   *p = pr_get_p(*pr); return modpr;
    2800             : }
    2801             : 
    2802             : /* Return an element of O_K which is set to x Mod T */
    2803             : GEN
    2804        1848 : modpr_genFq(GEN modpr)
    2805             : {
    2806        1848 :   switch(lg(modpr))
    2807             :   {
    2808             :     case SMALLMODPR: /* Fp */
    2809          28 :       return gen_1;
    2810             :     case LARGEMODPR:  /* painful case, p \mid index */
    2811         112 :       return gmael(modpr,mpr_NFP, 2);
    2812             :     default: /* trivial case : p \nmid index */
    2813             :     {
    2814        1708 :       long v = varn( modpr_get_T(modpr) );
    2815        1708 :       return pol_x(v);
    2816             :     }
    2817             :   }
    2818             : }
    2819             : 
    2820             : GEN
    2821      685540 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2822      685540 :   GEN modpr = to_ff_init(nf,pr,T,p,0);
    2823      685533 :   GEN tau = modpr_TAU(modpr);
    2824      685533 :   if (!tau) gel(modpr,mpr_TAU) = anti_uniformizer(nf, *pr);
    2825      685533 :   return modpr;
    2826             : }
    2827             : GEN
    2828       19328 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2829       19328 :   return to_ff_init(nf,pr,T,p,1);
    2830             : }
    2831             : 
    2832             : /* assume x in 'basis' form (t_COL) */
    2833             : GEN
    2834      646805 : zk_to_Fq(GEN x, GEN modpr)
    2835             : {
    2836      646805 :   GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2837      646805 :   GEN ffproj = gel(modpr,mpr_FFP);
    2838      646805 :   GEN T = modpr_get_T(modpr);
    2839      646805 :   return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
    2840             : }
    2841             : 
    2842             : /* REDUCTION Modulo a prime ideal */
    2843             : 
    2844             : /* nf a true nf */
    2845             : static GEN
    2846     4733978 : Rg_to_ff(GEN nf, GEN x0, GEN modpr)
    2847             : {
    2848     4733978 :   GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2849     4733978 :   long tx = typ(x);
    2850             : 
    2851     4733978 :   if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
    2852     4733978 :   switch(tx)
    2853             :   {
    2854     4088800 :     case t_INT: return modii(x, p);
    2855        2401 :     case t_FRAC: return Rg_to_Fp(x, p);
    2856             :     case t_POL:
    2857       17879 :       switch(lg(x))
    2858             :       {
    2859         147 :         case 2: return gen_0;
    2860         399 :         case 3: return Rg_to_Fp(gel(x,2), p);
    2861             :       }
    2862       17333 :       x = Q_remove_denom(x, &den);
    2863       17333 :       x = poltobasis(nf, x);
    2864             :       /* content(x) and den may not be coprime */
    2865       17277 :       break;
    2866             :     case t_COL:
    2867      624898 :       x = Q_remove_denom(x, &den);
    2868             :       /* content(x) and den are coprime */
    2869      624898 :       if (lg(x) == lg(nf_get_zk(nf))) break;
    2870          56 :     default: pari_err_TYPE("Rg_to_ff",x);
    2871           0 :       return NULL;
    2872             :   }
    2873      642119 :   if (den)
    2874             :   {
    2875       27654 :     long v = Z_pvalrem(den, p, &den);
    2876       27654 :     if (v)
    2877             :     {
    2878         371 :       if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
    2879             :       /* now v = valuation(true denominator of x) */
    2880         371 :       if (v > 0)
    2881             :       {
    2882         238 :         GEN tau = modpr_TAU(modpr);
    2883         238 :         if (!tau) pari_err_TYPE("zk_to_ff", x0);
    2884         238 :         x = nfmuli(nf,x, nfpow_u(nf, tau, v));
    2885         238 :         v -= ZV_pvalrem(x, p, &x);
    2886             :       }
    2887         371 :       if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2888         343 :       if (v) return gen_0;
    2889         329 :       if (is_pm1(den)) den = NULL;
    2890             :     }
    2891       27612 :     x = FpC_red(x, p);
    2892             :   }
    2893      642077 :   x = zk_to_Fq(x, modpr);
    2894      642077 :   if (den)
    2895             :   {
    2896       27444 :     GEN c = Fp_inv(den, p);
    2897       27444 :     x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
    2898             :   }
    2899      642077 :   return x;
    2900             : }
    2901             : 
    2902             : GEN
    2903         182 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
    2904             : {
    2905         182 :   pari_sp av = avma;
    2906         182 :   nf = checknf(nf); checkmodpr(modpr);
    2907         182 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(Rg_to_ff(nf,x,modpr),modpr)));
    2908             : }
    2909             : 
    2910             : GEN
    2911         238 : nfmodpr(GEN nf, GEN x, GEN pr)
    2912             : {
    2913         238 :   pari_sp av = avma;
    2914             :   GEN T, p, modpr;
    2915         238 :   nf = checknf(nf);
    2916         238 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2917         238 :   x = Rg_to_ff(nf, x, modpr);
    2918         154 :   x = Fq_to_FF(x, Tp_to_FF(T,p));
    2919         154 :   return gerepilecopy(av, x);
    2920             : }
    2921             : GEN
    2922          56 : nfmodprlift(GEN nf, GEN x, GEN pr)
    2923             : {
    2924          56 :   pari_sp av = avma;
    2925             :   GEN y, T, p, modpr;
    2926             :   long i, l, d;
    2927          56 :   nf = checknf(nf);
    2928          56 :   switch(typ(x))
    2929             :   {
    2930           0 :     case t_INT: return icopy(x);
    2931          28 :     case t_FFELT: break;
    2932             :     case t_VEC: case t_COL: case t_MAT:
    2933          28 :       y = cgetg_copy(x,&l);
    2934          28 :       for (i = 1; i < l; i++) gel(y,i) = nfmodprlift(nf,gel(x,i),pr);
    2935          28 :       return y;
    2936           0 :     default: pari_err_TYPE("nfmodprlit",x);
    2937             :   }
    2938          28 :   x = FF_to_FpXQ_i(x);
    2939          28 :   d = degpol(x);
    2940          28 :   if (d <= 0) { avma = av; return d? gen_0: icopy(gel(x,2)); }
    2941           0 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2942           0 :   return gerepilecopy(av, Fq_to_nf(x, modpr));
    2943             : }
    2944             : 
    2945             : /* lift A from residue field to nf */
    2946             : GEN
    2947     1174782 : Fq_to_nf(GEN A, GEN modpr)
    2948             : {
    2949             :   long dA;
    2950     1174782 :   if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
    2951         266 :   dA = degpol(A);
    2952         266 :   if (dA <= 0) return dA ? gen_0: gel(A,2);
    2953         259 :   return mulmat_pol(gel(modpr,mpr_NFP), A);
    2954             : }
    2955             : GEN
    2956           0 : FqV_to_nfV(GEN A, GEN modpr)
    2957             : {
    2958           0 :   long i,l = lg(A);
    2959           0 :   GEN B = cgetg(l,typ(A));
    2960           0 :   for (i=1; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    2961           0 :   return B;
    2962             : }
    2963             : GEN
    2964        1260 : FqM_to_nfM(GEN A, GEN modpr)
    2965             : {
    2966        1260 :   long i,j,h,l = lg(A);
    2967        1260 :   GEN B = cgetg(l, t_MAT);
    2968             : 
    2969        1260 :   if (l == 1) return B;
    2970        1057 :   h = lgcols(A);
    2971        4774 :   for (j=1; j<l; j++)
    2972             :   {
    2973        3717 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    2974        3717 :     for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
    2975             :   }
    2976        1057 :   return B;
    2977             : }
    2978             : GEN
    2979        4095 : FqX_to_nfX(GEN A, GEN modpr)
    2980             : {
    2981             :   long i, l;
    2982             :   GEN B;
    2983             : 
    2984        4095 :   if (typ(A)!=t_POL) return icopy(A); /* scalar */
    2985        4095 :   B = cgetg_copy(A, &l); B[1] = A[1];
    2986        4095 :   for (i=2; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    2987        4095 :   return B;
    2988             : }
    2989             : 
    2990             : /* reduce A to residue field */
    2991             : GEN
    2992     4733558 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
    2993             : {
    2994     4733558 :   pari_sp av = avma;
    2995     4733558 :   return gerepileupto(av, Rg_to_ff(checknf(nf), A, modpr));
    2996             : }
    2997             : /* A t_VEC/t_COL */
    2998             : GEN
    2999        3749 : nfV_to_FqV(GEN A, GEN nf,GEN modpr)
    3000             : {
    3001        3749 :   long i,l = lg(A);
    3002        3749 :   GEN B = cgetg(l,typ(A));
    3003        3749 :   for (i=1; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i), modpr);
    3004        3749 :   return B;
    3005             : }
    3006             : /* A  t_MAT */
    3007             : GEN
    3008         714 : nfM_to_FqM(GEN A, GEN nf,GEN modpr)
    3009             : {
    3010         714 :   long i,j,h,l = lg(A);
    3011         714 :   GEN B = cgetg(l,t_MAT);
    3012             : 
    3013         714 :   if (l == 1) return B;
    3014         714 :   h = lgcols(A);
    3015       18158 :   for (j=1; j<l; j++)
    3016             :   {
    3017       17444 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    3018       17444 :     for (i=1; i<h; i++) gel(Bj,i) = nf_to_Fq(nf, gel(Aj,i), modpr);
    3019             :   }
    3020         714 :   return B;
    3021             : }
    3022             : /* A t_POL */
    3023             : GEN
    3024       13251 : nfX_to_FqX(GEN A, GEN nf,GEN modpr)
    3025             : {
    3026       13251 :   long i,l = lg(A);
    3027       13251 :   GEN B = cgetg(l,t_POL); B[1] = A[1];
    3028       13251 :   for (i=2; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i),modpr);
    3029       13251 :   return normalizepol_lg(B, l);
    3030             : }
    3031             : 
    3032             : /*******************************************************************/
    3033             : /*                                                                 */
    3034             : /*                       RELATIVE ROUND 2                          */
    3035             : /*                                                                 */
    3036             : /*******************************************************************/
    3037             : 
    3038             : static void
    3039        2100 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
    3040             : {
    3041             :   long i;
    3042        2100 :   if (typ(Ix) == t_VEC) /* standard */
    3043        1470 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
    3044             :   else /* constant ideal */
    3045         630 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
    3046        2100 : }
    3047             : 
    3048             : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
    3049             :  * module generated by x and y. */
    3050             : static GEN
    3051        1050 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
    3052             : {
    3053        1050 :   long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
    3054        1050 :   GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
    3055        1050 :   fill(lx, H     , Hx, I     , Ix);
    3056        1050 :   fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
    3057             : }
    3058             : static GEN
    3059        1596 : rnfjoinmodules(GEN nf, GEN x, GEN y)
    3060             : {
    3061        1596 :   if (!x) return y;
    3062         511 :   if (!y) return x;
    3063         420 :   return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
    3064             : }
    3065             : 
    3066             : typedef struct {
    3067             :   GEN multab, T,p;
    3068             :   long h;
    3069             : } rnfeltmod_muldata;
    3070             : 
    3071             : static GEN
    3072        8722 : _sqr(void *data, GEN x)
    3073             : {
    3074        8722 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3075       14952 :   GEN z = x? tablesqr(D->multab,x)
    3076       14952 :            : tablemul_ei_ej(D->multab,D->h,D->h);
    3077        8722 :   return FqV_red(z,D->T,D->p);
    3078             : }
    3079             : static GEN
    3080        3255 : _msqr(void *data, GEN x)
    3081             : {
    3082        3255 :   GEN x2 = _sqr(data, x), z;
    3083        3255 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3084        3255 :   z = tablemul_ei(D->multab, x2, D->h);
    3085        3255 :   return FqV_red(z,D->T,D->p);
    3086             : }
    3087             : 
    3088             : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
    3089             : static GEN
    3090        2492 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
    3091             : {
    3092        2492 :   pari_sp av = avma;
    3093             :   GEN y;
    3094             :   rnfeltmod_muldata D;
    3095             : 
    3096        2492 :   if (!signe(n)) return gen_1;
    3097             : 
    3098        2492 :   D.multab = multab;
    3099        2492 :   D.h = h;
    3100        2492 :   D.T = T;
    3101        2492 :   D.p = p;
    3102        2492 :   y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
    3103        2492 :   return gerepilecopy(av, y);
    3104             : }
    3105             : 
    3106             : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
    3107             :  * a root, cf repres() */
    3108             : static GEN
    3109          21 : FqX_non_root(GEN P, GEN T, GEN p)
    3110             : {
    3111          21 :   long dP = degpol(P), f, vT;
    3112             :   long i, j, k, pi, pp;
    3113             :   GEN v;
    3114             : 
    3115          21 :   if (dP == 0) return gen_1;
    3116          21 :   pp = is_bigint(p) ? dP+1: itos(p);
    3117          21 :   v = cgetg(dP + 2, t_VEC);
    3118          21 :   gel(v,1) = gen_0;
    3119          21 :   if (T)
    3120           0 :   { f = degpol(T); vT = varn(T); }
    3121             :   else
    3122          21 :   { f = 1; vT = 0; }
    3123          42 :   for (i=pi=1; i<=f; i++,pi*=pp)
    3124             :   {
    3125          21 :     GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
    3126          42 :     for (j=1; j<pp; j++)
    3127             :     {
    3128          42 :       for (k=1; k<=pi; k++)
    3129             :       {
    3130          21 :         GEN z = Fq_add(gel(v,k), jgi, T,p);
    3131          21 :         if (!gequal0(FqX_eval(P, z, T,p))) return z;
    3132          21 :         gel(v, j*pi+k) = z;
    3133             :       }
    3134          21 :       if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
    3135             :     }
    3136             :   }
    3137          21 :   return NULL;
    3138             : }
    3139             : 
    3140             : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
    3141             :  * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
    3142             :  * vdisc = v_pr( disc(P) ).
    3143             :  * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
    3144             :  *   O = enlarged order, given by a pseudo-basis
    3145             :  *   flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
    3146             :  *   v = v_pr(disc(O)). */
    3147             : static GEN
    3148        1673 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
    3149             : {
    3150             :   GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
    3151             :   long m, vt, r, d, i, j, mpr;
    3152             : 
    3153        1673 :   if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
    3154        1666 :   if (vdisc == 1) return NULL; /* pr-maximal */
    3155        1666 :   if (!only_maximal && !gequal1(leading_coeff(P)))
    3156           0 :     pari_err_IMPL( "the full Dedekind criterion in the non-monic case");
    3157             :   /* either monic OR only_maximal = 1 */
    3158        1666 :   m = degpol(P);
    3159        1666 :   nfT = nf_get_pol(nf);
    3160        1666 :   modpr = nf_to_Fq_init(nf,&pr, &T, &p);
    3161        1666 :   Ppr = nfX_to_FqX(P, nf, modpr);
    3162        1666 :   mpr = degpol(Ppr);
    3163        1666 :   if (mpr < m) /* non-monic => only_maximal = 1 */
    3164             :   {
    3165          21 :     if (mpr < 0) return NULL;
    3166          21 :     if (! RgX_valrem(Ppr, &Ppr))
    3167             :     { /* non-zero constant coefficient */
    3168           0 :       Ppr = RgX_shift_shallow(RgX_recip_shallow(Ppr), m - mpr);
    3169           0 :       P = RgX_recip_shallow(P);
    3170             :     }
    3171             :     else
    3172             :     {
    3173          21 :       GEN z = FqX_non_root(Ppr, T, p);
    3174          21 :       if (!z) pari_err_IMPL( "Dedekind in the difficult case");
    3175           0 :       z = Fq_to_nf(z, modpr);
    3176           0 :       if (typ(z) == t_INT)
    3177           0 :         P = RgX_translate(P, z);
    3178             :       else
    3179           0 :         P = RgXQX_translate(P, z, T);
    3180           0 :       P = RgX_recip_shallow(P);
    3181           0 :       Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
    3182             :     }
    3183             :   }
    3184        1645 :   A = gel(FqX_factor(Ppr,T,p),1);
    3185        1645 :   r = lg(A); /* > 1 */
    3186        1645 :   g = gel(A,1);
    3187        1645 :   for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
    3188        1645 :   h = FqX_div(Ppr,g, T, p);
    3189        1645 :   gzk = FqX_to_nfX(g, modpr);
    3190        1645 :   hzk = FqX_to_nfX(h, modpr);
    3191             : 
    3192        1645 :   k = gsub(P, RgXQX_mul(gzk,hzk, nfT));
    3193        1645 :   tau = pr_get_tau(pr);
    3194        1645 :   switch(typ(tau))
    3195             :   {
    3196         903 :     case t_INT: k = gdiv(k, p); break;
    3197             :     case t_MAT:
    3198         742 :       k = RgX_to_nfX(nf, k);
    3199         742 :       k = RgX_Rg_div(tablemulvec(NULL,tau, k), p);
    3200         742 :       break;
    3201             :     case t_COL:
    3202           0 :       tau = coltoliftalg(nf, tau);
    3203           0 :       k = RgX_Rg_div(RgXQX_RgXQ_mul(k, tau, nfT), p);
    3204           0 :       break;
    3205             :   }
    3206        1645 :   k = nfX_to_FqX(k, nf, modpr);
    3207        1645 :   k = FqX_normalize(FqX_gcd(FqX_gcd(g,h,  T,p), k, T,p), T,p);
    3208        1645 :   d = degpol(k);  /* <= m */
    3209        1645 :   if (!d) return NULL; /* pr-maximal */
    3210         819 :   if (only_maximal) return gen_0; /* not maximal */
    3211             : 
    3212         798 :   A = cgetg(m+d+1,t_MAT);
    3213         798 :   I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
    3214             :  /* base[2] temporarily multiplied by p, for the final nfhnfmod,
    3215             :   * which requires integral ideals */
    3216         798 :   prinvp = pr_inv_p(pr); /* again multiplied by p */
    3217        4830 :   for (j=1; j<=m; j++)
    3218             :   {
    3219        4032 :     gel(A,j) = col_ei(m, j);
    3220        4032 :     gel(I,j) = p;
    3221             :   }
    3222         798 :   pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
    3223        1764 :   for (   ; j<=m+d; j++)
    3224             :   {
    3225         966 :     gel(A,j) = RgX_to_RgC(pal,m);
    3226         966 :     gel(I,j) = prinvp;
    3227         966 :     if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
    3228             :   }
    3229             :   /* the modulus is integral */
    3230         798 :   base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
    3231         798 :   gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
    3232         798 :   vt = vdisc - 2*d;
    3233         798 :   return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
    3234             : }
    3235             : 
    3236             : /* [L:K] = n */
    3237             : static GEN
    3238         742 : triv_order(long n)
    3239             : {
    3240         742 :   GEN z = cgetg(3, t_VEC);
    3241         742 :   gel(z,1) = matid(n);
    3242         742 :   gel(z,2) = const_vec(n, gen_1); return z;
    3243             : }
    3244             : 
    3245             : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
    3246             :  * is pr-maximal (resp. not pr-maximal). */
    3247             : GEN
    3248          77 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
    3249             : {
    3250          77 :   pari_sp av = avma;
    3251             :   GEN z, dP;
    3252             :   long v;
    3253             : 
    3254          77 :   nf = checknf(nf);
    3255          77 :   P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 0);
    3256          77 :   dP = RgX_disc(P); P = lift_shallow(P);
    3257          77 :   if (!pr)
    3258             :   {
    3259          21 :     GEN fa = idealfactor(nf, dP);
    3260          21 :     GEN Q = gel(fa,1), E = gel(fa,2);
    3261          21 :     pari_sp av2 = avma;
    3262          21 :     long i, l = lg(Q);
    3263          21 :     for (i = 1; i < l; i++, avma = av2)
    3264             :     {
    3265          21 :       v = itos(gel(E,i));
    3266          21 :       if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { avma=av; return gen_0; }
    3267           0 :       avma = av2;
    3268             :     }
    3269           0 :     avma = av; return gen_1;
    3270             :   }
    3271          56 :   else if (typ(pr) == t_VEC)
    3272             :   { /* flag = 1 is implicit */
    3273          56 :     if (lg(pr) == 1) { avma = av; return gen_1; }
    3274          56 :     if (typ(gel(pr,1)) == t_VEC)
    3275             :     { /* list of primes */
    3276          14 :       GEN Q = pr;
    3277          14 :       pari_sp av2 = avma;
    3278          14 :       long i, l = lg(Q);
    3279          14 :       for (i = 1; i < l; i++, avma = av2)
    3280             :       {
    3281          14 :         v = nfval(nf, dP, gel(Q,i));
    3282          14 :         if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { avma=av; return gen_0; }
    3283             :       }
    3284           0 :       avma = av; return gen_1;
    3285             :     }
    3286             :   }
    3287             :   /* single prime */
    3288          42 :   v = nfval(nf, dP, pr);
    3289          42 :   z = rnfdedekind_i(nf, P, pr, v, flag);
    3290          35 :   if (z)
    3291             :   {
    3292          14 :     if (flag) { avma = av; return gen_0; }
    3293           7 :     z = gerepilecopy(av, z);
    3294             :   }
    3295             :   else
    3296             :   {
    3297          21 :     avma = av; if (flag) return gen_1;
    3298           7 :     z = cgetg(4, t_VEC);
    3299           7 :     gel(z,1) = gen_1;
    3300           7 :     gel(z,2) = triv_order(degpol(P));
    3301           7 :     gel(z,3) = stoi(v);
    3302             :   }
    3303          14 :   return z;
    3304             : }
    3305             : 
    3306             : static int
    3307        3542 : ideal_is1(GEN x) {
    3308        3542 :   switch(typ(x))
    3309             :   {
    3310        1967 :     case t_INT: return is_pm1(x);
    3311        1232 :     case t_MAT: return RgM_isidentity(x);
    3312             :   }
    3313         343 :   return 0;
    3314             : }
    3315             : 
    3316             : /* FIXME: quadratic multiplication */
    3317             : static GEN
    3318        7119 : nfX_mul(GEN nf, GEN a, GEN b)
    3319             : {
    3320        7119 :   long da = degpol(a), db = degpol(b), dc, lc, k;
    3321             :   GEN c;
    3322        7119 :   if (da < 0 || db < 0) return gen_0;
    3323        7119 :   dc = da + db;
    3324        7119 :   if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
    3325        7119 :   lc = dc+3;
    3326        7119 :   c = cgetg(lc, t_POL); c[1] = a[1];
    3327       59661 :   for (k = 0; k <= dc; k++)
    3328             :   {
    3329       52542 :     long i, I = minss(k, da);
    3330       52542 :     GEN d = NULL;
    3331      185493 :     for (i = maxss(k-db, 0); i <= I; i++)
    3332             :     {
    3333      132951 :       GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
    3334      132951 :       d = d? nfadd(nf, d, e): e;
    3335             :     }
    3336       52542 :     gel(c, k+2) = d;
    3337             :   }
    3338        7119 :   return normalizepol_lg(c, lc);
    3339             : }
    3340             : /* assume b monic */
    3341             : static GEN
    3342        7119 : nfX_rem(GEN nf, GEN a, GEN b)
    3343             : {
    3344        7119 :   long da = degpol(a), db = degpol(b);
    3345        7119 :   if (da < 0) return gen_0;
    3346        7119 :   a = leafcopy(a);
    3347       24374 :   while (da >= db)
    3348             :   {
    3349       10136 :     long i, k = da;
    3350       10136 :     GEN A = gel(a, k+2);
    3351       78904 :     for (i = db-1, k--; i >= 0; i--, k--)
    3352       68768 :       gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
    3353       10136 :     a = normalizepol_lg(a, lg(a)-1);
    3354       10136 :     da = degpol(a);
    3355             :   }
    3356        7119 :   return a;
    3357             : }
    3358             : static GEN
    3359        7119 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
    3360             : {
    3361        7119 :   GEN c = nfX_mul(nf, a, b);
    3362        7119 :   if (typ(c) != t_POL) return c;
    3363        7119 :   return nfX_rem(nf, c, T);
    3364             : }
    3365             : /* return a in ideal A such that v_pr(a) = v_pr(A) */
    3366             : static GEN
    3367        1435 : minval(GEN nf, GEN A, GEN pr)
    3368             : {
    3369        1435 :   GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
    3370        1435 :   if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
    3371        1435 :   return a;
    3372             : }
    3373             : 
    3374             : /* nf a true nf. Return NULL if power order if pr-maximal */
    3375             : static GEN
    3376        1596 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
    3377             : {
    3378        1596 :   pari_sp av = avma, av1;
    3379             :   long i, j, k, n, vpol, cnt, sep;
    3380             :   GEN q, q1, p, T, modpr, W, I, MW, C, p1;
    3381             :   GEN Tauinv, Tau, prhinv, mpi, Id;
    3382             : 
    3383        1596 :   if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
    3384        1596 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3385        1596 :   av1 = avma;
    3386        1596 :   p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
    3387        1596 :   if (!p1) { avma = av; return NULL; }
    3388         791 :   if (is_pm1(gel(p1,1))) return gerepilecopy(av,gel(p1,2));
    3389         343 :   sep = itos(gel(p1,3));
    3390         343 :   W = gmael(p1,2,1);
    3391         343 :   I = gmael(p1,2,2);
    3392         343 :   gerepileall(av1, 2, &W, &I);
    3393             : 
    3394         343 :   mpi = zk_multable(nf, pr_get_gen(pr));
    3395         343 :   n = degpol(pol); vpol = varn(pol);
    3396         343 :   q1 = q = pr_norm(pr);
    3397         343 :   while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
    3398         343 :   Id = matid(n);
    3399             : 
    3400         343 :   prhinv = pr_inv(pr);
    3401         343 :   C = cgetg(n+1, t_MAT);
    3402         343 :   for (j=1; j<=n; j++) gel(C,j) = cgetg(n*n+1, t_COL);
    3403         343 :   MW = cgetg(n*n+1, t_MAT);
    3404         343 :   Tauinv = cgetg(n+1, t_VEC);
    3405         343 :   Tau    = cgetg(n+1, t_VEC);
    3406         343 :   av1 = avma;
    3407         714 :   for(cnt=1;; cnt++)
    3408             :   {
    3409         714 :     GEN I0 = leafcopy(I), W0 = leafcopy(W);
    3410             :     GEN Wa, Winv, Ip, A, MWmod, F, pseudo, G;
    3411             : 
    3412         714 :     if (DEBUGLEVEL>1) err_printf("    pass no %ld\n",cnt);
    3413        3920 :     for (j=1; j<=n; j++)
    3414             :     {
    3415             :       GEN tau, tauinv;
    3416        3206 :       if (ideal_is1(gel(I,j)))
    3417             :       {
    3418        1771 :         gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
    3419        1771 :         continue;
    3420             :       }
    3421        1435 :       gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
    3422        1435 :       gel(Tauinv,j) = tauinv = nfinv(nf, tau);
    3423        1435 :       gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
    3424        1435 :       gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
    3425             :     }
    3426             :     /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
    3427             : 
    3428             :    /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
    3429         714 :     Wa = RgM_to_RgXV(W,vpol);
    3430         714 :     Winv = nfM_inv(nf, W);
    3431             :     /* W_1 = 1 */
    3432         714 :     for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
    3433        3206 :     for (i=2; i<=n; i++)
    3434        9611 :       for (j=i; j<=n; j++)
    3435             :       {
    3436        7119 :         GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
    3437        7119 :         if (typ(z) != t_POL)
    3438           0 :           z = nfC_nf_mul(nf, gel(Winv,1), z);
    3439             :         else
    3440             :         {
    3441        7119 :           z = RgX_to_RgC(z, lg(Winv)-1);
    3442        7119 :           z = nfM_nfC_mul(nf, Winv, z);
    3443             :         }
    3444        7119 :         gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
    3445             :       }
    3446             : 
    3447             :     /* compute Ip =  pr-radical [ could use Ker(trace) if q large ] */
    3448         714 :     MWmod = nfM_to_FqM(MW,nf,modpr);
    3449         714 :     F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
    3450         714 :     for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
    3451         714 :     Ip = FqM_ker(F,T,p);
    3452         714 :     if (lg(Ip) == 1) { W = W0; I = I0; break; }
    3453             : 
    3454             :     /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
    3455         630 :     A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
    3456         630 :     for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
    3457         630 :     MW = nfM_mul(nf, nfM_inv(nf,A), MW);
    3458        3437 :     for (k=1; k<=n; k++)
    3459             :     {
    3460        2807 :       GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck = gel(C,k);
    3461       17990 :       for (j=1; j<=n; j++)
    3462             :       {
    3463       15183 :         GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
    3464       15183 :         for (i=1; i<=n; i++) gel(Ck, (j-1)*n+i) = nf_to_Fq(nf,gel(z,i),modpr);
    3465             :       }
    3466             :     }
    3467         630 :     G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
    3468             : 
    3469         630 :     pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
    3470             :     /* express W in terms of the power basis */
    3471         630 :     W = nfM_mul(nf, W, gel(pseudo,1));
    3472         630 :     I = gel(pseudo,2);
    3473             :     /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
    3474             :      * W[i,i] = Tau[i] */
    3475        3437 :     for (j=1; j<=n; j++)
    3476        2807 :       if (gel(Tau,j) != gen_1)
    3477             :       {
    3478        1183 :         gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
    3479        1183 :         gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
    3480             :       }
    3481         630 :     if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
    3482         630 :     if (sep <= 3 || gequal(I,I0)) break;
    3483             : 
    3484         371 :     if (gc_needed(av1,1))
    3485             :     {
    3486           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
    3487           0 :       gerepileall(av1,2, &W,&I);
    3488             :     }
    3489         371 :   }
    3490         343 :   return gerepilecopy(av, mkvec2(W, I));
    3491             : }
    3492             : 
    3493             : GEN
    3494      189328 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
    3495             : {
    3496      189328 :   switch(typ(c))
    3497             :   {
    3498       89688 :     case t_INT: case t_FRAC: return c;
    3499             :     case t_POL:
    3500        1750 :       if (lg(c) >= lg(T)) c = RgX_rem(c,T);
    3501        1750 :       break;
    3502             :     case t_POLMOD:
    3503       97883 :       if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
    3504       97561 :       c = gel(c,2);
    3505       97561 :       switch(typ(c))
    3506             :       {
    3507       86970 :         case t_POL: break;
    3508       10591 :         case t_INT: case t_FRAC: return c;
    3509           0 :         default: pari_err_TYPE(f, c);
    3510             :       }
    3511       86970 :       break;
    3512           7 :     default: pari_err_TYPE(f,c);
    3513             :   }
    3514             :   /* typ(c) = t_POL */
    3515       88720 :   if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
    3516       88713 :   switch(lg(c))
    3517             :   {
    3518        5362 :     case 2: return gen_0;
    3519             :     case 3:
    3520        4949 :       c = gel(c,2); if (is_rational_t(typ(c))) return c;
    3521           0 :       pari_err_TYPE(f,c);
    3522             :   }
    3523       78402 :   RgX_check_QX(c, f);
    3524       78388 :   return lift? c: mkpolmod(c, T);
    3525             : }
    3526             : /* check whether P is a polynomials with coeffs in number field Q[y]/(T) */
    3527             : GEN
    3528       64989 : RgX_nffix(const char *f, GEN T, GEN P, int lift)
    3529             : {
    3530       64989 :   long i, l, vT = varn(T);
    3531       64989 :   GEN Q = cgetg_copy(P, &l);
    3532       64989 :   if (typ(P) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), P);
    3533       64989 :   if (varncmp(varn(P), vT) >= 0) pari_err_PRIORITY(f, P, ">=", vT);
    3534       64968 :   Q[1] = P[1];
    3535       64968 :   for (i=2; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3536       64961 :   return normalizepol_lg(Q, l);
    3537             : }
    3538             : GEN
    3539          28 : RgV_nffix(const char *f, GEN T, GEN P, int lift)
    3540             : {
    3541             :   long i, l;
    3542          28 :   GEN Q = cgetg_copy(P, &l);
    3543          28 :   for (i=1; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3544          21 :   return Q;
    3545             : }
    3546             : 
    3547             : #if 0
    3548             : /* determinant of the trace pairing. FIXME: unused; for rnfmaxord ? */
    3549             : static GEN
    3550             : get_d(GEN nf, GEN pol, GEN A)
    3551             : {
    3552             :   long i, j, n = degpol(pol);
    3553             :   GEN W = RgM_to_RgXV(lift_shallow(matbasistoalg(nf,A)), varn(pol));
    3554             :   GEN T, nfT = nf_get_pol(nf), sym = polsym_gen(pol, NULL, n-1, nfT, NULL);
    3555             :   T = cgetg(n+1,t_MAT);
    3556             :   for (j=1; j<=n; j++) gel(T,j) = cgetg(n+1,t_COL);
    3557             :   for (j=1; j<=n; j++)
    3558             :     for (i=j; i<=n; i++)
    3559             :     {
    3560             :       GEN c = RgXQX_mul(gel(W,i),gel(W,j), nfT);
    3561             :       c = RgXQX_rem(c, pol, nfT);
    3562             :       c = simplify_shallow(quicktrace(c,sym));
    3563             :       gcoeff(T,j,i) = gcoeff(T,i,j) = c;
    3564             :     }
    3565             :   return nf_to_scalar_or_basis(nf, det(T));
    3566             : }
    3567             : #endif
    3568             : 
    3569             : /* nf = base field K
    3570             :  * pol= monic polynomial, coefficients in Z_K, defining a relative
    3571             :  *   extension L = K[X]/(pol). One MUST have varn(pol) << nf_get_varn(nf).
    3572             :  * Returns a pseudo-basis [A,I] of Z_L, set (D,d) to the relative
    3573             :  * discriminant, and f to the index-ideal */
    3574             : GEN
    3575        1113 : rnfallbase(GEN nf, GEN *ppol, GEN *pD, GEN *pd, GEN *pf)
    3576             : {
    3577             :   long i, n, l;
    3578        1113 :   GEN nfT, fa, E, P, z, D, disc, pol = *ppol;
    3579             : 
    3580        1113 :   nf = checknf(nf); nfT = nf_get_pol(nf);
    3581        1113 :   pol = RgX_nffix("rnfallbase", nfT,pol,0);
    3582        1113 :   if (!gequal1(leading_coeff(pol)))
    3583           0 :     pari_err_IMPL("non-monic relative polynomials");
    3584             : 
    3585        1113 :   n = degpol(pol);
    3586        1113 :   disc = nf_to_scalar_or_basis(nf, RgX_disc(pol));
    3587        1113 :   pol = lift_shallow(pol);
    3588        1113 :   fa = idealfactor(nf, disc);
    3589        1106 :   P = gel(fa,1); l = lg(P);
    3590        1106 :   E = gel(fa,2);
    3591        1106 :   z = NULL;
    3592        3066 :   for (i=1; i < l; i++)
    3593             :   {
    3594        1960 :     long e = itos(gel(E,i));
    3595        1960 :     if (e > 1) z = rnfjoinmodules(nf, z, rnfmaxord(nf, pol, gel(P,i), e));
    3596             :   }
    3597        1106 :   if (z) D = idealprod(nf, gel(z,2)); else { z = triv_order(n); D = gen_1; }
    3598        1106 :   if (isint1(D))
    3599             :   {
    3600         735 :     if (pf) *pf = gen_1;
    3601         735 :     D = disc;
    3602             :   }
    3603             :   else
    3604             :   {
    3605         371 :     if (pf) *pf = idealinv(nf, D);
    3606         371 :     D = idealmul(nf, disc, idealsqr(nf,D));
    3607             :   }
    3608        1106 :   if (pd)
    3609             :   {
    3610         763 :     GEN f = core2partial(Q_content(disc), 0);
    3611         763 :     *pd = gdiv(disc, sqri(gel(f,2)));
    3612             :   }
    3613        1106 :   *pD = D;
    3614        1106 :   *ppol = pol; return z;
    3615             : }
    3616             : 
    3617             : GEN
    3618          49 : rnfpseudobasis(GEN nf, GEN pol)
    3619             : {
    3620          49 :   pari_sp av = avma;
    3621          49 :   GEN D, d, z = rnfallbase(nf,&pol, &D, &d, NULL);
    3622          49 :   return gerepilecopy(av, mkvec4(gel(z,1), gel(z,2), D, d));
    3623             : }
    3624             : 
    3625             : GEN
    3626           7 : rnfdiscf(GEN nf, GEN pol)
    3627             : {
    3628           7 :   pari_sp av = avma;
    3629           7 :   GEN D, d; (void)rnfallbase(nf,&pol, &D, &d, NULL);
    3630           7 :   return gerepilecopy(av, mkvec2(D,d));
    3631             : }
    3632             : 
    3633             : GEN
    3634          35 : gen_if_principal(GEN bnf, GEN x)
    3635             : {
    3636          35 :   pari_sp av = avma;
    3637          35 :   GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
    3638          35 :   if (isintzero(z)) { avma = av; return NULL; }
    3639          28 :   return z;
    3640             : }
    3641             : 
    3642             : static int
    3643          63 : is_pseudo_matrix(GEN O)
    3644             : {
    3645         189 :   return (typ(O) ==t_VEC && lg(O) >= 3
    3646          63 :           && typ(gel(O,1)) == t_MAT
    3647          63 :           && typ(gel(O,2)) == t_VEC
    3648         126 :           && lgcols(O) == lg(gel(O,2)));
    3649             : }
    3650             : 
    3651             : /* given bnf and a pseudo-basis of an order in HNF [A,I], tries to simplify
    3652             :  * the HNF as much as possible. The resulting matrix will be upper triangular
    3653             :  * but the diagonal coefficients will not be equal to 1. The ideals are
    3654             :  * guaranteed to be integral and primitive. */
    3655             : GEN
    3656           0 : rnfsimplifybasis(GEN bnf, GEN x)
    3657             : {
    3658           0 :   pari_sp av = avma;
    3659             :   long i, l;
    3660             :   GEN y, Az, Iz, nf, A, I;
    3661             : 
    3662           0 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3663           0 :   if (!is_pseudo_matrix(x)) pari_err_TYPE("rnfsimplifybasis",x);
    3664           0 :   A = gel(x,1);
    3665           0 :   I = gel(x,2); l = lg(I);
    3666           0 :   y = cgetg(3, t_VEC);
    3667           0 :   Az = cgetg(l, t_MAT); gel(y,1) = Az;
    3668           0 :   Iz = cgetg(l, t_VEC); gel(y,2) = Iz;
    3669           0 :   for (i = 1; i < l; i++)
    3670             :   {
    3671             :     GEN c, d;
    3672           0 :     if (ideal_is1(gel(I,i))) {
    3673           0 :       gel(Iz,i) = gen_1;
    3674           0 :       gel(Az,i) = gel(A,i);
    3675           0 :       continue;
    3676             :     }
    3677             : 
    3678           0 :     gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
    3679           0 :     gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
    3680           0 :     if (c && ideal_is1(gel(Iz,i))) continue;
    3681             : 
    3682           0 :     d = gen_if_principal(bnf, gel(Iz,i));
    3683           0 :     if (d)
    3684             :     {
    3685           0 :       gel(Iz,i) = gen_1;
    3686           0 :       gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
    3687             :     }
    3688             :   }
    3689           0 :   return gerepilecopy(av, y);
    3690             : }
    3691             : 
    3692             : static GEN
    3693          70 : get_order(GEN nf, GEN O, const char *s)
    3694             : {
    3695          70 :   if (typ(O) == t_POL)
    3696           7 :     return rnfpseudobasis(nf, O);
    3697          63 :   if (!is_pseudo_matrix(O)) pari_err_TYPE(s, O);
    3698          63 :   return O;
    3699             : }
    3700             : 
    3701             : GEN
    3702          21 : rnfdet(GEN nf, GEN order)
    3703             : {
    3704          21 :   pari_sp av = avma;
    3705             :   GEN A, I, D;
    3706          21 :   nf = checknf(nf);
    3707          14 :   order = get_order(nf, order, "rnfdet");
    3708          14 :   A = gel(order,1);
    3709          14 :   I = gel(order,2);
    3710          14 :   D = idealmul(nf, nfM_det(nf,A), idealprod(nf,I));
    3711          14 :   return gerepileupto(av, D);
    3712             : }
    3713             : 
    3714             : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
    3715             :    t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
    3716             : static void
    3717          56 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
    3718             : {
    3719             :   GEN x, uv, y, da, db;
    3720             : 
    3721          56 :   a = idealinv(nf,a);
    3722          56 :   a = Q_remove_denom(a, &da);
    3723          56 :   b = Q_remove_denom(b, &db);
    3724          56 :   x = idealcoprime(nf,a,b);
    3725          56 :   uv = idealaddtoone(nf, idealmul(nf,x,a), b);
    3726          56 :   y = gel(uv,2);
    3727          56 :   if (da) x = RgC_Rg_mul(x,da);
    3728          56 :   if (db) y = RgC_Rg_div(y,db);
    3729          56 :   *px = x;
    3730          56 :   *py = y;
    3731          56 :   *pz = db ? negi(db): gen_m1;
    3732          56 :   *pt = nfdiv(nf, gel(uv,1), x);
    3733          56 : }
    3734             : 
    3735             : /* given a pseudo-basis of an order in HNF [A,I] (or [A,I,D,d]), gives an
    3736             :  * n x n matrix (not in HNF) of a pseudo-basis and an ideal vector
    3737             :  * [1,1,...,1,I] such that order = Z_K^(n-1) x I.
    3738             :  * Uses the approximation theorem ==> slow. */
    3739             : GEN
    3740          28 : rnfsteinitz(GEN nf, GEN order)
    3741             : {
    3742          28 :   pari_sp av = avma;
    3743             :   long i, n, l;
    3744             :   GEN A, I, p1;
    3745             : 
    3746          28 :   nf = checknf(nf);
    3747          28 :   order = get_order(nf, order, "rnfsteinitz");
    3748          28 :   A = RgM_to_nfM(nf, gel(order,1));
    3749          28 :   I = leafcopy(gel(order,2)); n=lg(A)-1;
    3750         189 :   for (i=1; i<n; i++)
    3751             :   {
    3752         161 :     GEN c1, c2, b, a = gel(I,i);
    3753         161 :     gel(I,i) = gen_1;
    3754         161 :     if (ideal_is1(a)) continue;
    3755             : 
    3756          56 :     c1 = gel(A,i);
    3757          56 :     c2 = gel(A,i+1);
    3758          56 :     b = gel(I,i+1);
    3759          56 :     if (ideal_is1(b))
    3760             :     {
    3761           0 :       gel(A,i) = c2;
    3762           0 :       gel(A,i+1) = gneg(c1);
    3763           0 :       gel(I,i+1) = a;
    3764             :     }
    3765             :     else
    3766             :     {
    3767          56 :       pari_sp av2 = avma;
    3768             :       GEN x, y, z, t;
    3769          56 :       nfidealdet1(nf,a,b, &x,&y,&z,&t);
    3770          56 :       x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
    3771          56 :       y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
    3772          56 :       gerepileall(av2, 2, &x,&y);
    3773          56 :       gel(A,i) = x;
    3774          56 :       gel(A,i+1) = y;
    3775          56 :       gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &p1);
    3776          56 :       if (p1) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), p1);
    3777             :     }
    3778             :   }
    3779          28 :   l = lg(order);
    3780          28 :   p1 = cgetg(l,t_VEC);
    3781          28 :   gel(p1,1) = A;
    3782          28 :   gel(p1,2) = I; for (i=3; i<l; i++) gel(p1,i) = gel(order,i);
    3783          28 :   return gerepilecopy(av, p1);
    3784             : }
    3785             : 
    3786             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3787             :  * and outputs a basis if it is free, an n+1-generating set if it is not */
    3788             : GEN
    3789          21 : rnfbasis(GEN bnf, GEN order)
    3790             : {
    3791          21 :   pari_sp av = avma;
    3792             :   long j, n;
    3793             :   GEN nf, A, I, cl, col, a;
    3794             : 
    3795          21 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3796          21 :   order = get_order(nf, order, "rnfbasis");
    3797          21 :   I = gel(order,2); n = lg(I)-1;
    3798          21 :   j=1; while (j<n && ideal_is1(gel(I,j))) j++;
    3799          21 :   if (j<n)
    3800             :   {
    3801           7 :     order = rnfsteinitz(nf,order);
    3802           7 :     I = gel(order,2);
    3803             :   }
    3804          21 :   A = gel(order,1);
    3805          21 :   col= gel(A,n); A = vecslice(A, 1, n-1);
    3806          21 :   cl = gel(I,n);
    3807          21 :   a = gen_if_principal(bnf, cl);
    3808          21 :   if (!a)
    3809             :   {
    3810           7 :     GEN v = idealtwoelt(nf, cl);
    3811           7 :     A = shallowconcat(A, gmul(gel(v,1), col));
    3812           7 :     a = gel(v,2);
    3813             :   }
    3814          21 :   A = shallowconcat(A, nfC_nf_mul(nf, col, a));
    3815          21 :   return gerepilecopy(av, A);
    3816             : }
    3817             : 
    3818             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3819             :  * and outputs a basis (not pseudo) in Hermite Normal Form if it exists, zero
    3820             :  * if not
    3821             :  */
    3822             : GEN
    3823           7 : rnfhnfbasis(GEN bnf, GEN order)
    3824             : {
    3825           7 :   pari_sp av = avma;
    3826             :   long j, n;
    3827             :   GEN nf, A, I, a;
    3828             : 
    3829           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3830           7 :   order = get_order(nf, order, "rnfbasis");
    3831           7 :   A = gel(order,1); A = RgM_shallowcopy(A);
    3832           7 :   I = gel(order,2); n = lg(A)-1;
    3833          42 :   for (j=1; j<=n; j++)
    3834             :   {
    3835          35 :     if (ideal_is1(gel(I,j))) continue;
    3836          14 :     a = gen_if_principal(bnf, gel(I,j));
    3837          14 :     if (!a) { avma = av; return gen_0; }
    3838          14 :     gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
    3839             :   }
    3840           7 :   return gerepilecopy(av,A);
    3841             : }
    3842             : 
    3843             : static long
    3844           7 : rnfisfree_aux(GEN bnf, GEN order)
    3845             : {
    3846             :   long n, j;
    3847             :   GEN nf, P, I;
    3848             : 
    3849           7 :   bnf = checkbnf(bnf);
    3850           7 :   if (is_pm1( bnf_get_no(bnf) )) return 1;
    3851           0 :   nf = bnf_get_nf(bnf);
    3852           0 :   order = get_order(nf, order, "rnfisfree");
    3853           0 :   I = gel(order,2); n = lg(I)-1;
    3854           0 :   j=1; while (j<=n && ideal_is1(gel(I,j))) j++;
    3855           0 :   if (j>n) return 1;
    3856             : 
    3857           0 :   P = gel(I,j);
    3858           0 :   for (j++; j<=n; j++)
    3859           0 :     if (!ideal_is1(gel(I,j))) P = idealmul(nf,P,gel(I,j));
    3860           0 :   return gequal0( isprincipal(bnf,P) );
    3861             : }
    3862             : 
    3863             : long
    3864           7 : rnfisfree(GEN bnf, GEN order)
    3865             : {
    3866           7 :   pari_sp av = avma;
    3867           7 :   long n = rnfisfree_aux(bnf, order);
    3868           7 :   avma = av; return n;
    3869             : }
    3870             : 
    3871             : /**********************************************************************/
    3872             : /**                                                                  **/
    3873             : /**                   COMPOSITUM OF TWO NUMBER FIELDS                **/
    3874             : /**                                                                  **/
    3875             : /**********************************************************************/
    3876             : static GEN
    3877         959 : compositum_fix(GEN nf, GEN A)
    3878             : {
    3879             :   int ok;
    3880         959 :   if (nf)
    3881             :   {
    3882         231 :     long i, l = lg(A);
    3883         231 :     A = shallowcopy(A);
    3884         231 :     for (i=2; i<l; i++) gel(A,i) = basistoalg(nf, gel(A,i));
    3885         231 :     ok = nfissquarefree(nf,A);
    3886             :   }
    3887             :   else
    3888             :   {
    3889         728 :     A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
    3890         728 :     ok = ZX_is_squarefree(A);
    3891             :   }
    3892         959 :   if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
    3893         952 :   return A;
    3894             : }
    3895             : INLINE long
    3896          14 : nextk(long k) { return k>0 ? -k : 1-k; }
    3897             : 
    3898             : /* modular version */
    3899             : GEN
    3900         525 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
    3901             : {
    3902         525 :   pari_sp av = avma;
    3903             :   int same;
    3904             :   long v, k;
    3905             :   GEN C, D, LPRS;
    3906             : 
    3907         525 :   if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
    3908         525 :   if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
    3909         525 :   if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
    3910         525 :   v = varn(A);
    3911         525 :   if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
    3912         525 :   if (nf)
    3913             :   {
    3914         140 :     nf = checknf(nf);
    3915         140 :     if (v == nf_get_varn(nf)) pari_err_PRIORITY("polcompositum", nf, "==",  v);
    3916             :   }
    3917         504 :   same = (A == B || RgX_equal(A,B));
    3918         504 :   A = compositum_fix(nf,A);
    3919         497 :   if (!same) B = compositum_fix(nf,B);
    3920             : 
    3921         497 :   D = LPRS = NULL; /* -Wall */
    3922         497 :   k = same? -1: 1;
    3923         497 :   if (nf)
    3924             :   {
    3925         119 :     long v0 = fetch_var();
    3926             :     GEN q;
    3927          14 :     for(;; k = nextk(k))
    3928             :     {
    3929         133 :       GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
    3930         133 :       GEN B1 = poleval(B,chgvar);
    3931         133 :       C = RgX_resultant_all(A,B1,&q);
    3932         133 :       C = gsubst(C,v0,pol_x(v));
    3933         133 :       if (nfissquarefree(nf,C)) break;
    3934          14 :     }
    3935         119 :     C = lift_if_rational(C);
    3936         119 :     if (flag&1)
    3937             :     {
    3938             :       GEN H0, H1;
    3939          77 :       H0 = gsubst(gel(q,2),v0,pol_x(v));
    3940          77 :       H1 = gsubst(gel(q,3),v0,pol_x(v));
    3941          77 :       if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
    3942          77 :       if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
    3943          77 :       H0 = lift_if_rational(H0);
    3944          77 :       H1 = lift_if_rational(H1);
    3945          77 :       LPRS = mkvec2(H0,H1);
    3946             :     }
    3947             :   }
    3948             :   else
    3949             :   {
    3950         378 :     B = leafcopy(B); setvarn(B,fetch_var_higher());
    3951         378 :     C = ZX_ZXY_resultant_all(A, B, &k, (flag&1)? &LPRS: NULL);
    3952         378 :     setvarn(C, v);
    3953             :   }
    3954             :   /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
    3955         497 :   if (same)
    3956             :   {
    3957          42 :     D = RgX_rescale(A, stoi(1 - k));
    3958          42 :     C = RgX_div(C, D);
    3959          42 :     if (degpol(C) <= 0)
    3960           0 :       C = mkvec(D);
    3961             :     else
    3962          42 :       C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
    3963             :   }
    3964         455 :   else if (flag & 2)
    3965         175 :     C = mkvec(C);
    3966             :   else
    3967         280 :     C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
    3968         490 :   gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
    3969         490 :   if (flag&1)
    3970             :   { /* a,b,c root of A,B,C = compositum, c = b - k a */
    3971         315 :     long i, l = lg(C);
    3972         315 :     GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
    3973         315 :     setvarn(mH0,v);
    3974         315 :     setvarn(H1,v);
    3975         651 :     for (i=1; i<l; i++)
    3976             :     {
    3977         336 :       GEN D = gel(C,i);
    3978         336 :       a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
    3979         336 :       b = gadd(pol_x(v), gmulsg(k,a));
    3980         336 :       gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
    3981             :     }
    3982             :   }
    3983         490 :   (void)delete_var();
    3984         490 :   settyp(C, t_VEC);
    3985         490 :   if (flag&2) C = gel(C,1);
    3986         490 :   return gerepilecopy(av, C);
    3987             : }
    3988             : GEN
    3989         385 : polcompositum0(GEN A, GEN B, long flag)
    3990         385 : { return nfcompositum(NULL,A,B,flag); }
    3991             : 
    3992             : GEN
    3993          35 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
    3994             : GEN
    3995         231 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }
    3996             : 
    3997             : /* Assume A,B irreducible (in particular squarefree) and define linearly
    3998             :  * disjoint extensions: no factorisation needed */
    3999             : GEN
    4000         385 : ZX_compositum_disjoint(GEN A, GEN B)
    4001             : {
    4002         385 :   long k = 1;
    4003         385 :   return ZX_ZXY_resultant_all(A, B, &k, NULL);
    4004             : }
    4005             : 
    4006             : GEN
    4007         112 : nfsplitting(GEN T, GEN D)
    4008             : {
    4009         112 :   pari_sp av = avma;
    4010             :   long d, v;
    4011             :   GEN F, K;
    4012         112 :   T = get_nfpol(T,&K);
    4013         105 :   if (!K)
    4014             :   {
    4015          98 :     if (typ(T) != t_POL) pari_err_TYPE("nfsplitting",T);
    4016          98 :     T = Q_primpart(T);
    4017          98 :     RgX_check_ZX(T,"nfsplitting");
    4018             :   }
    4019         105 :   d = degpol(T);
    4020         105 :   if (d<=1) return pol_x(0);
    4021          84 :   if (!K) {
    4022          77 :     if (!isint1(leading_coeff(T))) K = T = polredbest(T,0);
    4023          77 :     K = T;
    4024             :   }
    4025          84 :   if (D)
    4026             :   {
    4027          21 :     if (typ(D) != t_INT || signe(D) < 1) pari_err_TYPE("nfsplitting",D);
    4028             :   }
    4029             :   else
    4030             :   {
    4031          63 :     char *data = stack_strcat(pari_datadir, "/galdata");
    4032          63 :     long dmax = pari_is_dir(data)? 11: 7;
    4033          63 :     D = (d <= dmax)? gel(polgalois(T,DEFAULTPREC), 1): mpfact(d);
    4034             :   }
    4035          84 :   d = itos_or_0(D);
    4036          84 :   v = varn(T);
    4037          84 :   T = leafcopy(T); setvarn(T, fetch_var_higher());
    4038          84 :   for(F = T;;)
    4039             :   {
    4040         105 :     GEN P = gel(nffactor(K, F), 1), Q = gel(P,lg(P)-1);
    4041         105 :     if (degpol(gel(P,1)) == degpol(Q)) break;
    4042          91 :     F = rnfequation(K,Q);
    4043          91 :     if (degpol(F) == d) break;
    4044          21 :   }
    4045          84 :   if (umodiu(D,degpol(F)))
    4046             :   {
    4047           7 :     char *sD = itostr(D);
    4048           7 :     pari_warn(warner,stack_strcat("ignoring incorrect degree bound ",sD));
    4049             :   }
    4050          84 :   (void)delete_var();
    4051          84 :   setvarn(F,v);
    4052          84 :   return gerepilecopy(av, F);
    4053             : }

Generated by: LCOV version 1.11