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 - subcyclo.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 564 611 92.3 %
Date: 2022-07-03 07:33:15 Functions: 42 45 93.3 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_subcyclo
      19             : 
      20             : /*************************************************************************/
      21             : /**                                                                     **/
      22             : /**              Routines for handling subgroups of (Z/nZ)^*            **/
      23             : /**              without requiring discrete logarithms.                 **/
      24             : /**                                                                     **/
      25             : /*************************************************************************/
      26             : /* Subgroups are [gen,ord,bits] where
      27             :  * gen is a vecsmall of generators
      28             :  * ord is theirs relative orders
      29             :  * bits is a bit vector of the elements, of length(n). */
      30             : 
      31             : /*The algorithm is similar to testpermutation*/
      32             : static void
      33       85477 : znstar_partial_coset_func(long n, GEN H, void (*func)(void *data,long c)
      34             :     , void *data, long d, long c)
      35             : {
      36             :   GEN gen, ord, cache;
      37             :   long i, j, card;
      38             : 
      39       85477 :   if (!d) { (*func)(data,c); return; }
      40             : 
      41       61502 :   cache = const_vecsmall(d,c);
      42       61502 :   (*func)(data,c);  /* AFTER cache: may contain gerepileupto statement */
      43       61502 :   gen = gel(H,1);
      44       61502 :   ord = gel(H,2);
      45      122353 :   card = ord[1]; for (i = 2; i <= d; i++) card *= ord[i];
      46    73504522 :   for(i=1; i<card; i++)
      47             :   {
      48    73443020 :     long k, m = i;
      49    74016348 :     for(j=1; j<d && m%ord[j]==0 ;j++) m /= ord[j];
      50    73443020 :     cache[j] = Fl_mul(cache[j],gen[j],n);
      51    74016348 :     for (k=1; k<j; k++) cache[k] = cache[j];
      52    73443020 :     (*func)(data, cache[j]);
      53             :   }
      54             : }
      55             : 
      56             : static void
      57        4396 : znstar_coset_func(long n, GEN H, void (*func)(void *data,long c)
      58             :     , void *data, long c)
      59        4396 : { znstar_partial_coset_func(n, H, func,data, lg(gel(H,1))-1, c); }
      60             : 
      61             : /* Add the element of the bitvec of the coset c modulo the subgroup of H
      62             :  * generated by the first d generators to the bitvec bits.*/
      63             : 
      64             : static void
      65       81081 : znstar_partial_coset_bits_inplace(long n, GEN H, GEN bits, long d, long c)
      66             : {
      67       81081 :   pari_sp av = avma;
      68       81081 :   znstar_partial_coset_func(n,H, (void (*)(void *,long)) &F2v_set,
      69             :       (void *) bits, d, c);
      70       81081 :   set_avma(av);
      71       81081 : }
      72             : 
      73             : static void
      74        2240 : znstar_coset_bits_inplace(long n, GEN H, GEN bits, long c)
      75        2240 : { znstar_partial_coset_bits_inplace(n, H, bits, lg(gel(H,1))-1, c); }
      76             : 
      77             : static GEN
      78       78841 : znstar_partial_coset_bits(long n, GEN H, long d, long c)
      79             : {
      80       78841 :   GEN bits = zero_F2v(n);
      81       78841 :   znstar_partial_coset_bits_inplace(n,H,bits,d,c);
      82       78841 :   return bits;
      83             : }
      84             : 
      85             : /* Compute the bitvec of the elements of the subgroup of H generated by the
      86             :  * first d generators.*/
      87             : static GEN
      88       78841 : znstar_partial_bits(long n, GEN H, long d)
      89       78841 : { return znstar_partial_coset_bits(n, H, d, 1); }
      90             : 
      91             : /* Compute the bitvec of the elements of H. */
      92             : GEN
      93           0 : znstar_bits(long n, GEN H)
      94           0 : { return znstar_partial_bits(n,H,lg(gel(H,1))-1); }
      95             : 
      96             : /* Compute the subgroup of (Z/nZ)^* generated by the elements of
      97             :  * the vecsmall V */
      98             : GEN
      99       23597 : znstar_generate(long n, GEN V)
     100             : {
     101       23597 :   pari_sp av = avma;
     102       23597 :   GEN gen = cgetg(lg(V),t_VECSMALL);
     103       23597 :   GEN ord = cgetg(lg(V),t_VECSMALL), res = mkvec2(gen,ord);
     104       23597 :   GEN bits = znstar_partial_bits(n,NULL,0);
     105       23597 :   long i, r = 0;
     106     1388044 :   for(i=1; i<lg(V); i++)
     107             :   {
     108     1364447 :     ulong v = uel(V,i), g = v;
     109     1364447 :     long o = 0;
     110    36065484 :     while (!F2v_coeff(bits, (long)g)) { g = Fl_mul(g, v, (ulong)n); o++; }
     111     1364447 :     if (!o) continue;
     112       55244 :     r++;
     113       55244 :     gen[r] = v;
     114       55244 :     ord[r] = o+1;
     115       55244 :     cgiv(bits); bits = znstar_partial_bits(n,res,r);
     116             :   }
     117       23597 :   setlg(gen,r+1);
     118       23597 :   setlg(ord,r+1); return gerepilecopy(av, mkvec3(gen,ord,bits));
     119             : }
     120             : 
     121             : static ulong
     122        4697 : znstar_order(GEN H) { return zv_prod(gel(H,2)); }
     123             : 
     124             : /* Return the lists of element of H.
     125             :  * This can be implemented with znstar_coset_func instead. */
     126             : GEN
     127        3752 : znstar_elts(long n, GEN H)
     128             : {
     129        3752 :   long card = znstar_order(H);
     130        3752 :   GEN gen = gel(H,1), ord = gel(H,2);
     131        3752 :   GEN sg = cgetg(1 + card, t_VECSMALL);
     132             :   long k, j, l;
     133        3752 :   sg[1] = 1;
     134        5754 :   for (j = 1, l = 1; j < lg(gen); j++)
     135             :   {
     136        2002 :     long c = l * (ord[j]-1);
     137        4550 :     for (k = 1; k <= c; k++) sg[++l] = Fl_mul(sg[k], gen[j], n);
     138             :   }
     139        3752 :   vecsmall_sort(sg); return sg;
     140             : }
     141             : 
     142             : /* Take a znstar H and n dividing the modulus of H.
     143             :  * Output H reduced to modulus n */
     144             : GEN
     145         182 : znstar_reduce_modulus(GEN H, long n)
     146             : {
     147         182 :   pari_sp ltop=avma;
     148         182 :   GEN gen=cgetg(lgcols(H),t_VECSMALL);
     149             :   long i;
     150         637 :   for(i=1; i < lg(gen); i++)
     151         455 :     gen[i] = mael(H,1,i)%n;
     152         182 :   return gerepileupto(ltop, znstar_generate(n,gen));
     153             : }
     154             : 
     155             : /* Compute conductor of H, bits = H[3] */
     156             : long
     157        1498 : znstar_conductor_bits(GEN bits)
     158             : {
     159        1498 :   pari_sp av = avma;
     160        1498 :   long i, f = 1, cnd0 = bits[1];
     161        1498 :   GEN F = factoru(cnd0), P = gel(F,1), E = gel(F,2);
     162        4270 :   for (i = lg(P)-1; i > 0; i--)
     163             :   {
     164        2772 :     long p = P[i], e = E[i], cnd = cnd0;
     165        2870 :     for (  ; e >= 2; e--)
     166             :     {
     167         609 :       long q = cnd / p;
     168         609 :       if (!F2v_coeff(bits, 1 + q)) break;
     169          98 :       cnd = q;
     170             :     }
     171        2772 :     if (e == 1)
     172             :     {
     173        2261 :       if (p == 2) e = 0;
     174             :       else
     175             :       {
     176        1946 :         long h, g = pgener_Fl(p), q = cnd / p;
     177        1946 :         h = Fl_mul(g-1, Fl_inv(q % p, p), p); /* 1+h*q = g (mod p) */
     178        1946 :         if (F2v_coeff(bits, 1 + h*q)) e = 0;
     179             :       }
     180             :     }
     181        2772 :     if (e) f *= upowuu(p, e);
     182             :   }
     183        1498 :   return gc_long(av,f);
     184             : }
     185             : long
     186         847 : znstar_conductor(GEN H) { return znstar_conductor_bits(gel(H,3)); }
     187             : 
     188             : /* Compute the orbits of a subgroups of Z/nZ given by a generator
     189             :  * or a set of generators given as a vector.
     190             :  */
     191             : GEN
     192         469 : znstar_cosets(long n, long phi_n, GEN H)
     193             : {
     194         469 :   long k, c = 0, card = znstar_order(H), index = phi_n/card;
     195         469 :   GEN cosets = cgetg(index+1,t_VECSMALL);
     196         469 :   pari_sp ltop = avma;
     197         469 :   GEN bits = zero_F2v(n);
     198        2667 :   for (k = 1; k <= index; k++)
     199             :   {
     200        5845 :     for (c++ ; F2v_coeff(bits,c) || ugcd(c,n)!=1; c++);
     201        2198 :     cosets[k]=c;
     202        2198 :     znstar_coset_bits_inplace(n, H, bits, c);
     203             :   }
     204         469 :   set_avma(ltop); return cosets;
     205             : }
     206             : 
     207             : static GEN
     208           7 : znstar_quotient(long n, long phi_n, GEN H, GEN R, ulong l)
     209             : {
     210           7 :   long i, j, k, c = 0, card   = znstar_order(H), index  = phi_n/card;
     211           7 :   GEN cosets = cgetg(index+1,t_VECSMALL), mult = cgetg(index+1, t_VEC);
     212           7 :   GEN bits = zero_F2v(n), vbits= zero_F2m_copy(n,index);
     213          49 :   for (k = 1; k <= index; k++)
     214             :   {
     215         119 :     for (c++ ; F2v_coeff(bits,c) || ugcd(c,n)!=1; c++);
     216          42 :     cosets[k]=c;
     217          42 :     znstar_coset_bits_inplace(n, H, gel(vbits,k), c);
     218          42 :     F2v_add_inplace(bits, gel(vbits,k));
     219             :   }
     220             : 
     221          49 :   for (k = 1; k <= index; k++)
     222             :   {
     223          42 :     GEN v = cgetg(index+1, t_VECSMALL);
     224         294 :     for (j = 1; j <= index; j++)
     225             :     {
     226         252 :       long s = Fl_mul(cosets[k],cosets[j],n);
     227         882 :       for (i = 1; i <= index; i++)
     228         882 :         if (F2v_coeff(gel(vbits,i),s)) break;
     229         252 :       v[j] = R[i];
     230             :     }
     231          42 :     gel(mult,k) = v;
     232             :   }
     233           7 :   return Flv_Flm_polint(R, mult, l, 0);
     234             : }
     235             : 
     236             : /*************************************************************************/
     237             : /**                                                                     **/
     238             : /**                     znstar/HNF interface                            **/
     239             : /**                                                                     **/
     240             : /*************************************************************************/
     241             : 
     242             : static long
     243        2947 : mod_to_small(GEN x)
     244        2947 : { return itos(typ(x) == t_INTMOD ? gel(x,2): x); }
     245             : 
     246             : static GEN
     247        2170 : vecmod_to_vecsmall(GEN x)
     248        5117 : { pari_APPLY_long(mod_to_small(gel(x,i))) }
     249             : 
     250             : /* Convert a true znstar output by znstar to a `small znstar' */
     251             : GEN
     252        2170 : znstar_small(GEN zn)
     253             : {
     254        2170 :   GEN Z = cgetg(4,t_VEC);
     255        2170 :   gel(Z,1) = icopy(gmael3(zn,3,1,1));
     256        2170 :   gel(Z,2) = vec_to_vecsmall(gel(zn,2));
     257        2170 :   gel(Z,3) = vecmod_to_vecsmall(gel(zn,3)); return Z;
     258             : }
     259             : 
     260             : /* Compute generators for the subgroup of (Z/nZ)* given in HNF. */
     261             : GEN
     262        4172 : znstar_hnf_generators(GEN Z, GEN M)
     263             : {
     264        4172 :   long j, h, l = lg(M);
     265        4172 :   GEN gen = cgetg(l, t_VECSMALL);
     266        4172 :   pari_sp ltop = avma;
     267        4172 :   GEN zgen = gel(Z,3);
     268        4172 :   ulong n = itou(gel(Z,1));
     269        9121 :   for (j = 1; j < l; j++)
     270             :   {
     271        4949 :     GEN Mj = gel(M,j);
     272        4949 :     gen[j] = 1;
     273       12264 :     for (h = 1; h < l; h++)
     274             :     {
     275        7315 :       ulong u = itou(gel(Mj,h));
     276        7315 :       if (!u) continue;
     277        5376 :       gen[j] = Fl_mul(uel(gen,j), Fl_powu(uel(zgen,h), u, n), n);
     278             :     }
     279             :   }
     280        4172 :   set_avma(ltop); return gen;
     281             : }
     282             : 
     283             : GEN
     284        3752 : znstar_hnf(GEN Z, GEN M)
     285        3752 : { return znstar_generate(itos(gel(Z,1)),znstar_hnf_generators(Z,M)); }
     286             : 
     287             : GEN
     288        3752 : znstar_hnf_elts(GEN Z, GEN H)
     289             : {
     290        3752 :   pari_sp ltop = avma;
     291        3752 :   GEN G = znstar_hnf(Z,H);
     292        3752 :   long n = itos(gel(Z,1));
     293        3752 :   return gerepileupto(ltop, znstar_elts(n,G));
     294             : }
     295             : 
     296             : /*************************************************************************/
     297             : /**                                                                     **/
     298             : /**                     polsubcyclo                                     **/
     299             : /**                                                                     **/
     300             : /*************************************************************************/
     301             : 
     302             : static GEN
     303         504 : gscycloconductor(GEN g, long n, long flag)
     304             : {
     305         504 :   if (flag==2) retmkvec2(gcopy(g), stoi(n));
     306         497 :   return g;
     307             : }
     308             : 
     309             : static long
     310     1288539 : lift_check_modulus(GEN H, long n)
     311             : {
     312             :   long h;
     313     1288539 :   switch(typ(H))
     314             :   {
     315         105 :     case t_INTMOD:
     316         105 :       if (!equalsi(n, gel(H,1)))
     317           7 :         pari_err_MODULUS("galoissubcyclo", stoi(n), gel(H,1));
     318          98 :       H = gel(H,2); /* fall through */
     319     1288532 :     case t_INT:
     320     1288532 :       h = smodis(H,n);
     321     1288532 :       if (ugcd(h,n) != 1) pari_err_COPRIME("galoissubcyclo", H,stoi(n));
     322     1288525 :       return h ? h: 1;
     323             :   }
     324           0 :   pari_err_TYPE("galoissubcyclo [subgroup]", H);
     325             :   return 0;/*LCOV_EXCL_LINE*/
     326             : }
     327             : 
     328             : /* Compute z^ex using the baby-step/giant-step table powz
     329             :  * with only one multiply.
     330             :  * In the modular case, the result is not reduced. */
     331             : static GEN
     332     1655080 : polsubcyclo_powz(GEN powz, long ex)
     333             : {
     334     1655080 :   long m = lg(gel(powz,1))-1, q = ex/m, r = ex%m; /*ex=m*q+r*/
     335     1655080 :   GEN g = gmael(powz,1,r+1), G = gmael(powz,2,q+1);
     336     1655080 :   return (lg(powz)==4)? mulreal(g,G): gmul(g,G);
     337             : }
     338             : 
     339             : static GEN
     340        5985 : polsubcyclo_complex_bound(pari_sp av, GEN V, long prec)
     341             : {
     342        5985 :   GEN pol = real_i(roots_to_pol(V,0));
     343        5985 :   return gerepileuptoint(av, ceil_safe(gsupnorm(pol,prec)));
     344             : }
     345             : 
     346             : /* Newton sums mod le. if le==NULL, works with complex instead */
     347             : static GEN
     348       11032 : polsubcyclo_cyclic(long n, long d, long m ,long z, long g, GEN powz, GEN le)
     349             : {
     350       11032 :   GEN V = cgetg(d+1,t_VEC);
     351       11032 :   ulong base = 1;
     352             :   long i,k;
     353             :   pari_timer ti;
     354       11032 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     355       76244 :   for (i=1; i<=d; i++, base = Fl_mul(base,z,n))
     356             :   {
     357       65212 :     pari_sp av = avma;
     358       65212 :     long ex = base;
     359       65212 :     GEN s = gen_0;
     360      319032 :     for (k=0; k<m; k++, ex = Fl_mul(ex,g,n))
     361             :     {
     362      253820 :       s = gadd(s, polsubcyclo_powz(powz,ex));
     363      253820 :       if ((k&0xff)==0) s = gerepileupto(av,s);
     364             :     }
     365       65212 :     if (le) s = modii(s, le);
     366       65212 :     gel(V,i) = gerepileupto(av, s);
     367             :   }
     368       11032 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "polsubcyclo_cyclic");
     369       11032 :   return V;
     370             : }
     371             : 
     372             : struct _subcyclo_orbits_u
     373             : {
     374             :   GEN bab, gig;
     375             :   ulong l;
     376             :   ulong s;
     377             :   long m;
     378             : };
     379             : 
     380             : static void
     381           0 : _Fl_subcyclo_orbits(void *E, long k)
     382             : {
     383           0 :   struct _subcyclo_orbits_u *D = (struct _subcyclo_orbits_u *) E;
     384           0 :   ulong l = D->l;
     385           0 :   long q = k/D->m, r = k%D->m; /*k=m*q+r*/
     386           0 :   ulong g = uel(D->bab,r+1), G = uel(D->gig,q+1);
     387           0 :   D->s = Fl_add(D->s, Fl_mul(g, G, l), l);
     388           0 : }
     389             : 
     390             : /* Newton sums mod le. if le==NULL, works with complex instead */
     391             : static GEN
     392           0 : Fl_polsubcyclo_orbits(long n, GEN H, GEN O, ulong z, ulong l)
     393             : {
     394           0 :   long i, d = lg(O);
     395           0 :   GEN V = cgetg(d,t_VECSMALL);
     396             :   struct _subcyclo_orbits_u D;
     397           0 :   long m = 1+usqrt(n);
     398           0 :   D.l = l;
     399           0 :   D.m = m;
     400           0 :   D.bab = Fl_powers(z, m, l);
     401           0 :   D.gig = Fl_powers(uel(D.bab,m+1), m-1, l);
     402           0 :   for(i=1; i<d; i++)
     403             :   {
     404           0 :     D.s = 0;
     405           0 :     znstar_coset_func(n, H, _Fl_subcyclo_orbits, (void *) &D, O[i]);
     406           0 :     uel(V,i) = D.s;
     407             :   }
     408           0 :   return V;
     409             : }
     410             : 
     411             : struct _subcyclo_orbits_s
     412             : {
     413             :   GEN powz;
     414             :   GEN *s;
     415             :   ulong count;
     416             :   pari_sp ltop;
     417             : };
     418             : 
     419             : static void
     420     1401260 : _subcyclo_orbits(struct _subcyclo_orbits_s *data, long k)
     421             : {
     422     1401260 :   GEN powz = data->powz;
     423     1401260 :   GEN *s = data->s;
     424             : 
     425     1401260 :   if (!data->count) data->ltop = avma;
     426     1401260 :   *s = gadd(*s, polsubcyclo_powz(powz,k));
     427     1401260 :   data->count++;
     428     1401260 :   if ((data->count & 0xffUL) == 0) *s = gerepileupto(data->ltop, *s);
     429     1401260 : }
     430             : 
     431             : /* Newton sums mod le. if le==NULL, works with complex instead */
     432             : static GEN
     433         938 : polsubcyclo_orbits(long n, GEN H, GEN O, GEN powz, GEN le)
     434             : {
     435         938 :   long i, d = lg(O);
     436         938 :   GEN V = cgetg(d,t_VEC);
     437             :   struct _subcyclo_orbits_s data;
     438         938 :   long lle = le?lg(le)*2+1: 2*lg(gmael(powz,1,2))+3;/*dvmdii uses lx+ly space*/
     439         938 :   data.powz = powz;
     440        5334 :   for(i=1; i<d; i++)
     441             :   {
     442        4396 :     GEN s = gen_0;
     443        4396 :     pari_sp av = avma;
     444        4396 :     (void)new_chunk(lle);
     445        4396 :     data.count = 0;
     446        4396 :     data.s     = &s;
     447        4396 :     znstar_coset_func(n, H, (void (*)(void *,long)) _subcyclo_orbits,
     448        4396 :       (void *) &data, O[i]);
     449        4396 :     set_avma(av); /* HACK */
     450        4396 :     gel(V,i) = le? modii(s,le): gcopy(s);
     451             :   }
     452         938 :   return V;
     453             : }
     454             : 
     455             : static GEN
     456        6867 : polsubcyclo_start(long n, long d, long o, long e, GEN borne, long *ptr_val,long *ptr_l)
     457             : {
     458             :   pari_sp av;
     459             :   GEN le, z, gl;
     460             :   long i, l, val;
     461        6867 :   l = e*n+1;
     462       22393 :   while(!uisprime(l)) { l += n; e++; }
     463        6867 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: prime l=%ld\n",l);
     464        6867 :   gl = utoipos(l); av = avma;
     465        6867 :   if (!borne)
     466             :   { /* Use vecmax(Vec((x+o)^d)) = max{binomial(d,i)*o^i ;1<=i<=d} */
     467           0 :     i = d-(1+d)/(1+o);
     468           0 :     borne = mulii(binomial(utoipos(d),i),powuu(o,i));
     469             :   }
     470        6867 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: bound=2^%ld\n",expi(borne));
     471        6867 :   val = logint(shifti(borne,2), gl) + 1;
     472        6867 :   set_avma(av);
     473        6867 :   if (DEBUGLEVEL >= 4) err_printf("Subcyclo: val=%ld\n",val);
     474        6867 :   le = powiu(gl,val);
     475        6867 :   z = utoipos( Fl_powu(pgener_Fl(l), e, l) );
     476        6867 :   z = Zp_sqrtnlift(gen_1,utoipos(n),z,gl,val);
     477        6867 :   *ptr_val = val;
     478        6867 :   *ptr_l = l;
     479        6867 :   return gmodulo(z,le);
     480             : }
     481             : 
     482             : /*Fill in the powz table:
     483             :  *  powz[1]: baby-step
     484             :  *  powz[2]: giant-step
     485             :  *  powz[3] exists only if the field is real (value is ignored). */
     486             : static GEN
     487        5985 : polsubcyclo_complex_roots(long n, long real, long prec)
     488             : {
     489        5985 :   long i, m = (long)(1+sqrt((double) n));
     490        5985 :   GEN bab, gig, powz = cgetg(real?4:3, t_VEC);
     491             : 
     492        5985 :   bab = cgetg(m+1,t_VEC);
     493        5985 :   gel(bab,1) = gen_1;
     494        5985 :   gel(bab,2) = rootsof1u_cx(n, prec); /* = e_n(1) */
     495       32550 :   for (i=3; i<=m; i++) gel(bab,i) = gmul(gel(bab,2),gel(bab,i-1));
     496        5985 :   gig = cgetg(m+1,t_VEC);
     497        5985 :   gel(gig,1) = gen_1;
     498        5985 :   gel(gig,2) = gmul(gel(bab,2),gel(bab,m));;
     499       32550 :   for (i=3; i<=m; i++) gel(gig,i) = gmul(gel(gig,2),gel(gig,i-1));
     500        5985 :   gel(powz,1) = bab;
     501        5985 :   gel(powz,2) = gig;
     502        5985 :   if (real) gel(powz,3) = gen_0;
     503        5985 :   return powz;
     504             : }
     505             : 
     506             : static GEN
     507       59115 : muliimod_sz(GEN x, GEN y, GEN l, long siz)
     508             : {
     509       59115 :   pari_sp av = avma;
     510             :   GEN p1;
     511       59115 :   (void)new_chunk(siz); /* HACK */
     512       59115 :   p1 = mulii(x,y);
     513       59115 :   set_avma(av); return modii(p1,l);
     514             : }
     515             : 
     516             : static GEN
     517        5985 : polsubcyclo_roots(long n, GEN zl)
     518             : {
     519        5985 :   GEN le = gel(zl,1), z = gel(zl,2);
     520        5985 :   long i, lle = lg(le)*3; /*Assume dvmdii use lx+ly space*/
     521        5985 :   long m = (long)(1+sqrt((double) n));
     522        5985 :   GEN bab, gig, powz = cgetg(3,t_VEC);
     523             :   pari_timer ti;
     524        5985 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     525        5985 :   bab = cgetg(m+1,t_VEC);
     526        5985 :   gel(bab,1) = gen_1;
     527        5985 :   gel(bab,2) = icopy(z);
     528       32550 :   for (i=3; i<=m; i++) gel(bab,i) = muliimod_sz(z,gel(bab,i-1),le,lle);
     529        5985 :   gig = cgetg(m+1,t_VEC);
     530        5985 :   gel(gig,1) = gen_1;
     531        5985 :   gel(gig,2) = muliimod_sz(z,gel(bab,m),le,lle);;
     532       32550 :   for (i=3; i<=m; i++) gel(gig,i) = muliimod_sz(gel(gig,2),gel(gig,i-1),le,lle);
     533        5985 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "polsubcyclo_roots");
     534        5985 :   gel(powz,1) = bab;
     535        5985 :   gel(powz,2) = gig; return powz;
     536             : }
     537             : 
     538             : GEN
     539         455 : galoiscyclo(long n, long v)
     540             : {
     541         455 :   ulong av = avma;
     542             :   GEN grp, G, z, le, L, elts;
     543             :   long val, l, i, j, k;
     544         455 :   GEN zn = znstar(stoi(n));
     545         455 :   long card = itos(gel(zn,1));
     546         455 :   GEN gen = vec_to_vecsmall(lift_shallow(gel(zn,3)));
     547         455 :   GEN ord = vec_to_vecsmall(gel(zn,2));
     548         455 :   GEN T = polcyclo(n,v);
     549         455 :   long d = degpol(T);
     550         455 :   GEN borneabs = powuu(2,d);
     551         455 :   z = polsubcyclo_start(n,card/2,2,2*usqrt(d),borneabs,&val,&l);
     552         455 :   le = gel(z,1); z = gel(z,2);
     553         455 :   L = cgetg(1+card,t_VEC);
     554         455 :   gel(L,1) = z;
     555         959 :   for (j = 1, i = 1; j < lg(gen); j++)
     556             :   {
     557         504 :     long c = i * (ord[j]-1);
     558        2240 :     for (k = 1; k <= c; k++) gel(L,++i) = Fp_powu(gel(L,k), gen[j], le);
     559             :   }
     560         455 :   G = abelian_group(ord);
     561         455 :   elts = group_elts(G, card); /*not stack clean*/
     562         455 :   grp = cgetg(9, t_VEC);
     563         455 :   gel(grp,1) = T;
     564         455 :   gel(grp,2) = mkvec3(stoi(l), stoi(val), icopy(le));
     565         455 :   gel(grp,3) = L;
     566         455 :   gel(grp,4) = FpV_invVandermonde(L,  NULL, le);
     567         455 :   gel(grp,5) = gen_1;
     568         455 :   gel(grp,6) = elts;
     569         455 :   gel(grp,7) = gel(G,1);
     570         455 :   gel(grp,8) = gel(G,2);
     571         455 :   return gerepilecopy(av, grp);
     572             : }
     573             : 
     574             : /* Convert a bnrinit(Q,n) to an abelian group similar to znstar(n), with
     575             :  * t_INTMOD generators; set cx = 0 if the class field is real and to 1
     576             :  * otherwise */
     577             : static GEN
     578          56 : bnr_to_abgrp(GEN bnr, long *cx)
     579             : {
     580          56 :   GEN gen, F, v, bid, G, Ui = NULL;
     581             :   long l, i;
     582          56 :   checkbnr(bnr);
     583          56 :   bid = bnr_get_bid(bnr);
     584          56 :   G = bnr_get_clgp(bnr);
     585          56 :   if (lg(G) == 4)
     586          28 :     gen = abgrp_get_gen(G);
     587             :   else
     588             :   {
     589          28 :     Ui = gmael(bnr,4,3);
     590          28 :     if (ZM_isidentity(Ui)) Ui = NULL;
     591          28 :     gen = bid_get_gen(bid);
     592             :   }
     593          56 :   F = bid_get_ideal(bid);
     594          56 :   if (lg(F) != 2)
     595           7 :     pari_err_DOMAIN("bnr_to_abgrp", "bnr", "!=", strtoGENstr("Q"), bnr);
     596             :   /* F is the finite part of the conductor, cx is the infinite part*/
     597          49 :   F = gcoeff(F, 1, 1);
     598          49 :   *cx = signe(gel(bid_get_arch(bid), 1));
     599          49 :   l = lg(gen); v = cgetg(l, t_VEC);
     600         203 :   for (i = 1; i < l; ++i)
     601             :   {
     602         154 :     GEN x = gel(gen,i);
     603         154 :     if (typ(x) == t_COL) x = gel(x,1);
     604         154 :     gel(v,i) = gmodulo(absi_shallow(x), F);
     605             :   }
     606          49 :   if (Ui)
     607             :   { /* from bid.gen to bnr.gen (maybe one less) */
     608          21 :     GEN w = v;
     609          21 :     l = lg(Ui); v = cgetg(l, t_VEC);
     610          84 :     for (i = 1; i < l; i++) gel(v,i) = factorback2(w, gel(Ui, i));
     611             :   }
     612          49 :   return mkvec3(bnr_get_no(bnr), bnr_get_cyc(bnr), v);
     613             : }
     614             : 
     615             : static long
     616         931 : _itos(const char *fun, GEN n)
     617             : {
     618         931 :   if (is_bigint(n))
     619           0 :     pari_err_IMPL(stack_sprintf("conductor f > %ld in %s", LONG_MAX, fun));
     620         931 :   return itos(n);
     621             : }
     622             : long
     623         945 : subcyclo_nH(const char *fun, GEN N, GEN *psg)
     624             : {
     625         945 :   GEN V, Z = NULL, H = *psg;
     626         945 :   long i, l, n = 0, complex = 1;
     627         945 :   switch(typ(N))
     628             :   {
     629         504 :     case t_INT:
     630         504 :       n = _itos(fun, N);
     631         504 :       if (n < 1) pari_err_DOMAIN(fun, "degree", "<=", gen_0, N);
     632         497 :       break;
     633         441 :     case t_VEC:
     634         441 :       if (lg(N)==7)
     635          56 :         N = bnr_to_abgrp(N,&complex);
     636         385 :       else if (checkznstar_i(N))
     637          14 :         N = mkvec3(znstar_get_no(N), znstar_get_cyc(N),
     638             :                    gmodulo(znstar_get_gen(N), znstar_get_N(N)));
     639         434 :       if (lg(N)==4)
     640             :       { /* abgrp */
     641         434 :         GEN gen = abgrp_get_gen(N), z;
     642         434 :         if (typ(gen)!=t_VEC) pari_err_TYPE(fun,gen);
     643         434 :         Z = N;
     644         434 :         if (lg(gen) == 1) { n = 1; break; }
     645         434 :         z = gel(gen,1);
     646         434 :         if (typ(z) == t_INTMOD) { n = _itos(fun, gel(z,1)); break; }
     647             :       }
     648             :     default: /*fall through*/
     649           7 :       pari_err_TYPE(fun,N);
     650             :       return 0;/*LCOV_EXCL_LINE*/
     651             :   }
     652         924 :   if (!H) H = gen_1;
     653         924 :   switch(typ(H))
     654             :   {
     655         315 :      case t_INTMOD: case t_INT:
     656         315 :       V = mkvecsmall( lift_check_modulus(H,n) );
     657         308 :       break;
     658          14 :     case t_VECSMALL:
     659          14 :       l = lg(H); V = leafcopy(H);
     660          42 :       for (i = 1; i < l; i++) { V[i] %= n; if (V[i] < 0) V[i] += n; }
     661          14 :       break;
     662         154 :     case t_VEC: case t_COL:
     663         154 :       l = lg(H); V = cgetg(l,t_VECSMALL);
     664     1288371 :       for(i=1; i < l; i++) V[i] = lift_check_modulus(gel(H,i),n);
     665         147 :       break;
     666         441 :     case t_MAT:
     667         441 :       l = lg(H);
     668         441 :       if (l == 1 || l != lgcols(H))
     669           7 :         pari_err_TYPE(stack_strcat(fun," [H not in HNF]"),H);
     670         434 :       if (!Z) pari_err_TYPE(stack_strcat(fun," [N not a bnrinit or znstar]"),H);
     671         427 :       if (lg(gel(Z,2)) != l) pari_err_DIM(fun);
     672         420 :       V = znstar_hnf_generators(znstar_small(Z),H);
     673         420 :       break;
     674           0 :     default:
     675           0 :       pari_err_TYPE(fun,H);
     676             :       return 0;/*LCOV_EXCL_LINE*/
     677             :   }
     678         889 :   if (!complex) V = vecsmall_append(V, n-1); /*add complex conjugation*/
     679         889 :   *psg = V; return n;
     680             : }
     681             : 
     682             : GEN
     683         567 : galoissubcyclo(GEN N, GEN sg, long flag, long v)
     684             : {
     685         567 :   pari_sp ltop = avma, av;
     686             :   GEN H, B, zl, L, T, le, powz, O;
     687             :   long i, card, phi_n, val,l, n, cnd, complex;
     688             :   pari_timer ti;
     689             : 
     690         567 :   if (flag<0 || flag>3) pari_err_FLAG("galoissubcyclo");
     691         567 :   if (v < 0) v = 0;
     692         567 :   n = subcyclo_nH("galoissubcyclo", N, &sg);
     693         518 :   if (n==1)
     694             :   {
     695          21 :     set_avma(ltop); if (flag == 1) return gen_1;
     696          14 :     return gscycloconductor(deg1pol_shallow(gen_1, gen_m1, v), 1, flag);
     697             :   }
     698         497 :   H = znstar_generate(n, sg);
     699         497 :   if (DEBUGLEVEL >= 6)
     700             :   {
     701           0 :     err_printf("Subcyclo: elements:");
     702           0 :     for (i=1;i<n;i++)
     703           0 :       if (F2v_coeff(gel(H,3),i)) err_printf(" %ld",i);
     704           0 :     err_printf("\n");
     705             :   }
     706             :   /* field is real iff z -> conj(z) = z^-1 = z^(n-1) is in H */
     707         497 :   complex = !F2v_coeff(gel(H,3),n-1);
     708         497 :   if (DEBUGLEVEL >= 6) err_printf("Subcyclo: complex=%ld\n",complex);
     709         497 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     710         497 :   cnd = znstar_conductor(H);
     711         497 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "znstar_conductor");
     712         497 :   if (flag == 1)  { set_avma(ltop); return stoi(cnd); }
     713         497 :   if (cnd == 1)
     714             :   {
     715          28 :     set_avma(ltop); if (flag == 1) return gen_1;
     716          28 :     return gscycloconductor(deg1pol_shallow(gen_1,gen_m1,v),1,flag);
     717             :   }
     718         469 :   if (n != cnd)
     719             :   {
     720         175 :     H = znstar_reduce_modulus(H, cnd);
     721         175 :     n = cnd;
     722             :   }
     723         469 :   card = znstar_order(H);
     724         469 :   phi_n = eulerphiu(n);
     725         469 :   if (card == phi_n)
     726             :   {
     727           0 :     set_avma(ltop);
     728           0 :     return gscycloconductor(polcyclo(n,v),n,flag);
     729             :   }
     730         469 :   O = znstar_cosets(n, phi_n, H);
     731         469 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "znstar_cosets");
     732         469 :   if (DEBUGLEVEL >= 6) err_printf("Subcyclo: orbits=%Ps\n",O);
     733         469 :   if (DEBUGLEVEL >= 4)
     734           0 :     err_printf("Subcyclo: %ld orbits with %ld elements each\n",phi_n/card,card);
     735         469 :   av = avma;
     736         469 :   powz = polsubcyclo_complex_roots(n,!complex,LOWDEFAULTPREC);
     737         469 :   L = polsubcyclo_orbits(n,H,O,powz,NULL);
     738         469 :   B = polsubcyclo_complex_bound(av,L,LOWDEFAULTPREC);
     739         469 :   zl = polsubcyclo_start(n,phi_n/card,card,1,B,&val,&l);
     740         469 :   powz = polsubcyclo_roots(n,zl);
     741         469 :   le = gel(zl,1);
     742         469 :   L = polsubcyclo_orbits(n,H,O,powz,le);
     743         469 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     744         469 :   T = FpV_roots_to_pol(L,le,v);
     745         469 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "roots_to_pol");
     746         469 :   T = FpX_center(T,le,shifti(le,-1));
     747         469 :   if (flag==3)
     748             :   {
     749             :     GEN L2, aut;
     750           7 :     if (Flx_is_squarefree(ZX_to_Flx(T, l),l))
     751           7 :       L2 = ZV_to_Flv(L,l);
     752             :     else
     753             :     {
     754             :       ulong z;
     755           0 :       do l+=n; while (!uisprime(l) || !Flx_is_squarefree(ZX_to_Flx(T, l), l));
     756           0 :       z = rootsof1_Fl(n,l);
     757           0 :       L2 = Fl_polsubcyclo_orbits(n,H,O,z,l);
     758           0 :       if (DEBUGLEVEL >= 4)
     759           0 :         err_printf("galoissubcyclo: switching to unramified prime %lu\n",l);
     760             :     }
     761           7 :     aut  = znstar_quotient(n, phi_n, H, L2, l);
     762           7 :     return gerepileupto(ltop, galoisinitfromaut(T, aut, l));
     763             :   }
     764         462 :   return gerepileupto(ltop, gscycloconductor(T,n,flag));
     765             : }
     766             : 
     767             : /* Z = znstar(n) cyclic. n = 1,2,4,p^a or 2p^a,
     768             :  * and d | phi(n) = 1,1,2,(p-1)p^(a-1) */
     769             : static GEN
     770        5635 : polsubcyclo_g(long n, long d, GEN Z, long v)
     771             : {
     772        5635 :   pari_sp ltop = avma;
     773             :   long o, p, r, g, gd, l , val;
     774             :   GEN zl, L, T, le, B, powz;
     775             :   pari_timer ti;
     776        5635 :   if (d==1) return deg1pol_shallow(gen_1,gen_m1,v); /* get rid of n=1,2 */
     777        5635 :   if ((n & 3) == 2) n >>= 1;
     778             :   /* n = 4 or p^a, p odd */
     779        5635 :   o = itos(gel(Z,1));
     780        5635 :   g = itos(gmael3(Z,3,1,2));
     781        5635 :   p = n / ugcd(n,o); /* p^a / gcd(p^a,phi(p^a)) = p*/
     782        5635 :   r = ugcd(d,n); /* = p^(v_p(d)) < n */
     783        5635 :   n = r*p; /* n is now the conductor */
     784        5635 :   o = n-r; /* = phi(n) */
     785        5635 :   if (o == d) return polcyclo(n,v);
     786        5516 :   o /= d;
     787        5516 :   gd = Fl_powu(g%n, d, n);
     788             :   /*FIXME: If degree is small, the computation of B is a waste of time*/
     789        5516 :   powz = polsubcyclo_complex_roots(n,(o&1)==0,LOWDEFAULTPREC);
     790        5516 :   L = polsubcyclo_cyclic(n,d,o,g,gd,powz,NULL);
     791        5516 :   B = polsubcyclo_complex_bound(ltop,L,LOWDEFAULTPREC);
     792        5516 :   zl = polsubcyclo_start(n,d,o,1,B,&val,&l);
     793        5516 :   le = gel(zl,1);
     794        5516 :   powz = polsubcyclo_roots(n,zl);
     795        5516 :   L = polsubcyclo_cyclic(n,d,o,g,gd,powz,le);
     796        5516 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
     797        5516 :   T = FpV_roots_to_pol(L,le,v);
     798        5516 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "roots_to_pol");
     799        5516 :   return gerepileupto(ltop, FpX_center(T,le,shifti(le,-1)));
     800             : }
     801             : 
     802             : GEN
     803        5691 : polsubcyclo(long n, long d, long v)
     804             : {
     805        5691 :   pari_sp ltop = avma;
     806             :   GEN L, Z;
     807        5691 :   if (v<0) v = 0;
     808        5691 :   if (d<=0) pari_err_DOMAIN("polsubcyclo","d","<=",gen_0,stoi(d));
     809        5684 :   if (n<=0) pari_err_DOMAIN("polsubcyclo","n","<=",gen_0,stoi(n));
     810        5677 :   Z = znstar(stoi(n));
     811        5677 :   if (!dvdis(gel(Z,1), d)) { set_avma(ltop); return cgetg(1, t_VEC); }
     812        5670 :   if (lg(gel(Z,2)) == 2)
     813             :   { /* faster but Z must be cyclic */
     814        5635 :     set_avma(ltop);
     815        5635 :     return polsubcyclo_g(n, d, Z, v);
     816             :   }
     817          35 :   L = subgrouplist(gel(Z,2), mkvec(stoi(d)));
     818          35 :   if (lg(L) == 2)
     819           7 :     return gerepileupto(ltop, galoissubcyclo(Z, gel(L,1), 0, v));
     820             :   else
     821             :   {
     822          28 :     GEN V = cgetg(lg(L),t_VEC);
     823             :     long i;
     824         364 :     for (i=1; i< lg(V); i++) gel(V,i) = galoissubcyclo(Z, gel(L,i), 0, v);
     825          28 :     return gerepileupto(ltop, V);
     826             :   }
     827             : }
     828             : 
     829             : struct aurifeuille_t {
     830             :   GEN z, le;
     831             :   ulong l;
     832             :   long e;
     833             : };
     834             : 
     835             : /* Let z a primitive n-th root of 1, n > 1, A an integer such that
     836             :  * Aurifeuillian factorization of Phi_n(A) exists ( z.A is a square in Q(z) ).
     837             :  * Let G(p) the Gauss sum mod p prime:
     838             :  *      sum_x (x|p) z^(xn/p) for p odd,  i - 1 for p = 2 [ i := z^(n/4) ]
     839             :  * We have N(-1) = Nz = 1 (n != 1,2), and
     840             :  *      G^2 = (-1|p) p for p odd,  G^2 = -2i for p = 2
     841             :  * In particular, for odd A, (-1|A) A = g^2 is a square. If A = prod p^{e_p},
     842             :  * sigma_j(g) = \prod_p (sigma_j G(p)))^e_p = \prod_p (j|p)^e_p g = (j|A) g
     843             :  * n odd  : z^2 is a primitive root, A = g^2
     844             :  *   Phi_n(A) = N(A - z^2) = N(g - z) N(g + z)
     845             :  *
     846             :  * n = 2 (4) : -z^2 is a primitive root, -A = g^2
     847             :  *   Phi_n(A) = N(A - (-z^2)) = N(g^2 - z^2)  [ N(-1) = 1 ]
     848             :  *                            = N(g - z) N(g + z)
     849             :  *
     850             :  * n = 4 (8) : i z^2 primitive root, -Ai = g^2
     851             :  *   Phi_n(A) = N(A - i z^2) = N(-Ai -  z^2) = N(g - z) N(g + z)
     852             :  * sigma_j(g) / g =  (j|A)  if j = 1 (4)
     853             :  *                  (-j|A)i if j = 3 (4)
     854             :  *   */
     855             : /* factor Phi_n(A), Astar: A* = squarefree kernel of A, P = odd prime divisors
     856             :  * of n */
     857             : static GEN
     858         427 : factor_Aurifeuille_aux(GEN A, long Astar, long n, GEN P,
     859             :                        struct aurifeuille_t *S)
     860             : {
     861             :   pari_sp av;
     862         427 :   GEN f, a, b, s, powers, z = S->z, le = S->le;
     863         427 :   long j, k, maxjump, lastj, e = S->e;
     864         427 :   ulong l = S->l;
     865             :   char *invertible;
     866             : 
     867         427 :   if ((n & 7) == 4)
     868             :   { /* A^* even */
     869         350 :     GEN i = Fp_powu(z, n>>2, le), z2 = Fp_sqr(z, le);
     870             : 
     871         350 :     invertible = stack_malloc(n); /* even indices unused */
     872        1610 :     for (j = 1; j < n; j+=2) invertible[j] = 1;
     873         406 :     for (k = 1; k < lg(P); k++)
     874             :     {
     875          56 :       long p = P[k];
     876         168 :       for (j = p; j < n; j += 2*p) invertible[j] = 0;
     877             :     }
     878         350 :     lastj = 1; maxjump = 2;
     879        1260 :     for (j= 3; j < n; j+=2)
     880         910 :       if (invertible[j]) {
     881         798 :         long jump = j - lastj;
     882         798 :         if (jump > maxjump) maxjump = jump;
     883         798 :         lastj = j;
     884             :       }
     885         350 :     powers = cgetg(maxjump+1, t_VEC); /* powers[k] = z^k, odd indices unused */
     886         350 :     gel(powers,2) = z2;
     887         406 :     for (k = 4; k <= maxjump; k+=2)
     888         112 :       gel(powers,k) = odd(k>>1)? Fp_mul(gel(powers, k-2), z2, le)
     889          56 :                                : Fp_sqr(gel(powers, k>>1), le);
     890             : 
     891         350 :     if (Astar == 2)
     892             :     { /* important special case (includes A=2), split for efficiency */
     893         329 :       if (!equalis(A, 2))
     894             :       {
     895          14 :         GEN f = sqrti(shifti(A,-1)), mf = Fp_neg(f,le), fi = Fp_mul(f,i,le);
     896          14 :         a = Fp_add(mf, fi, le);
     897          14 :         b = Fp_sub(mf, fi, le);
     898             :       }
     899             :       else
     900             :       {
     901         315 :         a = subiu(i,1);
     902         315 :         b = subsi(-1,i);
     903             :       }
     904         329 :       av = avma;
     905         329 :       s = z; f = subii(a, s); lastj = 1;
     906         910 :       for (j = 3, k = 0; j < n; j+=2)
     907         581 :         if (invertible[j])
     908             :         {
     909         511 :           s = Fp_mul(gel(powers, j-lastj), s, le); /* z^j */
     910         511 :           lastj = j;
     911         511 :           f = Fp_mul(f, subii((j & 3) == 1? a: b, s), le);
     912         511 :           if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     913             :         }
     914             :     }
     915             :     else
     916             :     {
     917          21 :       GEN ma, mb, B = Fp_mul(A, i, le), gl = utoipos(l);
     918             :       long t;
     919          21 :       Astar >>= 1;
     920          21 :       t = Astar & 3; if (Astar < 0) t = 4-t; /* t = 1 or 3 */
     921          21 :       if (t == 1) B = Fp_neg(B, le);
     922          21 :       a = Zp_sqrtlift(B, Fp_sqrt(B, gl), gl, e);
     923          21 :       b = Fp_mul(a, i, le);
     924          21 :       ma = Fp_neg(a, le);
     925          21 :       mb = Fp_neg(b, le);
     926          21 :       av = avma;
     927          21 :       s = z; f = subii(a, s); lastj = 1;
     928         350 :       for (j = 3, k = 0; j<n; j+=2)
     929         329 :         if (invertible[j])
     930             :         {
     931             :           GEN t;
     932         287 :           if ((j & 3) == 1) t = (kross(j, Astar) < 0)? ma: a;
     933         154 :           else              t = (kross(j, Astar) < 0)? mb: b;
     934         287 :           s = Fp_mul(gel(powers, j-lastj), s, le); /* z^j */
     935         287 :           lastj = j;
     936         287 :           f = Fp_mul(f, subii(t, s), le);
     937         287 :           if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     938             :         }
     939             :     }
     940             :   }
     941             :   else /* A^* odd */
     942             :   {
     943             :     ulong g;
     944          77 :     if ((n & 3) == 2)
     945             :     { /* A^* = 3 (mod 4) */
     946           0 :       A = negi(A); Astar = -Astar;
     947           0 :       z = Fp_neg(z, le);
     948           0 :       n >>= 1;
     949             :     }
     950             :     /* A^* = 1 (mod 4) */
     951          77 :     g = Fl_sqrt(umodiu(A,l), l);
     952          77 :     a = Zp_sqrtlift(A, utoipos(g), utoipos(l), e);
     953          77 :     b = negi(a);
     954             : 
     955          77 :     invertible = stack_malloc(n);
     956        1337 :     for (j = 1; j < n; j++) invertible[j] = 1;
     957         196 :     for (k = 1; k < lg(P); k++)
     958             :     {
     959         119 :       long p = P[k];
     960         483 :       for (j = p; j < n; j += p) invertible[j] = 0;
     961             :     }
     962          77 :     lastj = 2; maxjump = 1;
     963        1183 :     for (j= 3; j < n; j++)
     964        1106 :       if (invertible[j]) {
     965         742 :         long jump = j - lastj;
     966         742 :         if (jump > maxjump) maxjump = jump;
     967         742 :         lastj = j;
     968             :       }
     969          77 :     powers = cgetg(maxjump+1, t_VEC); /* powers[k] = z^k */
     970          77 :     gel(powers,1) = z;
     971         161 :     for (k = 2; k <= maxjump; k++)
     972         168 :       gel(powers,k) = odd(k)? Fp_mul(gel(powers, k-1), z, le)
     973          84 :                             : Fp_sqr(gel(powers, k>>1), le);
     974          77 :     av = avma;
     975          77 :     s = z; f = subii(a, s); lastj = 1;
     976        1260 :     for(j = 2, k = 0; j < n; j++)
     977        1183 :       if (invertible[j])
     978             :       {
     979         819 :         s = Fp_mul(gel(powers, j-lastj), s, le);
     980         819 :         lastj = j;
     981         819 :         f = Fp_mul(f, subii(kross(j,Astar)==1? a: b, s), le);
     982         819 :         if (++k == 0x1ff) { gerepileall(av, 2, &s, &f); k = 0; }
     983             :       }
     984             :   }
     985         427 :   return f;
     986             : }
     987             : 
     988             : /* d != 2 mod 4; fd = factoru(odd(d)? d: d / 4) */
     989             : static void
     990         427 : Aurifeuille_init(GEN a, long d, GEN fd, struct aurifeuille_t *S)
     991             : {
     992         427 :   GEN bound, zl, sqrta = sqrtr_abs(itor(a, LOWDEFAULTPREC));
     993         427 :   ulong phi = eulerphiu_fact(fd);
     994         427 :   if (!odd(d)) phi <<= 1; /* eulerphi(d) */
     995         427 :   bound = ceil_safe(powru(addrs(sqrta,1), phi));
     996         427 :   zl = polsubcyclo_start(d, 0, 0, 1, bound, &(S->e), (long*)&(S->l));
     997         427 :   S->le = gel(zl,1);
     998         427 :   S->z  = gel(zl,2);
     999         427 : }
    1000             : 
    1001             : GEN
    1002         364 : factor_Aurifeuille_prime(GEN p, long d)
    1003             : {
    1004         364 :   pari_sp av = avma;
    1005             :   struct aurifeuille_t S;
    1006             :   GEN fd;
    1007             :   long pp;
    1008         364 :   if ((d & 3) == 2) { d >>= 1; p = negi(p); }
    1009         364 :   fd = factoru(odd(d)? d: d>>2);
    1010         364 :   pp = itos(p);
    1011         364 :   Aurifeuille_init(p, d, fd, &S);
    1012         364 :   return gerepileuptoint(av, factor_Aurifeuille_aux(p, pp, d, gel(fd,1), &S));
    1013             : }
    1014             : 
    1015             : /* an algebraic factor of Phi_d(a), a != 0 */
    1016             : GEN
    1017          63 : factor_Aurifeuille(GEN a, long d)
    1018             : {
    1019          63 :   pari_sp av = avma;
    1020             :   GEN fd, P, A;
    1021          63 :   long i, lP, va = vali(a), sa, astar, D;
    1022             :   struct aurifeuille_t S;
    1023             : 
    1024          63 :   if (d <= 0)
    1025           0 :     pari_err_DOMAIN("factor_Aurifeuille", "degre", "<=",gen_0,stoi(d));
    1026          63 :   if ((d & 3) == 2) { d >>= 1; a = negi(a); }
    1027          63 :   if ((va & 1) == (d & 1)) { set_avma(av); return gen_1; }
    1028          63 :   sa = signe(a);
    1029          63 :   if (odd(d))
    1030             :   {
    1031             :     long a4;
    1032          28 :     if (d == 1)
    1033             :     {
    1034           0 :       if (!Z_issquareall(a, &A)) return gen_1;
    1035           0 :       return gerepileuptoint(av, addiu(A,1));
    1036             :     }
    1037          28 :     A = va? shifti(a, -va): a;
    1038          28 :     a4 = mod4(A); if (sa < 0) a4 = 4 - a4;
    1039          28 :     if (a4 != 1) { set_avma(av); return gen_1; }
    1040             :   }
    1041          35 :   else if ((d & 7) == 4)
    1042          35 :     A = shifti(a, -va);
    1043             :   else
    1044             :   {
    1045           0 :     set_avma(av); return gen_1;
    1046             :   }
    1047             :   /* v_2(d) = 0 or 2. Kill 2 from factorization (minor efficiency gain) */
    1048          63 :   fd = factoru(odd(d)? d: d>>2); P = gel(fd,1); lP = lg(P);
    1049          63 :   astar = sa;
    1050          63 :   if (odd(va)) astar <<= 1;
    1051         147 :   for (i = 1; i < lP; i++)
    1052          84 :     if (odd( (Z_lvalrem(A, P[i], &A)) ) ) astar *= P[i];
    1053          63 :   if (sa < 0)
    1054             :   { /* negate in place if possible */
    1055          14 :     if (A == a) A = icopy(A);
    1056          14 :     setabssign(A);
    1057             :   }
    1058          63 :   if (!Z_issquare(A)) { set_avma(av); return gen_1; }
    1059             : 
    1060          63 :   D = odd(d)? 1: 4;
    1061         147 :   for (i = 1; i < lP; i++) D *= P[i];
    1062          63 :   if (D != d) { a = powiu(a, d/D); d = D; }
    1063             : 
    1064          63 :   Aurifeuille_init(a, d, fd, &S);
    1065          63 :   return gerepileuptoint(av, factor_Aurifeuille_aux(a, astar, d, P, &S));
    1066             : }

Generated by: LCOV version 1.13