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

Generated by: LCOV version 1.13