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

Generated by: LCOV version 1.11