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.14.0 lcov report (development 27775-aca467eab2) Lines: 2187 2304 94.9 %
Date: 2022-07-03 07:33:15 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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                       MAXIMAL ORDERS                            */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : #define DEBUGLEVEL DEBUGLEVEL_nf
      23             : 
      24             : /* allow p = -1 from factorizations, avoid oo loop on p = 1 */
      25             : static long
      26       12082 : safe_Z_pvalrem(GEN x, GEN p, GEN *z)
      27             : {
      28       12082 :   if (is_pm1(p))
      29             :   {
      30          28 :     if (signe(p) > 0) return gvaluation(x,p); /*error*/
      31          21 :     *z = absi(x); return 1;
      32             :   }
      33       12054 :   return Z_pvalrem(x, p, z);
      34             : }
      35             : /* D an integer, P a ZV, return a factorization matrix for D over P, removing
      36             :  * entries with 0 exponent. */
      37             : static GEN
      38        3500 : fact_from_factors(GEN D, GEN P, long flag)
      39             : {
      40        3500 :   long i, l = lg(P), iq = 1;
      41        3500 :   GEN Q = cgetg(l+1,t_COL);
      42        3500 :   GEN E = cgetg(l+1,t_COL);
      43       15575 :   for (i=1; i<l; i++)
      44             :   {
      45       12082 :     GEN p = gel(P,i);
      46             :     long k;
      47       12082 :     if (flag && !equalim1(p))
      48             :     {
      49          14 :       p = gcdii(p, D);
      50          14 :       if (is_pm1(p)) continue;
      51             :     }
      52       12082 :     k = safe_Z_pvalrem(D, p, &D);
      53       12075 :     if (k) { gel(Q,iq) = p; gel(E,iq) = utoipos(k); iq++; }
      54             :   }
      55        3493 :   D = absi_shallow(D);
      56        3493 :   if (!equali1(D))
      57             :   {
      58         560 :     long k = Z_isanypower(D, &D);
      59         560 :     if (!k) k = 1;
      60         560 :     gel(Q,iq) = D; gel(E,iq) = utoipos(k); iq++;
      61             :   }
      62        3493 :   setlg(Q,iq);
      63        3493 :   setlg(E,iq); return mkmat2(Q,E);
      64             : }
      65             : 
      66             : /* d a t_INT; f a t_MAT factorisation of some t_INT sharing some divisors
      67             :  * with d, or a prime (t_INT). Return a factorization F of d: "primes"
      68             :  * entries in f _may_ be composite, and are included as is in d. */
      69             : static GEN
      70        1960 : update_fact(GEN d, GEN f)
      71             : {
      72             :   GEN P;
      73        1960 :   switch (typ(f))
      74             :   {
      75        1946 :     case t_INT: case t_VEC: case t_COL: return f;
      76          14 :     case t_MAT:
      77          14 :       if (lg(f) == 3) { P = gel(f,1); break; }
      78             :     /*fall through*/
      79             :     default:
      80           7 :       pari_err_TYPE("nfbasis [factorization expected]",f);
      81             :       return NULL;/*LCOV_EXCL_LINE*/
      82             :   }
      83           7 :   return fact_from_factors(d, P, 1);
      84             : }
      85             : 
      86             : /* T = C T0(X/L); C = L^d / lt(T0), d = deg(T)
      87             :  * disc T = C^2(d - 1) L^-(d(d-1)) disc T0 = (L^d / lt(T0)^2)^(d-1) disc T0 */
      88             : static GEN
      89      810883 : set_disc(nfmaxord_t *S)
      90             : {
      91             :   GEN L, dT;
      92             :   long d;
      93      810883 :   if (S->T0 == S->T) return ZX_disc(S->T);
      94      248890 :   d = degpol(S->T0);
      95      248895 :   L = S->unscale;
      96      248895 :   if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
      97       11743 :     dT = ZX_disc(S->T); /* more efficient */
      98             :   else
      99             :   {
     100      237152 :     GEN l0 = leading_coeff(S->T0);
     101      237151 :     GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
     102      237141 :     dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
     103             :   }
     104      248874 :   return S->dT = dT;
     105             : }
     106             : 
     107             : /* dT != 0 */
     108             : static GEN
     109      787311 : poldiscfactors_i(GEN T, GEN dT, long flag)
     110             : {
     111             :   GEN U, fa, Z, E, P, Tp;
     112             :   long i, l;
     113             : 
     114      787311 :   fa = absZ_factor_limit_strict(dT, minuu(tridiv_bound(dT), maxprime()), &U);
     115      787361 :   if (!U) return fa;
     116         770 :   Z = mkcol(gel(U,1)); P = gel(fa,1); Tp = NULL;
     117        1652 :   while (lg(Z) != 1)
     118             :   { /* pop and handle last element of Z */
     119         882 :     GEN p = gel(Z, lg(Z)-1), r;
     120         882 :     setlg(Z, lg(Z)-1);
     121         882 :     if (!Tp) /* first time: p is composite and not a power */
     122         770 :       Tp = ZX_deriv(T);
     123             :     else
     124             :     {
     125         112 :       (void)Z_isanypower(p, &p);
     126         112 :       if ((flag || lgefint(p)==3) && BPSW_psp(p))
     127          89 :       { P = vec_append(P, p); continue; }
     128             :     }
     129         793 :     r = FpX_gcd_check(T, Tp, p);
     130         793 :     if (r)
     131          56 :       Z = shallowconcat(Z, Z_cba(r, diviiexact(p,r)));
     132         737 :     else if (flag)
     133           7 :       P = shallowconcat(P, gel(Z_factor(p),1));
     134             :     else
     135         730 :       P = vec_append(P, p);
     136             :   }
     137         770 :   ZV_sort_inplace(P); l = lg(P); E = cgetg(l, t_COL);
     138        6852 :   for (i = 1; i < l; i++) gel(E,i) = utoipos(Z_pvalrem(dT, gel(P,i), &dT));
     139         770 :   return mkmat2(P,E);
     140             : }
     141             : 
     142             : GEN
     143          42 : poldiscfactors(GEN T, long flag)
     144             : {
     145          42 :   pari_sp av = avma;
     146             :   GEN dT;
     147          42 :   if (typ(T) != t_POL || !RgX_is_ZX(T)) pari_err_TYPE("poldiscfactors",T);
     148          42 :   if (flag < 0 || flag > 1) pari_err_FLAG("poldiscfactors");
     149          42 :   dT = ZX_disc(T);
     150          42 :   if (!signe(dT)) retmkvec2(gen_0, Z_factor(gen_0));
     151          35 :   return gerepilecopy(av, mkvec2(dT, poldiscfactors_i(T, dT, flag)));
     152             : }
     153             : 
     154             : static void
     155      810894 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
     156             : {
     157      810894 :   GEN dT, L, E, P, fa = NULL;
     158             :   pari_timer t;
     159      810894 :   long l, ty = typ(T);
     160             : 
     161      810894 :   if (DEBUGLEVEL) timer_start(&t);
     162      810894 :   if (ty == t_VEC) {
     163       23548 :     if (lg(T) != 3) pari_err_TYPE("nfmaxord",T);
     164       23548 :     fa = gel(T,2); T = gel(T,1); ty = typ(T);
     165             :   }
     166      810894 :   if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
     167      810894 :   T = Q_primpart(T);
     168      810848 :   if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
     169      810843 :   RgX_check_ZX(T, "nfmaxord");
     170      810854 :   S->T0 = T;
     171      810854 :   S->T = T = ZX_Q_normalize(T, &L);
     172      810883 :   S->unscale = L;
     173      810883 :   S->dT = dT = set_disc(S);
     174      810824 :   S->certify = 1;
     175      810824 :   if (!signe(dT)) pari_err_IRREDPOL("nfmaxord",T);
     176      810822 :   if (fa)
     177             :   {
     178       23548 :     const long MIN = 100; /* include at least all p < 101 */
     179       23548 :     GEN P0 = NULL, U;
     180       23548 :     S->certify = 0;
     181       23548 :     if (!isint1(L)) fa = update_fact(dT, fa);
     182       23541 :     switch(typ(fa))
     183             :     {
     184         238 :       case t_MAT:
     185         238 :         if (!is_Z_factornon0(fa)) pari_err_TYPE("nfmaxord",fa);
     186         231 :         P0 = gel(fa,1); /* fall through */
     187        3493 :       case t_VEC: case t_COL:
     188        3493 :         if (!P0)
     189             :         {
     190        3262 :           if (!RgV_is_ZV(fa)) pari_err_TYPE("nfmaxord",fa);
     191        3262 :           P0 = fa;
     192             :         }
     193        3493 :         P = gel(absZ_factor_limit_strict(dT, MIN, &U), 1);
     194        3493 :         if (lg(P) != 0) { settyp(P, typ(P0)); P0 = shallowconcat(P0,P); }
     195        3493 :         P0 = ZV_sort_uniq(P0);
     196        3493 :         fa = fact_from_factors(dT, P0, 0);
     197        3486 :         break;
     198       20034 :       case t_INT:
     199       20034 :         fa = absZ_factor_limit(dT, (signe(fa) <= 0)? 1: maxuu(itou(fa), MIN));
     200       20034 :         break;
     201           7 :       default:
     202           7 :         pari_err_TYPE("nfmaxord",fa);
     203             :     }
     204             :   }
     205             :   else
     206             :   {
     207      787274 :     S->certify = !(flag & nf_PARTIALFACT);
     208      787274 :     fa = poldiscfactors_i(T, dT, 0);
     209             :   }
     210      810845 :   P = gel(fa,1); l = lg(P);
     211      810845 :   E = gel(fa,2);
     212      810845 :   if (l > 1 && is_pm1(gel(P,1)))
     213             :   {
     214          21 :     l--;
     215          21 :     P = vecslice(P, 2, l);
     216          21 :     E = vecslice(E, 2, l);
     217             :   }
     218      810849 :   S->dTP = P;
     219      810849 :   S->dTE = vec_to_vecsmall(E);
     220      810848 :   if (DEBUGLEVEL>2) timer_printf(&t, "disc. factorisation");
     221      810848 : }
     222             : 
     223             : static int
     224      212280 : fnz(GEN x,long j)
     225             : {
     226             :   long i;
     227      691757 :   for (i=1; i<j; i++)
     228      527584 :     if (signe(gel(x,i))) return 0;
     229      164173 :   return 1;
     230             : }
     231             : /* return list u[i], 2 by 2 coprime with the same prime divisors as ab */
     232             : static GEN
     233         273 : get_coprimes(GEN a, GEN b)
     234             : {
     235         273 :   long i, k = 1;
     236         273 :   GEN u = cgetg(3, t_COL);
     237         273 :   gel(u,1) = a;
     238         273 :   gel(u,2) = b;
     239             :   /* u1,..., uk 2 by 2 coprime */
     240         994 :   while (k+1 < lg(u))
     241             :   {
     242         721 :     GEN d, c = gel(u,k+1);
     243         721 :     if (is_pm1(c)) { k++; continue; }
     244        1218 :     for (i=1; i<=k; i++)
     245             :     {
     246         784 :       GEN ui = gel(u,i);
     247         784 :       if (is_pm1(ui)) continue;
     248         448 :       d = gcdii(c, ui);
     249         448 :       if (d == gen_1) continue;
     250         448 :       c = diviiexact(c, d);
     251         448 :       gel(u,i) = diviiexact(ui, d);
     252         448 :       u = vec_append(u, d);
     253             :     }
     254         434 :     gel(u,++k) = c;
     255             :   }
     256        1267 :   for (i = k = 1; i < lg(u); i++)
     257         994 :     if (!is_pm1(gel(u,i))) gel(u,k++) = gel(u,i);
     258         273 :   setlg(u, k); return u;
     259             : }
     260             : 
     261             : /*******************************************************************/
     262             : /*                                                                 */
     263             : /*                            ROUND 4                              */
     264             : /*                                                                 */
     265             : /*******************************************************************/
     266             : typedef struct {
     267             :   /* constants */
     268             :   long pisprime; /* -1: unknown, 1: prime,  0: composite */
     269             :   GEN p, f; /* goal: factor f p-adically */
     270             :   long df;
     271             :   GEN pdf; /* p^df = reduced discriminant of f */
     272             :   long mf; /* */
     273             :   GEN psf, pmf; /* stability precision for f, wanted precision for f */
     274             :   long vpsf; /* v_p(p_f) */
     275             :   /* these are updated along the way */
     276             :   GEN phi; /* a p-integer, in Q[X] */
     277             :   GEN phi0; /* a p-integer, in Q[X] from testb2 / testc2, to be composed with
     278             :              * phi when correct precision is known */
     279             :   GEN chi; /* characteristic polynomial of phi (mod psc) in Z[X] */
     280             :   GEN nu; /* irreducible divisor of chi mod p, in Z[X] */
     281             :   GEN invnu; /* numerator ( 1/ Mod(nu, chi) mod pmr ) */
     282             :   GEN Dinvnu;/* denominator ( ... ) */
     283             :   long vDinvnu; /* v_p(Dinvnu) */
     284             :   GEN prc, psc; /* reduced discriminant of chi, stability precision for chi */
     285             :   long vpsc; /* v_p(p_c) */
     286             :   GEN ns, nsf, precns; /* cached Newton sums for nsf and their precision */
     287             : } decomp_t;
     288             : static GEN maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag);
     289             : static GEN dbasis(GEN p, GEN f, long mf, GEN alpha, GEN U);
     290             : static GEN maxord(GEN p,GEN f,long mf);
     291             : static GEN ZX_Dedekind(GEN F, GEN *pg, GEN p);
     292             : 
     293             : static void
     294         455 : fix_PE(GEN *pP, GEN *pE, long i, GEN u, GEN N)
     295             : {
     296             :   GEN P, E;
     297         455 :   long k, l = lg(u), lP = lg(*pP);
     298             :   pari_sp av;
     299             : 
     300         455 :   *pP = P = shallowconcat(*pP, vecslice(u, 2, l-1));
     301         455 :   *pE = E = vecsmall_lengthen(*pE, lP + l-2);
     302         455 :   gel(P,i) = gel(u,1); av = avma;
     303         455 :   E[i] = Z_pvalrem(N, gel(P,i), &N);
     304         931 :   for (k=lP, lP=lg(P); k < lP; k++) E[k] = Z_pvalrem(N, gel(P,k), &N);
     305         455 :   set_avma(av);
     306         455 : }
     307             : static long
     308         553 : diag_denomval(GEN M, GEN p)
     309             : {
     310             :   long j, v, l;
     311         553 :   if (typ(M) != t_MAT) return 0;
     312         532 :   v = 0; l = lg(M);
     313        7926 :   for (j=1; j<l; j++)
     314             :   {
     315        7394 :     GEN t = gcoeff(M,j,j);
     316        7394 :     if (typ(t) == t_FRAC) v += Z_pval(gel(t,2), p);
     317             :   }
     318         532 :   return v;
     319             : }
     320             : /* Warning: data computed for T = ZX_Q_normalize(T0). If S.unscale !=
     321             :  * gen_1, caller must take steps to correct the components if it wishes
     322             :  * to stick to the original T0. Return a vector of p-maximal orders, for
     323             :  * those p s.t p^2 | disc(T) [ = S->dTP ]*/
     324             : static GEN
     325      810883 : get_maxord(nfmaxord_t *S, GEN T0, long flag)
     326             : {
     327             :   GEN P, E;
     328             :   VOLATILE GEN O;
     329             :   VOLATILE long lP, i, k;
     330             : 
     331      810883 :   nfmaxord_check_args(S, T0, flag);
     332      810836 :   P = S->dTP; lP = lg(P);
     333      810836 :   E = S->dTE;
     334      810836 :   O = cgetg(1, t_VEC);
     335     3135917 :   for (i=1; i<lP; i++)
     336             :   {
     337             :     VOLATILE pari_sp av;
     338             :     /* includes the silly case where P[i] = -1 */
     339     2325072 :     if (E[i] <= 1)
     340             :     {
     341     1302517 :       if (S->certify)
     342             :       {
     343     1294909 :         GEN p = gel(P,i);
     344     1294909 :         if (signe(p) > 0 && !BPSW_psp(p))
     345             :         {
     346         161 :           fix_PE(&P, &E, i, gel(Z_factor(p), 1), S->dT);
     347         161 :           lP = lg(P); i--; continue;
     348             :         }
     349             :       }
     350     1302378 :       O = vec_append(O, gen_1); continue;
     351             :     }
     352     1022555 :     av = avma;
     353     1022555 :     pari_CATCH(CATCH_ALL) {
     354         273 :       GEN u, err = pari_err_last();
     355             :       long l;
     356         273 :       switch(err_get_num(err))
     357             :       {
     358         273 :         case e_INV:
     359             :         {
     360         273 :           GEN p, x = err_get_compo(err, 2);
     361         273 :           if (typ(x) == t_INTMOD)
     362             :           { /* caught false prime, update factorization */
     363         273 :             p = gcdii(gel(x,1), gel(x,2));
     364         273 :             u = diviiexact(gel(x,1),p);
     365         273 :             if (DEBUGLEVEL) pari_warn(warner,"impossible inverse: %Ps", x);
     366         273 :             gerepileall(av, 2, &p, &u);
     367             : 
     368         273 :             u = get_coprimes(p, u); l = lg(u);
     369             :             /* no small factors, but often a prime power */
     370         819 :             for (k = 1; k < l; k++) (void)Z_isanypower(gel(u,k), &gel(u,k));
     371         273 :             break;
     372             :           }
     373             :           /* fall through */
     374             :         }
     375             :         case e_PRIME: case e_IRREDPOL:
     376             :         { /* we're here because we failed BPSW_isprime(), no point in
     377             :            * reporting a possible counter-example to the BPSW test */
     378           0 :           GEN p = gel(P,i);
     379           0 :           set_avma(av);
     380           0 :           if (DEBUGLEVEL)
     381           0 :             pari_warn(warner,"large composite in nfmaxord:loop(), %Ps", p);
     382           0 :           if (expi(p) < 100 || S->certify)
     383           0 :             u = gel(Z_factor(p), 1); /* factor(n < 2^100) should take ~20ms */
     384             :           else
     385             :           { /* give up, probably not maximal */
     386           0 :             GEN B, g, k = ZX_Dedekind(S->T, &g, p);
     387           0 :             k = FpX_normalize(k, p);
     388           0 :             B = dbasis(p, S->T, E[i], NULL, FpX_div(S->T,k,p));
     389           0 :             O = vec_append(O, B);
     390           0 :             pari_CATCH_reset(); continue;
     391             :           }
     392           0 :           break;
     393             :         }
     394           0 :         default: pari_err(0, err);
     395             :           return NULL;/*LCOV_EXCL_LINE*/
     396             :       }
     397         273 :       fix_PE(&P, &E, i, u, S->dT);
     398         273 :       lP = lg(P); av = avma;
     399     1022830 :     } pari_RETRY {
     400     1022830 :       GEN p = gel(P,i), O2;
     401     1022830 :       if (DEBUGLEVEL>2) err_printf("Treating p^k = %Ps^%ld\n",p,E[i]);
     402     1022830 :       O2 = maxord(p,S->T,E[i]);
     403     1022562 :       if (S->certify && !BPSW_psp(p)
     404         553 :                      && (odd(E[i]) || E[i] != 2*diag_denomval(O2, p)))
     405             :       {
     406          21 :         fix_PE(&P, &E, i, gel(Z_factor(p), 1), S->dT);
     407          21 :         lP = lg(P); i--;
     408             :       }
     409             :       else
     410     1022538 :         O = vec_append(O, O2);
     411     1022557 :     } pari_ENDCATCH;
     412             :   }
     413      810845 :   S->dTP = P; S->dTE = E; return O;
     414             : }
     415             : 
     416             : /* M a QM, return denominator of diagonal. All denominators are powers of
     417             :  * a given integer */
     418             : static GEN
     419       96097 : diag_denom(GEN M)
     420             : {
     421       96097 :   GEN d = gen_1;
     422       96097 :   long j, l = lg(M);
     423      672089 :   for (j=1; j<l; j++)
     424             :   {
     425      575992 :     GEN t = gcoeff(M,j,j);
     426      575992 :     if (typ(t) == t_INT) continue;
     427      205994 :     t = gel(t,2);
     428      205994 :     if (abscmpii(t,d) > 0) d = t;
     429             :   }
     430       96097 :   return d;
     431             : }
     432             : static void
     433      744406 : setPE(GEN D, GEN P, GEN *pP, GEN *pE)
     434             : {
     435      744406 :   long k, j, l = lg(P);
     436             :   GEN P2, E2;
     437      744406 :   *pP = P2 = cgetg(l, t_COL);
     438      744431 :   *pE = E2 = cgetg(l, t_VECSMALL);
     439     2862464 :   for (k = j = 1; j < l; j++)
     440             :   {
     441     2117983 :     long v = Z_pvalrem(D, gel(P,j), &D);
     442     2118003 :     if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
     443             :   }
     444      744481 :   setlg(P2, k);
     445      744478 :   setlg(E2, k);
     446      744477 : }
     447             : void
     448       99703 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
     449             : {
     450       99703 :   GEN O = get_maxord(S, T0, flag);
     451       99710 :   GEN f = S->T, P = S->dTP, a = NULL, da = NULL;
     452       99710 :   long n = degpol(f), lP = lg(P), i, j, k;
     453       99712 :   int centered = 0;
     454       99712 :   pari_sp av = avma;
     455             :   /* r1 & basden not initialized here */
     456       99712 :   S->r1 = -1;
     457       99712 :   S->basden = NULL;
     458      348703 :   for (i=1; i<lP; i++)
     459             :   {
     460      248992 :     GEN M, db, b = gel(O,i);
     461      248992 :     if (b == gen_1) continue;
     462       96097 :     db = diag_denom(b);
     463       96097 :     if (db == gen_1) continue;
     464             : 
     465             :     /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
     466       96097 :     b = Q_muli_to_int(b,db);
     467       96098 :     if (!da) { da = db; a = b; }
     468             :     else
     469             :     { /* optimization: easy as long as both matrix are diagonal */
     470      128748 :       j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
     471       48121 :       k = j-1; M = cgetg(2*n-k+1,t_MAT);
     472      176865 :       for (j=1; j<=k; j++)
     473             :       {
     474      128744 :         gel(M,j) = gel(a,j);
     475      128744 :         gcoeff(M,j,j) = mulii(gcoeff(a,j,j),gcoeff(b,j,j));
     476             :       }
     477             :       /* could reduce mod M(j,j) but not worth it: usually close to da*db */
     478      268855 :       for (  ; j<=n;     j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
     479      268844 :       for (  ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
     480       48111 :       da = mulii(da,db);
     481       48118 :       a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
     482       48119 :       gerepileall(av, 2, &a, &da);
     483       48119 :       centered = 1;
     484             :     }
     485             :   }
     486       99711 :   if (da)
     487             :   {
     488       47976 :     GEN index = diviiexact(da, gcoeff(a,1,1));
     489      226479 :     for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
     490       47973 :     if (!centered) a = ZM_hnfcenter(a);
     491       47974 :     a = RgM_Rg_div(a, da);
     492       47977 :     S->index = index;
     493       47977 :     S->dK = diviiexact(S->dT, sqri(index));
     494             :   }
     495             :   else
     496             :   {
     497       51735 :     S->index = gen_1;
     498       51735 :     S->dK = S->dT;
     499       51735 :     a = matid(n);
     500             :   }
     501       99710 :   setPE(S->dK, P, &S->dKP, &S->dKE);
     502       99707 :   S->basis = RgM_to_RgXV(a, varn(f));
     503       99707 : }
     504             : GEN
     505         938 : nfbasis(GEN x, GEN *pdK)
     506             : {
     507         938 :   pari_sp av = avma;
     508             :   nfmaxord_t S;
     509             :   GEN B;
     510         938 :   nfmaxord(&S, x, 0);
     511         938 :   B = RgXV_unscale(S.basis, S.unscale);
     512         938 :   if (pdK) *pdK = S.dK;
     513         938 :   return gc_all(av, pdK? 2: 1, &B, pdK);
     514             : }
     515             : /* field discriminant: faster than nfmaxord, use local data only */
     516             : static GEN
     517      711168 : maxord_disc(nfmaxord_t *S, GEN x)
     518             : {
     519      711168 :   GEN O = get_maxord(S, x, 0), I = gen_1;
     520      711156 :   long n = degpol(S->T), lP = lg(O), i, j;
     521     2786979 :   for (i = 1; i < lP; i++)
     522             :   {
     523     2075864 :     GEN b = gel(O,i);
     524     2075864 :     if (b == gen_1) continue;
     525     2699879 :     for (j = 1; j <= n; j++)
     526             :     {
     527     2109622 :       GEN c = gcoeff(b,j,j);
     528     2109622 :       if (typ(c) == t_FRAC) I = mulii(I, gel(c,2)) ;
     529             :     }
     530             :   }
     531      711115 :   return diviiexact(S->dT, sqri(I));
     532             : }
     533             : GEN
     534       66429 : nfdisc(GEN x)
     535             : {
     536       66429 :   pari_sp av = avma;
     537             :   nfmaxord_t S;
     538       66429 :   return gerepileuptoint(av, maxord_disc(&S, x));
     539             : }
     540             : GEN
     541      644760 : nfdiscfactors(GEN x)
     542             : {
     543      644760 :   pari_sp av = avma;
     544      644760 :   GEN E, P, D, nf = checknf_i(x);
     545      644758 :   if (nf)
     546             :   {
     547           7 :     D = nf_get_disc(nf);
     548           7 :     P = nf_get_ramified_primes(nf);
     549             :   }
     550             :   else
     551             :   {
     552             :     nfmaxord_t S;
     553      644751 :     D = maxord_disc(&S, x);
     554      644668 :     P = S.dTP;
     555             :   }
     556      644675 :   setPE(D, P, &P, &E); settyp(P, t_COL);
     557      644769 :   return gerepilecopy(av, mkvec2(D, mkmat2(P, zc_to_ZC(E))));
     558             : }
     559             : 
     560             : static ulong
     561     1590436 : Flx_checkdeflate(GEN x)
     562             : {
     563     1590436 :   ulong d = 0, i, lx = (ulong)lg(x);
     564     2536129 :   for (i=3; i<lx; i++)
     565     1693207 :     if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
     566     1590452 :   return d;
     567             : }
     568             : 
     569             : /* product of (monic) irreducible factors of f over Fp[X]
     570             :  * Assume f reduced mod p, otherwise valuation at x may be wrong */
     571             : static GEN
     572     1590425 : Flx_radical(GEN f, ulong p)
     573             : {
     574     1590425 :   long v0 = Flx_valrem(f, &f);
     575             :   ulong du, d, e;
     576             :   GEN u;
     577             : 
     578     1590436 :   d = Flx_checkdeflate(f);
     579     1590518 :   if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
     580      994630 :   if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
     581      994631 :   u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
     582      994613 :   du = degpol(u);
     583      994614 :   if (du)
     584             :   {
     585      311507 :     if (du == (ulong)degpol(f))
     586           0 :       f = Flx_radical(Flx_deflate(f,p), p);
     587             :     else
     588             :     {
     589      311509 :       u = Flx_normalize(u, p);
     590      311504 :       f = Flx_div(f, u, p);
     591      311505 :       if (p <= du)
     592             :       {
     593       66195 :         GEN w = (degpol(f) >= degpol(u))? Flx_rem(f, u, p): f;
     594       66194 :         w = Flxq_powu(w, du, u, p);
     595       66198 :         w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
     596       66197 :         f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
     597             :       }
     598             :     }
     599             :   }
     600      994611 :   if (v0) f = Flx_shift(f, 1);
     601      994582 :   return f;
     602             : }
     603             : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
     604             : static GEN
     605        5487 : FpX_radical(GEN f, GEN p)
     606             : {
     607             :   GEN u;
     608             :   long v0;
     609        5487 :   if (lgefint(p) == 3)
     610             :   {
     611        1717 :     ulong q = p[2];
     612        1717 :     return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
     613             :   }
     614        3770 :   v0 = ZX_valrem(f, &f);
     615        3770 :   u = FpX_gcd(f,FpX_deriv(f, p), p);
     616        3503 :   if (degpol(u)) f = FpX_div(f, u, p);
     617        3503 :   if (v0) f = RgX_shift(f, 1);
     618        3503 :   return f;
     619             : }
     620             : /* f / a */
     621             : static GEN
     622     1522539 : zx_z_div(GEN f, ulong a)
     623             : {
     624     1522539 :   long i, l = lg(f);
     625     1522539 :   GEN g = cgetg(l, t_VECSMALL);
     626     1522596 :   g[1] = f[1];
     627     5143388 :   for (i = 2; i < l; i++) g[i] = f[i] / a;
     628     1522596 :   return g;
     629             : }
     630             : /* Dedekind criterion; return k = gcd(g,h, (f-gh)/p), where
     631             :  *   f = \prod f_i^e_i, g = \prod f_i, h = \prod f_i^{e_i-1}
     632             :  * k = 1 iff Z[X]/(f) is p-maximal */
     633             : static GEN
     634     1528125 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
     635             : {
     636             :   GEN k, h, g, f, f2;
     637     1528125 :   ulong q = p[2];
     638     1528125 :   if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
     639     1522506 :   {
     640     1522643 :     ulong q2 = q*q;
     641     1522643 :     f2 = ZX_to_Flx(F, q2);
     642     1522537 :     f = Flx_red(f2, q);
     643     1522529 :     g = Flx_radical(f, q);
     644     1522559 :     h = Flx_div(f, g, q);
     645     1522543 :     k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
     646     1522602 :     k = Flx_gcd(k, Flx_gcd(g,h,q), q);
     647     1522574 :     k = Flx_to_ZX(k);
     648     1522481 :     g = Flx_to_ZX(g);
     649             :   }
     650             :   else
     651             :   {
     652        5482 :     f2 = FpX_red(F, sqri(p));
     653        5487 :     f = FpX_red(f2, p);
     654        5487 :     g = FpX_radical(f, p);
     655        5214 :     h = FpX_div(f, g, p);
     656        5214 :     k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
     657        5214 :     k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
     658             :   }
     659     1527718 :   *pg = g; return k;
     660             : }
     661             : 
     662             : /* p-maximal order of Z[x]/f; mf = v_p(Disc(f)) or < 0 [unknown].
     663             :  * Return gen_1 if p-maximal */
     664             : static GEN
     665     1528114 : maxord(GEN p, GEN f, long mf)
     666             : {
     667     1528114 :   const pari_sp av = avma;
     668     1528114 :   GEN res, g, k = ZX_Dedekind(f, &g, p);
     669     1527715 :   long dk = degpol(k);
     670     1527722 :   if (DEBUGLEVEL>2) err_printf("  ZX_Dedekind: gcd has degree %ld\n", dk);
     671     1527769 :   if (!dk) { set_avma(av); return gen_1; }
     672      868999 :   if (mf < 0) mf = ZpX_disc_val(f, p);
     673      869001 :   k = FpX_normalize(k, p);
     674      869033 :   if (2*dk >= mf-1)
     675      415705 :     res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
     676             :   else
     677             :   {
     678             :     GEN w, F1, F2;
     679             :     decomp_t S;
     680      453328 :     F1 = FpX_factor(k,p);
     681      453354 :     F2 = FpX_factor(FpX_div(g,k,p),p);
     682      453352 :     w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
     683      453348 :     res = maxord_i(&S, p, f, mf, w, 0);
     684             :   }
     685      869069 :   return gerepilecopy(av,res);
     686             : }
     687             : /* T monic separable ZX, p prime */
     688             : GEN
     689           0 : ZpX_primedec(GEN T, GEN p)
     690             : {
     691           0 :   const pari_sp av = avma;
     692           0 :   GEN w, F1, F2, res, g, k = ZX_Dedekind(T, &g, p);
     693             :   decomp_t S;
     694           0 :   if (!degpol(k)) return zm_to_ZM(FpX_degfact(T, p));
     695           0 :   k = FpX_normalize(k, p);
     696           0 :   F1 = FpX_factor(k,p);
     697           0 :   F2 = FpX_factor(FpX_div(g,k,p),p);
     698           0 :   w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
     699           0 :   res = maxord_i(&S, p, T, ZpX_disc_val(T, p), w, -1);
     700           0 :   if (!res)
     701             :   {
     702           0 :     long f = degpol(S.nu), e = degpol(T) / f;
     703           0 :     set_avma(av); retmkmat2(mkcols(f), mkcols(e));
     704             :   }
     705           0 :   return gerepilecopy(av,res);
     706             : }
     707             : 
     708             : static GEN
     709     3911125 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
     710             : {
     711     3911125 :   long j, n = degpol(f1);
     712     3911086 :   GEN h, a = cgetg(n+1,t_MAT);
     713     3911110 :   f1 = Flx_get_red(f1, pm);
     714     3911076 :   h = Flx_rem(f2,f1,pm);
     715     3911028 :   for (j=1;; j++)
     716             :   {
     717    14898307 :     gel(a,j) = Flx_to_Flv(h, n);
     718    14897370 :     if (j == n) break;
     719    10986422 :     h = Flx_rem(Flx_shift(h, 1), f1, pm);
     720             :   }
     721     3910948 :   return zlm_echelon(a, early_abort, p, pm);
     722             : }
     723             : /* Sylvester's matrix, mod p^m (assumes f1 monic). If early_abort
     724             :  * is set, return NULL if one pivot is 0 mod p^m */
     725             : static GEN
     726       26517 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
     727             : {
     728       26517 :   long j, n = degpol(f1);
     729       26517 :   GEN h, a = cgetg(n+1,t_MAT);
     730       26517 :   h = FpXQ_red(f2,f1,pm);
     731       26517 :   for (j=1;; j++)
     732             :   {
     733      307241 :     gel(a,j) = RgX_to_RgC(h, n);
     734      307242 :     if (j == n) break;
     735      280725 :     h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
     736             :   }
     737       26517 :   return ZpM_echelon(a, early_abort, p, pm);
     738             : }
     739             : 
     740             : /* polynomial gcd mod p^m (assumes f1 monic). Return a QpX ! */
     741             : static GEN
     742      244837 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
     743             : {
     744      244837 :   pari_sp av = avma;
     745      244837 :   GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
     746      244842 :   long c, l = lg(a), sv = f1[1];
     747      749745 :   for (c = 1; c < l; c++)
     748             :   {
     749      749746 :     ulong t = ucoeff(a,c,c);
     750      749746 :     if (t)
     751             :     {
     752      244843 :       a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
     753      244837 :       if (t == 1) return gerepilecopy(av, a);
     754       74978 :       return gerepileupto(av, RgX_Rg_div(a, utoipos(t)));
     755             :     }
     756             :   }
     757           0 :   set_avma(av);
     758           0 :   a = cgetg(2,t_POL); a[1] = sv; return a;
     759             : }
     760             : GEN
     761      252929 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
     762             : {
     763      252929 :   pari_sp av = avma;
     764             :   GEN a;
     765             :   long c, l, v;
     766      252929 :   if (lgefint(pm) == 3)
     767             :   {
     768      244843 :     ulong q = pm[2];
     769      244843 :     return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
     770             :   }
     771        8086 :   a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
     772        8086 :   l = lg(a); v = varn(f1);
     773       51163 :   for (c = 1; c < l; c++)
     774             :   {
     775       51163 :     GEN t = gcoeff(a,c,c);
     776       51163 :     if (signe(t))
     777             :     {
     778        8086 :       a = RgV_to_RgX(gel(a,c), v);
     779        8086 :       if (equali1(t)) return gerepilecopy(av, a);
     780        2349 :       return gerepileupto(av, RgX_Rg_div(a, t));
     781             :     }
     782             :   }
     783           0 :   set_avma(av); return pol_0(v);
     784             : }
     785             : 
     786             : /* Return m > 0, such that p^m ~ 2^16 for initial value of m; p > 1 */
     787             : static long
     788     3582082 : init_m(GEN p)
     789             : {
     790     3582082 :   if (lgefint(p) > 3) return 1;
     791     3581222 :   return (long)(16 / log2(p[2]));
     792             : }
     793             : 
     794             : /* reduced resultant mod p^m (assumes x monic) */
     795             : GEN
     796      988359 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
     797             : {
     798      988359 :   pari_sp av = avma;
     799             :   GEN z;
     800      988359 :   if (lgefint(pm) == 3)
     801             :   {
     802      977809 :     ulong q = pm[2];
     803      977809 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
     804      977893 :     if (lg(z) > 1)
     805             :     {
     806      977889 :       ulong c = ucoeff(z,1,1);
     807      977889 :       if (c) { set_avma(av); return utoipos(c); }
     808             :     }
     809             :   }
     810             :   else
     811             :   {
     812       10550 :     z = ZpX_sylvester_echelon(x,y,0,p,pm);
     813       10559 :     if (lg(z) > 1)
     814             :     {
     815       10559 :       GEN c = gcoeff(z,1,1);
     816       10559 :       if (signe(c)) return gerepileuptoint(av, c);
     817             :     }
     818             :   }
     819      124323 :   set_avma(av); return gen_0;
     820             : }
     821             : /* Assume Res(f,g) divides p^M. Return Res(f, g), using dynamic p-adic
     822             :  * precision (until result is nonzero or p^M). */
     823             : GEN
     824      930250 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
     825             : {
     826      930250 :   GEN R, q = NULL;
     827             :   long m;
     828      930250 :   m = init_m(p); if (m < 1) m = 1;
     829       58087 :   for(;; m <<= 1) {
     830      988387 :     if (M < 2*m) break;
     831       90312 :     q = q? sqri(q): powiu(p, m); /* p^m */
     832       90312 :     R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
     833             :   }
     834      898075 :   q = powiu(p, M);
     835      898023 :   R = ZpX_reduced_resultant(f,g, p, q); return signe(R)? R: q;
     836             : }
     837             : 
     838             : /* v_p(Res(x,y) mod p^m), assumes (lc(x),p) = 1 */
     839             : static long
     840     2696295 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
     841             : {
     842     2696295 :   pari_sp av = avma;
     843             :   GEN z;
     844             :   long i, l, v;
     845     2696295 :   if (lgefint(pm) == 3)
     846             :   {
     847     2688428 :     ulong q = pm[2], pp = p[2];
     848     2688428 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
     849     2688627 :     if (!z) return gc_long(av,-1); /* failure */
     850     2503641 :     v = 0; l = lg(z);
     851    11369041 :     for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
     852             :   }
     853             :   else
     854             :   {
     855        7867 :     z = ZpX_sylvester_echelon(x, y, 1, p, pm);
     856        7872 :     if (!z) return gc_long(av,-1); /* failure */
     857        7195 :     v = 0; l = lg(z);
     858       53951 :     for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
     859             :   }
     860     2510830 :   return v;
     861             : }
     862             : 
     863             : /* assume (lc(f),p) = 1; no assumption on g */
     864             : long
     865     2651843 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
     866             : {
     867     2651843 :   pari_sp av = avma;
     868     2651843 :   GEN q = NULL;
     869             :   long v, m;
     870     2651843 :   m = init_m(p); if (m < 2) m = 2;
     871       44485 :   for(;; m <<= 1) {
     872     2696339 :     if (m > M) m = M;
     873     2696339 :     q = q? sqri(q): powiu(p, m); /* p^m */
     874     2696277 :     v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) return gc_long(av,v);
     875      185662 :     if (m == M) return gc_long(av,M);
     876             :   }
     877             : }
     878             : 
     879             : /* assume f separable and (lc(f),p) = 1 */
     880             : long
     881      183531 : ZpX_disc_val(GEN f, GEN p)
     882             : {
     883      183531 :   pari_sp av = avma;
     884             :   long v;
     885      183531 :   if (degpol(f) == 1) return 0;
     886      183531 :   v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
     887      183532 :   return gc_long(av,v);
     888             : }
     889             : 
     890             : /* *e a ZX, *d, *z in Z, *d = p^(*vd). Simplify e / d by cancelling a
     891             :  * common factor p^v; if z!=NULL, update it by cancelling the same power of p */
     892             : static void
     893     3532524 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
     894             : {
     895             :   GEN newe;
     896     3532524 :   long ve = ZX_pvalrem(*e, p, &newe);
     897     3532647 :   if (ve) {
     898             :     GEN newd;
     899     1729879 :     long v = minss(*vd, ve);
     900     1730040 :     if (v) {
     901     1730145 :       if (v == *vd)
     902             :       { /* rare, denominator cancelled */
     903      375896 :         if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
     904      375896 :         newd = gen_1;
     905      375896 :         *vd = 0;
     906      375896 :         if (z) *z =diviiexact(*z, powiu(p, v));
     907             :       }
     908             :       else
     909             :       { /* v = ve < vd, generic case */
     910     1354249 :         GEN q = powiu(p, v);
     911     1354150 :         newd = diviiexact(*d, q);
     912     1353858 :         *vd -= v;
     913     1353858 :         if (z) *z = diviiexact(*z, q);
     914             :       }
     915     1729707 :       *e = newe;
     916     1729707 :       *d = newd;
     917             :     }
     918             :   }
     919     3532370 : }
     920             : 
     921             : /* return denominator, a power of p */
     922             : static GEN
     923     2733158 : QpX_denom(GEN x)
     924             : {
     925     2733158 :   long i, l = lg(x);
     926     2733158 :   GEN maxd = gen_1;
     927     9439217 :   for (i=2; i<l; i++)
     928             :   {
     929     6706057 :     GEN d = gel(x,i);
     930     6706057 :     if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
     931             :   }
     932     2733160 :   return maxd;
     933             : }
     934             : static GEN
     935      505278 : QpXV_denom(GEN x)
     936             : {
     937      505278 :   long l = lg(x), i;
     938      505278 :   GEN maxd = gen_1;
     939     1502896 :   for (i = 1; i < l; i++)
     940             :   {
     941      997619 :     GEN d = QpX_denom(gel(x,i));
     942      997617 :     if (cmpii(d, maxd) > 0) maxd = d;
     943             :   }
     944      505277 :   return maxd;
     945             : }
     946             : 
     947             : static GEN
     948     1735558 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
     949             : {
     950     1735558 :   *pdx = QpX_denom(x);
     951     1735563 :   if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
     952             :   else {
     953     1260320 :     x = Q_muli_to_int(x,*pdx);
     954     1260276 :     *pv = Z_pval(*pdx, p);
     955             :   }
     956     1735495 :   return x;
     957             : }
     958             : 
     959             : /* p^v * f o g mod (T,q). q = p^vq  */
     960             : static GEN
     961      285628 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
     962             : {
     963      285628 :   GEN D = NULL, z, df, dg, qD;
     964      285628 :   long vD = 0, vdf, vdg;
     965             : 
     966      285628 :   f = QpX_remove_denom(f, p, &df, &vdf);
     967      285628 :   if (typ(g) == t_VEC) /* [num,den,v_p(den)] */
     968           0 :   { vdg = itos(gel(g,3)); dg = gel(g,2); g = gel(g,1); }
     969             :   else
     970      285628 :     g = QpX_remove_denom(g, p, &dg, &vdg);
     971      285626 :   if (df) { D = df; vD = vdf; }
     972      285626 :   if (dg) {
     973       54706 :     long degf = degpol(f);
     974       54706 :     D = mul_content(D, powiu(dg, degf));
     975       54706 :     vD += degf * vdg;
     976             :   }
     977      285626 :   qD = D ? mulii(q, D): q;
     978      285613 :   if (dg) f = FpX_rescale(f, dg, qD);
     979      285616 :   z = FpX_FpXQ_eval(f, g, T, qD);
     980      285624 :   if (!D) {
     981           0 :     if (v) {
     982           0 :       if (v > 0)
     983           0 :         z = ZX_Z_mul(z, powiu(p, v));
     984             :       else
     985           0 :         z = RgX_Rg_div(z, powiu(p, -v));
     986             :     }
     987           0 :     return z;
     988             :   }
     989      285624 :   update_den(p, &z, &D, &vD, NULL);
     990      285623 :   qD = mulii(D,q);
     991      285613 :   if (v) vD -= v;
     992      285613 :   z = FpX_center_i(z, qD, shifti(qD,-1));
     993      285629 :   if (vD > 0)
     994      285629 :     z = RgX_Rg_div(z, powiu(p, vD));
     995           0 :   else if (vD < 0)
     996           0 :     z = ZX_Z_mul(z, powiu(p, -vD));
     997      285627 :   return z;
     998             : }
     999             : 
    1000             : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
    1001             : static GEN
    1002      453358 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
    1003             : {
    1004      453358 :   long i, l = lg(M);
    1005      453358 :   M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
    1006     2021370 :   for (i = 1; i < l; i++)
    1007     1568007 :     if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
    1008      453363 :   return M;
    1009             : }
    1010             : 
    1011             : /* Return Z-basis for Z[a] + U(a)/p Z[a] in Z[t]/(f), mf = v_p(disc f), U
    1012             :  * a ZX. Special cases: a = t is coded as NULL, U = 0 is coded as NULL */
    1013             : static GEN
    1014      616415 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
    1015             : {
    1016      616415 :   long n = degpol(f), i, dU;
    1017             :   GEN b, h;
    1018             : 
    1019      616426 :   if (n == 1) return matid(1);
    1020      616426 :   if (a && gequalX(a)) a = NULL;
    1021      616426 :   if (DEBUGLEVEL>5)
    1022             :   {
    1023           0 :     err_printf("  entering Dedekind Basis with parameters p=%Ps\n",p);
    1024           0 :     err_printf("  f = %Ps,\n  a = %Ps\n",f, a? a: pol_x(varn(f)));
    1025             :   }
    1026      616426 :   if (a)
    1027             :   {
    1028      200720 :     GEN pd = powiu(p, mf >> 1);
    1029      200717 :     GEN da, pdp = mulii(pd,p), D = pdp;
    1030             :     long vda;
    1031      200707 :     dU = U ? degpol(U): 0;
    1032      200708 :     b = cgetg(n+1, t_MAT);
    1033      200716 :     h = scalarpol(pd, varn(f));
    1034      200717 :     a = QpX_remove_denom(a, p, &da, &vda);
    1035      200719 :     if (da) D = mulii(D, da);
    1036      200713 :     gel(b,1) = scalarcol_shallow(pd, n);
    1037      570376 :     for (i=2; i<=n; i++)
    1038             :     {
    1039      369658 :       if (i == dU+1)
    1040           0 :         h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
    1041             :       else
    1042             :       {
    1043      369658 :         h = FpXQ_mul(h, a, f, D);
    1044      369651 :         if (da) h = ZX_Z_divexact(h, da);
    1045             :       }
    1046      369618 :       gel(b,i) = RgX_to_RgC(h,n);
    1047             :     }
    1048      200718 :     return ZpM_hnfmodid(b, p, pd);
    1049             :   }
    1050             :   else
    1051             :   {
    1052      415706 :     if (!U) return matid(n);
    1053      415706 :     dU = degpol(U);
    1054      415704 :     if (dU == n) return matid(n);
    1055      415704 :     U = FpX_normalize(U, p);
    1056      415704 :     b = cgetg(n+1, t_MAT);
    1057     1609483 :     for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
    1058      415711 :     h = RgX_Rg_div(U, p);
    1059      465516 :     for ( ; i <= n; i++)
    1060             :     {
    1061      465516 :       gel(b, i) = RgX_to_RgC(h,n);
    1062      465518 :       if (i == n) break;
    1063       49811 :       h = RgX_shift_shallow(h,1);
    1064             :     }
    1065      415707 :     return b;
    1066             :   }
    1067             : }
    1068             : 
    1069             : static GEN
    1070      505283 : get_partial_order_as_pols(GEN p, GEN f)
    1071             : {
    1072      505283 :   GEN O = maxord(p, f, -1);
    1073      505275 :   long v = varn(f);
    1074      505275 :   return O == gen_1? pol_x_powers(degpol(f), v): RgM_to_RgXV(O, v);
    1075             : }
    1076             : 
    1077             : static long
    1078        2232 : p_is_prime(decomp_t *S)
    1079             : {
    1080        2232 :   if (S->pisprime < 0) S->pisprime = BPSW_psp(S->p);
    1081        2232 :   return S->pisprime;
    1082             : }
    1083             : static GEN ZpX_monic_factor_squarefree(GEN f, GEN p, long prec);
    1084             : 
    1085             : /* if flag = 0, maximal order, else factorization to precision r = flag */
    1086             : static GEN
    1087      252924 : Decomp(decomp_t *S, long flag)
    1088             : {
    1089      252924 :   pari_sp av = avma;
    1090             :   GEN fred, pr2, pr, pk, ph2, ph, b1, b2, a, e, de, f1, f2, dt, th, chip;
    1091      252924 :   GEN p = S->p;
    1092      252924 :   long vde, vdt, k, r = maxss(flag, 2*S->df + 1);
    1093             : 
    1094      252925 :   if (DEBUGLEVEL>5) err_printf("  entering Decomp: %Ps^%ld\n  f = %Ps\n",
    1095             :                                p, r, S->f);
    1096      252925 :   else if (DEBUGLEVEL>2) err_printf("  entering Decomp\n");
    1097      252925 :   chip = FpX_red(S->chi, p);
    1098      252923 :   if (!FpX_valrem(chip, S->nu, p, &b1))
    1099             :   {
    1100           0 :     if (!p_is_prime(S)) pari_err_PRIME("Decomp",p);
    1101           0 :     pari_err_BUG("Decomp (not a factor)");
    1102             :   }
    1103      252928 :   b2 = FpX_div(chip, b1, p);
    1104      252927 :   a = FpX_mul(FpXQ_inv(b2, b1, p), b2, p);
    1105             :   /* E = e / de, e in Z[X], de in Z,  E = a(phi) mod (f, p) */
    1106      252922 :   th = QpX_remove_denom(S->phi, p, &dt, &vdt);
    1107      252928 :   if (dt)
    1108             :   {
    1109      121721 :     long dega = degpol(a);
    1110      121721 :     vde = dega * vdt;
    1111      121721 :     de = powiu(dt, dega);
    1112      121716 :     pr = mulii(p, de);
    1113      121713 :     a = FpX_rescale(a, dt, pr);
    1114             :   }
    1115             :   else
    1116             :   {
    1117      131207 :     vde = 0;
    1118      131207 :     de = gen_1;
    1119      131207 :     pr = p;
    1120             :   }
    1121      252926 :   e = FpX_FpXQ_eval(a, th, S->f, pr);
    1122      252923 :   update_den(p, &e, &de, &vde, NULL);
    1123             : 
    1124      252926 :   pk = p; k = 1;
    1125             :   /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
    1126     1169480 :   while (k < r + vde)
    1127             :   { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
    1128             :     GEN D;
    1129      916559 :     pk = sqri(pk); k <<= 1;
    1130      916507 :     e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
    1131      916583 :     de= mulii(de, sqri(de));
    1132      916507 :     vde *= 3;
    1133      916507 :     D = mulii(pk, de);
    1134      916517 :     e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
    1135      916530 :     update_den(p, &e, &de, &vde, NULL);
    1136             :   }
    1137             :   /* required precision of the factors */
    1138      252921 :   pr = powiu(p, r); pr2 = shifti(pr, -1);
    1139      252920 :   ph = mulii(de,pr);ph2 = shifti(ph, -1);
    1140      252923 :   e = FpX_center_i(FpX_red(e, ph), ph, ph2);
    1141      252923 :   fred = FpX_red(S->f, ph);
    1142             : 
    1143      252924 :   f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
    1144      252926 :   if (!is_pm1(de))
    1145             :   {
    1146      121719 :     fred = FpX_red(fred, pr);
    1147      121719 :     f1 = FpX_red(f1, pr);
    1148             :   }
    1149      252926 :   f2 = FpX_div(fred,f1, pr);
    1150      252925 :   f1 = FpX_center_i(f1, pr, pr2);
    1151      252926 :   f2 = FpX_center_i(f2, pr, pr2);
    1152             : 
    1153      252928 :   if (DEBUGLEVEL>5)
    1154           0 :     err_printf("  leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
    1155             : 
    1156      252928 :   if (flag < 0)
    1157             :   {
    1158           0 :     GEN m = vconcat(ZpX_primedec(f1, p), ZpX_primedec(f2, p));
    1159           0 :     return sort_factor(m, (void*)&cmpii, &cmp_nodata);
    1160             :   }
    1161      252928 :   else if (flag)
    1162             :   {
    1163         287 :     gerepileall(av, 2, &f1, &f2);
    1164         287 :     return shallowconcat(ZpX_monic_factor_squarefree(f1, p, flag),
    1165             :                          ZpX_monic_factor_squarefree(f2, p, flag));
    1166             :   } else {
    1167             :     GEN D, d1, d2, B1, B2, M;
    1168             :     long n, n1, n2, i;
    1169      252641 :     gerepileall(av, 4, &f1, &f2, &e, &de);
    1170      252641 :     D = de;
    1171      252641 :     B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
    1172      252642 :     B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
    1173      252639 :     d1 = QpXV_denom(B1);
    1174      252638 :     d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
    1175      252639 :     if (d1 != gen_1) {
    1176      156226 :       B1 = Q_muli_to_int(B1, d1);
    1177      156226 :       B2 = Q_muli_to_int(B2, d1);
    1178      156224 :       D = mulii(d1, D);
    1179             :     }
    1180      252637 :     fred = centermod_i(S->f, D, shifti(D,-1));
    1181      252638 :     M = cgetg(n+1, t_MAT);
    1182      799929 :     for (i=1; i<=n1; i++)
    1183      547289 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
    1184      252640 :     e = Z_ZX_sub(de, e); B2 -= n1;
    1185      702971 :     for (   ; i<=n; i++)
    1186      450331 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
    1187      252640 :     return ZpM_hnfmodid(M, p, D);
    1188             :   }
    1189             : }
    1190             : 
    1191             : /* minimum extension valuation: L/E */
    1192             : static void
    1193      620345 : vstar(GEN p,GEN h, long *L, long *E)
    1194             : {
    1195      620345 :   long first, j, k, v, w, m = degpol(h);
    1196             : 
    1197      620343 :   first = 1; k = 1; v = 0;
    1198     2555657 :   for (j=1; j<=m; j++)
    1199             :   {
    1200     1935308 :     GEN c = gel(h, m-j+2);
    1201     1935308 :     if (signe(c))
    1202             :     {
    1203     1859695 :       w = Z_pval(c,p);
    1204     1859701 :       if (first || w*k < v*j) { v = w; k = j; }
    1205     1859701 :       first = 0;
    1206             :     }
    1207             :   }
    1208             :   /* v/k = min_j ( v_p(h_{m-j}) / j ) */
    1209      620349 :   w = (long)ugcd(v,k);
    1210      620349 :   *L = v/w;
    1211      620349 :   *E = k/w;
    1212      620349 : }
    1213             : 
    1214             : static GEN
    1215       63385 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
    1216             : {
    1217             :   GEN z;
    1218       63385 :   a = Q_remove_denom(a, pda);
    1219       63384 :   *pvda = 0;
    1220       63384 :   if (*pda)
    1221             :   {
    1222       63384 :     long v = Z_pvalrem(*pda, p, &z);
    1223       63384 :     if (v) {
    1224       63384 :       *pda = powiu(p, v);
    1225       63382 :       *pvda = v;
    1226       63382 :       N  = mulii(*pda, N);
    1227             :     }
    1228             :     else
    1229           0 :       *pda = NULL;
    1230       63380 :     if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
    1231             :   }
    1232       63384 :   return centermod(a, N);
    1233             : }
    1234             : /* reduce the element a modulo N [ a power of p ], taking first care of the
    1235             :  * denominators */
    1236             : static GEN
    1237       47937 : redelt(GEN a, GEN N, GEN p)
    1238             : {
    1239             :   GEN da;
    1240             :   long vda;
    1241       47937 :   a = redelt_i(a, N, p, &da, &vda);
    1242       47936 :   if (da) a = RgX_Rg_div(a, da);
    1243       47937 :   return a;
    1244             : }
    1245             : 
    1246             : /* compute the c first Newton sums modulo pp of the
    1247             :    characteristic polynomial of a/d mod chi, d > 0 power of p (NULL = gen_1),
    1248             :    a, chi in Zp[X], vda = v_p(da)
    1249             :    ns = Newton sums of chi */
    1250             : static GEN
    1251      703352 : newtonsums(GEN p, GEN a, GEN da, long vda, GEN chi, long c, GEN pp, GEN ns)
    1252             : {
    1253             :   GEN va, pa, dpa, s;
    1254      703352 :   long j, k, vdpa, lns = lg(ns);
    1255             :   pari_sp av;
    1256             : 
    1257      703352 :   a = centermod(a, pp); av = avma;
    1258      703324 :   dpa = pa = NULL; /* -Wall */
    1259      703324 :   vdpa = 0;
    1260      703324 :   va = zerovec(c);
    1261     2888227 :   for (j = 1; j <= c; j++)
    1262             :   { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
    1263             :     long l;
    1264     2191524 :     pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
    1265     2191590 :     l = lg(pa); if (l == 2) break;
    1266     2191590 :     if (lns < l) l = lns;
    1267             : 
    1268     2191590 :     if (da) {
    1269     2055866 :       dpa = j == 1? da: mulii(dpa, da);
    1270     2055446 :       vdpa += vda;
    1271     2055446 :       update_den(p, &pa, &dpa, &vdpa, &pp);
    1272             :     }
    1273     2191190 :     s = mulii(gel(pa,2), gel(ns,2)); /* k = 2 */
    1274    10816105 :     for (k = 3; k < l; k++) s = addii(s, mulii(gel(pa,k), gel(ns,k)));
    1275     2190654 :     if (da) {
    1276             :       GEN r;
    1277     2055079 :       s = dvmdii(s, dpa, &r);
    1278     2055194 :       if (r != gen_0) return NULL;
    1279             :     }
    1280     2184204 :     gel(va,j) = centermodii(s, pp, shifti(pp,-1));
    1281             : 
    1282     2184379 :     if (gc_needed(av, 1))
    1283             :     {
    1284           7 :       if(DEBUGMEM>1) pari_warn(warnmem, "newtonsums");
    1285           7 :       gerepileall(av, dpa?4:3, &pa, &va, &pp, &dpa);
    1286             :     }
    1287             :   }
    1288      696703 :   for (; j <= c; j++) gel(va,j) = gen_0;
    1289      696703 :   return va;
    1290             : }
    1291             : 
    1292             : /* compute the characteristic polynomial of a/da mod chi (a in Z[X]), given
    1293             :  * by its Newton sums to a precision of pp using Newton sums */
    1294             : static GEN
    1295      696733 : newtoncharpoly(GEN pp, GEN p, GEN NS)
    1296             : {
    1297      696733 :   long n = lg(NS)-1, j, k;
    1298      696733 :   GEN c = cgetg(n + 2, t_VEC), pp2 = shifti(pp,-1);
    1299             : 
    1300      696680 :   gel(c,1) = (n & 1 ? gen_m1: gen_1);
    1301     2871581 :   for (k = 2; k <= n+1; k++)
    1302             :   {
    1303     2174898 :     pari_sp av2 = avma;
    1304     2174898 :     GEN s = gen_0;
    1305             :     ulong z;
    1306     2174898 :     long v = u_pvalrem(k - 1, p, &z);
    1307     9172080 :     for (j = 1; j < k; j++)
    1308             :     {
    1309     6998927 :       GEN t = mulii(gel(NS,j), gel(c,k-j));
    1310     6996213 :       if (!odd(j)) t = negi(t);
    1311     6997198 :       s = addii(s, t);
    1312             :     }
    1313     2173153 :     if (v) {
    1314      832223 :       s = gdiv(s, powiu(p, v));
    1315      832289 :       if (typ(s) != t_INT) return NULL;
    1316             :     }
    1317     2173128 :     s = mulii(s, Fp_inv(utoipos(z), pp));
    1318     2174073 :     gel(c,k) = gerepileuptoint(av2, Fp_center_i(s, pp, pp2));
    1319             :   }
    1320     1847983 :   for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
    1321      696686 :   return gtopoly(c, 0);
    1322             : }
    1323             : 
    1324             : static void
    1325      703233 : manage_cache(decomp_t *S, GEN f, GEN pp)
    1326             : {
    1327      703233 :   GEN t = S->precns;
    1328             : 
    1329      703233 :   if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
    1330      703225 :   if (cmpii(t, pp) < 0) t = pp;
    1331             : 
    1332      703307 :   if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
    1333             :   {
    1334      520588 :     if (DEBUGLEVEL>4)
    1335           0 :       err_printf("  Precision for cached Newton sums for %Ps: %Ps -> %Ps\n",
    1336           0 :                  f, S->precns? S->precns: gen_0, t);
    1337      520588 :     S->nsf = f;
    1338      520588 :     S->ns = FpX_Newton(f, degpol(f), t);
    1339      520610 :     S->precns = t;
    1340             :   }
    1341      703340 : }
    1342             : 
    1343             : /* return NULL if a mod f is not an integer
    1344             :  * The denominator of any integer in Zp[X]/(f) divides pdr */
    1345             : static GEN
    1346      703347 : mycaract(decomp_t *S, GEN f, GEN a, GEN pp, GEN pdr)
    1347             : {
    1348             :   pari_sp av;
    1349             :   GEN d, chi, prec1, prec2, prec3, ns;
    1350      703347 :   long vd, n = degpol(f);
    1351             : 
    1352      703345 :   if (gequal0(a)) return pol_0(varn(f));
    1353             : 
    1354      703344 :   a = QpX_remove_denom(a, S->p, &d, &vd);
    1355      703330 :   prec1 = pp;
    1356      703330 :   if (lgefint(S->p) == 3)
    1357      703299 :     prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
    1358      703247 :   if (d)
    1359             :   {
    1360      638064 :     GEN p1 = powiu(d, n);
    1361      638123 :     prec2 = mulii(prec1, p1);
    1362      638049 :     prec3 = mulii(prec1, gmin_shallow(mulii(p1, d), pdr));
    1363             :   }
    1364             :   else
    1365       65183 :     prec2 = prec3 = prec1;
    1366      703228 :   manage_cache(S, f, prec3);
    1367             : 
    1368      703354 :   av = avma;
    1369      703354 :   ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
    1370      703275 :   if (!ns) return NULL;
    1371      696710 :   chi = newtoncharpoly(prec1, S->p, ns);
    1372      696775 :   if (!chi) return NULL;
    1373      696684 :   setvarn(chi, varn(f));
    1374      696684 :   return gerepileupto(av, centermod(chi, pp));
    1375             : }
    1376             : 
    1377             : static GEN
    1378      638506 : get_nu(GEN chi, GEN p, long *ptl)
    1379             : { /* split off powers of x first for efficiency */
    1380      638506 :   long v = ZX_valrem(FpX_red(chi,p), &chi), n;
    1381             :   GEN P;
    1382      638493 :   if (!degpol(chi)) { *ptl = 1; return pol_x(varn(chi)); }
    1383      473898 :   P = gel(FpX_factor(chi,p), 1); n = lg(P)-1;
    1384      473922 :   *ptl = v? n+1: n; return gel(P,n);
    1385             : }
    1386             : 
    1387             : /* Factor characteristic polynomial chi of phi mod p. If it splits, update
    1388             :  * S->{phi, chi, nu} and return 1. In any case, set *nu to an irreducible
    1389             :  * factor mod p of chi */
    1390             : static int
    1391      477588 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
    1392             : {
    1393             :   long l;
    1394      477588 :   *nu  = get_nu(chi, S->p, &l);
    1395      477594 :   if (l == 1) return 0; /* single irreducible factor: doesn't split */
    1396             :   /* phi o phi0 mod (p, f) */
    1397      121718 :   S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
    1398      121719 :   S->chi = chi;
    1399      121719 :   S->nu = *nu; return 1;
    1400             : }
    1401             : 
    1402             : /* Return the prime element in Zp[phi], a t_INT (iff *Ep = 1) or QX;
    1403             :  * nup, chip are ZX. phi = NULL codes X
    1404             :  * If *Ep < oE or Ep divides Ediv (!=0) return NULL (uninteresting) */
    1405             : static GEN
    1406      560080 : getprime(decomp_t *S, GEN phi, GEN chip, GEN nup, long *Lp, long *Ep,
    1407             :          long oE, long Ediv)
    1408             : {
    1409             :   GEN z, chin, q, qp;
    1410             :   long r, s;
    1411             : 
    1412      560080 :   if (phi && dvdii(constant_coeff(chip), S->psc))
    1413             :   {
    1414        1656 :     chip = mycaract(S, S->chi, phi, S->pmf, S->prc);
    1415        1656 :     if (dvdii(constant_coeff(chip), S->pmf))
    1416        1258 :       chip = ZXQ_charpoly(phi, S->chi, varn(chip));
    1417             :   }
    1418      560076 :   if (degpol(nup) == 1)
    1419             :   {
    1420      523231 :     GEN c = gel(nup,2); /* nup = X + c */
    1421      523231 :     chin = signe(c)? RgX_translate(chip, negi(c)): chip;
    1422             :   }
    1423             :   else
    1424       36840 :     chin = ZXQ_charpoly(nup, chip, varn(chip));
    1425             : 
    1426      560076 :   vstar(S->p, chin, Lp, Ep);
    1427      560088 :   if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
    1428             : 
    1429      440816 :   if (*Ep == 1) return S->p;
    1430      303423 :   (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
    1431      303427 :   if (r <= 0)
    1432             :   {
    1433       60108 :     long t = 1 + ((-r) / *Ep);
    1434       60108 :     r += t * *Ep;
    1435       60108 :     s += t * *Lp;
    1436             :   }
    1437             :   /* r > 0 minimal such that r L/E - s = 1/E
    1438             :    * pi = nu^r / p^s is an element of valuation 1/E,
    1439             :    * so is pi + O(p) since 1/E < 1. May compute nu^r mod p^(s+1) */
    1440      303427 :   q = powiu(S->p, s); qp = mulii(q, S->p);
    1441      303395 :   nup = FpXQ_powu(nup, r, S->chi, qp);
    1442      303426 :   if (!phi) return RgX_Rg_div(nup, q); /* phi = X : no composition */
    1443       47937 :   z = compmod(S->p, nup, phi, S->chi, qp, -s);
    1444       47937 :   return signe(z)? z: NULL;
    1445             : }
    1446             : 
    1447             : static int
    1448      276891 : update_phi(decomp_t *S)
    1449             : {
    1450      276891 :   GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
    1451             :   long k, vpsc;
    1452      276889 :   for (k = 1;; k++)
    1453             :   {
    1454      279043 :     prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, S->vpsc);
    1455             :     /* if prc == S->psc then either chi is not separable or
    1456             :        the reduced discriminant of chi is too large */
    1457      279046 :     if (!equalii(prc, S->psc)) break;
    1458             : 
    1459             :     /* increase precision */
    1460        2154 :     S->vpsc = maxss(S->vpsf, S->vpsc + 1);
    1461        2154 :     S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
    1462             : 
    1463        2154 :     PHI = S->phi;
    1464        2154 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
    1465             :     /* change phi (in case not separable) */
    1466        2154 :     PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
    1467        2154 :     S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
    1468             :   }
    1469      276891 :   psc = mulii(sqri(prc), S->p);
    1470      276882 :   vpsc = 2*Z_pval(prc, S->p) + 1;
    1471             : 
    1472      276884 :   if (!PHI) /* break out of above loop immediately (k = 1) */
    1473             :   {
    1474      274738 :     PHI = S->phi;
    1475      274738 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
    1476      274741 :     if (S->phi0 || cmpii(psc,S->psc) > 0)
    1477             :     {
    1478             :       for(;;)
    1479             :       {
    1480      113946 :         S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
    1481      113945 :         prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, vpsc);
    1482      113946 :         if (!equalii(prc, psc)) break;
    1483          49 :         psc = mulii(psc, S->p); vpsc++;
    1484             :       }
    1485      113897 :       psc = mulii(sqri(prc), S->p);
    1486      113893 :       vpsc = 2*Z_pval(prc, S->p) + 1;
    1487             :     }
    1488             :   }
    1489      276888 :   S->phi = PHI;
    1490      276888 :   S->chi = FpX_red(S->chi, psc);
    1491             : 
    1492             :   /* may happen if p is unramified */
    1493      276884 :   if (is_pm1(prc)) return 0;
    1494      230883 :   S->prc = prc;
    1495      230883 :   S->psc = psc;
    1496      230883 :   S->vpsc = vpsc; return 1;
    1497             : }
    1498             : 
    1499             : /* return 1 if at least 2 factors mod p ==> chi splits
    1500             :  * Replace S->phi such that F increases (to D) */
    1501             : static int
    1502       68029 : testb2(decomp_t *S, long D, GEN theta)
    1503             : {
    1504       68029 :   long v = varn(S->chi), dlim = degpol(S->chi)-1;
    1505       68029 :   GEN T0 = S->phi, chi, phi, nu;
    1506       68029 :   if (DEBUGLEVEL>4) err_printf("  Increasing Fa\n");
    1507             :   for (;;)
    1508             :   {
    1509       68061 :     phi = gadd(theta, random_FpX(dlim, v, S->p));
    1510       68061 :     chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1511             :     /* phi nonprimary ? */
    1512       68062 :     if (split_char(S, chi, phi, T0, &nu)) return 1;
    1513       68062 :     if (degpol(nu) == D) break;
    1514             :   }
    1515             :   /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
    1516       68030 :   S->phi0 = T0;
    1517       68030 :   S->chi = chi;
    1518       68030 :   S->phi = phi;
    1519       68030 :   S->nu = nu; return 0;
    1520             : }
    1521             : 
    1522             : /* return 1 if at least 2 factors mod p ==> chi can be split.
    1523             :  * compute a new S->phi such that E = lcm(Ea, Et);
    1524             :  * A a ZX, T a t_INT (iff Et = 1, probably impossible ?) or QX */
    1525             : static int
    1526       47937 : testc2(decomp_t *S, GEN A, long Ea, GEN T, long Et)
    1527             : {
    1528       47937 :   GEN c, chi, phi, nu, T0 = S->phi;
    1529             : 
    1530       47937 :   if (DEBUGLEVEL>4) err_printf("  Increasing Ea\n");
    1531       47937 :   if (Et == 1) /* same as other branch, split for efficiency */
    1532           0 :     c = A; /* Et = 1 => s = 1, r = 0, t = 0 */
    1533             :   else
    1534             :   {
    1535             :     long r, s, t;
    1536       47937 :     (void)cbezout(Ea, Et, &r, &s); t = 0;
    1537       48035 :     while (r < 0) { r = r + Et; t++; }
    1538       48077 :     while (s < 0) { s = s + Ea; t++; }
    1539             : 
    1540             :     /* A^s T^r / p^t */
    1541       47937 :     c = RgXQ_mul(RgXQ_powu(A, s, S->chi), RgXQ_powu(T, r, S->chi), S->chi);
    1542       47937 :     c = RgX_Rg_div(c, powiu(S->p, t));
    1543       47937 :     c = redelt(c, S->psc, S->p);
    1544             :   }
    1545       47937 :   phi = RgX_add(c,  pol_x(varn(S->chi)));
    1546       47937 :   chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1547       47936 :   if (split_char(S, chi, phi, T0, &nu)) return 1;
    1548             :   /* E_phi = lcm(E_alpha,E_theta) */
    1549       47937 :   S->phi0 = T0;
    1550       47937 :   S->chi = chi;
    1551       47937 :   S->phi = phi;
    1552       47937 :   S->nu = nu; return 0;
    1553             : }
    1554             : 
    1555             : /* Return h^(-degpol(P)) P(x * h) if result is integral, NULL otherwise */
    1556             : static GEN
    1557       59570 : ZX_rescale_inv(GEN P, GEN h)
    1558             : {
    1559       59570 :   long i, l = lg(P);
    1560       59570 :   GEN Q = cgetg(l,t_POL), hi = h;
    1561       59570 :   gel(Q,l-1) = gel(P,l-1);
    1562      173356 :   for (i=l-2; i>=2; i--)
    1563             :   {
    1564             :     GEN r;
    1565      173352 :     gel(Q,i) = dvmdii(gel(P,i), hi, &r);
    1566      173353 :     if (signe(r)) return NULL;
    1567      173353 :     if (i == 2) break;
    1568      113784 :     hi = mulii(hi,h);
    1569             :   }
    1570       59573 :   Q[1] = P[1]; return Q;
    1571             : }
    1572             : 
    1573             : /* x p^-eq nu^-er mod p */
    1574             : static GEN
    1575      301328 : get_gamma(decomp_t *S, GEN x, long eq, long er)
    1576             : {
    1577      301328 :   GEN q, g = x, Dg = powiu(S->p, eq);
    1578      301306 :   long vDg = eq;
    1579      301306 :   if (er)
    1580             :   {
    1581       22286 :     if (!S->invnu)
    1582             :     {
    1583       15448 :       while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
    1584       15448 :       S->invnu = QXQ_inv(S->nu, S->chi);
    1585       15448 :       S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
    1586             :     }
    1587       22286 :     if (S->Dinvnu) {
    1588       22286 :       Dg = mulii(Dg, powiu(S->Dinvnu, er));
    1589       22286 :       vDg += er * S->vDinvnu;
    1590             :     }
    1591       22286 :     q = mulii(S->p, Dg);
    1592       22286 :     g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
    1593       22286 :     g = FpX_rem(g, S->chi, q);
    1594       22286 :     update_den(S->p, &g, &Dg, &vDg, NULL);
    1595       22285 :     g = centermod(g, mulii(S->p, Dg));
    1596             :   }
    1597      301306 :   if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
    1598      301326 :   return g;
    1599             : }
    1600             : static GEN
    1601      354241 : get_g(decomp_t *S, long Ea, long L, long E, GEN beta, GEN *pchig,
    1602             :       long *peq, long *per)
    1603             : {
    1604             :   long eq, er;
    1605      354241 :   GEN g, chig, chib = NULL;
    1606             :   for(;;) /* at most twice */
    1607             :   {
    1608      360897 :     if (L < 0)
    1609             :     {
    1610       60262 :       chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
    1611       60263 :       vstar(S->p, chib, &L, &E);
    1612             :     }
    1613      360899 :     eq = L / E; er = L*Ea / E - eq*Ea;
    1614             :     /* floor(L Ea/E) = eq Ea + er */
    1615      360899 :     if (er || !chib)
    1616             :     { /* g might not be an integer ==> chig = NULL */
    1617      301329 :       g = get_gamma(S, beta, eq, er);
    1618      301326 :       chig = mycaract(S, S->chi, g, S->psc, S->prc);
    1619             :     }
    1620             :     else
    1621             :     { /* g = beta/p^eq, special case of the above */
    1622       59570 :       GEN h = powiu(S->p, eq);
    1623       59568 :       g = RgX_Rg_div(beta, h);
    1624       59570 :       chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
    1625       59569 :       if (chig) chig = FpX_red(chig, S->pmf);
    1626             :     }
    1627             :     /* either success or second consecutive failure */
    1628      360898 :     if (chig || chib) break;
    1629             :     /* if g fails the v*-test, v(beta) was wrong. Retry once */
    1630        6656 :     L = -1;
    1631             :   }
    1632      354242 :   *pchig = chig; *peq = eq; *per = er; return g;
    1633             : }
    1634             : 
    1635             : /* return 1 if at least 2 factors mod p ==> chi can be split */
    1636             : static int
    1637      237685 : loop(decomp_t *S, long Ea)
    1638             : {
    1639      237685 :   pari_sp av = avma;
    1640      237685 :   GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
    1641      237680 :   long N = degpol(S->f), v = varn(S->f);
    1642      237680 :   S->invnu = NULL;
    1643             :   for (;;)
    1644      116556 :   { /* beta tends to a factor of chi */
    1645             :     long L, i, Fg, eq, er;
    1646      354236 :     GEN chig = NULL, d, g, nug;
    1647             : 
    1648      354236 :     if (DEBUGLEVEL>4) err_printf("  beta = %Ps\n", beta);
    1649      354236 :     L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
    1650      354241 :     if (L > S->mf) L = -1; /* from scratch */
    1651      354241 :     g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
    1652      354241 :     if (DEBUGLEVEL>4) err_printf("  (eq,er) = (%ld,%ld)\n", eq,er);
    1653             :     /* g = beta p^-eq  nu^-er (a unit), chig = charpoly(g) */
    1654      472093 :     if (split_char(S, chig, g,S->phi, &nug)) return 1;
    1655             : 
    1656      234408 :     Fg = degpol(nug);
    1657      234407 :     if (Fg == 1)
    1658             :     { /* frequent special case nug = x - d */
    1659             :       long Le, Ee;
    1660             :       GEN chie, nue, e, pie;
    1661      159023 :       d = negi(gel(nug,2));
    1662      159023 :       chie = RgX_translate(chig, d);
    1663      159021 :       nue = pol_x(v);
    1664      159019 :       e = RgX_Rg_sub(g, d);
    1665      159020 :       pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1666      159022 :       if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1667             :     }
    1668             :     else
    1669             :     {
    1670       75384 :       long Fa = degpol(S->nu), vdeng;
    1671             :       GEN deng, numg, nume;
    1672       78461 :       if (Fa % Fg) return testb2(S, ulcm(Fa,Fg), g);
    1673             :       /* nu & nug irreducible mod p, deg nug | deg nu. To improve beta, look
    1674             :        * for a root d of nug in Fp[phi] such that v_p(g - d) > 0 */
    1675        7355 :       if (ZX_equal(nug, S->nu))
    1676        5123 :         d = pol_x(v);
    1677             :       else
    1678             :       {
    1679        2232 :         if (!p_is_prime(S)) pari_err_PRIME("FpX_ffisom",S->p);
    1680        2232 :         d = FpX_ffisom(nug, S->nu, S->p);
    1681             :       }
    1682             :       /* write g = numg / deng, e = nume / deng */
    1683        7355 :       numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
    1684       11822 :       for (i = 1; i <= Fg; i++)
    1685             :       {
    1686             :         GEN chie, nue, e;
    1687       11822 :         if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
    1688       11822 :         nume = ZX_sub(numg, ZX_Z_mul(d, deng));
    1689             :         /* test e = nume / deng */
    1690       11822 :         if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
    1691        4467 :           continue;
    1692        7355 :         e = RgX_Rg_div(nume, deng);
    1693        7355 :         chie = mycaract(S, S->chi, e, S->psc, S->prc);
    1694        8547 :         if (split_char(S, chie, e,S->phi, &nue)) return 1;
    1695        5470 :         if (RgX_is_monomial(nue))
    1696             :         { /* v_p(e) = v_p(g - d) > 0 */
    1697             :           long Le, Ee;
    1698             :           GEN pie;
    1699        5470 :           pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1700        5470 :           if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1701        4278 :           break;
    1702             :         }
    1703             :       }
    1704        4278 :       if (i > Fg)
    1705             :       {
    1706           0 :         if (!p_is_prime(S)) pari_err_PRIME("nilord",S->p);
    1707           0 :         pari_err_BUG("nilord (no root)");
    1708             :       }
    1709             :     }
    1710      116555 :     if (eq) d = gmul(d, powiu(S->p,  eq));
    1711      116555 :     if (er) d = gmul(d, gpowgs(S->nu, er));
    1712      116555 :     beta = gsub(beta, d);
    1713             : 
    1714      116556 :     if (gc_needed(av,1))
    1715             :     {
    1716           0 :       if (DEBUGMEM > 1) pari_warn(warnmem, "nilord");
    1717           0 :       gerepileall(av, S->invnu? 6: 4, &beta, &(S->precns), &(S->ns), &(S->nsf), &(S->invnu), &(S->Dinvnu));
    1718             :     }
    1719             :   }
    1720             : }
    1721             : 
    1722             : /* E and F cannot decrease; return 1 if O = Zp[phi], 2 if we can get a
    1723             :  * decomposition and 0 otherwise */
    1724             : static long
    1725      392861 : progress(decomp_t *S, GEN *ppa, long *pE)
    1726             : {
    1727      392861 :   long E = *pE, F;
    1728      392861 :   GEN pa = *ppa;
    1729      392861 :   S->phi0 = NULL; /* no delayed composition */
    1730             :   for(;;)
    1731        2717 :   {
    1732             :     long l, La, Ea; /* N.B If E = 0, getprime cannot return NULL */
    1733      395578 :     GEN pia  = getprime(S, NULL, S->chi, S->nu, &La, &Ea, E,0);
    1734      395601 :     if (pia) { /* success, we break out in THIS loop */
    1735      392884 :       pa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
    1736      392887 :       E = Ea;
    1737      392887 :       if (La == 1) break; /* no need to change phi so that nu = pia */
    1738             :     }
    1739             :     /* phi += prime elt */
    1740       65380 :     S->phi = typ(pa) == t_INT? RgX_Rg_add_shallow(S->phi, pa)
    1741      160925 :                              : RgX_add(S->phi, pa);
    1742             :     /* recompute char. poly. chi from scratch */
    1743      160923 :     S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
    1744      160921 :     S->nu = get_nu(S->chi, S->p, &l);
    1745      160924 :     if (l > 1) return 2;
    1746      160924 :     if (!update_phi(S)) return 1; /* unramified */
    1747      160923 :     if (pia) break;
    1748             :   }
    1749      392885 :   *pE = E; *ppa = pa; F = degpol(S->nu);
    1750      392882 :   if (DEBUGLEVEL>4) err_printf("  (E, F) = (%ld,%ld)\n", E, F);
    1751      392882 :   if (E * F == degpol(S->f)) return 1;
    1752      237684 :   if (loop(S, E)) return 2;
    1753      115966 :   if (!update_phi(S)) return 1;
    1754       69960 :   return 0;
    1755             : }
    1756             : 
    1757             : /* flag != 0 iff we're looking for the p-adic factorization,
    1758             :    in which case it is the p-adic precision we want */
    1759             : static GEN
    1760      454115 : maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag)
    1761             : {
    1762      454115 :   long oE, n = lg(w)-1; /* factor of largest degree */
    1763      454115 :   GEN opa, D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
    1764      454118 :   S->pisprime = -1;
    1765      454118 :   S->p = p;
    1766      454118 :   S->mf = mf;
    1767      454118 :   S->nu = gel(w,n);
    1768      454118 :   S->df = Z_pval(D, p);
    1769      454119 :   S->pdf = powiu(p, S->df);
    1770      454100 :   S->phi = pol_x(varn(f));
    1771      454119 :   S->chi = S->f = f;
    1772      454119 :   if (n > 1) return Decomp(S, flag); /* FIXME: use bezout_lift_fact */
    1773             : 
    1774      322912 :   if (DEBUGLEVEL>4)
    1775           0 :     err_printf("  entering Nilord: %Ps^%ld\n  f = %Ps, nu = %Ps\n",
    1776             :                p, S->df, S->f, S->nu);
    1777      322912 :   else if (DEBUGLEVEL>2) err_printf("  entering Nilord\n");
    1778      322912 :   S->psf = S->psc = mulii(sqri(D), p);
    1779      322891 :   S->vpsf = S->vpsc = 2*S->df + 1;
    1780      322891 :   S->prc = mulii(D, p);
    1781      322885 :   S->chi = FpX_red(S->f, S->psc);
    1782      322911 :   S->pmf = powiu(p, S->mf+1);
    1783      322897 :   S->precns = NULL;
    1784      322897 :   for(opa = NULL, oE = 0;;)
    1785       69956 :   {
    1786      392853 :     long n = progress(S, &opa, &oE);
    1787      392879 :     if (n == 1) return flag? NULL: dbasis(p, S->f, S->mf, S->phi, S->chi);
    1788      191675 :     if (n == 2) return Decomp(S, flag);
    1789             :   }
    1790             : }
    1791             : 
    1792             : static int
    1793         812 : expo_is_squarefree(GEN e)
    1794             : {
    1795         812 :   long i, l = lg(e);
    1796        1183 :   for (i=1; i<l; i++)
    1797         945 :     if (e[i] != 1) return 0;
    1798         238 :   return 1;
    1799             : }
    1800             : /* pure round 4 */
    1801             : static GEN
    1802         770 : ZpX_round4(GEN f, GEN p, GEN w, long prec)
    1803             : {
    1804             :   decomp_t S;
    1805         770 :   GEN L = maxord_i(&S, p, f, ZpX_disc_val(f,p), w, prec);
    1806         770 :   return L? L: mkvec(f);
    1807             : }
    1808             : /* f a squarefree ZX with leading_coeff 1, degree > 0. Return list of
    1809             :  * irreducible factors in Zp[X] (computed mod p^prec) */
    1810             : static GEN
    1811        1071 : ZpX_monic_factor_squarefree(GEN f, GEN p, long prec)
    1812             : {
    1813        1071 :   pari_sp av = avma;
    1814             :   GEN L, fa, w, e;
    1815             :   long i, l;
    1816        1071 :   if (degpol(f) == 1) return mkvec(f);
    1817         812 :   fa = FpX_factor(f,p); w = gel(fa,1); e = gel(fa,2);
    1818             :   /* no repeated factors: Hensel lift */
    1819         812 :   if (expo_is_squarefree(e)) return ZpX_liftfact(f, w, powiu(p,prec), p, prec);
    1820         574 :   l = lg(w);
    1821         574 :   if (l == 2)
    1822             :   {
    1823         371 :     L = ZpX_round4(f,p,w,prec);
    1824         371 :     if (lg(L) == 2) { set_avma(av); return mkvec(f); }
    1825             :   }
    1826             :   else
    1827             :   { /* >= 2 factors mod p: partial Hensel lift */
    1828         203 :     GEN D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, ZpX_disc_val(f,p));
    1829         203 :     long r = maxss(2*Z_pval(D,p)+1, prec);
    1830         203 :     GEN W = cgetg(l, t_VEC);
    1831         665 :     for (i = 1; i < l; i++)
    1832         462 :       gel(W,i) = e[i] == 1? gel(w,i): FpX_powu(gel(w,i), e[i], p);
    1833         203 :     L = ZpX_liftfact(f, W, powiu(p,r), p, r);
    1834         665 :     for (i = 1; i < l; i++)
    1835         462 :       gel(L,i) = e[i] == 1? mkvec(gel(L,i))
    1836         462 :                           : ZpX_round4(gel(L,i), p, mkvec(gel(w,i)), prec);
    1837         203 :     L = shallowconcat1(L);
    1838             :   }
    1839         343 :   return gerepilecopy(av, L);
    1840             : }
    1841             : 
    1842             : /* assume T a ZX with leading_coeff 1, degree > 0 */
    1843             : GEN
    1844         490 : ZpX_monic_factor(GEN T, GEN p, long prec)
    1845             : {
    1846             :   GEN Q, P, E, F;
    1847             :   long L, l, i, v;
    1848             : 
    1849         490 :   if (degpol(T) == 1) return mkmat2(mkcol(T), mkcol(gen_1));
    1850         490 :   v = ZX_valrem(T, &T);
    1851         490 :   Q = ZX_squff(T, &F); l = lg(Q); L = v? l + 1: l;
    1852         490 :   P = cgetg(L, t_VEC);
    1853         490 :   E = cgetg(L, t_VEC);
    1854         987 :   for (i = 1; i < l; i++)
    1855             :   {
    1856         497 :     GEN w = ZpX_monic_factor_squarefree(gel(Q,i), p, prec);
    1857         497 :     gel(P,i) = w; settyp(w, t_COL);
    1858         497 :     gel(E,i) = const_col(lg(w)-1, utoipos(F[i]));
    1859             :   }
    1860         490 :   if (v) { gel(P,i) = pol_x(varn(T)); gel(E,i) = utoipos(v); }
    1861         490 :   return mkmat2(shallowconcat1(P), shallowconcat1(E));
    1862             : }
    1863             : 
    1864             : /* DT = multiple of disc(T) or NULL
    1865             :  * Return a multiple of the denominator of an algebraic integer (in Q[X]/(T))
    1866             :  * when expressed in terms of the power basis */
    1867             : GEN
    1868       44085 : indexpartial(GEN T, GEN DT)
    1869             : {
    1870       44085 :   pari_sp av = avma;
    1871             :   long i, nb;
    1872       44085 :   GEN fa, E, P, U, res = gen_1, dT = ZX_deriv(T);
    1873             : 
    1874       44079 :   if (!DT) DT = ZX_disc(T);
    1875       44079 :   fa = absZ_factor_limit_strict(DT, 0, &U);
    1876       44089 :   P = gel(fa,1);
    1877       44089 :   E = gel(fa,2); nb = lg(P)-1;
    1878      210877 :   for (i = 1; i <= nb; i++)
    1879             :   {
    1880      166806 :     long e = itou(gel(E,i)), e2 = e >> 1;
    1881      166813 :     GEN p = gel(P,i), q = p;
    1882      166813 :     if (e2 >= 2) q = ZpX_reduced_resultant_fast(T, dT, p, e2);
    1883      166815 :     res = mulii(res, q);
    1884             :   }
    1885       44071 :   if (U)
    1886             :   {
    1887        2114 :     long e = itou(gel(U,2)), e2 = e >> 1;
    1888        2114 :     GEN p = gel(U,1), q = powiu(p, odd(e)? e2+1: e2);
    1889        2114 :     res = mulii(res, q);
    1890             :   }
    1891       44071 :   return gerepileuptoint(av,res);
    1892             : }
    1893             : 
    1894             : /*******************************************************************/
    1895             : /*                                                                 */
    1896             : /*    2-ELT REPRESENTATION FOR PRIME IDEALS (dividing index)       */
    1897             : /*                                                                 */
    1898             : /*******************************************************************/
    1899             : /* to compute norm of elt in basis form */
    1900             : typedef struct {
    1901             :   long r1;
    1902             :   GEN M;  /* via embed_norm */
    1903             : 
    1904             :   GEN D, w, T; /* via resultant if M = NULL */
    1905             : } norm_S;
    1906             : 
    1907             : static GEN
    1908      528519 : get_norm(norm_S *S, GEN a)
    1909             : {
    1910      528519 :   if (S->M)
    1911             :   {
    1912             :     long e;
    1913      527016 :     GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
    1914      527063 :     if (e > -5) pari_err_PREC( "get_norm");
    1915      527063 :     return N;
    1916             :   }
    1917        1503 :   if (S->w) a = RgV_RgC_mul(S->w, a);
    1918        1503 :   return ZX_resultant_all(S->T, a, S->D, 0);
    1919             : }
    1920             : static void
    1921      217265 : init_norm(norm_S *S, GEN nf, GEN p)
    1922             : {
    1923      217265 :   GEN T = nf_get_pol(nf), M = nf_get_M(nf);
    1924      217270 :   long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
    1925             : 
    1926      217266 :   S->r1 = nf_get_r1(nf);
    1927      217273 :   if (N * ex <= prec2nbits(gprecision(M)) - 20)
    1928             :   { /* enough prec to use embed_norm */
    1929      217083 :     S->M = M;
    1930      217083 :     S->D = NULL;
    1931      217083 :     S->w = NULL;
    1932      217083 :     S->T = NULL;
    1933             :   }
    1934             :   else
    1935             :   {
    1936         195 :     GEN w = leafcopy(nf_get_zkprimpart(nf)), D = nf_get_zkden(nf), Dp = sqri(p);
    1937             :     long i;
    1938         195 :     if (!equali1(D))
    1939             :     {
    1940         195 :       GEN w1 = D;
    1941         195 :       long v = Z_pval(D, p);
    1942         195 :       D = powiu(p, v);
    1943         195 :       Dp = mulii(D, Dp);
    1944         195 :       gel(w, 1) = remii(w1, Dp);
    1945             :     }
    1946        4061 :     for (i=2; i<=N; i++) gel(w,i) = FpX_red(gel(w,i), Dp);
    1947         195 :     S->M = NULL;
    1948         195 :     S->D = D;
    1949         195 :     S->w = w;
    1950         195 :     S->T = T;
    1951             :   }
    1952      217278 : }
    1953             : /* f = f(pr/p), q = p^(f+1), a in pr.
    1954             :  * Return 1 if v_pr(a) = 1, and 0 otherwise */
    1955             : static int
    1956      528511 : is_uniformizer(GEN a, GEN q, norm_S *S) { return !dvdii(get_norm(S,a), q); }
    1957             : 
    1958             : /* Return x * y, x, y are t_MAT (Fp-basis of in O_K/p), assume (x,y)=1.
    1959             :  * Either x or y may be NULL (= O_K), not both */
    1960             : static GEN
    1961      810924 : mul_intersect(GEN x, GEN y, GEN p)
    1962             : {
    1963      810924 :   if (!x) return y;
    1964      468158 :   if (!y) return x;
    1965      353903 :   return FpM_intersect_i(x, y, p);
    1966             : }
    1967             : /* Fp-basis of (ZK/pr): applied to the primes found in primedec_aux()
    1968             :  * true nf */
    1969             : static GEN
    1970      346479 : Fp_basis(GEN nf, GEN pr)
    1971             : {
    1972             :   long i, j, l;
    1973             :   GEN x, y;
    1974             :   /* already in basis form (from Buchman-Lenstra) ? */
    1975      346479 :   if (typ(pr) == t_MAT) return pr;
    1976             :   /* ordinary prid (from Kummer) */
    1977       91564 :   x = pr_hnf(nf, pr);
    1978       91564 :   l = lg(x);
    1979       91564 :   y = cgetg(l, t_MAT);
    1980      863523 :   for (i=j=1; i<l; i++)
    1981      771958 :     if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
    1982       91565 :   setlg(y, j); return y;
    1983             : }
    1984             : /* Let Ip = prod_{ P | p } P be the p-radical. The list L contains the
    1985             :  * P (mod Ip) seen as sub-Fp-vector spaces of ZK/Ip.
    1986             :  * Return the list of (Ip / P) (mod Ip).
    1987             :  * N.B: All ideal multiplications are computed as intersections of Fp-vector
    1988             :  * spaces. true nf */
    1989             : static GEN
    1990      217281 : get_LV(GEN nf, GEN L, GEN p, long N)
    1991             : {
    1992      217281 :   long i, l = lg(L)-1;
    1993             :   GEN LV, LW, A, B;
    1994             : 
    1995      217281 :   LV = cgetg(l+1, t_VEC);
    1996      217281 :   if (l == 1) { gel(LV,1) = matid(N); return LV; }
    1997      114254 :   LW = cgetg(l+1, t_VEC);
    1998      460738 :   for (i=1; i<=l; i++) gel(LW,i) = Fp_basis(nf, gel(L,i));
    1999             : 
    2000             :   /* A[i] = L[1]...L[i-1], i = 2..l */
    2001      114256 :   A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
    2002      346480 :   for (i=1; i < l; i++) gel(A,i+1) = mul_intersect(gel(A,i), gel(LW,i), p);
    2003             :   /* B[i] = L[i+1]...L[l], i = 1..(l-1) */
    2004      114256 :   B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
    2005      346482 :   for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
    2006      460740 :   for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
    2007      114258 :   return LV;
    2008             : }
    2009             : 
    2010             : static void
    2011           0 : errprime(GEN p) { pari_err_PRIME("idealprimedec",p); }
    2012             : 
    2013             : /* P = Fp-basis (over O_K/p) for pr.
    2014             :  * V = Z-basis for I_p/pr. ramif != 0 iff some pr|p is ramified.
    2015             :  * Return a p-uniformizer for pr. Assume pr not inert, i.e. m > 0 */
    2016             : static GEN
    2017      316769 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
    2018             : {
    2019      316769 :   long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
    2020             :   GEN u, Mv, x, q;
    2021             : 
    2022      316769 :   f = N - m; /* we want v_p(Norm(x)) = p^f */
    2023      316769 :   q = powiu(p,f+1);
    2024             : 
    2025      316726 :   u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
    2026      316767 :   setlg(u, lg(P));
    2027      316767 :   u = centermod(ZM_ZC_mul(P, u), p);
    2028      316755 :   if (is_uniformizer(u, q, S)) return u;
    2029      163156 :   if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
    2030      134900 :     gel(u,1) = addii(gel(u,1), p); /* try u + p */
    2031             :   else
    2032       28256 :     gel(u,1) = subii(gel(u,1), p); /* try u - p */
    2033      163137 :   if (!ramif || is_uniformizer(u, q, S)) return u;
    2034             : 
    2035             :   /* P/p ramified, u in P^2, not in Q for all other Q|p */
    2036       86540 :   Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
    2037       86555 :   l = lg(P);
    2038      120557 :   for (i=1; i<l; i++)
    2039             :   {
    2040      120557 :     x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
    2041      120553 :     if (is_uniformizer(x, q, S)) return x;
    2042             :   }
    2043           0 :   errprime(p);
    2044             :   return NULL; /* LCOV_EXCL_LINE */
    2045             : }
    2046             : 
    2047             : /*******************************************************************/
    2048             : /*                                                                 */
    2049             : /*                   BUCHMANN-LENSTRA ALGORITHM                    */
    2050             : /*                                                                 */
    2051             : /*******************************************************************/
    2052             : static GEN
    2053     2995436 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
    2054     2995436 : { return mkvec5(p, u, utoipos(e), utoipos(f), t); }
    2055             : 
    2056             : /* nf a true nf, u in Z[X]/(T); pr = p Z_K + u Z_K of ramification index e */
    2057             : GEN
    2058     2516556 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
    2059             : {
    2060     2516556 :   GEN t, T = nf_get_pol(nf);
    2061     2516554 :   long f = degpol(u), N = degpol(T);
    2062             : 
    2063     2516569 :   if (f == N)
    2064             :   { /* inert */
    2065      220601 :     u = scalarcol_shallow(p,N);
    2066      220604 :     t = gen_1;
    2067             :   }
    2068             :   else
    2069             :   {
    2070     2295968 :     t = centermod(poltobasis(nf, FpX_div(T, u, p)), p);
    2071     2295790 :     u = centermod(poltobasis(nf, u), p);
    2072     2295750 :     if (e == 1)
    2073             :     { /* make sure v_pr(u) = 1 (automatic if e>1) */
    2074     2102141 :       GEN cw, w = Q_primitive_part(nf_to_scalar_or_alg(nf, u), &cw);
    2075     2102266 :       long v = cw? f - Q_pval(cw, p) * N: f;
    2076     2102267 :       if (ZpX_resultant_val(T, w, p, v + 1) > v)
    2077             :       {
    2078      108066 :         GEN c = gel(u,1);
    2079      108066 :         gel(u,1) = signe(c) > 0? subii(c, p): addii(c, p);
    2080             :       }
    2081             :     }
    2082     2295954 :     t = zk_multable(nf, t);
    2083             :   }
    2084     2516525 :   return mk_pr(p,u,e,f,t);
    2085             : }
    2086             : 
    2087             : typedef struct {
    2088             :   GEN nf, p;
    2089             :   long I;
    2090             : } eltmod_muldata;
    2091             : 
    2092             : static GEN
    2093      951809 : sqr_mod(void *data, GEN x)
    2094             : {
    2095      951809 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2096      951809 :   return FpC_red(nfsqri(D->nf, x), D->p);
    2097             : }
    2098             : static GEN
    2099      426636 : ei_msqr_mod(void *data, GEN x)
    2100             : {
    2101      426636 :   GEN x2 = sqr_mod(data, x);
    2102      426633 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2103      426633 :   return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
    2104             : }
    2105             : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
    2106             : static GEN
    2107      796948 : pow_ei_mod_p(GEN nf, long I, GEN p)
    2108             : {
    2109      796948 :   pari_sp av = avma;
    2110             :   eltmod_muldata D;
    2111      796948 :   long N = nf_get_degree(nf);
    2112      796952 :   GEN y = col_ei(N,I);
    2113      796958 :   if (I == 1) return y;
    2114      577332 :   D.nf = nf;
    2115      577332 :   D.p = p;
    2116      577332 :   D.I = I;
    2117      577332 :   y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
    2118      577338 :   return gerepileupto(av,y);
    2119             : }
    2120             : 
    2121             : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
    2122             : static GEN
    2123      217276 : pradical(GEN nf, GEN p, GEN *phi)
    2124             : {
    2125      217276 :   long i, N = nf_get_degree(nf);
    2126             :   GEN q,m,frob,rad;
    2127             : 
    2128             :   /* matrix of Frob: x->x^p over Z_K/p */
    2129      217275 :   frob = cgetg(N+1,t_MAT);
    2130     1004505 :   for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
    2131             : 
    2132      217278 :   m = frob; q = p;
    2133      314374 :   while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
    2134      217276 :   rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
    2135     1004460 :   for (i=1; i<=N; i++) gcoeff(frob,i,i) = subiu(gcoeff(frob,i,i), 1);
    2136      217244 :   *phi = frob; return rad;
    2137             : }
    2138             : 
    2139             : /* return powers of a: a^0, ... , a^d,  d = dim A */
    2140             : static GEN
    2141      168710 : get_powers(GEN mul, GEN p)
    2142             : {
    2143      168710 :   long i, d = lgcols(mul);
    2144      168710 :   GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
    2145             : 
    2146      168711 :   gel(P,0) = scalarcol_shallow(gen_1, d-1);
    2147      168712 :   z = gel(mul,1);
    2148      824327 :   for (i=1; i<=d; i++)
    2149             :   {
    2150      655621 :     gel(P,i) = z; /* a^i */
    2151      655621 :     if (i!=d) z = FpM_FpC_mul(mul, z, p);
    2152             :   }
    2153      168706 :   return pow;
    2154             : }
    2155             : 
    2156             : /* minimal polynomial of a in A (dim A = d).
    2157             :  * mul = multiplication table by a in A */
    2158             : static GEN
    2159      128124 : pol_min(GEN mul, GEN p)
    2160             : {
    2161      128124 :   pari_sp av = avma;
    2162      128124 :   GEN z = FpM_deplin(get_powers(mul, p), p);
    2163      128125 :   return gerepilecopy(av, RgV_to_RgX(z,0));
    2164             : }
    2165             : 
    2166             : static GEN
    2167      449177 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N, long flim)
    2168             : {
    2169             :   GEN u, t;
    2170             :   long e, f;
    2171             : 
    2172      449177 :   if (typ(P) == t_VEC)
    2173             :   { /* already done (Kummer) */
    2174       91564 :     f = pr_get_f(P);
    2175       91564 :     if (flim > 0 && f > flim) return NULL;
    2176       90206 :     if (flim == -2) return (GEN)f;
    2177       90206 :     return P;
    2178             :   }
    2179      357613 :   f = N - (lg(P)-1);
    2180      357613 :   if (flim > 0 && f > flim) return NULL;
    2181      356150 :   if (flim == -2) return (GEN)f;
    2182             :   /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
    2183             :    * so that v_P(t) = e(P/p)-1 */
    2184      355821 :   if (f == N) {
    2185       39059 :     u = scalarcol_shallow(p,N);
    2186       39059 :     t = gen_1;
    2187       39059 :     e = 1;
    2188             :   } else {
    2189             :     GEN mt;
    2190      316762 :     u = uniformizer(nf, S, P, V, p, ramif);
    2191      316735 :     t = FpM_deplin(zk_multable(nf,u), p);
    2192      316770 :     mt = zk_multable(nf, t);
    2193      316766 :     e = ramif? 1 + ZC_nfval(t,mk_pr(p,u,0,0,mt)): 1;
    2194      316742 :     t = mt;
    2195             :   }
    2196      355801 :   return mk_pr(p,u,e,f,t);
    2197             : }
    2198             : 
    2199             : /* true nf */
    2200             : static GEN
    2201      217281 : primedec_end(GEN nf, GEN L, GEN p, long flim)
    2202             : {
    2203      217281 :   long i, j, l = lg(L), N = nf_get_degree(nf);
    2204      217281 :   GEN LV = get_LV(nf, L,p,N);
    2205      217284 :   int ramif = dvdii(nf_get_disc(nf), p);
    2206      217260 :   norm_S S; init_norm(&S, nf, p);
    2207      666055 :   for (i = j = 1; i < l; i++)
    2208             :   {
    2209      449178 :     GEN P = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N, flim);
    2210      449172 :     if (!P) continue;
    2211      446351 :     gel(L,j++) = P;
    2212      446351 :     if (flim == -1) return P;
    2213             :   }
    2214      216877 :   setlg(L, j); return L;
    2215             : }
    2216             : 
    2217             : /* prime ideal decomposition of p; if flim>0, restrict to f(P,p) <= flim
    2218             :  * if flim = -1 return only the first P
    2219             :  * if flim = -2 return only the f(P/p) in a t_VECSMALL; true nf */
    2220             : static GEN
    2221     1868651 : primedec_aux(GEN nf, GEN p, long flim)
    2222             : {
    2223     1868651 :   const long TYP = (flim == -2)? t_VECSMALL: t_VEC;
    2224     1868651 :   GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
    2225             :   long i, k, c, iL, N;
    2226             :   int kummer;
    2227             : 
    2228     1868646 :   F = FpX_factor(T, p);
    2229     1868756 :   E = gel(F,2);
    2230     1868756 :   F = gel(F,1);
    2231             : 
    2232     1868756 :   k = lg(F); if (k == 1) errprime(p);
    2233     1868756 :   if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
    2234             :   {
    2235     1649798 :     L = cgetg(k, TYP);
    2236     4065438 :     for (i=1; i<k; i++)
    2237             :     {
    2238     2961770 :       GEN t = gel(F,i);
    2239     2961770 :       long f = degpol(t);
    2240     2961749 :       if (flim > 0 && f > flim) { setlg(L, i); break; }
    2241     2420328 :       if (flim == -2)
    2242           0 :         L[i] = f;
    2243             :       else
    2244     2420328 :         gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
    2245     2420371 :       if (flim == -1) return gel(L,1);
    2246             :     }
    2247     1645088 :     return L;
    2248             :   }
    2249             : 
    2250      218668 :   kummer = 0;
    2251      218668 :   g = FpXV_prod(F, p);
    2252      218663 :   h = FpX_div(T,g,p);
    2253      218673 :   f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
    2254             : 
    2255      218669 :   N = degpol(T);
    2256      218668 :   L = cgetg(N+1,TYP);
    2257      218673 :   iL = 1;
    2258      556892 :   for (i=1; i<k; i++)
    2259      339614 :     if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
    2260       91564 :     {
    2261       92957 :       GEN t = gel(F,i);
    2262       92957 :       kummer = 1;
    2263       92957 :       gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
    2264       92957 :       if (flim == -1) return gel(L,1);
    2265             :     }
    2266             :     else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
    2267      246655 :       E[i] = 0;
    2268             : 
    2269             :   /* phi matrix of x -> x^p - x in algebra Z_K/p */
    2270      217278 :   Ip = pradical(nf,p,&phi);
    2271             : 
    2272             :   /* split etale algebra Z_K / (p,Ip) */
    2273      217266 :   h = cgetg(N+1,t_VEC);
    2274      217279 :   if (kummer)
    2275             :   { /* split off Kummer factors */
    2276       49933 :     GEN mb, b = NULL;
    2277      203738 :     for (i=1; i<k; i++)
    2278      153805 :       if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
    2279       49933 :     if (!b) errprime(p);
    2280       49933 :     b = FpC_red(poltobasis(nf,b), p);
    2281       49932 :     mb = FpM_red(zk_multable(nf,b), p);
    2282             :     /* Fp-base of ideal (Ip, b) in ZK/p */
    2283       49930 :     gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
    2284             :   }
    2285             :   else
    2286      167346 :     gel(h,1) = Ip;
    2287             : 
    2288      217279 :   UN = col_ei(N, 1);
    2289      482711 :   for (c=1; c; c--)
    2290             :   { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
    2291             :        H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
    2292      265426 :     GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
    2293      265426 :     long dim, r = lg(H)-1;
    2294             : 
    2295      265426 :     M   = FpM_suppl(shallowconcat(H,UN), p);
    2296      265428 :     Mi  = FpM_inv(M, p);
    2297      265430 :     M2  = vecslice(M, r+1,N); /* M = (H|M2) invertible */
    2298      265430 :     Mi2 = rowslice(Mi,r+1,N);
    2299             :     /* FIXME: FpM_mul(,M2) could be done with vecpermute */
    2300      265433 :     phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
    2301      265428 :     mat1 = FpM_ker(phi2, p);
    2302      265434 :     dim = lg(mat1)-1; /* A2 product of 'dim' fields */
    2303      265434 :     if (dim > 1)
    2304             :     { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
    2305      128126 :       GEN R, a, mula, mul2, v = gel(mat1,2);
    2306             :       long n;
    2307             : 
    2308      128126 :       a = FpM_FpC_mul(M2,v, p); /* not a scalar */
    2309      128122 :       mula = FpM_red(zk_multable(nf,a), p);
    2310      128122 :       mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
    2311      128125 :       R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
    2312      128127 :       n = lg(R)-1;
    2313      396907 :       for (i=1; i<=n; i++)
    2314             :       {
    2315      268783 :         GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
    2316      268778 :         gel(h,c++) = FpM_image(shallowconcat(H, I), p);
    2317             :       }
    2318      128124 :       if (n == dim)
    2319      327992 :         for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
    2320             :     }
    2321             :     else /* A2 field ==> H maximal, f = N-r = dim(A2) */
    2322      137308 :       gel(L,iL++) = H;
    2323             :   }
    2324      217285 :   setlg(L, iL);
    2325      217282 :   return primedec_end(nf, L, p, flim);
    2326             : }
    2327             : 
    2328             : GEN
    2329     1861747 : idealprimedec_limit_f(GEN nf, GEN p, long f)
    2330             : {
    2331     1861747 :   pari_sp av = avma;
    2332             :   GEN v;
    2333     1861747 :   if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
    2334     1861747 :   if (f < 0) pari_err_DOMAIN("idealprimedec", "f", "<", gen_0, stoi(f));
    2335     1861747 :   v = primedec_aux(checknf(nf), p, f);
    2336     1861610 :   v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
    2337     1861703 :   return gerepileupto(av,v);
    2338             : }
    2339             : /* true nf */
    2340             : GEN
    2341        6587 : idealprimedec_galois(GEN nf, GEN p)
    2342             : {
    2343        6587 :   pari_sp av = avma;
    2344        6587 :   GEN v = primedec_aux(nf, p, -1);
    2345        6587 :   return gerepilecopy(av,v);
    2346             : }
    2347             : /* true nf */
    2348             : GEN
    2349         322 : idealprimedec_degrees(GEN nf, GEN p)
    2350             : {
    2351         322 :   pari_sp av = avma;
    2352         322 :   GEN v = primedec_aux(nf, p, -2);
    2353         322 :   vecsmall_sort(v); return gerepileuptoleaf(av, v);
    2354             : }
    2355             : GEN
    2356      491073 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
    2357      491073 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
    2358             : GEN
    2359      448528 : idealprimedec(GEN nf, GEN p)
    2360      448528 : { return idealprimedec_limit_f(nf, p, 0); }
    2361             : GEN
    2362        9793 : nf_pV_to_prV(GEN nf, GEN P)
    2363             : {
    2364             :   long i, l;
    2365        9793 :   GEN Q = cgetg_copy(P,&l);
    2366        9793 :   if (l == 1) return Q;
    2367        6342 :   for (i = 1; i < l; i++) gel(Q,i) = idealprimedec(nf, gel(P,i));
    2368        1946 :   return shallowconcat1(Q);
    2369             : }
    2370             : 
    2371             : /* return [Fp[x]: Fp] */
    2372             : static long
    2373        4151 : ffdegree(GEN x, GEN frob, GEN p)
    2374             : {
    2375        4151 :   pari_sp av = avma;
    2376        4151 :   long d, f = lg(frob)-1;
    2377        4151 :   GEN y = x;
    2378             : 
    2379       13363 :   for (d=1; d < f; d++)
    2380             :   {
    2381       11018 :     y = FpM_FpC_mul(frob, y, p);
    2382       11018 :     if (ZV_equal(y, x)) break;
    2383             :   }
    2384        4151 :   return gc_long(av,d);
    2385             : }
    2386             : 
    2387             : static GEN
    2388       89593 : lift_to_zk(GEN v, GEN c, long N)
    2389             : {
    2390       89593 :   GEN w = zerocol(N);
    2391       89593 :   long i, l = lg(c);
    2392      299887 :   for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
    2393       89593 :   return w;
    2394             : }
    2395             : 
    2396             : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
    2397             : static GEN
    2398      765324 : anti_uniformizer(GEN nf, GEN pr)
    2399             : {
    2400      765324 :   long N = nf_get_degree(nf), e = pr_get_e(pr);
    2401             :   GEN p, b, z;
    2402             : 
    2403      765305 :   if (e * pr_get_f(pr) == N) return gen_1;
    2404      351119 :   p = pr_get_p(pr);
    2405      351115 :   b = pr_get_tau(pr); /* ZM */
    2406      351111 :   if (e != 1)
    2407             :   {
    2408       22427 :     GEN q = powiu(pr_get_p(pr), e-1);
    2409       22427 :     b = ZM_Z_divexact(ZM_powu(b,e), q);
    2410             :   }
    2411             :   /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
    2412      351112 :   z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
    2413      351133 :   z = idealaddtoone_raw(nf, pr, z);
    2414      351105 :   return Z_ZC_sub(gen_1, FpC_center(FpC_red(z,p), p, shifti(p,-1)));
    2415             : }
    2416             : 
    2417             : #define mpr_TAU 1
    2418             : #define mpr_FFP 2
    2419             : #define mpr_NFP 5
    2420             : #define SMALLMODPR 4
    2421             : #define LARGEMODPR 6
    2422             : static GEN
    2423     2313404 : modpr_TAU(GEN modpr)
    2424             : {
    2425     2313404 :   GEN tau = gel(modpr,mpr_TAU);
    2426     2313404 :   return isintzero(tau)? NULL: tau;
    2427             : }
    2428             : 
    2429             : /* prh = HNF matrix, which is identity but for the first line. Return a
    2430             :  * projector to ZK / prh ~ Z/prh[1,1] */
    2431             : GEN
    2432      739444 : dim1proj(GEN prh)
    2433             : {
    2434      739444 :   long i, N = lg(prh)-1;
    2435      739444 :   GEN ffproj = cgetg(N+1, t_VEC);
    2436      739450 :   GEN x, q = gcoeff(prh,1,1);
    2437      739450 :   gel(ffproj,1) = gen_1;
    2438     1497962 :   for (i=2; i<=N; i++)
    2439             :   {
    2440      758607 :     x = gcoeff(prh,1,i);
    2441      758607 :     if (signe(x)) x = subii(q,x);
    2442      758512 :     gel(ffproj,i) = x;
    2443             :   }
    2444      739355 :   return ffproj;
    2445             : }
    2446             : 
    2447             : /* p not necessarily prime, but coprime to denom(basis) */
    2448             : GEN
    2449         203 : QXQV_to_FpM(GEN basis, GEN T, GEN p)
    2450             : {
    2451         203 :   long i, l = lg(basis), f = degpol(T);
    2452         203 :   GEN z = cgetg(l, t_MAT);
    2453        4515 :   for (i = 1; i < l; i++)
    2454             :   {
    2455        4312 :     GEN w = gel(basis,i);
    2456        4312 :     if (typ(w) == t_INT)
    2457           0 :       w = scalarcol_shallow(w, f);
    2458             :     else
    2459             :     {
    2460             :       GEN dx;
    2461        4312 :       w = Q_remove_denom(w, &dx);
    2462        4312 :       w = FpXQ_red(w, T, p);
    2463        4312 :       if (dx)
    2464             :       {
    2465           0 :         dx = Fp_inv(dx, p);
    2466           0 :         if (!equali1(dx)) w = FpX_Fp_mul(w, dx, p);
    2467             :       }
    2468        4312 :       w = RgX_to_RgC(w, f);
    2469             :     }
    2470        4312 :     gel(z,i) = w; /* w_i mod (T,p) */
    2471             :   }
    2472         203 :   return z;
    2473             : }
    2474             : 
    2475             : /* initialize reduction mod pr; if zk = 1, will only init data required to
    2476             :  * reduce *integral* element.  Realize (O_K/pr) as Fp[X] / (T), for a
    2477             :  * *monic* T; use variable vT for varn(T) */
    2478             : static GEN
    2479      824123 : modprinit(GEN nf, GEN pr, int zk, long vT)
    2480             : {
    2481      824123 :   pari_sp av = avma;
    2482             :   GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
    2483             :   long N, i, k, f;
    2484             : 
    2485      824123 :   nf = checknf(nf); checkprid(pr);
    2486      824109 :   if (vT < 0) vT = nf_get_varn(nf);
    2487      824094 :   f = pr_get_f(pr);
    2488      824089 :   N = nf_get_degree(nf);
    2489      824081 :   prh = pr_hnf(nf, pr);
    2490      824108 :   tau = zk? gen_0: anti_uniformizer(nf, pr);
    2491      824082 :   p = pr_get_p(pr);
    2492             : 
    2493      824080 :   if (f == 1)
    2494             :   {
    2495      723990 :     res = cgetg(SMALLMODPR, t_COL);
    2496      723991 :     gel(res,mpr_TAU) = tau;
    2497      723991 :     gel(res,mpr_FFP) = dim1proj(prh);
    2498      723919 :     gel(res,3) = pr; return gerepilecopy(av, res);
    2499             :   }
    2500             : 
    2501      100090 :   c = cgetg(f+1, t_VECSMALL);
    2502      100097 :   ffproj = cgetg(N+1, t_MAT);
    2503      411880 :   for (k=i=1; i<=N; i++)
    2504             :   {
    2505      311782 :     x = gcoeff(prh, i,i);
    2506      311782 :     if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
    2507             :     else
    2508       83904 :       gel(ffproj,i) = ZC_neg(gel(prh,i));
    2509             :   }
    2510      100098 :   ffproj = rowpermute(ffproj, c);
    2511      100098 :   if (! dvdii(nf_get_index(nf), p))
    2512             :   {
    2513       59514 :     GEN basis = nf_get_zkprimpart(nf), D = nf_get_zkden(nf);
    2514       59514 :     if (N == f)
    2515             :     { /* pr inert */
    2516       38905 :       T = nf_get_pol(nf);
    2517       38905 :       T = FpX_red(T,p);
    2518       38905 :       ffproj = RgV_to_RgM(basis, lg(basis)-1);
    2519             :     }
    2520             :     else
    2521             :     {
    2522       20609 :       T = RgV_RgC_mul(basis, pr_get_gen(pr));
    2523       20609 :       T = FpX_normalize(FpX_red(T,p),p);
    2524       20609 :       basis = FqV_red(vecpermute(basis,c), T, p);
    2525       20609 :       basis = RgV_to_RgM(basis, lg(basis)-1);
    2526       20609 :       ffproj = ZM_mul(basis, ffproj);
    2527             :     }
    2528       59514 :     setvarn(T, vT);
    2529       59514 :     ffproj = FpM_red(ffproj, p);
    2530       59514 :     if (!equali1(D))
    2531             :     {
    2532       30093 :       D = modii(D,p);
    2533       30093 :       if (!equali1(D)) ffproj = FpM_Fp_mul(ffproj, Fp_inv(D,p), p);
    2534             :     }
    2535             : 
    2536       59513 :     res = cgetg(SMALLMODPR+1, t_COL);
    2537       59514 :     gel(res,mpr_TAU) = tau;
    2538       59514 :     gel(res,mpr_FFP) = ffproj;
    2539       59514 :     gel(res,3) = pr;
    2540       59514 :     gel(res,4) = T; return gerepilecopy(av, res);
    2541             :   }
    2542             : 
    2543       40585 :   if (uisprime(f))
    2544             :   {
    2545       38240 :     mul = ei_multable(nf, c[2]);
    2546       38240 :     mul = vecpermute(mul, c);
    2547             :   }
    2548             :   else
    2549             :   {
    2550             :     GEN v, u, u2, frob;
    2551             :     long deg,deg1,deg2;
    2552             : 
    2553             :     /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
    2554        2345 :     frob = cgetg(f+1, t_MAT);
    2555       12061 :     for (i=1; i<=f; i++)
    2556             :     {
    2557        9716 :       x = pow_ei_mod_p(nf,c[i],p);
    2558        9716 :       gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
    2559             :     }
    2560        2345 :     u = col_ei(f,2); k = 2;
    2561        2345 :     deg1 = ffdegree(u, frob, p);
    2562        4130 :     while (deg1 < f)
    2563             :     {
    2564        1785 :       k++; u2 = col_ei(f, k);
    2565        1785 :       deg2 = ffdegree(u2, frob, p);
    2566        1785 :       deg = ulcm(deg1,deg2);
    2567        1785 :       if (deg == deg1) continue;
    2568        1778 :       if (deg == deg2) { deg1 = deg2; u = u2; continue; }
    2569          21 :       u = ZC_add(u, u2);
    2570          21 :       while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
    2571          21 :       deg1 = deg;
    2572             :     }
    2573        2345 :     v = lift_to_zk(u,c,N);
    2574             : 
    2575        2345 :     mul = cgetg(f+1,t_MAT);
    2576        2345 :     gel(mul,1) = v; /* assume w_1 = 1 */
    2577        9716 :     for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
    2578             :   }
    2579             : 
    2580             :   /* Z_K/pr = Fp(v), mul = mul by v */
    2581       40585 :   mul = FpM_red(mul, p);
    2582       40585 :   mul = FpM_mul(ffproj, mul, p);
    2583             : 
    2584       40586 :   pow = get_powers(mul, p);
    2585       40585 :   T = RgV_to_RgX(FpM_deplin(pow, p), vT);
    2586       40586 :   nfproj = cgetg(f+1, t_MAT);
    2587      127834 :   for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
    2588             : 
    2589       40586 :   setlg(pow, f+1);
    2590       40586 :   ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
    2591             : 
    2592       40586 :   res = cgetg(LARGEMODPR, t_COL);
    2593       40586 :   gel(res,mpr_TAU) = tau;
    2594       40586 :   gel(res,mpr_FFP) = ffproj;
    2595       40586 :   gel(res,3) = pr;
    2596       40586 :   gel(res,4) = T;
    2597       40586 :   gel(res,mpr_NFP) = nfproj; return gerepilecopy(av, res);
    2598             : }
    2599             : 
    2600             : GEN
    2601           7 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0, -1); }
    2602             : GEN
    2603       40249 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1, -1); }
    2604             : GEN
    2605          70 : nfmodprinit0(GEN nf, GEN pr, long v) { return modprinit(nf, pr, 0, v); }
    2606             : 
    2607             : /* x may be a modpr */
    2608             : static int
    2609     2864564 : ok_modpr(GEN x)
    2610     2864564 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
    2611             : void
    2612         210 : checkmodpr(GEN x)
    2613             : {
    2614         210 :   if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
    2615         210 :   checkprid(modpr_get_pr(x));
    2616         210 : }
    2617             : GEN
    2618        3549 : get_modpr(GEN x)
    2619        3549 : { return ok_modpr(x)? x: NULL; }
    2620             : 
    2621             : int
    2622     7781178 : checkprid_i(GEN x)
    2623             : {
    2624     7109456 :   return (typ(x) == t_VEC && lg(x) == 6
    2625     7072644 :           && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
    2626    14890634 :           && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
    2627             : }
    2628             : void
    2629     6825409 : checkprid(GEN x)
    2630     6825409 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
    2631             : GEN
    2632      740677 : get_prid(GEN x)
    2633             : {
    2634      740677 :   long lx = lg(x);
    2635      740677 :   if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
    2636      740677 :   if (checkprid_i(x)) return x;
    2637      534142 :   if (ok_modpr(x)) {
    2638        2989 :     x = modpr_get_pr(x);
    2639        2989 :     if (checkprid_i(x)) return x;
    2640             :   }
    2641      531153 :   return NULL;
    2642             : }
    2643             : 
    2644             : static GEN
    2645     2326669 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
    2646             : {
    2647     2326669 :   GEN modpr = ok_modpr(*pr)? *pr: modprinit(nf, *pr, zk, -1);
    2648     2326774 :   *T = modpr_get_T(modpr);
    2649     2326706 :   *pr = modpr_get_pr(modpr);
    2650     2326689 :   *p = pr_get_p(*pr); return modpr;
    2651             : }
    2652             : 
    2653             : /* Return an element of O_K which is set to x Mod T */
    2654             : GEN
    2655        4333 : modpr_genFq(GEN modpr)
    2656             : {
    2657        4333 :   switch(lg(modpr))
    2658             :   {
    2659         917 :     case SMALLMODPR: /* Fp */
    2660         917 :       return gen_1;
    2661        1568 :     case LARGEMODPR:  /* painful case, p \mid index */
    2662        1568 :       return gmael(modpr,mpr_NFP, 2);
    2663        1848 :     default: /* trivial case : p \nmid index */
    2664             :     {
    2665        1848 :       long v = varn( modpr_get_T(modpr) );
    2666        1848 :       return pol_x(v);
    2667             :     }
    2668             :   }
    2669             : }
    2670             : 
    2671             : GEN
    2672     2308140 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2673     2308140 :   GEN modpr = to_ff_init(nf,pr,T,p,0);
    2674     2308158 :   GEN tau = modpr_TAU(modpr);
    2675     2308117 :   if (!tau) gel(modpr,mpr_TAU) = anti_uniformizer(nf, *pr);
    2676     2308117 :   return modpr;
    2677             : }
    2678             : GEN
    2679       18523 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2680       18523 :   return to_ff_init(nf,pr,T,p,1);
    2681             : }
    2682             : 
    2683             : /* assume x in 'basis' form (t_COL) */
    2684             : GEN
    2685     2745527 : zk_to_Fq(GEN x, GEN modpr)
    2686             : {
    2687     2745527 :   GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2688     2745548 :   GEN ffproj = gel(modpr,mpr_FFP);
    2689     2745548 :   GEN T = modpr_get_T(modpr);
    2690     2745562 :   return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
    2691             : }
    2692             : 
    2693             : /* REDUCTION Modulo a prime ideal */
    2694             : 
    2695             : /* nf a true nf */
    2696             : static GEN
    2697     7985765 : Rg_to_ff(GEN nf, GEN x0, GEN modpr)
    2698             : {
    2699     7985765 :   GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2700     7985773 :   long tx = typ(x);
    2701             : 
    2702     7985773 :   if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
    2703     7985773 :   switch(tx)
    2704             :   {
    2705     5325115 :     case t_INT: return modii(x, p);
    2706        6608 :     case t_FRAC: return Rg_to_Fp(x, p);
    2707      193883 :     case t_POL:
    2708      193883 :       switch(lg(x))
    2709             :       {
    2710         224 :         case 2: return gen_0;
    2711       25766 :         case 3: return Rg_to_Fp(gel(x,2), p);
    2712             :       }
    2713      167893 :       x = Q_remove_denom(x, &den);
    2714      167887 :       x = poltobasis(nf, x);
    2715             :       /* content(x) and den may not be coprime */
    2716      167715 :       break;
    2717     2460219 :     case t_COL:
    2718     2460219 :       x = Q_remove_denom(x, &den);
    2719             :       /* content(x) and den are coprime */
    2720     2460215 :       if (lg(x)-1 == nf_get_degree(nf)) break;
    2721          48 :     default: pari_err_TYPE("Rg_to_ff",x);
    2722             :       return NULL;/*LCOV_EXCL_LINE*/
    2723             :   }
    2724     2627873 :   if (den)
    2725             :   {
    2726      111392 :     long v = Z_pvalrem(den, p, &den);
    2727      111392 :     if (v)
    2728             :     {
    2729        5796 :       if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
    2730             :       /* now v = valuation(true denominator of x) */
    2731        5796 :       if (v > 0)
    2732             :       {
    2733        5264 :         GEN tau = modpr_TAU(modpr);
    2734        5264 :         if (!tau) pari_err_TYPE("zk_to_ff", x0);
    2735        5264 :         x = nfmuli(nf,x, nfpow_u(nf, tau, v));
    2736        5264 :         v -= ZV_pvalrem(x, p, &x);
    2737             :       }
    2738        5796 :       if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2739        5768 :       if (v) return gen_0;
    2740        5313 :       if (is_pm1(den)) den = NULL;
    2741             :     }
    2742      110909 :     x = FpC_red(x, p);
    2743             :   }
    2744     2627390 :   x = zk_to_Fq(x, modpr);
    2745     2627449 :   if (den)
    2746             :   {
    2747      107591 :     GEN c = Fp_inv(den, p);
    2748      107591 :     x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
    2749             :   }
    2750     2627449 :   return x;
    2751             : }
    2752             : 
    2753             : GEN
    2754         210 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
    2755             : {
    2756         210 :   pari_sp av = avma;
    2757         210 :   nf = checknf(nf); checkmodpr(modpr);
    2758         210 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(Rg_to_ff(nf,x,modpr),modpr)));
    2759             : }
    2760             : 
    2761             : GEN
    2762         329 : nfmodpr(GEN nf, GEN x, GEN pr)
    2763             : {
    2764         329 :   pari_sp av = avma;
    2765             :   GEN T, p, modpr;
    2766         329 :   nf = checknf(nf);
    2767         329 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2768         322 :   if (typ(x) == t_MAT && lg(x) == 3)
    2769             :   {
    2770          35 :     GEN y, v = famat_nfvalrem(nf, x, pr, &y);
    2771          35 :     long s = signe(v);
    2772          35 :     if (s < 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2773          28 :     if (s > 0) return gc_const(av, gen_0);
    2774          14 :     x = FqV_factorback(nfV_to_FqV(gel(y,1), nf, modpr), gel(y,2), T, p);
    2775          14 :     return gerepileupto(av, x);
    2776             :   }
    2777         287 :   x = Rg_to_ff(nf, x, modpr);
    2778         175 :   x = Fq_to_FF(x, Tp_to_FF(T,p));
    2779         175 :   return gerepilecopy(av, x);
    2780             : }
    2781             : GEN
    2782          77 : nfmodprlift(GEN nf, GEN x, GEN pr)
    2783             : {
    2784          77 :   pari_sp av = avma;
    2785             :   GEN y, T, p, modpr;
    2786             :   long i, l, d;
    2787          77 :   nf = checknf(nf);
    2788          77 :   switch(typ(x))
    2789             :   {
    2790           7 :     case t_INT: return icopy(x);
    2791          42 :     case t_FFELT: break;
    2792          28 :     case t_VEC: case t_COL: case t_MAT:
    2793          28 :       y = cgetg_copy(x,&l);
    2794          63 :       for (i = 1; i < l; i++) gel(y,i) = nfmodprlift(nf,gel(x,i),pr);
    2795          28 :       return y;
    2796           0 :     default: pari_err_TYPE("nfmodprlit",x);
    2797             :   }
    2798          42 :   x = FF_to_FpXQ(x);
    2799          42 :   setvarn(x, nf_get_varn(nf));
    2800          42 :   d = degpol(x);
    2801          42 :   if (d <= 0) { set_avma(av); return d? gen_0: icopy(gel(x,2)); }
    2802          14 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2803          14 :   return gerepilecopy(av, Fq_to_nf(x, modpr));
    2804             : }
    2805             : 
    2806             : /* lift A from residue field to nf */
    2807             : GEN
    2808     1436105 : Fq_to_nf(GEN A, GEN modpr)
    2809             : {
    2810             :   long dA;
    2811     1436105 :   if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
    2812       42721 :   dA = degpol(A);
    2813       42721 :   if (dA <= 0) return dA ? gen_0: gel(A,2);
    2814       39459 :   return ZM_ZX_mul(gel(modpr,mpr_NFP), A);
    2815             : }
    2816             : GEN
    2817           0 : FqV_to_nfV(GEN x, GEN modpr)
    2818           0 : { pari_APPLY_same(Fq_to_nf(gel(x,i), modpr)) }
    2819             : GEN
    2820        8582 : FqM_to_nfM(GEN A, GEN modpr)
    2821             : {
    2822        8582 :   long i,j,h,l = lg(A);
    2823        8582 :   GEN B = cgetg(l, t_MAT);
    2824             : 
    2825        8582 :   if (l == 1) return B;
    2826        7945 :   h = lgcols(A);
    2827       36393 :   for (j=1; j<l; j++)
    2828             :   {
    2829       28448 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    2830      190736 :     for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
    2831             :   }
    2832        7945 :   return B;
    2833             : }
    2834             : GEN
    2835       10381 : FqX_to_nfX(GEN A, GEN modpr)
    2836             : {
    2837             :   long i, l;
    2838             :   GEN B;
    2839             : 
    2840       10381 :   if (typ(A)!=t_POL) return icopy(A); /* scalar */
    2841       10381 :   B = cgetg_copy(A, &l); B[1] = A[1];
    2842       47180 :   for (i=2; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    2843       10381 :   return B;
    2844             : }
    2845             : 
    2846             : /* reduce A to residue field */
    2847             : GEN
    2848     7985258 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
    2849             : {
    2850     7985258 :   pari_sp av = avma;
    2851     7985258 :   return gerepileupto(av, Rg_to_ff(checknf(nf), A, modpr));
    2852             : }
    2853             : /* A t_VEC/t_COL */
    2854             : GEN
    2855        4458 : nfV_to_FqV(GEN A, GEN nf,GEN modpr)
    2856             : {
    2857        4458 :   long i,l = lg(A);
    2858        4458 :   GEN B = cgetg(l,typ(A));
    2859       25552 :   for (i=1; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i), modpr);
    2860        4458 :   return B;
    2861             : }
    2862             : /* A  t_MAT */
    2863             : GEN
    2864        4557 : nfM_to_FqM(GEN A, GEN nf,GEN modpr)
    2865             : {
    2866        4557 :   long i,j,h,l = lg(A);
    2867        4557 :   GEN B = cgetg(l,t_MAT);
    2868             : 
    2869        4557 :   if (l == 1) return B;
    2870        4557 :   h = lgcols(A);
    2871      137641 :   for (j=1; j<l; j++)
    2872             :   {
    2873      133084 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    2874      953540 :     for (i=1; i<h; i++) gel(Bj,i) = nf_to_Fq(nf, gel(Aj,i), modpr);
    2875             :   }
    2876        4557 :   return B;
    2877             : }
    2878             : /* A t_POL */
    2879             : GEN
    2880        8827 : nfX_to_FqX(GEN A, GEN nf,GEN modpr)
    2881             : {
    2882        8827 :   long i,l = lg(A);
    2883        8827 :   GEN B = cgetg(l,t_POL); B[1] = A[1];
    2884       50694 :   for (i=2; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i),modpr);
    2885        8820 :   return normalizepol_lg(B, l);
    2886             : }
    2887             : 
    2888             : /*******************************************************************/
    2889             : /*                                                                 */
    2890             : /*                       RELATIVE ROUND 2                          */
    2891             : /*                                                                 */
    2892             : /*******************************************************************/
    2893             : /* Shallow functions */
    2894             : /* FIXME: use a bb_field and export the nfX_* routines */
    2895             : static GEN
    2896        3969 : nfX_sub(GEN nf, GEN x, GEN y)
    2897             : {
    2898        3969 :   long i, lx = lg(x), ly = lg(y);
    2899             :   GEN z;
    2900        3969 :   if (ly <= lx) {
    2901        3969 :     z = cgetg(lx,t_POL); z[1] = x[1];
    2902       25130 :     for (i=2; i < ly; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    2903        3969 :     for (   ; i < lx; i++) gel(z,i) = gel(x,i);
    2904        3969 :     z = normalizepol_lg(z, lx);
    2905             :   } else {
    2906           0 :     z = cgetg(ly,t_POL); z[1] = y[1];
    2907           0 :     for (i=2; i < lx; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    2908           0 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    2909           0 :     z = normalizepol_lg(z, ly);
    2910             :   }
    2911        3969 :   return z;
    2912             : }
    2913             : /* FIXME: quadratic multiplication */
    2914             : static GEN
    2915       58905 : nfX_mul(GEN nf, GEN a, GEN b)
    2916             : {
    2917       58905 :   long da = degpol(a), db = degpol(b), dc, lc, k;
    2918             :   GEN c;
    2919       58905 :   if (da < 0 || db < 0) return gen_0;
    2920       58905 :   dc = da + db;
    2921       58905 :   if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
    2922       58905 :   lc = dc+3;
    2923       58905 :   c = cgetg(lc, t_POL); c[1] = a[1];
    2924      478688 :   for (k = 0; k <= dc; k++)
    2925             :   {
    2926      419783 :     long i, I = minss(k, da);
    2927      419783 :     GEN d = NULL;
    2928     1438080 :     for (i = maxss(k-db, 0); i <= I; i++)
    2929             :     {
    2930     1018297 :       GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
    2931     1018297 :       d = d? nfadd(nf, d, e): e;
    2932             :     }
    2933      419783 :     gel(c, k+2) = d;
    2934             :   }
    2935       58905 :   return normalizepol_lg(c, lc);
    2936             : }
    2937             : /* assume b monic */
    2938             : static GEN
    2939       54936 : nfX_rem(GEN nf, GEN a, GEN b)
    2940             : {
    2941       54936 :   long da = degpol(a), db = degpol(b);
    2942       54936 :   if (da < 0) return gen_0;
    2943       54936 :   a = leafcopy(a);
    2944      133518 :   while (da >= db)
    2945             :   {
    2946       78582 :     long i, k = da;
    2947       78582 :     GEN A = gel(a, k+2);
    2948      579313 :     for (i = db-1, k--; i >= 0; i--, k--)
    2949      500731 :       gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
    2950       78582 :     a = normalizepol_lg(a, lg(a)-1);
    2951       78582 :     da = degpol(a);
    2952             :   }
    2953       54936 :   return a;
    2954             : }
    2955             : static GEN
    2956       54936 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
    2957             : {
    2958       54936 :   GEN c = nfX_mul(nf, a, b);
    2959       54936 :   if (typ(c) != t_POL) return c;
    2960       54936 :   return nfX_rem(nf, c, T);
    2961             : }
    2962             : 
    2963             : static void
    2964       10836 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
    2965             : {
    2966             :   long i;
    2967       10836 :   if (typ(Ix) == t_VEC) /* standard */
    2968       41307 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
    2969             :   else /* constant ideal */
    2970       10829 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
    2971       10836 : }
    2972             : 
    2973             : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
    2974             :  * module generated by x and y. */
    2975             : static GEN
    2976        5418 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
    2977             : {
    2978        5418 :   long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
    2979        5418 :   GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
    2980        5418 :   fill(lx, H     , Hx, I     , Ix);
    2981        5418 :   fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
    2982             : }
    2983             : static GEN
    2984        1841 : rnfjoinmodules(GEN nf, GEN x, GEN y)
    2985             : {
    2986        1841 :   if (!x) return y;
    2987        1127 :   if (!y) return x;
    2988        1127 :   return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
    2989             : }
    2990             : 
    2991             : typedef struct {
    2992             :   GEN multab, T,p;
    2993             :   long h;
    2994             : } rnfeltmod_muldata;
    2995             : 
    2996             : static GEN
    2997       63308 : _sqr(void *data, GEN x)
    2998             : {
    2999       63308 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3000       44653 :   GEN z = x? tablesqr(D->multab,x)
    3001       63308 :            : tablemul_ei_ej(D->multab,D->h,D->h);
    3002       63308 :   return FqV_red(z,D->T,D->p);
    3003             : }
    3004             : static GEN
    3005       10458 : _msqr(void *data, GEN x)
    3006             : {
    3007       10458 :   GEN x2 = _sqr(data, x), z;
    3008       10458 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    3009       10458 :   z = tablemul_ei(D->multab, x2, D->h);
    3010       10458 :   return FqV_red(z,D->T,D->p);
    3011             : }
    3012             : 
    3013             : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
    3014             : static GEN
    3015       18655 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
    3016             : {
    3017       18655 :   pari_sp av = avma;
    3018             :   GEN y;
    3019             :   rnfeltmod_muldata D;
    3020             : 
    3021       18655 :   if (!signe(n)) return gen_1;
    3022             : 
    3023       18655 :   D.multab = multab;
    3024       18655 :   D.h = h;
    3025       18655 :   D.T = T;
    3026       18655 :   D.p = p;
    3027       18655 :   y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
    3028       18655 :   return gerepilecopy(av, y);
    3029             : }
    3030             : 
    3031             : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
    3032             :  * a root, cf repres() */
    3033             : static GEN
    3034          21 : FqX_non_root(GEN P, GEN T, GEN p)
    3035             : {
    3036          21 :   long dP = degpol(P), f, vT;
    3037             :   long i, j, k, pi, pp;
    3038             :   GEN v;
    3039             : 
    3040          21 :   if (dP == 0) return gen_1;
    3041          21 :   pp = is_bigint(p) ? dP+1: itos(p);
    3042          21 :   v = cgetg(dP + 2, t_VEC);
    3043          21 :   gel(v,1) = gen_0;
    3044          21 :   if (T)
    3045           0 :   { f = degpol(T); vT = varn(T); }
    3046             :   else
    3047          21 :   { f = 1; vT = 0; }
    3048          42 :   for (i=pi=1; i<=f; i++,pi*=pp)
    3049             :   {
    3050          21 :     GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
    3051          42 :     for (j=1; j<pp; j++)
    3052             :     {
    3053          42 :       for (k=1; k<=pi; k++)
    3054             :       {
    3055          21 :         GEN z = Fq_add(gel(v,k), jgi, T,p);
    3056          21 :         if (!gequal0(FqX_eval(P, z, T,p))) return z;
    3057          21 :         gel(v, j*pi+k) = z;
    3058             :       }
    3059          21 :       if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
    3060             :     }
    3061             :   }
    3062          21 :   return NULL;
    3063             : }
    3064             : 
    3065             : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
    3066             :  * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
    3067             :  * vdisc = v_pr( disc(P) ).
    3068             :  * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
    3069             :  *   O = enlarged order, given by a pseudo-basis
    3070             :  *   flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
    3071             :  *   v = v_pr(disc(O)). */
    3072             : static GEN
    3073        4004 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
    3074             : {
    3075             :   GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
    3076             :   long m, vt, r, d, i, j, mpr;
    3077             : 
    3078        4004 :   if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
    3079        3997 :   if (vdisc == 1) return NULL; /* pr-maximal */
    3080        3997 :   if (!only_maximal && !gequal1(leading_coeff(P)))
    3081           0 :     pari_err_IMPL( "the full Dedekind criterion in the nonmonic case");
    3082             :   /* either monic OR only_maximal = 1 */
    3083        3997 :   m = degpol(P);
    3084        3997 :   nfT = nf_get_pol(nf);
    3085        3997 :   modpr = nf_to_Fq_init(nf,&pr, &T, &p);
    3086        3997 :   Ppr = nfX_to_FqX(P, nf, modpr);
    3087        3990 :   mpr = degpol(Ppr);
    3088        3990 :   if (mpr < m) /* nonmonic => only_maximal = 1 */
    3089             :   {
    3090          21 :     if (mpr < 0) return NULL;
    3091          21 :     if (! RgX_valrem(Ppr, &Ppr))
    3092             :     { /* nonzero constant coefficient */
    3093           0 :       Ppr = RgX_shift_shallow(RgX_recip_i(Ppr), m - mpr);
    3094           0 :       P = RgX_recip_i(P);
    3095             :     }
    3096             :     else
    3097             :     {
    3098          21 :       GEN z = FqX_non_root(Ppr, T, p);
    3099          21 :       if (!z) pari_err_IMPL( "Dedekind in the difficult case");
    3100           0 :       z = Fq_to_nf(z, modpr);
    3101           0 :       if (typ(z) == t_INT)
    3102           0 :         P = RgX_translate(P, z);
    3103             :       else
    3104           0 :         P = RgXQX_translate(P, z, T);
    3105           0 :       P = RgX_recip_i(P);
    3106           0 :       Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
    3107             :     }
    3108             :   }
    3109        3969 :   A = gel(FqX_factor(Ppr,T,p),1);
    3110        3969 :   r = lg(A); /* > 1 */
    3111        3969 :   g = gel(A,1);
    3112        8316 :   for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
    3113        3969 :   h = FqX_div(Ppr,g, T, p);
    3114        3969 :   gzk = FqX_to_nfX(g, modpr);
    3115        3969 :   hzk = FqX_to_nfX(h, modpr);
    3116        3969 :   k = nfX_sub(nf, P, nfX_mul(nf, gzk,hzk));
    3117        3969 :   tau = pr_get_tau(pr);
    3118        3969 :   switch(typ(tau))
    3119             :   {
    3120        1792 :     case t_INT: k = gdiv(k, p); break;
    3121        2177 :     case t_MAT: k = RgX_Rg_div(tablemulvec(NULL,tau, k), p); break;
    3122             :   }
    3123        3969 :   k = nfX_to_FqX(k, nf, modpr);
    3124        3969 :   k = FqX_normalize(FqX_gcd(FqX_gcd(g,h,  T,p), k, T,p), T,p);
    3125        3969 :   d = degpol(k);  /* <= m */
    3126        3969 :   if (!d) return NULL; /* pr-maximal */
    3127        2457 :   if (only_maximal) return gen_0; /* not maximal */
    3128             : 
    3129        2436 :   A = cgetg(m+d+1,t_MAT);
    3130        2436 :   I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
    3131             :  /* base[2] temporarily multiplied by p, for the final nfhnfmod,
    3132             :   * which requires integral ideals */
    3133        2436 :   prinvp = pr_inv_p(pr); /* again multiplied by p */
    3134       14476 :   for (j=1; j<=m; j++)
    3135             :   {
    3136       12040 :     gel(A,j) = col_ei(m, j);
    3137       12040 :     gel(I,j) = p;
    3138             :   }
    3139        2436 :   pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
    3140        5257 :   for (   ; j<=m+d; j++)
    3141             :   {
    3142        2821 :     gel(A,j) = RgX_to_RgC(pal,m);
    3143        2821 :     gel(I,j) = prinvp;
    3144        2821 :     if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
    3145             :   }
    3146             :   /* the modulus is integral */
    3147        2436 :   base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
    3148        2436 :   gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
    3149        2436 :   vt = vdisc - 2*d;
    3150        2436 :   return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
    3151             : }
    3152             : 
    3153             : /* [L:K] = n */
    3154             : static GEN
    3155         903 : triv_order(long n)
    3156             : {
    3157         903 :   GEN z = cgetg(3, t_VEC);
    3158         903 :   gel(z,1) = matid(n);
    3159         903 :   gel(z,2) = const_vec(n, gen_1); return z;
    3160             : }
    3161             : 
    3162             : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
    3163             :  * is pr-maximal (resp. not pr-maximal). */
    3164             : GEN
    3165          91 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
    3166             : {
    3167          91 :   pari_sp av = avma;
    3168             :   GEN z, dP;
    3169             :   long v;
    3170             : 
    3171          91 :   nf = checknf(nf);
    3172          91 :   P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 1);
    3173          91 :   dP = nfX_disc(nf, P);
    3174          91 :   if (gequal0(dP))
    3175           7 :     pari_err_DOMAIN("rnfdedekind","issquarefree(pol)","=",gen_0,P);
    3176          84 :   if (!pr)
    3177             :   {
    3178          21 :     GEN fa = idealfactor(nf, dP);
    3179          21 :     GEN Q = gel(fa,1), E = gel(fa,2);
    3180          21 :     pari_sp av2 = avma;
    3181          21 :     long i, l = lg(Q);
    3182          21 :     for (i = 1; i < l; i++, set_avma(av2))
    3183             :     {
    3184          21 :       v = itos(gel(E,i));
    3185          21 :       if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
    3186           0 :       set_avma(av2);
    3187             :     }
    3188           0 :     set_avma(av); return gen_1;
    3189             :   }
    3190          63 :   else if (typ(pr) == t_VEC)
    3191             :   { /* flag = 1 is implicit */
    3192          63 :     if (lg(pr) == 1) { set_avma(av); return gen_1; }
    3193          63 :     if (typ(gel(pr,1)) == t_VEC)
    3194             :     { /* list of primes */
    3195          14 :       GEN Q = pr;
    3196          14 :       pari_sp av2 = avma;
    3197          14 :       long i, l = lg(Q);
    3198          14 :       for (i = 1; i < l; i++, set_avma(av2))
    3199             :       {
    3200          14 :         v = nfval(nf, dP, gel(Q,i));
    3201          14 :         if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
    3202             :       }
    3203           0 :       set_avma(av); return gen_1;
    3204             :     }
    3205             :   }
    3206             :   /* single prime */
    3207          49 :   v = nfval(nf, dP, pr);
    3208          49 :   z = rnfdedekind_i(nf, P, pr, v, flag);
    3209          42 :   if (z)
    3210             :   {
    3211          21 :     if (flag) { set_avma(av); return gen_0; }
    3212          14 :     z = gerepilecopy(av, z);
    3213             :   }
    3214             :   else
    3215             :   {
    3216          21 :     set_avma(av); if (flag) return gen_1;
    3217           7 :     z = cgetg(4, t_VEC);
    3218           7 :     gel(z,1) = gen_1;
    3219           7 :     gel(z,2) = triv_order(degpol(P));
    3220           7 :     gel(z,3) = stoi(v);
    3221             :   }
    3222          21 :   return z;
    3223             : }
    3224             : 
    3225             : static int
    3226       23555 : ideal_is1(GEN x) {
    3227       23555 :   switch(typ(x))
    3228             :   {
    3229       10360 :     case t_INT: return is_pm1(x);
    3230       12320 :     case t_MAT: return RgM_isidentity(x);
    3231             :   }
    3232         875 :   return 0;
    3233             : }
    3234             : 
    3235             : /* return a in ideal A such that v_pr(a) = v_pr(A) */
    3236             : static GEN
    3237       13041 : minval(GEN nf, GEN A, GEN pr)
    3238             : {
    3239       13041 :   GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
    3240       13041 :   if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
    3241       13041 :   return a;
    3242             : }
    3243             : 
    3244             : /* nf a true nf. Return NULL if power order is pr-maximal */
    3245             : static GEN
    3246        3920 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
    3247             : {
    3248        3920 :   pari_sp av = avma, av1;
    3249             :   long i, j, k, n, nn, vpol, cnt, sep;
    3250             :   GEN q, q1, p, T, modpr, W, I, p1;
    3251             :   GEN prhinv, mpi, Id;
    3252             : 
    3253        3920 :   if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
    3254        3920 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3255        3920 :   av1 = avma;
    3256        3920 :   p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
    3257        3913 :   if (!p1) return gc_NULL(av);
    3258        2422 :   if (is_pm1(gel(p1,1))) return gerepilecopy(av,gel(p1,2));
    3259        1036 :   sep = itos(gel(p1,3));
    3260        1036 :   W = gmael(p1,2,1);
    3261        1036 :   I = gmael(p1,2,2);
    3262        1036 :   gerepileall(av1, 2, &W, &I);
    3263             : 
    3264        1036 :   mpi = zk_multable(nf, pr_get_gen(pr));
    3265        1036 :   n = degpol(pol); nn = n*n;
    3266        1036 :   vpol = varn(pol);
    3267        1036 :   q1 = q = pr_norm(pr);
    3268        1477 :   while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
    3269        1036 :   Id = matid(n);
    3270        1036 :   prhinv = pr_inv(pr);
    3271        1036 :   av1 = avma;
    3272        1036 :   for(cnt=1;; cnt++)
    3273        3521 :   {
    3274        4557 :     GEN I0 = leafcopy(I), W0 = leafcopy(W);
    3275             :     GEN Wa, Winv, Ip, A, MW, MWmod, F, pseudo, C, G;
    3276        4557 :     GEN Tauinv = cgetg(n+1, t_VEC), Tau = cgetg(n+1, t_VEC);
    3277             : 
    3278        4557 :     if (DEBUGLEVEL>1) err_printf("    pass no %ld\n",cnt);
    3279       27769 :     for (j=1; j<=n; j++)
    3280             :     {
    3281             :       GEN tau, tauinv;
    3282       23212 :       if (ideal_is1(gel(I,j)))
    3283             :       {
    3284       10171 :         gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
    3285       10171 :         continue;
    3286             :       }
    3287       13041 :       gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
    3288       13041 :       gel(Tauinv,j) = tauinv = nfinv(nf, tau);
    3289       13041 :       gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
    3290       13041 :       gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
    3291             :     }
    3292             :     /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
    3293             : 
    3294             :    /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
    3295        4557 :     Wa = RgM_to_RgXV(W,vpol);
    3296        4557 :     Winv = nfM_inv(nf, W);
    3297        4557 :     MW = cgetg(nn+1, t_MAT);
    3298             :     /* W_1 = 1 */
    3299       27769 :     for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
    3300       23212 :     for (i=2; i<=n; i++)
    3301       73591 :       for (j=i; j<=n; j++)
    3302             :       {
    3303       54936 :         GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
    3304       54936 :         if (typ(z) != t_POL)
    3305           0 :           z = nfC_nf_mul(nf, gel(Winv,1), z);
    3306             :         else
    3307             :         {
    3308       54936 :           z = RgX_to_RgC(z, lg(Winv)-1);
    3309       54936 :           z = nfM_nfC_mul(nf, Winv, z);
    3310             :         }
    3311       54936 :         gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
    3312             :       }
    3313             : 
    3314             :     /* compute Ip =  pr-radical [ could use Ker(trace) if q large ] */
    3315        4557 :     MWmod = nfM_to_FqM(MW,nf,modpr);
    3316        4557 :     F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
    3317       23212 :     for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
    3318        4557 :     Ip = FqM_ker(F,T,p);
    3319        4557 :     if (lg(Ip) == 1) { W = W0; I = I0; break; }
    3320             : 
    3321             :     /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
    3322        4291 :     A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
    3323       11501 :     for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
    3324        4291 :     MW = nfM_mul(nf, nfM_inv(nf,A), MW);
    3325        4291 :     C = cgetg(n+1, t_MAT);
    3326       26201 :     for (k=1; k<=n; k++)
    3327             :     {
    3328       21910 :       GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck;
    3329       21910 :       gel(C,k) = Ck = cgetg(nn+1, t_COL);
    3330      147042 :       for (j=1; j<=n; j++)
    3331             :       {
    3332      125132 :         GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
    3333      889518 :         for (i=1; i<=n; i++) gel(Ck, (j-1)*n+i) = nf_to_Fq(nf,gel(z,i),modpr);
    3334             :       }
    3335             :     }
    3336        4291 :     G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
    3337             : 
    3338        4291 :     pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
    3339             :     /* express W in terms of the power basis */
    3340        4291 :     W = nfM_mul(nf, W, gel(pseudo,1));
    3341        4291 :     I = gel(pseudo,2);
    3342             :     /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
    3343             :      * W[i,i] = Tau[i] */
    3344       26201 :     for (j=1; j<=n; j++)
    3345       21910 :       if (gel(Tau,j) != gen_1)
    3346             :       {
    3347       12194 :         gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
    3348       12194 :         gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
    3349             :       }
    3350        4291 :     if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
    3351        4291 :     if (sep <= 3 || gequal(I,I0)) break;
    3352             : 
    3353        3521 :     if (gc_needed(av1,2))
    3354             :     {
    3355           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
    3356           0 :       gerepileall(av1,2, &W,&I);
    3357             :     }
    3358             :   }
    3359        1036 :   return gerepilecopy(av, mkvec2(W, I));
    3360             : }
    3361             : 
    3362             : GEN
    3363      791383 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
    3364             : {
    3365      791383 :   switch(typ(c))
    3366             :   {
    3367      439771 :     case t_INT: case t_FRAC: return c;
    3368       66404 :     case t_POL:
    3369       66404 :       if (lg(c) >= lg(T)) c = RgX_rem(c,T);
    3370       66404 :       break;
    3371      285201 :     case t_POLMOD:
    3372      285201 :       if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
    3373      284900 :       c = gel(c,2);
    3374      284900 :       switch(typ(c))
    3375             :       {
    3376      249914 :         case t_POL: break;
    3377       34986 :         case t_INT: case t_FRAC: return c;
    3378           0 :         default: pari_err_TYPE(f, c);
    3379             :       }
    3380      249914 :       break;
    3381           7 :     default: pari_err_TYPE(f,c);
    3382             :   }
    3383             :   /* typ(c) = t_POL */
    3384      316318 :   if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
    3385      316311 :   switch(lg(c))
    3386             :   {
    3387       13244 :     case 2: return gen_0;
    3388       26519 :     case 3:
    3389       26519 :       c = gel(c,2); if (is_rational_t(typ(c))) return c;
    3390           0 :       pari_err_TYPE(f,c);
    3391             :   }
    3392      276548 :   RgX_check_QX(c, f);
    3393      276534 :   return lift? c: mkpolmod(c, T);
    3394             : }
    3395             : /* check whether P is a polynomials with coeffs in number field Q[y]/(T) */
    3396             : GEN
    3397      265193 : RgX_nffix(const char *f, GEN T, GEN P, int lift)
    3398             : {
    3399      265193 :   long i, l, vT = varn(T);
    3400      265193 :   GEN Q = cgetg_copy(P, &l);
    3401      265193 :   if (typ(P) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), P);
    3402      265193 :   if (varncmp(varn(P), vT) >= 0) pari_err_PRIORITY(f, P, ">=", vT);
    3403      265179 :   Q[1] = P[1];
    3404     1016508 :   for (i=2; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3405      265172 :   return normalizepol_lg(Q, l);
    3406             : }
    3407             : GEN
    3408          28 : RgV_nffix(const char *f, GEN T, GEN P, int lift)
    3409             : {
    3410             :   long i, l;
    3411          28 :   GEN Q = cgetg_copy(P, &l);
    3412          77 :   for (i=1; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3413          21 :   return Q;
    3414             : }
    3415             : 
    3416             : static GEN
    3417        2002 : get_d(GEN nf, GEN d)
    3418             : {
    3419        2002 :   GEN b = idealredmodpower(nf, d, 2, 100000);
    3420        2002 :   return nfmul(nf, d, nfsqr(nf,b));
    3421             : }
    3422             : 
    3423             : /* true nf */
    3424             : static GEN
    3425        3213 : pr_factorback(GEN nf, GEN fa)
    3426             : {
    3427        3213 :   GEN P = gel(fa,1), E = gel(fa,2), z = gen_1;
    3428        3213 :   long i, l = lg(P);
    3429        7497 :   for (i = 1; i < l; i++) z = idealmulpowprime(nf, z, gel(P,i), gel(E,i));
    3430        3213 :   return z;
    3431             : }
    3432             : /* true nf */
    3433             : static GEN
    3434        3213 : pr_factorback_scal(GEN nf, GEN fa)
    3435             : {
    3436        3213 :   GEN D = pr_factorback(nf,fa);
    3437        3213 :   if (typ(D) == t_MAT && RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
    3438        3213 :   return D;
    3439             : }
    3440             : 
    3441             : /* nf = base field K
    3442             :  * pol= monic polynomial in Z_K[X] defining a relative extension L = K[X]/(pol).
    3443             :  * Returns a pseudo-basis [A,I] of Z_L, set *pD to [D,d] and *pf to the
    3444             :  * index-ideal; rnf is used when lim != 0 and may be NULL */
    3445             : GEN
    3446        1946 : rnfallbase(GEN nf, GEN pol, GEN lim, GEN rnf, GEN *pD, GEN *pf, GEN *pDKP)
    3447             : {
    3448             :   long i, j, jf, l;
    3449             :   GEN fa, E, P, Ef, Pf, z, disc;
    3450             : 
    3451        1946 :   nf = checknf(nf); pol = liftpol_shallow(pol);
    3452        1946 :   if (!gequal1(leading_coeff(pol)))
    3453           7 :     pari_err_IMPL("nonmonic relative polynomials in rnfallbase");
    3454        1939 :   disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
    3455        1939 :   if (gequal0(disc))
    3456           7 :     pari_err_DOMAIN("rnfpseudobasis","issquarefree(pol)","=",gen_0, pol);
    3457        1932 :   if (lim)
    3458             :   {
    3459             :     GEN rnfeq, zknf, dzknf, U, vU, dA, A, MB, dB, BdB, vj, B, Tabs;
    3460         329 :     GEN D = idealhnf_shallow(nf, disc);
    3461         329 :     long rU, m = nf_get_degree(nf), n = degpol(pol), N = n*m;
    3462             :     nfmaxord_t S;
    3463             : 
    3464         329 :     if (typ(lim) == t_INT)
    3465          35 :       P = ZV_union_shallow(nf_get_ramified_primes(nf),
    3466          35 :                            gel(Z_factor_limit(gcoeff(D,1,1), itou(lim)), 1));
    3467             :     else
    3468             :     {
    3469         294 :       P = cgetg_copy(lim, &l);
    3470         882 :       for (i = 1; i < l; i++)
    3471             :       {
    3472         588 :         GEN p = gel(lim,i);
    3473         588 :         if (typ(p) != t_INT) p = pr_get_p(p);
    3474         588 :         gel(P,i) = p;
    3475             :       }
    3476         294 :       P = ZV_sort_uniq(P);
    3477             :     }
    3478         329 :     if (rnf)
    3479             :     {
    3480         280 :       rnfeq = rnf_get_map(rnf);
    3481         280 :       zknf = rnf_get_nfzk(rnf);
    3482             :     }
    3483             :     else
    3484             :     {
    3485          49 :       rnfeq = nf_rnfeq(nf, pol);
    3486          49 :       zknf = nf_nfzk(nf, rnfeq);
    3487             :     }
    3488         329 :     dzknf = gel(zknf,1);
    3489         329 :     if (gequal1(dzknf)) dzknf = NULL;
    3490         329 :     Tabs = gel(rnfeq,1);
    3491         329 :     nfmaxord(&S, mkvec2(Tabs,P), 0);
    3492         329 :     B = RgXV_unscale(S.basis, S.unscale);
    3493         329 :     BdB = Q_remove_denom(B, &dB);
    3494         329 :     MB = RgXV_to_RgM(BdB, N); /* HNF */
    3495             : 
    3496         329 :     vU = cgetg(N+1, t_VEC);
    3497         329 :     vj = cgetg(N+1, t_VECSMALL);
    3498         329 :     gel(vU,1) = U = cgetg(m+1, t_MAT);
    3499         329 :     gel(U,1) = col_ei(N, 1);
    3500         329 :     A = dB? (dzknf? gdiv(dB,dzknf): dB): NULL;
    3501         329 :     if (A && gequal1(A)) A = NULL;
    3502         679 :     for (j = 2; j <= m; j++)
    3503             :     {
    3504         350 :       GEN t = gel(zknf,j);
    3505         350 :       if (A) t = ZX_Z_mul(t, A);
    3506         350 :       gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
    3507             :     }
    3508        2163 :     for (i = 2; i <= N; i++)
    3509             :     {
    3510        1834 :       GEN b = gel(BdB,i);
    3511        1834 :       gel(vU,i) = U = cgetg(m+1, t_MAT);
    3512        1834 :       gel(U,1) = hnf_solve(MB, RgX_to_RgC(b, N));
    3513        4004 :       for (j = 2; j <= m; j++)
    3514             :       {
    3515        2170 :         GEN t = ZX_rem(ZX_mul(b, gel(zknf,j)), Tabs);
    3516        2170 :         if (dzknf) t = gdiv(t, dzknf);
    3517        2170 :         gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
    3518             :       }
    3519             :     }
    3520         329 :     vj[1] = 1; U = gel(vU,1); rU = m;
    3521         812 :     for (i = j = 2; i <= N; i++)
    3522             :     {
    3523         812 :       GEN V = shallowconcat(U, gel(vU,i));
    3524         812 :       if (ZM_rank(V) != rU)
    3525             :       {
    3526         812 :         U = V; rU += m; vj[j++] = i;
    3527         812 :         if (rU == N) break;
    3528             :       }
    3529             :     }
    3530         329 :     if (dB) for(;;)
    3531         371 :     {
    3532         693 :       GEN c = gen_1, H = ZM_hnfmodid(U, dB);
    3533         693 :       long ic = 0;
    3534        5964 :       for (i = 1; i <= N; i++)
    3535        5271 :         if (cmpii(gcoeff(H,i,i), c) > 0) { c = gcoeff(H,i,i); ic = i; }
    3536         693 :       if (!ic) break;
    3537         371 :       vj[j++] = ic;
    3538         371 :       U = shallowconcat(H, gel(vU, ic));
    3539             :     }
    3540         329 :     setlg(vj, j);
    3541         329 :     B = vecpermute(B, vj);
    3542             : 
    3543         329 :     l = lg(B);
    3544         329 :     A = cgetg(l,t_MAT);
    3545        1841 :     for (j = 1; j < l; j++)
    3546             :     {
    3547        1512 :       GEN t = eltabstorel_lift(rnfeq, gel(B,j));
    3548        1512 :       gel(A,j) = Rg_to_RgC(t, n);
    3549             :     }
    3550         329 :     A = RgM_to_nfM(nf, A);
    3551         329 :     A = Q_remove_denom(A, &dA);
    3552         329 :     if (!dA)
    3553             :     { /* order is maximal */
    3554          14 :       z = triv_order(n);
    3555          14 :       if (pf) *pf = gen_1;
    3556             :     }
    3557             :     else
    3558             :     {
    3559             :       GEN fi;
    3560             :       /* the first n columns of A are probably in HNF already */
    3561         315 :       A = shallowconcat(vecslice(A,n+1,lg(A)-1), vecslice(A,1,n));
    3562         315 :       A = mkvec2(A, const_vec(l-1,gen_1));
    3563         315 :       if (DEBUGLEVEL > 2) err_printf("rnfallbase: nfhnf in dim %ld\n", l-1);
    3564         315 :       z = nfhnfmod(nf, A, nfdetint(nf,A));
    3565         315 :       gel(z,2) = gdiv(gel(z,2), dA);
    3566         315 :       fi = idealprod(nf,gel(z,2));
    3567         315 :       D = idealmul(nf, D, idealsqr(nf, fi));
    3568         315 :       if (pf) *pf = idealinv(nf, fi);
    3569             :     }
    3570         329 :     if (RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
    3571         329 :     if (pDKP) { settyp(S.dKP, t_VEC); *pDKP = S.dKP; }
    3572         329 :     *pD = mkvec2(D, get_d(nf, disc)); return z;
    3573             :   }
    3574        1603 :   fa = idealfactor(nf, disc);
    3575        1603 :   P = gel(fa,1); l = lg(P); z = NULL;
    3576        1603 :   E = gel(fa,2);
    3577        1603 :   Pf = cgetg(l, t_COL);
    3578        1603 :   Ef = cgetg(l, t_COL);
    3579        5271 :   for (i = j = jf = 1; i < l; i++)
    3580             :   {
    3581        3675 :     GEN pr = gel(P,i);
    3582        3675 :     long e = itos(gel(E,i));
    3583        3675 :     if (e > 1)
    3584             :     {
    3585        2793 :       GEN vD = rnfmaxord(nf, pol, pr, e);
    3586        2786 :       if (vD)
    3587             :       {
    3588        1841 :         long ef = idealprodval(nf, gel(vD,2), pr);
    3589        1841 :         z = rnfjoinmodules(nf, z, vD);
    3590        1841 :         if (ef) { gel(Pf, jf) = pr; gel(Ef, jf++) = stoi(-ef); }
    3591        1841 :         e += 2 * ef;
    3592             :       }
    3593             :     }
    3594        3668 :     if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
    3595             :   }
    3596        1596 :   setlg(P,j);
    3597        1596 :   setlg(E,j);
    3598        1596 :   if (pDKP) *pDKP = prV_primes(P);
    3599        1596 :   if (pf)
    3600             :   {
    3601        1540 :     setlg(Pf, jf);
    3602        1540 :     setlg(Ef, jf); *pf = pr_factorback_scal(nf, mkmat2(Pf,Ef));
    3603             :   }
    3604        1596 :   *pD = mkvec2(pr_factorback_scal(nf,fa), get_d(nf, disc));
    3605        1596 :   return z? z: triv_order(degpol(pol));
    3606             : }
    3607             : 
    3608             : static GEN
    3609        1393 : RgX_to_algX(GEN nf, GEN x)
    3610             : {
    3611             :   long i, l;
    3612        1393 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
    3613        7427 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_alg(nf, gel(x,i));
    3614        1393 :   return y;
    3615             : }
    3616             : 
    3617             : GEN
    3618        1407 : nfX_to_monic(GEN nf, GEN T, GEN *pL)
    3619             : {
    3620             :   GEN lT, g, a;
    3621        1407 :   long i, l = lg(T);
    3622        1407 :   if (l == 2) return pol_0(varn(T));
    3623        1407 :   if (l == 3) return pol_1(varn(T));
    3624        1407 :   nf = checknf(nf);
    3625        1407 :   T = Q_primpart(RgX_to_nfX(nf, T));
    3626        1407 :   lT = leading_coeff(T); if (pL) *pL = lT;
    3627        1407 :   if (isint1(T)) return T;
    3628        1407 :   g = cgetg_copy(T, &l); g[1] = T[1]; a = lT;
    3629        1407 :   gel(g, l-1) = gen_1;
    3630        1407 :   gel(g, l-2) = gel(T,l-2);
    3631        1407 :   if (l == 4) { gel(g,l-2) = nf_to_scalar_or_alg(nf, gel(g,l-2)); return g; }
    3632        1393 :   if (typ(lT) == t_INT)
    3633             :   {
    3634        1379 :     gel(g, l-3) = gmul(a, gel(T,l-3));
    3635        3227 :     for (i = l-4; i > 1; i--) { a = mulii(a,lT); gel(g,i) = gmul(a, gel(T,i)); }
    3636             :   }
    3637             :   else
    3638             :   {
    3639          14 :     gel(g, l-3) = nfmul(nf, a, gel(T,l-3));
    3640          35 :     for (i = l-3; i > 1; i--)
    3641             :     {
    3642          21 :       a = nfmul(nf,a,lT);
    3643          21 :       gel(g,i) = nfmul(nf, a, gel(T,i));
    3644             :     }
    3645             :   }
    3646        1393 :   return RgX_to_algX(nf, g);
    3647             : }
    3648             : 
    3649             : GEN
    3650         735 : rnfdisc_factored(GEN nf, GEN pol, GEN *pd)
    3651             : {
    3652             :   long i, j, l;
    3653             :   GEN fa, E, P, disc, lim;
    3654             : 
    3655         735 :   pol = rnfdisc_get_T(nf, pol, &lim);
    3656         735 :   disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
    3657         735 :   if (gequal0(disc))
    3658           0 :     pari_err_DOMAIN("rnfdisc","issquarefree(pol)","=",gen_0, pol);
    3659         735 :   pol = nfX_to_monic(nf, pol, NULL);
    3660         735 :   fa = idealfactor_partial(nf, disc, lim);
    3661         735 :   P = gel(fa,1); l = lg(P);
    3662         735 :   E = gel(fa,2);
    3663        2065 :   for (i = j = 1; i < l; i++)
    3664             :   {
    3665        1330 :     long e = itos(gel(E,i));
    3666        1330 :     GEN pr = gel(P,i);
    3667        1330 :     if (e > 1)
    3668             :     {
    3669        1127 :       GEN vD = rnfmaxord(nf, pol, pr, e);
    3670        1127 :       if (vD) e += 2*idealprodval(nf, gel(vD,2), pr);
    3671             :     }
    3672        1330 :     if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
    3673             :   }
    3674         735 :   if (pd) *pd = get_d(nf, disc);
    3675         735 :   setlg(P, j);
    3676         735 :   setlg(E, j); return fa;
    3677             : }
    3678             : GEN
    3679          77 : rnfdiscf(GEN nf, GEN pol)
    3680             : {
    3681          77 :   pari_sp av = avma;
    3682             :   GEN d, fa;
    3683          77 :   nf = checknf(nf); fa = rnfdisc_factored(nf, pol, &d);
    3684          77 :   return gerepilecopy(av, mkvec2(pr_factorback_scal(nf,fa), d));
    3685             : }
    3686             : 
    3687             : GEN
    3688          35 : gen_if_principal(GEN bnf, GEN x)
    3689             : {
    3690          35 :   pari_sp av = avma;
    3691          35 :   GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
    3692          35 :   return isintzero(z)? gc_NULL(av): z;
    3693             : }
    3694             : 
    3695             : /* given bnf and a HNF pseudo-basis of a proj. module, simplify the HNF as
    3696             :  * much as possible. The resulting matrix will be upper triangular but the
    3697             :  * diagonal coefficients will not be equal to 1. The ideals are integral and
    3698             :  * primitive. */
    3699             : GEN
    3700           0 : rnfsimplifybasis(GEN bnf, GEN M)
    3701             : {
    3702           0 :   pari_sp av = avma;
    3703             :   long i, l;
    3704             :   GEN y, Az, Iz, nf, A, I;
    3705             : 
    3706           0 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3707           0 :   if (!check_ZKmodule_i(M)) pari_err_TYPE("rnfsimplifybasis",M);
    3708           0 :   A = gel(M,1);
    3709           0 :   I = gel(M,2); l = lg(I);
    3710           0 :   Az = cgetg(l, t_MAT);
    3711           0 :   Iz = cgetg(l, t_VEC); y = mkvec2(Az, Iz);
    3712           0 :   for (i = 1; i < l; i++)
    3713             :   {
    3714             :     GEN c, d;
    3715           0 :     if (ideal_is1(gel(I,i)))
    3716             :     {
    3717           0 :       gel(Iz,i) = gen_1;
    3718           0 :       gel(Az,i) = gel(A,i); continue;
    3719             :     }
    3720             : 
    3721           0 :     gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
    3722           0 :     gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
    3723           0 :     if (c && ideal_is1(gel(Iz,i))) continue;
    3724             : 
    3725           0 :     d = gen_if_principal(bnf, gel(Iz,i));
    3726           0 :     if (d)
    3727             :     {
    3728           0 :       gel(Iz,i) = gen_1;
    3729           0 :       gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
    3730             :     }
    3731             :   }
    3732           0 :   return gerepilecopy(av, y);
    3733             : }
    3734             : 
    3735             : static GEN
    3736          63 : get_module(GEN nf, GEN O, const char *s)
    3737             : {
    3738          63 :   if (typ(O) == t_POL) return rnfpseudobasis(nf, O);
    3739          56 :   if (!check_ZKmodule_i(O)) pari_err_TYPE(s, O);
    3740          56 :   return shallowcopy(O);
    3741             : }
    3742             : 
    3743             : GEN
    3744          14 : rnfdet(GEN nf, GEN M)
    3745             : {
    3746          14 :   pari_sp av = avma;
    3747             :   GEN D;
    3748          14 :   nf = checknf(nf);
    3749          14 :   M = get_module(nf, M, "rnfdet");
    3750          14 :   D = idealmul(nf, nfM_det(nf, gel(M,1)), idealprod(nf, gel(M,2)));
    3751          14 :   return gerepileupto(av, D);
    3752             : }
    3753             : 
    3754             : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
    3755             :    t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
    3756             : static void
    3757          63 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
    3758             : {
    3759             :   GEN x, uv, y, da, db;
    3760             : 
    3761          63 :   a = idealinv(nf,a);
    3762          63 :   a = Q_remove_denom(a, &da);
    3763          63 :   b = Q_remove_denom(b, &db);
    3764          63 :   x = idealcoprime(nf,a,b);
    3765          63 :   uv = idealaddtoone(nf, idealmul(nf,x,a), b);
    3766          63 :   y = gel(uv,2);
    3767          63 :   if (da) x = gmul(x,da);
    3768          63 :   if (db) y = gdiv(y,db);
    3769          63 :   *px = x;
    3770          63 :   *py = y;
    3771          63 :   *pz = db ? negi(db): gen_m1;
    3772          63 :   *pt = nfdiv(nf, gel(uv,1), x);
    3773          63 : }
    3774             : 
    3775             : /* given a pseudo-basis of a proj. module in HNF [A,I] (or [A,I,D,d]), gives
    3776             :  * an n x n matrix (not HNF) of a pseudo-basis and an ideal vector
    3777             :  * [1,...,1,I] such that M ~ Z_K^(n-1) x I. Uses the approximation theorem.*/
    3778             : GEN
    3779          28 : rnfsteinitz(GEN nf, GEN M)
    3780             : {
    3781          28 :   pari_sp av = avma;
    3782             :   long i, n;
    3783             :   GEN A, I;
    3784             : 
    3785          28 :   nf = checknf(nf);
    3786          28 :   M = get_module(nf, M, "rnfsteinitz");
    3787          28 :   A = RgM_to_nfM(nf, gel(M,1));
    3788          28 :   I = leafcopy(gel(M,2)); n = lg(A)-1;
    3789         189 :   for (i = 1; i < n; i++)
    3790             :   {
    3791         161 :     GEN c1, c2, b, a = gel(I,i);
    3792         161 :     gel(I,i) = gen_1;
    3793         161 :     if (ideal_is1(a)) continue;
    3794             : 
    3795          63 :     c1 = gel(A,i);
    3796          63 :     c2 = gel(A,i+1);
    3797          63 :     b = gel(I,i+1);
    3798          63 :     if (ideal_is1(b))
    3799             :     {
    3800           0 :       gel(A,i) = c2;
    3801           0 :       gel(A,i+1) = gneg(c1);
    3802           0 :       gel(I,i+1) = a;
    3803             :     }
    3804             :     else
    3805             :     {
    3806          63 :       pari_sp av2 = avma;
    3807             :       GEN x, y, z, t, c;
    3808          63 :       nfidealdet1(nf,a,b, &x,&y,&z,&t);
    3809          63 :       x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
    3810          63 :       y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
    3811          63 :       gerepileall(av2, 2, &x,&y);
    3812          63 :       gel(A,i) = x;
    3813          63 :       gel(A,i+1) = y;
    3814          63 :       gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &c);
    3815          63 :       if (c) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), c);
    3816             :     }
    3817             :   }
    3818          28 :   gel(M,1) = A;
    3819          28 :   gel(M,2) = I; return gerepilecopy(av, M);
    3820             : }
    3821             : 
    3822             : /* Given bnf and a proj. module (or a t_POL -> rnfpseudobasis), and outputs a
    3823             :  * basis if it is free, an n+1-generating set if it is not */
    3824             : GEN
    3825          21 : rnfbasis(GEN bnf, GEN M)
    3826             : {
    3827          21 :   pari_sp av = avma;
    3828             :   long j, n;
    3829             :   GEN nf, A, I, cl, col, a;
    3830             : 
    3831          21 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3832          21 :   M = get_module(nf, M, "rnfbasis");
    3833          21 :   I = gel(M,2); n = lg(I)-1;
    3834          98 :   j = 1; while (j < n && ideal_is1(gel(I,j))) j++;
    3835          21 :   if (j < n) { M = rnfsteinitz(nf,M); I = gel(M,2); }
    3836          21 :   A = gel(M,1);
    3837          21 :   col= gel(A,n); A = vecslice(A, 1, n-1);
    3838          21 :   cl = gel(I,n);
    3839          21 :   a = gen_if_principal(bnf, cl);
    3840          21 :   if (!a)
    3841             :   {
    3842           7 :     GEN v = idealtwoelt(nf, cl);
    3843           7 :     A = vec_append(A, gmul(gel(v,1), col));
    3844           7 :     a = gel(v,2);
    3845             :   }
    3846          21 :   A = vec_append(A, nfC_nf_mul(nf, col, a));
    3847          21 :   return gerepilecopy(av, A);
    3848             : }
    3849             : 
    3850             : /* Given a Z_K-module M (or a polynomial => rnfpseudobasis) outputs a
    3851             :  * Z_K-basis in HNF if it exists, zero if not */
    3852             : GEN
    3853           7 : rnfhnfbasis(GEN bnf, GEN M)
    3854             : {
    3855           7 :   pari_sp av = avma;
    3856             :   long j, l;
    3857             :   GEN nf, A, I, a;
    3858             : 
    3859           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3860           7 :   if (typ(M) == t_POL) M = rnfpseudobasis(nf, M);
    3861             :   else
    3862             :   {
    3863           7 :     if (typ(M) != t_VEC) pari_err_TYPE("rnfhnfbasis", M);
    3864           7 :     if (lg(M) == 5) M = mkvec2(gel(M,1), gel(M,2));
    3865           7 :     M = nfhnf(nf, M); /* in case M is not in HNF */
    3866             :   }
    3867           7 :   A = shallowcopy(gel(M,1));
    3868           7 :   I = gel(M,2); l = lg(A);
    3869          42 :   for (j = 1; j < l; j++)
    3870             :   {
    3871          35 :     if (ideal_is1(gel(I,j))) continue;
    3872          14 :     a = gen_if_principal(bnf, gel(I,j));
    3873          14 :     if (!a) return gc_const(av, gen_0);
    3874          14 :     gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
    3875             :   }
    3876           7 :   return gerepilecopy(av,A);
    3877             : }
    3878             : 
    3879             : long
    3880           7 : rnfisfree(GEN bnf, GEN M)
    3881             : {
    3882           7 :   pari_sp av = avma;
    3883             :   GEN nf, P, I;
    3884             :   long l, j;
    3885             : 
    3886           7 :   bnf = checkbnf(bnf);
    3887           7 :   if (is_pm1( bnf_get_no(bnf) )) return 1;
    3888           0 :   nf = bnf_get_nf(bnf);
    3889           0 :   M = get_module(nf, M, "rnfisfree");
    3890           0 :   I = gel(M,2); l = lg(I); P = NULL;
    3891           0 :   for (j = 1; j < l; j++)
    3892           0 :     if (!ideal_is1(gel(I,j))) P = P? idealmul(nf, P, gel(I,j)): gel(I,j);
    3893           0 :   return gc_long(av, P? gequal0( isprincipal(bnf,P) ): 1);
    3894             : }
    3895             : 
    3896             : /**********************************************************************/
    3897             : /**                                                                  **/
    3898             : /**                   COMPOSITUM OF TWO NUMBER FIELDS                **/
    3899             : /**                                                                  **/
    3900             : /**********************************************************************/
    3901             : static GEN
    3902       26366 : compositum_fix(GEN nf, GEN A)
    3903             : {
    3904             :   int ok;
    3905       26366 :   if (nf)
    3906             :   {
    3907         854 :     A = Q_primpart(liftpol_shallow(A)); RgX_check_ZXX(A,"polcompositum");
    3908         854 :     ok = nfissquarefree(nf,A);
    3909             :   }
    3910             :   else
    3911             :   {
    3912       25512 :     A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
    3913       25505 :     ok = ZX_is_squarefree(A);
    3914             :   }
    3915       26367 :   if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
    3916       26360 :   return A;
    3917             : }
    3918             : #define next_lambda(a) (a>0 ? -a : 1-a)
    3919             : 
    3920             : static long
    3921         434 : nfcompositum_lambda(GEN nf, GEN A, GEN B, long lambda)
    3922             : {
    3923         434 :   pari_sp av = avma;
    3924             :   forprime_t S;
    3925         434 :   GEN T = nf_get_pol(nf);
    3926         434 :   long vT = varn(T);
    3927             :   ulong p;
    3928         434 :   init_modular_big(&S);
    3929         434 :   p = u_forprime_next(&S);
    3930             :   while (1)
    3931          14 :   {
    3932             :     GEN Hp, Tp, a;
    3933         448 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    3934         448 :     a = ZXX_to_FlxX(RgX_rescale(A, stoi(-lambda)), p, vT);
    3935         448 :     Tp = ZX_to_Flx(T, p);
    3936         448 :     Hp = FlxqX_direct_compositum(a, ZXX_to_FlxX(B, p, vT), Tp, p);
    3937         448 :     if (!FlxqX_is_squarefree(Hp, Tp, p))
    3938          14 :       { lambda = next_lambda(lambda); continue; }
    3939         434 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    3940         434 :     return gc_long(av, lambda);
    3941             :   }
    3942             : }
    3943             : 
    3944             : /* modular version */
    3945             : GEN
    3946       13284 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
    3947             : {
    3948       13284 :   pari_sp av = avma;
    3949             :   int same;
    3950             :   long v, k;
    3951             :   GEN C, D, LPRS;
    3952             : 
    3953       13284 :   if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
    3954       13284 :   if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
    3955       13284 :   if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
    3956       13284 :   v = varn(A);
    3957       13284 :   if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
    3958       13284 :   if (nf)
    3959             :   {
    3960         476 :     nf = checknf(nf);
    3961         469 :     if (varncmp(v,nf_get_varn(nf))>=0) pari_err_PRIORITY("polcompositum", nf, ">=",  v);
    3962             :   }
    3963       13242 :   same = (A == B || RgX_equal(A,B));
    3964       13242 :   A = compositum_fix(nf,A);
    3965       13231 :   B = same ? A: compositum_fix(nf,B);
    3966             : 
    3967       13234 :   D = LPRS = NULL; /* -Wall */
    3968       13234 :   k = same? -1: 1;
    3969       13234 :   if (nf)
    3970             :   {
    3971         434 :     long v0 = fetch_var();
    3972         434 :     GEN q, T = nf_get_pol(nf);
    3973         434 :     A = liftpol_shallow(A);
    3974         434 :     B = liftpol_shallow(B);
    3975         434 :     k = nfcompositum_lambda(nf, A, B, k);
    3976         434 :     if (flag&1)
    3977             :     {
    3978             :       GEN H0, H1;
    3979         182 :       GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
    3980         182 :       GEN B1 = poleval(QXQX_to_mod_shallow(B, T), chgvar);
    3981         182 :       C = RgX_resultant_all(QXQX_to_mod_shallow(A, T), B1, &q);
    3982         182 :       C = gsubst(C,v0,pol_x(v));
    3983         182 :       C = lift_if_rational(C);
    3984         182 :       H0 = gsubst(gel(q,2),v0,pol_x(v));
    3985         182 :       H1 = gsubst(gel(q,3),v0,pol_x(v));
    3986         182 :       if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
    3987         182 :       if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
    3988         182 :       H0 = lift_if_rational(H0);
    3989         182 :       H1 = lift_if_rational(H1);
    3990         182 :       LPRS = mkvec2(H0,H1);
    3991             :     }
    3992             :     else
    3993             :     {
    3994         252 :       C = nf_direct_compositum(nf, RgX_rescale(A,stoi(-k)), B);
    3995         252 :       setvarn(C, v); C = QXQX_to_mod_shallow(C, T);
    3996             :     }
    3997             :   }
    3998             :   else
    3999             :   {
    4000       12800 :     B = leafcopy(B); setvarn(B,fetch_var_higher());
    4001        3080 :     C = (flag&1)? ZX_ZXY_resultant_all(A, B, &k, &LPRS)
    4002       12800 :                 : ZX_compositum(A, B, &k);
    4003       12800 :     setvarn(C, v);
    4004             :   }
    4005             :   /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
    4006       13234 :   if (flag & 2)
    4007       10217 :     C = mkvec(C);
    4008             :   else
    4009             :   {
    4010        3017 :     if (same)
    4011             :     {
    4012          91 :       D = RgX_rescale(A, stoi(1 - k));
    4013          91 :       if (nf) D = QXQX_to_mod_shallow(D, nf_get_pol(nf));
    4014          91 :       C = RgX_div(C, D);
    4015          91 :       if (degpol(C) <= 0)
    4016           0 :         C = mkvec(D);
    4017             :       else
    4018          91 :         C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
    4019             :     }
    4020             :     else
    4021        2926 :       C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
    4022             :   }
    4023       13233 :   gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
    4024       13232 :   if (flag&1)
    4025             :   { /* a,b,c root of A,B,C = compositum, c = b - k a */
    4026        3262 :     long i, l = lg(C);
    4027        3262 :     GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
    4028        3262 :     setvarn(mH0,v);
    4029        3262 :     setvarn(H1,v);
    4030        6601 :     for (i=1; i<l; i++)
    4031             :     {
    4032        3339 :       GEN D = gel(C,i);
    4033        3339 :       a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
    4034        3339 :       b = gadd(pol_x(v), gmulsg(k,a));
    4035        3339 :       if (degpol(D) == 1) b = RgX_rem(b,D);
    4036        3339 :       gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
    4037             :     }
    4038             :   }
    4039       13232 :   (void)delete_var();
    4040       13232 :   settyp(C, t_VEC);
    4041       13232 :   if (flag&2) C = gel(C,1);
    4042       13232 :   return gerepilecopy(av, C);
    4043             : }
    4044             : GEN
    4045       12809 : polcompositum0(GEN A, GEN B, long flag)
    4046       12809 : { return nfcompositum(NULL,A,B,flag); }
    4047             : 
    4048             : GEN
    4049          91 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
    4050             : GEN
    4051        2828 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }

Generated by: LCOV version 1.13