Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - galconj.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20459-9710128) Lines: 1512 1589 95.2 %
Date: 2017-04-29 05:33:44 Functions: 91 93 97.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  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             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : /*************************************************************************/
      17             : /**                                                                     **/
      18             : /**                           GALOIS CONJUGATES                         **/
      19             : /**                                                                     **/
      20             : /*************************************************************************/
      21             : 
      22             : static int
      23         102 : is2sparse(GEN x)
      24             : {
      25         102 :   long i, l = lg(x);
      26         102 :   if (odd(l-3)) return 0;
      27         336 :   for(i=3; i<l; i+=2)
      28         282 :     if (signe(gel(x,i))) return 0;
      29          54 :   return 1;
      30             : }
      31             : 
      32             : static GEN
      33         546 : galoisconj1(GEN nf)
      34             : {
      35         546 :   GEN x = get_nfpol(nf, &nf), f = nf? nf : x, y, z;
      36         546 :   long i, lz, v = varn(x), nbmax;
      37         546 :   pari_sp av = avma;
      38         546 :   RgX_check_ZX(x, "nfgaloisconj");
      39         546 :   nbmax = numberofconjugates(x, 2);
      40         546 :   if (nbmax==1) retmkcol(pol_x(v));
      41         144 :   if (nbmax==2 && is2sparse(x))
      42             :   {
      43          54 :     GEN res = cgetg(3,t_COL);
      44          54 :     gel(res,1) = deg1pol_shallow(gen_m1, gen_0, v);
      45          54 :     gel(res,2) = pol_x(v);
      46          54 :     return res;
      47             :   }
      48          90 :   x = leafcopy(x); setvarn(x, fetch_var_higher());
      49          90 :   z = nfroots(f, x); lz = lg(z);
      50          90 :   y = cgetg(lz, t_COL);
      51         528 :   for (i = 1; i < lz; i++)
      52             :   {
      53         438 :     GEN t = lift(gel(z,i));
      54         438 :     if (typ(t) == t_POL) setvarn(t, v);
      55         438 :     gel(y,i) = t;
      56             :   }
      57          90 :   (void)delete_var();
      58          90 :   return gerepileupto(av, y);
      59             : }
      60             : 
      61             : /*************************************************************************/
      62             : /**                                                                     **/
      63             : /**                           GALOISCONJ4                               **/
      64             : /**                                                                     **/
      65             : /*************************************************************************/
      66             : /*DEBUGLEVEL:
      67             :   1: timing
      68             :   2: outline
      69             :   4: complete outline
      70             :   6: detail
      71             :   7: memory
      72             :   9: complete detail
      73             : */
      74             : struct galois_borne {
      75             :   GEN  l;
      76             :   long valsol;
      77             :   long valabs;
      78             :   GEN  bornesol;
      79             :   GEN  ladicsol;
      80             :   GEN  ladicabs;
      81             : };
      82             : struct galois_lift {
      83             :   GEN  T;
      84             :   GEN  den;
      85             :   GEN  p;
      86             :   GEN  L;
      87             :   GEN  Lden;
      88             :   long e;
      89             :   GEN  Q;
      90             :   GEN  TQ;
      91             :   struct galois_borne *gb;
      92             : };
      93             : struct galois_testlift {
      94             :   long n;
      95             :   long f;
      96             :   long g;
      97             :   GEN  bezoutcoeff;
      98             :   GEN  pauto;
      99             :   GEN  C;
     100             :   GEN  Cd;
     101             : };
     102             : struct galois_test { /* data for permutation test */
     103             :   GEN order; /* order of tests pour galois_test_perm */
     104             :   GEN borne, lborne; /* coefficient bounds */
     105             :   GEN ladic;
     106             :   GEN PV; /* NULL or vector of test matrices (Vmatrix) */
     107             :   GEN TM; /* transpose of M */
     108             :   GEN L; /* p-adic roots, known mod ladic */
     109             :   GEN M; /* vandermonde inverse */
     110             : };
     111             : /* result of the study of Frobenius degrees */
     112             : enum ga_code {ga_all_normal=1,ga_ext_2=2,ga_non_wss=4};
     113             : struct galois_analysis {
     114             :   long p; /* prime to be lifted */
     115             :   long deg; /* degree of the lift */
     116             :   long mindeg; /* minimal acceptable degree */
     117             :   long ord;
     118             :   long l; /* l: prime number such that T is totally split mod l */
     119             :   long p4;
     120             :   enum ga_code group;
     121             : };
     122             : struct galois_frobenius {
     123             :   long p;
     124             :   long fp;
     125             :   long deg;
     126             :   GEN Tmod;
     127             :   GEN psi;
     128             : };
     129             : 
     130             : /* given complex roots L[i], i <= n of some monic T in C[X], return
     131             :  * the T'(L[i]), computed stably via products of differences */
     132             : static GEN
     133        3150 : vandermondeinverseprep(GEN L)
     134             : {
     135        3150 :   long i, j, n = lg(L);
     136             :   GEN V;
     137        3150 :   V = cgetg(n, t_VEC);
     138       30750 :   for (i = 1; i < n; i++)
     139             :   {
     140       27600 :     pari_sp ltop = avma;
     141       27600 :     GEN W = cgetg(n-1,t_VEC);
     142       27600 :     long k = 1;
     143      669792 :     for (j = 1; j < n; j++)
     144      642192 :       if (i != j) gel(W, k++) = gsub(gel(L,i),gel(L,j));
     145       27600 :     gel(V,i) = gerepileupto(ltop, RgV_prod(W));
     146             :   }
     147        3150 :   return V;
     148             : }
     149             : 
     150             : /* Compute the inverse of the van der Monde matrix of T multiplied by den */
     151             : GEN
     152        3072 : vandermondeinverse(GEN L, GEN T, GEN den, GEN prep)
     153             : {
     154        3072 :   pari_sp ltop = avma;
     155        3072 :   long i, n = lg(L)-1;
     156             :   GEN M, P;
     157        3072 :   if (!prep) prep = vandermondeinverseprep(L);
     158        3072 :   if (den && !equali1(den)) T = RgX_Rg_mul(T,den);
     159        3072 :   M = cgetg(n+1, t_MAT);
     160       29610 :   for (i = 1; i <= n; i++)
     161             :   {
     162       26538 :     P = RgX_Rg_div(RgX_div_by_X_x(T, gel(L,i), NULL), gel(prep,i));
     163       26538 :     gel(M,i) = RgX_to_RgC(P,n);
     164             :   }
     165        3072 :   return gerepilecopy(ltop, M);
     166             : }
     167             : 
     168             : /* #r = r1 + r2 */
     169             : GEN
     170        1200 : embed_roots(GEN ro, long r1)
     171             : {
     172        1200 :   long r2 = lg(ro)-1-r1;
     173             :   GEN L;
     174        1200 :   if (!r2) L = ro;
     175             :   else
     176             :   {
     177         990 :     long i,j, N = r1+2*r2;
     178         990 :     L = cgetg(N+1, t_VEC);
     179         990 :     for (i = 1; i <= r1; i++) gel(L,i) = gel(ro,i);
     180        3756 :     for (j = i; j <= N; i++)
     181             :     {
     182        2766 :       GEN z = gel(ro,i);
     183        2766 :       gel(L,j++) = z;
     184        2766 :       gel(L,j++) = mkcomplex(gel(z,1), gneg(gel(z,2)));
     185             :     }
     186             :   }
     187        1200 :   return L;
     188             : }
     189             : GEN
     190       16674 : embed_disc(GEN z, long r1, long prec)
     191             : {
     192       16674 :   pari_sp av = avma;
     193       16674 :   GEN t = real_1(prec);
     194       16674 :   long i, j, n = lg(z)-1, r2 = n-r1;
     195       79614 :   for (i = 1; i < r1; i++)
     196             :   {
     197       62940 :     GEN zi = gel(z,i);
     198       62940 :     for (j = i+1; j <= r1; j++) t = gmul(t, gsub(zi, gel(z,j)));
     199             :   }
     200       78228 :   for (j = r1+1; j <= n; j++)
     201             :   {
     202       61554 :     GEN zj = gel(z,j), a = gel(zj,1), b = gel(zj,2), b2 = gsqr(b);
     203       67530 :     for (i = 1; i <= r1; i++)
     204             :     {
     205        5976 :       GEN zi = gel(z,i);
     206        5976 :       t = gmul(t, gadd(gsqr(gsub(zi, a)), b2));
     207             :     }
     208       61554 :     t = gmul(t, b);
     209             :   }
     210       16674 :   if (r2) t = gmul2n(t, r2);
     211       16674 :   if (r2 > 1)
     212             :   {
     213        9654 :     GEN T = real_1(prec);
     214       61164 :     for (i = r1+1; i < n; i++)
     215             :     {
     216       51510 :       GEN zi = gel(z,i), a = gel(zi,1), b = gel(zi,2);
     217      403446 :       for (j = i+1; j <= n; j++)
     218             :       {
     219      351936 :         GEN zj = gel(z,j), c = gel(zj,1), d = gel(zj,2);
     220      351936 :         GEN f = gsqr(gsub(a,c)), g = gsqr(gsub(b,d)), h = gsqr(gadd(b,d));
     221      351936 :         T = gmul(T, gmul(gadd(f,g), gadd(f,h)));
     222             :       }
     223             :     }
     224        9654 :     t = gmul(t, T);
     225             :   }
     226       16674 :   t = gsqr(t);
     227       16674 :   if (odd(r2)) t = gneg(t);
     228       16674 :   return gerepileupto(av, t);
     229             : }
     230             : 
     231             : /* Compute bound for the coefficients of automorphisms.
     232             :  * T a ZX, dn a t_INT denominator or NULL */
     233             : GEN
     234        3150 : initgaloisborne(GEN T, GEN dn, long prec, GEN *ptL, GEN *ptprep, GEN *ptdis)
     235             : {
     236             :   GEN L, prep, den, nf, r;
     237             :   pari_timer ti;
     238             : 
     239        3150 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     240        3150 :   T = get_nfpol(T, &nf);
     241        3150 :   r = nf ? nf_get_roots(nf) : NULL;
     242        3150 :   if (nf &&  precision(gel(r, 1)) >= prec)
     243        1200 :     L = embed_roots(r, nf_get_r1(nf));
     244             :   else
     245        1950 :     L = QX_complex_roots(T, prec);
     246        3150 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"roots");
     247        3150 :   prep = vandermondeinverseprep(L);
     248        3150 :   if (!dn)
     249             :   {
     250        1848 :     GEN dis, res = RgV_prod(gabs(prep,prec));
     251             :     /*Add +1 to cater for accuracy error in res */
     252        1848 :     dis = ZX_disc_all(T, 1+expi(ceil_safe(res)));
     253        1848 :     den = indexpartial(T,dis);
     254        1848 :     if (ptdis) *ptdis = dis;
     255             :   }
     256             :   else
     257             :   {
     258        1302 :     if (typ(dn) != t_INT || signe(dn) <= 0)
     259           0 :       pari_err_TYPE("initgaloisborne [incorrect denominator]", dn);
     260        1302 :     den = dn;
     261             :   }
     262        3150 :   if (ptprep) *ptprep = prep;
     263        3150 :   *ptL = L; return den;
     264             : }
     265             : 
     266             : /* ||| M ||| with respect to || x ||_oo, M t_MAT */
     267             : GEN
     268        6624 : matrixnorm(GEN M, long prec)
     269             : {
     270        6624 :   long i,j,m, l = lg(M);
     271        6624 :   GEN B = real_0(prec);
     272             : 
     273        6624 :   if (l == 1) return B;
     274        6624 :   m = lgcols(M);
     275       31986 :   for (i = 1; i < m; i++)
     276             :   {
     277       25362 :     GEN z = gabs(gcoeff(M,i,1), prec);
     278       25362 :     for (j = 2; j < l; j++) z = gadd(z, gabs(gcoeff(M,i,j), prec));
     279       25362 :     if (gcmp(z, B) > 0) B = z;
     280             :   }
     281        6624 :   return B;
     282             : }
     283             : 
     284             : static GEN
     285        1704 : galoisborne(GEN T, GEN dn, struct galois_borne *gb, long d)
     286             : {
     287             :   pari_sp ltop, av2;
     288             :   GEN borne, borneroots, bornetrace, borneabs;
     289             :   long prec;
     290             :   GEN L, M, prep, den;
     291             :   pari_timer ti;
     292        1704 :   const long step=3;
     293             : 
     294        1704 :   prec = nbits2prec(bit_accuracy(ZX_max_lg(T)));
     295        1704 :   den = initgaloisborne(T,dn,prec, &L,&prep,NULL);
     296        1704 :   if (!dn) dn = den;
     297        1704 :   ltop = avma;
     298        1704 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     299        1704 :   M = vandermondeinverse(L, RgX_gtofp(T, prec), den, prep);
     300        1704 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"vandermondeinverse");
     301        1704 :   borne = matrixnorm(M, prec);
     302        1704 :   borneroots = gsupnorm(L, prec); /*t_REAL*/
     303        1704 :   bornetrace = gmulsg((2*step)*degpol(T)/d,
     304        1704 :                       powru(borneroots, minss(degpol(T), step)));
     305        1704 :   borneroots = ceil_safe(gmul(borne, borneroots));
     306        1704 :   borneabs = ceil_safe(gmax(gmul(borne, bornetrace), powru(bornetrace, d)));
     307        1704 :   av2 = avma;
     308             :   /*We use d-1 test, so we must overlift to 2^BITS_IN_LONG*/
     309        1704 :   gb->valsol = logint(shifti(borneroots,2+BITS_IN_LONG), gb->l) + 1;
     310        1704 :   gb->valabs = logint(shifti(borneabs,2), gb->l) + 1;
     311        1704 :   gb->valabs = maxss(gb->valsol, gb->valabs);
     312        1704 :   if (DEBUGLEVEL >= 4)
     313           0 :     err_printf("GaloisConj: val1=%ld val2=%ld\n", gb->valsol, gb->valabs);
     314        1704 :   avma = av2;
     315        1704 :   gb->bornesol = gerepileuptoint(ltop, shifti(borneroots,1));
     316        1704 :   if (DEBUGLEVEL >= 9)
     317           0 :     err_printf("GaloisConj: Bound %Ps\n",borneroots);
     318        1704 :   gb->ladicsol = powiu(gb->l, gb->valsol);
     319        1704 :   gb->ladicabs = powiu(gb->l, gb->valabs);
     320        1704 :   return dn;
     321             : }
     322             : 
     323             : static GEN
     324        1614 : makeLden(GEN L,GEN den, struct galois_borne *gb)
     325        1614 : { return FpC_Fp_mul(L, den, gb->ladicsol); }
     326             : 
     327             : /* Initialize the galois_lift structure */
     328             : static void
     329        1668 : initlift(GEN T, GEN den, GEN p, GEN L, GEN Lden, struct galois_borne *gb, struct galois_lift *gl)
     330             : {
     331        1668 :   pari_sp av = avma;
     332             :   long e;
     333        1668 :   gl->gb = gb;
     334        1668 :   gl->T = T;
     335        1668 :   gl->den = is_pm1(den)? gen_1: den;
     336        1668 :   gl->p = p;
     337        1668 :   gl->L = L;
     338        1668 :   gl->Lden = Lden;
     339        1668 :   e = logint(shifti(gb->bornesol, 2+BITS_IN_LONG),p) + 1;
     340        1668 :   avma = av;
     341        1668 :   if (e < 2) e = 2;
     342        1668 :   gl->e = e;
     343        1668 :   gl->Q = powiu(p, e);
     344        1668 :   gl->TQ = FpX_red(T,gl->Q);
     345        1668 : }
     346             : 
     347             : /* Check whether f is (with high probability) a solution and compute its
     348             :  * permutation */
     349             : static int
     350        3552 : poltopermtest(GEN f, struct galois_lift *gl, GEN pf)
     351             : {
     352             :   pari_sp av;
     353        3552 :   GEN fx, fp, B = gl->gb->bornesol;
     354             :   long i, j, ll;
     355       31620 :   for (i = 2; i < lg(f); i++)
     356       29520 :     if (abscmpii(gel(f,i),B) > 0)
     357             :     {
     358        1452 :       if (DEBUGLEVEL>=4) err_printf("GaloisConj: Solution too large.\n");
     359        1452 :       if (DEBUGLEVEL>=8) err_printf("f=%Ps\n borne=%Ps\n",f,B);
     360        1452 :       return 0;
     361             :     }
     362        2100 :   ll = lg(gl->L);
     363        2100 :   fp = const_vecsmall(ll-1, 1); /* left on stack */
     364        2100 :   av = avma;
     365       33438 :   for (i = 1; i < ll; i++, avma = av)
     366             :   {
     367       31344 :     fx = FpX_eval(f, gel(gl->L,i), gl->gb->ladicsol);
     368      539370 :     for (j = 1; j < ll; j++)
     369      539364 :       if (fp[j] && equalii(fx, gel(gl->Lden,j))) { pf[i]=j; fp[j]=0; break; }
     370       31344 :     if (j == ll) return 0;
     371             :   }
     372        2094 :   return 1;
     373             : }
     374             : 
     375             : struct monoratlift
     376             : {
     377             :   struct galois_lift *gl;
     378             :   GEN frob;
     379             :   long early;
     380             : };
     381             : 
     382             : static int
     383        5142 : monoratlift(void *E, GEN S, GEN q)
     384             : {
     385        5142 :   struct monoratlift *d = (struct monoratlift *) E;
     386        5142 :   struct galois_lift *gl = d->gl;
     387        5142 :   GEN qm1old = sqrti(shifti(q,-2));
     388        5142 :   GEN tlift = FpX_ratlift(S,q,qm1old,qm1old,gl->den);
     389        5142 :   if (tlift)
     390             :   {
     391         960 :     pari_sp ltop = avma;
     392         960 :     if(DEBUGLEVEL>=4)
     393           0 :       err_printf("MonomorphismLift: trying early solution %Ps\n",tlift);
     394         960 :     if (gl->den != gen_1) {
     395         618 :       GEN N = gl->gb->ladicsol, N2 = shifti(N,-1);
     396         618 :       tlift = FpX_center(FpX_red(Q_muli_to_int(tlift, gl->den), N), N,N2);
     397             :     }
     398         960 :     if (poltopermtest(tlift, gl, d->frob))
     399             :     {
     400         954 :       if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: true early solution.\n");
     401         954 :       d->early = 1;
     402         954 :       avma = ltop; return 1;
     403             :     }
     404           6 :     avma = ltop;
     405           6 :     if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: false early solution.\n");
     406             :   }
     407        4188 :   return 0;
     408             : }
     409             : 
     410             : static GEN
     411        1836 : monomorphismratlift(GEN P, GEN S, struct galois_lift *gl, GEN frob)
     412             : {
     413             :   pari_timer ti;
     414        1836 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     415        1836 :   if (frob)
     416             :   {
     417             :     struct monoratlift d;
     418        1644 :     d.gl = gl; d.frob = frob; d.early = 0;
     419        1644 :     S = ZpX_ZpXQ_liftroot_ea(P,S,gl->T,gl->p, gl->e, (void*)&d, monoratlift);
     420        1644 :     S = d.early ? NULL: S;
     421             :   }
     422             :   else
     423         192 :     S = ZpX_ZpXQ_liftroot(P,S,gl->T,gl->p, gl->e);
     424        1836 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "monomorphismlift()");
     425        1836 :   return S;
     426             : }
     427             : 
     428             : /* Let T be a polynomial in Z[X] , p a prime number, S in Fp[X]/(T) so
     429             :  * that T(S)=0 [p,T]. Lift S in S_0 so that T(S_0)=0 [T,p^e]
     430             :  * Unclean stack */
     431             : static GEN
     432        1836 : automorphismlift(GEN S, struct galois_lift *gl, GEN frob)
     433             : {
     434        1836 :   return monomorphismratlift(gl->T, S, gl, frob);
     435             : }
     436             : 
     437             : static GEN
     438        1668 : galoisdolift(struct galois_lift *gl, GEN frob)
     439             : {
     440        1668 :   pari_sp av = avma;
     441        1668 :   GEN Tp = FpX_red(gl->T, gl->p);
     442        1668 :   GEN S = FpX_Frobenius(Tp, gl->p);
     443        1668 :   return gerepileupto(av, automorphismlift(S, gl, frob));
     444             : }
     445             : 
     446             : static void
     447         606 : inittestlift(GEN plift, GEN Tmod, struct galois_lift *gl,
     448             :              struct galois_testlift *gt)
     449             : {
     450             :   pari_timer ti;
     451         606 :   gt->n = lg(gl->L) - 1;
     452         606 :   gt->g = lg(Tmod) - 1;
     453         606 :   gt->f = gt->n / gt->g;
     454         606 :   gt->bezoutcoeff = bezout_lift_fact(gl->T, Tmod, gl->p, gl->e);
     455         606 :   if (DEBUGLEVEL >= 2) timer_start(&ti);
     456         606 :   gt->pauto = FpXQ_autpowers(plift, gt->f-1, gl->TQ, gl->Q);
     457         606 :   if (DEBUGLEVEL >= 2) timer_printf(&ti, "Frobenius power");
     458         606 : }
     459             : 
     460             : /* Explanation of the intheadlong technique:
     461             :  * Let C be a bound, B = BITS_IN_LONG, M > C*2^B a modulus and 0 <= a_i < M for
     462             :  * i=1,...,n where n < 2^B. We want to test if there exist k,l, |k| < C < M/2^B,
     463             :  * such that sum a_i = k + l*M
     464             :  * We write a_i*2^B/M = b_i+c_i with b_i integer and 0<=c_i<1, so that
     465             :  *   sum b_i - l*2^B = k*2^B/M - sum c_i
     466             :  * Since -1 < k*2^B/M < 1 and 0<=c_i<1, it follows that
     467             :  *   -n-1 < sum b_i - l*2^B < 1  i.e.  -n <= sum b_i -l*2^B <= 0
     468             :  * So we compute z = - sum b_i [mod 2^B] and check if 0 <= z <= n. */
     469             : 
     470             : /* Assume 0 <= x < mod. */
     471             : static ulong
     472      844926 : intheadlong(GEN x, GEN mod)
     473             : {
     474      844926 :   pari_sp av = avma;
     475      844926 :   long res = (long) itou(divii(shifti(x,BITS_IN_LONG),mod));
     476      844926 :   avma = av; return res;
     477             : }
     478             : static GEN
     479       21318 : vecheadlong(GEN W, GEN mod)
     480             : {
     481       21318 :   long i, l = lg(W);
     482       21318 :   GEN V = cgetg(l, t_VECSMALL);
     483       21318 :   for(i=1; i<l; i++) V[i] = intheadlong(gel(W,i), mod);
     484       21318 :   return V;
     485             : }
     486             : static GEN
     487        1182 : matheadlong(GEN W, GEN mod)
     488             : {
     489        1182 :   long i, l = lg(W);
     490        1182 :   GEN V = cgetg(l,t_MAT);
     491        1182 :   for(i=1; i<l; i++) gel(V,i) = vecheadlong(gel(W,i), mod);
     492        1182 :   return V;
     493             : }
     494             : static ulong
     495       36216 : polheadlong(GEN P, long n, GEN mod)
     496             : {
     497       36216 :   return (lg(P)>n+2)? intheadlong(gel(P,n+2),mod): 0;
     498             : }
     499             : 
     500             : #define headlongisint(Z,N) (-(ulong)(Z)<=(ulong)(N))
     501             : 
     502             : static long
     503         696 : frobeniusliftall(GEN sg, long el, GEN *psi, struct galois_lift *gl,
     504             :                  struct galois_testlift *gt, GEN frob)
     505             : {
     506         696 :   pari_sp av, ltop2, ltop = avma;
     507         696 :   long i,j,k, c = lg(sg)-1, n = lg(gl->L)-1, m = gt->g, d = m / c;
     508             :   GEN pf, u, v, C, Cd, SG, cache;
     509         696 :   long N1, N2, R1, Ni, ord = gt->f, c_idx = gt->g-1;
     510             :   ulong headcache;
     511         696 :   long hop = 0;
     512             :   GEN NN, NQ;
     513             :   pari_timer ti;
     514             : 
     515         696 :   *psi = pf = cgetg(m, t_VECSMALL);
     516         696 :   ltop2 = avma;
     517         696 :   NN = diviiexact(mpfact(m), mului(c, powiu(mpfact(d), c)));
     518         696 :   if (DEBUGLEVEL >= 4)
     519           0 :     err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     520         696 :   N1=10000000;
     521         696 :   NQ=divis_rem(NN,N1,&R1);
     522         696 :   if (abscmpiu(NQ,1000000000)>0)
     523             :   {
     524           0 :     pari_warn(warner,"Combinatorics too hard : would need %Ps tests!\n"
     525             :         "I will skip it, but it may induce an infinite loop",NN);
     526           0 :     avma = ltop; *psi = NULL; return 0;
     527             :   }
     528         696 :   N2=itos(NQ); if(!N2) N1=R1;
     529         696 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     530         696 :   avma = ltop2;
     531         696 :   C = gt->C;
     532         696 :   Cd= gt->Cd;
     533         696 :   v = FpXQ_mul(gel(gt->pauto, 1+el%ord), gel(gt->bezoutcoeff, m),gl->TQ,gl->Q);
     534         696 :   if (gl->den != gen_1) v = FpX_Fp_mul(v, gl->den, gl->Q);
     535         696 :   SG = cgetg(lg(sg),t_VECSMALL);
     536         696 :   for(i=1; i<lg(SG); i++) SG[i] = (el*sg[i])%ord + 1;
     537         696 :   cache = cgetg(m+1,t_VECSMALL); cache[m] = polheadlong(v,1,gl->Q);
     538         696 :   headcache = polheadlong(v,2,gl->Q);
     539         696 :   for (i = 1; i < m; i++) pf[i] = 1 + i/d;
     540         696 :   av = avma;
     541      199722 :   for (Ni = 0, i = 0; ;i++)
     542             :   {
     543      947466 :     for (j = c_idx ; j > 0; j--)
     544             :     {
     545      747744 :       long h = SG[pf[j]];
     546      747744 :       if (!mael(C,h,j))
     547             :       {
     548        2964 :         pari_sp av3 = avma;
     549        2964 :         GEN r = FpXQ_mul(gel(gt->pauto,h), gel(gt->bezoutcoeff,j),gl->TQ,gl->Q);
     550        2964 :         if (gl->den != gen_1) r = FpX_Fp_mul(r, gl->den, gl->Q);
     551        2964 :         gmael(C,h,j) = gclone(r);
     552        2964 :         mael(Cd,h,j) = polheadlong(r,1,gl->Q);
     553        2964 :         avma = av3;
     554             :       }
     555      747744 :       uel(cache,j) = uel(cache,j+1)+umael(Cd,h,j);
     556             :     }
     557      199722 :     if (headlongisint(uel(cache,1),n))
     558             :     {
     559        2478 :       ulong head = headcache;
     560        2478 :       for (j = 1; j < m; j++) head += polheadlong(gmael(C,SG[pf[j]],j),2,gl->Q);
     561        2478 :       if (headlongisint(head,n))
     562             :       {
     563         564 :         u = v;
     564         564 :         for (j = 1; j < m; j++) u = ZX_add(u, gmael(C,SG[pf[j]],j));
     565         564 :         u = FpX_center(FpX_red(u, gl->Q), gl->Q, shifti(gl->Q,-1));
     566         564 :         if (poltopermtest(u, gl, frob))
     567             :         {
     568         558 :           if (DEBUGLEVEL >= 4)
     569             :           {
     570           0 :             timer_printf(&ti, "");
     571           0 :             err_printf("GaloisConj: %d hops on %Ps tests\n",hop,addis(mulss(Ni,N1),i));
     572             :           }
     573         558 :           avma = ltop2; return 1;
     574             :         }
     575           6 :         if (DEBUGLEVEL >= 4) err_printf("M");
     576             :       }
     577        1914 :       else hop++;
     578             :     }
     579      199164 :     if (DEBUGLEVEL >= 4 && i % maxss(N1/20, 1) == 0)
     580           0 :       timer_printf(&ti, "GaloisConj:Testing %Ps", addis(mulss(Ni,N1),i));
     581      199164 :     avma = av;
     582      199164 :     if (i == N1-1)
     583             :     {
     584         138 :       if (Ni==N2-1) N1 = R1;
     585         138 :       if (Ni==N2) break;
     586           0 :       Ni++; i = 0;
     587           0 :       if (DEBUGLEVEL>=4) timer_start(&ti);
     588             :     }
     589      199026 :     for (j = 2; j < m && pf[j-1] >= pf[j]; j++)
     590             :       /*empty*/; /* to kill clang Warning */
     591      199026 :     for (k = 1; k < j-k && pf[k] != pf[j-k]; k++) { lswap(pf[k], pf[j-k]); }
     592      199026 :     for (k = j - 1; pf[k] >= pf[j]; k--)
     593             :       /*empty*/;
     594      199026 :     lswap(pf[j], pf[k]); c_idx = j;
     595      199026 :   }
     596         138 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: not found, %d hops \n",hop);
     597         138 :   *psi = NULL; avma = ltop; return 0;
     598             : }
     599             : 
     600             : /* Compute the test matrix for the i-th line of V. Clone. */
     601             : static GEN
     602        1182 : Vmatrix(long i, struct galois_test *td)
     603             : {
     604        1182 :   pari_sp av = avma;
     605        1182 :   GEN m = gclone( matheadlong(FpC_FpV_mul(td->L, row(td->M,i), td->ladic), td->ladic));
     606        1182 :   avma = av; return m;
     607             : }
     608             : 
     609             : /* Initialize galois_test */
     610             : static void
     611        1026 : inittest(GEN L, GEN M, GEN borne, GEN ladic, struct galois_test *td)
     612             : {
     613        1026 :   long i, n = lg(L)-1;
     614        1026 :   GEN p = cgetg(n+1, t_VECSMALL);
     615        1026 :   if (DEBUGLEVEL >= 8) err_printf("GaloisConj: Init Test\n");
     616        1026 :   td->order = p;
     617        1026 :   for (i = 1; i <= n-2; i++) p[i] = i+2;
     618        1026 :   p[n-1] = 1; p[n] = 2;
     619        1026 :   td->borne = borne;
     620        1026 :   td->lborne = subii(ladic, borne);
     621        1026 :   td->ladic = ladic;
     622        1026 :   td->L = L;
     623        1026 :   td->M = M;
     624        1026 :   td->TM = shallowtrans(M);
     625        1026 :   td->PV = zero_zv(n);
     626        1026 :   gel(td->PV, 2) = Vmatrix(2, td);
     627        1026 : }
     628             : 
     629             : /* Free clones stored inside galois_test */
     630             : static void
     631        1026 : freetest(struct galois_test *td)
     632             : {
     633             :   long i;
     634       16116 :   for (i = 1; i < lg(td->PV); i++)
     635       15090 :     if (td->PV[i]) { gunclone(gel(td->PV,i)); td->PV[i] = 0; }
     636        1026 : }
     637             : 
     638             : /* Check if the integer P seen as a p-adic number is close to an integer less
     639             :  * than td->borne in absolute value */
     640             : static long
     641       32190 : padicisint(GEN P, struct galois_test *td)
     642             : {
     643       32190 :   pari_sp ltop = avma;
     644       32190 :   GEN U  = modii(P, td->ladic);
     645       32190 :   long r = cmpii(U, td->borne) <= 0 || cmpii(U, td->lborne) >= 0;
     646       32190 :   avma = ltop; return r;
     647             : }
     648             : 
     649             : /* Check if the permutation pf is valid according to td.
     650             :  * If not, update td to make subsequent test faster (hopefully) */
     651             : static long
     652       79368 : galois_test_perm(struct galois_test *td, GEN pf)
     653             : {
     654       79368 :   pari_sp av = avma;
     655       79368 :   long i, j, n = lg(td->L)-1;
     656       79368 :   GEN V, P = NULL;
     657      112566 :   for (i = 1; i < n; i++)
     658             :   {
     659      111018 :     long ord = td->order[i];
     660      111018 :     GEN PW = gel(td->PV, ord);
     661      111018 :     if (PW)
     662             :     {
     663       78828 :       ulong head = umael(PW,1,pf[1]);
     664       78828 :       for (j = 2; j <= n; j++) head += umael(PW,j,pf[j]);
     665       78828 :       if (!headlongisint(head,n)) break;
     666             :     } else
     667             :     {
     668       32190 :       if (!P) P = vecpermute(td->L, pf);
     669       32190 :       V = FpV_dotproduct(gel(td->TM,ord), P, td->ladic);
     670       32190 :       if (!padicisint(V, td)) {
     671         156 :         gel(td->PV, ord) = Vmatrix(ord, td);
     672         156 :         if (DEBUGLEVEL >= 4) err_printf("M");
     673         156 :         break;
     674             :       }
     675             :     }
     676             :   }
     677       79368 :   if (i == n) { avma = av; return 1; }
     678       77820 :   if (DEBUGLEVEL >= 4) err_printf("%d.", i);
     679       77820 :   if (i > 1)
     680             :   {
     681         702 :     long z = td->order[i];
     682         702 :     for (j = i; j > 1; j--) td->order[j] = td->order[j-1];
     683         702 :     td->order[1] = z;
     684         702 :     if (DEBUGLEVEL >= 8) err_printf("%Ps", td->order);
     685             :   }
     686       77820 :   avma = av; return 0;
     687             : }
     688             : /*Compute a*b/c when a*b will overflow*/
     689             : static long
     690           0 : muldiv(long a,long b,long c)
     691             : {
     692           0 :   return (long)((double)a*(double)b/c);
     693             : }
     694             : 
     695             : /* F = cycle decomposition of sigma,
     696             :  * B = cycle decomposition of cl(tau).
     697             :  * Check all permutations pf who can possibly correspond to tau, such that
     698             :  * tau*sigma*tau^-1 = sigma^s and tau^d = sigma^t, where d = ord cl(tau)
     699             :  * x: vector of choices,
     700             :  * G: vector allowing linear access to elts of F.
     701             :  * Choices multiple of e are not changed. */
     702             : static GEN
     703        1674 : testpermutation(GEN F, GEN B, GEN x, long s, long e, long cut,
     704             :                 struct galois_test *td)
     705             : {
     706        1674 :   pari_sp av, avm = avma;
     707             :   long a, b, c, d, n, p1, p2, p3, p4, p5, p6, l1, l2, N1, N2, R1;
     708        1674 :   long i, j, cx, hop = 0, start = 0;
     709             :   GEN pf, ar, G, W, NN, NQ;
     710             :   pari_timer ti;
     711        1674 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     712        1674 :   a = lg(F)-1; b = lg(gel(F,1))-1;
     713        1674 :   c = lg(B)-1; d = lg(gel(B,1))-1;
     714        1674 :   n = a*b;
     715        1674 :   s = (b+s) % b;
     716        1674 :   pf = cgetg(n+1, t_VECSMALL);
     717        1674 :   av = avma;
     718        1674 :   ar = cgetg(a+2, t_VECSMALL); ar[a+1]=0;
     719        1674 :   G  = cgetg(a+1, t_VECSMALL);
     720        1674 :   W  = gel(td->PV, td->order[n]);
     721       14544 :   for (cx=1, i=1, j=1; cx <= a; cx++, i++)
     722             :   {
     723       12870 :     gel(G,cx) = gel(F, coeff(B,i,j));
     724       12870 :     if (i == d) { i = 0; j++; }
     725             :   }
     726        1674 :   NN = divis(powuu(b, c * (d - d/e)), cut);
     727        1674 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     728        1674 :   N1 = 1000000;
     729        1674 :   NQ = divis_rem(NN,N1,&R1);
     730        1674 :   if (abscmpiu(NQ,100000000)>0)
     731             :   {
     732           0 :     avma = avm;
     733           0 :     pari_warn(warner,"Combinatorics too hard: would need %Ps tests!\n"
     734             :                      "I'll skip it but you will get a partial result...",NN);
     735           0 :     return identity_perm(n);
     736             :   }
     737        1674 :   N2 = itos(NQ);
     738        1872 :   for (l2 = 0; l2 <= N2; l2++)
     739             :   {
     740        1674 :     long nbiter = (l2<N2) ? N1: R1;
     741        1674 :     if (DEBUGLEVEL >= 2 && N2) err_printf("%d%% ", muldiv(l2,100,N2));
     742     4799622 :     for (l1 = 0; l1 < nbiter; l1++)
     743             :     {
     744     4799424 :       if (start)
     745             :       {
     746    12557664 :         for (i=1, j=e; i < a;)
     747             :         {
     748     7759914 :           if ((++(x[i])) != b) break;
     749     2962164 :           x[i++] = 0;
     750     2962164 :           if (i == j) { i++; j += e; }
     751             :         }
     752             :       }
     753        1674 :       else { start=1; i = a-1; }
     754             :       /* intheadlong test: overflow in + is OK, we compute mod 2^BIL */
     755    20065194 :       for (p1 = i+1, p5 = p1%d - 1 ; p1 >= 1; p1--, p5--) /* p5 = (p1%d) - 1 */
     756             :       {
     757    15265770 :         ulong V = 0;
     758    15265770 :         if (p5 == - 1) { p5 = d - 1; p6 = p1 + 1 - d; } else p6 = p1 + 1;
     759    15265770 :         p4 = p5 ? x[p1-1] : 0;
     760    15265770 :         V = 0;
     761    51585168 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     762             :         {
     763    36319398 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     764    36319398 :           p3 += s; if (p3 > b) p3 -= b;
     765             :         }
     766    15265770 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     767    23478696 :         for (p2 = p4; p2 >= 1; p2--)
     768             :         {
     769     8212926 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     770     8212926 :           p3 -= s; if (p3 <= 0) p3 += b;
     771             :         }
     772    15265770 :         ar[p1] = ar[p1+1] + V;
     773             :       }
     774     4799424 :       if (!headlongisint(uel(ar,1),n)) continue;
     775             : 
     776             :       /* intheadlong succeeds. Full computation */
     777     2487426 :       for (p1=1, p5=d; p1 <= a; p1++, p5++)
     778             :       {
     779     2408130 :         if (p5 == d) { p5 = 0; p4 = 0; } else p4 = x[p1-1];
     780     2408130 :         if (p5 == d-1) p6 = p1+1-d; else p6 = p1+1;
     781     6689472 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     782             :         {
     783     4281342 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     784     4281342 :           p3 += s; if (p3 > b) p3 -= b;
     785             :         }
     786     2408130 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     787     3126744 :         for (p2 = p4; p2 >= 1; p2--)
     788             :         {
     789      718614 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     790      718614 :           p3 -= s; if (p3 <= 0) p3 += b;
     791             :         }
     792             :       }
     793       79296 :       if (galois_test_perm(td, pf))
     794             :       {
     795        1476 :         if (DEBUGLEVEL >= 1)
     796             :         {
     797           0 :           GEN nb = addis(mulss(l2,N1),l1);
     798           0 :           timer_printf(&ti, "testpermutation(%Ps)", nb);
     799           0 :           if (DEBUGLEVEL >= 2 && hop)
     800           0 :             err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, nb);
     801             :         }
     802        1476 :         avma = av; return pf;
     803             :       }
     804       77820 :       hop++;
     805             :     }
     806             :   }
     807         198 :   if (DEBUGLEVEL >= 1)
     808             :   {
     809           0 :     timer_printf(&ti, "testpermutation(%Ps)", NN);
     810           0 :     if (DEBUGLEVEL >= 2 && hop)
     811           0 :       err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, NN);
     812             :   }
     813         198 :   avma = avm; return NULL;
     814             : }
     815             : 
     816             : /* List of subgroups of (Z/mZ)^* whose order divide o, and return the list
     817             :  * of their elements, sorted by increasing order */
     818             : static GEN
     819         696 : listznstarelts(long m, long o)
     820             : {
     821         696 :   pari_sp av = avma;
     822             :   GEN L, zn, zns;
     823             :   long i, phi, ind, l;
     824         696 :   if (m == 2) retmkvec(mkvecsmall(1));
     825         696 :   zn = znstar(stoi(m));
     826         696 :   phi = itos(gel(zn,1));
     827         696 :   o = ugcd(o, phi); /* do we impose this on input ? */
     828         696 :   zns = znstar_small(zn);
     829         696 :   L = cgetg(o+1, t_VEC);
     830        2142 :   for (i=1,ind = phi; ind; ind -= phi/o, i++) /* by *decreasing* exact index */
     831        1446 :     gel(L,i) = subgrouplist(gel(zn,2), mkvec(utoipos(ind)));
     832         696 :   L = shallowconcat1(L); l = lg(L);
     833         696 :   for (i = 1; i < l; i++) gel(L,i) = znstar_hnf_elts(zns, gel(L,i));
     834         696 :   return gerepilecopy(av, L);
     835             : }
     836             : 
     837             : /* A sympol is a symmetric polynomial
     838             :  *
     839             :  * Currently sympol are couple of t_VECSMALL [v,w]
     840             :  * v[1]...v[k], w[1]...w[k]  represent the polynomial sum(i=1,k,v[i]*s_w[i])
     841             :  * where s_i(X_1,...,X_n) = sum(j=1,n,X_j^i) */
     842             : 
     843             : /*Return s_e*/
     844             : static GEN
     845        3186 : sympol_eval_newtonsum(long e, GEN O, GEN mod)
     846             : {
     847        3186 :   long f = lg(O), g = lg(gel(O,1)), i, j;
     848        3186 :   GEN PL = cgetg(f, t_COL);
     849       17910 :   for(i=1; i<f; i++)
     850             :   {
     851       14724 :     pari_sp av = avma;
     852       14724 :     GEN s = gen_0;
     853       14724 :     for(j=1; j<g; j++) s = addii(s, Fp_powu(gmael(O,i,j), (ulong)e, mod));
     854       14724 :     gel(PL,i) = gerepileuptoint(av, remii(s,mod));
     855             :   }
     856        3186 :   return PL;
     857             : }
     858             : 
     859             : static GEN
     860        2160 : sympol_eval(GEN v, GEN NS)
     861             : {
     862        2160 :   pari_sp av = avma;
     863             :   long i;
     864        2160 :   GEN S = gen_0;
     865        4884 :   for (i=1; i<lg(v); i++)
     866        2724 :     if (v[i]) S = gadd(S, gmulsg(v[i], gel(NS,i)));
     867        2160 :   return gerepileupto(av, S);
     868             : }
     869             : 
     870             : /* Let sigma be an automorphism of L (as a polynomial with rational coefs)
     871             :  * Let 'sym' be a symmetric polynomial defining alpha in L.
     872             :  * We have alpha = sym(x,sigma(x),,,sigma^(g-1)(x)). Compute alpha mod p */
     873             : static GEN
     874        1008 : sympol_aut_evalmod(GEN sym, long g, GEN sigma, GEN Tp, GEN p)
     875             : {
     876        1008 :   pari_sp ltop=avma;
     877        1008 :   long i, j, npows = brent_kung_optpow(degpol(Tp)-1, g-1, 1);
     878        1008 :   GEN s, f, pows, v = zv_to_ZV(gel(sym,1)), w = zv_to_ZV(gel(sym,2));
     879        1008 :   sigma = RgX_to_FpX(sigma, p);
     880        1008 :   pows  = FpXQ_powers(sigma,npows,Tp,p);
     881        1008 :   f = pol_x(varn(sigma));
     882        1008 :   s = pol_0(varn(sigma));
     883        5376 :   for(i=1; i<=g;i++)
     884             :   {
     885        4368 :     if (i > 1) f = FpX_FpXQV_eval(f,pows,Tp,p);
     886        9186 :     for(j=1; j<lg(v); j++)
     887        4818 :       s = FpX_add(s, FpX_Fp_mul(FpXQ_pow(f,gel(w,j),Tp,p),gel(v,j),p),p);
     888             :   }
     889        1008 :   return gerepileupto(ltop, s);
     890             : }
     891             : 
     892             : /* Let Sp be as computed with sympol_aut_evalmod
     893             :  * Let Tmod be the factorisation of T mod p.
     894             :  * Return the factorisation of the minimal polynomial of S mod p w.r.t. Tmod */
     895             : static GEN
     896        1008 : fixedfieldfactmod(GEN Sp, GEN p, GEN Tmod)
     897             : {
     898        1008 :   long i, l = lg(Tmod);
     899        1008 :   GEN F = cgetg(l,t_VEC);
     900        4230 :   for(i=1; i<l; i++)
     901             :   {
     902        3222 :     GEN Ti = gel(Tmod,i);
     903        3222 :     gel(F,i) = FpXQ_minpoly(FpX_rem(Sp,Ti,p), Ti,p);
     904             :   }
     905        1008 :   return F;
     906             : }
     907             : 
     908             : static GEN
     909        1950 : fixedfieldsurmer(GEN mod, GEN l, GEN p, long v, GEN NS, GEN W)
     910             : {
     911        1950 :   const long step=3;
     912        1950 :   long i, j, n = lg(W)-1, m = 1L<<((n-1)<<1);
     913        1950 :   GEN sym = cgetg(n+1,t_VECSMALL), mod2 = shifti(mod,-1);
     914        1950 :   for (j=1;j<n;j++) sym[j] = step;
     915        1950 :   sym[n] = 0;
     916        1950 :   if (DEBUGLEVEL>=4) err_printf("FixedField: Weight: %Ps\n",W);
     917        2226 :   for (i=0; i<m; i++)
     918             :   {
     919        2160 :     pari_sp av = avma;
     920             :     GEN L, P;
     921        2160 :     for (j=1; sym[j]==step; j++) sym[j]=0;
     922        2160 :     sym[j]++;
     923        2160 :     if (DEBUGLEVEL>=6) err_printf("FixedField: Sym: %Ps\n",sym);
     924        2160 :     L = sympol_eval(sym,NS);
     925        2160 :     if (!vec_is1to1(FpC_red(L,l))) { avma = av; continue; }
     926        2040 :     P = FpX_center(FpV_roots_to_pol(L,mod,v),mod,mod2);
     927        2040 :     if (!p || FpX_is_squarefree(P,p)) return mkvec3(mkvec2(sym,W),L,P);
     928         156 :     avma = av;
     929             :   }
     930          66 :   return NULL;
     931             : }
     932             : 
     933             : /*Check whether the line of NS are pair-wise distinct.*/
     934             : static long
     935        2076 : sympol_is1to1_lg(GEN NS, long n)
     936             : {
     937        2076 :   long i, j, k, l = lgcols(NS);
     938       11904 :   for (i=1; i<l; i++)
     939       59460 :     for(j=i+1; j<l; j++)
     940             :     {
     941       50892 :       for(k=1; k<n; k++)
     942       50766 :         if (!equalii(gmael(NS,k,j),gmael(NS,k,i))) break;
     943       49632 :       if (k>=n) return 0;
     944             :     }
     945        1950 :   return 1;
     946             : }
     947             : 
     948             : /* Let O a set of orbits of roots (see fixedfieldorbits) modulo mod,
     949             :  * l | mod and p two prime numbers. Return a vector [sym,s,P] where:
     950             :  * sym is a sympol, s is the set of images of sym on O and
     951             :  * P is the polynomial with roots s. */
     952             : static GEN
     953        1884 : fixedfieldsympol(GEN O, GEN mod, GEN l, GEN p, long v)
     954             : {
     955        1884 :   pari_sp ltop=avma;
     956        1884 :   const long n=(BITS_IN_LONG>>1)-1;
     957        1884 :   GEN NS = cgetg(n+1,t_MAT), sym = NULL, W = cgetg(n+1,t_VECSMALL);
     958        1884 :   long i, e=1;
     959        1884 :   if (DEBUGLEVEL>=4)
     960           0 :     err_printf("FixedField: Size: %ldx%ld\n",lg(O)-1,lg(gel(O,1))-1);
     961        3960 :   for (i=1; !sym && i<=n; i++)
     962             :   {
     963        2076 :     GEN L = sympol_eval_newtonsum(e++, O, mod);
     964        2076 :     if (lg(O)>2)
     965        2034 :       while (vec_isconst(L)) L = sympol_eval_newtonsum(e++, O, mod);
     966        2076 :     W[i] = e-1; gel(NS,i) = L;
     967        2076 :     if (sympol_is1to1_lg(NS,i+1))
     968        1950 :       sym = fixedfieldsurmer(mod,l,p,v,NS,vecsmall_shorten(W,i));
     969             :   }
     970        1884 :   if (!sym) pari_err_BUG("fixedfieldsympol [p too small]");
     971        1884 :   if (DEBUGLEVEL>=2) err_printf("FixedField: Found: %Ps\n",gel(sym,1));
     972        1884 :   return gerepilecopy(ltop,sym);
     973             : }
     974             : 
     975             : /* Let O a set of orbits as indices and L the corresponding roots.
     976             :  * Return the set of orbits as roots. */
     977             : static GEN
     978        1884 : fixedfieldorbits(GEN O, GEN L)
     979             : {
     980        1884 :   GEN S = cgetg(lg(O), t_MAT);
     981             :   long i;
     982        1884 :   for (i = 1; i < lg(O); i++) gel(S,i) = vecpermute(L, gel(O,i));
     983        1884 :   return S;
     984             : }
     985             : 
     986             : static GEN
     987         672 : fixedfieldinclusion(GEN O, GEN PL)
     988             : {
     989         672 :   long i, j, f = lg(O)-1, g = lg(gel(O,1))-1;
     990         672 :   GEN S = cgetg(f*g + 1, t_COL);
     991        4914 :   for (i = 1; i <= f; i++)
     992             :   {
     993        4242 :     GEN Oi = gel(O,i);
     994        4242 :     for (j = 1; j <= g; j++) gel(S, Oi[j]) = gel(PL, i);
     995             :   }
     996         672 :   return S;
     997             : }
     998             : 
     999             : /* Polynomial attached to a vector of conjugates. Not stack clean */
    1000             : static GEN
    1001        5670 : vectopol(GEN v, GEN M, GEN den , GEN mod, GEN mod2, long x)
    1002             : {
    1003        5670 :   long l = lg(v)+1, i;
    1004        5670 :   GEN z = cgetg(l,t_POL);
    1005        5670 :   z[1] = evalsigne(1)|evalvarn(x);
    1006       70062 :   for (i=2; i<l; i++)
    1007       64392 :     gel(z,i) = gdiv(centermodii(ZMrow_ZC_mul(M,v,i-1), mod, mod2), den);
    1008        5670 :   return normalizepol_lg(z, l);
    1009             : }
    1010             : 
    1011             : /* Polynomial associate to a permutation of the roots. Not stack clean */
    1012             : static GEN
    1013        4698 : permtopol(GEN p, GEN L, GEN M, GEN den, GEN mod, GEN mod2, long x)
    1014             : {
    1015        4698 :   if (lg(p) != lg(L)) pari_err_TYPE("permtopol [permutation]", p);
    1016        4698 :   return vectopol(vecpermute(L,p), M, den, mod, mod2, x);
    1017             : }
    1018             : 
    1019             : static GEN
    1020         378 : galoisgrouptopol(GEN res, GEN L, GEN M, GEN den, GEN mod, long v)
    1021             : {
    1022         378 :   long i, l = lg(res);
    1023         378 :   GEN mod2 = shifti(mod,-1), aut = cgetg(l, t_COL);
    1024        2328 :   for (i = 1; i < l; i++)
    1025             :   {
    1026        1950 :     if (DEBUGLEVEL>=6) err_printf("%d ",i);
    1027        1950 :     gel(aut,i) = permtopol(gel(res,i), L, M, den, mod, mod2, v);
    1028             :   }
    1029         378 :   return aut;
    1030             : }
    1031             : 
    1032             : static void
    1033        1206 : notgalois(long p, struct galois_analysis *ga)
    1034             : {
    1035        1206 :   if (DEBUGLEVEL >= 2) err_printf("GaloisAnalysis:non Galois for p=%ld\n", p);
    1036        1206 :   ga->p = p;
    1037        1206 :   ga->deg = 0;
    1038        1206 : }
    1039             : 
    1040             : /*Gather information about the group*/
    1041             : static long
    1042        2844 : init_group(long n, long np, GEN Fp, GEN Fe, long *porder)
    1043             : {
    1044        2844 :   const long prim_nonwss_orders[] = { 36,48,56,60,75,80,196,200 };
    1045        2844 :   long i, phi_order = 1, order = 1, group = 0;
    1046             : 
    1047             :  /* non-WSS groups of this order? */
    1048       25512 :   for (i=0; i < (long)numberof(prim_nonwss_orders); i++)
    1049       22680 :     if (n % prim_nonwss_orders[i] == 0) { group |= ga_non_wss; break; }
    1050        2844 :   if (np == 2 && Fp[2] == 3 && Fe[2] == 1 && Fe[1] > 2) group |= ga_ext_2;
    1051             : 
    1052        4398 :   for (i = np; i > 0; i--)
    1053             :   {
    1054        3306 :     long p = Fp[i];
    1055        3306 :     if (phi_order % p == 0) { group |= ga_all_normal; break; }
    1056        2874 :     order *= p; phi_order *= p-1;
    1057        2874 :     if (Fe[i] > 1) break;
    1058             :   }
    1059        2844 :   *porder = order; return group;
    1060             : }
    1061             : 
    1062             : /*is a "better" than b ? (if so, update karma) */
    1063             : static int
    1064       12750 : improves(long a, long b, long plift, long p, long n, long *karma)
    1065             : {
    1066       12750 :   if (!plift || a > b) { *karma = ugcd(p-1,n); return 1; }
    1067       12066 :   if (a == b) {
    1068       10326 :     long k = ugcd(p-1,n);
    1069       10326 :     if (k > *karma) { *karma = k; return 1; }
    1070             :   }
    1071       11028 :   return 0; /* worse */
    1072             : }
    1073             : 
    1074             : /* return 0 if not galois or not wss */
    1075             : static int
    1076        2844 : galoisanalysis(GEN T, struct galois_analysis *ga, long calcul_l)
    1077             : {
    1078        2844 :   pari_sp ltop = avma, av;
    1079        2844 :   long group, linf, n, p, i, karma = 0;
    1080             :   GEN F, Fp, Fe, Fpe, O;
    1081             :   long np, order, plift, nbmax, nbtest, deg;
    1082             :   pari_timer ti;
    1083             :   forprime_t S;
    1084        2844 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    1085        2844 :   n = degpol(T);
    1086        2844 :   O = zero_zv(n);
    1087        2844 :   F = factoru_pow(n);
    1088        2844 :   Fp = gel(F,1); np = lg(Fp)-1;
    1089        2844 :   Fe = gel(F,2);
    1090        2844 :   Fpe= gel(F,3);
    1091        2844 :   group = init_group(n, np, Fp, Fe, &order);
    1092             : 
    1093             :   /*Now we study the orders of the Frobenius elements*/
    1094        2844 :   deg = Fp[np]; /* largest prime | n */
    1095        2844 :   plift = 0;
    1096        2844 :   nbtest = 0;
    1097        2844 :   nbmax = 8+(n>>1);
    1098        2844 :   u_forprime_init(&S, n*maxss(expu(n)-3, 2), ULONG_MAX);
    1099        2844 :   av = avma;
    1100       28056 :   while (!plift || (nbtest < nbmax && (nbtest <=8 || order < (n>>1)))
    1101        1650 :                 || (n == 24 && O[6] == 0 && O[4] == 0)
    1102        1650 :                 || ((group&ga_non_wss) && order == Fp[np]))
    1103             :   {
    1104       23562 :     long d, o, norm_o = 1;
    1105             :     GEN D, Tp;
    1106             : 
    1107       23562 :     if ((group&ga_non_wss) && nbtest >= 3*nbmax) break; /* in all cases */
    1108       23562 :     nbtest++; avma = av;
    1109       23562 :     p = u_forprime_next(&S);
    1110       23562 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1111       23562 :     Tp = ZX_to_Flx(T,p);
    1112       23562 :     if (!Flx_is_squarefree(Tp,p)) { if (!--nbtest) nbtest = 1; continue; }
    1113             : 
    1114       22188 :     D = Flx_nbfact_by_degree(Tp, &d, p);
    1115       22188 :     o = n / d; /* d factors, all should have degree o */
    1116       22188 :     if (D[o] != d) { notgalois(p, ga); avma = ltop; return 0; }
    1117             : 
    1118       20994 :     if (!O[o]) O[o] = p;
    1119       20994 :     if (o % deg) goto ga_end; /* NB: deg > 1 */
    1120       14928 :     if ((group&ga_all_normal) && o < order) goto ga_end;
    1121             : 
    1122             :     /*Frob_p has order o > 1, find a power which generates a normal subgroup*/
    1123       14844 :     if (o * Fp[1] >= n)
    1124        8250 :       norm_o = o; /*subgroups of smallest index are normal*/
    1125             :     else
    1126             :     {
    1127        7206 :       for (i = np; i > 0; i--)
    1128             :       {
    1129        7206 :         if (o % Fpe[i]) break;
    1130         612 :         norm_o *= Fpe[i];
    1131             :       }
    1132             :     }
    1133             :     /* Frob_p^(o/norm_o) generates a normal subgroup of order norm_o */
    1134       14844 :     if (norm_o != 1)
    1135             :     {
    1136        8862 :       if (!(group&ga_all_normal) || o > order)
    1137        2094 :         karma = ugcd(p-1,n);
    1138        6768 :       else if (!improves(norm_o, deg, plift,p,n, &karma)) goto ga_end;
    1139             :       /* karma0=0, deg0<=norm_o -> the first improves() returns 1 */
    1140        3138 :       deg = norm_o; group |= ga_all_normal; /* STORE */
    1141             :     }
    1142        5982 :     else if (group&ga_all_normal) goto ga_end;
    1143        5982 :     else if (!improves(o, order, plift,p,n, &karma)) goto ga_end;
    1144             : 
    1145        3816 :     order = o; plift = p; /* STORE */
    1146             :     ga_end:
    1147       20994 :     if (DEBUGLEVEL >= 5)
    1148           0 :       err_printf("GaloisAnalysis:Nbtest=%ld,p=%ld,o=%ld,n_o=%d,best p=%ld,ord=%ld,k=%ld\n", nbtest, p, o, norm_o, plift, order,karma);
    1149             :   }
    1150             :   /* To avoid looping on non-wss group.
    1151             :    * TODO: check for large groups. Would it be better to disable this check if
    1152             :    * we are in a good case (ga_all_normal && !(ga_ext_2) (e.g. 60)) ?*/
    1153        1650 :   ga->p = plift;
    1154        1650 :   if (!plift || ((group&ga_non_wss) && order == Fp[np]))
    1155             :   {
    1156           0 :     pari_warn(warner,"Galois group almost certainly not weakly super solvable");
    1157           0 :     return 0;
    1158             :   }
    1159        1650 :   linf = 2*n*usqrt(n);
    1160        1650 :   if (calcul_l && O[1] <= linf)
    1161             :   {
    1162             :     pari_sp av2;
    1163             :     forprime_t S2;
    1164             :     ulong p;
    1165         786 :     u_forprime_init(&S2, linf+1,ULONG_MAX);
    1166         786 :     av2 = avma;
    1167       28182 :     while ((p = u_forprime_next(&S2)))
    1168             :     { /*find a totally split prime l > linf*/
    1169       27396 :       GEN Tp = ZX_to_Flx(T, p);
    1170       27396 :       long nb = Flx_nbroots(Tp, p);
    1171       27396 :       if (nb == n) { O[1] = p; break; }
    1172       26622 :       if (nb && Flx_is_squarefree(Tp,p)) {
    1173          12 :         notgalois(p,ga);
    1174          12 :         avma = ltop; return 0;
    1175             :       }
    1176       26610 :       avma = av2;
    1177             :     }
    1178         774 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1179             :   }
    1180        1638 :   ga->group = (enum ga_code)group;
    1181        1638 :   ga->deg = deg;
    1182        1638 :   ga->mindeg =  n == 135 ? 15: 0; /* otherwise the second phase is too slow */
    1183        1638 :   ga->ord = order;
    1184        1638 :   ga->l  = O[1];
    1185        1638 :   ga->p4 = n >= 4 ? O[4] : 0;
    1186        1638 :   if (DEBUGLEVEL >= 4)
    1187           0 :     err_printf("GaloisAnalysis:p=%ld l=%ld group=%ld deg=%ld ord=%ld\n",
    1188           0 :                plift, O[1], group, deg, order);
    1189        1638 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisanalysis()");
    1190        1638 :   avma = ltop; return 1;
    1191             : }
    1192             : 
    1193             : static GEN
    1194          24 : a4galoisgen(struct galois_test *td)
    1195             : {
    1196          24 :   const long n = 12;
    1197          24 :   pari_sp ltop = avma, av, av2;
    1198          24 :   long i, j, k, N, hop = 0;
    1199             :   GEN MT, O,O1,O2,O3, ar, mt, t, u, res, orb, pft, pfu, pfv;
    1200             : 
    1201          24 :   res = cgetg(3, t_VEC);
    1202          24 :   pft = cgetg(n+1, t_VECSMALL);
    1203          24 :   pfu = cgetg(n+1, t_VECSMALL);
    1204          24 :   pfv = cgetg(n+1, t_VECSMALL);
    1205          24 :   gel(res,1) = mkvec3(pft,pfu,pfv);
    1206          24 :   gel(res,2) = mkvecsmall3(2,2,3);
    1207          24 :   av = avma;
    1208          24 :   ar = cgetg(5, t_VECSMALL);
    1209          24 :   mt = gel(td->PV, td->order[n]);
    1210          24 :   t = identity_perm(n) + 1; /* Sorry for this hack */
    1211          24 :   u = cgetg(n+1, t_VECSMALL) + 1; /* too lazy to correct */
    1212          24 :   MT = cgetg(n+1, t_MAT);
    1213          24 :   for (j = 1; j <= n; j++) gel(MT,j) = cgetg(n+1, t_VECSMALL);
    1214         312 :   for (j = 1; j <= n; j++)
    1215        1872 :     for (i = 1; i < j; i++)
    1216        1584 :       ucoeff(MT,i,j) = ucoeff(MT,j,i) = ucoeff(mt,i,j)+ucoeff(mt,j,i);
    1217             :   /* MT(i,i) unused */
    1218             : 
    1219          24 :   av2 = avma;
    1220             :   /* N = itos(gdiv(mpfact(n), mpfact(n >> 1))) >> (n >> 1); */
    1221             :   /* n = 2k = 12; N = (2k)! / (k! * 2^k) = 10395 */
    1222          24 :   N = 10395;
    1223          24 :   if (DEBUGLEVEL>=4) err_printf("A4GaloisConj: will test %ld permutations\n", N);
    1224          24 :   uel(ar,4) = umael(MT,11,12);
    1225          24 :   uel(ar,3) = uel(ar,4) + umael(MT,9,10);
    1226          24 :   uel(ar,2) = uel(ar,3) + umael(MT,7,8);
    1227          24 :   uel(ar,1) = uel(ar,2) + umael(MT,5,6);
    1228       62766 :   for (i = 0; i < N; i++)
    1229             :   {
    1230             :     long g;
    1231       62766 :     if (i)
    1232             :     {
    1233       62742 :       long a, x = i, y = 1;
    1234       88452 :       do { y += 2; a = x%y; x = x/y; } while (!a);
    1235       62742 :       switch (y)
    1236             :       {
    1237             :       case 3:
    1238       41838 :         lswap(t[2], t[2-a]);
    1239       41838 :         break;
    1240             :       case 5:
    1241       16728 :         x = t[0]; t[0] = t[2]; t[2] = t[1]; t[1] = x;
    1242       16728 :         lswap(t[4], t[4-a]);
    1243       16728 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1244       16728 :         break;
    1245             :       case 7:
    1246        3594 :         x = t[0]; t[0] = t[4]; t[4] = t[3]; t[3] = t[1]; t[1] = t[2]; t[2] = x;
    1247        3594 :         lswap(t[6], t[6-a]);
    1248        3594 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1249        3594 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1250        3594 :         break;
    1251             :       case 9:
    1252         534 :         x = t[0]; t[0] = t[6]; t[6] = t[5]; t[5] = t[3]; t[3] = x;
    1253         534 :         lswap(t[1], t[4]);
    1254         534 :         lswap(t[8], t[8-a]);
    1255         534 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1256         534 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1257         534 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1258         534 :         break;
    1259             :       case 11:
    1260          48 :         x = t[0]; t[0] = t[8]; t[8] = t[7]; t[7] = t[5]; t[5] = t[1];
    1261          48 :         t[1] = t[6]; t[6] = t[3]; t[3] = t[2]; t[2] = t[4]; t[4] = x;
    1262          48 :         lswap(t[10], t[10-a]);
    1263          48 :         uel(ar,4) = umael(MT,t[10],t[11]);
    1264          48 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1265          48 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1266          48 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1267             :       }
    1268             :     }
    1269       62766 :     g = uel(ar,1)+umael(MT,t[0],t[1])+umael(MT,t[2],t[3]);
    1270       62766 :     if (headlongisint(g,n))
    1271             :     {
    1272         168 :       for (k = 0; k < n; k += 2)
    1273             :       {
    1274         144 :         pft[t[k]] = t[k+1];
    1275         144 :         pft[t[k+1]] = t[k];
    1276             :       }
    1277          24 :       if (galois_test_perm(td, pft)) break;
    1278           0 :       hop++;
    1279             :     }
    1280       62742 :     avma = av2;
    1281             :   }
    1282          24 :   if (DEBUGLEVEL >= 1 && hop)
    1283           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1284          24 :   if (i == N) { avma = ltop; return gen_0; }
    1285             :   /* N = itos(gdiv(mpfact(n >> 1), mpfact(n >> 2))) >> 1; */
    1286          24 :   N = 60;
    1287          24 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: sigma=%Ps \n", pft);
    1288          96 :   for (k = 0; k < n; k += 4)
    1289             :   {
    1290          72 :     u[k+3] = t[k+3];
    1291          72 :     u[k+2] = t[k+1];
    1292          72 :     u[k+1] = t[k+2];
    1293          72 :     u[k]   = t[k];
    1294             :   }
    1295        1116 :   for (i = 0; i < N; i++)
    1296             :   {
    1297        1116 :     ulong g = 0;
    1298        1116 :     if (i)
    1299             :     {
    1300        1092 :       long a, x = i, y = -2;
    1301        1722 :       do { y += 4; a = x%y; x = x/y; } while (!a);
    1302        1092 :       lswap(u[0],u[2]);
    1303        1092 :       switch (y)
    1304             :       {
    1305             :       case 2:
    1306         546 :         break;
    1307             :       case 6:
    1308         462 :         lswap(u[4],u[6]);
    1309         462 :         if (!(a & 1))
    1310             :         {
    1311         186 :           a = 4 - (a>>1);
    1312         186 :           lswap(u[6], u[a]);
    1313         186 :           lswap(u[4], u[a-2]);
    1314             :         }
    1315         462 :         break;
    1316             :       case 10:
    1317          84 :         x = u[6];
    1318          84 :         u[6] = u[3];
    1319          84 :         u[3] = u[2];
    1320          84 :         u[2] = u[4];
    1321          84 :         u[4] = u[1];
    1322          84 :         u[1] = u[0];
    1323          84 :         u[0] = x;
    1324          84 :         if (a >= 3) a += 2;
    1325          84 :         a = 8 - a;
    1326          84 :         lswap(u[10],u[a]);
    1327          84 :         lswap(u[8], u[a-2]);
    1328          84 :         break;
    1329             :       }
    1330             :     }
    1331        1116 :     for (k = 0; k < n; k += 2) g += mael(MT,u[k],u[k+1]);
    1332        1116 :     if (headlongisint(g,n))
    1333             :     {
    1334         168 :       for (k = 0; k < n; k += 2)
    1335             :       {
    1336         144 :         pfu[u[k]] = u[k+1];
    1337         144 :         pfu[u[k+1]] = u[k];
    1338             :       }
    1339          24 :       if (galois_test_perm(td, pfu)) break;
    1340           0 :       hop++;
    1341             :     }
    1342        1092 :     avma = av2;
    1343             :   }
    1344          24 :   if (i == N) { avma = ltop; return gen_0; }
    1345          24 :   if (DEBUGLEVEL >= 1 && hop)
    1346           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1347          24 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: tau=%Ps \n", pfu);
    1348          24 :   avma = av2;
    1349          24 :   orb = mkvec2(pft,pfu);
    1350          24 :   O = vecperm_orbits(orb, 12);
    1351          24 :   if (DEBUGLEVEL >= 4) {
    1352           0 :     err_printf("A4GaloisConj: orb=%Ps\n", orb);
    1353           0 :     err_printf("A4GaloisConj: O=%Ps \n", O);
    1354             :   }
    1355          24 :   av2 = avma;
    1356          24 :   O1 = gel(O,1); O2 = gel(O,2); O3 = gel(O,3);
    1357          36 :   for (j = 0; j < 2; j++)
    1358             :   {
    1359          36 :     pfv[O1[1]] = O2[1];
    1360          36 :     pfv[O1[2]] = O2[3+j];
    1361          36 :     pfv[O1[3]] = O2[4 - (j << 1)];
    1362          36 :     pfv[O1[4]] = O2[2+j];
    1363         120 :     for (i = 0; i < 4; i++)
    1364             :     {
    1365         108 :       ulong g = 0;
    1366         108 :       switch (i)
    1367             :       {
    1368          36 :       case 0: break;
    1369          36 :       case 1: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1370          24 :       case 2: lswap(O3[1], O3[4]); lswap(O3[2], O3[3]); break;
    1371          12 :       case 3: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1372             :       }
    1373         108 :       pfv[O2[1]]          = O3[1];
    1374         108 :       pfv[O2[3+j]]        = O3[4-j];
    1375         108 :       pfv[O2[4 - (j<<1)]] = O3[2 + (j<<1)];
    1376         108 :       pfv[O2[2+j]]        = O3[3-j];
    1377         108 :       pfv[O3[1]]          = O1[1];
    1378         108 :       pfv[O3[4-j]]        = O1[2];
    1379         108 :       pfv[O3[2 + (j<<1)]] = O1[3];
    1380         108 :       pfv[O3[3-j]]        = O1[4];
    1381         108 :       for (k = 1; k <= n; k++) g += mael(mt,k,pfv[k]);
    1382         108 :       if (headlongisint(g,n) && galois_test_perm(td, pfv))
    1383             :       {
    1384          24 :         avma = av;
    1385          24 :         if (DEBUGLEVEL >= 1)
    1386           0 :           err_printf("A4GaloisConj: %ld hop over %d iterations max\n",
    1387             :                      hop, 10395 + 68);
    1388          24 :         return res;
    1389             :       }
    1390          84 :       hop++; avma = av2;
    1391             :     }
    1392             :   }
    1393           0 :   avma = ltop; return gen_0; /* Fail */
    1394             : }
    1395             : 
    1396             : /* S4 */
    1397             : static void
    1398         168 : s4makelift(GEN u, struct galois_lift *gl, GEN liftpow)
    1399             : {
    1400         168 :   GEN s = automorphismlift(u, gl, NULL);
    1401             :   long i;
    1402         168 :   gel(liftpow,1) = s;
    1403        3864 :   for (i = 2; i < lg(liftpow); i++)
    1404        3696 :     gel(liftpow,i) = FpXQ_mul(gel(liftpow,i-1), s, gl->TQ, gl->Q);
    1405         168 : }
    1406             : static long
    1407        2622 : s4test(GEN u, GEN liftpow, struct galois_lift *gl, GEN phi)
    1408             : {
    1409        2622 :   pari_sp av = avma;
    1410             :   GEN res, Q, Q2;
    1411        2622 :   long bl, i, d = lg(u)-2;
    1412             :   pari_timer ti;
    1413        2622 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
    1414        2622 :   if (!d) return 0;
    1415        2622 :   Q = gl->Q; Q2 = shifti(Q,-1);
    1416        2622 :   res = gel(u,2);
    1417       61476 :   for (i = 1; i < d; i++)
    1418       58854 :     if (lg(gel(liftpow,i))>2)
    1419       58854 :       res = addii(res, mulii(gmael(liftpow,i,2), gel(u,i+2)));
    1420        2622 :   res = remii(res,Q);
    1421        2622 :   if (gl->den != gen_1) res = mulii(res, gl->den);
    1422        2622 :   res = centermodii(res, Q,Q2);
    1423        2622 :   if (abscmpii(res, gl->gb->bornesol) > 0) { avma = av; return 0; }
    1424         240 :   res = scalar_ZX_shallow(gel(u,2),varn(u));
    1425        5364 :   for (i = 1; i < d ; i++)
    1426        5124 :     if (lg(gel(liftpow,i))>2)
    1427        5124 :       res = ZX_add(res, ZX_Z_mul(gel(liftpow,i), gel(u,i+2)));
    1428         240 :   res = FpX_red(res, Q);
    1429         240 :   if (gl->den != gen_1) res = FpX_Fp_mul(res, gl->den, Q);
    1430         240 :   res = FpX_center(res, Q, shifti(Q,-1));
    1431         240 :   bl = poltopermtest(res, gl, phi);
    1432         240 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "s4test()");
    1433         240 :   avma = av; return bl;
    1434             : }
    1435             : 
    1436             : static GEN
    1437         504 : aux(long a, long b, GEN T, GEN M, GEN p, GEN *pu)
    1438             : {
    1439         504 :   *pu = FpX_mul(gel(T,b), gel(T,a),p);
    1440        1512 :   return FpX_chinese_coprime(gmael(M,a,b), gmael(M,b,a),
    1441        1008 :                              gel(T,b), gel(T,a), *pu, p);
    1442             : }
    1443             : 
    1444             : static GEN
    1445         168 : s4releveauto(GEN misom,GEN Tmod,GEN Tp,GEN p,long a1,long a2,long a3,long a4,long a5,long a6)
    1446             : {
    1447         168 :   pari_sp av = avma;
    1448             :   GEN u4,u5;
    1449             :   GEN pu1, pu2, pu3, pu4;
    1450         168 :   GEN u1 = aux(a1, a2, Tmod, misom, p, &pu1);
    1451         168 :   GEN u2 = aux(a3, a4, Tmod, misom, p, &pu2);
    1452         168 :   GEN u3 = aux(a5, a6, Tmod, misom, p, &pu3);
    1453         168 :   pu4 = FpX_mul(pu1,pu2,p);
    1454         168 :   u4 = FpX_chinese_coprime(u1,u2,pu1,pu2,pu4,p);
    1455         168 :   u5 = FpX_chinese_coprime(u4,u3,pu4,pu3,Tp,p);
    1456         168 :   return gerepileupto(av, u5);
    1457             : }
    1458             : static GEN
    1459        4404 : lincomb(GEN A, GEN B, GEN pauto, long j)
    1460             : {
    1461        4404 :   long k = (-j) & 3;
    1462        4404 :   if (j == k) return ZX_mul(ZX_add(A,B), gel(pauto, j+1));
    1463        2262 :   return ZX_add(ZX_mul(A, gel(pauto, j+1)), ZX_mul(B, gel(pauto, k+1)));
    1464             : }
    1465             : /* FIXME: could use the intheadlong technique */
    1466             : static GEN
    1467          24 : s4galoisgen(struct galois_lift *gl)
    1468             : {
    1469          24 :   const long n = 24;
    1470             :   struct galois_testlift gt;
    1471          24 :   pari_sp av, ltop2, ltop = avma;
    1472             :   long i, j;
    1473          24 :   GEN sigma, tau, phi, res, r1,r2,r3,r4, pj, p = gl->p, Q = gl->Q, TQ = gl->TQ;
    1474             :   GEN sg, Tp, Tmod, isom, isominv, misom, Bcoeff, pauto, liftpow, aut;
    1475             : 
    1476          24 :   res = cgetg(3, t_VEC);
    1477          24 :   r1 = cgetg(n+1, t_VECSMALL);
    1478          24 :   r2 = cgetg(n+1, t_VECSMALL);
    1479          24 :   r3 = cgetg(n+1, t_VECSMALL);
    1480          24 :   r4 = cgetg(n+1, t_VECSMALL);
    1481          24 :   gel(res,1)= mkvec4(r1,r2,r3,r4);
    1482          24 :   gel(res,2) = mkvecsmall4(2,2,3,2);
    1483          24 :   ltop2 = avma;
    1484          24 :   sg = identity_perm(6);
    1485          24 :   pj = zero_zv(6);
    1486          24 :   sigma = cgetg(n+1, t_VECSMALL);
    1487          24 :   tau = cgetg(n+1, t_VECSMALL);
    1488          24 :   phi = cgetg(n+1, t_VECSMALL);
    1489          24 :   Tp = FpX_red(gl->T,p);
    1490          24 :   Tmod = gel(FpX_factor(Tp,p), 1);
    1491          24 :   isom    = cgetg(lg(Tmod), t_VEC);
    1492          24 :   isominv = cgetg(lg(Tmod), t_VEC);
    1493          24 :   misom   = cgetg(lg(Tmod), t_MAT);
    1494          24 :   aut = galoisdolift(gl, NULL);
    1495          24 :   inittestlift(aut, Tmod, gl, &gt);
    1496          24 :   Bcoeff = gt.bezoutcoeff;
    1497          24 :   pauto = gt.pauto;
    1498         168 :   for (i = 1; i < lg(isom); i++)
    1499             :   {
    1500         144 :     gel(misom,i) = cgetg(lg(Tmod), t_COL);
    1501         144 :     gel(isom,i) = FpX_ffisom(gel(Tmod,1), gel(Tmod,i), p);
    1502         144 :     if (DEBUGLEVEL >= 6)
    1503           0 :       err_printf("S4GaloisConj: Computing isomorphisms %d:%Ps\n", i,
    1504           0 :                  gel(isom,i));
    1505         144 :     gel(isominv,i) = FpXQ_ffisom_inv(gel(isom,i), gel(Tmod,i),p);
    1506             :   }
    1507         168 :   for (i = 1; i < lg(isom); i++)
    1508        1008 :     for (j = 1; j < lg(isom); j++)
    1509        1728 :       gmael(misom,i,j) = FpX_FpXQ_eval(gel(isominv,i),gel(isom,j),
    1510         864 :                                          gel(Tmod,j),p);
    1511          24 :   liftpow = cgetg(24, t_VEC);
    1512          24 :   av = avma;
    1513          42 :   for (i = 0; i < 3; i++, avma = av)
    1514             :   {
    1515             :     pari_sp av1, av2, av3;
    1516             :     GEN u, u1, u2, u3;
    1517             :     long j1, j2, j3;
    1518          42 :     if (i)
    1519             :     {
    1520          18 :       if (i == 1) { lswap(sg[2],sg[3]); }
    1521           0 :       else        { lswap(sg[1],sg[3]); }
    1522             :     }
    1523          42 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[2],sg[3],sg[4],sg[5],sg[6]);
    1524          42 :     s4makelift(u, gl, liftpow);
    1525          42 :     av1 = avma;
    1526         138 :     for (j1 = 0; j1 < 4; j1++, avma = av1)
    1527             :     {
    1528         120 :       u1 = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto,j1);
    1529         120 :       u1 = FpX_rem(u1, TQ, Q); av2 = avma;
    1530         534 :       for (j2 = 0; j2 < 4; j2++, avma = av2)
    1531             :       {
    1532         438 :         u2 = lincomb(gel(Bcoeff,sg[3]),gel(Bcoeff,sg[4]), pauto,j2);
    1533         438 :         u2 = FpX_rem(FpX_add(u1, u2, Q), TQ,Q); av3 = avma;
    1534        2130 :         for (j3 = 0; j3 < 4; j3++, avma = av3)
    1535             :         {
    1536        1716 :           u3 = lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[2]), pauto,j3);
    1537        1716 :           u3 = FpX_rem(FpX_add(u2, u3, Q), TQ,Q);
    1538        1716 :           if (DEBUGLEVEL >= 4)
    1539           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/4:%d/4:%d/4:%Ps\n",
    1540             :                        i,j1,j2,j3, sg);
    1541        1716 :           if (s4test(u3, liftpow, gl, sigma))
    1542             :           {
    1543          24 :             pj[1] = j3;
    1544          24 :             pj[2] = j2;
    1545          24 :             pj[3] = j1; goto suites4;
    1546             :           }
    1547             :         }
    1548             :       }
    1549             :     }
    1550             :   }
    1551           0 :   avma = ltop; return gen_0;
    1552             : suites4:
    1553          24 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: sigma=%Ps\n", sigma);
    1554          24 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: pj=%Ps\n", pj);
    1555          24 :   avma = av;
    1556          54 :   for (j = 1; j <= 3; j++)
    1557             :   {
    1558             :     pari_sp av2, av3;
    1559             :     GEN u;
    1560             :     long w, l, z;
    1561          54 :     z = sg[1]; sg[1] = sg[3]; sg[3] = sg[5]; sg[5] = z;
    1562          54 :     z = sg[2]; sg[2] = sg[4]; sg[4] = sg[6]; sg[6] = z;
    1563          54 :     z = pj[1]; pj[1] = pj[2]; pj[2] = pj[3]; pj[3] = z;
    1564         132 :     for (l = 0; l < 2; l++, avma = av)
    1565             :     {
    1566         102 :       u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[3],sg[2],sg[4],sg[5],sg[6]);
    1567         102 :       s4makelift(u, gl, liftpow);
    1568         102 :       av2 = avma;
    1569         282 :       for (w = 0; w < 4; w += 2, avma = av2)
    1570             :       {
    1571             :         GEN uu;
    1572         204 :         pj[6] = (w + pj[3]) & 3;
    1573         204 :         uu = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto, pj[6]);
    1574         204 :         uu = FpX_rem(FpX_red(uu,Q), TQ, Q);
    1575         204 :         av3 = avma;
    1576         972 :         for (i = 0; i < 4; i++, avma = av3)
    1577             :         {
    1578             :           GEN u;
    1579         792 :           pj[4] = i;
    1580         792 :           pj[5] = (i + pj[2] - pj[1]) & 3;
    1581         792 :           if (DEBUGLEVEL >= 4)
    1582           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/2:%d/2:%d/4:%Ps:%Ps\n",
    1583             :                        j-1, w >> 1, l, i, sg, pj);
    1584        2376 :           u = ZX_add(lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[3]), pauto,pj[4]),
    1585        2376 :                      lincomb(gel(Bcoeff,sg[2]),gel(Bcoeff,sg[4]), pauto,pj[5]));
    1586         792 :           u = FpX_rem(FpX_add(uu,u,Q), TQ, Q);
    1587         792 :           if (s4test(u, liftpow, gl, tau)) goto suites4_2;
    1588             :         }
    1589             :       }
    1590          78 :       lswap(sg[3], sg[4]);
    1591          78 :       pj[2] = (-pj[2]) & 3;
    1592             :     }
    1593             :   }
    1594           0 :   avma = ltop; return gen_0;
    1595             : suites4_2:
    1596          24 :   avma = av;
    1597             :   {
    1598          24 :     long abc = (pj[1] + pj[2] + pj[3]) & 3;
    1599          24 :     long abcdef = ((abc + pj[4] + pj[5] - pj[6]) & 3) >> 1;
    1600             :     GEN u;
    1601             :     pari_sp av2;
    1602          24 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[4],sg[2],sg[5],sg[3],sg[6]);
    1603          24 :     s4makelift(u, gl, liftpow);
    1604          24 :     av2 = avma;
    1605         114 :     for (j = 0; j < 8; j++)
    1606             :     {
    1607             :       long h, g, i;
    1608         114 :       h = j & 3;
    1609         114 :       g = (abcdef + ((j & 4) >> 1)) & 3;
    1610         114 :       i = (h + abc - g) & 3;
    1611         228 :       u = ZX_add(   lincomb(gel(Bcoeff,sg[1]), gel(Bcoeff,sg[4]), pauto, g),
    1612         228 :                     lincomb(gel(Bcoeff,sg[2]), gel(Bcoeff,sg[5]), pauto, h));
    1613         114 :       u = FpX_add(u, lincomb(gel(Bcoeff,sg[3]), gel(Bcoeff,sg[6]), pauto, i),Q);
    1614         114 :       u = FpX_rem(u, TQ, Q);
    1615         114 :       if (DEBUGLEVEL >= 4)
    1616           0 :         err_printf("S4GaloisConj: Testing %d/8 %d:%d:%d\n", j,g,h,i);
    1617         114 :       if (s4test(u, liftpow, gl, phi)) break;
    1618          90 :       avma = av2;
    1619             :     }
    1620             :   }
    1621          24 :   if (j == 8) { avma = ltop; return gen_0; }
    1622         600 :   for (i = 1; i <= n; i++)
    1623             :   {
    1624         576 :     r1[i] = sigma[tau[i]];
    1625         576 :     r2[i] = phi[sigma[tau[phi[i]]]];
    1626         576 :     r3[i] = phi[sigma[i]];
    1627         576 :     r4[i] = sigma[i];
    1628             :   }
    1629          24 :   avma = ltop2; return res;
    1630             : }
    1631             : 
    1632             : static GEN
    1633         390 : galoisfindgroups(GEN lo, GEN sg, long f)
    1634             : {
    1635         390 :   pari_sp ltop = avma;
    1636             :   long i, j, k;
    1637         390 :   GEN V = cgetg(lg(lo), t_VEC);
    1638        1464 :   for(j=1,i=1; i<lg(lo); i++)
    1639             :   {
    1640        1074 :     pari_sp av = avma;
    1641        1074 :     GEN loi = gel(lo,i), W = cgetg(lg(loi),t_VECSMALL);
    1642        1074 :     for (k=1; k<lg(loi); k++) W[k] = loi[k] % f;
    1643        1074 :     W = vecsmall_uniq(W);
    1644        1074 :     if (zv_equal(W, sg)) gel(V,j++) = loi;
    1645        1074 :     avma = av;
    1646             :   }
    1647         390 :   setlg(V,j); return gerepilecopy(ltop,V);
    1648             : }
    1649             : 
    1650             : static long
    1651        1788 : galoisfrobeniustest(GEN aut, struct galois_lift *gl, GEN frob)
    1652             : {
    1653        1788 :   pari_sp av = avma;
    1654        1788 :   GEN tlift = aut;
    1655             :   long res;
    1656        1788 :   if (gl->den != gen_1) tlift = FpX_Fp_mul(tlift, gl->den, gl->Q);
    1657        1788 :   tlift = FpX_center(tlift, gl->Q, shifti(gl->Q,-1));
    1658        1788 :   res = poltopermtest(tlift, gl, frob);
    1659        1788 :   avma = av; return res;
    1660             : }
    1661             : 
    1662             : static GEN
    1663         558 : galoismakepsi(long g, GEN sg, GEN pf)
    1664             : {
    1665         558 :   GEN psi=cgetg(g+1,t_VECSMALL);
    1666             :   long i;
    1667         558 :   for (i = 1; i < g; i++) psi[i] = sg[pf[i]];
    1668         558 :   psi[g] = sg[1]; return psi;
    1669             : }
    1670             : 
    1671             : static GEN
    1672        1644 : galoisfrobeniuslift(GEN T, GEN den, GEN L,  GEN Lden,
    1673             :     struct galois_frobenius *gf,  struct galois_borne *gb, long mindeg)
    1674             : {
    1675        1644 :   pari_sp ltop=avma, av2;
    1676             :   struct galois_testlift gt;
    1677             :   struct galois_lift gl;
    1678        1644 :   long i, j, k, n = lg(L)-1, deg = 1, g = lg(gf->Tmod)-1;
    1679        1644 :   GEN F,Fp,Fe, aut, frob, ip = utoipos(gf->p), res = cgetg(lg(L), t_VECSMALL);
    1680        1644 :   gf->psi = const_vecsmall(g,1);
    1681        1644 :   av2 = avma;
    1682        1644 :   initlift(T, den, ip, L, Lden, gb, &gl);
    1683        1644 :   if (DEBUGLEVEL >= 4)
    1684           0 :     err_printf("GaloisConj: p=%ld e=%ld deg=%ld fp=%ld\n",
    1685             :                             gf->p, gl.e, deg, gf->fp);
    1686        1644 :   aut = galoisdolift(&gl, res);
    1687        1644 :   if (!aut || galoisfrobeniustest(aut,&gl,res))
    1688             :   {
    1689        1062 :     avma = av2; gf->deg = gf->fp; return res;
    1690             :   }
    1691         582 :   if (gf->fp <= mindeg) return NULL;
    1692         582 :   inittestlift(aut,gf->Tmod, &gl, &gt);
    1693         582 :   gt.C = cgetg(gf->fp+1,t_VEC);
    1694         582 :   gt.Cd= cgetg(gf->fp+1,t_VEC);
    1695        4122 :   for (i = 1; i <= gf->fp; i++) {
    1696        3540 :     gel(gt.C,i)  = zero_zv(gt.g);
    1697        3540 :     gel(gt.Cd,i) = zero_zv(gt.g);
    1698             :   }
    1699             : 
    1700         582 :   F =factoru(gf->fp);
    1701         582 :   Fp = gel(F,1);
    1702         582 :   Fe = gel(F,2);
    1703         582 :   frob = cgetg(lg(L), t_VECSMALL);
    1704        1236 :   for(k=lg(Fp)-1;k>=1;k--)
    1705             :   {
    1706         654 :     pari_sp btop=avma;
    1707         654 :     GEN psi=NULL, fres=NULL, sg = identity_perm(1);
    1708         654 :     long el=gf->fp, dg=1, dgf=1, e, pr;
    1709        1614 :     for(e=1; e<=Fe[k]; e++)
    1710             :     {
    1711             :       GEN lo, pf;
    1712             :       long l;
    1713        1098 :       dg *= Fp[k]; el /= Fp[k];
    1714        1500 :       if (dg < mindeg) continue;
    1715        1098 :       if (DEBUGLEVEL>=4) err_printf("Trying degre %d.\n",dg);
    1716        1098 :       if (galoisfrobeniustest(gel(gt.pauto,el+1),&gl,frob))
    1717             :       {
    1718         402 :         psi = const_vecsmall(g,1);
    1719         402 :         dgf = dg; fres = gcopy(frob); continue;
    1720             :       }
    1721         696 :       lo = listznstarelts(dg, n / gf->fp);
    1722         696 :       if (e!=1) lo = galoisfindgroups(lo, sg, dgf);
    1723         696 :       if (DEBUGLEVEL>=4) err_printf("Galoisconj:Subgroups list:%Ps\n", lo);
    1724        1434 :       for (l = 1; l < lg(lo); l++)
    1725        1296 :         if (lg(gel(lo,l))>2 && frobeniusliftall(gel(lo,l), el, &pf,&gl,&gt, frob))
    1726             :         {
    1727         558 :           sg  = gcopy(gel(lo,l));
    1728         558 :           psi = galoismakepsi(g,sg,pf);
    1729         558 :           dgf = dg; fres = gcopy(frob); break;
    1730             :         }
    1731         696 :       if (l == lg(lo)) break;
    1732             :     }
    1733         654 :     if (dgf == 1) { avma = btop; continue; }
    1734         594 :     pr = deg*dgf;
    1735         594 :     if (deg == 1)
    1736             :     {
    1737         528 :       for(i=1;i<lg(res);i++) res[i]=fres[i];
    1738         528 :       for(i=1;i<lg(psi);i++) gf->psi[i]=psi[i];
    1739             :     }
    1740             :     else
    1741             :     {
    1742          66 :       GEN cp = perm_mul(res,fres);
    1743          66 :       for(i=1;i<lg(res);i++) res[i] = cp[i];
    1744          66 :       for(i=1;i<lg(psi);i++) gf->psi[i] = (dgf*gf->psi[i] + deg*psi[i]) % pr;
    1745             :     }
    1746         594 :     deg = pr; avma = btop;
    1747             :   }
    1748        4122 :   for (i = 1; i <= gf->fp; i++)
    1749       16458 :     for (j = 1; j <= gt.g; j++)
    1750       12918 :       if (mael(gt.C,i,j)) gunclone(gmael(gt.C,i,j));
    1751         582 :   if (DEBUGLEVEL>=4 && res) err_printf("Best lift: %d\n",deg);
    1752         582 :   if (deg==1) { avma = ltop; return NULL; }
    1753             :   else
    1754             :   {
    1755             :     /* Normalize result so that psi[g]=1 */
    1756         528 :     long im = Fl_inv(gf->psi[g], deg);
    1757         528 :     GEN cp = perm_pow(res, im);
    1758         528 :     for(i=1;i<lg(res);i++) res[i] = cp[i];
    1759         528 :     for(i=1;i<lg(gf->psi);i++) gf->psi[i] = Fl_mul(im, gf->psi[i], deg);
    1760         528 :     avma = av2; gf->deg = deg; return res;
    1761             :   }
    1762             : }
    1763             : 
    1764             : /* return NULL if not Galois */
    1765             : static GEN
    1766        1590 : galoisfindfrobenius(GEN T, GEN L, GEN den, struct galois_frobenius *gf,
    1767             :     struct galois_borne *gb, const struct galois_analysis *ga)
    1768             : {
    1769        1590 :   pari_sp ltop = avma, av;
    1770        1590 :   long Try = 0, n = degpol(T), deg, gmask = (ga->group&ga_ext_2)? 3: 1;
    1771        1590 :   GEN frob, Lden = makeLden(L,den,gb);
    1772             :   forprime_t S;
    1773             : 
    1774        1590 :   u_forprime_init(&S, ga->p, ULONG_MAX);
    1775        1590 :   av = avma;
    1776        1590 :   deg = gf->deg = ga->deg;
    1777        3234 :   while ((gf->p = u_forprime_next(&S)))
    1778             :   {
    1779             :     pari_sp lbot;
    1780             :     GEN Ti, Tp;
    1781             :     long nb, d;
    1782        1644 :     avma = av;
    1783        1644 :     Tp = ZX_to_Flx(T, gf->p);
    1784        1644 :     if (!Flx_is_squarefree(Tp, gf->p)) continue;
    1785        1644 :     Ti = gel(Flx_factor(Tp, gf->p), 1);
    1786        1644 :     nb = lg(Ti)-1; d = degpol(gel(Ti,1));
    1787        1644 :     if (d < ga->mindeg) continue;
    1788        1644 :     if (nb > 1 && degpol(gel(Ti,nb)) != d) { avma = ltop; return NULL; }
    1789        1644 :     if (((gmask&1)==0 || d % deg) && ((gmask&2)==0 || odd(d))) continue;
    1790        1644 :     if (DEBUGLEVEL >= 1) err_printf("GaloisConj: Trying p=%ld\n", gf->p);
    1791        1644 :     FlxV_to_ZXV_inplace(Ti);
    1792        1644 :     gf->fp = d;
    1793        1644 :     gf->Tmod = Ti; lbot = avma;
    1794        1644 :     frob = galoisfrobeniuslift(T, den, L, Lden, gf, gb, ga->mindeg);
    1795        1644 :     if (frob)
    1796             :     {
    1797             :       GEN *gptr[3];
    1798        1590 :       if (gf->deg < ga->mindeg)
    1799             :       {
    1800           0 :         if (DEBUGLEVEL >= 4)
    1801           0 :           err_printf("GaloisConj: lift degree too small %ld < %ld\n",
    1802             :                      gf->deg, ga->mindeg);
    1803           0 :         continue;
    1804             :       }
    1805        1590 :       gf->Tmod = gcopy(Ti);
    1806        1590 :       gptr[0]=&gf->Tmod; gptr[1]=&gf->psi; gptr[2]=&frob;
    1807        1590 :       gerepilemanysp(ltop,lbot,gptr,3); return frob;
    1808             :     }
    1809          54 :     if ((ga->group&ga_all_normal) && d % deg == 0) gmask &= ~1;
    1810             :     /* The first prime degree is always divisible by deg, so we don't
    1811             :      * have to worry about ext_2 being used before regular supersolvable*/
    1812          54 :     if (!gmask) { avma = ltop; return NULL; }
    1813          54 :     if ((ga->group&ga_non_wss) && ++Try > ((3*n)>>1))
    1814             :     {
    1815           0 :       pari_warn(warner,"Galois group probably not weakly super solvable");
    1816           0 :       return NULL;
    1817             :     }
    1818             :   }
    1819           0 :   if (!gf->p) pari_err_OVERFLOW("galoisfindfrobenius [ran out of primes]");
    1820           0 :   return NULL;
    1821             : }
    1822             : 
    1823             : /* compute g such that tau(Pmod[#])= tau(Pmod[g]) */
    1824             : 
    1825             : static long
    1826        1272 : get_image(GEN tau, GEN P, GEN Pmod, GEN p)
    1827             : {
    1828        1272 :   pari_sp av = avma;
    1829        1272 :   long g, gp = lg(Pmod)-1;
    1830        1272 :   tau = RgX_to_FpX(tau, p);
    1831        1272 :   tau = FpX_FpXQ_eval(gel(Pmod, gp), tau, P, p);
    1832        1272 :   tau = FpX_normalize(FpX_gcd(P, tau, p), p);
    1833        2628 :   for (g = 1; g <= gp; g++)
    1834        2628 :     if (ZX_equal(tau, gel(Pmod,g))) { avma = av; return g; }
    1835           0 :   avma = av; return 0;
    1836             : }
    1837             : 
    1838             : static GEN
    1839             : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1840             :           const struct galois_analysis *ga);
    1841             : static GEN
    1842        1008 : galoisgenfixedfield(GEN Tp, GEN Pmod, GEN V, GEN ip, struct galois_borne *gb)
    1843             : {
    1844             :   GEN  P, PL, Pden, PM, Pp;
    1845             :   GEN  tau, PG, Pg;
    1846             :   long g, lP;
    1847        1008 :   long x=varn(Tp);
    1848        1008 :   P=gel(V,3);
    1849        1008 :   PL=gel(V,2);
    1850        1008 :   Pp = FpX_red(P,ip);
    1851        1008 :   if (DEBUGLEVEL>=6)
    1852           0 :     err_printf("GaloisConj: Fixed field %Ps\n",P);
    1853        1008 :   if (degpol(P)==2)
    1854             :   {
    1855         690 :     PG=cgetg(3,t_VEC);
    1856         690 :     gel(PG,1) = mkvec( mkvecsmall2(2,1) );
    1857         690 :     gel(PG,2) = mkvecsmall(2);
    1858         690 :     tau = deg1pol_shallow(gen_m1, negi(gel(P,3)), x);
    1859         690 :     g = get_image(tau, Pp, Pmod, ip);
    1860         690 :     if (!g) return NULL;
    1861         690 :     Pg = mkvecsmall(g);
    1862             :   }
    1863             :   else
    1864             :   {
    1865             :     struct galois_analysis Pga;
    1866             :     struct galois_borne Pgb;
    1867             :     GEN mod, mod2;
    1868             :     long j;
    1869         324 :     if (!galoisanalysis(P, &Pga, 0)) return NULL;
    1870         312 :     Pgb.l = gb->l;
    1871         312 :     Pden = galoisborne(P, NULL, &Pgb, degpol(P));
    1872             : 
    1873         312 :     if (Pgb.valabs > gb->valabs)
    1874             :     {
    1875          24 :       if (DEBUGLEVEL>=4)
    1876           0 :         err_printf("GaloisConj: increase prec of p-adic roots of %ld.\n"
    1877           0 :             ,Pgb.valabs-gb->valabs);
    1878          24 :       PL = ZpX_liftroots(P,PL,gb->l,Pgb.valabs);
    1879             :     }
    1880         288 :     else if (Pgb.valabs < gb->valabs)
    1881         270 :       PL = FpC_red(PL, Pgb.ladicabs);
    1882         312 :     PM = FpV_invVandermonde(PL, Pden, Pgb.ladicabs);
    1883         312 :     PG = galoisgen(P, PL, PM, Pden, &Pgb, &Pga);
    1884         312 :     if (PG == gen_0) return NULL;
    1885         312 :     lP = lg(gel(PG,1));
    1886         312 :     mod = Pgb.ladicabs; mod2 = shifti(mod, -1);
    1887         312 :     Pg = cgetg(lP, t_VECSMALL);
    1888         894 :     for (j = 1; j < lP; j++)
    1889             :     {
    1890         582 :       pari_sp btop=avma;
    1891         582 :       tau = permtopol(gmael(PG,1,j), PL, PM, Pden, mod, mod2, x);
    1892         582 :       g = get_image(tau, Pp, Pmod, ip);
    1893         582 :       if (!g) return NULL;
    1894         582 :       Pg[j] = g;
    1895         582 :       avma = btop;
    1896             :     }
    1897             :   }
    1898        1002 :   return mkvec2(PG,Pg);
    1899             : }
    1900             : 
    1901             : /* Let sigma^m=1,  tau*sigma*tau^-1=sigma^s.
    1902             :  * Compute n so that (sigma*tau)^e = sigma^n*tau^e
    1903             :  * We have n = sum_{k=0}^{e-1} s^k mod m.
    1904             :  * so n*(1-s) = 1-s^e mod m
    1905             :  * Unfortunately (1-s) might not invertible mod m.
    1906             :  */
    1907             : 
    1908             : static long
    1909        2952 : stpow(long s, long e, long m)
    1910             : {
    1911             :   long i;
    1912        2952 :   long n = 1;
    1913        4740 :   for (i = 1; i < e; i++)
    1914        1788 :     n = (1 + n * s) % m;
    1915        2952 :   return n;
    1916             : }
    1917             : 
    1918             : static GEN
    1919        1272 : wpow(long s, long m, long e, long n)
    1920             : {
    1921        1272 :   GEN   w = cgetg(n+1,t_VECSMALL);
    1922        1272 :   long si = s;
    1923             :   long i;
    1924        1272 :   w[1] = 1;
    1925        1272 :   for(i=2; i<=n; i++) w[i] = w[i-1]*e;
    1926        2748 :   for(i=n; i>=1; i--)
    1927             :   {
    1928        1476 :     si = Fl_powu(si,e,m);
    1929        1476 :     w[i] = Fl_mul(s-1, stpow(si, w[i], m), m);
    1930             :   }
    1931        1272 :   return w;
    1932             : }
    1933             : 
    1934             : static GEN
    1935        1272 : galoisgenliftauto(GEN O, GEN gj, long s, long n, struct galois_test *td)
    1936             : {
    1937        1272 :   pari_sp av = avma;
    1938             :   long sr, k;
    1939        1272 :   long deg = lg(gel(O,1))-1;
    1940        1272 :   GEN  X  = cgetg(lg(O), t_VECSMALL);
    1941        1272 :   GEN  oX = cgetg(lg(O), t_VECSMALL);
    1942        1272 :   GEN  B  = perm_cycles(gj);
    1943        1272 :   long oj = lg(gel(B,1)) - 1;
    1944        1272 :   GEN  F  = factoru(oj);
    1945        1272 :   GEN  Fp = gel(F,1);
    1946        1272 :   GEN  Fe = gel(F,2);
    1947        1272 :   GEN  pf = identity_perm(n);
    1948        1272 :   if (DEBUGLEVEL >= 6)
    1949           0 :     err_printf("GaloisConj: %Ps of relative order %d\n", gj, oj);
    1950        2544 :   for (k=lg(Fp)-1; k>=1; k--)
    1951             :   {
    1952        1272 :     long f, dg = 1, el = oj, osel = 1, a = 0;
    1953        1272 :     long p  = Fp[k], e  = Fe[k], op = oj / upowuu(p,e);
    1954             :     long i;
    1955        1272 :     GEN  pf1 = NULL, w, wg, Be = cgetg(e+1,t_VEC);
    1956        1272 :     gel(Be,e) = cyc_pow(B, op);
    1957        1272 :     for(i=e-1; i>=1; i--) gel(Be,i) = cyc_pow(gel(Be,i+1), p);
    1958        1272 :     w = wpow(Fl_powu(s,op,deg),deg,p,e);
    1959        1272 :     wg = cgetg(e+2,t_VECSMALL);
    1960        1272 :     wg[e+1] = deg;
    1961        1272 :     for (i=e; i>=1; i--) wg[i] = ugcd(wg[i+1], w[i]);
    1962        1272 :     for (i=1; i<lg(O); i++) oX[i] = 0;
    1963        2748 :     for (f=1; f<=e; f++)
    1964             :     {
    1965             :       long sel, t;
    1966        1476 :       GEN Bel = gel(Be,f);
    1967        1476 :       dg *= p; el /= p;
    1968        1476 :       sel = Fl_powu(s,el,deg);
    1969        1476 :       if (DEBUGLEVEL >= 6) err_printf("GaloisConj: B=%Ps\n", Bel);
    1970        1476 :       sr  = cgcd(stpow(sel,p,deg),deg);
    1971        1476 :       if (DEBUGLEVEL >= 6)
    1972           0 :         err_printf("GaloisConj: exp %d: s=%ld [%ld] a=%ld w=%ld wg=%ld sr=%ld\n",
    1973           0 :             dg, sel, deg, a, w[f], wg[f+1], sr);
    1974        1776 :       for (t = 0; t < sr; t++)
    1975        1776 :         if ((a+t*w[f])%wg[f+1]==0)
    1976             :         {
    1977             :           long i, j, k, st;
    1978        1674 :           for (i = 1; i < lg(X); i++) X[i] = 0;
    1979        6906 :           for (i = 0; i < lg(X)-1; i+=dg)
    1980       11586 :             for (j = 1, k = p, st = t; k <= dg; j++, k += p)
    1981             :             {
    1982        6354 :               X[k+i] = (oX[j+i] + st)%deg;
    1983        6354 :               st = (t + st*osel)%deg;
    1984             :             }
    1985        1674 :           pf1 = testpermutation(O, Bel, X, sel, p, sr, td);
    1986        1674 :           if (pf1) break;
    1987             :         }
    1988        1476 :       if (!pf1) return NULL;
    1989        1476 :       for (i=1; i<lg(O); i++) oX[i] = X[i];
    1990        1476 :       osel = sel; a = (a+t*w[f])%deg;
    1991             :     }
    1992        1272 :     pf = perm_mul(pf, perm_pow(pf1, el));
    1993             :   }
    1994        1272 :   return gerepileuptoleaf(av, pf);
    1995             : }
    1996             : 
    1997             : static GEN
    1998        1638 : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1999             :           const struct galois_analysis *ga)
    2000             : {
    2001             :   struct galois_test td;
    2002             :   struct galois_frobenius gf;
    2003        1638 :   pari_sp ltop = avma;
    2004        1638 :   long p, deg, x, j, n = degpol(T), lP;
    2005             :   GEN sigma, Tmod, res, res1, ip, frob, O, PG, PG1, PG2, Pg;
    2006             : 
    2007        1638 :   if (!ga->deg) return gen_0;
    2008        1638 :   x = varn(T);
    2009        1638 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: denominator:%Ps\n", den);
    2010        1638 :   if (n == 12 && ga->ord==3 && !ga->p4)
    2011             :   { /* A4 is very probable: test it first */
    2012          24 :     pari_sp av = avma;
    2013          24 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing A4 first\n");
    2014          24 :     inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2015          24 :     PG = a4galoisgen(&td);
    2016          24 :     freetest(&td);
    2017          24 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2018           0 :     avma = av;
    2019             :   }
    2020        1614 :   if (n == 24 && ga->ord==3)
    2021             :   { /* S4 is very probable: test it first */
    2022          24 :     pari_sp av = avma;
    2023             :     struct galois_lift gl;
    2024          24 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing S4 first\n");
    2025          24 :     initlift(T, den, stoi(ga->p4), L, makeLden(L,den,gb), gb, &gl);
    2026          24 :     PG = s4galoisgen(&gl);
    2027          24 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2028           0 :     avma = av;
    2029             :   }
    2030        1590 :   frob = galoisfindfrobenius(T, L, den, &gf, gb, ga);
    2031        1590 :   if (!frob) { avma=ltop; return gen_0; }
    2032        1590 :   p = gf.p; ip = utoipos(p);
    2033        1590 :   Tmod = gf.Tmod;
    2034        1590 :   O = perm_cycles(frob);
    2035        1590 :   deg = lg(gel(O,1))-1;
    2036        1590 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Orbit:%Ps\n", O);
    2037        1590 :   if (deg == n)        /* cyclic */
    2038         582 :     return gerepilecopy(ltop, mkvec2(mkvec(frob), mkvecsmall(deg)));
    2039        1008 :   sigma = permtopol(frob, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), x);
    2040        1008 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Frobenius:%Ps\n", sigma);
    2041             :   {
    2042        1008 :     pari_sp btop=avma;
    2043             :     GEN V, Tp, Sp, Pmod;
    2044        1008 :     GEN OL = fixedfieldorbits(O,L);
    2045        1008 :     V  = fixedfieldsympol(OL, gb->ladicabs, gb->l, ip, x);
    2046        1008 :     Tp = FpX_red(T,ip);
    2047        1008 :     Sp = sympol_aut_evalmod(gel(V,1),deg,sigma,Tp,ip);
    2048        1008 :     Pmod = fixedfieldfactmod(Sp,ip,Tmod);
    2049        1008 :     PG = galoisgenfixedfield(Tp, Pmod, V, ip, gb);
    2050        1008 :     if (PG == NULL) { avma = ltop; return gen_0; }
    2051        1002 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Back to Earth:%Ps\n", PG);
    2052        1002 :     PG=gerepilecopy(btop, PG);
    2053             :   }
    2054        1002 :   inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2055        1002 :   PG1 = gmael(PG, 1, 1); lP = lg(PG1);
    2056        1002 :   PG2 = gmael(PG, 1, 2);
    2057        1002 :   Pg = gel(PG, 2);
    2058        1002 :   res = cgetg(3, t_VEC);
    2059        1002 :   gel(res,1) = res1 = cgetg(lP + 1, t_VEC);
    2060        1002 :   gel(res,2) = vecsmall_prepend(PG2, deg);
    2061        1002 :   gel(res1, 1) = vecsmall_copy(frob);
    2062        2274 :   for (j = 1; j < lP; j++)
    2063             :   {
    2064        1272 :     GEN pf = galoisgenliftauto(O, gel(PG1, j), gf.psi[Pg[j]], n, &td);
    2065        1272 :     if (!pf) { freetest(&td); avma = ltop; return gen_0; }
    2066        1272 :     gel(res1, j+1) = pf;
    2067             :   }
    2068        1002 :   if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Fini!\n");
    2069        1002 :   freetest(&td);
    2070        1002 :   return gerepileupto(ltop, res);
    2071             : }
    2072             : 
    2073             : /* T = polcyclo(N) */
    2074             : static GEN
    2075          54 : conjcyclo(GEN T, long N)
    2076             : {
    2077          54 :   pari_sp av = avma;
    2078          54 :   long i, k = 1, d = eulerphiu(N), v = varn(T);
    2079          54 :   GEN L = cgetg(d+1,t_COL);
    2080         480 :   for (i=1; i<=N; i++)
    2081         426 :     if (ugcd(i, N)==1)
    2082             :     {
    2083         228 :       GEN s = pol_xn(i, v);
    2084         228 :       if (i >= d) s = ZX_rem(s, T);
    2085         228 :       gel(L,k++) = s;
    2086             :     }
    2087          54 :   return gerepileupto(av, gen_sort(L, (void*)&gcmp, &gen_cmp_RgX));
    2088             : }
    2089             : 
    2090             : /* T: polynomial or nf, den multiple of common denominator of solutions or
    2091             :  * NULL (unknown). If T is nf, and den unknown, use den = denom(nf.zk) */
    2092             : static GEN
    2093        2748 : galoisconj4_main(GEN T, GEN den, long flag)
    2094             : {
    2095        2748 :   pari_sp ltop = avma;
    2096             :   GEN nf, G, L, M, aut;
    2097             :   struct galois_analysis ga;
    2098             :   struct galois_borne gb;
    2099             :   long n;
    2100             :   pari_timer ti;
    2101             : 
    2102        2748 :   T = get_nfpol(T, &nf);
    2103        2748 :   n = poliscyclo(T);
    2104        2748 :   if (n) return flag? galoiscyclo(n, varn(T)): conjcyclo(T, n);
    2105        2550 :   n = degpol(T);
    2106        2550 :   if (nf)
    2107        1794 :   { if (!den) den = nf_get_zkden(nf); }
    2108             :   else
    2109             :   {
    2110         756 :     if (n <= 0) pari_err_IRREDPOL("galoisinit",T);
    2111         756 :     RgX_check_ZX(T, "galoisinit");
    2112         756 :     if (!ZX_is_squarefree(T))
    2113           6 :       pari_err_DOMAIN("galoisinit","issquarefree(pol)","=",gen_0,T);
    2114         750 :     if (!gequal1(gel(T,n+2))) pari_err_IMPL("galoisinit(non-monic)");
    2115             :   }
    2116        2538 :   if (n == 1)
    2117             :   {
    2118          12 :     if (!flag) { G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G;}
    2119          12 :     ga.l = 3;
    2120          12 :     ga.deg = 1;
    2121          12 :     den = gen_1;
    2122             :   }
    2123        2526 :   else if (!galoisanalysis(T, &ga, 1)) { avma = ltop; return utoipos(ga.p); }
    2124             : 
    2125        1338 :   if (den)
    2126             :   {
    2127         942 :     if (typ(den) != t_INT) pari_err_TYPE("galoisconj4 [2nd arg integer]", den);
    2128         942 :     den = absi(den);
    2129             :   }
    2130        1338 :   gb.l = utoipos(ga.l);
    2131        1338 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2132        1338 :   den = galoisborne(T, den, &gb, degpol(T));
    2133        1338 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisborne()");
    2134        1338 :   L = ZpX_roots(T, gb.l, gb.valabs);
    2135        1338 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "ZpX_roots");
    2136        1338 :   M = FpV_invVandermonde(L, den, gb.ladicabs);
    2137        1338 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "FpV_invVandermonde()");
    2138        1338 :   if (n == 1)
    2139             :   {
    2140          12 :     G = cgetg(3, t_VEC);
    2141          12 :     gel(G,1) = cgetg(1, t_VEC);
    2142          12 :     gel(G,2) = cgetg(1, t_VECSMALL);
    2143             :   }
    2144             :   else
    2145        1326 :     G = galoisgen(T, L, M, den, &gb, &ga);
    2146        1338 :   if (DEBUGLEVEL >= 6) err_printf("GaloisConj: %Ps\n", G);
    2147        1338 :   if (G == gen_0) { avma = ltop; return gen_0; }
    2148        1332 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2149        1332 :   if (flag)
    2150             :   {
    2151         954 :     GEN grp = cgetg(9, t_VEC);
    2152         954 :     gel(grp,1) = ZX_copy(T);
    2153         954 :     gel(grp,2) = mkvec3(utoipos(ga.l), utoipos(gb.valabs), icopy(gb.ladicabs));
    2154         954 :     gel(grp,3) = ZC_copy(L);
    2155         954 :     gel(grp,4) = ZM_copy(M);
    2156         954 :     gel(grp,5) = icopy(den);
    2157         954 :     gel(grp,6) = group_elts(G,n);
    2158         954 :     gel(grp,7) = gcopy(gel(G,1));
    2159         954 :     gel(grp,8) = gcopy(gel(G,2)); return gerepileupto(ltop, grp);
    2160             :   }
    2161         378 :   aut = galoisgrouptopol(group_elts(G, n),L,M,den,gb.ladicsol, varn(T));
    2162         378 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "Computation of polynomials");
    2163         378 :   return gerepileupto(ltop, gen_sort(aut, (void*)&gcmp, &gen_cmp_RgX));
    2164             : }
    2165             : 
    2166             : /* Heuristic computation of #Aut(T), pinit = first prime to be tested */
    2167             : long
    2168         546 : numberofconjugates(GEN T, long pinit)
    2169             : {
    2170         546 :   pari_sp av = avma;
    2171         546 :   long c, nbtest, nbmax, n = degpol(T);
    2172             :   ulong p;
    2173             :   forprime_t S;
    2174             : 
    2175         546 :   if (n == 1) return 1;
    2176         546 :   nbmax = (n < 10)? 20: (n<<1) + 1;
    2177         546 :   nbtest = 0;
    2178             : #if 0
    2179             :   c = ZX_sturm(T); c = ugcd(c, n-c); /* too costly: finite primes are cheaper */
    2180             : #else
    2181         546 :   c = n;
    2182             : #endif
    2183         546 :   u_forprime_init(&S, pinit, ULONG_MAX);
    2184         546 :   while((p = u_forprime_next(&S)))
    2185             :   {
    2186        4554 :     GEN L, Tp = ZX_to_Flx(T,p);
    2187             :     long i, nb;
    2188        4554 :     if (!Flx_is_squarefree(Tp, p)) continue;
    2189             :     /* unramified */
    2190        3678 :     nbtest++;
    2191        3678 :     L = Flx_nbfact_by_degree(Tp, &nb, p); /* L[i] = #factors of degree i */
    2192        3678 :     if (L[n/nb] == nb) {
    2193        3024 :       if (c == n && nbtest > 10) break; /* probably Galois */
    2194             :     }
    2195             :     else
    2196             :     {
    2197        1200 :       c = ugcd(c, L[1]);
    2198        7842 :       for (i = 2; i <= n; i++)
    2199        7044 :         if (L[i]) { c = ugcd(c, L[i]*i); if (c == 1) break; }
    2200        1200 :       if (c == 1) break;
    2201             :     }
    2202        3264 :     if (nbtest == nbmax) break;
    2203        3132 :     if (DEBUGLEVEL >= 6)
    2204           0 :       err_printf("NumberOfConjugates [%ld]:c=%ld,p=%ld\n", nbtest,c,p);
    2205        3132 :     avma = av;
    2206             :   }
    2207         546 :   if (DEBUGLEVEL >= 2) err_printf("NumberOfConjugates:c=%ld,p=%ld\n", c, p);
    2208         546 :   avma = av; return c;
    2209             : }
    2210             : static GEN
    2211           0 : galoisconj4(GEN nf, GEN d)
    2212             : {
    2213           0 :   pari_sp av = avma;
    2214             :   GEN G, T;
    2215           0 :   G = galoisconj4_main(nf, d, 0);
    2216           0 :   if (typ(G) != t_INT) return G; /* Success */
    2217           0 :   avma = av; T = get_nfpol(nf, &nf);
    2218           0 :   G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G; /* Fail */
    2219             : 
    2220             : }
    2221             : 
    2222             : /* d multiplicative bound for the automorphism's denominators */
    2223             : GEN
    2224        1770 : galoisconj(GEN nf, GEN d)
    2225             : {
    2226        1770 :   pari_sp av = avma;
    2227        1770 :   GEN G, NF, T = get_nfpol(nf,&NF);
    2228        1770 :   if (degpol(T) == 2)
    2229             :   { /* fast shortcut */
    2230         798 :     GEN a = gel(T,4), b = gel(T,3);
    2231         798 :     long v = varn(T);
    2232         798 :     RgX_check_ZX(T, "galoisconj");
    2233         798 :     if (!gequal1(a)) pari_err_IMPL("galoisconj(non-monic)");
    2234         798 :     b = negi(b);
    2235         798 :     G = cgetg(3, t_COL);
    2236         798 :     gel(G,1) = pol_x(v);
    2237         798 :     gel(G,2) = deg1pol(gen_m1, b, v); return G;
    2238             :   }
    2239         972 :   G = galoisconj4_main(nf, d, 0);
    2240         972 :   if (typ(G) != t_INT) return G; /* Success */
    2241         540 :   avma = av; return galoisconj1(nf);
    2242             : }
    2243             : 
    2244             : /* FIXME: obsolete, use galoisconj(nf, d) directly */
    2245             : GEN
    2246          36 : galoisconj0(GEN nf, long flag, GEN d, long prec)
    2247             : {
    2248             :   (void)prec;
    2249          36 :   switch(flag) {
    2250             :     case 2:
    2251          30 :     case 0: return galoisconj(nf, d);
    2252           6 :     case 1: return galoisconj1(nf);
    2253           0 :     case 4: return galoisconj4(nf, d);
    2254             :   }
    2255           0 :   pari_err_FLAG("nfgaloisconj");
    2256             :   return NULL; /*LCOV_EXCL_LINE*/
    2257             : }
    2258             : 
    2259             : /******************************************************************************/
    2260             : /* Galois theory related algorithms                                           */
    2261             : /******************************************************************************/
    2262             : GEN
    2263        2886 : checkgal(GEN gal)
    2264             : {
    2265        2886 :   if (typ(gal) == t_POL) pari_err_TYPE("checkgal [apply galoisinit first]",gal);
    2266        2886 :   if (typ(gal) != t_VEC || lg(gal) != 9) pari_err_TYPE("checkgal",gal);
    2267        2880 :   return gal;
    2268             : }
    2269             : 
    2270             : GEN
    2271        1776 : galoisinit(GEN nf, GEN den)
    2272             : {
    2273        1776 :   GEN G = galoisconj4_main(nf, den, 1);
    2274        1764 :   return (typ(G) == t_INT)? gen_0: G;
    2275             : }
    2276             : 
    2277             : static GEN
    2278        1362 : galoispermtopol_i(GEN gal, GEN perm, GEN mod, GEN mod2)
    2279             : {
    2280        1362 :   switch (typ(perm))
    2281             :   {
    2282             :     case t_VECSMALL:
    2283        1158 :       return permtopol(perm, gal_get_roots(gal), gal_get_invvdm(gal),
    2284             :                              gal_get_den(gal), mod, mod2,
    2285        1158 :                              varn(gal_get_pol(gal)));
    2286             :     case t_VEC: case t_COL: case t_MAT:
    2287             :     {
    2288             :       long i, lv;
    2289         204 :       GEN v = cgetg_copy(perm, &lv);
    2290         204 :       if (DEBUGLEVEL>=4) err_printf("GaloisPermToPol:");
    2291         618 :       for (i = 1; i < lv; i++)
    2292             :       {
    2293         414 :         gel(v,i) = galoispermtopol_i(gal, gel(perm,i), mod, mod2);
    2294         414 :         if (DEBUGLEVEL>=4) err_printf("%ld ",i);
    2295             :       }
    2296         204 :       if (DEBUGLEVEL>=4) err_printf("\n");
    2297         204 :       return v;
    2298             :     }
    2299             :   }
    2300           0 :   pari_err_TYPE("galoispermtopol", perm);
    2301             :   return NULL; /* LCOV_EXCL_LINE */
    2302             : }
    2303             : 
    2304             : GEN
    2305         948 : galoispermtopol(GEN gal, GEN perm)
    2306             : {
    2307         948 :   pari_sp av = avma;
    2308             :   GEN mod, mod2;
    2309         948 :   gal = checkgal(gal);
    2310         948 :   mod = gal_get_mod(gal);
    2311         948 :   mod2 = shifti(mod,-1);
    2312         948 :   return gerepilecopy(av, galoispermtopol_i(gal, perm, mod, mod2));
    2313             : }
    2314             : 
    2315             : GEN
    2316          54 : galoiscosets(GEN O, GEN perm)
    2317             : {
    2318          54 :   long i, j, k, u, f, l = lg(O);
    2319          54 :   GEN RC, C = cgetg(l,t_VECSMALL), o = gel(O,1);
    2320          54 :   pari_sp av = avma;
    2321          54 :   f = lg(o); u = o[1]; RC = zero_zv(lg(perm)-1);
    2322         240 :   for(i=1,j=1; j<l; i++)
    2323             :   {
    2324         186 :     GEN p = gel(perm,i);
    2325         186 :     if (RC[ p[u] ]) continue;
    2326         114 :     for(k=1; k<f; k++) RC[ p[ o[k] ] ] = 1;
    2327         114 :     C[j++] = i;
    2328             :   }
    2329          54 :   avma = av; return C;
    2330             : }
    2331             : 
    2332             : static GEN
    2333          54 : fixedfieldfactor(GEN L, GEN O, GEN perm, GEN M, GEN den, GEN mod, GEN mod2,
    2334             :                  long x,long y)
    2335             : {
    2336          54 :   pari_sp ltop = avma;
    2337          54 :   long i, j, k, l = lg(O), lo = lg(gel(O,1));
    2338          54 :   GEN V, res, cosets = galoiscosets(O,perm), F = cgetg(lo+1,t_COL);
    2339             : 
    2340          54 :   gel(F, lo) = gen_1;
    2341          54 :   if (DEBUGLEVEL>=4) err_printf("GaloisFixedField:cosets=%Ps \n",cosets);
    2342          54 :   if (DEBUGLEVEL>=6) err_printf("GaloisFixedField:den=%Ps mod=%Ps \n",den,mod);
    2343          54 :   V = cgetg(l,t_COL); res = cgetg(l,t_VEC);
    2344         168 :   for (i = 1; i < l; i++)
    2345             :   {
    2346         114 :     pari_sp av = avma;
    2347         114 :     GEN G = cgetg(l,t_VEC), Lp = vecpermute(L, gel(perm, cosets[i]));
    2348         384 :     for (k = 1; k < l; k++)
    2349         270 :       gel(G,k) = FpV_roots_to_pol(vecpermute(Lp, gel(O,k)), mod, x);
    2350         414 :     for (j = 1; j < lo; j++)
    2351             :     {
    2352         300 :       for(k = 1; k < l; k++) gel(V,k) = gmael(G,k,j+1);
    2353         300 :       gel(F,j) = vectopol(V, M, den, mod, mod2, y);
    2354             :     }
    2355         114 :     gel(res,i) = gerepileupto(av,gtopolyrev(F,x));
    2356             :   }
    2357          54 :   return gerepileupto(ltop,res);
    2358             : }
    2359             : 
    2360             : static void
    2361        1278 : chk_perm(GEN perm, long n)
    2362             : {
    2363        1278 :   if (typ(perm) != t_VECSMALL || lg(perm)!=n+1)
    2364           0 :     pari_err_TYPE("galoisfixedfield", perm);
    2365        1278 : }
    2366             : 
    2367             : static int
    2368        2292 : is_group(GEN g)
    2369             : {
    2370        6870 :   return typ(g)==t_VEC && lg(g)==3 && typ(gel(g,1))==t_VEC
    2371        3066 :       && typ(gel(g,2))==t_VECSMALL;
    2372             : }
    2373             : 
    2374             : GEN
    2375         876 : galoisfixedfield(GEN gal, GEN perm, long flag, long y)
    2376             : {
    2377         876 :   pari_sp ltop = avma;
    2378             :   GEN T, L, P, S, PL, O, res, mod, mod2;
    2379             :   long vT, n, i;
    2380         876 :   if (flag<0 || flag>2) pari_err_FLAG("galoisfixedfield");
    2381         876 :   gal = checkgal(gal); T = gal_get_pol(gal);
    2382         876 :   vT = varn(T);
    2383         876 :   L = gal_get_roots(gal); n = lg(L)-1;
    2384         876 :   mod = gal_get_mod(gal);
    2385         876 :   if (typ(perm) == t_VEC)
    2386             :   {
    2387         660 :     if (is_group(perm)) perm = gel(perm, 1);
    2388         660 :     for (i = 1; i < lg(perm); i++) chk_perm(gel(perm,i), n);
    2389         660 :     O = vecperm_orbits(perm, n);
    2390             :   }
    2391             :   else
    2392             :   {
    2393         216 :     chk_perm(perm, n);
    2394         216 :     O = perm_cycles(perm);
    2395             :   }
    2396             : 
    2397             :   {
    2398         876 :     GEN OL= fixedfieldorbits(O,L);
    2399         876 :     GEN V = fixedfieldsympol(OL, mod, gal_get_p(gal), NULL, vT);
    2400         876 :     PL= gel(V,2);
    2401         876 :     P = gel(V,3);
    2402             :   }
    2403         876 :   if (flag==1) return gerepileupto(ltop,P);
    2404         672 :   mod2 = shifti(mod,-1);
    2405         672 :   S = fixedfieldinclusion(O, PL);
    2406         672 :   S = vectopol(S, gal_get_invvdm(gal), gal_get_den(gal), mod, mod2, vT);
    2407         672 :   if (flag==0)
    2408         618 :     res = cgetg(3, t_VEC);
    2409             :   else
    2410             :   {
    2411             :     GEN PM, Pden;
    2412             :     struct galois_borne Pgb;
    2413          54 :     long val = itos(gal_get_e(gal));
    2414          54 :     Pgb.l = gal_get_p(gal);
    2415          54 :     Pden = galoisborne(P, NULL, &Pgb, degpol(T)/degpol(P));
    2416          54 :     if (Pgb.valabs > val)
    2417             :     {
    2418          12 :       if (DEBUGLEVEL>=4)
    2419           0 :         err_printf("GaloisConj: increase p-adic prec by %ld.\n", Pgb.valabs-val);
    2420          12 :       PL = ZpX_liftroots(P, PL, Pgb.l, Pgb.valabs);
    2421          12 :       L  = ZpX_liftroots(T, L, Pgb.l, Pgb.valabs);
    2422          12 :       mod = Pgb.ladicabs; mod2 = shifti(mod,-1);
    2423             :     }
    2424          54 :     PM = FpV_invVandermonde(PL, Pden, mod);
    2425          54 :     if (y < 0) y = 1;
    2426          54 :     if (varncmp(y, vT) <= 0)
    2427           0 :       pari_err_PRIORITY("galoisfixedfield", T, "<=", y);
    2428          54 :     res = cgetg(4, t_VEC);
    2429          54 :     gel(res,3) = fixedfieldfactor(L,O,gal_get_group(gal), PM,Pden,mod,mod2,vT,y);
    2430             :   }
    2431         672 :   gel(res,1) = gcopy(P);
    2432         672 :   gel(res,2) = gmodulo(S, T);
    2433         672 :   return gerepileupto(ltop, res);
    2434             : }
    2435             : 
    2436             : /* gal a galois group output the underlying wss group */
    2437             : GEN
    2438        1194 : galois_group(GEN gal) { return mkvec2(gal_get_gen(gal), gal_get_orders(gal)); }
    2439             : 
    2440             : GEN
    2441        1632 : checkgroup(GEN g, GEN *S)
    2442             : {
    2443        1632 :   if (is_group(g)) { *S = NULL; return g; }
    2444         876 :   g  = checkgal(g);
    2445         870 :   *S = gal_get_group(g); return galois_group(g);
    2446             : }
    2447             : 
    2448             : GEN
    2449        1458 : checkgroupelts(GEN gal)
    2450             : {
    2451        1458 :   if (typ(gal)!=t_VEC) pari_err_TYPE("checkgroup", gal);
    2452        2364 :   if ((lg(gal)==9 && typ(gel(gal,1))==t_POL) ||
    2453        1434 :       (lg(gal)==3 && typ(gel(gal,1))==t_VEC && typ(gel(gal,2))==t_VECSMALL))
    2454             :   {
    2455         978 :     GEN elts, G = checkgroup(gal, &elts);
    2456         978 :     if (lg(gel(G,1))==1) return mkvec(cgetg(1,t_VECSMALL));
    2457         966 :     if(!elts) elts = group_elts(G, group_domain(G));
    2458         966 :     return elts;
    2459             :   }
    2460             :   else
    2461             :   {
    2462         468 :     long i, n = lg(gal)-1;
    2463        6732 :     for(i=1; i<=n; i++)
    2464             :     {
    2465        6294 :       if(typ(gel(gal,i)) != t_VECSMALL)
    2466          18 :         pari_err_TYPE("checkgroup (element)", gel(gal,i));
    2467        6276 :       if(lg(gel(gal,i))!=lg(gel(gal,1)))
    2468          12 :         pari_err_DIM("checkgroup [length of permutations]");
    2469             :     }
    2470         438 :     return gal;
    2471             :   }
    2472             : }
    2473             : 
    2474             : GEN
    2475         144 : galoisisabelian(GEN gal, long flag)
    2476             : {
    2477         144 :   pari_sp av = avma;
    2478         144 :   GEN S, G = checkgroup(gal,&S);
    2479         144 :   if (!group_isabelian(G)) { avma=av; return gen_0; }
    2480         126 :   switch(flag)
    2481             :   {
    2482          42 :     case 0: return gerepileupto(av, group_abelianHNF(G,S));
    2483          42 :     case 1: avma=av; return gen_1;
    2484          42 :     case 2: return gerepileupto(av, group_abelianSNF(G,S));
    2485           0 :     default: pari_err_FLAG("galoisisabelian");
    2486             :   }
    2487             :   return NULL; /* LCOV_EXCL_LINE */
    2488             : }
    2489             : 
    2490             : long
    2491          48 : galoisisnormal(GEN gal, GEN sub)
    2492             : {
    2493          48 :   pari_sp av = avma;
    2494          48 :   GEN S, G = checkgroup(gal, &S), H = checkgroup(sub, &S);
    2495          48 :   long res = group_subgroup_isnormal(G, H);
    2496          48 :   avma = av;
    2497          48 :   return res;
    2498             : }
    2499             : 
    2500             : static GEN
    2501         252 : conjclasses_count(GEN conj, long nb)
    2502             : {
    2503         252 :   long i, l = lg(conj);
    2504         252 :   GEN c = zero_zv(nb);
    2505        3054 :   for(i=1; i<l; i++)
    2506        2802 :     c[conj[i]]++;
    2507         252 :   return c;
    2508             : }
    2509             : static GEN
    2510         252 : conjclasses_expand(GEN elts, GEN conj, long nb)
    2511             : {
    2512         252 :   GEN c = conjclasses_count(conj, nb);
    2513         252 :   long i, l = lg(conj);
    2514         252 :   GEN e = cgetg(nb+1, t_VEC);
    2515        2412 :   for(i=1; i<=nb; i++)
    2516        2160 :     gel(e,i) = cgetg(c[i]+1, t_VEC);
    2517        3054 :   for(i=1; i<l; i++)
    2518             :   {
    2519        2802 :     long ci = conj[i];
    2520        2802 :     gmael2(e, ci, c[ci]) = gel(elts, i);
    2521        2802 :     c[ci]--;
    2522             :   }
    2523         252 :   return e;
    2524             : }
    2525             : 
    2526             : GEN
    2527         252 : galoisconjclasses(GEN gal)
    2528             : {
    2529         252 :   pari_sp av = avma;
    2530         252 :   GEN conj, elts = checkgroupelts(gal);
    2531             :   long nb;
    2532         252 :   elts = gen_sort(elts,(void*)vecsmall_lexcmp,cmp_nodata);
    2533         252 :   conj = groupelts_conjclasses(elts, &nb);
    2534         252 :   return gerepilecopy(av, conjclasses_expand(elts, conj, nb));
    2535             : }
    2536             : 
    2537             : GEN
    2538          54 : galoissubgroups(GEN gal)
    2539             : {
    2540          54 :   pari_sp av = avma;
    2541          54 :   GEN S, G = checkgroup(gal,&S);
    2542          54 :   return gerepileupto(av, group_subgroups(G));
    2543             : }
    2544             : 
    2545             : GEN
    2546          42 : galoissubfields(GEN G, long flag, long v)
    2547             : {
    2548          42 :   pari_sp av = avma;
    2549          42 :   GEN L = galoissubgroups(G);
    2550          42 :   long i, l = lg(L);
    2551          42 :   GEN S = cgetg(l, t_VEC);
    2552          42 :   for (i = 1; i < l; ++i) gel(S,i) = galoisfixedfield(G, gmael(L,i,1), flag, v);
    2553          42 :   return gerepileupto(av, S);
    2554             : }
    2555             : 
    2556             : GEN
    2557          24 : galoisexport(GEN gal, long format)
    2558             : {
    2559          24 :   pari_sp av = avma;
    2560          24 :   GEN S, G = checkgroup(gal,&S);
    2561          24 :   return gerepileupto(av, group_export(G,format));
    2562             : }
    2563             : 
    2564             : GEN
    2565         336 : galoisidentify(GEN gal)
    2566             : {
    2567         336 :   pari_sp av = avma;
    2568         336 :   GEN S, G = checkgroup(gal,&S);
    2569         330 :   long idx = group_ident(G,S), card = group_order(G);
    2570         330 :   avma = av; return mkvec2s(card, idx);
    2571             : }
    2572             : 
    2573             : static GEN
    2574         252 : groupelts_chartable(GEN elts)
    2575             : {
    2576         252 :   pari_sp av = avma;
    2577             :   GEN al, cc, conjclass, rep, ctp, ct0, dec, jg, f;
    2578             :   long n, i, j, k, l, expo, nbcl;
    2579             :   ulong p, pov2, ze;
    2580         252 :   n = lg(elts)-1;
    2581             :   /* exponent of G */
    2582         252 :   expo = groupelts_exponent(elts);
    2583         252 :   p = unextprime(2*n+1);
    2584         252 :   if(expo>1) while (p%expo!=1) p = unextprime(p+1);
    2585             :   /* compute character table modulo p: idempotents of Z(KG) */
    2586         252 :   al = alggroupcenter(elts,utoi(p),&cc);
    2587         252 :   elts = gel(cc,1);
    2588         252 :   conjclass = gel(cc,2);
    2589         252 :   rep = gel(cc,3);
    2590         252 :   nbcl = lg(rep)-1;
    2591         252 :   dec = algsimpledec(al,1);
    2592         252 :   ctp = cgetg(nbcl+1,t_VEC);
    2593        2412 :   for(i=1;i<=nbcl;i++)
    2594             :   {
    2595        2160 :     GEN e = Flv_Fl_mul(ZV_to_Flv(gmael3(dec,i,3,1), p),n%p,p);
    2596        2160 :     gel(ctp,i) = Flv_Fl_div(e, usqrt(e[1]), p);
    2597             :   }
    2598         252 :   ze = Fl_inv(Fl_powu(pgener_Fl(p),(p-1)/expo, p), p);
    2599             : 
    2600             :   /* lift character table to Z[zeta_e] */
    2601         252 :   f = polcyclo(expo,0);
    2602         252 :   pov2 = p>>1;
    2603         252 :   jg = new_chunk(expo);
    2604         252 :   ct0 = cgetg(nbcl+1, t_MAT);
    2605        2412 :   for(j=1;j<=nbcl;j++)
    2606             :   {
    2607        2160 :     ulong zek = 1;
    2608        2160 :     GEN g = gel(elts,rep[j]);
    2609        2160 :     GEN h = identity_perm(lg(g)-1);
    2610       18282 :     for (l=0; l<expo; l++)
    2611             :     {
    2612       16122 :       jg[l] = conjclass[vecsearch(elts,h,NULL)];
    2613       16122 :       h = perm_mul(h, g);
    2614             :     }
    2615        2160 :     gel(ct0, j) = cgetg(nbcl+1, t_COL);
    2616       25104 :     for(i=1;i<=nbcl;i++)
    2617             :     {
    2618       22944 :       GEN chip = gel(ctp,i);
    2619       22944 :       GEN cij = cgetg(expo+2, t_POL);
    2620       22944 :       cij[1] = evalvarn(0) | evalsigne(1);
    2621             :       /* chi(g) = sum_{k=0}^{e-1} a_k ze^k
    2622             :        * a_k = 1/e sum_{l=0}^{e-1} chi(g^l) ze^{-k*l} */
    2623      203610 :       for (k=0; k<expo; k++)
    2624             :       {
    2625      180666 :         ulong a = 0, z = 1;
    2626     2071824 :         for (l=0; l<expo; l++)
    2627             :         {
    2628     1891158 :           a = Fl_add(a, Fl_mul(uel(chip,jg[l]), z, p), p);
    2629     1891158 :           z = Fl_mul(z, zek, p);
    2630             :         }
    2631      180666 :         a = Fl_div(a, expo, p);
    2632      180666 :         gel(cij,k+2) = stoi(Fl_center(a, p, pov2));
    2633      180666 :         zek = Fl_mul(zek, ze, p);
    2634             :       }
    2635       22944 :       (void) ZX_renormalize(cij, expo+2);
    2636       22944 :       gcoeff(ct0,i,j) = ZX_rem(cij, f);
    2637             :     }
    2638             :   }
    2639         252 :   ct0 = gen_sort(shallowtrans(ct0),(void*)cmp_universal,cmp_nodata);
    2640         252 :   for(i=1; !vec_isconst(gel(ct0, i)); i++) continue;
    2641         252 :   if (i>1) swap(gel(ct0,1),gel(ct0,i));
    2642         252 :   return gerepilecopy(av, mkvec2(ct0,stoi(expo)));
    2643             : }
    2644             : 
    2645             : GEN
    2646         252 : galoischartable(GEN gal)
    2647             : {
    2648         252 :   GEN elts = checkgroupelts(gal);
    2649         252 :   return groupelts_chartable(elts);
    2650             : }

Generated by: LCOV version 1.11