Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - galconj.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.13.0 lcov report (development 25825-edc109b529) Lines: 1853 2273 81.5 %
Date: 2020-09-21 06:08:33 Functions: 115 149 77.2 %
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         280 : is2sparse(GEN x)
      24             : {
      25         280 :   long i, l = lg(x);
      26         280 :   if (odd(l-3)) return 0;
      27         826 :   for(i=3; i<l; i+=2)
      28         651 :     if (signe(gel(x,i))) return 0;
      29         175 :   return 1;
      30             : }
      31             : 
      32             : static GEN
      33         945 : galoisconj1(GEN nf)
      34             : {
      35         945 :   GEN x = get_nfpol(nf, &nf), f = nf? nf : x, y, z;
      36         945 :   long i, lz, v = varn(x), nbmax;
      37         945 :   pari_sp av = avma;
      38         945 :   RgX_check_ZX(x, "nfgaloisconj");
      39         945 :   nbmax = numberofconjugates(x, 2);
      40         945 :   if (nbmax==1) retmkcol(pol_x(v));
      41         378 :   if (nbmax==2 && is2sparse(x))
      42             :   {
      43         175 :     GEN res = cgetg(3,t_COL);
      44         175 :     gel(res,1) = deg1pol_shallow(gen_m1, gen_0, v);
      45         175 :     gel(res,2) = pol_x(v);
      46         175 :     return res;
      47             :   }
      48         203 :   x = leafcopy(x); setvarn(x, fetch_var_higher());
      49         203 :   z = nfroots(f, x); lz = lg(z);
      50         203 :   y = cgetg(lz, t_COL);
      51        1008 :   for (i = 1; i < lz; i++)
      52             :   {
      53         805 :     GEN t = lift(gel(z,i));
      54         805 :     if (typ(t) == t_POL) setvarn(t, v);
      55         805 :     gel(y,i) = t;
      56             :   }
      57         203 :   (void)delete_var();
      58         203 :   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             :   GEN  dis;
      82             : };
      83             : struct galois_lift {
      84             :   GEN  T;
      85             :   GEN  den;
      86             :   GEN  p;
      87             :   GEN  L;
      88             :   GEN  Lden;
      89             :   long e;
      90             :   GEN  Q;
      91             :   GEN  TQ;
      92             :   struct galois_borne *gb;
      93             : };
      94             : struct galois_testlift {
      95             :   long n;
      96             :   long f;
      97             :   long g;
      98             :   GEN  bezoutcoeff;
      99             :   GEN  pauto;
     100             :   GEN  C;
     101             :   GEN  Cd;
     102             : };
     103             : struct galois_test { /* data for permutation test */
     104             :   GEN order; /* order of tests pour galois_test_perm */
     105             :   GEN borne, lborne; /* coefficient bounds */
     106             :   GEN ladic;
     107             :   GEN PV; /* NULL or vector of test matrices (Vmatrix) */
     108             :   GEN TM; /* transpose of M */
     109             :   GEN L; /* p-adic roots, known mod ladic */
     110             :   GEN M; /* vandermonde inverse */
     111             : };
     112             : /* result of the study of Frobenius degrees */
     113             : enum ga_code {ga_all_normal=1,ga_ext_2=2,ga_non_wss=4,
     114             :               ga_all_nilpotent=8,ga_easy=16};
     115             : struct galois_analysis {
     116             :   long p; /* prime to be lifted */
     117             :   long deg; /* degree of the lift */
     118             :   long ord;
     119             :   long l; /* l: prime number such that T is totally split mod l */
     120             :   long p4;
     121             :   long group;
     122             : };
     123             : struct galois_frobenius {
     124             :   long p;
     125             :   long fp;
     126             :   long deg;
     127             :   GEN Tmod;
     128             :   GEN psi;
     129             : };
     130             : 
     131             : /* given complex roots L[i], i <= n of some monic T in C[X], return
     132             :  * the T'(L[i]), computed stably via products of differences */
     133             : static GEN
     134       11371 : vandermondeinverseprep(GEN L)
     135             : {
     136       11371 :   long i, j, n = lg(L);
     137             :   GEN V;
     138       11371 :   V = cgetg(n, t_VEC);
     139       75613 :   for (i = 1; i < n; i++)
     140             :   {
     141       64242 :     pari_sp ltop = avma;
     142       64242 :     GEN W = cgetg(n-1,t_VEC);
     143       64242 :     long k = 1;
     144     1141398 :     for (j = 1; j < n; j++)
     145     1077156 :       if (i != j) gel(W, k++) = gsub(gel(L,i),gel(L,j));
     146       64242 :     gel(V,i) = gerepileupto(ltop, RgV_prod(W));
     147             :   }
     148       11371 :   return V;
     149             : }
     150             : 
     151             : /* Compute the inverse of the van der Monde matrix of T multiplied by den */
     152             : GEN
     153       11112 : vandermondeinverse(GEN L, GEN T, GEN den, GEN prep)
     154             : {
     155       11112 :   pari_sp ltop = avma;
     156       11112 :   long i, n = lg(L)-1;
     157             :   GEN M, P;
     158       11112 :   if (!prep) prep = vandermondeinverseprep(L);
     159       11112 :   if (den && !equali1(den)) T = RgX_Rg_mul(T,den);
     160       11112 :   M = cgetg(n+1, t_MAT);
     161       71630 :   for (i = 1; i <= n; i++)
     162             :   {
     163       60518 :     P = RgX_Rg_div(RgX_div_by_X_x(T, gel(L,i), NULL), gel(prep,i));
     164       60518 :     gel(M,i) = RgX_to_RgC(P,n);
     165             :   }
     166       11112 :   return gerepilecopy(ltop, M);
     167             : }
     168             : 
     169             : /* #r = r1 + r2 */
     170             : GEN
     171          15 : embed_roots(GEN ro, long r1)
     172             : {
     173          15 :   long r2 = lg(ro)-1-r1;
     174             :   GEN L;
     175          15 :   if (!r2) L = ro;
     176             :   else
     177             :   {
     178          15 :     long i,j, N = r1+2*r2;
     179          15 :     L = cgetg(N+1, t_VEC);
     180          99 :     for (i = 1; i <= r1; i++) gel(L,i) = gel(ro,i);
     181          98 :     for (j = i; j <= N; i++)
     182             :     {
     183          83 :       GEN z = gel(ro,i);
     184          83 :       gel(L,j++) = z;
     185          83 :       gel(L,j++) = mkcomplex(gel(z,1), gneg(gel(z,2)));
     186             :     }
     187             :   }
     188          15 :   return L;
     189             : }
     190             : GEN
     191       35399 : embed_disc(GEN z, long r1, long prec)
     192             : {
     193       35399 :   pari_sp av = avma;
     194       35399 :   GEN t = real_1(prec);
     195       35399 :   long i, j, n = lg(z)-1, r2 = n-r1;
     196      130431 :   for (i = 1; i < r1; i++)
     197             :   {
     198       95032 :     GEN zi = gel(z,i);
     199      665042 :     for (j = i+1; j <= r1; j++) t = gmul(t, gsub(zi, gel(z,j)));
     200             :   }
     201      193242 :   for (j = r1+1; j <= n; j++)
     202             :   {
     203      157843 :     GEN zj = gel(z,j), a = gel(zj,1), b = gel(zj,2), b2 = gsqr(b);
     204      174601 :     for (i = 1; i <= r1; i++)
     205             :     {
     206       16758 :       GEN zi = gel(z,i);
     207       16758 :       t = gmul(t, gadd(gsqr(gsub(zi, a)), b2));
     208             :     }
     209      157843 :     t = gmul(t, b);
     210             :   }
     211       35399 :   if (r2) t = gmul2n(t, r2);
     212       35399 :   if (r2 > 1)
     213             :   {
     214       24164 :     GEN T = real_1(prec);
     215      156975 :     for (i = r1+1; i < n; i++)
     216             :     {
     217      132811 :       GEN zi = gel(z,i), a = gel(zi,1), b = gel(zi,2);
     218      864591 :       for (j = i+1; j <= n; j++)
     219             :       {
     220      731780 :         GEN zj = gel(z,j), c = gel(zj,1), d = gel(zj,2);
     221      731780 :         GEN f = gsqr(gsub(a,c)), g = gsqr(gsub(b,d)), h = gsqr(gadd(b,d));
     222      731780 :         T = gmul(T, gmul(gadd(f,g), gadd(f,h)));
     223             :       }
     224             :     }
     225       24164 :     t = gmul(t, T);
     226             :   }
     227       35399 :   t = gsqr(t);
     228       35399 :   if (odd(r2)) t = gneg(t);
     229       35399 :   return gerepileupto(av, t);
     230             : }
     231             : 
     232             : /* Compute bound for the coefficients of automorphisms.
     233             :  * T a ZX, den a t_INT denominator or NULL */
     234             : GEN
     235       11371 : initgaloisborne(GEN T, GEN den, long prec, GEN *ptL, GEN *ptprep, GEN *ptdis)
     236             : {
     237             :   GEN L, prep, nf, r;
     238             :   pari_timer ti;
     239             : 
     240       11371 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     241       11371 :   T = get_nfpol(T, &nf);
     242       11371 :   r = nf ? nf_get_roots(nf) : NULL;
     243       11371 :   if (nf &&  precision(gel(r, 1)) >= prec)
     244          15 :     L = embed_roots(r, nf_get_r1(nf));
     245             :   else
     246       11356 :     L = QX_complex_roots(T, prec);
     247       11371 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"roots");
     248       11371 :   prep = vandermondeinverseprep(L);
     249       11371 :   if (!den || ptdis)
     250             :   {
     251        3689 :     GEN res = RgV_prod(gabs(prep,prec));
     252        3689 :     GEN D = ZX_disc_all(T, 1 + expi(ceil_safe(res))); /* +1 if inaccurate res */
     253        3689 :     if (ptdis) *ptdis = D;
     254        3689 :     if (!den) den = indexpartial(T,D);
     255             :   }
     256       11371 :   if (ptprep) *ptprep = prep;
     257       11371 :   *ptL = L; return den;
     258             : }
     259             : 
     260             : /* ||| M ||| with respect to || x ||_oo, M t_MAT */
     261             : GEN
     262       27071 : matrixnorm(GEN M, long prec)
     263             : {
     264       27071 :   long i,j,m, l = lg(M);
     265       27071 :   GEN B = real_0(prec);
     266             : 
     267       27071 :   if (l == 1) return B;
     268       27071 :   m = lgcols(M);
     269       87192 :   for (i = 1; i < m; i++)
     270             :   {
     271       60121 :     GEN z = gabs(gcoeff(M,i,1), prec);
     272      804246 :     for (j = 2; j < l; j++) z = gadd(z, gabs(gcoeff(M,i,j), prec));
     273       60121 :     if (gcmp(z, B) > 0) B = z;
     274             :   }
     275       27071 :   return B;
     276             : }
     277             : 
     278             : static GEN
     279        3430 : galoisborne(GEN T, GEN dn, struct galois_borne *gb, long d)
     280             : {
     281             :   pari_sp ltop, av2;
     282             :   GEN borne, borneroots, bornetrace, borneabs;
     283             :   long prec;
     284             :   GEN L, M, prep, den;
     285             :   pari_timer ti;
     286        3430 :   const long step=3;
     287             : 
     288        3430 :   prec = nbits2prec(bit_accuracy(ZX_max_lg(T)));
     289        3430 :   den = initgaloisborne(T,dn,prec, &L,&prep,&gb->dis);
     290        3430 :   if (!dn) dn = den;
     291        3430 :   ltop = avma;
     292        3430 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     293        3430 :   M = vandermondeinverse(L, RgX_gtofp(T, prec), den, prep);
     294        3430 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"vandermondeinverse");
     295        3430 :   borne = matrixnorm(M, prec);
     296        3430 :   borneroots = gsupnorm(L, prec); /*t_REAL*/
     297        3430 :   bornetrace = gmulsg((2*step)*degpol(T)/d,
     298        3430 :                       powru(borneroots, minss(degpol(T), step)));
     299        3430 :   borneroots = ceil_safe(gmul(borne, borneroots));
     300        3430 :   borneabs = ceil_safe(gmax_shallow(gmul(borne, bornetrace),
     301             :                                     powru(bornetrace, d)));
     302        3430 :   av2 = avma;
     303             :   /*We use d-1 test, so we must overlift to 2^BITS_IN_LONG*/
     304        3430 :   gb->valsol = logint(shifti(borneroots,2+BITS_IN_LONG), gb->l) + 1;
     305        3430 :   gb->valabs = logint(shifti(borneabs,2), gb->l) + 1;
     306        3430 :   gb->valabs = maxss(gb->valsol, gb->valabs);
     307        3430 :   if (DEBUGLEVEL >= 4)
     308           0 :     err_printf("GaloisConj: val1=%ld val2=%ld\n", gb->valsol, gb->valabs);
     309        3430 :   set_avma(av2);
     310        3430 :   gb->bornesol = gerepileuptoint(ltop, shifti(borneroots,1));
     311        3430 :   if (DEBUGLEVEL >= 9)
     312           0 :     err_printf("GaloisConj: Bound %Ps\n",borneroots);
     313        3430 :   gb->ladicsol = powiu(gb->l, gb->valsol);
     314        3430 :   gb->ladicabs = powiu(gb->l, gb->valabs);
     315        3430 :   return dn;
     316             : }
     317             : 
     318             : static GEN
     319        3269 : makeLden(GEN L,GEN den, struct galois_borne *gb)
     320        3269 : { return FpC_Fp_mul(L, den, gb->ladicsol); }
     321             : 
     322             : /* Initialize the galois_lift structure */
     323             : static void
     324        3332 : initlift(GEN T, GEN den, ulong p, GEN L, GEN Lden, struct galois_borne *gb, struct galois_lift *gl)
     325             : {
     326             :   pari_sp av;
     327             :   long e;
     328        3332 :   gl->gb = gb;
     329        3332 :   gl->T = T;
     330        3332 :   gl->den = is_pm1(den)? gen_1: den;
     331        3332 :   gl->p = utoipos(p);
     332        3332 :   gl->L = L;
     333        3332 :   gl->Lden = Lden;
     334        3332 :   av = avma;
     335        3332 :   e = logint(shifti(gb->bornesol, 2+BITS_IN_LONG), gl->p) + 1;
     336        3332 :   set_avma(av);
     337        3332 :   if (e < 2) e = 2;
     338        3332 :   gl->e = e;
     339        3332 :   gl->Q = powuu(p, e);
     340        3332 :   gl->TQ = FpX_red(T,gl->Q);
     341        3332 : }
     342             : 
     343             : /* Check whether f is (with high probability) a solution and compute its
     344             :  * permutation */
     345             : static int
     346       10708 : poltopermtest(GEN f, struct galois_lift *gl, GEN pf)
     347             : {
     348             :   pari_sp av;
     349       10708 :   GEN fx, fp, B = gl->gb->bornesol;
     350             :   long i, j, ll;
     351       61666 :   for (i = 2; i < lg(f); i++)
     352       55789 :     if (abscmpii(gel(f,i),B) > 0)
     353             :     {
     354        4831 :       if (DEBUGLEVEL>=4) err_printf("GaloisConj: Solution too large.\n");
     355        4831 :       if (DEBUGLEVEL>=8) err_printf("f=%Ps\n borne=%Ps\n",f,B);
     356        4831 :       return 0;
     357             :     }
     358        5877 :   ll = lg(gl->L);
     359        5877 :   fp = const_vecsmall(ll-1, 1); /* left on stack */
     360        5877 :   av = avma;
     361       63404 :   for (i = 1; i < ll; i++, set_avma(av))
     362             :   {
     363       57547 :     fx = FpX_eval(f, gel(gl->L,i), gl->gb->ladicsol);
     364      742203 :     for (j = 1; j < ll; j++)
     365      742183 :       if (fp[j] && equalii(fx, gel(gl->Lden,j))) { pf[i]=j; fp[j]=0; break; }
     366       57547 :     if (j == ll) return 0;
     367             :   }
     368        5857 :   return 1;
     369             : }
     370             : 
     371             : static long
     372        6878 : galoisfrobeniustest(GEN aut, struct galois_lift *gl, GEN frob)
     373             : {
     374        6878 :   pari_sp av = avma;
     375        6878 :   GEN tlift = aut;
     376        6878 :   if (gl->den != gen_1) tlift = FpX_Fp_mul(tlift, gl->den, gl->Q);
     377        6878 :   tlift = FpX_center_i(tlift, gl->Q, shifti(gl->Q,-1));
     378        6878 :   return gc_long(av, poltopermtest(tlift, gl, frob));
     379             : }
     380             : 
     381             : static GEN
     382       14763 : monoratlift(void *E, GEN S, GEN q)
     383             : {
     384       14763 :   pari_sp ltop = avma;
     385       14763 :   struct galois_lift *gl = (struct galois_lift *) E;
     386       14763 :   GEN qm1 = sqrti(shifti(q,-2)), N = gl->Q;
     387       14763 :   GEN tlift = FpX_ratlift(S, q, qm1, qm1, gl->den);
     388       14763 :   if (tlift)
     389             :   {
     390        2272 :     pari_sp ltop = avma;
     391        2272 :     GEN frob = cgetg(lg(gl->L), t_VECSMALL);
     392        2272 :     if(DEBUGLEVEL>=4)
     393           0 :       err_printf("MonomorphismLift: trying early solution %Ps\n",tlift);
     394        2272 :     if (gl->den != gen_1)
     395        1649 :       tlift = FpX_Fp_mul(FpX_red(Q_muli_to_int(tlift, gl->den), N),
     396             :                          Fp_inv(gl->den, N), N);
     397        2272 :     if (galoisfrobeniustest(tlift, gl, frob))
     398             :     {
     399        2252 :       if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: true early solution.\n");
     400        2252 :       return gerepilecopy(ltop, tlift);
     401             :     }
     402          20 :     if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: false early solution.\n");
     403             :   }
     404       12511 :   set_avma(ltop);
     405       12511 :   return NULL;
     406             : }
     407             : 
     408             : static GEN
     409        4389 : monomorphismratlift(GEN P, GEN S, struct galois_lift *gl)
     410             : {
     411             :   pari_timer ti;
     412        4389 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     413        4389 :   S = ZpX_ZpXQ_liftroot_ea(P,S,gl->T,gl->p, gl->e, (void*)gl, monoratlift);
     414        4389 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "monomorphismlift()");
     415        4389 :   return S;
     416             : }
     417             : 
     418             : /* Let T be a polynomial in Z[X] , p a prime number, S in Fp[X]/(T) so
     419             :  * that T(S)=0 [p,T]. Lift S in S_0 so that T(S_0)=0 [T,p^e]
     420             :  * Unclean stack */
     421             : static GEN
     422        4389 : automorphismlift(GEN S, struct galois_lift *gl)
     423             : {
     424        4389 :   return monomorphismratlift(gl->T, S, gl);
     425             : }
     426             : 
     427             : static GEN
     428        3332 : galoisdolift(struct galois_lift *gl)
     429             : {
     430        3332 :   pari_sp av = avma;
     431        3332 :   GEN Tp = FpX_red(gl->T, gl->p);
     432        3332 :   GEN S = FpX_Frobenius(Tp, gl->p);
     433        3332 :   return gerepileupto(av, automorphismlift(S, gl));
     434             : }
     435             : 
     436             : static GEN
     437         637 : galoisdoliftn(struct galois_lift *gl, long e)
     438             : {
     439         637 :   pari_sp av = avma;
     440         637 :   GEN Tp = FpX_red(gl->T, gl->p);
     441         637 :   GEN S = FpXQ_autpow(FpX_Frobenius(Tp, gl->p), e, Tp, gl->p);
     442         637 :   return gerepileupto(av, automorphismlift(S, gl));
     443             : }
     444             : 
     445             : static ulong
     446          70 : findpsi(GEN D, ulong pstart, GEN P, GEN S, long o, GEN *Tmod, GEN *Tpsi)
     447             : {
     448             :   forprime_t iter;
     449             :   ulong p;
     450          70 :   long n = degpol(P), i, j, g = n/o;
     451          70 :   GEN psi = cgetg(g+1, t_VECSMALL);
     452          70 :   u_forprime_init(&iter, pstart, ULONG_MAX);
     453        2191 :   while ((p = u_forprime_next(&iter)))
     454             :   {
     455             :     GEN F, Sp;
     456        2191 :     long gp = 0;
     457        2191 :     if (smodis(D, p) == 0)
     458         189 :       continue;
     459        2002 :     F = gel(Flx_factor(ZX_to_Flx(P, p), p), 1);
     460        2002 :     if (lg(F)-1 != g) continue;
     461         693 :     Sp = RgX_to_Flx(S, p);
     462        1778 :     for (j = 1; j <= g; j++)
     463             :     {
     464        1659 :       GEN Fj = gel(F, j);
     465        1659 :       GEN Sj = Flx_rem(Sp, Fj, p);
     466        1659 :       GEN A = Flxq_autpowers(Flx_Frobenius(Fj, p), o,  Fj, p);
     467        5670 :       for (i = 1; i <= o; i++)
     468        5096 :         if (gequal(Sj, gel(A,i+1)))
     469             :         {
     470        1085 :           psi[j] = i; break;
     471             :         }
     472        1659 :       if (i > o) break;
     473        1085 :       if (gp==0 && i==1) gp=j;
     474             :     }
     475         693 :     if (gp && j > g)
     476             :     {
     477             :       /* Normalize result so that psi[l]=1 */
     478          70 :       if (gp!=1)
     479             :       {
     480           7 :         swap(gel(F,1),gel(F,gp));
     481           7 :         lswap(uel(psi,1),uel(psi,gp));
     482             :       }
     483          70 :       *Tpsi = Flv_Fl_div(psi,psi[g],o);
     484          70 :       *Tmod = FlxV_to_ZXV(F);
     485          70 :       return p;
     486             :     }
     487             :   }
     488           0 :   return 0;
     489             : }
     490             : 
     491             : static void
     492         567 : inittestlift(GEN plift, GEN Tmod, struct galois_lift *gl,
     493             :              struct galois_testlift *gt)
     494             : {
     495             :   pari_timer ti;
     496         567 :   gt->n = lg(gl->L) - 1;
     497         567 :   gt->g = lg(Tmod) - 1;
     498         567 :   gt->f = gt->n / gt->g;
     499         567 :   gt->bezoutcoeff = bezout_lift_fact(gl->T, Tmod, gl->p, gl->e);
     500         567 :   if (DEBUGLEVEL >= 2) timer_start(&ti);
     501         567 :   gt->pauto = FpXQ_autpowers(plift, gt->f-1, gl->TQ, gl->Q);
     502         567 :   if (DEBUGLEVEL >= 2) timer_printf(&ti, "Frobenius power");
     503         567 : }
     504             : 
     505             : /* Explanation of the intheadlong technique:
     506             :  * Let C be a bound, B = BITS_IN_LONG, M > C*2^B a modulus and 0 <= a_i < M for
     507             :  * i=1,...,n where n < 2^B. We want to test if there exist k,l, |k| < C < M/2^B,
     508             :  * such that sum a_i = k + l*M
     509             :  * We write a_i*2^B/M = b_i+c_i with b_i integer and 0<=c_i<1, so that
     510             :  *   sum b_i - l*2^B = k*2^B/M - sum c_i
     511             :  * Since -1 < k*2^B/M < 1 and 0<=c_i<1, it follows that
     512             :  *   -n-1 < sum b_i - l*2^B < 1  i.e.  -n <= sum b_i -l*2^B <= 0
     513             :  * So we compute z = - sum b_i [mod 2^B] and check if 0 <= z <= n. */
     514             : 
     515             : /* Assume 0 <= x < mod. */
     516             : static ulong
     517     1069950 : intheadlong(GEN x, GEN mod)
     518             : {
     519     1069950 :   pari_sp av = avma;
     520     1069950 :   long res = (long) itou(divii(shifti(x,BITS_IN_LONG),mod));
     521     1069950 :   return gc_long(av,res);
     522             : }
     523             : static GEN
     524       33376 : vecheadlong(GEN W, GEN mod)
     525             : {
     526       33376 :   long i, l = lg(W);
     527       33376 :   GEN V = cgetg(l, t_VECSMALL);
     528     1073058 :   for(i=1; i<l; i++) V[i] = intheadlong(gel(W,i), mod);
     529       33376 :   return V;
     530             : }
     531             : static GEN
     532        2520 : matheadlong(GEN W, GEN mod)
     533             : {
     534        2520 :   long i, l = lg(W);
     535        2520 :   GEN V = cgetg(l,t_MAT);
     536       35896 :   for(i=1; i<l; i++) gel(V,i) = vecheadlong(gel(W,i), mod);
     537        2520 :   return V;
     538             : }
     539             : static ulong
     540       30268 : polheadlong(GEN P, long n, GEN mod)
     541             : {
     542       30268 :   return (lg(P)>n+2)? intheadlong(gel(P,n+2),mod): 0;
     543             : }
     544             : 
     545             : #define headlongisint(Z,N) (-(ulong)(Z)<=(ulong)(N))
     546             : 
     547             : static long
     548         532 : frobeniusliftall(GEN sg, long el, GEN *psi, struct galois_lift *gl,
     549             :                  struct galois_testlift *gt, GEN frob)
     550             : {
     551         532 :   pari_sp av, ltop2, ltop = avma;
     552         532 :   long i,j,k, c = lg(sg)-1, n = lg(gl->L)-1, m = gt->g, d = m / c;
     553             :   GEN pf, u, v, C, Cd, SG, cache;
     554         532 :   long N1, N2, R1, Ni, ord = gt->f, c_idx = gt->g-1;
     555             :   ulong headcache;
     556         532 :   long hop = 0;
     557             :   GEN NN, NQ;
     558             :   pari_timer ti;
     559             : 
     560         532 :   *psi = pf = cgetg(m, t_VECSMALL);
     561         532 :   ltop2 = avma;
     562         532 :   NN = diviiexact(mpfact(m), mului(c, powiu(mpfact(d), c)));
     563         532 :   if (DEBUGLEVEL >= 4)
     564           0 :     err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     565         532 :   N1=10000000;
     566         532 :   NQ=divis_rem(NN,N1,&R1);
     567         532 :   if (abscmpiu(NQ,1000000000)>0)
     568             :   {
     569           0 :     pari_warn(warner,"Combinatorics too hard : would need %Ps tests!\n"
     570             :         "I will skip it, but it may induce an infinite loop",NN);
     571           0 :     *psi = NULL; return gc_long(ltop,0);
     572             :   }
     573         532 :   N2=itos(NQ); if(!N2) N1=R1;
     574         532 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     575         532 :   set_avma(ltop2);
     576         532 :   C = gt->C;
     577         532 :   Cd= gt->Cd;
     578         532 :   v = FpXQ_mul(gel(gt->pauto, 1+el%ord), gel(gt->bezoutcoeff, m),gl->TQ,gl->Q);
     579         532 :   if (gl->den != gen_1) v = FpX_Fp_mul(v, gl->den, gl->Q);
     580         532 :   SG = cgetg(lg(sg),t_VECSMALL);
     581        1624 :   for(i=1; i<lg(SG); i++) SG[i] = (el*sg[i])%ord + 1;
     582         532 :   cache = cgetg(m+1,t_VECSMALL); cache[m] = polheadlong(v,1,gl->Q);
     583         532 :   headcache = polheadlong(v,2,gl->Q);
     584        1330 :   for (i = 1; i < m; i++) pf[i] = 1 + i/d;
     585         532 :   av = avma;
     586         532 :   for (Ni = 0, i = 0; ;i++)
     587             :   {
     588      292166 :     for (j = c_idx ; j > 0; j--)
     589             :     {
     590      230587 :       long h = SG[pf[j]];
     591      230587 :       if (!mael(C,h,j))
     592             :       {
     593        1078 :         pari_sp av3 = avma;
     594        1078 :         GEN r = FpXQ_mul(gel(gt->pauto,h), gel(gt->bezoutcoeff,j),gl->TQ,gl->Q);
     595        1078 :         if (gl->den != gen_1) r = FpX_Fp_mul(r, gl->den, gl->Q);
     596        1078 :         gmael(C,h,j) = gclone(r);
     597        1078 :         mael(Cd,h,j) = polheadlong(r,1,gl->Q);
     598        1078 :         set_avma(av3);
     599             :       }
     600      230587 :       uel(cache,j) = uel(cache,j+1)+umael(Cd,h,j);
     601             :     }
     602       61579 :     if (headlongisint(uel(cache,1),n))
     603             :     {
     604        2149 :       ulong head = headcache;
     605       30275 :       for (j = 1; j < m; j++) head += polheadlong(gmael(C,SG[pf[j]],j),2,gl->Q);
     606        2149 :       if (headlongisint(head,n))
     607             :       {
     608         511 :         u = v;
     609        1253 :         for (j = 1; j < m; j++) u = ZX_add(u, gmael(C,SG[pf[j]],j));
     610         511 :         u = FpX_center_i(FpX_red(u, gl->Q), gl->Q, shifti(gl->Q,-1));
     611         511 :         if (poltopermtest(u, gl, frob))
     612             :         {
     613         504 :           if (DEBUGLEVEL >= 4)
     614             :           {
     615           0 :             timer_printf(&ti, "");
     616           0 :             err_printf("GaloisConj: %d hops on %Ps tests\n",hop,addis(mulss(Ni,N1),i));
     617             :           }
     618         504 :           return gc_long(ltop2,1);
     619             :         }
     620           7 :         if (DEBUGLEVEL >= 4) err_printf("M");
     621             :       }
     622        1638 :       else hop++;
     623             :     }
     624       61075 :     if (DEBUGLEVEL >= 4 && i % maxss(N1/20, 1) == 0)
     625           0 :       timer_printf(&ti, "GaloisConj:Testing %Ps", addis(mulss(Ni,N1),i));
     626       61075 :     set_avma(av);
     627       61075 :     if (i == N1-1)
     628             :     {
     629          28 :       if (Ni==N2-1) N1 = R1;
     630          28 :       if (Ni==N2) break;
     631           0 :       Ni++; i = 0;
     632           0 :       if (DEBUGLEVEL>=4) timer_start(&ti);
     633             :     }
     634      168742 :     for (j = 2; j < m && pf[j-1] >= pf[j]; j++)
     635             :       /*empty*/; /* to kill clang Warning */
     636       99512 :     for (k = 1; k < j-k && pf[k] != pf[j-k]; k++) { lswap(pf[k], pf[j-k]); }
     637      107751 :     for (k = j - 1; pf[k] >= pf[j]; k--)
     638             :       /*empty*/;
     639       61047 :     lswap(pf[j], pf[k]); c_idx = j;
     640             :   }
     641          28 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: not found, %d hops \n",hop);
     642          28 :   *psi = NULL; return gc_long(ltop,0);
     643             : }
     644             : 
     645             : /* Compute the test matrix for the i-th line of V. Clone. */
     646             : static GEN
     647        2520 : Vmatrix(long i, struct galois_test *td)
     648             : {
     649        2520 :   pari_sp av = avma;
     650        2520 :   GEN m = gclone( matheadlong(FpC_FpV_mul(td->L, row(td->M,i), td->ladic), td->ladic));
     651        2520 :   set_avma(av); return m;
     652             : }
     653             : 
     654             : /* Initialize galois_test */
     655             : static void
     656        2296 : inittest(GEN L, GEN M, GEN borne, GEN ladic, struct galois_test *td)
     657             : {
     658        2296 :   long i, n = lg(L)-1;
     659        2296 :   GEN p = cgetg(n+1, t_VECSMALL);
     660        2296 :   if (DEBUGLEVEL >= 8) err_printf("GaloisConj: Init Test\n");
     661        2296 :   td->order = p;
     662       23282 :   for (i = 1; i <= n-2; i++) p[i] = i+2;
     663        2296 :   p[n-1] = 1; p[n] = 2;
     664        2296 :   td->borne = borne;
     665        2296 :   td->lborne = subii(ladic, borne);
     666        2296 :   td->ladic = ladic;
     667        2296 :   td->L = L;
     668        2296 :   td->M = M;
     669        2296 :   td->TM = shallowtrans(M);
     670        2296 :   td->PV = zero_zv(n);
     671        2296 :   gel(td->PV, 2) = Vmatrix(2, td);
     672        2296 : }
     673             : 
     674             : /* Free clones stored inside galois_test */
     675             : static void
     676        2296 : freetest(struct galois_test *td)
     677             : {
     678             :   long i;
     679       27874 :   for (i = 1; i < lg(td->PV); i++)
     680       25578 :     if (td->PV[i]) { gunclone(gel(td->PV,i)); td->PV[i] = 0; }
     681        2296 : }
     682             : 
     683             : /* Check if the integer P seen as a p-adic number is close to an integer less
     684             :  * than td->borne in absolute value */
     685             : static long
     686       65240 : padicisint(GEN P, struct galois_test *td)
     687             : {
     688       65240 :   pari_sp ltop = avma;
     689       65240 :   GEN U  = modii(P, td->ladic);
     690       65240 :   long r = cmpii(U, td->borne) <= 0 || cmpii(U, td->lborne) >= 0;
     691       65240 :   return gc_long(ltop, r);
     692             : }
     693             : 
     694             : /* Check if the permutation pf is valid according to td.
     695             :  * If not, update td to make subsequent test faster (hopefully) */
     696             : static long
     697      114219 : galois_test_perm(struct galois_test *td, GEN pf)
     698             : {
     699      114219 :   pari_sp av = avma;
     700      114219 :   long i, j, n = lg(td->L)-1;
     701      114219 :   GEN V, P = NULL;
     702      180642 :   for (i = 1; i < n; i++)
     703             :   {
     704      176848 :     long ord = td->order[i];
     705      176848 :     GEN PW = gel(td->PV, ord);
     706      176848 :     if (PW)
     707             :     {
     708      111608 :       ulong head = umael(PW,1,pf[1]);
     709     7120176 :       for (j = 2; j <= n; j++) head += umael(PW,j,pf[j]);
     710      111608 :       if (!headlongisint(head,n)) break;
     711             :     } else
     712             :     {
     713       65240 :       if (!P) P = vecpermute(td->L, pf);
     714       65240 :       V = FpV_dotproduct(gel(td->TM,ord), P, td->ladic);
     715       65240 :       if (!padicisint(V, td)) {
     716         224 :         gel(td->PV, ord) = Vmatrix(ord, td);
     717         224 :         if (DEBUGLEVEL >= 4) err_printf("M");
     718         224 :         break;
     719             :       }
     720             :     }
     721             :   }
     722      114219 :   if (i == n) return gc_long(av,1);
     723      110425 :   if (DEBUGLEVEL >= 4) err_printf("%d.", i);
     724      110425 :   if (i > 1)
     725             :   {
     726         742 :     long z = td->order[i];
     727        1526 :     for (j = i; j > 1; j--) td->order[j] = td->order[j-1];
     728         742 :     td->order[1] = z;
     729         742 :     if (DEBUGLEVEL >= 8) err_printf("%Ps", td->order);
     730             :   }
     731      110425 :   return gc_long(av,0);
     732             : }
     733             : /*Compute a*b/c when a*b will overflow*/
     734             : static long
     735           0 : muldiv(long a,long b,long c)
     736             : {
     737           0 :   return (long)((double)a*(double)b/c);
     738             : }
     739             : 
     740             : /* F = cycle decomposition of sigma,
     741             :  * B = cycle decomposition of cl(tau).
     742             :  * Check all permutations pf who can possibly correspond to tau, such that
     743             :  * tau*sigma*tau^-1 = sigma^s and tau^d = sigma^t, where d = ord cl(tau)
     744             :  * x: vector of choices,
     745             :  * G: vector allowing linear access to elts of F.
     746             :  * Choices multiple of e are not changed. */
     747             : static GEN
     748        4452 : testpermutation(GEN F, GEN B, GEN x, long s, long e, long cut,
     749             :                 struct galois_test *td)
     750             : {
     751        4452 :   pari_sp av, avm = avma;
     752             :   long a, b, c, d, n, p1, p2, p3, p4, p5, p6, l1, l2, N1, N2, R1;
     753        4452 :   long i, j, cx, hop = 0, start = 0;
     754             :   GEN pf, ar, G, W, NN, NQ;
     755             :   pari_timer ti;
     756        4452 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     757        4452 :   a = lg(F)-1; b = lg(gel(F,1))-1;
     758        4452 :   c = lg(B)-1; d = lg(gel(B,1))-1;
     759        4452 :   n = a*b;
     760        4452 :   s = (b+s) % b;
     761        4452 :   pf = cgetg(n+1, t_VECSMALL);
     762        4452 :   av = avma;
     763        4452 :   ar = cgetg(a+2, t_VECSMALL); ar[a+1]=0;
     764        4452 :   G  = cgetg(a+1, t_VECSMALL);
     765        4452 :   W  = gel(td->PV, td->order[n]);
     766       41496 :   for (cx=1, i=1, j=1; cx <= a; cx++, i++)
     767             :   {
     768       37044 :     gel(G,cx) = gel(F, coeff(B,i,j));
     769       37044 :     if (i == d) { i = 0; j++; }
     770             :   }
     771        4452 :   NN = divis(powuu(b, c * (d - d/e)), cut);
     772        4452 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     773        4452 :   N1 = 1000000;
     774        4452 :   NQ = divis_rem(NN,N1,&R1);
     775        4452 :   if (abscmpiu(NQ,100000000)>0)
     776             :   {
     777           0 :     set_avma(avm);
     778           0 :     pari_warn(warner,"Combinatorics too hard: would need %Ps tests!\n"
     779             :                      "I'll skip it but you will get a partial result...",NN);
     780           0 :     return identity_perm(n);
     781             :   }
     782        4452 :   N2 = itos(NQ);
     783        5278 :   for (l2 = 0; l2 <= N2; l2++)
     784             :   {
     785        4452 :     long nbiter = (l2<N2) ? N1: R1;
     786        4452 :     if (DEBUGLEVEL >= 2 && N2) err_printf("%d%% ", muldiv(l2,100,N2));
     787    10129791 :     for (l1 = 0; l1 < nbiter; l1++)
     788             :     {
     789    10128965 :       if (start)
     790             :       {
     791    18125450 :         for (i=1, j=e; i < a;)
     792             :         {
     793    18125450 :           if ((++(x[i])) != b) break;
     794     8000937 :           x[i++] = 0;
     795     8000937 :           if (i == j) { i++; j += e; }
     796             :         }
     797             :       }
     798        4452 :       else { start=1; i = a-1; }
     799             :       /* intheadlong test: overflow in + is OK, we compute mod 2^BIL */
     800    46022529 :       for (p1 = i+1, p5 = p1%d - 1 ; p1 >= 1; p1--, p5--) /* p5 = (p1%d) - 1 */
     801             :       {
     802             :         GEN G1, G6;
     803    35893564 :         ulong V = 0;
     804    35893564 :         if (p5 == - 1) { p5 = d - 1; p6 = p1 + 1 - d; } else p6 = p1 + 1;
     805    35893564 :         G1 = gel(G,p1); G6 = gel(G,p6);
     806    35893564 :         p4 = p5 ? x[p1-1] : 0;
     807   109233243 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     808             :         {
     809    73339679 :           V += umael(W,uel(G6,p3),uel(G1,p2));
     810    73339679 :           p3 += s; if (p3 > b) p3 -= b;
     811             :         }
     812    35893564 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     813    50186822 :         for (p2 = p4; p2 >= 1; p2--)
     814             :         {
     815    14293258 :           V += umael(W,uel(G6,p3),uel(G1,p2));
     816    14293258 :           p3 -= s; if (p3 <= 0) p3 += b;
     817             :         }
     818    35893564 :         uel(ar,p1) = uel(ar,p1+1) + V;
     819             :       }
     820    10128965 :       if (!headlongisint(uel(ar,1),n)) continue;
     821             : 
     822             :       /* intheadlong succeeds. Full computation */
     823     3498831 :       for (p1=1, p5=d; p1 <= a; p1++, p5++)
     824             :       {
     825     3384780 :         if (p5 == d) { p5 = 0; p4 = 0; } else p4 = x[p1-1];
     826     3384780 :         if (p5 == d-1) p6 = p1+1-d; else p6 = p1+1;
     827     9479484 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     828             :         {
     829     6094704 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     830     6094704 :           p3 += s; if (p3 > b) p3 -= b;
     831             :         }
     832     3384780 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     833     4405275 :         for (p2 = p4; p2 >= 1; p2--)
     834             :         {
     835     1020495 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     836     1020495 :           p3 -= s; if (p3 <= 0) p3 += b;
     837             :         }
     838             :       }
     839      114051 :       if (galois_test_perm(td, pf))
     840             :       {
     841        3626 :         if (DEBUGLEVEL >= 1)
     842             :         {
     843           0 :           GEN nb = addis(mulss(l2,N1),l1);
     844           0 :           timer_printf(&ti, "testpermutation(%Ps)", nb);
     845           0 :           if (DEBUGLEVEL >= 2 && hop)
     846           0 :             err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, nb);
     847             :         }
     848        3626 :         set_avma(av); return pf;
     849             :       }
     850      110425 :       hop++;
     851             :     }
     852             :   }
     853         826 :   if (DEBUGLEVEL >= 1)
     854             :   {
     855           0 :     timer_printf(&ti, "testpermutation(%Ps)", NN);
     856           0 :     if (DEBUGLEVEL >= 2 && hop)
     857           0 :       err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, NN);
     858             :   }
     859         826 :   return gc_NULL(avm);
     860             : }
     861             : 
     862             : /* List of subgroups of (Z/mZ)^* whose order divide o, and return the list
     863             :  * of their elements, sorted by increasing order */
     864             : static GEN
     865         553 : listznstarelts(long m, long o)
     866             : {
     867         553 :   pari_sp av = avma;
     868             :   GEN L, zn, zns;
     869             :   long i, phi, ind, l;
     870         553 :   if (m == 2) retmkvec(mkvecsmall(1));
     871         539 :   zn = znstar(stoi(m));
     872         539 :   phi = itos(gel(zn,1));
     873         539 :   o = ugcd(o, phi); /* do we impose this on input ? */
     874         539 :   zns = znstar_small(zn);
     875         539 :   L = cgetg(o+1, t_VEC);
     876        1631 :   for (i=1,ind = phi; ind; ind -= phi/o, i++) /* by *decreasing* exact index */
     877        1092 :     gel(L,i) = subgrouplist(gel(zn,2), mkvec(utoipos(ind)));
     878         539 :   L = shallowconcat1(L); l = lg(L);
     879        1610 :   for (i = 1; i < l; i++) gel(L,i) = znstar_hnf_elts(zns, gel(L,i));
     880         539 :   return gerepilecopy(av, L);
     881             : }
     882             : 
     883             : /* A sympol is a symmetric polynomial
     884             :  *
     885             :  * Currently sympol are couple of t_VECSMALL [v,w]
     886             :  * v[1]...v[k], w[1]...w[k]  represent the polynomial sum(i=1,k,v[i]*s_w[i])
     887             :  * where s_i(X_1,...,X_n) = sum(j=1,n,X_j^i) */
     888             : 
     889             : static GEN
     890        5993 : Flm_newtonsum(GEN M, ulong e, ulong p)
     891             : {
     892        5993 :   long f = lg(M), g = lg(gel(M,1)), i, j;
     893        5993 :   GEN NS = cgetg(f, t_VECSMALL);
     894       33535 :   for(i=1; i<f; i++)
     895             :   {
     896       27542 :     ulong s = 0;
     897       27542 :     GEN Mi = gel(M,i);
     898      118302 :     for(j = 1; j < g; j++)
     899       90760 :       s = Fl_add(s, Fl_powu(uel(Mi,j), e, p), p);
     900       27542 :     uel(NS,i) = s;
     901             :   }
     902        5993 :   return NS;
     903             : }
     904             : 
     905             : static GEN
     906        4137 : Flv_sympol_eval(GEN v, GEN NS, ulong p)
     907             : {
     908        4137 :   pari_sp av = avma;
     909        4137 :   long i, l = lg(v);
     910        4137 :   GEN S = Flv_Fl_mul(gel(NS,1), uel(v,1), p);
     911        4508 :   for (i=2; i<l; i++)
     912         371 :     if (v[i]) S = Flv_add(S, Flv_Fl_mul(gel(NS,i), uel(v,i), p), p);
     913        4137 :   return gerepileuptoleaf(av, S);
     914             : }
     915             : 
     916             : static GEN
     917        4137 : sympol_eval_newtonsum(long e, GEN O, GEN mod)
     918             : {
     919        4137 :   long f = lg(O), g = lg(gel(O,1)), i, j;
     920        4137 :   GEN PL = cgetg(f, t_COL);
     921       24451 :   for(i=1; i<f; i++)
     922             :   {
     923       20314 :     pari_sp av = avma;
     924       20314 :     GEN s = gen_0;
     925       78148 :     for(j=1; j<g; j++) s = addii(s, Fp_powu(gmael(O,i,j), e, mod));
     926       20314 :     gel(PL,i) = gerepileuptoint(av, remii(s,mod));
     927             :   }
     928        4137 :   return PL;
     929             : }
     930             : 
     931             : static GEN
     932        4074 : sympol_eval(GEN sym, GEN O, GEN mod)
     933             : {
     934        4074 :   pari_sp av = avma;
     935             :   long i;
     936        4074 :   GEN v = gel(sym,1), w = gel(sym,2);
     937        4074 :   GEN S = gen_0;
     938        8456 :   for (i=1; i<lg(v); i++)
     939        4382 :     if (v[i]) S = gadd(S, gmulsg(v[i],  sympol_eval_newtonsum(w[i], O, mod)));
     940        4074 :   return gerepileupto(av, S);
     941             : }
     942             : 
     943             : /* Let sigma be an automorphism of L (as a polynomial with rational coefs)
     944             :  * Let 'sym' be a symmetric polynomial defining alpha in L.
     945             :  * We have alpha = sym(x,sigma(x),,,sigma^(g-1)(x)). Compute alpha mod p */
     946             : static GEN
     947        2261 : sympol_aut_evalmod(GEN sym, long g, GEN sigma, GEN Tp, GEN p)
     948             : {
     949        2261 :   pari_sp ltop=avma;
     950        2261 :   long i, j, npows = brent_kung_optpow(degpol(Tp)-1, g-1, 1);
     951        2261 :   GEN s, f, pows, v = zv_to_ZV(gel(sym,1)), w = zv_to_ZV(gel(sym,2));
     952        2261 :   sigma = RgX_to_FpX(sigma, p);
     953        2261 :   pows  = FpXQ_powers(sigma,npows,Tp,p);
     954        2261 :   f = pol_x(varn(sigma));
     955        2261 :   s = pol_0(varn(sigma));
     956        9219 :   for(i=1; i<=g;i++)
     957             :   {
     958        6958 :     if (i > 1) f = FpX_FpXQV_eval(f,pows,Tp,p);
     959       14294 :     for(j=1; j<lg(v); j++)
     960        7336 :       s = FpX_add(s, FpX_Fp_mul(FpXQ_pow(f,gel(w,j),Tp,p),gel(v,j),p),p);
     961             :   }
     962        2261 :   return gerepileupto(ltop, s);
     963             : }
     964             : 
     965             : /* Let Sp be as computed with sympol_aut_evalmod
     966             :  * Let Tmod be the factorisation of T mod p.
     967             :  * Return the factorisation of the minimal polynomial of S mod p w.r.t. Tmod */
     968             : static GEN
     969        2261 : fixedfieldfactmod(GEN Sp, GEN p, GEN Tmod)
     970             : {
     971        2261 :   long i, l = lg(Tmod);
     972        2261 :   GEN F = cgetg(l,t_VEC);
     973        8638 :   for(i=1; i<l; i++)
     974             :   {
     975        6377 :     GEN Ti = gel(Tmod,i);
     976        6377 :     gel(F,i) = FpXQ_minpoly(FpX_rem(Sp,Ti,p), Ti,p);
     977             :   }
     978        2261 :   return F;
     979             : }
     980             : 
     981             : static GEN
     982        4074 : fixedfieldsurmer(ulong l, GEN NS, GEN W)
     983             : {
     984        4074 :   const long step=3;
     985        4074 :   long i, j, n = lg(W)-1, m = 1L<<((n-1)<<1);
     986        4074 :   GEN sym = cgetg(n+1,t_VECSMALL);
     987        4382 :   for (j=1;j<n;j++) sym[j] = step;
     988        4074 :   sym[n] = 0;
     989        4074 :   if (DEBUGLEVEL>=4) err_printf("FixedField: Weight: %Ps\n",W);
     990        4137 :   for (i=0; i<m; i++)
     991             :   {
     992        4137 :     pari_sp av = avma;
     993             :     GEN L;
     994        4445 :     for (j=1; sym[j]==step; j++) sym[j]=0;
     995        4137 :     sym[j]++;
     996        4137 :     if (DEBUGLEVEL>=6) err_printf("FixedField: Sym: %Ps\n",sym);
     997        4137 :     L = Flv_sympol_eval(sym, NS, l);
     998        4137 :     if (!vecsmall_is1to1(L)) { set_avma(av); continue; }
     999        4074 :     return mkvec2(sym,W);
    1000             :   }
    1001           0 :   return NULL;
    1002             : }
    1003             : 
    1004             : /*Check whether the line of NS are pair-wise distinct.*/
    1005             : static long
    1006        4382 : sympol_is1to1_lg(GEN NS, long n)
    1007             : {
    1008        4382 :   long i, j, k, l = lgcols(NS);
    1009       23429 :   for (i=1; i<l; i++)
    1010      105875 :     for(j=i+1; j<l; j++)
    1011             :     {
    1012       89607 :       for(k=1; k<n; k++)
    1013       89299 :         if (mael(NS,k,j)!=mael(NS,k,i)) break;
    1014       86828 :       if (k>=n) return 0;
    1015             :     }
    1016        4074 :   return 1;
    1017             : }
    1018             : 
    1019             : /* Let O a set of orbits of roots (see fixedfieldorbits) modulo mod,
    1020             :  * l | mod and p two prime numbers. Return a vector [sym,s,P] where:
    1021             :  * sym is a sympol, s is the set of images of sym on O and
    1022             :  * P is the polynomial with roots s. */
    1023             : static GEN
    1024        4074 : fixedfieldsympol(GEN O, ulong l)
    1025             : {
    1026        4074 :   pari_sp ltop=avma;
    1027        4074 :   const long n=(BITS_IN_LONG>>1)-1;
    1028        4074 :   GEN NS = cgetg(n+1,t_MAT), sym = NULL, W = cgetg(n+1,t_VECSMALL);
    1029        4074 :   long i, e=1;
    1030        4074 :   if (DEBUGLEVEL>=4)
    1031           0 :     err_printf("FixedField: Size: %ldx%ld\n",lg(O)-1,lg(gel(O,1))-1);
    1032        4074 :   O = ZM_to_Flm(O,l);
    1033        8456 :   for (i=1; !sym && i<=n; i++)
    1034             :   {
    1035        4382 :     GEN L = Flm_newtonsum(O, e++, l);
    1036        4382 :     if (lg(O)>2)
    1037        5902 :       while (vecsmall_isconst(L)) L = Flm_newtonsum(O, e++, l);
    1038        4382 :     W[i] = e-1; gel(NS,i) = L;
    1039        4382 :     if (sympol_is1to1_lg(NS,i+1))
    1040        4074 :       sym = fixedfieldsurmer(l,NS,vecsmall_shorten(W,i));
    1041             :   }
    1042        4074 :   if (!sym) pari_err_BUG("fixedfieldsympol [p too small]");
    1043        4074 :   if (DEBUGLEVEL>=2) err_printf("FixedField: Found: %Ps\n",gel(sym,1));
    1044        4074 :   return gerepilecopy(ltop,sym);
    1045             : }
    1046             : 
    1047             : /* Let O a set of orbits as indices and L the corresponding roots.
    1048             :  * Return the set of orbits as roots. */
    1049             : static GEN
    1050        4074 : fixedfieldorbits(GEN O, GEN L)
    1051             : {
    1052        4074 :   GEN S = cgetg(lg(O), t_MAT);
    1053             :   long i;
    1054       23072 :   for (i = 1; i < lg(O); i++) gel(S,i) = vecpermute(L, gel(O,i));
    1055        4074 :   return S;
    1056             : }
    1057             : 
    1058             : static GEN
    1059         910 : fixedfieldinclusion(GEN O, GEN PL)
    1060             : {
    1061         910 :   long i, j, f = lg(O)-1, g = lg(gel(O,1))-1;
    1062         910 :   GEN S = cgetg(f*g + 1, t_COL);
    1063        6342 :   for (i = 1; i <= f; i++)
    1064             :   {
    1065        5432 :     GEN Oi = gel(O,i);
    1066       23142 :     for (j = 1; j <= g; j++) gel(S, Oi[j]) = gel(PL, i);
    1067             :   }
    1068         910 :   return S;
    1069             : }
    1070             : 
    1071             : /* Polynomial attached to a vector of conjugates. Not stack clean */
    1072             : static GEN
    1073       21140 : vectopol(GEN v, GEN M, GEN den , GEN mod, GEN mod2, long x)
    1074             : {
    1075       21140 :   long l = lg(v)+1, i;
    1076       21140 :   GEN z = cgetg(l,t_POL);
    1077       21140 :   z[1] = evalsigne(1)|evalvarn(x);
    1078      258454 :   for (i=2; i<l; i++)
    1079      237314 :     gel(z,i) = gdiv(centermodii(ZMrow_ZC_mul(M,v,i-1), mod, mod2), den);
    1080       21140 :   return normalizepol_lg(z, l);
    1081             : }
    1082             : 
    1083             : /* Polynomial associate to a permutation of the roots. Not stack clean */
    1084             : static GEN
    1085       19754 : permtopol(GEN p, GEN L, GEN M, GEN den, GEN mod, GEN mod2, long x)
    1086             : {
    1087       19754 :   if (lg(p) != lg(L)) pari_err_TYPE("permtopol [permutation]", p);
    1088       19754 :   return vectopol(vecpermute(L,p), M, den, mod, mod2, x);
    1089             : }
    1090             : 
    1091             : static GEN
    1092        1218 : galoisvecpermtopol(GEN gal, GEN vec, GEN mod, GEN mod2)
    1093             : {
    1094        1218 :   long i, l = lg(vec);
    1095        1218 :   long v = varn(gal_get_pol(gal));
    1096        1218 :   GEN L = gal_get_roots(gal);
    1097        1218 :   GEN M = gal_get_invvdm(gal);
    1098        1218 :   GEN P = cgetg(l, t_MAT);
    1099        6993 :   for (i=1; i<l; i++)
    1100        5775 :     gel(P, i) = vecpermute(L,gel(vec,i));
    1101        1218 :   P = RgM_to_RgXV(FpM_center(FpM_mul(M, P, mod), mod, mod2), v);
    1102        1218 :   return gdiv(P, gal_get_den(gal));
    1103             : }
    1104             : 
    1105             : static void
    1106        2128 : notgalois(long p, struct galois_analysis *ga)
    1107             : {
    1108        2128 :   if (DEBUGLEVEL >= 2) err_printf("GaloisAnalysis:non Galois for p=%ld\n", p);
    1109        2128 :   ga->p = p;
    1110        2128 :   ga->deg = 0;
    1111        2128 : }
    1112             : 
    1113             : /*Gather information about the group*/
    1114             : static long
    1115        5453 : init_group(long n, long np, GEN Fp, GEN Fe, long *porder)
    1116             : {
    1117        5453 :   const long prim_nonwss_orders[] = { 48,56,60,72,75,80,196,200,216 };
    1118        5453 :   long i, phi_order = 1, order = 1, group = 0;
    1119             :   ulong p;
    1120             : 
    1121             :  /* non-WSS groups of this order? */
    1122       54341 :   for (i=0; i < (long)numberof(prim_nonwss_orders); i++)
    1123       48909 :     if (n % prim_nonwss_orders[i] == 0) { group |= ga_non_wss; break; }
    1124        5453 :   if (np == 2 && Fp[2] == 3 && Fe[2] == 1 && Fe[1] > 2) group |= ga_ext_2;
    1125             : 
    1126        8169 :   for (i = np; i > 0; i--)
    1127             :   {
    1128        6629 :     long p = Fp[i];
    1129        6629 :     if (phi_order % p == 0) { group |= ga_all_normal; break; }
    1130        5502 :     order *= p; phi_order *= p-1;
    1131        5502 :     if (Fe[i] > 1) break;
    1132             :   }
    1133        5453 :   if (uisprimepower(n, &p) || n == 135) group |= ga_all_nilpotent;
    1134        5453 :   if (n <= 104) group |= ga_easy; /* no need to use polynomial algo */
    1135        5453 :   *porder = order; return group;
    1136             : }
    1137             : 
    1138             : /*is a "better" than b ? (if so, update karma) */
    1139             : static int
    1140       24045 : improves(long a, long b, long plift, long p, long n, long *karma)
    1141             : {
    1142       24045 :   if (!plift || a > b) { *karma = ugcd(p-1,n); return 1; }
    1143       22617 :   if (a == b) {
    1144       20167 :     long k = ugcd(p-1,n);
    1145       20167 :     if (k > *karma) { *karma = k; return 1; }
    1146             :   }
    1147       20454 :   return 0; /* worse */
    1148             : }
    1149             : 
    1150             : /* return 0 if not galois or not wss */
    1151             : static int
    1152        5453 : galoisanalysis(GEN T, struct galois_analysis *ga, long calcul_l, GEN bad)
    1153             : {
    1154        5453 :   pari_sp ltop = avma, av;
    1155        5453 :   long group, linf, n, p, i, karma = 0;
    1156             :   GEN F, Fp, Fe, Fpe, O;
    1157             :   long np, order, plift, nbmax, nbtest, deg;
    1158             :   pari_timer ti;
    1159             :   forprime_t S;
    1160        5453 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    1161        5453 :   n = degpol(T);
    1162        5453 :   O = zero_zv(n);
    1163        5453 :   F = factoru_pow(n);
    1164        5453 :   Fp = gel(F,1); np = lg(Fp)-1;
    1165        5453 :   Fe = gel(F,2);
    1166        5453 :   Fpe= gel(F,3);
    1167        5453 :   group = init_group(n, np, Fp, Fe, &order);
    1168             : 
    1169             :   /*Now we study the orders of the Frobenius elements*/
    1170        5453 :   deg = Fp[np]; /* largest prime | n */
    1171        5453 :   plift = 0;
    1172        5453 :   nbtest = 0;
    1173        5453 :   nbmax = 8+(n>>1);
    1174        5453 :   u_forprime_init(&S, n*maxss(expu(n)-3, 2), ULONG_MAX);
    1175        5453 :   av = avma;
    1176       48020 :   while (!plift || (nbtest < nbmax && (nbtest <=8 || order < (n>>1)))
    1177        3346 :                 || ((n == 24 || n==36) && O[6] == 0 && O[4] == 0)
    1178        3346 :                 || ((group&ga_non_wss) && order == Fp[np]))
    1179             :   {
    1180       44674 :     long d, o, norm_o = 1;
    1181             :     GEN D, Tp;
    1182             : 
    1183       44674 :     if ((group&ga_non_wss) && nbtest >= 3*nbmax) break; /* in all cases */
    1184       44674 :     nbtest++; set_avma(av);
    1185       44674 :     p = u_forprime_next(&S);
    1186       44674 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1187       47488 :     if (bad && dvdiu(bad, p)) continue;
    1188       44674 :     Tp = ZX_to_Flx(T,p);
    1189       44674 :     if (!Flx_is_squarefree(Tp,p)) { if (!--nbtest) nbtest = 1; continue; }
    1190             : 
    1191       41860 :     D = Flx_nbfact_by_degree(Tp, &d, p);
    1192       41860 :     o = n / d; /* d factors, all should have degree o */
    1193       41860 :     if (D[o] != d) { notgalois(p, ga); return gc_bool(ltop,0); }
    1194             : 
    1195       39753 :     if (!O[o]) O[o] = p;
    1196       39753 :     if (o % deg) goto ga_end; /* NB: deg > 1 */
    1197       28091 :     if ((group&ga_all_normal) && o < order) goto ga_end;
    1198             : 
    1199             :     /*Frob_p has order o > 1, find a power which generates a normal subgroup*/
    1200       27937 :     if (o * Fp[1] >= n)
    1201       17003 :       norm_o = o; /*subgroups of smallest index are normal*/
    1202             :     else
    1203             :     {
    1204       12348 :       for (i = np; i > 0; i--)
    1205             :       {
    1206       12348 :         if (o % Fpe[i]) break;
    1207        1414 :         norm_o *= Fpe[i];
    1208             :       }
    1209             :     }
    1210             :     /* Frob_p^(o/norm_o) generates a normal subgroup of order norm_o */
    1211       27937 :     if (norm_o != 1)
    1212             :     {
    1213       18417 :       if (!(group&ga_all_normal) || o > order)
    1214        3892 :         karma = ugcd(p-1,n);
    1215       14525 :       else if (!improves(norm_o, deg, plift,p,n, &karma)) goto ga_end;
    1216             :       /* karma0=0, deg0<=norm_o -> the first improves() returns 1 */
    1217        6188 :       deg = norm_o; group |= ga_all_normal; /* STORE */
    1218             :     }
    1219        9520 :     else if (group&ga_all_normal) goto ga_end;
    1220        9520 :     else if (!improves(o, order, plift,p,n, &karma)) goto ga_end;
    1221             : 
    1222        7483 :     order = o; plift = p; /* STORE */
    1223       39753 :     ga_end:
    1224       39753 :     if (DEBUGLEVEL >= 5)
    1225           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);
    1226             :   }
    1227             :   /* To avoid looping on non-wss group.
    1228             :    * TODO: check for large groups. Would it be better to disable this check if
    1229             :    * we are in a good case (ga_all_normal && !(ga_ext_2) (e.g. 60)) ?*/
    1230        3346 :   ga->p = plift;
    1231        3346 :   if (!plift || ((group&ga_non_wss) && order == Fp[np]))
    1232             :   {
    1233           0 :     if (DEBUGLEVEL)
    1234           0 :       pari_warn(warner,"Galois group probably not weakly super solvable");
    1235           0 :     return 0;
    1236             :   }
    1237        3346 :   linf = 2*n*usqrt(n);
    1238        3346 :   if (calcul_l && O[1] <= linf)
    1239             :   {
    1240             :     pari_sp av2;
    1241             :     forprime_t S2;
    1242             :     ulong p;
    1243        1533 :     u_forprime_init(&S2, linf+1,ULONG_MAX);
    1244        1533 :     av2 = avma;
    1245       42077 :     while ((p = u_forprime_next(&S2)))
    1246             :     { /*find a totally split prime l > linf*/
    1247       42077 :       GEN Tp = ZX_to_Flx(T, p);
    1248       42077 :       long nb = Flx_nbroots(Tp, p);
    1249       42077 :       if (nb == n) { O[1] = p; break; }
    1250       40565 :       if (nb && Flx_is_squarefree(Tp,p)) { notgalois(p,ga); return gc_bool(ltop,0); }
    1251       40544 :       set_avma(av2);
    1252             :     }
    1253        1512 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1254             :   }
    1255        3325 :   ga->group = group;
    1256        3325 :   ga->deg = deg;
    1257        3325 :   ga->ord = order;
    1258        3325 :   ga->l  = O[1];
    1259        3325 :   ga->p4 = n >= 4 ? O[4] : 0;
    1260        3325 :   if (DEBUGLEVEL >= 4)
    1261           0 :     err_printf("GaloisAnalysis:p=%ld l=%ld group=%ld deg=%ld ord=%ld\n",
    1262           0 :                plift, O[1], group, deg, order);
    1263        3325 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisanalysis()");
    1264        3325 :   return gc_bool(ltop,1);
    1265             : }
    1266             : 
    1267             : static GEN
    1268          56 : a4galoisgen(struct galois_test *td)
    1269             : {
    1270          56 :   const long n = 12;
    1271          56 :   pari_sp ltop = avma, av, av2;
    1272          56 :   long i, j, k, N, hop = 0;
    1273             :   GEN MT, O,O1,O2,O3, ar, mt, t, u, res, orb, pft, pfu, pfv;
    1274             : 
    1275          56 :   res = cgetg(3, t_VEC);
    1276          56 :   pft = cgetg(n+1, t_VECSMALL);
    1277          56 :   pfu = cgetg(n+1, t_VECSMALL);
    1278          56 :   pfv = cgetg(n+1, t_VECSMALL);
    1279          56 :   gel(res,1) = mkvec3(pft,pfu,pfv);
    1280          56 :   gel(res,2) = mkvecsmall3(2,2,3);
    1281          56 :   av = avma;
    1282          56 :   ar = cgetg(5, t_VECSMALL);
    1283          56 :   mt = gel(td->PV, td->order[n]);
    1284          56 :   t = identity_perm(n) + 1; /* Sorry for this hack */
    1285          56 :   u = cgetg(n+1, t_VECSMALL) + 1; /* too lazy to correct */
    1286          56 :   MT = cgetg(n+1, t_MAT);
    1287         728 :   for (j = 1; j <= n; j++) gel(MT,j) = cgetg(n+1, t_VECSMALL);
    1288         728 :   for (j = 1; j <= n; j++)
    1289        4368 :     for (i = 1; i < j; i++)
    1290        3696 :       ucoeff(MT,i,j) = ucoeff(MT,j,i) = ucoeff(mt,i,j)+ucoeff(mt,j,i);
    1291             :   /* MT(i,i) unused */
    1292             : 
    1293          56 :   av2 = avma;
    1294             :   /* N = itos(gdiv(mpfact(n), mpfact(n >> 1))) >> (n >> 1); */
    1295             :   /* n = 2k = 12; N = (2k)! / (k! * 2^k) = 10395 */
    1296          56 :   N = 10395;
    1297          56 :   if (DEBUGLEVEL>=4) err_printf("A4GaloisConj: will test %ld permutations\n", N);
    1298          56 :   uel(ar,4) = umael(MT,11,12);
    1299          56 :   uel(ar,3) = uel(ar,4) + umael(MT,9,10);
    1300          56 :   uel(ar,2) = uel(ar,3) + umael(MT,7,8);
    1301          56 :   uel(ar,1) = uel(ar,2) + umael(MT,5,6);
    1302      125711 :   for (i = 0; i < N; i++)
    1303             :   {
    1304             :     long g;
    1305      125711 :     if (i)
    1306             :     {
    1307      125655 :       long a, x = i, y = 1;
    1308      177128 :       do { y += 2; a = x%y; x = x/y; } while (!a);
    1309      125655 :       switch (y)
    1310             :       {
    1311       83796 :       case 3:
    1312       83796 :         lswap(t[2], t[2-a]);
    1313       83796 :         break;
    1314       33501 :       case 5:
    1315       33501 :         x = t[0]; t[0] = t[2]; t[2] = t[1]; t[1] = x;
    1316       33501 :         lswap(t[4], t[4-a]);
    1317       33501 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1318       33501 :         break;
    1319        7195 :       case 7:
    1320        7195 :         x = t[0]; t[0] = t[4]; t[4] = t[3]; t[3] = t[1]; t[1] = t[2]; t[2] = x;
    1321        7195 :         lswap(t[6], t[6-a]);
    1322        7195 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1323        7195 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1324        7195 :         break;
    1325        1070 :       case 9:
    1326        1070 :         x = t[0]; t[0] = t[6]; t[6] = t[5]; t[5] = t[3]; t[3] = x;
    1327        1070 :         lswap(t[1], t[4]);
    1328        1070 :         lswap(t[8], t[8-a]);
    1329        1070 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1330        1070 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1331        1070 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1332        1070 :         break;
    1333          93 :       case 11:
    1334          93 :         x = t[0]; t[0] = t[8]; t[8] = t[7]; t[7] = t[5]; t[5] = t[1];
    1335          93 :         t[1] = t[6]; t[6] = t[3]; t[3] = t[2]; t[2] = t[4]; t[4] = x;
    1336          93 :         lswap(t[10], t[10-a]);
    1337          93 :         uel(ar,4) = umael(MT,t[10],t[11]);
    1338          93 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1339          93 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1340          93 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1341             :       }
    1342          56 :     }
    1343      125711 :     g = uel(ar,1)+umael(MT,t[0],t[1])+umael(MT,t[2],t[3]);
    1344      125711 :     if (headlongisint(g,n))
    1345             :     {
    1346         392 :       for (k = 0; k < n; k += 2)
    1347             :       {
    1348         336 :         pft[t[k]] = t[k+1];
    1349         336 :         pft[t[k+1]] = t[k];
    1350             :       }
    1351          56 :       if (galois_test_perm(td, pft)) break;
    1352           0 :       hop++;
    1353             :     }
    1354      125655 :     set_avma(av2);
    1355             :   }
    1356          56 :   if (DEBUGLEVEL >= 1 && hop)
    1357           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1358          56 :   if (i == N) return gc_NULL(ltop);
    1359             :   /* N = itos(gdiv(mpfact(n >> 1), mpfact(n >> 2))) >> 1; */
    1360          56 :   N = 60;
    1361          56 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: sigma=%Ps \n", pft);
    1362         224 :   for (k = 0; k < n; k += 4)
    1363             :   {
    1364         168 :     u[k+3] = t[k+3];
    1365         168 :     u[k+2] = t[k+1];
    1366         168 :     u[k+1] = t[k+2];
    1367         168 :     u[k]   = t[k];
    1368             :   }
    1369        2592 :   for (i = 0; i < N; i++)
    1370             :   {
    1371        2592 :     ulong g = 0;
    1372        2592 :     if (i)
    1373             :     {
    1374        2536 :       long a, x = i, y = -2;
    1375        3992 :       do { y += 4; a = x%y; x = x/y; } while (!a);
    1376        2536 :       lswap(u[0],u[2]);
    1377        2536 :       switch (y)
    1378             :       {
    1379        1268 :       case 2:
    1380        1268 :         break;
    1381        1080 :       case 6:
    1382        1080 :         lswap(u[4],u[6]);
    1383        1080 :         if (!(a & 1))
    1384             :         {
    1385         439 :           a = 4 - (a>>1);
    1386         439 :           lswap(u[6], u[a]);
    1387         439 :           lswap(u[4], u[a-2]);
    1388             :         }
    1389        1080 :         break;
    1390         188 :       case 10:
    1391         188 :         x = u[6];
    1392         188 :         u[6] = u[3];
    1393         188 :         u[3] = u[2];
    1394         188 :         u[2] = u[4];
    1395         188 :         u[4] = u[1];
    1396         188 :         u[1] = u[0];
    1397         188 :         u[0] = x;
    1398         188 :         if (a >= 3) a += 2;
    1399         188 :         a = 8 - a;
    1400         188 :         lswap(u[10],u[a]);
    1401         188 :         lswap(u[8], u[a-2]);
    1402         188 :         break;
    1403             :       }
    1404          56 :     }
    1405       18144 :     for (k = 0; k < n; k += 2) g += mael(MT,u[k],u[k+1]);
    1406        2592 :     if (headlongisint(g,n))
    1407             :     {
    1408         392 :       for (k = 0; k < n; k += 2)
    1409             :       {
    1410         336 :         pfu[u[k]] = u[k+1];
    1411         336 :         pfu[u[k+1]] = u[k];
    1412             :       }
    1413          56 :       if (galois_test_perm(td, pfu)) break;
    1414           0 :       hop++;
    1415             :     }
    1416        2536 :     set_avma(av2);
    1417             :   }
    1418          56 :   if (i == N) return gc_NULL(ltop);
    1419          56 :   if (DEBUGLEVEL >= 1 && hop)
    1420           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1421          56 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: tau=%Ps \n", pfu);
    1422          56 :   set_avma(av2);
    1423          56 :   orb = mkvec2(pft,pfu);
    1424          56 :   O = vecperm_orbits(orb, 12);
    1425          56 :   if (DEBUGLEVEL >= 4) {
    1426           0 :     err_printf("A4GaloisConj: orb=%Ps\n", orb);
    1427           0 :     err_printf("A4GaloisConj: O=%Ps \n", O);
    1428             :   }
    1429          56 :   av2 = avma;
    1430          56 :   O1 = gel(O,1); O2 = gel(O,2); O3 = gel(O,3);
    1431          77 :   for (j = 0; j < 2; j++)
    1432             :   {
    1433          77 :     pfv[O1[1]] = O2[1];
    1434          77 :     pfv[O1[2]] = O2[3+j];
    1435          77 :     pfv[O1[3]] = O2[4 - (j << 1)];
    1436          77 :     pfv[O1[4]] = O2[2+j];
    1437         245 :     for (i = 0; i < 4; i++)
    1438             :     {
    1439         224 :       ulong g = 0;
    1440         224 :       switch (i)
    1441             :       {
    1442          77 :       case 0: break;
    1443          70 :       case 1: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1444          56 :       case 2: lswap(O3[1], O3[4]); lswap(O3[2], O3[3]); break;
    1445          21 :       case 3: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1446             :       }
    1447         224 :       pfv[O2[1]]          = O3[1];
    1448         224 :       pfv[O2[3+j]]        = O3[4-j];
    1449         224 :       pfv[O2[4 - (j<<1)]] = O3[2 + (j<<1)];
    1450         224 :       pfv[O2[2+j]]        = O3[3-j];
    1451         224 :       pfv[O3[1]]          = O1[1];
    1452         224 :       pfv[O3[4-j]]        = O1[2];
    1453         224 :       pfv[O3[2 + (j<<1)]] = O1[3];
    1454         224 :       pfv[O3[3-j]]        = O1[4];
    1455        2912 :       for (k = 1; k <= n; k++) g += mael(mt,k,pfv[k]);
    1456         224 :       if (headlongisint(g,n) && galois_test_perm(td, pfv))
    1457             :       {
    1458          56 :         set_avma(av);
    1459          56 :         if (DEBUGLEVEL >= 1)
    1460           0 :           err_printf("A4GaloisConj: %ld hop over %d iterations max\n",
    1461             :                      hop, 10395 + 68);
    1462          56 :         return res;
    1463             :       }
    1464         168 :       hop++; set_avma(av2);
    1465             :     }
    1466             :   }
    1467           0 :   return gc_NULL(ltop);
    1468             : }
    1469             : 
    1470             : /* S4 */
    1471             : static GEN
    1472        1288 : s4makelift(GEN u, struct galois_lift *gl)
    1473        1288 : { return FpXQ_powers(u, degpol(gl->T)-1, gl->TQ, gl->Q); }
    1474             : 
    1475             : static long
    1476      237817 : s4test(GEN u, GEN liftpow, struct galois_lift *gl, GEN phi)
    1477             : {
    1478      237817 :   pari_sp av = avma;
    1479             :   GEN res, Q, Q2;
    1480      237817 :   long bl, i, d = lg(u)-2;
    1481             :   pari_timer ti;
    1482      237817 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
    1483      237817 :   if (!d) return 0;
    1484      237817 :   Q = gl->Q; Q2 = shifti(Q,-1);
    1485      237817 :   res = gel(u,2);
    1486     8493728 :   for (i = 2; i <= d; i++)
    1487     8255911 :     if (lg(gel(liftpow,i))>2)
    1488     8255911 :       res = addii(res, mulii(gmael(liftpow,i,2), gel(u,i+1)));
    1489      237817 :   res = remii(res,Q);
    1490      237817 :   if (gl->den != gen_1) res = mulii(res, gl->den);
    1491      237817 :   res = centermodii(res, Q,Q2);
    1492      237817 :   if (abscmpii(res, gl->gb->bornesol) > 0) return gc_long(av,0);
    1493        3319 :   res = scalar_ZX_shallow(gel(u,2),varn(u));
    1494      113780 :   for (i = 2; i <= d ; i++)
    1495      110461 :     if (lg(gel(liftpow,i))>2)
    1496      110461 :       res = ZX_add(res, ZX_Z_mul(gel(liftpow,i), gel(u,i+1)));
    1497        3319 :   res = FpX_red(res, Q);
    1498        3319 :   if (gl->den != gen_1) res = FpX_Fp_mul(res, gl->den, Q);
    1499        3319 :   res = FpX_center_i(res, Q, shifti(Q,-1));
    1500        3319 :   bl = poltopermtest(res, gl, phi);
    1501        3319 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "s4test()");
    1502        3319 :   return gc_long(av,bl);
    1503             : }
    1504             : 
    1505             : static GEN
    1506         343 : s4releveauto(GEN M, GEN B, GEN T, GEN p,long a1,long a2,long a3,long a4,long a5,long a6)
    1507             : {
    1508         343 :   GEN       F = ZX_mul(gel(M,a1),gel(B,a2));
    1509         343 :   F = ZX_add(F, ZX_mul(gel(M,a2),gel(B,a1)));
    1510         343 :   F = ZX_add(F, ZX_mul(gel(M,a3),gel(B,a4)));
    1511         343 :   F = ZX_add(F, ZX_mul(gel(M,a4),gel(B,a3)));
    1512         343 :   F = ZX_add(F, ZX_mul(gel(M,a5),gel(B,a6)));
    1513         343 :   F = ZX_add(F, ZX_mul(gel(M,a6),gel(B,a5)));
    1514         343 :   return FpXQ_red(F, T, p);
    1515             : }
    1516             : 
    1517             : static GEN
    1518      316752 : lincomb(GEN B, long a, long b, long j)
    1519             : {
    1520      316752 :   long k = (-j) & 3;
    1521      316752 :   return ZX_add(gmael(B,a,j+1), gmael(B,b,k+1));
    1522             : }
    1523             : 
    1524             : static GEN
    1525          63 : FpXV_ffisom(GEN V, GEN p)
    1526             : {
    1527          63 :   pari_sp av = avma;
    1528          63 :   long i, j, l = lg(V);
    1529          63 :   GEN S = cgetg(l, t_VEC), Si = cgetg(l, t_VEC), M;
    1530         483 :   for (i = 1; i < l; i++)
    1531             :   {
    1532         420 :     gel(S,i) = FpX_ffisom(gel(V,1), gel(V,i), p);
    1533         420 :     gel(Si,i) = FpXQ_ffisom_inv(gel(S,i), gel(V,i), p);
    1534             :   }
    1535          63 :   M = cgetg(l, t_MAT);
    1536         483 :   for (j = 1; j < l; j++)
    1537         420 :     gel(M,j) = FpXC_FpXQ_eval(Si, gel(S,j), gel(V,j), p);
    1538          63 :   return gerepileupto(av, M);
    1539             : }
    1540             : 
    1541             : static GEN
    1542          63 : mkliftpow(GEN x, GEN T, GEN p, struct galois_lift *gl)
    1543         483 : { pari_APPLY_same(automorphismlift(FpXV_chinese(gel(x,i), T, p, NULL), gl)) }
    1544             : 
    1545             : #define rot3(x,y,z) {long _t=x; x=y; y=z; z=_t;}
    1546             : #define rot4(x,y,z,t) {long _u=x; x=y; y=z; z=t; t=_u;}
    1547             : 
    1548             : /* FIXME: could use the intheadlong technique */
    1549             : static GEN
    1550          49 : s4galoisgen(struct galois_lift *gl)
    1551             : {
    1552          49 :   const long n = 24;
    1553             :   struct galois_testlift gt;
    1554          49 :   pari_sp av, ltop2, ltop = avma;
    1555             :   long i, j;
    1556          49 :   GEN sigma, tau, phi, res, r1,r2,r3,r4, pj, p = gl->p, Q = gl->Q, TQ = gl->TQ;
    1557             :   GEN sg, Tp, Tmod, misom, B, Bcoeff, liftpow, liftp, aut;
    1558             : 
    1559          49 :   res = cgetg(3, t_VEC);
    1560          49 :   r1 = cgetg(n+1, t_VECSMALL);
    1561          49 :   r2 = cgetg(n+1, t_VECSMALL);
    1562          49 :   r3 = cgetg(n+1, t_VECSMALL);
    1563          49 :   r4 = cgetg(n+1, t_VECSMALL);
    1564          49 :   gel(res,1)= mkvec4(r1,r2,r3,r4);
    1565          49 :   gel(res,2) = mkvecsmall4(2,2,3,2);
    1566          49 :   ltop2 = avma;
    1567          49 :   sg = identity_perm(6);
    1568          49 :   pj = zero_zv(6);
    1569          49 :   sigma = cgetg(n+1, t_VECSMALL);
    1570          49 :   tau = cgetg(n+1, t_VECSMALL);
    1571          49 :   phi = cgetg(n+1, t_VECSMALL);
    1572          49 :   Tp = FpX_red(gl->T,p);
    1573          49 :   Tmod = gel(FpX_factor(Tp,p), 1);
    1574          49 :   misom = FpXV_ffisom(Tmod, p);
    1575          49 :   aut = galoisdolift(gl);
    1576          49 :   inittestlift(aut, Tmod, gl, &gt);
    1577          49 :   B = FqC_FqV_mul(gt.pauto, gt.bezoutcoeff, gl->TQ, Q);
    1578          49 :   Bcoeff = gt.bezoutcoeff;
    1579          49 :   liftp = mkliftpow(shallowtrans(misom), Tmod, p, gl);
    1580          49 :   av = avma;
    1581          84 :   for (i = 0; i < 3; i++, set_avma(av))
    1582             :   {
    1583             :     pari_sp av1, av2, av3;
    1584             :     GEN u, u1, u2, u3;
    1585             :     long j1, j2, j3;
    1586          84 :     if (i)
    1587             :     {
    1588          35 :       if (i == 1) { lswap(sg[2],sg[3]); }
    1589           0 :       else        { lswap(sg[1],sg[3]); }
    1590             :     }
    1591          84 :     u = s4releveauto(liftp,Bcoeff,TQ,Q,sg[1],sg[2],sg[3],sg[4],sg[5],sg[6]);
    1592          84 :     liftpow = s4makelift(u, gl);
    1593          84 :     av1 = avma;
    1594         279 :     for (j1 = 0; j1 < 4; j1++, set_avma(av1))
    1595             :     {
    1596         244 :       u1 = lincomb(B,sg[5],sg[6],j1);
    1597         244 :       av2 = avma;
    1598        1107 :       for (j2 = 0; j2 < 4; j2++, set_avma(av2))
    1599             :       {
    1600         912 :         u2 = lincomb(B,sg[3],sg[4],j2);
    1601         912 :         u2 = FpX_add(u1, u2, Q); av3 = avma;
    1602        4440 :         for (j3 = 0; j3 < 4; j3++, set_avma(av3))
    1603             :         {
    1604        3577 :           u3 = lincomb(B,sg[1],sg[2],j3);
    1605        3577 :           u3 = FpX_add(u2, u3, Q);
    1606        3577 :           if (DEBUGLEVEL >= 4)
    1607           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/4:%d/4:%d/4:%Ps\n",
    1608             :                        i,j1,j2,j3, sg);
    1609        3577 :           if (s4test(u3, liftpow, gl, sigma))
    1610             :           {
    1611          49 :             pj[1] = j3;
    1612          49 :             pj[2] = j2;
    1613          49 :             pj[3] = j1; goto suites4;
    1614             :           }
    1615             :         }
    1616             :       }
    1617             :     }
    1618             :   }
    1619           0 :   return gc_NULL(ltop);
    1620          49 : suites4:
    1621          49 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: sigma=%Ps\n", sigma);
    1622          49 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: pj=%Ps\n", pj);
    1623          49 :   set_avma(av);
    1624         112 :   for (j = 1; j <= 3; j++)
    1625             :   {
    1626             :     pari_sp av2, av3;
    1627             :     GEN u;
    1628             :     long w, l;
    1629         112 :     rot3(sg[1], sg[3], sg[5])
    1630         112 :     rot3(sg[2], sg[4], sg[6])
    1631         112 :     rot3(pj[1], pj[2], pj[3])
    1632         273 :     for (l = 0; l < 2; l++, set_avma(av))
    1633             :     {
    1634         210 :       u = s4releveauto(liftp,Bcoeff,TQ,Q,sg[1],sg[3],sg[2],sg[4],sg[5],sg[6]);
    1635         210 :       liftpow = s4makelift(u, gl);
    1636         210 :       av2 = avma;
    1637         581 :       for (w = 0; w < 4; w += 2, set_avma(av2))
    1638             :       {
    1639             :         GEN uu;
    1640         420 :         pj[6] = (w + pj[3]) & 3;
    1641         420 :         uu = lincomb(B, sg[5], sg[6], pj[6]);
    1642         420 :         uu = FpX_red(uu, Q);
    1643         420 :         av3 = avma;
    1644        1963 :         for (i = 0; i < 4; i++, set_avma(av3))
    1645             :         {
    1646             :           GEN u;
    1647        1592 :           pj[4] = i;
    1648        1592 :           pj[5] = (i + pj[2] - pj[1]) & 3;
    1649        1592 :           if (DEBUGLEVEL >= 4)
    1650           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/2:%d/2:%d/4:%Ps:%Ps\n",
    1651             :                        j-1, w >> 1, l, i, sg, pj);
    1652        1592 :           u = ZX_add(lincomb(B,sg[1],sg[3],pj[4]),
    1653        1592 :                      lincomb(B,sg[2],sg[4],pj[5]));
    1654        1592 :           u = FpX_add(uu,u,Q);
    1655        1592 :           if (s4test(u, liftpow, gl, tau)) goto suites4_2;
    1656             :         }
    1657             :       }
    1658         161 :       lswap(sg[3], sg[4]);
    1659         161 :       pj[2] = (-pj[2]) & 3;
    1660             :     }
    1661             :   }
    1662           0 :   return gc_NULL(ltop);
    1663          49 : suites4_2:
    1664          49 :   set_avma(av);
    1665             :   {
    1666          49 :     long abc = (pj[1] + pj[2] + pj[3]) & 3;
    1667          49 :     long abcdef = ((abc + pj[4] + pj[5] - pj[6]) & 3) >> 1;
    1668             :     GEN u;
    1669             :     pari_sp av2;
    1670          49 :     u = s4releveauto(liftp,Bcoeff,TQ,Q,sg[1],sg[4],sg[2],sg[5],sg[3],sg[6]);
    1671          49 :     liftpow = s4makelift(u, gl);
    1672          49 :     av2 = avma;
    1673         255 :     for (j = 0; j < 8; j++)
    1674             :     {
    1675             :       long h, g, i;
    1676         255 :       h = j & 3;
    1677         255 :       g = (abcdef + ((j & 4) >> 1)) & 3;
    1678         255 :       i = (h + abc - g) & 3;
    1679         255 :       u = ZX_add(lincomb(B,sg[1],sg[4], g), lincomb(B,sg[2],sg[5], h));
    1680         255 :       u = FpX_add(u, lincomb(B,sg[3],sg[6], i),Q);
    1681         255 :       if (DEBUGLEVEL >= 4)
    1682           0 :         err_printf("S4GaloisConj: Testing %d/8 %d:%d:%d\n", j,g,h,i);
    1683         255 :       if (s4test(u, liftpow, gl, phi)) break;
    1684         206 :       set_avma(av2);
    1685             :     }
    1686             :   }
    1687          49 :   if (j == 8) return gc_NULL(ltop);
    1688        1225 :   for (i = 1; i <= n; i++)
    1689             :   {
    1690        1176 :     r1[i] = sigma[tau[i]];
    1691        1176 :     r2[i] = phi[sigma[tau[phi[i]]]];
    1692        1176 :     r3[i] = phi[sigma[i]];
    1693        1176 :     r4[i] = sigma[i];
    1694             :   }
    1695          49 :   set_avma(ltop2); return res;
    1696             : }
    1697             : 
    1698             : static GEN
    1699         910 : f36releveauto2(GEN Bl, GEN T, GEN p,GEN a)
    1700             : {
    1701         910 :   GEN      F = gmael(Bl,a[1],a[1]);
    1702         910 :   F = ZX_add(F,gmael(Bl,a[2],a[3]));
    1703         910 :   F = ZX_add(F,gmael(Bl,a[3],a[2]));
    1704         910 :   F = ZX_add(F,gmael(Bl,a[4],a[5]));
    1705         910 :   F = ZX_add(F,gmael(Bl,a[5],a[4]));
    1706         910 :   F = ZX_add(F,gmael(Bl,a[6],a[7]));
    1707         910 :   F = ZX_add(F,gmael(Bl,a[7],a[6]));
    1708         910 :   F = ZX_add(F,gmael(Bl,a[8],a[9]));
    1709         910 :   F = ZX_add(F,gmael(Bl,a[9],a[8]));
    1710         910 :   return FpXQ_red(F, T, p);
    1711             : }
    1712             : 
    1713             : static GEN
    1714          35 : f36releveauto4(GEN Bl, GEN T, GEN p,GEN a)
    1715             : {
    1716          35 :   GEN      F = gmael(Bl,a[1],a[1]);
    1717          35 :   F = ZX_add(F,gmael(Bl,a[2],a[3]));
    1718          35 :   F = ZX_add(F,gmael(Bl,a[3],a[4]));
    1719          35 :   F = ZX_add(F,gmael(Bl,a[4],a[5]));
    1720          35 :   F = ZX_add(F,gmael(Bl,a[5],a[2]));
    1721          35 :   F = ZX_add(F,gmael(Bl,a[6],a[7]));
    1722          35 :   F = ZX_add(F,gmael(Bl,a[7],a[8]));
    1723          35 :   F = ZX_add(F,gmael(Bl,a[8],a[9]));
    1724          35 :   F = ZX_add(F,gmael(Bl,a[9],a[6]));
    1725          35 :   return FpXQ_red(F, T, p);
    1726             : }
    1727             : 
    1728             : static GEN
    1729          14 : f36galoisgen(struct galois_lift *gl)
    1730             : {
    1731          14 :   const long n = 36;
    1732             :   struct galois_testlift gt;
    1733          14 :   pari_sp av, ltop2, ltop = avma;
    1734             :   long i;
    1735          14 :   GEN sigma, tau, rho, res, r1,r2,r3, pj, pk, p = gl->p, Q = gl->Q, TQ = gl->TQ;
    1736             :   GEN sg, s4, sp,  Tp, Tmod, misom, Bcoeff, liftpow, aut, liftp, B, Bl, tam;
    1737          14 :   res = cgetg(3, t_VEC);
    1738          14 :   r1 = cgetg(n+1, t_VECSMALL);
    1739          14 :   r2 = cgetg(n+1, t_VECSMALL);
    1740          14 :   r3 = cgetg(n+1, t_VECSMALL);
    1741          14 :   gel(res,1)= mkvec3(r1,r2,r3);
    1742          14 :   gel(res,2) = mkvecsmall3(3,3,4);
    1743          14 :   ltop2 = avma;
    1744          14 :   sg = identity_perm(9);
    1745          14 :   s4 = identity_perm(9);
    1746          14 :   sp = identity_perm(9);
    1747          14 :   pj = zero_zv(4);
    1748          14 :   pk = zero_zv(2);
    1749          14 :   sigma = cgetg(n+1, t_VECSMALL);
    1750          14 :   tau = r3;
    1751          14 :   rho = cgetg(n+1, t_VECSMALL);
    1752          14 :   Tp = FpX_red(gl->T,p);
    1753          14 :   Tmod = gel(FpX_factor(Tp,p), 1);
    1754          14 :   misom = FpXV_ffisom(Tmod, p);
    1755          14 :   aut = galoisdolift(gl);
    1756          14 :   inittestlift(aut, Tmod, gl, &gt);
    1757          14 :   Bcoeff = gt.bezoutcoeff;
    1758          14 :   B = FqC_FqV_mul(gt.pauto, Bcoeff, gl->TQ, gl->Q);
    1759          14 :   liftp = mkliftpow(shallowtrans(misom), Tmod, p, gl);
    1760          14 :   Bl = FqC_FqV_mul(liftp,Bcoeff, gl->TQ, gl->Q);
    1761          14 :   av = avma;
    1762         910 :   for (i = 0; i < 105; i++, set_avma(av))
    1763             :   {
    1764             :     pari_sp av0, av1, av2, av3;
    1765             :     GEN u0, u1, u2, u3;
    1766             :     long j0, j1, j2, j3, s;
    1767         910 :     if (i)
    1768             :     {
    1769         896 :       rot3(sg[7],sg[8],sg[9])
    1770         896 :       if (i%3==0)
    1771             :       {
    1772         294 :         s=sg[5]; sg[5]=sg[6]; sg[6]=sg[7]; sg[7]=sg[8]; sg[8]=sg[9]; sg[9]=s;
    1773         294 :         if (i%15==0)
    1774             :         {
    1775          49 :           s=sg[3]; sg[3]=sg[4]; sg[4]=sg[5];
    1776          49 :           sg[5]=sg[6]; sg[6]=sg[7]; sg[7]=sg[8]; sg[8]=sg[9]; sg[9]=s;
    1777             :         }
    1778             :       }
    1779             :     }
    1780         910 :     liftpow = s4makelift(f36releveauto2(Bl, TQ, Q, sg), gl);
    1781         910 :     av0 = avma;
    1782        4522 :     for (j0 = 0; j0 < 4; j0++, set_avma(av0))
    1783             :     {
    1784        3626 :       u0 = lincomb(B,sg[8],sg[9],j0);
    1785        3626 :       u0 = FpX_add(u0, gmael(B,sg[1],3), Q); av1 = avma;
    1786       18095 :       for (j1 = 0; j1 < 4; j1++, set_avma(av1))
    1787             :       {
    1788       14483 :         u1 = lincomb(B,sg[6],sg[7],j1);
    1789       14483 :         u1 = FpX_add(u0, u1, Q); av2 = avma;
    1790       72380 :         for (j2 = 0; j2 < 4; j2++, set_avma(av2))
    1791             :         {
    1792       57911 :           u2 = lincomb(B,sg[4],sg[5],j2);
    1793       57911 :           u2 = FpX_add(u1, u2, Q); av3 = avma;
    1794      289527 :           for (j3 = 0; j3 < 4; j3++, set_avma(av3))
    1795             :           {
    1796      231630 :             u3 = lincomb(B,sg[2],sg[3],j3);
    1797      231630 :             u3 = FpX_add(u2, u3, Q);
    1798      231630 :             if (s4test(u3, liftpow, gl, sigma))
    1799             :             {
    1800          14 :               pj[1] = j3;
    1801          14 :               pj[2] = j2;
    1802          14 :               pj[3] = j1;
    1803          14 :               pj[4] = j0; goto suitef36;
    1804             :             }
    1805             :           }
    1806             :         }
    1807             :       }
    1808             :     }
    1809             :   }
    1810           0 :   return gc_NULL(ltop);
    1811          14 : suitef36:
    1812          14 :   s4[1]=sg[1]; s4[2]=sg[2]; s4[4]=sg[3];
    1813          14 :   s4[3]=sg[4]; s4[5]=sg[5]; s4[6]=sg[6];
    1814          14 :   s4[8]=sg[7]; s4[7]=sg[8]; s4[9]=sg[9];
    1815          14 :   for (i = 0; i < 12; i++, set_avma(av))
    1816             :   {
    1817             :     pari_sp av0, av1;
    1818             :     GEN u0, u1;
    1819             :     long j0, j1;
    1820          14 :     if (i)
    1821             :     {
    1822           0 :       lswap(s4[3],s4[5]); pj[2] = (-pj[2])&3;
    1823           0 :       if (odd(i)) { lswap(s4[7],s4[9]); pj[4]=(-pj[4])&3; }
    1824           0 :       if (i%4==0)
    1825             :       {
    1826           0 :         rot3(s4[3],s4[6],s4[7]);
    1827           0 :         rot3(s4[5],s4[8],s4[9]);
    1828           0 :         rot3(pj[2],pj[3],pj[4]);
    1829             :       }
    1830             :     }
    1831          14 :     liftpow = s4makelift(f36releveauto4(Bl, TQ, Q, s4), gl);
    1832          14 :     av0 = avma;
    1833          21 :     for (j0 = 0; j0 < 4; j0++, set_avma(av0))
    1834             :     {
    1835          21 :       u0 = FpX_add(gmael(B,s4[1],2), gmael(B,s4[2],1+j0),Q);
    1836          21 :       u0 = FpX_add(u0, gmael(B,s4[3],1+smodss(pj[2]-j0,4)),Q);
    1837          21 :       u0 = FpX_add(u0, gmael(B,s4[4],1+smodss(j0-pj[1]-pj[2],4)),Q);
    1838          21 :       u0 = FpX_add(u0, gmael(B,s4[5],1+smodss(pj[1]-j0,4)),Q);
    1839          21 :       av1 = avma;
    1840          84 :       for (j1 = 0; j1 < 4; j1++, set_avma(av1))
    1841             :       {
    1842          77 :         u1 = FpX_add(u0, gmael(B,s4[6],1+j1),Q);
    1843          77 :         u1 = FpX_add(u1, gmael(B,s4[7],1+smodss(pj[4]-j1,4)),Q);
    1844          77 :         u1 = FpX_add(u1, gmael(B,s4[8],1+smodss(j1-pj[3]-pj[4],4)),Q);
    1845          77 :         u1 = FpX_add(u1, gmael(B,s4[9],1+smodss(pj[3]-j1,4)),Q);
    1846          77 :         if (s4test(u1, liftpow, gl, tau))
    1847             :         {
    1848          14 :           pk[1] = j0;
    1849          14 :           pk[2] = j1; goto suitef36_2;
    1850             :         }
    1851             :       }
    1852             :     }
    1853             :   }
    1854           0 :   return gc_NULL(ltop);
    1855          14 : suitef36_2:
    1856          14 :   sp[1]=s4[9]; sp[2]=s4[1]; sp[3]=s4[2];
    1857          14 :   sp[4]=s4[7]; sp[5]=s4[3]; sp[6]=s4[8];
    1858          14 :   sp[8]=s4[4]; sp[7]=s4[5]; sp[9]=s4[6];
    1859          21 :   for (i = 0; i < 4; i++, set_avma(av))
    1860             :   {
    1861          21 :     const int w[4][6]={{0,0,1,3,0,2},{1,0,2,1,1,2},{3,3,2,0,3,1},{0,1,3,0,0,3}};
    1862             :     pari_sp av0, av1, av2;
    1863             :     GEN u0, u1, u2;
    1864             :     long j0, j1,j2,j3,j4,j5;
    1865          21 :     if (i)
    1866             :     {
    1867           7 :       rot4(sp[3],sp[5],sp[8],sp[7])
    1868           7 :       pk[1]=(-pk[1])&3;
    1869             :     }
    1870          21 :     liftpow = s4makelift(f36releveauto4(Bl,TQ,Q,sp), gl);
    1871          21 :     av0 = avma;
    1872          56 :     for (j0 = 0; j0 < 4; j0++, set_avma(av0))
    1873             :     {
    1874          49 :       u0 = FpX_add(gmael(B,sp[1],2), gmael(B,sp[2],1+j0),Q);
    1875          49 :       av1 = avma;
    1876         210 :       for (j1 = 0; j1 < 4; j1++, set_avma(av1))
    1877             :       {
    1878         175 :         u1 = FpX_add(u0, gmael(B,sp[3],1+j1),Q);
    1879         175 :         j3 = (-pk[1]-pj[3]+j0+j1-w[i][0]*pj[1]-w[i][3]*pj[2])&3;
    1880         175 :         u1 = FpX_add(u1, gmael(B,sp[6],1+j3),Q);
    1881         175 :         j5 = (-pk[1]+2*j0+2*j1-w[i][2]*pj[1]-w[i][5]*pj[2])&3;
    1882         175 :         u1 = FpX_add(u1, gmael(B,sp[8],1+j5),Q);
    1883         175 :         av2 = avma;
    1884         847 :         for (j2 = 0; j2 < 4; j2++, set_avma(av2))
    1885             :         {
    1886         686 :           u2 = FpX_add(u1, gmael(B,sp[4],1+j2),Q);
    1887         686 :           u2 = FpX_add(u2, gmael(B,sp[5],1+smodss(-j0-j1-j2,4)),Q);
    1888         686 :           j4 = (-pk[1]-pk[2]+pj[3]+pj[4]-j2-w[i][1]*pj[1]-w[i][4]*pj[2])&3;
    1889         686 :           u2 = FpX_add(u2, gmael(B,sp[7],1+j4),Q);
    1890         686 :           u2 = FpX_add(u2, gmael(B,sp[9],1+smodss(-j3-j4-j5,4)),Q);
    1891         686 :           if (s4test(u2, liftpow, gl, rho))
    1892          14 :             goto suitef36_3;
    1893             :         }
    1894             :       }
    1895             :     }
    1896             :   }
    1897           0 :   return gc_NULL(ltop);
    1898          14 : suitef36_3:
    1899          14 :   tam = perm_inv(tau);
    1900         518 :   for (i = 1; i <= n; i++)
    1901             :   {
    1902         504 :     r1[tau[i]] = rho[i];
    1903         504 :     r2[i] = tam[rho[i]];
    1904             :   }
    1905          14 :   set_avma(ltop2); return res;
    1906             : }
    1907             : 
    1908             : static GEN
    1909          49 : galoisfindgroups(GEN lo, GEN sg, long f)
    1910             : {
    1911          49 :   pari_sp ltop = avma;
    1912             :   long i, j, k;
    1913          49 :   GEN V = cgetg(lg(lo), t_VEC);
    1914         140 :   for(j=1,i=1; i<lg(lo); i++)
    1915             :   {
    1916          91 :     pari_sp av = avma;
    1917          91 :     GEN loi = gel(lo,i), W = cgetg(lg(loi),t_VECSMALL);
    1918         231 :     for (k=1; k<lg(loi); k++) W[k] = loi[k] % f;
    1919          91 :     W = vecsmall_uniq(W);
    1920          91 :     if (zv_equal(W, sg)) gel(V,j++) = loi;
    1921          91 :     set_avma(av);
    1922             :   }
    1923          49 :   setlg(V,j); return gerepilecopy(ltop,V);
    1924             : }
    1925             : 
    1926             : static GEN
    1927         504 : galoismakepsi(long g, GEN sg, GEN pf)
    1928             : {
    1929         504 :   GEN psi=cgetg(g+1,t_VECSMALL);
    1930             :   long i;
    1931        1232 :   for (i = 1; i < g; i++) psi[i] = sg[pf[i]];
    1932         504 :   psi[g] = sg[1]; return psi;
    1933             : }
    1934             : 
    1935             : static GEN
    1936        2576 : galoisfrobeniuslift_nilp(GEN T, GEN den, GEN L,  GEN Lden,
    1937             :     struct galois_frobenius *gf,  struct galois_borne *gb)
    1938             : {
    1939        2576 :   pari_sp ltop=avma, av2;
    1940             :   struct galois_lift gl;
    1941        2576 :   long i, k, deg = 1, g = lg(gf->Tmod)-1;
    1942        2576 :   GEN F,Fp,Fe, aut, frob, res = cgetg(lg(L), t_VECSMALL);
    1943        2576 :   gf->psi = const_vecsmall(g,1);
    1944        2576 :   av2 = avma;
    1945        2576 :   initlift(T, den, gf->p, L, Lden, gb, &gl);
    1946        2576 :   if (DEBUGLEVEL >= 4)
    1947           0 :     err_printf("GaloisConj: p=%ld e=%ld deg=%ld fp=%ld\n",
    1948             :                             gf->p, gl.e, deg, gf->fp);
    1949        2576 :   aut = galoisdolift(&gl);
    1950        2576 :   if (galoisfrobeniustest(aut,&gl,res))
    1951             :   {
    1952        2037 :     set_avma(av2); gf->deg = gf->fp; return res;
    1953             :   }
    1954             : 
    1955         539 :   F =factoru(gf->fp);
    1956         539 :   Fp = gel(F,1);
    1957         539 :   Fe = gel(F,2);
    1958         539 :   frob = cgetg(lg(L), t_VECSMALL);
    1959        1078 :   for(k = lg(Fp)-1; k>=1; k--)
    1960             :   {
    1961         539 :     pari_sp btop=avma;
    1962         539 :     GEN fres=NULL;
    1963         539 :     long el = gf->fp, dg = 1, dgf = 1, e, pr;
    1964        1078 :     for(e=1; e<=Fe[k]; e++)
    1965             :     {
    1966        1078 :       dg *= Fp[k]; el /= Fp[k];
    1967        1078 :       if (DEBUGLEVEL>=4) err_printf("Trying degre %d.\n",dg);
    1968        1078 :       if (el==1) break;
    1969         637 :       aut = galoisdoliftn(&gl, el);
    1970         637 :       if (!galoisfrobeniustest(aut,&gl,frob))
    1971          98 :         break;
    1972         539 :       dgf = dg; fres = gcopy(frob);
    1973             :     }
    1974         539 :     if (dgf == 1) { set_avma(btop); continue; }
    1975         476 :     pr = deg*dgf;
    1976         476 :     if (deg == 1)
    1977             :     {
    1978       11200 :       for(i=1;i<lg(res);i++) res[i]=fres[i];
    1979             :     }
    1980             :     else
    1981             :     {
    1982           0 :       GEN cp = perm_mul(res,fres);
    1983           0 :       for(i=1;i<lg(res);i++) res[i] = cp[i];
    1984             :     }
    1985         476 :     deg = pr; set_avma(btop);
    1986             :   }
    1987         539 :   if (DEBUGLEVEL>=4 && res) err_printf("Best lift: %d\n",deg);
    1988         539 :   if (deg==1) return gc_NULL(ltop);
    1989             :   else
    1990             :   {
    1991         476 :     set_avma(av2); gf->deg = deg; return res;
    1992             :   }
    1993             : }
    1994             : 
    1995             : static GEN
    1996         693 : galoisfrobeniuslift(GEN T, GEN den, GEN L,  GEN Lden,
    1997             :     struct galois_frobenius *gf,  struct galois_borne *gb)
    1998             : {
    1999         693 :   pari_sp ltop=avma, av2;
    2000             :   struct galois_testlift gt;
    2001             :   struct galois_lift gl;
    2002         693 :   long i, j, k, n = lg(L)-1, deg = 1, g = lg(gf->Tmod)-1;
    2003         693 :   GEN F,Fp,Fe, aut, frob, res = cgetg(lg(L), t_VECSMALL);
    2004         693 :   gf->psi = const_vecsmall(g,1);
    2005         693 :   av2 = avma;
    2006         693 :   initlift(T, den, gf->p, L, Lden, gb, &gl);
    2007         693 :   if (DEBUGLEVEL >= 4)
    2008           0 :     err_printf("GaloisConj: p=%ld e=%ld deg=%ld fp=%ld\n",
    2009             :                             gf->p, gl.e, deg, gf->fp);
    2010         693 :   aut = galoisdolift(&gl);
    2011         693 :   if (galoisfrobeniustest(aut,&gl,res))
    2012             :   {
    2013         189 :     set_avma(av2); gf->deg = gf->fp; return res;
    2014             :   }
    2015         504 :   inittestlift(aut,gf->Tmod, &gl, &gt);
    2016         504 :   gt.C = cgetg(gf->fp+1,t_VEC);
    2017         504 :   gt.Cd= cgetg(gf->fp+1,t_VEC);
    2018        3276 :   for (i = 1; i <= gf->fp; i++) {
    2019        2772 :     gel(gt.C,i)  = zero_zv(gt.g);
    2020        2772 :     gel(gt.Cd,i) = zero_zv(gt.g);
    2021             :   }
    2022             : 
    2023         504 :   F =factoru(gf->fp);
    2024         504 :   Fp = gel(F,1);
    2025         504 :   Fe = gel(F,2);
    2026         504 :   frob = cgetg(lg(L), t_VECSMALL);
    2027        1155 :   for(k=lg(Fp)-1;k>=1;k--)
    2028             :   {
    2029         651 :     pari_sp btop=avma;
    2030         651 :     GEN psi=NULL, fres=NULL, sg = identity_perm(1);
    2031         651 :     long el=gf->fp, dg=1, dgf=1, e, pr;
    2032        1302 :     for(e=1; e<=Fe[k]; e++)
    2033             :     {
    2034             :       GEN lo, pf;
    2035             :       long l;
    2036         700 :       dg *= Fp[k]; el /= Fp[k];
    2037         700 :       if (DEBUGLEVEL>=4) err_printf("Trying degre %d.\n",dg);
    2038         700 :       if (galoisfrobeniustest(gel(gt.pauto,el+1),&gl,frob))
    2039             :       {
    2040         147 :         psi = const_vecsmall(g,1);
    2041         147 :         dgf = dg; fres = gcopy(frob); continue;
    2042             :       }
    2043         553 :       lo = listznstarelts(dg, n / gf->fp);
    2044         553 :       if (e!=1) lo = galoisfindgroups(lo, sg, dgf);
    2045         553 :       if (DEBUGLEVEL>=4) err_printf("Galoisconj:Subgroups list:%Ps\n", lo);
    2046        1113 :       for (l = 1; l < lg(lo); l++)
    2047        1064 :         if (lg(gel(lo,l))>2 && frobeniusliftall(gel(lo,l), el, &pf,&gl,&gt, frob))
    2048             :         {
    2049         504 :           sg  = gcopy(gel(lo,l));
    2050         504 :           psi = galoismakepsi(g,sg,pf);
    2051         504 :           dgf = dg; fres = gcopy(frob); break;
    2052             :         }
    2053         553 :       if (l == lg(lo)) break;
    2054             :     }
    2055         651 :     if (dgf == 1) { set_avma(btop); continue; }
    2056         616 :     pr = deg*dgf;
    2057         616 :     if (deg == 1)
    2058             :     {
    2059        6972 :       for(i=1;i<lg(res);i++) res[i]=fres[i];
    2060        1715 :       for(i=1;i<lg(psi);i++) gf->psi[i]=psi[i];
    2061             :     }
    2062             :     else
    2063             :     {
    2064         112 :       GEN cp = perm_mul(res,fres);
    2065        2422 :       for(i=1;i<lg(res);i++) res[i] = cp[i];
    2066         378 :       for(i=1;i<lg(psi);i++) gf->psi[i] = (dgf*gf->psi[i] + deg*psi[i]) % pr;
    2067             :     }
    2068         616 :     deg = pr; set_avma(btop);
    2069             :   }
    2070        3276 :   for (i = 1; i <= gf->fp; i++)
    2071        9240 :     for (j = 1; j <= gt.g; j++) guncloneNULL(gmael(gt.C,i,j));
    2072         504 :   if (DEBUGLEVEL>=4 && res) err_printf("Best lift: %d\n",deg);
    2073         504 :   if (deg==1) return gc_NULL(ltop);
    2074             :   else
    2075             :   {
    2076             :     /* Normalize result so that psi[g]=1 */
    2077         504 :     long im = Fl_inv(gf->psi[g], deg);
    2078         504 :     GEN cp = perm_pow(res, im);
    2079        6972 :     for(i=1;i<lg(res);i++) res[i] = cp[i];
    2080        1715 :     for(i=1;i<lg(gf->psi);i++) gf->psi[i] = Fl_mul(im, gf->psi[i], deg);
    2081         504 :     set_avma(av2); gf->deg = deg; return res;
    2082             :   }
    2083             : }
    2084             : 
    2085             : /* return NULL if not Galois */
    2086             : static GEN
    2087        3206 : galoisfindfrobenius(GEN T, GEN L, GEN den, GEN bad, struct galois_frobenius *gf,
    2088             :     struct galois_borne *gb, const struct galois_analysis *ga)
    2089             : {
    2090        3206 :   pari_sp ltop = avma, av;
    2091        3206 :   long Try = 0, n = degpol(T), deg, gmask = (ga->group&ga_ext_2)? 3: 1;
    2092        3206 :   GEN frob, Lden = makeLden(L,den,gb);
    2093        3206 :   long is_nilpotent = ga->group&ga_all_nilpotent;
    2094             :   forprime_t S;
    2095             : 
    2096        3206 :   u_forprime_init(&S, ga->p, ULONG_MAX);
    2097        3206 :   av = avma;
    2098        3206 :   deg = gf->deg = ga->deg;
    2099        3269 :   while ((gf->p = u_forprime_next(&S)))
    2100             :   {
    2101             :     pari_sp lbot;
    2102             :     GEN Ti, Tp;
    2103             :     long nb, d;
    2104        3269 :     set_avma(av);
    2105        3269 :     Tp = ZX_to_Flx(T, gf->p);
    2106        3269 :     if (!Flx_is_squarefree(Tp, gf->p)) continue;
    2107        3269 :     if (bad && dvdiu(bad, gf->p)) continue;
    2108        3269 :     Ti = gel(Flx_factor(Tp, gf->p), 1);
    2109        3269 :     nb = lg(Ti)-1; d = degpol(gel(Ti,1));
    2110        3269 :     if (nb > 1 && degpol(gel(Ti,nb)) != d) return gc_NULL(ltop);
    2111        3269 :     if (((gmask&1)==0 || d % deg) && ((gmask&2)==0 || odd(d))) continue;
    2112        3269 :     if (DEBUGLEVEL >= 1) err_printf("GaloisConj: Trying p=%ld\n", gf->p);
    2113        3269 :     FlxV_to_ZXV_inplace(Ti);
    2114        3269 :     gf->fp = d;
    2115        3269 :     gf->Tmod = Ti; lbot = avma;
    2116        3269 :     if (is_nilpotent)
    2117        2576 :       frob = galoisfrobeniuslift_nilp(T, den, L, Lden, gf, gb);
    2118             :     else
    2119         693 :       frob = galoisfrobeniuslift(T, den, L, Lden, gf, gb);
    2120        3269 :     if (frob)
    2121             :     {
    2122             :       GEN *gptr[3];
    2123        3206 :       gf->Tmod = gcopy(Ti);
    2124        3206 :       gptr[0]=&gf->Tmod; gptr[1]=&gf->psi; gptr[2]=&frob;
    2125        3206 :       gerepilemanysp(ltop,lbot,gptr,3); return frob;
    2126             :     }
    2127          63 :     if (is_nilpotent) continue;
    2128           0 :     if ((ga->group&ga_all_normal) && d % deg == 0) gmask &= ~1;
    2129             :     /* The first prime degree is always divisible by deg, so we don't
    2130             :      * have to worry about ext_2 being used before regular supersolvable*/
    2131           0 :     if (!gmask) return gc_NULL(ltop);
    2132           0 :     if ((ga->group&ga_non_wss) && ++Try > ((3*n)>>1))
    2133             :     {
    2134           0 :       if (DEBUGLEVEL)
    2135           0 :         pari_warn(warner,"Galois group probably not weakly super solvable");
    2136           0 :       return NULL;
    2137             :     }
    2138             :   }
    2139           0 :   if (!gf->p) pari_err_OVERFLOW("galoisfindfrobenius [ran out of primes]");
    2140           0 :   return NULL;
    2141             : }
    2142             : 
    2143             : /* compute g such that tau(Pmod[#])= tau(Pmod[g]) */
    2144             : 
    2145             : static long
    2146        3283 : get_image(GEN tau, GEN P, GEN Pmod, GEN p)
    2147             : {
    2148        3283 :   pari_sp av = avma;
    2149        3283 :   long g, gp = lg(Pmod)-1;
    2150        3283 :   tau = RgX_to_FpX(tau, p);
    2151        3283 :   tau = FpX_FpXQ_eval(gel(Pmod, gp), tau, P, p);
    2152        3283 :   tau = FpX_normalize(FpX_gcd(P, tau, p), p);
    2153        6762 :   for (g = 1; g <= gp; g++)
    2154        6762 :     if (ZX_equal(tau, gel(Pmod,g))) return gc_long(av,g);
    2155           0 :   return gc_long(av,0);
    2156             : }
    2157             : 
    2158             : static GEN
    2159        4788 : gg_get_std(GEN G)
    2160             : {
    2161        4788 :   return !G ? NULL: lg(G)==3 ? G: mkvec2(gel(G,1),gmael(G,5,1));
    2162             : }
    2163             : 
    2164             : static GEN galoisgen(GEN T, GEN L, GEN M, GEN den, GEN bad, struct galois_borne *gb,
    2165             :           const struct galois_analysis *ga);
    2166             : 
    2167             : static GEN
    2168        2261 : galoisgenfixedfield(GEN Tp, GEN Pmod, GEN PL, GEN P, GEN ip, GEN bad, struct galois_borne *gb)
    2169             : {
    2170             :   GEN  Pden, PM;
    2171             :   GEN  tau, PG, Pg;
    2172             :   long g, lP;
    2173        2261 :   long x = varn(Tp);
    2174        2261 :   GEN Pp = FpX_red(P, ip);
    2175        2261 :   if (DEBUGLEVEL>=6)
    2176           0 :     err_printf("GaloisConj: Fixed field %Ps\n",P);
    2177        2261 :   if (degpol(P)==2 && !bad)
    2178             :   {
    2179        1470 :     PG=cgetg(3,t_VEC);
    2180        1470 :     gel(PG,1) = mkvec( mkvecsmall2(2,1) );
    2181        1470 :     gel(PG,2) = mkvecsmall(2);
    2182        1470 :     tau = deg1pol_shallow(gen_m1, negi(gel(P,3)), x);
    2183        1470 :     g = get_image(tau, Pp, Pmod, ip);
    2184        1470 :     if (!g) return NULL;
    2185        1470 :     Pg = mkvecsmall(g);
    2186             :   }
    2187             :   else
    2188             :   {
    2189             :     struct galois_analysis Pga;
    2190             :     struct galois_borne Pgb;
    2191             :     GEN mod, mod2;
    2192             :     long j;
    2193         798 :     if (!galoisanalysis(P, &Pga, 0, NULL)) return NULL;
    2194         777 :     if (bad) Pga.group &= ~ga_easy;
    2195         777 :     Pgb.l = gb->l;
    2196         777 :     Pden = galoisborne(P, NULL, &Pgb, degpol(P));
    2197             : 
    2198         777 :     if (Pgb.valabs > gb->valabs)
    2199             :     {
    2200         125 :       if (DEBUGLEVEL>=4)
    2201           0 :         err_printf("GaloisConj: increase prec of p-adic roots of %ld.\n"
    2202           0 :             ,Pgb.valabs-gb->valabs);
    2203         125 :       PL = ZpX_liftroots(P,PL,gb->l,Pgb.valabs);
    2204             :     }
    2205         652 :     else if (Pgb.valabs < gb->valabs)
    2206         588 :       PL = FpC_red(PL, Pgb.ladicabs);
    2207         777 :     PM = FpV_invVandermonde(PL, Pden, Pgb.ladicabs);
    2208         777 :     PG = galoisgen(P, PL, PM, Pden, bad ? lcmii(Pgb.dis, bad): NULL, &Pgb, &Pga);
    2209         777 :     if (!PG) return NULL;
    2210         770 :     lP = lg(gel(PG,1));
    2211         770 :     mod = Pgb.ladicabs; mod2 = shifti(mod, -1);
    2212         770 :     Pg = cgetg(lP, t_VECSMALL);
    2213        2583 :     for (j = 1; j < lP; j++)
    2214             :     {
    2215        1813 :       pari_sp btop=avma;
    2216        1813 :       tau = permtopol(gmael(PG,1,j), PL, PM, Pden, mod, mod2, x);
    2217        1813 :       g = get_image(tau, Pp, Pmod, ip);
    2218        1813 :       if (!g) return NULL;
    2219        1813 :       Pg[j] = g;
    2220        1813 :       set_avma(btop);
    2221             :     }
    2222             :   }
    2223        2240 :   return mkvec2(PG,Pg);
    2224             : }
    2225             : 
    2226             : static GEN
    2227        2261 : galoisgenfixedfield0(GEN O, GEN L, GEN sigma, GEN T, GEN bad, GEN *pt_V,
    2228             :                      struct galois_frobenius *gf, struct galois_borne *gb)
    2229             : {
    2230        2261 :   pari_sp btop = avma;
    2231        2261 :   long vT = varn(T);
    2232        2261 :   GEN mod = gb->ladicabs, mod2 = shifti(gb->ladicabs,-1);
    2233             :   GEN OL, sym, P, PL, p, Tp, Sp, Pmod, PG;
    2234        2261 :   OL = fixedfieldorbits(O,L);
    2235        2261 :   sym  = fixedfieldsympol(OL, itou(gb->l));
    2236        2261 :   PL = sympol_eval(sym, OL, mod);
    2237        2261 :   P = FpX_center_i(FpV_roots_to_pol(PL, mod, vT), mod, mod2);
    2238        2261 :   if (!FpX_is_squarefree(P,utoipos(gf->p)))
    2239             :   {
    2240          70 :     GEN badp = lcmii(bad? bad: gb->dis, ZX_disc(P));
    2241          70 :     gf->p  = findpsi(badp, gf->p, T, sigma, gf->deg, &gf->Tmod, &gf->psi);
    2242             :   }
    2243        2261 :   p  = utoipos(gf->p);
    2244        2261 :   Tp = FpX_red(T,p);
    2245        2261 :   Sp = sympol_aut_evalmod(sym, gf->deg, sigma, Tp, p);
    2246        2261 :   Pmod = fixedfieldfactmod(Sp, p, gf->Tmod);
    2247        2261 :   PG = galoisgenfixedfield(Tp, Pmod, PL, P, p, bad, gb);
    2248        2261 :   if (PG == NULL) return NULL;
    2249        2240 :   if (DEBUGLEVEL >= 4)
    2250           0 :     err_printf("GaloisConj: Back to Earth:%Ps\n", gg_get_std(gel(PG,1)));
    2251        2240 :   if (pt_V) *pt_V = mkvec3(sym, PL, P);
    2252        2240 :   gerepileall(btop, pt_V ? 4: 3, &gf->Tmod, &gf->psi, &PG, pt_V);
    2253        2240 :   return PG;
    2254             : }
    2255             : 
    2256             : /* Let sigma^m=1, tau*sigma*tau^-1=sigma^s. Return n = sum_{0<=k<e,0} s^k mod m
    2257             :  * so that (sigma*tau)^e = sigma^n*tau^e. N.B. n*(1-s) = 1-s^e mod m,
    2258             :  * unfortunately (1-s) may not invertible mod m */
    2259             : static long
    2260        7294 : stpow(long s, long e, long m)
    2261             : {
    2262        7294 :   long i, n = 1;
    2263       11452 :   for (i = 1; i < e; i++) n = (1 + n * s) % m;
    2264        7294 :   return n;
    2265             : }
    2266             : 
    2267             : static GEN
    2268        3283 : wpow(long s, long m, long e, long n)
    2269             : {
    2270        3283 :   GEN   w = cgetg(n+1,t_VECSMALL);
    2271        3283 :   long si = s;
    2272             :   long i;
    2273        3283 :   w[1] = 1;
    2274        3647 :   for(i=2; i<=n; i++) w[i] = w[i-1]*e;
    2275        6930 :   for(i=n; i>=1; i--)
    2276             :   {
    2277        3647 :     si = Fl_powu(si,e,m);
    2278        3647 :     w[i] = Fl_mul(s-1, stpow(si, w[i], m), m);
    2279             :   }
    2280        3283 :   return w;
    2281             : }
    2282             : 
    2283             : static GEN
    2284        3283 : galoisgenliftauto(GEN O, GEN gj, long s, long n, struct galois_test *td)
    2285             : {
    2286        3283 :   pari_sp av = avma;
    2287             :   long sr, k;
    2288        3283 :   long deg = lg(gel(O,1))-1;
    2289        3283 :   GEN  X  = cgetg(lg(O), t_VECSMALL);
    2290        3283 :   GEN  oX = cgetg(lg(O), t_VECSMALL);
    2291        3283 :   GEN  B  = perm_cycles(gj);
    2292        3283 :   long oj = lg(gel(B,1)) - 1;
    2293        3283 :   GEN  F  = factoru(oj);
    2294        3283 :   GEN  Fp = gel(F,1);
    2295        3283 :   GEN  Fe = gel(F,2);
    2296        3283 :   GEN  pf = identity_perm(n);
    2297        3283 :   if (DEBUGLEVEL >= 6)
    2298           0 :     err_printf("GaloisConj: %Ps of relative order %d\n", gj, oj);
    2299        6545 :   for (k=lg(Fp)-1; k>=1; k--)
    2300             :   {
    2301        3283 :     long f, dg = 1, el = oj, osel = 1, a = 0;
    2302        3283 :     long p  = Fp[k], e  = Fe[k], op = oj / upowuu(p,e);
    2303             :     long i;
    2304        3283 :     GEN  pf1 = NULL, w, wg, Be = cgetg(e+1,t_VEC);
    2305        3283 :     gel(Be,e) = cyc_pow(B, op);
    2306        3647 :     for(i=e-1; i>=1; i--) gel(Be,i) = cyc_pow(gel(Be,i+1), p);
    2307        3283 :     w = wpow(Fl_powu(s,op,deg),deg,p,e);
    2308        3283 :     wg = cgetg(e+2,t_VECSMALL);
    2309        3283 :     wg[e+1] = deg;
    2310        6930 :     for (i=e; i>=1; i--) wg[i] = ugcd(wg[i+1], w[i]);
    2311       24451 :     for (i=1; i<lg(O); i++) oX[i] = 0;
    2312        6909 :     for (f=1; f<=e; f++)
    2313             :     {
    2314             :       long sel, t;
    2315        3647 :       GEN Bel = gel(Be,f);
    2316        3647 :       dg *= p; el /= p;
    2317        3647 :       sel = Fl_powu(s,el,deg);
    2318        3647 :       if (DEBUGLEVEL >= 6) err_printf("GaloisConj: B=%Ps\n", Bel);
    2319        3647 :       sr  = ugcd(stpow(sel,p,deg),deg);
    2320        3647 :       if (DEBUGLEVEL >= 6)
    2321           0 :         err_printf("GaloisConj: exp %d: s=%ld [%ld] a=%ld w=%ld wg=%ld sr=%ld\n",
    2322           0 :             dg, sel, deg, a, w[f], wg[f+1], sr);
    2323        4543 :       for (t = 0; t < sr; t++)
    2324        4522 :         if ((a+t*w[f])%wg[f+1]==0)
    2325             :         {
    2326             :           long i, j, k, st;
    2327       41496 :           for (i = 1; i < lg(X); i++) X[i] = 0;
    2328       20454 :           for (i = 0; i < lg(X)-1; i+=dg)
    2329       34426 :             for (j = 1, k = p, st = t; k <= dg; j++, k += p)
    2330             :             {
    2331       18424 :               X[k+i] = (oX[j+i] + st)%deg;
    2332       18424 :               st = (t + st*osel)%deg;
    2333             :             }
    2334        4452 :           pf1 = testpermutation(O, Bel, X, sel, p, sr, td);
    2335        4452 :           if (pf1) break;
    2336             :         }
    2337        3647 :       if (!pf1) return NULL;
    2338       31024 :       for (i=1; i<lg(O); i++) oX[i] = X[i];
    2339        3626 :       osel = sel; a = (a+t*w[f])%deg;
    2340             :     }
    2341        3262 :     pf = perm_mul(pf, perm_pow(pf1, el));
    2342             :   }
    2343        3262 :   return gerepileuptoleaf(av, pf);
    2344             : }
    2345             : 
    2346             : static GEN
    2347           0 : FlxV_Flx_gcd(GEN x, GEN T, ulong p)
    2348           0 : { pari_APPLY_same(Flx_normalize(Flx_gcd(gel(x,i),T,p),p)) }
    2349             : 
    2350             : static GEN
    2351           0 : Flx_FlxV_minpolymod(GEN y, GEN x, ulong p)
    2352           0 : { pari_APPLY_same(Flxq_minpoly(Flx_rem(y, gel(x,i), p), gel(x,i), p)) }
    2353             : 
    2354             : static GEN
    2355           0 : FlxV_minpolymod(GEN x, GEN y, ulong p)
    2356           0 : { pari_APPLY_same(Flx_FlxV_minpolymod(gel(x,i), y, p)) }
    2357             : 
    2358             : static GEN
    2359           0 : factperm(GEN x)
    2360             : {
    2361           0 :   pari_APPLY_same(gen_indexsort(gel(x,i), (void*)cmp_Flx, cmp_nodata))
    2362             : }
    2363             : 
    2364             : /* compute (prod p_i^e_i)(1) */
    2365             : 
    2366             : static long
    2367           0 : permprodeval(GEN p, GEN e, long s)
    2368             : {
    2369           0 :   long i, j, l = lg(p);
    2370           0 :   for (i=l-1; i>=1; i--)
    2371             :   {
    2372           0 :     GEN pi = gel(p,i);
    2373           0 :     long ei = uel(e,i);
    2374           0 :     for(j = 1; j <= ei; j++)
    2375           0 :       s = uel(pi, s);
    2376             :   }
    2377           0 :   return s;
    2378             : }
    2379             : 
    2380             : static GEN
    2381           0 : pc_to_perm(GEN pc, GEN gen, long n)
    2382             : {
    2383           0 :   long i, l = lg(pc);
    2384           0 :   GEN s = identity_perm(n);
    2385           0 :   for (i=1; i<l; i++)
    2386           0 :     s = perm_mul(gel(gen,pc[i]),s);
    2387           0 :   return s;
    2388             : }
    2389             : 
    2390             : static GEN
    2391           0 : genorbit(GEN ordH, GEN permfact_Hp, long fr, long n, long k, long j)
    2392             : {
    2393           0 :   pari_sp av = avma;
    2394           0 :   long l = lg(gel(permfact_Hp,1))-1, no = 1, b, i;
    2395           0 :   GEN W = zero_zv(l);
    2396           0 :   GEN orb = cgetg(l+1, t_VECSMALL);
    2397           0 :   GEN gen = cgetg(l+1, t_VEC);
    2398           0 :   GEN E = cgetg(k+1, t_VECSMALL);
    2399           0 :   for(b = 0; b < n; b++)
    2400             :   {
    2401           0 :     long bb = b, s;
    2402           0 :     for(i = 1; i <= k; i++)
    2403             :     {
    2404           0 :       uel(E,i) = bb % uel(ordH,i);
    2405           0 :       bb /= uel(ordH,i);
    2406             :     }
    2407           0 :     if (E[j]) continue;
    2408           0 :     s = permprodeval(permfact_Hp, E, fr);
    2409           0 :     if (s>lg(W)-1) pari_err_BUG("W1");
    2410           0 :     if (W[s]) continue;
    2411           0 :     W[s] = 1;
    2412           0 :     if (no > l) pari_err_BUG("genorbit");
    2413           0 :     uel(orb,no) = s;
    2414           0 :     gel(gen,no) = zv_copy(E);
    2415           0 :     no++;
    2416             :   }
    2417           0 :   if(no<l) pari_err_BUG("genorbit");
    2418           0 :   return gerepilecopy(av, mkvec2(orb,gen));
    2419             : }
    2420             : 
    2421           0 : INLINE GEN br_get(GEN br, long i, long j) { return gmael(br,j,i-j); }
    2422           0 : static GEN pcgrp_get_ord(GEN G) { return gel(G,1); }
    2423           0 : static GEN pcgrp_get_pow(GEN G) { return gel(G,2); }
    2424           0 : static GEN pcgrp_get_br(GEN G)  { return gel(G,3); }
    2425             : 
    2426             : static GEN
    2427         945 : cyclic_pc(long n)
    2428             : {
    2429         945 :   return mkvec3(mkvecsmall(n),mkvec(cgetg(1,t_VECSMALL)), mkvec(cgetg(1,t_VEC)));
    2430             : }
    2431             : 
    2432             : static GEN
    2433           0 : pc_normalize(GEN g, GEN G)
    2434             : {
    2435           0 :   long i, l = lg(g)-1, o = 1;
    2436           0 :   GEN ord = pcgrp_get_ord(G), pw = pcgrp_get_pow(G), br = pcgrp_get_br(G);
    2437           0 :   for (i = 1; i < l; i++)
    2438             :   {
    2439           0 :     if (g[i] == g[i+1])
    2440             :     {
    2441           0 :       if (++o == ord[g[i]])
    2442             :       {
    2443           0 :         GEN v = vecsmall_concat(vecslice(g,1,i-o+1),gel(pw,g[i]));
    2444           0 :         GEN w = vecsmall_concat(v,vecslice(g,i+2,l));
    2445           0 :         return pc_normalize(w, G);
    2446             :       }
    2447             :     }
    2448           0 :     else if (g[i] > g[i+1])
    2449             :     {
    2450           0 :       GEN v = vecsmall_concat(vecslice(g,1,i-1), br_get(br,g[i],g[i+1]));
    2451           0 :       GEN w = vecsmall_concat(mkvecsmall2(g[i+1],g[i]),vecslice(g,i+2,l));
    2452           0 :       v = vecsmall_concat(v, w);
    2453           0 :       return pc_normalize(v, G);
    2454             :     }
    2455           0 :     else o = 1;
    2456             :   }
    2457           0 :   return g;
    2458             : }
    2459             : 
    2460             : static GEN
    2461           0 : pc_inv(GEN g, GEN G)
    2462             : {
    2463           0 :   long i, l = lg(g);
    2464           0 :   GEN ord = pcgrp_get_ord(G), pw  = pcgrp_get_pow(G);
    2465           0 :   GEN v = cgetg(l, t_VEC);
    2466           0 :   if (l==1) return v;
    2467           0 :   for(i = 1; i < l; i++)
    2468             :   {
    2469           0 :     ulong gi = uel(g,i);
    2470           0 :     gel(v,l-i) = vecsmall_concat(pc_inv(gel(pw, gi), G),
    2471           0 :                                  const_vecsmall(uel(ord,gi)-1,gi));
    2472             :   }
    2473           0 :   return pc_normalize(shallowconcat1(v), G);
    2474             : }
    2475             : 
    2476             : static GEN
    2477           0 : pc_mul(GEN g, GEN h, GEN G)
    2478             : {
    2479           0 :   return pc_normalize(vecsmall_concat(g,h), G);
    2480             : }
    2481             : 
    2482             : static GEN
    2483           0 : pc_bracket(GEN g, GEN h, GEN G)
    2484             : {
    2485           0 :   GEN gh = pc_mul(g, h, G);
    2486           0 :   GEN hg = pc_mul(h, g, G);
    2487           0 :   long i, l1 = lg(gh), l2 = lg(hg), lm = minss(l1,l2);
    2488           0 :   for (i = 1; i < lm; i++)
    2489           0 :     if (gh[l1-i] != hg[l2-i]) break;
    2490           0 :   return pc_mul(vecsmall_shorten(gh,l1-i), pc_inv(vecsmall_shorten(hg,l2-i), G), G);
    2491             : }
    2492             : 
    2493             : static GEN
    2494           0 : pc_exp(GEN v)
    2495             : {
    2496           0 :   long i, l = lg(v);
    2497           0 :   GEN w = cgetg(l, t_VEC);
    2498           0 :   if (l==1) return w;
    2499           0 :   for (i = 1; i < l; i++)
    2500           0 :     gel(w,i) = const_vecsmall(v[i], i+1);
    2501           0 :   return shallowconcat1(w);
    2502             : }
    2503             : static GEN
    2504           0 : vecsmall_increase(GEN x)
    2505           0 : { pari_APPLY_ulong(x[i]+1) }
    2506             : 
    2507             : static GEN
    2508           0 : vecvecsmall_increase(GEN x)
    2509           0 : { pari_APPLY_same(vecsmall_increase(gel(x,i))) }
    2510             : 
    2511             : static GEN
    2512           0 : pcgrp_lift(GEN G, long deg)
    2513             : {
    2514           0 :   GEN ord = pcgrp_get_ord(G), pw  = pcgrp_get_pow(G), br = pcgrp_get_br(G);
    2515           0 :   long i, l = lg(br);
    2516           0 :   GEN Ord = vecsmall_prepend(ord, deg);
    2517           0 :   GEN Pw = vec_prepend(vecvecsmall_increase(pw), cgetg(1,t_VECSMALL));
    2518           0 :   GEN Br = cgetg(l+1, t_VEC);
    2519           0 :   gel(Br,1) = const_vec(l-1, cgetg(1, t_VECSMALL));
    2520           0 :   for (i = 1; i < l; i++)
    2521           0 :     gel(Br,i+1) = vecvecsmall_increase(gel(br, i));
    2522           0 :   return mkvec3(Ord, Pw, Br);
    2523             : }
    2524             : 
    2525             : static GEN
    2526           0 : brl_add(GEN x, GEN a)
    2527             : {
    2528           0 :   pari_APPLY_same(vecsmall_concat(const_vecsmall(uel(a,i),1),gel(x,i)))
    2529             : }
    2530             : 
    2531             : static void
    2532           0 : pcgrp_insert(GEN G, long j, GEN a)
    2533             : {
    2534           0 :   GEN pw  = pcgrp_get_pow(G), br = pcgrp_get_br(G);
    2535           0 :   gel(pw,j) = vecsmall_concat(gel(a,1),gel(pw, j));
    2536           0 :   gel(br,j) = brl_add(gel(br, j), gel(a,2));
    2537           0 : }
    2538             : 
    2539             : static long
    2540           0 : getfr(GEN f, GEN h)
    2541             : {
    2542           0 :   long i, l = lg(f);
    2543           0 :   for (i = 1; i < l; i++)
    2544           0 :     if (zv_equal(gel(f,i), h)) return i;
    2545           0 :   pari_err_BUG("galoisinit");
    2546           0 :   return 0;
    2547             : }
    2548             : 
    2549             : static long
    2550           0 : get_pow(GEN pf, long o, GEN pw, GEN gen)
    2551             : {
    2552           0 :   long i, n  = lg(pf)-1;
    2553           0 :   GEN p1 = perm_pow(pf, o);
    2554           0 :   GEN p2 = pc_to_perm(pw, gen, n);
    2555           0 :   for(i = 0; ; i++)
    2556             :   {
    2557           0 :     if (zv_equal(p1, p2)) break;
    2558           0 :     p2 = perm_mul(gel(gen,1), p2);
    2559             :   }
    2560           0 :   return i;
    2561             : }
    2562             : 
    2563             : struct galois_perm
    2564             : {
    2565             :   GEN L;
    2566             :   GEN M;
    2567             :   GEN den;
    2568             :   GEN mod, mod2;
    2569             :   long x;
    2570             :   GEN cache;
    2571             : };
    2572             : 
    2573             : static void
    2574           0 : galoisperm_init(struct galois_perm *gp, GEN L, GEN M, GEN den, GEN mod, GEN mod2, long x)
    2575             : {
    2576           0 :   gp->L = L;
    2577           0 :   gp->M = M;
    2578           0 :   gp->den = den;
    2579           0 :   gp->mod = mod;
    2580           0 :   gp->mod2 = mod2;
    2581           0 :   gp->x = x;
    2582           0 :   gp->cache = zerovec(lg(L)-1);
    2583           0 : }
    2584             : 
    2585             : static void
    2586           0 : galoisperm_free(struct galois_perm *gp)
    2587             : {
    2588           0 :   long i, l = lg(gp->cache);
    2589           0 :   for (i=1; i<l; i++)
    2590           0 :     if (!isintzero(gel(gp->cache,i)))
    2591           0 :       gunclone(gel(gp->cache,i));
    2592           0 : }
    2593             : 
    2594             : static GEN
    2595           0 : permtoaut(GEN p, struct galois_perm *gp)
    2596             : {
    2597           0 :   pari_sp av = avma;
    2598           0 :   if (isintzero(gel(gp->cache,p[1])))
    2599             :   {
    2600           0 :     GEN pol = permtopol(p, gp->L, gp->M, gp->den, gp->mod, gp->mod2, gp->x);
    2601           0 :     gel(gp->cache,p[1]) = gclone(pol);
    2602             :   }
    2603           0 :   set_avma(av);
    2604           0 :   return gel(gp->cache,p[1]);
    2605             : }
    2606             : 
    2607             : static GEN
    2608           0 : pc_evalcache(GEN W, GEN u, GEN ss, GEN genG, GEN T, GEN p, struct galois_perm *gp)
    2609             : {
    2610             :   GEN sp, v;
    2611             :   long ns;
    2612           0 :   if (lg(ss) == 1) return gel(u,2);
    2613           0 :   sp = pc_to_perm(ss, genG, degpol(T));
    2614           0 :   ns = sp[1];
    2615           0 :   if (!isintzero(gel(W,ns))) return gel(W,ns);
    2616           0 :   v = RgX_to_FpX(permtoaut(sp, gp), p);
    2617           0 :   gel(W,ns) = FpX_FpXQV_eval(v, u, T, p);
    2618           0 :   return gel(W,ns);
    2619             : }
    2620             : 
    2621             : static ulong
    2622           0 : findp(GEN D, GEN P, GEN S, long o, GEN *Tmod)
    2623             : {
    2624             :   forprime_t iter;
    2625             :   ulong p;
    2626           0 :   long n = degpol(P);
    2627           0 :   u_forprime_init(&iter, n*maxss(expu(n)-3, 2), ULONG_MAX);
    2628           0 :   while ((p = u_forprime_next(&iter)))
    2629             :   {
    2630             :     GEN F, F1, Sp;
    2631           0 :     if (smodis(D, p) == 0)
    2632           0 :       continue;
    2633           0 :     F = gel(Flx_factor(ZX_to_Flx(P, p), p), 1);
    2634           0 :     F1 = gel(F,1);
    2635           0 :     if (degpol(F1) != o)
    2636           0 :       continue;
    2637           0 :     Sp = RgX_to_Flx(S, p);
    2638           0 :     if (gequal(Flx_rem(Sp, F1, p), Flx_Frobenius(F1, p)))
    2639             :     {
    2640           0 :       *Tmod = FlxV_to_ZXV(F);
    2641           0 :       return p;
    2642             :     }
    2643             :   }
    2644           0 :   return 0;
    2645             : }
    2646             : 
    2647             : static GEN
    2648           0 : nilp_froblift(GEN genG, GEN autH, long j, GEN pcgrp,
    2649             :   GEN idp, GEN incl, GEN H, struct galois_lift *gl, struct galois_perm *gp)
    2650             : {
    2651           0 :   pari_sp av = avma;
    2652           0 :   GEN T = gl->T, p = gl->p, pe = gl->Q;
    2653           0 :   ulong pp = itou(p);
    2654           0 :   long e   = gl->e;
    2655           0 :   GEN pf   = cgetg(lg(gl->L), t_VECSMALL);
    2656           0 :   GEN Tp   = ZX_to_Flx(T, pp);
    2657           0 :   GEN Hp   = ZX_to_Flx(H, pp);
    2658           0 :   GEN ord = pcgrp_get_ord(pcgrp);
    2659           0 :   GEN pcp = gel(pcgrp_get_pow(pcgrp),j+1);
    2660           0 :   long o  = uel(ord,1);
    2661           0 :   GEN ordH = vecslice(ord,2,lg(ord)-1);
    2662           0 :   long n = zv_prod(ordH), k = lg(ordH)-1, l = k-j, m = upowuu(o, l), v = varn(T);
    2663           0 :   GEN factTp = gel(Flx_factor(Tp, pp), 1);
    2664           0 :   long fp = degpol(gel(factTp, 1));
    2665           0 :   GEN frobp = Flxq_autpow(Flx_Frobenius(Tp, pp), fp-1, Tp, pp);
    2666           0 :   GEN frob = ZpX_ZpXQ_liftroot(T, Flx_to_ZX(frobp), T, p, e);
    2667           0 :   if (galoisfrobeniustest(frob, gl, pf))
    2668             :   {
    2669           0 :     GEN pfi = perm_inv(pf);
    2670           0 :     long d = get_pow(pfi, uel(ord,j+1), pcp, genG);
    2671           0 :     return mkvec3(pfi, mkvec2(const_vecsmall(d,1),zero_zv(l+1)), gel(factTp, 1));
    2672             :   }
    2673             :   else
    2674             :   {
    2675           0 :     GEN frobG = FpXQ_powers(frob, usqrt(degpol(T)), T, pe);
    2676           0 :     GEN autHp = RgXV_to_FlxV(autH,pp);
    2677           0 :     GEN inclp = RgX_to_Flx(incl,pp);
    2678           0 :     GEN factHp = gel(Flx_factor(Hp, pp),1);
    2679           0 :     long fr = getfr(factHp, idp);
    2680           0 :     GEN minHp  = FlxV_minpolymod(autHp, factHp, pp);
    2681           0 :     GEN permfact_Hp = factperm(minHp);
    2682           0 :     GEN permfact_Gp = FlxV_Flx_gcd(FlxC_Flxq_eval(factHp, inclp, Tp, pp), Tp, pp);
    2683           0 :     GEN bezout_Gpe = bezout_lift_fact(T, FlxV_to_ZXV(permfact_Gp), p, e);
    2684           0 :     GEN id = gmael(Flx_factor(gel(permfact_Gp, fr),pp),1,1);
    2685           0 :     GEN orbgen = genorbit(ordH, permfact_Hp, fr, n, k, j);
    2686           0 :     GEN orb = gel(orbgen,1), gen = gel(orbgen,2);
    2687           0 :     long nborb = lg(orb)-1;
    2688           0 :     GEN A = cgetg(l+1, t_VECSMALL);
    2689           0 :     GEN W = zerovec(lg(gl->L)-1);
    2690           0 :     GEN br = pcgrp_get_br(pcgrp), brj = gcopy(gel(br, j+1));
    2691           0 :     GEN U = cgetg(nborb+1, t_VEC);
    2692             :     long a, b, i;
    2693           0 :     for(a = 0; a < m; a++)
    2694             :     {
    2695             :       pari_sp av2;
    2696           0 :       GEN B = pol_0(v);
    2697           0 :       long aa = a;
    2698           0 :       for(i = 1; i <= l; i++)
    2699             :       {
    2700           0 :         uel(A,i) = aa % o;
    2701           0 :         aa /= o;
    2702             :       }
    2703           0 :       gel(br,j+1) = brl_add(brj, A);
    2704           0 :       for(b = 1; b <= nborb; b++)
    2705             :       {
    2706           0 :         GEN br = pc_bracket(pc_exp(gel(gen,b)), mkvecsmall(j+1), pcgrp);
    2707           0 :         gel(U, b) = pc_evalcache(W, frobG, br, genG, T, pe, gp);
    2708             :       }
    2709           0 :       av2 = avma;
    2710           0 :       for(b = 1; b <= nborb; b++)
    2711             :       {
    2712           0 :         long s = permprodeval(permfact_Hp, gel(gen,b), fr);
    2713           0 :         B = FpX_add(B, FpXQ_mul(gel(U, b), gel(bezout_Gpe,s), T, pe), pe);
    2714             :       }
    2715           0 :       if (galoisfrobeniustest(B, gl, pf))
    2716             :       {
    2717           0 :         GEN pfi = perm_inv(pf);
    2718           0 :         long d = get_pow(pfi, uel(ord,j+1), pcp, genG);
    2719           0 :         gel(br,j+1) = brj;
    2720           0 :         return gerepilecopy(av,mkvec3(pfi,mkvec2(const_vecsmall(d,1),A),id));
    2721             :       }
    2722           0 :       set_avma(av2);
    2723             :     }
    2724           0 :     return gc_NULL(av);
    2725             :   }
    2726             : }
    2727             : 
    2728             : static GEN
    2729           0 : galoisgenlift_nilp(GEN PG, GEN O, GEN V, GEN T, GEN frob, GEN sigma,
    2730             :   struct galois_borne *gb, struct galois_frobenius *gf, struct galois_perm *gp)
    2731             : {
    2732           0 :   long j, n = degpol(T), deg = gf->deg;
    2733           0 :   ulong p = gf->p;
    2734           0 :   GEN L = gp->L, M =  gp->M, den = gp->den;
    2735           0 :   GEN S = fixedfieldinclusion(O, gel(V,2));
    2736           0 :   GEN incl = vectopol(S, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), varn(T));
    2737           0 :   GEN H = gel(V,3);
    2738           0 :   GEN PG1 = gmael(PG, 1, 1);
    2739           0 :   GEN PG2 = gmael(PG, 1, 2);
    2740           0 :   GEN PG3 = gmael(PG, 1, 3);
    2741           0 :   GEN PG4 = gmael(PG, 1, 4);
    2742           0 :   long lP = lg(PG1);
    2743           0 :   GEN PG5 = pcgrp_lift(gmael(PG, 1, 5), deg);
    2744           0 :   GEN res = cgetg(6, t_VEC), res1, res2, res3;
    2745           0 :   gel(res,1) = res1 = cgetg(lP + 1, t_VEC);
    2746           0 :   gel(res,2) = res2 = cgetg(lP + 1, t_VEC);
    2747           0 :   gel(res,3) = res3 = cgetg(lP + 1, t_VEC);
    2748           0 :   gel(res,4) = vecsmall_prepend(PG4, p);
    2749           0 :   gel(res,5) = PG5;
    2750           0 :   gel(res1, 1) = frob;
    2751           0 :   gel(res2, 1) = ZX_to_Flx(gel(gf->Tmod,1), p);
    2752           0 :   gel(res3, 1) = sigma;
    2753           0 :   for (j = 1; j < lP; j++)
    2754             :   {
    2755             :     struct galois_lift gl;
    2756           0 :     GEN Lden = makeLden(L,den,gb);
    2757             :     GEN pf;
    2758           0 :     initlift(T, den, uel(PG4,j), L, Lden, gb, &gl);
    2759           0 :     pf = nilp_froblift(vecslice(res1,1,j), PG3, j, PG5, gel(PG2,j), incl, H, &gl, gp);
    2760           0 :     if (!pf) return NULL;
    2761           0 :     if (DEBUGLEVEL>=2)
    2762           0 :       err_printf("found: %ld/%ld: %Ps: %Ps\n", n, j+1, gel(pf,2),gel(pf,1));
    2763           0 :     pcgrp_insert(PG5, j+1, gel(pf,2));
    2764           0 :     gel(res1, j+1) = gel(pf,1);
    2765           0 :     gel(res2, j+1) = gel(pf,3);
    2766           0 :     gel(res3, j+1) = gcopy(permtoaut(gel(pf,1), gp));
    2767             :   }
    2768           0 :   if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Fini!\n");
    2769           0 :   return res;
    2770             : }
    2771             : 
    2772             : static GEN
    2773        2240 : galoisgenlift(GEN PG, GEN Pg, GEN O, GEN L, GEN M, GEN frob,
    2774             :               struct galois_borne *gb, struct galois_frobenius *gf)
    2775             : {
    2776             :   struct galois_test td;
    2777             :   GEN res, res1;
    2778        2240 :   GEN PG1 = gel(PG, 1), PG2 = gel(PG, 2);
    2779        2240 :   long lP = lg(PG1), j, n = lg(L)-1;
    2780        2240 :   inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2781        2240 :   res = cgetg(3, t_VEC);
    2782        2240 :   gel(res,1) = res1 = cgetg(lP + 1, t_VEC);
    2783        2240 :   gel(res,2) = vecsmall_prepend(PG2, gf->deg);
    2784        2240 :   gel(res1, 1) = vecsmall_copy(frob);
    2785        5502 :   for (j = 1; j < lP; j++)
    2786             :   {
    2787        3283 :     GEN pf = galoisgenliftauto(O, gel(PG1, j), gf->psi[Pg[j]], n, &td);
    2788        3283 :     if (!pf) { freetest(&td); return NULL; }
    2789        3262 :     gel(res1, j+1) = pf;
    2790             :   }
    2791        2219 :   if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Fini!\n");
    2792        2219 :   freetest(&td);
    2793        2219 :   return res;
    2794             : }
    2795             : 
    2796             : static ulong
    2797        3206 : psi_order(GEN psi, ulong d)
    2798             : {
    2799        3206 :   long i, l = lg(psi);
    2800        3206 :   ulong s = 1;
    2801       10402 :   for (i=1; i<l; i++)
    2802        7196 :     s = clcm(s, d/cgcd(uel(psi, i)-1, d));
    2803        3206 :   return s;
    2804             : }
    2805             : 
    2806             : static GEN
    2807        3325 : galoisgen(GEN T, GEN L, GEN M, GEN den, GEN bad, struct galois_borne *gb,
    2808             :           const struct galois_analysis *ga)
    2809             : {
    2810             :   struct galois_test td;
    2811             :   struct galois_frobenius gf, ogf;
    2812        3325 :   pari_sp ltop = avma;
    2813        3325 :   long x, n = degpol(T), is_central;
    2814             :   long po;
    2815        3325 :   GEN sigma, res, frob, O, PG, V, ofrob = NULL;
    2816             : 
    2817        3325 :   if (!ga->deg) return NULL;
    2818        3325 :   x = varn(T);
    2819        3325 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: denominator:%Ps\n", den);
    2820        3325 :   if (n == 12 && ga->ord==3 && !ga->p4)
    2821             :   { /* A4 is very probable: test it first */
    2822          56 :     pari_sp av = avma;
    2823          56 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing A4 first\n");
    2824          56 :     inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2825          56 :     PG = a4galoisgen(&td);
    2826          56 :     freetest(&td);
    2827          56 :     if (PG) return gerepileupto(ltop, PG);
    2828           0 :     set_avma(av);
    2829             :   }
    2830        3269 :   if (n == 24 && ga->ord==3 && ga->p4)
    2831             :   { /* S4 is very probable: test it first */
    2832          49 :     pari_sp av = avma;
    2833             :     struct galois_lift gl;
    2834          49 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing S4 first\n");
    2835          49 :     initlift(T, den, ga->p4, L, makeLden(L,den,gb), gb, &gl);
    2836          49 :     PG = s4galoisgen(&gl);
    2837          49 :     if (PG) return gerepileupto(ltop, PG);
    2838           0 :     set_avma(av);
    2839             :   }
    2840        3220 :   if (n == 36 && ga->ord==3 && ga->p4)
    2841             :   { /* F36 is very probable: test it first */
    2842          14 :     pari_sp av = avma;
    2843             :     struct galois_lift gl;
    2844          14 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing 3x3:4 first (p=%ld)\n",ga->p4);
    2845          14 :     initlift(T, den, ga->p4, L, makeLden(L,den,gb), gb, &gl);
    2846          14 :     PG = f36galoisgen(&gl);
    2847          14 :     if (PG) return gerepileupto(ltop, PG);
    2848           0 :     set_avma(av);
    2849             :   }
    2850        3206 :   frob = galoisfindfrobenius(T, L, den, bad, &gf, gb, ga);
    2851        3206 :   if (!frob) return gc_NULL(ltop);
    2852        3206 :   po = psi_order(gf.psi, gf.deg);
    2853        3206 :   if (!(ga->group&ga_easy) && po < gf.deg && gf.deg/radicalu(gf.deg)%po == 0)
    2854             :   {
    2855           0 :     is_central = 1;
    2856           0 :     if (!bad) bad = gb->dis;
    2857           0 :     if (po > 1)
    2858             :     {
    2859           0 :       ofrob = frob; ogf = gf;
    2860           0 :       frob = perm_pow(frob, po);
    2861           0 :       gf.deg /= po;
    2862             :     }
    2863        3206 :   } else is_central = 0;
    2864        3206 :   sigma = permtopol(frob, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), x);
    2865        3206 :   if (is_central && gf.fp != gf.deg)
    2866           0 :   { gf.p = findp(bad, T, sigma, gf.deg, &gf.Tmod); gf.fp = gf.deg;
    2867           0 :     gf.psi = const_vecsmall(lg(gf.Tmod)-1, 1);
    2868             :   }
    2869        3206 :   if (gf.deg == n)        /* cyclic */
    2870             :   {
    2871         945 :     GEN Tp = ZX_to_Flx(gel(gf.Tmod,1), gf.p);
    2872         945 :     res = mkvec5(mkvec(frob), mkvec(Tp), mkvec(sigma), mkvecsmall(gf.p), cyclic_pc(n));
    2873         945 :     return gerepilecopy(ltop, res);
    2874             :   }
    2875        2261 :   O = perm_cycles(frob);
    2876        2261 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Frobenius:%Ps\n", sigma);
    2877        2261 :   PG = galoisgenfixedfield0(O, L, sigma, T, is_central ? bad: NULL,
    2878             :                                             is_central ? &V:  NULL, &gf, gb);
    2879        2261 :   if (PG == NULL) return gc_NULL(ltop);
    2880        2240 :   if (is_central && lg(gel(PG,1))!=3)
    2881           0 :   {
    2882             :     struct galois_perm gp;
    2883           0 :     galoisperm_init(&gp, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), varn(T));
    2884           0 :     res = galoisgenlift_nilp(PG, O, V, T, frob, sigma, gb, &gf, &gp);
    2885           0 :     galoisperm_free(&gp);
    2886             :   }
    2887             :   else
    2888             :   {
    2889        2240 :     if (is_central && po > 1)
    2890             :     { /* backtrack powering of frob */
    2891           0 :       frob = ofrob; gf = ogf;
    2892           0 :       O = perm_cycles(ofrob);
    2893           0 :       sigma = permtopol(ofrob, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), x);
    2894           0 :       PG = galoisgenfixedfield0(O, L, sigma, T, NULL, NULL, &gf, gb);
    2895           0 :       if (PG == NULL) return gc_NULL(ltop);
    2896             :     }
    2897        2240 :     res = galoisgenlift(gg_get_std(gel(PG,1)), gel(PG,2), O, L, M, frob, gb, &gf);
    2898             :   }
    2899        2240 :   if (!res) return gc_NULL(ltop);
    2900        2219 :   return gerepilecopy(ltop, res);
    2901             : }
    2902             : 
    2903             : /* T = polcyclo(N) */
    2904             : static GEN
    2905          77 : conjcyclo(GEN T, long N)
    2906             : {
    2907          77 :   pari_sp av = avma;
    2908          77 :   long i, k = 1, d = eulerphiu(N), v = varn(T);
    2909          77 :   GEN L = cgetg(d+1,t_COL);
    2910         756 :   for (i=1; i<=N; i++)
    2911         679 :     if (ugcd(i, N)==1)
    2912             :     {
    2913         336 :       GEN s = pol_xn(i, v);
    2914         336 :       if (i >= d) s = ZX_rem(s, T);
    2915         336 :       gel(L,k++) = s;
    2916             :     }
    2917          77 :   return gerepileupto(av, gen_sort(L, (void*)&gcmp, &gen_cmp_RgX));
    2918             : }
    2919             : 
    2920             : static GEN
    2921           0 : aut_to_groupelts(GEN aut, GEN L, ulong p)
    2922             : {
    2923           0 :   pari_sp av = avma;
    2924           0 :   long i, d = lg(aut)-1;
    2925           0 :   GEN P = ZV_to_Flv(L, p);
    2926           0 :   GEN N = FlxV_Flv_multieval(RgXV_to_FlxV(aut, p), P, p);
    2927           0 :   GEN q = perm_inv(vecsmall_indexsort(P));
    2928           0 :   GEN G = cgetg(d+1, t_VEC);
    2929           0 :   for (i=1; i<=d; i++)
    2930           0 :     gel(G,i) = perm_mul(vecsmall_indexsort(gel(N,i)), q);
    2931           0 :   return gerepilecopy(av, vecvecsmall_sort_shallow(G));
    2932             : }
    2933             : 
    2934             : static GEN
    2935           0 : galoisinitfromaut(GEN T, GEN aut)
    2936             : {
    2937           0 :   pari_sp ltop = avma;
    2938           0 :   GEN nf, A, G, L, M, grp, den=NULL;
    2939             :   struct galois_analysis ga;
    2940             :   struct galois_borne gb;
    2941             :   long n;
    2942             :   pari_timer ti;
    2943             : 
    2944           0 :   T = get_nfpol(T, &nf);
    2945           0 :   n = degpol(T);
    2946           0 :   if (nf)
    2947           0 :   { if (!den) den = nf_get_zkden(nf); }
    2948             :   else
    2949             :   {
    2950           0 :     if (n <= 0) pari_err_IRREDPOL("galoisinit",T);
    2951           0 :     RgX_check_ZX(T, "galoisinit");
    2952           0 :     if (!ZX_is_squarefree(T))
    2953           0 :       pari_err_DOMAIN("galoisinit","issquarefree(pol)","=",gen_0,T);
    2954           0 :     if (!gequal1(gel(T,n+2))) pari_err_IMPL("galoisinit(non-monic)");
    2955             :   }
    2956           0 :   if (!galoisanalysis(T, &ga, 1, NULL)) pari_err_IMPL("galoisinit");
    2957           0 :   gb.l = utoipos(ga.l);
    2958           0 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2959           0 :   den = galoisborne(T, den, &gb, degpol(T));
    2960           0 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisborne()");
    2961           0 :   L = ZpX_roots(T, gb.l, gb.valabs);
    2962           0 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "ZpX_roots");
    2963           0 :   M = FpV_invVandermonde(L, den, gb.ladicabs);
    2964           0 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "FpV_invVandermonde()");
    2965           0 :   A = aut_to_groupelts(aut, L, ga.l);
    2966           0 :   G = groupelts_to_group(A);
    2967           0 :   if (!G) pari_err_IMPL("galoisinit(non WSS group)");
    2968           0 :   A = group_elts(G,n);
    2969           0 :   grp = cgetg(9, t_VEC);
    2970           0 :   gel(grp,1) = T;
    2971           0 :   gel(grp,2) = mkvec3(utoipos(ga.l), utoipos(gb.valabs), gb.ladicabs);
    2972           0 :   gel(grp,3) = L;
    2973           0 :   gel(grp,4) = M;
    2974           0 :   gel(grp,5) = den;
    2975           0 :   gel(grp,6) = A;
    2976           0 :   gel(grp,7) = gel(G,1);
    2977           0 :   gel(grp,8) = gel(G,2);
    2978           0 :   return gerepilecopy(ltop, grp);
    2979             : }
    2980             : 
    2981             : /* T: polynomial or nf, den multiple of common denominator of solutions or
    2982             :  * NULL (unknown). If T is nf, and den unknown, use den = denom(nf.zk) */
    2983             : static GEN
    2984        4998 : galoisconj4_main(GEN T, GEN den, long flag)
    2985             : {
    2986        4998 :   pari_sp ltop = avma;
    2987             :   GEN nf, G, L, M, aut, grp;
    2988             :   struct galois_analysis ga;
    2989             :   struct galois_borne gb;
    2990             :   long n;
    2991             :   pari_timer ti;
    2992             : 
    2993        4998 :   T = get_nfpol(T, &nf);
    2994        4998 :   n = poliscyclo(T);
    2995        4998 :   if (n) return flag? galoiscyclo(n, varn(T)): conjcyclo(T, n);
    2996        4697 :   n = degpol(T);
    2997        4697 :   if (nf)
    2998        3290 :   { if (!den) den = nf_get_zkden(nf); }
    2999             :   else
    3000             :   {
    3001        1407 :     if (n <= 0) pari_err_IRREDPOL("galoisinit",T);
    3002        1407 :     RgX_check_ZX(T, "galoisinit");
    3003        1407 :     if (!ZX_is_squarefree(T))
    3004           7 :       pari_err_DOMAIN("galoisinit","issquarefree(pol)","=",gen_0,T);
    3005        1400 :     if (!gequal1(gel(T,n+2))) pari_err_IMPL("galoisinit(non-monic)");
    3006             :   }
    3007        4683 :   if (n == 1)
    3008             :   {
    3009          21 :     if (!flag) { G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G;}
    3010          21 :     ga.l = 3;
    3011          21 :     ga.deg = 1;
    3012          21 :     den = gen_1;
    3013             :   }
    3014        4662 :   else if (!galoisanalysis(T, &ga, 1, NULL)) return gc_NULL(ltop);
    3015             : 
    3016        2569 :   if (den)
    3017             :   {
    3018        1932 :     if (typ(den) != t_INT) pari_err_TYPE("galoisconj4 [2nd arg integer]", den);
    3019        1932 :     den = absi_shallow(den);
    3020             :   }
    3021        2569 :   gb.l = utoipos(ga.l);
    3022        2569 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    3023        2569 :   den = galoisborne(T, den, &gb, degpol(T));
    3024        2569 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisborne()");
    3025        2569 :   L = ZpX_roots(T, gb.l, gb.valabs);
    3026        2569 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "ZpX_roots");
    3027        2569 :   M = FpV_invVandermonde(L, den, gb.ladicabs);
    3028        2569 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "FpV_invVandermonde()");
    3029        2569 :   if (n == 1)
    3030             :   {
    3031          21 :     G = cgetg(3, t_VEC);
    3032          21 :     gel(G,1) = cgetg(1, t_VEC);
    3033          21 :     gel(G,2) = cgetg(1, t_VECSMALL);
    3034             :   }
    3035             :   else
    3036        2548 :     G = gg_get_std(galoisgen(T, L, M, den, NULL, &gb, &ga));
    3037        2569 :   if (DEBUGLEVEL >= 6) err_printf("GaloisConj: %Ps\n", G);
    3038        2569 :   if (!G) return gc_NULL(ltop);
    3039        2534 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    3040        2534 :   grp = cgetg(9, t_VEC);
    3041        2534 :   gel(grp,1) = T;
    3042        2534 :   gel(grp,2) = mkvec3(utoipos(ga.l), utoipos(gb.valabs), gb.ladicabs);
    3043        2534 :   gel(grp,3) = L;
    3044        2534 :   gel(grp,4) = M;
    3045        2534 :   gel(grp,5) = den;
    3046        2534 :   gel(grp,6) = group_elts(G,n);
    3047        2534 :   gel(grp,7) = gel(G,1);
    3048        2534 :   gel(grp,8) = gel(G,2);
    3049        2534 :   if (flag) return gerepilecopy(ltop, grp);
    3050         980 :   aut = galoisvecpermtopol(grp, gal_get_group(grp), gb.ladicabs, shifti(gb.ladicabs,-1));
    3051         980 :   settyp(aut, t_COL);
    3052         980 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "Computation of polynomials");
    3053         980 :   return gerepileupto(ltop, gen_sort(aut, (void*)&gcmp, &gen_cmp_RgX));
    3054             : }
    3055             : 
    3056             : /* Heuristic computation of #Aut(T), pinit = first prime to be tested */
    3057             : long
    3058         945 : numberofconjugates(GEN T, long pinit)
    3059             : {
    3060         945 :   pari_sp av = avma;
    3061         945 :   long c, nbtest, nbmax, n = degpol(T);
    3062             :   ulong p;
    3063             :   forprime_t S;
    3064             : 
    3065         945 :   if (n == 1) return 1;
    3066         945 :   nbmax = (n < 10)? 20: (n<<1) + 1;
    3067         945 :   nbtest = 0;
    3068             : #if 0
    3069             :   c = ZX_sturm(T); c = ugcd(c, n-c); /* too costly: finite primes are cheaper */
    3070             : #else
    3071         945 :   c = n;
    3072             : #endif
    3073         945 :   u_forprime_init(&S, pinit, ULONG_MAX);
    3074       10521 :   while((p = u_forprime_next(&S)))
    3075             :   {
    3076       10521 :     GEN L, Tp = ZX_to_Flx(T,p);
    3077             :     long i, nb;
    3078       10521 :     if (!Flx_is_squarefree(Tp, p)) continue;
    3079             :     /* unramified */
    3080        8876 :     nbtest++;
    3081        8876 :     L = Flx_nbfact_by_degree(Tp, &nb, p); /* L[i] = #factors of degree i */
    3082        8876 :     if (L[n/nb] == nb) {
    3083        6538 :       if (c == n && nbtest > 10) break; /* probably Galois */
    3084             :     }
    3085             :     else
    3086             :     {
    3087        3248 :       c = ugcd(c, L[1]);
    3088       30100 :       for (i = 2; i <= n; i++)
    3089       27419 :         if (L[i]) { c = ugcd(c, L[i]*i); if (c == 1) break; }
    3090        3248 :       if (c == 1) break;
    3091             :     }
    3092        8274 :     if (nbtest == nbmax) break;
    3093        7931 :     if (DEBUGLEVEL >= 6)
    3094           0 :       err_printf("NumberOfConjugates [%ld]:c=%ld,p=%ld\n", nbtest,c,p);
    3095        7931 :     set_avma(av);
    3096             :   }
    3097         945 :   if (DEBUGLEVEL >= 2) err_printf("NumberOfConjugates:c=%ld,p=%ld\n", c, p);
    3098         945 :   return gc_long(av,c);
    3099             : }
    3100             : static GEN
    3101           0 : galoisconj4(GEN nf, GEN d)
    3102             : {
    3103           0 :   pari_sp av = avma;
    3104             :   GEN G, T;
    3105           0 :   G = galoisconj4_main(nf, d, 0);
    3106           0 :   if (G) return G; /* Success */
    3107           0 :   set_avma(av); T = get_nfpol(nf, &nf);
    3108           0 :   G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G; /* Fail */
    3109             : 
    3110             : }
    3111             : 
    3112             : /* d multiplicative bound for the automorphism's denominators */
    3113             : GEN
    3114       12369 : galoisconj(GEN nf, GEN d)
    3115             : {
    3116       12369 :   pari_sp av = avma;
    3117       12369 :   GEN G, NF, T = get_nfpol(nf,&NF);
    3118       12369 :   if (degpol(T) == 2)
    3119             :   { /* fast shortcut */
    3120       10374 :     GEN a = gel(T,4), b = gel(T,3);
    3121       10374 :     long v = varn(T);
    3122       10374 :     RgX_check_ZX(T, "galoisconj");
    3123       10374 :     if (!gequal1(a)) pari_err_IMPL("galoisconj(non-monic)");
    3124       10374 :     G = cgetg(3, t_COL);
    3125       10374 :     gel(G,1) = deg1pol_shallow(gen_m1, negi(b), v);
    3126       10374 :     gel(G,2) = pol_x(v);
    3127       10374 :     return G;
    3128             :   }
    3129        1995 :   G = galoisconj4_main(nf, d, 0);
    3130        1995 :   if (G) return G; /* Success */
    3131         938 :   set_avma(av); return galoisconj1(nf);
    3132             : }
    3133             : 
    3134             : /* FIXME: obsolete, use galoisconj(nf, d) directly */
    3135             : GEN
    3136          49 : galoisconj0(GEN nf, long flag, GEN d, long prec)
    3137             : {
    3138             :   (void)prec;
    3139          49 :   switch(flag) {
    3140          42 :     case 2:
    3141          42 :     case 0: return galoisconj(nf, d);
    3142           7 :     case 1: return galoisconj1(nf);
    3143           0 :     case 4: return galoisconj4(nf, d);
    3144             :   }
    3145           0 :   pari_err_FLAG("nfgaloisconj");
    3146             :   return NULL; /*LCOV_EXCL_LINE*/
    3147             : }
    3148             : 
    3149             : /******************************************************************************/
    3150             : /* Galois theory related algorithms                                           */
    3151             : /******************************************************************************/
    3152             : GEN
    3153       21434 : checkgal(GEN gal)
    3154             : {
    3155       21434 :   if (typ(gal) == t_POL) pari_err_TYPE("checkgal [apply galoisinit first]",gal);
    3156       21434 :   if (typ(gal) != t_VEC || lg(gal) != 9) pari_err_TYPE("checkgal",gal);
    3157       21427 :   return gal;
    3158             : }
    3159             : 
    3160             : GEN
    3161        3003 : galoisinit(GEN nf, GEN den)
    3162             : {
    3163             :   GEN G;
    3164        3003 :   if (is_vec_t(typ(nf)) && lg(nf)==3 && is_vec_t(typ(gel(nf,2))))
    3165           0 :     return galoisinitfromaut(gel(nf,1), gel(nf,2));
    3166        3003 :   G = galoisconj4_main(nf, den, 1);
    3167        2989 :   return G? G: gen_0;
    3168             : }
    3169             : 
    3170             : static GEN
    3171       14973 : galoispermtopol_i(GEN gal, GEN perm, GEN mod, GEN mod2)
    3172             : {
    3173       14973 :   switch (typ(perm))
    3174             :   {
    3175       14735 :     case t_VECSMALL:
    3176       14735 :       return permtopol(perm, gal_get_roots(gal), gal_get_invvdm(gal),
    3177             :                              gal_get_den(gal), mod, mod2,
    3178       14735 :                              varn(gal_get_pol(gal)));
    3179         238 :     case t_VEC: case t_COL: case t_MAT:
    3180         238 :       return galoisvecpermtopol(gal, perm, mod, mod2);
    3181             :   }
    3182           0 :   pari_err_TYPE("galoispermtopol", perm);
    3183             :   return NULL; /* LCOV_EXCL_LINE */
    3184             : }
    3185             : 
    3186             : GEN
    3187       14973 : galoispermtopol(GEN gal, GEN perm)
    3188             : {
    3189       14973 :   pari_sp av = avma;
    3190             :   GEN mod, mod2;
    3191       14973 :   gal = checkgal(gal);
    3192       14973 :   mod = gal_get_mod(gal);
    3193       14973 :   mod2 = shifti(mod,-1);
    3194       14973 :   return gerepilecopy(av, galoispermtopol_i(gal, perm, mod, mod2));
    3195             : }
    3196             : 
    3197             : GEN
    3198          84 : galoiscosets(GEN O, GEN perm)
    3199             : {
    3200          84 :   long i, j, k, u, f, l = lg(O);
    3201          84 :   GEN RC, C = cgetg(l,t_VECSMALL), o = gel(O,1);
    3202          84 :   pari_sp av = avma;
    3203          84 :   f = lg(o); u = o[1]; RC = zero_zv(lg(perm)-1);
    3204         357 :   for(i=1,j=1; j<l; i++)
    3205             :   {
    3206         273 :     GEN p = gel(perm,i);
    3207         273 :     if (RC[ p[u] ]) continue;
    3208         693 :     for(k=1; k<f; k++) RC[ p[ o[k] ] ] = 1;
    3209         217 :     C[j++] = i;
    3210             :   }
    3211          84 :   set_avma(av); return C;
    3212             : }
    3213             : 
    3214             : static GEN
    3215          84 : fixedfieldfactor(GEN L, GEN O, GEN perm, GEN M, GEN den, GEN mod, GEN mod2,
    3216             :                  long x,long y)
    3217             : {
    3218          84 :   pari_sp ltop = avma;
    3219          84 :   long i, j, k, l = lg(O), lo = lg(gel(O,1));
    3220          84 :   GEN V, res, cosets = galoiscosets(O,perm), F = cgetg(lo+1,t_COL);
    3221             : 
    3222          84 :   gel(F, lo) = gen_1;
    3223          84 :   if (DEBUGLEVEL>=4) err_printf("GaloisFixedField:cosets=%Ps \n",cosets);
    3224          84 :   if (DEBUGLEVEL>=6) err_printf("GaloisFixedField:den=%Ps mod=%Ps \n",den,mod);
    3225          84 :   V = cgetg(l,t_COL); res = cgetg(l,t_VEC);
    3226         301 :   for (i = 1; i < l; i++)
    3227             :   {
    3228         217 :     pari_sp av = avma;
    3229         217 :     GEN G = cgetg(l,t_VEC), Lp = vecpermute(L, gel(perm, cosets[i]));
    3230         924 :     for (k = 1; k < l; k++)
    3231         707 :       gel(G,k) = FpV_roots_to_pol(vecpermute(Lp, gel(O,k)), mod, x);
    3232         693 :     for (j = 1; j < lo; j++)
    3233             :     {
    3234        1708 :       for(k = 1; k < l; k++) gel(V,k) = gmael(G,k,j+1);
    3235         476 :       gel(F,j) = vectopol(V, M, den, mod, mod2, y);
    3236             :     }
    3237         217 :     gel(res,i) = gerepileupto(av,gtopolyrev(F,x));
    3238             :   }
    3239          84 :   return gerepileupto(ltop,res);
    3240             : }
    3241             : 
    3242             : static void
    3243        2597 : chk_perm(GEN perm, long n)
    3244             : {
    3245        2597 :   if (typ(perm) != t_VECSMALL || lg(perm)!=n+1)
    3246           0 :     pari_err_TYPE("galoisfixedfield", perm);
    3247        2597 : }
    3248             : 
    3249             : static int
    3250        6944 : is_group(GEN g)
    3251             : {
    3252        6937 :   return typ(g)==t_VEC && lg(g)==3 && typ(gel(g,1))==t_VEC
    3253       13881 :       && typ(gel(g,2))==t_VECSMALL;
    3254             : }
    3255             : 
    3256             : GEN
    3257        1813 : galoisfixedfield(GEN gal, GEN perm, long flag, long y)
    3258             : {
    3259        1813 :   pari_sp ltop = avma;
    3260             :   GEN T, L, P, S, PL, O, res, mod, mod2, OL, sym;
    3261             :   long vT, n, i;
    3262        1813 :   if (flag<0 || flag>2) pari_err_FLAG("galoisfixedfield");
    3263        1813 :   gal = checkgal(gal); T = gal_get_pol(gal);
    3264        1813 :   vT = varn(T);
    3265        1813 :   L = gal_get_roots(gal); n = lg(L)-1;
    3266        1813 :   mod = gal_get_mod(gal);
    3267        1813 :   if (typ(perm) == t_VEC)
    3268             :   {
    3269        1407 :     if (is_group(perm)) perm = gel(perm, 1);
    3270        3598 :     for (i = 1; i < lg(perm); i++) chk_perm(gel(perm,i), n);
    3271        1407 :     O = vecperm_orbits(perm, n);
    3272             :   }
    3273             :   else
    3274             :   {
    3275         406 :     chk_perm(perm, n);
    3276         406 :     O = perm_cycles(perm);
    3277             :   }
    3278        1813 :   mod2 = shifti(mod,-1);
    3279        1813 :   OL = fixedfieldorbits(O, L);
    3280        1813 :   sym = fixedfieldsympol(OL, itou(gal_get_p(gal)));
    3281        1813 :   PL = sympol_eval(sym, OL, mod);
    3282        1813 :   P = FpX_center_i(FpV_roots_to_pol(PL, mod, vT), mod, mod2);
    3283        1813 :   if (flag==1) return gerepilecopy(ltop,P);
    3284         910 :   S = fixedfieldinclusion(O, PL);
    3285         910 :   S = vectopol(S, gal_get_invvdm(gal), gal_get_den(gal), mod, mod2, vT);
    3286         910 :   if (flag==0)
    3287         826 :     res = cgetg(3, t_VEC);
    3288             :   else
    3289             :   {
    3290             :     GEN PM, Pden;
    3291             :     struct galois_borne Pgb;
    3292          84 :     long val = itos(gal_get_e(gal));
    3293          84 :     Pgb.l = gal_get_p(gal);
    3294          84 :     Pden = galoisborne(P, NULL, &Pgb, degpol(T)/degpol(P));
    3295          84 :     if (Pgb.valabs > val)
    3296             :     {
    3297           7 :       if (DEBUGLEVEL>=4)
    3298           0 :         err_printf("GaloisConj: increase p-adic prec by %ld.\n", Pgb.valabs-val);
    3299           7 :       PL = ZpX_liftroots(P, PL, Pgb.l, Pgb.valabs);
    3300           7 :       L  = ZpX_liftroots(T, L, Pgb.l, Pgb.valabs);
    3301           7 :       mod = Pgb.ladicabs; mod2 = shifti(mod,-1);
    3302             :     }
    3303          84 :     PM = FpV_invVandermonde(PL, Pden, mod);
    3304          84 :     if (y < 0) y = 1;
    3305          84 :     if (varncmp(y, vT) <= 0)
    3306           0 :       pari_err_PRIORITY("galoisfixedfield", T, "<=", y);
    3307          84 :     setvarn(P, y);
    3308          84 :     res = cgetg(4, t_VEC);
    3309          84 :     gel(res,3) = fixedfieldfactor(L,O,gal_get_group(gal), PM,Pden,mod,mod2,vT,y);
    3310             :   }
    3311         910 :   gel(res,1) = gcopy(P);
    3312         910 :   gel(res,2) = gmodulo(S, T);
    3313         910 :   return gerepileupto(ltop, res);
    3314             : }
    3315             : 
    3316             : /* gal a galois group output the underlying wss group */
    3317             : GEN
    3318        1498 : galois_group(GEN gal) { return mkvec2(gal_get_gen(gal), gal_get_orders(gal)); }
    3319             : 
    3320             : GEN
    3321         938 : checkgroup(GEN g, GEN *S)
    3322             : {
    3323         938 :   if (is_group(g)) { *S = NULL; return g; }
    3324         581 :   g  = checkgal(g);
    3325         574 :   *S = gal_get_group(g); return galois_group(g);
    3326             : }
    3327             : 
    3328             : GEN
    3329        4613 : checkgroupelts(GEN G)
    3330             : {
    3331             :   long i, n;
    3332        4613 :   if (typ(G)!=t_VEC) pari_err_TYPE("checkgroupelts", G);
    3333        4599 :   if (is_group(G))
    3334             :   { /* subgroup of S_n */
    3335         371 :     if (lg(gel(G,1))==1) return mkvec(mkvecsmall(1));
    3336         371 :     return group_elts(G, group_domain(G));
    3337             :   }
    3338        4228 :   if (lg(G)==9 && typ(gel(G,1))==t_POL)
    3339        3913 :     return gal_get_group(G); /* galoisinit */
    3340             :   /* vector of permutations ? */
    3341         315 :   n = lg(G)-1;
    3342         315 :   if (n==0) pari_err_DIM("checkgroupelts");
    3343        4984 :   for (i = 1; i <= n; i++)
    3344             :   {
    3345        4704 :     if (typ(gel(G,i)) != t_VECSMALL)
    3346          21 :       pari_err_TYPE("checkgroupelts (element)", gel(G,i));
    3347        4683 :     if (lg(gel(G,i)) != lg(gel(G,1)))
    3348          14 :       pari_err_DIM("checkgroupelts [length of permutations]");
    3349             :   }
    3350         280 :   return G;
    3351             : }
    3352             : 
    3353             : GEN
    3354         168 : galoisisabelian(GEN gal, long flag)
    3355             : {
    3356         168 :   pari_sp av = avma;
    3357         168 :   GEN S, G = checkgroup(gal,&S);
    3358         168 :   if (!group_isabelian(G)) { set_avma(av); return gen_0; }
    3359         147 :   switch(flag)
    3360             :   {
    3361          49 :     case 0: return gerepileupto(av, group_abelianHNF(G,S));
    3362          49 :     case 1: set_avma(av); return gen_1;
    3363          49 :     case 2: return gerepileupto(av, group_abelianSNF(G,S));
    3364           0 :     default: pari_err_FLAG("galoisisabelian");
    3365             :   }
    3366             :   return NULL; /* LCOV_EXCL_LINE */
    3367             : }
    3368             : 
    3369             : long
    3370          56 : galoisisnormal(GEN gal, GEN sub)
    3371             : {
    3372          56 :   pari_sp av = avma;
    3373          56 :   GEN S, G = checkgroup(gal, &S), H = checkgroup(sub, &S);
    3374          56 :   long res = group_subgroup_isnormal(G, H);
    3375          56 :   set_avma(av);
    3376          56 :   return res;
    3377             : }
    3378             : 
    3379             : static GEN
    3380         308 : conjclasses_count(GEN conj, long nb)
    3381             : {
    3382         308 :   long i, l = lg(conj);
    3383         308 :   GEN c = zero_zv(nb);
    3384        4039 :   for (i = 1; i < l; i++) c[conj[i]]++;
    3385         308 :   return c;
    3386             : }
    3387             : GEN
    3388         308 : galoisconjclasses(GEN G)
    3389             : {
    3390         308 :   pari_sp av = avma;
    3391         308 :   GEN c, e, cc = group_to_cc(G);
    3392         308 :   GEN elts = gel(cc,1), conj = gel(cc,2), repr = gel(cc,3);
    3393         308 :   long i, l = lg(conj), lc = lg(repr);
    3394         308 :   c = conjclasses_count(conj, lc-1);
    3395         308 :   e = cgetg(lc, t_VEC);
    3396        3143 :   for (i = 1; i < lc; i++) gel(e,i) = cgetg(c[i]+1, t_VEC);
    3397        4039 :   for (i = 1; i < l; i++)
    3398             :   {
    3399        3731 :     long ci = conj[i];
    3400        3731 :     gmael(e, ci, c[ci]) = gel(elts, i);
    3401        3731 :     c[ci]--;
    3402             :   }
    3403         308 :   return gerepilecopy(av, e);
    3404             : }
    3405             : 
    3406             : GEN
    3407         140 : galoissubgroups(GEN gal)
    3408             : {
    3409         140 :   pari_sp av = avma;
    3410         140 :   GEN S, G = checkgroup(gal,&S);
    3411         140 :   return gerepileupto(av, group_subgroups(G));
    3412             : }
    3413             : 
    3414             : GEN
    3415          84 : galoissubfields(GEN G, long flag, long v)
    3416             : {
    3417          84 :   pari_sp av = avma;
    3418          84 :   GEN L = galoissubgroups(G);
    3419          84 :   long i, l = lg(L);
    3420          84 :   GEN S = cgetg(l, t_VEC);
    3421        1309 :   for (i = 1; i < l; ++i) gel(S,i) = galoisfixedfield(G, gmael(L,i,1), flag, v);
    3422          84 :   return gerepileupto(av, S);
    3423             : }
    3424             : 
    3425             : GEN
    3426          28 : galoisexport(GEN gal, long format)
    3427             : {
    3428          28 :   pari_sp av = avma;
    3429          28 :   GEN S, G = checkgroup(gal,&S);
    3430          28 :   return gerepileupto(av, group_export(G,format));
    3431             : }
    3432             : 
    3433             : GEN
    3434         420 : galoisidentify(GEN gal)
    3435             : {
    3436         420 :   pari_sp av = avma;
    3437         420 :   GEN S, G = checkgroup(gal,&S);
    3438         413 :   long idx = group_ident(G,S), card = group_order(G);
    3439         413 :   set_avma(av); return mkvec2s(card, idx);
    3440             : }
    3441             : 
    3442             : /* index of conjugacy class containing g */
    3443             : static long
    3444       36939 : cc_id(GEN cc, GEN g)
    3445             : {
    3446       36939 :   GEN conj = gel(cc,2);
    3447       36939 :   long k = signe(gel(cc,4))? g[1]: vecvecsmall_search(gel(cc,1),g,0);
    3448       36939 :   return conj[k];
    3449             : }
    3450             : 
    3451             : static GEN
    3452        4186 : Qevproj_RgX(GEN c, long d, GEN pro)
    3453        4186 : { return RgV_to_RgX(Qevproj_down(RgX_to_RgC(c,d), pro), varn(c)); }
    3454             : /* c in Z[X] / (X^o-1), To = polcyclo(o), T = polcyclo(expo), e = expo/o
    3455             :  * return c(X^e) mod T as an element of Z[X] / (To) */
    3456             : static GEN
    3457        3920 : chival(GEN c, GEN T, GEN To, long e, GEN pro, long phie)
    3458             : {
    3459        3920 :   c = ZX_rem(c, To);
    3460        3920 :   if (e != 1) c = ZX_rem(RgX_inflate(c,e), T);
    3461        3920 :   if (pro) c = Qevproj_RgX(c, phie, pro);
    3462        3920 :   return c;
    3463             : }
    3464             : /* chi(g^l) = sum_{k=0}^{o-1} a_k zeta_o^{l*k} for all l;
    3465             : * => a_k = 1/o sum_{l=0}^{o-1} chi(g^l) zeta_o^{-k*l}. Assume o > 1 */
    3466             : static GEN
    3467         861 : chiFT(GEN cp, GEN jg, GEN vze, long e, long o, ulong p, ulong pov2)
    3468             : {
    3469         861 :   const long var = 1;
    3470         861 :   ulong oinv = Fl_inv(o,p);
    3471             :   long k, l;
    3472         861 :   GEN c = cgetg(o+2, t_POL);
    3473        5642 :   for (k = 0; k < o; k++)
    3474             :   {
    3475        4781 :     ulong a = 0;
    3476       51478 :     for (l=0; l<o; l++)
    3477             :     {
    3478       46697 :       ulong z = vze[Fl_mul(k,l,o)*e + 1];/* zeta_o^{-k*l} */
    3479       46697 :       a = Fl_add(a, Fl_mul(uel(cp,jg[l+1]), z, p), p);
    3480             :     }
    3481        4781 :     gel(c,k+2) = stoi(Fl_center(Fl_mul(a,oinv,p), p, pov2)); /* a_k */
    3482             :   }
    3483         861 :   c[1] = evalvarn(var) | evalsigne(1); return ZX_renormalize(c,o+2);
    3484             : }
    3485             : static GEN
    3486         546 : cc_chartable(GEN cc)
    3487             : {
    3488             :   GEN al, elts, rep, ctp, ct, dec, id, vjg, H, vord, operm;
    3489             :   long i, j, k, f, l, expo, lcl, n;
    3490             :   ulong p, pov2;
    3491             : 
    3492         546 :   elts = gel(cc,1); n = lg(elts)-1;
    3493         546 :   if (n == 1) return mkvec2(mkmat(mkcol(gen_1)), gen_1);
    3494         532 :   rep = gel(cc,3);
    3495         532 :   lcl = lg(rep);
    3496         532 :   vjg = cgetg(lcl, t_VEC);
    3497         532 :   vord = cgetg(lcl,t_VECSMALL);
    3498         532 :   id = identity_perm(lg(gel(elts,1))-1);
    3499         532 :   expo = 1;
    3500        4879 :   for(j=1;j<lcl;j++)
    3501             :   {
    3502        4347 :     GEN jg, h = id, g = gel(elts,rep[j]);
    3503             :     long o;
    3504        4347 :     vord[j] = o = perm_order(g);
    3505        4347 :     expo = ulcm(expo, o);
    3506        4347 :     gel(vjg,j) = jg = cgetg(o+1,t_VECSMALL);
    3507       27671 :     for (l=1; l<=o; l++)
    3508             :     {
    3509       23324 :       jg[l] = cc_id(cc, h); /* index of conjugacy class of g^(l-1) */
    3510       23324 :       if (l < o) h = perm_mul(h, g);
    3511             :     }
    3512             :   }
    3513             :   /* would sort conjugacy classes by inc. order */
    3514         532 :   operm = vecsmall_indexsort(vord);
    3515             : 
    3516             :   /* expo > 1, exponent of G */
    3517         532 :   p = unextprime(2*n+1);
    3518        1043 :   while (p%expo != 1) p = unextprime(p+1);
    3519             :   /* compute character table modulo p: idempotents of Z(KG) */
    3520         532 :   al = conjclasses_algcenter(cc, utoipos(p));
    3521         532 :   dec = algsimpledec_ss(al,1);
    3522         532 :   ctp = cgetg(lcl,t_VEC);
    3523        4879 :   for(i=1; i<lcl; i++)
    3524             :   {
    3525        4347 :     GEN e = ZV_to_Flv(gmael3(dec,i,3,1), p); /*(1/n)[(dim chi)chi(g): g in G]*/
    3526        4347 :     ulong d = usqrt(Fl_mul(e[1], n, p)); /* = chi(1) <= sqrt(n) < sqrt(p) */
    3527        4347 :     gel(ctp,i) = Flv_Fl_mul(e,Fl_div(n,d,p), p); /*[chi(g): g in G]*/
    3528             :   }
    3529             :   /* Find minimal f such that table is defined over Q(zeta(f)): the conductor
    3530             :    * of the class field Q(\zeta_e)^H defined by subgroup
    3531             :    * H = { k in (Z/e)^*: g^k ~ g, for all g } */
    3532         532 :   H = coprimes_zv(expo);
    3533        3458 :   for (k = 2; k < expo; k++)
    3534             :   {
    3535        2926 :     if (!H[k]) continue;
    3536        2548 :     for (j = 2; j < lcl; j++) /* skip g ~ 1 */
    3537        2366 :       if (umael(vjg,j,(k % vord[j])+1) != umael(vjg,j,2)) { H[k] = 0; break; }
    3538             :   }
    3539         532 :   f = znstar_conductor_bits(Flv_to_F2v(H));
    3540             :   /* lift character table to Z[zeta_f] */
    3541         532 :   pov2 = p>>1;
    3542         532 :   ct = cgetg(lcl, t_MAT);
    3543         532 :   if (f == 1)
    3544             :   { /* rational representation */
    3545         938 :     for (j=1; j<lcl; j++) gel(ct,j) = cgetg(lcl,t_COL);
    3546         938 :     for(j=1; j<lcl; j++)
    3547             :     {
    3548         791 :       GEN jg = gel(vjg,j); /* jg[l+1] = class of g^l */
    3549         791 :       long t = lg(jg) > 2? jg[2]: jg[1];
    3550        6706 :       for(i=1; i<lcl; i++)
    3551             :       {
    3552        5915 :         GEN cp = gel(ctp,i); /* cp[i] = chi(g_i) mod \P */
    3553        5915 :         gcoeff(ct,j,i) = stoi(Fl_center(cp[t], p, pov2));
    3554             :       }
    3555             :     }
    3556             :   }
    3557             :   else
    3558             :   {
    3559         385 :     const long var = 1;
    3560         385 :     ulong ze = Fl_powu(pgener_Fl(p),(p-1)/expo, p); /* seen as zeta_e^(-1) */
    3561         385 :     GEN vze = Fl_powers(ze, expo-1, p); /* vze[i] = ze^(i-1) */
    3562         385 :     GEN vzeZX = const_vec(p, gen_0);
    3563         385 :     GEN T = polcyclo(expo, var), vT = const_vec(expo,NULL), pro = NULL;
    3564         385 :     long phie = degpol(T), id1 = gel(vjg,1)[1]; /* index of 1_G, always 1 ? */
    3565         385 :     gel(vT, expo) = T;
    3566         385 :     if (f != expo)
    3567             :     {
    3568         147 :       long phif = eulerphiu(f);
    3569         147 :       GEN zf = ZX_rem(pol_xn(expo/f,var), T), zfj = zf;
    3570         147 :       GEN M = cgetg(phif+1, t_MAT);
    3571         147 :       gel(M,1) = col_ei(phie,1);
    3572         518 :       for (j = 2; j <= phif; j++)
    3573             :       {
    3574         371 :         gel(M,j) = RgX_to_RgC(zfj, phie);
    3575         371 :         if (j < phif) zfj = ZX_rem(ZX_mul(zfj, zf), T);
    3576             :       }
    3577         147 :       pro = Qevproj_init(M);
    3578             :     }
    3579         385 :     gel(vzeZX,1) = pol_1(var);
    3580        3416 :     for (i = 2; i <= expo; i++)
    3581             :     {
    3582        3031 :       GEN t = ZX_rem(pol_xn(expo-(i-1), var), T);
    3583        3031 :       if (pro) t = Qevproj_RgX(t, phie, pro);
    3584        3031 :       gel(vzeZX, vze[i]) = t;
    3585             :     }
    3586        3941 :     for(i=1; i<lcl; i++)
    3587             :     { /* loop over characters */
    3588        3556 :       GEN cp = gel(ctp,i), C, cj; /* cp[j] = chi(g_j) mod \P */
    3589        3556 :       long dim = cp[id1];
    3590        3556 :       gel(ct, i) = C = const_col(lcl-1, NULL);
    3591        3556 :       gel(C,operm[1]) = utoi(dim); /* chi(1_G) */
    3592       40978 :       for (j=lcl-1; j > 1; j--)
    3593             :       { /* loop over conjugacy classes, decreasing order: skip 1_G */
    3594       37422 :         long e, jperm = operm[j], o = vord[jperm];
    3595       37422 :         GEN To, jg = gel(vjg,jperm); /* jg[l+1] = class of g^l */
    3596             : 
    3597       37422 :         if (gel(C, jperm)) continue; /* done already */
    3598       35903 :         if (dim == 1) { gel(C, jperm) = gel(vzeZX, cp[jg[2]]); continue; }
    3599         861 :         e = expo / o;
    3600         861 :         cj = chiFT(cp, jg, vze, e, o, p, pov2);
    3601         861 :         To = gel(vT, o); if (!To) To = gel(vT,o) = polcyclo(o, var);
    3602         861 :         gel(C, jperm) = chival(cj, T, To, e, pro, phie);
    3603        3920 :         for (k = 2; k < o; k++)
    3604             :         {
    3605        3059 :           GEN ck = RgX_inflate(cj, k); /* chi(g^k) */
    3606        3059 :           gel(C, jg[k+1]) = chival(ck, T, To, e, pro, phie);
    3607             :         }
    3608             :       }
    3609             :     }
    3610             :   }
    3611         532 :   ct = gen_sort_shallow(ct,(void*)cmp_universal,cmp_nodata);
    3612        1736 :   i = 1; while (!vec_isconst(gel(ct,i))) i++;
    3613         532 :   if (i > 1) swap(gel(ct,1), gel(ct,i));
    3614         532 :   return mkvec2(ct, utoipos(f));
    3615             : }
    3616             : GEN
    3617         546 : galoischartable(GEN gal)
    3618             : {
    3619         546 :   pari_sp av = avma;
    3620         546 :   GEN cc = group_to_cc(gal);
    3621         546 :   return gerepilecopy(av, cc_chartable(cc));
    3622             : }
    3623             : 
    3624             : static void
    3625        1491 : checkgaloischar(GEN ch, GEN repr)
    3626             : {
    3627        1491 :   if (gvar(ch) == 0) pari_err_PRIORITY("galoischarpoly",ch,"=",0);
    3628        1491 :   if (!is_vec_t(typ(ch))) pari_err_TYPE("galoischarpoly", ch);
    3629        1491 :   if (lg(repr) != lg(ch)) pari_err_DIM("galoischarpoly");
    3630        1491 : }
    3631             : 
    3632             : static long
    3633        1547 : galoischar_dim(GEN ch)
    3634             : {
    3635        1547 :   pari_sp av = avma;
    3636        1547 :   long d = gtos(simplify_shallow(lift_shallow(gel(ch,1))));
    3637        1547 :   return gc_long(av,d);
    3638             : }
    3639             : 
    3640             : static GEN
    3641       12355 : galoischar_aut_charpoly(GEN cc, GEN ch, GEN p, long d)
    3642             : {
    3643       12355 :   GEN q = p, V = cgetg(d+2, t_POL);
    3644             :   long i;
    3645       12355 :   V[1] = evalsigne(1)|evalvarn(0);
    3646       25970 :   for (i = 1; i <= d; i++)
    3647             :   {
    3648       13615 :     gel(V,i+1) = gel(ch, cc_id(cc,q));
    3649       13615 :     if (i < d) q = perm_mul(q, p);
    3650             :   }
    3651       12355 :   return liftpol_shallow(RgXn_expint(RgX_neg(V),d+1));
    3652             : }
    3653             : 
    3654             : static GEN
    3655        1491 : galoischar_charpoly(GEN cc, GEN ch, long o)
    3656             : {
    3657        1491 :   GEN chm, V, elts = gel(cc,1), repr = gel(cc,3);
    3658        1491 :   long i, d, l = lg(ch), v = gvar(ch);
    3659        1491 :   checkgaloischar(ch, repr);
    3660        1491 :   chm = v < 0 ? ch: gmodulo(ch, polcyclo(o, v));
    3661        1491 :   V = cgetg(l, t_COL); d = galoischar_dim(ch);
    3662       13846 :   for (i = 1; i < l; i++)
    3663       12355 :     gel(V,i) = galoischar_aut_charpoly(cc, chm, gel(elts,repr[i]), d);
    3664        1491 :   return V;
    3665             : }
    3666             : 
    3667             : GEN
    3668        1435 : galoischarpoly(GEN gal, GEN ch, long o)
    3669             : {
    3670        1435 :   pari_sp av = avma;
    3671        1435 :   GEN cc = group_to_cc(gal);
    3672        1435 :   return gerepilecopy(av, galoischar_charpoly(cc, ch, o));
    3673             : }
    3674             : 
    3675             : static GEN
    3676          56 : cc_char_det(GEN cc, GEN ch, long o)
    3677             : {
    3678          56 :   long i, l = lg(ch), d = galoischar_dim(ch);
    3679          56 :   GEN V = galoischar_charpoly(cc, ch, o);
    3680         280 :   for (i = 1; i < l; i++) gel(V,i) = leading_coeff(gel(V,i));
    3681          56 :   return odd(d)? gneg(V): V;
    3682             : }
    3683             : 
    3684             : GEN
    3685          56 : galoischardet(GEN gal, GEN ch, long o)
    3686             : {
    3687          56 :   pari_sp av = avma;
    3688          56 :   GEN cc = group_to_cc(gal);
    3689          56 :   return gerepilecopy(av, cc_char_det(cc, ch, o));
    3690             : }

Generated by: LCOV version 1.13