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

Generated by: LCOV version 1.13