Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

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

Generated by: LCOV version 1.11