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

Generated by: LCOV version 1.11