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 24988-2584e74448) Lines: 2147 2268 94.7 %
Date: 2020-01-26 05:57:03 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        2191 : safe_Z_pvalrem(GEN x, GEN p, GEN *z)
      24             : {
      25        2191 :   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        2163 :   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        2793 :   for (i=1; i<l; i++)
      41             :   {
      42        2191 :     GEN p = gel(P,i);
      43             :     long k;
      44        2191 :     if (flag && !equalim1(p))
      45             :     {
      46          14 :       p = gcdii(p, D);
      47          14 :       if (is_pm1(p)) continue;
      48             :     }
      49        2191 :     k = safe_Z_pvalrem(D, p, &D);
      50        2184 :     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         602 : update_fact(GEN d, GEN f)
      68             : {
      69             :   GEN P;
      70         602 :   switch (typ(f))
      71             :   {
      72         588 :     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       22470 : set_disc(nfmaxord_t *S)
      87             : {
      88             :   GEN l0, L, dT;
      89             :   long d;
      90       22470 :   if (S->T0 == S->T) return ZX_disc(S->T);
      91        7056 :   d = degpol(S->T0);
      92        7056 :   l0 = leading_coeff(S->T0);
      93        7056 :   L = S->unscale;
      94        7056 :   if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
      95         812 :     dT = ZX_disc(S->T); /* more efficient */
      96             :   else
      97             :   {
      98        6244 :     GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
      99        6244 :     dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
     100             :   }
     101        7056 :   return S->dT = dT;
     102             : }
     103             : 
     104             : /* dT != 0 */
     105             : static GEN
     106       16961 : poldiscfactors_i(GEN T, GEN dT, long flag)
     107             : {
     108       16961 :   GEN fa = absZ_factor_limit(dT, minuu(tridiv_bound(dT), maxprime()));
     109       16961 :   GEN Z, E, P = gel(fa,1), Tp = NULL;
     110       16961 :   long i, first = 1, l = lg(P);
     111       16961 :   if (l == 1) return fa;
     112       15561 :   setlg(P, l-1); Z = mkcol(gel(P, l-1));
     113       46851 :   while (lg(Z) != 1)
     114             :   { /* handle last element of Z */
     115       15729 :     GEN p = gel(Z, lg(Z)-1), r;
     116       15729 :     setlg(Z, lg(Z)-1);
     117       15729 :     if (!first) (void)Z_isanypower(p, &p);
     118       15729 :     first = 0;
     119       15729 :     if ((flag || lgefint(p)==3) && BPSW_psp(p))
     120       15502 :     { P = vec_append(P, p); continue; }
     121         227 :     if (!Tp) Tp = ZX_deriv(T);
     122         227 :     r = FpX_gcd_check(T, Tp, p);
     123         227 :     if (r)
     124          84 :       Z = shallowconcat(Z, Z_cba(r, diviiexact(p,r)));
     125         143 :     else if (flag)
     126          56 :       P = shallowconcat(P, gel(Z_factor(p),1));
     127             :     else
     128          87 :       P = vec_append(P, p);
     129             :   }
     130       15561 :   ZV_sort_inplace(P); l = lg(P); E = cgetg(l, t_COL);
     131       46683 :   for (i = 1; i < l; i++)
     132       31122 :     gel(E,i) = utoipos(Z_pvalrem(dT, gel(P,i), &dT));
     133       15561 :   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       22470 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
     150             : {
     151       22470 :   GEN dT, L, E, P, fa = NULL;
     152             :   pari_timer t;
     153       22470 :   long l, ty = typ(T);
     154             : 
     155       22470 :   if (DEBUGLEVEL) timer_start(&t);
     156       22470 :   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       22470 :   if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
     161       22470 :   T = Q_primpart(T);
     162       22470 :   if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
     163       22470 :   RgX_check_ZX(T, "nfmaxord");
     164       22470 :   S->T0 = T;
     165       22470 :   T = ZX_Q_normalize(T, &L);
     166       22470 :   S->unscale = L;
     167       22470 :   S->T = T;
     168       22470 :   S->dT = dT = set_disc(S);
     169       22470 :   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       16926 :     fa = poldiscfactors_i(T, dT, !(flag & nf_PARTIALFACT));
     199       22442 :   P = gel(fa,1); l = lg(P);
     200       22442 :   E = gel(fa,2);
     201       22442 :   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       22442 :   S->dTP = P;
     208       22442 :   S->dTE = vec_to_vecsmall(E);
     209       22442 :   if (DEBUGLEVEL>2) timer_printf(&t, "disc. factorisation");
     210       22442 : }
     211             : 
     212             : static int
     213       66816 : fnz(GEN x,long j)
     214             : {
     215             :   long i;
     216      319562 :   for (i=1; i<j; i++)
     217      260588 :     if (signe(gel(x,i))) return 0;
     218       58974 :   return 1;
     219             : }
     220             : /* return list u[i], 2 by 2 coprime with the same prime divisors as ab */
     221             : static GEN
     222          91 : get_coprimes(GEN a, GEN b)
     223             : {
     224          91 :   long i, k = 1;
     225          91 :   GEN u = cgetg(3, t_COL);
     226          91 :   gel(u,1) = a;
     227          91 :   gel(u,2) = b;
     228             :   /* u1,..., uk 2 by 2 coprime */
     229         441 :   while (k+1 < lg(u))
     230             :   {
     231         259 :     GEN d, c = gel(u,k+1);
     232         259 :     if (is_pm1(c)) { k++; continue; }
     233         511 :     for (i=1; i<=k; i++)
     234             :     {
     235         350 :       GEN ui = gel(u,i);
     236         350 :       if (is_pm1(ui)) continue;
     237         168 :       d = gcdii(c, ui);
     238         168 :       if (d == gen_1) continue;
     239         168 :       c = diviiexact(c, d);
     240         168 :       gel(u,i) = diviiexact(ui, d);
     241         168 :       u = shallowconcat(u, d);
     242             :     }
     243         161 :     gel(u,++k) = c;
     244             :   }
     245         441 :   for (i = k = 1; i < lg(u); i++)
     246         350 :     if (!is_pm1(gel(u,i))) gel(u,k++) = gel(u,i);
     247          91 :   setlg(u, k); return u;
     248             : }
     249             : 
     250             : /*******************************************************************/
     251             : /*                                                                 */
     252             : /*                            ROUND 4                              */
     253             : /*                                                                 */
     254             : /*******************************************************************/
     255             : typedef struct {
     256             :   /* constants */
     257             :   long pisprime; /* -1: unknown, 1: prime,  0: composite */
     258             :   GEN p, f; /* goal: factor f p-adically */
     259             :   long df;
     260             :   GEN pdf; /* p^df = reduced discriminant of f */
     261             :   long mf; /* */
     262             :   GEN psf, pmf; /* stability precision for f, wanted precision for f */
     263             :   long vpsf; /* v_p(p_f) */
     264             :   /* these are updated along the way */
     265             :   GEN phi; /* a p-integer, in Q[X] */
     266             :   GEN phi0; /* a p-integer, in Q[X] from testb2 / testc2, to be composed with
     267             :              * phi when correct precision is known */
     268             :   GEN chi; /* characteristic polynomial of phi (mod psc) in Z[X] */
     269             :   GEN nu; /* irreducible divisor of chi mod p, in Z[X] */
     270             :   GEN invnu; /* numerator ( 1/ Mod(nu, chi) mod pmr ) */
     271             :   GEN Dinvnu;/* denominator ( ... ) */
     272             :   long vDinvnu; /* v_p(Dinvnu) */
     273             :   GEN prc, psc; /* reduced discriminant of chi, stability precision for chi */
     274             :   long vpsc; /* v_p(p_c) */
     275             :   GEN ns, nsf, precns; /* cached Newton sums for nsf and their precision */
     276             : } decomp_t;
     277             : static GEN maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag);
     278             : static GEN dbasis(GEN p, GEN f, long mf, GEN alpha, GEN U);
     279             : static GEN maxord(GEN p,GEN f,long mf);
     280             : static GEN ZX_Dedekind(GEN F, GEN *pg, GEN p);
     281             : 
     282             : /* Warning: data computed for T = ZX_Q_normalize(T0). If S.unscale !=
     283             :  * gen_1, caller must take steps to correct the components if it wishes
     284             :  * to stick to the original T0. Return a vector of p-maximal orders, for
     285             :  * those p s.t p^2 | disc(T) [ = S->dTP ]*/
     286             : static GEN
     287       22470 : get_maxord(nfmaxord_t *S, GEN T0, long flag)
     288             : {
     289             :   VOLATILE GEN P, E, O;
     290             :   VOLATILE long lP, i, k;
     291             : 
     292       22470 :   nfmaxord_check_args(S, T0, flag);
     293       22442 :   P = S->dTP; lP = lg(P);
     294       22442 :   E = S->dTE;
     295       22442 :   O = cgetg(1, t_VEC);
     296      102236 :   for (i=1; i<lP; i++)
     297             :   {
     298             :     VOLATILE pari_sp av;
     299             :     /* includes the silly case where P[i] = -1 */
     300       79794 :     if (E[i] <= 1) { O = shallowconcat(O, gen_1); continue; }
     301       62924 :     av = avma;
     302       62924 :     pari_CATCH(CATCH_ALL) {
     303          91 :       GEN N, u, err = pari_err_last();
     304             :       long l;
     305          91 :       switch(err_get_num(err))
     306             :       {
     307             :         case e_INV:
     308             :         {
     309          91 :           GEN p, x = err_get_compo(err, 2);
     310          91 :           if (typ(x) == t_INTMOD)
     311             :           { /* caught false prime, update factorization */
     312          91 :             p = gcdii(gel(x,1), gel(x,2));
     313          91 :             u = diviiexact(gel(x,1),p);
     314          91 :             if (DEBUGLEVEL) pari_warn(warner,"impossible inverse: %Ps", x);
     315          91 :             gerepileall(av, 2, &p, &u);
     316             : 
     317          91 :             u = get_coprimes(p, u); l = lg(u);
     318             :             /* no small factors, but often a prime power */
     319          91 :             for (k = 1; k < l; k++) (void)Z_isanypower(gel(u,k), &gel(u,k));
     320          91 :             break;
     321             :           }
     322             :           /* fall through */
     323             :         }
     324             :         case e_PRIME: case e_IRREDPOL:
     325             :         { /* we're here because we failed BPSW_isprime(), no point in
     326             :            * reporting a possible counter-example to the BPSW test */
     327           0 :           GEN p = gel(P,i);
     328           0 :           set_avma(av);
     329           0 :           if (DEBUGLEVEL)
     330           0 :             pari_warn(warner,"large composite in nfmaxord:loop(), %Ps", p);
     331           0 :           if (expi(p) < 100) /* factor should require ~20ms for this */
     332           0 :             u = gel(Z_factor(p), 1);
     333             :           else
     334             :           { /* give up, probably not maximal */
     335           0 :             GEN B, g, k = ZX_Dedekind(S->T, &g, p);
     336           0 :             k = FpX_normalize(k, p);
     337           0 :             B = dbasis(p, S->T, E[i], NULL, FpX_div(S->T,k,p));
     338           0 :             O = shallowconcat(O, mkvec(B));
     339           0 :             pari_CATCH_reset(); continue;
     340             :           }
     341           0 :           break;
     342             :         }
     343           0 :         default: pari_err(0, err);
     344             :           return NULL;/*LCOV_EXCL_LINE*/
     345             :       }
     346          91 :       l = lg(u);
     347          91 :       gel(P,i) = gel(u,1);
     348          91 :       P = shallowconcat(P, vecslice(u, 2, l-1));
     349          91 :       av = avma;
     350          91 :       N = S->dT; E[i] = Z_pvalrem(N, gel(P,i), &N);
     351          91 :       for (k=lP, lP=lg(P); k < lP; k++) E[k] = Z_pvalrem(N, gel(P,k), &N);
     352       63015 :     } pari_RETRY {
     353       63015 :       if (DEBUGLEVEL>2) err_printf("Treating p^k = %Ps^%ld\n",P[i],E[i]);
     354       63015 :       O = shallowconcat(O, mkvec( maxord(gel(P,i),S->T,E[i]) ));
     355       62924 :     } pari_ENDCATCH;
     356             :   }
     357       22442 :   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       12469 : diag_denom(GEN M)
     364             : {
     365       12469 :   GEN d = gen_1;
     366       12469 :   long j, l = lg(M);
     367      140432 :   for (j=1; j<l; j++)
     368             :   {
     369      127963 :     GEN t = gcoeff(M,j,j);
     370      127963 :     if (typ(t) == t_INT) continue;
     371       32306 :     t = gel(t,2);
     372       32306 :     if (abscmpii(t,d) > 0) d = t;
     373             :   }
     374       12469 :   return d;
     375             : }
     376             : static void
     377       19292 : setPE(GEN D, GEN P, GEN *pP, GEN *pE)
     378             : {
     379       19292 :   long k, j, l = lg(P);
     380             :   GEN P2, E2;
     381       19292 :   *pP = P2 = cgetg(l, t_COL);
     382       19292 :   *pE = E2 = cgetg(l, t_VECSMALL);
     383       59935 :   for (k = j = 1; j < l; j++)
     384             :   {
     385       40643 :     long v = Z_pvalrem(D, gel(P,j), &D);
     386       40643 :     if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
     387             :   }
     388       19292 :   setlg(P2, k);
     389       19292 :   setlg(E2, k);
     390       19292 : }
     391             : void
     392       19278 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
     393             : {
     394       19278 :   GEN O = get_maxord(S, T0, flag);
     395       19271 :   GEN f = S->T, P = S->dTP, a = NULL, da = NULL;
     396       19271 :   long n = degpol(f), lP = lg(P), i, j, k;
     397       19271 :   int centered = 0;
     398       19271 :   pari_sp av = avma;
     399             :   /* r1 & basden not initialized here */
     400       19271 :   S->r1 = -1;
     401       19271 :   S->basden = NULL;
     402       59865 :   for (i=1; i<lP; i++)
     403             :   {
     404       40594 :     GEN M, db, b = gel(O,i);
     405       40594 :     if (b == gen_1) continue;
     406       12469 :     db = diag_denom(b);
     407       12469 :     if (db == gen_1) continue;
     408             : 
     409             :     /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
     410       12469 :     b = Q_muli_to_int(b,db);
     411       12469 :     if (!da) { da = db; a = b; }
     412             :     else
     413             :     { /* optimization: easy as long as both matrix are diagonal */
     414        7842 :       j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
     415        7842 :       k = j-1; M = cgetg(2*n-k+1,t_MAT);
     416       44989 :       for (j=1; j<=k; j++)
     417             :       {
     418       37147 :         gel(M,j) = gel(a,j);
     419       37147 :         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        7842 :       for (  ; j<=n;     j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
     423        7842 :       for (  ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
     424        7842 :       da = mulii(da,db);
     425        7842 :       a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
     426        7842 :       gerepileall(av, 2, &a, &da);
     427        7842 :       centered = 1;
     428             :     }
     429             :   }
     430       19271 :   if (da)
     431             :   {
     432        4627 :     GEN index = diviiexact(da, gcoeff(a,1,1));
     433        4627 :     for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
     434        4627 :     if (!centered) a = ZM_hnfcenter(a);
     435        4627 :     a = RgM_Rg_div(a, da);
     436        4627 :     S->index = index;
     437        4627 :     S->dK = diviiexact(S->dT, sqri(index));
     438             :   }
     439             :   else
     440             :   {
     441       14644 :     S->index = gen_1;
     442       14644 :     S->dK = S->dT;
     443       14644 :     a = matid(n);
     444             :   }
     445       19271 :   setPE(S->dK, P, &S->dKP, &S->dKE);
     446       19271 :   S->basis = RgM_to_RgXV(a, varn(f));
     447       19271 : }
     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      124822 : Flx_checkdeflate(GEN x)
     506             : {
     507      124822 :   ulong d = 0, i, lx = (ulong)lg(x);
     508      284669 :   for (i=3; i<lx; i++)
     509      244960 :     if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
     510      124822 :   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      124822 : Flx_radical(GEN f, ulong p)
     517             : {
     518      124822 :   long v0 = Flx_valrem(f, &f);
     519             :   ulong du, d, e;
     520             :   GEN u;
     521             : 
     522      124822 :   d = Flx_checkdeflate(f);
     523      124822 :   if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
     524      105752 :   if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
     525      105752 :   u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
     526      105752 :   du = degpol(u);
     527      105752 :   if (du)
     528             :   {
     529       71898 :     if (du == (ulong)degpol(f))
     530           0 :       f = Flx_radical(Flx_deflate(f,p), p);
     531             :     else
     532             :     {
     533       71898 :       u = Flx_normalize(u, p);
     534       71898 :       f = Flx_div(f, u, p);
     535       71898 :       if (p <= du)
     536             :       {
     537        8587 :         GEN w = (degpol(f) >= degpol(u))? Flx_rem(f, u, p): f;
     538        8587 :         w = Flxq_powu(w, du, u, p);
     539        8587 :         w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
     540        8587 :         f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
     541             :       }
     542             :     }
     543             :   }
     544      105752 :   if (v0) f = Flx_shift(f, 1);
     545      105752 :   return f;
     546             : }
     547             : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
     548             : static GEN
     549        3416 : FpX_radical(GEN f, GEN p)
     550             : {
     551             :   GEN u;
     552             :   long v0;
     553        3416 :   if (lgefint(p) == 3)
     554             :   {
     555         574 :     ulong q = p[2];
     556         574 :     return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
     557             :   }
     558        2842 :   v0 = ZX_valrem(f, &f);
     559        2842 :   u = FpX_gcd(f,FpX_deriv(f, p), p);
     560        2751 :   if (degpol(u)) f = FpX_div(f, u, p);
     561        2751 :   if (v0) f = RgX_shift(f, 1);
     562        2751 :   return f;
     563             : }
     564             : /* f / a */
     565             : static GEN
     566      115661 : zx_z_div(GEN f, ulong a)
     567             : {
     568      115661 :   long i, l = lg(f);
     569      115661 :   GEN g = cgetg(l, t_VECSMALL);
     570      115661 :   g[1] = f[1];
     571      115661 :   for (i = 2; i < l; i++) g[i] = f[i] / a;
     572      115661 :   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      119077 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
     579             : {
     580             :   GEN k, h, g, f, f2;
     581      119077 :   ulong q = p[2];
     582      119077 :   if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
     583      115661 :   {
     584      115661 :     ulong q2 = q*q;
     585      115661 :     f2 = ZX_to_Flx(F, q2);
     586      115661 :     f = Flx_red(f2, q);
     587      115661 :     g = Flx_radical(f, q);
     588      115661 :     h = Flx_div(f, g, q);
     589      115661 :     k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
     590      115661 :     k = Flx_gcd(k, Flx_gcd(g,h,q), q);
     591      115661 :     k = Flx_to_ZX(k);
     592      115661 :     g = Flx_to_ZX(g);
     593             :   }
     594             :   else
     595             :   {
     596        3416 :     f2 = FpX_red(F, sqri(p));
     597        3416 :     f = FpX_red(f2, p);
     598        3416 :     g = FpX_radical(f, p);
     599        3325 :     h = FpX_div(f, g, p);
     600        3325 :     k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
     601        3325 :     k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
     602             :   }
     603      118986 :   *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      119077 : maxord(GEN p, GEN f, long mf)
     610             : {
     611      119077 :   const pari_sp av = avma;
     612      119077 :   GEN res, g, k = ZX_Dedekind(f, &g, p);
     613      118986 :   long dk = degpol(k);
     614      118986 :   if (DEBUGLEVEL>2) err_printf("  ZX_Dedekind: gcd has degree %ld\n", dk);
     615      118986 :   if (!dk) { set_avma(av); return gen_1; }
     616       78835 :   if (mf < 0) mf = ZpX_disc_val(f, p);
     617       78835 :   k = FpX_normalize(k, p);
     618       78835 :   if (2*dk >= mf-1)
     619       39335 :     res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
     620             :   else
     621             :   {
     622             :     GEN w, F1, F2;
     623             :     decomp_t S;
     624       39500 :     F1 = FpX_factor(k,p);
     625       39500 :     F2 = FpX_factor(FpX_div(g,k,p),p);
     626       39500 :     w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
     627       39500 :     res = maxord_i(&S, p, f, mf, w, 0);
     628             :   }
     629       78835 :   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      974049 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
     654             : {
     655      974049 :   long j, n = degpol(f1);
     656      974040 :   GEN h, a = cgetg(n+1,t_MAT);
     657      974040 :   f1 = Flx_get_red(f1, pm);
     658      974036 :   h = Flx_rem(f2,f1,pm);
     659     4256181 :   for (j=1;; j++)
     660             :   {
     661     7538325 :     gel(a,j) = Flx_to_Flv(h, n);
     662     4256176 :     if (j == n) break;
     663     3282143 :     h = Flx_rem(Flx_shift(h, 1), f1, pm);
     664             :   }
     665      974033 :   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       15961 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
     671             : {
     672       15961 :   long j, n = degpol(f1);
     673       15961 :   GEN h, a = cgetg(n+1,t_MAT);
     674       15961 :   h = FpXQ_red(f2,f1,pm);
     675      158194 :   for (j=1;; j++)
     676             :   {
     677      300427 :     gel(a,j) = RgX_to_RgC(h, n);
     678      158194 :     if (j == n) break;
     679      142233 :     h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
     680             :   }
     681       15961 :   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       22294 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
     687             : {
     688       22294 :   pari_sp av = avma;
     689       22294 :   GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
     690       22294 :   long c, l = lg(a), sv = f1[1];
     691      132386 :   for (c = 1; c < l; c++)
     692             :   {
     693      132386 :     ulong t = ucoeff(a,c,c);
     694      132386 :     if (t)
     695             :     {
     696       22294 :       a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
     697       22294 :       if (t == 1) return gerepilecopy(av, a);
     698        3072 :       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       28318 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
     706             : {
     707       28318 :   pari_sp av = avma;
     708             :   GEN a;
     709             :   long c, l, v;
     710       28318 :   if (lgefint(pm) == 3)
     711             :   {
     712       22294 :     ulong q = pm[2];
     713       22294 :     return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
     714             :   }
     715        6024 :   a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
     716        6024 :   l = lg(a); v = varn(f1);
     717       40969 :   for (c = 1; c < l; c++)
     718             :   {
     719       40969 :     GEN t = gcoeff(a,c,c);
     720       40969 :     if (signe(t))
     721             :     {
     722        6024 :       a = RgV_to_RgX(gel(a,c), v);
     723        6024 :       if (equali1(t)) return gerepilecopy(av, a);
     724        1745 :       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      902939 : init_m(GEN p)
     733             : {
     734      902939 :   if (lgefint(p) > 3) return 1;
     735      902692 :   return (long)(16 / log2(p[2]));
     736             : }
     737             : 
     738             : /* reduced resultant mod p^m (assumes x monic) */
     739             : GEN
     740       94326 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
     741             : {
     742       94326 :   pari_sp av = avma;
     743             :   GEN z;
     744       94326 :   if (lgefint(pm) == 3)
     745             :   {
     746       88850 :     ulong q = pm[2];
     747       88850 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
     748       88850 :     if (lg(z) > 1)
     749             :     {
     750       88850 :       ulong c = ucoeff(z,1,1);
     751       88850 :       if (c) { set_avma(av); return utoipos(c); }
     752             :     }
     753             :   }
     754             :   else
     755             :   {
     756        5476 :     z = ZpX_sylvester_echelon(x,y,0,p,pm);
     757        5476 :     if (lg(z) > 1)
     758             :     {
     759        5476 :       GEN c = gcoeff(z,1,1);
     760        5476 :       if (signe(c)) return gerepileuptoint(av, c);
     761             :     }
     762             :   }
     763       36482 :   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       65308 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
     769             : {
     770       65308 :   GEN R, q = NULL;
     771             :   long m;
     772       65308 :   m = init_m(p); if (m < 1) m = 1;
     773       29018 :   for(;; m <<= 1) {
     774      123344 :     if (M < 2*m) break;
     775       44855 :     q = q? sqri(q): powiu(p, m); /* p^m */
     776       44855 :     R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
     777             :   }
     778       49471 :   q = powiu(p, M);
     779       49471 :   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      867368 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
     785             : {
     786      867368 :   pari_sp av = avma;
     787             :   GEN z;
     788             :   long i, l, v;
     789      867368 :   if (lgefint(pm) == 3)
     790             :   {
     791      862907 :     ulong q = pm[2], pp = p[2];
     792      862907 :     z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
     793      862913 :     if (!z) return gc_long(av,-1); /* failure */
     794      790810 :     v = 0; l = lg(z);
     795      790810 :     for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
     796             :   }
     797             :   else
     798             :   {
     799        4461 :     z = ZpX_sylvester_echelon(x, y, 1, p, pm);
     800        4461 :     if (!z) return gc_long(av,-1); /* failure */
     801        3881 :     v = 0; l = lg(z);
     802        3881 :     for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
     803             :   }
     804      794690 :   return v;
     805             : }
     806             : 
     807             : /* assume (lc(f),p) = 1; no assumption on g */
     808             : long
     809      837626 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
     810             : {
     811      837626 :   pari_sp av = avma;
     812      837626 :   GEN q = NULL;
     813             :   long v, m;
     814      837626 :   m = init_m(p); if (m < 2) m = 2;
     815       29730 :   for(;; m <<= 1) {
     816      897091 :     if (m > M) m = M;
     817      867361 :     q = q? sqri(q): powiu(p, m); /* p^m */
     818      867368 :     v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) return gc_long(av,v);
     819       72683 :     if (m == M) return gc_long(av,M);
     820             :   }
     821             : }
     822             : 
     823             : /* assume f separable and (lc(f),p) = 1 */
     824             : long
     825       33424 : ZpX_disc_val(GEN f, GEN p)
     826             : {
     827       33424 :   pari_sp av = avma;
     828             :   long v;
     829       33424 :   if (degpol(f) == 1) return 0;
     830       33424 :   v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
     831       33424 :   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      627816 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
     838             : {
     839             :   GEN newe;
     840      627816 :   long ve = ZX_pvalrem(*e, p, &newe);
     841      627816 :   if (ve) {
     842             :     GEN newd;
     843      359710 :     long v = minss(*vd, ve);
     844      359710 :     if (v) {
     845      359710 :       if (v == *vd)
     846             :       { /* rare, denominator cancelled */
     847       42941 :         if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
     848       42941 :         newd = gen_1;
     849       42941 :         *vd = 0;
     850       42941 :         if (z) *z =diviiexact(*z, powiu(p, v));
     851             :       }
     852             :       else
     853             :       { /* v = ve < vd, generic case */
     854      316769 :         GEN q = powiu(p, v);
     855      316769 :         newd = diviiexact(*d, q);
     856      316769 :         *vd -= v;
     857      316769 :         if (z) *z = diviiexact(*z, q);
     858             :       }
     859      359710 :       *e = newe;
     860      359710 :       *d = newd;
     861             :     }
     862             :   }
     863      627816 : }
     864             : 
     865             : /* return denominator, a power of p */
     866             : static GEN
     867      401983 : QpX_denom(GEN x)
     868             : {
     869      401983 :   long i, l = lg(x);
     870      401983 :   GEN maxd = gen_1;
     871     2070935 :   for (i=2; i<l; i++)
     872             :   {
     873     1668952 :     GEN d = gel(x,i);
     874     1668952 :     if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
     875             :   }
     876      401983 :   return maxd;
     877             : }
     878             : static GEN
     879       56062 : QpXV_denom(GEN x)
     880             : {
     881       56062 :   long l = lg(x), i;
     882       56062 :   GEN maxd = gen_1;
     883      287811 :   for (i = 1; i < l; i++)
     884             :   {
     885      231749 :     GEN d = QpX_denom(gel(x,i));
     886      231749 :     if (cmpii(d, maxd) > 0) maxd = d;
     887             :   }
     888       56062 :   return maxd;
     889             : }
     890             : 
     891             : static GEN
     892      170234 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
     893             : {
     894      170234 :   *pdx = QpX_denom(x);
     895      170234 :   if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
     896             :   else {
     897      128527 :     x = Q_muli_to_int(x,*pdx);
     898      128527 :     *pv = Z_pval(*pdx, p);
     899             :   }
     900      170234 :   return x;
     901             : }
     902             : 
     903             : /* p^v * f o g mod (T,q). q = p^vq  */
     904             : static GEN
     905       24551 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
     906             : {
     907       24551 :   GEN D = NULL, z, df, dg, qD;
     908       24551 :   long vD = 0, vdf, vdg;
     909             : 
     910       24551 :   f = QpX_remove_denom(f, p, &df, &vdf);
     911       24551 :   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       24551 :     g = QpX_remove_denom(g, p, &dg, &vdg);
     915       24551 :   if (df) { D = df; vD = vdf; }
     916       24551 :   if (dg) {
     917        4397 :     long degf = degpol(f);
     918        4397 :     D = mul_content(D, powiu(dg, degf));
     919        4397 :     vD += degf * vdg;
     920             :   }
     921       24551 :   qD = D ? mulii(q, D): q;
     922       24551 :   if (dg) f = FpX_rescale(f, dg, qD);
     923       24551 :   z = FpX_FpXQ_eval(f, g, T, qD);
     924       24551 :   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       24551 :   update_den(p, &z, &D, &vD, NULL);
     934       24551 :   qD = mulii(D,q);
     935       24551 :   if (v) vD -= v;
     936       24551 :   z = FpX_center_i(z, qD, shifti(qD,-1));
     937       24551 :   if (vD > 0)
     938       24551 :     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       24551 :   return z;
     942             : }
     943             : 
     944             : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
     945             : static GEN
     946       39500 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
     947             : {
     948       39500 :   long i, l = lg(M);
     949       39500 :   M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
     950      321420 :   for (i = 1; i < l; i++)
     951      281920 :     if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
     952       39500 :   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       50804 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
     959             : {
     960       50804 :   long n = degpol(f), i, dU;
     961             :   GEN b, h;
     962             : 
     963       50804 :   if (n == 1) return matid(1);
     964       50804 :   if (a && gequalX(a)) a = NULL;
     965       50804 :   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       50804 :   if (a)
     971             :   {
     972       11469 :     GEN pd = powiu(p, mf >> 1);
     973       11469 :     GEN da, pdp = mulii(pd,p), D = pdp;
     974             :     long vda;
     975       11469 :     dU = U ? degpol(U): 0;
     976       11469 :     b = cgetg(n+1, t_MAT);
     977       11469 :     h = scalarpol(pd, varn(f));
     978       11469 :     a = QpX_remove_denom(a, p, &da, &vda);
     979       11469 :     if (da) D = mulii(D, da);
     980       11469 :     gel(b,1) = scalarcol_shallow(pd, n);
     981       50171 :     for (i=2; i<=n; i++)
     982             :     {
     983       38702 :       if (i == dU+1)
     984           0 :         h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
     985             :       else
     986             :       {
     987       38702 :         h = FpXQ_mul(h, a, f, D);
     988       38702 :         if (da) h = ZX_Z_divexact(h, da);
     989             :       }
     990       38702 :       gel(b,i) = RgX_to_RgC(h,n);
     991             :     }
     992       11469 :     return ZpM_hnfmodid(b, p, pd);
     993             :   }
     994             :   else
     995             :   {
     996       39335 :     if (!U) return matid(n);
     997       39335 :     dU = degpol(U);
     998       39335 :     if (dU == n) return matid(n);
     999       39335 :     U = FpX_normalize(U, p);
    1000       39335 :     b = cgetg(n+1, t_MAT);
    1001       39335 :     for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
    1002       39335 :     h = RgX_Rg_div(U, p);
    1003       52588 :     for ( ; i <= n; i++)
    1004             :     {
    1005       52588 :       gel(b, i) = RgX_to_RgC(h,n);
    1006       52588 :       if (i == n) break;
    1007       13253 :       h = RgX_shift_shallow(h,1);
    1008             :     }
    1009       39335 :     return b;
    1010             :   }
    1011             : }
    1012             : 
    1013             : static GEN
    1014       56062 : get_partial_order_as_pols(GEN p, GEN f)
    1015             : {
    1016       56062 :   GEN O = maxord(p, f, -1);
    1017       56062 :   long v = varn(f);
    1018       56062 :   return O == gen_1? pol_x_powers(degpol(f), v): RgM_to_RgXV(O, v);
    1019             : }
    1020             : 
    1021             : static long
    1022        1170 : p_is_prime(decomp_t *S)
    1023             : {
    1024        1170 :   if (S->pisprime < 0) S->pisprime = BPSW_psp(S->p);
    1025        1170 :   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       28318 : Decomp(decomp_t *S, long flag)
    1032             : {
    1033       28318 :   pari_sp av = avma;
    1034             :   GEN fred, pr2, pr, pk, ph2, ph, b1, b2, a, e, de, f1, f2, dt, th, chip;
    1035       28318 :   GEN p = S->p;
    1036       28318 :   long vde, vdt, k, r = maxss(flag, 2*S->df + 1);
    1037             : 
    1038       28318 :   if (DEBUGLEVEL>5) err_printf("  entering Decomp: %Ps^%ld\n  f = %Ps\n",
    1039             :                                p, r, S->f);
    1040       28318 :   else if (DEBUGLEVEL>2) err_printf("  entering Decomp\n");
    1041       28318 :   chip = FpX_red(S->chi, p);
    1042       28318 :   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       28318 :   b2 = FpX_div(chip, b1, p);
    1048       28318 :   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       28318 :   th = QpX_remove_denom(S->phi, p, &dt, &vdt);
    1051       28318 :   if (dt)
    1052             :   {
    1053       11434 :     long dega = degpol(a);
    1054       11434 :     vde = dega * vdt;
    1055       11434 :     de = powiu(dt, dega);
    1056       11434 :     pr = mulii(p, de);
    1057       11434 :     a = FpX_rescale(a, dt, pr);
    1058             :   }
    1059             :   else
    1060             :   {
    1061       16884 :     vde = 0;
    1062       16884 :     de = gen_1;
    1063       16884 :     pr = p;
    1064             :   }
    1065       28318 :   e = FpX_FpXQ_eval(a, th, S->f, pr);
    1066       28318 :   update_den(p, &e, &de, &vde, NULL);
    1067             : 
    1068       28318 :   pk = p; k = 1;
    1069             :   /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
    1070      179441 :   while (k < r + vde)
    1071             :   { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
    1072             :     GEN D;
    1073      122805 :     pk = sqri(pk); k <<= 1;
    1074      122805 :     e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
    1075      122805 :     de= mulii(de, sqri(de));
    1076      122805 :     vde *= 3;
    1077      122805 :     D = mulii(pk, de);
    1078      122805 :     e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
    1079      122805 :     update_den(p, &e, &de, &vde, NULL);
    1080             :   }
    1081             :   /* required precision of the factors */
    1082       28318 :   pr = powiu(p, r); pr2 = shifti(pr, -1);
    1083       28318 :   ph = mulii(de,pr);ph2 = shifti(ph, -1);
    1084       28318 :   e = FpX_center_i(FpX_red(e, ph), ph, ph2);
    1085       28318 :   fred = FpX_red(S->f, ph);
    1086             : 
    1087       28318 :   f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
    1088       28318 :   if (!is_pm1(de))
    1089             :   {
    1090       11434 :     fred = FpX_red(fred, pr);
    1091       11434 :     f1 = FpX_red(f1, pr);
    1092             :   }
    1093       28318 :   f2 = FpX_div(fred,f1, pr);
    1094       28318 :   f1 = FpX_center_i(f1, pr, pr2);
    1095       28318 :   f2 = FpX_center_i(f2, pr, pr2);
    1096             : 
    1097       28318 :   if (DEBUGLEVEL>5)
    1098           0 :     err_printf("  leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
    1099             : 
    1100       28318 :   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       28318 :   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       28031 :     gerepileall(av, 4, &f1, &f2, &e, &de);
    1114       28031 :     D = de;
    1115       28031 :     B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
    1116       28031 :     B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
    1117       28031 :     d1 = QpXV_denom(B1);
    1118       28031 :     d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
    1119       28031 :     if (d1 != gen_1) {
    1120       24242 :       B1 = Q_muli_to_int(B1, d1);
    1121       24242 :       B2 = Q_muli_to_int(B2, d1);
    1122       24242 :       D = mulii(d1, D);
    1123             :     }
    1124       28031 :     fred = centermod_i(S->f, D, shifti(D,-1));
    1125       28031 :     M = cgetg(n+1, t_MAT);
    1126      172382 :     for (i=1; i<=n1; i++)
    1127      144351 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
    1128       28031 :     e = Z_ZX_sub(de, e); B2 -= n1;
    1129      115429 :     for (   ; i<=n; i++)
    1130       87398 :       gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
    1131       28031 :     return ZpM_hnfmodid(M, p, D);
    1132             :   }
    1133             : }
    1134             : 
    1135             : /* minimum extension valuation: L/E */
    1136             : static void
    1137       57753 : vstar(GEN p,GEN h, long *L, long *E)
    1138             : {
    1139       57753 :   long first, j, k, v, w, m = degpol(h);
    1140             : 
    1141       57753 :   first = 1; k = 1; v = 0;
    1142      404024 :   for (j=1; j<=m; j++)
    1143             :   {
    1144      346271 :     GEN c = gel(h, m-j+2);
    1145      346271 :     if (signe(c))
    1146             :     {
    1147      333117 :       w = Z_pval(c,p);
    1148      333117 :       if (first || w*k < v*j) { v = w; k = j; }
    1149      333117 :       first = 0;
    1150             :     }
    1151             :   }
    1152             :   /* v/k = min_j ( v_p(h_{m-j}) / j ) */
    1153       57753 :   w = (long)ugcd(v,k);
    1154       57753 :   *L = v/w;
    1155       57753 :   *E = k/w;
    1156       57753 : }
    1157             : 
    1158             : static GEN
    1159        6429 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
    1160             : {
    1161             :   GEN z;
    1162        6429 :   a = Q_remove_denom(a, pda);
    1163        6429 :   *pvda = 0;
    1164        6429 :   if (*pda)
    1165             :   {
    1166        6429 :     long v = Z_pvalrem(*pda, p, &z);
    1167        6429 :     if (v) {
    1168        6429 :       *pda = powiu(p, v);
    1169        6429 :       *pvda = v;
    1170        6429 :       N  = mulii(*pda, N);
    1171             :     }
    1172             :     else
    1173           0 :       *pda = NULL;
    1174        6429 :     if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
    1175             :   }
    1176        6429 :   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       78064 : 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       78064 :   long j, k, vdpa, lns = lg(ns);
    1199             :   pari_sp av;
    1200             : 
    1201       78064 :   a = centermod(a, pp); av = avma;
    1202       78064 :   dpa = pa = NULL; /* -Wall */
    1203       78064 :   vdpa = 0;
    1204       78064 :   va = zerovec(c);
    1205      529451 :   for (j = 1; j <= c; j++)
    1206             :   { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
    1207             :     long l;
    1208      452598 :     pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
    1209      452598 :     l = lg(pa); if (l == 2) break;
    1210      452598 :     if (lns < l) l = lns;
    1211             : 
    1212      452598 :     if (da) {
    1213      443276 :       dpa = j == 1? da: mulii(dpa, da);
    1214      443276 :       vdpa += vda;
    1215      443276 :       update_den(p, &pa, &dpa, &vdpa, &pp);
    1216             :     }
    1217      452598 :     s = mulii(gel(pa,2), gel(ns,2)); /* k = 2 */
    1218      452598 :     for (k = 3; k < l; k++) s = addii(s, mulii(gel(pa,k), gel(ns,k)));
    1219      452598 :     if (da) {
    1220             :       GEN r;
    1221      443276 :       s = dvmdii(s, dpa, &r);
    1222      443276 :       if (r != gen_0) return NULL;
    1223             :     }
    1224      451387 :     gel(va,j) = centermodii(s, pp, shifti(pp,-1));
    1225             : 
    1226      451387 :     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       76853 :   for (; j <= c; j++) gel(va,j) = gen_0;
    1233       76853 :   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       76853 : newtoncharpoly(GEN pp, GEN p, GEN NS)
    1240             : {
    1241       76853 :   long n = lg(NS)-1, j, k;
    1242       76853 :   GEN c = cgetg(n + 2, t_VEC), pp2 = shifti(pp,-1);
    1243             : 
    1244       76853 :   gel(c,1) = (n & 1 ? gen_m1: gen_1);
    1245      525599 :   for (k = 2; k <= n+1; k++)
    1246             :   {
    1247      448767 :     pari_sp av2 = avma;
    1248      448767 :     GEN s = gen_0;
    1249             :     ulong z;
    1250      448767 :     long v = u_pvalrem(k - 1, p, &z);
    1251     3561003 :     for (j = 1; j < k; j++)
    1252             :     {
    1253     3112236 :       GEN t = mulii(gel(NS,j), gel(c,k-j));
    1254     3112236 :       if (!odd(j)) t = negi(t);
    1255     3112236 :       s = addii(s, t);
    1256             :     }
    1257      448767 :     if (v) {
    1258      159549 :       s = gdiv(s, powiu(p, v));
    1259      159549 :       if (typ(s) != t_INT) return NULL;
    1260             :     }
    1261      448746 :     s = mulii(s, Fp_inv(utoipos(z), pp));
    1262      448746 :     gel(c,k) = gerepileuptoint(av2, Fp_center_i(s, pp, pp2));
    1263             :   }
    1264       76832 :   for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
    1265       76832 :   return gtopoly(c, 0);
    1266             : }
    1267             : 
    1268             : static void
    1269       78064 : manage_cache(decomp_t *S, GEN f, GEN pp)
    1270             : {
    1271       78064 :   GEN t = S->precns;
    1272             : 
    1273       78064 :   if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
    1274       78064 :   if (cmpii(t, pp) < 0) t = pp;
    1275             : 
    1276       78064 :   if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
    1277             :   {
    1278       46484 :     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       46484 :     S->nsf = f;
    1282       46484 :     S->ns = FpX_Newton(f, degpol(f), t);
    1283       46484 :     S->precns = t;
    1284             :   }
    1285       78064 : }
    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       78064 : 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       78064 :   long vd, n = degpol(f);
    1295             : 
    1296       78064 :   if (gequal0(a)) return pol_0(varn(f));
    1297             : 
    1298       78064 :   a = QpX_remove_denom(a, S->p, &d, &vd);
    1299       78064 :   prec1 = pp;
    1300       78064 :   if (lgefint(S->p) == 3)
    1301       78033 :     prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
    1302       78064 :   if (d)
    1303             :   {
    1304       75244 :     GEN p1 = powiu(d, n);
    1305       75244 :     prec2 = mulii(prec1, p1);
    1306       75244 :     prec3 = mulii(prec1, gmin_shallow(mulii(p1, d), pdr));
    1307             :   }
    1308             :   else
    1309        2820 :     prec2 = prec3 = prec1;
    1310       78064 :   manage_cache(S, f, prec3);
    1311             : 
    1312       78064 :   av = avma;
    1313       78064 :   ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
    1314       78064 :   if (!ns) return NULL;
    1315       76853 :   chi = newtoncharpoly(prec1, S->p, ns);
    1316       76853 :   if (!chi) return NULL;
    1317       76832 :   setvarn(chi, varn(f));
    1318       76832 :   return gerepileupto(av, centermod(chi, pp));
    1319             : }
    1320             : 
    1321             : static GEN
    1322       67158 : get_nu(GEN chi, GEN p, long *ptl)
    1323             : { /* split off powers of x first for efficiency */
    1324       67158 :   long v = ZX_valrem(FpX_red(chi,p), &chi), n;
    1325             :   GEN P;
    1326       67158 :   if (!degpol(chi)) { *ptl = 1; return pol_x(varn(chi)); }
    1327       61617 :   P = gel(FpX_factor(chi,p), 1); n = lg(P)-1;
    1328       61617 :   *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       60606 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
    1336             : {
    1337             :   long l;
    1338       60606 :   *nu  = get_nu(chi, S->p, &l);
    1339       60606 :   if (l == 1) return 0; /* single irreducible factor: doesn't split */
    1340             :   /* phi o phi0 mod (p, f) */
    1341       11434 :   S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
    1342       11434 :   S->chi = chi;
    1343       11434 :   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       55549 : 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       55549 :   if (phi && dvdii(constant_coeff(chip), S->psc))
    1357             :   {
    1358         217 :     chip = mycaract(S, S->chi, phi, S->pmf, S->prc);
    1359         217 :     if (dvdii(constant_coeff(chip), S->pmf))
    1360          14 :       chip = ZXQ_charpoly(phi, S->chi, varn(chip));
    1361             :   }
    1362       55549 :   if (degpol(nup) == 1)
    1363             :   {
    1364       46580 :     GEN c = gel(nup,2); /* nup = X + c */
    1365       46580 :     chin = signe(c)? RgX_translate(chip, negi(c)): chip;
    1366             :   }
    1367             :   else
    1368        8969 :     chin = ZXQ_charpoly(nup, chip, varn(chip));
    1369             : 
    1370       55549 :   vstar(S->p, chin, Lp, Ep);
    1371       55549 :   if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
    1372             : 
    1373       31773 :   if (*Ep == 1) return S->p;
    1374       17761 :   (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
    1375       17761 :   if (r <= 0)
    1376             :   {
    1377        2404 :     long t = 1 + ((-r) / *Ep);
    1378        2404 :     r += t * *Ep;
    1379        2404 :     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       17761 :   q = powiu(S->p, s); qp = mulii(q, S->p);
    1385       17761 :   nup = FpXQ_powu(nup, r, S->chi, qp);
    1386       17761 :   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       17820 : update_phi(decomp_t *S)
    1393             : {
    1394       17820 :   GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
    1395             :   long k;
    1396       17933 :   for (k = 1;; k++)
    1397             :   {
    1398       18046 :     prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, S->vpsc);
    1399       17933 :     if (!equalii(prc, S->psc)) break;
    1400             : 
    1401             :     /* increase precision */
    1402         113 :     S->vpsc = maxss(S->vpsf, S->vpsc + 1);
    1403         113 :     S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
    1404             : 
    1405         113 :     PHI = S->phi;
    1406         113 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
    1407         113 :     PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
    1408         113 :     S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
    1409             :   }
    1410       17820 :   psc = mulii(sqri(prc), S->p);
    1411             : 
    1412       17820 :   if (!PHI) /* ok above for k = 1 */
    1413             :   {
    1414       17707 :     PHI = S->phi;
    1415       17707 :     if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
    1416       17707 :     if (S->phi0 || cmpii(psc,S->psc) > 0)
    1417       11218 :       S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
    1418             :   }
    1419       17820 :   S->phi = PHI;
    1420       17820 :   S->chi = FpX_red(S->chi, psc);
    1421             : 
    1422             :   /* may happen if p is unramified */
    1423       17820 :   if (is_pm1(prc)) return 0;
    1424       13104 :   S->psc = psc;
    1425       13104 :   S->vpsc = 2*Z_pval(prc, S->p) + 1;
    1426       13104 :   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        9426 : testb2(decomp_t *S, long D, GEN theta)
    1433             : {
    1434        9426 :   long v = varn(S->chi), dlim = degpol(S->chi)-1;
    1435        9426 :   GEN T0 = S->phi, chi, phi, nu;
    1436        9426 :   if (DEBUGLEVEL>4) err_printf("  Increasing Fa\n");
    1437             :   for (;;)
    1438             :   {
    1439        9482 :     phi = gadd(theta, random_FpX(dlim, v, S->p));
    1440        9454 :     chi = mycaract(S, S->chi, phi, S->psf, S->prc);
    1441             :     /* phi non-primary ? */
    1442        9454 :     if (split_char(S, chi, phi, T0, &nu)) return 1;
    1443        9447 :     if (degpol(nu) == D) break;
    1444             :   }
    1445             :   /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
    1446        9419 :   S->phi0 = T0;
    1447        9419 :   S->chi = chi;
    1448        9419 :   S->phi = phi;
    1449        9419 :   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        1874 : ZX_rescale_inv(GEN P, GEN h)
    1488             : {
    1489        1874 :   long i, l = lg(P);
    1490        1874 :   GEN Q = cgetg(l,t_POL), hi = h;
    1491        1874 :   gel(Q,l-1) = gel(P,l-1);
    1492       10747 :   for (i=l-2; i>=2; i--)
    1493             :   {
    1494             :     GEN r;
    1495       10747 :     gel(Q,i) = dvmdii(gel(P,i), hi, &r);
    1496       10747 :     if (signe(r)) return NULL;
    1497       10747 :     if (i == 2) break;
    1498        8873 :     hi = mulii(hi,h);
    1499             :   }
    1500        1874 :   Q[1] = P[1]; return Q;
    1501             : }
    1502             : 
    1503             : /* x p^-eq nu^-er mod p */
    1504             : static GEN
    1505       45380 : get_gamma(decomp_t *S, GEN x, long eq, long er)
    1506             : {
    1507       45380 :   GEN q, g = x, Dg = powiu(S->p, eq);
    1508       45380 :   long vDg = eq;
    1509       45380 :   if (er)
    1510             :   {
    1511        8866 :     if (!S->invnu)
    1512             :     {
    1513        4580 :       while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
    1514        4580 :       S->invnu = QXQ_inv(S->nu, S->chi);
    1515        4580 :       S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
    1516             :     }
    1517        8866 :     if (S->Dinvnu) {
    1518        8866 :       Dg = mulii(Dg, powiu(S->Dinvnu, er));
    1519        8866 :       vDg += er * S->vDinvnu;
    1520             :     }
    1521        8866 :     q = mulii(S->p, Dg);
    1522        8866 :     g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
    1523        8866 :     g = FpX_rem(g, S->chi, q);
    1524        8866 :     update_den(S->p, &g, &Dg, &vDg, NULL);
    1525        8866 :     g = centermod(g, mulii(S->p, Dg));
    1526             :   }
    1527       45380 :   if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
    1528       45380 :   return g;
    1529             : }
    1530             : static GEN
    1531       46022 : 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       46022 :   GEN g, chig, chib = NULL;
    1536             :   for(;;) /* at most twice */
    1537             :   {
    1538       48486 :     if (L < 0)
    1539             :     {
    1540        2204 :       chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
    1541        2204 :       vstar(S->p, chib, &L, &E);
    1542             :     }
    1543       47254 :     eq = L / E; er = L*Ea / E - eq*Ea;
    1544             :     /* floor(L Ea/E) = eq Ea + er */
    1545       47254 :     if (er || !chib)
    1546             :     { /* g might not be an integer ==> chig = NULL */
    1547       45380 :       g = get_gamma(S, beta, eq, er);
    1548       45380 :       chig = mycaract(S, S->chi, g, S->psc, S->prc);
    1549             :     }
    1550             :     else
    1551             :     { /* g = beta/p^eq, special case of the above */
    1552        1874 :       GEN h = powiu(S->p, eq);
    1553        1874 :       g = RgX_Rg_div(beta, h);
    1554        1874 :       chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
    1555        1874 :       if (chig) chig = FpX_red(chig, S->pmf);
    1556             :     }
    1557             :     /* either success or second consecutive failure */
    1558       47254 :     if (chig || chib) break;
    1559             :     /* if g fails the v*-test, v(beta) was wrong. Retry once */
    1560        1232 :     L = -1;
    1561             :   }
    1562       46022 :   *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       22702 : loop(decomp_t *S, long Ea)
    1568             : {
    1569       22702 :   pari_sp av = avma;
    1570       22702 :   GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
    1571       22702 :   long N = degpol(S->f), v = varn(S->f);
    1572       22702 :   S->invnu = NULL;
    1573             :   for (;;)
    1574       23320 :   { /* beta tends to a factor of chi */
    1575             :     long L, i, Fg, eq, er;
    1576       46022 :     GEN chig = NULL, d, g, nug;
    1577             : 
    1578       46022 :     if (DEBUGLEVEL>4) err_printf("  beta = %Ps\n", beta);
    1579       46022 :     L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
    1580       46022 :     if (L > S->mf) L = -1; /* from scratch */
    1581       46022 :     g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
    1582       46022 :     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       68724 :     if (split_char(S, chig, g,S->phi, &nug)) return 1;
    1585             : 
    1586       35105 :     Fg = degpol(nug);
    1587       35105 :     if (Fg == 1)
    1588             :     { /* frequent special case nug = x - d */
    1589             :       long Le, Ee;
    1590             :       GEN chie, nue, e, pie;
    1591       22398 :       d = negi(gel(nug,2));
    1592       22398 :       chie = RgX_translate(chig, d);
    1593       22398 :       nue = pol_x(v);
    1594       22398 :       e = RgX_Rg_sub(g, d);
    1595       22398 :       pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1596       22398 :       if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1597             :     }
    1598             :     else
    1599             :     {
    1600       12707 :       long Fa = degpol(S->nu), vdeng;
    1601             :       GEN deng, numg, nume;
    1602       22847 :       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        3281 :       if (ZX_equal(nug, S->nu))
    1606        2111 :         d = pol_x(v);
    1607             :       else
    1608             :       {
    1609        1170 :         if (!p_is_prime(S)) pari_err_PRIME("FpX_ffisom",S->p);
    1610        1170 :         d = FpX_ffisom(nug, S->nu, S->p);
    1611             :       }
    1612             :       /* write g = numg / deng, e = nume / deng */
    1613        3281 :       numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
    1614        5758 :       for (i = 1; i <= Fg; i++)
    1615             :       {
    1616             :         GEN chie, nue, e;
    1617        5758 :         if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
    1618        5758 :         nume = ZX_sub(numg, ZX_Z_mul(d, deng));
    1619             :         /* test e = nume / deng */
    1620        5758 :         if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
    1621        2477 :           continue;
    1622        3281 :         e = RgX_Rg_div(nume, deng);
    1623        3281 :         chie = mycaract(S, S->chi, e, S->psc, S->prc);
    1624        3995 :         if (split_char(S, chie, e,S->phi, &nue)) return 1;
    1625        2771 :         if (RgX_is_monomial(nue))
    1626             :         { /* v_p(e) = v_p(g - d) > 0 */
    1627             :           long Le, Ee;
    1628             :           GEN pie;
    1629        2771 :           pie = getprime(S, e, chie, nue, &Le, &Ee,  0,Ea);
    1630        2771 :           if (pie) return testc2(S, S->nu, Ea, pie, Ee);
    1631        2567 :           break;
    1632             :         }
    1633             :       }
    1634        2567 :       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       23320 :     if (eq) d = gmul(d, powiu(S->p,  eq));
    1641       23320 :     if (er) d = gmul(d, gpowgs(S->nu, er));
    1642       23320 :     beta = gsub(beta, d);
    1643             : 
    1644       23320 :     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       29924 : progress(decomp_t *S, GEN *ppa, long *pE)
    1656             : {
    1657       29924 :   long E = *pE, F;
    1658       29924 :   GEN pa = *ppa;
    1659       29924 :   S->phi0 = NULL; /* no delayed composition */
    1660             :   for(;;)
    1661         456 :   {
    1662             :     long l, La, Ea; /* N.B If E = 0, getprime cannot return NULL */
    1663       30380 :     GEN pia  = getprime(S, NULL, S->chi, S->nu, &La, &Ea, E,0);
    1664       30380 :     if (pia) { /* success, we break out in THIS loop */
    1665       29924 :       pa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
    1666       29924 :       E = Ea;
    1667       59848 :       if (La == 1) break; /* no need to change phi so that nu = pia */
    1668             :     }
    1669             :     /* phi += prime elt */
    1670       15987 :     S->phi = typ(pa) == t_INT? RgX_Rg_add_shallow(S->phi, pa)
    1671        9435 :                              : RgX_add(S->phi, pa);
    1672             :     /* recompute char. poly. chi from scratch */
    1673        6552 :     S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
    1674        6552 :     S->nu = get_nu(S->chi, S->p, &l);
    1675        6552 :     if (l > 1) return 2;
    1676        6552 :     if (!update_phi(S)) return 1; /* unramified */
    1677        6552 :     if (pia) break;
    1678             :   }
    1679       29924 :   *pE = E; *ppa = pa; F = degpol(S->nu);
    1680       29924 :   if (DEBUGLEVEL>4) err_printf("  (E, F) = (%ld,%ld)\n", E, F);
    1681       29924 :   if (E * F == degpol(S->f)) return 1;
    1682       22702 :   if (loop(S, E)) return 2;
    1683       11268 :   if (!update_phi(S)) return 1;
    1684        6552 :   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       40256 : maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag)
    1691             : {
    1692       40256 :   long oE, n = lg(w)-1; /* factor of largest degree */
    1693       40256 :   GEN opa, D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
    1694       40256 :   S->pisprime = -1;
    1695       40256 :   S->p = p;
    1696       40256 :   S->mf = mf;
    1697       40256 :   S->nu = gel(w,n);
    1698       40256 :   S->df = Z_pval(D, p);
    1699       40256 :   S->pdf = powiu(p, S->df);
    1700       40256 :   S->phi = pol_x(varn(f));
    1701       40256 :   S->chi = S->f = f;
    1702       40256 :   if (n > 1) return Decomp(S, flag); /* FIXME: use bezout_lift_fact */
    1703             : 
    1704       23372 :   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       23372 :   else if (DEBUGLEVEL>2) err_printf("  entering Nilord\n");
    1708       23372 :   S->psf = S->psc = mulii(sqri(D), p);
    1709       23372 :   S->vpsf = S->vpsc = 2*S->df + 1;
    1710       23372 :   S->prc = mulii(D, p);
    1711       23372 :   S->chi = FpX_red(S->f, S->psc);
    1712       23372 :   S->pmf = powiu(p, S->mf+1);
    1713       23372 :   S->precns = NULL;
    1714       23372 :   for(opa = NULL, oE = 0;;)
    1715        6552 :   {
    1716       29924 :     long n = progress(S, &opa, &oE);
    1717       29924 :     if (n == 1) return flag? NULL: dbasis(p, S->f, S->mf, S->phi, S->chi);
    1718       17986 :     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        1743 : indexpartial(GEN T, GEN DT)
    1797             : {
    1798        1743 :   pari_sp av = avma;
    1799             :   long i, nb;
    1800        1743 :   GEN fa, E, P, res = gen_1, dT = ZX_deriv(T);
    1801             : 
    1802        1743 :   if (!DT) DT = ZX_disc(T);
    1803        1743 :   fa = absZ_factor_limit(DT, 0);
    1804        1743 :   P = gel(fa,1);
    1805        1743 :   E = gel(fa,2); nb = lg(P)-1;
    1806       14077 :   for (i = 1; i <= nb; i++)
    1807             :   {
    1808       12334 :     long e = itou(gel(E,i)), e2 = e >> 1;
    1809       12334 :     GEN p = gel(P,i), q = p;
    1810       12334 :     if (i == nb)
    1811        1729 :       q = powiu(p, (odd(e) && !BPSW_psp(p))? e2+1: e2);
    1812       10605 :     else if (e2 >= 2)
    1813        6916 :       q = ZpX_reduced_resultant_fast(T, dT, p, e2);
    1814       12334 :     res = mulii(res, q);
    1815             :   }
    1816        1743 :   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       60336 : get_norm(norm_S *S, GEN a)
    1834             : {
    1835       60336 :   if (S->M)
    1836             :   {
    1837             :     long e;
    1838       59611 :     GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
    1839       59611 :     if (e > -5) pari_err_PREC( "get_norm");
    1840       59611 :     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       19400 : init_norm(norm_S *S, GEN nf, GEN p)
    1847             : {
    1848       19400 :   GEN T = nf_get_pol(nf), M = nf_get_M(nf);
    1849       19400 :   long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
    1850             : 
    1851       19400 :   S->r1 = nf_get_r1(nf);
    1852       19400 :   if (N * ex <= prec2nbits(gprecision(M)) - 20)
    1853             :   { /* enough prec to use embed_norm */
    1854       19312 :     S->M = M;
    1855       19312 :     S->D = NULL;
    1856       19312 :     S->w = NULL;
    1857       19312 :     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       19400 : }
    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       60336 : 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      133034 : mul_intersect(GEN x, GEN y, GEN p)
    1887             : {
    1888      133034 :   if (!x) return y;
    1889       84662 :   if (!y) return x;
    1890       68538 :   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       55094 : 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       55094 :   if (typ(pr) == t_MAT) return pr;
    1901             :   /* ordinary prid (from Kummer) */
    1902       15666 :   x = pr_hnf(nf, pr);
    1903       15666 :   l = lg(x);
    1904       15666 :   y = cgetg(l, t_MAT);
    1905      176183 :   for (i=j=1; i<l; i++)
    1906      160517 :     if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
    1907       15666 :   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       19400 : get_LV(GEN nf, GEN L, GEN p, long N)
    1916             : {
    1917       19400 :   long i, l = lg(L)-1;
    1918             :   GEN LV, LW, A, B;
    1919             : 
    1920       19400 :   LV = cgetg(l+1, t_VEC);
    1921       19400 :   if (l == 1) { gel(LV,1) = matid(N); return LV; }
    1922       16124 :   LW = cgetg(l+1, t_VEC);
    1923       16124 :   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       16124 :   A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
    1927       16124 :   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       16124 :   B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
    1930       16124 :   for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
    1931       16124 :   for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
    1932       16124 :   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       40856 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
    1943             : {
    1944       40856 :   long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
    1945             :   GEN u, Mv, x, q;
    1946             : 
    1947       40856 :   f = N - m; /* we want v_p(Norm(x)) = p^f */
    1948       40856 :   q = powiu(p,f+1);
    1949             : 
    1950       40856 :   u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
    1951       40856 :   setlg(u, lg(P));
    1952       40856 :   u = centermod(ZM_ZC_mul(P, u), p);
    1953       40856 :   if (is_uniformizer(u, q, S)) return u;
    1954       13313 :   if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
    1955       11528 :     gel(u,1) = addii(gel(u,1), p); /* try u + p */
    1956             :   else
    1957        1785 :     gel(u,1) = subii(gel(u,1), p); /* try u - p */
    1958       13313 :   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        5813 :   Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
    1962        5813 :   l = lg(P);
    1963       13583 :   for (i=1; i<l; i++)
    1964             :   {
    1965       13583 :     x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
    1966       13583 :     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     1008272 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
    1979     1008272 : { return mkvec5(p, u, utoipos(e), utoipos(f), t); }
    1980             : 
    1981             : /* nf a true nf, u in Z[X]/(T); pr = p Z_K + u Z_K of ramification index e */
    1982             : GEN
    1983      953026 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
    1984             : {
    1985      953026 :   GEN t, T = nf_get_pol(nf);
    1986      953024 :   long f = degpol(u), N = degpol(T);
    1987             : 
    1988      953023 :   if (f == N)
    1989             :   { /* inert */
    1990      136493 :     u = scalarcol_shallow(p,N);
    1991      136493 :     t = gen_1;
    1992             :   }
    1993             :   else
    1994             :   {
    1995      816530 :     t = centermod(poltobasis(nf, FpX_div(T, u, p)), p);
    1996      816521 :     u = centermod(poltobasis(nf, u), p);
    1997      816508 :     if (e == 1)
    1998             :     { /* make sure v_pr(u) = 1 (automatic if e>1) */
    1999      752418 :       GEN cw, w = Q_primitive_part(nf_to_scalar_or_alg(nf, u), &cw);
    2000      752423 :       long v = cw? f - Q_pval(cw, p) * N: f;
    2001      752423 :       if (ZpX_resultant_val(T, w, p, v + 1) > v)
    2002             :       {
    2003       49819 :         GEN c = gel(u,1);
    2004       49819 :         gel(u,1) = signe(c) > 0? subii(c, p): addii(c, p);
    2005             :       }
    2006             :     }
    2007      816521 :     t = zk_multable(nf, t);
    2008             :   }
    2009      953003 :   return mk_pr(p,u,e,f,t);
    2010             : }
    2011             : 
    2012             : typedef struct {
    2013             :   GEN nf, p;
    2014             :   long I;
    2015             : } eltmod_muldata;
    2016             : 
    2017             : static GEN
    2018      209488 : sqr_mod(void *data, GEN x)
    2019             : {
    2020      209488 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2021      209488 :   return FpC_red(nfsqri(D->nf, x), D->p);
    2022             : }
    2023             : static GEN
    2024      104833 : ei_msqr_mod(void *data, GEN x)
    2025             : {
    2026      104833 :   GEN x2 = sqr_mod(data, x);
    2027      104833 :   eltmod_muldata *D = (eltmod_muldata*)data;
    2028      104833 :   return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
    2029             : }
    2030             : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
    2031             : static GEN
    2032      123944 : pow_ei_mod_p(GEN nf, long I, GEN p)
    2033             : {
    2034      123944 :   pari_sp av = avma;
    2035             :   eltmod_muldata D;
    2036      123944 :   long N = nf_get_degree(nf);
    2037      123944 :   GEN y = col_ei(N,I);
    2038      123944 :   if (I == 1) return y;
    2039      104236 :   D.nf = nf;
    2040      104236 :   D.p = p;
    2041      104236 :   D.I = I;
    2042      104236 :   y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
    2043      104236 :   return gerepileupto(av,y);
    2044             : }
    2045             : 
    2046             : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
    2047             : static GEN
    2048       19400 : pradical(GEN nf, GEN p, GEN *phi)
    2049             : {
    2050       19400 :   long i, N = nf_get_degree(nf);
    2051             :   GEN q,m,frob,rad;
    2052             : 
    2053             :   /* matrix of Frob: x->x^p over Z_K/p */
    2054       19400 :   frob = cgetg(N+1,t_MAT);
    2055       19400 :   for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
    2056             : 
    2057       19400 :   m = frob; q = p;
    2058       19400 :   while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
    2059       19400 :   rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
    2060       19400 :   for (i=1; i<=N; i++) gcoeff(frob,i,i) = subiu(gcoeff(frob,i,i), 1);
    2061       19400 :   *phi = frob; return rad;
    2062             : }
    2063             : 
    2064             : /* return powers of a: a^0, ... , a^d,  d = dim A */
    2065             : static GEN
    2066       23521 : get_powers(GEN mul, GEN p)
    2067             : {
    2068       23521 :   long i, d = lgcols(mul);
    2069       23521 :   GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
    2070             : 
    2071       23521 :   gel(P,0) = scalarcol_shallow(gen_1, d-1);
    2072       23521 :   z = gel(mul,1);
    2073      126381 :   for (i=1; i<=d; i++)
    2074             :   {
    2075      102860 :     gel(P,i) = z; /* a^i */
    2076      102860 :     if (i!=d) z = FpM_FpC_mul(mul, z, p);
    2077             :   }
    2078       23521 :   return pow;
    2079             : }
    2080             : 
    2081             : /* minimal polynomial of a in A (dim A = d).
    2082             :  * mul = multiplication table by a in A */
    2083             : static GEN
    2084       20567 : pol_min(GEN mul, GEN p)
    2085             : {
    2086       20567 :   pari_sp av = avma;
    2087       20567 :   GEN z = FpM_deplin(get_powers(mul, p), p);
    2088       20567 :   return gerepilecopy(av, RgV_to_RgX(z,0));
    2089             : }
    2090             : 
    2091             : static GEN
    2092       58090 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N, long flim)
    2093             : {
    2094             :   GEN u, t;
    2095             :   long e, f;
    2096             : 
    2097       58090 :   if (typ(P) == t_VEC)
    2098             :   { /* already done (Kummer) */
    2099       15666 :     f = pr_get_f(P);
    2100       15666 :     if (flim > 0 && f > flim) return NULL;
    2101       14854 :     if (flim == -2) return (GEN)f;
    2102       14854 :     return P;
    2103             :   }
    2104       42424 :   f = N - (lg(P)-1);
    2105       42424 :   if (flim > 0 && f > flim) return NULL;
    2106       41822 :   if (flim == -2) return (GEN)f;
    2107             :   /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
    2108             :    * so that v_P(t) = e(P/p)-1 */
    2109       41612 :   if (f == N) {
    2110         756 :     u = scalarcol_shallow(p,N);
    2111         756 :     t = gen_1;
    2112         756 :     e = 1;
    2113             :   } else {
    2114             :     GEN mt;
    2115       40856 :     u = uniformizer(nf, S, P, V, p, ramif);
    2116       40856 :     t = FpM_deplin(zk_multable(nf,u), p);
    2117       40856 :     mt = zk_multable(nf, t);
    2118       40856 :     e = ramif? 1 + ZC_nfval(t,mk_pr(p,u,0,0,mt)): 1;
    2119       40856 :     t = mt;
    2120             :   }
    2121       41612 :   return mk_pr(p,u,e,f,t);
    2122             : }
    2123             : 
    2124             : /* true nf */
    2125             : static GEN
    2126       19400 : primedec_end(GEN nf, GEN L, GEN p, long flim)
    2127             : {
    2128       19400 :   long i, j, l = lg(L), N = nf_get_degree(nf);
    2129       19400 :   GEN LV = get_LV(nf, L,p,N);
    2130       19400 :   int ramif = dvdii(nf_get_disc(nf), p);
    2131       19400 :   norm_S S; init_norm(&S, nf, p);
    2132       77133 :   for (i = j = 1; i < l; i++)
    2133             :   {
    2134       58090 :     GEN P = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N, flim);
    2135       58090 :     if (!P) continue;
    2136       56676 :     gel(L,j++) = P;
    2137       56676 :     if (flim == -1) return P;
    2138             :   }
    2139       19043 :   setlg(L, j); return L;
    2140             : }
    2141             : 
    2142             : /* prime ideal decomposition of p; if flim>0, restrict to f(P,p) <= flim
    2143             :  * if flim = -1 return only the first P
    2144             :  * if flim = -2 return only the f(P/p) in a t_VECSMALL */
    2145             : static GEN
    2146      738767 : primedec_aux(GEN nf, GEN p, long flim)
    2147             : {
    2148      738767 :   const long TYP = (flim == -2)? t_VECSMALL: t_VEC;
    2149      738767 :   GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
    2150             :   long i, k, c, iL, N;
    2151             :   int kummer;
    2152             : 
    2153      738766 :   F = FpX_factor(T, p);
    2154      738781 :   E = gel(F,2);
    2155      738781 :   F = gel(F,1);
    2156             : 
    2157      738781 :   k = lg(F); if (k == 1) errprime(p);
    2158      738781 :   if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
    2159             :   {
    2160      718051 :     L = cgetg(k, TYP);
    2161     1647660 :     for (i=1; i<k; i++)
    2162             :     {
    2163     1185572 :       GEN t = gel(F,i);
    2164     1185572 :       long f = degpol(t);
    2165     1185590 :       if (flim > 0 && f > flim) { setlg(L, i); break; }
    2166      934064 :       if (flim == -2)
    2167           0 :         L[i] = f;
    2168             :       else
    2169      934064 :         gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
    2170      934033 :       if (flim == -1) return gel(L,1);
    2171             :     }
    2172      713614 :     return L;
    2173             :   }
    2174             : 
    2175       20709 :   kummer = 0;
    2176       20709 :   g = FpXV_prod(F, p);
    2177       20709 :   h = FpX_div(T,g,p);
    2178       20709 :   f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
    2179             : 
    2180       20709 :   N = degpol(T);
    2181       20709 :   L = cgetg(N+1,TYP);
    2182       20709 :   iL = 1;
    2183       63294 :   for (i=1; i<k; i++)
    2184       43894 :     if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
    2185       15666 :     {
    2186       16975 :       GEN t = gel(F,i);
    2187       16975 :       kummer = 1;
    2188       16975 :       gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
    2189       16975 :       if (flim == -1) return gel(L,1);
    2190             :     }
    2191             :     else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
    2192       26919 :       E[i] = 0;
    2193             : 
    2194             :   /* phi matrix of x -> x^p - x in algebra Z_K/p */
    2195       19400 :   Ip = pradical(nf,p,&phi);
    2196             : 
    2197             :   /* split etale algebra Z_K / (p,Ip) */
    2198       19400 :   h = cgetg(N+1,t_VEC);
    2199       19400 :   if (kummer)
    2200             :   { /* split off Kummer factors */
    2201        6937 :     GEN mb, b = NULL;
    2202       32634 :     for (i=1; i<k; i++)
    2203       25697 :       if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
    2204        6937 :     if (!b) errprime(p);
    2205        6937 :     b = FpC_red(poltobasis(nf,b), p);
    2206        6937 :     mb = FpM_red(zk_multable(nf,b), p);
    2207             :     /* Fp-base of ideal (Ip, b) in ZK/p */
    2208        6937 :     gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
    2209             :   }
    2210             :   else
    2211       12463 :     gel(h,1) = Ip;
    2212             : 
    2213       19400 :   UN = col_ei(N, 1);
    2214       48332 :   for (c=1; c; c--)
    2215             :   { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
    2216             :        H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
    2217       28932 :     GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
    2218       28932 :     long dim, r = lg(H)-1;
    2219             : 
    2220       28932 :     M   = FpM_suppl(shallowconcat(H,UN), p);
    2221       28932 :     Mi  = FpM_inv(M, p);
    2222       28932 :     M2  = vecslice(M, r+1,N); /* M = (H|M2) invertible */
    2223       28932 :     Mi2 = rowslice(Mi,r+1,N);
    2224             :     /* FIXME: FpM_mul(,M2) could be done with vecpermute */
    2225       28932 :     phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
    2226       28932 :     mat1 = FpM_ker(phi2, p);
    2227       28932 :     dim = lg(mat1)-1; /* A2 product of 'dim' fields */
    2228       28932 :     if (dim > 1)
    2229             :     { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
    2230       20567 :       GEN R, a, mula, mul2, v = gel(mat1,2);
    2231             :       long n;
    2232             : 
    2233       20567 :       a = FpM_FpC_mul(M2,v, p); /* not a scalar */
    2234       20567 :       mula = FpM_red(zk_multable(nf,a), p);
    2235       20567 :       mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
    2236       20567 :       R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
    2237       20567 :       n = lg(R)-1;
    2238       64438 :       for (i=1; i<=n; i++)
    2239             :       {
    2240       43871 :         GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
    2241       43871 :         gel(h,c++) = FpM_image(shallowconcat(H, I), p);
    2242             :       }
    2243       20567 :       if (n == dim)
    2244       16417 :         for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
    2245             :     }
    2246             :     else /* A2 field ==> H maximal, f = N-r = dim(A2) */
    2247        8365 :       gel(L,iL++) = H;
    2248             :   }
    2249       19400 :   setlg(L, iL);
    2250       19400 :   return primedec_end(nf, L, p, flim);
    2251             : }
    2252             : 
    2253             : GEN
    2254      732454 : idealprimedec_limit_f(GEN nf, GEN p, long f)
    2255             : {
    2256      732454 :   pari_sp av = avma;
    2257             :   GEN v;
    2258      732454 :   if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
    2259      732454 :   if (f < 0) pari_err_DOMAIN("idealprimedec", "f", "<", gen_0, stoi(f));
    2260      732454 :   v = primedec_aux(checknf(nf), p, f);
    2261      732450 :   v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
    2262      732464 :   return gerepileupto(av,v);
    2263             : }
    2264             : GEN
    2265        6111 : idealprimedec_galois(GEN nf, GEN p)
    2266             : {
    2267        6111 :   pari_sp av = avma;
    2268        6111 :   GEN v = primedec_aux(checknf(nf), p, -1);
    2269        6111 :   return gerepilecopy(av,v);
    2270             : }
    2271             : GEN
    2272         203 : idealprimedec_degrees(GEN nf, GEN p)
    2273             : {
    2274         203 :   pari_sp av = avma;
    2275         203 :   GEN v = primedec_aux(checknf(nf), p, -2);
    2276         203 :   vecsmall_sort(v); return gerepileuptoleaf(av, v);
    2277             : }
    2278             : GEN
    2279      241591 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
    2280      241591 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
    2281             : GEN
    2282      164995 : idealprimedec(GEN nf, GEN p)
    2283      164995 : { return idealprimedec_limit_f(nf, p, 0); }
    2284             : GEN
    2285        1120 : nf_pV_to_prV(GEN nf, GEN P)
    2286             : {
    2287             :   long i, l;
    2288        1120 :   GEN Q = cgetg_copy(P,&l);
    2289        1120 :   if (l == 1) return Q;
    2290        1078 :   for (i = 1; i < l; i++) gel(Q,i) = idealprimedec(nf, gel(P,i));
    2291        1078 :   return shallowconcat1(Q);
    2292             : }
    2293             : 
    2294             : /* return [Fp[x]: Fp] */
    2295             : static long
    2296         490 : ffdegree(GEN x, GEN frob, GEN p)
    2297             : {
    2298         490 :   pari_sp av = avma;
    2299         490 :   long d, f = lg(frob)-1;
    2300         490 :   GEN y = x;
    2301             : 
    2302        1981 :   for (d=1; d < f; d++)
    2303             :   {
    2304        1673 :     y = FpM_FpC_mul(frob, y, p);
    2305        1673 :     if (ZV_equal(y, x)) break;
    2306             :   }
    2307         490 :   return gc_long(av,d);
    2308             : }
    2309             : 
    2310             : static GEN
    2311        8127 : lift_to_zk(GEN v, GEN c, long N)
    2312             : {
    2313        8127 :   GEN w = zerocol(N);
    2314        8127 :   long i, l = lg(c);
    2315        8127 :   for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
    2316        8127 :   return w;
    2317             : }
    2318             : 
    2319             : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
    2320             : static GEN
    2321      376079 : anti_uniformizer(GEN nf, GEN pr)
    2322             : {
    2323      376079 :   long N = nf_get_degree(nf), e = pr_get_e(pr);
    2324             :   GEN p, b, z;
    2325             : 
    2326      376073 :   if (e * pr_get_f(pr) == N) return gen_1;
    2327      104770 :   p = pr_get_p(pr);
    2328      104763 :   b = pr_get_tau(pr); /* ZM */
    2329      104762 :   if (e != 1)
    2330             :   {
    2331        2786 :     GEN q = powiu(pr_get_p(pr), e-1);
    2332        2786 :     b = ZM_Z_divexact(ZM_powu(b,e), q);
    2333             :   }
    2334             :   /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
    2335      104762 :   z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
    2336      104783 :   z = idealaddtoone_raw(nf, pr, z);
    2337      104777 :   return Z_ZC_sub(gen_1, FpC_center(FpC_red(z,p), p, shifti(p,-1)));
    2338             : }
    2339             : 
    2340             : #define mpr_TAU 1
    2341             : #define mpr_FFP 2
    2342             : #define mpr_NFP 5
    2343             : #define SMALLMODPR 4
    2344             : #define LARGEMODPR 6
    2345             : static GEN
    2346      859986 : modpr_TAU(GEN modpr)
    2347             : {
    2348      859986 :   GEN tau = gel(modpr,mpr_TAU);
    2349      859986 :   return isintzero(tau)? NULL: tau;
    2350             : }
    2351             : 
    2352             : /* prh = HNF matrix, which is identity but for the first line. Return a
    2353             :  * projector to ZK / prh ~ Z/prh[1,1] */
    2354             : GEN
    2355      381243 : dim1proj(GEN prh)
    2356             : {
    2357      381243 :   long i, N = lg(prh)-1;
    2358      381243 :   GEN ffproj = cgetg(N+1, t_VEC);
    2359      381263 :   GEN x, q = gcoeff(prh,1,1);
    2360      381263 :   gel(ffproj,1) = gen_1;
    2361      598933 :   for (i=2; i<=N; i++)
    2362             :   {
    2363      217707 :     x = gcoeff(prh,1,i);
    2364      217707 :     if (signe(x)) x = subii(q,x);
    2365      217670 :     gel(ffproj,i) = x;
    2366             :   }
    2367      381226 :   return ffproj;
    2368             : }
    2369             : 
    2370             : /* p not necessarily prime, but coprime to denom(basis) */
    2371             : GEN
    2372         203 : QXQV_to_FpM(GEN basis, GEN T, GEN p)
    2373             : {
    2374         203 :   long i, l = lg(basis), f = degpol(T);
    2375         203 :   GEN z = cgetg(l, t_MAT);
    2376        4928 :   for (i = 1; i < l; i++)
    2377             :   {
    2378        4725 :     GEN w = gel(basis,i);
    2379        4725 :     if (typ(w) == t_INT)
    2380           0 :       w = scalarcol_shallow(w, f);
    2381             :     else
    2382             :     {
    2383             :       GEN dx;
    2384        4725 :       w = Q_remove_denom(w, &dx);
    2385        4725 :       w = FpXQ_red(w, T, p);
    2386        4725 :       if (dx)
    2387             :       {
    2388           0 :         dx = Fp_inv(dx, p);
    2389           0 :         if (!equali1(dx)) w = FpX_Fp_mul(w, dx, p);
    2390             :       }
    2391        4725 :       w = RgX_to_RgC(w, f);
    2392             :     }
    2393        4725 :     gel(z,i) = w; /* w_i mod (T,p) */
    2394             :   }
    2395         203 :   return z;
    2396             : }
    2397             : 
    2398             : /* initialize reduction mod pr; if zk = 1, will only init data required to
    2399             :  * reduce *integral* element.  Realize (O_K/pr) as Fp[X] / (T), for a
    2400             :  * *monic* T; use variable vT for varn(T) */
    2401             : static GEN
    2402      396895 : modprinit(GEN nf, GEN pr, int zk, long vT)
    2403             : {
    2404      396895 :   pari_sp av = avma;
    2405             :   GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
    2406             :   long N, i, k, f;
    2407             : 
    2408      396895 :   nf = checknf(nf); checkprid(pr);
    2409      396876 :   if (vT < 0) vT = nf_get_varn(nf);
    2410      396876 :   f = pr_get_f(pr);
    2411      396876 :   N = nf_get_degree(nf);
    2412      396875 :   prh = pr_hnf(nf, pr);
    2413      396878 :   tau = zk? gen_0: anti_uniformizer(nf, pr);
    2414      396870 :   p = pr_get_p(pr);
    2415             : 
    2416      396870 :   if (f == 1)
    2417             :   {
    2418      376850 :     res = cgetg(SMALLMODPR, t_COL);
    2419      376850 :     gel(res,mpr_TAU) = tau;
    2420      376850 :     gel(res,mpr_FFP) = dim1proj(prh);
    2421      376831 :     gel(res,3) = pr; return gerepilecopy(av, res);
    2422             :   }
    2423             : 
    2424       20020 :   c = cgetg(f+1, t_VECSMALL);
    2425       20020 :   ffproj = cgetg(N+1, t_MAT);
    2426      130417 :   for (k=i=1; i<=N; i++)
    2427             :   {
    2428      110397 :     x = gcoeff(prh, i,i);
    2429      110397 :     if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
    2430             :     else
    2431       53921 :       gel(ffproj,i) = ZC_neg(gel(prh,i));
    2432             :   }
    2433       20020 :   ffproj = rowpermute(ffproj, c);
    2434       20020 :   if (! dvdii(nf_get_index(nf), p))
    2435             :   {
    2436       17066 :     GEN basis = nf_get_zkprimpart(nf), D = nf_get_zkden(nf);
    2437       17066 :     if (N == f)
    2438             :     { /* pr inert */
    2439        8554 :       T = nf_get_pol(nf);
    2440        8554 :       T = FpX_red(T,p);
    2441        8554 :       ffproj = RgV_to_RgM(basis, lg(basis)-1);
    2442             :     }
    2443             :     else
    2444             :     {
    2445        8512 :       T = RgV_RgC_mul(basis, pr_get_gen(pr));
    2446        8512 :       T = FpX_normalize(FpX_red(T,p),p);
    2447        8512 :       basis = FqV_red(vecpermute(basis,c), T, p);
    2448        8512 :       basis = RgV_to_RgM(basis, lg(basis)-1);
    2449        8512 :       ffproj = ZM_mul(basis, ffproj);
    2450             :     }
    2451       17066 :     setvarn(T, vT);
    2452       17066 :     ffproj = FpM_red(ffproj, p);
    2453       17066 :     if (!equali1(D))
    2454             :     {
    2455        2436 :       D = modii(D,p);
    2456        2436 :       if (!equali1(D)) ffproj = FpM_Fp_mul(ffproj, Fp_inv(D,p), p);
    2457             :     }
    2458             : 
    2459       17066 :     res = cgetg(SMALLMODPR+1, t_COL);
    2460       17066 :     gel(res,mpr_TAU) = tau;
    2461       17066 :     gel(res,mpr_FFP) = ffproj;
    2462       17066 :     gel(res,3) = pr;
    2463       17066 :     gel(res,4) = T; return gerepilecopy(av, res);
    2464             :   }
    2465             : 
    2466        2954 :   if (uisprime(f))
    2467             :   {
    2468        2646 :     mul = ei_multable(nf, c[2]);
    2469        2646 :     mul = vecpermute(mul, c);
    2470             :   }
    2471             :   else
    2472             :   {
    2473             :     GEN v, u, u2, frob;
    2474             :     long deg,deg1,deg2;
    2475             : 
    2476             :     /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
    2477         308 :     frob = cgetg(f+1, t_MAT);
    2478        1890 :     for (i=1; i<=f; i++)
    2479             :     {
    2480        1582 :       x = pow_ei_mod_p(nf,c[i],p);
    2481        1582 :       gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
    2482             :     }
    2483         308 :     u = col_ei(f,2); k = 2;
    2484         308 :     deg1 = ffdegree(u, frob, p);
    2485         777 :     while (deg1 < f)
    2486             :     {
    2487         161 :       k++; u2 = col_ei(f, k);
    2488         161 :       deg2 = ffdegree(u2, frob, p);
    2489         161 :       deg = ulcm(deg1,deg2);
    2490         161 :       if (deg == deg1) continue;
    2491         154 :       if (deg == deg2) { deg1 = deg2; u = u2; continue; }
    2492          21 :       u = ZC_add(u, u2);
    2493          21 :       while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
    2494          21 :       deg1 = deg;
    2495             :     }
    2496         308 :     v = lift_to_zk(u,c,N);
    2497             : 
    2498         308 :     mul = cgetg(f+1,t_MAT);
    2499         308 :     gel(mul,1) = v; /* assume w_1 = 1 */
    2500         308 :     for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
    2501             :   }
    2502             : 
    2503             :   /* Z_K/pr = Fp(v), mul = mul by v */
    2504        2954 :   mul = FpM_red(mul, p);
    2505        2954 :   mul = FpM_mul(ffproj, mul, p);
    2506             : 
    2507        2954 :   pow = get_powers(mul, p);
    2508        2954 :   T = RgV_to_RgX(FpM_deplin(pow, p), vT);
    2509        2954 :   nfproj = cgetg(f+1, t_MAT);
    2510        2954 :   for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
    2511             : 
    2512        2954 :   setlg(pow, f+1);
    2513        2954 :   ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
    2514             : 
    2515        2954 :   res = cgetg(LARGEMODPR, t_COL);
    2516        2954 :   gel(res,mpr_TAU) = tau;
    2517        2954 :   gel(res,mpr_FFP) = ffproj;
    2518        2954 :   gel(res,3) = pr;
    2519        2954 :   gel(res,4) = T;
    2520        2954 :   gel(res,mpr_NFP) = nfproj; return gerepilecopy(av, res);
    2521             : }
    2522             : 
    2523             : GEN
    2524           7 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0, -1); }
    2525             : GEN
    2526        9489 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1, -1); }
    2527             : GEN
    2528          56 : nfmodprinit0(GEN nf, GEN pr, long v) { return modprinit(nf, pr, 0, v); }
    2529             : 
    2530             : /* x may be a modpr */
    2531             : static int
    2532     1420358 : ok_modpr(GEN x)
    2533     1420358 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
    2534             : void
    2535         210 : checkmodpr(GEN x)
    2536             : {
    2537         210 :   if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
    2538         210 :   checkprid(modpr_get_pr(x));
    2539         210 : }
    2540             : GEN
    2541        3493 : get_modpr(GEN x)
    2542        3493 : { return ok_modpr(x)? x: NULL; }
    2543             : 
    2544             : int
    2545     3228845 : checkprid_i(GEN x)
    2546             : {
    2547     5904237 :   return (typ(x) == t_VEC && lg(x) == 6
    2548     2638757 :           && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
    2549     5867554 :           && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
    2550             : }
    2551             : void
    2552     2430423 : checkprid(GEN x)
    2553     2430423 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
    2554             : GEN
    2555      754397 : get_prid(GEN x)
    2556             : {
    2557      754397 :   long lx = lg(x);
    2558      754397 :   if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
    2559      754397 :   if (checkprid_i(x)) return x;
    2560      550487 :   if (ok_modpr(x)) {
    2561        2989 :     x = modpr_get_pr(x);
    2562        2989 :     if (checkprid_i(x)) return x;
    2563             :   }
    2564      547498 :   return NULL;
    2565             : }
    2566             : 
    2567             : static GEN
    2568      866168 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
    2569             : {
    2570      866168 :   GEN modpr = ok_modpr(*pr)? *pr: modprinit(nf, *pr, zk, -1);
    2571      866167 :   *T = modpr_get_T(modpr);
    2572      866158 :   *pr = modpr_get_pr(modpr);
    2573      866163 :   *p = pr_get_p(*pr); return modpr;
    2574             : }
    2575             : 
    2576             : /* Return an element of O_K which is set to x Mod T */
    2577             : GEN
    2578        4207 : modpr_genFq(GEN modpr)
    2579             : {
    2580        4207 :   switch(lg(modpr))
    2581             :   {
    2582             :     case SMALLMODPR: /* Fp */
    2583         917 :       return gen_1;
    2584             :     case LARGEMODPR:  /* painful case, p \mid index */
    2585        1442 :       return gmael(modpr,mpr_NFP, 2);
    2586             :     default: /* trivial case : p \nmid index */
    2587             :     {
    2588        1848 :       long v = varn( modpr_get_T(modpr) );
    2589        1848 :       return pol_x(v);
    2590             :     }
    2591             :   }
    2592             : }
    2593             : 
    2594             : GEN
    2595      854865 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2596      854865 :   GEN modpr = to_ff_init(nf,pr,T,p,0);
    2597      854858 :   GEN tau = modpr_TAU(modpr);
    2598      854856 :   if (!tau) gel(modpr,mpr_TAU) = anti_uniformizer(nf, *pr);
    2599      854856 :   return modpr;
    2600             : }
    2601             : GEN
    2602       11305 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
    2603       11305 :   return to_ff_init(nf,pr,T,p,1);
    2604             : }
    2605             : 
    2606             : /* assume x in 'basis' form (t_COL) */
    2607             : GEN
    2608     1294520 : zk_to_Fq(GEN x, GEN modpr)
    2609             : {
    2610     1294520 :   GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2611     1294545 :   GEN ffproj = gel(modpr,mpr_FFP);
    2612     1294545 :   GEN T = modpr_get_T(modpr);
    2613     1294560 :   return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
    2614             : }
    2615             : 
    2616             : /* REDUCTION Modulo a prime ideal */
    2617             : 
    2618             : /* nf a true nf */
    2619             : static GEN
    2620     5703070 : Rg_to_ff(GEN nf, GEN x0, GEN modpr)
    2621             : {
    2622     5703070 :   GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
    2623     5703075 :   long tx = typ(x);
    2624             : 
    2625     5703075 :   if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
    2626     5703075 :   switch(tx)
    2627             :   {
    2628     4397967 :     case t_INT: return modii(x, p);
    2629        6370 :     case t_FRAC: return Rg_to_Fp(x, p);
    2630             :     case t_POL:
    2631      169286 :       switch(lg(x))
    2632             :       {
    2633         231 :         case 2: return gen_0;
    2634       24849 :         case 3: return Rg_to_Fp(gel(x,2), p);
    2635             :       }
    2636      144206 :       x = Q_remove_denom(x, &den);
    2637      144193 :       x = poltobasis(nf, x);
    2638             :       /* content(x) and den may not be coprime */
    2639      144035 :       break;
    2640             :     case t_COL:
    2641     1129469 :       x = Q_remove_denom(x, &den);
    2642             :       /* content(x) and den are coprime */
    2643     1129469 :       if (lg(x)-1 == nf_get_degree(nf)) break;
    2644          48 :     default: pari_err_TYPE("Rg_to_ff",x);
    2645             :       return NULL;/*LCOV_EXCL_LINE*/
    2646             :   }
    2647     1273448 :   if (den)
    2648             :   {
    2649      116051 :     long v = Z_pvalrem(den, p, &den);
    2650      116051 :     if (v)
    2651             :     {
    2652        5649 :       if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
    2653             :       /* now v = valuation(true denominator of x) */
    2654        5649 :       if (v > 0)
    2655             :       {
    2656        5131 :         GEN tau = modpr_TAU(modpr);
    2657        5131 :         if (!tau) pari_err_TYPE("zk_to_ff", x0);
    2658        5131 :         x = nfmuli(nf,x, nfpow_u(nf, tau, v));
    2659        5131 :         v -= ZV_pvalrem(x, p, &x);
    2660             :       }
    2661        5649 :       if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2662        5621 :       if (v) return gen_0;
    2663        5271 :       if (is_pm1(den)) den = NULL;
    2664             :     }
    2665      115673 :     x = FpC_red(x, p);
    2666             :   }
    2667     1273070 :   x = zk_to_Fq(x, modpr);
    2668     1273124 :   if (den)
    2669             :   {
    2670      112397 :     GEN c = Fp_inv(den, p);
    2671      112397 :     x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
    2672             :   }
    2673     1273124 :   return x;
    2674             : }
    2675             : 
    2676             : GEN
    2677         210 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
    2678             : {
    2679         210 :   pari_sp av = avma;
    2680         210 :   nf = checknf(nf); checkmodpr(modpr);
    2681         210 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(Rg_to_ff(nf,x,modpr),modpr)));
    2682             : }
    2683             : 
    2684             : GEN
    2685         301 : nfmodpr(GEN nf, GEN x, GEN pr)
    2686             : {
    2687         301 :   pari_sp av = avma;
    2688             :   GEN T, p, modpr;
    2689         301 :   nf = checknf(nf);
    2690         301 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2691         294 :   if (typ(x) == t_MAT && lg(x) == 3)
    2692             :   {
    2693          21 :     GEN y, v = famat_nfvalrem(nf, x, pr, &y);
    2694          21 :     long s = signe(v);
    2695          21 :     if (s < 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
    2696          14 :     if (s > 0) return gc_const(av, gen_0);
    2697           7 :     x = FqV_factorback(nfV_to_FqV(gel(y,1), nf, modpr), gel(y,2), T, p);
    2698           7 :     return gerepileupto(av, x);
    2699             :   }
    2700         273 :   x = Rg_to_ff(nf, x, modpr);
    2701         161 :   x = Fq_to_FF(x, Tp_to_FF(T,p));
    2702         161 :   return gerepilecopy(av, x);
    2703             : }
    2704             : GEN
    2705          70 : nfmodprlift(GEN nf, GEN x, GEN pr)
    2706             : {
    2707          70 :   pari_sp av = avma;
    2708             :   GEN y, T, p, modpr;
    2709             :   long i, l, d;
    2710          70 :   nf = checknf(nf);
    2711          70 :   switch(typ(x))
    2712             :   {
    2713           7 :     case t_INT: return icopy(x);
    2714          35 :     case t_FFELT: break;
    2715             :     case t_VEC: case t_COL: case t_MAT:
    2716          28 :       y = cgetg_copy(x,&l);
    2717          28 :       for (i = 1; i < l; i++) gel(y,i) = nfmodprlift(nf,gel(x,i),pr);
    2718          28 :       return y;
    2719           0 :     default: pari_err_TYPE("nfmodprlit",x);
    2720             :   }
    2721          35 :   x = FF_to_FpXQ_i(x);
    2722          35 :   d = degpol(x);
    2723          35 :   if (d <= 0) { set_avma(av); return d? gen_0: icopy(gel(x,2)); }
    2724           7 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2725           7 :   return gerepilecopy(av, Fq_to_nf(x, modpr));
    2726             : }
    2727             : 
    2728             : /* lift A from residue field to nf */
    2729             : GEN
    2730     1346527 : Fq_to_nf(GEN A, GEN modpr)
    2731             : {
    2732             :   long dA;
    2733     1346527 :   if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
    2734        5558 :   dA = degpol(A);
    2735        5558 :   if (dA <= 0) return dA ? gen_0: gel(A,2);
    2736        2289 :   return ZM_ZX_mul(gel(modpr,mpr_NFP), A);
    2737             : }
    2738             : GEN
    2739           0 : FqV_to_nfV(GEN x, GEN modpr)
    2740           0 : { pari_APPLY_same(Fq_to_nf(gel(x,i), modpr)) }
    2741             : GEN
    2742        8232 : FqM_to_nfM(GEN A, GEN modpr)
    2743             : {
    2744        8232 :   long i,j,h,l = lg(A);
    2745        8232 :   GEN B = cgetg(l, t_MAT);
    2746             : 
    2747        8232 :   if (l == 1) return B;
    2748        7637 :   h = lgcols(A);
    2749       35028 :   for (j=1; j<l; j++)
    2750             :   {
    2751       27391 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    2752       27391 :     for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
    2753             :   }
    2754        7637 :   return B;
    2755             : }
    2756             : GEN
    2757        9086 : FqX_to_nfX(GEN A, GEN modpr)
    2758             : {
    2759             :   long i, l;
    2760             :   GEN B;
    2761             : 
    2762        9086 :   if (typ(A)!=t_POL) return icopy(A); /* scalar */
    2763        9086 :   B = cgetg_copy(A, &l); B[1] = A[1];
    2764        9086 :   for (i=2; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
    2765        9086 :   return B;
    2766             : }
    2767             : 
    2768             : /* reduce A to residue field */
    2769             : GEN
    2770     5702595 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
    2771             : {
    2772     5702595 :   pari_sp av = avma;
    2773     5702595 :   return gerepileupto(av, Rg_to_ff(checknf(nf), A, modpr));
    2774             : }
    2775             : /* A t_VEC/t_COL */
    2776             : GEN
    2777        4449 : nfV_to_FqV(GEN A, GEN nf,GEN modpr)
    2778             : {
    2779        4449 :   long i,l = lg(A);
    2780        4449 :   GEN B = cgetg(l,typ(A));
    2781        4449 :   for (i=1; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i), modpr);
    2782        4449 :   return B;
    2783             : }
    2784             : /* A  t_MAT */
    2785             : GEN
    2786        4368 : nfM_to_FqM(GEN A, GEN nf,GEN modpr)
    2787             : {
    2788        4368 :   long i,j,h,l = lg(A);
    2789        4368 :   GEN B = cgetg(l,t_MAT);
    2790             : 
    2791        4368 :   if (l == 1) return B;
    2792        4368 :   h = lgcols(A);
    2793      130466 :   for (j=1; j<l; j++)
    2794             :   {
    2795      126098 :     GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
    2796      126098 :     for (i=1; i<h; i++) gel(Bj,i) = nf_to_Fq(nf, gel(Aj,i), modpr);
    2797             :   }
    2798        4368 :   return B;
    2799             : }
    2800             : /* A t_POL */
    2801             : GEN
    2802       12887 : nfX_to_FqX(GEN A, GEN nf,GEN modpr)
    2803             : {
    2804       12887 :   long i,l = lg(A);
    2805       12887 :   GEN B = cgetg(l,t_POL); B[1] = A[1];
    2806       12887 :   for (i=2; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i),modpr);
    2807       12880 :   return normalizepol_lg(B, l);
    2808             : }
    2809             : 
    2810             : /*******************************************************************/
    2811             : /*                                                                 */
    2812             : /*                       RELATIVE ROUND 2                          */
    2813             : /*                                                                 */
    2814             : /*******************************************************************/
    2815             : /* Shallow functions */
    2816             : /* FIXME: use a bb_field and export the nfX_* routines */
    2817             : static GEN
    2818        3465 : nfX_sub(GEN nf, GEN x, GEN y)
    2819             : {
    2820        3465 :   long i, lx = lg(x), ly = lg(y);
    2821             :   GEN z;
    2822        3465 :   if (ly <= lx) {
    2823        3465 :     z = cgetg(lx,t_POL); z[1] = x[1];
    2824        3465 :     for (i=2; i < ly; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    2825        3465 :     for (   ; i < lx; i++) gel(z,i) = gel(x,i);
    2826        3465 :     z = normalizepol_lg(z, lx);
    2827             :   } else {
    2828           0 :     z = cgetg(ly,t_POL); z[1] = y[1];
    2829           0 :     for (i=2; i < lx; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
    2830           0 :     for (   ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
    2831           0 :     z = normalizepol_lg(z, ly);
    2832             :   }
    2833        3465 :   return z;
    2834             : }
    2835             : /* FIXME: quadratic multiplication */
    2836             : static GEN
    2837       55398 : nfX_mul(GEN nf, GEN a, GEN b)
    2838             : {
    2839       55398 :   long da = degpol(a), db = degpol(b), dc, lc, k;
    2840             :   GEN c;
    2841       55398 :   if (da < 0 || db < 0) return gen_0;
    2842       55398 :   dc = da + db;
    2843       55398 :   if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
    2844       55398 :   lc = dc+3;
    2845       55398 :   c = cgetg(lc, t_POL); c[1] = a[1];
    2846      443877 :   for (k = 0; k <= dc; k++)
    2847             :   {
    2848      388479 :     long i, I = minss(k, da);
    2849      388479 :     GEN d = NULL;
    2850     1315321 :     for (i = maxss(k-db, 0); i <= I; i++)
    2851             :     {
    2852      926842 :       GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
    2853      926842 :       d = d? nfadd(nf, d, e): e;
    2854             :     }
    2855      388479 :     gel(c, k+2) = d;
    2856             :   }
    2857       55398 :   return normalizepol_lg(c, lc);
    2858             : }
    2859             : /* assume b monic */
    2860             : static GEN
    2861       51933 : nfX_rem(GEN nf, GEN a, GEN b)
    2862             : {
    2863       51933 :   long da = degpol(a), db = degpol(b);
    2864       51933 :   if (da < 0) return gen_0;
    2865       51933 :   a = leafcopy(a);
    2866      178423 :   while (da >= db)
    2867             :   {
    2868       74557 :     long i, k = da;
    2869       74557 :     GEN A = gel(a, k+2);
    2870      540820 :     for (i = db-1, k--; i >= 0; i--, k--)
    2871      466263 :       gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
    2872       74557 :     a = normalizepol_lg(a, lg(a)-1);
    2873       74557 :     da = degpol(a);
    2874             :   }
    2875       51933 :   return a;
    2876             : }
    2877             : static GEN
    2878       51933 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
    2879             : {
    2880       51933 :   GEN c = nfX_mul(nf, a, b);
    2881       51933 :   if (typ(c) != t_POL) return c;
    2882       51933 :   return nfX_rem(nf, c, T);
    2883             : }
    2884             : 
    2885             : static void
    2886       10458 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
    2887             : {
    2888             :   long i;
    2889       10458 :   if (typ(Ix) == t_VEC) /* standard */
    2890        6342 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
    2891             :   else /* constant ideal */
    2892        4116 :     for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
    2893       10458 : }
    2894             : 
    2895             : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
    2896             :  * module generated by x and y. */
    2897             : static GEN
    2898        5229 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
    2899             : {
    2900        5229 :   long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
    2901        5229 :   GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
    2902        5229 :   fill(lx, H     , Hx, I     , Ix);
    2903        5229 :   fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
    2904             : }
    2905             : static GEN
    2906        1813 : rnfjoinmodules(GEN nf, GEN x, GEN y)
    2907             : {
    2908        1813 :   if (!x) return y;
    2909        1113 :   if (!y) return x;
    2910        1113 :   return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
    2911             : }
    2912             : 
    2913             : typedef struct {
    2914             :   GEN multab, T,p;
    2915             :   long h;
    2916             : } rnfeltmod_muldata;
    2917             : 
    2918             : static GEN
    2919       59955 : _sqr(void *data, GEN x)
    2920             : {
    2921       59955 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    2922      102046 :   GEN z = x? tablesqr(D->multab,x)
    2923      102046 :            : tablemul_ei_ej(D->multab,D->h,D->h);
    2924       59955 :   return FqV_red(z,D->T,D->p);
    2925             : }
    2926             : static GEN
    2927       10731 : _msqr(void *data, GEN x)
    2928             : {
    2929       10731 :   GEN x2 = _sqr(data, x), z;
    2930       10731 :   rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
    2931       10731 :   z = tablemul_ei(D->multab, x2, D->h);
    2932       10731 :   return FqV_red(z,D->T,D->p);
    2933             : }
    2934             : 
    2935             : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
    2936             : static GEN
    2937       17864 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
    2938             : {
    2939       17864 :   pari_sp av = avma;
    2940             :   GEN y;
    2941             :   rnfeltmod_muldata D;
    2942             : 
    2943       17864 :   if (!signe(n)) return gen_1;
    2944             : 
    2945       17864 :   D.multab = multab;
    2946       17864 :   D.h = h;
    2947       17864 :   D.T = T;
    2948       17864 :   D.p = p;
    2949       17864 :   y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
    2950       17864 :   return gerepilecopy(av, y);
    2951             : }
    2952             : 
    2953             : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
    2954             :  * a root, cf repres() */
    2955             : static GEN
    2956          21 : FqX_non_root(GEN P, GEN T, GEN p)
    2957             : {
    2958          21 :   long dP = degpol(P), f, vT;
    2959             :   long i, j, k, pi, pp;
    2960             :   GEN v;
    2961             : 
    2962          21 :   if (dP == 0) return gen_1;
    2963          21 :   pp = is_bigint(p) ? dP+1: itos(p);
    2964          21 :   v = cgetg(dP + 2, t_VEC);
    2965          21 :   gel(v,1) = gen_0;
    2966          21 :   if (T)
    2967           0 :   { f = degpol(T); vT = varn(T); }
    2968             :   else
    2969          21 :   { f = 1; vT = 0; }
    2970          42 :   for (i=pi=1; i<=f; i++,pi*=pp)
    2971             :   {
    2972          21 :     GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
    2973          42 :     for (j=1; j<pp; j++)
    2974             :     {
    2975          42 :       for (k=1; k<=pi; k++)
    2976             :       {
    2977          21 :         GEN z = Fq_add(gel(v,k), jgi, T,p);
    2978          21 :         if (!gequal0(FqX_eval(P, z, T,p))) return z;
    2979          21 :         gel(v, j*pi+k) = z;
    2980             :       }
    2981          21 :       if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
    2982             :     }
    2983             :   }
    2984          21 :   return NULL;
    2985             : }
    2986             : 
    2987             : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
    2988             :  * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
    2989             :  * vdisc = v_pr( disc(P) ).
    2990             :  * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
    2991             :  *   O = enlarged order, given by a pseudo-basis
    2992             :  *   flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
    2993             :  *   v = v_pr(disc(O)). */
    2994             : static GEN
    2995        3500 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
    2996             : {
    2997             :   GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
    2998             :   long m, vt, r, d, i, j, mpr;
    2999             : 
    3000        3500 :   if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
    3001        3493 :   if (vdisc == 1) return NULL; /* pr-maximal */
    3002        3493 :   if (!only_maximal && !gequal1(leading_coeff(P)))
    3003           0 :     pari_err_IMPL( "the full Dedekind criterion in the non-monic case");
    3004             :   /* either monic OR only_maximal = 1 */
    3005        3493 :   m = degpol(P);
    3006        3493 :   nfT = nf_get_pol(nf);
    3007        3493 :   modpr = nf_to_Fq_init(nf,&pr, &T, &p);
    3008        3493 :   Ppr = nfX_to_FqX(P, nf, modpr);
    3009        3486 :   mpr = degpol(Ppr);
    3010        3486 :   if (mpr < m) /* non-monic => only_maximal = 1 */
    3011             :   {
    3012          21 :     if (mpr < 0) return NULL;
    3013          21 :     if (! RgX_valrem(Ppr, &Ppr))
    3014             :     { /* non-zero constant coefficient */
    3015           0 :       Ppr = RgX_shift_shallow(RgX_recip_shallow(Ppr), m - mpr);
    3016           0 :       P = RgX_recip_shallow(P);
    3017             :     }
    3018             :     else
    3019             :     {
    3020          21 :       GEN z = FqX_non_root(Ppr, T, p);
    3021          21 :       if (!z) pari_err_IMPL( "Dedekind in the difficult case");
    3022           0 :       z = Fq_to_nf(z, modpr);
    3023           0 :       if (typ(z) == t_INT)
    3024           0 :         P = RgX_translate(P, z);
    3025             :       else
    3026           0 :         P = RgXQX_translate(P, z, T);
    3027           0 :       P = RgX_recip_shallow(P);
    3028           0 :       Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
    3029             :     }
    3030             :   }
    3031        3465 :   A = gel(FqX_factor(Ppr,T,p),1);
    3032        3465 :   r = lg(A); /* > 1 */
    3033        3465 :   g = gel(A,1);
    3034        3465 :   for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
    3035        3465 :   h = FqX_div(Ppr,g, T, p);
    3036        3465 :   gzk = FqX_to_nfX(g, modpr);
    3037        3465 :   hzk = FqX_to_nfX(h, modpr);
    3038        3465 :   k = nfX_sub(nf, P, nfX_mul(nf, gzk,hzk));
    3039        3465 :   tau = pr_get_tau(pr);
    3040        3465 :   switch(typ(tau))
    3041             :   {
    3042        1470 :     case t_INT: k = gdiv(k, p); break;
    3043        1995 :     case t_MAT: k = RgX_Rg_div(tablemulvec(NULL,tau, k), p); break;
    3044             :   }
    3045        3465 :   k = nfX_to_FqX(k, nf, modpr);
    3046        3465 :   k = FqX_normalize(FqX_gcd(FqX_gcd(g,h,  T,p), k, T,p), T,p);
    3047        3465 :   d = degpol(k);  /* <= m */
    3048        3465 :   if (!d) return NULL; /* pr-maximal */
    3049        2170 :   if (only_maximal) return gen_0; /* not maximal */
    3050             : 
    3051        2149 :   A = cgetg(m+d+1,t_MAT);
    3052        2149 :   I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
    3053             :  /* base[2] temporarily multiplied by p, for the final nfhnfmod,
    3054             :   * which requires integral ideals */
    3055        2149 :   prinvp = pr_inv_p(pr); /* again multiplied by p */
    3056       12656 :   for (j=1; j<=m; j++)
    3057             :   {
    3058       10507 :     gel(A,j) = col_ei(m, j);
    3059       10507 :     gel(I,j) = p;
    3060             :   }
    3061        2149 :   pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
    3062        4634 :   for (   ; j<=m+d; j++)
    3063             :   {
    3064        2485 :     gel(A,j) = RgX_to_RgC(pal,m);
    3065        2485 :     gel(I,j) = prinvp;
    3066        2485 :     if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
    3067             :   }
    3068             :   /* the modulus is integral */
    3069        2149 :   base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
    3070        2149 :   gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
    3071        2149 :   vt = vdisc - 2*d;
    3072        2149 :   return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
    3073             : }
    3074             : 
    3075             : /* [L:K] = n */
    3076             : static GEN
    3077         882 : triv_order(long n)
    3078             : {
    3079         882 :   GEN z = cgetg(3, t_VEC);
    3080         882 :   gel(z,1) = matid(n);
    3081         882 :   gel(z,2) = const_vec(n, gen_1); return z;
    3082             : }
    3083             : 
    3084             : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
    3085             :  * is pr-maximal (resp. not pr-maximal). */
    3086             : GEN
    3087          84 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
    3088             : {
    3089          84 :   pari_sp av = avma;
    3090             :   GEN z, dP;
    3091             :   long v;
    3092             : 
    3093          84 :   nf = checknf(nf);
    3094          84 :   P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 0);
    3095          84 :   dP = RgX_disc(P); P = lift_shallow(P);
    3096          84 :   if (!pr)
    3097             :   {
    3098          21 :     GEN fa = idealfactor(nf, dP);
    3099          21 :     GEN Q = gel(fa,1), E = gel(fa,2);
    3100          21 :     pari_sp av2 = avma;
    3101          21 :     long i, l = lg(Q);
    3102          21 :     for (i = 1; i < l; i++, set_avma(av2))
    3103             :     {
    3104          21 :       v = itos(gel(E,i));
    3105          21 :       if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
    3106           0 :       set_avma(av2);
    3107             :     }
    3108           0 :     set_avma(av); return gen_1;
    3109             :   }
    3110          63 :   else if (typ(pr) == t_VEC)
    3111             :   { /* flag = 1 is implicit */
    3112          63 :     if (lg(pr) == 1) { set_avma(av); return gen_1; }
    3113          63 :     if (typ(gel(pr,1)) == t_VEC)
    3114             :     { /* list of primes */
    3115          14 :       GEN Q = pr;
    3116          14 :       pari_sp av2 = avma;
    3117          14 :       long i, l = lg(Q);
    3118          14 :       for (i = 1; i < l; i++, set_avma(av2))
    3119             :       {
    3120          14 :         v = nfval(nf, dP, gel(Q,i));
    3121          14 :         if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
    3122             :       }
    3123           0 :       set_avma(av); return gen_1;
    3124             :     }
    3125             :   }
    3126             :   /* single prime */
    3127          49 :   v = nfval(nf, dP, pr);
    3128          49 :   z = rnfdedekind_i(nf, P, pr, v, flag);
    3129          42 :   if (z)
    3130             :   {
    3131          21 :     if (flag) { set_avma(av); return gen_0; }
    3132          14 :     z = gerepilecopy(av, z);
    3133             :   }
    3134             :   else
    3135             :   {
    3136          21 :     set_avma(av); if (flag) return gen_1;
    3137           7 :     z = cgetg(4, t_VEC);
    3138           7 :     gel(z,1) = gen_1;
    3139           7 :     gel(z,2) = triv_order(degpol(P));
    3140           7 :     gel(z,3) = stoi(v);
    3141             :   }
    3142          21 :   return z;
    3143             : }
    3144             : 
    3145             : static int
    3146       22575 : ideal_is1(GEN x) {
    3147       22575 :   switch(typ(x))
    3148             :   {
    3149        9765 :     case t_INT: return is_pm1(x);
    3150       12047 :     case t_MAT: return RgM_isidentity(x);
    3151             :   }
    3152         763 :   return 0;
    3153             : }
    3154             : 
    3155             : /* return a in ideal A such that v_pr(a) = v_pr(A) */
    3156             : static GEN
    3157       12656 : minval(GEN nf, GEN A, GEN pr)
    3158             : {
    3159       12656 :   GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
    3160       12656 :   if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
    3161       12656 :   return a;
    3162             : }
    3163             : 
    3164             : /* nf a true nf. Return NULL if power order if pr-maximal */
    3165             : static GEN
    3166        3416 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
    3167             : {
    3168        3416 :   pari_sp av = avma, av1;
    3169             :   long i, j, k, n, nn, vpol, cnt, sep;
    3170             :   GEN q, q1, p, T, modpr, W, I, p1;
    3171             :   GEN prhinv, mpi, Id;
    3172             : 
    3173        3416 :   if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
    3174        3416 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3175        3416 :   av1 = avma;
    3176        3416 :   p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
    3177        3409 :   if (!p1) return gc_NULL(av);
    3178        2135 :   if (is_pm1(gel(p1,1))) return gerepilecopy(av,gel(p1,2));
    3179         924 :   sep = itos(gel(p1,3));
    3180         924 :   W = gmael(p1,2,1);
    3181         924 :   I = gmael(p1,2,2);
    3182         924 :   gerepileall(av1, 2, &W, &I);
    3183             : 
    3184         924 :   mpi = zk_multable(nf, pr_get_gen(pr));
    3185         924 :   n = degpol(pol); nn = n*n;
    3186         924 :   vpol = varn(pol);
    3187         924 :   q1 = q = pr_norm(pr);
    3188         924 :   while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
    3189         924 :   Id = matid(n);
    3190         924 :   prhinv = pr_inv(pr);
    3191         924 :   av1 = avma;
    3192        4368 :   for(cnt=1;; cnt++)
    3193        3444 :   {
    3194        4368 :     GEN I0 = leafcopy(I), W0 = leafcopy(W);
    3195             :     GEN Wa, Winv, Ip, A, MW, MWmod, F, pseudo, C, G;
    3196        4368 :     GEN Tauinv = cgetg(n+1, t_VEC), Tau = cgetg(n+1, t_VEC);
    3197             : 
    3198        4368 :     if (DEBUGLEVEL>1) err_printf("    pass no %ld\n",cnt);
    3199       26600 :     for (j=1; j<=n; j++)
    3200             :     {
    3201             :       GEN tau, tauinv;
    3202       22232 :       if (ideal_is1(gel(I,j)))
    3203             :       {
    3204        9576 :         gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
    3205        9576 :         continue;
    3206             :       }
    3207       12656 :       gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
    3208       12656 :       gel(Tauinv,j) = tauinv = nfinv(nf, tau);
    3209       12656 :       gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
    3210       12656 :       gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
    3211             :     }
    3212             :     /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
    3213             : 
    3214             :    /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
    3215        4368 :     Wa = RgM_to_RgXV(W,vpol);
    3216        4368 :     Winv = nfM_inv(nf, W);
    3217        4368 :     MW = cgetg(nn+1, t_MAT);
    3218             :     /* W_1 = 1 */
    3219        4368 :     for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
    3220       22232 :     for (i=2; i<=n; i++)
    3221       69797 :       for (j=i; j<=n; j++)
    3222             :       {
    3223       51933 :         GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
    3224       51933 :         if (typ(z) != t_POL)
    3225           0 :           z = nfC_nf_mul(nf, gel(Winv,1), z);
    3226             :         else
    3227             :         {
    3228       51933 :           z = RgX_to_RgC(z, lg(Winv)-1);
    3229       51933 :           z = nfM_nfC_mul(nf, Winv, z);
    3230             :         }
    3231       51933 :         gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
    3232             :       }
    3233             : 
    3234             :     /* compute Ip =  pr-radical [ could use Ker(trace) if q large ] */
    3235        4368 :     MWmod = nfM_to_FqM(MW,nf,modpr);
    3236        4368 :     F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
    3237        4368 :     for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
    3238        4368 :     Ip = FqM_ker(F,T,p);
    3239        4368 :     if (lg(Ip) == 1) { W = W0; I = I0; break; }
    3240             : 
    3241             :     /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
    3242        4116 :     A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
    3243        4116 :     for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
    3244        4116 :     MW = nfM_mul(nf, nfM_inv(nf,A), MW);
    3245        4116 :     C = cgetg(n+1, t_MAT);
    3246       25095 :     for (k=1; k<=n; k++)
    3247             :     {
    3248       20979 :       GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck;
    3249       20979 :       gel(C,k) = Ck = cgetg(nn+1, t_COL);
    3250      139300 :       for (j=1; j<=n; j++)
    3251             :       {
    3252      118321 :         GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
    3253      118321 :         for (i=1; i<=n; i++) gel(Ck, (j-1)*n+i) = nf_to_Fq(nf,gel(z,i),modpr);
    3254             :       }
    3255             :     }
    3256        4116 :     G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
    3257             : 
    3258        4116 :     pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
    3259             :     /* express W in terms of the power basis */
    3260        4116 :     W = nfM_mul(nf, W, gel(pseudo,1));
    3261        4116 :     I = gel(pseudo,2);
    3262             :     /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
    3263             :      * W[i,i] = Tau[i] */
    3264       25095 :     for (j=1; j<=n; j++)
    3265       20979 :       if (gel(Tau,j) != gen_1)
    3266             :       {
    3267       11830 :         gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
    3268       11830 :         gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
    3269             :       }
    3270        4116 :     if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
    3271        4116 :     if (sep <= 3 || gequal(I,I0)) break;
    3272             : 
    3273        3444 :     if (gc_needed(av1,2))
    3274             :     {
    3275           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
    3276           0 :       gerepileall(av1,2, &W,&I);
    3277             :     }
    3278             :   }
    3279         924 :   return gerepilecopy(av, mkvec2(W, I));
    3280             : }
    3281             : 
    3282             : GEN
    3283      490826 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
    3284             : {
    3285      490826 :   switch(typ(c))
    3286             :   {
    3287      213471 :     case t_INT: case t_FRAC: return c;
    3288             :     case t_POL:
    3289        4893 :       if (lg(c) >= lg(T)) c = RgX_rem(c,T);
    3290        4893 :       break;
    3291             :     case t_POLMOD:
    3292      272455 :       if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
    3293      272154 :       c = gel(c,2);
    3294      272154 :       switch(typ(c))
    3295             :       {
    3296      236832 :         case t_POL: break;
    3297       35322 :         case t_INT: case t_FRAC: return c;
    3298           0 :         default: pari_err_TYPE(f, c);
    3299             :       }
    3300      236832 :       break;
    3301           7 :     default: pari_err_TYPE(f,c);
    3302             :   }
    3303             :   /* typ(c) = t_POL */
    3304      241725 :   if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
    3305      241718 :   switch(lg(c))
    3306             :   {
    3307       12915 :     case 2: return gen_0;
    3308             :     case 3:
    3309       21602 :       c = gel(c,2); if (is_rational_t(typ(c))) return c;
    3310           0 :       pari_err_TYPE(f,c);
    3311             :   }
    3312      207201 :   RgX_check_QX(c, f);
    3313      207187 :   return lift? c: mkpolmod(c, T);
    3314             : }
    3315             : /* check whether P is a polynomials with coeffs in number field Q[y]/(T) */
    3316             : GEN
    3317      197778 : RgX_nffix(const char *f, GEN T, GEN P, int lift)
    3318             : {
    3319      197778 :   long i, l, vT = varn(T);
    3320      197778 :   GEN Q = cgetg_copy(P, &l);
    3321      197778 :   if (typ(P) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), P);
    3322      197778 :   if (varncmp(varn(P), vT) >= 0) pari_err_PRIORITY(f, P, ">=", vT);
    3323      197764 :   Q[1] = P[1];
    3324      197764 :   for (i=2; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3325      197757 :   return normalizepol_lg(Q, l);
    3326             : }
    3327             : GEN
    3328          28 : RgV_nffix(const char *f, GEN T, GEN P, int lift)
    3329             : {
    3330             :   long i, l;
    3331          28 :   GEN Q = cgetg_copy(P, &l);
    3332          28 :   for (i=1; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
    3333          21 :   return Q;
    3334             : }
    3335             : 
    3336             : static GEN
    3337        1897 : get_d(GEN nf, GEN d)
    3338             : {
    3339        1897 :   GEN b = idealredmodpower(nf, d, 2, 100000);
    3340        1897 :   return nfmul(nf, d, nfsqr(nf,b));
    3341             : }
    3342             : 
    3343             : static GEN
    3344        3129 : pr_factorback(GEN nf, GEN fa)
    3345             : {
    3346        3129 :   GEN P = gel(fa,1), E = gel(fa,2), z = gen_1;
    3347        3129 :   long i, l = lg(P);
    3348        3129 :   for (i = 1; i < l; i++) z = idealmulpowprime(nf, z, gel(P,i), gel(E,i));
    3349        3129 :   return z;
    3350             : }
    3351             : static GEN
    3352        3129 : pr_factorback_scal(GEN nf, GEN fa)
    3353             : {
    3354        3129 :   GEN D = pr_factorback(nf,fa);
    3355        3129 :   if (typ(D) == t_MAT && RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
    3356        3129 :   return D;
    3357             : }
    3358             : 
    3359             : /* nf = base field K
    3360             :  * pol= monic polynomial in Z_K[X] defining a relative extension L = K[X]/(pol).
    3361             :  * Returns a pseudo-basis [A,I] of Z_L, set *pD to [D,d] and *pf to the
    3362             :  * index-ideal; rnf is used when lim != 0 and may be NULL */
    3363             : GEN
    3364        1848 : rnfallbase(GEN nf, GEN pol, GEN lim, GEN rnf, GEN *pD, GEN *pf, GEN *pDKP)
    3365             : {
    3366             :   long i, j, jf, l;
    3367             :   GEN fa, E, P, Ef, Pf, z, disc;
    3368             : 
    3369        1848 :   nf = checknf(nf);
    3370        1848 :   if (!gequal1(leading_coeff(pol)))
    3371           7 :     pari_err_IMPL("non-monic relative polynomials in rnfallbase");
    3372        1841 :   disc = nf_to_scalar_or_basis(nf, RgX_disc(pol));
    3373        1841 :   if (lim)
    3374             :   {
    3375             :     GEN rnfeq, zknf, dzknf, U, vU, dA, A, MB, dB, BdB, vj, B, Tabs;
    3376         273 :     GEN D = idealhnf(nf, disc);
    3377         273 :     long rU, m = nf_get_degree(nf), n = degpol(pol), N = n*m;
    3378             :     nfmaxord_t S;
    3379             : 
    3380         273 :     if (typ(lim) == t_INT)
    3381          35 :       P = ZV_union_shallow(nf_get_ramified_primes(nf),
    3382          35 :                            gel(Z_factor_limit(gcoeff(D,1,1), itou(lim)), 1));
    3383             :     else
    3384             :     {
    3385         238 :       P = cgetg_copy(lim, &l);
    3386         714 :       for (i = 1; i < l; i++)
    3387             :       {
    3388         476 :         GEN p = gel(lim,i);
    3389         476 :         if (typ(p) != t_INT) p = pr_get_p(p);
    3390         476 :         gel(P,i) = p;
    3391             :       }
    3392         238 :       P = ZV_sort_uniq(P);
    3393             :     }
    3394         273 :     if (rnf)
    3395             :     {
    3396         224 :       rnfeq = rnf_get_map(rnf);
    3397         224 :       zknf = rnf_get_nfzk(rnf);
    3398             :     }
    3399             :     else
    3400             :     {
    3401          49 :       rnfeq = nf_rnfeq(nf, pol);
    3402          49 :       zknf = nf_nfzk(nf, rnfeq);
    3403             :     }
    3404         273 :     dzknf = gel(zknf,1);
    3405         273 :     if (gequal1(dzknf)) dzknf = NULL;
    3406         273 :     Tabs = gel(rnfeq,1);
    3407         273 :     nfmaxord(&S, mkvec2(Tabs,P), 0);
    3408         273 :     B = RgXV_unscale(S.basis, S.unscale);
    3409         273 :     BdB = Q_remove_denom(B, &dB);
    3410         273 :     MB = RgXV_to_RgM(BdB, N); /* HNF */
    3411             : 
    3412         273 :     vU = cgetg(N+1, t_VEC);
    3413         273 :     vj = cgetg(N+1, t_VECSMALL);
    3414         273 :     gel(vU,1) = U = cgetg(m+1, t_MAT);
    3415         273 :     gel(U,1) = col_ei(N, 1);
    3416         273 :     A = dB? (dzknf? gdiv(dB,dzknf): dB): NULL;
    3417         273 :     if (A && gequal1(A)) A = NULL;
    3418         574 :     for (j = 2; j <= m; j++)
    3419             :     {
    3420         301 :       GEN t = gel(zknf,j);
    3421         301 :       if (A) t = ZX_Z_mul(t, A);
    3422         301 :       gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
    3423             :     }
    3424        1855 :     for (i = 2; i <= N; i++)
    3425             :     {
    3426        1582 :       GEN b = gel(BdB,i);
    3427        1582 :       gel(vU,i) = U = cgetg(m+1, t_MAT);
    3428        1582 :       gel(U,1) = hnf_solve(MB, RgX_to_RgC(b, N));
    3429        3507 :       for (j = 2; j <= m; j++)
    3430             :       {
    3431        1925 :         GEN t = ZX_rem(ZX_mul(b, gel(zknf,j)), Tabs);
    3432        1925 :         if (dzknf) t = gdiv(t, dzknf);
    3433        1925 :         gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
    3434             :       }
    3435             :     }
    3436         273 :     vj[1] = 1; U = gel(vU,1); rU = m;
    3437         707 :     for (i = j = 2; i <= N; i++)
    3438             :     {
    3439         707 :       GEN V = shallowconcat(U, gel(vU,i));
    3440         707 :       if (ZM_rank(V) != rU)
    3441             :       {
    3442         707 :         U = V; rU += m; vj[j++] = i;
    3443         707 :         if (rU == N) break;
    3444             :       }
    3445             :     }
    3446         273 :     if (dB) for(;;)
    3447         336 :     {
    3448         602 :       GEN c = gen_1, H = ZM_hnfmodid(U, dB);
    3449         602 :       long ic = 0;
    3450        5355 :       for (i = 1; i <= N; i++)
    3451        4753 :         if (cmpii(gcoeff(H,i,i), c) > 0) { c = gcoeff(H,i,i); ic = i; }
    3452         602 :       if (!ic) break;
    3453         336 :       vj[j++] = ic;
    3454         336 :       U = shallowconcat(H, gel(vU, ic));
    3455             :     }
    3456         273 :     setlg(vj, j);
    3457         273 :     B = vecpermute(B, vj);
    3458             : 
    3459         273 :     l = lg(B);
    3460         273 :     A = cgetg(l,t_MAT);
    3461        1589 :     for (j = 1; j < l; j++)
    3462             :     {
    3463        1316 :       GEN t = eltabstorel_lift(rnfeq, gel(B,j));
    3464        1316 :       gel(A,j) = Rg_to_RgC(t, n);
    3465             :     }
    3466         273 :     A = RgM_to_nfM(nf, A);
    3467         273 :     A = Q_remove_denom(A, &dA);
    3468         273 :     if (!dA)
    3469             :     { /* order is maximal */
    3470          14 :       z = triv_order(n);
    3471          14 :       if (pf) *pf = gen_1;
    3472             :     }
    3473             :     else
    3474             :     {
    3475             :       GEN fi;
    3476             :       /* the first n columns of A are probably in HNF already */
    3477         259 :       A = shallowconcat(vecslice(A,n+1,lg(A)-1), vecslice(A,1,n));
    3478         259 :       A = mkvec2(A, const_vec(l-1,gen_1));
    3479         259 :       if (DEBUGLEVEL > 2) err_printf("rnfallbase: nfhnf in dim %ld\n", l-1);
    3480         259 :       z = nfhnfmod(nf, A, nfdetint(nf,A));
    3481         259 :       gel(z,2) = gdiv(gel(z,2), dA);
    3482         259 :       fi = idealprod(nf,gel(z,2));
    3483         259 :       D = idealmul(nf, D, idealsqr(nf, fi));
    3484         259 :       if (pf) *pf = idealinv(nf, fi);
    3485             :     }
    3486         273 :     if (RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
    3487         273 :     if (pDKP) { settyp(S.dKP, t_VEC); *pDKP = S.dKP; }
    3488         273 :     *pD = mkvec2(D, get_d(nf, disc)); return z;
    3489             :   }
    3490        1568 :   pol = lift_shallow(pol);
    3491        1568 :   fa = idealfactor(nf, disc);
    3492        1568 :   P = gel(fa,1); l = lg(P); z = NULL;
    3493        1568 :   E = gel(fa,2);
    3494        1568 :   Pf = cgetg(l, t_COL);
    3495        1568 :   Ef = cgetg(l, t_COL);
    3496        5040 :   for (i = j = jf = 1; i < l; i++)
    3497             :   {
    3498        3479 :     GEN pr = gel(P,i);
    3499        3479 :     long e = itos(gel(E,i));
    3500        3479 :     if (e > 1)
    3501             :     {
    3502        2751 :       GEN vD = rnfmaxord(nf, pol, pr, e);
    3503        2744 :       if (vD)
    3504             :       {
    3505        1813 :         long ef = idealprodval(nf, gel(vD,2), pr);
    3506        1813 :         z = rnfjoinmodules(nf, z, vD);
    3507        1813 :         if (ef) { gel(Pf, jf) = pr; gel(Ef, jf++) = stoi(-ef); }
    3508        1813 :         e += 2 * ef;
    3509             :       }
    3510             :     }
    3511        3472 :     if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
    3512             :   }
    3513        1561 :   setlg(P,j);
    3514        1561 :   setlg(E,j);
    3515        1561 :   if (pDKP)
    3516             :   {
    3517        1505 :     GEN v = cgetg(j, t_VEC);
    3518        1505 :     for (i = 1; i < j; i++) gel(v,i) = pr_get_p(gel(P,i));
    3519        1505 :     *pDKP = ZV_sort_uniq(v);
    3520             :   }
    3521        1561 :   if (pf)
    3522             :   {
    3523        1505 :     setlg(Pf, jf);
    3524        1505 :     setlg(Ef, jf); *pf = pr_factorback_scal(nf, mkmat2(Pf,Ef));
    3525             :   }
    3526        1561 :   *pD = mkvec2(pr_factorback_scal(nf,fa), get_d(nf, disc));
    3527        1561 :   return z? z: triv_order(degpol(pol));
    3528             : }
    3529             : 
    3530             : static GEN
    3531         875 : RgX_to_algX(GEN nf, GEN x)
    3532             : {
    3533             :   long i, l;
    3534         875 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
    3535         875 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_alg(nf, gel(x,i));
    3536         875 :   return y;
    3537             : }
    3538             : 
    3539             : GEN
    3540         875 : nfX_to_monic(GEN nf, GEN T, GEN *pL)
    3541             : {
    3542             :   GEN lT, g, a;
    3543         875 :   long i, l = lg(T);
    3544         875 :   if (l == 2) return pol_0(varn(T));
    3545         875 :   if (l == 3) return pol_1(varn(T));
    3546         875 :   nf = checknf(nf);
    3547         875 :   T = Q_primpart(RgX_to_nfX(nf, T));
    3548         875 :   lT = leading_coeff(T); if (pL) *pL = lT;
    3549         875 :   if (isint1(T)) return T;
    3550         875 :   g = cgetg_copy(T, &l); g[1] = T[1]; a = lT;
    3551         875 :   gel(g, l-1) = gen_1;
    3552         875 :   gel(g, l-2) = gel(T,l-2);
    3553         875 :   if (l == 4) return g;
    3554         875 :   if (typ(lT) == t_INT)
    3555             :   {
    3556         861 :     gel(g, l-3) = gmul(a, gel(T,l-3));
    3557         861 :     for (i = l-4; i > 1; i--) { a = mulii(a,lT); gel(g,i) = gmul(a, gel(T,i)); }
    3558             :   }
    3559             :   else
    3560             :   {
    3561          14 :     gel(g, l-3) = nfmul(nf, a, gel(T,l-3));
    3562          35 :     for (i = l-3; i > 1; i--)
    3563             :     {
    3564          21 :       a = nfmul(nf,a,lT);
    3565          21 :       gel(g,i) = nfmul(nf, a, gel(T,i));
    3566             :     }
    3567             :   }
    3568         875 :   return RgX_to_algX(nf, g);
    3569             : }
    3570             : 
    3571             : GEN
    3572         462 : rnfdisc_factored(GEN nf, GEN pol, GEN *pd)
    3573             : {
    3574             :   long i, j, l;
    3575             :   GEN fa, E, P, disc, lim;
    3576             : 
    3577         462 :   nf = checknf(nf);
    3578         462 :   pol = rnfdisc_get_T(nf, pol, &lim);
    3579         462 :   pol = nfX_to_monic(nf, pol, NULL);
    3580             : 
    3581         462 :   disc = nf_to_scalar_or_basis(nf, RgX_disc(pol));
    3582         462 :   pol = lift_shallow(pol);
    3583         462 :   fa = idealfactor_partial(nf, disc, lim);
    3584         462 :   P = gel(fa,1); l = lg(P);
    3585         462 :   E = gel(fa,2);
    3586        1211 :   for (i = j = 1; i < l; i++)
    3587             :   {
    3588         749 :     long e = itos(gel(E,i));
    3589         749 :     GEN pr = gel(P,i);
    3590         749 :     if (e > 1)
    3591             :     {
    3592         665 :       GEN vD = rnfmaxord(nf, pol, pr, e);
    3593         665 :       if (vD) e += 2*idealprodval(nf, gel(vD,2), pr);
    3594             :     }
    3595         749 :     if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
    3596             :   }
    3597         462 :   if (pd) *pd = get_d(nf, disc);
    3598         462 :   setlg(P, j);
    3599         462 :   setlg(E, j); return fa;
    3600             : }
    3601             : GEN
    3602          63 : rnfdiscf(GEN nf, GEN pol)
    3603             : {
    3604          63 :   pari_sp av = avma;
    3605          63 :   GEN d, fa = rnfdisc_factored(nf, pol, &d);
    3606          63 :   return gerepilecopy(av, mkvec2(pr_factorback_scal(nf,fa), d));
    3607             : }
    3608             : 
    3609             : GEN
    3610          35 : gen_if_principal(GEN bnf, GEN x)
    3611             : {
    3612          35 :   pari_sp av = avma;
    3613          35 :   GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
    3614          35 :   return isintzero(z)? gc_NULL(av): z;
    3615             : }
    3616             : 
    3617             : static int
    3618          63 : is_pseudo_matrix(GEN O)
    3619             : {
    3620         126 :   return (typ(O) ==t_VEC && lg(O) >= 3
    3621          63 :           && typ(gel(O,1)) == t_MAT
    3622          63 :           && typ(gel(O,2)) == t_VEC
    3623         126 :           && lgcols(O) == lg(gel(O,2)));
    3624             : }
    3625             : 
    3626             : /* given bnf and a pseudo-basis of an order in HNF [A,I], tries to simplify
    3627             :  * the HNF as much as possible. The resulting matrix will be upper triangular
    3628             :  * but the diagonal coefficients will not be equal to 1. The ideals are
    3629             :  * guaranteed to be integral and primitive. */
    3630             : GEN
    3631           0 : rnfsimplifybasis(GEN bnf, GEN x)
    3632             : {
    3633           0 :   pari_sp av = avma;
    3634             :   long i, l;
    3635             :   GEN y, Az, Iz, nf, A, I;
    3636             : 
    3637           0 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3638           0 :   if (!is_pseudo_matrix(x)) pari_err_TYPE("rnfsimplifybasis",x);
    3639           0 :   A = gel(x,1);
    3640           0 :   I = gel(x,2); l = lg(I);
    3641           0 :   y = cgetg(3, t_VEC);
    3642           0 :   Az = cgetg(l, t_MAT); gel(y,1) = Az;
    3643           0 :   Iz = cgetg(l, t_VEC); gel(y,2) = Iz;
    3644           0 :   for (i = 1; i < l; i++)
    3645             :   {
    3646             :     GEN c, d;
    3647           0 :     if (ideal_is1(gel(I,i))) {
    3648           0 :       gel(Iz,i) = gen_1;
    3649           0 :       gel(Az,i) = gel(A,i);
    3650           0 :       continue;
    3651             :     }
    3652             : 
    3653           0 :     gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
    3654           0 :     gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
    3655           0 :     if (c && ideal_is1(gel(Iz,i))) continue;
    3656             : 
    3657           0 :     d = gen_if_principal(bnf, gel(Iz,i));
    3658           0 :     if (d)
    3659             :     {
    3660           0 :       gel(Iz,i) = gen_1;
    3661           0 :       gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
    3662             :     }
    3663             :   }
    3664           0 :   return gerepilecopy(av, y);
    3665             : }
    3666             : 
    3667             : static GEN
    3668          70 : get_order(GEN nf, GEN O, const char *s)
    3669             : {
    3670          70 :   if (typ(O) == t_POL)
    3671           7 :     return rnfpseudobasis(nf, O);
    3672          63 :   if (!is_pseudo_matrix(O)) pari_err_TYPE(s, O);
    3673          63 :   return O;
    3674             : }
    3675             : 
    3676             : GEN
    3677          14 : rnfdet(GEN nf, GEN order)
    3678             : {
    3679          14 :   pari_sp av = avma;
    3680             :   GEN A, I, D;
    3681          14 :   nf = checknf(nf);
    3682          14 :   order = get_order(nf, order, "rnfdet");
    3683          14 :   A = gel(order,1);
    3684          14 :   I = gel(order,2);
    3685          14 :   D = idealmul(nf, nfM_det(nf,A), idealprod(nf,I));
    3686          14 :   return gerepileupto(av, D);
    3687             : }
    3688             : 
    3689             : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
    3690             :    t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
    3691             : static void
    3692          63 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
    3693             : {
    3694             :   GEN x, uv, y, da, db;
    3695             : 
    3696          63 :   a = idealinv(nf,a);
    3697          63 :   a = Q_remove_denom(a, &da);
    3698          63 :   b = Q_remove_denom(b, &db);
    3699          63 :   x = idealcoprime(nf,a,b);
    3700          63 :   uv = idealaddtoone(nf, idealmul(nf,x,a), b);
    3701          63 :   y = gel(uv,2);
    3702          63 :   if (da) x = gmul(x,da);
    3703          63 :   if (db) y = gdiv(y,db);
    3704          63 :   *px = x;
    3705          63 :   *py = y;
    3706          63 :   *pz = db ? negi(db): gen_m1;
    3707          63 :   *pt = nfdiv(nf, gel(uv,1), x);
    3708          63 : }
    3709             : 
    3710             : /* given a pseudo-basis of an order in HNF [A,I] (or [A,I,D,d]), gives an
    3711             :  * n x n matrix (not in HNF) of a pseudo-basis and an ideal vector
    3712             :  * [1,1,...,1,I] such that order = Z_K^(n-1) x I.
    3713             :  * Uses the approximation theorem ==> slow. */
    3714             : GEN
    3715          28 : rnfsteinitz(GEN nf, GEN order)
    3716             : {
    3717          28 :   pari_sp av = avma;
    3718             :   long i, n, l;
    3719             :   GEN A, I, p1;
    3720             : 
    3721          28 :   nf = checknf(nf);
    3722          28 :   order = get_order(nf, order, "rnfsteinitz");
    3723          28 :   A = RgM_to_nfM(nf, gel(order,1));
    3724          28 :   I = leafcopy(gel(order,2)); n=lg(A)-1;
    3725         189 :   for (i=1; i<n; i++)
    3726             :   {
    3727         161 :     GEN c1, c2, b, a = gel(I,i);
    3728         161 :     gel(I,i) = gen_1;
    3729         161 :     if (ideal_is1(a)) continue;
    3730             : 
    3731          63 :     c1 = gel(A,i);
    3732          63 :     c2 = gel(A,i+1);
    3733          63 :     b = gel(I,i+1);
    3734          63 :     if (ideal_is1(b))
    3735             :     {
    3736           0 :       gel(A,i) = c2;
    3737           0 :       gel(A,i+1) = gneg(c1);
    3738           0 :       gel(I,i+1) = a;
    3739             :     }
    3740             :     else
    3741             :     {
    3742          63 :       pari_sp av2 = avma;
    3743             :       GEN x, y, z, t;
    3744          63 :       nfidealdet1(nf,a,b, &x,&y,&z,&t);
    3745          63 :       x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
    3746          63 :       y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
    3747          63 :       gerepileall(av2, 2, &x,&y);
    3748          63 :       gel(A,i) = x;
    3749          63 :       gel(A,i+1) = y;
    3750          63 :       gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &p1);
    3751          63 :       if (p1) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), p1);
    3752             :     }
    3753             :   }
    3754          28 :   l = lg(order);
    3755          28 :   p1 = cgetg(l,t_VEC);
    3756          28 :   gel(p1,1) = A;
    3757          28 :   gel(p1,2) = I; for (i=3; i<l; i++) gel(p1,i) = gel(order,i);
    3758          28 :   return gerepilecopy(av, p1);
    3759             : }
    3760             : 
    3761             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3762             :  * and outputs a basis if it is free, an n+1-generating set if it is not */
    3763             : GEN
    3764          21 : rnfbasis(GEN bnf, GEN order)
    3765             : {
    3766          21 :   pari_sp av = avma;
    3767             :   long j, n;
    3768             :   GEN nf, A, I, cl, col, a;
    3769             : 
    3770          21 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3771          21 :   order = get_order(nf, order, "rnfbasis");
    3772          21 :   I = gel(order,2); n = lg(I)-1;
    3773          21 :   j=1; while (j<n && ideal_is1(gel(I,j))) j++;
    3774          21 :   if (j<n)
    3775             :   {
    3776           7 :     order = rnfsteinitz(nf,order);
    3777           7 :     I = gel(order,2);
    3778             :   }
    3779          21 :   A = gel(order,1);
    3780          21 :   col= gel(A,n); A = vecslice(A, 1, n-1);
    3781          21 :   cl = gel(I,n);
    3782          21 :   a = gen_if_principal(bnf, cl);
    3783          21 :   if (!a)
    3784             :   {
    3785           7 :     GEN v = idealtwoelt(nf, cl);
    3786           7 :     A = shallowconcat(A, gmul(gel(v,1), col));
    3787           7 :     a = gel(v,2);
    3788             :   }
    3789          21 :   A = shallowconcat(A, nfC_nf_mul(nf, col, a));
    3790          21 :   return gerepilecopy(av, A);
    3791             : }
    3792             : 
    3793             : /* Given bnf and either an order as output by rnfpseudobasis or a polynomial,
    3794             :  * and outputs a basis (not pseudo) in Hermite Normal Form if it exists, zero
    3795             :  * if not
    3796             :  */
    3797             : GEN
    3798           7 : rnfhnfbasis(GEN bnf, GEN order)
    3799             : {
    3800           7 :   pari_sp av = avma;
    3801             :   long j, n;
    3802             :   GEN nf, A, I, a;
    3803             : 
    3804           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3805           7 :   order = get_order(nf, order, "rnfbasis");
    3806           7 :   A = gel(order,1); A = RgM_shallowcopy(A);
    3807           7 :   I = gel(order,2); n = lg(A)-1;
    3808          42 :   for (j=1; j<=n; j++)
    3809             :   {
    3810          35 :     if (ideal_is1(gel(I,j))) continue;
    3811          14 :     a = gen_if_principal(bnf, gel(I,j));
    3812          14 :     if (!a) { set_avma(av); return gen_0; }
    3813          14 :     gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
    3814             :   }
    3815           7 :   return gerepilecopy(av,A);
    3816             : }
    3817             : 
    3818             : static long
    3819           7 : rnfisfree_aux(GEN bnf, GEN order)
    3820             : {
    3821             :   long n, j;
    3822             :   GEN nf, P, I;
    3823             : 
    3824           7 :   bnf = checkbnf(bnf);
    3825           7 :   if (is_pm1( bnf_get_no(bnf) )) return 1;
    3826           0 :   nf = bnf_get_nf(bnf);
    3827           0 :   order = get_order(nf, order, "rnfisfree");
    3828           0 :   I = gel(order,2); n = lg(I)-1;
    3829           0 :   j=1; while (j<=n && ideal_is1(gel(I,j))) j++;
    3830           0 :   if (j>n) return 1;
    3831             : 
    3832           0 :   P = gel(I,j);
    3833           0 :   for (j++; j<=n; j++)
    3834           0 :     if (!ideal_is1(gel(I,j))) P = idealmul(nf,P,gel(I,j));
    3835           0 :   return gequal0( isprincipal(bnf,P) );
    3836             : }
    3837             : 
    3838             : long
    3839           7 : rnfisfree(GEN bnf, GEN order)
    3840           7 : { pari_sp av = avma; return gc_long(av, rnfisfree_aux(bnf,order)); }
    3841             : 
    3842             : /**********************************************************************/
    3843             : /**                                                                  **/
    3844             : /**                   COMPOSITUM OF TWO NUMBER FIELDS                **/
    3845             : /**                                                                  **/
    3846             : /**********************************************************************/
    3847             : static GEN
    3848        2296 : compositum_fix(GEN nf, GEN A)
    3849             : {
    3850             :   int ok;
    3851        2296 :   if (nf)
    3852             :   {
    3853         784 :     A = Q_primpart(liftpol_shallow(A)); RgX_check_ZXX(A,"polcompositum");
    3854         784 :     ok = nfissquarefree(nf,A);
    3855             :   }
    3856             :   else
    3857             :   {
    3858        1512 :     A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
    3859        1512 :     ok = ZX_is_squarefree(A);
    3860             :   }
    3861        2296 :   if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
    3862        2289 :   return A;
    3863             : }
    3864             : #define next_lambda(a) (a>0 ? -a : 1-a)
    3865             : 
    3866             : static long
    3867         399 : nfcompositum_lambda(GEN nf, GEN A, GEN B, long lambda)
    3868             : {
    3869         399 :   pari_sp av = avma;
    3870             :   forprime_t S;
    3871         399 :   GEN T = nf_get_pol(nf);
    3872         399 :   long vT = varn(T);
    3873             :   ulong p;
    3874         399 :   init_modular_big(&S);
    3875         399 :   p = u_forprime_next(&S);
    3876             :   while (1)
    3877          14 :   {
    3878             :     GEN Hp, Tp, a;
    3879         413 :     if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
    3880         413 :     a = ZXX_to_FlxX(RgX_rescale(A, stoi(-lambda)), p, vT);
    3881         413 :     Tp = ZX_to_Flx(T, p);
    3882         413 :     Hp = FlxqX_direct_compositum(a, ZXX_to_FlxX(B, p, vT), Tp, p);
    3883         413 :     if (!FlxqX_is_squarefree(Hp, Tp, p))
    3884          14 :       { lambda = next_lambda(lambda); continue; }
    3885         399 :     if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
    3886         399 :     return gc_long(av, lambda);
    3887             :   }
    3888             : }
    3889             : 
    3890             : /* modular version */
    3891             : GEN
    3892        1225 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
    3893             : {
    3894        1225 :   pari_sp av = avma;
    3895             :   int same;
    3896             :   long v, k;
    3897             :   GEN C, D, LPRS;
    3898             : 
    3899        1225 :   if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
    3900        1225 :   if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
    3901        1225 :   if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
    3902        1225 :   v = varn(A);
    3903        1225 :   if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
    3904        1225 :   if (nf)
    3905             :   {
    3906         441 :     nf = checknf(nf);
    3907         434 :     if (varncmp(v,nf_get_varn(nf))>=0) pari_err_PRIORITY("polcompositum", nf, ">=",  v);
    3908             :   }
    3909        1183 :   same = (A == B || RgX_equal(A,B));
    3910        1183 :   A = compositum_fix(nf,A);
    3911        1176 :   B = same ? A: compositum_fix(nf,B);
    3912             : 
    3913        1176 :   D = LPRS = NULL; /* -Wall */
    3914        1176 :   k = same? -1: 1;
    3915        1176 :   if (nf)
    3916             :   {
    3917         399 :     long v0 = fetch_var();
    3918             :     GEN q;
    3919         399 :     GEN T = nf_get_pol(nf);
    3920         399 :     k = nfcompositum_lambda(nf, liftpol(A), liftpol(B), k);
    3921         399 :     if (flag&1)
    3922             :     {
    3923             :       GEN H0, H1;
    3924         182 :       GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
    3925         182 :       GEN B1 = poleval(QXQX_to_mod_shallow(B, T), chgvar);
    3926         182 :       C = RgX_resultant_all(QXQX_to_mod_shallow(A, T), B1, &q);
    3927         182 :       C = gsubst(C,v0,pol_x(v));
    3928         182 :       C = lift_if_rational(C);
    3929         182 :       H0 = gsubst(gel(q,2),v0,pol_x(v));
    3930         182 :       H1 = gsubst(gel(q,3),v0,pol_x(v));
    3931         182 :       if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
    3932         182 :       if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
    3933         182 :       H0 = lift_if_rational(H0);
    3934         182 :       H1 = lift_if_rational(H1);
    3935         182 :       LPRS = mkvec2(H0,H1);
    3936             :     } else
    3937             :     {
    3938         217 :       C = nf_direct_compositum(nf, RgX_rescale(liftpol(A),stoi(-k)), liftpol(B));
    3939         217 :       setvarn(C, v); C = QXQX_to_mod_shallow(C, T);
    3940             :     }
    3941             :   }
    3942             :   else
    3943             :   {
    3944         777 :     B = leafcopy(B); setvarn(B,fetch_var_higher());
    3945        1554 :     C = (flag&1)? ZX_ZXY_resultant_all(A, B, &k, &LPRS)
    3946         777 :                 : ZX_compositum(A, B, &k);
    3947         777 :     setvarn(C, v);
    3948             :   }
    3949             :   /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
    3950        1176 :   if (flag & 2)
    3951         546 :     C = mkvec(C);
    3952             :   else
    3953             :   {
    3954         630 :     if (same)
    3955             :     {
    3956          49 :       D = RgX_rescale(A, stoi(1 - k));
    3957          49 :       if (nf) D = QXQX_to_mod_shallow(D, nf_get_pol(nf));
    3958          49 :       C = RgX_div(C, D);
    3959          49 :       if (degpol(C) <= 0)
    3960           0 :         C = mkvec(D);
    3961             :       else
    3962          49 :         C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
    3963             :     }
    3964             :     else
    3965         581 :       C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
    3966             :   }
    3967        1176 :   gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
    3968        1176 :   if (flag&1)
    3969             :   { /* a,b,c root of A,B,C = compositum, c = b - k a */
    3970         777 :     long i, l = lg(C);
    3971         777 :     GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
    3972         777 :     setvarn(mH0,v);
    3973         777 :     setvarn(H1,v);
    3974        1575 :     for (i=1; i<l; i++)
    3975             :     {
    3976         798 :       GEN D = gel(C,i);
    3977         798 :       a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
    3978         798 :       b = gadd(pol_x(v), gmulsg(k,a));
    3979         798 :       if (degpol(D) == 1) b = RgX_rem(b,D);
    3980         798 :       gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
    3981             :     }
    3982             :   }
    3983        1176 :   (void)delete_var();
    3984        1176 :   settyp(C, t_VEC);
    3985        1176 :   if (flag&2) C = gel(C,1);
    3986        1176 :   return gerepilecopy(av, C);
    3987             : }
    3988             : GEN
    3989         784 : polcompositum0(GEN A, GEN B, long flag)
    3990         784 : { return nfcompositum(NULL,A,B,flag); }
    3991             : 
    3992             : GEN
    3993          42 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
    3994             : GEN
    3995         497 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }

Generated by: LCOV version 1.13