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 - pclgp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29115-f22e516b23) Lines: 1835 2379 77.1 %
Date: 2024-02-22 08:05:55 Functions: 153 180 85.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000, 2012  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : #define DEBUGLEVEL DEBUGLEVEL_subcyclo
      18             : 
      19             : /* written by Takashi Fukuda */
      20             : 
      21             : #define onevec(x) const_vec(x,gen_1)
      22             : #define nullvec() cgetg(1, t_VEC)
      23             : #define order_f_x(f, x) Fl_order(x%f, eulerphiu(f), f)
      24             : 
      25             : #define USE_MLL         (1L<<0)
      26             : #define NO_PLUS_PART    (1L<<1)
      27             : #define NO_MINUS_PART   (1L<<2)
      28             : #define SKIP_PROPER     (1L<<3)
      29             : #define SAVE_MEMORY     (1L<<4)
      30             : #define USE_FULL_EL     (1L<<5)
      31             : #define USE_BASIS       (1L<<6)
      32             : #define USE_FACTOR      (1L<<7)
      33             : #define USE_GALOIS_POL  (1L<<8)
      34             : #define USE_F           (1L<<9)
      35             : 
      36             : static ulong
      37      178002 : _get_d(GEN H) { return umael(H, 2, 1);}
      38             : static ulong
      39      119820 : _get_f(GEN H) { return umael(H, 2, 2);}
      40             : static ulong
      41       30843 : _get_h(GEN H) { return umael(H, 2, 3);}
      42             : static long
      43       28392 : _get_s(GEN H) { return umael(H, 2, 4);}
      44             : static long
      45       53957 : _get_g(GEN H) { return umael(H, 2, 5);}
      46             : static GEN
      47       30787 : _get_H(GEN H) { return gel(H, 3);}
      48             : static ulong
      49      112755 : K_get_d(GEN K) { return _get_d(gel(K,1)); }
      50             : static ulong
      51       82965 : K_get_f(GEN K) { return _get_f(gel(K,1)); }
      52             : static ulong
      53       17424 : K_get_h(GEN K) { return _get_h(gel(K,1)); }
      54             : static long
      55           0 : K_get_s(GEN K) { return _get_s(gel(K,1)); }
      56             : static ulong
      57       17102 : K_get_g(GEN K) { return _get_g(gel(K,1)); }
      58             : static GEN
      59       17368 : K_get_H(GEN K) { return _get_H(gel(K,1)); }
      60             : static ulong
      61       59314 : K_get_dchi(GEN K) { return gel(K,6)[1]; }
      62             : static ulong
      63       23660 : K_get_nconj(GEN K) { return gel(K,6)[2]; }
      64             : 
      65             : /* G=<s> is a cyclic group of order n and t=s^(-1).
      66             :  *  convert sum_i a_i*s^i to sum_i b_i*t^i */
      67             : static GEN
      68          14 : Flx_recip1_inplace(GEN x, long pn)
      69             : {
      70          14 :   long i, lx = lg(x);
      71          14 :   if(lx-2 != pn) /* This case scarcely occurs */
      72             :   {
      73           0 :     long ly = pn+2;
      74           0 :     GEN y = const_vecsmall(ly, 0);
      75           0 :     y[1] = x[1];y[2] = x[2];
      76           0 :     for(i=3;i<lx;i++) y[ly+2-i] = x[i];
      77           0 :     return Flx_renormalize(y, ly);
      78             :   }
      79             :   else /* almost all cases */
      80             :   {
      81          14 :     long t, mid = (lx+1)>>1;
      82        7168 :     for(i=3;i<=mid;i++)
      83             :     {
      84        7154 :       t = x[i];x[i] = x[lx+2-i];x[lx+2-i] = t;
      85             :     }
      86          14 :     return Flx_renormalize(x, lx);
      87             :   }
      88             : }
      89             : 
      90             : /* Return h^degpol(P) P(x / h) */
      91             : static GEN
      92          14 : Flx_rescale_inplace(GEN P, ulong h, ulong p)
      93             : {
      94          14 :   long i, l = lg(P);
      95          14 :   ulong hi = h;
      96       14322 :   for (i=l-2; i>=2; i--)
      97             :   {
      98       14322 :     P[i] = Fl_mul(P[i], hi, p);
      99       14322 :     if (i == 2) break;
     100       14308 :     hi = Fl_mul(hi,h, p);
     101             :   }
     102          14 :   return P;
     103             : }
     104             : 
     105             : static GEN
     106          14 : zx_to_Flx_inplace(GEN x, ulong p)
     107             : {
     108          14 :   long i, lx = lg(x);
     109       14350 :   for (i=2; i<lx; i++) uel(x,i) = umodsu(x[i], p);
     110          14 :   return Flx_renormalize(x, lx);
     111             : }
     112             : 
     113             : /* zero pol of n components (i.e. deg=n-1). need to pass to ZX_renormalize */
     114             : INLINE GEN
     115       53263 : pol_zero(long n)
     116             : {
     117             :   long i;
     118       53263 :   GEN p = cgetg(n+2, t_POL);
     119       53263 :   p[1] = evalsigne(1) | evalvarn(0);
     120     1799812 :   for (i = 2; i < n+2; i++) gel(p, i) = gen_0;
     121       53263 :   return p;
     122             : }
     123             : 
     124             : /* e[i+1] = L*i + K for i >= n; determine K,L and reduce n if possible */
     125             : static GEN
     126          21 : vecsmall2vec2(GEN e, long n)
     127             : {
     128          21 :   long L = e[n+1] - e[n], K = e[n+1] - L*n;
     129          42 :   n--; while (n >= 0 && e[n+1] - L*n == K) n--;
     130          21 :   if (n < 0) e = nullvec(); else { setlg(e, n+2); e = zv_to_ZV(e); }
     131          21 :   return mkvec3(utoi(L), stoi(K), e); /* L >= 0 */
     132             : }
     133             : 
     134             : /* z=zeta_{p^n}; return k s.t. (z-1)^k || f(z) assuming deg(f)<phi(p^n) */
     135             : static long
     136          42 : zx_p_val(GEN f, ulong p, ulong n)
     137             : {
     138          42 :   pari_sp av = avma;
     139          42 :   ulong x = zx_lval(f, p);
     140          42 :   if (x) { f = zx_z_divexact(f, upowuu(p, x)); x *= (p-1)*upowuu(p, n-1); }
     141          42 :   x += Flx_val(Flx_translate1(zx_to_Flx(f, p), p));
     142          42 :   return gc_long(av, x);
     143             : }
     144             : 
     145             : static long
     146         315 : ZX_p_val(GEN f, ulong p, ulong n)
     147             : {
     148         315 :   pari_sp av = avma;
     149         315 :   ulong x = ZX_lval(f, p);
     150         315 :   if (x) { f = ZX_Z_divexact(f, powuu(p, x)); x *= (p-1)*upowuu(p, n-1); }
     151         315 :   x += Flx_val(Flx_translate1(ZX_to_Flx(f, p), p));
     152         315 :   return gc_long(av, x);
     153             : }
     154             : 
     155             : static GEN
     156          35 : set_A(GEN B, int *chi)
     157             : {
     158          35 :   long a, i, j, B1 = B[1], l = lg(B);
     159          35 :   GEN A = cgetg(l, t_VECSMALL);
     160     1687014 :   for (a = 0, j = 1; j < B1; j++) a += chi[j];
     161          35 :   A[1] = a;
     162         714 :   for (i = 2; i < l; i++)
     163             :   {
     164         679 :     long Bi = B[i];
     165    28159243 :     for (a = A[i-1], j = B[i-1]; j < Bi; j++) a += chi[j];
     166         679 :     A[i] = a;
     167             :   }
     168          35 :   return A;
     169             : }
     170             : 
     171             : /* g_n(a)=g_n(b) <==> a^2=b^2 mod 2^(n+2) <==> a=b,-b mod 2^(n+2)
     172             :  * g_n(a)=g_n(1+q0)^k <==> a=x(1+q0)^k x=1,-1
     173             :  * gam[1+a]=k, k<0  ==> g_n(a)=0
     174             :  *             k>=0 ==> g_n(a)^(-1)=gamma^k, gamma=g_n(1+q0) */
     175             : static GEN
     176          14 : set_gam2(long q01, long n)
     177             : {
     178             :   long i, x, x1, pn, pn2;
     179             :   GEN gam;
     180          14 :   pn = (1L<<n);
     181          14 :   pn2 = (pn<<2);
     182          14 :   gam = const_vecsmall(pn2, -1);
     183          14 :   x=Fl_inv(q01, pn2); x1=1;
     184       14350 :   for (i=0; i<pn; i++)
     185             :   {
     186       14336 :     gam[1+x1] = gam[1+Fl_neg(x1, pn2)] = i;
     187       14336 :     x1 = Fl_mul(x1, x, pn2);
     188             :   }
     189          14 :   return gam;
     190             : }
     191             : 
     192             : /* g_n(a)=g_n(b) <==> a^(p-1)=b^(p-1) mod p^(n+1) <==> a=xb x=<g^(p^n)>
     193             :  * g_n(a)=g_n(1+q0)^k <==> a=x(1+q0)^k x=<g^(p^n)>
     194             :  * gam[1+a]=k, k<0  ==> g_n(a)=0
     195             :  *             k>=0 ==> g_n(a)^(-1)=gamma^k, gamma=g_n(1+q0) */
     196             : static GEN
     197         476 : set_gam(long q01, long p, long n)
     198             : {
     199             :   long i, j, g, g1, x, x1, p1, pn, pn1;
     200             :   GEN A, gam;
     201         476 :   p1 = p-1; pn = upowuu(p, n); pn1 = p*pn;
     202         476 :   gam = const_vecsmall(pn1, -1);
     203         476 :   g = pgener_Zl(p); g1 = Fl_powu(g, pn, pn1);
     204         476 :   A = Fl_powers(g1, p1-1, pn1);  /* A[1+i]=g^(i*p^n) mod p^(n+1), 0<=i<=p-2 */
     205         476 :   x = Fl_inv(q01, pn1); x1 = 1;
     206      568694 :   for (i=0; i<pn; i++)
     207             :   {
     208     2086126 :     for (j=1; j<=p1; j++) gam[1+Fl_mul(x1, A[j], pn1)] = i;
     209      568218 :     x1 = Fl_mul(x1, x, pn1);
     210             :   }
     211         476 :   return gam;
     212             : }
     213             : 
     214             : /* k=Q(sqrt(m)), A_n=p-class gr. of k_n, |A_n|=p^(e_n)
     215             :  * return e_n-e_(n-1)
     216             :  * essential assumption : m is not divisible by p
     217             :  * Gold, Acta Arith. XXVI (1974), p.22 formula (3) */
     218             : static long
     219          35 : ediff(ulong p, long m, ulong n, int *chi)
     220             : {
     221          35 :   pari_sp av = avma;
     222             :   long j, lx, *px;
     223             :   ulong i, d, s, y, g, p1, pn, pn1, pn_1, phipn, phipn1;
     224             :   GEN A, B, x, gs, cs;
     225             : 
     226          35 :   d=((m-1)%4==0)?labs(m):4*labs(m);
     227          35 :   p1=p-1; pn_1=upowuu(p, n-1); pn=p*pn_1; pn1=p*pn; phipn=p1*pn_1; phipn1=p1*pn;
     228          35 :   lx=2*p1*phipn;
     229          35 :   y=Fl_inv(pn1%d, d); g=pgener_Zl(p);  /* pn1 may > d */
     230          35 :   cs = cgetg(2+phipn, t_VECSMALL); cs[1] = evalvarn(0);
     231          35 :   x = cgetg(1+lx, t_VECSMALL);
     232          35 :   gs = Fl_powers(g, phipn1-1, pn1); /* gs[1+i]=g^i(mod p^(n+1)), 0<=i<p^(n+1) */
     233             : 
     234         105 :   for (px=x,i=0; i<p1; i++)
     235             :   {
     236          70 :     long ipn=i*pn+1,ipnpn=ipn+phipn;
     237         546 :     for (s=0; s<phipn; s++)
     238             :     {
     239         476 :       *++px = (y*gs[s+ipn])%d;  /* gs[s+ipn] may > d */
     240         476 :       *++px = (y*gs[(s%pn_1)+ipnpn])%d;
     241             :     }
     242             :   }
     243          35 :   B = vecsmall_uniq(x);
     244          35 :   A = set_A(B, chi);
     245         273 :   for (s=0; s<phipn; s++)
     246             :   {
     247         238 :     long a=0, ipn=1, spn1=s%pn_1;
     248         714 :     for (i=0; i<p1; i++)
     249             :     {
     250         476 :       if ((j=zv_search(B, (y*gs[s+ipn])%d))<=0)
     251           0 :         pari_err_BUG("zv_search failed\n");
     252         476 :       a+=A[j];
     253         476 :       if ((j=zv_search(B, (y*gs[spn1+ipn+phipn])%d))<=0)
     254           0 :         pari_err_BUG("zv_search failed\n");
     255         476 :       a-=A[j];
     256         476 :       ipn+=pn;
     257             :     }
     258         238 :     cs[2+s] = a;
     259             :   }
     260          35 :   cs = zx_renormalize(cs, lg(cs));
     261          35 :   y = (lg(cs)==3) ? phipn*z_lval(cs[2], p) : zx_p_val(cs, p, n);
     262          35 :   return gc_long(av, y);
     263             : }
     264             : 
     265             : static GEN
     266           0 : quadteichstk(GEN Chi, int *chi, GEN Gam, long p, long m, long n)
     267             : {
     268           0 :   GEN Gam1 = Gam+1, xi;
     269             :   long i, j, j0, d, f0, pn, pn1, deg, pn1d;
     270             : 
     271           0 :   d = ((m&3)==1)?m:m<<2;
     272           0 :   f0 = ulcm(p, d)/p;
     273           0 :   pn = upowuu(p, n); pn1 = p*pn; pn1d = pn1%d;
     274           0 :   xi = cgetg(pn+2, t_POL); xi[1] = evalsigne(1) | evalvarn(0);
     275           0 :   for (i=0; i<pn; i++) gel(xi, 2+i) = const_vecsmall(p, 0);
     276           0 :   for (j=1; j<pn1; j++)
     277             :   {
     278             :     long jp, ipn1d, *xij0;
     279           0 :     if ((j0 = Gam1[j])<0) continue;
     280           0 :     jp = j%p; ipn1d = j%d; xij0 = gel(xi, 2+j0)+2;
     281           0 :     for (i=1; i<f0; i++)
     282             :     {
     283             :       int sgn;
     284           0 :       if ((ipn1d += pn1d) >= d) ipn1d -= d;
     285           0 :       if ((sgn = chi[ipn1d])==0) continue;
     286           0 :       deg = Chi[jp];  /* jp!=0 because j0>=0 */
     287           0 :       if (sgn>0) xij0[deg] += i;
     288           0 :       else xij0[deg] -= i;
     289             :     }
     290             :   }
     291           0 :   for (i=0; i<pn; i++) gel(xi, 2+i) = zx_renormalize(gel(xi, 2+i), p+1);
     292           0 :   return FlxX_renormalize(xi, pn+2);  /* zxX_renormalize does not exist */
     293             : }
     294             : 
     295             : #ifdef DEBUG_QUADSTK
     296             : /* return f0*xi_n */
     297             : static GEN
     298             : quadstkp_by_def(int *chi, GEN gam, long n, long p, long f, long f0)
     299             : {
     300             :   long i, a, a1, pn, pn1, qn;
     301             :   GEN x, x2, gam1 = gam+1;
     302             :   pn = upowuu(p, n); pn1 = p*pn; qn = f0*pn1;
     303             :   x = const_vecsmall(pn+1, 0); x2 = x+2;
     304             :   for (a=1; a<qn; a++)
     305             :   {
     306             :     int sgn;
     307             :     if ((a1=gam1[a%pn1])<0 || (sgn=chi[a%f])==0) continue;
     308             :     if (sgn>0) x2[a1]+=a;
     309             :     else x2[a1]-=a;
     310             :   }
     311             :   for (i=0; i<pn; i++)
     312             :   {
     313             :     if (x2[i]%pn1) pari_err_BUG("stickel. ele. is not integral.\n");
     314             :     else x2[i]/=pn1;
     315             :   }
     316             :   return zx_renormalize(x, pn+2);
     317             : }
     318             : #endif
     319             : 
     320             : /* f!=p
     321             :  * xi_n = f0^(-1)*
     322             :  *   sum_{0<=j<pn1,(j,p)=1}(Q_n/Q,j)^(-1)*(sum_{0<=i<f0}i*chi^(-1)(pn1*i+j)) */
     323             : static GEN
     324          14 : quadstkp1(int *chi, GEN gam, long n, long p, long f, long f0)
     325             : {
     326             :   long i, j, j0, pn, pn1, pn1f, den;
     327             :   GEN x, x2;
     328          14 :   pn = upowuu(p, n); pn1 = p*pn; pn1f = pn1%f;
     329          14 :   x = const_vecsmall(pn+1, 0); x2 = x+2;
     330          14 :   if (f==3) den = (chi[p%f]>0)?f0<<1:2;
     331          14 :   else if (f==4) den = (chi[p%f]>0)?f0<<1:f0;
     332          14 :   else den = f0<<1;
     333     1653372 :   for (j=1; j<pn1; j++)
     334             :   {
     335             :     long ipn1;
     336     1653358 :     if (j%p==0) continue;
     337     1102248 :     j0 = gam[1+j]; ipn1 = j%f;
     338   263437272 :     for (i=1; i<f0; i++)
     339             :     {
     340             :       int sgn;
     341   262335024 :       if ((ipn1+=pn1f)>=f) ipn1-=f;
     342   262335024 :       if ((sgn = chi[ipn1])>0) x2[j0]+=i;
     343   131716319 :       else if (sgn<0) x2[j0]-=i;
     344             :     }
     345             :   }
     346      551138 :   for (i=0; i<pn; i++)
     347             :   {
     348      551124 :     if (x2[i]%den) pari_err_BUG("stickel. ele. is not integral.\n");
     349      551124 :     else x2[i]/=den;
     350             :   }
     351          14 :   return zx_renormalize(x, pn+2);
     352             : }
     353             : 
     354             : /* f==p */
     355             : static GEN
     356           0 : quadstkp2(int *chi, GEN gam, long n, long p)
     357             : {
     358             :   long a, a1, i, pn, pn1, amodp;
     359           0 :   GEN x, x2, gam1 = gam+1;
     360           0 :   pn = upowuu(p, n); pn1 = p*pn;
     361           0 :   x = const_vecsmall(pn+1, 0); x2 = x+2;
     362           0 :   for (a=1,amodp=0; a<pn1; a++)
     363             :   {
     364             :     int sgn;
     365           0 :     if (++amodp==p) {amodp = 0; continue; }
     366           0 :     if ((sgn = chi[amodp])==0) continue;
     367           0 :     a1=gam1[a];
     368           0 :     if (sgn>0) x2[a1]+=a;
     369           0 :     else x2[a1]-=a;
     370             :   }
     371           0 :   for (i=0; i<pn; i++)
     372             :   {
     373           0 :     if (x2[i]%pn1) pari_err_BUG("stickel. ele. is not integral.\n");
     374           0 :     else x2[i]/=pn1;
     375             :   }
     376           0 :   return zx_renormalize(x, pn+2);
     377             : }
     378             : 
     379             : /*  p>=3
     380             :  *  f = conductor of Q(sqrt(m))
     381             :  *  q0 = lcm(f,p) = f0*p
     382             :  *  qn = q0*p^n = f0*p^(n+1)
     383             :  *  xi_n = qn^(-1)*sum_{1<=a<=qn,(a,qn)=1} a*chi(a)^(-1)*(Q_n/Q,a)^(-1) */
     384             : static GEN
     385          14 : quadstkp(long p, long m, long n, int *chi)
     386             : {
     387             :   long f, f0, pn, pn1, q0;
     388             :   GEN gam;
     389          14 :   f = ((m-1)%4==0)?labs(m):4*labs(m);
     390          14 :   pn = upowuu(p, n); pn1 = p*pn;
     391          14 :   if (f % p) { q0 = f * p; f0 = f; } else { q0 = f; f0 = f / p; }
     392          14 :   gam = set_gam((1+q0)%pn1, p, n);
     393             : #ifdef DEBUG_QUADSTK
     394             :   return quadstkp_by_def(chi, gam, n, p, f, f0);
     395             : #else
     396          14 :   return (f0!=1)?quadstkp1(chi, gam, n, p, f, f0):quadstkp2(chi, gam, n, p);
     397             : #endif
     398             : }
     399             : 
     400             : /* p=2 */
     401             : static GEN
     402          14 : quadstk2(long m, long n, int *chi)
     403             : {
     404             :   long i, j, j0, f, f0, pn, pn1, pn2, pn2f, q0;
     405             :   GEN x, x2, gam;
     406          14 :   f = ((m-1)%4==0)?labs(m):4*labs(m);
     407          14 :   pn = 1L<<n; pn1 = pn<<1; pn2 = pn1<<1; pn2f = pn2%f;
     408          14 :   q0 = (f&1)?f*4:f;
     409          14 :   f0 = (f&1)?f:f/4;
     410          14 :   x = const_vecsmall(pn+1, 0); x2 = x+2;
     411          14 :   gam = set_gam2((1+q0)%pn2, n);
     412       57344 :   for (j=1; j<pn2; j++)
     413             :   {
     414             :     long ipn2;
     415       57330 :     if (!(j&1)) continue;
     416       28672 :     j0 = gam[1+j];
     417       28672 :     ipn2 = j%f;
     418             :     /* for (i=1; i<f0; i++) x2[j0]+=i*chi[(i*pn2+j)%f]; */
     419     1691648 :     for (i=1; i<f0; i++)
     420             :     {
     421             :       int sgn;
     422     1662976 :       if ((ipn2+=pn2f)>=f) ipn2-=f;
     423     1662976 :       if ((sgn=chi[ipn2])>0) x2[j0]+=i;
     424      845390 :       else if (sgn<0) x2[j0]-=i;
     425             :     }
     426             :   }
     427       14350 :   for (f0<<=1, i=0; i<pn; i++)
     428             :   {
     429       14336 :     if (x2[i]%f0) pari_err_BUG("stickel. ele. is not integral.\n");
     430       14336 :     else x2[i]/=f0;
     431             :   }
     432          14 :   return zx_renormalize(x, pn+2);
     433             : }
     434             : 
     435             : /* Chin is a generator of the group of the characters of G(Q_n/Q).
     436             :  * chin[1+a]=k, k<0  ==> Chin(a)=0
     437             :  *              k>=0 ==> Chin(a)=zeta_{p^n}^k */
     438             : static GEN
     439          21 : set_chin(long p, long n)
     440             : {
     441          21 :   long i, j, x = 1, g, gpn, pn, pn1;
     442             :   GEN chin, chin1;
     443          21 :   pn = upowuu(p, n); pn1 = p*pn;
     444          21 :   chin = const_vecsmall(pn1, -1); chin1 = chin+1;
     445          21 :   g = pgener_Zl(p); gpn = Fl_powu(g, pn, pn1);
     446         294 :   for (i=0; i<pn; i++)
     447             :   {
     448         273 :     long y = x;
     449         819 :     for (j=1; j<p; j++)
     450             :     {
     451         546 :       chin1[y] = i;
     452         546 :       y = Fl_mul(y, gpn, pn1);
     453             :     }
     454         273 :     x = Fl_mul(x, g, pn1);
     455             :   }
     456          21 :   return chin;
     457             : }
     458             : 
     459             : /* k=Q(sqrt(m)), A_n=p-class gr. of k_n, |A_n|=p^(e_n), p|m
     460             :  * return e_n-e_(n-1).
     461             :  * There is an another method using the Stickelberger element based on
     462             :  * Coates-Lichtenbaum, Ann. Math. vol.98 No.3 (1973), 498-550, Lemma 2.15.
     463             :  * If kro(m,p)!=1, then orders of two groups coincide.
     464             :  * ediff_ber is faster than the Stickelberger element. */
     465             : static long
     466          21 : ediff_ber(ulong p, long m, ulong n, int *chi)
     467             : {
     468          21 :   pari_sp av = avma;
     469             :   long a, d, e, x, y, pn, pn1, qn1;
     470          21 :   GEN B, B2, chin = set_chin(p, n)+1;
     471             : 
     472          21 :   d = ((m-1)%4==0)?labs(m):4*labs(m);
     473          21 :   pn = upowuu(p, n); pn1 = p*pn; qn1 = (d*pn)>>1;
     474          21 :   B = const_vecsmall(pn+1, 0); B2 = B+2;
     475   522105969 :   for (a=x=y=1; a <= qn1; a++) /* x=a%d, y=a%pn1 */
     476             :   {
     477   522105948 :     int sgn = chi[x];
     478   522105948 :     if (sgn)
     479             :     {
     480   172937856 :       long k = chin[y];
     481   172937856 :       if (k >= 0) { if (sgn > 0) B2[k]++; else B2[k]--; }
     482             :     }
     483   522105948 :     if (++x == d) x = 0;
     484   522105948 :     if (++y == pn1) y = 0;
     485             :   }
     486          21 :   B = zx_renormalize(B, pn+2);
     487           7 :   e = (n==1)? zx_p_val(B, p, n)
     488          21 :             : ZX_p_val(ZX_rem(zx_to_ZX(B), polcyclo(pn, 0)), p, n);
     489          21 :   if (p==3 && chi[2] < 0) e--;  /* 2 is a primitive root of 3^n (n>=1) */
     490          21 :   return gc_long(av, e);
     491             : }
     492             : 
     493             : #ifdef DEBUG
     494             : /* slow */
     495             : static int*
     496             : set_quad_chi_1(long m)
     497             : {
     498             :   long a, d, f;
     499             :   int *chi;
     500             :   d=((m-1)%4==0)?m:4*m; f=labs(d);
     501             :   chi= (int*)stack_calloc(sizeof(int)*f);
     502             :   for (a=1; a<f; a++) chi[a]=kross(d, a);
     503             :   return chi;
     504             : }
     505             : #endif
     506             : 
     507             : /* chi[a]=kross(d, a)   0<=a<=f-1
     508             :  * d=discriminant of Q(sqrt(m)), f=abs(d)
     509             :  *
     510             :  * Algorithm: m=-p1*p2*...*pr ==> kross(d,gi)=-1 (1<=i<=r), gi=proot(pi)
     511             :  * set_quad_chi_1(m)=set_quad_chi_2(m) for all square-free m s.t. |m|<10^5. */
     512             : static int*
     513          49 : set_quad_chi_2(long m)
     514             : {
     515          49 :   long d = (m-1) % 4? 4*m: m, f = labs(d);
     516          49 :   GEN fa = factoru(f), P = gel(fa, 1), E = gel(fa,2), u, v;
     517          49 :   long i, j, np, nm, l = lg(P);
     518          49 :   int *chi = (int*)stack_calloc(sizeof(int)*f);
     519          49 :   pari_sp av = avma;
     520          49 :   int *plus = (int*)stack_calloc(sizeof(int)*f), *p0 = plus;
     521          49 :   int *minus = (int*)stack_calloc(sizeof(int)*f), *p1 = minus;
     522             : 
     523          49 :   u = cgetg(32, t_VECSMALL);
     524          49 :   v = cgetg(32, t_VECSMALL);
     525         147 :   for (i = 1; i < l; i++)
     526             :   {
     527          98 :     ulong p = upowuu(P[i], E[i]);
     528          98 :     u[i] = p * Fl_inv(p, f / p);
     529          98 :     v[i] = Fl_sub(1, u[i], f);
     530             :   }
     531          49 :   if (E[1]==2)       /* f=4*(-m) */
     532             :   {
     533          14 :     *p0++ = Fl_add(v[1], u[1], f);
     534          14 :     *p1++ = Fl_add(Fl_mul(3, v[1], f), u[1], f);
     535          14 :     i = 2;
     536             :   }
     537          35 :   else if (E[1]==3)  /* f=8*(-m) */
     538             :   {
     539             :     ulong a;
     540           7 :     *p0++ = Fl_add(v[1], u[1], f);
     541           7 :     a = Fl_add(Fl_mul(3, v[1], f), u[1], f);
     542           7 :     if (kross(d, a) > 0) *p0++ = a; else *p1++ = a;
     543           7 :     a = Fl_add(Fl_mul(5, v[1], f), u[1], f);
     544           7 :     if (kross(d, a) > 0) *p0++ = a; else *p1++ = a;
     545           7 :     a = Fl_add(Fl_mul(7, v[1], f), u[1], f);
     546           7 :     if (kross(d, a) > 0) *p0++ = a; else *p1++ = a;
     547           7 :     i = 2;
     548             :   }
     549             :   else              /* f=-m */
     550          28 :   {*p0++ = 1; i = 1; }
     551         126 :   for (; i < l; i++)
     552             :   {
     553          77 :     ulong gn, g = pgener_Fl(P[i]);
     554          77 :     gn = g = Fl_add(Fl_mul(g, v[i], f), u[i], f);
     555          77 :     np = p0-plus;
     556          77 :     nm = p1-minus;
     557             :     for (;;)
     558             :     {
     559     4802406 :       for (j = 0; j < np; j++) *p1++ = Fl_mul(plus[j], gn, f);
     560     4799655 :       for (j = 0; j < nm; j++) *p0++ = Fl_mul(minus[j], gn, f);
     561        7679 :       gn = Fl_mul(gn, g, f); if (gn == 1) break;
     562     4763745 :       for (j= 0; j < np; j++) *p0++ = Fl_mul(plus[j], gn, f);
     563     4761022 :       for (j = 0; j < nm; j++) *p1++ = Fl_mul(minus[j], gn, f);
     564        7602 :       gn = Fl_mul(gn, g, f); if (gn == 1) break;
     565             :     }
     566             :   }
     567          49 :   np = p0-plus;
     568          49 :   nm = p1-minus;
     569     9548224 :   for (i = 0; i < np; i++) chi[plus[i]] = 1;
     570     9548224 :   for (i = 0; i < nm; i++) chi[minus[i]] = -1;
     571          49 :   set_avma(av); return chi;
     572             : }
     573             : 
     574             : static long
     575        8995 : srh_x(GEN T, long n, long x)
     576             : {
     577       30086 :   for (; x<n; x++) if (!T[x]) return x;
     578         623 :   return -1;
     579             : }
     580             : 
     581             : /* G is a cyclic group of order d. hat(G)=<chi>.
     582             :  * chi, chi^p, ... , chi^(p^(d_chi-1)) are conjugate.
     583             :  * {chi^j | j in C} are repre. of Q_p-congacy classes of inj. chars.
     584             :  *
     585             :  * C is a set of representatives of H/<p>, where H=(Z/dZ)^* */
     586             : static GEN
     587        1134 : set_C(long p, long d, long d_chi, long n_conj)
     588             : {
     589        1134 :   long i, j, x, y, pmodd = p%d;
     590        1134 :   GEN T = const_vecsmall(d, 0)+1;
     591        1134 :   GEN C = cgetg(1+n_conj, t_VECSMALL);
     592        1134 :   if (n_conj==1) { C[1] = 1; return C; }
     593        9618 :   for (i=0, x=1; x >= 0; x = srh_x(T, d, x))
     594             :   {
     595        8995 :     if (cgcd(x, d)==1) C[++i] = x;
     596       40929 :     for (j=0, y=x; j<d_chi; j++) T[y = Fl_mul(y, pmodd, d)] = 1;
     597             :   }
     598         623 :   return C;
     599             : }
     600             : 
     601             : static GEN
     602         343 : FpX_one_cyclo(long n, GEN p)
     603             : {
     604         343 :   if (lgefint(p)==3)
     605         301 :     return Flx_to_ZX(Flx_factcyclo(n, p[2], 1));
     606             :   else
     607          42 :     return FpX_factcyclo(n, p, 1);
     608             : }
     609             : 
     610             : static void
     611       17094 : Flx_red_inplace(GEN x, ulong p)
     612             : {
     613       17094 :   long i, l = lg(x);
     614      274540 :   for (i=2; i<l; i++) x[i] = uel(x, i)%p;
     615       17094 :   Flx_renormalize(x, l);
     616       17094 : }
     617             : 
     618             : /* x[i], T[i] < pn */
     619             : static GEN
     620       39046 : Flxq_xi_conj(GEN x, GEN T, long j, long d, long pn)
     621             : {
     622       39046 :   long i, deg = degpol(x);
     623       39046 :   GEN z = const_vecsmall(d+1, 0);
     624     1032304 :   for (i=0; i<=deg; i++) z[2+Fl_mul(i, j, d)] = x[2+i];
     625       39046 :   return Flx_rem(Flx_renormalize(z, d+2), T, pn);
     626             : }
     627             : 
     628             : static GEN
     629         966 : FlxqX_xi_conj(GEN x, GEN T, long j, long d, long pn)
     630             : {
     631         966 :   long i, l = lg(x);
     632             :   GEN z;
     633         966 :   z = cgetg(l, t_POL); z[1] = evalsigne(1) | evalvarn(0);
     634       40012 :   for (i=2; i<l; i++) gel(z, i) = Flxq_xi_conj(gel(x, i), T, j, d, pn);
     635         966 :   return z;
     636             : }
     637             : 
     638             : static GEN
     639           0 : FlxqX_xi_norm(GEN x, GEN T, long p, long d, long pn)
     640             : {
     641           0 :   long i, d_chi = degpol(T);
     642           0 :   GEN z = x, z1 = x;
     643           0 :   for (i=1; i<d_chi; i++)
     644             :   {
     645           0 :     z1 = FlxqX_xi_conj(z1, T, p, d, pn);
     646           0 :     z = FlxqX_mul(z, z1, T, pn);
     647             :   }
     648           0 :   return z;
     649             : }
     650             : 
     651             : /* assume 0 <= x[i], y[j] <= m-1 */
     652             : static GEN
     653          15 : FpV_shift_add(GEN x, GEN y, GEN m, long start, long end)
     654             : {
     655             :   long i, j;
     656      222300 :   for (i=start, j=1; i<=end; i++, j++)
     657             :   {
     658      222285 :     pari_sp av = avma;
     659      222285 :     GEN z = addii(gel(x, i), gel(y, j));
     660      222285 :     gel(x, i) = (cmpii(z, m) >= 0)? gerepileuptoint(av, subii(z, m)): z;
     661             :   }
     662          15 :   return x;
     663             : }
     664             : 
     665             : /* assume 0 <= x[i], y[j] <= m-1 */
     666             : static GEN
     667          10 : FpV_shift_sub(GEN x, GEN y, GEN m, long start, long end)
     668             : {
     669             :   long i, j;
     670      112430 :   for (i=start, j=1; i<=end; i++, j++)
     671             :   {
     672      112420 :     pari_sp av = avma;
     673      112420 :     GEN z = subii(gel(x, i), gel(y, j));
     674      112420 :     gel(x, i) = (signe(z) < 0)? gerepileuptoint(av, addii(z, m)): z;
     675             :   }
     676          10 :   return x;
     677             : }
     678             : 
     679             : /* assume 0 <= x[i], y[j] <= m-1 */
     680             : static GEN
     681         173 : Flv_shift_add(GEN x, GEN y, ulong m, long start, long end)
     682             : {
     683             :   long i, j;
     684     2320113 :   for (i=start, j=1; i<=end; i++, j++)
     685             :   {
     686     2319940 :     ulong xi = x[i], yj = y[j];
     687     2319940 :     x[i] = Fl_add(xi, yj, m);
     688             :   }
     689         173 :   return x;
     690             : }
     691             : 
     692             : /* assume 0 <= x[i], y[j] <= m-1 */
     693             : static GEN
     694         102 : Flv_shift_sub(GEN x, GEN y, ulong m, long start, long end)
     695             : {
     696             :   long i, j;
     697     1165182 :   for (i=start, j=1; i<=end; i++, j++)
     698             :   {
     699     1165080 :     ulong xi = x[i], yj = y[j];
     700     1165080 :     x[i] = Fl_sub(xi, yj, m);
     701             :   }
     702         102 :   return x;
     703             : }
     704             : 
     705             : /* return 0 if p|x. else return 1 */
     706             : INLINE long
     707         896 : Flx_divcheck(GEN x, ulong p)
     708             : {
     709         896 :   long i, l = lg(x);
     710         910 :   for (i=2; i<l; i++) if (uel(x, i)%p) return 1;
     711         448 :   return 0;
     712             : }
     713             : 
     714             : static long
     715         448 : FlxX_weier_deg(GEN pol, long p)
     716             : {
     717         448 :   long i, l = lg(pol);
     718         896 :   for (i=2; i<l && Flx_divcheck(gel(pol, i), p)==0; i++);
     719         448 :   return (i<l)?i-2:-1;
     720             : }
     721             : 
     722             : static long
     723        1582 : Flx_weier_deg(GEN pol, long p)
     724             : {
     725        1582 :   long i, l = lg(pol);
     726        3997 :   for (i=2; i<l && pol[i]%p==0; i++);
     727        1582 :   return (i<l)?i-2:-1;
     728             : }
     729             : 
     730             : static GEN
     731         308 : Flxn_shift_mul(GEN g, long n, GEN p, long d, long m)
     732             : {
     733         308 :   return Flx_shift(Flxn_mul(g, p, d, m), n);
     734             : }
     735             : 
     736             : INLINE long
     737        1057 : deg_trunc(long lam, long p, long n, long pn)
     738             : {
     739             :   long r, x, d;
     740        1260 :   for (r=1,x=p; x<lam; r++) x *= p;  /* r is min int s.t. lam<=p^r */
     741        1057 :   if ((d = (n-r+2)*lam+1)>=pn) d = pn;
     742        1057 :   return d;
     743             : }
     744             : 
     745             : /*  Flx_translate1_basecase(g, pn) becomes slow when degpol(g)>1000.
     746             :  *  So I wrote Flxn_translate1().
     747             :  *  I need lambda to truncate pol.
     748             :  *  But I need to translate T --> 1+T to know lambda.
     749             :  *  Though the code has a little overhead, it is still fast. */
     750             : static GEN
     751         756 : Flxn_translate1(GEN g, long p, long n)
     752             : {
     753             :   long i, j, d, lam, pn, start;
     754         756 :   if (n==1) start = 3;
     755          70 :   else if (n==2) start = 9;
     756          70 :   else start = 10;
     757         756 :   pn = upowuu(p, n);
     758         756 :   for (lam=start; lam; lam<<=1)  /* least upper bound is 3 */
     759             :   {
     760             :     GEN z;
     761         756 :     d = deg_trunc(lam, p, n, pn);
     762         756 :     z = const_vecsmall(d+1, 0);  /* z[2],...,z[d+1] <--> a_0,...,a_{d-1} */
     763       44954 :     for (i=degpol(g); i>=0; i--)
     764             :     {
     765     1683486 :       for (j=d+1; j>2; j--) z[j] = Fl_add(z[j], z[j-1], pn);  /* z = z*(1+T) */
     766       44198 :       z[2] = Fl_add(z[2], g[2+i], pn);
     767             :     }
     768         756 :     z = Flx_renormalize(z, d+2);
     769         756 :     if (Flx_weier_deg(z, p) <= lam) return z;
     770             :   }
     771             :   return NULL; /*LCOV_EXCL_LINE*/
     772             : }
     773             : 
     774             : static GEN
     775         224 : FlxXn_translate1(GEN g, long p, long n)
     776             : {
     777             :   long i, j, d, lam, pn, start;
     778             :   GEN z;
     779         224 :   if (n==1) start = 3;
     780           0 :   else if (n==2) start = 9;
     781           0 :   else start = 10;
     782         224 :   pn = upowuu(p, n);
     783         224 :   for (lam=start; lam; lam<<=1)  /* least upper bound is 3 */
     784             :   {
     785         224 :     d = deg_trunc(lam, p, n, pn);
     786         224 :     z = const_vec(d+1, pol0_Flx(0));  /* z[2],...,z[d+1] <--> a_0,...,a_{d-1} */
     787         224 :     settyp(z, t_POL); z[1] = evalsigne(1) | evalvarn(0);
     788        9408 :     for (i=degpol(g); i>=0; i--)
     789             :     {
     790       64288 :       for (j=d+1; j>2; j--) gel(z, j) = Flx_add(gel(z, j), gel(z, j-1), pn);
     791        9184 :       gel(z, 2) = Flx_add(gel(z, 2), gel(g, 2+i), pn);
     792             :     }
     793         224 :     z = FlxX_renormalize(z, d+2);
     794         224 :     if (FlxX_weier_deg(z, p) <= lam) return z;
     795             :   }
     796             :   return NULL; /*LCOV_EXCL_LINE*/
     797             : }
     798             : 
     799             : /* lam < 0 => error (lambda can't be determined)
     800             :  * lam = 0 => return 1
     801             :  * lam > 0 => return dist. poly. of degree lam. */
     802             : static GEN
     803          84 : Flxn_Weierstrass_prep(GEN g, long p, long n, long d_chi)
     804             : {
     805          84 :   long i, r0, d, dg = degpol(g), lam, pn, t;
     806             :   ulong lam0;
     807             :   GEN U, UINV, P, PU, g0, g1, gp, gU;
     808          84 :   if ((lam = Flx_weier_deg(g, p))==0) return(pol1_Flx(0));
     809          77 :   else if (lam<0)
     810           0 :     pari_err(e_MISC,"Flxn_Weierstrass_prep: precision too low. Increase n!");
     811          77 :   lam0 = lam/d_chi;
     812          77 :   pn = upowuu(p, n);
     813          77 :   d = deg_trunc(lam, p, n, pn);
     814          77 :   if (d>dg) d = dg;
     815          77 :   if (d<=lam) d=1+lam;
     816         140 :   for (r0=1; upowuu(p, r0)<lam0; r0++);
     817          77 :   g = Flxn_red(g, d);
     818          77 :   t = Fl_inv(g[2+lam], pn);
     819          77 :   g = Flx_Fl_mul(g, t, pn);  /* normalized so as g[2+lam]=1 */
     820          77 :   U = Flx_shift(g, -lam);
     821          77 :   UINV = Flxn_inv(U, d, pn);
     822          77 :   P = zx_z_divexact(Flxn_red(g, lam), p);  /* assume g[i] <= LONG_MAX */
     823          77 :   PU = Flxn_mul(P, UINV, d, pn);
     824          77 :   gU = Flxn_mul(g, UINV, d, pn);
     825          77 :   g0 = pol1_Flx(0);
     826          77 :   g1 = pol1_Flx(0);
     827         385 :   for (i=1; i<n; i++)
     828             :   {
     829         308 :     g1 = Flxn_shift_mul(g1, -lam, PU, d, pn);
     830         308 :     gp = Flx_Fl_mul(g1, upowuu(p, i), pn);
     831         308 :     g0 = (i&1)?Flx_sub(g0, gp, pn):Flx_add(g0, gp, pn);
     832             :   }
     833          77 :   g0 = Flxn_mul(g0, gU, lam+1, pn);
     834          77 :   g0 = Flx_red(g0, upowuu(p, (p==2)?n-r0:n+1-r0));
     835          77 :   return g0;
     836             : }
     837             : 
     838             : /* xi_n and Iwasawa pol. for Q(sqrt(m)) and p
     839             :  *
     840             :  * (flag&1)!=0 ==> output xi_n
     841             :  * (flag&2)!=0 ==> output power series
     842             :  * (flag&4)!=0 ==> output Iwasawa polynomial */
     843             : static GEN
     844          14 : imagquadstkpol(long p, long m, long n)
     845             : {
     846          14 :   long pn = upowuu(p, n);
     847             :   GEN pol, stk, stk2;
     848             :   int *chi;
     849          14 :   if (p==2 && (m==-1 || m==-2 || m==-3 || m==-6)) return nullvec();
     850          14 :   if (p==3 && m==-3) return nullvec();
     851          14 :   if (p==2 && m%2==0) m /= 2;
     852          14 :   chi = set_quad_chi_2(m);
     853          14 :   stk = (p==2)? quadstk2(m, n, chi): quadstkp(p, m, n, chi);
     854          14 :   stk2 = zx_to_Flx(stk, pn);
     855          14 :   pol = Flxn_Weierstrass_prep(zlx_translate1(stk2, p, n), p, n, 1);
     856          14 :   return degpol(pol)? mkvec(Flx_to_ZX(pol)): nullvec();
     857             : }
     858             : 
     859             : /* a mod p == g^i mod p ==> omega(a)=zeta_(p-1)^(-i)
     860             :  *  Chi[g^i mod p]=i (0 <= i <= p-2) */
     861             : static GEN
     862           0 : get_teich(long p, long g)
     863             : {
     864           0 :   long i, gi = 1, p1 = p-1;
     865           0 :   GEN Chi = cgetg(p, t_VECSMALL);
     866           0 :   for (i=0; i<p1; i++) { Chi[gi] = i; gi = Fl_mul(gi, g, p); }
     867           0 :   return Chi;
     868             : }
     869             : 
     870             : /* Ichimura-Sumida criterion for Greenberg conjecture for real quadratic field.
     871             :  * chi: character of Q(sqrt(m)), omega: Teichmuller character mod p or 4.
     872             :  * Get Stickelberger element from chi^* = omega*chi^(-1) and convert it to
     873             :  * power series by the correspondence (Q_n/Q,1+q0)^(-1) <-> (1+T)(1+q0)^(-1) */
     874             : static GEN
     875          14 : realquadstkpol(long p, long m, long n)
     876             : {
     877             :   int *chi;
     878          14 :   long pnm1 = upowuu(p, n-1),pn = p*pnm1, pn1 = p*pn, d, q0;
     879             :   GEN stk, ser, pol;
     880          14 :   if (m==1) pari_err_DOMAIN("quadstkpol", "m", "=", gen_1, gen_1);
     881          14 :   if (p==2 && (m&1)==0) m>>=1;
     882          14 :   d = ((m&3)==1)?m:m<<2;
     883          14 :   q0 = ulcm((p==2)?4:p, d);
     884          14 :   if (p==2)
     885             :   {
     886          14 :     chi = set_quad_chi_2(-m);
     887          14 :     stk = quadstk2(-m, n, chi);
     888          14 :     stk = zx_to_Flx_inplace(stk, pn);
     889             :   }
     890           0 :   else if (p==3 && m%3==0 && kross(-m/3,3)==1)
     891           0 :   {
     892           0 :     long m3 = m/3;
     893           0 :     chi = set_quad_chi_2(-m3);
     894           0 :     stk = quadstkp(3, -m3, n, chi);
     895           0 :     stk = zx_to_Flx_inplace(stk, pn);
     896             :   }
     897             :   else
     898             :   {
     899           0 :     long g = pgener_Zl(p);
     900           0 :     long x = Fl_powu(Fl_inv(g, p), pnm1, pn);
     901           0 :     GEN Chi = get_teich(p, g);
     902           0 :     GEN Gam = set_gam((1+q0)%pn1, p, n);
     903           0 :     chi = set_quad_chi_2(m);
     904           0 :     stk = quadteichstk(Chi, chi, Gam, p, m, n);  /* exact */
     905           0 :     stk = zxX_to_FlxX(stk, pn);  /* approx. */
     906           0 :     stk = FlxY_evalx(stk, x, pn);
     907             :   }
     908          14 :   stk = Flx_rescale_inplace(Flx_recip1_inplace(stk, pn), (1+q0)%pn, pn);
     909          14 :   ser = Flxn_translate1(stk, p, n);
     910          14 :   pol = Flxn_Weierstrass_prep(ser, p, n, 1);
     911          14 :   return degpol(pol)? mkvec(Flx_to_ZX(pol)): nullvec();
     912             : }
     913             : 
     914             : /* m > 0 square-free. lambda_2(Q(sqrt(-m)))
     915             :  * Kida, Tohoku Math. J. vol.31 (1979), 91-96, Theorem 1. */
     916             : static GEN
     917           0 : quadlambda2(long m)
     918             : {
     919             :   long i, l, L;
     920             :   GEN P;
     921           0 :   if ((m&1)==0) m >>= 1;  /* lam_2(Q(sqrt(-m)))=lam_2(Q(sqrt(-2*m))) */
     922           0 :   if (m <= 3) return mkvecs(0);
     923           0 :   P = gel(factoru(m), 1); l = lg(P);
     924           0 :   for (L = -1,i = 1; i < l; i++) L += 1L << (-3 + vals(P[i]-1) + vals(P[i]+1));
     925           0 :   return mkvecs(L);
     926             : }
     927             : 
     928             : /* Iwasawa lambda invariant of Q(sqrt(m)) (m<0) for p
     929             :  * |A_n|=p^(e[n])
     930             :  * kross(m,p)!=1 : e[n]-e[n-1]<eulerphi(p^n)  ==> lambda=e[n]-e[n-1]
     931             :  * kross(m,p)==1 : e[n]-e[n-1]<=eulerphi(p^n) ==> lambda=e[n]-e[n-1]
     932             :  * Gold, Acta Arith. XXVI (1974), p.25, Cor. 3
     933             :  * Gold, Acta Arith. XXVI (1975), p.237, Cor. */
     934             : static GEN
     935          21 : quadlambda(long p, long m)
     936             : {
     937             :   long flag, n, phipn;
     938          21 :   GEN e = cgetg(31, t_VECSMALL);
     939             :   int *chi;
     940          21 :   if (m>0) pari_err_IMPL("plus part of lambda invariant in quadlambda()");
     941          21 :   if (p==2) return quadlambda2(-m);
     942          21 :   if (p==3 && m==-3) return mkvec3(gen_0, gen_0, nullvec());
     943          21 :   flag = kross(m, p);
     944          21 :   e[1] = Z_lval(quadclassno(quaddisc(stoi(m))), p);
     945          21 :   if (flag!=1 && e[1]==0) return mkvec3(gen_0, gen_0, nullvec());
     946          21 :   chi = set_quad_chi_2(m);
     947          21 :   phipn = p-1;  /* phipn=phi(p^n) */
     948          56 :   for (n=1; n; n++, phipn *= p)
     949             :   {
     950          56 :     long L = flag? ediff(p, m, n, chi): ediff_ber(p, m, n, chi);
     951          56 :     e[n+1] = e[n] + L;
     952          56 :     if ((flag!=1 && (L < phipn))|| (flag==1 && (L <= phipn))) break;
     953             :   }
     954          21 :   return vecsmall2vec2(e, n);
     955             : }
     956             : 
     957             : /* factor n-th cyclotomic polynomial mod p^r and return a minimal
     958             :  *  polynomial of zeta_n over Q_p.
     959             :  *  phi(n)=deg*n_conj, n_conj == 1 <=> polcyclo(n) is irred mod p. */
     960             : static GEN
     961         945 : set_minpol(ulong n, GEN p, ulong r, long n_conj)
     962             : {
     963             :   GEN z, v, pol, pr;
     964             :   pari_timer ti;
     965         945 :   if (umodiu(p, n)==1) /* zeta_n in Z_p, faster than polcyclo() */
     966             :   {
     967         420 :     GEN prm1 = powiu(p, r-1), pr = mulii(prm1, p); /* pr=p^r */
     968         420 :     GEN prn = diviuexact(subii(pr, prm1), n);      /* prn=phi(p^r)/n */
     969         420 :     z = Fp_pow(pgener_Fp(p), prn, pr);
     970         420 :     return deg1pol_shallow(gen_1, Fp_neg(z, pr), 0);
     971             :   }
     972         525 :   pr = powiu(p, r);
     973         525 :   pol = polcyclo(n, 0);
     974         525 :   if (n_conj==1) return FpX_red(pol, pr);
     975         343 :   if (DEBUGLEVEL>3) timer_start(&ti);
     976         343 :   z = FpX_one_cyclo(n, p);
     977         343 :   if (DEBUGLEVEL>3) timer_printf(&ti, "FpX_one_cyclo:n=%ld  ", n);
     978         343 :   v = ZpX_liftfact(pol, mkvec2(z, FpX_div(pol, z, p)), pr, p, r);
     979         343 :   return gel(v, 1);
     980             : }
     981             : 
     982             : static GEN
     983          91 : set_minpol_teich(ulong g_K, GEN p, ulong r)
     984             : {
     985          91 :   GEN prm1 = powiu(p, r-1), pr = mulii(prm1, p), z;
     986          91 :   z = Fp_pow(Fp_inv(utoi(g_K), p), prm1, pr);
     987          91 :   return deg1pol_shallow(gen_1, Fp_neg(z, pr), 0);
     988             : }
     989             : 
     990             : static long
     991       18963 : srh_1(GEN H)
     992             : {
     993       18963 :   GEN bits = gel(H, 3);
     994       18963 :   ulong f = bits[1];
     995       18963 :   return F2v_coeff(bits, f-1);
     996             : }
     997             : 
     998             : /* (1/f)sum_{1<=a<=f}a*chi^{-1}(a) = -(1/(2-chi(a)))sum_{1<=a<=f/2} chi^{-1}(a)
     999             :  *  does not overflow */
    1000             : static GEN
    1001       13146 : zx_ber_num(GEN Chi, long f, long d)
    1002             : {
    1003       13146 :   long i, f2 = f>>1;
    1004       13146 :   GEN x = const_vecsmall(d+1, 0), x2 = x+2;
    1005    51965081 :   for (i = 1; i <= f2; i++)
    1006    51951935 :     if (Chi[i] >= 0) x2[Chi[i]] ++;
    1007       13146 :   return zx_renormalize(x, d+2);
    1008             : }
    1009             : 
    1010             : /* x a zx
    1011             :  * zx_ber_num is O(f). ZX[FpX,Flx]_ber_conj is O(d). Sometimes d<<f. */
    1012             : static GEN
    1013       26257 : ZX_ber_conj(GEN x, long j, long d)
    1014             : {
    1015       26257 :   long i, deg = degpol(x);
    1016       26257 :   GEN y = pol_zero(d), x2 = x+2, y2 = y+2;
    1017      818202 :   for (i=0; i<=deg; i++) gel(y2, Fl_mul(i, j, d)) = stoi(x2[i]);
    1018       26257 :   return ZX_renormalize(y, d+2);
    1019             : }
    1020             : 
    1021             : /* x a zx */
    1022             : static GEN
    1023         252 : FpX_ber_conj(GEN x, long j, long d, GEN p)
    1024             : {
    1025         252 :   long i, deg = degpol(x);
    1026         252 :   GEN y = pol_zero(d), x2 = x+2, y2 = y+2;
    1027         756 :   for (i=0; i<=deg; i++) gel(y2, Fl_mul(i, j, d)) = modsi(x2[i], p);
    1028         252 :   return FpX_renormalize(y, d+2);
    1029             : }
    1030             : 
    1031             : /* x a zx */
    1032             : static GEN
    1033       21756 : Flx_ber_conj(GEN x, long j, long d, ulong p)
    1034             : {
    1035       21756 :   long i, deg = degpol(x);
    1036       21756 :   GEN y = const_vecsmall(d+1, 0), x2 = x+2, y2 = y+2;
    1037     1076565 :   for (i=0; i<=deg; i++) y2[Fl_mul(i, j, d)] = umodsu(x2[i], p);
    1038       21756 :   return Flx_renormalize(y, d+2);
    1039             : }
    1040             : 
    1041             : static GEN
    1042       26257 : ZX_ber_den(GEN Chi, long j, long d)
    1043             : {
    1044       26257 :   GEN x = pol_zero(d), x2 = x+2;
    1045       26257 :   if (Chi[2]>=0) gel(x2, Fl_neg(Fl_mul(Chi[2], j, d), d)) = gen_1;
    1046       26257 :   gel(x2, 0) = subiu(gel(x2, 0), 2);
    1047       26257 :   return ZX_renormalize(x, d+2);
    1048             : }
    1049             : 
    1050             : static GEN
    1051       14490 : Flx_ber_den(GEN Chi, long j, long d, ulong p)
    1052             : {
    1053       14490 :   GEN x = const_vecsmall(d+1, 0), x2 = x+2;
    1054       14490 :   if (Chi[2]>=0) x2[Fl_neg(Fl_mul(Chi[2], j, d), d)] = 1;
    1055       14490 :   x2[0] = Fl_sub(x2[0], 2, p);
    1056       14490 :   return Flx_renormalize(x, d+2);
    1057             : }
    1058             : 
    1059             : /* x is ZX of deg <= d-1 */
    1060             : static GEN
    1061         196 : ber_conj(GEN x, long k, long d)
    1062             : {
    1063         196 :   long i, deg = degpol(x);
    1064         196 :   GEN z = pol_zero(d);
    1065         196 :   if (k==1)
    1066           0 :     for (i=0; i<=deg; i++) gel(z, 2+i) = gel(x, 2+i);
    1067             :   else
    1068      122206 :     for (i=0; i<=deg; i++) gel(z, 2+Fl_mul(i, k, d)) = gel(x, 2+i);
    1069         196 :   return ZX_renormalize(z, d+2);
    1070             : }
    1071             : 
    1072             : /* The computation is fast when p^n and el=1+k*f*p^n are less than 2^64
    1073             :  *  for m <= n <= M
    1074             :  *  We believe M>=3 is enough when f%p=0 and M>=2 is enough for other case
    1075             :  *  because we expect that p^2 does not divide |A_{K,psi}| for a large p.
    1076             :  *  FIXME: M should be set according to p and f. */
    1077             : static void
    1078         196 : set_p_f(GEN pp, ulong f, long *pm, long *pM)
    1079             : {
    1080         196 :   ulong p = itou_or_0(pp);
    1081         196 :   if (!p || p >= 2000000) { *pm=2; *pM = dvdui(f, pp)? 3: 2; }
    1082         189 :   else if (p == 3)      { *pm=5; *pM=20; }
    1083         119 :   else if (p == 5)      { *pm=5; *pM=13; }
    1084          56 :   else if (p == 7)      { *pm=5; *pM=11; }
    1085          42 :   else if (p == 11)     { *pm=5; *pM=9; }
    1086          35 :   else if (p == 13)     { *pm=5; *pM=8; }
    1087          28 :   else if (p < 400)     { *pm=5; *pM=7; }
    1088           0 :   else if (p < 5000)    { *pm=3; *pM=5; }
    1089           0 :   else if (p < 50000)   { *pm=2; *pM=4; }
    1090           0 :   else                  { *pm=2; *pM=3; }
    1091         196 : }
    1092             : 
    1093             : static GEN
    1094       18795 : subgp2ary(GEN H, long n)
    1095             : {
    1096       18795 :   GEN v = gel(H, 3), w = cgetg(n+1, t_VECSMALL);
    1097       18795 :   long i, j, f = v[1];
    1098   399982464 :   for (i = 1, j = 0; i <= f; i++)
    1099   399963669 :     if (F2v_coeff(v,i)) w[++j] = i;
    1100       18795 :   return w;
    1101             : }
    1102             : 
    1103             : static GEN
    1104       19124 : Flv_FlvV_factorback(GEN g, GEN x, ulong q)
    1105       90216 : { pari_APPLY_ulong(Flv_factorback(g, gel(x,i), q)) }
    1106             : 
    1107             : /* lift chi character on G/H to character on G */
    1108             : static GEN
    1109       18795 : zncharlift(GEN chi, GEN ncycGH, GEN U, GEN cycG)
    1110             : {
    1111       18795 :   GEN nchi = char_normalize(chi, ncycGH);
    1112       18795 :   GEN c = ZV_ZM_mul(gel(nchi, 2), U), d = gel(nchi, 1);
    1113       18795 :   return char_denormalize(cycG, d, c);
    1114             : }
    1115             : 
    1116             : /* 0 <= c[i] < d, i=1..r; (c[1],...,c[r], d) = 1; find e[i] such that
    1117             :  * sum e[i]*c[i] = 1 mod d */
    1118             : static GEN
    1119       18795 : Flv_extgcd(GEN c, ulong d)
    1120             : {
    1121       18795 :   long i, j, u, f, l = lg(c);
    1122       18795 :   GEN e = zero_zv(l-1);
    1123       18795 :   if (l == 1) return e;
    1124       46053 :   for (f = d, i = 1; f != 1 && i < l; i++)
    1125             :   {
    1126       27258 :     f = cbezout(f, itou(gel(c,i)), &u, &e[i]);
    1127       27258 :     if (!e[i]) continue;
    1128       25004 :     e[i] = umodsu(e[i], d);
    1129       25004 :     u = umodsu(u, d);
    1130       32998 :     if (u != 1) for (j = 1; j < i; j++) e[j] = Fl_mul(e[j], u, d);
    1131             :   }
    1132       18795 :   return e;
    1133             : }
    1134             : 
    1135             : /* f!=p; return exact xi. */
    1136             : static GEN
    1137         462 : get_xi_1(GEN Chi, GEN Gam, long p, long f, long n, long d, ulong pm)
    1138             : {
    1139         462 :   GEN Gam1 = Gam+1, xi;
    1140             :   long i, j, j0, f0, pn, pn1, deg, pn1f;
    1141             : 
    1142         462 :   f0 = (f%p)?f:f/p;
    1143         462 :   pn = upowuu(p, n); pn1 = p*pn; pn1f = pn1%f;
    1144         462 :   xi = cgetg(pn+2, t_POL); xi[1] = evalsigne(1) | evalvarn(0);
    1145       17556 :   for (i=0; i<pn; i++) gel(xi, 2+i) = const_vecsmall(d+1, 0);
    1146      432754 :   for (j=1; j<pn1; j++)
    1147             :   {
    1148             :     long ipn1,*xij0;
    1149      432292 :     if ((j0 = Gam1[j])<0) continue;
    1150      415660 :     ipn1 = j%f; xij0 = gel(xi, 2+j0)+2;
    1151  4027401588 :     for (i=1; i<f0; i++)
    1152             :     {
    1153  4026985928 :       if ((ipn1 += pn1f) >= f) ipn1 -= f;
    1154  4026985928 :       if (ipn1==0 || (deg = Chi[ipn1])<0) continue;
    1155  2203029612 :       xij0[deg] += i;
    1156             :     }
    1157             :   }
    1158       17556 :   for (i=0; i<pn; i++) Flx_red_inplace(gel(xi, 2+i), pm);
    1159         462 :   return FlxX_renormalize(xi, pn+2);
    1160             : }
    1161             : 
    1162             : /* f=p; return p^(n+1)*xi mod pm. */
    1163             : static GEN
    1164           0 : get_xi_2(GEN Chi, GEN Gam, long p, long f, long n, long d, ulong pm)
    1165             : {
    1166             :   long a, amodf, i, j0, pn, pn1, deg;
    1167           0 :   GEN Gam1 = Gam+1, xi;
    1168             : 
    1169           0 :   pn = upowuu(p, n); pn1 = p*pn;
    1170           0 :   xi = cgetg(pn+2, t_POL); xi[1] = evalsigne(1) | evalvarn(0);
    1171           0 :   for (i=0; i<pn; i++) gel(xi, 2+i) = const_vecsmall(d+1, 0);
    1172           0 :   for (a=1,amodf=0; a<pn1; a++)  /* xi is exact */
    1173             :   {
    1174           0 :     if (++amodf==f) amodf = 0;
    1175           0 :     if ((j0=Gam1[a])<0 || amodf==0 || (deg=Chi[amodf])<0) continue;
    1176           0 :     mael(xi, 2+j0, 2+deg) += a;
    1177             :   }
    1178           0 :   for (i=0; i<pn; i++) Flx_red_inplace(gel(xi, 2+i), pm);
    1179           0 :   return FlxX_renormalize(xi, pn+2);
    1180             : }
    1181             : 
    1182             : static GEN
    1183          56 : pol_chi_xi(GEN K, long p, long j, long n)
    1184             : {
    1185          56 :   pari_sp av = avma;
    1186          56 :   GEN MinPol2 = gel(K, 7), xi = gel(K, 8);
    1187          56 :   long d = K_get_d(K), f = K_get_f(K), d_chi = K_get_dchi(K);
    1188          56 :   long wd, minpolpow = (f==p)?2*n+1:n, pm = upowuu(p, minpolpow);
    1189             :   GEN ser, pol, xi_conj;
    1190             :   pari_timer ti;
    1191             : 
    1192             :   /* xi is FlxX mod p^m, MinPol2 is Flx mod p^m, xi_conj is FlxqX. */
    1193          56 :   xi_conj = FlxqX_xi_conj(xi, MinPol2, j, d, pm);
    1194          56 :   if (d_chi==1)  /* d_chi==1 if f==p */
    1195             :   {
    1196          56 :     xi_conj = FlxX_to_Flx(xi_conj);
    1197          56 :     if (f==p) xi_conj = zx_z_divexact(xi_conj, upowuu(p, n+1));
    1198             :   }
    1199             :   /* Now xi_conj is mod p^n */
    1200          56 :   if (DEBUGLEVEL>1) timer_start(&ti);
    1201          56 :   ser = (d_chi==1) ? Flxn_translate1(xi_conj, p, n)
    1202          56 :     : FlxXn_translate1(xi_conj, p, n);
    1203          56 :   if (DEBUGLEVEL>1) timer_printf(&ti, "Flx%sn_translate1",(d_chi==1)?"":"X");
    1204          56 :   wd = (d_chi==1)?Flx_weier_deg(ser, p):FlxX_weier_deg(ser, p);
    1205          56 :   if (wd<0) pari_err(e_MISC,"pol_chi_xi: precision too low. Increase n!\n");
    1206          56 :   else if (wd==0) return pol_1(0);
    1207             :   /* wd>0, convert to dist. poly. */
    1208          56 :   if (d_chi>1)  /* f!=p. minpolpow==n */
    1209             :   {
    1210           0 :     ser = FlxqX_xi_norm(ser, MinPol2, p, d, upowuu(p, n));
    1211           0 :     ser = FlxX_to_Flx(ser);
    1212             :   }
    1213          56 :   pol = Flx_to_ZX(Flxn_Weierstrass_prep(ser, p, n, d_chi));
    1214          56 :   setvarn(pol, fetch_user_var("T"));
    1215             : #ifdef DEBUG
    1216             :   if (wd>0 && d_chi>1)
    1217             :     err_printf("(wd,d_chi,p,f,d,j,H)=(%ld,%ld,%ld,%ld,%ld,%ld,%Ps)\n",
    1218             :         wd,d_chi,p,f,d,j,gmael3(K, 1, 1, 1));
    1219             : #endif
    1220          56 :   return gerepilecopy(av, pol);
    1221             : }
    1222             : 
    1223             : /* return 0 if lam_psi (psi=chi^j) is determined to be zero.
    1224             :  * else return -1.
    1225             :  * If psi(p)!=1, then N_{Q(zeta_d)/Q}(1-psi(p))!=0 (mod p) */
    1226             : static long
    1227       14504 : lam_chi_ber(GEN K, long p, long j)
    1228             : {
    1229       14504 :   pari_sp av = avma;
    1230       14504 :   GEN B1, B2, Chi = gel(K, 2), MinPol2 = gel(K, 7), B_num = gel(K, 8);
    1231       14504 :   long x, p2 = p*p, d = K_get_d(K), f = K_get_f(K);
    1232             : 
    1233       14504 :   if (f == d+1 && p == f && j == 1) return 0;  /* Teichmuller */
    1234             : 
    1235       14490 :   B1 = Flx_rem(Flx_ber_conj(B_num, j, d, p2), MinPol2, p2);
    1236       14490 :   B2 = Flx_rem(Flx_ber_den(Chi, j, d, p2), MinPol2, p2);
    1237       14490 :   if (degpol(B1)<0 || degpol(B2)<0)
    1238          49 :     return gc_long(av, -1); /* 0 mod p^2 */
    1239       14441 :   x = zx_lval(B1, p) - zx_lval(B2, p);
    1240       14441 :   if (x<0) pari_err_BUG("subcycloiwasawa [Bernoulli number]");
    1241       14441 :   return gc_long(av, x==0 ? 0: -1);
    1242             : }
    1243             : 
    1244             : static long
    1245         910 : lam_chi_xi(GEN K, long p, long j, long n)
    1246             : {
    1247         910 :   pari_sp av = avma;
    1248         910 :   GEN xi_conj, z, MinPol2 = gel(K, 7), xi = gel(K, 8);
    1249         910 :   long d = K_get_d(K), f = K_get_f(K), d_chi = K_get_dchi(K);
    1250         910 :   long wd, minpolpow = (f==p)?n+2:1, pm = upowuu(p, minpolpow);
    1251             : 
    1252             :   /* xi is FlxX mod p^m, MinPol2 is Flx mod p^m, xi_conj is FlxqX. */
    1253         910 :   xi_conj = FlxqX_xi_conj(xi, MinPol2, j, d, pm);
    1254         910 :   if (d_chi==1)  /* d_chi==1 if f==p */
    1255             :   {
    1256         686 :     xi_conj = FlxX_to_Flx(xi_conj);
    1257         686 :     if (f==p) xi_conj = zx_z_divexact(xi_conj, upowuu(p, n+1));
    1258             :   }
    1259             :   /* Now xi_conj is mod p^n */
    1260         686 :   z = (d_chi==1) ? Flxn_translate1(xi_conj, p, n)
    1261         910 :     : FlxXn_translate1(xi_conj, p, n);
    1262         910 :   wd = (d_chi==1)?Flx_weier_deg(z, p):FlxX_weier_deg(z, p);
    1263             : #ifdef DEBUG
    1264             :   if (wd>0 && d_chi>1)
    1265             :     err_printf("(wd,d_chi,p,f,d,j,H)=(%ld,%ld,%ld,%ld,%ld,%ld,%Ps)\n",
    1266             :         wd,d_chi,p,f,d,j,gmael3(K, 1, 1, 1));
    1267             : #endif
    1268         910 :   return gc_long(av, wd<0 ? -1 : wd*d_chi);
    1269             : }
    1270             : 
    1271             : /* K = [H1, Chi, Minpol, C, [d_chi, n_conj]] */
    1272             : static GEN
    1273          56 : imag_cyc_pol(GEN K, long p, long n)
    1274             : {
    1275          56 :   pari_sp av = avma;
    1276          56 :   GEN Chi = gel(K, 2), MinPol = gel(K, 3), C = gel(K, 4), MinPol2;
    1277          56 :   long d_K = K_get_d(K), f = K_get_f(K), n_conj = K_get_nconj(K);
    1278          56 :   long i, q0, pn1, pM, pmodf = p%f, n_done = 0;
    1279          56 :   GEN z = nullvec(), Gam, xi, Lam, K2;
    1280             : 
    1281          56 :   Lam = const_vecsmall(n_conj, -1);
    1282          56 :   if (pmodf==0 || Chi[pmodf]) /* mark trivial chi-part using Bernoulli number */
    1283             :   {
    1284          14 :     MinPol2 = ZX_to_Flx(MinPol, p*p);  /* p^2 for B_{1,chi} */
    1285          14 :     K2 = shallowconcat(K, mkvec2(MinPol2, zx_ber_num(Chi, f, d_K)));
    1286          42 :     for (i=1; i<=n_conj; i++)
    1287          28 :       if ((Lam[i] = lam_chi_ber(K2, p, C[i])) == 0) n_done++;
    1288          14 :     if (n_conj==n_done) return gerepilecopy(av, z); /* all chi-parts trivial */
    1289             :   }
    1290          49 :   q0 = (f%p)? f*p: f;
    1291          49 :   pn1 = upowuu(p, n+1);
    1292          49 :   Gam = set_gam((1+q0)%pn1, p, n);
    1293          49 :   pM = upowuu(p, (f==p)? 2*n+1: n);
    1294          49 :   MinPol2 = ZX_to_Flx(MinPol, pM);
    1295           0 :   xi = (f==p)? get_xi_2(Chi, Gam, p, f, n, d_K, pM)
    1296          49 :              : get_xi_1(Chi, Gam, p, f, n, d_K, pM);
    1297          49 :   K2 = shallowconcat(K, mkvec2(MinPol2, xi));
    1298         105 :   for (i=1; i<=n_conj; i++)
    1299             :   {
    1300             :     GEN z1;
    1301          56 :     if (Lam[i]>=0) continue;
    1302          56 :     z1 = pol_chi_xi(K2, p, C[i], n);
    1303          56 :     if (degpol(z1)) z = vec_append(z, z1);  /* degpol(z1) may be zero */
    1304             :   }
    1305          49 :   return gerepilecopy(av, z);
    1306             : }
    1307             : 
    1308             : /* K is an imaginary cyclic extension of degree d contained in Q(zeta_f)
    1309             :  * H is the subgr of G=(Z/fZ)^* corresponding to K
    1310             :  * h=|H|, d*h=phi(f)
    1311             :  * G/H=<g> i.e. g^d \in H
    1312             :  * d_chi=[Q_p(zeta_d):Q_p], i.e. p^d_chi=1 (mod d)
    1313             :  * An inj. char. of G(K/Q) is automatically imaginary.
    1314             :  *
    1315             :  * G(K/Q)=G/H=<g>, chi:G(K/Q) -> overline{Q_p} s.t. chi(g)=zeta_d^(-1)
    1316             :  * Chi[a]=k, k<0  => chi(a)=0
    1317             :  *           k>=0 => chi(a)=zeta_d^(-k)
    1318             :  * psi=chi^j, j in C : repre. of inj. odd char.
    1319             :  * psi(p)==1 <=> chi(p)^j==0 <=> j*Chi[p]=0 (mod d) <=> Chi[p]==0
    1320             :  *
    1321             :  * K = [H1, Chi, Minpol, C, [d_chi, n_conj]] */
    1322             : static long
    1323        3262 : imag_cyc_lam(GEN K, long p)
    1324             : {
    1325        3262 :   pari_sp av = avma;
    1326        3262 :   GEN Chi = gel(K, 2), MinPol = gel(K, 3), C = gel(K, 4), MinPol2;
    1327        3262 :   long d_K = K_get_d(K), f = K_get_f(K), n_conj = K_get_nconj(K);
    1328        3262 :   long i, q0, n, pmodf = p%f, n_done = 0;
    1329             :   ulong pn1, pM;
    1330        3262 :   GEN p0 = utoi(p), Gam, Lam, xi, K2;
    1331             : 
    1332        3262 :   q0 = (f%p)? f*p: f;
    1333        3262 :   Lam = const_vecsmall(n_conj, -1);
    1334        3262 :   if (pmodf==0 || Chi[pmodf])  /* 1st trial is Bernoulli number */
    1335             :   {
    1336        3052 :     MinPol2 = ZX_to_Flx(MinPol, p*p);  /* p^2 for B_{1,chi} */
    1337        3052 :     K2 = shallowconcat(K, mkvec2(MinPol2, zx_ber_num(Chi, f, d_K)));
    1338       17528 :     for (i=1; i<=n_conj; i++)
    1339       14476 :       if ((Lam[i] = lam_chi_ber(K2, p, C[i])) == 0) n_done++;
    1340        3052 :     if (n_conj==n_done) return gc_long(av, 0);  /* all chi-parts trivial */
    1341             :   }
    1342         413 :   pM = pn1 = p;
    1343         413 :   for (n=1; n>=0; n++)  /* 2nd trial is Stickelberger element */
    1344             :   {
    1345         413 :     pn1 *= p; /* p^(n+1) */
    1346         413 :     if (f == p)
    1347             :     { /* do not use set_minpol: it returns a new pol for each call */
    1348           0 :       GEN fac, cofac, v, pol = polcyclo(d_K, 0);
    1349           0 :       pM = pn1 * p; /* p^(n+2) */
    1350           0 :       fac = FpX_red(MinPol, p0); cofac = FpX_div(pol, fac, p0);
    1351           0 :       v = ZpX_liftfact(pol, mkvec2(fac, cofac), utoipos(pM), p0, n+2);
    1352           0 :       MinPol2 = gel(v, 1);
    1353             :     }
    1354         413 :     Gam = set_gam((1+q0)%pn1, p, n);
    1355         413 :     MinPol2 = ZX_to_Flx(MinPol, pM);
    1356           0 :     xi = (f==p)? get_xi_2(Chi, Gam, p, f, n, d_K, pM)
    1357         413 :                : get_xi_1(Chi, Gam, p, f, n, d_K, pM);
    1358         413 :     K2 = shallowconcat(K, mkvec2(MinPol2, xi));
    1359        2205 :     for (i=1; i<=n_conj; i++)
    1360        1792 :       if (Lam[i]<0 && (Lam[i] = lam_chi_xi(K2, p, C[i], n)) >= 0) n_done++;
    1361         413 :     if (n_conj==n_done) break;
    1362             :   }
    1363         413 :   return gc_long(av, zv_sum(Lam));
    1364             : }
    1365             : static GEN
    1366         329 : GHinit(long f, GEN HH, GEN *pcycGH)
    1367             : {
    1368         329 :   GEN G = znstar0(utoipos(f), 1);
    1369         329 :   GEN U, Ui, cycG, cycGH, ncycGH, gG, gGH, vChar, vH1, P, gH = gel(HH, 1);
    1370         329 :   long i, expG, n_f, lgH = lg(gH); /* gens. of H */
    1371         329 :   P = cgetg(lgH, t_MAT);
    1372         805 :   for (i = 1; i < lgH; i++) gel(P,i) = Zideallog(G, utoi(gH[i]));
    1373             : 
    1374             :   /* group structure of G/H */
    1375         329 :   cycG = znstar_get_cyc(G);
    1376         329 :   expG = itou(gel(cycG, 1));
    1377             :   /* gG generators of G, gGH generators of G/H: gGH = g.Ui, g = gGH.U */
    1378         329 :   cycGH = ZM_snf_group(hnfmodid(P, cycG), &U, &Ui);
    1379         329 :   ncycGH = cyc_normalize(cycGH);
    1380         329 :   gG = ZV_to_Flv(znstar_get_gen(G), f); /* gens. of G */
    1381             :   /* generators of G/H */
    1382         329 :   gGH = Flv_FlvV_factorback(gG, ZM_to_Flm(Ui, expG), f);
    1383         329 :   vChar = chargalois(cycGH, NULL); n_f = lg(vChar)-2;
    1384         329 :   vH1 = cgetg(n_f+1, t_VEC);
    1385       19124 :   for (i = 1; i <= n_f; i++)
    1386             :   { /* skip trivial character */
    1387       18795 :     GEN chi = gel(vChar,i+1), nchi = char_normalize(chi, ncycGH);
    1388       18795 :     GEN chiG, E, H1, C = gel(nchi, 2);
    1389       18795 :     long e, he, gen, d = itou(gel(nchi, 1));
    1390             :     /* chi(prod g[i]^e[i]) = e(sum e[i]*C[i] / d), chi has order d = #(G/H1)*/
    1391       18795 :     E = Flv_extgcd(C, d); /* \sum C[i]*E[i] = 1 in Z/dZ */
    1392             : 
    1393       18795 :     chiG = zncharlift(chi, ncycGH, U, cycG);
    1394       18795 :     H1 =  charker(cycG, chiG); /* H1 < G with G/H1 cyclic */
    1395       18795 :     e = itou( zncharconductor(G, chiG) ); /* cond H1 = cond chi */
    1396       18795 :     H1 = Flv_FlvV_factorback(zv_to_Flv(gG, e), ZM_to_Flm(H1, expG), e);
    1397       18795 :     gen = Flv_factorback(zv_to_Flv(gGH, e), E, e);
    1398       18795 :     H1 = znstar_generate(e, H1); /* G/H1 = <gen>, chi(gen) = e(1/d) */
    1399       18795 :     he = eulerphiu(e) / d;
    1400             :     /* G/H1 = <gen> cyclic of index d, e = cond(H1) */
    1401       18795 :     gel(vH1,i) = mkvec3(H1, mkvecsmall5(d,e,he,srh_1(H1), gen),
    1402             :                         subgp2ary(H1, he));
    1403             :   }
    1404         329 :   if (pcycGH) *pcycGH = cycGH;
    1405         329 :   return vH1;
    1406             : }
    1407             : 
    1408             : /* aH=g^iH ==> chi(a)=zeta_n^(-i); Chi[g^iH]=i; Chi[0] is never accessed */
    1409             : static GEN
    1410       13419 : get_chi(GEN H1)
    1411             : {
    1412       13419 :   GEN H = _get_H(H1);
    1413       13419 :   long i, j, gi, d = _get_d(H1), f = _get_f(H1), h = _get_h(H1), g = _get_g(H1);
    1414       13419 :   GEN Chi = const_vecsmall(f-1, -1);
    1415             : 
    1416     5584159 :   for (j=1; j<=h; j++) Chi[H[j]] = 0; /* i = 0 */
    1417      396592 :   for (i = 1, gi = g; i < d; i++)
    1418             :   {
    1419    40492081 :     for (j=1; j<=h; j++) Chi[Fl_mul(gi, H[j], f)] = i;
    1420      383173 :     gi = Fl_mul(gi, g, f);
    1421             :   }
    1422       13419 :   return Chi;
    1423             : }
    1424             : 
    1425             : static void
    1426          14 : errpdiv(const char *f, GEN p, long d)
    1427             : {
    1428          14 :   pari_err_DOMAIN(f, "p", "divides",
    1429          14 :                   strtoGENstr(stack_sprintf("[F:Q] = %ld", d)), p);
    1430           0 : }
    1431             : /* p odd doesn't divide degF; return lambda invariant if n==0 and
    1432             :  * iwasawa polynomials if n>=1 */
    1433             : static GEN
    1434          49 : abeliwasawa(long p, long f, GEN HH, long degF, long n)
    1435             : {
    1436          49 :   long lam = 0, i, n_f;
    1437          49 :   GEN vH1, vData, z = nullvec(), p0 = utoi(p) ;
    1438             : 
    1439          49 :   vH1 = GHinit(f, HH, NULL); n_f = lg(vH1)-1;
    1440          49 :   vData = const_vec(degF, NULL);
    1441        6608 :   for (i=1; i<=n_f; i++) /* prescan. set Teichmuller */
    1442             :   {
    1443        6573 :     GEN H1 = gel(vH1,i);
    1444        6573 :     long d_K = _get_d(H1), f_K = _get_f(H1), g_K = _get_g(H1);
    1445             : 
    1446        6573 :     if (f_K == d_K+1 && p == f_K)  /* found K=Q(zeta_p) */
    1447             :     {
    1448          14 :       long d_chi = 1, n_conj = eulerphiu(d_K);
    1449          14 :       GEN C = set_C(p, d_K, d_chi, n_conj);
    1450          14 :       long minpow = n? 2*n+1: 2;
    1451          14 :       GEN MinPol = set_minpol_teich(g_K, p0, minpow);
    1452          14 :       gel(vData, d_K) = mkvec4(MinPol, C, NULL, mkvecsmall2(d_chi, n_conj));
    1453          14 :       break;
    1454             :     }
    1455             :   }
    1456             : 
    1457        6664 :   for (i=1; i<=n_f; i++)
    1458             :   {
    1459        6615 :     GEN H1 = gel(vH1,i), z1, Chi, K;
    1460        6615 :     long d_K = _get_d(H1), s = _get_s(H1);
    1461             : 
    1462        6615 :     if (s) continue;  /* F is real */
    1463             : #ifdef DEBUG
    1464             :     err_printf("  handling %s cyclic subfield K, deg(K)=%ld, cond(K)=%ld\n",
    1465             :         s? "a real": "an imaginary", d_K, _get_f(H1));
    1466             : #endif
    1467        3318 :     if (!gel(vData, d_K))
    1468             :     {
    1469         126 :       long d_chi = order_f_x(d_K, p), n_conj = eulerphiu(d_K)/d_chi;
    1470         126 :       GEN C = set_C(p, d_K, d_chi, n_conj);
    1471         126 :       long minpow = n? n+1: 2;
    1472         126 :       GEN MinPol = set_minpol(d_K, p0, minpow, n_conj);
    1473         126 :       gel(vData, d_K) = mkvec4(MinPol, C, NULL, mkvecsmall2(d_chi, n_conj));
    1474             :     }
    1475        3318 :     Chi = get_chi(H1);
    1476        3318 :     K = shallowconcat(mkvec2(H1, Chi), gel(vData, d_K));
    1477        3318 :     if (n==0) lam += imag_cyc_lam(K, p);
    1478          56 :     else if (lg(z1 = imag_cyc_pol(K, p, n)) > 1) z = shallowconcat(z, z1);
    1479             :   }
    1480          49 :   return n? z: mkvecs(lam);
    1481             : }
    1482             : 
    1483             : static GEN
    1484          77 : ary2mat(GEN x, long n)
    1485             : {
    1486             :   long i, j;
    1487          77 :   GEN z = cgetg(n+1,t_MAT);
    1488         182 :   for (i=1; i<=n; i++)
    1489             :   {
    1490         105 :     gel(z,i) = cgetg(n+1,t_COL);
    1491         280 :     for (j=1; j<=n; j++) gmael(z, i, j) = utoi(x[(i-1)*n+j-1]);
    1492             :   }
    1493          77 :   return z;
    1494             : }
    1495             : 
    1496             : static long
    1497           0 : is_cyclic(GEN x)
    1498             : {
    1499           0 :   GEN y = gel(x, 2);
    1500           0 :   long i, l = lg(y), n = 0;
    1501           0 :   for (i = 1; i < l; i++) if (signe(gel(y,i))) n++;
    1502           0 :   return n <= 1;
    1503             : }
    1504             : 
    1505             : static GEN
    1506          77 : make_p_part(GEN y, ulong p, long d_pow)
    1507             : {
    1508          77 :   long i, l = lg(y);
    1509          77 :   GEN z = cgetg(l, t_VECSMALL);
    1510         182 :   for (i = 1; i < l; i++) z[i] = signe(gel(y,i))? Z_lval(gel(y,i), p): d_pow;
    1511          77 :   return z;
    1512             : }
    1513             : 
    1514             : static GEN
    1515          77 : structure_MLL(GEN y, long d_pow)
    1516             : {
    1517          77 :   long y0, i, l = lg(y);
    1518          77 :   GEN x = gen_0, E = cgetg(l, t_VEC);
    1519         182 :   for (i = 1; i < l; i++)
    1520             :   {
    1521         105 :     if ((y0 = d_pow-y[i]) < 0) y0 = 0;
    1522         105 :     x = addiu(x, y0);
    1523         105 :     gel(E, l-i) = utoi(y0);
    1524             :   }
    1525          77 :   return mkvec2(x, E);
    1526             : }
    1527             : 
    1528             : static long
    1529          14 : find_del_el(GEN *oldgr, GEN newgr, long n, long n_el, long d_chi)
    1530             : {
    1531          14 :   if (n_el==1) return 1;
    1532          14 :   if (equalis(gmael(newgr, 2, 1), n_el)) return n;
    1533          14 :   if (cmpii(gel(*oldgr, 1), gel(newgr, 1)) >= 0) return n;
    1534          14 :   if (n > 1 && is_cyclic(newgr)) { *oldgr = newgr; return n-1; }
    1535          14 :   if (n == n_el) return n;
    1536          14 :   if (cmpis(gel(newgr, 1), n*d_chi) < 0) return n;
    1537          14 :   return 0;
    1538             : }
    1539             : 
    1540             : static GEN
    1541           7 : subgr2vecsmall(GEN H, long h, long f)
    1542             : {
    1543             :   long i;
    1544           7 :   GEN z = const_vecsmall(f-1, 0); /* f=lg(z) */
    1545        2023 :   for (i=1; i<=h; i++) z[H[i]] = 1; /* H[i]!=0 */
    1546           7 :   return z;
    1547             : }
    1548             : 
    1549             : /* K is the subfield of Q(zeta_f) with degree d corresponding to the subgroup
    1550             :  * H in (Z/fZ)^*; for a divisor e of f, zeta_e \in K <=> H \subset He. */
    1551             : static long
    1552         119 : root_of_1(long f, GEN H)
    1553             : {
    1554         119 :   GEN g = gel(H, 1); /* generators */
    1555         119 :   long e = f, i, l = lg(g);
    1556         119 :   for (i = 1; i < l; i++)
    1557             :   {
    1558          98 :     e = cgcd(e, g[i] - 1);
    1559          98 :     if (e <= 2) return 2;
    1560             :   }
    1561          21 :   return odd(e)? (e<<1): e;
    1562             : }
    1563             : 
    1564             : static long
    1565         259 : find_ele(GEN H)
    1566             : {
    1567         259 :   long i, f=lg(H);
    1568      369852 :   for (i=1; i<f; i++) if (H[i]) return i;
    1569           7 :   return 0;
    1570             : }
    1571             : 
    1572             : static void
    1573         252 : delete_ele(GEN H, long j, long el)
    1574             : {
    1575         252 :   long f = lg(H), x = 1;
    1576        2016 :   do H[Fl_mul(j,x,f)] = 0;
    1577        2016 :   while ((x=Fl_mul(x,el,f))!=1);
    1578         252 : }
    1579             : 
    1580             : static GEN
    1581           7 : get_coset(GEN H, long h, long f, long el)
    1582             : {
    1583           7 :   long i, j, k = h/order_f_x(f, el);
    1584           7 :   GEN H2, coset = const_vecsmall(k, 0);
    1585           7 :   H2 = subgr2vecsmall(H, h, f);
    1586         259 :   for (i=0; (j=find_ele(H2))>0; i++)
    1587             :   {
    1588         252 :     coset[1+i] = j;
    1589         252 :     delete_ele(H2, j, el);
    1590             :   }
    1591           7 :   if (i != k) pari_err_BUG("failed to find coset\n");
    1592           7 :   return coset;
    1593             : }
    1594             : 
    1595             : static long
    1596        3024 : srh_pol(GEN xpows, GEN vn, GEN pols, long el, long k, long f)
    1597             : {
    1598        3024 :   pari_sp av = avma;
    1599        3024 :   long i, j, l = lg(pols), d = degpol(gel(pols, 1));
    1600        3024 :   GEN pol = gel(pols, 1);
    1601             : 
    1602      654696 :   for (i=1; i<l; i++)
    1603             :   {
    1604             :     GEN x, y, z;
    1605      654696 :     if (vn[i]==0) continue;
    1606      331170 :     y = gel(pols, vn[i]);
    1607      331170 :     z = pol0_Flx(0);
    1608     3311700 :     for (j=0; j<=d; j++)
    1609     2980530 :       z = Flx_add(z, Flx_Fl_mul(gel(xpows, 1+Fl_mul(j, k, f)), y[2+j], el), el);
    1610      331170 :     x = Flx_rem(z, pol, el);
    1611      331170 :     if (lg(x)==2)
    1612        3024 :     {vn[i] = 0; return gc_long(av, i); }  /* pols[i] is min pol of zeta_f^k */
    1613             :   }
    1614           0 :   pari_err_BUG("subcyclopclgp [minimal polinomial]");
    1615           0 :   return 0;  /* to suppress warning */
    1616             : }
    1617             : 
    1618             : /* e_chi[i mod dK] = chi(i*j), i = 0..dK-1; beware: e_chi is translated ! */
    1619             : static GEN
    1620       35857 : get_e_chi(GEN K, ulong j, ulong d, ulong *pdK)
    1621             : {
    1622       35857 :   ulong i, dK = K_get_d(K);
    1623       35857 :   GEN TR = gel(K,4) + 2, e_chi = cgetg(dK+1, t_VECSMALL) + 1;
    1624       35857 :   if (j == 1)
    1625      286902 :     for (i = 0; i < dK; i++) e_chi[i] = umodiu(gel(TR, i), d);
    1626             :   else
    1627      983345 :     for (i = 0; i < dK; i++) e_chi[i] = umodiu(gel(TR, Fl_mul(i, j, dK)), d);
    1628       35857 :   *pdK = dK; return e_chi;
    1629             : }
    1630             : static GEN
    1631        5145 : get_e_chi_ll(GEN K, ulong j, GEN d)
    1632             : {
    1633        5145 :   ulong i, dK = umael3(K, 1, 2, 1);
    1634        5145 :   GEN TR = gel(K,4) + 2, e_chi = cgetg(dK+1, t_VEC) + 1;
    1635      246785 :   for (i = 0; i < dK; i++) gel(e_chi,i) = modii(gel(TR, Fl_mul(i, j, dK)), d);
    1636        5145 :   return e_chi;
    1637             : }
    1638             : 
    1639             : /* el=1 (mod f) */
    1640             : static long
    1641           0 : chk_el_real_f(GEN K, ulong p, ulong d_pow, ulong el, ulong j0)
    1642             : {
    1643           0 :   pari_sp av = avma;
    1644           0 :   GEN H = K_get_H(K);
    1645           0 :   ulong d_K, f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    1646           0 :   ulong  i, j, gi, d = upowuu(p, d_pow), dp = d*p;
    1647           0 :   ulong g_el, z_f, flag = 0, el1f = (el-1)/f, el1dp = (el-1)/dp;
    1648           0 :   GEN e_chi = get_e_chi(K, j0, dp, &d_K);
    1649           0 :   GEN vz_f, xi_el = cgetg(d_K+1, t_VECSMALL)+1;
    1650             : 
    1651           0 :   g_el = pgener_Fl(el);
    1652           0 :   z_f = Fl_powu(g_el, el1f, el);
    1653           0 :   vz_f = Fl_powers(z_f, f-1, el)+1;
    1654             : 
    1655           0 :   for (gi = 1, i = 0; i < d_K; i++)
    1656             :   {
    1657           0 :     ulong x = 1;
    1658           0 :     for (j = 1; j <= h; j++)
    1659             :     {
    1660           0 :       ulong y = Fl_mul(H[j], gi, f);
    1661           0 :       x = Fl_mul(x, vz_f[y]-1, el); /* vz_f[y] = z_f^y  */
    1662             :     }
    1663           0 :     gi = Fl_mul(gi, g_K, f);
    1664           0 :     xi_el[i] = x;  /* xi_el[i] = xi^{g_K^i} mod el */
    1665             :   }
    1666           0 :   for (i=0; i<d_K; i++)
    1667             :   {
    1668           0 :     ulong x = 1;
    1669           0 :     for (j=0; j<d_K; j++)
    1670           0 :       x = Fl_mul(x, Fl_powu(xi_el[j], e_chi[(i+j)%d_K], el), el);
    1671           0 :     if ((x = Fl_powu(x, el1dp, el))!=1) flag = 1;
    1672           0 :     if (Fl_powu(x, p, el)!=1) return gc_long(av,0);
    1673             :   }
    1674           0 :   return gc_long(av, flag?1:0);
    1675             : }
    1676             : 
    1677             : /* For a cyclic field K contained in Q(zeta_f),
    1678             :  * computes minimal polynomial T of theta=Tr_{Q(zeta_f)/K}(zeta_f) over Q
    1679             :  * and conjugates of theta */
    1680             : static GEN
    1681          14 : minpol_theta(GEN K)
    1682             : {
    1683          14 :   GEN HH = gmael3(K,1,1,1);
    1684          14 :   return galoissubcyclo(utoi(K_get_f(K)), HH, 0, 0);
    1685             : }
    1686             : 
    1687             : /*  xi[1+i] = i-th conj of xi = Tr_{Q(zeta_f)/K}(1-zeta_f).
    1688             :  * |1-(cos(x)+i*sin(x))|^2 = 2(1-cos(x)) */
    1689             : static GEN
    1690           0 : xi_approx(GEN K, long prec)
    1691             : {
    1692           0 :   pari_sp av = avma;
    1693           0 :   GEN H = K_get_H(K);
    1694           0 :   long d_K = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    1695           0 :   GEN xi = cgetg(d_K+1, t_COL), vz_f = grootsof1(f, prec);
    1696           0 :   long i, j, g = 1, h2 = h>>1;
    1697           0 :   for (i=1; i<=d_K; i++)
    1698             :   {
    1699           0 :     GEN y = real_1(prec);
    1700           0 :     for (j=1; j<=h2; j++)
    1701             :     {
    1702           0 :       GEN z = gmael(vz_f, 1+Fl_mul(H[j], g, f), 1);
    1703           0 :       y = mulrr(y, shiftr(subsr(1, z), 1));
    1704             :     }
    1705           0 :     gel(xi, i) = y;
    1706           0 :     g = Fl_mul(g, g_K, f);
    1707             :   }
    1708           0 :   return gerepilecopy(av, xi);
    1709             : }
    1710             : 
    1711             : static GEN
    1712          47 : theta_xi_el(GEN K, ulong el)
    1713             : {
    1714          47 :   pari_sp av = avma;
    1715          47 :   GEN H = K_get_H(K);
    1716          47 :   ulong d_K = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    1717          47 :   GEN theta = cgetg(d_K+1, t_VECSMALL), xi = cgetg(d_K+1, t_VECSMALL), vz_f;
    1718          47 :   ulong i, j, g = 1, x, y, g_el, z_f;
    1719             : 
    1720          47 :   g_el = pgener_Fl(el);
    1721          47 :   z_f = Fl_powu(g_el, (el-1)/f, el);
    1722          47 :   vz_f = Fl_powers(z_f, f-1, el);
    1723        1109 :   for (i=1; i<=d_K; i++)
    1724             :   {
    1725        1062 :     x = 0; y = 1;
    1726       28254 :     for (j=1; j<=h; j++)
    1727             :     {
    1728       27192 :       ulong z = vz_f[1+Fl_mul(H[j], g, f)];
    1729       27192 :       x = Fl_add(x, z, el);
    1730       27192 :       y = Fl_mul(y, z-1, el);
    1731             :     }
    1732        1062 :     theta[i] = x;
    1733        1062 :     xi[i] = y;
    1734        1062 :     g = Fl_mul(g, g_K, f);
    1735             :   }
    1736          47 :   return gerepilecopy(av, mkvec2(theta, xi));
    1737             : }
    1738             : 
    1739             : static GEN
    1740          47 : make_Xi(GEN xi, long d)
    1741             : {
    1742             :   long i, j;
    1743          47 :   GEN Xi = cgetg(d+1, t_MAT);
    1744        1109 :   for (j=0; j<d; j++)
    1745             :   {
    1746        1062 :     GEN x = cgetg(d+1, t_VECSMALL);
    1747        1062 :     gel(Xi, 1+j) = x;
    1748       27714 :     for (i=0; i<d; i++) x[1+i] = xi[1+(i+j)%d];
    1749             :   }
    1750          47 :   return Xi;
    1751             : }
    1752             : 
    1753             : static GEN
    1754          47 : make_Theta(GEN theta, ulong d, ulong el)
    1755             : {
    1756             :   ulong i;
    1757          47 :   GEN Theta = cgetg(d+1, t_MAT);
    1758        1109 :   for (i=1; i<=d; i++) gel(Theta, i) = Fl_powers(theta[i], d-1, el);
    1759          47 :   return Flm_inv(Theta, el);
    1760             : }
    1761             : 
    1762             : static GEN
    1763          47 : Xi_el(GEN K, GEN tInvA, ulong el)
    1764             : {
    1765          47 :   pari_sp av = avma;
    1766          47 :   ulong d_K = K_get_d(K);
    1767          47 :   GEN tx = theta_xi_el(K, el), Theta, Xi, X;
    1768             : 
    1769          47 :   if ((Theta = make_Theta(gel(tx, 1), d_K, el))==NULL) return NULL;
    1770          47 :   Xi = make_Xi(gel(tx, 2), d_K);
    1771          47 :   X = Flm_mul(Flm_mul(Xi, Theta, el), ZM_to_Flm(tInvA, el), el);
    1772          47 :   return gerepilecopy(av, X);
    1773             : }
    1774             : 
    1775             : static GEN
    1776           0 : pol_xi_el(GEN K, ulong el)
    1777             : {
    1778           0 :   pari_sp av = avma;
    1779           0 :   ulong d_K = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    1780           0 :   GEN H = K_get_H(K), xi = cgetg(d_K+1, t_VECSMALL), vz_f;
    1781           0 :   ulong i, j, g = 1, y, g_el, z_f;
    1782             : 
    1783           0 :   g_el = pgener_Fl(el);
    1784           0 :   z_f = Fl_powu(g_el, (el-1)/f, el);
    1785           0 :   vz_f = Fl_powers(z_f, f-1, el);
    1786           0 :   for (i=1; i<=d_K; i++)
    1787             :   {
    1788           0 :     y = 1;
    1789           0 :     for (j=1; j<=h; j++)
    1790             :     {
    1791           0 :       ulong z = vz_f[1+Fl_mul(H[j], g, f)];
    1792           0 :       y = Fl_mul(y, z-1, el);
    1793             :     }
    1794           0 :     xi[i] = y;
    1795           0 :     g = Fl_mul(g, g_K, f);
    1796             :   }
    1797           0 :   return gerepilecopy(av, Flv_roots_to_pol(xi, el, 0));
    1798             : }
    1799             : 
    1800             : /* theta[1+i] = i-th conj of theta; xi[1+i] = i-th conj of xi. */
    1801             : static GEN
    1802          14 : theta_xi_approx(GEN K, long prec)
    1803             : {
    1804          14 :   pari_sp av = avma;
    1805          14 :   GEN H = K_get_H(K);
    1806          14 :   long d_K = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    1807          14 :   GEN theta = cgetg(d_K+1, t_VEC), xi = cgetg(d_K+1, t_VEC);
    1808          14 :   GEN vz_f = grootsof1(f, prec);
    1809          14 :   long i, j, g = 1, h2 = h>>1;
    1810             : 
    1811         238 :   for (i=1; i<=d_K; i++)
    1812             :   {
    1813         224 :     GEN x = real_0(prec), y = real_1(prec);
    1814        5068 :     for (j=1; j<=h2; j++)
    1815             :     {
    1816        4844 :       GEN z = gmael(vz_f, 1+Fl_mul(H[j], g, f), 1);
    1817        4844 :       x = addrr(x, z);
    1818        4844 :       y = mulrr(y, shiftr(subsr(1, z), 1));
    1819             :     }
    1820         224 :     gel(theta, i) = shiftr(x, 1);
    1821         224 :     gel(xi, i) = y;
    1822         224 :     g = Fl_mul(g, g_K, f);
    1823             :   }
    1824          14 :   return gerepilecopy(av, mkvec2(theta, xi));
    1825             : }
    1826             : 
    1827             : static GEN
    1828          14 : bound_coeff_xi(GEN K, GEN tInvA)
    1829             : {
    1830          14 :   pari_sp av = avma;
    1831          14 :   long d_K = K_get_d(K), prec = MEDDEFAULTPREC, i;
    1832          14 :   GEN tInvV, R = cgetg(d_K+1, t_MAT), theta_xi = theta_xi_approx(K, prec);
    1833          14 :   GEN theta = gel(theta_xi, 1), xi = gel(theta_xi, 2), x1, x2, bound;
    1834             : 
    1835         238 :   for (i=1; i<=d_K; i++)
    1836             :   {
    1837         224 :     GEN z = gpowers(gel(theta, i), d_K-1);
    1838         224 :     settyp(z, t_COL);
    1839         224 :     gel(R, i) = z;
    1840             :   }
    1841          14 :   tInvV = RgM_mul(RgM_inv(R), tInvA);
    1842          14 :   x1 = gsupnorm(tInvV, prec); x2 = gsupnorm(xi, prec);
    1843          14 :   bound = mulrs(mulrr(x1, x2), 3*d_K);
    1844          14 :   return gerepilecopy(av, bound);
    1845             : }
    1846             : 
    1847             : static GEN
    1848          14 : get_Xi(GEN K, GEN tInvA)
    1849             : {
    1850          14 :   pari_sp av = avma;
    1851             :   GEN M0, XI, EL, Xi;
    1852          14 :   ulong f = K_get_f(K), el, e, n, i;
    1853             :   forprime_t T0;
    1854             : 
    1855          14 :   M0 = bound_coeff_xi(K, tInvA);
    1856          14 :   e = expo(M0)+1; n = e/(BITS_IN_LONG-1); n++;
    1857          14 :   EL = cgetg(1+n, t_VECSMALL);
    1858          14 :   XI = cgetg(1+n, t_VEC);
    1859          14 :   u_forprime_arith_init(&T0, LONG_MAX, ULONG_MAX, 1, f);
    1860             : 
    1861          61 :   for (i=1; i<=n; i++)
    1862             :   {
    1863          47 :     el = u_forprime_next(&T0);
    1864          47 :     while ((Xi=Xi_el(K, tInvA, el))==NULL) el = u_forprime_next(&T0);
    1865          47 :     gel(XI, i) = Xi;
    1866          47 :     EL[i] = el;
    1867             :   }
    1868          14 :   return gerepileupto(av, nmV_chinese_center(XI, EL, NULL));
    1869             : }
    1870             : 
    1871             : /* K is a cyclic field of conductor f with degree d=d_K
    1872             :  * xi = Norm_{Q(zeta_f)/K}(1-zeta_f)
    1873             :  * 1: T, min poly of a=Tr_{Q(zeta_f)/K}(zeta_f) over Q
    1874             :  * 2: B, power basis of K with respect to a
    1875             :  * 3: A, rational matrix s.t. t(v_1,...v_d) = A*t(1,a,...,a^{d-1})
    1876             :  * 4: Xi, integer matrix s.t. t(xi^(1),...,xi^(d)) = Xi*t(v_1,...,v_d) */
    1877             : static GEN
    1878          14 : xi_data_basis(GEN K)
    1879             : {
    1880          14 :   pari_sp av = avma;
    1881          14 :   GEN T = minpol_theta(K);
    1882             :   GEN InvA, A, M, Xi, A_den;
    1883          14 :   GEN D, B = nfbasis(T, &D);
    1884             :   pari_timer ti;
    1885          14 :   if (DEBUGLEVEL>1) timer_start(&ti);
    1886          14 :   A = RgXV_to_RgM(B, lg(B)-1);
    1887          14 :   M = gmael(A, 1, 1);
    1888          14 :   if (!equali1(M)) A = RgM_Rg_div(A, M);
    1889          14 :   InvA = QM_inv(A);
    1890          14 :   A = Q_remove_denom(A, &A_den);
    1891          14 :   if (A_den==NULL) A_den = gen_1;
    1892          14 :   Xi = get_Xi(K, shallowtrans(InvA));
    1893          14 :   if (DEBUGLEVEL>1) timer_printf(&ti, "xi_data_basis");
    1894          14 :   return gerepilecopy(av, mkvec5(T, B, shallowtrans(A), Xi, A_den));
    1895             : }
    1896             : 
    1897             : /* When factorization of polcyclo mod el is difficult, one can try to
    1898             :  * check the condition of el using an integral basis of K.
    1899             :  * This is useful when d_K is small. */
    1900             : static long
    1901          14 : chk_el_real_basis(GEN K, long p, long d_pow, long el, long j0)
    1902             : {
    1903          14 :   pari_sp av = avma;
    1904          14 :   GEN xi = gel(K, 7), T = gel(xi, 1), A = gel(xi, 3), Xi = gel(xi, 4);
    1905          14 :   GEN A_den = gel(xi, 5);
    1906          14 :   ulong i, j, x, found = 0;
    1907             :   GEN v_el, xi_el;
    1908             :   GEN e_chi, xi_e_chi;
    1909             :   ulong d_K, d, dp, el1dp;
    1910             : 
    1911          14 :   if (dvdiu(A_den, el)) return 0;
    1912             : 
    1913          14 :   d = upowuu(p, d_pow); dp = d*p; el1dp = (el-1)/dp;
    1914          14 :   e_chi = get_e_chi(K, j0, dp, &d_K);
    1915          14 :   xi_e_chi = cgetg(d_K+1, t_VECSMALL)+1;
    1916             : 
    1917          14 :   if (DEBUGLEVEL>1) err_printf("chk_el_real_basis: d_K=%ld el=%ld\n",d_K,el);
    1918          14 :   A = ZM_to_Flm(A, el);
    1919          14 :   A = Flm_Fl_mul(A, Fl_inv(umodiu(A_den, el), el), el);
    1920          14 :   x = Flx_oneroot_split(ZX_to_Flx(T, el), el);
    1921          14 :   v_el = Flm_Flc_mul(A, Fl_powers(x, d_K-1, el), el);
    1922          14 :   xi_el = Flm_Flc_mul(ZM_to_Flm(Xi, el), v_el, el);
    1923          14 :   if (DEBUGLEVEL>2) err_printf("el=%ld xi_el=%Ps\n", el, xi_el);
    1924         238 :   for (i=0; i<d_K; i++)
    1925             :   {
    1926         224 :     long z = 1;
    1927        5208 :     for (j=0; j<d_K; j++)
    1928        4984 :       z = Fl_mul(z, Fl_powu(xi_el[1+j], e_chi[(i+j)%d_K], el), el);
    1929         224 :     xi_e_chi[i] = z;
    1930             :   }
    1931          14 :   if (DEBUGLEVEL>2) err_printf("xi_e_chi=%Ps\n", xi_e_chi-1);
    1932         238 :   for (i=0; i<d_K; i++)
    1933             :   {
    1934         224 :     long x = Fl_powu(xi_e_chi[i], el1dp, el);
    1935         224 :     if (x!=1) found = 1;
    1936         224 :     if (Fl_powu(x, p, el)!=1) return gc_long(av, 0);
    1937             :   }
    1938          14 :   return gc_long(av, found);
    1939             : }
    1940             : 
    1941             : static GEN
    1942           0 : bound_pol_xi(GEN K)
    1943             : {
    1944           0 :   pari_sp av = avma;
    1945           0 :   GEN xi = xi_approx(K, MEDDEFAULTPREC);
    1946           0 :   GEN M = real_1(MEDDEFAULTPREC), one = rtor(dbltor(1.0001), MEDDEFAULTPREC);
    1947           0 :   long i, n = lg(xi);
    1948             : 
    1949           0 :   for (i=1; i<n; i++) M = mulrr(M, addrr(one, gel(xi, i)));
    1950           0 :   M = mulrs(M, 3);
    1951           0 :   return gerepilecopy(av, M);
    1952             : }
    1953             : 
    1954             : static GEN
    1955           0 : minpol_xi(GEN K)
    1956             : {
    1957           0 :   pari_sp av = avma;
    1958             :   GEN M0, POL, EL;
    1959           0 :   ulong f = K_get_f(K), el, e, n, i;
    1960             :   forprime_t T0;
    1961             : 
    1962           0 :   M0 = bound_pol_xi(K);
    1963           0 :   e = expo(M0)+1; n = e/(BITS_IN_LONG-1); n++;
    1964           0 :   EL = cgetg(1+n, t_VECSMALL);
    1965           0 :   POL = cgetg(1+n, t_VEC);
    1966           0 :   u_forprime_arith_init(&T0, LONG_MAX, ULONG_MAX, 1, f);
    1967           0 :   for (i=1; i<=n; i++)
    1968             :   {
    1969           0 :     el = u_forprime_next(&T0);
    1970           0 :     gel(POL, i) = pol_xi_el(K, el);
    1971           0 :     EL[i] = el;
    1972             :   }
    1973           0 :   return gerepileupto(av, nxV_chinese_center(POL, EL, NULL));
    1974             : }
    1975             : 
    1976             : static long
    1977           0 : find_conj_el(GEN K, GEN pol, GEN Den)
    1978             : {
    1979           0 :   pari_sp av = avma;
    1980           0 :   GEN H = K_get_H(K);
    1981           0 :   ulong d_K = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g = K_get_g(K);
    1982           0 :   ulong j, k, el, g_el, z_f, xi = 1, xi_g = 1;
    1983           0 :   GEN T = NULL, vz_f;
    1984             : 
    1985           0 :   for (el=f+1; el; el+=f)
    1986           0 :     if (uisprime(el) && dvdiu(Den, el)==0)
    1987             :     {
    1988           0 :       T = ZX_to_Flx(pol, el);
    1989           0 :       T = Flx_Fl_mul(T, Fl_inv(umodiu(Den, el), el), el);
    1990           0 :       break;
    1991             :     }
    1992           0 :   g_el = pgener_Fl(el);
    1993           0 :   z_f = Fl_powu(g_el, (el-1)/f, el);
    1994           0 :   vz_f = Fl_powers(z_f, f-1, el);
    1995           0 :   for (j=1; j<=h; j++)
    1996           0 :     xi = Fl_mul(xi, vz_f[1+H[j]]-1, el);
    1997           0 :   for (j=1; j<=h; j++)
    1998           0 :     xi_g = Fl_mul(xi_g, vz_f[1+Fl_mul(H[j], g, f)]-1, el);
    1999           0 :   for (k=1; k<=d_K; k++)
    2000             :   {
    2001           0 :     xi = Flx_eval(T, xi, el);
    2002           0 :     if (xi == xi_g) break;
    2003             :   }
    2004           0 :   if (xi != xi_g) pari_err_BUG("find_conj_el");
    2005           0 :   return gc_long(av, k);
    2006             : }
    2007             : 
    2008             : /* G = H_1*H_2*...*H_m is cyclic of order n, H_i=<perms[i]>
    2009             :  * G is not necessarily a direct product.
    2010             :  * If p^e || n, then p^e || |H_i| for some i.
    2011             :  * return a generator of G. */
    2012             : static GEN
    2013           0 : find_gen(GEN perms, long n)
    2014             : {
    2015           0 :   GEN fa = factoru(n), P = gel(fa, 1), E = gel(fa, 2);
    2016           0 :   long i, j, l = lg(perms), r = lg(P);
    2017           0 :   GEN gen = cgetg(r, t_VEC), orders = cgetg(l, t_VECSMALL), perm;
    2018             : 
    2019           0 :   for (i=1; i<l; i++) orders[i] = perm_orderu(gel(perms, i));
    2020           0 :   for (i=1; i<r; i++)
    2021             :   {
    2022           0 :     long pe = upowuu(P[i], E[i]);
    2023           0 :     for (j=1; j<l; j++) if (orders[j]%pe==0) break;
    2024           0 :     gel(gen, i) = perm_powu(gel(perms, j), orders[j]/pe);
    2025             :   }
    2026           0 :   perm = gel(gen, 1);
    2027           0 :   for (i=2; i<l; i++) perm = perm_mul(perm, gel(gen, i));
    2028           0 :   return perm;
    2029             : }
    2030             : 
    2031             : /* R is the roots of T. R[1+i] = R[1]^(g_K^i), 0 <= i <= d_K-1
    2032             :  * 1: min poly T of xi over Q
    2033             :  * 2: F(x)\in Q[x] s.t. xi^(g_K)=F(xi) */
    2034             : static GEN
    2035           0 : xi_data_galois(GEN K)
    2036             : {
    2037           0 :   pari_sp av = avma;
    2038             :   GEN T, G, perms, perm, pol, pol2, Den;
    2039           0 :   ulong k, d_K = K_get_d(K);
    2040             :   pari_timer ti;
    2041             : 
    2042           0 :   if (DEBUGLEVEL>1) timer_start(&ti);
    2043           0 :   T = minpol_xi(K);
    2044           0 :   if (DEBUGLEVEL>1) timer_printf(&ti, "minpol_xi");
    2045           0 :   G = galoisinit(T, NULL);
    2046           0 :   if (DEBUGLEVEL>1) timer_printf(&ti, "galoisinit");
    2047           0 :   perms = gal_get_gen(G);
    2048           0 :   perm = (lg(perms)==2)?gel(perms, 1):find_gen(perms, d_K);
    2049           0 :   if (DEBUGLEVEL>1) timer_start(&ti);
    2050           0 :   pol = galoispermtopol(G, perm);
    2051           0 :   if (DEBUGLEVEL>1) timer_printf(&ti, "galoispermtopol");
    2052           0 :   pol = Q_remove_denom(pol, &Den);
    2053           0 :   if (Den==NULL) Den = gen_1;
    2054           0 :   k = find_conj_el(K, pol, Den);
    2055           0 :   if (DEBUGLEVEL>1) timer_printf(&ti,"find_conj");
    2056           0 :   pol2 = galoispermtopol(G, perm_powu(perm, k));
    2057           0 :   pol2 = Q_remove_denom(pol2, &Den);
    2058           0 :   if (Den==NULL) Den = gen_1;
    2059           0 :   return gerepilecopy(av, mkvec3(T, pol2, Den));
    2060             : }
    2061             : 
    2062             : /* If g(X)\in Q[X] s.t. g(xi)=xi^{g_K} was found,
    2063             :  * then we fix an integer x_0 s.t. xi=x_0 (mod el) and construct x_i
    2064             :  * s.t. xi^{g_K^i}=x_i (mod el) using g(X). */
    2065             : static long
    2066           0 : chk_el_real_galois(GEN K, long p, long d_pow, long el, long j0)
    2067             : {
    2068           0 :   pari_sp av = avma;
    2069           0 :   GEN xi = gel(K, 7), T = gel(xi, 1), F = gel(xi, 2), Den = gel(xi, 3);
    2070             :   GEN Fel, xi_el, xi_e_chi, e_chi;
    2071           0 :   ulong i, j, x, found = 0;
    2072             :   ulong d_K, d, dp, el1dp;
    2073             : 
    2074           0 :   if (dvdiu(Den, el)) return 0;
    2075             : 
    2076           0 :   d = upowuu(p, d_pow); dp = d*p; el1dp = (el-1)/dp;
    2077           0 :   e_chi = get_e_chi(K, j0, dp, &d_K);
    2078           0 :   xi_el = cgetg(d_K+1, t_VECSMALL)+1;
    2079           0 :   xi_e_chi = cgetg(d_K+1, t_VECSMALL)+1;
    2080             : 
    2081           0 :   if (DEBUGLEVEL>1) err_printf("chk_el_real_galois: d_K=%ld el=%ld\n",d_K,el);
    2082           0 :   Fel = ZX_to_Flx(F, el);
    2083           0 :   Fel = Flx_Fl_mul(Fel, Fl_inv(umodiu(Den, el), el), el);
    2084           0 :   x = Flx_oneroot_split(ZX_to_Flx(T, el), el);
    2085           0 :   for (i=0; i<d_K; i++) { xi_el[i] = x; x = Flx_eval(Fel, x, el); }
    2086           0 :   if (DEBUGLEVEL>2) err_printf("el=%ld xi_el=%Ps\n", el, xi_el-1);
    2087           0 :   for (i=0; i<d_K; i++)
    2088             :   {
    2089           0 :     long z = 1;
    2090           0 :     for (j=0; j<d_K; j++)
    2091           0 :       z = Fl_mul(z, Fl_powu(xi_el[j], e_chi[(i+j)%d_K], el), el);
    2092           0 :     xi_e_chi[i] = z;
    2093             :   }
    2094           0 :   if (DEBUGLEVEL>2) err_printf("xi_e_chi=%Ps\n", xi_e_chi-1);
    2095           0 :   for (i=0; i<d_K; i++)
    2096             :   {
    2097           0 :     long x = Fl_powu(xi_e_chi[i], el1dp, el);
    2098           0 :     if (x!=1) found = 1;
    2099           0 :     if (Fl_powu(x, p, el)!=1) return gc_long(av, 0);
    2100             :   }
    2101           0 :   return gc_long(av, found);
    2102             : }
    2103             : 
    2104             : /* checks the condition of el using the irreducible polynomial G_K(X) of zeta_f
    2105             :  * over K. G_K(X) mod el is enough for our purpose and it is obtained by
    2106             :  * factoring polcyclo(f) mod el */
    2107             : static long
    2108           7 : chk_el_real_factor(GEN K, long p, long d_pow, long el, long j0)
    2109             : {
    2110           7 :   pari_sp av = avma;
    2111           7 :   GEN H = K_get_H(K);
    2112           7 :   ulong f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    2113           7 :   ulong i, j, k, d_K, d = upowuu(p, d_pow), dp = d*p, found = 0;
    2114             :   GEN pols, coset, vn_g, polnum, xpows, G_K;
    2115           7 :   ulong el1dp = (el-1)/dp, n_coset, n_g, gi;
    2116           7 :   GEN e_chi = get_e_chi(K, j0, dp, &d_K);
    2117             :   pari_timer ti;
    2118             : 
    2119           7 :   if (DEBUGLEVEL>1) err_printf("chk_el_real_factor: f=%ld el=%ld\n",f,el);
    2120           7 :   coset = get_coset(H, h, f, el);
    2121           7 :   if (DEBUGLEVEL>1)
    2122             :   {
    2123           0 :     timer_start(&ti);
    2124           0 :     err_printf("factoring polyclo(d) (mod %ld)\n",f, el);
    2125             :   }
    2126           7 :   pols = Flx_factcyclo(f, el, 0);
    2127           7 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Flx_factcyclo(%lu,%lu)",f,el);
    2128           7 :   n_coset = lg(coset)-1;
    2129           7 :   n_g = lg(pols)-1;
    2130           7 :   vn_g = identity_perm(n_g);
    2131             : 
    2132           7 :   polnum = const_vec(d_K, NULL);
    2133          91 :   for (i=1; i<=d_K; i++) gel(polnum, i) = const_vecsmall(n_coset, 0);
    2134           7 :   xpows = Flxq_powers(polx_Flx(0), f-1, gel(pols, 1), el);
    2135          91 :   for (gi=1,i=1; i<=d_K; i++)
    2136             :   {
    2137        3108 :     for (j=1; j<=n_coset; j++)
    2138             :     {
    2139        3024 :       long x, conj = Fl_mul(gi, coset[j], f);
    2140        3024 :       x = srh_pol(xpows, vn_g, pols, el, conj, f);
    2141        3024 :       gel(polnum, i)[j] = x;
    2142             :     }
    2143          84 :     gi = Fl_mul(gi, g_K, f);
    2144             :   }
    2145           7 :   G_K = const_vec(d_K, NULL);
    2146          91 :   for (i=1; i<=d_K; i++)
    2147             :   {
    2148          84 :     GEN z = pol1_Flx(0);
    2149        3108 :     for (j=1; j<=n_coset; j++) z = Flx_mul(z, gel(pols, gel(polnum,i)[j]), el);
    2150          84 :     gel(G_K, i) = z;
    2151             :   }
    2152           7 :   if (DEBUGLEVEL>2) err_printf("G_K(x)=%Ps\n",Flx_to_ZX(gel(G_K, 1)));
    2153          91 :   for (k=0; k<d_K; k++)
    2154             :   {
    2155          84 :     long x = 1;
    2156        1092 :     for (i = 0; i < d_K; i++)
    2157             :     {
    2158             :       long x0, t;
    2159        1008 :       x0 = Flx_eval(gel(G_K, 1+i), 1, el);
    2160        1008 :       t = Fl_powu(x0, e_chi[(i+k)%d_K], el);
    2161        1008 :       x = Fl_mul(x, t, el);
    2162             :     }
    2163          84 :     x = Fl_powu(x, el1dp, el);
    2164          84 :     if (x!=1) found = 1;
    2165          84 :     if (Fl_powu(x, p, el)!=1) return gc_long(av, 0);
    2166             :   }
    2167           7 :   return gc_long(av, found);
    2168             : }
    2169             : 
    2170             : static long
    2171          21 : chk_el_real_chi(GEN K, ulong p, ulong d_pow, ulong el, ulong j0, long flag)
    2172             : {
    2173          21 :   ulong f = K_get_f(K);
    2174             : 
    2175          21 :   if (el%f == 1)
    2176           0 :     return chk_el_real_f(K, p, d_pow, el, j0); /* must be faster */
    2177          21 :   if (flag&USE_BASIS)
    2178          14 :     return chk_el_real_basis(K, p, d_pow, el, j0);
    2179           7 :   if (flag&USE_GALOIS_POL)
    2180           0 :     return chk_el_real_galois(K, p, d_pow, el, j0);
    2181           7 :   return chk_el_real_factor(K, p, d_pow, el, j0);
    2182             : }
    2183             : 
    2184             : static long
    2185         616 : chk_ell_real(GEN K, long d2, GEN ell, long j0)
    2186             : {
    2187         616 :   pari_sp av = avma;
    2188         616 :   GEN H = K_get_H(K);
    2189         616 :   ulong f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    2190             :   ulong d_K, i, j, gi;
    2191         616 :   GEN e_chi = get_e_chi(K, j0, d2, &d_K);
    2192         616 :   GEN g_ell, z_f, vz_f, xi_el = cgetg(d_K+1, t_VEC)+1;
    2193         616 :   GEN ell_1 = subiu(ell,1), ell1d2 = diviuexact(ell_1, d2);
    2194             : 
    2195         616 :   g_ell = pgener_Fp(ell);
    2196         616 :   z_f = Fp_pow(g_ell, diviuexact(subiu(ell, 1), f), ell);
    2197         616 :   vz_f = Fp_powers(z_f, f-1, ell)+1;
    2198       12964 :   for (gi=1, i=0; i<d_K; i++)
    2199             :   {
    2200       12348 :     GEN x = gen_1;
    2201     1074948 :     for (j = 1; j <= h; j++)
    2202             :     {
    2203     1062600 :       ulong y = Fl_mul(H[j], gi, f);
    2204     1062600 :       x = Fp_mul(x, subiu(gel(vz_f, y), 1), ell); /* vz_f[y] = z_f^y  */
    2205             :     }
    2206       12348 :     gi = Fl_mul(gi, g_K, f);
    2207       12348 :     gel(xi_el, i) = x;  /* xi_el[i]=xi^{g_K^i} */
    2208             :   }
    2209        1421 :   for (i=0; i<d_K; i++)
    2210             :   {
    2211        1386 :     GEN x = gen_1;
    2212       31976 :     for (j=0; j<d_K; j++)
    2213       30590 :       x = Fp_mul(x, Fp_powu(gel(xi_el, j), e_chi[(i+j)%d_K], ell), ell);
    2214        1386 :     x = Fp_pow(x, ell1d2, ell);
    2215        1386 :     if (!equali1(x)) return gc_long(av, 0);
    2216             :   }
    2217          35 :   return gc_long(av, 1);
    2218             : }
    2219             : 
    2220             : static GEN
    2221          21 : next_el_real(GEN K, long p, long d_pow, GEN elg, long j0, long flag)
    2222             : {
    2223          21 :   GEN Chi = gel(K, 2);
    2224          21 :   ulong f = K_get_f(K), h = K_get_h(K), d = upowuu(p, d_pow), d2 = d*d;
    2225          21 :   ulong D = (flag & USE_F)? d2*f: d2<<1, el = elg[1] + D;
    2226             : 
    2227             :   /* O(el*h) -> O(el*log(el)) by FFT */
    2228          21 :   if (1000 < h && el < h) { el = (h/D)*D+1; if (el < h) el += D; }
    2229          21 :   if (flag&USE_F)  /* el=1 (mod f) */
    2230             :   {
    2231           0 :     for (;; el += D)
    2232           0 :       if (uisprime(el) && chk_el_real_f(K, p, d_pow, el, j0)) break;
    2233             :   }
    2234             :   else
    2235             :   {
    2236         413 :     for (;; el += D)
    2237         455 :       if (Chi[el%f]==0 && uisprime(el) &&
    2238          42 :           chk_el_real_chi(K, p, d_pow, el, j0, flag)) break;
    2239             :   }
    2240          21 :   return mkvecsmall2(el, pgener_Fl(el));
    2241             : }
    2242             : 
    2243             : static GEN
    2244          35 : next_ell_real(GEN K, GEN ellg, long d2, GEN df0l, long j0)
    2245             : {
    2246          35 :   GEN ell = gel(ellg, 1);
    2247        7602 :   for (ell = addii(ell, df0l);; ell = addii(ell, df0l))
    2248        7602 :     if (BPSW_psp(ell) && chk_ell_real(K, d2, ell, j0))
    2249          35 :       return mkvec2(ell, pgener_Fp(ell));
    2250             : }
    2251             : 
    2252             : /* #velg >= n */
    2253             : static long
    2254           0 : delete_el(GEN velg, long n)
    2255             : {
    2256             :   long i, l;
    2257           0 :   if (DEBUGLEVEL>1) err_printf("deleting %ld ...\n", gmael(velg, n, 1));
    2258           0 :   for (l = lg(velg)-1; l >= 1; l--) if (gel(velg, l)) break;
    2259           0 :   for (i = n; i < l; i++) gel(velg, i) = gel(velg, i+1);
    2260           0 :   return l;
    2261             : }
    2262             : 
    2263             : /* velg has n components */
    2264             : static GEN
    2265          21 : set_ell_real(GEN K, GEN velg, long n, long d_chi, long d2, long f0, long j0)
    2266             : {
    2267          21 :   long i, n_ell = n*d_chi;
    2268          21 :   GEN z = cgetg(n_ell + 1, t_VEC);
    2269          21 :   GEN df0l = muluu(d2, f0), ellg = mkvec2(gen_1, gen_1);
    2270          42 :   for (i=1; i<=n; i++) df0l = muliu(df0l, gel(velg, i)[1]);
    2271          56 :   for (i=1; i<=n_ell; i++) ellg = gel(z, i)= next_ell_real(K, ellg, d2, df0l, j0);
    2272          21 :   return z;
    2273             : }
    2274             : 
    2275             : static GEN
    2276         182 : G_K_vell(GEN K, GEN vellg, ulong gk)
    2277             : {
    2278         182 :   pari_sp av = avma;
    2279         182 :   GEN H = K_get_H(K);
    2280         182 :   ulong f = K_get_f(K), h = K_get_h(K);
    2281         182 :   GEN z_f, vz_f, A, P, M, z =  cgetg(h+1, t_VEC);
    2282         182 :   ulong i, lv = lg(vellg);
    2283             : 
    2284         182 :   A=cgetg(lv, t_VEC);
    2285         182 :   P=cgetg(lv, t_VEC);
    2286         728 :   for (i=1; i<lv; i++)
    2287             :   {
    2288         546 :     GEN ell = gmael(vellg, i, 1), g_ell = gmael(vellg, i, 2);
    2289         546 :     gel(A, i) = Fp_pow(g_ell, diviuexact(subiu(ell, 1), f), ell);
    2290         546 :     gel(P, i) = ell;
    2291             :   }
    2292         182 :   z_f = ZV_chinese(A, P, &M);
    2293         182 :   vz_f = Fp_powers(z_f, f-1, M)+1;
    2294        3822 :   for (i=1; i<=h; i++) gel(z, i) = gel(vz_f, Fl_mul(H[i], gk, f));
    2295         182 :   return gerepilecopy(av, FpV_roots_to_pol(z, M, 0));
    2296             : }
    2297             : 
    2298             : /* f=cond(K), M=product of ell in vell, G(K/Q)=<g_K>
    2299             :  * G_K[1+i]=minimal polynomial of zeta_f^{g_k^i} over K mod M, 0 <= i < d_K */
    2300             : static GEN
    2301           7 : make_G_K(GEN K, GEN vellg)
    2302             : {
    2303           7 :   ulong d_K = K_get_d(K), f = K_get_f(K), g_K = K_get_g(K);
    2304           7 :   GEN G_K = cgetg(d_K+1, t_VEC);
    2305           7 :   ulong i, g = 1;
    2306             : 
    2307         189 :   for (i=0; i<d_K; i++)
    2308             :   {
    2309         182 :     gel(G_K, 1+i) = G_K_vell(K, vellg, g);
    2310         182 :     g = Fl_mul(g, g_K, f);
    2311             :   }
    2312           7 :   return G_K;
    2313             : }
    2314             : 
    2315             : static GEN
    2316          12 : G_K_p(GEN K, GEN ellg, ulong gk)
    2317             : {
    2318          12 :   pari_sp av = avma;
    2319          12 :   ulong i, f = K_get_f(K), h = K_get_h(K);
    2320          12 :   GEN ell = gel(ellg, 1), g_ell = gel(ellg, 2);
    2321          12 :   GEN H = K_get_H(K), z_f, vz_f, z = cgetg(h+1, t_VEC);
    2322             : 
    2323          12 :   z_f = Fp_pow(g_ell, diviuexact(subiu(ell, 1), f), ell);
    2324          12 :   vz_f = Fp_powers(z_f, f-1, ell)+1;
    2325        3468 :   for (i=1; i<=h; i++) gel(z, i) = gel(vz_f, Fl_mul(H[i], gk, f));
    2326          12 :   return gerepilecopy(av, FpV_roots_to_pol(z, ell, 0));
    2327             : }
    2328             : 
    2329             : static GEN
    2330         114 : G_K_l(GEN K, GEN ellg, ulong gk)
    2331             : {
    2332         114 :   pari_sp av = avma;
    2333         114 :   ulong ell = itou(gel(ellg, 1)), g_ell = itou(gel(ellg, 2));
    2334         114 :   ulong f = K_get_f(K), h = K_get_h(K), i, z_f;
    2335         114 :   GEN H = K_get_H(K), vz_f, z = cgetg(h+1, t_VEC);
    2336             : 
    2337         114 :   z_f = Fl_powu(g_ell, (ell-1) / f, ell);
    2338         114 :   vz_f = Fl_powers(z_f, f-1, ell)+1;
    2339       26898 :   for (i=1; i<=h; i++) z[i] = vz_f[Fl_mul(H[i], gk, f)];
    2340         114 :   return gerepilecopy(av, Flv_roots_to_pol(z, ell, 0));
    2341             : }
    2342             : 
    2343             : static GEN
    2344           6 : vz_vell(long d, GEN vellg, GEN *pM)
    2345             : {
    2346           6 :   long i, l = lg(vellg);
    2347           6 :   GEN A = cgetg(l, t_VEC), P = cgetg(l, t_VEC), z;
    2348             : 
    2349          18 :   for (i = 1; i < l; i++)
    2350             :   {
    2351          12 :     GEN ell = gmael(vellg, i, 1), g_ell = gmael(vellg, i, 2);
    2352          12 :     gel(A, i) = Fp_pow(g_ell, diviuexact(subiu(ell, 1), d), ell);
    2353          12 :     gel(P, i) = ell;
    2354             :   }
    2355           6 :   z = ZV_chinese(A, P, pM); return Fp_powers(z, d-1, *pM);
    2356             : }
    2357             : 
    2358             : static GEN
    2359           0 : D_xi_el_vell_FFT(GEN K, GEN elg, GEN vellg, ulong d, ulong j0, GEN vG_K)
    2360             : {
    2361           0 :   pari_sp av = avma;
    2362           0 :   ulong d_K, h = K_get_h(K), d_chi = K_get_dchi(K);
    2363           0 :   ulong el = elg[1], g_el = elg[2], el_1 = el-1;
    2364             :   ulong i, j, i2, k, dwel;
    2365           0 :   GEN u = cgetg(el+2, t_POL) , v = cgetg(h+3, t_POL);
    2366           0 :   GEN w = cgetg(el+1, t_VEC), ww;
    2367           0 :   GEN M, vz_el, G_K, z = const_vec(d_chi, gen_1);
    2368           0 :   GEN e_chi = get_e_chi(K, j0, d, &d_K);
    2369             : 
    2370           0 :   vz_el = vz_vell(el, vellg, &M);
    2371           0 :   u[1] = evalsigne(1) | evalvarn(0);
    2372           0 :   v[1] = evalsigne(1) | evalvarn(0);
    2373             : 
    2374           0 :   for (i=i2=0; i<el; i++)
    2375             :   {
    2376           0 :     ulong j2 = i2?el-i2:i2; /* i2=(i*i)%el */
    2377           0 :     gel(u, 2+i) = gel(vz_el, 1+j2);
    2378           0 :     if ((i2+=i+i+1)>=el) i2%=el;
    2379             :   }
    2380           0 :   for (k=0; k<d_K; k++)
    2381             :   {
    2382           0 :     pari_sp av = avma;
    2383             :     pari_timer ti;
    2384             :     long gd, gi;
    2385           0 :     GEN x1 = gen_1;
    2386           0 :     G_K = gel(vG_K, 1+k);
    2387           0 :     for (i=i2=0; i<=h; i++)
    2388             :     {
    2389           0 :       gel(v, 2+i) = Fp_mul(gel(G_K, 2+i), gel(vz_el, 1+i2), M);
    2390           0 :       if ((i2+=i+i+1)>=el) i2%=el;
    2391             :     }
    2392           0 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2393           0 :     ww = ZX_mul(u, v);
    2394           0 :     if (DEBUGLEVEL>2)
    2395           0 :       timer_printf(&ti, "ZX_mul:%ld/%ld h*el=%ld*%ld", k, d_K, h, el);
    2396           0 :     dwel = degpol(ww)-el;
    2397           0 :     for (i=0; i<=dwel; i++) gel(w, 1+i) = addii(gel(ww, 2+i), gel(ww, 2+i+el));
    2398           0 :     for (; i<el; i++) gel(w, 1+i) = gel(ww, 2+i);
    2399           0 :     for (i=i2=1; i<el; i++)  /* w[i]=G_K(z_el^(2*i)) */
    2400             :     {
    2401           0 :       gel(w, i) = Fp_mul(gel(w, 1+i), gel(vz_el, 1+i2), M);
    2402           0 :       if ((i2+=i+i+1)>=el) i2%=el;
    2403             :     }
    2404           0 :     gd = Fl_powu(g_el, d, el);  /* a bit faster */
    2405           0 :     gi = g_el;
    2406           0 :     for (i=1; i<d; i++)
    2407             :     {
    2408           0 :       GEN xi = gen_1;
    2409           0 :       long gdi = gi;
    2410           0 :       for (j=0; i+j<el_1; j+=d)
    2411             :       {
    2412           0 :         xi = Fp_mul(xi, gel(w, (gdi+gdi)%el), M);
    2413           0 :         gdi = Fl_mul(gdi, gd, el);
    2414             :       }
    2415           0 :       gi = Fl_mul(gi, g_el, el);
    2416           0 :       xi = Fp_powu(xi, i, M);
    2417           0 :       x1 = Fp_mul(x1, xi, M);
    2418             :     }
    2419           0 :     for (i=1; i<=d_chi; i++)
    2420             :     {
    2421           0 :       GEN x2 = Fp_powu(x1, e_chi[(k+i-1)%d_K], M);
    2422           0 :       gel(z, i) = Fp_mul(gel(z, i), x2, M);
    2423             :     }
    2424           0 :     z = gerepilecopy(av, z);
    2425             :   }
    2426           0 :   return gerepilecopy(av, z);
    2427             : }
    2428             : 
    2429             : static GEN
    2430           0 : D_xi_el_vell(GEN K, GEN elg, GEN vellg, ulong d, ulong j0)
    2431             : {
    2432           0 :   pari_sp av = avma;
    2433           0 :   GEN H = K_get_H(K);
    2434           0 :   ulong f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    2435             :   GEN z_f, z_el, vz_f, vz_el;
    2436           0 :   ulong el = elg[1], g_el = elg[2], el_1 = el-1;
    2437           0 :   ulong i, j, k, d_K, lv = lg(vellg), d_chi = K_get_dchi(K);
    2438           0 :   GEN A, B, P, M, z = const_vec(d_chi, gen_1);
    2439           0 :   GEN e_chi = get_e_chi(K, j0, d, &d_K);
    2440             : 
    2441           0 :   A=cgetg(lv, t_VEC);
    2442           0 :   B=cgetg(lv, t_VEC);
    2443           0 :   P=cgetg(lv, t_VEC);
    2444           0 :   for (i = 1; i < lv; i++)
    2445             :   {
    2446           0 :     GEN ell = gmael(vellg, i, 1), g_ell = gmael(vellg, i, 2);
    2447           0 :     GEN ell_1 = subiu(ell, 1);
    2448           0 :     gel(A, i) = Fp_pow(g_ell, diviuexact(ell_1, f), ell);
    2449           0 :     gel(B, i) = Fp_pow(g_ell, diviuexact(ell_1, el), ell);
    2450           0 :     gel(P, i) = ell;
    2451             :   }
    2452           0 :   z_f = ZV_chinese(A, P, &M);
    2453           0 :   z_el = ZV_chinese(B, P, NULL);
    2454           0 :   vz_f = Fp_powers(z_f, f-1, M);
    2455           0 :   vz_el = Fp_powers(z_el, el-1, M);
    2456           0 :   for (k = 0; k < d_K; k++)
    2457             :   {
    2458           0 :     pari_sp av = avma;
    2459           0 :     GEN x0 = gen_1;
    2460           0 :     long gk = Fl_powu(g_K, k, f);
    2461           0 :     for (i=1; i<el_1; i++)
    2462             :     {
    2463           0 :       long gi = Fl_powu(g_el, i, el);
    2464           0 :       GEN x1 = gen_1;
    2465           0 :       GEN x2 = gel(vz_el, 1+gi);
    2466           0 :       for (j=1; j<=h; j++)
    2467             :       {
    2468           0 :         long y = Fl_mul(H[j], gk, f);
    2469           0 :         x1 = Fp_mul(x1, Fp_sub(x2, gel(vz_f, 1+y), M), M);
    2470             :       }
    2471           0 :       x1 = Fp_powu(x1, i%d, M);
    2472           0 :       x0 = Fp_mul(x0, x1, M);
    2473             :     }
    2474           0 :     for (i=1; i<=d_chi; i++)
    2475             :     {
    2476           0 :       GEN x2 = Fp_powu(x0, e_chi[(k+i-1)%d_K], M);
    2477           0 :       gel(z, i) = Fp_mul(gel(z, i), x2, M);
    2478             :     }
    2479           0 :     z = gerepilecopy(av, z);
    2480             :   }
    2481           0 :   return gerepilecopy(av, z);
    2482             : }
    2483             : 
    2484             : static GEN
    2485          34 : D_xi_el_Flx_mul(GEN K, GEN elg, GEN ellg, GEN vG_K, ulong d, ulong j0)
    2486             : {
    2487          34 :   pari_sp av = avma;
    2488          34 :   ulong d_K, f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    2489          34 :   ulong el = elg[1], g_el = elg[2], el_1 = el-1, d_chi = K_get_dchi(K);
    2490          34 :   ulong ell = itou(gel(ellg, 1)), g_ell = itou(gel(ellg, 2)), z_el;
    2491          34 :   GEN u = cgetg(el+2, t_VECSMALL), v = cgetg(h+3, t_VECSMALL);
    2492          34 :   GEN w = cgetg(el+1, t_VECSMALL), ww;
    2493          34 :   GEN vz_el, G_K, z = const_vecsmall(d_chi, 1);
    2494          34 :   GEN e_chi = get_e_chi(K, j0, d, &d_K);
    2495             :   ulong i, j, i2, k, dwel;
    2496             : 
    2497          34 :   u[1] = evalvarn(0);
    2498          34 :   v[1] = evalvarn(0);
    2499          34 :   z_el = Fl_powu(g_ell, (ell - 1) / el, ell);
    2500          34 :   vz_el = Fl_powers(z_el, el_1, ell)+1;
    2501             : 
    2502      700376 :   for (i=i2=0; i<el; i++)
    2503             :   {
    2504      700342 :     ulong j2 = i2?el-i2:i2;
    2505      700342 :     u[2+i] = vz_el[j2];
    2506      700342 :     if ((i2+=i+i+1)>=el) i2%=el;  /* i2=(i*i)%el */
    2507             :   }
    2508         694 :   for (k=0; k<d_K; k++)
    2509             :   {
    2510         660 :     pari_sp av = avma;
    2511             :     pari_timer ti;
    2512         660 :     ulong gk = Fl_powu(g_K, k, f);
    2513         660 :     long gd, gi, x1 = 1;
    2514         660 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2515         660 :     G_K = (vG_K==NULL)?G_K_l(K, ellg, gk):ZX_to_Flx(gel(vG_K, 1+k), ell);
    2516         660 :     if (DEBUGLEVEL>2) timer_printf(&ti, "G_K_l");
    2517       39024 :     for (i=i2=0; i<=h; i++)
    2518             :     {
    2519       38364 :       v[2+i] = Fl_mul(G_K[2+i], vz_el[i2], ell);
    2520       38364 :       if ((i2+=i+i+1)>=el) i2%=el;  /* i2=(i*i)%el */
    2521             :     }
    2522         660 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2523         660 :     ww = Flx_mul(u, v, ell);
    2524         660 :     if (DEBUGLEVEL>2)
    2525           0 :       timer_printf(&ti, "Flx_mul:%ld/%ld h*el=%ld*%ld", k, d_K, h, el);
    2526         660 :     dwel=degpol(ww)-el; /* dwel=h-1 */
    2527       38364 :     for (i=0; i<=dwel; i++) w[1+i] = Fl_add(ww[2+i], ww[2+i+el], ell);
    2528     8388480 :     for (; i<el; i++) w[1+i] = ww[2+i];
    2529     8425524 :     for (i=i2=1; i<el; i++)  /* w[i]=G_K(z_el^(2*i)) */
    2530             :     {
    2531     8424864 :       w[i] = Fl_mul(w[1+i], vz_el[i2], ell);
    2532     8424864 :       if ((i2+=i+i+1)>=el) i2%=el;  /* i2=(i*i)%el */
    2533             :     }
    2534         660 :     gd = Fl_powu(g_el, d, el);  /* a bit faster */
    2535         660 :     gi = g_el;
    2536        4596 :     for (i=1; i<d; i++)
    2537             :     {
    2538        3936 :       long xi = 1, gdi = gi;
    2539     8163696 :       for (j=0; i+j<el_1; j+=d)
    2540             :       {
    2541     8159760 :         xi = Fl_mul(xi, w[(gdi+gdi)%el], ell);
    2542     8159760 :         gdi = Fl_mul(gdi, gd, el);
    2543             :       }
    2544        3936 :       gi = Fl_mul(gi, g_el, el);
    2545        3936 :       xi = Fl_powu(xi, i, ell);
    2546        3936 :       x1 = Fl_mul(x1, xi, ell);
    2547             :     }
    2548        2412 :     for (i=1; i<=d_chi; i++)
    2549             :     {
    2550        1752 :       long x2 = Fl_powu(x1, e_chi[(k+i-1)%d_K], ell);
    2551        1752 :       z[i] = Fl_mul(z[i], x2, ell);
    2552             :     }
    2553         660 :     set_avma(av);
    2554             :   }
    2555          34 :   return gerepilecopy(av, Flv_to_ZV(z));
    2556             : }
    2557             : 
    2558             : static GEN
    2559          35 : D_xi_el_ZX_mul(GEN K, GEN elg, GEN ellg, GEN vG_K, ulong d, ulong j0)
    2560             : {
    2561          35 :   pari_sp av = avma;
    2562          35 :   GEN ell = gel(ellg,1), g_ell, u, v, w, ww, z_el, vz_el, G_K, z, e_chi;
    2563             :   ulong d_K, f, h, g_K, el, g_el, el_1, d_chi, i, j, i2, k, dwel;
    2564             : 
    2565          35 :   if (lgefint(ell) == 3) return D_xi_el_Flx_mul(K, elg, ellg, vG_K, d, j0);
    2566           1 :   f = K_get_f(K); h = K_get_h(K); g_K = K_get_g(K);
    2567           1 :   el = elg[1]; g_el = elg[2]; el_1 = el-1; d_chi = K_get_dchi(K);
    2568           1 :   g_ell = gel(ellg, 2);
    2569           1 :   z = const_vec(d_chi, gen_1);
    2570           1 :   e_chi = get_e_chi(K, j0, d, &d_K);
    2571             : 
    2572           1 :   u = cgetg(el+2,t_POL); u[1] = evalsigne(1) | evalvarn(0);
    2573           1 :   v = cgetg(h+3, t_POL); v[1] = evalsigne(1) | evalvarn(0);
    2574           1 :   w = cgetg(el+1, t_VEC);
    2575           1 :   z_el = Fp_pow(g_ell, diviuexact(subiu(ell, 1), el), ell);
    2576           1 :   vz_el = Fp_powers(z_el, el_1, ell)+1;
    2577             : 
    2578      114998 :   for (i=i2=0; i<el; i++)
    2579             :   {
    2580      114997 :     ulong j2 = i2?el-i2:i2; /* i2=(i*i)%el */
    2581      114997 :     gel(u, 2+i) = gel(vz_el, j2);
    2582      114997 :     if ((i2+=i+i+1)>=el) i2%=el;
    2583             :   }
    2584          13 :   for (k=0; k<d_K; k++)
    2585             :   {
    2586          12 :     pari_sp av = avma;
    2587             :     pari_timer ti;
    2588          12 :     long gd, gi, gk = Fl_powu(g_K, k, f);
    2589          12 :     GEN x1 = gen_1;
    2590          12 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2591          12 :     G_K = (vG_K==NULL) ? G_K_p(K, ellg, gk):RgX_to_FpX(gel(vG_K, 1+k), ell);
    2592          12 :     if (DEBUGLEVEL>2) timer_printf(&ti, "G_K_p");
    2593        3480 :     for (i=i2=0; i<=h; i++)
    2594             :     {
    2595        3468 :       gel(v, 2+i) = Fp_mul(gel(G_K, 2+i), gel(vz_el, i2), ell);
    2596        3468 :       if ((i2+=i+i+1)>=el) i2%=el;
    2597             :     }
    2598          12 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2599          12 :     ww = ZX_mul(u, v);
    2600          12 :     if (DEBUGLEVEL>2)
    2601           0 :       timer_printf(&ti, "ZX_mul:%ld/%ld h*el=%ld*%ld", k, d_K, h, el);
    2602          12 :     dwel = degpol(ww)-el;
    2603        3468 :     for (i=0; i<=dwel; i++) gel(w, 1+i) = addii(gel(ww, 2+i), gel(ww, 2+i+el));
    2604     1376520 :     for (; i<el; i++) gel(w, 1+i) = gel(ww, 2+i);
    2605     1379964 :     for (i=i2=1; i<el; i++)  /* w[i]=G_K(z_el^(2*i)) */
    2606             :     {
    2607     1379952 :       gel(w, i) = Fp_mul(gel(w, 1+i), gel(vz_el, i2), ell);
    2608     1379952 :       if ((i2+=i+i+1)>=el) i2%=el;
    2609             :     }
    2610          12 :     gd = Fl_powu(g_el, d, el);  /* a bit faster */
    2611          12 :     gi = g_el;
    2612         444 :     for (i=1; i<d; i++)
    2613             :     {
    2614         432 :       GEN xi = gen_1;
    2615         432 :       long gdi = gi;
    2616     1343088 :       for (j=0; i+j<el_1; j+=d)
    2617             :       {
    2618     1342656 :         xi = Fp_mul(xi, gel(w, (gdi+gdi)%el), ell);
    2619     1342656 :         gdi = Fl_mul(gdi, gd, el);
    2620             :       }
    2621         432 :       gi = Fl_mul(gi, g_el, el);
    2622         432 :       xi = Fp_powu(xi, i, ell);
    2623         432 :       x1 = Fp_mul(x1, xi, ell);
    2624             :     }
    2625          24 :     for (i=1; i<=d_chi; i++)
    2626             :     {
    2627          12 :       GEN x2 = Fp_powu(x1, e_chi[(k+i-1)%d_K], ell);
    2628          12 :       gel(z, i) = Fp_mul(gel(z, i), x2, ell);
    2629             :     }
    2630          12 :     z = gerepilecopy(av, z);
    2631             :   }
    2632           1 :   return gerepilecopy(av, z);
    2633             : }
    2634             : 
    2635             : static GEN
    2636           0 : D_xi_el_ss(GEN K, GEN elg, GEN ellg, ulong d, ulong j0)
    2637             : {
    2638           0 :   pari_sp av = avma;
    2639           0 :   GEN H = K_get_H(K);
    2640           0 :   ulong d_K, f = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    2641           0 :   ulong el = elg[1], g_el = elg[2], el_1 = el-1;
    2642           0 :   ulong ell = itou(gel(ellg, 1)), g_ell = itou(gel(ellg, 2));
    2643           0 :   ulong i, j, k, gk, z_f, z_el, d_chi = K_get_dchi(K);
    2644           0 :   GEN vz_f, vz_el, z = const_vecsmall(d_chi, 1);
    2645           0 :   GEN e_chi = get_e_chi(K, j0, d, &d_K);
    2646             : 
    2647           0 :   z_f = Fl_powu(g_ell, (ell - 1) / f, ell);
    2648           0 :   z_el = Fl_powu(g_ell, (ell - 1) / el, ell);
    2649           0 :   vz_f = Fl_powers(z_f, f-1, ell)+1;
    2650           0 :   vz_el = Fl_powers(z_el, el-1, ell)+1;
    2651           0 :   gk = 1; /* g_K^k */
    2652           0 :   for (k = 0; k < d_K; k++)
    2653             :   {
    2654           0 :     ulong x0 = 1, gi = g_el; /* g_el^i */
    2655           0 :     for (i = 1; i < el_1; i++)
    2656             :     {
    2657           0 :       ulong x1 = 1, x2 = vz_el[gi];
    2658           0 :       for (j=1; j<=h; j++)
    2659             :       {
    2660           0 :         ulong y = Fl_mul(H[j], gk, f);
    2661           0 :         x1 = Fl_mul(x1, Fl_sub(x2, vz_f[y], ell), ell);
    2662             :       }
    2663           0 :       x1 = Fl_powu(x1, i%d, ell);
    2664           0 :       x0 = Fl_mul(x0, x1, ell);
    2665           0 :       gi = Fl_mul(gi, g_el, el);
    2666             :     }
    2667           0 :     for (i = 1; i <= d_chi; i++)
    2668             :     {
    2669           0 :       ulong x2 = Fl_powu(x0, e_chi[(k+i-1)%d_K], ell);
    2670           0 :       z[i] = Fl_mul(z[i], x2, ell);
    2671             :     }
    2672           0 :     gk = Fl_mul(gk, g_K, f);
    2673             :   }
    2674           0 :   return gerepileupto(av, Flv_to_ZV(z));
    2675             : }
    2676             : 
    2677             : static GEN
    2678           0 : D_xi_el_sl(GEN K, GEN elg, GEN ellg, ulong d, ulong j0)
    2679             : {
    2680           0 :   pari_sp av = avma;
    2681           0 :   GEN ell = gel(ellg, 1), H;
    2682             :   GEN g_ell, ell_1, z_f, z_el, vz_f, vz_el, z, e_chi;
    2683             :   ulong d_K, f, h, g_K, el, g_el, el_1, d_chi, i, j, k, gk;
    2684             : 
    2685           0 :   if (lgefint(ell) == 3) return D_xi_el_ss(K, elg, ellg, d, j0);
    2686           0 :   H = K_get_H(K);
    2687           0 :   f = K_get_f(K); h = K_get_h(K); g_K = K_get_g(K);
    2688           0 :   el = elg[1]; g_el = elg[2]; el_1 = el-1; d_chi = K_get_dchi(K);
    2689           0 :   g_ell = gel(ellg, 2); ell_1 = subiu(ell, 1);
    2690           0 :   z = const_vec(d_chi, gen_1);
    2691           0 :   e_chi = get_e_chi(K, j0, d, &d_K);
    2692             : 
    2693           0 :   z_f = Fp_pow(g_ell, diviuexact(ell_1, f), ell);
    2694           0 :   z_el = Fp_pow(g_ell, diviuexact(ell_1, el), ell);
    2695           0 :   vz_f = Fp_powers(z_f, f-1, ell) + 1;
    2696           0 :   vz_el = Fp_powers(z_el, el-1, ell) + 1;
    2697           0 :   gk = 1; /* g_K^k */
    2698           0 :   for (k = 0; k < d_K; k++)
    2699             :   {
    2700           0 :     pari_sp av2 = avma;
    2701           0 :     GEN x0 = gen_1;
    2702           0 :     ulong gi = g_el; /* g_el^i */
    2703           0 :     for (i = 1; i < el_1; i++)
    2704             :     {
    2705           0 :       pari_sp av3 = avma;
    2706           0 :       GEN x1 = gen_1, x2 = gel(vz_el, gi);
    2707           0 :       for (j = 1; j <= h; j++)
    2708             :       {
    2709           0 :         ulong y = Fl_neg(Fl_mul(H[j], gk, f), f);
    2710           0 :         x1 = Fp_mul(x1, Fp_sub(x2, gel(vz_f, y), ell), ell);
    2711             :       }
    2712           0 :       x1 = Fp_powu(x1, i%d, ell);
    2713           0 :       x0 = gerepileuptoint(av3, Fp_mul(x0, x1, ell));
    2714           0 :       gi = Fl_mul(gi, g_el, el);
    2715             :     }
    2716           0 :     for (i=1; i<=d_chi; i++)
    2717             :     {
    2718           0 :       GEN x2 = Fp_powu(x0, e_chi[(k+i-1)%d_K], ell);
    2719           0 :       gel(z, i) = Fp_mul(gel(z, i), x2, ell);
    2720             :     }
    2721           0 :     if (k == d_K-1) break;
    2722           0 :     z = gerepilecopy(av2, z);
    2723           0 :     gk = Fl_mul(gk, g_K, f);
    2724             :   }
    2725           0 :   return gerepilecopy(av, z);
    2726             : }
    2727             : 
    2728             : static long
    2729         175 : get_y(GEN z, GEN ellg, long d)
    2730             : {
    2731         175 :   GEN ell = gel(ellg, 1), g_ell = gel(ellg, 2);
    2732         175 :   GEN elld = diviuexact(subiu(ell, 1), d);
    2733         175 :   GEN g_elld = Fp_pow(g_ell, elld, ell);
    2734         175 :   GEN x = Fp_pow(modii(z, ell), elld, ell);
    2735             :   long k;
    2736      211778 :   for (k=0; k<d; k++)
    2737             :   {
    2738      211778 :     if (equali1(x)) break;
    2739      211603 :     x = Fp_mul(x, g_elld, ell);
    2740             :   }
    2741         175 :   if (k==0) k=d;
    2742         161 :   else if (d<=k) pari_err_BUG("subcyclopclgp [MLL]");
    2743         175 :   return k;
    2744             : }
    2745             : 
    2746             : static void
    2747           0 : real_MLLn(long *y, GEN K, ulong p, ulong d_pow, ulong n,
    2748             :     GEN velg, GEN vellg, GEN vG_K, ulong j0)
    2749             : {
    2750           0 :   pari_sp av = avma;
    2751           0 :   ulong i, j, k, d = upowuu(p, d_pow), h = gmael(K, 1, 2)[3];
    2752           0 :   ulong row = lg(vellg)-1;
    2753           0 :   for (i=1; i<=n; i++)
    2754             :   {
    2755           0 :     GEN elg = gel(velg, i), z;
    2756           0 :     ulong el = elg[1], nz;
    2757             :     pari_timer ti;
    2758           0 :     if (DEBUGLEVEL>1) timer_start(&ti);
    2759           0 :     z = (h<el) ? D_xi_el_vell_FFT(K, elg, vellg, d, j0, vG_K)
    2760           0 :                : D_xi_el_vell(K, elg, vellg, d, j0);
    2761           0 :     if (DEBUGLEVEL>1) timer_printf(&ti, "subcyclopclgp:[D_xi_el]");
    2762           0 :     if (DEBUGLEVEL>2) err_printf("z=%Ps\n", z);
    2763           0 :     nz = lg(z)-1;
    2764           0 :     for (k = 1; k <= nz; k++)
    2765           0 :       for (j=1; j<=row; j++)
    2766           0 :         y[(j-1)*row+(i-1)*nz+k-1] = get_y(gel(z, k), gel(vellg, j), d);
    2767           0 :     set_avma(av);
    2768             :   }
    2769           0 : }
    2770             : 
    2771             : static void
    2772          14 : real_MLL1(long *y, GEN K, ulong p, ulong d_pow, GEN velg, GEN vellg, ulong j0)
    2773             : {
    2774          14 :   ulong h = gmael(K, 1, 2)[3], d = upowuu(p, d_pow);
    2775          14 :   GEN elg = gel(velg, 1), ellg = gel(vellg, 1), z;
    2776          14 :   ulong el = elg[1];
    2777             :   pari_timer ti;
    2778             : 
    2779          14 :   if (DEBUGLEVEL>2) timer_start(&ti);
    2780          14 :   z = h < el? D_xi_el_ZX_mul(K, elg, ellg, NULL, d, j0)
    2781          14 :             : D_xi_el_sl(K, elg, ellg, d, j0);
    2782          14 :   if (DEBUGLEVEL>2) timer_printf(&ti, "subcyclopclgp:[D_xi_el]");
    2783          14 :   if (DEBUGLEVEL>2) err_printf("z=%Ps\n", z);
    2784          14 :   y[0] = get_y(gel(z, 1), ellg, d);
    2785          14 : }
    2786             : 
    2787             : static void
    2788           7 : real_MLL(long *y, GEN K, ulong p, ulong d_pow, ulong n,
    2789             :     GEN velg, GEN vellg, GEN vG_K, ulong j0)
    2790             : {
    2791           7 :   ulong i, j, k, d = upowuu(p, d_pow), h = gmael(K, 1, 2)[3];
    2792           7 :   ulong row = lg(vellg)-1;
    2793          28 :   for (j=1; j<=row; j++)
    2794             :   {
    2795          21 :     GEN ellg = gel(vellg, j);
    2796          42 :     for (i=1; i<=n; i++)
    2797             :     {
    2798          21 :       pari_sp av2 = avma;
    2799          21 :       GEN elg = gel(velg, i), z;
    2800          21 :       ulong el = elg[1], nz;
    2801             :       pari_timer ti;
    2802          21 :       if (DEBUGLEVEL>2) timer_start(&ti);
    2803          21 :       z = h < el? D_xi_el_ZX_mul(K, elg, ellg, vG_K, d, j0)
    2804          21 :                 : D_xi_el_sl(K, elg, ellg, d, j0);
    2805          21 :       if (DEBUGLEVEL>2) timer_printf(&ti, "subcyclopclgp:[D_xi_el]");
    2806          21 :       if (DEBUGLEVEL>3) err_printf("z=%Ps\n", z);
    2807          21 :       nz = lg(z)-1;
    2808          84 :       for (k = 1; k <= nz; k++)
    2809          63 :         y[(j-1)*row+(i-1)*nz+k-1] = get_y(gel(z, k), ellg, d);
    2810          21 :       set_avma(av2);
    2811             :     }
    2812             :   }
    2813           7 : }
    2814             : 
    2815             : static long
    2816          21 : use_basis(long d_K, long f) { return (d_K<=10 || (d_K<=30 && f<=5000)); }
    2817             : 
    2818             : static long
    2819           7 : use_factor(ulong f)
    2820           7 : { GEN fa = factoru(f), P = gel(fa, 1); return (P[lg(P)-1]<500); }
    2821             : 
    2822             : /* group structure, destroy gr */
    2823             : static GEN
    2824          63 : get_str(GEN gr)
    2825             : {
    2826          63 :   GEN z = gel(gr,2);
    2827          63 :   long i, j, l = lg(z);
    2828         154 :   for (i = j = 1; i < l; i++)
    2829          91 :     if (lgefint(gel(z, i)) > 2) gel(z,j++) = gel(z,i);
    2830          63 :   setlg(z, j); return z;
    2831             : }
    2832             : 
    2833             : static GEN
    2834          21 : cyc_real_MLL(GEN K, ulong p, long d_pow, long j0, long flag)
    2835             : {
    2836          21 :   ulong d_K = K_get_d(K), f = K_get_f(K), d_chi = K_get_dchi(K);
    2837          21 :   ulong n, n0 = 1, f0, n_el = d_pow, d = upowuu(p, d_pow), rank = n_el*d_chi;
    2838          21 :   GEN velg = const_vec(n_el, NULL), vellg = NULL;
    2839          21 :   GEN oldgr = mkvec2(gen_0, NULL), newgr = mkvec2(gen_0, NULL);
    2840          21 :   long *y0 = (long*)stack_calloc(sizeof(long)*rank*rank);
    2841             : 
    2842          21 :   if (DEBUGLEVEL>1)
    2843           0 :     err_printf("cyc_real_MLL:p=%ld d_pow=%ld deg(K)=%ld cond(K)=%ld g_K=%ld\n",
    2844             :         p, d_pow, d_K, f, K_get_g(K));
    2845          21 :   gel(K, 2) = get_chi(gel(K,1));
    2846          21 :   if (f-1 <= (d_K<<1)) flag |= USE_F;
    2847          21 :   else if (use_basis(d_K, f)) flag |= USE_BASIS;
    2848           7 :   else if (use_factor(f)) flag |= USE_FACTOR;
    2849           0 :   else flag |= USE_GALOIS_POL;
    2850          21 :   if (flag&USE_BASIS) K = vec_append(K, xi_data_basis(K));
    2851           7 :   else if (flag&USE_GALOIS_POL) K = vec_append(K, xi_data_galois(K));
    2852          21 :   f0 = f%p?f:f/p;
    2853          21 :   gel(velg, 1) = next_el_real(K, p, d_pow, mkvecsmall2(1, 1), j0, flag);
    2854          21 :   if (flag&USE_FULL_EL)
    2855             :   {
    2856           0 :     for (n=2; n<=n_el; n++)
    2857           0 :       gel(velg, n) = next_el_real(K, p, d_pow, gel(velg, n+1), j0, flag);
    2858           0 :     n0 = n_el;
    2859             :   }
    2860             : 
    2861          21 :   for (n=n0; n<=n_el; n++) /* loop while structure is unknown */
    2862             :   {
    2863          21 :     pari_sp av2 = avma;
    2864             :     long n_ell, m, M;
    2865             :     GEN y;
    2866             :     pari_timer ti;
    2867          21 :     if (DEBUGLEVEL>2) timer_start(&ti);
    2868          21 :     vellg = set_ell_real(K, velg, n, d_chi, d*d, f0, j0);
    2869          21 :     n_ell = lg(vellg) -1; /* equal to n*d_chi */
    2870          21 :     if (DEBUGLEVEL>2) timer_printf(&ti, "set_ell_real");
    2871          21 :     if (DEBUGLEVEL>3) err_printf("vel=%Ps\nvell=%Ps\n", velg, vellg);
    2872          21 :     if (n_ell==1)
    2873          14 :       real_MLL1(y0, K, p, d_pow, velg, vellg, j0);
    2874             :     else
    2875             :     {
    2876             :       GEN vG_K;
    2877           7 :       if (DEBUGLEVEL>2) timer_start(&ti);
    2878           7 :       vG_K = make_G_K(K, vellg);
    2879           7 :       if (DEBUGLEVEL>2) timer_printf(&ti, "make_G_K");
    2880           7 :       if (lgefint(gmael(vellg, n_ell, 1))<=3 || (flag&SAVE_MEMORY))
    2881           7 :         real_MLL(y0, K, p, d_pow, n, velg, vellg, vG_K, j0);
    2882             :       else
    2883           0 :         real_MLLn(y0, K, p, d_pow, n, velg, vellg, vG_K, j0);
    2884             :     }
    2885          21 :     set_avma(av2);
    2886          21 :     y = ary2mat(y0, n_ell);
    2887          21 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    2888          21 :     y = ZM_snf(y);
    2889          21 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    2890          21 :     y = make_p_part(y, p, d_pow);
    2891          21 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    2892          21 :     newgr = structure_MLL(y, d_pow);
    2893          21 :     if (DEBUGLEVEL>3)
    2894           0 :       err_printf("d_pow=%ld d_chi=%ld old=%Ps new=%Ps\n",d_pow,d_chi,oldgr,newgr);
    2895          21 :     if (equalsi(d_pow*d_chi, gel(newgr, 1))) break;
    2896           0 :     if ((m = find_del_el(&oldgr, newgr, n, n_el, d_chi)))
    2897           0 :     { M = m = delete_el(velg, m); n--; }
    2898             :     else
    2899           0 :     { M = n+1; m = n; }
    2900           0 :     gel(velg, M) = next_el_real(K, p, d_pow, gel(velg, m), j0, flag);
    2901             :   }
    2902          21 :   return get_str(newgr);
    2903             : }
    2904             : 
    2905             : static GEN
    2906           0 : cyc_buch(long dK, GEN p, long d_pow)
    2907             : {
    2908           0 :   GEN z = Buchquad(stoi(dK), 0.0, 0.0, 0), cyc = gel(z,2);
    2909           0 :   long i, l = lg(cyc);
    2910           0 :   if (Z_pval(gel(z,1), p) != d_pow) pari_err_BUG("subcyclopclgp [Buchquad]");
    2911           0 :   for (i = 1; i < l; i++)
    2912             :   {
    2913           0 :     long x = Z_pval(gel(cyc, i), p); if (!x) break;
    2914           0 :     gel(cyc, i) = utoipos(x);
    2915             :   }
    2916           0 :   setlg(cyc, i); return cyc;
    2917             : }
    2918             : 
    2919             : static void
    2920           0 : verbose_output(GEN K, GEN p, long pow, long j)
    2921             : {
    2922           0 :   long d = K_get_d(K), f = K_get_f(K), s = K_get_s(K), d_chi = K_get_dchi(K);
    2923           0 :   err_printf("|A_K_psi|=%Ps^%ld, psi=chi^%ld, d_psi=%ld, %s,\n\
    2924             :     [K:Q]=%ld, [f,H]=[%ld, %Ps]\n",
    2925           0 :     p,pow*d_chi,j,d_chi,s?"real":"imaginary",d,f,zv_to_ZV(gmael3(K,1,1,1)));
    2926           0 : }
    2927             : 
    2928             : static int
    2929       35091 : cyc_real_pre(GEN K, GEN xi, ulong p, ulong j, long el)
    2930             : {
    2931       35091 :   pari_sp av = avma;
    2932       35091 :   ulong i, d_K, x = 1;
    2933       35091 :   GEN e_chi = get_e_chi(K, j, p, &d_K);
    2934             : 
    2935       35091 :   xi++;
    2936     1254827 :   for (i = 0; i < d_K; i++) x = Fl_mul(x, Fl_powu(xi[i], e_chi[i], el), el);
    2937       35091 :   return gc_ulong(av, Fl_powu(x, (el-1)/p, el));
    2938             : }
    2939             : 
    2940             : /* return vec[-1,[],0], vec[0,[],0], vec[1,[1],0], vec[2,[1,1],0] etc */
    2941             : static GEN
    2942       26579 : cyc_real_ss(GEN K, GEN xi, ulong p, long j, long pow, long el, ulong pn, long flag)
    2943             : {
    2944       26579 :   ulong d_chi = K_get_dchi(K);
    2945       26579 :   if (cyc_real_pre(K, xi, pn, j, el) == 1) return NULL; /* not determined */
    2946       21273 :   if (--pow==0) return mkvec3(gen_0, nullvec(), gen_0); /* trivial */
    2947         105 :   if (DEBUGLEVEL) verbose_output(K, utoi(p), pow, j);
    2948         105 :   if (flag&USE_MLL)
    2949             :   {
    2950          21 :     pari_sp av = avma;
    2951          21 :     GEN gr = (K_get_d(K) == 2)? cyc_buch(K_get_f(K), utoi(p), pow)
    2952          21 :                                : cyc_real_MLL(K, p, pow, j, flag);
    2953          21 :     return gerepilecopy(av, mkvec3(utoipos(d_chi * pow), gr, gen_0));
    2954             :   }
    2955          84 :   if (pow==1) return mkvec3(utoi(d_chi), onevec(d_chi), gen_0);
    2956          21 :   return mkvec3(utoi(pow*d_chi), nullvec(), gen_0);
    2957             : }
    2958             : 
    2959             : static GEN
    2960        5145 : cyc_real_ll(GEN K, GEN xi, GEN p, long j, long pow, GEN el, GEN pn, long flag)
    2961             : {
    2962        5145 :   pari_sp av = avma;
    2963        5145 :   ulong i, d_K = K_get_d(K), d_chi = K_get_dchi(K);
    2964        5145 :   GEN e_chi = get_e_chi_ll(K, j, pn), x = gen_1;
    2965             : 
    2966        5145 :   xi++;
    2967      246785 :   for (i = 0; i < d_K; i++)
    2968      241640 :     x = Fp_mul(x, Fp_pow(gel(xi, i), gel(e_chi, i), el), el);
    2969        5145 :   x = Fp_pow(x, diviiexact(subiu(el, 1), pn), el); /* x = x^(el-1)/pn mod el */
    2970        5145 :   set_avma(av); if (equali1(x)) return NULL; /* not determined */
    2971        5145 :   if (--pow==0) return mkvec3(gen_0, nullvec(), gen_0); /* trivial */
    2972           0 :   if (DEBUGLEVEL) verbose_output(K, p, pow, j);
    2973           0 :   if (flag&USE_MLL)
    2974           0 :     pari_err_IMPL(stack_sprintf("flag=%ld for large prime", USE_MLL));
    2975           0 :   if (pow==1) return mkvec3(utoi(d_chi), onevec(d_chi), gen_0);
    2976           0 :   return mkvec3(utoi(pow*d_chi), nullvec(), gen_0);
    2977             : }
    2978             : 
    2979             : /* xi[1+i] = xi^(g^i), 0 <= i <= d-1 */
    2980             : static GEN
    2981       13797 : xi_conj_s(GEN K, ulong el)
    2982             : {
    2983       13797 :   pari_sp av = avma;
    2984       13797 :   GEN  H = K_get_H(K);
    2985       13797 :   long d = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g = K_get_g(K);
    2986       13797 :   long i, gi = 1, z = Fl_powu(pgener_Fl(el), (el-1)/f, el);
    2987       13797 :   GEN vz = Fl_powers(z, f, el)+1, xi = cgetg(d+1, t_VECSMALL);
    2988             : 
    2989      372953 :   for (i=1; i<=d; i++)
    2990             :   {
    2991      359156 :     long j, x = 1;
    2992   170777852 :     for (j=1; j<=h; j++)
    2993   170418696 :       x = Fl_mul(x, vz[Fl_mul(H[j], gi, f)]-1, el);
    2994      359156 :     xi[i] = x;
    2995      359156 :     gi = Fl_mul(gi, g, f);
    2996             :   }
    2997       13797 :   return gerepilecopy(av, xi);
    2998             : }
    2999             : 
    3000             : static GEN
    3001        1673 : xi_conj_l(GEN K, GEN el)
    3002             : {
    3003        1673 :   pari_sp av = avma;
    3004        1673 :   GEN  H = K_get_H(K);
    3005        1673 :   long d = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g = K_get_g(K);
    3006        1673 :   long i, gi = 1;
    3007        1673 :   GEN z = Fp_pow(pgener_Fp(el), diviuexact(subiu(el, 1), f), el);
    3008        1673 :   GEN vz = Fp_powers(z, f, el)+1, xi = cgetg(d+1, t_VEC);
    3009             : 
    3010       70462 :   for (i=1; i<=d; i++)
    3011             :   {
    3012             :     long j;
    3013       68789 :     GEN x = gen_1;
    3014     6756043 :     for (j=1; j<=h; j++)
    3015     6687254 :       x = Fp_mul(x, subiu(gel(vz, Fl_mul(H[j], gi, f)), 1), el);
    3016       68789 :     gel(xi, i) = x;
    3017       68789 :     gi = Fl_mul(gi, g, f);
    3018             :   }
    3019        1673 :   return gerepilecopy(av, xi);
    3020             : }
    3021             : 
    3022             : static GEN
    3023       10164 : pclgp_cyc_real(GEN K, GEN p, long max_pow, long flag)
    3024             : {
    3025       10164 :   const long NUM_EL = 20;
    3026       10164 :   GEN C = gel(K, 5);
    3027       10164 :   long f_K = K_get_f(K), n_conj = K_get_nconj(K);
    3028       10164 :   long i, pow, n_el, n_done = 0;
    3029       10164 :   GEN gr = nullvec(), Done = const_vecsmall(n_conj, 0), xi;
    3030       10164 :   long first = 1;
    3031             : 
    3032       10290 :   for (pow=1; pow<=max_pow; pow++)
    3033             :   {
    3034       10290 :     GEN pn = powiu(p, pow), fpn = muliu(pn, f_K), el = addiu(fpn, 1);
    3035      109753 :     for (n_el = 0; n_el < NUM_EL; el = addii(el, fpn))
    3036             :     {
    3037             :       ulong uel;
    3038      109627 :       if (!BPSW_psp(el)) continue;
    3039       15470 :       n_el++; uel = itou_or_0(el);
    3040       15470 :       if (uel)
    3041             :       {
    3042       13797 :         xi = xi_conj_s(K, uel);
    3043       13797 :         if (first && n_conj > 10) /* mark trivial chi-part */
    3044             :         {
    3045        8715 :           for (i = 1; i <= n_conj; i++)
    3046             :           {
    3047        8512 :             if (cyc_real_pre(K, xi, p[2], C[i], uel) == 1) continue;
    3048        8260 :             Done[i] = 1;
    3049        8260 :             if (++n_done == n_conj) return gr;
    3050             :           }
    3051         203 :           first = 0; continue;
    3052             :         }
    3053             :       }
    3054             :       else
    3055        1673 :         xi = xi_conj_l(K, el);
    3056       55132 :       for (i = 1; i <= n_conj; i++)
    3057             :       {
    3058             :         GEN z;
    3059       50029 :         if (Done[i]) continue;
    3060       31724 :         if (uel)
    3061       26579 :           z = cyc_real_ss(K, xi, p[2], C[i], pow, uel, itou(pn), flag);
    3062             :         else
    3063        5145 :           z = cyc_real_ll(K, xi, p, C[i], pow, el, pn, flag);
    3064       31724 :         if (!z) continue;
    3065       26418 :         Done[i] = 1;
    3066       26418 :         if (!isintzero(gel(z, 1))) gr = vec_append(gr, z);
    3067       26418 :         if (++n_done == n_conj) return gr;
    3068             :       }
    3069             :     }
    3070             :   }
    3071           0 :   pari_err_BUG("pclgp_cyc_real: max_pow is not enough");
    3072             :   return NULL; /*LCOV_EXCL_LINE*/
    3073             : }
    3074             : 
    3075             : /* return (el, g_el) */
    3076             : static GEN
    3077          56 : next_el_imag(GEN elg, long f)
    3078             : {
    3079          56 :   long el = elg[1];
    3080          56 :   if (odd(f)) f<<=1;
    3081         140 :   while (!uisprime(el+=f));
    3082          56 :   return mkvecsmall2(el, pgener_Fl(el));
    3083             : }
    3084             : 
    3085             : /* return (ell, g_ell) */
    3086             : static GEN
    3087          70 : next_ell_imag(GEN ellg, GEN df0l)
    3088             : {
    3089          70 :   GEN ell = gel(ellg, 1);
    3090         770 :   while (!BPSW_psp(ell = addii(ell, df0l)));
    3091          70 :   return mkvec2(ell, pgener_Fp(ell));
    3092             : }
    3093             : 
    3094             : static GEN
    3095          56 : set_ell_imag(GEN velg, long n, long d_chi, GEN df0)
    3096             : {
    3097          56 :   long i, n_ell = n*d_chi;
    3098          56 :   GEN z = cgetg(n_ell + 1, t_VEC);
    3099          56 :   GEN df0l = shifti(df0, 1), ellg = mkvec2(gen_1, gen_1);
    3100         126 :   for (i=1; i<=n; i++) df0l = muliu(df0l, gel(velg, i)[1]);
    3101         126 :   for (i=1; i<=n_ell; i++) ellg = gel(z, i)= next_ell_imag(ellg, df0l);
    3102          56 :   return z;
    3103             : }
    3104             : 
    3105             : /* U(X)=u(x)+u(X)*X^f+...+f(X)*X^((m-1)f) or u(x)-u(X)*X^f+...
    3106             :  * U(X)V(X)=u(X)V(X)(1+X^f+...+X^((m-1)f))
    3107             :  *         =w_0+w_1*X+...+w_{f+el-3}*X^(f+el-3)
    3108             :  * w_i (1 <= i <= f+el-2) are needed.
    3109             :  * w_{f+el-2}=0 if el-1 == f.
    3110             :  * W_i = w_i + w_{i+el-1} (1 <= i <= f-1). */
    3111             : static GEN
    3112          85 : gauss_Flx_mul(ulong f, GEN elg, GEN ellg)
    3113             : {
    3114          85 :   pari_sp av = avma;
    3115          85 :   ulong el = elg[1], g_el= elg[2];
    3116          85 :   ulong el_1 = el-1, f2 = f<<1, lv = el_1, lu = f, m = el_1/f;
    3117          85 :   ulong ell = itou(gel(ellg, 1)), g_ell = itou(gel(ellg, 2));
    3118          85 :   ulong z_2f = Fl_powu(g_ell, (ell - 1) / f2, ell);
    3119          85 :   ulong z_el = Fl_powu(g_ell, (ell - 1) / el, ell);
    3120             :   ulong i, i2, gi;
    3121          85 :   GEN W = cgetg(f+1, t_VECSMALL), vz_2f, vz_el;
    3122          85 :   GEN u = cgetg(lu+2, t_VECSMALL), v = cgetg(lv+2, t_VECSMALL), w0;
    3123             : 
    3124          85 :   u[1] = evalsigne(1);
    3125          85 :   v[1] = evalsigne(1);
    3126          85 :   vz_2f = Fl_powers(z_2f, f2-1, ell);
    3127          85 :   vz_el = Fl_powers(z_el, el_1, ell);
    3128      528459 :   for (i=i2=0; i<lu; i++)
    3129             :   {
    3130             :     long j2; /* i2=(i*i)%f2, gi=g_el^i */
    3131      528374 :     j2 = i2?f2-i2:i2;
    3132      528374 :     u[2+i] = vz_2f[1+j2];
    3133      528374 :     if ((i2+=i+i+1)>=f2) i2-=f2; /* same as i2%=f2 */
    3134             :   }
    3135     1600537 :   for (gi=1,i=i2=0; i<lv; i++)
    3136             :   {
    3137     1600452 :     v[2+i] = Fl_mul(vz_2f[1+i2], vz_el[1+gi], ell);
    3138     1600452 :     gi = Fl_mul(gi, g_el, el);
    3139     1600452 :     if ((i2+=i+i+1)>=f2) i2%=f2; /* i2-=f2 does not work */
    3140             :   }
    3141          85 :   w0 = Flx_mul(u, v, ell) + 1;
    3142          85 :   if (m==1)
    3143             :   { /* el_1=f */
    3144           0 :     for (i=1; i<f; i++) W[i] = Fl_add(w0[i], w0[i+lv], ell);
    3145           0 :     W[f] = w0[f];
    3146             :   }
    3147             :   else
    3148             :   {
    3149          85 :     ulong start = 1+f, end = f+el-1;
    3150          85 :     GEN w = cgetg(end+1, t_VECSMALL);
    3151     2128826 :     for (i=1; i<end; i++) w[i] = w0[i];
    3152          85 :     w[end] = 0;
    3153         360 :     for (i=1; i<m; i++, start+=f)
    3154         550 :       w = both_odd(f,i)? Flv_shift_sub(w, w0, ell, start, end)
    3155         275 :                        : Flv_shift_add(w, w0, ell, start, end);
    3156      528459 :     for (i=0; i<f; i++) W[1+i] = Fl_add(w[1+i], w[1+i+lv], ell);
    3157             :   }
    3158      528374 :   for (i=i2=1; i<f; i++)
    3159             :   {
    3160      528289 :     W[i]=Fl_mul(W[1+i], vz_2f[1+i2], ell);
    3161      528289 :     if ((i2+=i+i+1)>=f2) i2%=f2;
    3162             :   }
    3163             :   /* W[r]=tau_{LL}^{sigma_r}, 1<= r <= f-1 */
    3164          85 :   return gerepilecopy(av, Flv_to_ZV(W));
    3165             : }
    3166             : 
    3167             : static GEN
    3168          90 : gauss_ZX_mul(ulong f, GEN elg, GEN ellg)
    3169             : {
    3170          90 :   pari_sp av = avma, av2;
    3171             :   ulong el, g_el, el_1, f2, lv, lu, m, i, i2, gi;
    3172          90 :   GEN  ell = gel(ellg, 1), g_ell, ell_1, z_2f, z_el, W, vz_2f, vz_el, u, v, w0;
    3173             : 
    3174          90 :   if (lgefint(ell) == 3) return gauss_Flx_mul(f, elg, ellg);
    3175           5 :   g_ell = gel(ellg, 2); ell_1 = subiu(ell, 1);
    3176           5 :   el = elg[1]; g_el = elg[2]; el_1 = el-1;
    3177           5 :   f2 = f<<1; lv=el_1; lu=f; m=el_1/f;
    3178           5 :   z_2f = Fp_pow(g_ell, diviuexact(ell_1, f2), ell);
    3179           5 :   vz_2f = Fp_powers(z_2f, f2-1, ell);
    3180           5 :   W = cgetg(f+1, t_VEC);
    3181           5 :   av2 = avma;
    3182           5 :   z_el = Fp_pow(g_ell, diviuexact(ell_1, el), ell);
    3183           5 :   vz_el = Fp_powers(z_el, el_1, ell);
    3184           5 :   u = cgetg(lu+2, t_POL); u[1] = evalsigne(1) | evalvarn(0);
    3185           5 :   v = cgetg(lv+2, t_POL); v[1] = evalsigne(1) | evalvarn(0);
    3186       35264 :   for (gi=1,i=i2=0; i<lu; i++)
    3187             :   {
    3188             :     long j2; /* i2=(i*i)%f2, gi=g_el^i */
    3189       35259 :     j2 = i2?f2-i2:i2;
    3190       35259 :     gel(u, 2+i) = gel(vz_2f, 1+j2);
    3191       35259 :     if ((i2+=i+i+1)>=f2) i2-=f2;
    3192             :   }
    3193       82787 :   for (gi=1,i=i2=0; i<lv; i++)
    3194             :   {
    3195       82782 :     gel(v, 2+i) = Fp_mul(gel(vz_2f, 1+i2), gel(vz_el, 1+gi), ell);
    3196       82782 :     gi = Fl_mul(gi, g_el, el);
    3197       82782 :     if ((i2+=i+i+1)>=f2) i2%=f2;
    3198             :   }
    3199           5 :   w0 = gerepileupto(av2, FpX_mul(u, v, ell)) + 1; av2 = avma;
    3200           5 :   if (m==1)
    3201             :   {
    3202           0 :     for (i=1; i < f; i++) gel(W,i) = Fp_add(gel(w0, i), gel(w0, i+lv), ell);
    3203           0 :     gel(W, f) = gel(w0, f);
    3204             :   }
    3205             :   else
    3206             :   {
    3207           5 :     ulong start = 1+f, end = f+el-1;
    3208           5 :     GEN w = cgetg(end+1, t_VEC);
    3209      118041 :     for (i=1; i<end; i++) gel(w, i) = gel(w0, i);
    3210           5 :     gel(w, end) = gen_0;
    3211          15 :     for (i=1; i<m; i++, start+=f)
    3212             :     {
    3213          13 :       w = both_odd(f,i)? FpV_shift_sub(w, w0, ell, start, end)
    3214          10 :                        : FpV_shift_add(w, w0, ell, start, end);
    3215          10 :       if ((i & 7) == 0) w = gerepilecopy(av2, w);
    3216             :     }
    3217       35264 :     for (i = 1; i <= f; i++) gel(W, i) = addii(gel(w, i), gel(w, i+lv));
    3218             :   }
    3219       35259 :   for (i = i2 = 1; i < f; i++)
    3220             :   {
    3221       35254 :     gel(W, i) = Fp_mul(gel(W, 1+i), gel(vz_2f, 1+i2), ell);
    3222       35254 :     if ((i2+=i+i+1) >= f2) i2 %= f2;
    3223             :   }
    3224           5 :   return gerepilecopy(av, W);  /* W[r]=tau_{LL}^{sigma_r}, 1<= r <= f-1 */
    3225             : }
    3226             : 
    3227             : /* fast but consumes memory */
    3228             : static GEN
    3229           4 : gauss_el_vell(ulong f, GEN elg, GEN vellg, GEN vz_2f)
    3230             : {
    3231           4 :   pari_sp av = avma, av2;
    3232           4 :   ulong el = elg[1], g_el = elg[2], el_1 = el-1;
    3233           4 :   ulong lv=el_1, f2=f<<1, lu=f, m=el_1/f;
    3234           4 :   GEN W = cgetg(f+1, t_VEC), vz_el, u, v, w0, M;
    3235             :   ulong i, i2, gi;
    3236             : 
    3237           4 :   av2 = avma;
    3238           4 :   vz_el = vz_vell(el, vellg, &M);
    3239           4 :   u = cgetg(lu+2, t_POL); u[1] = evalsigne(1) | evalvarn(0);
    3240           4 :   v = cgetg(lv+2, t_POL); v[1] = evalsigne(1) | evalvarn(0);
    3241       25554 :   for (i=i2=0; i<lu; i++)
    3242             :   {
    3243             :     long j2; /* i2=(i*i)%f2, gi=g_el^i */
    3244       25550 :     j2 = i2?f2-i2:i2;
    3245       25550 :     gel(u, 2+i) = gel(vz_2f, 1+j2);
    3246       25550 :     if ((i2+=i+i+1)>=f2) i2%=f2;
    3247             :   }
    3248       86874 :   for (gi=1,i=i2=0; i<lv; i++)
    3249             :   {
    3250       86870 :     gel(v, 2+i) = Fp_mul(gel(vz_2f, 1+i2), gel(vz_el, 1+gi), M);
    3251       86870 :     gi = Fl_mul(gi, g_el, el);
    3252       86870 :     if ((i2+=i+i+1)>=f2) i2%=f2;
    3253             :   }
    3254           4 :   M = gclone(M);
    3255           4 :   w0 = gerepileupto(av2, FpX_mul(u, v, M)) + 1;
    3256           4 :   u = M; M = icopy(M); gunclone(u);
    3257           4 :   av2 = avma;
    3258           4 :   if (m==1)
    3259             :   { /* el_1=f */
    3260           0 :     for (i=1; i < f; i++) gel(W,i) = Fp_add(gel(w0, i), gel(w0, i+lv), M);
    3261           0 :     gel(W, f) = gel(w0, f);
    3262             :   }
    3263             :   else
    3264             :   {
    3265           4 :     ulong start = 1+f, end = f+el-1;
    3266           4 :     GEN w = cgetg(end+1, t_VEC);
    3267      112420 :     for (i=1; i<end; i++) gel(w, i) = gel(w0, i);
    3268           4 :     gel(w, end) = gen_0;
    3269          19 :     for (i=1; i<m; i++, start+=f)
    3270             :     {
    3271          22 :       w = both_odd(f,i)? FpV_shift_sub(w, w0, M, start, end)
    3272          15 :                        : FpV_shift_add(w, w0, M, start, end);
    3273          15 :       if ((i & 7) == 0) w = gerepilecopy(av2, w);
    3274             :     }
    3275       25554 :     for (i = 1; i <= f; i++) gel(W, i) = Fp_add(gel(w, i), gel(w, i+lv), M);
    3276             :   }
    3277       25550 :   for (i = i2 = 1; i < f; i++)
    3278             :   {
    3279       25546 :     gel(W, i) = Fp_mul(gel(W, 1+i), gel(vz_2f, 1+i2), M);
    3280       25546 :     if ((i2+=i+i+1) >= f2) i2 %= f2;
    3281             :   }
    3282           4 :   return gerepilecopy(av, W);  /* W[r]=tau_{LL}^{sigma_r}, 1<= r <= f-1 */
    3283             : }
    3284             : 
    3285             : static GEN
    3286          94 : norm_chi(GEN K, GEN TAU, ulong p, long d_pow, GEN ell, long j0)
    3287             : {
    3288          94 :   pari_sp av = avma;
    3289          94 :   GEN H = K_get_H(K);
    3290          94 :   ulong d_K, f_K = K_get_f(K), h = K_get_h(K), g_K = K_get_g(K);
    3291          94 :   ulong i, j, gi, pd = upowuu(p, d_pow), d_chi = K_get_dchi(K);
    3292          94 :   GEN z = const_vec(d_chi, gen_1);
    3293          94 :   GEN e_chi = get_e_chi(K, j0, pd, &d_K);
    3294             : 
    3295        1420 :   for (gi=1, i=0; i<d_K; i++)
    3296             :   {
    3297        1326 :     GEN y = gen_1;
    3298      230862 :     for (j=1; j<=h; j++)
    3299      229536 :       y = Fp_mul(y, gel(TAU, Fl_mul(gi, H[j], f_K)), ell);
    3300        1326 :     gi = Fl_mul(gi, g_K, f_K);
    3301        2652 :     for (j=1; j<=d_chi; j++)
    3302             :     {
    3303        1326 :       GEN y2 = Fp_powu(y, e_chi[(i+j-1)%d_K], ell);
    3304        1326 :       gel(z, j) = Fp_mul(gel(z, j), y2, ell);
    3305             :     }
    3306             :   }
    3307          94 :   return gerepilecopy(av, z);
    3308             : }
    3309             : 
    3310             : static void
    3311           2 : imag_MLLn(long *y, GEN K, ulong p, long d_pow, long n,
    3312             :     GEN velg, GEN vellg, long j0)
    3313             : {
    3314           2 :   long f = K_get_f(K), d = upowuu(p, d_pow), row = lg(vellg)-1, i, j, k, nz;
    3315           2 :   GEN g, z, M, vz_2f = vz_vell(f << 1, vellg, &M);
    3316           6 :   for (i=1; i<=n; i++)
    3317             :   {
    3318           4 :     pari_sp av = avma;
    3319           4 :     GEN elg = gel(velg, i);
    3320           4 :     if (DEBUGLEVEL>1) err_printf("(f,el-1)=(%ld,%ld*%ld)\n", f,(elg[1]-1)/f,f);
    3321           4 :     g = gauss_el_vell(f, elg, vellg, vz_2f);
    3322           4 :     z = norm_chi(K, g, p, d_pow, M, j0);
    3323           4 :     nz = lg(z)-1;
    3324           8 :     for (k = 1; k <= nz; k++)
    3325          12 :       for (j = 1; j <= row; j++)
    3326           8 :         y[(j-1)*row+(i-1)*nz+k-1] = get_y(gel(z, k), gel(vellg, j), d);
    3327           4 :     set_avma(av);
    3328             :   }
    3329           2 : }
    3330             : 
    3331             : static void
    3332          42 : imag_MLL1(long *y, GEN K, ulong p, long d_pow, GEN velg, GEN vellg, long j0)
    3333             : {
    3334          42 :   long f = K_get_f(K), d = upowuu(p, d_pow);
    3335          42 :   GEN elg = gel(velg, 1), ellg = gel(vellg, 1), ell = gel(ellg, 1), g, z;
    3336             : 
    3337          42 :   if (DEBUGLEVEL>1) err_printf("(f,el-1)=(%ld,%ld*%ld)\n", f, (elg[1]-1)/f, f);
    3338          42 :   g = gauss_ZX_mul(f, elg, ellg);
    3339          42 :   z = norm_chi(K, g, p, d_pow, ell, j0);
    3340          42 :   y[0] = get_y(gel(z, 1), ellg, d);
    3341          42 : }
    3342             : 
    3343             : static void
    3344          12 : imag_MLL(long *y, GEN K, ulong p, long d_pow, long n, GEN velg, GEN vellg,
    3345             :     long j0)
    3346             : {
    3347          12 :   pari_sp av = avma;
    3348          12 :   long i, j, f = K_get_f(K), d = upowuu(p, d_pow), row = lg(vellg)-1;
    3349             : 
    3350          36 :   for (j=1; j<=row; j++)
    3351             :   {
    3352          24 :     GEN ellg = gel(vellg, j), ell = gel(ellg, 1);
    3353          72 :     for (i=1; i<=n; i++)
    3354             :     {
    3355          48 :       GEN elg = gel(velg, i), g, z;
    3356             :       ulong k, nz;
    3357          48 :       if (DEBUGLEVEL>1) err_printf("(f,el-1)=(%ld,%ld*%ld)\n",f,(elg[1]-1)/f,f);
    3358          48 :       g = gauss_ZX_mul(f, elg, ellg);
    3359          48 :       z = norm_chi(K, g, p, d_pow, ell, j0);
    3360          48 :       nz = lg(z)-1;
    3361          96 :       for (k = 1; k <= nz; k++)
    3362          48 :         y[(j-1)*row+(i-1)*nz+k-1] = get_y(gel(z, k), ellg, d);
    3363          48 :       set_avma(av);
    3364             :     }
    3365             :   }
    3366          12 : }
    3367             : 
    3368             : /* return an upper bound >= 0 if one was found, otherwise return -1.
    3369             :  * set chi-part to be (1) if chi is Teichmuller character.
    3370             :  * B_{1,omega^(-1)} is not p-adic integer. */
    3371             : static GEN
    3372          42 : cyc_imag_MLL(GEN K, ulong p, long d_pow, long j, long flag)
    3373             : {
    3374          42 :   long f = K_get_f(K), d_chi = K_get_dchi(K);
    3375          42 :   long n, n0 = 1, n_el = d_pow, d = upowuu(p, d_pow), rank = n_el*d_chi;
    3376          42 :   GEN df0, velg = const_vec(n_el, NULL), vellg = NULL;
    3377          42 :   GEN oldgr = mkvec2(gen_0, NULL), newgr = mkvec2(gen_0, NULL);
    3378          42 :   long *y0 = (long*)stack_calloc(sizeof(long)*rank*rank);
    3379             : 
    3380          42 :   if (DEBUGLEVEL>1)
    3381           0 :     err_printf("cyc_imag_MLL:p=%ld d_pow=%ld deg(K)=%ld cond(K)=%ld avma=%ld\n",
    3382             :         p, d_pow, K_get_d(K), f, avma);
    3383          42 :   df0 = muluu(d, f%p?f:f/p);
    3384          42 :   gel(velg, 1) = next_el_imag(mkvecsmall2(1, 1), f);
    3385          42 :   if (flag&USE_FULL_EL)
    3386             :   {
    3387           0 :     for (n=2; n<=n_el; n++) gel(velg, n) = next_el_imag(gel(velg, n-1), f);
    3388           0 :     n0 = n_el;
    3389             :   }
    3390          56 :   for (n=n0; n<=n_el; n++) /* loop while structure is unknown */
    3391             :   {
    3392          56 :     pari_sp av2 = avma;
    3393             :     pari_timer ti;
    3394             :     long n_ell, m, M;
    3395             :     GEN y;
    3396          56 :     vellg = set_ell_imag(velg, n, d_chi, df0);
    3397          56 :     n_ell = lg(vellg)-1;  /* equal to n*d_chi */
    3398          56 :     if (DEBUGLEVEL>2) err_printf("velg=%Ps\nvellg=%Ps\n", velg, vellg);
    3399          56 :     if (DEBUGLEVEL>2) timer_start(&ti);
    3400          56 :     if (n_ell==1)
    3401          42 :       imag_MLL1(y0, K, p, d_pow, velg, vellg, j);
    3402          14 :     else if (lgefint(gmael(vellg, n, 1))<=3 || (flag&SAVE_MEMORY))
    3403          12 :       imag_MLL(y0, K, p, d_pow, n, velg, vellg, j);
    3404             :     else
    3405           2 :       imag_MLLn(y0, K, p, d_pow, n, velg, vellg, j);
    3406          56 :     set_avma(av2);
    3407          56 :     if (DEBUGLEVEL>2) timer_printf(&ti, "gauss sum");
    3408          56 :     y = ary2mat(y0, n_ell);
    3409          56 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    3410          56 :     y = ZM_snf(y);
    3411          56 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    3412          56 :     y = make_p_part(y, p, d_pow);
    3413          56 :     if (DEBUGLEVEL>3) err_printf("y=%Ps\n", y);
    3414          56 :     newgr = structure_MLL(y, d_pow);
    3415          56 :     if (DEBUGLEVEL>3)
    3416           0 :       err_printf("d_pow=%ld d_chi=%ld old=%Ps new=%Ps\n",d_pow,d_chi,oldgr,newgr);
    3417          56 :     if (equalsi(d_pow*d_chi, gel(newgr, 1))) break;
    3418          14 :     if ((m = find_del_el(&oldgr, newgr, n, n_el, d_chi)))
    3419           0 :     { M = m = delete_el(velg, m); n--; }
    3420             :     else
    3421          14 :     { M = n+1; m = n; }
    3422          14 :     gel(velg, M) = next_el_imag(gel(velg, m), f);
    3423             :   }
    3424          42 :   return get_str(newgr);
    3425             : }
    3426             : 
    3427             : /* When |A_psi|=p^e, A_psi=(p^e1,...,p^er) (psi=chi^j),
    3428             :  *  return vec[e, [e1, ... ,er], 1].
    3429             :  * If gr str is not determined, return vec[e, [], 1].
    3430             :  * If |A_chi|=1, return vec[0, [], 1].
    3431             :  * If |A_chi|=p, return vec[1, [1], 1].
    3432             :  * If e is not determined, return vec[-1, [], 1].
    3433             :  * If psi is Teichmuller, return vec[0, [], 1].
    3434             :  * B_{1,omega^(-1)} is not p-adic integer. */
    3435             : static GEN
    3436       26334 : cyc_imag(GEN K, GEN B, GEN p, long j, GEN powp, long flag)
    3437             : {
    3438       26334 :   pari_sp av = avma;
    3439       26334 :   GEN MinPol = gel(K, 3), Chi = gel(K, 2), B1, B2, gr;
    3440       26334 :   long x, d_K = K_get_d(K), f_K = K_get_f(K), d_chi = K_get_dchi(K);
    3441             : 
    3442       26334 :   if (f_K == d_K+1 && equaliu(p, f_K) && j == 1) /* Teichmuller */
    3443          77 :     return mkvec3(gen_0, nullvec(), gen_1);
    3444       26257 :   B1 = FpX_rem(ZX_ber_conj(B, j, d_K), MinPol, powp);
    3445       26257 :   B2 = FpX_rem(ZX_ber_den(Chi, j, d_K), MinPol, powp);
    3446       26257 :   if (degpol(B1)<0 || degpol(B2)<0)
    3447             :   {
    3448           0 :     set_avma(av);
    3449           0 :     return mkvec3(gen_m1, nullvec(), gen_1); /* B=0(mod p^pow) */
    3450             :   }
    3451       26257 :   x = ZX_pval(B1, p) - ZX_pval(B2, p);
    3452       26257 :   set_avma(av);
    3453       26257 :   if (x<0) pari_err_BUG("subcyclopclgp [Bernoulli number]");
    3454       26257 :   if (DEBUGLEVEL && x) verbose_output(K, p, x, j);
    3455       26257 :   if (x==0) return mkvec3(gen_0, nullvec(), gen_1); /* trivial */
    3456         588 :   if (x==1) return mkvec3(utoi(d_chi), onevec(d_chi), gen_1);
    3457         140 :   if ((flag&USE_MLL)==0) return mkvec3(utoi(x*d_chi), nullvec(), gen_1);
    3458          42 :   gr = d_K == 2? cyc_buch(-f_K, p, x): cyc_imag_MLL(K, itou(p), x, j, flag);
    3459          42 :   return gerepilecopy(av, mkvec3(utoipos(d_chi * x), gr, gen_1));
    3460             : }
    3461             : 
    3462             : /* handle representatives of all injective characters, d_chi=[Q_p(zeta_d):Q_p],
    3463             :  * d=d_K */
    3464             : static GEN
    3465       10080 : pclgp_cyc_imag(GEN K, GEN p, long start_pow, long max_pow, long flag)
    3466             : {
    3467       10080 :   GEN C = gel(K, 5), Chi = gel(K, 2);
    3468       10080 :   long n_conj = K_get_nconj(K), d_K = K_get_d(K), f_K = K_get_f(K);
    3469       10080 :   long i, pow, n_done = 0;
    3470       10080 :   GEN gr = nullvec(), Done = const_vecsmall(n_conj, 0);
    3471       10080 :   GEN B = zx_ber_num(Chi, f_K, d_K), B_num;
    3472             : 
    3473       10080 :   if (lgefint(p)==3 && n_conj>10) /* mark trivial chi-part by pre-calculation */
    3474             :   {
    3475         595 :     ulong up = itou(p);
    3476         595 :     GEN minpol = ZX_to_Flx(gel(K, 3), up);
    3477        7350 :     for (i=1; i<=n_conj; i++)
    3478             :     {
    3479        7168 :       pari_sp av = avma;
    3480             :       long degB;
    3481        7168 :       B_num = Flx_rem(Flx_ber_conj(B, C[i], d_K, up), minpol, up);
    3482        7168 :       degB = degpol(B_num);
    3483        7168 :       set_avma(av);
    3484        7168 :       if (degB<0) continue;
    3485        6937 :       Done[i] = 1;
    3486        6937 :       if (++n_done == n_conj) return gr;
    3487             :     }
    3488             :   }
    3489        9667 :   for (pow = start_pow; pow<=max_pow; pow++)
    3490             :   {
    3491        9667 :     GEN powp = powiu(p, pow);
    3492       27503 :     for (i = 1; i <= n_conj; i++)
    3493             :     {
    3494             :       GEN z;
    3495       27503 :       if (Done[i]) continue;
    3496       26334 :       z = cyc_imag(K, B, p, C[i], powp, flag);
    3497       26334 :       if (equalim1(gel(z, 1))) continue;
    3498       26334 :       Done[i] = 1;
    3499       26334 :       if (!isintzero(gel(z, 1))) gr = vec_append(gr, z);
    3500       26334 :       if (++n_done == n_conj) return gr;
    3501             :     }
    3502             :   }
    3503           0 :   pari_err_BUG("pclgp_cyc_imag: max_pow is not enough");
    3504             :   return NULL; /*LCOV_EXCL_LINE*/
    3505             : }
    3506             : 
    3507             : static GEN
    3508         392 : gather_part(GEN g, long sgn)
    3509             : {
    3510         392 :   long i, j, l = lg(g), ord = 0, flag = 1;
    3511         392 :   GEN z2 = cgetg(l, t_VEC);
    3512        1778 :   for (i = j = 1; i < l; i++)
    3513             :   {
    3514        1386 :     GEN t = gel(g,i);
    3515        1386 :     if (equaliu(gel(t, 3), sgn))
    3516             :     {
    3517         693 :       ord += itou(gel(t, 1));
    3518         693 :       if (lg(gel(t, 2)) == 1) flag = 0;
    3519         693 :       gel(z2, j++) = gel(t, 2);
    3520             :     }
    3521             :   }
    3522         392 :   if (flag==0 || ord==0) z2 = nullvec();
    3523             :   else
    3524             :   {
    3525         126 :     setlg(z2, j); z2 = shallowconcat1(z2);
    3526         126 :     ZV_sort_inplace(z2); vecreverse_inplace(z2);
    3527             :   }
    3528         392 :   return mkvec2(utoi(ord), z2);
    3529             : }
    3530             : 
    3531             : #ifdef DEBUG
    3532             : static void
    3533             : handling(GEN K)
    3534             : {
    3535             :   long d_K = K_get_d(K), f_K = K_get_f(K), s_K = K_get_s(K), g_K = K_get_g(K);
    3536             :   long d_chi = K_get_dchi(K);
    3537             :   err_printf("  handling %s cyclic subfield K,\
    3538             :       deg(K)=%ld, cond(K)=%ld g_K=%ld d_chi=%ld H=%Ps\n",
    3539             :       s_K? "a real": "an imaginary",d_K,f_K,g_K,d_chi,zv_to_ZV(gmael3(K,1,1,1)));
    3540             : }
    3541             : #endif
    3542             : 
    3543             : /* HH a t_VECSMALL listing group generators
    3544             :  * Aoki and Fukuda, LNCS vol.4076 (2006), 56-74. */
    3545             : static GEN
    3546         161 : pclgp(GEN p0, long f, GEN HH, long degF, long flag)
    3547             : {
    3548             :   long start_pow, max_pow, ip, lp, i, n_f;
    3549         161 :   GEN vH1, z, vData, cycGH, vp = typ(p0) == t_INT? mkvec(p0): p0;
    3550             : 
    3551         161 :   vH1 = GHinit(f, HH, &cycGH); n_f = lg(vH1)-1;
    3552             : #ifdef DEBUG
    3553             :   err_printf("F is %s, deg(F)=%ld, ", srh_1(HH)? "real": "imaginary", degF);
    3554             :   err_printf("cond(F)=%ld, G(F/Q)=%Ps\n",f, cycGH);
    3555             :   err_printf("F has %ld cyclic subfield%s except for Q.\n", n_f,n_f>1?"s":"");
    3556             : #endif
    3557             : 
    3558         161 :   lp = lg(vp); z = cgetg(lp, t_MAT);
    3559         357 :   for (ip = 1; ip < lp; ip++)
    3560             :   {
    3561         196 :     pari_sp av = avma;
    3562         196 :     long n_sub=0, n_chi=0;
    3563         196 :     GEN gr=nullvec(), p = gel(vp, ip), zi;
    3564             :     /* find conductor e of cyclic subfield K and set the subgroup HE of (Z/eZ)^*
    3565             :      * corresponding to K */
    3566         196 :     set_p_f(p, f, &start_pow, &max_pow);
    3567         196 :     vData = const_vec(degF, NULL);
    3568             : 
    3569       16982 :     for (i=1; i<=n_f; i++) /* prescan. set Teichmuller */
    3570             :     {
    3571       16863 :       GEN H1 = gel(vH1, i);
    3572       16863 :       long d_K = _get_d(H1), f_K = _get_f(H1), g_K = _get_g(H1);
    3573             : 
    3574       16863 :       if (f_K == d_K+1 && equaliu(p, f_K)) /* found K=Q(zeta_p) */
    3575             :       {
    3576             :         pari_timer ti;
    3577          77 :         GEN pnmax = powiu(p, max_pow), vNewton, C, MinPol;
    3578          77 :         long d_chi = 1, n_conj = eulerphiu(d_K);
    3579          77 :         ulong pmodd = umodiu(p, d_K);
    3580             : 
    3581          77 :         C = set_C(pmodd, d_K, d_chi, n_conj);
    3582          77 :         MinPol = set_minpol_teich(g_K, p, max_pow);
    3583          77 :         if (DEBUGLEVEL>3) timer_start(&ti);
    3584          77 :         vNewton = FpX_Newton(MinPol, d_K+1, pnmax);
    3585          77 :         if (DEBUGLEVEL>3)
    3586           0 :           timer_printf(&ti, "FpX_Newton: teich: %ld %ld", degpol(MinPol), d_K);
    3587          77 :         gel(vData, d_K) = mkvec4(MinPol, vNewton, C,
    3588             :                                  mkvecsmall2(d_chi, n_conj));
    3589          77 :         break;
    3590             :       }
    3591             :     }
    3592             : 
    3593       20440 :     for (i=1; i<=n_f; i++) /* handle all cyclic K */
    3594             :     {
    3595       20244 :       GEN H1 = gel(vH1, i), K, z1, Chi;
    3596       20244 :       long d_K = _get_d(H1), s_K = _get_s(H1);
    3597             :       pari_sp av2;
    3598             : 
    3599       20244 :       if ((flag&SKIP_PROPER) && degF != d_K) continue;
    3600       20244 :       if (!gel(vData, d_K))
    3601             :       {
    3602             :         pari_timer ti;
    3603         819 :         GEN pnmax = powiu(p, max_pow), vNewton, C, MinPol;
    3604         819 :         ulong pmodd = umodiu(p, d_K);
    3605         819 :         long d_chi = order_f_x(d_K, pmodd), n_conj = eulerphiu(d_K)/d_chi;
    3606             : 
    3607         819 :         C = set_C(pmodd, d_K, d_chi, n_conj);
    3608         819 :         MinPol = set_minpol(d_K, p, max_pow, n_conj);
    3609         819 :         if (DEBUGLEVEL>3) timer_start(&ti);
    3610             :         /* vNewton[2+i] = vNewton[2+i+d_K]. We need vNewton[2+i] for
    3611             :          * 0 <= i < d_K. But vNewton[2+d_K-1] may be 0 and will be deleted.
    3612             :          * So we need vNewton[2+d_K] not to delete vNewton[2+d_K-1]. */
    3613         819 :         vNewton = FpX_Newton(MinPol, d_K+1, pnmax);
    3614         819 :         if (DEBUGLEVEL>3)
    3615           0 :           timer_printf(&ti, "FpX_Newton: %ld %ld", degpol(MinPol), d_K);
    3616         819 :         gel(vData, d_K) = mkvec4(MinPol, vNewton, C,
    3617             :                                  mkvecsmall2(d_chi, n_conj));
    3618             :       }
    3619       20244 :       av2 = avma;
    3620       20244 :       Chi = s_K? NULL: get_chi(H1);
    3621       20244 :       K = shallowconcat(mkvec2(H1, Chi), gel(vData, d_K));
    3622             : #ifdef DEBUG
    3623             :       handling(K);
    3624             : #endif
    3625       20244 :       if (s_K && !(flag&NO_PLUS_PART))
    3626       10164 :         z1 = pclgp_cyc_real(K, p, max_pow, flag);
    3627       10080 :       else if (!s_K && !(flag&NO_MINUS_PART))
    3628       10080 :         z1 = pclgp_cyc_imag(K, p, start_pow, max_pow, flag);
    3629           0 :       else { set_avma(av2); continue; }
    3630       20244 :       n_sub++; n_chi += gmael(vData, d_K, 4)[2]; /* += n_conj */
    3631       20244 :       if (lg(z1) == 1) set_avma(av2);
    3632         658 :       else gr = gerepilecopy(av2, shallowconcat(gr, z1));
    3633             :     }
    3634         196 :     zi = mkcol(p);
    3635         196 :     zi = vec_append(zi, (flag&NO_PLUS_PART)?nullvec():gather_part(gr, 0));
    3636         196 :     zi = vec_append(zi, (flag&NO_MINUS_PART)?nullvec():gather_part(gr, 1));
    3637         196 :     zi = shallowconcat(zi, mkcol3(cycGH, utoi(n_sub), utoi(n_chi)));
    3638         196 :     gel(z, ip) = gerepilecopy(av, zi);
    3639             :   }
    3640         161 :   return typ(p0) == t_INT? shallowtrans(gel(z,1)): shallowtrans(z);
    3641             : }
    3642             : 
    3643             : static GEN
    3644         413 : reduce_gcd(GEN x1, GEN x2)
    3645             : {
    3646         413 :   GEN d = gcdii(x1, x2);
    3647         413 :   x1 = diviiexact(x1, d);
    3648         413 :   x2 = diviiexact(x2, d);
    3649         413 :   return mkvec2(x1, x2);
    3650             : }
    3651             : 
    3652             : /* norm of x0 (= pol of zeta_d with deg <= d-1) by g of order n
    3653             :  * x0^{1+g+g^2+...+g^(n-1)} */
    3654             : static GEN
    3655          49 : ber_norm_cyc(GEN x0, long g, long n, long d)
    3656             : {
    3657          49 :   pari_sp av = avma;
    3658          49 :   long i, ei, di, fi = 0, l = ulogint(n, 2);
    3659          49 :   GEN xi = x0;
    3660          49 :   ei = 1L << l; di = n / ei;
    3661         203 :   for (i = 1; i <= l; i++)
    3662             :   {
    3663         154 :     if (odd(di)) fi += ei;
    3664         154 :     ei = 1L << (l-i); di = n / ei;
    3665         154 :     xi = ZX_mod_Xnm1(ZX_mul(xi, ber_conj(xi, Fl_powu(g, ei, d), d)), d);
    3666         154 :     if (odd(di))
    3667          42 :       xi = ZX_mod_Xnm1(ZX_mul(xi, ber_conj(x0, Fl_powu(g, fi, d), d)), d);
    3668             :   }
    3669          49 :   return gerepilecopy(av, xi);
    3670             : }
    3671             : 
    3672             : /* x0 a ZX of deg < d */
    3673             : static GEN
    3674          21 : ber_norm_by_cyc(GEN x0, long d, GEN MinPol)
    3675             : {
    3676          21 :   pari_sp av=avma;
    3677          21 :   GEN x = x0, z = znstar(utoi(d)), cyc = gel(z, 2), gen = gel(z, 3);
    3678          21 :   long i, l = lg(cyc);
    3679             :   pari_timer ti;
    3680             : 
    3681          21 :   if (DEBUGLEVEL>1) timer_start(&ti);
    3682          70 :   for (i = 1; i < l; i++)
    3683          49 :     x = ber_norm_cyc(x, itou(gmael(gen, i, 2)), itou(gel(cyc, i)), d);
    3684          21 :   if (DEBUGLEVEL>1) timer_printf(&ti, "ber_norm_by_cyc [ber_norm_cyc]");
    3685          21 :   x = ZX_rem(x, MinPol);  /* slow */
    3686          21 :   if (DEBUGLEVEL>1) timer_printf(&ti, "ber_norm_by_cyc [ZX_rem]");
    3687          21 :   if (lg(x) != 3) pari_err_BUG("subcyclohminus [norm of Bernoulli number]");
    3688          21 :   return gerepilecopy(av, gel(x, 2));
    3689             : }
    3690             : 
    3691             : /* MinPol = polcyclo(d_K, 0).
    3692             :  * MinPol = fac*cofac (mod p).
    3693             :  * B is zv.
    3694             :  * K : H1, MinPol, [fac, cofac], C, [d_chi, n_conj] */
    3695             : static long
    3696          98 : ber_norm_by_val(GEN K, GEN B, GEN p)
    3697             : {
    3698          98 :   pari_sp av = avma;
    3699          98 :   GEN MinPol = gel(K, 2), C = gel(K, 4);
    3700          98 :   GEN vfac = gel(K, 3), fac = gel(vfac, 1), cofac = gel(vfac, 2);
    3701          98 :   long d_chi = K_get_dchi(K), n_conj = K_get_nconj(K), d_K = K_get_d(K);
    3702          98 :   long i, r, n_done = 0, x = 0, dcofac = degpol(cofac);
    3703             :   GEN pr, Done;
    3704             : 
    3705          98 :   Done = const_vecsmall(n_conj, 0);
    3706          98 :   if (lgefint(p)==3)
    3707             :   { /* mark trivial chi-part by pre-calculation */
    3708          98 :     ulong up = itou(p);
    3709          98 :     GEN facs = ZX_to_Flx(fac, up);
    3710         196 :     for (i = 1; i <= n_conj; i++)
    3711             :     {
    3712          98 :       pari_sp av2 = avma;
    3713          98 :       GEN B_conj = Flx_rem(Flx_ber_conj(B, C[i], d_K, up), facs, up);
    3714          98 :       long degB = degpol(B_conj);
    3715          98 :       set_avma(av2); if (degB < 0) continue;
    3716           0 :       Done[i] = 1; if (++n_done == n_conj) return gc_long(av, x);
    3717             :     }
    3718             :   }
    3719             :   else
    3720             :   {
    3721           0 :     for (i = 1; i <= n_conj; i++)
    3722             :     {
    3723           0 :       pari_sp av2 = avma;
    3724           0 :       GEN B_conj = FpX_rem(FpX_ber_conj(B, C[i], d_K, p), fac, p);
    3725           0 :       long degB = degpol(B_conj);
    3726           0 :       set_avma(av2); if (degB < 0) continue;
    3727           0 :       Done[i] = 1; if (++n_done == n_conj) return gc_long(av, x);
    3728             :     }
    3729             :   }
    3730         252 :   for (pr = p, r = 2; r; r <<= 1)
    3731             :   {
    3732             :     GEN polr;
    3733         252 :     pr = sqri(pr); /* p^r */
    3734         252 :     polr = (dcofac==0)? FpX_red(MinPol, pr)
    3735         252 :                       : gel(ZpX_liftfact(MinPol, vfac, pr, p, r), 1);
    3736         406 :     for (i = 1; i <= n_conj; i++)
    3737             :     {
    3738         252 :       pari_sp av2 = avma;
    3739             :       GEN B_conj;
    3740             :       long degB;
    3741         252 :       if (Done[i]) continue;
    3742         252 :       B_conj = FpX_rem(FpX_ber_conj(B, C[i], d_K, pr), polr, pr);
    3743         252 :       degB = degpol(B_conj);
    3744         252 :       set_avma(av2); if (degB < 0) continue;
    3745          98 :       x += d_chi * ZX_pval(B_conj, p);
    3746          98 :       Done[i] = 1; if (++n_done == n_conj) return gc_long(av, x);
    3747             :     }
    3748             :   }
    3749             :   pari_err_BUG("ber_norm_by_val"); return 0;/*LCOV_EXCL_LINE*/
    3750             : }
    3751             : 
    3752             : /* n > 2, p = odd prime not dividing n, e > 0, pe = p^e; d = n*p^e
    3753             :  * return generators of the subgroup H of (Z/dZ)^* corresponding to
    3754             :  * Q(zeta_{p^e}): H = {1<=a<=d | gcd(a,n)=1, a=1(mod p^e)} */
    3755             : static GEN
    3756           0 : znstar_subgr(ulong n, ulong pe, ulong d)
    3757             : {
    3758           0 :   GEN z = znstar(utoi(n)), g = gel(z, 3), G;
    3759           0 :   long i, l = lg(g);
    3760           0 :   G = cgetg(l, t_VECSMALL);
    3761           0 :   for (i=1; i<l; i++) G[i] = u_chinese_coprime(itou(gmael(g,i,2)), 1, n, pe, d);
    3762           0 :   return mkvec2(gel(z,2), G);
    3763             : }
    3764             : 
    3765             : /* K is a cyclic extension of degree n*p^e (n>=4 is even).
    3766             :  * x a ZX of deg < n*p^e. */
    3767             : static long
    3768           0 : ber_norm_with_val(GEN x, long n, ulong p, ulong e)
    3769             : {
    3770           0 :   pari_sp av = avma;
    3771           0 :   long i, j, r, degx, pe = upowuu(p, e), d = n*pe;
    3772           0 :   GEN z, gr, gen, y = cgetg(pe+2, t_POL), MinPol = polcyclo(n, 0);
    3773           0 :   y[1] = evalsigne(1) | evalvarn(0);
    3774           0 :   z = znstar_subgr(n, pe, d);
    3775           0 :   gr = gel(z, 1); gen = gel(z, 2); r = lg(gr)-1;
    3776           0 :   for (i=1; i<=r; i++)
    3777           0 :     x = ber_norm_cyc(x, itou(gel(gen, i)), itou(gel(gr, i)), d);
    3778           0 :   degx = degpol(x);
    3779           0 :   for (j=0; j<pe; j++)
    3780             :   {
    3781           0 :     GEN t = pol_zero(n), z;
    3782           0 :     long a = j; /* a=i*pe+j */
    3783           0 :     for (i=0; i<n; i++)
    3784             :     {
    3785           0 :       if (a>degx) break;
    3786           0 :       gel(t, 2+a%n) = gel(x, 2+a);
    3787           0 :       a += pe;
    3788             :     }
    3789           0 :     z = ZX_rem(ZX_renormalize(t, 2+n), MinPol);
    3790           0 :     if (degpol(z)<0) gel(y, 2+j) = gen_0;
    3791           0 :     else if (degpol(z)==0) gel(y, 2+j) = gel(z, 2);
    3792           0 :     else pari_err_BUG("ber_norm_subgr");
    3793             :   }
    3794           0 :   y = ZX_renormalize(y, pe+2);
    3795           0 :   if (e>1) y = ZX_rem(y, polcyclo(pe, 0));
    3796           0 :   return gc_long(av,  ZX_p_val(y, p, e));
    3797             : }
    3798             : 
    3799             : /* K is a cyclic extension of degree 2*p^e. x a ZX of deg < 2*p^e. In most
    3800             :  * cases, deg(x)=2*p^e-1. But deg(x) can be any value in [0, 2*p^e-1]. */
    3801             : static long
    3802         301 : ber_norm_with_val2(GEN x, ulong p, ulong e)
    3803             : {
    3804         301 :   pari_sp av = avma;
    3805         301 :   long i, d = degpol(x), pe = upowuu(p, e);
    3806         301 :   GEN y = pol_zero(pe);
    3807         301 :   if (d == 2*pe-1)
    3808             :   {
    3809       38416 :     for (i = 0; i < pe; i++)
    3810       76230 :       gel(y, 2+i) = odd(i)? subii(gel(x, 2+i+pe), gel(x, 2+i))
    3811       38115 :                           : subii(gel(x, 2+i), gel(x, 2+i+pe));
    3812             :   }
    3813             :   else
    3814             :   {
    3815           0 :     for (i = 0; i < pe && i <= d; i++)
    3816           0 :       gel(y, 2+i) = odd(i)? negi(gel(x, 2+i)): gel(x, 2+i);
    3817           0 :     for (i = pe; i <= d; i++)
    3818           0 :       gel(y, 2+i-pe) = odd(i)? subii(gel(y, 2+i-pe), gel(x, 2+i))
    3819           0 :                              : addii(gel(y, 2+i-pe), gel(x, 2+i));
    3820             :   }
    3821         301 :   y = ZX_renormalize(y, 2+pe);
    3822         301 :   if (e > 1) y = ZX_rem(y, polcyclo(pe, 0));
    3823         301 :   return gc_long(av, ZX_p_val(y, p, e));
    3824             : }
    3825             : 
    3826             : /* K : H1, MinPol, [fac, cofac], C, [d_chi, n_conj] */
    3827             : static GEN
    3828         812 : ber_cyc5(GEN K, GEN p)
    3829             : {
    3830         812 :   pari_sp av = avma;
    3831         812 :   GEN MinPol = gel(K, 2), H = K_get_H(K);
    3832         812 :   long d = K_get_d(K), f = K_get_f(K), h = K_get_h(K), g = K_get_g(K);
    3833         812 :   GEN x, x1, x2, y, B = const_vecsmall(d+1, 0);
    3834         812 :   long i, j, gi, e, f2 = f>>1, dMinPol = degpol(MinPol), chi2 = -1, *B2 = B+2;
    3835             : 
    3836             :   /* get_chi inlined here to save memory */
    3837    18111989 :   for (j=1; j<=h; j++) /* i = 0 */
    3838             :   {
    3839    18111177 :     if (H[j] == 2) chi2 = 0;
    3840    18111177 :     if (H[j] <= f2) B2[0]++; /* Chi[H[j]] = 0 */
    3841             :   }
    3842       97314 :   for (i = 1, gi = g; i < d; i++)
    3843             :   {
    3844    93017085 :     for (j=1; j<=h; j++)
    3845             :     {
    3846    92920583 :       long t = Fl_mul(gi, H[j], f); /* Chi[t] = i */
    3847    92920583 :       if (t == 2) chi2 = i;
    3848    92920583 :       if (t <= f2) B2[i]++;
    3849             :     }
    3850       96502 :     gi = Fl_mul(gi, g, f);
    3851             :   }
    3852         812 :   y = zx_to_ZX(zx_renormalize(B, d+2));
    3853             : 
    3854         812 :   if (p)
    3855             :   {
    3856             :     ulong n;
    3857         399 :     e = u_pvalrem(d, p, &n);
    3858         399 :     if (e == 0)
    3859          98 :       x1 = utoi(ber_norm_by_val(K, B, p));
    3860         301 :     else if (n > 2)
    3861           0 :       x1 = utoi(ber_norm_with_val(y, n, itou(p), e));
    3862             :     else
    3863         301 :       x1 = utoi(ber_norm_with_val2(y, itou(p), e));
    3864             :   }
    3865             :   else
    3866             :   {
    3867         413 :     if (dMinPol > 100)
    3868          21 :       x1 = ber_norm_by_cyc(y, d, MinPol);
    3869             :     else
    3870         392 :       x1 = ZX_resultant(MinPol, ZX_rem(y, MinPol));
    3871             :   }
    3872             : 
    3873         812 :   if (chi2 < 0) /* chi2 = Chi[2] */
    3874           0 :     x2 = shifti(gen_1, 2*dMinPol);
    3875         812 :   else if (chi2 == 0)
    3876          21 :     x2 = shifti(gen_1, dMinPol);
    3877             :   else
    3878             :   {
    3879         791 :     long e = d/ugcd(chi2, d);
    3880         791 :     x2 = powiu(polcyclo_eval(e, gen_2), eulerphiu(d)/eulerphiu(e));
    3881         791 :     x2 = shifti(x2, dMinPol);
    3882             :   }
    3883         812 :   if (p) x = stoi(itou(x1)-Z_pval(x2, p)); else x = reduce_gcd(x1, x2);
    3884         812 :   return gerepilecopy(av, x);
    3885             : }
    3886             : 
    3887             : /*  Hirabayashi-Yoshino, Manuscripta Math. vol.60, 423-436 (1988), Theorem 1
    3888             :  *
    3889             :  *  F is a subfield of Q(zeta_f)
    3890             :  *  f=p^a => Q=1
    3891             :  *  If F=Q(zeta_f), Q=1 <=> f=p^a
    3892             :  *  If f=4*p^a, p^a*q^b (p,q are odd primes), Q=2 <=> [Q(zeta_f):F] is odd */
    3893             : static long
    3894          21 : unit_index(ulong d, ulong f)
    3895             : {
    3896             :   ulong r, d_f;
    3897          21 :   GEN fa = factoru(f), P = gel(fa, 1), E = gel(fa, 2); r = lg(P)-1;
    3898          21 :   if (r==1) return 1;  /* f=P^a */
    3899           7 :   d_f = eulerphiu_fact(fa);
    3900           7 :   if (d==d_f) return 2;  /* F=Q(zeta_f) */
    3901           0 :   if (r==2 && ((P[1]==2 && E[1]==2) || P[1]>2)) return odd(d_f/d)?2:1;
    3902           0 :   return 0;
    3903             : }
    3904             : 
    3905             : /* Compute relative class number h of the subfield K of Q(zeta_f)
    3906             :  * corresponding to the subgroup HH of (Z/fZ)^*.
    3907             :  * If p!=NULL, then return valuation(h,p). */
    3908             : static GEN
    3909         119 : rel_class_num(long f, GEN HH, long degF, GEN p)
    3910             : {
    3911             :   long i, n_f, W, Q;
    3912         119 :   GEN vH1, vData, x, z = gen_1, z1 = gen_0, z2 = mkvec2(gen_1, gen_1);
    3913             : 
    3914         119 :   vH1 = GHinit(f, HH, NULL); n_f = lg(vH1)-1;
    3915         119 :   vData = const_vec(degF, NULL);
    3916        1652 :   for (i=1; i<=n_f; i++)
    3917             :   {
    3918        1533 :     GEN H1 = gel(vH1, i), K;
    3919        1533 :     long d_K = _get_d(H1), s = _get_s(H1);
    3920             : 
    3921        1533 :     if (s) continue;  /* F is real */
    3922             : #ifdef DEBUG
    3923             :     err_printf("  handling %s cyclic subfield K, deg(K)=%ld, cond(K)=%ld\n",
    3924             :         s? "a real": "an imaginary", d_K, _get_f(H1));
    3925             : #endif
    3926         812 :     if (!gel(vData, d_K))
    3927             :     {
    3928             :       GEN C, MinPol, fac, cofac;
    3929             :       ulong d_chi, n_conj;
    3930         497 :       MinPol = polcyclo(d_K,0);
    3931         497 :       if (p && umodui(d_K, p))
    3932          98 :       {
    3933          98 :         ulong pmodd = umodiu(p, d_K);
    3934          98 :         GEN MinPol_p = FpX_red(MinPol, p);
    3935          98 :         d_chi = order_f_x(d_K, pmodd);
    3936          98 :         n_conj = eulerphiu(d_K)/d_chi;
    3937          98 :         if (n_conj==1) fac = MinPol_p;  /* polcyclo(d_K) is irred mod p */
    3938           0 :         else fac = FpX_one_cyclo(d_K, p);
    3939          98 :         cofac = FpX_div(MinPol_p, fac, p);
    3940          98 :         C = set_C(pmodd, d_K, d_chi, n_conj);
    3941             :       }
    3942             :       else
    3943             :       {
    3944         399 :         fac = cofac = C = NULL;
    3945         399 :         d_chi = n_conj = 0;
    3946             :       }
    3947         497 :       gel(vData, d_K) = mkvec5(MinPol, mkvec2(fac, cofac), C,
    3948             :                                NULL, mkvecsmall2(d_chi, n_conj));
    3949             :     }
    3950         812 :     K = vec_prepend(gel(vData, d_K), H1);
    3951         812 :     z = ber_cyc5(K, p);
    3952         812 :     if (p) z1 = addii(z1, z);
    3953             :     else
    3954             :     {
    3955         413 :       gel(z2, 1) = mulii(gel(z2, 1), gel(z, 1));
    3956         413 :       gel(z2, 2) = mulii(gel(z2, 2), gel(z, 2));
    3957             :     }
    3958             :   }
    3959         119 :   W = root_of_1(f, HH);
    3960         119 :   if (p) return addiu(z1, z_pval(W, p));
    3961          21 :   Q = unit_index(degF, f);
    3962          21 :   x = dvmdii(muliu(gel(z2,1), 2 * W), gel(z2,2), &z1);
    3963          21 :   if (signe(z1)) pari_err_BUG("subcyclohminus [norm of Bernoulli number]");
    3964          21 :   if (!Q && mpodd(x)) Q = 2; /* FIXME: can this happen ? */
    3965          21 :   if (Q == 1) x = shifti(x, -1);
    3966          21 :   return mkvec2(x, utoi(Q));
    3967             : }
    3968             : 
    3969             : static void
    3970         336 : checkp(const char *fun, long degF, GEN p)
    3971             : {
    3972         336 :   if (!BPSW_psp(p)) pari_err_PRIME(fun, p);
    3973         329 :   if (equaliu(p, 2)) pari_err_DOMAIN(fun,"p","=", gen_2, p);
    3974         315 :   if (degF && dvdsi(degF, p)) errpdiv(fun, p, degF);
    3975         301 : }
    3976             : 
    3977             : /* if flag is set, handle quadratic fields specially (don't set H) */
    3978             : static long
    3979         427 : subcyclo_init(const char *fun, GEN FH, long *pdegF, GEN *pH, long flag)
    3980             : {
    3981         427 :   long f = 0, degF = 2;
    3982         427 :   GEN F = NULL, H = NULL;
    3983         427 :   if (typ(FH) == t_POL)
    3984             :   {
    3985          70 :     degF = degpol(FH);
    3986          70 :     if (degF < 1 || !RgX_is_ZX(FH)) pari_err_TYPE(fun, FH);
    3987          70 :     if (flag && degF == 2)
    3988             :     {
    3989          49 :       F = coredisc(ZX_disc(FH));
    3990          49 :       if (is_bigint(F))
    3991           0 :         pari_err_IMPL(stack_sprintf("conductor f > %lu in %s", ULONG_MAX, fun));
    3992          49 :       f = itos(F); if (f == 1) degF = 1;
    3993             :     }
    3994             :     else
    3995             :     {
    3996          21 :       GEN z, bnf = Buchall(pol_x(fetch_var()), 0, DEFAULTPREC);
    3997          21 :       z = rnfconductor(bnf, FH); H = gel(z,3);
    3998          21 :       f = subcyclo_nH(fun, gel(z,2), &H);
    3999          21 :       delete_var();
    4000          21 :       H = znstar_generate(f, H); /* group elements */
    4001             :     }
    4002             :   }
    4003             :   else
    4004             :   {
    4005         357 :     long l = lg(FH), fH;
    4006         357 :     if (typ(FH) == t_INT) F = FH;
    4007         273 :     else if (typ(FH) == t_VEC && (l == 2 || l == 3))
    4008             :     {
    4009         273 :       F = gel(FH, 1);
    4010         273 :       if (l == 3) H = gel(FH, 2);
    4011             :     }
    4012           0 :     else pari_err_TYPE(fun, FH);
    4013         357 :     f = subcyclo_nH(fun, F, &H);
    4014         350 :     H = znstar_generate(f, H); /* group elements */
    4015         350 :     fH = znstar_conductor(H);
    4016         350 :     if (fH == 1) degF = 1;
    4017             :     else
    4018             :     {
    4019         350 :       if (fH != f) { H = znstar_reduce_modulus(H, fH); f = fH; }
    4020         350 :       degF = eulerphiu(f) / zv_prod(gel(H, 2));
    4021             :     }
    4022             :   }
    4023         420 :   *pH = H; *pdegF = degF; return f;
    4024             : }
    4025             : 
    4026             : GEN
    4027         210 : subcyclopclgp(GEN FH, GEN p, long flag)
    4028             : {
    4029         210 :   pari_sp av = avma;
    4030             :   GEN H;
    4031         210 :   long degF, f = subcyclo_init("subcyclopclgp", FH, &degF, &H, 0);
    4032         203 :   if (typ(p) == t_VEC)
    4033             :   {
    4034          28 :     long i, l = lg(p);
    4035          77 :     for (i = 1; i < l; i++) checkp("subcyclopclgp", degF, gel(p, i));
    4036          14 :     if (f == 1) { set_avma(av); return const_vec(l-1, nullvec()); }
    4037             :   }
    4038             :   else
    4039             :   {
    4040         175 :     checkp("subcyclopclgp", degF, p);
    4041         154 :     if (f == 1) { set_avma(av); return nullvec(); }
    4042             :   }
    4043         168 :   if (flag >= USE_BASIS) pari_err_FLAG("subcyclopclgp");
    4044         161 :   return gerepilecopy(av, pclgp(p, f, H, degF, flag));
    4045             : }
    4046             : 
    4047             : static GEN
    4048          98 : subcycloiwasawa_i(GEN FH, GEN P, long n)
    4049             : {
    4050             :   long B, p, f, degF;
    4051             :   GEN H;
    4052          98 :   const char *fun = "subcycloiwasawa";
    4053             : 
    4054          98 :   if (typ(P) != t_INT) pari_err_TYPE(fun, P);
    4055          98 :   if (n < 0) pari_err_DOMAIN(fun, "n", "<", gen_0, stoi(n));
    4056          98 :   B = 1L << (BITS_IN_LONG/4);
    4057          98 :   if (is_bigint(P) || cmpiu(P, B) > 0)
    4058           0 :     pari_err_IMPL(stack_sprintf("prime p > %ld in %s", B, fun));
    4059          98 :   p = itos(P);
    4060          98 :   if (p <= 1 || !uisprime(p)) pari_err_PRIME(fun, P);
    4061          98 :   f = subcyclo_init(fun, FH, &degF, &H, 1);
    4062          98 :   if (degF == 1) return NULL;
    4063          98 :   if (degF == 2)
    4064             :   {
    4065          49 :     long m = ((f & 3) == 0)? f / 4: f;
    4066          49 :     if (H && !srh_1(H)) m = -m;
    4067          49 :     if (!n) return quadlambda(p, m);
    4068          28 :     return m < 0? imagquadstkpol(p, m, n): realquadstkpol(p, m, n);
    4069             :   }
    4070          49 :   if (p == 2) pari_err_DOMAIN(fun, "p", "=", gen_2, gen_2);
    4071          49 :   if (srh_1(H)) return NULL;
    4072          49 :   if (degF % p == 0) errpdiv("abeliwasawa", P, degF);
    4073          49 :   return abeliwasawa(p, f, H, degF, n);
    4074             : }
    4075             : GEN
    4076          98 : subcycloiwasawa(GEN FH, GEN P, long n)
    4077             : {
    4078          98 :   pari_sp av = avma;
    4079          98 :   GEN z = subcycloiwasawa_i(FH, P, n);
    4080          98 :   if (!z) { set_avma(av); return n? nullvec(): mkvec(gen_0); }
    4081          98 :   return gerepilecopy(av, z);
    4082             : }
    4083             : 
    4084             : GEN
    4085         119 : subcyclohminus(GEN FH, GEN P)
    4086             : {
    4087         119 :   const char *fun = "subcyclohminus";
    4088         119 :   pari_sp av = avma;
    4089             :   GEN H;
    4090         119 :   long degF, f = subcyclo_init(fun, FH, &degF, &H, 0);
    4091         119 :   if (P)
    4092             :   {
    4093          98 :     if (typ(P) != t_INT) pari_err_TYPE(fun, P);
    4094          98 :     if (isintzero(P)) P = NULL; else checkp(fun, 0, P);
    4095             :   }
    4096         119 :   if (degF == 1 ||  srh_1(H) == 1) return gen_1;
    4097         119 :   return gerepilecopy(av, rel_class_num(f, H, degF, P));
    4098             : }

Generated by: LCOV version 1.14