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 - nffactor.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20443-183d202) Lines: 1110 1207 92.0 %
Date: 2017-03-27 05:17:48 Functions: 65 69 94.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2004  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             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*            POLYNOMIAL FACTORIZATION IN A NUMBER FIELD           */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : static GEN nfsqff(GEN nf,GEN pol,long fl,GEN den);
      23             : static int nfsqff_use_Trager(long n, long dpol);
      24             : 
      25             : enum { FACTORS = 0, ROOTS, ROOTS_SPLIT };
      26             : 
      27             : /* for nf_bestlift: reconstruction of algebraic integers known mod P^k,
      28             :  * P maximal ideal above p */
      29             : typedef struct {
      30             :   long k;    /* input known mod P^k */
      31             :   GEN p, pk; /* p^k */
      32             :   GEN den;   /* denom(prk^-1) = p^k [ assume pr unramified ] */
      33             :   GEN prk;   /* |.|^2 LLL-reduced basis (b_i) of P^k  (NOT T2-reduced) */
      34             :   GEN prkHNF;/* HNF basis of P^k */
      35             :   GEN iprk;  /* den * prk^-1 */
      36             :   GEN GSmin; /* min |b_i^*|^2 */
      37             : 
      38             :   GEN Tp; /* Tpk mod p */
      39             :   GEN Tpk;
      40             :   GEN ZqProj;/* projector to Zp / P^k = Z/p^k[X] / Tpk */
      41             : 
      42             :   GEN tozk;
      43             :   GEN topow;
      44             :   GEN topowden; /* topow x / topowden = basistoalg(x) */
      45             :   GEN dn; /* NULL (we trust nf.zk) or a t_INT > 1 (an alg. integer has
      46             :              denominator dividing dn, when expressed on nf.zk */
      47             : } nflift_t;
      48             : 
      49             : typedef struct
      50             : {
      51             :   nflift_t *L;
      52             :   GEN nf;
      53             :   GEN pol, polbase; /* leading coeff is a t_INT */
      54             :   GEN fact;
      55             :   GEN pr;
      56             :   GEN Br, bound, ZC, BS_2;
      57             : } nfcmbf_t;
      58             : 
      59             : /*******************************************************************/
      60             : /*              RATIONAL RECONSTRUCTION (use ratlift)              */
      61             : /*******************************************************************/
      62             : /* NOT stack clean. a, b stay on the stack */
      63             : static GEN
      64     2478005 : lift_to_frac(GEN t, GEN mod, GEN amax, GEN bmax, GEN denom)
      65             : {
      66             :   GEN a, b;
      67     2478005 :   if (signe(t) < 0) t = addii(t, mod); /* in case t is a centerlift */
      68     2478005 :   if (!Fp_ratlift(t, mod, amax,bmax, &a,&b)
      69     2471600 :      || (denom && !dvdii(denom,b))
      70     2470600 :      || !is_pm1(gcdii(a,b))) return NULL;
      71     2470520 :   if (is_pm1(b)) { cgiv(b); return a; }
      72     1097505 :   return mkfrac(a, b);
      73             : }
      74             : 
      75             : /* Compute rational lifting for all the components of M modulo mod.
      76             :  * Assume all Fp_ratlift preconditions are met; we allow centerlifts but in
      77             :  * that case are no longer stack clean. If one component fails, return NULL.
      78             :  * If denom != NULL, check that the denominators divide denom.
      79             :  *
      80             :  * We suppose gcd(mod, denom) = 1, then a and b are coprime; so we can use
      81             :  * mkfrac rather than gdiv */
      82             : GEN
      83       84160 : FpM_ratlift(GEN M, GEN mod, GEN amax, GEN bmax, GEN denom)
      84             : {
      85       84160 :   pari_sp av = avma;
      86       84160 :   long i, j, h, l = lg(M);
      87       84160 :   GEN a, N = cgetg_copy(M, &l);
      88       84160 :   if (l == 1) return N;
      89       53155 :   h = lgcols(M);
      90      144325 :   for (j = 1; j < l; ++j)
      91             :   {
      92       95170 :     gel(N,j) = cgetg(h, t_COL);
      93     1641375 :     for (i = 1; i < h; ++i)
      94             :     {
      95     1550205 :       a = lift_to_frac(gcoeff(M,i,j), mod, amax,bmax,denom);
      96     1550205 :       if (!a) { avma = av; return NULL; }
      97     1546205 :       gcoeff(N,i,j) = a;
      98             :     }
      99             :   }
     100       49155 :   return N;
     101             : }
     102             : GEN
     103      302425 : FpC_ratlift(GEN P, GEN mod, GEN amax, GEN bmax, GEN denom)
     104             : {
     105      302425 :   pari_sp ltop = avma;
     106             :   long j, l;
     107      302425 :   GEN a, Q = cgetg_copy(P, &l);
     108     1220715 :   for (j = 1; j < l; ++j)
     109             :   {
     110      918290 :     a = lift_to_frac(gel(P,j), mod, amax,bmax,denom);
     111      918290 :     if (!a) { avma = ltop; return NULL; }
     112      918290 :     gel(Q,j) = a;
     113             :   }
     114      302425 :   return Q;
     115             : }
     116             : GEN
     117        4285 : FpX_ratlift(GEN P, GEN mod, GEN amax, GEN bmax, GEN denom)
     118             : {
     119        4285 :   pari_sp ltop = avma;
     120             :   long j, l;
     121        4285 :   GEN a, Q = cgetg_copy(P, &l);
     122        4285 :   Q[1] = P[1];
     123       10310 :   for (j = 2; j < l; ++j)
     124             :   {
     125        9510 :     a = lift_to_frac(gel(P,j), mod, amax,bmax,denom);
     126        9510 :     if (!a) { avma = ltop; return NULL; }
     127        6025 :     gel(Q,j) = a;
     128             :   }
     129         800 :   return Q;
     130             : }
     131             : 
     132             : /*******************************************************************/
     133             : /*              GCD in K[X], K NUMBER FIELD                        */
     134             : /*******************************************************************/
     135             : /* P,Q in Z[X,Y], T in Z[Y] irreducible. compute GCD in Q[Y]/(T)[X].
     136             :  *
     137             :  * M. Encarnacion "On a modular Algorithm for computing GCDs of polynomials
     138             :  * over number fields" (ISSAC'94).
     139             :  *
     140             :  * We procede as follows
     141             :  *  1:compute the gcd modulo primes discarding bad primes as they are detected.
     142             :  *  2:reconstruct the result via FpM_ratlift, stoping as soon as we get weird
     143             :  *    denominators.
     144             :  *  3:if FpM_ratlift succeeds, try the full division.
     145             :  * Suppose accuracy is insufficient to get the result right: FpM_ratlift will
     146             :  * rarely succeed, and even if it does the polynomial we get has sensible
     147             :  * coefficients, so the full division will not be too costly.
     148             :  *
     149             :  * If not NULL, den must be a multiple of the denominator of the gcd,
     150             :  * for example the discriminant of T.
     151             :  *
     152             :  * NOTE: if T is not irreducible, nfgcd may loop forever, esp. if gcd | T */
     153             : GEN
     154        2125 : nfgcd_all(GEN P, GEN Q, GEN T, GEN den, GEN *Pnew)
     155             : {
     156        2125 :   pari_sp btop, ltop = avma;
     157        2125 :   GEN lP, lQ, M, dsol, R, bo, sol, mod = NULL;
     158        2125 :   long vP = varn(P), vT = varn(T), dT = degpol(T), dM = 0, dR;
     159             :   forprime_t S;
     160             : 
     161        2125 :   if (!signe(P)) { if (Pnew) *Pnew = pol_0(vT); return gcopy(Q); }
     162        2125 :   if (!signe(Q)) { if (Pnew) *Pnew = pol_1(vT);   return gcopy(P); }
     163             :   /*Compute denominators*/
     164        2125 :   if (!den) den = ZX_disc(T);
     165        2125 :   lP = leading_coeff(P);
     166        2125 :   lQ = leading_coeff(Q);
     167        2125 :   if ( !((typ(lP)==t_INT && is_pm1(lP)) || (typ(lQ)==t_INT && is_pm1(lQ))) )
     168         515 :     den = mulii(den, gcdii(ZX_resultant(lP, T), ZX_resultant(lQ, T)));
     169             : 
     170        2125 :   init_modular_small(&S);
     171        2125 :   btop = avma;
     172             :   for(;;)
     173             :   {
     174        2815 :     ulong p = u_forprime_next(&S);
     175        2815 :     if (!p) pari_err_OVERFLOW("nfgcd [ran out of primes]");
     176             :     /*Discard primes dividing disc(T) or lc(PQ) */
     177        2815 :     if (!umodiu(den, p)) continue;
     178        2815 :     if (DEBUGLEVEL>5) err_printf("nfgcd: p=%lu\n",p);
     179             :     /*Discard primes when modular gcd does not exist*/
     180        2815 :     if ((R = FlxqX_safegcd(ZXX_to_FlxX(P,p,vT),
     181             :                            ZXX_to_FlxX(Q,p,vT),
     182           0 :                            ZX_to_Flx(T,p), p)) == NULL) continue;
     183        2815 :     dR = degpol(R);
     184        2815 :     if (dR == 0) { avma = ltop; if (Pnew) *Pnew = P; return pol_1(vP); }
     185        1125 :     if (mod && dR > dM) continue; /* p divides Res(P/gcd, Q/gcd). Discard. */
     186             : 
     187        1125 :     R = FlxX_to_Flm(R, dT);
     188             :     /* previous primes divided Res(P/gcd, Q/gcd)? Discard them. */
     189        1125 :     if (!mod || dR < dM) { M = ZM_init_CRT(R, p); mod = utoipos(p); dM = dR; continue; }
     190         690 :     (void)ZM_incremental_CRT(&M,R, &mod,p);
     191         690 :     if (gc_needed(btop, 1))
     192             :     {
     193           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"nfgcd");
     194           0 :       gerepileall(btop, 2, &M, &mod);
     195             :     }
     196             :     /* I suspect it must be better to take amax > bmax*/
     197         690 :     bo = sqrti(shifti(mod, -1));
     198         690 :     if ((sol = FpM_ratlift(M, mod, bo, bo, den)) == NULL) continue;
     199         435 :     sol = RgM_to_RgXX(sol,vP,vT);
     200         435 :     dsol = Q_primpart(sol);
     201             : 
     202         435 :     if (!ZXQX_dvd(Q, dsol, T)) continue;
     203         435 :     if (Pnew)
     204             :     {
     205          85 :       *Pnew = RgXQX_pseudodivrem(P, dsol, T, &R);
     206          85 :       if (signe(R)) continue;
     207             :     }
     208             :     else
     209             :     {
     210         350 :       if (!ZXQX_dvd(P, dsol, T)) continue;
     211             :     }
     212         435 :     gerepileall(ltop, Pnew? 2: 1, &dsol, Pnew);
     213         435 :     return dsol; /* both remainders are 0 */
     214         690 :   }
     215             : }
     216             : GEN
     217         925 : nfgcd(GEN P, GEN Q, GEN T, GEN den)
     218         925 : { return nfgcd_all(P,Q,T,den,NULL); }
     219             : 
     220             : int
     221        1035 : nfissquarefree(GEN nf, GEN x)
     222             : {
     223        1035 :   pari_sp av = avma;
     224        1035 :   GEN g, y = RgX_deriv(x);
     225        1035 :   if (RgX_is_rational(x))
     226         480 :     g = QX_gcd(x, y);
     227             :   else
     228             :   {
     229         555 :     GEN T = get_nfpol(nf,&nf);
     230         555 :     x = Q_primpart( liftpol_shallow(x) );
     231         555 :     y = Q_primpart( liftpol_shallow(y) );
     232         555 :     g = nfgcd(x, y, T, nf? nf_get_index(nf): NULL);
     233             :   }
     234        1035 :   avma = av; return (degpol(g) == 0);
     235             : }
     236             : 
     237             : /*******************************************************************/
     238             : /*             FACTOR OVER (Z_K/pr)[X] --> FqX_factor              */
     239             : /*******************************************************************/
     240             : GEN
     241           5 : nffactormod(GEN nf, GEN x, GEN pr)
     242             : {
     243           5 :   long j, l, vx = varn(x), vn;
     244           5 :   pari_sp av = avma;
     245             :   GEN F, E, rep, xrd, modpr, T, p;
     246             : 
     247           5 :   nf = checknf(nf);
     248           5 :   vn = nf_get_varn(nf);
     249           5 :   if (typ(x)!=t_POL) pari_err_TYPE("nffactormod",x);
     250           5 :   if (varncmp(vx,vn) >= 0) pari_err_PRIORITY("nffactormod", x, ">=", vn);
     251             : 
     252           5 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
     253           5 :   xrd = nfX_to_FqX(x, nf, modpr);
     254           5 :   rep = FqX_factor(xrd,T,p);
     255           5 :   settyp(rep, t_MAT);
     256           5 :   F = gel(rep,1); l = lg(F);
     257           5 :   E = gel(rep,2); settyp(E, t_COL);
     258          10 :   for (j = 1; j < l; j++) {
     259           5 :     gel(F,j) = FqX_to_nfX(gel(F,j), modpr);
     260           5 :     gel(E,j) = stoi(E[j]);
     261             :   }
     262           5 :   return gerepilecopy(av, rep);
     263             : }
     264             : 
     265             : /*******************************************************************/
     266             : /*               MAIN ROUTINES nfroots / nffactor                  */
     267             : /*******************************************************************/
     268             : static GEN
     269        1650 : QXQX_normalize(GEN P, GEN T)
     270             : {
     271        1650 :   GEN P0 = leading_coeff(P);
     272        1650 :   long t = typ(P0);
     273        1650 :   if (t == t_POL)
     274             :   {
     275         420 :     if (degpol(P0)) return RgXQX_RgXQ_mul(P, QXQ_inv(P0,T), T);
     276         400 :     P0 = gel(P0,2); t = typ(P0);
     277             :   }
     278             :   /* t = t_INT/t_FRAC */
     279        1630 :   if (t == t_INT && is_pm1(P0) && signe(P0) > 0) return P; /* monic */
     280         605 :   return RgX_Rg_div(P, P0);
     281             : }
     282             : /* assume leading term of P is an integer */
     283             : static GEN
     284        1130 : RgX_int_normalize(GEN P)
     285             : {
     286        1130 :   GEN P0 = leading_coeff(P);
     287             :   /* cater for t_POL */
     288        1130 :   if (typ(P0) == t_POL)
     289             :   {
     290          40 :     P0 = gel(P0,2); /* non-0 constant */
     291          40 :     P = shallowcopy(P);
     292          40 :     gel(P,lg(P)-1) = P0; /* now leading term is a t_INT */
     293             :   }
     294        1130 :   if (typ(P0) != t_INT) pari_err_BUG("RgX_int_normalize");
     295        1130 :   if (is_pm1(P0)) return signe(P0) > 0? P: RgX_neg(P);
     296         675 :   return RgX_Rg_div(P, P0);
     297             : }
     298             : 
     299             : /* discard change of variable if nf is of the form [nf,c] as return by nfinit
     300             :  * for non-monic polynomials */
     301             : static GEN
     302         180 : proper_nf(GEN nf)
     303         180 : { return (lg(nf) == 3)? gel(nf,1): nf; }
     304             : 
     305             : /* if *pnf = NULL replace if by a "quick" K = nfinit(T), ensuring maximality
     306             :  * by small primes only. Return a multiplicative bound for the denominator of
     307             :  * algebraic integers in Z_K in terms of K.zk */
     308             : static GEN
     309        1050 : fix_nf(GEN *pnf, GEN *pT, GEN *pA)
     310             : {
     311        1050 :   GEN nf, NF, fa, P, Q, q, D, T = *pT;
     312             :   nfmaxord_t S;
     313             :   long i, l;
     314             : 
     315        1050 :   if (*pnf) return gen_1;
     316         180 :   nfmaxord(&S, T, nf_PARTIALFACT);
     317         180 :   NF = nfinit_complete(&S, 0, DEFAULTPREC);
     318         180 :   *pnf = nf = proper_nf(NF);
     319         180 :   if (nf != NF) { /* t_POL defining base field changed (not monic) */
     320          25 :     GEN A = *pA, a = cgetg_copy(A, &l);
     321          25 :     GEN rev = gel(NF,2), pow, dpow;
     322             : 
     323          25 :     *pT = T = nf_get_pol(nf); /* need to update T */
     324          25 :     pow = QXQ_powers(lift_shallow(rev), degpol(T)-1, T);
     325          25 :     pow = Q_remove_denom(pow, &dpow);
     326          25 :     a[1] = A[1];
     327         110 :     for (i=2; i<l; i++) {
     328          85 :       GEN c = gel(A,i);
     329          85 :       if (typ(c) == t_POL) c = QX_ZXQV_eval(c, pow, dpow);
     330          85 :       gel(a,i) = c;
     331             :     }
     332          25 :     *pA = Q_primpart(a); /* need to update A */
     333             :   }
     334             : 
     335         180 :   D = nf_get_disc(nf);
     336         180 :   if (is_pm1(D)) return gen_1;
     337         175 :   fa = absZ_factor_limit(D, 0);
     338         175 :   P = gel(fa,1); q = gel(P, lg(P)-1);
     339         175 :   if (BPSW_psp(q)) return gen_1;
     340             :   /* nf_get_disc(nf) may be incorrect */
     341           5 :   P = nf_get_ramified_primes(nf);
     342           5 :   l = lg(P);
     343           5 :   Q = q; q = gen_1;
     344          30 :   for (i = 1; i < l; i++)
     345             :   {
     346          25 :     GEN p = gel(P,i);
     347          25 :     if (Z_pvalrem(Q, p, &Q) && !BPSW_psp(p)) q = mulii(q, p);
     348             :   }
     349           5 :   return q;
     350             : }
     351             : 
     352             : /* lt(A) is an integer; ensure it is not a constant t_POL. In place */
     353             : static void
     354        1155 : ensure_lt_INT(GEN A)
     355             : {
     356        1155 :   long n = lg(A)-1;
     357        1155 :   GEN lt = gel(A,n);
     358        1155 :   while (typ(lt) != t_INT) gel(A,n) = lt = gel(lt,2);
     359        1155 : }
     360             : 
     361             : /* set B = A/gcd(A,A'), squarefree */
     362             : static GEN
     363        1075 : get_nfsqff_data(GEN *pnf, GEN *pT, GEN *pA, GEN *pB, GEN *ptbad)
     364             : {
     365        1075 :   GEN den, bad, D, B, A = *pA, T = *pT;
     366        1075 :   long n = degpol(T);
     367             : 
     368        1075 :   A = Q_primpart( QXQX_normalize(A, T) );
     369        1075 :   if (nfsqff_use_Trager(n, degpol(A)))
     370             :   {
     371          85 :     *pnf = T;
     372          85 :     bad = den = ZX_disc(T);
     373          85 :     if (is_pm1(leading_coeff(T))) den = indexpartial(T, den);
     374             :   }
     375             :   else
     376             :   {
     377         990 :     den = fix_nf(pnf, &T, &A);
     378         990 :     bad = nf_get_index(*pnf);
     379         990 :     if (den != gen_1) bad = mulii(bad, den);
     380             :   }
     381        1075 :   D = nfgcd_all(A, RgX_deriv(A), T, bad, &B);
     382        1075 :   if (degpol(D)) B = Q_primpart( QXQX_normalize(B, T) );
     383        1075 :   if (ptbad) *ptbad = bad;
     384        1075 :   *pA = A;
     385        1075 :   *pB = B; ensure_lt_INT(B);
     386        1075 :   *pT = T; return den;
     387             : }
     388             : 
     389             : /* return the roots of pol in nf */
     390             : GEN
     391        1435 : nfroots(GEN nf,GEN pol)
     392             : {
     393        1435 :   pari_sp av = avma;
     394             :   GEN z, A, B, T, den;
     395             :   long d, dT;
     396             : 
     397        1435 :   if (!nf) return nfrootsQ(pol);
     398         775 :   T = get_nfpol(nf, &nf);
     399         775 :   RgX_check_ZX(T,"nfroots");
     400         775 :   A = RgX_nffix("nfroots", T,pol,1);
     401         775 :   d = degpol(A);
     402         775 :   if (d < 0) pari_err_ROOTS0("nfroots");
     403         775 :   if (d == 0) return cgetg(1,t_VEC);
     404         775 :   if (d == 1)
     405             :   {
     406           5 :     A = QXQX_normalize(A,T);
     407           5 :     A = mkpolmod(gneg_i(gel(A,2)), T);
     408           5 :     return gerepilecopy(av, mkvec(A));
     409             :   }
     410         770 :   dT = degpol(T);
     411         770 :   if (dT == 1) return gerepileupto(av, nfrootsQ(simplify_shallow(A)));
     412             : 
     413         770 :   den = get_nfsqff_data(&nf, &T, &A, &B, NULL);
     414         770 :   if (RgX_is_ZX(B))
     415             :   {
     416         260 :     GEN v = gel(ZX_factor(B), 1);
     417         260 :     long i, l = lg(v), p = mael(factoru(dT),1,1); /* smallest prime divisor */
     418         260 :     z = cgetg(1, t_VEC);
     419         710 :     for (i = 1; i < l; i++)
     420             :     {
     421         450 :       GEN b = gel(v,i); /* irreducible / Q */
     422         450 :       long db = degpol(b);
     423         450 :       if (db != 1 && degpol(b) < p) continue;
     424         450 :       z = shallowconcat(z, nfsqff(nf, b, ROOTS, den));
     425             :     }
     426             :   }
     427             :   else
     428         510 :     z = nfsqff(nf,B, ROOTS, den);
     429         770 :   z = gerepileupto(av, QXQV_to_mod(z, T));
     430         770 :   gen_sort_inplace(z, (void*)&cmp_RgX, &cmp_nodata, NULL);
     431         770 :   return z;
     432             : }
     433             : 
     434             : static GEN
     435       90930 : _norml2(GEN x) { return RgC_fpnorml2(x, DEFAULTPREC); }
     436             : 
     437             : /* return a minimal lift of elt modulo id, as a ZC */
     438             : static GEN
     439       26285 : nf_bestlift(GEN elt, GEN bound, nflift_t *L)
     440             : {
     441             :   GEN u;
     442       26285 :   long i,l = lg(L->prk), t = typ(elt);
     443       26285 :   if (t != t_INT)
     444             :   {
     445        6950 :     if (t == t_POL) elt = ZM_ZX_mul(L->tozk, elt);
     446        6950 :     u = ZM_ZC_mul(L->iprk,elt);
     447        6950 :     for (i=1; i<l; i++) gel(u,i) = diviiround(gel(u,i), L->den);
     448             :   }
     449             :   else
     450             :   {
     451       19335 :     u = ZC_Z_mul(gel(L->iprk,1), elt);
     452       19335 :     for (i=1; i<l; i++) gel(u,i) = diviiround(gel(u,i), L->den);
     453       19335 :     elt = scalarcol(elt, l-1);
     454             :   }
     455       26285 :   u = ZC_sub(elt, ZM_ZC_mul(L->prk, u));
     456       26285 :   if (bound && gcmp(_norml2(u), bound) > 0) u = NULL;
     457       26285 :   return u;
     458             : }
     459             : 
     460             : /* Warning: return L->topowden * (best lift). */
     461             : static GEN
     462       19875 : nf_bestlift_to_pol(GEN elt, GEN bound, nflift_t *L)
     463             : {
     464       19875 :   pari_sp av = avma;
     465       19875 :   GEN u,v = nf_bestlift(elt,bound,L);
     466       19875 :   if (!v) return NULL;
     467       16220 :   if (ZV_isscalar(v))
     468             :   {
     469       11245 :     if (L->topowden)
     470       11245 :       u = mulii(L->topowden, gel(v,1));
     471             :     else
     472           0 :       u = icopy(gel(v,1));
     473       11245 :     u = gerepileuptoint(av, u);
     474             :   }
     475             :   else
     476             :   {
     477        4975 :     v = gclone(v); avma = av;
     478        4975 :     u = RgV_dotproduct(L->topow, v);
     479        4975 :     gunclone(v);
     480             :   }
     481       16220 :   return u;
     482             : }
     483             : 
     484             : /* return the T->powden * (lift of pol with coefficients of T2-norm <= C)
     485             :  * if it exists. */
     486             : static GEN
     487        4500 : nf_pol_lift(GEN pol, GEN bound, nflift_t *L)
     488             : {
     489        4500 :   long i, l = lg(pol);
     490        4500 :   GEN x = cgetg(l,t_POL);
     491             : 
     492        4500 :   x[1] = pol[1];
     493        4500 :   gel(x,l-1) = mul_content(gel(pol,l-1), L->topowden);
     494       18755 :   for (i=l-2; i>1; i--)
     495             :   {
     496       17910 :     GEN t = nf_bestlift_to_pol(gel(pol,i), bound, L);
     497       17910 :     if (!t) return NULL;
     498       14255 :     gel(x,i) = t;
     499             :   }
     500         845 :   return x;
     501             : }
     502             : 
     503             : static GEN
     504           0 : zerofact(long v)
     505             : {
     506           0 :   GEN z = cgetg(3, t_MAT);
     507           0 :   gel(z,1) = mkcol(pol_0(v));
     508           0 :   gel(z,2) = mkcol(gen_1); return z;
     509             : }
     510             : 
     511             : /* Return the factorization of A in Q[X]/(T) in rep [pre-allocated with
     512             :  * cgetg(3,t_MAT)], reclaiming all memory between avma and rep.
     513             :  * y is the vector of irreducible factors of B = Q_primpart( A/gcd(A,A') ).
     514             :  * Bad primes divide 'bad' */
     515             : static void
     516         385 : fact_from_sqff(GEN rep, GEN A, GEN B, GEN y, GEN T, GEN bad)
     517             : {
     518         385 :   pari_sp av = (pari_sp)rep;
     519         385 :   long n = lg(y)-1;
     520             :   GEN ex;
     521             : 
     522         385 :   if (A != B)
     523             :   { /* not squarefree */
     524          15 :     if (n == 1)
     525             :     { /* perfect power, simple ! */
     526           0 :       long e = degpol(A) / degpol(gel(y,1));
     527           0 :       y = gerepileupto(av, QXQXV_to_mod(y, T));
     528           0 :       ex = mkcol(utoipos(e));
     529             :     }
     530             :     else
     531             :     { /* compute valuations mod a prime of degree 1 (avoid coeff explosion) */
     532          15 :       GEN quo, p, r, Bp, lb = leading_coeff(B), E = cgetalloc(t_VECSMALL,n+1);
     533          15 :       pari_sp av1 = avma;
     534             :       ulong pp;
     535             :       long j;
     536             :       forprime_t S;
     537          15 :       u_forprime_init(&S, degpol(T), ULONG_MAX);
     538          75 :       for (; ; avma = av1)
     539             :       {
     540          90 :         pp = u_forprime_next(&S);
     541          90 :         if (! umodiu(bad,pp) || !umodiu(lb, pp)) continue;
     542          80 :         p = utoipos(pp);
     543          80 :         r = FpX_oneroot(T, p);
     544          80 :         if (!r) continue;
     545          35 :         Bp = FpXY_evalx(B, r, p);
     546          35 :         if (FpX_is_squarefree(Bp, p)) break;
     547          75 :       }
     548             : 
     549          15 :       quo = FpXY_evalx(Q_primpart(A), r, p);
     550          40 :       for (j=n; j>=2; j--)
     551             :       {
     552          25 :         GEN junk, fact = Q_remove_denom(gel(y,j), &junk);
     553          25 :         long e = 0;
     554          25 :         fact = FpXY_evalx(fact, r, p);
     555          70 :         for(;; e++)
     556             :         {
     557          95 :           GEN q = FpX_divrem(quo,fact,p,ONLY_DIVIDES);
     558          95 :           if (!q) break;
     559          70 :           quo = q;
     560          70 :         }
     561          25 :         E[j] = e;
     562             :       }
     563          15 :       E[1] = degpol(quo) / degpol(gel(y,1));
     564          15 :       y = gerepileupto(av, QXQXV_to_mod(y, T));
     565          15 :       ex = zc_to_ZC(E); pari_free((void*)E);
     566             :     }
     567             :   }
     568             :   else
     569             :   {
     570         370 :     y = gerepileupto(av, QXQXV_to_mod(y, T));
     571         370 :     ex = const_col(n, gen_1);
     572             :   }
     573         385 :   gel(rep,1) = y; settyp(y, t_COL);
     574         385 :   gel(rep,2) = ex;
     575         385 : }
     576             : 
     577             : /* return the factorization of x in nf */
     578             : GEN
     579         385 : nffactor(GEN nf,GEN pol)
     580             : {
     581         385 :   GEN bad, A, B, y, T, den, rep = cgetg(3, t_MAT);
     582         385 :   pari_sp av = avma;
     583             :   long dA;
     584             :   pari_timer ti;
     585             : 
     586         385 :   if (DEBUGLEVEL>2) { timer_start(&ti); err_printf("\nEntering nffactor:\n"); }
     587         385 :   T = get_nfpol(nf, &nf);
     588         385 :   RgX_check_ZX(T,"nffactor");
     589         385 :   A = RgX_nffix("nffactor",T,pol,1);
     590         380 :   dA = degpol(A);
     591         380 :   if (dA <= 0) {
     592           0 :     avma = (pari_sp)(rep + 3);
     593           0 :     return (dA == 0)? trivial_fact(): zerofact(varn(pol));
     594             :   }
     595         380 :   if (dA == 1) {
     596             :     GEN c;
     597          30 :     A = Q_primpart( QXQX_normalize(A, T) );
     598          30 :     A = gerepilecopy(av, A); c = gel(A,2);
     599          30 :     if (typ(c) == t_POL && degpol(c) > 0) gel(A,2) = mkpolmod(c, ZX_copy(T));
     600          30 :     gel(rep,1) = mkcol(A);
     601          30 :     gel(rep,2) = mkcol(gen_1); return rep;
     602             :   }
     603         350 :   if (degpol(T) == 1) return gerepileupto(av, QX_factor(simplify_shallow(A)));
     604             : 
     605         305 :   den = get_nfsqff_data(&nf, &T, &A, &B, &bad);
     606         305 :   if (DEBUGLEVEL>2) timer_printf(&ti, "squarefree test");
     607         305 :   if (RgX_is_ZX(B))
     608             :   {
     609         255 :     GEN v = gel(ZX_factor(B), 1);
     610         255 :     long i, l = lg(v);
     611         255 :     y = cgetg(1, t_VEC);
     612         525 :     for (i = 1; i < l; i++)
     613             :     {
     614         270 :       GEN b = gel(v,i); /* irreducible / Q */
     615         270 :       y = shallowconcat(y, nfsqff(nf, b, 0, den));
     616             :     }
     617             :   }
     618             :   else
     619          50 :     y = nfsqff(nf,B, 0, den);
     620         305 :   if (DEBUGLEVEL>3) err_printf("number of factor(s) found: %ld\n", lg(y)-1);
     621             : 
     622         305 :   fact_from_sqff(rep, A, B, y, T, bad);
     623         305 :   return sort_factor_pol(rep, cmp_RgX);
     624             : }
     625             : 
     626             : /* assume x scalar or t_COL, G t_MAT */
     627             : static GEN
     628       10125 : arch_for_T2(GEN G, GEN x)
     629             : {
     630       20250 :   return (typ(x) == t_COL)? RgM_RgC_mul(G,x)
     631       10125 :                           : RgC_Rg_mul(gel(G,1),x);
     632             : }
     633             : 
     634             : /* polbase a zkX with t_INT leading coeff; return a bound for T_2(P),
     635             :  * P | polbase in C[X]. NB: Mignotte bound: A | S ==>
     636             :  *  |a_i| <= binom(d-1, i-1) || S ||_2 + binom(d-1, i) lc(S)
     637             :  *
     638             :  * Apply to sigma(S) for all embeddings sigma, then take the L_2 norm over
     639             :  * sigma, then take the sup over i */
     640             : static GEN
     641         270 : nf_Mignotte_bound(GEN nf, GEN polbase)
     642         270 : { GEN lS = leading_coeff(polbase); /* t_INT */
     643             :   GEN p1, C, N2, binlS, bin;
     644         270 :   long prec = nf_get_prec(nf), n = nf_get_degree(nf), r1 = nf_get_r1(nf);
     645         270 :   long i, j, d = degpol(polbase);
     646             : 
     647         270 :   binlS = bin = vecbinomial(d-1);
     648         270 :   if (!isint1(lS)) binlS = ZC_Z_mul(bin,lS);
     649             : 
     650         270 :   N2 = cgetg(n+1, t_VEC);
     651             :   for (;;)
     652             :   {
     653         270 :     GEN G = nf_get_G(nf), matGS = cgetg(d+2, t_MAT);
     654             : 
     655         270 :     for (j=0; j<=d; j++) gel(matGS,j+1) = arch_for_T2(G, gel(polbase,j+2));
     656         270 :     matGS = shallowtrans(matGS);
     657         660 :     for (j=1; j <= r1; j++) /* N2[j] = || sigma_j(S) ||_2 */
     658             :     {
     659         390 :       GEN c = sqrtr( _norml2(gel(matGS,j)) );
     660         390 :       gel(N2,j) = c; if (!signe(c)) goto PRECPB;
     661             :     }
     662        1035 :     for (   ; j <= n; j+=2)
     663             :     {
     664         765 :       GEN q1 = _norml2(gel(matGS, j));
     665         765 :       GEN q2 = _norml2(gel(matGS, j+1));
     666         765 :       GEN c = sqrtr( gmul2n(addrr(q1, q2), -1) );
     667         765 :       gel(N2,j) = gel(N2,j+1) = c; if (!signe(c)) goto PRECPB;
     668             :     }
     669         270 :     break; /* done */
     670             : PRECPB:
     671           0 :     prec = precdbl(prec);
     672           0 :     nf = nfnewprec_shallow(nf, prec);
     673           0 :     if (DEBUGLEVEL>1) pari_warn(warnprec, "nf_factor_bound", prec);
     674           0 :   }
     675             : 
     676             :   /* Take sup over 0 <= i <= d of
     677             :    * sum_j | binom(d-1, i-1) ||sigma_j(S)||_2 + binom(d-1,i) lc(S) |^2 */
     678             : 
     679             :   /* i = 0: n lc(S)^2 */
     680         270 :   C = mului(n, sqri(lS));
     681             :   /* i = d: sum_sigma ||sigma(S)||_2^2 */
     682         270 :   p1 = gnorml2(N2); if (gcmp(C, p1) < 0) C = p1;
     683        6150 :   for (i = 1; i < d; i++)
     684             :   {
     685        5880 :     GEN B = gel(bin,i), L = gel(binlS,i+1);
     686        5880 :     GEN s = sqrr(addri(mulir(B, gel(N2,1)),  L)); /* j=1 */
     687        5880 :     for (j = 2; j <= n; j++) s = addrr(s, sqrr(addri(mulir(B, gel(N2,j)), L)));
     688        5880 :     if (mpcmp(C, s) < 0) C = s;
     689             :   }
     690         270 :   return C;
     691             : }
     692             : 
     693             : /* return a bound for T_2(P), P | polbase
     694             :  * max |b_i|^2 <= 3^{3/2 + d} / (4 \pi d) [P]_2,
     695             :  * where [P]_2 is Bombieri's 2-norm
     696             :  * Sum over conjugates */
     697             : static GEN
     698         270 : nf_Beauzamy_bound(GEN nf, GEN polbase)
     699             : {
     700             :   GEN lt, C, s, POL, bin;
     701         270 :   long d = degpol(polbase), n = nf_get_degree(nf), prec = nf_get_prec(nf);
     702         270 :   bin = vecbinomial(d);
     703         270 :   POL = polbase + 2;
     704             :   /* compute [POL]_2 */
     705             :   for (;;)
     706             :   {
     707         270 :     GEN G = nf_get_G(nf);
     708             :     long i;
     709             : 
     710         270 :     s = real_0(prec);
     711        6690 :     for (i=0; i<=d; i++)
     712             :     {
     713        6420 :       GEN c = gel(POL,i);
     714        6420 :       if (gequal0(c)) continue;
     715        3705 :       c = _norml2(arch_for_T2(G,c));
     716        3705 :       if (!signe(c)) goto PRECPB;
     717             :       /* s += T2(POL[i]) / binomial(d,i) */
     718        3705 :       s = addrr(s, divri(c, gel(bin,i+1)));
     719             :     }
     720         270 :     break;
     721             : PRECPB:
     722           0 :     prec = precdbl(prec);
     723           0 :     nf = nfnewprec_shallow(nf, prec);
     724           0 :     if (DEBUGLEVEL>1) pari_warn(warnprec, "nf_factor_bound", prec);
     725           0 :   }
     726         270 :   lt = leading_coeff(polbase);
     727         270 :   s = mulri(s, muliu(sqri(lt), n));
     728         270 :   C = powruhalf(stor(3,DEFAULTPREC), 3 + 2*d); /* 3^{3/2 + d} */
     729         270 :   return divrr(mulrr(C, s), mulur(d, mppi(DEFAULTPREC)));
     730             : }
     731             : 
     732             : static GEN
     733         270 : nf_factor_bound(GEN nf, GEN polbase)
     734             : {
     735         270 :   pari_sp av = avma;
     736         270 :   GEN a = nf_Mignotte_bound(nf, polbase);
     737         270 :   GEN b = nf_Beauzamy_bound(nf, polbase);
     738         270 :   if (DEBUGLEVEL>2)
     739             :   {
     740           0 :     err_printf("Mignotte bound: %Ps\n",a);
     741           0 :     err_printf("Beauzamy bound: %Ps\n",b);
     742             :   }
     743         270 :   return gerepileupto(av, gmin(a, b));
     744             : }
     745             : 
     746             : /* return Bs: if r a root of sigma_i(P), |r| < Bs[i] */
     747             : static GEN
     748         845 : nf_root_bounds(GEN P, GEN T)
     749             : {
     750             :   long lR, i, j, l, prec;
     751             :   GEN Ps, R, V, nf;
     752             : 
     753         845 :   if (RgX_is_rational(P)) return polrootsbound(P);
     754         450 :   T = get_nfpol(T, &nf);
     755             : 
     756         450 :   P = Q_primpart(P);
     757         450 :   prec = ZXX_max_lg(P) + 1;
     758         450 :   l = lg(P);
     759         450 :   if (nf && nf_get_prec(nf) >= prec)
     760         420 :     R = nf_get_roots(nf);
     761             :   else
     762          30 :     R = QX_complex_roots(T, prec);
     763         450 :   lR = lg(R);
     764         450 :   V = cgetg(lR, t_VEC);
     765         450 :   Ps = cgetg(l, t_POL); /* sigma (P) */
     766         450 :   Ps[1] = P[1];
     767        1320 :   for (j=1; j<lg(R); j++)
     768             :   {
     769         870 :     GEN r = gel(R,j);
     770         870 :     for (i=2; i<l; i++) gel(Ps,i) = poleval(gel(P,i), r);
     771         870 :     gel(V,j) = polrootsbound(Ps);
     772             :   }
     773         450 :   return V;
     774             : }
     775             : 
     776             : /* return B such that if x in O_K, K = Z[X]/(T), then the L2-norm of the
     777             :  * coordinates of the numerator of x [on the power, resp. integral, basis if T
     778             :  * is a polynomial, resp. an nf] is  <= B T_2(x)
     779             :  * den = multiplicative bound for denom(x) */
     780             : static GEN
     781        1140 : L2_bound(GEN nf, GEN den)
     782             : {
     783        1140 :   GEN M, L, prep, T = nf_get_pol(nf), tozk = nf_get_invzk(nf);
     784        1140 :   long bit = bit_accuracy(ZX_max_lg(T)) + bit_accuracy(ZM_max_lg(tozk));
     785        1140 :   long prec = nbits2prec(bit + degpol(T));
     786        1140 :   (void)initgaloisborne(nf, den, prec, &L, &prep, NULL);
     787        1140 :   M = vandermondeinverse(L, RgX_gtofp(T,prec), den, prep);
     788        1140 :   return RgM_fpnorml2(RgM_mul(tozk,M), DEFAULTPREC);
     789             : }
     790             : 
     791             : /* || L ||_p^p in dimension n (L may be a scalar) */
     792             : static GEN
     793        1585 : normlp(GEN L, long p, long n)
     794             : {
     795        1585 :   long i,l, t = typ(L);
     796             :   GEN z;
     797             : 
     798        1585 :   if (!is_vec_t(t)) return gmulsg(n, gpowgs(L, p));
     799             : 
     800         855 :   l = lg(L); z = gen_0;
     801             :   /* assert(n == l-1); */
     802        2520 :   for (i=1; i<l; i++)
     803        1665 :     z = gadd(z, gpowgs(gel(L,i), p));
     804         855 :   return z;
     805             : }
     806             : 
     807             : /* S = S0 + tS1, P = P0 + tP1 (Euclidean div. by t integer). For a true
     808             :  * factor (vS, vP), we have:
     809             :  *    | S vS + P vP |^2 < Btra
     810             :  * This implies | S1 vS + P1 vP |^2 < Bhigh, assuming t > sqrt(Btra).
     811             :  * d = dimension of low part (= [nf:Q])
     812             :  * n0 = bound for |vS|^2
     813             :  * */
     814             : static double
     815         305 : get_Bhigh(long n0, long d)
     816             : {
     817         305 :   double sqrtd = sqrt((double)d);
     818         305 :   double z = n0*sqrtd + sqrtd/2 * (d * (n0+1));
     819         305 :   z = 1. + 0.5 * z; return z * z;
     820             : }
     821             : 
     822             : typedef struct {
     823             :   GEN d;
     824             :   GEN dPinvS;   /* d P^(-1) S   [ integral ] */
     825             :   double **PinvSdbl; /* P^(-1) S as double */
     826             :   GEN S1, P1;   /* S = S0 + S1 q, idem P */
     827             : } trace_data;
     828             : 
     829             : /* S1 * u - P1 * round(P^-1 S u). K non-zero coords in u given by ind */
     830             : static GEN
     831       67395 : get_trace(GEN ind, trace_data *T)
     832             : {
     833       67395 :   long i, j, l, K = lg(ind)-1;
     834             :   GEN z, s, v;
     835             : 
     836       67395 :   s = gel(T->S1, ind[1]);
     837       67395 :   if (K == 1) return s;
     838             : 
     839             :   /* compute s = S1 u */
     840       65905 :   for (j=2; j<=K; j++) s = ZC_add(s, gel(T->S1, ind[j]));
     841             : 
     842             :   /* compute v := - round(P^1 S u) */
     843       65905 :   l = lg(s);
     844       65905 :   v = cgetg(l, t_VECSMALL);
     845      799100 :   for (i=1; i<l; i++)
     846             :   {
     847      733195 :     double r, t = 0.;
     848             :     /* quick approximate computation */
     849      733195 :     for (j=1; j<=K; j++) t += T->PinvSdbl[ ind[j] ][i];
     850      733195 :     r = floor(t + 0.5);
     851      733195 :     if (fabs(t + 0.5 - r) < 0.0001)
     852             :     { /* dubious, compute exactly */
     853          70 :       z = gen_0;
     854          70 :       for (j=1; j<=K; j++) z = addii(z, ((GEN**)T->dPinvS)[ ind[j] ][i]);
     855          70 :       v[i] = - itos( diviiround(z, T->d) );
     856             :     }
     857             :     else
     858      733125 :       v[i] = - (long)r;
     859             :   }
     860       65905 :   return ZC_add(s, ZM_zc_mul(T->P1, v));
     861             : }
     862             : 
     863             : static trace_data *
     864         540 : init_trace(trace_data *T, GEN S, nflift_t *L, GEN q)
     865             : {
     866         540 :   long e = gexpo(S), i,j, l,h;
     867             :   GEN qgood, S1, invd;
     868             : 
     869         540 :   if (e < 0) return NULL; /* S = 0 */
     870             : 
     871         510 :   qgood = int2n(e - 32); /* single precision check */
     872         510 :   if (cmpii(qgood, q) > 0) q = qgood;
     873             : 
     874         510 :   S1 = gdivround(S, q);
     875         510 :   if (gequal0(S1)) return NULL;
     876             : 
     877         160 :   invd = invr(itor(L->den, DEFAULTPREC));
     878             : 
     879         160 :   T->dPinvS = ZM_mul(L->iprk, S);
     880         160 :   l = lg(S);
     881         160 :   h = lgcols(T->dPinvS);
     882         160 :   T->PinvSdbl = (double**)cgetg(l, t_MAT);
     883        2380 :   for (j = 1; j < l; j++)
     884             :   {
     885        2220 :     double *t = (double *) stack_malloc_align(h * sizeof(double), sizeof(double));
     886        2220 :     GEN c = gel(T->dPinvS,j);
     887        2220 :     pari_sp av = avma;
     888        2220 :     T->PinvSdbl[j] = t;
     889        2220 :     for (i=1; i < h; i++) t[i] = rtodbl(mulri(invd, gel(c,i)));
     890        2220 :     avma = av;
     891             :   }
     892             : 
     893         160 :   T->d  = L->den;
     894         160 :   T->P1 = gdivround(L->prk, q);
     895         160 :   T->S1 = S1; return T;
     896             : }
     897             : 
     898             : static void
     899       11160 : update_trace(trace_data *T, long k, long i)
     900             : {
     901       22320 :   if (!T) return;
     902        6500 :   gel(T->S1,k)     = gel(T->S1,i);
     903        6500 :   gel(T->dPinvS,k) = gel(T->dPinvS,i);
     904        6500 :   T->PinvSdbl[k]   = T->PinvSdbl[i];
     905             : }
     906             : 
     907             : /* reduce coeffs mod (T,pk), then center mod pk */
     908             : static GEN
     909       11095 : FqX_centermod(GEN z, GEN T, GEN pk, GEN pks2)
     910             : {
     911             :   long i, l;
     912             :   GEN y;
     913       11095 :   if (!T) return centermod_i(z, pk, pks2);
     914       10200 :   y = FpXQX_red(z, T, pk); l = lg(y);
     915       96135 :   for (i = 2; i < l; i++)
     916             :   {
     917       85935 :     GEN c = gel(y,i);
     918       85935 :     if (typ(c) == t_INT)
     919       57310 :       c = centermodii(c, pk, pks2);
     920             :     else
     921       28625 :       c = FpX_center(c, pk, pks2);
     922       85935 :     gel(y,i) = c;
     923             :   }
     924       10200 :   return y;
     925             : }
     926             : 
     927             : typedef struct {
     928             :   GEN lt, C, Clt, C2lt, C2ltpol;
     929             : } div_data;
     930             : 
     931             : static void
     932        1160 : init_div_data(div_data *D, GEN pol, nflift_t *L)
     933             : {
     934        1160 :   GEN C = mul_content(L->topowden, L->dn);
     935        1160 :   GEN C2lt, Clt, lc = leading_coeff(pol), lt = is_pm1(lc)? NULL: absi(lc);
     936        1160 :   if (C)
     937             :   {
     938        1160 :     GEN C2 = sqri(C);
     939        1160 :     if (lt) {
     940         255 :       C2lt = mulii(C2, lt);
     941         255 :       Clt = mulii(C,lt);
     942             :     } else {
     943         905 :       C2lt = C2;
     944         905 :       Clt = C;
     945             :     }
     946             :   }
     947             :   else
     948           0 :     C2lt = Clt = lt;
     949        1160 :   D->lt = lt;
     950        1160 :   D->C = C;
     951        1160 :   D->Clt = Clt;
     952        1160 :   D->C2lt = C2lt;
     953        1160 :   D->C2ltpol = C2lt? RgX_Rg_mul(pol, C2lt): pol;
     954        1160 : }
     955             : static void
     956         815 : update_target(div_data *D, GEN pol)
     957         815 : { D->C2ltpol = D->Clt? RgX_Rg_mul(pol, D->Clt): pol; }
     958             : 
     959             : /* nb = number of modular factors; return a "good" K such that naive
     960             :  * recombination of up to maxK modular factors is not too costly */
     961             : long
     962        6925 : cmbf_maxK(long nb)
     963             : {
     964        6925 :   if (nb >  10) return 3;
     965        6525 :   return nb-1;
     966             : }
     967             : /* Naive recombination of modular factors: combine up to maxK modular
     968             :  * factors, degree <= klim
     969             :  *
     970             :  * target = polynomial we want to factor
     971             :  * famod = array of modular factors.  Product should be congruent to
     972             :  * target/lc(target) modulo p^a
     973             :  * For true factors: S1,S2 <= p^b, with b <= a and p^(b-a) < 2^31 */
     974             : /* set *done = 1 if factorisation is known to be complete */
     975             : static GEN
     976         270 : nfcmbf(nfcmbf_t *T, long klim, long *pmaxK, int *done)
     977             : {
     978         270 :   GEN nf = T->nf, famod = T->fact, bound = T->bound;
     979         270 :   GEN ltdn, nfpol = nf_get_pol(nf);
     980         270 :   long K = 1, cnt = 1, i,j,k, curdeg, lfamod = lg(famod)-1, dnf = degpol(nfpol);
     981         270 :   pari_sp av0 = avma;
     982         270 :   GEN Tpk = T->L->Tpk, pk = T->L->pk, pks2 = shifti(pk,-1);
     983         270 :   GEN ind      = cgetg(lfamod+1, t_VECSMALL);
     984         270 :   GEN deg      = cgetg(lfamod+1, t_VECSMALL);
     985         270 :   GEN degsofar = cgetg(lfamod+1, t_VECSMALL);
     986         270 :   GEN fa       = cgetg(lfamod+1, t_VEC);
     987         270 :   const double Bhigh = get_Bhigh(lfamod, dnf);
     988             :   trace_data _T1, _T2, *T1, *T2;
     989             :   div_data D;
     990             :   pari_timer ti;
     991             : 
     992         270 :   timer_start(&ti);
     993             : 
     994         270 :   *pmaxK = cmbf_maxK(lfamod);
     995         270 :   init_div_data(&D, T->pol, T->L);
     996         270 :   ltdn = mul_content(D.lt, T->L->dn);
     997             :   {
     998         270 :     GEN q = ceil_safe(sqrtr(T->BS_2));
     999         270 :     GEN t1,t2, lt2dn = mul_content(ltdn, D.lt);
    1000         270 :     GEN trace1   = cgetg(lfamod+1, t_MAT);
    1001         270 :     GEN trace2   = cgetg(lfamod+1, t_MAT);
    1002        2315 :     for (i=1; i <= lfamod; i++)
    1003             :     {
    1004        2045 :       pari_sp av = avma;
    1005        2045 :       GEN P = gel(famod,i);
    1006        2045 :       long d = degpol(P);
    1007             : 
    1008        2045 :       deg[i] = d; P += 2;
    1009        2045 :       t1 = gel(P,d-1);/* = - S_1 */
    1010        2045 :       t2 = Fq_sqr(t1, Tpk, pk);
    1011        2045 :       if (d > 1) t2 = Fq_sub(t2, gmul2n(gel(P,d-2), 1), Tpk, pk);
    1012             :       /* t2 = S_2 Newton sum */
    1013        2045 :       if (ltdn)
    1014             :       {
    1015          90 :         t1 = Fq_Fp_mul(t1, ltdn, Tpk, pk);
    1016          90 :         t2 = Fq_Fp_mul(t2, lt2dn, Tpk, pk);
    1017             :       }
    1018        2045 :       gel(trace1,i) = gclone( nf_bestlift(t1, NULL, T->L) );
    1019        2045 :       gel(trace2,i) = gclone( nf_bestlift(t2, NULL, T->L) ); avma = av;
    1020             :     }
    1021         270 :     T1 = init_trace(&_T1, trace1, T->L, q);
    1022         270 :     T2 = init_trace(&_T2, trace2, T->L, q);
    1023        2315 :     for (i=1; i <= lfamod; i++) {
    1024        2045 :       gunclone(gel(trace1,i));
    1025        2045 :       gunclone(gel(trace2,i));
    1026             :     }
    1027             :   }
    1028         270 :   degsofar[0] = 0; /* sentinel */
    1029             : 
    1030             :   /* ind runs through strictly increasing sequences of length K,
    1031             :    * 1 <= ind[i] <= lfamod */
    1032             : nextK:
    1033         500 :   if (K > *pmaxK || 2*K > lfamod) goto END;
    1034         415 :   if (DEBUGLEVEL > 3)
    1035           0 :     err_printf("\n### K = %d, %Ps combinations\n", K,binomial(utoipos(lfamod), K));
    1036         415 :   setlg(ind, K+1); ind[1] = 1;
    1037         415 :   i = 1; curdeg = deg[ind[1]];
    1038             :   for(;;)
    1039             :   { /* try all combinations of K factors */
    1040       79565 :     for (j = i; j < K; j++)
    1041             :     {
    1042        8970 :       degsofar[j] = curdeg;
    1043        8970 :       ind[j+1] = ind[j]+1; curdeg += deg[ind[j+1]];
    1044             :     }
    1045       70595 :     if (curdeg <= klim) /* trial divide */
    1046             :     {
    1047             :       GEN t, y, q;
    1048             :       pari_sp av;
    1049             : 
    1050       70595 :       av = avma;
    1051       70595 :       if (T1)
    1052             :       { /* d-1 test */
    1053       34750 :         t = get_trace(ind, T1);
    1054       34750 :         if (rtodbl(_norml2(t)) > Bhigh)
    1055             :         {
    1056       33935 :           if (DEBUGLEVEL>6) err_printf(".");
    1057       33935 :           avma = av; goto NEXT;
    1058             :         }
    1059             :       }
    1060       36660 :       if (T2)
    1061             :       { /* d-2 test */
    1062       32645 :         t = get_trace(ind, T2);
    1063       32645 :         if (rtodbl(_norml2(t)) > Bhigh)
    1064             :         {
    1065       32205 :           if (DEBUGLEVEL>3) err_printf("|");
    1066       32205 :           avma = av; goto NEXT;
    1067             :         }
    1068             :       }
    1069        4455 :       avma = av;
    1070        4455 :       y = ltdn; /* full computation */
    1071       15550 :       for (i=1; i<=K; i++)
    1072             :       {
    1073       11095 :         GEN q = gel(famod, ind[i]);
    1074       11095 :         if (y) q = gmul(y, q);
    1075       11095 :         y = FqX_centermod(q, Tpk, pk, pks2);
    1076             :       }
    1077        4455 :       y = nf_pol_lift(y, bound, T->L);
    1078        4455 :       if (!y)
    1079             :       {
    1080        3655 :         if (DEBUGLEVEL>3) err_printf("@");
    1081        3655 :         avma = av; goto NEXT;
    1082             :       }
    1083             :       /* y = topowden*dn*lt*\prod_{i in ind} famod[i] is apparently in O_K[X],
    1084             :        * in fact in (Z[Y]/nf.pol)[X] due to multiplication by C = topowden*dn.
    1085             :        * Try out this candidate factor */
    1086         800 :       q = RgXQX_divrem(D.C2ltpol, y, nfpol, ONLY_DIVIDES);
    1087         800 :       if (!q)
    1088             :       {
    1089          20 :         if (DEBUGLEVEL>3) err_printf("*");
    1090          20 :         avma = av; goto NEXT;
    1091             :       }
    1092             :       /* Original T->pol in O_K[X] with leading coeff lt in Z,
    1093             :        * y = C*lt \prod famod[i] is in O_K[X] with leading coeff in Z
    1094             :        * q = C^2*lt*pol / y = C * (lt*pol) / (lt*\prod famod[i]) is a
    1095             :        * K-rational factor, in fact in Z[Y]/nf.pol)[X] as above, with
    1096             :        * leading term C*lt. */
    1097         780 :       update_target(&D, q);
    1098         780 :       gel(fa,cnt++) = D.C2lt? RgX_int_normalize(y): y; /* make monic */
    1099        7370 :       for (i=j=k=1; i <= lfamod; i++)
    1100             :       { /* remove used factors */
    1101        6590 :         if (j <= K && i == ind[j]) j++;
    1102             :         else
    1103             :         {
    1104        5580 :           gel(famod,k) = gel(famod,i);
    1105        5580 :           update_trace(T1, k, i);
    1106        5580 :           update_trace(T2, k, i);
    1107        5580 :           deg[k] = deg[i]; k++;
    1108             :         }
    1109             :       }
    1110         780 :       lfamod -= K;
    1111         780 :       *pmaxK = cmbf_maxK(lfamod);
    1112         780 :       if (lfamod < 2*K) goto END;
    1113         595 :       i = 1; curdeg = deg[ind[1]];
    1114         595 :       if (DEBUGLEVEL > 2)
    1115             :       {
    1116           0 :         err_printf("\n"); timer_printf(&ti, "to find factor %Ps",y);
    1117           0 :         err_printf("remaining modular factor(s): %ld\n", lfamod);
    1118             :       }
    1119         595 :       continue;
    1120             :     }
    1121             : 
    1122             : NEXT:
    1123       69815 :     for (i = K+1;;)
    1124             :     {
    1125       78785 :       if (--i == 0) { K++; goto nextK; }
    1126       78555 :       if (++ind[i] <= lfamod - K + i)
    1127             :       {
    1128       69585 :         curdeg = degsofar[i-1] + deg[ind[i]];
    1129       69585 :         if (curdeg <= klim) break;
    1130             :       }
    1131        8970 :     }
    1132       70180 :   }
    1133             : END:
    1134         270 :   *done = 1;
    1135         270 :   if (degpol(D.C2ltpol) > 0)
    1136             :   { /* leftover factor */
    1137         270 :     GEN q = D.C2ltpol;
    1138         270 :     if (D.C2lt) q = RgX_int_normalize(q);
    1139         270 :     if (lfamod >= 2*K)
    1140             :     { /* restore leading coefficient [#930] */
    1141          35 :       if (D.lt) q = RgX_Rg_mul(q, D.lt);
    1142          35 :       *done = 0; /* ... may still be reducible */
    1143             :     }
    1144         270 :     setlg(famod, lfamod+1);
    1145         270 :     gel(fa,cnt++) = q;
    1146             :   }
    1147         270 :   if (DEBUGLEVEL>6) err_printf("\n");
    1148         270 :   if (cnt == 2) {
    1149          30 :     avma = av0;
    1150          30 :     return mkvec(T->pol);
    1151             :   }
    1152             :   else
    1153             :   {
    1154         240 :     setlg(fa, cnt);
    1155         240 :     return gerepilecopy(av0, fa);
    1156             :   }
    1157             : }
    1158             : 
    1159             : static GEN
    1160          20 : nf_chk_factors(nfcmbf_t *T, GEN P, GEN M_L, GEN famod, GEN pk)
    1161             : {
    1162          20 :   GEN nf = T->nf, bound = T->bound;
    1163          20 :   GEN nfT = nf_get_pol(nf);
    1164             :   long i, r;
    1165          20 :   GEN pol = P, list, piv, y;
    1166          20 :   GEN Tpk = T->L->Tpk;
    1167             :   div_data D;
    1168             : 
    1169          20 :   piv = ZM_hnf_knapsack(M_L);
    1170          20 :   if (!piv) return NULL;
    1171          10 :   if (DEBUGLEVEL>3) err_printf("ZM_hnf_knapsack output:\n%Ps\n",piv);
    1172             : 
    1173          10 :   r  = lg(piv)-1;
    1174          10 :   list = cgetg(r+1, t_VEC);
    1175          10 :   init_div_data(&D, pol, T->L);
    1176          10 :   for (i = 1;;)
    1177             :   {
    1178          45 :     pari_sp av = avma;
    1179          45 :     if (DEBUGLEVEL) err_printf("nf_LLL_cmbf: checking factor %ld\n", i);
    1180          45 :     y = chk_factors_get(D.lt, famod, gel(piv,i), Tpk, pk);
    1181             : 
    1182          45 :     if (! (y = nf_pol_lift(y, bound, T->L)) ) return NULL;
    1183          45 :     y = gerepilecopy(av, y);
    1184             :     /* y is the candidate factor */
    1185          45 :     pol = RgXQX_divrem(D.C2ltpol, y, nfT, ONLY_DIVIDES);
    1186          45 :     if (!pol) return NULL;
    1187             : 
    1188          45 :     if (D.C2lt) y = RgX_int_normalize(y);
    1189          45 :     gel(list,i) = y;
    1190          45 :     if (++i >= r) break;
    1191             : 
    1192          35 :     update_target(&D, pol);
    1193          35 :   }
    1194          10 :   gel(list,i) = RgX_int_normalize(pol); return list;
    1195             : }
    1196             : 
    1197             : static GEN
    1198        9740 : nf_to_Zq(GEN x, GEN T, GEN pk, GEN pks2, GEN proj)
    1199             : {
    1200             :   GEN y;
    1201        9740 :   if (typ(x) != t_COL) return centermodii(x, pk, pks2);
    1202        1510 :   if (!T)
    1203             :   {
    1204        1460 :     y = ZV_dotproduct(proj, x);
    1205        1460 :     return centermodii(y, pk, pks2);
    1206             :   }
    1207          50 :   y = ZM_ZC_mul(proj, x);
    1208          50 :   y = RgV_to_RgX(y, varn(T));
    1209          50 :   return FpX_center(FpX_rem(y, T, pk), pk, pks2);
    1210             : }
    1211             : 
    1212             : /* Assume P in nfX form, lc(P) != 0 mod p. Reduce P to Zp[X]/(T) mod p^a */
    1213             : static GEN
    1214         845 : ZqX(GEN P, GEN pk, GEN T, GEN proj)
    1215             : {
    1216         845 :   long i, l = lg(P);
    1217         845 :   GEN z, pks2 = shifti(pk,-1);
    1218             : 
    1219         845 :   z = cgetg(l,t_POL); z[1] = P[1];
    1220         845 :   for (i=2; i<l; i++) gel(z,i) = nf_to_Zq(gel(P,i),T,pk,pks2,proj);
    1221         845 :   return normalizepol_lg(z, l);
    1222             : }
    1223             : 
    1224             : static GEN
    1225         845 : ZqX_normalize(GEN P, GEN lt, nflift_t *L)
    1226             : {
    1227         845 :   GEN R = lt? RgX_Rg_mul(P, Fp_inv(lt, L->pk)): P;
    1228         845 :   return ZqX(R, L->pk, L->Tpk, L->ZqProj);
    1229             : }
    1230             : 
    1231             : /* k allowing to reconstruct x, |x|^2 < C, from x mod pr^k */
    1232             : /* return log [  2sqrt(C/d) * ( (3/2)sqrt(gamma) )^(d-1) ] ^d / log N(pr)
    1233             :  * cf. Belabas relative van Hoeij algorithm, lemma 3.12 */
    1234             : static double
    1235         845 : bestlift_bound(GEN C, long d, double alpha, GEN Npr)
    1236             : {
    1237         845 :   const double y = 1 / (alpha - 0.25); /* = 2 if alpha = 3/4 */
    1238             :   double t;
    1239         845 :   C = gtofp(C,DEFAULTPREC);
    1240             :   /* (1/2)log (4C/d) + (d-1)(log 3/2 sqrt(gamma)) */
    1241         845 :   t = rtodbl(mplog(gmul2n(divru(C,d), 2))) * 0.5 + (d-1) * log(1.5 * sqrt(y));
    1242         845 :   return ceil((t * d) / log(gtodouble(Npr)));
    1243             : }
    1244             : 
    1245             : static GEN
    1246        1155 : get_R(GEN M)
    1247             : {
    1248             :   GEN R;
    1249        1155 :   long i, l, prec = nbits2prec( gexpo(M) + 64 );
    1250             : 
    1251             :   for(;;)
    1252             :   {
    1253        1155 :     R = gaussred_from_QR(M, prec);
    1254        1155 :     if (R) break;
    1255           0 :     prec = precdbl(prec);
    1256           0 :   }
    1257        1155 :   l = lg(R);
    1258        1155 :   for (i=1; i<l; i++) gcoeff(R,i,i) = gen_1;
    1259        1155 :   return R;
    1260             : }
    1261             : 
    1262             : static void
    1263        1140 : init_proj(nflift_t *L, GEN nfT)
    1264             : {
    1265        1140 :   if (L->Tp)
    1266             :   {
    1267          85 :     GEN coTp = FpX_div(FpX_red(nfT, L->p), L->Tp,  L->p); /* Tp's cofactor */
    1268             :     GEN z, proj;
    1269          85 :     z = ZpX_liftfact(nfT, mkvec2(L->Tp, coTp), L->pk, L->p, L->k);
    1270          85 :     L->Tpk = gel(z,1);
    1271          85 :     proj = QXQV_to_FpM(L->topow, L->Tpk, L->pk);
    1272          85 :     if (L->topowden)
    1273          85 :       proj = FpM_red(ZM_Z_mul(proj, Fp_inv(L->topowden, L->pk)), L->pk);
    1274          85 :     L->ZqProj = proj;
    1275             :   }
    1276             :   else
    1277             :   {
    1278        1055 :     L->Tpk = NULL;
    1279        1055 :     L->ZqProj = dim1proj(L->prkHNF);
    1280             :   }
    1281        1140 : }
    1282             : 
    1283             : /* Square of the radius of largest ball inscript in PRK's fundamental domain,
    1284             :  *   whose orthogonalized vector's norms are the Bi
    1285             :  * Rmax ^2 =  min 1/4T_i where T_i = sum ( s_ij^2 / B_j) */
    1286             : static GEN
    1287        1155 : max_radius(GEN PRK, GEN B)
    1288             : {
    1289        1155 :   GEN S, smax = gen_0;
    1290        1155 :   pari_sp av = avma;
    1291        1155 :   long i, j, d = lg(PRK)-1;
    1292             : 
    1293        1155 :   S = RgM_inv( get_R(PRK) ); if (!S) pari_err_PREC("max_radius");
    1294        7850 :   for (i=1; i<=d; i++)
    1295             :   {
    1296        6695 :     GEN s = gen_0;
    1297      102720 :     for (j=1; j<=d; j++)
    1298       96025 :       s = mpadd(s, mpdiv( mpsqr(gcoeff(S,i,j)), gel(B,j)));
    1299        6695 :     if (mpcmp(s, smax) > 0) smax = s;
    1300             :   }
    1301        1155 :   return gerepileupto(av, ginv(gmul2n(smax, 2)));
    1302             : }
    1303             : 
    1304             : static void
    1305        1140 : bestlift_init(long a, GEN nf, GEN pr, GEN C, nflift_t *L)
    1306             : {
    1307        1140 :   const double alpha = 0.99; /* LLL parameter */
    1308        1140 :   const long d = nf_get_degree(nf);
    1309        1140 :   pari_sp av = avma, av2;
    1310             :   GEN prk, PRK, B, GSmin, pk;
    1311             :   pari_timer ti;
    1312             : 
    1313        1140 :   timer_start(&ti);
    1314        1140 :   if (!a) a = (long)bestlift_bound(C, d, alpha, pr_norm(pr));
    1315             : 
    1316          15 :   for (;; avma = av, a += (a==1)? 1: (a>>1)) /* roughly a *= 1.5 */
    1317             :   {
    1318        1155 :     if (DEBUGLEVEL>2) err_printf("exponent %ld\n",a);
    1319        1155 :     prk = idealpows(nf, pr, a);
    1320        1155 :     av2 = avma;
    1321        1155 :     pk = gcoeff(prk,1,1);
    1322        1155 :     PRK = ZM_lll_norms(prk, alpha, LLL_INPLACE, &B);
    1323        1155 :     GSmin = max_radius(PRK, B);
    1324        1155 :     if (gcmp(GSmin, C) >= 0) break;
    1325          15 :   }
    1326        1140 :   gerepileall(av2, 2, &PRK, &GSmin);
    1327        1140 :   if (DEBUGLEVEL>2)
    1328           0 :     err_printf("for this exponent, GSmin = %Ps\nTime reduction: %ld\n",
    1329             :       GSmin, timer_delay(&ti));
    1330        1140 :   L->k = a;
    1331        1140 :   L->den = L->pk = pk;
    1332        1140 :   L->prk = PRK;
    1333        1140 :   L->iprk = ZM_inv(PRK, pk);
    1334        1140 :   L->GSmin= GSmin;
    1335        1140 :   L->prkHNF = prk;
    1336        1140 :   init_proj(L, nf_get_pol(nf));
    1337        1140 : }
    1338             : 
    1339             : /* Let X = Tra * M_L, Y = bestlift(X) return V s.t Y = X - PRK V
    1340             :  * and set *eT2 = gexpo(Y)  [cf nf_bestlift, but memory efficient] */
    1341             : static GEN
    1342         175 : get_V(GEN Tra, GEN M_L, GEN PRK, GEN PRKinv, GEN pk, long *eT2)
    1343             : {
    1344         175 :   long i, e = 0, l = lg(M_L);
    1345         175 :   GEN V = cgetg(l, t_MAT);
    1346         175 :   *eT2 = 0;
    1347        2105 :   for (i = 1; i < l; i++)
    1348             :   { /* cf nf_bestlift(Tra * c) */
    1349        1930 :     pari_sp av = avma, av2;
    1350        1930 :     GEN v, T2 = ZM_ZC_mul(Tra, gel(M_L,i));
    1351             : 
    1352        1930 :     v = gdivround(ZM_ZC_mul(PRKinv, T2), pk); /* small */
    1353        1930 :     av2 = avma;
    1354        1930 :     T2 = ZC_sub(T2, ZM_ZC_mul(PRK, v));
    1355        1930 :     e = gexpo(T2); if (e > *eT2) *eT2 = e;
    1356        1930 :     avma = av2;
    1357        1930 :     gel(V,i) = gerepileupto(av, v); /* small */
    1358             :   }
    1359         175 :   return V;
    1360             : }
    1361             : 
    1362             : static GEN
    1363          35 : nf_LLL_cmbf(nfcmbf_t *T, long rec)
    1364             : {
    1365          35 :   const double BitPerFactor = 0.4; /* nb bits / modular factor */
    1366          35 :   nflift_t *L = T->L;
    1367          35 :   GEN famod = T->fact, ZC = T->ZC, Br = T->Br, P = T->pol, dn = T->L->dn;
    1368          35 :   long dnf = nf_get_degree(T->nf), dP = degpol(P);
    1369             :   long i, C, tmax, n0;
    1370             :   GEN lP, Bnorm, Tra, T2, TT, CM_L, m, list, ZERO, Btra;
    1371             :   double Bhigh;
    1372             :   pari_sp av, av2;
    1373          35 :   long ti_LLL = 0, ti_CF = 0;
    1374             :   pari_timer ti2, TI;
    1375             : 
    1376          35 :   lP = absi(leading_coeff(P));
    1377          35 :   if (is_pm1(lP)) lP = NULL;
    1378             : 
    1379          35 :   n0 = lg(famod) - 1;
    1380             :  /* Lattice: (S PRK), small vector (vS vP). To find k bound for the image,
    1381             :   * write S = S1 q + S0, P = P1 q + P0
    1382             :   * |S1 vS + P1 vP|^2 <= Bhigh for all (vS,vP) assoc. to true factors */
    1383          35 :   Btra = mulrr(ZC, mulur(dP*dP, normlp(Br, 2, dnf)));
    1384          35 :   Bhigh = get_Bhigh(n0, dnf);
    1385          35 :   C = (long)ceil(sqrt(Bhigh/n0)) + 1; /* C^2 n0 ~ Bhigh */
    1386          35 :   Bnorm = dbltor( n0 * C * C + Bhigh );
    1387          35 :   ZERO = zeromat(n0, dnf);
    1388             : 
    1389          35 :   av = avma;
    1390          35 :   TT = cgetg(n0+1, t_VEC);
    1391          35 :   Tra  = cgetg(n0+1, t_MAT);
    1392          35 :   for (i=1; i<=n0; i++) TT[i] = 0;
    1393          35 :   CM_L = scalarmat_s(C, n0);
    1394             :   /* tmax = current number of traces used (and computed so far) */
    1395         130 :   for(tmax = 0;; tmax++)
    1396             :   {
    1397         130 :     long a, b, bmin, bgood, delta, tnew = tmax + 1, r = lg(CM_L)-1;
    1398             :     GEN M_L, q, CM_Lp, oldCM_L, S1, P1, VV;
    1399         130 :     int first = 1;
    1400             : 
    1401             :     /* bound for f . S_k(genuine factor) = ZC * bound for T_2(S_tnew) */
    1402         130 :     Btra = mulrr(ZC, mulur(dP*dP, normlp(Br, 2*tnew, dnf)));
    1403         130 :     bmin = logint(ceil_safe(sqrtr(Btra)), gen_2) + 1;
    1404         130 :     if (DEBUGLEVEL>2)
    1405           0 :       err_printf("\nLLL_cmbf: %ld potential factors (tmax = %ld, bmin = %ld)\n",
    1406             :                  r, tmax, bmin);
    1407             : 
    1408             :     /* compute Newton sums (possibly relifting first) */
    1409         130 :     if (gcmp(L->GSmin, Btra) < 0)
    1410             :     {
    1411             :       GEN polred;
    1412             : 
    1413           0 :       bestlift_init((L->k)<<1, T->nf, T->pr, Btra, L);
    1414           0 :       polred = ZqX_normalize(T->polbase, lP, L);
    1415           0 :       famod = ZqX_liftfact(polred, famod, L->Tpk, L->pk, L->p, L->k);
    1416           0 :       for (i=1; i<=n0; i++) TT[i] = 0;
    1417             :     }
    1418        2450 :     for (i=1; i<=n0; i++)
    1419             :     {
    1420        2320 :       GEN h, lPpow = lP? powiu(lP, tnew): NULL;
    1421        2320 :       GEN z = polsym_gen(gel(famod,i), gel(TT,i), tnew, L->Tpk, L->pk);
    1422        2320 :       gel(TT,i) = z;
    1423        2320 :       h = gel(z,tnew+1);
    1424             :       /* make Newton sums integral */
    1425        2320 :       lPpow = mul_content(lPpow, dn);
    1426        2320 :       if (lPpow)
    1427           0 :         h = (typ(h) == t_INT)? Fp_mul(h, lPpow, L->pk): FpX_Fp_mul(h, lPpow, L->pk);
    1428        2320 :       gel(Tra,i) = nf_bestlift(h, NULL, L); /* S_tnew(famod) */
    1429             :     }
    1430             : 
    1431             :     /* compute truncation parameter */
    1432         130 :     if (DEBUGLEVEL>2) { timer_start(&ti2); timer_start(&TI); }
    1433         130 :     oldCM_L = CM_L;
    1434         130 :     av2 = avma;
    1435         130 :     b = delta = 0; /* -Wall */
    1436             : AGAIN:
    1437         175 :     M_L = Q_div_to_int(CM_L, utoipos(C));
    1438         175 :     VV = get_V(Tra, M_L, L->prk, L->iprk, L->pk, &a);
    1439         175 :     if (first)
    1440             :     { /* initialize lattice, using few p-adic digits for traces */
    1441         130 :       bgood = (long)(a - maxss(32, (long)(BitPerFactor * r)));
    1442         130 :       b = maxss(bmin, bgood);
    1443         130 :       delta = a - b;
    1444             :     }
    1445             :     else
    1446             :     { /* add more p-adic digits and continue reduction */
    1447          45 :       if (a < b) b = a;
    1448          45 :       b = maxss(b-delta, bmin);
    1449          45 :       if (b - delta/2 < bmin) b = bmin; /* near there. Go all the way */
    1450             :     }
    1451             : 
    1452             :     /* restart with truncated entries */
    1453         175 :     q = int2n(b);
    1454         175 :     P1 = gdivround(L->prk, q);
    1455         175 :     S1 = gdivround(Tra, q);
    1456         175 :     T2 = ZM_sub(ZM_mul(S1, M_L), ZM_mul(P1, VV));
    1457         175 :     m = vconcat( CM_L, T2 );
    1458         175 :     if (first)
    1459             :     {
    1460         130 :       first = 0;
    1461         130 :       m = shallowconcat( m, vconcat(ZERO, P1) );
    1462             :       /*     [ C M_L   0  ]
    1463             :        * m = [            ]   square matrix
    1464             :        *     [  T2'   PRK ]   T2' = Tra * M_L  truncated
    1465             :        */
    1466             :     }
    1467         175 :     CM_L = LLL_check_progress(Bnorm, n0, m, b == bmin, /*dbg:*/ &ti_LLL);
    1468         175 :     if (DEBUGLEVEL>2)
    1469           0 :       err_printf("LLL_cmbf: (a,b) =%4ld,%4ld; r =%3ld -->%3ld, time = %ld\n",
    1470           0 :                  a,b, lg(m)-1, CM_L? lg(CM_L)-1: 1, timer_delay(&TI));
    1471         210 :     if (!CM_L) { list = mkcol(RgX_int_normalize(P)); break; }
    1472         150 :     if (b > bmin)
    1473             :     {
    1474          45 :       CM_L = gerepilecopy(av2, CM_L);
    1475          45 :       goto AGAIN;
    1476             :     }
    1477         105 :     if (DEBUGLEVEL>2) timer_printf(&ti2, "for this trace");
    1478             : 
    1479         105 :     i = lg(CM_L) - 1;
    1480         105 :     if (i == r && ZM_equal(CM_L, oldCM_L))
    1481             :     {
    1482          50 :       CM_L = oldCM_L;
    1483          50 :       avma = av2; continue;
    1484             :     }
    1485             : 
    1486          55 :     CM_Lp = FpM_image(CM_L, utoipos(27449)); /* inexpensive test */
    1487          55 :     if (lg(CM_Lp) != lg(CM_L))
    1488             :     {
    1489           0 :       if (DEBUGLEVEL>2) err_printf("LLL_cmbf: rank decrease\n");
    1490           0 :       CM_L = ZM_hnf(CM_L);
    1491             :     }
    1492             : 
    1493          55 :     if (i <= r && i*rec < n0)
    1494             :     {
    1495             :       pari_timer ti;
    1496          20 :       if (DEBUGLEVEL>2) timer_start(&ti);
    1497          20 :       list = nf_chk_factors(T, P, Q_div_to_int(CM_L,utoipos(C)), famod, L->pk);
    1498          20 :       if (DEBUGLEVEL>2) ti_CF += timer_delay(&ti);
    1499          20 :       if (list) break;
    1500             :     }
    1501          45 :     CM_L = gerepilecopy(av2, CM_L);
    1502          45 :     if (gc_needed(av,1))
    1503             :     {
    1504           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"nf_LLL_cmbf");
    1505           0 :       gerepileall(av, L->Tpk? 9: 8,
    1506             :                       &CM_L,&TT,&Tra,&famod,&L->pk,&L->GSmin,&L->prk,&L->iprk,&L->Tpk);
    1507             :     }
    1508          95 :   }
    1509          35 :   if (DEBUGLEVEL>2)
    1510           0 :     err_printf("* Time LLL: %ld\n* Time Check Factor: %ld\n",ti_LLL,ti_CF);
    1511          35 :   return list;
    1512             : }
    1513             : 
    1514             : static GEN
    1515         270 : nf_combine_factors(nfcmbf_t *T, GEN polred, long klim)
    1516             : {
    1517         270 :   nflift_t *L = T->L;
    1518             :   GEN res;
    1519             :   long maxK;
    1520             :   int done;
    1521             :   pari_timer ti;
    1522             : 
    1523         270 :   if (DEBUGLEVEL>2) timer_start(&ti);
    1524         270 :   T->fact = ZqX_liftfact(polred, T->fact, L->Tpk, L->pk, L->p, L->k);
    1525         270 :   if (DEBUGLEVEL>2) timer_printf(&ti, "Hensel lift");
    1526         270 :   res = nfcmbf(T, klim, &maxK, &done);
    1527         270 :   if (DEBUGLEVEL>2) timer_printf(&ti, "Naive recombination");
    1528         270 :   if (!done)
    1529             :   {
    1530          35 :     long l = lg(res)-1;
    1531             :     GEN v;
    1532          35 :     if (l > 1)
    1533             :     {
    1534           5 :       T->pol = gel(res,l);
    1535           5 :       T->polbase = RgX_to_nfX(T->nf, T->pol);
    1536             :     }
    1537          35 :     v = nf_LLL_cmbf(T, maxK);
    1538             :     /* remove last elt, possibly unfactored. Add all new ones. */
    1539          35 :     setlg(res, l); res = shallowconcat(res, v);
    1540             :   }
    1541         270 :   return res;
    1542             : }
    1543             : 
    1544             : static GEN
    1545         575 : nf_DDF_roots(GEN pol, GEN polred, GEN nfpol, long fl, nflift_t *L)
    1546             : {
    1547             :   GEN z, Cltx_r, ltdn;
    1548             :   long i, m;
    1549             :   div_data D;
    1550             : 
    1551         575 :   init_div_data(&D, pol, L);
    1552         575 :   ltdn = mul_content(D.lt, L->dn);
    1553         575 :   z = ZqX_roots(polred, L->Tpk, L->p, L->k);
    1554         575 :   Cltx_r = deg1pol_shallow(D.Clt? D.Clt: gen_1, NULL, varn(pol));
    1555        2235 :   for (m=1,i=1; i<lg(z); i++)
    1556             :   {
    1557        1660 :     GEN q, r = gel(z,i);
    1558             :     pari_sp av;
    1559             :     /* lt*dn*topowden * r = Clt * r */
    1560        1660 :     r = nf_bestlift_to_pol(ltdn? gmul(ltdn,r): r, NULL, L);
    1561        1660 :     av = avma;
    1562        1660 :     gel(Cltx_r,2) = gneg(r); /* check P(r) == 0 */
    1563        1660 :     q = RgXQX_divrem(D.C2ltpol, Cltx_r, nfpol, ONLY_DIVIDES); /* integral */
    1564        1660 :     avma = av;
    1565             :     /* don't go on with q, usually much larger that C2ltpol */
    1566        1660 :     if (q) {
    1567        1550 :       if (D.Clt) r = gdiv(r, D.Clt);
    1568        1550 :       gel(z,m++) = r;
    1569             :     }
    1570         110 :     else if (fl == ROOTS_SPLIT) return cgetg(1, t_VEC);
    1571             :   }
    1572         575 :   z[0] = evaltyp(t_VEC) | evallg(m);
    1573         575 :   return z;
    1574             : }
    1575             : 
    1576             : /* returns a factor of T in Fp of degree <= maxf, NULL if none exist */
    1577             : static GEN
    1578       19450 : get_good_factor(GEN T, ulong p, long maxf)
    1579             : {
    1580       19450 :   pari_sp av = avma;
    1581       19450 :   GEN r, list = gel(Flx_factor(ZX_to_Flx(T,p),p), 1);
    1582       19450 :   if (maxf == 1)
    1583             :   { /* deg.1 factors are best */
    1584       18670 :     r = gel(list,1);
    1585       18670 :     if (degpol(r) == 1) return r;
    1586             :   }
    1587             :   else
    1588             :   { /* otherwise, pick factor of largish degree */
    1589             :     long i, dr;
    1590        1450 :     for (i = lg(list)-1; i > 0; i--)
    1591             :     {
    1592        1055 :       r = gel(list,i); dr = degpol(r);
    1593        1055 :       if (dr <= maxf) return r;
    1594             :     }
    1595             :   }
    1596       14155 :   avma = av; return NULL; /* failure */
    1597             : }
    1598             : 
    1599             : /* Optimization problem: factorization of polynomials over large Fq is slow,
    1600             :  * BUT bestlift correspondingly faster.
    1601             :  * Return maximal residue degree to be considered when picking a prime ideal */
    1602             : static long
    1603        1275 : get_maxf(long nfdeg)
    1604             : {
    1605        1275 :   long maxf = 1;
    1606        1275 :   if      (nfdeg >= 45) maxf =32;
    1607        1265 :   else if (nfdeg >= 30) maxf =16;
    1608        1255 :   else if (nfdeg >= 15) maxf = 8;
    1609        1275 :   return maxf;
    1610             : }
    1611             : 
    1612             : /* Select a prime ideal pr over which to factor polbase.
    1613             :  * Return the number of factors (or roots, according to flag fl) mod pr,
    1614             :  * Input:
    1615             :  *   ct: number of attempts to find best
    1616             :  * Set:
    1617             :  *   lt: leading term of polbase (t_INT or NULL [ for 1 ])
    1618             :  *   pr: a suitable maximal ideal
    1619             :  *   Fa: factors found mod pr
    1620             :  *   Tp: polynomial defining Fq/Fp */
    1621             : static long
    1622         980 : nf_pick_prime(long ct, GEN nf, GEN polbase, long fl,
    1623             :               GEN *lt, GEN *pr, GEN *Tp)
    1624             : {
    1625         980 :   GEN nfpol = nf_get_pol(nf), bad = mulii(nf_get_disc(nf), nf_get_index(nf));
    1626         980 :   long maxf, nfdeg = degpol(nfpol), dpol = degpol(polbase), nbf = 0;
    1627             :   ulong pp;
    1628             :   forprime_t S;
    1629             :   pari_timer ti_pr;
    1630             : 
    1631         980 :   if (DEBUGLEVEL>3) timer_start(&ti_pr);
    1632         980 :   *lt  = leading_coeff(polbase); /* t_INT */
    1633         980 :   if (gequal1(*lt)) *lt = NULL;
    1634         980 :   *pr = NULL;
    1635         980 :   *Tp = NULL;
    1636             : 
    1637         980 :   maxf = get_maxf(nfdeg);
    1638         980 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    1639             :   /* select pr such that pol has the smallest number of factors, ct attempts */
    1640         980 :   while ((pp = u_forprime_next(&S)))
    1641             :   {
    1642             :     GEN aT, apr, ap, amodpr, red, r;
    1643             :     long anbf;
    1644       19300 :     ulong ltp = 0;
    1645       19300 :     pari_sp av2 = avma;
    1646             : 
    1647             :     /* first step : select prime of high inertia degree */
    1648       34125 :     if (! umodiu(bad,pp)) continue;
    1649       17210 :     if (*lt) { ltp = umodiu(*lt, pp); if (!ltp) continue; }
    1650       17065 :     r = get_good_factor(nfpol, pp, maxf);
    1651       17065 :     if (!r) continue;
    1652             : 
    1653        5000 :     ap = utoipos(pp);
    1654        5000 :     apr = idealprimedec_kummer(nf, Flx_to_ZX(r), 1, ap);
    1655        5000 :     amodpr = zk_to_Fq_init(nf,&apr,&aT,&ap);
    1656             : 
    1657             :     /* second step : evaluate factorisation mod apr */
    1658        5000 :     red = nfX_to_FqX(polbase, nf, amodpr);
    1659        5000 :     if (!aT)
    1660             :     { /* degree 1 */
    1661        4655 :       red = ZX_to_Flx(red, pp);
    1662        4655 :       if (ltp) red = Flx_normalize(red, pp);
    1663        4655 :       if (!Flx_is_squarefree(red, pp)) { avma = av2; continue; }
    1664        4190 :       anbf = fl == FACTORS? Flx_nbfact(red, pp): Flx_nbroots(red, pp);
    1665             :     }
    1666             :     else
    1667             :     {
    1668         345 :       if (ltp) red = FqX_normalize(red, aT,ap);
    1669         345 :       if (!FqX_is_squarefree(red,aT,ap)) { avma = av2; continue; }
    1670         470 :       anbf = fl == FACTORS? FqX_nbfact(red, aT, ap)
    1671         470 :                           : FqX_nbroots(red, aT, ap);
    1672             :     }
    1673        4610 :     if (fl == ROOTS_SPLIT && anbf < dpol) return anbf;
    1674        4445 :     if (anbf <= 1)
    1675             :     {
    1676         325 :       if (fl == FACTORS) return anbf; /* irreducible */
    1677         295 :       if (!anbf) return 0; /* no root */
    1678             :     }
    1679        4340 :     if (DEBUGLEVEL>3)
    1680           0 :       err_printf("%3ld %s at prime\n  %Ps\nTime: %ld\n",
    1681             :                  anbf, fl == FACTORS?"factors": "roots", apr, timer_delay(&ti_pr));
    1682             : 
    1683        4340 :     if (!nbf || anbf < nbf
    1684        3345 :              || (anbf == nbf && pr_get_f(apr) > pr_get_f(*pr)))
    1685             :     {
    1686        1005 :       nbf = anbf;
    1687        1005 :       *pr = apr;
    1688        1005 :       *Tp = aT;
    1689             :     }
    1690        3335 :     else avma = av2;
    1691        4340 :     if (--ct <= 0) break;
    1692             :   }
    1693         845 :   if (!nbf) pari_err_OVERFLOW("nf_pick_prime [ran out of primes]");
    1694         845 :   return nbf;
    1695             : }
    1696             : 
    1697             : /* assume lt(T) is a t_INT and T square free */
    1698             : static GEN
    1699         160 : nfsqff_trager(GEN u, GEN T, GEN dent)
    1700             : {
    1701         160 :   long k = 0, i, lx;
    1702         160 :   GEN U, P, x0, mx0, fa, n = ZX_ZXY_rnfequation(T, u, &k);
    1703             :   int tmonic;
    1704         160 :   if (DEBUGLEVEL>4) err_printf("nfsqff_trager: choosing k = %ld\n",k);
    1705             : 
    1706             :   /* n guaranteed to be squarefree */
    1707         160 :   fa = ZX_DDF(Q_primpart(n)); lx = lg(fa);
    1708         160 :   if (lx == 2) return mkcol(u);
    1709             : 
    1710         105 :   tmonic = is_pm1(leading_coeff(T));
    1711         105 :   P = cgetg(lx,t_COL);
    1712         105 :   x0 = deg1pol_shallow(stoi(-k), gen_0, varn(T));
    1713         105 :   mx0 = deg1pol_shallow(stoi(k), gen_0, varn(T));
    1714         105 :   U = RgXQX_translate(u, mx0, T);
    1715         105 :   if (!tmonic) U = Q_primpart(U);
    1716         450 :   for (i=lx-1; i>0; i--)
    1717             :   {
    1718         345 :     GEN f = gel(fa,i), F = nfgcd(U, f, T, dent);
    1719         345 :     F = RgXQX_translate(F, x0, T);
    1720             :     /* F = gcd(f, u(t - x0)) [t + x0] = gcd(f(t + x0), u), more efficient */
    1721         345 :     if (typ(F) != t_POL || degpol(F) == 0)
    1722           0 :       pari_err_IRREDPOL("factornf [modulus]",T);
    1723         345 :     gel(P,i) = QXQX_normalize(F, T);
    1724             :   }
    1725         105 :   gen_sort_inplace(P, (void*)&cmp_RgX, &gen_cmp_RgX, NULL);
    1726         105 :   return P;
    1727             : }
    1728             : 
    1729             : /* Factor polynomial a on the number field defined by polynomial T, using
    1730             :  * Trager's trick */
    1731             : GEN
    1732          80 : polfnf(GEN a, GEN T)
    1733             : {
    1734          80 :   GEN rep = cgetg(3, t_MAT), A, B, y, dent, bad;
    1735             :   long dA;
    1736             :   int tmonic;
    1737             : 
    1738          80 :   if (typ(a)!=t_POL) pari_err_TYPE("polfnf",a);
    1739          80 :   if (typ(T)!=t_POL) pari_err_TYPE("polfnf",T);
    1740          80 :   T = Q_primpart(T); tmonic = is_pm1(leading_coeff(T));
    1741          80 :   RgX_check_ZX(T,"polfnf");
    1742          80 :   A = Q_primpart( QXQX_normalize(RgX_nffix("polfnf",T,a,1), T) );
    1743          80 :   dA = degpol(A);
    1744          80 :   if (dA <= 0)
    1745             :   {
    1746           0 :     avma = (pari_sp)(rep + 3);
    1747           0 :     return (dA == 0)? trivial_fact(): zerofact(varn(A));
    1748             :   }
    1749          80 :   bad = dent = ZX_disc(T);
    1750          80 :   if (tmonic) dent = indexpartial(T, dent);
    1751          80 :   (void)nfgcd_all(A,RgX_deriv(A), T, dent, &B);
    1752          80 :   if (degpol(B) != dA) B = Q_primpart( QXQX_normalize(B, T) );
    1753          80 :   ensure_lt_INT(B);
    1754          80 :   y = nfsqff_trager(B, T, dent);
    1755          80 :   fact_from_sqff(rep, A, B, y, T, bad);
    1756          80 :   return sort_factor_pol(rep, cmp_RgX);
    1757             : }
    1758             : 
    1759             : static int
    1760        2065 : nfsqff_use_Trager(long n, long dpol)
    1761             : {
    1762        2065 :   return dpol*3<n;
    1763             : }
    1764             : 
    1765             : /* return the factorization of the square-free polynomial pol. Not memory-clean
    1766             :    The coeffs of pol are in Z_nf and its leading term is a rational integer.
    1767             :    deg(pol) > 0, deg(nfpol) > 1
    1768             :    fl is either FACTORS (return factors), or ROOTS / ROOTS_SPLIT (return roots):
    1769             :      - ROOTS, return only the roots of x in nf
    1770             :      - ROOTS_SPLIT, as ROOTS if pol splits, [] otherwise
    1771             :    den is usually 1, otherwise nf.zk is doubtful, and den bounds the
    1772             :    denominator of an arbitrary element of Z_nf on nf.zk */
    1773             : static GEN
    1774        1340 : nfsqff(GEN nf, GEN pol, long fl, GEN den)
    1775             : {
    1776        1340 :   long n, nbf, dpol = degpol(pol);
    1777             :   GEN pr, C0, polbase;
    1778        1340 :   GEN N2, res, polred, lt, nfpol = typ(nf)==t_POL?nf:nf_get_pol(nf);
    1779             :   nfcmbf_t T;
    1780             :   nflift_t L;
    1781             :   pari_timer ti, ti_tot;
    1782             : 
    1783        1340 :   if (DEBUGLEVEL>2) { timer_start(&ti); timer_start(&ti_tot); }
    1784        1340 :   n = degpol(nfpol);
    1785             :   /* deg = 1 => irreducible */
    1786        1340 :   if (dpol == 1) {
    1787         280 :     if (fl == FACTORS) return mkvec(QXQX_normalize(pol, nfpol));
    1788         275 :     return mkvec(gneg(gdiv(gel(pol,2),gel(pol,3))));
    1789             :   }
    1790        1060 :   if (typ(nf)==t_POL || nfsqff_use_Trager(n,dpol))
    1791             :   {
    1792             :     GEN z;
    1793          80 :     if (DEBUGLEVEL>2) err_printf("Using Trager's method\n");
    1794          80 :     if (typ(nf) != t_POL) den =  mulii(den, nf_get_index(nf));
    1795          80 :     z = nfsqff_trager(Q_primpart(pol), nfpol, den);
    1796          80 :     if (fl != FACTORS) {
    1797          65 :       long i, l = lg(z);
    1798         205 :       for (i = 1; i < l; i++)
    1799             :       {
    1800         155 :         GEN LT, t = gel(z,i); if (degpol(t) > 1) break;
    1801         140 :         LT = gel(t,3);
    1802         140 :         if (typ(LT) == t_POL) LT = gel(LT,2); /* constant */
    1803         140 :         gel(z,i) = gdiv(gel(t,2), negi(LT));
    1804             :       }
    1805          65 :       setlg(z, i);
    1806          65 :       if (fl == ROOTS_SPLIT && i != l) return cgetg(1,t_VEC);
    1807             :     }
    1808          80 :     return z;
    1809             :   }
    1810             : 
    1811         980 :   polbase = RgX_to_nfX(nf, pol);
    1812         980 :   nbf = nf_pick_prime(5, nf, polbase, fl, &lt, &pr, &L.Tp);
    1813         980 :   if (fl == ROOTS_SPLIT && nbf < dpol) return cgetg(1,t_VEC);
    1814         950 :   if (nbf <= 1)
    1815             :   {
    1816         165 :     if (fl == FACTORS) return mkvec(QXQX_normalize(pol, nfpol)); /* irred. */
    1817         135 :     if (!nbf) return cgetg(1,t_VEC); /* no root */
    1818             :   }
    1819             : 
    1820         845 :   if (DEBUGLEVEL>2) {
    1821           0 :     timer_printf(&ti, "choice of a prime ideal");
    1822           0 :     err_printf("Prime ideal chosen: %Ps\n", pr);
    1823             :   }
    1824         845 :   L.tozk = nf_get_invzk(nf);
    1825         845 :   L.topow= nf_get_zkprimpart(nf);
    1826         845 :   L.topowden = nf_get_zkden(nf);
    1827         845 :   if (is_pm1(den)) den = NULL;
    1828         845 :   L.dn = den;
    1829         845 :   T.ZC = L2_bound(nf, den);
    1830         845 :   T.Br = nf_root_bounds(pol, nf); if (lt) T.Br = gmul(T.Br, lt);
    1831             : 
    1832             :   /* C0 = bound for T_2(Q_i), Q | P */
    1833         845 :   if (fl != FACTORS) C0 = normlp(T.Br, 2, n);
    1834         270 :   else               C0 = nf_factor_bound(nf, polbase);
    1835         845 :   T.bound = mulrr(T.ZC, C0); /* bound for |Q_i|^2 in Z^n on chosen Z-basis */
    1836             : 
    1837         845 :   N2 = mulur(dpol*dpol, normlp(T.Br, 4, n)); /* bound for T_2(lt * S_2) */
    1838         845 :   T.BS_2 = mulrr(T.ZC, N2); /* bound for |S_2|^2 on chosen Z-basis */
    1839             : 
    1840         845 :   if (DEBUGLEVEL>2) {
    1841           0 :     timer_printf(&ti, "bound computation");
    1842           0 :     err_printf("  1) T_2 bound for %s: %Ps\n",
    1843             :                fl == FACTORS?"factor": "root", C0);
    1844           0 :     err_printf("  2) Conversion from T_2 --> | |^2 bound : %Ps\n", T.ZC);
    1845           0 :     err_printf("  3) Final bound: %Ps\n", T.bound);
    1846             :   }
    1847             : 
    1848         845 :   L.p = pr_get_p(pr);
    1849         845 :   if (L.Tp && degpol(L.Tp) == 1) L.Tp = NULL;
    1850         845 :   bestlift_init(0, nf, pr, T.bound, &L);
    1851         845 :   if (DEBUGLEVEL>2) timer_start(&ti);
    1852         845 :   polred = ZqX_normalize(polbase, lt, &L); /* monic */
    1853             : 
    1854         845 :   if (fl != FACTORS) {
    1855         575 :     GEN z = nf_DDF_roots(pol, polred, nfpol, fl, &L);
    1856         575 :     if (lg(z) == 1) return cgetg(1, t_VEC);
    1857         565 :     return z;
    1858             :   }
    1859             : 
    1860         270 :   T.fact = gel(FqX_factor(polred, L.Tp, L.p), 1);
    1861         270 :   if (DEBUGLEVEL>2) timer_printf(&ti, "splitting mod %Ps", pr);
    1862         270 :   T.pr = pr;
    1863         270 :   T.L  = &L;
    1864         270 :   T.polbase = polbase;
    1865         270 :   T.pol   = pol;
    1866         270 :   T.nf    = nf;
    1867         270 :   res = nf_combine_factors(&T, polred, dpol-1);
    1868         270 :   if (DEBUGLEVEL>2)
    1869           0 :     err_printf("Total Time: %ld\n===========\n", timer_delay(&ti_tot));
    1870         270 :   return res;
    1871             : }
    1872             : 
    1873             : /* assume pol monic in nf.zk[X] */
    1874             : GEN
    1875          60 : nfroots_if_split(GEN *pnf, GEN pol)
    1876             : {
    1877          60 :   GEN T = get_nfpol(*pnf,pnf), den = fix_nf(pnf, &T, &pol);
    1878          60 :   pari_sp av = avma;
    1879          60 :   GEN z = nfsqff(*pnf, pol, ROOTS_SPLIT, den);
    1880          60 :   if (lg(z) == 1) { avma = av; return NULL; }
    1881          30 :   return gerepilecopy(av, z);
    1882             : }
    1883             : 
    1884             : /*******************************************************************/
    1885             : /*                                                                 */
    1886             : /*              Roots of unity in a number field                   */
    1887             : /*     (alternative to nfrootsof1 using factorization in K[X])     */
    1888             : /*                                                                 */
    1889             : /*******************************************************************/
    1890             : /* Code adapted from nffactor. Structure of the algorithm; only step 1 is
    1891             :  * specific to roots of unity.
    1892             :  *
    1893             :  * [Step 1]: guess roots via ramification. If trivial output this.
    1894             :  * [Step 2]: select prime [p] unramified and ideal [pr] above
    1895             :  * [Step 3]: evaluate the maximal exponent [k] such that the fondamental domain
    1896             :  *           of a LLL-reduction of [prk] = pr^k contains a ball of radius larger
    1897             :  *           than the norm of any root of unity.
    1898             :  * [Step 3]: select a heuristic exponent,
    1899             :  *           LLL reduce prk=pr^k and verify the exponent is sufficient,
    1900             :  *           otherwise try a larger one.
    1901             :  * [Step 4]: factor the cyclotomic polynomial mod [pr],
    1902             :  *           Hensel lift to pr^k and find the representative in the ball
    1903             :  *           If there is it is a primitive root */
    1904             : 
    1905             : typedef struct {
    1906             :   GEN q;
    1907             :   GEN modpr;
    1908             :   GEN pr;
    1909             :   nflift_t *L;
    1910             : } prklift_t;
    1911             : 
    1912             : /* Choose prime ideal unramified with "large" inertia degree */
    1913             : static void
    1914         295 : nf_pick_prime_for_units(GEN nf, prklift_t *P)
    1915             : {
    1916         295 :   GEN nfpol = nf_get_pol(nf), bad = mulii(nf_get_disc(nf), nf_get_index(nf));
    1917         295 :   GEN aT, amodpr, apr, ap = NULL, r = NULL;
    1918         295 :   long nfdeg = degpol(nfpol), maxf = get_maxf(nfdeg);
    1919             :   ulong pp;
    1920             :   forprime_t S;
    1921             : 
    1922         295 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    1923         295 :   while ( (pp = u_forprime_next(&S)) )
    1924             :   {
    1925        3155 :     if (! umodiu(bad,pp)) continue;
    1926        2385 :     r = get_good_factor(nfpol, pp, maxf);
    1927        2385 :     if (r) break;
    1928             :   }
    1929         295 :   if (!r) pari_err_OVERFLOW("nf_pick_prime [ran out of primes]");
    1930         295 :   ap = utoipos(pp);
    1931         295 :   apr = idealprimedec_kummer(nf, Flx_to_ZX(r), 1, ap);
    1932         295 :   amodpr = zk_to_Fq_init(nf,&apr,&aT,&ap);
    1933         295 :   P->pr = apr;
    1934         295 :   P->q = pr_norm(apr);
    1935         295 :   P->modpr = amodpr;
    1936         295 :   P->L->p = ap;
    1937         295 :   P->L->Tp = aT;
    1938         295 :   P->L->tozk = nf_get_invzk(nf);
    1939         295 :   P->L->topow = nf_get_zkprimpart(nf);
    1940         295 :   P->L->topowden = nf_get_zkden(nf);
    1941         295 : }
    1942             : 
    1943             : /* *Heuristic* exponent k such that the fundamental domain of pr^k
    1944             :  * should contain the ball of radius C */
    1945             : static double
    1946         295 : mybestlift_bound(GEN C)
    1947             : {
    1948         295 :   C = gtofp(C,DEFAULTPREC);
    1949             : #if 0 /* d = nf degree, Npr = Norm(pr) */
    1950             :   const double alpha = 0.99; /* LLL parameter */
    1951             :   const double y = 1 / (alpha - 0.25); /* = 2 if alpha = 3/4 */
    1952             :   double t;
    1953             :   t = rtodbl(mplog(gmul2n(divru(C,d), 4))) * 0.5 + (d-1) * log(1.5 * sqrt(y));
    1954             :   return ceil((t * d) / log(gtodouble(Npr))); /* proved upper bound */
    1955             : #endif
    1956         295 :   return ceil(log(gtodouble(C)) / 0.2) + 3;
    1957             : }
    1958             : 
    1959             : /* simplified nf_DDF_roots: polcyclo(n) monic in ZX either splits or has no
    1960             :  * root in nf.
    1961             :  * Return a root or NULL (no root) */
    1962             : static GEN
    1963         305 : nfcyclo_root(long n, GEN nfpol, nflift_t *L)
    1964             : {
    1965         305 :   GEN q, r, Cltx_r, pol = polcyclo(n,0), gn = utoipos(n);
    1966             :   div_data D;
    1967             : 
    1968         305 :   init_div_data(&D, pol, L);
    1969         305 :   (void)Fq_sqrtn(gen_1, gn, L->Tp, L->p, &r);
    1970             :   /* r primitive n-th root of 1 in Fq */
    1971         305 :   r = Zq_sqrtnlift(gen_1, gn, r, L->Tpk, L->p, L->k);
    1972             :   /* lt*dn*topowden * r = Clt * r */
    1973         305 :   r = nf_bestlift_to_pol(r, NULL, L);
    1974         305 :   Cltx_r = deg1pol_shallow(D.Clt? D.Clt: gen_1, gneg(r), varn(pol));
    1975             :   /* check P(r) == 0 */
    1976         305 :   q = RgXQX_divrem(D.C2ltpol, Cltx_r, nfpol, ONLY_DIVIDES); /* integral */
    1977         305 :   if (!q) return NULL;
    1978         285 :   if (D.Clt) r = gdiv(r, D.Clt);
    1979         285 :   return r;
    1980             : }
    1981             : 
    1982             : /* Guesses the number of roots of unity in number field [nf].
    1983             :  * Computes gcd of N(P)-1 for some primes. The value returned is a proven
    1984             :  * multiple of the correct value. */
    1985             : static long
    1986         685 : guess_roots(GEN nf)
    1987             : {
    1988         685 :   long c = 0, nfdegree = nf_get_degree(nf), B = nfdegree + 20, l;
    1989         685 :   ulong p = 2;
    1990         685 :   GEN T = nf_get_pol(nf), D = nf_get_disc(nf), index = nf_get_index(nf);
    1991         685 :   GEN nbroots = NULL;
    1992             :   forprime_t S;
    1993             :   pari_sp av;
    1994             : 
    1995         685 :   (void)u_forprime_init(&S, 3, ULONG_MAX);
    1996         685 :   av = avma;
    1997             :   /* result must be stationary (counter c) for at least B loops */
    1998       23600 :   for (l=1; (p = u_forprime_next(&S)); l++)
    1999             :   {
    2000             :     GEN old, F, pf_1, Tp;
    2001       23600 :     long i, nb, gcdf = 0;
    2002             : 
    2003       23600 :     if (!umodiu(D,p) || !umodiu(index,p)) continue;
    2004       22375 :     Tp = ZX_to_Flx(T,p); /* squarefree */
    2005       22375 :     F = Flx_nbfact_by_degree(Tp, &nb, p);
    2006             :     /* the gcd of the p^f - 1 is p^(gcd of the f's) - 1 */
    2007      194740 :     for (i = 1; i <= nfdegree; i++)
    2008      178350 :       if (F[i]) {
    2009       22645 :         gcdf = gcdf? cgcd(gcdf, i): i;
    2010       22645 :         if (gcdf == 1) break;
    2011             :       }
    2012       22375 :     pf_1 = subiu(powuu(p, gcdf), 1);
    2013       22375 :     old = nbroots;
    2014       22375 :     nbroots = nbroots? gcdii(pf_1, nbroots): pf_1;
    2015       22375 :     if (DEBUGLEVEL>5)
    2016           0 :       err_printf("p=%lu; gcf(f(P/p))=%ld; nbroots | %Ps",p, gcdf, nbroots);
    2017             :     /* if same result go on else reset the stop counter [c] */
    2018       22375 :     if (old && equalii(nbroots,old))
    2019       20830 :     { if (!is_bigint(nbroots) && ++c > B) break; }
    2020             :     else
    2021        1545 :       c = 0;
    2022             :   }
    2023         685 :   if (!nbroots) pari_err_OVERFLOW("guess_roots [ran out of primes]");
    2024         685 :   if (DEBUGLEVEL>5) err_printf("%ld loops\n",l);
    2025         685 :   avma = av; return itos(nbroots);
    2026             : }
    2027             : 
    2028             : /* T(x) an irreducible ZX. Is it of the form Phi_n(c \pm x) ?
    2029             :  * Return NULL if not, and a root of 1 of maximal order in Z[x]/(T) otherwise
    2030             :  *
    2031             :  * N.B. Set n_squarefree = 1 if n is squarefree, and 0 otherwise.
    2032             :  * This last parameter is inconvenient, but it allows a cheap
    2033             :  * stringent test. (n guessed from guess_roots())*/
    2034             : static GEN
    2035         225 : ZXirred_is_cyclo_translate(GEN T, long n_squarefree)
    2036             : {
    2037         225 :   long r, m, d = degpol(T);
    2038         225 :   GEN T1, c = divis_rem(gel(T, d+1), d, &r); /* d-1 th coeff divisible by d ? */
    2039             :   /* The trace coefficient of polcyclo(n) is \pm1 if n is square free, and 0
    2040             :    * otherwise. */
    2041         225 :   if (!n_squarefree)
    2042         105 :   { if (r) return NULL; }
    2043             :   else
    2044             :   {
    2045         120 :     if (r < -1)
    2046             :     {
    2047           0 :       r += d;
    2048           0 :       c = subiu(c, 1);
    2049             :     }
    2050         120 :     else if (r == d-1)
    2051             :     {
    2052          25 :       r = -1;
    2053          25 :       c = addiu(c, 1);
    2054             :     }
    2055         120 :     if (r != 1 && r != -1) return NULL;
    2056             :   }
    2057         190 :   if (signe(c)) /* presumably Phi_guess(c \pm x) */
    2058          25 :     T = RgX_translate(T, negi(c));
    2059         190 :   if (!n_squarefree) T = RgX_deflate_max(T, &m);
    2060             :   /* presumably Phi_core(guess)(\pm x), cyclotomic iff original T was */
    2061         190 :   T1 = ZX_graeffe(T);
    2062         190 :   if (ZX_equal(T, T1)) /* T = Phi_n, n odd */
    2063          25 :     return deg1pol_shallow(gen_m1, negi(c), varn(T));
    2064         165 :   else if (ZX_equal(T1, ZX_unscale(T, gen_m1))) /* T = Phi_{2n}, nodd */
    2065         150 :     return deg1pol_shallow(gen_1, c, varn(T));
    2066          15 :   return NULL;
    2067             : }
    2068             : 
    2069             : static GEN
    2070        1105 : trivroots(void) { return mkvec2(gen_2, gen_m1); }
    2071             : /* Number of roots of unity in number field [nf]. */
    2072             : GEN
    2073        1575 : rootsof1(GEN nf)
    2074             : {
    2075             :   prklift_t P;
    2076             :   nflift_t L;
    2077             :   GEN fa, LP, LE, C0, z, prim_root, disc, nfpol;
    2078             :   pari_timer ti;
    2079             :   long i, l, nbguessed, nbroots, nfdegree;
    2080             :   pari_sp av;
    2081             : 
    2082        1575 :   nf = checknf(nf);
    2083        1575 :   if (nf_get_r1(nf)) return trivroots();
    2084             : 
    2085             :   /* Step 1 : guess number of roots and discard trivial case 2 */
    2086         685 :   if (DEBUGLEVEL>2) timer_start(&ti);
    2087         685 :   nbguessed = guess_roots(nf);
    2088         685 :   if (DEBUGLEVEL>2)
    2089           0 :     timer_printf(&ti, "guessing roots of 1 [guess = %ld]", nbguessed);
    2090         685 :   if (nbguessed == 2) return trivroots();
    2091             : 
    2092         470 :   nfdegree = nf_get_degree(nf);
    2093         470 :   fa = factoru(nbguessed);
    2094         470 :   LP = gel(fa,1); l = lg(LP);
    2095         470 :   LE = gel(fa,2);
    2096         470 :   disc = nf_get_disc(nf);
    2097        1285 :   for (i = 1; i < l; i++)
    2098             :   {
    2099         815 :     long p = LP[i];
    2100             :     /* Degree and ramification test: find largest k such that Q(zeta_{p^k})
    2101             :      * may be a subfield of K. Q(zeta_p^k) has degree (p-1)p^(k-1)
    2102             :      * and v_p(discriminant) = ((p-1)k-1)p^(k-1); so we must have
    2103             :      * v_p(disc_K) >= ((p-1)k-1) * n / (p-1) = kn - q, where q = n/(p-1) */
    2104         815 :     if (p == 2)
    2105             :     { /* the test simplifies a little in that case */
    2106             :       long v, vnf, k;
    2107         470 :       if (LE[i] == 1) continue;
    2108         165 :       vnf = vals(nfdegree);
    2109         165 :       v = vali(disc);
    2110         185 :       for (k = minss(LE[i], vnf+1); k >= 1; k--)
    2111         185 :         if (v >= nfdegree*(k-1)) { nbguessed >>= LE[i]-k; LE[i] = k; break; }
    2112             :       /* N.B the test above always works for k = 1: LE[i] >= 1 */
    2113             :     }
    2114             :     else
    2115             :     {
    2116             :       long v, vnf, k;
    2117         345 :       ulong r, q = udivuu_rem(nfdegree, p-1, &r);
    2118         345 :       if (r) { nbguessed /= upowuu(p, LE[i]); LE[i] = 0; continue; }
    2119             :       /* q = n/(p-1) */
    2120         345 :       vnf = u_lval(q, p);
    2121         345 :       v = Z_lval(disc, p);
    2122         345 :       for (k = minss(LE[i], vnf+1); k >= 0; k--)
    2123         345 :         if (v >= nfdegree*k-(long)q)
    2124         345 :         { nbguessed /= upowuu(p, LE[i]-k); LE[i] = k; break; }
    2125             :       /* N.B the test above always works for k = 0: LE[i] >= 0 */
    2126             :     }
    2127             :   }
    2128         470 :   if (DEBUGLEVEL>2)
    2129           0 :     timer_printf(&ti, "after ramification conditions [guess = %ld]", nbguessed);
    2130         470 :   if (nbguessed == 2) return trivroots();
    2131         470 :   av = avma;
    2132             : 
    2133             :   /* Step 1.5 : test if nf.pol == subst(polcyclo(nbguessed), x, \pm x+c) */
    2134         470 :   if (eulerphiu_fact(fa) == (ulong)nfdegree)
    2135             :   {
    2136         225 :     GEN elt = ZXirred_is_cyclo_translate(nf_get_pol(nf),
    2137             :                                          uissquarefree_fact(fa));
    2138         225 :     if (elt)
    2139             :     {
    2140         175 :       if (DEBUGLEVEL>2)
    2141           0 :         timer_printf(&ti, "checking for cyclotomic polynomial [yes]");
    2142         175 :       return gerepilecopy(av, mkvec2(utoipos(nbguessed), elt));
    2143             :     }
    2144          50 :     avma = av;
    2145             :   }
    2146         295 :   if (DEBUGLEVEL>2)
    2147           0 :     timer_printf(&ti, "checking for cyclotomic polynomial [no]");
    2148             : 
    2149             :   /* Step 2 : choose a prime ideal for local lifting */
    2150         295 :   P.L = &L; nf_pick_prime_for_units(nf, &P);
    2151         295 :   if (DEBUGLEVEL>2)
    2152           0 :     timer_printf(&ti, "choosing prime %Ps, degree %ld",
    2153           0 :              P.L->p, P.L->Tp? degpol(P.L->Tp): 1);
    2154             : 
    2155             :   /* Step 3 : compute a reduced pr^k allowing lifting of local solutions */
    2156             :   /* evaluate maximum L2 norm of a root of unity in nf */
    2157         295 :   C0 = gmulsg(nfdegree, L2_bound(nf, gen_1));
    2158             :   /* lift and reduce pr^k */
    2159         295 :   if (DEBUGLEVEL>2) err_printf("Lift pr^k; GSmin wanted: %Ps\n",C0);
    2160         295 :   bestlift_init((long)mybestlift_bound(C0), nf, P.pr, C0, P.L);
    2161         295 :   P.L->dn = NULL;
    2162         295 :   if (DEBUGLEVEL>2) timer_start(&ti);
    2163             : 
    2164             :   /* Step 4 : actual computation of roots */
    2165         295 :   nbroots = 2; prim_root = gen_m1;
    2166         295 :   nfpol = nf_get_pol(nf);
    2167         835 :   for (i = 1; i < l; i++)
    2168             :   { /* for all prime power factors of nbguessed, find a p^k-th root of unity */
    2169         540 :     long k, p = LP[i];
    2170         795 :     for (k = minss(LE[i], Z_lval(subiu(P.q,1UL),p)); k > 0; k--)
    2171             :     { /* find p^k-th roots */
    2172         540 :       pari_sp av = avma;
    2173         540 :       long pk = upowuu(p,k);
    2174         540 :       if (pk==2) continue; /* no need to test second roots ! */
    2175         305 :       z = nfcyclo_root(pk, nfpol, P.L);
    2176         305 :       if (DEBUGLEVEL>2) timer_printf(&ti, "for factoring Phi_%ld^%ld", p,k);
    2177         305 :       if (z) {
    2178         285 :         if (DEBUGLEVEL>2) err_printf("  %ld-th root of unity found.\n", pk);
    2179         285 :         if (p==2) { nbroots = pk; prim_root = z; }
    2180         230 :         else     { nbroots *= pk; prim_root = nfmul(nf, prim_root,z); }
    2181         285 :         break;
    2182             :       }
    2183          20 :       avma = av;
    2184          20 :       if (DEBUGLEVEL) pari_warn(warner,"rootsof1: wrong guess");
    2185             :     }
    2186             :   }
    2187         295 :   return gerepilecopy(av, mkvec2(utoi(nbroots), prim_root));
    2188             : }
    2189             : 
    2190             : static long
    2191           0 : zk_equal1(GEN y)
    2192             : {
    2193           0 :   if (typ(y) == t_INT) return equali1(y);
    2194           0 :   return equali1(gel(y,1)) && ZV_isscalar(y);
    2195             : }
    2196             : /* x^w = 1 */
    2197             : static GEN
    2198           0 : is_primitive_root(GEN nf, GEN fa, GEN x, long w)
    2199             : {
    2200           0 :   GEN P = gel(fa,1);
    2201           0 :   long i, l = lg(P);
    2202             : 
    2203           0 :   for (i = 1; i < l; i++)
    2204             :   {
    2205           0 :     long p = itos(gel(P,i));
    2206           0 :     GEN y = nfpow_u(nf,x, w/p);
    2207           0 :     if (zk_equal1(y) > 0) /* y = 1 */
    2208             :     {
    2209           0 :       if (p != 2 || !equali1(gcoeff(fa,i,2))) return NULL;
    2210           0 :       x = gneg_i(x);
    2211             :     }
    2212             :   }
    2213           0 :   return x;
    2214             : }
    2215             : GEN
    2216           0 : rootsof1_kannan(GEN nf)
    2217             : {
    2218           0 :   pari_sp av = avma;
    2219             :   long N, k, i, ws, prec;
    2220             :   GEN z, y, d, list, w;
    2221             : 
    2222           0 :   nf = checknf(nf);
    2223           0 :   if ( nf_get_r1(nf) ) return trivroots();
    2224             : 
    2225           0 :   N = nf_get_degree(nf); prec = nf_get_prec(nf);
    2226             :   for (;;)
    2227             :   {
    2228           0 :     GEN R = R_from_QR(nf_get_G(nf), prec);
    2229           0 :     if (R)
    2230             :     {
    2231           0 :       y = fincke_pohst(mkvec(R), utoipos(N), N * N, 0, NULL);
    2232           0 :       if (y) break;
    2233             :     }
    2234           0 :     prec = precdbl(prec);
    2235           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"rootsof1",prec);
    2236           0 :     nf = nfnewprec_shallow(nf,prec);
    2237           0 :   }
    2238           0 :   if (itos(ground(gel(y,2))) != N) pari_err_BUG("rootsof1 (bug1)");
    2239           0 :   w = gel(y,1); ws = itos(w);
    2240           0 :   if (ws == 2) { avma = av; return trivroots(); }
    2241             : 
    2242           0 :   d = Z_factor(w); list = gel(y,3); k = lg(list);
    2243           0 :   for (i=1; i<k; i++)
    2244             :   {
    2245           0 :     z = is_primitive_root(nf, d, gel(list,i), ws);
    2246           0 :     if (z) return gerepilecopy(av, mkvec2(w, z));
    2247             :   }
    2248           0 :   pari_err_BUG("rootsof1");
    2249             :   return NULL; /* LCOV_EXCL_LINE */
    2250             : }

Generated by: LCOV version 1.11