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 23332-367b47754) Lines: 2115 2235 94.6 %
Date: 2018-12-10 05:41:52 Functions: 167 171 97.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13