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

Generated by: LCOV version 1.14