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 - kummer.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23694-b3ccec097) Lines: 984 1066 92.3 %
Date: 2019-03-20 05:44:21 Functions: 69 71 97.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                      KUMMER EXTENSIONS                          */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : typedef struct {
      23             :   GEN R; /* nf.pol */
      24             :   GEN x; /* tau ( Mod(x, R) ) */
      25             :   GEN zk;/* action of tau on nf.zk (as t_MAT) */
      26             : } tau_s;
      27             : 
      28             : typedef struct {
      29             :   GEN polnf, invexpoteta1, powg;
      30             :   tau_s *tau;
      31             :   long m;
      32             : } toK_s;
      33             : 
      34             : typedef struct {
      35             :   GEN R; /* ZX, compositum(P,Q) */
      36             :   GEN p; /* QX, Mod(p,R) root of P */
      37             :   GEN q; /* QX, Mod(q,R) root of Q */
      38             :   long k; /* Q[X]/R generated by q + k p */
      39             :   GEN rev;
      40             : } compo_s;
      41             : 
      42             : static long
      43        3801 : prank(GEN cyc, long ell)
      44             : {
      45             :   long i;
      46       10437 :   for (i=1; i<lg(cyc); i++)
      47        7091 :     if (smodis(gel(cyc,i),ell)) break;
      48        3801 :   return i-1;
      49             : }
      50             : 
      51             : /* increment y, which runs through [0,d-1]^(k-1). Return 0 when done. */
      52             : static int
      53         483 : increment(GEN y, long k, long d)
      54             : {
      55         483 :   long i = k, j;
      56             :   do
      57             :   {
      58         728 :     if (--i == 0) return 0;
      59         637 :     y[i]++;
      60         637 :   } while (y[i] >= d);
      61         392 :   for (j = i+1; j < k; j++) y[j] = 0;
      62         392 :   return 1;
      63             : }
      64             : 
      65             : static int
      66        1862 : ok_congruence(GEN X, ulong ell, long lW, GEN vecMsup)
      67             : {
      68             :   long i, l;
      69        1862 :   l = lg(X);
      70        3101 :   for (i=lW; i<l; i++)
      71        1400 :     if (X[i] == 0) return 0;
      72        1701 :   if (lW >= l && zv_equal0(X)) return 0;
      73        1701 :   l = lg(vecMsup);
      74        2086 :   for (i=1; i<l; i++)
      75         385 :     if (zv_equal0(Flm_Flc_mul(gel(vecMsup,i),X, ell))) return 0;
      76        1701 :   return 1;
      77             : }
      78             : 
      79             : static int
      80        1176 : ok_sign(GEN X, GEN msign, GEN arch)
      81             : {
      82        1176 :   return zv_equal(Flm_Flc_mul(msign, X, 2), arch);
      83             : }
      84             : 
      85             : /* REDUCTION MOD ell-TH POWERS */
      86             : 
      87             : /* make be integral by multiplying by t in (Q^*)^ell */
      88             : static GEN
      89        1519 : reduce_mod_Qell(GEN bnfz, GEN be, GEN gell)
      90             : {
      91             :   GEN c;
      92        1519 :   be = nf_to_scalar_or_basis(bnfz, be);
      93        1519 :   be = Q_primitive_part(be, &c);
      94        1519 :   if (c)
      95             :   {
      96         658 :     GEN d, fa = factor(c);
      97         658 :     gel(fa,2) = FpC_red(gel(fa,2), gell);
      98         658 :     d = factorback(fa);
      99         658 :     be = typ(be) == t_INT? mulii(be,d): ZC_Z_mul(be, d);
     100             :   }
     101        1519 :   return be;
     102             : }
     103             : 
     104             : /* return q, q^n r = x, v_pr(r) < n for all pr. Insist q is a genuine n-th
     105             :  * root (i.e r = 1) if strict != 0. */
     106             : static GEN
     107        3157 : idealsqrtn(GEN nf, GEN x, GEN gn, int strict)
     108             : {
     109        3157 :   long i, l, n = itos(gn);
     110             :   GEN fa, q, Ex, Pr;
     111             : 
     112        3157 :   fa = idealfactor(nf, x);
     113        3157 :   Pr = gel(fa,1); l = lg(Pr);
     114        3157 :   Ex = gel(fa,2); q = NULL;
     115        7077 :   for (i=1; i<l; i++)
     116             :   {
     117        3920 :     long ex = itos(gel(Ex,i));
     118        3920 :     GEN e = stoi(ex / n);
     119        3920 :     if (strict && ex % n) pari_err_SQRTN("idealsqrtn", fa);
     120        3920 :     if (q) q = idealmulpowprime(nf, q, gel(Pr,i), e);
     121        1575 :     else   q = idealpow(nf, gel(Pr,i), e);
     122             :   }
     123        3157 :   return q? q: gen_1;
     124             : }
     125             : 
     126             : static GEN
     127        1519 : reducebeta(GEN bnfz, GEN b, GEN ell)
     128             : {
     129        1519 :   GEN y, cb, nf = bnf_get_nf(bnfz);
     130             : 
     131        1519 :   if (DEBUGLEVEL>1) err_printf("reducing beta = %Ps\n",b);
     132        1519 :   b = reduce_mod_Qell(nf, b, ell);
     133             :   /* reduce l-th root */
     134        1519 :   y = idealsqrtn(nf, b, ell, 0); /* (b) = y^ell I, I integral */
     135        1519 :   if (typ(y) == t_MAT && !is_pm1(gcoeff(y,1,1)))
     136             :   {
     137         588 :     GEN T = idealred(nf, mkvec2(y, gen_1)), t = gel(T,2);
     138             :     /* (t)*T[1] = y, T[1] integral and small */
     139         588 :     if (gcmp(idealnorm(nf,t), gen_1) > 0)
     140         315 :       b = nfmul(nf, b, nfpow(nf, t, negi(ell)));
     141             :   }
     142        1519 :   if (DEBUGLEVEL>1) err_printf("beta reduced via ell-th root = %Ps\n",b);
     143        1519 :   b = Q_primitive_part(b, &cb);
     144        1519 :   if (cb)
     145             :   {
     146         539 :     y = nfroots(nf, gsub(monomial(gen_1, itou(ell), fetch_var_higher()),
     147             :                          basistoalg(nf,b)));
     148         539 :     delete_var();
     149             :   }
     150        1519 :   if (cb && lg(y) != 1) b = gen_1;
     151             :   else
     152             :   { /* log. embeddings of fu^ell */
     153        1365 :     GEN fu = bnf_get_fu_nocheck(bnfz), logfu = bnf_get_logfu(bnfz);
     154        1365 :     GEN elllogfu = RgM_Rg_mul(real_i(logfu), ell);
     155        1365 :     long prec = nf_get_prec(nf);
     156             :     for (;;)
     157          21 :     {
     158        1386 :       GEN emb, z = get_arch_real(nf, b, &emb, prec);
     159        1386 :       if (z)
     160             :       {
     161        1365 :         GEN ex = RgM_Babai(elllogfu, z);
     162        1365 :         if (ex)
     163             :         {
     164        1365 :           y = nffactorback(nf, fu, RgC_Rg_mul(ex,ell));
     165        2730 :           b = nfdiv(nf, b, y); break;
     166             :         }
     167             :       }
     168          21 :       prec = precdbl(prec);
     169          21 :       if (DEBUGLEVEL) pari_warn(warnprec,"reducebeta",prec);
     170          21 :       nf = nfnewprec_shallow(nf,prec);
     171             :     }
     172             :   }
     173        1519 :   if (cb) b = gmul(b, cb);
     174        1519 :   if (DEBUGLEVEL>1) err_printf("beta LLL-reduced mod U^l = %Ps\n",b);
     175        1519 :   return b;
     176             : }
     177             : 
     178             : /* FIXME: remove */
     179             : static GEN
     180         658 : tauofalg(GEN x, tau_s *tau) {
     181         658 :   long tx = typ(x);
     182         658 :   if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
     183         658 :   if (tx == t_POL) x = RgX_RgXQ_eval(x, tau->x, tau->R);
     184         658 :   return mkpolmod(x, tau->R);
     185             : }
     186             : 
     187             : /* compute Gal(K(\zeta_l)/K) */
     188             : static void
     189         476 : get_tau(tau_s *tau, GEN nf, compo_s *C, ulong g)
     190             : {
     191             :   GEN U;
     192             : 
     193             :   /* compute action of tau: q^g + kp */
     194         476 :   U = RgX_add(RgXQ_powu(C->q, g, C->R), RgX_muls(C->p, C->k));
     195         476 :   U = RgX_RgXQ_eval(C->rev, U, C->R);
     196             : 
     197         476 :   tau->x  = U;
     198         476 :   tau->R  = C->R;
     199         476 :   tau->zk = nfgaloismatrix(nf, U);
     200         476 : }
     201             : 
     202             : static GEN tauoffamat(GEN x, tau_s *tau);
     203             : 
     204             : static GEN
     205       14490 : tauofelt(GEN x, tau_s *tau)
     206             : {
     207       14490 :   switch(typ(x))
     208             :   {
     209       11529 :     case t_COL: return RgM_RgC_mul(tau->zk, x);
     210        2303 :     case t_MAT: return tauoffamat(x, tau);
     211         658 :     default: return tauofalg(x, tau);
     212             :   }
     213             : }
     214             : static GEN
     215        2639 : tauofvec(GEN x, tau_s *tau)
     216             : {
     217             :   long i, l;
     218        2639 :   GEN y = cgetg_copy(x, &l);
     219        2639 :   for (i=1; i<l; i++) gel(y,i) = tauofelt(gel(x,i), tau);
     220        2639 :   return y;
     221             : }
     222             : /* [x, tau(x), ..., tau^(m-1)(x)] */
     223             : static GEN
     224        1183 : powtau(GEN x, long m, tau_s *tau)
     225             : {
     226        1183 :   GEN y = cgetg(m+1, t_VEC);
     227             :   long i;
     228        1183 :   gel(y,1) = x;
     229        1183 :   for (i=2; i<=m; i++) gel(y,i) = tauofelt(gel(y,i-1), tau);
     230        1183 :   return y;
     231             : }
     232             : /* x^lambda */
     233             : static GEN
     234        1232 : lambdaofelt(GEN x, toK_s *T)
     235             : {
     236        1232 :   tau_s *tau = T->tau;
     237        1232 :   long i, m = T->m;
     238        1232 :   GEN y = trivial_fact(), powg = T->powg; /* powg[i] = g^i */
     239        2814 :   for (i=1; i<m; i++)
     240             :   {
     241        1582 :     y = famat_mulpows_shallow(y, x, uel(powg,m-i+1));
     242        1582 :     x = tauofelt(x, tau);
     243             :   }
     244        1232 :   return famat_mul_shallow(y, x);
     245             : }
     246             : static GEN
     247         994 : lambdaofvec(GEN x, toK_s *T)
     248             : {
     249             :   long i, l;
     250         994 :   GEN y = cgetg_copy(x, &l);
     251         994 :   for (i=1; i<l; i++) gel(y,i) = lambdaofelt(gel(x,i), T);
     252         994 :   return y;
     253             : }
     254             : 
     255             : static GEN
     256        2303 : tauoffamat(GEN x, tau_s *tau)
     257             : {
     258        2303 :   return mkmat2(tauofvec(gel(x,1), tau), gel(x,2));
     259             : }
     260             : 
     261             : static GEN
     262         350 : tauofideal(GEN id, tau_s *tau)
     263             : {
     264         350 :   return ZM_hnfmodid(RgM_mul(tau->zk, id), gcoeff(id, 1,1));
     265             : }
     266             : 
     267             : static int
     268         875 : isprimeidealconj(GEN P, GEN Q, tau_s *tau)
     269             : {
     270         875 :   GEN p = pr_get_p(P);
     271         875 :   GEN x = pr_get_gen(P);
     272         875 :   if (!equalii(p, pr_get_p(Q))
     273         721 :    || pr_get_e(P) != pr_get_e(Q)
     274         721 :    || pr_get_f(P) != pr_get_f(Q)) return 0;
     275         714 :   if (ZV_equal(x, pr_get_gen(Q))) return 1;
     276             :   for(;;)
     277             :   {
     278        2562 :     if (ZC_prdvd(x,Q)) return 1;
     279        1211 :     x = FpC_red(tauofelt(x, tau), p);
     280        1211 :     if (ZC_prdvd(x,P)) return 0;
     281             :   }
     282             : }
     283             : 
     284             : static int
     285        3549 : isconjinprimelist(GEN S, GEN pr, tau_s *tau)
     286             : {
     287             :   long i, l;
     288             : 
     289        3549 :   if (!tau) return 0;
     290        1358 :   l = lg(S);
     291        1806 :   for (i=1; i<l; i++)
     292         875 :     if (isprimeidealconj(gel(S,i),pr,tau)) return 1;
     293         931 :   return 0;
     294             : }
     295             : 
     296             : /* assume x in basistoalg form */
     297             : static GEN
     298        1631 : downtoK(toK_s *T, GEN x)
     299             : {
     300        1631 :   long degKz = lg(T->invexpoteta1) - 1;
     301        1631 :   GEN y = gmul(T->invexpoteta1, Rg_to_RgC(lift_shallow(x), degKz));
     302        1631 :   return gmodulo(gtopolyrev(y,varn(T->polnf)), T->polnf);
     303             : }
     304             : 
     305             : static GEN
     306           0 : no_sol(long all, long i)
     307             : {
     308           0 :   if (!all) pari_err_BUG(stack_sprintf("kummer [bug%ld]", i));
     309           0 :   return cgetg(1,t_VEC);
     310             : }
     311             : 
     312             : static GEN
     313         966 : get_gell(GEN bnr, GEN subgp, long all)
     314             : {
     315             :   GEN gell;
     316         966 :   if (all && all != -1) return utoipos(labs(all));
     317         931 :   if (!subgp) return ZV_prod(bnr_get_cyc(bnr));
     318         931 :   gell = det(subgp);
     319         931 :   if (typ(gell) != t_INT) pari_err_TYPE("rnfkummer",gell);
     320         931 :   return gell;
     321             : }
     322             : 
     323             : typedef struct {
     324             :   GEN Sm, Sml1, Sml2, Sl, ESml2;
     325             : } primlist;
     326             : 
     327             : static int
     328        1484 : build_list_Hecke(primlist *L, GEN nfz, GEN fa, GEN gothf, long ell, tau_s *tau)
     329             : {
     330             :   GEN listpr, listex, pr, factell;
     331        1484 :   long vp, i, l, degKz = nf_get_degree(nfz);
     332             : 
     333        1484 :   if (!fa) fa = idealfactor(nfz, gothf);
     334        1484 :   listpr = gel(fa,1);
     335        1484 :   listex = gel(fa,2); l = lg(listpr);
     336        1484 :   L->Sm  = vectrunc_init(l);
     337        1484 :   L->Sml1= vectrunc_init(l);
     338        1484 :   L->Sml2= vectrunc_init(l);
     339        1484 :   L->Sl  = vectrunc_init(l+degKz);
     340        1484 :   L->ESml2=vecsmalltrunc_init(l);
     341        3283 :   for (i=1; i<l; i++)
     342             :   {
     343        1799 :     pr = gel(listpr,i);
     344        1799 :     vp = itos(gel(listex,i));
     345        1799 :     if (!equaliu(pr_get_p(pr), ell))
     346             :     {
     347        1428 :       if (vp != 1) return 1;
     348        1428 :       if (!isconjinprimelist(L->Sm,pr,tau)) vectrunc_append(L->Sm,pr);
     349             :     }
     350             :     else
     351             :     {
     352         371 :       long e = pr_get_e(pr), vd = (vp-1)*(ell-1)-ell*e;
     353         371 :       if (vd > 0) return 4;
     354         371 :       if (vd==0)
     355             :       {
     356          70 :         if (!isconjinprimelist(L->Sml1,pr,tau)) vectrunc_append(L->Sml1, pr);
     357             :       }
     358             :       else
     359             :       {
     360         301 :         if (vp==1) return 2;
     361         301 :         if (!isconjinprimelist(L->Sml2,pr,tau))
     362             :         {
     363         301 :           vectrunc_append(L->Sml2, pr);
     364         301 :           vecsmalltrunc_append(L->ESml2, vp);
     365             :         }
     366             :       }
     367             :     }
     368             :   }
     369        1484 :   factell = idealprimedec(nfz,utoipos(ell)); l = lg(factell);
     370        3605 :   for (i=1; i<l; i++)
     371             :   {
     372        2121 :     pr = gel(factell,i);
     373        2121 :     if (!idealval(nfz,gothf,pr) && !isconjinprimelist(L->Sl,pr,tau))
     374        1743 :       vectrunc_append(L->Sl, pr);
     375             :   }
     376        1484 :   return 0; /* OK */
     377             : }
     378             : 
     379             : /* Return a Flm */
     380             : static GEN
     381        2345 : logall(GEN nf, GEN vec, long lW, long mginv, long ell, GEN pr, long ex)
     382             : {
     383        2345 :   GEN m, M, sprk = log_prk_init(nf, pr, ex);
     384        2345 :   long ellrank, i, l = lg(vec);
     385             : 
     386        2345 :   ellrank = prank(gel(sprk,1), ell);
     387        2345 :   M = cgetg(l,t_MAT);
     388       10570 :   for (i=1; i<l; i++)
     389             :   {
     390        8225 :     m = log_prk(nf, gel(vec,i), sprk);
     391        8225 :     setlg(m, ellrank+1);
     392        8225 :     if (i < lW) m = gmulsg(mginv, m);
     393        8225 :     gel(M,i) = ZV_to_Flv(m, ell);
     394             :   }
     395        2345 :   return M;
     396             : }
     397             : 
     398             : /* compute the u_j (see remark 5.2.15.) */
     399             : static GEN
     400        1456 : get_u(GEN cyc, long rc, ulong ell)
     401             : {
     402        1456 :   long i, l = lg(cyc);
     403        1456 :   GEN u = cgetg(l,t_VECSMALL);
     404        1456 :   for (i=1; i<=rc; i++) uel(u,i) = 0;
     405        1456 :   for (   ; i<  l; i++) uel(u,i) = Fl_inv(uel(cyc,i), ell);
     406        1456 :   return u;
     407             : }
     408             : 
     409             : /* alg. 5.2.15. with remark */
     410             : static GEN
     411        1428 : isprincipalell(GEN bnfz, GEN id, GEN cycgen, GEN u, ulong ell, long rc)
     412             : {
     413        1428 :   long i, l = lg(cycgen);
     414        1428 :   GEN v, b, db, y = bnfisprincipal0(bnfz, id, nf_FORCE|nf_GENMAT);
     415             : 
     416        1428 :   v = ZV_to_Flv(gel(y,1), ell);
     417        1428 :   b = gel(y,2);
     418        1428 :   if (typ(b) == t_COL)
     419             :   {
     420        1365 :     b = Q_remove_denom(gel(y,2), &db);
     421        1365 :     if (db) b = famat_mulpows_shallow(b, db, -1);
     422             :   }
     423        1659 :   for (i=rc+1; i<l; i++)
     424             :   {
     425         231 :     ulong e = Fl_mul( uel(v,i), uel(u,i), ell);
     426         231 :     b = famat_mulpows_shallow(b, gel(cycgen,i), e);
     427             :   }
     428        1428 :   setlg(v,rc+1); return mkvec2(v, b);
     429             : }
     430             : 
     431             : static GEN
     432         350 : famat_factorback(GEN v, GEN e)
     433             : {
     434         350 :   long i, l = lg(e);
     435         350 :   GEN V = trivial_fact();
     436         350 :   for (i=1; i<l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
     437         350 :   return V;
     438             : }
     439             : 
     440             : static GEN
     441        3136 : famat_factorbacks(GEN v, GEN e)
     442             : {
     443        3136 :   long i, l = lg(e);
     444        3136 :   GEN V = trivial_fact();
     445        3136 :   for (i=1; i<l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
     446        3136 :   return V;
     447             : }
     448             : 
     449             : static GEN
     450        1519 : compute_beta(GEN X, GEN vecWB, GEN ell, GEN bnfz)
     451             : {
     452             :   GEN BE, be;
     453        1519 :   BE = famat_reduce(famat_factorbacks(vecWB, X));
     454        1519 :   gel(BE,2) = centermod(gel(BE,2), ell);
     455        1519 :   be = nffactorback(bnfz, BE, NULL);
     456        1519 :   be = reducebeta(bnfz, be, ell);
     457        1519 :   if (DEBUGLEVEL>1) err_printf("beta reduced = %Ps\n",be);
     458        1519 :   return be;
     459             : }
     460             : 
     461             : static GEN
     462        1456 : get_Selmer(GEN bnf, GEN cycgen, long rc)
     463             : {
     464        1456 :   GEN U = bnf_build_units(bnf), tu = gel(U,1), fu = vecslice(U, 2, lg(U)-1);
     465        1456 :   return shallowconcat(shallowconcat(fu,mkvec(tu)), vecslice(cycgen,1,rc));
     466             : }
     467             : 
     468             : GEN
     469       59108 : lift_if_rational(GEN x)
     470             : {
     471             :   long lx, i;
     472             :   GEN y;
     473             : 
     474       59108 :   switch(typ(x))
     475             :   {
     476        8631 :     default: break;
     477             : 
     478             :     case t_POLMOD:
     479       35168 :       y = gel(x,2);
     480       35168 :       if (typ(y) == t_POL)
     481             :       {
     482       12831 :         long d = degpol(y);
     483       12831 :         if (d > 0) return x;
     484        2415 :         return (d < 0)? gen_0: gel(y,2);
     485             :       }
     486       22337 :       return y;
     487             : 
     488        7091 :     case t_POL: lx = lg(x);
     489        7091 :       for (i=2; i<lx; i++) gel(x,i) = lift_if_rational(gel(x,i));
     490        7091 :       break;
     491        8218 :     case t_VEC: case t_COL: case t_MAT: lx = lg(x);
     492        8218 :       for (i=1; i<lx; i++) gel(x,i) = lift_if_rational(gel(x,i));
     493             :   }
     494       23940 :   return x;
     495             : }
     496             : 
     497             : /* A column vector representing a subgroup of prime index */
     498             : static GEN
     499           0 : grptocol(GEN H)
     500             : {
     501           0 :   long i, j, l = lg(H);
     502           0 :   GEN col = cgetg(l, t_VECSMALL);
     503           0 :   for (i = 1; i < l; i++)
     504             :   {
     505           0 :     ulong ell = itou( gcoeff(H,i,i) );
     506           0 :     if (ell == 1) col[i] = 0; else { col[i] = ell-1; break; }
     507             :   }
     508           0 :   for (j=i; ++j < l; ) col[j] = itou( gcoeff(H,i,j) );
     509           0 :   return col;
     510             : }
     511             : 
     512             : /* Reorganize kernel basis so that the tests of ok_congruence can be ok
     513             :  * for y[ncyc]=1 and y[1..ncyc]=1 */
     514             : static GEN
     515          14 : fix_kernel(GEN K, GEN M, GEN vecMsup, long lW, long ell)
     516             : {
     517          14 :   pari_sp av = avma;
     518          14 :   long i, j, idx, ffree, dK = lg(K)-1;
     519          14 :   GEN Ki, Kidx = cgetg(dK+1, t_VECSMALL);
     520             : 
     521             :   /* First step: Gauss elimination on vectors lW...lg(M)-1 */
     522          28 :   for (idx = lg(K), i = lg(M)-1; i >= lW; i--)
     523             :   {
     524          14 :     for (j = dK; j > 0; j--) if (coeff(K, i, j)) break;
     525          14 :     if (!j || j == dK) continue;
     526             :     /* ensure that K[i,dK] != 0 */
     527           0 :     for (j = idx; j < dK; j++)
     528           0 :       if (coeff(K, i, j) && coeff(K, Kidx[j], dK) != ell - 1)
     529           0 :         Flv_add_inplace(gel(K,dK), gel(K,j), ell);
     530           0 :     idx--;
     531           0 :     if (j != idx) swap(gel(K, j), gel(K, idx));
     532           0 :     Kidx[idx] = i;
     533           0 :     if (coeff(K,i,idx) != 1)
     534           0 :       Flv_Fl_div_inplace(gel(K,idx), coeff(K,i,idx), ell);
     535           0 :     Ki = gel(K,idx);
     536           0 :     if (coeff(K,i,dK) != 1)
     537             :     {
     538           0 :       ulong t = Fl_sub(coeff(K,i,dK), 1, ell);
     539           0 :       Flv_sub_inplace(gel(K,dK), Flv_Fl_mul(Ki, t, ell), ell);
     540             :     }
     541           0 :     for (j = dK; --j > 0; )
     542             :     {
     543           0 :       if (j == idx) continue;
     544           0 :       if (coeff(K,i,j))
     545           0 :         Flv_sub_inplace(gel(K,j), Flv_Fl_mul(Ki, coeff(K,i,j), ell), ell);
     546             :     }
     547             :   }
     548             :   /* ffree = first vector that is not "free" for the scalar products */
     549          14 :   ffree = idx;
     550             :   /* Second step: for each hyperplane equation in vecMsup, do the same
     551             :    * thing as before. */
     552          14 :   for (i=1; i < lg(vecMsup); i++)
     553             :   {
     554           0 :     GEN Msup = gel(vecMsup,i);
     555             :     ulong dotprod;
     556           0 :     if (lgcols(Msup) != 2) continue;
     557           0 :     Msup = zm_row(Msup, 1);
     558           0 :     for (j=ffree; j > 0; j--)
     559             :     {
     560           0 :       dotprod = Flv_dotproduct(Msup, gel(K,j), ell);
     561           0 :       if (dotprod)
     562             :       {
     563           0 :         if (j != --ffree) swap(gel(K, j), gel(K, ffree));
     564           0 :         if (dotprod != 1) Flv_Fl_div_inplace(gel(K, ffree), dotprod, ell);
     565           0 :         break;
     566             :       }
     567             :     }
     568           0 :     if (!j)
     569             :     { /* Do our best to ensure that vecMsup.K[dK] != 0 */
     570           0 :       if (Flv_dotproduct(Msup, gel(K,dK), ell) == 0)
     571             :       {
     572           0 :         for (j = ffree-1; j <= dK; j++)
     573           0 :           if (Flv_dotproduct(Msup, gel(K,j), ell)
     574           0 :               && coeff(K,Kidx[j],dK) != ell-1)
     575           0 :             Flv_add_inplace(gel(K,dK), gel(K,j), ell);
     576             :       }
     577           0 :       continue;
     578             :     }
     579           0 :     Ki = gel(K,ffree);
     580           0 :     dotprod = Flv_dotproduct(Msup, gel(K,dK), ell);
     581           0 :     if (dotprod != 1)
     582             :     {
     583           0 :       ulong t = Fl_sub(dotprod,1,ell);
     584           0 :       Flv_sub_inplace(gel(K,dK), Flv_Fl_mul(Ki,t,ell), ell);
     585             :     }
     586           0 :     for (j = dK; j > 0; j--)
     587             :     {
     588           0 :       if (j == ffree) continue;
     589           0 :       dotprod = Flv_dotproduct(Msup, gel(K,j), ell);
     590           0 :       if (dotprod) Flv_sub_inplace(gel(K,j), Flv_Fl_mul(Ki,dotprod,ell), ell);
     591             :     }
     592             :   }
     593          14 :   if (ell == 2)
     594             :   {
     595          14 :     for (i = ffree, j = ffree-1; i <= dK && j; i++, j--)
     596           0 :     { swap(gel(K,i), gel(K,j)); }
     597             :   }
     598             :   /* Try to ensure that y = vec_ei(n, i) gives a good candidate */
     599          14 :   for (i = 1; i < dK; i++) Flv_add_inplace(gel(K,i), gel(K,dK), ell);
     600          14 :   return gerepilecopy(av, K);
     601             : }
     602             : 
     603             : static GEN
     604          14 : Flm_init(long m, long n)
     605             : {
     606          14 :   GEN M = cgetg(n+1, t_MAT);
     607          14 :   long i; for (i = 1; i <= n; i++) gel(M,i) = cgetg(m+1, t_VECSMALL);
     608          14 :   return M;
     609             : }
     610             : static void
     611         308 : Flv_fill(GEN v, GEN y)
     612             : {
     613         308 :   long i, l = lg(y);
     614         308 :   for (i = 1; i < l; i++) v[i] = y[i];
     615         308 : }
     616             : 
     617             : static GEN
     618        1946 : get_badbnf(GEN bnf)
     619             : {
     620             :   long i, l;
     621        1946 :   GEN bad = gen_1, gen = bnf_get_gen(bnf);
     622        1946 :   l = lg(gen);
     623        3528 :   for (i = 1; i < l; i++)
     624             :   {
     625        1582 :     GEN g = gel(gen,i);
     626        1582 :     bad = lcmii(bad, gcoeff(g,1,1));
     627             :   }
     628        1946 :   return bad;
     629             : }
     630             : /* Let K base field, L/K described by bnr (conductor f) + H. Return a list of
     631             :  * primes coprime to f*ell of degree 1 in K whose images in Cl_f(K) generate H:
     632             :  * thus they all split in Lz/Kz; t in Kz is such that
     633             :  * t^(1/p) generates Lz => t is an ell-th power in k(pr) for all such primes.
     634             :  * Restrict to primes not dividing
     635             :  * - the index fz of the polynomial defining Kz, or
     636             :  * - the modulus, or
     637             :  * - ell, or
     638             :  * - a generator in bnf.gen or bnfz.gen */
     639             : static GEN
     640        1456 : get_prlist(GEN bnr, GEN H, ulong ell, GEN bnfz)
     641             : {
     642        1456 :   pari_sp av0 = avma;
     643             :   forprime_t T;
     644             :   ulong p;
     645             :   GEN L, nf, cyc, bad, cond, condZ, Hsofar;
     646        1456 :   L = cgetg(1, t_VEC);
     647        1456 :   cyc = bnr_get_cyc(bnr);
     648        1456 :   nf = bnr_get_nf(bnr);
     649             : 
     650        1456 :   cond = gel(bnr_get_mod(bnr), 1);
     651        1456 :   condZ = gcoeff(cond,1,1);
     652        1456 :   bad = get_badbnf(bnr_get_bnf(bnr));
     653        1456 :   if (bnfz)
     654             :   {
     655         490 :     GEN badz = lcmii(get_badbnf(bnfz), nf_get_index(bnf_get_nf(bnfz)));
     656         490 :     bad = mulii(bad,badz);
     657             :   }
     658        1456 :   bad = lcmii(muliu(condZ, ell), bad);
     659             :   /* restrict to primes not dividing bad */
     660             : 
     661        1456 :   u_forprime_init(&T, 2, ULONG_MAX);
     662        1456 :   Hsofar = cgetg(1, t_MAT);
     663       20321 :   while ((p = u_forprime_next(&T)))
     664             :   {
     665             :     GEN LP;
     666             :     long i, l;
     667       18865 :     if (p == ell || !umodiu(bad, p)) continue;
     668       15400 :     LP = idealprimedec_limit_f(nf, utoipos(p), 1);
     669       15400 :     l = lg(LP);
     670       23996 :     for (i = 1; i < l; i++)
     671             :     {
     672       10052 :       pari_sp av = avma;
     673       10052 :       GEN M, P = gel(LP,i), v = bnrisprincipal(bnr, P, 0);
     674       10052 :       if (!hnf_invimage(H, v)) { set_avma(av); continue; }
     675        2961 :       M = shallowconcat(Hsofar, v);
     676        2961 :       M = ZM_hnfmodid(M, cyc);
     677        2961 :       if (ZM_equal(M, Hsofar)) continue;
     678        2289 :       L = shallowconcat(L, mkvec(P));
     679        2289 :       Hsofar = M;
     680             :       /* the primes in L generate H */
     681        2289 :       if (ZM_equal(M, H)) return gerepilecopy(av0, L);
     682             :     }
     683             :   }
     684           0 :   pari_err_BUG("rnfkummer [get_prlist]");
     685           0 :   return NULL;
     686             : }
     687             : /*Lprz list of prime ideals in Kz that must split completely in Lz/Kz, vecWA
     688             :  * generators for the S-units used to build the Kummer generators. Return
     689             :  * matsmall M such that \prod WA[j]^x[j] ell-th power mod pr[i] iff
     690             :  * \sum M[i,j] x[j] = 0 (mod ell) */
     691             : static GEN
     692        1456 : subgroup_info(GEN bnfz, GEN Lprz, long ell, GEN vecWA)
     693             : {
     694        1456 :   GEN nfz = bnf_get_nf(bnfz), M, gell = utoipos(ell), Lell = mkvec(gell);
     695        1456 :   long i, j, l = lg(vecWA), lz = lg(Lprz);
     696        1456 :   M = cgetg(l, t_MAT);
     697        1456 :   for (j=1; j<l; j++) gel(M,j) = cgetg(lz, t_VECSMALL);
     698        3745 :   for (i=1; i < lz; i++)
     699             :   {
     700        2289 :     GEN pr = gel(Lprz,i), EX = subiu(pr_norm(pr), 1);
     701        2289 :     GEN N, g,T,p, prM = idealhnf(nfz, pr);
     702        2289 :     GEN modpr = zk_to_Fq_init(nfz, &pr,&T,&p);
     703        2289 :     long v = Z_lvalrem(divis(EX,ell), ell, &N) + 1; /* Norm(pr)-1 = N * ell^v */
     704        2289 :     GEN ellv = powuu(ell, v);
     705        2289 :     g = gener_Fq_local(T,p, Lell);
     706        2289 :     g = Fq_pow(g,N, T,p); /* order ell^v */
     707       10850 :     for (j=1; j < l; j++)
     708             :     {
     709        8561 :       GEN logc, c = gel(vecWA,j);
     710        8561 :       if (typ(c) == t_MAT) /* famat */
     711        3017 :         c = famat_makecoprime(nfz, gel(c,1), gel(c,2), pr, prM, EX);
     712        8561 :       c = nf_to_Fq(nfz, c, modpr);
     713        8561 :       c = Fq_pow(c, N, T,p);
     714        8561 :       logc = Fq_log(c, g, ellv, T,p);
     715        8561 :       ucoeff(M, i,j) = umodiu(logc, ell);
     716             :     }
     717             :   }
     718        1456 :   return M;
     719             : }
     720             : 
     721             : /* if all>0, give all equations of degree 'all'. Assume bnr modulus is the
     722             :  * conductor */
     723             : static GEN
     724         980 : rnfkummersimple(GEN bnr, GEN subgroup, long ell, long all)
     725             : {
     726         980 :   long i, j, degK, dK, lSml2, lSl2, lSp, rc, lW, prec, rk = 0, ncyc = 0;
     727         980 :   long firstpass = all<0;
     728             :   GEN bnf, nf,bid, ideal, arch, cycgen, cyc, Sp, prSp, matP;
     729             :   GEN gell, xell, u, M, K, y, vecMsup, vecW, vecWB, vecBp, msign;
     730         980 :   GEN mat = NULL, matgrp = NULL, be1 = NULL, res = NULL;
     731             :   primlist L;
     732             : 
     733         980 :   bnf = bnr_get_bnf(bnr); (void)bnf_build_units(bnf);
     734         980 :   nf  = bnf_get_nf(bnf);
     735         980 :   degK = nf_get_degree(nf);
     736             : 
     737         980 :   bid = bnr_get_bid(bnr);
     738         980 :   ideal= bid_get_ideal(bid);
     739         980 :   arch = bid_get_arch(bid); /* this is the conductor */
     740         980 :   i = build_list_Hecke(&L, nf, bid_get_fact2(bid), ideal, ell, NULL);
     741         980 :   if (i) return no_sol(all,i);
     742             : 
     743         980 :   lSml2 = lg(L.Sml2);
     744         980 :   Sp = shallowconcat(L.Sm, L.Sml1); lSp = lg(Sp);
     745         980 :   prSp = shallowconcat(L.Sml2, L.Sl); lSl2 = lg(prSp);
     746             : 
     747         980 :   cycgen = bnf_build_cycgen(bnf);
     748         980 :   cyc = bnf_get_cyc(bnf); rc = prank(cyc, ell);
     749             : 
     750         980 :   vecW = get_Selmer(bnf, cycgen, rc);
     751         980 :   u = get_u(ZV_to_Flv(cyc,ell), rc, ell);
     752             : 
     753         980 :   vecBp = cgetg(lSp, t_VEC);
     754         980 :   matP  = cgetg(lSp, t_MAT);
     755        1792 :   for (j = 1; j < lSp; j++)
     756             :   {
     757         812 :     GEN L = isprincipalell(bnf,gel(Sp,j), cycgen,u,ell,rc);
     758         812 :     gel( matP,j) = gel(L,1);
     759         812 :     gel(vecBp,j) = gel(L,2);
     760             :   }
     761         980 :   vecWB = shallowconcat(vecW, vecBp);
     762             : 
     763         980 :   prec = DEFAULTPREC +
     764         980 :       nbits2extraprec(((degK-1) * (gexpo(vecWB) + gexpo(nf_get_M(nf)))));
     765         980 :   if (nf_get_prec(nf) < prec) nf = nfnewprec_shallow(nf, prec);
     766         980 :   msign = nfsign(nf, vecWB);
     767         980 :   arch = ZV_to_zv(arch);
     768             : 
     769         980 :   vecMsup = cgetg(lSml2,t_VEC);
     770         980 :   M = NULL;
     771        2359 :   for (i = 1; i < lSl2; i++)
     772             :   {
     773        1379 :     GEN pr = gel(prSp,i);
     774        1379 :     long e = pr_get_e(pr), z = ell * (e / (ell-1));
     775             : 
     776        1379 :     if (i < lSml2)
     777             :     {
     778         161 :       z += 1 - L.ESml2[i];
     779         161 :       gel(vecMsup,i) = logall(nf, vecWB, 0,0, ell, pr,z+1);
     780             :     }
     781        1379 :     M = vconcat(M, logall(nf, vecWB, 0,0, ell, pr,z));
     782             :   }
     783         980 :   lW = lg(vecW);
     784         980 :   M = vconcat(M, shallowconcat(zero_Flm(rc,lW-1), matP));
     785         980 :   if (!all)
     786             :   { /* primes landing in subgroup must be totally split */
     787         966 :     GEN Lpr = get_prlist(bnr, subgroup, ell, NULL);
     788         966 :     GEN M2 = subgroup_info(bnf, Lpr, ell, vecWB);
     789         966 :     M = vconcat(M, M2);
     790             :   }
     791         980 :   K = Flm_ker(M, ell);
     792         980 :   if (all < 0) K = fix_kernel(K, M, vecMsup, lW, ell);
     793         980 :   dK = lg(K)-1;
     794         980 :   y = cgetg(dK+1,t_VECSMALL);
     795         980 :   if (all) res = cgetg(1,t_VEC); /* in case all = 1 */
     796         980 :   if (all < 0)
     797             :   {
     798          14 :     ncyc = dK; rk = 0; mat = Flm_init(dK, ncyc);
     799          14 :     if (all == -1) matgrp = Flm_init(lg(bnr_get_cyc(bnr)), ncyc+1);
     800             :   }
     801         980 :   xell = pol_xn(ell, 0);
     802         980 :   gell = utoipos(ell);
     803             :   do {
     804         994 :     dK = lg(K)-1;
     805        2086 :     while (dK)
     806             :     {
     807        1064 :       for (i=1; i<dK; i++) y[i] = 0;
     808        1064 :       y[i] = 1; /* y = [0,...,0,1,0,...,0], 1 at i'th position */
     809             :       do
     810             :       {
     811        1442 :         pari_sp av = avma;
     812        1442 :         GEN be, P=NULL, X;
     813        1442 :         if (all < 0)
     814             :         {
     815         308 :           Flv_fill(gel(mat, rk+1), y);
     816         308 :           setlg(mat, rk+2);
     817         308 :           if (Flm_rank(mat, ell) <= rk) continue;
     818             :         }
     819        1337 : FOUND:  X = Flm_Flc_mul(K, y, ell);
     820        1337 :         if (ok_congruence(X, ell, lW, vecMsup) && ok_sign(X, msign, arch))
     821             :         {/* be satisfies all congruences, x^ell - be is irreducible, signature
     822             :           * and relative discriminant are correct */
     823         994 :           if (all < 0) rk++;
     824         994 :           be = compute_beta(X, vecWB, gell, bnf);
     825         994 :           be = nf_to_scalar_or_alg(nf, be);
     826         994 :           if (typ(be) == t_POL) be = mkpolmod(be, nf_get_pol(nf));
     827         994 :           if (all == -1)
     828             :           {
     829           0 :             pari_sp av2 = avma;
     830           0 :             GEN Kgrp, colgrp = grptocol(rnfnormgroup(bnr, gsub(xell, be)));
     831           0 :             if (ell != 2)
     832             :             {
     833           0 :               if (rk == 1) be1 = be;
     834             :               else
     835             :               { /* Compute the pesky scalar */
     836           0 :                 GEN K2, C = cgetg(4, t_MAT);
     837           0 :                 gel(C,1) = gel(matgrp,1);
     838           0 :                 gel(C,2) = colgrp;
     839           0 :                 gel(C,3) = grptocol(rnfnormgroup(bnr, gsub(xell, gmul(be1,be))));
     840           0 :                 K2 = Flm_ker(C, ell);
     841           0 :                 if (lg(K2) != 2) pari_err_BUG("linear algebra");
     842           0 :                 K2 = gel(K2,1);
     843           0 :                 if (K2[1] != K2[2])
     844           0 :                   Flv_Fl_mul_inplace(colgrp, Fl_div(K2[2],K2[1],ell), ell);
     845             :               }
     846             :             }
     847           0 :             Flv_fill(gel(matgrp,rk), colgrp);
     848           0 :             setlg(matgrp, rk+1);
     849           0 :             Kgrp = Flm_ker(matgrp, ell);
     850           0 :             if (lg(Kgrp) == 2)
     851             :             {
     852           0 :               setlg(gel(Kgrp,1), rk+1);
     853           0 :               y = Flm_Flc_mul(mat, gel(Kgrp,1), ell);
     854           0 :               all = 0; goto FOUND;
     855             :             }
     856           0 :             set_avma(av2);
     857             :           }
     858             :           else
     859             :           {
     860         994 :             P = gsub(xell, be);
     861         994 :             if (all)
     862          28 :               res = shallowconcat(res, gerepileupto(av, P));
     863             :             else
     864             :             {
     865         966 :               if (ZM_equal(rnfnormgroup(bnr,P),subgroup)) return P; /*DONE*/
     866           0 :               set_avma(av); continue;
     867             :             }
     868             :           }
     869          28 :           if (all < 0 && rk == ncyc) return res;
     870          28 :           if (firstpass) break;
     871             :         }
     872         343 :         else set_avma(av);
     873         448 :       } while (increment(y, dK, ell));
     874          98 :       y[dK--] = 0;
     875             :     }
     876          28 :   } while (firstpass--);
     877          14 :   return all? res: gen_0;
     878             : }
     879             : 
     880             : /* alg. 5.3.11 (return only discrete log mod ell) */
     881             : static GEN
     882        1638 : isvirtualunit(GEN bnf, GEN v, GEN cycgen, GEN cyc, GEN gell, long rc)
     883             : {
     884        1638 :   GEN L, b, eps, y, q, nf = bnf_get_nf(bnf), w = idealsqrtn(nf, v, gell, 1);
     885        1638 :   long i, l = lg(cycgen);
     886             : 
     887        1638 :   L = bnfisprincipal0(bnf, w, nf_GENMAT|nf_FORCE);
     888        1638 :   q = gel(L,1);
     889        1638 :   if (ZV_equal0(q)) { eps = v; y = q; }
     890             :   else
     891             :   {
     892         350 :     y = cgetg(l,t_COL);
     893         350 :     for (i=1; i<l; i++) gel(y,i) = diviiexact(mulii(gell,gel(q,i)), gel(cyc,i));
     894         350 :     eps = famat_mulpow_shallow(famat_factorback(cycgen,y), gel(L,2), gell);
     895         350 :     eps = famat_mul_shallow(famat_inv(eps), v);
     896             :   }
     897        1638 :   setlg(y, rc+1);
     898        1638 :   b = bnfisunit(bnf,eps);
     899        1638 :   if (lg(b) == 1) pari_err_BUG("isvirtualunit");
     900        1638 :   return shallowconcat(lift_shallow(b), y);
     901             : }
     902             : 
     903             : /* J a vector of elements in nfz = relative extension of nf by polrel,
     904             :  * return the Steinitz element attached to the module generated by J */
     905             : static GEN
     906         672 : Stelt(GEN nf, GEN J, GEN polrel)
     907             : {
     908         672 :   long i, l = lg(J), vx = varn(polrel);
     909         672 :   GEN A = cgetg(l, t_VEC), I = cgetg(l, t_VEC);
     910        4550 :   for (i = 1; i < l; i++)
     911             :   {
     912        3878 :     GEN v = gel(J,i);
     913        3878 :     if (typ(v) == t_POL) { v = RgX_rem(v, polrel); setvarn(v,vx); }
     914        3878 :     gel(A,i) = v;
     915        3878 :     gel(I,i) = gen_1;
     916             :   }
     917         672 :   A = RgV_to_RgM(A, degpol(polrel));
     918         672 :   return idealprod(nf, gel(nfhnf(nf, mkvec2(A,I)),2));
     919             : }
     920             : 
     921             : static GEN
     922         133 : polrelKzK(toK_s *T, GEN x)
     923             : {
     924         133 :   GEN P = roots_to_pol(powtau(x, T->m, T->tau), 0);
     925         133 :   long i, l = lg(P);
     926         133 :   for (i=2; i<l; i++) gel(P,i) = downtoK(T, gel(P,i));
     927         133 :   return P;
     928             : }
     929             : 
     930             : /* N: Cl_m(Kz) --> Cl_m(K), lift subgroup from bnr to bnrz using Algo 4.1.11 */
     931             : static GEN
     932         133 : invimsubgroup(GEN bnrz, GEN bnr, GEN subgroup, toK_s *T)
     933             : {
     934             :   long l, j;
     935             :   GEN P, cyc, gen, U, polrel, StZk;
     936         133 :   GEN nf = bnr_get_nf(bnr), nfz = bnr_get_nf(bnrz);
     937         133 :   GEN polz = nf_get_pol(nfz), zkzD = nf_get_zkprimpart(nfz);
     938             : 
     939         133 :   polrel = polrelKzK(T, pol_x(varn(polz)));
     940         133 :   StZk = Stelt(nf, zkzD, polrel);
     941         133 :   cyc = bnr_get_cyc(bnrz); l = lg(cyc);
     942         133 :   gen = bnr_get_gen(bnrz);
     943         133 :   P = cgetg(l,t_MAT);
     944         672 :   for (j=1; j<l; j++)
     945             :   {
     946         539 :     GEN g, id = idealhnf_shallow(nfz, gel(gen,j));
     947         539 :     g = Stelt(nf, RgV_RgM_mul(zkzD, id), polrel);
     948         539 :     g = idealdiv(nf, g, StZk); /* N_{Kz/K}(gen[j]) */
     949         539 :     gel(P,j) = isprincipalray(bnr, g);
     950             :   }
     951         133 :   (void)ZM_hnfall_i(shallowconcat(P, subgroup), &U, 1);
     952         133 :   setlg(U, l); for (j=1; j<l; j++) setlg(U[j], l);
     953         133 :   return ZM_hnfmodid(U, cyc);
     954             : }
     955             : 
     956             : static GEN
     957         525 : pol_from_Newton(GEN S)
     958             : {
     959         525 :   long i, k, l = lg(S);
     960         525 :   GEN C = cgetg(l+1, t_VEC), c = C + 1;
     961         525 :   gel(c,0) = gen_1;
     962         525 :   gel(c,1) = gel(S,1); /* gen_0 in our case */
     963        1729 :   for (k = 2; k < l; k++)
     964             :   {
     965        1204 :     GEN s = gel(S,k);
     966        1204 :     for (i = 2; i < k-1; i++) s = gadd(s, gmul(gel(S,i), gel(c,k-i)));
     967        1204 :     gel(c,k) = gdivgs(s, -k);
     968             :   }
     969         525 :   return gtopoly(C, 0);
     970             : }
     971             : 
     972             : /* - mu_b = sum_{0 <= i < m} floor(r_b r_{d-1-i} / ell) tau^i */
     973             : static GEN
     974        1176 : get_mmu(long b, GEN r, long ell)
     975             : {
     976        1176 :   long i, m = lg(r)-1;
     977        1176 :   GEN M = cgetg(m+1, t_VEC);
     978        1176 :   for (i = 0; i < m; i++) gel(M,i+1) = stoi((r[b + 1] * r[m - i]) / ell);
     979        1176 :   return M;
     980             : }
     981             : 
     982             : /* coeffs(x, a..b) in variable v >= varn(x) */
     983             : static GEN
     984       10780 : split_pol(GEN x, long v, long a, long b)
     985             : {
     986       10780 :   long i, l = degpol(x);
     987       10780 :   GEN y = x + a, z;
     988             : 
     989       10780 :   if (l < b) b = l;
     990       10780 :   if (a > b || varn(x) != v) return pol_0(v);
     991        9562 :   l = b-a + 3;
     992        9562 :   z = cgetg(l, t_POL); z[1] = x[1];
     993        9562 :   for (i = 2; i < l; i++) gel(z,i) = gel(y,i);
     994        9562 :   return normalizepol_lg(z, l);
     995             : }
     996             : 
     997             : /* return (den_a * z) mod (v^ell - num_a/den_a), assuming deg(z) < 2*ell
     998             :  * allow either num/den to be NULL (= 1) */
     999             : static GEN
    1000        5390 : mod_Xell_a(GEN z, long v, long ell, GEN num_a, GEN den_a)
    1001             : {
    1002        5390 :   GEN z1 = split_pol(z, v, ell, degpol(z));
    1003        5390 :   GEN z0 = split_pol(z, v, 0,   ell-1); /* z = v^ell z1 + z0*/
    1004        5390 :   if (den_a) z0 = gmul(den_a, z0);
    1005        5390 :   if (num_a) z1 = gmul(num_a, z1);
    1006        5390 :   return gadd(z0, z1);
    1007             : }
    1008             : static GEN
    1009        1701 : to_alg(GEN nfz, GEN c, long v)
    1010             : {
    1011             :   GEN z, D;
    1012        1701 :   if (typ(c) != t_COL) return c;
    1013        1176 :   z = gmul(nf_get_zkprimpart(nfz), c);
    1014        1176 :   if (typ(z) == t_POL) setvarn(z, v);
    1015        1176 :   D = nf_get_zkden(nfz);
    1016        1176 :   if (!equali1(D)) z = RgX_Rg_div(z, D);
    1017        1176 :   return z;
    1018             : }
    1019             : 
    1020             : /* th. 5.3.5. and prop. 5.3.9. */
    1021             : static GEN
    1022         525 : compute_polrel(GEN nfz, toK_s *T, GEN be, long g, long ell)
    1023             : {
    1024         525 :   long i, k, m = T->m, vT = fetch_var(), vz = fetch_var();
    1025             :   GEN r, powtaubet, S, p1, root, num_t, den_t, nfzpol, powtau_prim_invbe;
    1026             :   GEN prim_Rk, C_Rk, prim_root, C_root, prim_invbe, C_invbe;
    1027             :   pari_timer ti;
    1028             : 
    1029         525 :   r = cgetg(m+1,t_VECSMALL); /* r[i+1] = g^i mod ell */
    1030         525 :   r[1] = 1;
    1031         525 :   for (i=2; i<=m; i++) r[i] = (r[i-1] * g) % ell;
    1032         525 :   powtaubet = powtau(be, m, T->tau);
    1033         525 :   if (DEBUGLEVEL>1) { err_printf("Computing Newton sums: "); timer_start(&ti); }
    1034         525 :   prim_invbe = Q_primitive_part(nfinv(nfz, be), &C_invbe);
    1035         525 :   powtau_prim_invbe = powtau(prim_invbe, m, T->tau);
    1036             : 
    1037         525 :   root = cgetg(ell + 2, t_POL);
    1038         525 :   root[1] = evalsigne(1) | evalvarn(0);
    1039         525 :   for (i = 0; i < ell; i++) gel(root,2+i) = gen_0;
    1040        1701 :   for (i = 0; i < m; i++)
    1041             :   { /* compute (1/be) ^ (-mu) instead of be^mu [mu << 0].
    1042             :      * 1/be = C_invbe * prim_invbe */
    1043        1176 :     GEN mmu = get_mmu(i, r, ell);
    1044             :     /* p1 = prim_invbe ^ -mu */
    1045        1176 :     p1 = to_alg(nfz, nffactorback(nfz, powtau_prim_invbe, mmu), vz);
    1046        1176 :     if (C_invbe) p1 = gmul(p1, powgi(C_invbe, RgV_sumpart(mmu, m)));
    1047             :     /* root += zeta_ell^{r_i} T^{r_i} be^mu_i */
    1048        1176 :     gel(root, 2 + r[i+1]) = monomial(p1, r[i+1], vT);
    1049             :   }
    1050             :   /* Other roots are as above with z_ell --> z_ell^j.
    1051             :    * Treat all contents (C_*) and principal parts (prim_*) separately */
    1052         525 :   prim_Rk = prim_root = Q_primitive_part(root, &C_root);
    1053         525 :   C_Rk = C_root;
    1054             : 
    1055         525 :   r = vecsmall_reverse(r); /* theta^ell = be^( sum tau^a r_{d-1-a} ) */
    1056             :   /* Compute modulo X^ell - 1, T^ell - t, nfzpol(vz) */
    1057         525 :   p1 = to_alg(nfz, nffactorback(nfz, powtaubet, r), vz);
    1058         525 :   num_t = Q_remove_denom(p1, &den_t);
    1059             : 
    1060         525 :   nfzpol = leafcopy(nf_get_pol(nfz));
    1061         525 :   setvarn(nfzpol, vz);
    1062         525 :   S = cgetg(ell+1, t_VEC); /* Newton sums */
    1063         525 :   gel(S,1) = gen_0;
    1064        1729 :   for (k = 2; k <= ell; k++)
    1065             :   { /* compute the k-th Newton sum */
    1066        1204 :     pari_sp av = avma;
    1067        1204 :     GEN z, D, Rk = gmul(prim_Rk, prim_root);
    1068        1204 :     C_Rk = mul_content(C_Rk, C_root);
    1069        1204 :     Rk = mod_Xell_a(Rk, 0, ell, NULL, NULL); /* mod X^ell - 1 */
    1070        5418 :     for (i = 2; i < lg(Rk); i++)
    1071             :     {
    1072        4214 :       if (typ(gel(Rk,i)) != t_POL) continue;
    1073        4186 :       z = mod_Xell_a(gel(Rk,i), vT, ell, num_t,den_t); /* mod T^ell - t */
    1074        4186 :       gel(Rk,i) = RgXQX_red(z, nfzpol); /* mod nfz.pol */
    1075             :     }
    1076        1204 :     if (den_t) C_Rk = mul_content(C_Rk, ginv(den_t));
    1077        1204 :     prim_Rk = Q_primitive_part(Rk, &D);
    1078        1204 :     C_Rk = mul_content(C_Rk, D); /* root^k = prim_Rk * C_Rk */
    1079             : 
    1080             :     /* Newton sum is ell * constant coeff (in X), which has degree 0 in T */
    1081        1204 :     z = polcoef_i(prim_Rk, 0, 0);
    1082        1204 :     z = polcoef_i(z      , 0,vT);
    1083        1204 :     z = downtoK(T, gmulgs(z, ell));
    1084        1204 :     if (C_Rk) z = gmul(z, C_Rk);
    1085        1204 :     gerepileall(av, C_Rk? 3: 2, &z, &prim_Rk, &C_Rk);
    1086        1204 :     if (DEBUGLEVEL>1) { err_printf("%ld(%ld) ", k, timer_delay(&ti)); err_flush(); }
    1087        1204 :     gel(S,k) = z;
    1088             :   }
    1089         525 :   if (DEBUGLEVEL>1) err_printf("\n");
    1090         525 :   (void)delete_var();
    1091         525 :   (void)delete_var(); return pol_from_Newton(S);
    1092             : }
    1093             : 
    1094             : /* lift elt t in nf to nfz, algebraic form */
    1095             : static GEN
    1096         609 : lifttoKz(GEN nf, GEN t, compo_s *C)
    1097             : {
    1098         609 :   GEN x = nf_to_scalar_or_alg(nf, t);
    1099         609 :   if (typ(x) != t_POL) return x;
    1100         609 :   return RgX_RgXQ_eval(x, C->p, C->R);
    1101             : }
    1102             : /* lift ideal id in nf to nfz */
    1103             : static GEN
    1104         504 : ideallifttoKz(GEN nfz, GEN nf, GEN id, compo_s *C)
    1105             : {
    1106         504 :   GEN I = idealtwoelt(nf,id);
    1107         504 :   GEN x = nf_to_scalar_or_alg(nf, gel(I,2));
    1108         504 :   if (typ(x) != t_POL) return gel(I,1);
    1109         329 :   gel(I,2) = algtobasis(nfz, RgX_RgXQ_eval(x, C->p, C->R));
    1110         329 :   return idealhnf_two(nfz,I);
    1111             : }
    1112             : /* lift ideal pr in nf to ONE prime in nfz (the others are conjugate under tau
    1113             :  * and bring no further information on e_1 W). Assume pr coprime to
    1114             :  * index of both nf and nfz, and unramified in Kz/K (minor simplification) */
    1115             : static GEN
    1116         714 : prlifttoKz(GEN nfz, GEN nf, GEN pr, compo_s *C)
    1117             : {
    1118         714 :   GEN F, p = pr_get_p(pr), t = pr_get_gen(pr), T = nf_get_pol(nfz);
    1119         714 :   if (nf_get_degree(nf) != 1)
    1120             :   { /* restrict to primes above pr */
    1121         609 :     t = Q_primpart( lifttoKz(nf,t,C) );
    1122         609 :     T = FpX_gcd(FpX_red(T,p), FpX_red(t,p), p);
    1123         609 :     T = FpX_normalize(T, p);
    1124             :   }
    1125         714 :   F = FpX_factor(T, p);
    1126         714 :   return idealprimedec_kummer(nfz,gcoeff(F,1,1), pr_get_e(pr), p);
    1127             : }
    1128             : static GEN
    1129         490 : get_przlist(GEN L, GEN nfz, GEN nf, compo_s *C)
    1130             : {
    1131             :   long i, l;
    1132         490 :   GEN M = cgetg_copy(L, &l);
    1133         490 :   for (i = 1; i < l; i++) gel(M,i) = prlifttoKz(nfz, nf, gel(L,i), C);
    1134         490 :   return M;
    1135             : }
    1136             : 
    1137             : static void
    1138         476 : compositum_red(compo_s *C, GEN P, GEN Q)
    1139             : {
    1140         476 :   GEN p, q, a, z = gel(compositum2(P, Q),1);
    1141         476 :   a = gel(z,1);
    1142         476 :   p = gel(gel(z,2), 2);
    1143         476 :   q = gel(gel(z,3), 2);
    1144         476 :   C->k = itos( gel(z,4) );
    1145             :   /* reduce R. FIXME: should be polredbest(a, 1), but breaks rnfkummer bench */
    1146         476 :   z = polredabs0(a, nf_ORIG|nf_PARTIALFACT);
    1147         476 :   C->R = gel(z,1);
    1148         476 :   a = gel(gel(z,2), 2);
    1149         476 :   C->p = RgX_RgXQ_eval(p, a, C->R);
    1150         476 :   C->q = RgX_RgXQ_eval(q, a, C->R);
    1151         476 :   C->rev = QXQ_reverse(a, C->R);
    1152         476 :   if (DEBUGLEVEL>1) err_printf("polred(compositum) = %Ps\n",C->R);
    1153         476 : }
    1154             : 
    1155             : /* replace P->C^(-deg P) P(xC) for the largest integer C such that coefficients
    1156             :  * remain algebraic integers. Lift *rational* coefficients */
    1157             : static void
    1158         525 : nfX_Z_normalize(GEN nf, GEN P)
    1159             : {
    1160             :   long i, l;
    1161         525 :   GEN C, Cj, PZ = cgetg_copy(P, &l);
    1162         525 :   PZ[1] = P[1];
    1163        2779 :   for (i = 2; i < l; i++) /* minor variation on RgX_to_nfX (create PZ) */
    1164             :   {
    1165        2254 :     GEN z = nf_to_scalar_or_basis(nf, gel(P,i));
    1166        2254 :     if (typ(z) == t_INT)
    1167        1680 :       gel(PZ,i) = gel(P,i) = z;
    1168             :     else
    1169         574 :       gel(PZ,i) = ZV_content(z);
    1170             :   }
    1171         525 :   (void)ZX_Z_normalize(PZ, &C);
    1172             : 
    1173         525 :   if (C == gen_1) return;
    1174         119 :   Cj = C;
    1175         490 :   for (i = l-2; i > 1; i--)
    1176             :   {
    1177         371 :     if (i != l-2) Cj = mulii(Cj, C);
    1178         371 :     gel(P,i) = gdiv(gel(P,i), Cj);
    1179             :   }
    1180             : }
    1181             : 
    1182             : static GEN
    1183         476 : _rnfkummer_step4(GEN bnfz, GEN gen, GEN cycgen, GEN u, ulong ell, long rc,
    1184             :                  long d, long m, long g, tau_s *tau)
    1185             : {
    1186         476 :   GEN Q, vecC, vecB = cgetg(rc+1,t_VEC), Tc = cgetg(rc+1,t_MAT);
    1187             :   long i, j;
    1188         826 :   for (j=1; j<=rc; j++)
    1189             :   {
    1190         350 :     GEN p1 = tauofideal(gel(gen,j), tau);
    1191         350 :     p1 = isprincipalell(bnfz, p1, cycgen,u,ell,rc);
    1192         350 :     gel(Tc,j)  = gel(p1,1);
    1193         350 :     gel(vecB,j)= gel(p1,2);
    1194             :   }
    1195             : 
    1196         476 :   if (!rc) vecC = cgetg(1,t_VEC);
    1197             :   else
    1198             :   {
    1199             :     GEN p1, p2;
    1200         266 :     vecC = const_vec(rc, trivial_fact());
    1201         266 :     p1 = Flm_powers(Tc, m-2, ell);
    1202         266 :     p2 = vecB;
    1203         602 :     for (j=1; j<=m-1; j++)
    1204             :     {
    1205         336 :       GEN z = Flm_Fl_mul(gel(p1,m-j), Fl_mul(j,d,ell), ell);
    1206         336 :       p2 = tauofvec(p2, tau);
    1207         770 :       for (i=1; i<=rc; i++)
    1208         868 :         gel(vecC,i) = famat_mul_shallow(gel(vecC,i),
    1209         434 :                                         famat_factorbacks(p2, gel(z,i)));
    1210             :     }
    1211         266 :     for (i=1; i<=rc; i++) gel(vecC,i) = famat_reduce(gel(vecC,i));
    1212             :   }
    1213         476 :   Q = Flm_ker(Flm_Fl_add(Flm_transpose(Tc), Fl_neg(g, ell), ell), ell);
    1214         476 :   return mkvec2(vecC, Q);
    1215             : }
    1216             : 
    1217             : static GEN
    1218         476 : _rnfkummer_step5(GEN bnfz, GEN vselmer, GEN cycgen, GEN gell, long rc,
    1219             :                  long rv, long g, tau_s *tau)
    1220             : {
    1221         476 :   GEN Tv, P, vecW, cyc = bnf_get_cyc(bnfz);
    1222             :   long j, lW;
    1223         476 :   ulong ell = itou(gell);
    1224         476 :   Tv = cgetg(rv+1,t_MAT);
    1225        2114 :   for (j=1; j<=rv; j++)
    1226             :   {
    1227        1638 :     GEN p1 = tauofelt(gel(vselmer,j), tau);
    1228        1638 :     if (typ(p1) == t_MAT) /* famat */
    1229         350 :       p1 = nffactorback(bnfz, gel(p1,1), FpC_red(gel(p1,2),gell));
    1230        1638 :     gel(Tv,j) = ZV_to_Flv(isvirtualunit(bnfz, p1, cycgen,cyc,gell,rc), ell);
    1231             :   }
    1232         476 :   P = Flm_ker(Flm_Fl_add(Tv, Fl_neg(g, ell), ell), ell);
    1233         476 :   lW = lg(P);
    1234         476 :   vecW = cgetg(lW,t_VEC);
    1235         476 :   for (j=1; j<lW; j++) gel(vecW,j) = famat_factorbacks(vselmer, gel(P,j));
    1236         476 :   return vecW;
    1237             : }
    1238             : 
    1239             : static GEN
    1240         504 : _rnfkummer_step18(toK_s *T, GEN bnr, GEN subgroup, GEN bnfz, GEN M,
    1241             :      GEN vecWB, GEN vecMsup, ulong g, ulong ell, long lW, long all)
    1242             : {
    1243         504 :   long i, dK, ncyc = 0;
    1244         504 :   GEN bnf = bnr_get_bnf(bnr), nf  = bnf_get_nf(bnf), polnf = nf_get_pol(nf);
    1245         504 :   GEN nfz = bnf_get_nf(bnfz), gell = utoipos(ell);
    1246         504 :   GEN K, y, res = NULL, mat = NULL;
    1247         504 :   long firstpass = all < 0, rk = 0;
    1248         504 :   K = Flm_ker(M, ell);
    1249         504 :   if (all < 0) K = fix_kernel(K, M, vecMsup, lW, ell);
    1250         504 :   if (DEBUGLEVEL>2) err_printf("Step 18\n");
    1251         504 :   dK = lg(K)-1;
    1252         504 :   y = cgetg(dK+1,t_VECSMALL);
    1253         504 :   if (all) res = cgetg(1, t_VEC);
    1254         504 :   if (all < 0) { ncyc = dK; rk = 0; mat = zero_Flm(lg(M)-1, ncyc); }
    1255             : 
    1256             :   do {
    1257         504 :     dK = lg(K)-1;
    1258        1029 :     while (dK)
    1259             :     {
    1260         511 :       for (i=1; i<dK; i++) y[i] = 0;
    1261         511 :       y[i] = 1; /* y = [0,...,0,1,0,...,0], 1 at dK'th position */
    1262             :       do
    1263             :       { /* cf. algo 5.3.18 */
    1264         525 :         GEN H, be, P, X = Flm_Flc_mul(K, y, ell);
    1265         525 :         if (ok_congruence(X, ell, lW, vecMsup))
    1266             :         {
    1267         525 :           pari_sp av = avma;
    1268         525 :           if (all < 0)
    1269             :           {
    1270           0 :             gel(mat, rk+1) = X;
    1271           0 :             if (Flm_rank(mat,ell) <= rk) continue;
    1272           0 :             rk++;
    1273             :           }
    1274         525 :           be = compute_beta(X, vecWB, gell, bnfz);
    1275         525 :           P = compute_polrel(nfz, T, be, g, ell);
    1276         525 :           nfX_Z_normalize(nf, P);
    1277         525 :           if (DEBUGLEVEL>1) err_printf("polrel(beta) = %Ps\n", P);
    1278         525 :           if (!all) {
    1279         490 :             H = rnfnormgroup(bnr, P);
    1280         490 :             if (ZM_equal(subgroup, H)) return P; /* DONE */
    1281           0 :             set_avma(av); continue;
    1282             :           } else {
    1283          35 :             GEN P0 = Q_primpart(lift_shallow(P));
    1284          35 :             GEN g = nfgcd(P0, RgX_deriv(P0), polnf, nf_get_index(nf));
    1285          35 :             if (degpol(g)) continue;
    1286          35 :             H = rnfnormgroup(bnr, P);
    1287          35 :             if (!ZM_equal(subgroup,H) && !bnrisconductor(bnr,H)) continue;
    1288             :           }
    1289          35 :           P = gerepilecopy(av, P);
    1290          35 :           res = shallowconcat(res, P);
    1291          35 :           if (all < 0 && rk == ncyc) return res;
    1292          35 :           if (firstpass) break;
    1293             :         }
    1294          35 :       } while (increment(y, dK, ell));
    1295          21 :       y[dK--] = 0;
    1296             :     }
    1297          14 :   } while (firstpass--);
    1298          14 :   if (!res) pari_err_BUG("kummer [no solution]");
    1299          14 :   return res;
    1300             : }
    1301             : 
    1302             : struct rnfkummer
    1303             : {
    1304             :   GEN bnfz, u, vecC, Q, vecW;
    1305             :   ulong g, ell;
    1306             :   long rc;
    1307             :   compo_s COMPO;
    1308             :   tau_s tau;
    1309             :   toK_s T;
    1310             : };
    1311             : 
    1312             : static void
    1313         476 : rnfkummer_init(struct rnfkummer *kum, GEN bnf, ulong ell, long prec)
    1314             : {
    1315         476 :   compo_s *COMPO = &kum->COMPO;
    1316         476 :   tau_s *tau = &kum->tau;
    1317         476 :   toK_s *T = &kum->T;
    1318         476 :   GEN nf  = bnf_get_nf(bnf), polnf = nf_get_pol(nf), gell = utoi(ell);
    1319             :   GEN vselmer, bnfz, nfz, cyc, gen, cycgen, step4, u, vecC, vecW, Q;
    1320         476 :   long vnf = varn(polnf), rc, ru, rv, degK, degKz, m, d;
    1321             :   ulong g;
    1322             :   pari_timer ti;
    1323             :   /* step 1 of alg 5.3.5. */
    1324         476 :   if (DEBUGLEVEL>2) err_printf("Step 1\n");
    1325         476 :   compositum_red(COMPO, polnf, polcyclo(ell,vnf));
    1326             :   /* step 2 */
    1327         476 :   if (DEBUGLEVEL>2) err_printf("Step 2\n");
    1328         476 :   if (DEBUGLEVEL) timer_printf(&ti, "[rnfkummer] compositum");
    1329         476 :   degK  = degpol(polnf);
    1330         476 :   degKz = degpol(COMPO->R);
    1331         476 :   m = degKz / degK;
    1332         476 :   d = (ell-1) / m;
    1333         476 :   g = Fl_powu(pgener_Fl(ell), d, ell);
    1334         476 :   if (Fl_powu(g, m, ell*ell) == 1) g += ell;
    1335             :   /* ord(g) = m in all (Z/ell^k)^* */
    1336             :   /* step 3 */
    1337         476 :   if (DEBUGLEVEL>2) err_printf("Step 3\n");
    1338             :   /* could factor disc(R) using th. 2.1.6. */
    1339         476 :   bnfz = Buchall(COMPO->R, nf_FORCE, maxss(prec,BIGDEFAULTPREC));
    1340         476 :   if (DEBUGLEVEL) timer_printf(&ti, "[rnfkummer] bnfinit(Kz)");
    1341         476 :   cycgen = bnf_build_cycgen(bnfz);
    1342         476 :   nfz = bnf_get_nf(bnfz);
    1343         476 :   cyc = bnf_get_cyc(bnfz); rc = prank(cyc,ell);
    1344         476 :   gen = bnf_get_gen(bnfz);
    1345         476 :   u = get_u(ZV_to_Flv(cyc, ell), rc, ell);
    1346             : 
    1347         476 :   vselmer = get_Selmer(bnfz, cycgen, rc);
    1348         476 :   if (DEBUGLEVEL) timer_printf(&ti, "[rnfkummer] Selmer group");
    1349         476 :   ru = (degKz>>1)-1;
    1350         476 :   rv = rc+ru+1;
    1351         476 :   get_tau(tau, nfz, COMPO, g);
    1352             : 
    1353             :   /* step 4 */
    1354         476 :   if (DEBUGLEVEL>2) err_printf("Step 4\n");
    1355         476 :   step4 = _rnfkummer_step4(bnfz, gen, cycgen, u, ell, rc, d, m, g, tau);
    1356         476 :   vecC = gel(step4,1);
    1357         476 :   Q    = gel(step4,2);
    1358             :   /* step 5 */
    1359         476 :   if (DEBUGLEVEL>2) err_printf("Step 5\n");
    1360         476 :   vecW = _rnfkummer_step5(bnfz, vselmer, cycgen, gell, rc, rv, g, tau);
    1361             :   /* step 8 */
    1362         476 :   if (DEBUGLEVEL>2) err_printf("Step 8\n");
    1363             :   /* left inverse */
    1364         476 :   T->invexpoteta1 = RgM_inv(RgXQ_matrix_pow(COMPO->p, degKz, degK, COMPO->R));
    1365         476 :   T->polnf = polnf;
    1366         476 :   T->tau = tau;
    1367         476 :   T->m = m;
    1368         476 :   T->powg = Fl_powers(g, m, ell);
    1369         476 :   kum->bnfz = bnfz;
    1370         476 :   kum->ell = ell;
    1371         476 :   kum->u = u;
    1372         476 :   kum->vecC = vecC;
    1373         476 :   kum->Q = Q;
    1374         476 :   kum->vecW = vecW;
    1375         476 :   kum->g = g;
    1376         476 :   kum->rc = rc;
    1377         476 : }
    1378             : 
    1379             : static GEN
    1380         504 : rnfkummer_ell(struct rnfkummer *kum, GEN bnr, GEN subgroup, long all)
    1381             : {
    1382         504 :   ulong ell = kum->ell, mginv;
    1383         504 :   GEN bnfz = kum->bnfz, nfz = bnf_get_nf(bnfz), cycgen = bnf_build_cycgen(bnfz);
    1384         504 :   GEN nf = bnr_get_nf(bnr), bid = bnr_get_bid(bnr), ideal = bid_get_ideal(bid);
    1385         504 :   GEN vecC = kum->vecC, vecW = kum->vecW, u = kum->u, Q = kum->Q;
    1386         504 :   long lW = lg(vecW), rc = kum->rc, i, j, lSml2, lSp, lSl2, dc;
    1387         504 :   toK_s *T = &kum->T;
    1388         504 :   ulong g = kum->g;
    1389             :   primlist L;
    1390             :   GEN gothf, idealz, Sp, prSp, vecAp, vecBp, matP, vecWA, vecWB, vecMsup;
    1391             :   GEN M;
    1392         504 :   idealz = ideallifttoKz(nfz, nf, ideal, &kum->COMPO);
    1393         504 :   if (umodiu(gcoeff(ideal,1,1), ell)) gothf = idealz;
    1394             :   else
    1395             :   { /* ell | N(ideal) */
    1396         133 :     GEN bnrz = Buchray(bnfz, idealz, nf_INIT|nf_GEN);
    1397         133 :     GEN subgroupz = invimsubgroup(bnrz, bnr, subgroup, T);
    1398         133 :     gothf = bnrconductor_i(bnrz,subgroupz,0);
    1399             :   }
    1400             :   /* step 9, 10, 11 */
    1401         504 :   if (DEBUGLEVEL>2) err_printf("Step 9, 10 and 11\n");
    1402         504 :   i = build_list_Hecke(&L, nfz, NULL, gothf, ell, T->tau);
    1403         504 :   if (i) return no_sol(all,i);
    1404             : 
    1405         504 :   lSml2 = lg(L.Sml2);
    1406         504 :   Sp = shallowconcat(L.Sm, L.Sml1); lSp = lg(Sp);
    1407         504 :   prSp = shallowconcat(L.Sml2, L.Sl); lSl2 = lg(prSp);
    1408             : 
    1409             :   /* step 12 */
    1410         504 :   if (DEBUGLEVEL>2) err_printf("Step 12\n");
    1411         504 :   vecAp = cgetg(lSp, t_VEC);
    1412         504 :   vecBp = cgetg(lSp, t_VEC);
    1413         504 :   matP  = cgetg(lSp, t_MAT);
    1414             : 
    1415         770 :   for (j = 1; j < lSp; j++)
    1416             :   {
    1417             :     GEN e, a;
    1418         266 :     GEN p1 = isprincipalell(bnfz, gel(Sp,j), cycgen,u,ell,rc);
    1419         266 :     e = gel(p1,1); gel(matP,j) = gel(p1, 1);
    1420         266 :     a = gel(p1,2);
    1421         266 :     gel(vecBp,j) = famat_mul_shallow(famat_factorbacks(vecC, zv_neg(e)), a);
    1422             :   }
    1423         504 :   vecAp = lambdaofvec(vecBp, T);
    1424             :   /* step 13 */
    1425         504 :   if (DEBUGLEVEL>2) err_printf("Step 13\n");
    1426         504 :   vecWA = shallowconcat(vecW, vecAp);
    1427         504 :   vecWB = shallowconcat(vecW, vecBp);
    1428             : 
    1429             :   /* step 14, 15, and 17 */
    1430         504 :   if (DEBUGLEVEL>2) err_printf("Step 14, 15 and 17\n");
    1431         504 :   mginv = Fl_div(T->m, g, ell);
    1432         504 :   vecMsup = cgetg(lSml2,t_VEC);
    1433         504 :   M = NULL;
    1434        1169 :   for (i = 1; i < lSl2; i++)
    1435             :   {
    1436         665 :     GEN pr = gel(prSp,i);
    1437         665 :     long e = pr_get_e(pr), z = ell * (e / (ell-1));
    1438             : 
    1439         665 :     if (i < lSml2)
    1440             :     {
    1441         140 :       z += 1 - L.ESml2[i];
    1442         140 :       gel(vecMsup,i) = logall(nfz, vecWA,lW,mginv,ell, pr,z+1);
    1443             :     }
    1444         665 :     M = vconcat(M, logall(nfz, vecWA,lW,mginv,ell, pr,z));
    1445             :   }
    1446         504 :   dc = lg(Q)-1;
    1447         504 :   if (dc)
    1448             :   {
    1449         168 :     GEN QtP = Flm_mul(Flm_transpose(Q), matP, ell);
    1450         168 :     M = vconcat(M, shallowconcat(zero_Flm(dc,lW-1), QtP));
    1451             :   }
    1452         504 :   if (!M) M = zero_Flm(1, lSp-1 + lW-1);
    1453             : 
    1454         504 :   if (!all)
    1455             :   { /* primes landing in subgroup must be totally split */
    1456         490 :     GEN lambdaWB = shallowconcat(lambdaofvec(vecW, T), vecAp);/*vecWB^lambda*/
    1457         490 :     GEN Lpr = get_prlist(bnr, subgroup, ell, bnfz);
    1458         490 :     GEN Lprz= get_przlist(Lpr, nfz, nf, &kum->COMPO);
    1459         490 :     GEN M2 = subgroup_info(bnfz, Lprz, ell, lambdaWB);
    1460         490 :     M = vconcat(M, M2);
    1461             :   }
    1462         504 :   if (DEBUGLEVEL>2) err_printf("Step 16\n");
    1463             :   /* step 16 && 18 & ff */
    1464         504 :   return _rnfkummer_step18(T,bnr,subgroup,bnfz, M, vecWB, vecMsup, g, ell, lW, all);
    1465             : }
    1466             : 
    1467             : static GEN
    1468         966 : _rnfkummer(GEN bnr, GEN subgroup, long all, long prec)
    1469             : {
    1470             :   long vnf;
    1471             :   ulong ell;
    1472             :   GEN polnf, bnf, nf, gell, p1;
    1473             :   struct rnfkummer kum;
    1474             :   pari_timer t;
    1475             : 
    1476         966 :   if (DEBUGLEVEL) timer_start(&t);
    1477         966 :   checkbnr(bnr);
    1478         966 :   bnf = bnr_get_bnf(bnr);
    1479         966 :   nf  = bnf_get_nf(bnf);
    1480         966 :   polnf = nf_get_pol(nf); vnf = varn(polnf);
    1481         966 :   if (!vnf) pari_err_PRIORITY("rnfkummer", polnf, "=", 0);
    1482             :   /* step 7 */
    1483         966 :   p1 = bnrconductor_i(bnr, subgroup, 2);
    1484         966 :   if (DEBUGLEVEL) timer_printf(&t, "[rnfkummer] conductor");
    1485         966 :   bnr      = gel(p1,2);
    1486         966 :   subgroup = gel(p1,3);
    1487         966 :   gell = get_gell(bnr,subgroup,all);
    1488         966 :   ell = itou(gell);
    1489         966 :   if (ell == 1) return pol_x(0);
    1490         966 :   if (!uisprime(ell)) pari_err_IMPL("kummer for composite relative degree");
    1491         966 :   if (all && all != -1 && umodiu(bnr_get_no(bnr), ell))
    1492           7 :     return cgetg(1, t_VEC);
    1493         959 :   if (bnf_get_tuN(bnf) % ell == 0)
    1494         609 :     return rnfkummersimple(bnr, subgroup, ell, all);
    1495             : 
    1496         350 :   if (all == -1) all = 0;
    1497         350 :   rnfkummer_init(&kum, bnr_get_bnf(bnr), ell, prec);
    1498         350 :   return rnfkummer_ell(&kum, bnr, subgroup, all);
    1499             : }
    1500             : 
    1501             : GEN
    1502         763 : rnfkummer(GEN bnr, GEN subgroup, long all, long prec)
    1503             : {
    1504         763 :   pari_sp av = avma;
    1505         763 :   return gerepilecopy(av, _rnfkummer(bnr, subgroup, all, prec));
    1506             : }
    1507             : 
    1508             : /*******************************************************************/
    1509             : /*                        bnrclassfield                            */
    1510             : /*******************************************************************/
    1511             : 
    1512             : /* TODO: could be exported */
    1513             : static void
    1514      194313 : gsetvarn(GEN x, long v)
    1515             : {
    1516             :   long i;
    1517      194313 :   switch(typ(x))
    1518             :   {
    1519             :     case t_POL: case t_SER:
    1520        1428 :       setvarn(x, v); return;
    1521             :     case t_LIST:
    1522           0 :       x = list_data(x); if (!x) return;
    1523             :       /* fall through t_VEC */
    1524             :     case t_VEC: case t_COL: case t_MAT:
    1525       22575 :       for (i = lg(x)-1; i > 0; i--) gsetvarn(gel(x,i), v);
    1526             :   }
    1527             : }
    1528             : 
    1529             : /* emb root of pol as polmod modulo pol2, return relative polynomial */
    1530             : static GEN
    1531         182 : relative_pol(GEN pol, GEN emb, GEN pol2)
    1532             : {
    1533             :   GEN eqn, polrel;
    1534         182 :   if (degree(pol)==1) return pol2;
    1535         140 :   emb = liftpol(emb);
    1536         140 :   eqn = gsub(emb, pol_x(varn(pol)));
    1537         140 :   eqn = Q_remove_denom(eqn, NULL);
    1538         140 :   polrel = nfgcd(pol2, eqn, pol, NULL);
    1539         140 :   return RgX_Rg_div(polrel, leading_coeff(polrel));
    1540             : }
    1541             : 
    1542             : /* pol defines K/nf */
    1543             : static GEN
    1544         203 : bnrclassfield_tower(GEN bnr, GEN subgroup, GEN TB, GEN p, long finaldeg, long absolute, long prec)
    1545             : {
    1546         203 :   pari_sp av = avma;
    1547             :   GEN nf, nf2, rnf, bnf, bnf2, bnr2, q, H, dec, cyc, pk, sgpk, pol2, emb, emb2, famod, fa, Lbad;
    1548             :   long i, r1, ell, sp, spk, last;
    1549             :   forprime_t iter;
    1550             : 
    1551         203 :   bnf = bnr_get_bnf(bnr);
    1552         203 :   nf = bnf_get_nf(bnf);
    1553         203 :   rnf = rnfinit0(nf, TB, 1);
    1554         203 :   nf2 = rnf_build_nfabs(rnf, prec);
    1555         203 :   gsetvarn(nf2, varn(nf_get_pol(nf)));
    1556         203 :   r1 = nf_get_r1(nf2);
    1557         203 :   bnf2 = Buchall(nf2, 0, prec);
    1558             : 
    1559         203 :   sp = itos(p);
    1560         203 :   spk = sp * rnf_get_degree(rnf);
    1561         203 :   pk = stoi(spk);
    1562         203 :   sgpk = hnfmodid(subgroup,pk);
    1563         203 :   last = spk==finaldeg;
    1564             : 
    1565             :   /* compute conductor */
    1566         203 :   famod = gel(bid_get_fact2(bnr_get_bid(bnr)),1);
    1567         203 :   if (lg(famod)==1)
    1568             :   {
    1569         140 :     fa = trivial_fact();
    1570         140 :     Lbad = cgetg(1, t_VECSMALL);
    1571             :   }
    1572             :   else
    1573             :   {
    1574          63 :     long j=1;
    1575          63 :     fa = cgetg(3, t_MAT);
    1576          63 :     gel(fa,1) = cgetg(lg(famod), t_VEC);
    1577          63 :     Lbad = cgetg(lg(famod), t_VEC);
    1578         161 :     for(i=1; i<lg(famod); i++)
    1579             :     {
    1580          98 :       GEN pr = gel(famod,i);
    1581          98 :       gmael(fa,1,i) = rnfidealprimedec(rnf, pr);
    1582          98 :       q = pr_get_p(pr);
    1583          98 :       if (lgefint(q) == 3) gel(Lbad,j++) = q;
    1584             :     }
    1585          63 :     setlg(Lbad,j);
    1586          63 :     Lbad = ZV_to_zv(ZV_sort_uniq(Lbad));
    1587          63 :     gel(fa,1) = shallowconcat1(gel(fa,1));
    1588          63 :     settyp(gel(fa,1), t_COL);
    1589          63 :     gel(fa,2) = cgetg(lg(gel(fa,1)), t_COL);
    1590         175 :     for (i=1; i<lg(gel(fa,1)); i++)
    1591             :     {
    1592         112 :       GEN pr = gcoeff(fa,i,1);
    1593         112 :       long e = equalii(p, pr_get_p(pr))? 1 + (pr_get_e(pr)*sp) / (sp-1): 1;
    1594         112 :       gcoeff(fa,i,2) = utoipos(e);
    1595             :     }
    1596             :   }
    1597         203 :   bnr2 = bnrinit0(bnf2, mkvec2(fa, const_vec(r1,gen_1)), 0);
    1598             : 
    1599             :   /* compute subgroup */
    1600         203 :   cyc = bnr_get_cyc(bnr2);
    1601         203 :   H = Flm_image(zv_diagonal(ZV_to_Flv(cyc,sp)), sp);
    1602         203 :   u_forprime_init(&iter, 2, ULONG_MAX);
    1603       12467 :   while ((ell = u_forprime_next(&iter))) if (!zv_search(Lbad, ell))
    1604             :   {
    1605       12166 :     dec = idealprimedec_limit_f(nf, utoi(ell), 1);
    1606       23891 :     for (i=1; i<lg(dec); i++)
    1607             :     {
    1608       11725 :       GEN pr = gel(dec,i), Pr = gel(rnfidealprimedec(rnf, pr), 1);
    1609       11725 :       long f = pr_get_f(Pr) / pr_get_f(pr);
    1610       11725 :       GEN vpr = FpC_Fp_mul(isprincipalray(bnr, pr), utoi(f), pk);
    1611       11725 :       if (gequal0(ZC_hnfrem(vpr,sgpk)))
    1612        1176 :         H = vec_append(H, ZV_to_Flv(isprincipalray(bnr2, Pr), sp));
    1613             :     }
    1614       12166 :     if (lg(H) > lg(cyc)+3)
    1615             :     {
    1616         203 :       H = Flm_image(H, sp);
    1617         203 :       if (lg(cyc)-lg(H) == 1) break;
    1618             :     }
    1619             :   }
    1620         203 :   H = hnfmodid(shallowconcat(zm_to_ZM(H), diagonal_shallow(cyc)), p);
    1621             : 
    1622             :   /* polynomial over nf2 */
    1623         203 :   pol2 = _rnfkummer(bnr2, H, 0, prec);
    1624             :   /* absolute polynomial */
    1625         203 :   pol2 = rnfequation2(nf2, pol2);
    1626         203 :   emb2 = gel(pol2,2); /* generator of nf2 as polmod modulo pol2 */
    1627         203 :   pol2 = gel(pol2,1);
    1628             :   /* polynomial over nf */
    1629         203 :   if (!absolute || !last)
    1630             :   {
    1631         182 :     emb = rnf_get_alpha(rnf); /* generator of nf as polynomial in nf2 */
    1632         182 :     emb = poleval(emb, emb2); /* generator of nf as polmod modulo pol2 */
    1633         182 :     pol2 = relative_pol(nf_get_pol(nf), emb, pol2);
    1634             :   }
    1635         203 :   if (!last) pol2 = rnfpolredbest(nf, pol2, 0);
    1636             : 
    1637         203 :   obj_free(rnf);
    1638         203 :   pol2 = gerepilecopy(av, pol2);
    1639         203 :   if (last) return pol2;
    1640          56 :   TB = mkvec2(pol2, gel(TB,2));
    1641          56 :   return bnrclassfield_tower(bnr, subgroup, TB, p, finaldeg, absolute, prec);
    1642             : }
    1643             : 
    1644             : /* subgroups H_i of bnr s.t. bnr/H_i is cyclic and inter_i H_i = subgroup */
    1645             : static GEN
    1646         350 : cyclic_compos(GEN subgroup)
    1647             : {
    1648         350 :   pari_sp av = avma;
    1649         350 :   GEN U, L, pe, D = smithclean( ZM_snfall_i(subgroup, &U, NULL, 1) );
    1650         350 :   long i, l = lg(D);
    1651             : 
    1652         350 :   L = cgetg(l, t_VEC);
    1653         350 :   if (l == 1) return L;
    1654         350 :   pe = gel(D,1); U = matinvmod(U, pe);
    1655         875 :   for (i = 1; i < l; i++)
    1656         525 :     gel(L,i) = hnfmodid(shallowconcat(subgroup, vecsplice(U,i)),pe);
    1657         350 :   return gerepilecopy(av, L);
    1658             : }
    1659             : 
    1660             : /* set kum=NULL if roots of unity already in base field */
    1661             : /* absolute=1 allowed if extension is cyclic with exponent>1 */
    1662             : static GEN
    1663         350 : bnrclassfield_primepower(struct rnfkummer *ptkum, GEN bnr, GEN subgroup, GEN p,
    1664             :   GEN P, long absolute, long prec)
    1665             : {
    1666         350 :   GEN res, subs = cyclic_compos(subgroup);
    1667         350 :   long i, l = lg(subs);
    1668             : 
    1669         350 :   res = cgetg(l,t_VEC);
    1670         875 :   for (i = 1; i < l; i++)
    1671             :   {
    1672         525 :     GEN H = gel(subs,i), cnd = bnrconductor_i(bnr, hnfmodid(H,p), 2);
    1673         525 :     GEN pol, pe, bnr2 = gel(cnd,2), Hp = gel(cnd,3);
    1674         525 :     if (ptkum)  pol = rnfkummer_ell(ptkum, bnr2, Hp, 0);
    1675         371 :     else        pol = rnfkummersimple(bnr2, Hp, itos(p), 0);
    1676         525 :     pe = ZM_det_triangular(H);
    1677         525 :     if (!equalii(p,pe))
    1678         147 :       pol = bnrclassfield_tower(bnr, H, mkvec2(pol,P), p, itos(pe), absolute, prec);
    1679         525 :     gel(res,i) = pol;
    1680             :   }
    1681         350 :   return res;
    1682             : }
    1683             : 
    1684             : static void
    1685         315 : bnrclassfield_sanitize(GEN *pbnr, GEN *pH)
    1686             : {
    1687         315 :   GEN cyc, cnd, bnr = *pbnr, H = *pH;
    1688         315 :   if (nftyp(bnr)==typ_BNF) bnr = bnrinit0(bnr, gen_1, 0); else checkbnr(bnr);
    1689         301 :   cyc = bnr_get_cyc(bnr);
    1690         301 :   if (!H) H = gen_0;
    1691         301 :   switch(typ(H))
    1692             :   {
    1693         245 :     case t_INT: H = scalarmat_shallow(H, lg(cyc)-1);
    1694         294 :     case t_MAT: H = hnfmodid(H, cyc); break;
    1695           7 :     default: pari_err_TYPE("bnrclassfield [subgroup]", H);
    1696             :   }
    1697         280 :   cnd = bnrconductor_i(bnr, H, 2);
    1698         280 :   *pbnr = gel(cnd,2);
    1699         280 :   *pH = gel(cnd,3);
    1700         280 : }
    1701             : 
    1702             : /* partition of v into two subsets whose products are as balanced as possible */
    1703             : /* assume v sorted */
    1704             : static GEN
    1705          98 : vecsmall_balance(GEN v)
    1706             : {
    1707             :   forvec_t T;
    1708          98 :   GEN xbounds, x, vuniq, mult, ind, prod, prodbest = gen_0, bound,
    1709          98 :       xbest = NULL, res1, res2;
    1710          98 :   long i=1, j, k1, k2;
    1711          98 :   if (lg(v) == 3) return mkvec2(mkvecsmall(1), mkvecsmall(2));
    1712          35 :   vuniq = cgetg(lg(v), t_VECSMALL);
    1713          35 :   mult = cgetg(lg(v), t_VECSMALL);
    1714          35 :   ind = cgetg(lg(v), t_VECSMALL);
    1715          35 :   vuniq[1] = v[1];
    1716          35 :   mult[1] = 1;
    1717          35 :   ind[1] = 1;
    1718         140 :   for (j=2; j<lg(v); j++)
    1719             :   {
    1720         105 :     if (v[j] == vuniq[i]) mult[i]++;
    1721             :     else
    1722             :     {
    1723          14 :       i++;
    1724          14 :       vuniq[i] = v[j];
    1725          14 :       mult[i] = 1;
    1726          14 :       ind[i] = j;
    1727             :     }
    1728             :   }
    1729          35 :   setlg(vuniq, ++i);
    1730          35 :   setlg(mult, i);
    1731          35 :   setlg(ind, i);
    1732             : 
    1733          35 :   vuniq = zv_to_ZV(vuniq);
    1734          35 :   prod = factorback2(vuniq, mult);
    1735          35 :   bound = sqrti(prod);
    1736          35 :   xbounds = cgetg(lg(mult), t_VEC);
    1737          35 :   for (i=1; i<lg(mult); i++) gel(xbounds,i) = mkvec2s(0,mult[i]);
    1738             : 
    1739          35 :   forvec_init(&T, xbounds, 0);
    1740         273 :   while ((x = forvec_next(&T)))
    1741             :   {
    1742         203 :     prod = factorback2(vuniq, x);
    1743         203 :     if (cmpii(prod,bound)<=0 && cmpii(prod,prodbest)>0)
    1744             :     {
    1745          91 :       prodbest = prod;
    1746          91 :       xbest = gcopy(x);
    1747             :     }
    1748             :   }
    1749          35 :   res1 = cgetg(lg(v), t_VECSMALL);
    1750          35 :   res2 = cgetg(lg(v), t_VECSMALL);
    1751          84 :   for (i=1,k1=1,k2=1; i<lg(xbest); i++)
    1752             :   {
    1753          49 :     for (j=0; j<itos(gel(xbest,i)); j++) res1[k1++] = ind[i]+j;
    1754          49 :     for (; j<mult[i]; j++)               res2[k2++] = ind[i]+j;
    1755             :   }
    1756          35 :   setlg(res1, k1);
    1757          35 :   setlg(res2, k2); return mkvec2(res1, res2);
    1758             : }
    1759             : 
    1760             : /* TODO nfcompositum should accept vectors of pols */
    1761             : /* assume all fields are linearly disjoint */
    1762             : /* assume the polynomials are sorted by degree */
    1763             : static GEN
    1764         273 : nfcompositumall(GEN nf, GEN L)
    1765             : {
    1766             :   GEN pol, vdeg, part;
    1767             :   long i;
    1768         273 :   if (lg(L)==2) return gel(L,1);
    1769          98 :   vdeg = cgetg(lg(L), t_VECSMALL);
    1770          98 :   for (i=1; i<lg(L); i++) vdeg[i] = degree(gel(L,i));
    1771          98 :   part = vecsmall_balance(vdeg);
    1772          98 :   pol = cgetg(3, t_VEC);
    1773         294 :   for (i = 1; i < 3; i++)
    1774             :   {
    1775         196 :     GEN L2 = vecpermute(L, gel(part,i)), T = nfcompositumall(nf, L2);
    1776         196 :     gel(pol,i) = rnfpolredbest(nf, T, 0);
    1777             :   }
    1778          98 :   return nfcompositum(nf, gel(pol,1), gel(pol,2), 2);
    1779             : }
    1780             : 
    1781             : static GEN
    1782         329 : bnrclassfield_i(GEN bnr, GEN subgroup, long flag, long prec)
    1783             : {
    1784             :   GEN N, fa, res, bnf, nf, P, PN, Pmod, EN;
    1785             :   long i, absolute, lPN;
    1786             :   struct rnfkummer kum;
    1787         329 :   if (flag<0 || flag>2) pari_err_FLAG("bnrclassfield [must be 0,1 or 2]");
    1788         315 :   bnrclassfield_sanitize(&bnr, &subgroup);
    1789             : 
    1790         280 :   N = ZM_det_triangular(subgroup);
    1791         280 :   if (equali1(N)) return pol_x(0);
    1792         273 :   fa = Z_factor(N);
    1793         273 :   PN = gel(fa,1); lPN = lg(PN);
    1794         273 :   EN = gel(fa,2);
    1795         630 :   for (i = 1; i < lPN; i++)
    1796         364 :     if (lgefint(gel(PN,i)) > 3)
    1797           7 :       pari_err_OVERFLOW("bnrclassfield [extension of too large degree]");
    1798         266 :   bnf = bnr_get_bnf(bnr);
    1799         266 :   nf = bnf_get_nf(bnf);
    1800         266 :   res = cgetg(lPN, t_VEC);
    1801             : 
    1802             :   /* one prime, exponent > 1 */
    1803         266 :   absolute = flag==2 && lPN==2 && !equali1(gel(EN,1));
    1804             : 
    1805         266 :   P = gel(absZ_factor(nf_get_disc(nf)),1);
    1806         266 :   Pmod = shallowcopy(gel(bid_get_fact(bnr_get_bid(bnr)),1));
    1807         266 :   for (i=1; i<lg(Pmod); i++) gel(Pmod,i) = pr_get_p(gel(Pmod,i));
    1808         266 :   P = ZV_sort_uniq(shallowconcat(P, Pmod));
    1809             : 
    1810         616 :   for (i=1; i<lg(res); i++)
    1811             :   {
    1812         350 :     struct rnfkummer *pkum = NULL;
    1813         350 :     GEN p = gel(PN,i), H = hnfmodid(subgroup, powii(p, gel(EN,i)));
    1814         350 :     long sp = itos(p);
    1815         350 :     absolute &= (lg(H)==2 || equali1(gcoeff(H,2,2))); /* cyclic */
    1816         350 :     if (bnf_get_tuN(bnf) % sp)
    1817             :     {
    1818         126 :       pkum = &kum;
    1819         126 :       rnfkummer_init(pkum, bnf, sp, prec);
    1820             :     }
    1821         350 :     gel(res,i) = bnrclassfield_primepower(pkum, bnr, H, p, P, absolute, prec);
    1822             :   }
    1823         266 :   res = liftpol_shallow(shallowconcat1(res));
    1824         266 :   res = gen_sort(res, (void*)cmp_RgX, gen_cmp_RgX);
    1825         266 :   if (flag)
    1826             :   {
    1827          77 :     res = nfcompositumall(nf, res);
    1828          77 :     if (flag==2 && !absolute) res = rnfequation(nf, res);
    1829             :   }
    1830         266 :   return res;
    1831             : }
    1832             : 
    1833             : /* flag:
    1834             :  * 0 list of polynomials whose compositum is the extension
    1835             :  * 1 single polynomial
    1836             :  * 2 single absolute polynomial */
    1837             : GEN
    1838         329 : bnrclassfield(GEN bnr, GEN subgroup, long flag, long prec)
    1839             : {
    1840         329 :   pari_sp av = avma;
    1841         329 :   return gerepilecopy(av, bnrclassfield_i(bnr, subgroup, flag, prec));
    1842             : }

Generated by: LCOV version 1.13