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

Generated by: LCOV version 1.13