Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11