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

Generated by: LCOV version 1.11