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 20277-2bd9113) Lines: 1110 1205 92.1 %
Date: 2017-02-21 05:49:51 Functions: 65 69 94.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11