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

Generated by: LCOV version 1.11