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 20422-b487f4d) Lines: 2004 2327 86.1 %
Date: 2017-03-22 05:51:54 Functions: 160 171 93.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11