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

Generated by: LCOV version 1.13