Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 26712-590d837a1c) Lines: 2159 2280 94.7 %
Date: 2021-06-22 07:13:04 Functions: 167 171 97.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13