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 - hyperell.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21343-6216058) Lines: 482 503 95.8 %
Date: 2017-11-19 06:21:17 Functions: 41 41 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2014  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /********************************************************************/
      15             : /**                                                                **/
      16             : /**                     HYPERELLIPTIC CURVES                       **/
      17             : /**                                                                **/
      18             : /********************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : /* Implementation of Kedlaya Algorithm for counting point on hyperelliptic
      23             : curves by Bill Allombert based on a GP script by Bernadette Perrin-Riou.
      24             : 
      25             : References:
      26             : Pierrick Gaudry and Nicolas G\"urel
      27             : Counting Points in Medium Characteristic Using Kedlaya's Algorithm
      28             : Experiment. Math.  Volume 12, Number 4 (2003), 395-402.
      29             :    http://projecteuclid.org/euclid.em/1087568016
      30             : 
      31             : Harrison, M. An extension of Kedlaya's algorithm for hyperelliptic
      32             :   curves. Journal of Symbolic Computation, 47 (1) (2012), 89-101.
      33             :   http://arxiv.org/pdf/1006.4206v3.pdf
      34             : */
      35             : 
      36             : /* We use the basis of differentials (x^i*dx/y^k) (i=1 to 2*g-1),
      37             :    with k either 1 or 3, depending on p and d, see Harrison paper */
      38             : 
      39             : static long
      40        1120 : get_basis(long p, long d)
      41             : {
      42        1120 :   if (odd(d))
      43         560 :     return p < d-1 ? 3 : 1;
      44             :   else
      45         560 :     return 2*p <= d-2 ? 3 : 1;
      46             : }
      47             : 
      48             : static GEN
      49       11137 : FpXXQ_red(GEN S, GEN T, GEN p)
      50             : {
      51       11137 :   pari_sp av = avma;
      52       11137 :   long i, dS = degpol(S);
      53             :   GEN A, C;
      54       11137 :   if (signe(S)==0) return pol_0(varn(T));
      55       11137 :   A = cgetg(dS+3, t_POL);
      56       11137 :   C = pol_0(varn(T));
      57      989394 :   for(i=dS; i>0; i--)
      58             :   {
      59      978257 :     GEN Si = FpX_add(C, gel(S,i+2), p);
      60      978257 :     GEN R, Q = FpX_divrem(Si, T, p, &R);
      61      978257 :     gel(A,i+2) = R;
      62      978257 :     C = Q;
      63             :   }
      64       11137 :   gel(A,2) = FpX_add(C, gel(S,2), p);
      65       11137 :   A[1] = S[1];
      66       11137 :   return gerepilecopy(av, FpXX_renormalize(A,dS+3));
      67             : }
      68             : 
      69             : static GEN
      70        2065 : FpXXQ_sqr(GEN x, GEN T, GEN p)
      71             : {
      72        2065 :   pari_sp av = avma;
      73        2065 :   long n = degpol(T);
      74        2065 :   GEN z = FpX_red(ZXX_sqr_Kronecker(x, n), p);
      75        2065 :   z = Kronecker_to_ZXX(z, n, varn(T));
      76        2065 :   return gerepileupto(av, FpXXQ_red(z, T, p));
      77             : }
      78             : 
      79             : static GEN
      80        9072 : FpXXQ_mul(GEN x, GEN y, GEN T, GEN p)
      81             : {
      82        9072 :   pari_sp av = avma;
      83        9072 :   long n = degpol(T);
      84        9072 :   GEN z = FpX_red(ZXX_mul_Kronecker(x, y, n), p);
      85        9072 :   z = Kronecker_to_ZXX(z, n, varn(T));
      86        9072 :   return gerepileupto(av, FpXXQ_red(z, T, p));
      87             : }
      88             : 
      89             : static GEN
      90         693 : ZpXXQ_invsqrt(GEN S, GEN T, ulong p, long e)
      91             : {
      92         693 :   pari_sp av = avma, av2;
      93             :   ulong mask;
      94         693 :   long v = varn(S), n=1;
      95         693 :   GEN a = pol_1(v);
      96         693 :   if (e <= 1) return gerepilecopy(av, a);
      97         693 :   mask = quadratic_prec_mask(e);
      98         693 :   av2 = avma;
      99        3444 :   for (;mask>1;)
     100             :   {
     101             :     GEN q, q2, q22, f, fq, afq;
     102        2058 :     long n2 = n;
     103        2058 :     n<<=1; if (mask & 1) n--;
     104        2058 :     mask >>= 1;
     105        2058 :     q = powuu(p,n); q2 = powuu(p,n2);
     106        2058 :     f = RgX_sub(FpXXQ_mul(FpXX_red(S, q), FpXXQ_sqr(a, T, q), T, q), pol_1(v));
     107        2058 :     fq = ZXX_Z_divexact(f, q2);
     108        2058 :     q22 = shifti(addiu(q2,1),-1);
     109        2058 :     afq = FpXX_Fp_mul(FpXXQ_mul(a, fq, T, q2), q22, q2);
     110        2058 :     a = RgX_sub(a, ZXX_Z_mul(afq, q2));
     111        2058 :     if (gc_needed(av2,1))
     112             :     {
     113           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_invsqrt, e = %ld", n);
     114           0 :       a = gerepileupto(av2, a);
     115             :     }
     116             :   }
     117         693 :   return gerepileupto(av, a);
     118             : }
     119             : 
     120             : static GEN
     121      624771 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
     122             : 
     123             : static void
     124          14 : is_sing(GEN H, ulong p)
     125             : {
     126          14 :   pari_err_DOMAIN("hyperellpadicfrobenius","H","is singular at",utoi(p),H);
     127           0 : }
     128             : 
     129             : static void
     130         693 : get_UV(GEN *U, GEN *V, GEN T, ulong p, long e)
     131             : {
     132         693 :   GEN q = powuu(p,e), d;
     133         693 :   GEN dT = FpX_deriv(T, q);
     134         693 :   GEN R = polresultantext(T, dT);
     135         693 :   long v = varn(T);
     136         693 :   if (dvdiu(gel(R,3),p)) is_sing(T, p);
     137         693 :   d = Fp_inv(gel(R,3), q);
     138         693 :   *U = FpX_Fp_mul(FpX_red(to_ZX(gel(R,1),v),q),d,q);
     139         693 :   *V = FpX_Fp_mul(FpX_red(to_ZX(gel(R,2),v),q),d,q);
     140         693 : }
     141             : 
     142             : static GEN
     143       68936 : frac_to_Fp(GEN a, GEN b, GEN p)
     144             : {
     145       68936 :   GEN d = gcdii(a, b);
     146       68936 :   return Fp_div(diviiexact(a, d), diviiexact(b, d), p);
     147             : }
     148             : 
     149             : static GEN
     150        4949 : ZpXXQ_frob(GEN S, GEN U, GEN V, long k, GEN T, ulong p, long e)
     151             : {
     152        4949 :   pari_sp av = avma, av2;
     153        4949 :   long i, pr = degpol(S), dT = degpol(T), vT = varn(T);
     154        4949 :   GEN q = powuu(p,e);
     155        4949 :   GEN Tp = FpX_deriv(T, q), Tp1 = RgX_shift_shallow(Tp, 1);
     156        4949 :   GEN M = to_ZX(gel(S,pr+2),vT) , R;
     157        4949 :   av2 = avma;
     158      596988 :   for(i = pr-1; i>=k; i--)
     159             :   {
     160             :     GEN A, B, H, Bc;
     161             :     ulong v, r;
     162      592039 :     H = FpX_divrem(FpX_mul(V,M,q), T, q, &B);
     163      592039 :     A = FpX_add(FpX_mul(U,M,q), FpX_mul(H, Tp, q),q);
     164      592039 :     v = u_lvalrem(2*i+1,p,&r);
     165      592039 :     Bc = FpX_deriv(B, q);
     166      592039 :     Bc = FpX_Fp_mul(ZX_Z_divexact(Bc,powuu(p,v)),Fp_div(gen_2, utoi(r), q), q);
     167      592039 :     M = FpX_add(to_ZX(gel(S,i+2),vT), FpX_add(A, Bc, q), q);
     168      592039 :     if (gc_needed(av2,1))
     169             :     {
     170           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 1, i = %ld", i);
     171           0 :       M = gerepileupto(av2, M);
     172             :     }
     173             :   }
     174        4949 :   if (degpol(M)<dT-1)
     175        2625 :     return gerepileupto(av, M);
     176        2324 :   R = RgX_shift_shallow(M,dT-degpol(M)-2);
     177        2324 :   av2 = avma;
     178      120302 :   for(i = degpol(M)-dT+2; i>=1; i--)
     179             :   {
     180             :     GEN B, c;
     181      117978 :     R = RgX_shift_shallow(R, 1);
     182      117978 :     gel(R,2) = gel(M, i+1);
     183      117978 :     if (degpol(R) < dT) continue;
     184       67431 :     B = FpX_add(FpX_mulu(T, 2*i, q), Tp1, q);
     185       67431 :     c = frac_to_Fp(leading_coeff(R), leading_coeff(B), q);
     186       67431 :     R = FpX_sub(R, FpX_Fp_mul(B, c, q), q);
     187       67431 :     if (gc_needed(av2,1))
     188             :     {
     189           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
     190           0 :       R = gerepileupto(av2, R);
     191             :     }
     192             :   }
     193        2324 :   if (degpol(R)==dT-1)
     194             :   {
     195        1505 :     GEN c = frac_to_Fp(leading_coeff(R), leading_coeff(Tp), q);
     196        1505 :     R = FpX_sub(R, FpX_Fp_mul(Tp, c, q), q);
     197        1505 :     return gerepileupto(av, R);
     198             :   } else
     199         819 :     return gerepilecopy(av, R);
     200             : }
     201             : 
     202             : static GEN
     203        6230 : revdigits(GEN v)
     204             : {
     205        6230 :   long i, n = lg(v)-1;
     206        6230 :   GEN w = cgetg(n+2, t_POL);
     207        6230 :   w[1] = evalsigne(1)|evalvarn(0);
     208       92071 :   for (i=0; i<n; i++)
     209       85841 :     gel(w,i+2) = gel(v,n-i);
     210        6230 :   return FpXX_renormalize(w, n+2);
     211             : }
     212             : 
     213             : static GEN
     214        4949 : diff_red(GEN s, GEN A, long m, GEN T, GEN p)
     215             : {
     216        4949 :   long v, n, vT = varn(T);
     217             :   GEN Q, sQ, qS;
     218             :   pari_timer ti;
     219        4949 :   if (DEBUGLEVEL>1) timer_start(&ti);
     220        4949 :   Q = revdigits(FpX_digits(A,T,p));
     221        4949 :   n = degpol(Q);
     222        4949 :   if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
     223        4949 :   sQ = FpXXQ_mul(s,Q,T,p);
     224        4949 :   if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
     225        4949 :   qS = RgX_shift_shallow(sQ,m-n);
     226        4949 :   v = ZX_val(sQ);
     227        4949 :   if (n > m + v)
     228             :   {
     229        2296 :     long i, l = n-m-v;
     230        2296 :     GEN rS = cgetg(l+1,t_VEC);
     231       14833 :     for (i = l-1; i >=0 ; i--)
     232       12537 :       gel(rS,i+1) = to_ZX(gel(sQ, 1+v+l-i), vT);
     233        2296 :     rS = FpXV_FpX_fromdigits(rS,T,p);
     234        2296 :     gel(qS,2) = FpX_add(FpX_mul(rS, T, p), gel(qS, 2), p);
     235        2296 :     if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
     236             :   }
     237        4949 :   return qS;
     238             : }
     239             : 
     240             : static GEN
     241        4949 : ZC_to_padic(GEN C, GEN q)
     242             : {
     243        4949 :   long i, l = lg(C);
     244        4949 :   GEN V = cgetg(l,t_COL);
     245       49770 :   for(i = 1; i < l; i++)
     246       44821 :     gel(V, i) = gadd(gel(C, i), q);
     247        4949 :   return V;
     248             : }
     249             : 
     250             : static GEN
     251         693 : ZM_to_padic(GEN M, GEN q)
     252             : {
     253         693 :   long i, l = lg(M);
     254         693 :   GEN V = cgetg(l,t_MAT);
     255        5642 :   for(i = 1; i < l; i++)
     256        4949 :     gel(V, i) = ZC_to_padic(gel(M, i), q);
     257         693 :   return V;
     258             : }
     259             : 
     260             : static GEN
     261        1631 : ZX_to_padic(GEN P, GEN q)
     262             : {
     263        1631 :   long i, l = lg(P);
     264        1631 :   GEN Q = cgetg(l, t_POL);
     265        1631 :   Q[1] = P[1];
     266        5656 :   for (i=2; i<l ;i++)
     267        4025 :     gel(Q,i) = gadd(gel(P,i), q);
     268        1631 :   return normalizepol(Q);
     269             : }
     270             : 
     271             : static GEN
     272         441 : ZXC_to_padic(GEN x, GEN q)
     273         441 : { pari_APPLY_type(t_COL, ZX_to_padic(gel(x, i), q)) }
     274             : 
     275             : static GEN
     276         140 : ZXM_to_padic(GEN x, GEN q)
     277         140 : { pari_APPLY_same(ZXC_to_padic(gel(x, i), q)) }
     278             : 
     279             : static GEN
     280         693 : ZlX_hyperellpadicfrobenius(GEN H, ulong p, long n)
     281             : {
     282         693 :   pari_sp av = avma;
     283             :   long k, N, i, d;
     284             :   GEN F, s, Q, pN1, U, V;
     285             :   pari_timer ti;
     286         693 :   if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
     287         693 :   if (p == 2) is_sing(H, 2);
     288         693 :   d = degpol(H);
     289         693 :   if (d <= 0)
     290           0 :     pari_err_CONSTPOL("hyperellpadicfrobenius");
     291         693 :   if (n < 1)
     292           0 :     pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
     293         693 :   k = get_basis(p, d);
     294         693 :   N = n + ulogint(2*n, p) + 1;
     295         693 :   pN1 = powuu(p,N+1);
     296         693 :   Q = RgX_to_FpX(H, pN1);
     297         693 :   if (dvdiu(leading_coeff(Q),p)) is_sing(H, p);
     298         693 :   setvarn(Q,1);
     299         693 :   if (DEBUGLEVEL>1) timer_start(&ti);
     300         693 :   s = revdigits(FpX_digits(RgX_inflate(Q, p), Q, pN1));
     301         693 :   if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
     302         693 :   s = ZpXXQ_invsqrt(s, Q, p, N);
     303         693 :   if (k==3)
     304           7 :     s = FpXXQ_mul(s, FpXXQ_sqr(s, Q, pN1), Q, pN1);
     305         693 :   if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
     306         693 :   get_UV(&U, &V, Q, p, N+1);
     307         693 :   F = cgetg(d, t_MAT);
     308        5642 :   for (i = 1; i < d; i++)
     309             :   {
     310        4949 :     pari_sp av2 = avma;
     311             :     GEN M, D;
     312        4949 :     D = diff_red(s, monomial(utoi(p),p*i-1,1),(k*p-1)>>1, Q, pN1);
     313        4949 :     if (DEBUGLEVEL>1) timer_printf(&ti,"red");
     314        4949 :     M = ZpXXQ_frob(D, U, V, (k-1)>>1, Q, p, N + 1);
     315        4949 :     if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
     316        4949 :     gel(F, i) = gerepilecopy(av2, RgX_to_RgC(M, d-1));
     317             :   }
     318         693 :   return gerepileupto(av, F);
     319             : }
     320             : 
     321             : GEN
     322         693 : hyperellpadicfrobenius(GEN H, ulong p, long n)
     323             : {
     324         693 :   pari_sp av = avma;
     325         693 :   GEN M = ZlX_hyperellpadicfrobenius(H, p, n);
     326         693 :   GEN q = zeropadic(utoi(p),n);
     327         693 :   return gerepileupto(av, ZM_to_padic(M, q));
     328             : }
     329             : 
     330             : INLINE GEN
     331        2072 : FpXXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
     332             : 
     333             : static GEN
     334        1680 : ZpXQXXQ_red(GEN F, GEN S, GEN T, GEN q, GEN p, long e)
     335             : {
     336        1680 :   pari_sp av = avma;
     337        1680 :   long i, dF = degpol(F);
     338             :   GEN A, C;
     339        1680 :   if (signe(F)==0) return pol_0(varn(S));
     340        1659 :   A = cgetg(dF+3, t_POL);
     341        1659 :   C = pol_0(varn(S));
     342       93331 :   for(i=dF; i>0; i--)
     343             :   {
     344       91672 :     GEN Fi = FpXX_add(C, gel(F,i+2), q);
     345       91672 :     GEN R, Q = ZpXQX_divrem(Fi, S, T, q, p, e, &R);
     346       91672 :     gel(A,i+2) = R;
     347       91672 :     C = Q;
     348             :   }
     349        1659 :   gel(A,2) = FpXX_add(C, gel(F,2), q);
     350        1659 :   A[1] = F[1];
     351        1659 :   return gerepilecopy(av, FpXXX_renormalize(A,dF+3));
     352             : }
     353             : 
     354             : static GEN
     355         413 : ZpXQXXQ_sqr(GEN x, GEN S, GEN T, GEN q, GEN p, long e)
     356             : {
     357         413 :   pari_sp av = avma;
     358             :   GEN z, kx;
     359         413 :   long n = degpol(S);
     360         413 :   kx = ZXX_to_Kronecker(x, n);
     361         413 :   z = Kronecker_to_ZXX(FpXQX_sqr(kx, T, q), n, varn(S));
     362         413 :   return gerepileupto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     363             : }
     364             : 
     365             : static GEN
     366        1267 : ZpXQXXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN q, GEN p, long e)
     367             : {
     368        1267 :   pari_sp av = avma;
     369             :   GEN z, kx, ky;
     370        1267 :   long n = degpol(S);
     371        1267 :   kx = ZXX_to_Kronecker(x, n);
     372        1267 :   ky = ZXX_to_Kronecker(y, n);
     373        1267 :   z = Kronecker_to_ZXX(FpXQX_mul(ky, kx, T, q), n, varn(S));
     374        1267 :   return gerepileupto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     375             : }
     376             : 
     377             : static GEN
     378         413 : FpXXX_red(GEN z, GEN p)
     379             : {
     380             :   GEN res;
     381         413 :   long i, l = lg(z);
     382         413 :   res = cgetg(l,t_POL); res[1] = z[1];
     383       16807 :   for (i=2; i<l; i++)
     384             :   {
     385       16394 :     GEN zi = gel(z,i);
     386       16394 :     if (typ(zi)==t_INT)
     387           0 :       gel(res,i) = modii(zi,p);
     388             :     else
     389       16394 :      gel(res,i) = FpXX_red(zi,p);
     390             :   }
     391         413 :   return FpXXX_renormalize(res,lg(res));
     392             : }
     393             : 
     394             : static GEN
     395         413 : FpXXX_Fp_mul(GEN z, GEN a, GEN p)
     396             : {
     397         413 :   return FpXXX_red(RgX_Rg_mul(z, a), p);
     398             : }
     399             : 
     400             : static GEN
     401         147 : ZpXQXXQ_invsqrt(GEN F, GEN S, GEN T, ulong p, long e)
     402             : {
     403         147 :   pari_sp av = avma, av2, av3;
     404             :   ulong mask;
     405         147 :   long v = varn(F), n=1;
     406             :   pari_timer ti;
     407         147 :   GEN a = pol_1(v), pp = utoi(p);
     408         147 :   if (DEBUGLEVEL>1) timer_start(&ti);
     409         147 :   if (e <= 1) return gerepilecopy(av, a);
     410         147 :   mask = quadratic_prec_mask(e);
     411         147 :   av2 = avma;
     412         707 :   for (;mask>1;)
     413             :   {
     414             :     GEN q, q2, q22, f, fq, afq;
     415         413 :     long n2 = n;
     416         413 :     n<<=1; if (mask & 1) n--;
     417         413 :     mask >>= 1;
     418         413 :     q = powuu(p,n); q2 = powuu(p,n2);
     419         413 :     av3 = avma;
     420         413 :     f = RgX_sub(ZpXQXXQ_mul(F, ZpXQXXQ_sqr(a, S, T, q, pp, n), S, T, q, pp, n), pol_1(v));
     421         413 :     fq = gerepileupto(av3, RgX_Rg_divexact(f, q2));
     422         413 :     q22 = shifti(addiu(q2,1),-1);
     423         413 :     afq = FpXXX_Fp_mul(ZpXQXXQ_mul(a, fq, S, T, q2, pp, n2), q22, q2);
     424         413 :     a = RgX_sub(a, RgX_Rg_mul(afq, q2));
     425         413 :     if (gc_needed(av2,1))
     426             :     {
     427           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_invsqrt, e = %ld", n);
     428           0 :       a = gerepileupto(av2, a);
     429             :     }
     430             :   }
     431         147 :   return gerepileupto(av, a);
     432             : }
     433             : 
     434             : static GEN
     435        6573 : frac_to_Fq(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     436             : {
     437        6573 :   GEN d = gcdii(ZX_content(a), ZX_content(b));
     438        6573 :   return ZpXQ_div(ZX_Z_divexact(a, d), ZX_Z_divexact(b, d), T, q, p, e);
     439             : }
     440             : 
     441             : static GEN
     442         441 : ZpXQXXQ_frob(GEN F, GEN U, GEN V, long k, GEN S, GEN T, ulong p, long e)
     443             : {
     444         441 :   pari_sp av = avma, av2;
     445         441 :   long i, pr = degpol(F), dS = degpol(S), v = varn(T);
     446         441 :   GEN q = powuu(p,e), pp = utoi(p);
     447         441 :   GEN Sp = RgX_deriv(S), Sp1 = RgX_shift_shallow(Sp, 1);
     448         441 :   GEN M = gel(F,pr+2), R;
     449         441 :   av2 = avma;
     450       51191 :   for(i = pr-1; i>=k; i--)
     451             :   {
     452             :     GEN A, B, H, Bc;
     453             :     ulong v, r;
     454       50750 :     H = ZpXQX_divrem(FpXQX_mul(V, M, T, q), S, T, q, utoi(p), e, &B);
     455       50750 :     A = FpXX_add(FpXQX_mul(U, M, T, q), FpXQX_mul(H, Sp, T, q),q);
     456       50750 :     v = u_lvalrem(2*i+1,p,&r);
     457       50750 :     Bc = RgX_deriv(B);
     458       50750 :     Bc = FpXX_Fp_mul(ZXX_Z_divexact(Bc,powuu(p,v)), Fp_div(gen_2, utoi(r), q), q);
     459       50750 :     M = FpXX_add(gel(F,i+2), FpXX_add(A, Bc, q), q);
     460       50750 :     if (gc_needed(av2,1))
     461             :     {
     462           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_frob, step 1, i = %ld", i);
     463           0 :       M = gerepileupto(av2, M);
     464             :     }
     465             :   }
     466         441 :   if (degpol(M)<dS-1)
     467         238 :     return gerepileupto(av, M);
     468         203 :   R = RgX_shift_shallow(M,dS-degpol(M)-2);
     469         203 :   av2 = avma;
     470        7175 :   for(i = degpol(M)-dS+2; i>=1; i--)
     471             :   {
     472             :     GEN B, c;
     473        6972 :     R = RgX_shift_shallow(R, 1);
     474        6972 :     gel(R,2) = gel(M, i+1);
     475        6972 :     if (degpol(R) < dS) continue;
     476        6412 :     B = FpXX_add(FpXX_mulu(S, 2*i, q), Sp1, q);
     477        6412 :     c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(B),v), T, q, pp, e);
     478        6412 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(B, c, T, q), q);
     479        6412 :     if (gc_needed(av2,1))
     480             :     {
     481           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
     482           0 :       R = gerepileupto(av2, R);
     483             :     }
     484             :   }
     485         203 :   if (degpol(R)==dS-1)
     486             :   {
     487         161 :     GEN c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(Sp),v), T, q, pp, e);
     488         161 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(Sp, c, T, q), q);
     489         161 :     return gerepileupto(av, R);
     490             :   } else
     491          42 :     return gerepilecopy(av, R);
     492             : }
     493             : 
     494             : 
     495             : static GEN
     496         441 : Fq_diff_red(GEN s, GEN A, long m, GEN S, GEN T, GEN q, GEN p, long e)
     497             : {
     498             :   long v, n;
     499             :   GEN Q, sQ, qS;
     500             :   pari_timer ti;
     501         441 :   if (DEBUGLEVEL>1) timer_start(&ti);
     502         441 :   Q = revdigits(ZpXQX_digits(A, S, T, q, p, e));
     503         441 :   n = degpol(Q);
     504         441 :   if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
     505         441 :   sQ = ZpXQXXQ_mul(s, Q, S, T, q, p, e);
     506         441 :   if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
     507         441 :   qS = RgX_shift_shallow(sQ,m-n);
     508         441 :   v = ZX_val(sQ);
     509         441 :   if (n > m + v)
     510             :   {
     511         189 :     long i, l = n-m-v;
     512         189 :     GEN rS = cgetg(l+1,t_VEC);
     513        1547 :     for (i = l-1; i >=0 ; i--)
     514        1358 :       gel(rS,i+1) = gel(sQ, 1+v+l-i);
     515         189 :     rS = FpXQXV_FpXQX_fromdigits(rS, S, T, q);
     516         189 :     gel(qS,2) = FpXX_add(FpXQX_mul(rS, S, T, q), gel(qS, 2), q);
     517         189 :     if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
     518             :   }
     519         441 :   return qS;
     520             : }
     521             : 
     522             : static void
     523         147 : Fq_get_UV(GEN *U, GEN *V, GEN S, GEN T, ulong p, long e)
     524             : {
     525         147 :   GEN q = powuu(p, e), d;
     526         147 :   GEN dS = RgX_deriv(S);
     527         147 :   GEN R  = polresultantext(S, dS), C;
     528         147 :   long v = varn(S);
     529         147 :   if (signe(FpX_red(to_ZX(gel(R,3),v),utoi(p)))==0) is_sing(S, p);
     530         140 :   C = FpXQ_red(to_ZX(gel(R, 3),v), T, q);
     531         140 :   d = ZpXQ_inv(C, T, utoi(p), e);
     532         140 :   *U = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,1),v),T,q),d,T,q);
     533         140 :   *V = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,2),v),T,q),d,T,q);
     534         140 : }
     535             : 
     536             : static GEN
     537         441 : ZXX_to_FpXC(GEN x, long N, GEN p, long v)
     538             : {
     539             :   long i, l;
     540             :   GEN z;
     541         441 :   l = lg(x)-1; x++;
     542         441 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     543         441 :   z = cgetg(N+1,t_COL);
     544        2030 :   for (i=1; i<l ; i++)
     545             :   {
     546        1589 :     GEN xi = gel(x, i);
     547        1589 :     gel(z,i) = typ(xi)==t_INT? scalarpol(Fp_red(xi, p), v): FpX_red(xi, p);
     548             :   }
     549         483 :   for (   ; i<=N ; i++)
     550          42 :     gel(z,i) = pol_0(v);
     551         441 :   return z;
     552             : }
     553             : 
     554             : GEN
     555         147 : ZlXQX_hyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     556             : {
     557         147 :   pari_sp av = avma;
     558             :   long k, N, i, d, N1;
     559             :   GEN xp, F, s, q, Q, pN1, U, V, pp;
     560             :   pari_timer ti;
     561         147 :   if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
     562         147 :   if (p == 2) is_sing(H, 2);
     563         147 :   d = degpol(H);
     564         147 :   if (d <= 0)
     565           0 :     pari_err_CONSTPOL("hyperellpadicfrobenius");
     566         147 :   if (n < 1)
     567           0 :     pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
     568         147 :   k = get_basis(p, d); pp = utoi(p);
     569         147 :   N = n + ulogint(2*n, p) + 1;
     570         147 :   q = powuu(p,n); N1 = N+1;
     571         147 :   pN1 = powuu(p,N1); T = FpX_get_red(T, pN1);
     572         147 :   Q = RgX_to_FqX(H, T, pN1);
     573         147 :   if (signe(FpX_red(to_ZX(leading_coeff(Q),varn(Q)),pp))==0) is_sing(H, p);
     574         147 :   if (DEBUGLEVEL>1) timer_start(&ti);
     575         147 :   xp = ZpX_Frobenius(T, pp, N1);
     576         147 :   s = RgX_inflate(FpXY_FpXQ_evalx(Q, xp, T, pN1), p);
     577         147 :   s = revdigits(ZpXQX_digits(s, Q, T, pN1, pp, N1));
     578         147 :   if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
     579         147 :   s = ZpXQXXQ_invsqrt(s, Q, T, p, N);
     580         147 :   if (k==3)
     581           0 :     s = ZpXQXXQ_mul(s, ZpXQXXQ_sqr(s, Q, T, pN1, pp, N1), Q, T, pN1, pp, N1);
     582         147 :   if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
     583         147 :   Fq_get_UV(&U, &V, Q, T, p, N+1);
     584         140 :   if (DEBUGLEVEL>1) timer_printf(&ti,"get_UV");
     585         140 :   F = cgetg(d, t_MAT);
     586         581 :   for (i = 1; i < d; i++)
     587             :   {
     588         441 :     pari_sp av2 = avma;
     589             :     GEN M, D;
     590         441 :     D = Fq_diff_red(s, monomial(pp,p*i-1,1),(k*p-1)>>1, Q, T, pN1, pp, N1);
     591         441 :     if (DEBUGLEVEL>1) timer_printf(&ti,"red");
     592         441 :     M = ZpXQXXQ_frob(D, U, V, (k - 1)>>1, Q, T, p, N1);
     593         441 :     if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
     594         441 :     gel(F, i) = gerepileupto(av2, ZXX_to_FpXC(M, d-1, q, varn(T)));
     595             :   }
     596         140 :   return gerepileupto(av, F);
     597             : }
     598             : 
     599             : GEN
     600         147 : nfhyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     601             : {
     602         147 :   pari_sp av = avma;
     603         147 :   GEN M = ZlXQX_hyperellpadicfrobenius(lift_shallow(H),T,p,n);
     604         140 :   GEN MM = ZpXQM_prodFrobenius(M, T, utoi(p), n);
     605         140 :   GEN q = zeropadic(utoi(p),n);
     606         140 :   GEN m = gmul(ZXM_to_padic(MM, q), gmodulo(gen_1, T));
     607         140 :   return gerepileupto(av, m);
     608             : }
     609             : 
     610             : static GEN
     611          70 : F2x_genus2charpoly_naive(GEN P, GEN Q)
     612             : {
     613          70 :   long a, b = 1, c = 0;
     614          70 :   GEN T = mkvecsmall2(P[1], 7);
     615          70 :   GEN PT = F2x_rem(P, T), QT = F2x_rem(Q, T);
     616          70 :   long q0 = F2x_eval(Q, 0), q1 = F2x_eval(Q, 1);
     617          70 :   long dP = F2x_degree(P), dQ = F2x_degree(Q);
     618          70 :   a= dQ<3 ? 0: dP<=5 ? 1: -1;
     619          70 :   a += (q0? F2x_eval(P, 0)? -1: 1: 0) + (q1? F2x_eval(P, 1)? -1: 1: 0);
     620          70 :   b += q0 + q1;
     621          70 :   if (lgpol(QT))
     622          63 :     c = (F2xq_trace(F2xq_div(PT, F2xq_sqr(QT, T), T), T)==0 ? 1: -1);
     623          70 :   return mkvecsmalln(6, 0UL, 4, 2*a, (b+2*c+a*a)>>1, a, 1UL);
     624             : }
     625             : 
     626             : static GEN
     627         217 : Flx_difftable(GEN P, ulong p)
     628             : {
     629         217 :   long i, n = degpol(P);
     630         217 :   GEN V = cgetg(n+2, t_VEC);
     631         217 :   gel(V, n+1) = P;
     632        1484 :   for(i = n; i >= 1; i--)
     633        1267 :     gel(V, i) = Flx_diff1(gel(V, i+1), p);
     634         217 :   return V;
     635             : }
     636             : 
     637             : static GEN
     638        1519 : FlxV_Fl2_eval_pre(GEN V, GEN x, ulong D, ulong p, ulong pi)
     639             : {
     640        1519 :   long i, n = lg(V)-1;
     641        1519 :   GEN r = cgetg(n+1, t_VEC);
     642       11627 :   for (i = 1; i <= n; i++)
     643       10108 :     gel(r, i) = Flx_Fl2_eval_pre(gel(V, i), x, D, p, pi);
     644        1519 :   return r;
     645             : }
     646             : 
     647             : static GEN
     648       44478 : Fl2V_next(GEN V, ulong p)
     649             : {
     650       44478 :   long i, n = lg(V)-1;
     651       44478 :   GEN r = cgetg(n+1, t_VEC);
     652       44478 :   gel(r, 1) = gel(V, 1);
     653      285516 :   for (i = 2; i <= n; i++)
     654      241038 :     gel(r, i) = Flv_add(gel(V, i), gel(V, i-1), p);
     655       44478 :   return r;
     656             : }
     657             : 
     658             : static GEN
     659         217 : Flx_genus2charpoly_naive(GEN H, ulong p)
     660             : {
     661         217 :   pari_sp av = avma, av2;
     662         217 :   ulong pi = get_Fl_red(p);
     663         217 :   ulong i, j, p2 = p>>1, D = 2, e = ((p&2UL) == 0) ? -1 : 1;
     664         217 :   long a, b, c = 0, n = degpol(H);
     665         217 :   GEN t, k = const_vecsmall(p, -1);
     666         217 :   k[1] = 0;
     667         217 :   for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p)) k[j+1] = 1;
     668         217 :   while (k[1+D] >= 0) D++;
     669         217 :   b = n == 5 ? 0 : 1;
     670         217 :   a = b ? k[1+Flx_lead(H)]: 0;
     671         217 :   t = Flx_difftable(H, p);
     672         217 :   av2 = avma;
     673        3472 :   for (i=0; i < p; i++)
     674             :   {
     675        3255 :     ulong v = Flx_eval(H, i, p);
     676        3255 :     a += k[1+v];
     677        3255 :     b += !!v;
     678             :   }
     679        1736 :   for (j=1; j <= p2; j++)
     680             :   {
     681        1519 :     GEN V = FlxV_Fl2_eval_pre(t, mkvecsmall2(0, j), D, p, pi);
     682       45997 :     for (i=0;; i++)
     683             :     {
     684       45997 :       GEN r2 = gel(V, n+1);
     685       91994 :       c += uel(r2,2) ?
     686       43764 :         (uel(r2,1) ? k[1+Fl2_norm_pre(r2, D, p, pi)]: e)
     687       89761 :          : !!uel(r2,1);
     688       45997 :       if (i == p-1) break;
     689       44478 :       V = Fl2V_next(V, p);
     690       44478 :     }
     691        1519 :     avma = av2;
     692             :   }
     693         217 :   avma = av;
     694         217 :   return mkvecsmalln(6, 0UL, p*p, a*p, (b+2*c+a*a)>>1, a, 1UL);
     695             : }
     696             : 
     697             : static GEN
     698         658 : charpoly_funceq(GEN P, GEN q)
     699             : {
     700         658 :   long i, l, g = degpol(P)>>1;
     701         658 :   GEN Q = cgetg_copy(P, &l);
     702         658 :   Q[1] = P[1];
     703        3696 :   for (i=0; i<=g; i++)
     704        3038 :     gel(Q, i+2) = mulii(gel(P, 2*g-i+2), powiu(q, g-i));
     705        3038 :   for (; i<=2*g; i++)
     706        2380 :     gel(Q, i+2) = icopy(gel(P, i+2));
     707         658 :   return Q;
     708             : }
     709             : 
     710             : static long
     711         665 : hyperell_Weil_bound(GEN q, long g, GEN p)
     712             : {
     713         665 :   pari_sp av = avma;
     714         665 :   GEN w = mulii(binomialuu(2*g,g),sqrtint(shifti(powiu(q, g),2)));
     715         665 :   long e = logint(w, p) + 1;
     716         665 :   avma = av; return e;
     717             : }
     718             : 
     719             : GEN
     720         959 : hyperellcharpoly(GEN PQ)
     721             : {
     722         959 :   pari_sp av = avma;
     723         959 :   GEN H, M, R, T=NULL, pp=NULL, q;
     724         959 :   long d, g, n, eps = 0;
     725             :   ulong p;
     726         959 :   if (is_vec_t(typ(PQ)) && lg(PQ)==3)
     727          77 :     H = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
     728             :   else
     729         882 :     H = PQ;
     730         959 :   if (typ(H)!=t_POL || !RgX_is_FpXQX(H, &T, &pp) || !pp)
     731           0 :     pari_err_TYPE("hyperellcharpoly",H);
     732         959 :   p = itou(pp);
     733         959 :   if (!T)
     734             :   {
     735         812 :     if (p==2 && is_vec_t(typ(PQ)))
     736             :     {
     737          70 :       long dP, dQ, v = varn(H);
     738          70 :       GEN P = gel(PQ,1), Q = gel(PQ,2);
     739          70 :       if (typ(P)!=t_POL)  P = scalarpol(P, v);
     740          70 :       if (typ(Q)!=t_POL)  Q = scalarpol(Q, v);
     741          70 :       dP = degpol(P); dQ = degpol(Q);
     742          70 :       if (dP<=6 && dQ <=3 && (dQ==3 || dP>=5))
     743             :       {
     744          70 :         GEN P2 = RgX_to_F2x(P), Q2 = RgX_to_F2x(Q);
     745          70 :         GEN D = F2x_add(F2x_mul(P2, F2x_sqr(F2x_deriv(Q2))), F2x_sqr(F2x_deriv(P2)));
     746          70 :         if (F2x_degree(F2x_gcd(D, Q2))) is_sing(PQ, 2);
     747          70 :         if (dP==6 && dQ<3 && F2x_coeff(P2,5)==F2x_coeff(Q2,2))
     748           0 :           is_sing(PQ, 2); /* The curve is singular at infinity */
     749          70 :         R = zx_to_ZX(F2x_genus2charpoly_naive(P2, Q2));
     750          70 :         return gerepileupto(av, R);
     751             :       }
     752             :     }
     753         742 :     H = RgX_to_FpX(H, pp);
     754         742 :     d = degpol(H); g = (d-1)>>1;
     755         742 :     if (p > 2 && ((d == 5 && p < 17500) || (d == 6 && p < 24500)))
     756             :     {
     757         224 :       GEN Hp = ZX_to_Flx(H, p);
     758         224 :       if (!Flx_is_squarefree(Hp, p)) is_sing(H, p);
     759         217 :       R = zx_to_ZX(Flx_genus2charpoly_naive(Hp, p));
     760         217 :       return gerepileupto(av, R);
     761             :     }
     762         518 :     n = hyperell_Weil_bound(pp, g, pp);
     763         518 :     eps = odd(d)? 0: Fp_issquare(leading_coeff(H), pp);
     764         518 :     M = hyperellpadicfrobenius(H, p, n);
     765         518 :     R = centerlift(carberkowitz(M, 0));
     766         518 :     q = pp;
     767             :   }
     768             :   else
     769             :   {
     770             :     int fixvar;
     771         147 :     T = typ(T)==t_FFELT? FF_mod(T): RgX_to_FpX(T, pp);
     772         147 :     q = powuu(p, degpol(T));
     773         147 :     fixvar = (varncmp(varn(T),varn(H)) <= 0);
     774         147 :     if (fixvar) setvarn(T, fetch_var());
     775         147 :     H = RgX_to_FpXQX(H, T, pp);
     776         147 :     d = degpol(H); eps = odd(d)? 0: Fq_issquare(leading_coeff(H), T, pp);
     777         147 :     g = (d-1)>>1;
     778         147 :     n = hyperell_Weil_bound(q, g, pp);
     779         147 :     M = nfhyperellpadicfrobenius(H, T, p, n);
     780         140 :     R = simplify_shallow(centerlift(liftpol_shallow(carberkowitz(M, 0))));
     781         140 :     if (fixvar) (void)delete_var();
     782             :   }
     783         658 :   if (!odd(d))
     784             :   {
     785         280 :     GEN b = get_basis(p, d) == 3 ? gen_1 : q;
     786         280 :     GEN pn = powuu(p, n);
     787         280 :     R = FpX_div_by_X_x(R, eps? b: negi(b), pn, NULL);
     788         280 :     R = FpX_center(R, pn, shifti(pn,-1));
     789             :   }
     790         658 :   R = charpoly_funceq(R, q);
     791         658 :   return gerepilecopy(av, R);
     792             : }

Generated by: LCOV version 1.11