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

Generated by: LCOV version 1.11