Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23712-7b25a218b) Lines: 2148 2268 94.7 %
Date: 2019-03-24 05:44:59 Functions: 169 173 97.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13