Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - base2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20277-2bd9113) Lines: 1983 2303 86.1 %
Date: 2017-02-21 05:49:51 Functions: 158 169 93.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11