Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24782-f7724578b4) Lines: 2139 2259 94.7 %
Date: 2019-12-06 05:56:26 Functions: 167 171 97.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13