Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - nffactor.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21741-70cf009) Lines: 1126 1219 92.4 %
Date: 2018-01-21 06:18:30 Functions: 67 71 94.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11