Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - hyperell.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.1 lcov report (development 29950-285c5b69ed) Lines: 1128 1207 93.5 %
Date: 2025-02-05 09:09:51 Functions: 98 100 98.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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /********************************************************************/
      16             : /**                                                                **/
      17             : /**                     HYPERELLIPTIC CURVES                       **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : #define DEBUGLEVEL DEBUGLEVEL_hyperell
      24             : 
      25             : /* Implementation of Kedlaya Algorithm for counting point on hyperelliptic
      26             : curves by Bill Allombert based on a GP script by Bernadette Perrin-Riou.
      27             : 
      28             : References:
      29             : Pierrick Gaudry and Nicolas G\"urel
      30             : Counting Points in Medium Characteristic Using Kedlaya's Algorithm
      31             : Experiment. Math.  Volume 12, Number 4 (2003), 395-402.
      32             :    http://projecteuclid.org/euclid.em/1087568016
      33             : 
      34             : Harrison, M. An extension of Kedlaya's algorithm for hyperelliptic
      35             :   curves. Journal of Symbolic Computation, 47 (1) (2012), 89-101.
      36             :   http://arxiv.org/pdf/1006.4206v3.pdf
      37             : */
      38             : 
      39             : /* We use the basis of differentials (x^i*dx/y^k) (i=1 to 2*g-1),
      40             :    with k either 1 or 3, depending on p and d, see Harrison paper */
      41             : 
      42             : static long
      43        1764 : get_basis(long p, long d)
      44             : {
      45        1764 :   if (odd(d))
      46         868 :     return p < d-1 ? 3 : 1;
      47             :   else
      48         896 :     return 2*p <= d-2 ? 3 : 1;
      49             : }
      50             : 
      51             : static GEN
      52       20265 : FpXXQ_red(GEN S, GEN T, GEN p)
      53             : {
      54       20265 :   pari_sp av = avma;
      55       20265 :   long i, dS = degpol(S);
      56             :   GEN A, C;
      57       20265 :   if (signe(S)==0) return pol_0(varn(T));
      58       20265 :   A = cgetg(dS+3, t_POL);
      59       20265 :   C = pol_0(varn(T));
      60     1520393 :   for(i=dS; i>0; i--)
      61             :   {
      62     1500128 :     GEN Si = FpX_add(C, gel(S,i+2), p);
      63     1500128 :     GEN R, Q = FpX_divrem(Si, T, p, &R);
      64     1500128 :     gel(A,i+2) = R;
      65     1500128 :     C = Q;
      66             :   }
      67       20265 :   gel(A,2) = FpX_add(C, gel(S,2), p);
      68       20265 :   A[1] = S[1];
      69       20265 :   return gerepilecopy(av, FpXX_renormalize(A,dS+3));
      70             : }
      71             : 
      72             : static GEN
      73        3402 : FpXXQ_sqr(GEN x, GEN T, GEN p)
      74             : {
      75        3402 :   pari_sp av = avma;
      76        3402 :   long n = degpol(T);
      77        3402 :   GEN z = FpX_red(ZXX_sqr_Kronecker(x, n), p);
      78        3402 :   z = Kronecker_to_ZXX(z, n, varn(T));
      79        3402 :   return gerepileupto(av, FpXXQ_red(z, T, p));
      80             : }
      81             : 
      82             : static GEN
      83       16863 : FpXXQ_mul(GEN x, GEN y, GEN T, GEN p)
      84             : {
      85       16863 :   pari_sp av = avma;
      86       16863 :   long n = degpol(T);
      87       16863 :   GEN z = FpX_red(ZXX_mul_Kronecker(x, y, n), p);
      88       16863 :   z = Kronecker_to_ZXX(z, n, varn(T));
      89       16863 :   return gerepileupto(av, FpXXQ_red(z, T, p));
      90             : }
      91             : 
      92             : static GEN
      93        1309 : ZpXXQ_invsqrt(GEN S, GEN T, ulong p, long e)
      94             : {
      95        1309 :   pari_sp av = avma, av2;
      96             :   ulong mask;
      97        1309 :   long v = varn(S), n=1;
      98        1309 :   GEN a = pol_1(v);
      99        1309 :   if (e <= 1) return gerepilecopy(av, a);
     100        1309 :   mask = quadratic_prec_mask(e);
     101        1309 :   av2 = avma;
     102        4676 :   for (;mask>1;)
     103             :   {
     104             :     GEN q, q2, q22, f, fq, afq;
     105        3367 :     long n2 = n;
     106        3367 :     n<<=1; if (mask & 1) n--;
     107        3367 :     mask >>= 1;
     108        3367 :     q = powuu(p,n); q2 = powuu(p,n2);
     109        3367 :     f = RgX_sub(FpXXQ_mul(FpXX_red(S, q), FpXXQ_sqr(a, T, q), T, q), pol_1(v));
     110        3367 :     fq = ZXX_Z_divexact(f, q2);
     111        3367 :     q22 = shifti(addiu(q2,1),-1);
     112        3367 :     afq = FpXX_Fp_mul(FpXXQ_mul(a, fq, T, q2), q22, q2);
     113        3367 :     a = RgX_sub(a, ZXX_Z_mul(afq, q2));
     114        3367 :     if (gc_needed(av2,1))
     115             :     {
     116           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_invsqrt, e = %ld", n);
     117           0 :       a = gerepileupto(av2, a);
     118             :     }
     119             :   }
     120        1309 :   return gerepileupto(av, a);
     121             : }
     122             : 
     123             : static GEN
     124     1029007 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
     125             : 
     126             : static void
     127          14 : is_sing(GEN H, ulong p)
     128             : {
     129          14 :   pari_err_DOMAIN("hyperellpadicfrobenius","H","is singular at",utoi(p),H);
     130           0 : }
     131             : 
     132             : static void
     133        1309 : get_UV(GEN *U, GEN *V, GEN T, ulong p, long e)
     134             : {
     135        1309 :   GEN q = powuu(p,e), d;
     136        1309 :   GEN dT = FpX_deriv(T, q);
     137        1309 :   GEN R = polresultantext(T, dT);
     138        1309 :   long v = varn(T);
     139        1309 :   if (dvdiu(gel(R,3),p)) is_sing(T, p);
     140        1309 :   d = Zp_inv(gel(R,3), utoi(p), e);
     141        1309 :   *U = FpX_Fp_mul(FpX_red(to_ZX(gel(R,1),v),q),d,q);
     142        1309 :   *V = FpX_Fp_mul(FpX_red(to_ZX(gel(R,2),v),q),d,q);
     143        1309 : }
     144             : 
     145             : static GEN
     146      133847 : frac_to_Fp(GEN a, GEN b, GEN p)
     147             : {
     148      133847 :   GEN d = gcdii(a, b);
     149      133847 :   return Fp_div(diviiexact(a, d), diviiexact(b, d), p);
     150             : }
     151             : 
     152             : static GEN
     153       10094 : ZpXXQ_frob(GEN S, GEN U, GEN V, long k, GEN T, ulong p, long e)
     154             : {
     155       10094 :   pari_sp av = avma, av2;
     156       10094 :   long i, pr = degpol(S), dT = degpol(T), vT = varn(T);
     157       10094 :   GEN q = powuu(p,e);
     158       10094 :   GEN Tp = FpX_deriv(T, q), Tp1 = RgX_shift_shallow(Tp, 1);
     159       10094 :   GEN M = to_ZX(gel(S,pr+2),vT) , R;
     160       10094 :   av2 = avma;
     161      987868 :   for(i = pr-1; i>=k; i--)
     162             :   {
     163             :     GEN A, B, H, Bc;
     164             :     ulong v, r;
     165      977774 :     H = FpX_divrem(FpX_mul(V,M,q), T, q, &B);
     166      977774 :     A = FpX_add(FpX_mul(U,M,q), FpX_mul(H, Tp, q),q);
     167      977774 :     v = u_lvalrem(2*i+1,p,&r);
     168      977774 :     Bc = ZX_deriv(B);
     169      977774 :     Bc = FpX_Fp_mul(ZX_divuexact(Bc,upowuu(p,v)),Fp_divu(gen_2, r, q), q);
     170      977774 :     M = FpX_add(to_ZX(gel(S,i+2),vT), FpX_add(A, Bc, q), q);
     171      977774 :     if (gc_needed(av2,1))
     172             :     {
     173           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 1, i = %ld", i);
     174           0 :       M = gerepileupto(av2, M);
     175             :     }
     176             :   }
     177       10094 :   if (degpol(M)<dT-1)
     178        5488 :     return gerepileupto(av, M);
     179        4606 :   R = RgX_shift_shallow(M,dT-degpol(M)-2);
     180        4606 :   av2 = avma;
     181      237629 :   for(i = degpol(M)-dT+2; i>=1; i--)
     182             :   {
     183             :     GEN B, c;
     184      233023 :     R = RgX_shift_shallow(R, 1);
     185      233023 :     gel(R,2) = gel(M, i+1);
     186      233023 :     if (degpol(R) < dT) continue;
     187      130935 :     B = FpX_add(FpX_mulu(T, 2*i, q), Tp1, q);
     188      130935 :     c = frac_to_Fp(leading_coeff(R), leading_coeff(B), q);
     189      130935 :     R = FpX_sub(R, FpX_Fp_mul(B, c, q), q);
     190      130935 :     if (gc_needed(av2,1))
     191             :     {
     192           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
     193           0 :       R = gerepileupto(av2, R);
     194             :     }
     195             :   }
     196        4606 :   if (degpol(R)==dT-1)
     197             :   {
     198        2912 :     GEN c = frac_to_Fp(leading_coeff(R), leading_coeff(Tp), q);
     199        2912 :     R = FpX_sub(R, FpX_Fp_mul(Tp, c, q), q);
     200        2912 :     return gerepileupto(av, R);
     201             :   } else
     202        1694 :     return gerepilecopy(av, R);
     203             : }
     204             : 
     205             : static GEN
     206       12026 : revdigits(GEN v)
     207             : {
     208       12026 :   long i, n = lg(v)-1;
     209       12026 :   GEN w = cgetg(n+2, t_POL);
     210       12026 :   w[1] = evalsigne(1)|evalvarn(0);
     211      168784 :   for (i=0; i<n; i++)
     212      156758 :     gel(w,i+2) = gel(v,n-i);
     213       12026 :   return FpXX_renormalize(w, n+2);
     214             : }
     215             : 
     216             : static GEN
     217       10094 : diff_red(GEN s, GEN A, long m, GEN T, GEN p)
     218             : {
     219       10094 :   long v, n, vT = varn(T);
     220             :   GEN Q, sQ, qS;
     221             :   pari_timer ti;
     222       10094 :   if (DEBUGLEVEL>1) timer_start(&ti);
     223       10094 :   Q = revdigits(FpX_digits(A,T,p));
     224       10094 :   n = degpol(Q);
     225       10094 :   if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
     226       10094 :   sQ = FpXXQ_mul(s,Q,T,p);
     227       10094 :   if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
     228       10094 :   qS = RgX_shift_shallow(sQ,m-n);
     229       10094 :   v = ZX_val(sQ);
     230       10094 :   if (n > m + v)
     231             :   {
     232        4564 :     long i, l = n-m-v;
     233        4564 :     GEN rS = cgetg(l+1,t_VEC);
     234       29190 :     for (i = l-1; i >=0 ; i--)
     235       24626 :       gel(rS,i+1) = to_ZX(gel(sQ, 1+v+l-i), vT);
     236        4564 :     rS = FpXV_FpX_fromdigits(rS,T,p);
     237        4564 :     gel(qS,2) = FpX_add(FpX_mul(rS, T, p), gel(qS, 2), p);
     238        4564 :     if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
     239             :   }
     240       10094 :   return qS;
     241             : }
     242             : 
     243             : static GEN
     244       10094 : ZC_to_padic(GEN C, GEN q)
     245             : {
     246       10094 :   long i, l = lg(C);
     247       10094 :   GEN V = cgetg(l,t_COL);
     248      102914 :   for(i = 1; i < l; i++)
     249       92820 :     gel(V, i) = gadd(gel(C, i), q);
     250       10094 :   return V;
     251             : }
     252             : 
     253             : static GEN
     254        1309 : ZM_to_padic(GEN M, GEN q)
     255             : {
     256        1309 :   long i, l = lg(M);
     257        1309 :   GEN V = cgetg(l,t_MAT);
     258       11403 :   for(i = 1; i < l; i++)
     259       10094 :     gel(V, i) = ZC_to_padic(gel(M, i), q);
     260        1309 :   return V;
     261             : }
     262             : 
     263             : static GEN
     264        1743 : ZX_to_padic(GEN P, GEN q)
     265             : {
     266        1743 :   long i, l = lg(P);
     267        1743 :   GEN Q = cgetg(l, t_POL);
     268        1743 :   Q[1] = P[1];
     269        5978 :   for (i=2; i<l ;i++)
     270        4235 :     gel(Q,i) = gadd(gel(P,i), q);
     271        1743 :   return normalizepol(Q);
     272             : }
     273             : 
     274             : static GEN
     275         469 : ZXC_to_padic(GEN x, GEN q)
     276        2212 : { pari_APPLY_type(t_COL, ZX_to_padic(gel(x, i), q)) }
     277             : 
     278             : static GEN
     279         147 : ZXM_to_padic(GEN x, GEN q)
     280         616 : { pari_APPLY_same(ZXC_to_padic(gel(x, i), q)) }
     281             : 
     282             : static GEN
     283        1309 : ZlX_hyperellpadicfrobenius(GEN H, ulong p, long n)
     284             : {
     285        1309 :   pari_sp av = avma;
     286             :   long k, N, i, d;
     287             :   GEN F, s, Q, pN1, U, V;
     288             :   pari_timer ti;
     289        1309 :   if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
     290        1309 :   if (p == 2) is_sing(H, 2);
     291        1309 :   d = degpol(H);
     292        1309 :   if (d <= 0)
     293           0 :     pari_err_CONSTPOL("hyperellpadicfrobenius");
     294        1309 :   if (n < 1)
     295           0 :     pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
     296        1309 :   k = get_basis(p, d);
     297        1309 :   N = n + ulogint(2*n, p) + 1;
     298        1309 :   pN1 = powuu(p,N+1);
     299        1309 :   Q = RgX_to_FpX(H, pN1);
     300        1309 :   if (dvdiu(leading_coeff(Q),p)) is_sing(H, p);
     301        1309 :   setvarn(Q,1);
     302        1309 :   if (DEBUGLEVEL>1) timer_start(&ti);
     303        1309 :   s = revdigits(FpX_digits(RgX_inflate(Q, p), Q, pN1));
     304        1309 :   if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
     305        1309 :   s = ZpXXQ_invsqrt(s, Q, p, N);
     306        1309 :   if (k==3)
     307          35 :     s = FpXXQ_mul(s, FpXXQ_sqr(s, Q, pN1), Q, pN1);
     308        1309 :   if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
     309        1309 :   get_UV(&U, &V, Q, p, N+1);
     310        1309 :   F = cgetg(d, t_MAT);
     311       11403 :   for (i = 1; i < d; i++)
     312             :   {
     313       10094 :     pari_sp av2 = avma;
     314             :     GEN M, D;
     315       10094 :     D = diff_red(s, monomial(utoipos(p),p*i-1,1),(k*p-1)>>1, Q, pN1);
     316       10094 :     if (DEBUGLEVEL>1) timer_printf(&ti,"red");
     317       10094 :     M = ZpXXQ_frob(D, U, V, (k-1)>>1, Q, p, N + 1);
     318       10094 :     if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
     319       10094 :     gel(F, i) = gerepilecopy(av2, RgX_to_RgC(M, d-1));
     320             :   }
     321        1309 :   return gerepileupto(av, F);
     322             : }
     323             : 
     324             : GEN
     325        1309 : hyperellpadicfrobenius(GEN H, ulong p, long n)
     326             : {
     327        1309 :   pari_sp av = avma;
     328        1309 :   GEN M = ZlX_hyperellpadicfrobenius(H, p, n);
     329        1309 :   GEN q = zeropadic_shallow(utoipos(p),n);
     330        1309 :   return gerepileupto(av, ZM_to_padic(M, q));
     331             : }
     332             : 
     333             : INLINE GEN
     334        2247 : FpXXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
     335             : 
     336             : static GEN
     337        1806 : ZpXQXXQ_red(GEN F, GEN S, GEN T, GEN q, GEN p, long e)
     338             : {
     339        1806 :   pari_sp av = avma;
     340        1806 :   long i, dF = degpol(F);
     341             :   GEN A, C;
     342        1806 :   if (signe(F)==0) return pol_0(varn(S));
     343        1806 :   A = cgetg(dF+3, t_POL);
     344        1806 :   C = pol_0(varn(S));
     345       96404 :   for(i=dF; i>0; i--)
     346             :   {
     347       94598 :     GEN Fi = FpXX_add(C, gel(F,i+2), q);
     348       94598 :     GEN R, Q = ZpXQX_divrem(Fi, S, T, q, p, e, &R);
     349       94598 :     gel(A,i+2) = R;
     350       94598 :     C = Q;
     351             :   }
     352        1806 :   gel(A,2) = FpXX_add(C, gel(F,2), q);
     353        1806 :   A[1] = F[1];
     354        1806 :   return gerepilecopy(av, FpXXX_renormalize(A,dF+3));
     355             : }
     356             : 
     357             : static GEN
     358         448 : ZpXQXXQ_sqr(GEN x, GEN S, GEN T, GEN q, GEN p, long e)
     359             : {
     360         448 :   pari_sp av = avma;
     361             :   GEN z, kx;
     362         448 :   long n = degpol(S);
     363         448 :   kx = RgXX_to_Kronecker(x, n);
     364         448 :   z = Kronecker_to_ZXX(FpXQX_sqr(kx, T, q), n, varn(S));
     365         448 :   return gerepileupto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     366             : }
     367             : 
     368             : static GEN
     369        1358 : ZpXQXXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN q, GEN p, long e)
     370             : {
     371        1358 :   pari_sp av = avma;
     372             :   GEN z, kx, ky;
     373        1358 :   long n = degpol(S);
     374        1358 :   kx = RgXX_to_Kronecker(x, n);
     375        1358 :   ky = RgXX_to_Kronecker(y, n);
     376        1358 :   z = Kronecker_to_ZXX(FpXQX_mul(ky, kx, T, q), n, varn(S));
     377        1358 :   return gerepileupto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     378             : }
     379             : 
     380             : static GEN
     381         441 : FpXXX_red(GEN z, GEN p)
     382             : {
     383             :   GEN res;
     384         441 :   long i, l = lg(z);
     385         441 :   res = cgetg(l,t_POL); res[1] = z[1];
     386       17388 :   for (i=2; i<l; i++)
     387             :   {
     388       16947 :     GEN zi = gel(z,i);
     389       16947 :     if (typ(zi)==t_INT)
     390           0 :       gel(res,i) = modii(zi,p);
     391             :     else
     392       16947 :      gel(res,i) = FpXX_red(zi,p);
     393             :   }
     394         441 :   return FpXXX_renormalize(res,lg(res));
     395             : }
     396             : 
     397             : static GEN
     398         441 : FpXXX_Fp_mul(GEN z, GEN a, GEN p)
     399             : {
     400         441 :   return FpXXX_red(RgX_Rg_mul(z, a), p);
     401             : }
     402             : 
     403             : static GEN
     404         154 : ZpXQXXQ_invsqrt(GEN F, GEN S, GEN T, ulong p, long e)
     405             : {
     406         154 :   pari_sp av = avma, av2, av3;
     407             :   ulong mask;
     408         154 :   long v = varn(F), n=1;
     409             :   pari_timer ti;
     410         154 :   GEN a = pol_1(v), pp = utoipos(p);
     411         154 :   if (DEBUGLEVEL>1) timer_start(&ti);
     412         154 :   if (e <= 1) return gerepilecopy(av, a);
     413         154 :   mask = quadratic_prec_mask(e);
     414         154 :   av2 = avma;
     415         595 :   for (;mask>1;)
     416             :   {
     417             :     GEN q, q2, q22, f, fq, afq;
     418         441 :     long n2 = n;
     419         441 :     n<<=1; if (mask & 1) n--;
     420         441 :     mask >>= 1;
     421         441 :     q = powuu(p,n); q2 = powuu(p,n2);
     422         441 :     av3 = avma;
     423         441 :     f = RgX_sub(ZpXQXXQ_mul(F, ZpXQXXQ_sqr(a, S, T, q, pp, n), S, T, q, pp, n), pol_1(v));
     424         441 :     fq = gerepileupto(av3, RgX_Rg_divexact(f, q2));
     425         441 :     q22 = shifti(addiu(q2,1),-1);
     426         441 :     afq = FpXXX_Fp_mul(ZpXQXXQ_mul(a, fq, S, T, q2, pp, n2), q22, q2);
     427         441 :     a = RgX_sub(a, RgX_Rg_mul(afq, q2));
     428         441 :     if (gc_needed(av2,1))
     429             :     {
     430           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_invsqrt, e = %ld", n);
     431           0 :       a = gerepileupto(av2, a);
     432             :     }
     433             :   }
     434         154 :   return gerepileupto(av, a);
     435             : }
     436             : 
     437             : static GEN
     438        6573 : frac_to_Fq(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     439             : {
     440        6573 :   GEN d = gcdii(ZX_content(a), ZX_content(b));
     441        6573 :   return ZpXQ_div(ZX_Z_divexact(a, d), ZX_Z_divexact(b, d), T, q, p, e);
     442             : }
     443             : 
     444             : static GEN
     445         469 : ZpXQXXQ_frob(GEN F, GEN U, GEN V, long k, GEN S, GEN T, ulong p, long e)
     446             : {
     447         469 :   pari_sp av = avma, av2;
     448         469 :   long i, pr = degpol(F), dS = degpol(S), v = varn(T);
     449         469 :   GEN q = powuu(p,e), pp = utoipos(p);
     450         469 :   GEN Sp = RgX_deriv(S), Sp1 = RgX_shift_shallow(Sp, 1);
     451         469 :   GEN M = gel(F,pr+2), R;
     452         469 :   av2 = avma;
     453       52311 :   for(i = pr-1; i>=k; i--)
     454             :   {
     455             :     GEN A, B, H, Bc;
     456             :     ulong v, r;
     457       51842 :     H = ZpXQX_divrem(FpXQX_mul(V, M, T, q), S, T, q, utoipos(p), e, &B);
     458       51842 :     A = FpXX_add(FpXQX_mul(U, M, T, q), FpXQX_mul(H, Sp, T, q),q);
     459       51842 :     v = u_lvalrem(2*i+1,p,&r);
     460       51842 :     Bc = RgX_deriv(B);
     461       51842 :     Bc = FpXX_Fp_mul(ZXX_Z_divexact(Bc,powuu(p,v)), Fp_divu(gen_2, r, q), q);
     462       51842 :     M = FpXX_add(gel(F,i+2), FpXX_add(A, Bc, q), q);
     463       51842 :     if (gc_needed(av2,1))
     464             :     {
     465           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_frob, step 1, i = %ld", i);
     466           0 :       M = gerepileupto(av2, M);
     467             :     }
     468             :   }
     469         469 :   if (degpol(M)<dS-1)
     470         266 :     return gerepileupto(av, M);
     471         203 :   R = RgX_shift_shallow(M,dS-degpol(M)-2);
     472         203 :   av2 = avma;
     473        7175 :   for(i = degpol(M)-dS+2; i>=1; i--)
     474             :   {
     475             :     GEN B, c;
     476        6972 :     R = RgX_shift_shallow(R, 1);
     477        6972 :     gel(R,2) = gel(M, i+1);
     478        6972 :     if (degpol(R) < dS) continue;
     479        6412 :     B = FpXX_add(FpXX_mulu(S, 2*i, q), Sp1, q);
     480        6412 :     c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(B),v), T, q, pp, e);
     481        6412 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(B, c, T, q), q);
     482        6412 :     if (gc_needed(av2,1))
     483             :     {
     484           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
     485           0 :       R = gerepileupto(av2, R);
     486             :     }
     487             :   }
     488         203 :   if (degpol(R)==dS-1)
     489             :   {
     490         161 :     GEN c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(Sp),v), T, q, pp, e);
     491         161 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(Sp, c, T, q), q);
     492         161 :     return gerepileupto(av, R);
     493             :   } else
     494          42 :     return gerepilecopy(av, R);
     495             : }
     496             : 
     497             : static GEN
     498         469 : Fq_diff_red(GEN s, GEN A, long m, GEN S, GEN T, GEN q, GEN p, long e)
     499             : {
     500             :   long v, n;
     501             :   GEN Q, sQ, qS;
     502             :   pari_timer ti;
     503         469 :   if (DEBUGLEVEL>1) timer_start(&ti);
     504         469 :   Q = revdigits(ZpXQX_digits(A, S, T, q, p, e));
     505         469 :   n = degpol(Q);
     506         469 :   if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
     507         469 :   sQ = ZpXQXXQ_mul(s, Q, S, T, q, p, e);
     508         469 :   if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
     509         469 :   qS = RgX_shift_shallow(sQ,m-n);
     510         469 :   v = ZX_val(sQ);
     511         469 :   if (n > m + v)
     512             :   {
     513         189 :     long i, l = n-m-v;
     514         189 :     GEN rS = cgetg(l+1,t_VEC);
     515        1547 :     for (i = l-1; i >=0 ; i--)
     516        1358 :       gel(rS,i+1) = gel(sQ, 1+v+l-i);
     517         189 :     rS = FpXQXV_FpXQX_fromdigits(rS, S, T, q);
     518         189 :     gel(qS,2) = FpXX_add(FpXQX_mul(rS, S, T, q), gel(qS, 2), q);
     519         189 :     if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
     520             :   }
     521         469 :   return qS;
     522             : }
     523             : 
     524             : static void
     525         154 : Fq_get_UV(GEN *U, GEN *V, GEN S, GEN T, ulong p, long e)
     526             : {
     527         154 :   GEN q = powuu(p, e), pp = utoipos(p), d;
     528         154 :   GEN dS = RgX_deriv(S), R  = polresultantext(S, dS), C;
     529         154 :   long v = varn(S);
     530         154 :   if (signe(FpX_red(to_ZX(gel(R,3),v), pp))==0) is_sing(S, p);
     531         147 :   C = FpXQ_red(to_ZX(gel(R, 3),v), T, q);
     532         147 :   d = ZpXQ_inv(C, T, pp, e);
     533         147 :   *U = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,1),v),T,q),d,T,q);
     534         147 :   *V = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,2),v),T,q),d,T,q);
     535         147 : }
     536             : 
     537             : static GEN
     538         469 : ZXX_to_FpXC(GEN x, long N, GEN p, long v)
     539             : {
     540             :   long i, l;
     541             :   GEN z;
     542         469 :   l = lg(x)-1; x++;
     543         469 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     544         469 :   z = cgetg(N+1,t_COL);
     545        2170 :   for (i=1; i<l ; i++)
     546             :   {
     547        1701 :     GEN xi = gel(x, i);
     548        1701 :     gel(z,i) = typ(xi)==t_INT? scalarpol(Fp_red(xi, p), v): FpX_red(xi, p);
     549             :   }
     550         511 :   for (   ; i<=N ; i++)
     551          42 :     gel(z,i) = pol_0(v);
     552         469 :   return z;
     553             : }
     554             : 
     555             : GEN
     556         154 : ZlXQX_hyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     557             : {
     558         154 :   pari_sp av = avma;
     559             :   long k, N, i, d, N1;
     560             :   GEN xp, F, s, q, Q, pN1, U, V, pp;
     561             :   pari_timer ti;
     562         154 :   if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
     563         154 :   if (p == 2) is_sing(H, 2);
     564         154 :   d = degpol(H);
     565         154 :   if (d <= 0) pari_err_CONSTPOL("hyperellpadicfrobenius");
     566         154 :   if (n < 1) pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
     567         154 :   k = get_basis(p, d); pp = utoipos(p);
     568         154 :   N = n + ulogint(2*n, p) + 1;
     569         154 :   q = powuu(p,n); N1 = N+1;
     570         154 :   pN1 = powuu(p,N1); T = FpX_get_red(T, pN1);
     571         154 :   Q = RgX_to_FqX(H, T, pN1);
     572         154 :   if (signe(FpX_red(to_ZX(leading_coeff(Q),varn(Q)),pp))==0) is_sing(H, p);
     573         154 :   if (DEBUGLEVEL>1) timer_start(&ti);
     574         154 :   xp = ZpX_Frobenius(T, pp, N1);
     575         154 :   s = RgX_inflate(FpXY_FpXQ_evalx(Q, xp, T, pN1), p);
     576         154 :   s = revdigits(ZpXQX_digits(s, Q, T, pN1, pp, N1));
     577         154 :   if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
     578         154 :   s = ZpXQXXQ_invsqrt(s, Q, T, p, N);
     579         154 :   if (k==3)
     580           7 :     s = ZpXQXXQ_mul(s, ZpXQXXQ_sqr(s, Q, T, pN1, pp, N1), Q, T, pN1, pp, N1);
     581         154 :   if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
     582         154 :   Fq_get_UV(&U, &V, Q, T, p, N+1);
     583         147 :   if (DEBUGLEVEL>1) timer_printf(&ti,"get_UV");
     584         147 :   F = cgetg(d, t_MAT);
     585         616 :   for (i = 1; i < d; i++)
     586             :   {
     587         469 :     pari_sp av2 = avma;
     588             :     GEN M, D;
     589         469 :     D = Fq_diff_red(s, monomial(pp,p*i-1,1),(k*p-1)>>1, Q, T, pN1, pp, N1);
     590         469 :     if (DEBUGLEVEL>1) timer_printf(&ti,"red");
     591         469 :     M = ZpXQXXQ_frob(D, U, V, (k - 1)>>1, Q, T, p, N1);
     592         469 :     if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
     593         469 :     gel(F, i) = gerepileupto(av2, ZXX_to_FpXC(M, d-1, q, varn(T)));
     594             :   }
     595         147 :   return gerepileupto(av, F);
     596             : }
     597             : 
     598             : GEN
     599         154 : nfhyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     600             : {
     601         154 :   pari_sp av = avma;
     602         154 :   GEN pp = utoipos(p), q = zeropadic_shallow(pp, n);
     603         154 :   GEN M = ZlXQX_hyperellpadicfrobenius(lift_shallow(H),T,p,n);
     604         147 :   GEN MM = ZpXQM_prodFrobenius(M, T, pp, n);
     605         147 :   GEN m = gmul(ZXM_to_padic(MM, q), gmodulo(gen_1, T));
     606         147 :   return gerepileupto(av, m);
     607             : }
     608             : 
     609             : GEN
     610         595 : hyperellpadicfrobenius0(GEN H, GEN Tp, long n)
     611             : {
     612             :   GEN T, p;
     613         595 :   if (!ff_parse_Tp(Tp, &T,&p,0)) pari_err_TYPE("hyperellpadicfrobenius", Tp);
     614         595 :   if (lgefint(p) > 3) pari_err_IMPL("large prime in hyperellpadicfrobenius");
     615           7 :   return T? nfhyperellpadicfrobenius(H, T, itou(p), n)
     616         602 :           : hyperellpadicfrobenius(H, itou(p), n);
     617             : }
     618             : 
     619             : static GEN
     620          84 : F2x_genus2charpoly_naive(GEN P, GEN Q)
     621             : {
     622          84 :   long a, b = 1, c = 0;
     623          84 :   GEN T = mkvecsmall2(P[1], 7);
     624          84 :   GEN PT = F2x_rem(P, T), QT = F2x_rem(Q, T);
     625          84 :   long q0 = F2x_eval(Q, 0), q1 = F2x_eval(Q, 1);
     626          84 :   long dP = F2x_degree(P), dQ = F2x_degree(Q);
     627          84 :   a= dQ<3 ? 0: dP<=5 ? 1: -1;
     628          84 :   a += (q0? F2x_eval(P, 0)? -1: 1: 0) + (q1? F2x_eval(P, 1)? -1: 1: 0);
     629          84 :   b += q0 + q1;
     630          84 :   if (lgpol(QT))
     631          70 :     c = (F2xq_trace(F2xq_div(PT, F2xq_sqr(QT, T), T), T)==0 ? 1: -1);
     632          84 :   return mkvecsmalln(6, 0UL, 4UL, 2*a, (b+2*c+a*a)>>1, a, 1UL);
     633             : }
     634             : 
     635             : static GEN
     636         259 : Flx_difftable(GEN P, ulong p)
     637             : {
     638         259 :   long i, n = degpol(P);
     639         259 :   GEN V = cgetg(n+2, t_VEC);
     640         259 :   gel(V, n+1) = P;
     641        1771 :   for(i = n; i >= 1; i--)
     642        1512 :     gel(V, i) = Flx_diff1(gel(V, i+1), p);
     643         259 :   return V;
     644             : }
     645             : 
     646             : static GEN
     647        1582 : FlxV_Fl2_eval_pre(GEN V, GEN x, ulong D, ulong p, ulong pi)
     648             : {
     649        1582 :   long i, n = lg(V)-1;
     650        1582 :   GEN r = cgetg(n+1, t_VEC);
     651       12110 :   for (i = 1; i <= n; i++)
     652       10528 :     gel(r, i) = Flx_Fl2_eval_pre(gel(V, i), x, D, p, pi);
     653        1582 :   return r;
     654             : }
     655             : 
     656             : static GEN
     657       44716 : Fl2V_next(GEN V, ulong p)
     658             : {
     659       44716 :   long i, n = lg(V)-1;
     660       44716 :   GEN r = cgetg(n+1, t_VEC);
     661       44716 :   gel(r, 1) = gel(V, 1);
     662      287056 :   for (i = 2; i <= n; i++)
     663      242340 :     gel(r, i) = Flv_add(gel(V, i), gel(V, i-1), p);
     664       44716 :   return r;
     665             : }
     666             : 
     667             : static GEN
     668         259 : Flx_genus2charpoly_naive(GEN H, ulong p)
     669             : {
     670         259 :   pari_sp av = avma, av2;
     671         259 :   ulong pi = get_Fl_red(p);
     672         259 :   ulong i, j, p2 = p>>1, D = 2, e = ((p&2UL) == 0) ? -1 : 1;
     673         259 :   long a, b, c = 0, n = degpol(H);
     674         259 :   GEN t, k = const_vecsmall(p, -1);
     675         259 :   k[1] = 0;
     676        1841 :   for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p)) k[j+1] = 1;
     677         308 :   while (k[1+D] >= 0) D++;
     678         259 :   b = n == 5 ? 0 : 1;
     679         259 :   a = b ? k[1+Flx_lead(H)]: 0;
     680         259 :   t = Flx_difftable(H, p);
     681         259 :   av2 = avma;
     682        3682 :   for (i=0; i < p; i++)
     683             :   {
     684        3423 :     ulong v = Flx_eval(H, i, p);
     685        3423 :     a += k[1+v];
     686        3423 :     b += !!v;
     687             :   }
     688        1841 :   for (j=1; j <= p2; j++)
     689             :   {
     690        1582 :     GEN V = FlxV_Fl2_eval_pre(t, mkvecsmall2(0, j), D, p, pi);
     691        1582 :     for (i=0;; i++)
     692       44716 :     {
     693       46298 :       GEN r2 = gel(V, n+1);
     694       92596 :       c += uel(r2,2) ?
     695       44009 :         (uel(r2,1) ? uel(k,1+Fl2_norm_pre(r2, D, p, pi)): e)
     696       90307 :          : !!uel(r2,1);
     697       46298 :       if (i == p-1) break;
     698       44716 :       V = Fl2V_next(V, p);
     699             :     }
     700        1582 :     set_avma(av2);
     701             :   }
     702         259 :   set_avma(av);
     703         259 :   return mkvecsmalln(6, 0UL, p*p, a*p, (b+2*c+a*a)>>1, a, 1UL);
     704             : }
     705             : 
     706             : static GEN
     707         679 : charpoly_funceq(GEN P, GEN q)
     708             : {
     709         679 :   long i, l, g = degpol(P)>>1;
     710         679 :   GEN R, Q = gpowers0(q, g-1, q); /* Q[i] = q^i, i <= g */
     711         679 :   R = cgetg_copy(P, &l); R[1] = P[1];
     712        3164 :   for (i=0; i<g; i++) gel(R, i+2) = mulii(gel(P, 2*g-i+2), gel(Q, g-i));
     713        3843 :   for (; i<=2*g; i++) gel(R, i+2) = icopy(gel(P, i+2));
     714         679 :   return R;
     715             : }
     716             : 
     717             : static long
     718         686 : hyperell_Weil_bound(GEN q, ulong g, GEN p)
     719             : {
     720         686 :   pari_sp av = avma;
     721         686 :   GEN w = mulii(binomialuu(2*g,g),sqrtint(shifti(powiu(q, g),2)));
     722         686 :   return gc_long(av, logint(w,p) + 1);
     723             : }
     724             : 
     725             : /* return 4P + Q^2 */
     726             : static GEN
     727      289139 : check_hyperell(GEN PQ)
     728             : {
     729             :   GEN H;
     730      289139 :   if (is_vec_t(typ(PQ)) && lg(PQ)==3)
     731      225651 :     H = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
     732             :   else
     733       63488 :     H = gmul2n(PQ, 2);
     734      289139 :   return typ(H) == t_POL? H: NULL;
     735             : }
     736             : 
     737             : GEN
     738        1036 : hyperellcharpoly(GEN PQ)
     739             : {
     740        1036 :   pari_sp av = avma;
     741        1036 :   GEN M, R, T=NULL, pp=NULL, q;
     742        1036 :   long d, n, eps = 0;
     743             :   ulong p;
     744        1036 :   GEN H = check_hyperell(PQ);
     745        1036 :   if (!H || !RgX_is_FpXQX(H, &T, &pp) || !pp)
     746           0 :     pari_err_TYPE("hyperellcharpoly", PQ);
     747        1036 :   p = itou(pp);
     748        1036 :   if (!T)
     749             :   {
     750         889 :     if (p==2 && is_vec_t(typ(PQ)))
     751             :     {
     752          84 :       long dP, dQ, v = varn(H);
     753          84 :       GEN P = gel(PQ,1), Q = gel(PQ,2);
     754          84 :       if (typ(P)!=t_POL)  P = scalarpol(P, v);
     755          84 :       if (typ(Q)!=t_POL)  Q = scalarpol(Q, v);
     756          84 :       dP = degpol(P); dQ = degpol(Q);
     757          84 :       if (dP<=6 && dQ <=3 && (dQ==3 || dP>=5))
     758             :       {
     759          84 :         GEN P2 = RgX_to_F2x(P), Q2 = RgX_to_F2x(Q);
     760          84 :         GEN D = F2x_add(F2x_mul(P2, F2x_sqr(F2x_deriv(Q2))), F2x_sqr(F2x_deriv(P2)));
     761          84 :         if (F2x_degree(F2x_gcd(D, Q2))) is_sing(PQ, 2);
     762          84 :         if (dP==6 && dQ<3 && F2x_coeff(P2,5)==F2x_coeff(Q2,2))
     763           0 :           is_sing(PQ, 2); /* The curve is singular at infinity */
     764          84 :         R = zx_to_ZX(F2x_genus2charpoly_naive(P2, Q2));
     765          84 :         return gerepileupto(av, R);
     766             :       }
     767             :     }
     768         805 :     H = RgX_to_FpX(H, pp);
     769         805 :     d = degpol(H);
     770         805 :     if (d <= 0) is_sing(H, p);
     771         805 :     if (p > 2 && ((d == 5 && p < 17500) || (d == 6 && p < 24500)))
     772             :     {
     773         266 :       GEN Hp = ZX_to_Flx(H, p);
     774         266 :       if (!Flx_is_squarefree(Hp, p)) is_sing(H, p);
     775         259 :       R = zx_to_ZX(Flx_genus2charpoly_naive(Hp, p));
     776         259 :       return gerepileupto(av, R);
     777             :     }
     778         539 :     n = hyperell_Weil_bound(pp, (d-1)>>1, pp);
     779         539 :     eps = odd(d)? 0: Fp_issquare(leading_coeff(H), pp);
     780         539 :     M = hyperellpadicfrobenius(H, p, n);
     781         539 :     R = centerlift(carberkowitz(M, 0));
     782         539 :     q = pp;
     783             :   }
     784             :   else
     785             :   {
     786             :     int fixvar;
     787         147 :     T = typ(T)==t_FFELT? FF_mod(T): RgX_to_FpX(T, pp);
     788         147 :     q = powuu(p, degpol(T));
     789         147 :     fixvar = (varncmp(varn(T),varn(H)) <= 0);
     790         147 :     if (fixvar) setvarn(T, fetch_var());
     791         147 :     H = RgX_to_FpXQX(H, T, pp);
     792         147 :     d = degpol(H);
     793         147 :     if (d <= 0) is_sing(H, p);
     794         147 :     eps = odd(d)? 0: Fq_issquare(leading_coeff(H), T, pp);
     795         147 :     n = hyperell_Weil_bound(q, (d-1)>>1, pp);
     796         147 :     M = nfhyperellpadicfrobenius(H, T, p, n);
     797         140 :     R = simplify_shallow(centerlift(liftpol_shallow(carberkowitz(M, 0))));
     798         140 :     if (fixvar) (void)delete_var();
     799             :   }
     800         679 :   if (!odd(d))
     801             :   {
     802         301 :     GEN b = get_basis(p, d) == 3 ? gen_1 : q;
     803         301 :     GEN pn = powuu(p, n);
     804         301 :     R = FpX_div_by_X_x(R, eps? b: negi(b), pn, NULL);
     805         301 :     R = FpX_center_i(R, pn, shifti(pn,-1));
     806             :   }
     807         679 :   return gerepileupto(av, charpoly_funceq(R, q));
     808             : }
     809             : 
     810             : int
     811        3493 : hyperellisoncurve(GEN W, GEN P)
     812             : {
     813        3493 :   pari_sp av = avma;
     814             :   long res;
     815             :   GEN x, y;
     816        3493 :   if (typ(P)!=t_VEC || lg(P)!=3) pari_err_TYPE("hyperellisoncurve",P);
     817        3493 :   x = gel(P,1); y = gel(P,2);
     818        3493 :   if (typ(W)==t_POL)
     819           0 :     res = gequal(gsqr(y), poleval(W,x));
     820             :   else
     821             :   {
     822        3493 :     if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
     823        3493 :     res = gequal(gmul(y, gadd(y,poleval(gel(W,2), x))), poleval(gel(W,1), x));
     824             :   }
     825        3493 :   return gc_int(av, res);
     826             : }
     827             : 
     828             : GEN
     829          35 : hyperellordinate(GEN W, GEN x)
     830             : {
     831          35 :   pari_sp av = avma;
     832          35 :   if (typ(W)==t_POL)
     833             :   {
     834          14 :     GEN d = poleval(W,x), y;
     835          14 :     if (gequal0(d)) { return gerepilecopy(av, mkvec(d)); }
     836          14 :     if (!issquareall(d, &y)) { set_avma(av); return cgetg(1,t_VEC); }
     837           7 :     return gerepilecopy(av, mkvec2(y, gneg(y)));
     838             :   }
     839             :   else
     840             :   {
     841             :     GEN b, c, d, rd, y;
     842          21 :     if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
     843          21 :     b = poleval(gel(W,2), x); c = poleval(gel(W,1), x);
     844          21 :     d = gadd(gsqr(b), gmul2n(c, 2));
     845          21 :     if (gequal0(d)) { return gerepilecopy(av, mkvec(gmul2n(gneg(b),-1))); }
     846          14 :     if (!issquareall(d, &rd)) { set_avma(av); return cgetg(1,t_VEC); }
     847           7 :     y = gmul2n(gsub(rd, b), -1);
     848           7 :     return gerepilecopy(av, mkvec2(y, gsub(y,rd)));
     849             :   }
     850             : }
     851             : 
     852             : GEN
     853      118950 : hyperelldisc(GEN PQ)
     854             : {
     855      118950 :   pari_sp av = avma;
     856      118950 :   GEN D, H = check_hyperell(PQ);
     857             :   long d, g;
     858      118950 :   if (!H || signe(H)==0) pari_err_TYPE("hyperelldisc",PQ);
     859      118950 :   d = degpol(H); g = ((d+1)>>1)-1;
     860      118950 :   D = gmul2n(RgX_disc(H),-4*(g+1));
     861      118950 :   if (odd(d)) D = gmul(D, gsqr(leading_coeff(H)));
     862      118950 :   return gerepileupto(av, D);
     863             : }
     864             : 
     865             : static long
     866      126502 : get_ep(GEN W)
     867             : {
     868      126502 :   GEN P = gel(W,1), Q = gel(W,2);
     869      126502 :   if (signe(Q)==0) return ZX_lval(P,2);
     870       86307 :   return minss(ZX_lval(P,2), ZX_lval(Q,2));
     871             : }
     872             : 
     873             : static GEN
     874       50798 : algo51(GEN W, GEN M)
     875             : {
     876       50798 :   GEN P = gel(W,1), Q = gel(W,2);
     877             :   for(;;)
     878       10640 :   {
     879       61438 :     long vP = ZX_lval(P,2);
     880       61438 :     long vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
     881             :     long r;
     882             :     /* 1 */
     883       61438 :     if (vQ==0) break;
     884             :     /* 2 */
     885       36120 :     if (vP==0)
     886             :     {
     887             :       GEN H, H1;
     888             :       /* a */
     889       29602 :       RgX_even_odd(FpX_red(P,gen_2),&H, &H1);
     890       29602 :       if (signe(H1)) break;
     891             :       /* b */
     892       14951 :       P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
     893       14951 :       Q = ZX_sub(Q, ZX_mulu(H, 2));
     894       14951 :       vP = ZX_lval(P,2);
     895       14951 :       vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
     896             :     }
     897             :     /* 2c */
     898       21469 :     if (vP==1) break;
     899             :     /* 2d */
     900       10640 :     r = minss(2*vQ, vP)>>1;
     901       10640 :     if (M) gel(M,1) = shifti(gel(M,1), r);
     902       10640 :     P = ZX_shifti(P, -2*r);
     903       10640 :     Q = ZX_shifti(Q, -r);
     904             :   }
     905       50798 :   return mkvec2(P,Q);
     906             : }
     907             : 
     908             : static GEN
     909      103343 : algo52(GEN W, GEN c, long *pt_lambda)
     910             : {
     911             :   long lambda;
     912      103343 :   GEN P = gel(W,1), Q = gel(W,2);
     913             :   for(;;)
     914      116959 :   {
     915             :     GEN H, H1;
     916             :     /* 1 */
     917      220302 :     GEN Pc = ZX_affine(P,gen_2,c), Qc = ZX_affine(Q,gen_2,c);
     918      220302 :     long mP = ZX_lval(Pc,2), mQ = signe(Qc) ? ZX_lval(Qc,2): mP+1;
     919             :     /* 2 */
     920      220302 :     if (2*mQ <= mP) { lambda = 2*mQ; break; }
     921             :     /* 3 */
     922      188031 :     if (odd(mP)) { lambda = mP; break; }
     923             :     /* 4 */
     924      127718 :     RgX_even_odd(FpX_red(ZX_shifti(Pc, -mP),gen_2),&H, &H1);
     925      127718 :     if (signe(H1)) { lambda = mP; break; }
     926             :     /* 5 */
     927      116959 :      P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
     928      116959 :      Q = ZX_sub(Q, ZX_mulu(H, 2));
     929             :   }
     930      103343 :   *pt_lambda = lambda;
     931      103343 :   return mkvec2(P,Q);
     932             : }
     933             : 
     934             : static long
     935      147379 : test53(long lambda, long ep, long g)
     936             : {
     937      147379 :   return (lambda <= g+1) || (odd(g) && lambda<g+3 && ep==1);
     938             : }
     939             : 
     940             : static long
     941      189382 : test55(GEN W, long ep, long g)
     942             : {
     943      189382 :   GEN P = gel(W,1), Q = gel(W,2);
     944      189382 :   GEN Pe = FpX_red(ep ? ZX_shifti(P,-1): P, gen_2);
     945      189382 :   GEN Qe = FpX_red(ep ? ZX_shifti(Q,-1): Q, gen_2);
     946      189382 :   if (ep==0)
     947             :   {
     948      149101 :     if (signe(Qe)!=0) return ZX_val(Qe) >= (g + 3)>>1;
     949       90723 :     else return ZX_val(FpX_deriv(Pe, gen_2)) >= g+1;
     950             :   }
     951             :   else
     952       40281 :     return ZX_val(Qe) >= (g+1)>>1 && ZX_val(Pe) >= g + 1;
     953             : }
     954             : 
     955             : static GEN
     956       50798 : hyperell_reverse(GEN W, long g)
     957             : {
     958       50798 :   return mkvec2(RgXn_recip_shallow(gel(W,1),2*g+3),
     959       50798 :                 RgXn_recip_shallow(gel(W,2),g+2));
     960             : }
     961             : 
     962             : static GEN
     963       50784 : algo56(GEN W, long g)
     964             : {
     965             :   long ep;
     966       50784 :   GEN M = mkvec2(gen_1, matid(2)), Woo;
     967       50784 :   W = algo51(W, M);
     968       50784 :   Woo = hyperell_reverse(W, g);
     969       50784 :   ep = get_ep(Woo);
     970       50784 :   if (test55(Woo,ep,g))
     971             :   {
     972             :     long lambda;
     973       11737 :     Woo = algo52(Woo, gen_0, &lambda);
     974       11737 :     if (!test53(lambda,ep,g))
     975             :     {
     976        5969 :       long r = lambda>>1;
     977        5969 :       gel(M,1) = shifti(gel(M,1), r);
     978        5969 :       gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0, gen_1, gen_2, gen_0));
     979        5969 :       W = mkvec2(ZX_shifti(ZX_unscale(gel(Woo,1), gen_2), -2*r),
     980        5969 :                  ZX_shifti(ZX_unscale(gel(Woo,2), gen_2), -r));
     981             :     }
     982             :   }
     983             :   for(;;)
     984       24892 :   {
     985       75676 :     long j, ep = get_ep(W);
     986      189340 :     for (j = 0; j < 2; j++)
     987             :     {
     988             :       long lambda;
     989      138556 :       GEN c = utoi(j);
     990      138556 :       GEN Pc = ZX_affine(gel(W,1), gen_2, c);
     991      138556 :       GEN Qc = ZX_affine(gel(W,2), gen_2, c);
     992      138556 :       if (test55(mkvec2(Pc, Qc), ep, g))
     993             :       {
     994       91564 :         GEN Wc = algo52(W, c, &lambda);
     995       91564 :         if (!test53(lambda,ep,g))
     996             :         {
     997       24892 :           long r = lambda>>1;
     998       24892 :           gel(M,1) = shifti(gel(M,1), r);
     999       24892 :           gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_2, c, gen_0, gen_1));
    1000       24892 :           W = mkvec2(ZX_shifti(ZX_affine(gel(Wc,1), gen_2,c), -2*r),
    1001       24892 :                      ZX_shifti(ZX_affine(gel(Wc,2), gen_2,c), -r));
    1002       24892 :           break;
    1003             :         }
    1004             :       }
    1005             :     }
    1006       75676 :     if (j==2) break;
    1007             :   }
    1008       50784 :   return mkvec2(W, M);
    1009             : }
    1010             : 
    1011             : static GEN
    1012          14 : algo56bis(GEN W, long g, long inf, long thr)
    1013             : {
    1014          14 :   pari_sp av = avma;
    1015          14 :   GEN vl = cgetg(3,t_VEC);
    1016          14 :   long nl = 1;
    1017          14 :   W = algo51(W, NULL);
    1018          14 :   if (inf)
    1019             :   {
    1020          14 :     GEN Woo = hyperell_reverse(W, g);
    1021          14 :     GEN Pc = ZX_unscale(gel(W,1), gen_2);
    1022          14 :     GEN Qc = ZX_unscale(gel(W,2), gen_2);
    1023          14 :     long ep = get_ep(Woo);
    1024          14 :     if (test55(mkvec2(Pc, Qc), ep, g))
    1025             :     {
    1026             :       long lambda;
    1027          14 :       Woo = algo52(Woo, gen_0, &lambda);
    1028          14 :       if (lambda == thr)
    1029             :       {
    1030           0 :         long r = lambda>>1;
    1031           0 :         gel(vl,nl++) = mkvec2(ZX_shifti(ZX_unscale(gel(Woo,1), gen_2), -2*r),
    1032           0 :                               ZX_shifti(ZX_unscale(gel(Woo,2), gen_2), -r));
    1033             :       }
    1034             :     }
    1035             :   }
    1036             :   {
    1037          14 :     long j, ep = get_ep(W);
    1038          42 :     for (j = 0; j < 2; j++)
    1039             :     {
    1040             :       long lambda;
    1041          28 :       GEN c = utoi(j);
    1042          28 :       GEN Pc = ZX_affine(gel(W,1), gen_2, c);
    1043          28 :       GEN Qc = ZX_affine(gel(W,2), gen_2, c);
    1044          28 :       if (test55(mkvec2(Pc, Qc), ep, g))
    1045             :       {
    1046          28 :         GEN Wc = algo52(W, c, &lambda);
    1047          28 :         if (lambda == thr)
    1048             :         {
    1049           0 :           long r = lambda>>1;
    1050           0 :           gel(vl,nl++) = mkvec2(ZX_shifti(ZX_affine(gel(Wc,1), gen_2,c), -2*r),
    1051           0 :                                 ZX_shifti(ZX_affine(gel(Wc,2), gen_2,c), -r));
    1052             :         }
    1053             :       }
    1054             :     }
    1055             :   }
    1056          14 :   setlg(vl, nl);
    1057          14 :   return gerepilecopy(av,vl);
    1058             : }
    1059             : 
    1060             : /* return the (degree 2) apolar invariant (the nth transvectant of P and P) */
    1061             : static GEN
    1062          84 : ZX_apolar(GEN P, long n)
    1063             : {
    1064          84 :   pari_sp av = avma;
    1065          84 :   long d = degpol(P), i;
    1066          84 :   GEN s = gen_0, g = cgetg(n+2,t_VEC);
    1067          84 :   gel(g,1) = gen_1;
    1068         588 :   for (i = 1; i <= n; i++) gel(g,i+1) = muliu(gel(g,i),i); /* g[i+1] = i! */
    1069         658 :   for (i = n-d; i <= d; i++)
    1070             :   {
    1071         574 :      GEN a = mulii(mulii(gel(g,i+1),gel(g,n-i+1)),
    1072         574 :                    mulii(gel(P,i+2),gel(P,n-i+2)));
    1073         574 :      s = odd(i)? subii(s, a): addii(s, a);
    1074             :   }
    1075          84 :   return gerepileuptoint(av,s);
    1076             : }
    1077             : 
    1078             : static GEN
    1079       53017 : algo57(GEN F, long g, GEN pr)
    1080             : {
    1081             :   long i, l;
    1082       53017 :   GEN D, C = content(F);
    1083       53017 :   GEN e = gel(core2(shifti(C,-vali(C))),2);
    1084       53017 :   GEN M = mkvec2(e, matid(2));
    1085       53017 :   long minvd = (2*g+1)>>(odd(g) ? 4:2);
    1086       53017 :   F = ZX_Z_divexact(F, sqri(e));
    1087       53017 :   D = absi(hyperelldisc(F));
    1088       53017 :   if (!pr)
    1089             :   {
    1090          84 :     GEN A = gcdii(D, ZX_apolar(F, 2*g+2));
    1091          84 :     pr = gel(factor(shifti(A, -vali(A))),1);
    1092             :   }
    1093       53017 :   l = lg(pr);
    1094      312772 :   for (i = 1; i < l; i++)
    1095             :   {
    1096             :     long ep;
    1097      259755 :     GEN p = gel(pr, i), ps2 = shifti(p,-1), Fe;
    1098      259755 :     if (equaliu(p,2) || Z_pval(D,p) < minvd) continue;
    1099      197533 :     ep = ZX_pvalrem(F,p, &Fe); Fe = FpX_red(Fe, p);
    1100      197533 :     if (degpol(Fe) < g+1+ep)
    1101             :     {
    1102        6406 :       GEN Fi = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
    1103        6406 :       long lambda = ZX_pval(Fi,p);
    1104        6406 :       if (!test53(lambda,ep,g))
    1105             :       {
    1106        3815 :         GEN ppr = powiu(p,lambda>>1);
    1107        3815 :         F = ZX_Z_divexact(Fi,sqri(ppr));
    1108        3815 :         gel(M,1) = mulii(gel(M,1), ppr);
    1109        3815 :         gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0,gen_1,p,gen_0));
    1110             :       }
    1111             :     }
    1112             :     for(;;)
    1113       25186 :     {
    1114             :       GEN Fe, R;
    1115      222719 :       long j, lR, ep = ZX_pvalrem(F,p, &Fe);
    1116      222719 :       R = FpX_roots_mult(FpX_red(Fe, p), g+2-ep, p); lR = lg(R);
    1117      235205 :       for (j = 1; j<lR; j++)
    1118             :       {
    1119       37672 :         GEN c = Fp_center(gel(R,j), p, ps2);
    1120       37672 :         GEN Fi = ZX_affine(F,p,c);
    1121       37672 :         long lambda = ZX_pval(Fi,p);
    1122       37672 :         if (!test53(lambda,ep,g))
    1123             :         {
    1124       25186 :           GEN ppr = powiu(p,lambda>>1);
    1125       25186 :           F = ZX_Z_divexact(Fi, sqri(ppr));
    1126       25186 :           gel(M,1) = mulii(gel(M,1), ppr);
    1127       25186 :           gel(M,2) = ZM2_mul(gel(M,2), mkmat22(p,c,gen_0,gen_1));
    1128       25186 :           break;
    1129             :         }
    1130             :       }
    1131      222719 :       if (j==lR) break;
    1132             :     }
    1133             :   }
    1134       53017 :   return mkvec2(F, M);
    1135             : }
    1136             : 
    1137             : /* if inf=0, ignore point at infinity */
    1138             : static GEN
    1139        9051 : algo57bis(GEN F, long g, GEN p, long inf, long thr)
    1140             : {
    1141        9051 :   pari_sp av = avma;
    1142        9051 :   GEN vl = cgetg(3,t_VEC), Fe;
    1143        9051 :   long nl = 1, ep = ZX_pvalrem(F,p, &Fe);
    1144        9051 :   Fe = FpX_red(Fe, p);
    1145             :   {
    1146        9051 :     GEN R = FpX_roots_mult(Fe, thr-ep, p);
    1147        9051 :     long j, lR = lg(R);
    1148       17822 :     for (j = 1; j<lR; j++)
    1149             :     {
    1150        8771 :       GEN Fj = ZX_affine(F, p, gel(R,j));
    1151        8771 :       long lambda = ZX_pvalrem(Fj, p, &Fj);
    1152        8771 :       if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
    1153             :     }
    1154             :   }
    1155        9051 :   if (inf==1 && 2*g+2-degpol(Fe) >= thr-ep)
    1156             :   {
    1157         105 :     GEN Fj = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
    1158         105 :     long lambda = ZX_pvalrem(Fj, p, &Fj);
    1159         105 :     if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
    1160             :   }
    1161        9051 :   setlg(vl, nl);
    1162        9051 :   return gerepilecopy(av,vl);
    1163             : }
    1164             : 
    1165             : static GEN
    1166        9065 : next_model(GEN G, long g, GEN p, long inf, long thr)
    1167             : {
    1168        9079 :   return equaliu(p,2) ? algo56bis(G, g,    inf, thr)
    1169        9079 :                       : algo57bis(G, g, p, inf, thr);
    1170             : }
    1171             : 
    1172             : static GEN
    1173        4438 : get_extremal(GEN F, GEN G, long g, GEN p)
    1174             : {
    1175             :   while (1)
    1176        3654 :   {
    1177             :     GEN Wi;
    1178        4438 :     Wi = next_model(G, g, p, 0, g+2);
    1179        4438 :     if (lg(Wi)==1) return F;
    1180        3850 :     F = gel(Wi,1);
    1181        3850 :     Wi = next_model(F, g, p, 0, g+1);
    1182        3850 :     if (lg(Wi)==1) return F;
    1183        3654 :     G = gel(Wi,1);
    1184             :   }
    1185             : }
    1186             : 
    1187             : GEN
    1188         945 : hyperellextremalmodels(GEN F, long g, GEN p)
    1189             : {
    1190         945 :   pari_sp av = avma;
    1191             :   GEN R, W;
    1192             :   long l;
    1193         945 :   if (equaliu(p,2))
    1194             :   {
    1195          14 :     if (get_ep(F)>0) return mkvec(F);
    1196             :   } else
    1197         931 :     if( ZX_pval(F,p) > 0) return mkvec(F);
    1198         777 :   W = next_model(F, g, p, 1, g+1); l = lg(W);
    1199         777 :   if (l==1) { set_avma(av); return mkvec(F); }
    1200         588 :   R = cgetg(3, t_VEC);
    1201         588 :   gel(R,1) = get_extremal(F, gel(W,1), g, p);
    1202         588 :   gel(R,2) = l==3 ? get_extremal(F, gel(W,2), g, p) : F;
    1203         588 :   if (gel(R,2) == gel(R,1)) setlg(R,2);
    1204         588 :   return gerepilecopy(av, R);
    1205             : }
    1206             : 
    1207             : static GEN
    1208      303719 : RgX_RgM2_eval(GEN P, GEN A, GEN Bp, long d)
    1209             : {
    1210      303719 :   if (signe(P)==0)
    1211       79834 :     return P;
    1212             :   else
    1213             :   {
    1214      223885 :     long dP = degpol(P);
    1215      223885 :     GEN R = RgX_homogenous_evalpow(P, A, Bp);
    1216      223885 :     if (d > dP)
    1217       17277 :       R = gmul(R, gel(Bp,1+d-dP));
    1218      223885 :     return R;
    1219             :   }
    1220             : }
    1221             : 
    1222             : static GEN
    1223       53031 : minimalmodel_merge(GEN W2, GEN Modd, long g, long v)
    1224             : {
    1225       53031 :   GEN P = gel(W2,1), Q = gel(W2,2);
    1226       53031 :   GEN e = gel(Modd,1), M = gel(Modd,2);
    1227       53031 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1228       53031 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1229       53031 :   GEN Bp = gpowers(B, 2*g+2);
    1230       53031 :   long f = mod4(e)==1 ? 1: -1;
    1231       53031 :   GEN m = shifti(f > 0 ? subui(1,e): addui(1,e), -2);
    1232       53031 :   GEN  m24 = subii(shifti(m,1), shifti(sqri(m),2));
    1233       53031 :   P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
    1234       53031 :   Q = RgX_RgM2_eval(Q, A, Bp, g+1);
    1235       53031 :   P = ZX_Z_divexact(ZX_add(P, ZX_Z_mul(ZX_sqr(Q), m24)),sqri(e));
    1236       53031 :   if (f < 0) Q = ZX_neg(Q);
    1237       53031 :   return mkvec2(P,Q);
    1238             : }
    1239             : 
    1240             : static GEN
    1241      106048 : hyperell_redQ(GEN W)
    1242             : {
    1243      106048 :   GEN P = gel(W,1), Q = gel(W,2);
    1244      106048 :   GEN Pr, Qr = FpX_red(Q, gen_2);
    1245      106048 :   Pr = ZX_add(P, ZX_shifti(ZX_mul(ZX_sub(Q, Qr),ZX_add(Q, Qr)),-2));
    1246      106048 :   return mkvec2(Pr, Qr);
    1247             : }
    1248             : 
    1249             : static GEN
    1250       50735 : minimalmodel_getH(GEN W, GEN Qn, GEN e, GEN M, long g, long v)
    1251             : {
    1252       50735 :   GEN Q = gel(W,2);
    1253       50735 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1254       50735 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1255       50735 :   GEN Bp = gpowers(B, g+1);
    1256       50735 :   return ZX_shifti(ZX_sub(ZX_Z_mul(Qn,e),RgX_RgM2_eval(Q, A, Bp, g+1)), -1);
    1257             : }
    1258             : 
    1259             : static void
    1260       53073 : check_hyperell_Q(const char *fun, GEN *pW, GEN *pF)
    1261             : {
    1262       53073 :   GEN W = *pW, F = check_hyperell(W);
    1263             :   long v, d;
    1264       53073 :   if (!F || !signe(F) || !RgX_is_ZX(F)) pari_err_TYPE(fun, W);
    1265       53066 :   if (!signe(ZX_disc(F))) pari_err_DOMAIN(fun,"disc(W)","==",gen_0,W);
    1266       53059 :   v = varn(F); d = degpol(F);
    1267       53059 :   if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
    1268             :   else
    1269             :   {
    1270       43603 :     GEN P = gel(W, 1), Q = gel(W, 2);
    1271       43603 :     long g = ((d+1) >> 1) - 1;
    1272       43603 :     if (typ(P)!=t_POL) P = scalarpol_shallow(P, v);
    1273       43603 :     if (typ(Q)!=t_POL) Q = scalarpol_shallow(Q, v);
    1274       43603 :     if (!RgX_is_ZX(P) || !RgX_is_ZX(Q)) pari_err_TYPE(fun,W);
    1275       43603 :     if (degpol(P) > 2*g+2) pari_err_DOMAIN(fun, "deg(P)", ">", utoi(2*g+2), P);
    1276       43603 :     if (degpol(Q) > g+1) pari_err_DOMAIN(fun, "deg(Q)", ">", utoi(g+1), Q);
    1277       43603 :     W = mkvec2(P, Q);
    1278             :   }
    1279       53059 :   if (d < 3) pari_err_DOMAIN(fun, "genus", "=", gen_0, gen_0);
    1280       53045 :   *pW = W; *pF = F;
    1281       53045 : }
    1282             : 
    1283             : GEN
    1284       53031 : hyperellminimalmodel(GEN W, GEN *pM, GEN pr)
    1285             : {
    1286       53031 :   pari_sp av = avma;
    1287             :   GEN Wr, F, WM2, F2, W2, M2, Modd, Wf, ef, Mf, Hf;
    1288             :   long d, g, v;
    1289       53031 :   check_hyperell_Q("hyperellminimalmodel",&W, &F);
    1290       53031 :   if (pr && (!is_vec_t(typ(pr)) || !RgV_is_ZV(pr)))
    1291          14 :     pari_err_TYPE("hyperellminimalmodel",pr);
    1292       53017 :   d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
    1293       53017 :   Wr = hyperell_redQ(W);
    1294       53017 :   if (!pr || RgV_isin(pr, gen_2))
    1295             :   {
    1296       50784 :     WM2 = algo56(Wr,g); W2 = gel(WM2, 1); M2 = gel(WM2, 2);
    1297       50784 :     F2 = check_hyperell(W2);
    1298             :   }
    1299             :   else
    1300             :   {
    1301        2233 :     W2 = Wr; F2 = F; M2 = mkvec2(gen_1, matid(2));
    1302             :   }
    1303       53017 :   Modd = gel(algo57(F2, g, pr), 2);
    1304       53017 :   Wf = hyperell_redQ(minimalmodel_merge(W2, Modd, g, v));
    1305       53017 :   if (!pM) return gerepilecopy(av, Wf);
    1306       50721 :   ef = mulii(gel(M2,1), gel(Modd,1));
    1307       50721 :   Mf = ZM2_mul(gel(M2,2), gel(Modd,2));
    1308       50721 :   Hf = minimalmodel_getH(W, gel(Wf,2), ef, Mf, g, v);
    1309       50721 :   *pM =  mkvec3(ef, Mf, Hf);
    1310       50721 :   return gc_all(av, 2, &Wf, pM);
    1311             : }
    1312             : 
    1313             : GEN
    1314          14 : hyperellminimaldisc(GEN W, GEN pr)
    1315             : {
    1316          14 :   pari_sp av = avma;
    1317          14 :   GEN C = hyperellminimalmodel(W, NULL, pr);
    1318          14 :   return gerepileuptoint(av, hyperelldisc(C));
    1319             : }
    1320             : 
    1321             : static GEN
    1322          35 : redqfbsplit(GEN a, GEN b, GEN c, GEN d)
    1323             : {
    1324          35 :   GEN p = subii(d,b), q = shifti(a,1);
    1325          35 :   GEN U, Q, u, v, w = bezout(p, q, &u, &v);
    1326             : 
    1327          35 :   if (!equali1(w)) { p = diviiexact(p, w); q = diviiexact(q, w); }
    1328          35 :   U = mkmat22(p, negi(v), q, u);
    1329          35 :   Q = qfb_ZM_apply(mkvec3(a,b,c), U);
    1330          35 :   b = gel(Q, 2); c = gel(Q,3);
    1331          35 :   if (signe(b) < 0) gel(U,2) = mkcol2(v, negi(u));
    1332          35 :   gel(U,2) = ZC_lincomb(gen_1, truedivii(negi(c), d), gel(U,2), gel(U,1));
    1333          35 :   return U;
    1334             : }
    1335             : 
    1336             : static GEN
    1337       16386 : polreduce(GEN P, GEN M)
    1338             : {
    1339       16386 :   long v = varn(P), dP = degpol(P), d = odd(dP) ? dP+1: dP;
    1340       16386 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1341       16386 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1342       16386 :   return RgX_RgM2_eval(P, A, gpowers(B, d), d);
    1343             : }
    1344             : 
    1345             : /* assume deg(P) > 2 */
    1346             : static GEN
    1347        8193 : red_Cremona_Stoll(GEN P, GEN *pM)
    1348             : {
    1349             :   GEN q1, q2, q3, M, R;
    1350        8193 :   long i, prec = nbits2prec(2*gexpo(P)) + EXTRAPRECWORD, d = degpol(P);
    1351        8193 :   GEN dP = ZX_deriv(P);
    1352             :   for (;;)
    1353           0 :   {
    1354        8193 :     GEN r = QX_complex_roots(P, prec);
    1355        8193 :     q1 = gen_0; q2 = gen_0; q3 = gen_0;
    1356       41000 :     for (i = 1; i <= d; i++)
    1357             :     {
    1358       32807 :       GEN ri = gel(r,i);
    1359       32807 :       GEN s = ginv(gabs(RgX_cxeval(dP,ri,NULL), prec));
    1360       32807 :       if (d!=4) s = gpow(s, gdivgs(gen_2,d-2), prec);
    1361       32807 :       q1 = gadd(q1, s);
    1362       32807 :       q2 = gsub(q2, gmul(real_i(ri), s));
    1363       32807 :       q3 = gadd(q3, gmul(gnorm(ri), s));
    1364             :     }
    1365        8193 :     M = lllgram(mkmat22(q1,q2,q2,q3));
    1366        8193 :     if (M && lg(M) == 3) break;
    1367           0 :     prec = precdbl(prec);
    1368             :   }
    1369        8193 :   R = polreduce(P, M);
    1370        8193 :   *pM = M;
    1371        8193 :   return R;
    1372             : }
    1373             : 
    1374             : /* assume deg(P) > 2 */
    1375             : GEN
    1376        8193 : ZX_hyperellred(GEN P, GEN *pM)
    1377             : {
    1378        8193 :   pari_sp av = avma;
    1379        8193 :   long d = degpol(P);
    1380             :   GEN q1, q2, q3, D, vD;
    1381        8193 :   GEN a = gel(P,d+2), b = gel(P,d+1), c = gel(P, d);
    1382             :   GEN M, R, M2;
    1383             : 
    1384        8193 :   q1 = muliu(sqri(a), d);
    1385        8193 :   q2 = shifti(mulii(a,b), 1);
    1386        8193 :   q3 = subii(sqri(b), shifti(mulii(a,c), 1));
    1387        8193 :   D = gcdii(gcdii(q1, q2), q3);
    1388        8193 :   if (!equali1(D))
    1389             :   {
    1390        8172 :     q1 = diviiexact(q1, D);
    1391        8172 :     q2 = diviiexact(q2, D);
    1392        8172 :     q3 = diviiexact(q3, D);
    1393             :   }
    1394        8193 :   D = qfb_disc3(q1, q2, q3);
    1395        8193 :   if (!signe(D))
    1396          49 :     M = mkmat22(gen_1, truedivii(negi(q2),shifti(q1,1)), gen_0, gen_1);
    1397        8144 :   else if (issquareall(D,&vD))
    1398          35 :     M = redqfbsplit(q1, q2, q3, vD);
    1399             :   else
    1400        8109 :     M = gel(qfbredsl2(mkqfb(q1,q2,q3,D), NULL), 2);
    1401        8193 :   R = red_Cremona_Stoll(polreduce(P, M), &M2);
    1402        8193 :   if (pM) *pM = gmul(M, M2);
    1403        8193 :   return gc_all(av, pM ? 2: 1, &R, pM);
    1404             : }
    1405             : 
    1406             : GEN
    1407          42 : hyperellred(GEN W, GEN *pM)
    1408             : {
    1409          42 :   pari_sp av = avma;
    1410             :   long g, d, v;
    1411             :   GEN F, M, Wf, Hf;
    1412          42 :   check_hyperell_Q("hyperellred", &W, &F);
    1413          14 :   d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
    1414          14 :   (void) ZX_hyperellred(F, &M);
    1415          14 :   Wf = hyperell_redQ(minimalmodel_merge(W, mkvec2(gen_1, M), g, v));
    1416          14 :   Hf = minimalmodel_getH(W, gel(Wf,2), gen_1, M, g, v);
    1417          14 :   if (pM) *pM = mkvec3(gen_1, M, Hf);
    1418          14 :   return gc_all(av, pM ? 2: 1, &Wf, pM);
    1419             : }
    1420             : 
    1421             : static void
    1422       65296 : check_hyperell_Rg(const char *fun, GEN *pW, GEN *pF)
    1423             : {
    1424       65296 :   GEN W = *pW, F = check_hyperell(W);
    1425             :   long v;
    1426       65296 :   if (!F)
    1427           7 :     pari_err_TYPE(fun, W);
    1428       65289 :   if (degpol(F) <= 0) pari_err_CONSTPOL(fun);
    1429       65282 :   v = varn(F);
    1430       65282 :   if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
    1431             :   else
    1432             :   {
    1433       65254 :     GEN P = gel(W, 1), Q = gel(W, 2);
    1434       65254 :     long g = ((degpol(F)+1)>>1)-1;
    1435       65254 :     if( typ(P)!=t_POL) P = scalarpol(P, v);
    1436       65254 :     if( typ(Q)!=t_POL) Q = scalarpol(Q, v);
    1437       65254 :     if (degpol(P) > 2*g+2)
    1438           0 :       pari_err_DOMAIN(fun, "poldegree(P)", ">", utoi(2*g+2), P);
    1439       65254 :     if (degpol(Q) > g+1)
    1440           0 :       pari_err_DOMAIN(fun, "poldegree(Q)", ">", utoi(g+1), Q);
    1441             : 
    1442       65254 :     W = mkvec2(P, Q);
    1443             :   }
    1444       65282 :   if (pF) *pF = F;
    1445       65282 :   *pW = W;
    1446       65282 : }
    1447             : 
    1448             : static void
    1449       65282 : check_hyperell_vc(const char *fun, GEN C, long v, GEN *e, GEN *M, GEN *H)
    1450             : {
    1451       65282 :   if (typ(C) != t_VEC || lg(C) != 4) pari_err_TYPE(fun,C);
    1452       65275 :   *e = gel(C,1); *M = gel(C,2); *H = gel(C,3);
    1453       65275 :   if (typ(*M) != t_MAT || lg(*M) != 3 || lgcols(*M) != 3) pari_err_TYPE(fun,C);
    1454       65268 :   if (typ(*H)!=t_POL || varncmp(varn(*H),v) > 0) *H = scalarpol_shallow(*H,v);
    1455       65268 : }
    1456             : 
    1457             : GEN
    1458       65296 : hyperellchangecurve(GEN W, GEN C)
    1459             : {
    1460       65296 :   pari_sp av = avma;
    1461             :   GEN F, P, Q, A, B, Bp, e, M, H;
    1462             :   long d, g, v;
    1463       65296 :   check_hyperell_Rg("hyperellchangecurve",&W,&F);
    1464       65282 :   P = gel(W,1); Q = gel(W,2);
    1465       65282 :   d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
    1466       65282 :   check_hyperell_vc("hyperellchangecurve", C, v, &e, &M, &H);
    1467       65268 :   if (varncmp(gvar(M),v) <= 0)
    1468           0 :     pari_err_PRIORITY("hyperellchangecurve",M,"<=",v);
    1469       65268 :   A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1470       65268 :   B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1471       65268 :   Bp = gpowers(B, 2*g+2);
    1472       65268 :   P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
    1473       65268 :   Q = RgX_RgM2_eval(Q, A, Bp, g+1);
    1474       65268 :   P = RgX_Rg_div(RgX_sub(P, RgX_mul(H,RgX_add(Q,H))), gsqr(e));
    1475       65268 :   Q = RgX_Rg_div(RgX_add(Q, RgX_mul2n(H,1)), e);
    1476       65268 :   return gerepilecopy(av, mkvec2(P,Q));
    1477             : }
    1478             : 
    1479             : /****************************************************************************/
    1480             : /***                                                                      ***/
    1481             : /***                        genus2charpoly                                ***/
    1482             : /***                                                                      ***/
    1483             : /****************************************************************************/
    1484             : 
    1485             : /* Half stable reduction */
    1486             : 
    1487             : static long
    1488         588 : Zst_val(GEN P, GEN f, GEN p, long vt, GEN *pR)
    1489             : {
    1490         588 :   pari_sp av = avma;
    1491         588 :   long v = varn(P);
    1492             :   while(1)
    1493        1260 :   {
    1494        1848 :     long i, j, dm = LONG_MAX;
    1495        1848 :     GEN Pm = NULL;
    1496        1848 :     long dP = degpol(P);
    1497        7532 :     for (i = 0; i <= minss(dP, dm); i++)
    1498             :     {
    1499        5684 :       GEN Py = gel(P, i+2);
    1500        5684 :       if (signe(Py))
    1501             :       {
    1502        4186 :         if (typ(Py)==t_POL)
    1503             :         {
    1504        3864 :           long dPy = degpol(Py);
    1505       12502 :           for (j = 0; j <= minss(dPy, dm-i); j++)
    1506             :           {
    1507        8638 :             GEN c = gel(Py, j+2);
    1508        8638 :             if (signe(c))
    1509             :             {
    1510        3556 :                 if (i+j < dm)
    1511             :                 {
    1512        1848 :                   dm = i+j;
    1513        1848 :                   Pm = monomial(gen_1, dm, v);
    1514        1848 :                   gel(Pm,dm+2) = gen_0;
    1515             :                 }
    1516        3556 :                 gel(Pm,i+2) = c;
    1517             :             }
    1518             :           }
    1519             :         } else
    1520             :         {
    1521         322 :           if (i < dm)
    1522             :           {
    1523          77 :             dm = i;
    1524          77 :             Pm = monomial(Py, dm, v);
    1525             :           }
    1526             :           else
    1527         245 :             gel(Pm, i+2) = Py;
    1528             :         }
    1529             :       }
    1530             :     }
    1531        1848 :     Pm = RgX_renormalize(Pm);
    1532        1848 :     if (ZX_pval(Pm,p)==0)
    1533             :     {
    1534         588 :       *pR = gerepilecopy(av, P);
    1535         588 :       return dm;
    1536             :     }
    1537        1260 :     Pm = RgX_homogenize_deg(Pm, dm, vt);
    1538        1260 :     P = gadd(gsub(P, Pm), gmul(f, ZXX_Z_divexact(Pm, p)));
    1539             :   }
    1540             : }
    1541             : 
    1542             : static long
    1543         588 : Zst_normval(GEN P, GEN f, GEN p, long vt, GEN *pR)
    1544             : {
    1545         588 :   long v = Zst_val(P, f, p, vt, pR);
    1546         588 :   long e = RgX_val(*pR)>>1;
    1547         588 :   if (e > 0)
    1548             :   {
    1549           0 :     v -= 2*e;
    1550           0 :     *pR = RgX_shift(*pR, -2*e);
    1551             :   }
    1552         588 :   return v;
    1553             : }
    1554             : 
    1555             : static GEN
    1556        1176 : RgXY_swapsafe(GEN P, long v1, long v2)
    1557             : {
    1558        1176 :   if (varn(P)==v2)
    1559             :   {
    1560          77 :     P = shallowcopy(P); setvarn(P,v1); return P;
    1561             :   } else
    1562        1099 :     return RgXY_swap(P, RgXY_degreex(P), v2);
    1563             : }
    1564             : 
    1565             : static GEN
    1566         588 : Zst_red1(GEN P, GEN f, GEN p, long vt)
    1567             : {
    1568         588 :   pari_sp av = avma;
    1569             :   GEN r, f1, f2, P1, P2;
    1570         588 :   long vs = varn(P);
    1571         588 :   long w = Zst_normval(P, f, p, vt, &r), ww = w-odd(w);
    1572         588 :   GEN st = monomial(pol_x(vt), 1, vs);
    1573         588 :   f1 = gsubst(f, vt, st);
    1574         588 :   P1 = gsubst(gdiv(r, monomial(gen_1,ww,vs)),vt,st);
    1575         588 :   f2 = gsubst(f, vs, st);
    1576         588 :   P2 = gsubst(gdiv(r, monomial(gen_1,ww,vt)),vs,st);
    1577         588 :   f2 = RgXY_swapsafe(f2, vs, vt);
    1578         588 :   P2 = RgXY_swapsafe(P2, vs, vt);
    1579         588 :   return gerepilecopy(av, mkvec4(P1, f1, P2, f2));
    1580             : }
    1581             : 
    1582             : static GEN
    1583        1176 : Zst_reduce(GEN P, GEN p, long vt, long *pv)
    1584             : {
    1585             :   GEN C;
    1586        1176 :   long v = RgX_val(P);
    1587        1176 :   *pv = v + ZXX_pvalrem(RgX_shift(P, -v), p, &P);
    1588        1176 :   C = constant_coeff(P);
    1589        1176 :   C = typ(C) == t_POL ? C: scalarpol_shallow(C, vt);
    1590        1176 :   return FpX_red(C, p);
    1591             : }
    1592             : 
    1593             : static GEN
    1594         588 : Zst_red3(GEN C, GEN p, long vt)
    1595             : {
    1596             :   while(1)
    1597         511 :   {
    1598         588 :     GEN P1 = gel(C,1) ,f1 = gel(C,2), Poo = gel(C,3), foo= gel(C,4);
    1599             :     long e;
    1600         588 :     GEN Qoop = Zst_reduce(Poo, p, vt, &e), Qp, R;
    1601         588 :     if (RgX_val(Qoop) >= 3-e)
    1602             :     {
    1603           0 :       C = Zst_red1(Poo, foo, p, vt);
    1604         511 :       continue;
    1605             :     }
    1606         588 :     Qp = Zst_reduce(P1, p, vt, &e);
    1607         588 :     R = FpX_roots_mult(Qp, 3-e, p);
    1608         588 :     if (lg(R) > 1)
    1609         511 :     {
    1610         511 :       GEN xz = deg1pol_shallow(gen_1, gel(R,1), vt);
    1611         511 :       C = Zst_red1(gsubst(P1, vt, xz), gsubst(f1, vt, xz), p, vt);
    1612         511 :       continue;
    1613             :     }
    1614          77 :     return Qp;
    1615             :   }
    1616             : }
    1617             : 
    1618             : static GEN
    1619          77 : genus2_halfstablemodel_i(GEN P, GEN p, long vt)
    1620             : {
    1621             :   GEN Qp, R, Poo, Qoop;
    1622          77 :   long e = ZX_pvalrem(P, p, &Qp);
    1623          77 :   R = FpX_roots_mult(FpX_red(Qp,p), 4-e, p);
    1624          77 :   if (lg(R) > 1)
    1625             :   {
    1626          77 :     GEN C = Zst_red1(ZX_translate(P, gel(R,1)), pol_x(vt), p, vt);
    1627          77 :     return Zst_red3(C, p, vt);
    1628             :   }
    1629           0 :   Poo = RgXn_recip_shallow(P, 7);
    1630           0 :   e = ZX_pvalrem(Poo, p, &Qoop);
    1631           0 :   Qoop = FpX_red(Qoop,p);
    1632           0 :   if (RgX_val(Qoop)>=4-e)
    1633             :   {
    1634           0 :     GEN C = Zst_red1(Poo, pol_x(vt), p, vt);
    1635           0 :     return Zst_red3(C, p, vt);
    1636             :   }
    1637           0 :   return gcopy(P);
    1638             : }
    1639             : 
    1640             : static GEN
    1641          77 : genus2_halfstablemodel(GEN P, GEN p)
    1642             : {
    1643          77 :   pari_sp av = avma;
    1644          77 :   long vt = fetch_var(), vs = varn(P);
    1645          77 :   GEN S = genus2_halfstablemodel_i(P, p, vt);
    1646          77 :   setvarn(S, vs); delete_var();
    1647          77 :   return gerepilecopy(av, S);
    1648             : }
    1649             : 
    1650             : /* semi-stable reduction */
    1651             : 
    1652             : static GEN
    1653        1393 : genus2_redmodel(GEN P, GEN p)
    1654             : {
    1655        1393 :   GEN M = FpX_factor(P, p);
    1656        1393 :   GEN F = gel(M,1), E = gel(M,2);
    1657        1393 :   long i, k, r = lg(F);
    1658        1393 :   GEN U = scalarpol(leading_coeff(P), varn(P));
    1659        1393 :   GEN G = cgetg(r, t_COL);
    1660        3353 :   for (i=1, k=0; i<r; i++)
    1661             :   {
    1662        1960 :     if (E[i]>1)
    1663        1491 :       gel(G,++k) = gel(F,i);
    1664        1960 :     if (odd(E[i]))
    1665        1463 :       U = FpX_mul(U, gel(F,i), p);
    1666             :   }
    1667        1393 :   setlg(G,++k);
    1668        1393 :   return mkvec2(G,U);
    1669             : }
    1670             : 
    1671             : static GEN
    1672        7322 : xdminusone(long d)
    1673             : {
    1674        7322 :   return gsub(pol_xn(d, 0),gen_1);
    1675             : }
    1676             : 
    1677             : static GEN
    1678         126 : ellfromeqncharpoly(GEN P, GEN Q, GEN p)
    1679             : {
    1680             :   long v;
    1681             :   GEN E, F, t, y;
    1682         126 :   v = fetch_var();
    1683         126 :   y = pol_x(v);
    1684         126 :   F = gsub(gadd(ZX_sqr(y), gmul(y, Q)), P);
    1685         126 :   E = ellinit(ellfromeqn(F), p, DEFAULTPREC);
    1686         126 :   delete_var();
    1687         126 :   t = ellap(E, p);
    1688         126 :   obj_free(E);
    1689         126 :   return mkpoln(3, gen_1, negi(t), p);
    1690             : }
    1691             : 
    1692             : static GEN
    1693           0 : nfellcharpoly(GEN e, GEN T, GEN p)
    1694             : {
    1695             :   GEN nf, E, t;
    1696           0 :   e = shallowcopy(e);
    1697           0 :   nf = nfinit(mkvec2(T, mkvec(p)), DEFAULTPREC);
    1698             :   while(1)
    1699             :   {
    1700           0 :     E = ellinit(e, nf, DEFAULTPREC);
    1701           0 :     if (lg(E)!=1) break;
    1702           0 :     gel(e,5) = gadd(gel(e,5), p);
    1703             :   }
    1704           0 :   t = elleulerf(E, p);
    1705           0 :   obj_free(E);
    1706           0 :   return RgX_recip(ginv(t));
    1707             : }
    1708             : 
    1709             : static GEN
    1710           0 : genus2_red5(GEN P, GEN T, GEN p)
    1711             : {
    1712           0 :   long vx = varn(P), vy = varn(T);
    1713           0 :   GEN f = shallowcopy(T);
    1714           0 :   setvarn(f, vx);
    1715             :   while(1)
    1716           0 :   {
    1717             :     GEN Pr, R, r, Rs;
    1718           0 :     (void) ZXX_pvalrem(P, p, &Pr);
    1719           0 :     R = FpXQX_roots_mult(Pr, 3, T, p);
    1720           0 :     if (lg(R)==1) return P;
    1721           0 :     r = FpX_center(gel(R,1), p, shifti(p,-1));
    1722           0 :     r = gel(R,1);
    1723           0 :     P = RgX_affine(P, p, r);
    1724           0 :     setvarn(r, vx);
    1725           0 :     f = RgX_Rg_div(gsub(f, r), p);
    1726           0 :     Rs = RgX_rem(RgXY_swap(P, 3, vy), gsub(f, pol_x(vy)));
    1727           0 :     P = RgXY_swap(Rs, 3, vy);
    1728           0 :     (void) ZXX_pvalrem(P, sqri(p), &P);
    1729             :   }
    1730             : }
    1731             : 
    1732             : static GEN
    1733         945 : genus2_type5(GEN P, GEN p)
    1734             : {
    1735             :   GEN E, F, T, a, a2, Q;
    1736             :   long v;
    1737         945 :   (void) ZX_pvalrem(P, p, &F);
    1738         945 :   F = FpX_red(F, p);
    1739         945 :   if (degpol(F) < 1) return NULL;
    1740         917 :   F = FpX_factor(F, p);
    1741         917 :   if (mael(F,2,1) != 3 || degpol(gmael(F,1,1)) != 2) return NULL;
    1742           0 :   T = gmael(F, 1, 1);
    1743           0 :   v = fetch_var_higher();
    1744           0 :   Q = RgV_to_RgX(ZX_digits(P, T), v);
    1745           0 :   Q = genus2_red5(Q, T, p);
    1746           0 :   a = gel(Q,5); a2 = ZX_sqr(a);
    1747           0 :   E = mkvec5(gen_0, gel(Q,4), gen_0, ZX_mul(gel(Q,3),a), ZX_mul(gel(Q,2),a2));
    1748           0 :   delete_var();
    1749           0 :   return nfellcharpoly(E, T, p);
    1750             : }
    1751             : 
    1752             : /* Assume P has semistable reduction at p */
    1753             : static GEN
    1754        1393 : genus2_eulerfact_semistable(GEN P, GEN p)
    1755             : {
    1756        1393 :   GEN Pp = FpX_red(P, p);
    1757        1393 :   GEN GU = genus2_redmodel(Pp, p);
    1758        1393 :   long d = 6-degpol(Pp), v = d/2, w = odd(d);
    1759             :   GEN abe, tor;
    1760        1393 :   GEN ki, kp = pol_1(0), kq = pol_1(0);
    1761        1393 :   GEN F = gel(GU,1), Q = gel(GU,2);
    1762        1393 :   long dQ = degpol(Q), lF = lg(F)-1;
    1763             : 
    1764           7 :   abe = dQ >= 5 ? hyperellcharpoly(gmul(Q,gmodulo(gen_1,p)))
    1765        2779 :       : dQ >= 3 ? ellfromeqncharpoly(Q,gen_0,p)
    1766        1386 :                 : pol_1(0);
    1767        1134 :   ki = dQ != 0 ? xdminusone(1)
    1768        1652 :               : Fp_issquare(gel(Q,2),p) ? ZX_sqr(xdminusone(1))
    1769         259 :                                         : xdminusone(2);
    1770        1393 :   if (lF)
    1771             :   {
    1772             :     long i;
    1773        2639 :     for(i=1; i <= lF; i++)
    1774             :     {
    1775        1491 :       GEN Fi = gel(F, i);
    1776        1491 :       long d = degpol(Fi);
    1777        1491 :       GEN e = FpX_rem(Q, Fi, p);
    1778        1988 :       GEN kqf = lgpol(e)==0 ? xdminusone(d):
    1779         980 :                 FpXQ_issquare(e, Fi, p) ? ZX_sqr(xdminusone(d))
    1780         497 :                                         : xdminusone(2*d);
    1781        1491 :       kp = gmul(kp, xdminusone(d));
    1782        1491 :       kq = gmul(kq, kqf);
    1783             :     }
    1784             :   }
    1785        1393 :   if (v)
    1786             :   {
    1787         749 :     GEN kqoo = w==1 ? xdminusone(1):
    1788          28 :                Fp_issquare(leading_coeff(Q), p)? ZX_sqr(xdminusone(1))
    1789          14 :                                               : xdminusone(2);
    1790         735 :     kp = gmul(kp, xdminusone(1));
    1791         735 :     kq = gmul(kq, kqoo);
    1792             :   }
    1793        1393 :   tor = RgX_div(ZX_mul(xdminusone(1), kq), ZX_mul(ki, kp));
    1794        1393 :   return ZX_mul(abe, tor);
    1795             : }
    1796             : 
    1797             : GEN
    1798         931 : genus2_eulerfact(GEN P, GEN p, long ra, long rt)
    1799             : {
    1800         931 :   pari_sp av = avma;
    1801         931 :   GEN W, R = genus2_type5(P, p), E;
    1802         931 :   if (R) return R;
    1803         931 :   W = hyperellextremalmodels(P, 2, p);
    1804         931 :   if (lg(W) < 3)
    1805             :   {
    1806         546 :     GEN F = genus2_eulerfact_semistable(P,p);
    1807         546 :     if (degpol(F)!=2*ra+rt)
    1808             :     {
    1809          77 :       GEN S = genus2_halfstablemodel(P, p);
    1810          77 :       F = genus2_eulerfact_semistable(S, p);
    1811             :     }
    1812         546 :     if (degpol(F)!=2*ra+rt) pari_err_BUG("genus2charpoly");
    1813         546 :     return F;
    1814             :   }
    1815         385 :   E =  gmul(genus2_eulerfact_semistable(gel(W,1),p),
    1816         385 :             genus2_eulerfact_semistable(gel(W,2),p));
    1817         385 :   return gerepileupto(av, E);
    1818             : }
    1819             : 
    1820             : /*   p = 2  */
    1821             : 
    1822             : static GEN
    1823          28 : F2x_genus2_find_trans(GEN P, GEN Q, GEN F)
    1824             : {
    1825          28 :   pari_sp av = avma;
    1826          28 :   long i, d = F2x_degree(F), v = P[1];
    1827             :   GEN M, C, V;
    1828          28 :   M = cgetg(d+1, t_MAT);
    1829          84 :   for (i=1; i<=d; i++)
    1830             :   {
    1831          56 :     GEN Mi = F2x_rem(F2x_add(F2x_shift(Q,i-1), monomial_F2x(2*i-2,v)), F);
    1832          56 :     gel(M,i) = F2x_to_F2v(Mi, d);
    1833             :   }
    1834          28 :   C = F2x_to_F2v(F2x_rem(P, F), d);
    1835          28 :   V = F2m_F2c_invimage(M, C);
    1836          28 :   return gerepileuptoleaf(av, F2v_to_F2x(V, v));
    1837             : }
    1838             : 
    1839             : static GEN
    1840          35 : F2x_genus2_trans(GEN P, GEN Q, GEN H)
    1841             : {
    1842          35 :   return F2x_add(P,F2x_add(F2x_mul(H,Q), F2x_sqr(H)));
    1843             : }
    1844             : 
    1845             : static GEN
    1846          42 : F2x_genus_redoo(GEN P, GEN Q, long k)
    1847             : {
    1848          42 :   if (F2x_degree(P)==2*k)
    1849             :   {
    1850          14 :     long c = F2x_coeff(P,2*k-1), dQ = F2x_degree(Q);
    1851          14 :     if ((dQ==k-1 && c==1) || (dQ<k-1 && c==0))
    1852           7 :      return F2x_genus2_trans(P, Q, monomial_F2x(k, P[1]));
    1853             :   }
    1854          35 :   return P;
    1855             : }
    1856             : 
    1857             : static GEN
    1858          35 : F2x_pseudodisc(GEN P, GEN Q)
    1859             : {
    1860          35 :   GEN dP = F2x_deriv(P), dQ = F2x_deriv(Q);
    1861          35 :   return F2x_gcd(Q, F2x_add(F2x_mul(P, F2x_sqr(dQ)), F2x_sqr(dP)));
    1862             : }
    1863             : 
    1864             : static GEN
    1865          14 : F2x_genus_red(GEN P, GEN Q)
    1866             : {
    1867             :   long dP, dQ;
    1868             :   GEN F, FF;
    1869          14 :   P = F2x_genus_redoo(P, Q, 3);
    1870          14 :   P = F2x_genus_redoo(P, Q, 2);
    1871          14 :   P = F2x_genus_redoo(P, Q, 1);
    1872          14 :   dP = F2x_degree(P);
    1873          14 :   dQ = F2x_degree(Q);
    1874          14 :   FF = F = F2x_pseudodisc(P,Q);
    1875          35 :   while(F2x_degree(F)>0)
    1876             :   {
    1877          21 :     GEN M = gel(F2x_factor(F),1);
    1878          21 :     long i, l = lg(M);
    1879          49 :     for(i=1; i<l; i++)
    1880             :     {
    1881          28 :       GEN R = F2x_sqr(gel(M,i));
    1882          28 :       GEN H = F2x_genus2_find_trans(P, Q, R);
    1883          28 :       P = F2x_div(F2x_genus2_trans(P, Q, H), R);
    1884          28 :       Q = F2x_div(Q, gel(M,i));
    1885             :     }
    1886          21 :     F = F2x_pseudodisc(P, Q);
    1887             :   }
    1888          14 :   return mkvec4(P,Q,FF,mkvecsmall2(dP,dQ));
    1889             : }
    1890             : 
    1891             : /* Number of solutions of x^2+b*x+c */
    1892             : static long
    1893          21 : F2xqX_quad_nbroots(GEN b, GEN c, GEN T)
    1894             : {
    1895          21 :   if (lgpol(b) > 0)
    1896             :   {
    1897          14 :     GEN d = F2xq_div(c, F2xq_sqr(b, T), T);
    1898          14 :     return F2xq_trace(d, T)? 0: 2;
    1899             :   }
    1900             :   else
    1901           7 :     return 1;
    1902             : }
    1903             : 
    1904             : static GEN
    1905          14 : genus2_eulerfact2_semistable(GEN PQ)
    1906             : {
    1907          14 :   GEN V = F2x_genus_red(ZX_to_F2x(gel(PQ, 1)), ZX_to_F2x(gel(PQ, 2)));
    1908          14 :   GEN P = gel(V, 1), Q = gel(V, 2);
    1909          14 :   GEN F = gel(V, 3), v = gel(V, 4);
    1910             :   GEN abe, tor;
    1911          14 :   GEN ki, kp = pol_1(0), kq = pol_1(0);
    1912          14 :   long dP = F2x_degree(P), dQ = F2x_degree(Q), d = maxss(dP, 2*dQ);
    1913          14 :   if (!lgpol(F)) return pol_1(0);
    1914          21 :   ki = dQ!=0 || dP>0 ? xdminusone(1):
    1915           7 :       dP==-1 ? ZX_sqr(xdminusone(1)): xdminusone(2);
    1916          28 :   abe = d>=5? hyperellcharpoly(gmul(PQ,gmodulss(1,2))):
    1917          14 :         d>=3? ellfromeqncharpoly(F2x_to_ZX(P), F2x_to_ZX(Q), gen_2):
    1918          14 :         pol_1(0);
    1919          14 :   if (lgpol(F))
    1920             :   {
    1921          14 :     GEN M = gel(F2x_factor(F), 1);
    1922          14 :     long i, lF = lg(M)-1;
    1923          35 :     for(i=1; i <= lF; i++)
    1924             :     {
    1925          21 :       GEN Fi = gel(M, i);
    1926          21 :       long d = F2x_degree(Fi);
    1927          21 :       long nb  = F2xqX_quad_nbroots(F2x_rem(Q, Fi), F2x_rem(P, Fi), Fi);
    1928          35 :       GEN kqf = nb==1 ? xdminusone(d):
    1929           0 :                 nb==2 ? ZX_sqr(xdminusone(d))
    1930          14 :                       : xdminusone(2*d);
    1931          21 :       kp = gmul(kp, xdminusone(d));
    1932          21 :       kq = gmul(kq, kqf);
    1933             :     }
    1934             :   }
    1935          14 :   if (maxss(v[1],2*v[2])<5)
    1936             :   {
    1937          14 :     GEN kqoo = v[1]>2*v[2] ? xdminusone(1):
    1938           0 :                v[1]<2*v[2] ? ZX_sqr(xdminusone(1))
    1939           7 :                            : xdminusone(2);
    1940           7 :     kp = gmul(kp, xdminusone(1));
    1941           7 :     kq = gmul(kq, kqoo);
    1942             :   }
    1943          14 :   tor = RgX_div(ZX_mul(xdminusone(1),kq), ZX_mul(ki, kp));
    1944          14 :   return ZX_mul(abe, tor);
    1945             : }
    1946             : 
    1947             : GEN
    1948          14 : genus2_eulerfact2(GEN F, GEN PQ)
    1949             : {
    1950          14 :   pari_sp av = avma;
    1951          14 :   GEN W, R = genus2_type5(F, gen_2), E;
    1952          14 :   if (R) return R;
    1953          14 :   W = hyperellextremalmodels(PQ, 2, gen_2);
    1954          14 :   if (lg(W) < 3) return genus2_eulerfact2_semistable(PQ);
    1955           0 :   E = gmul(genus2_eulerfact2_semistable(gel(W,1)),
    1956           0 :            genus2_eulerfact2_semistable(gel(W,2)));
    1957           0 :   return gerepileupto(av, E);
    1958             : }
    1959             : 
    1960             : GEN
    1961         882 : genus2charpoly(GEN G, GEN p)
    1962             : {
    1963         882 :   pari_sp av = avma;
    1964         882 :   GEN gr = genus2red(G, p), F;
    1965         882 :   GEN PQ = gel(gr, 3), L = gel(gr, 4), r = gel(L, 4);
    1966         882 :   GEN P = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
    1967         882 :   if (equaliu(p,2))
    1968           0 :     F = genus2_eulerfact2(P, PQ);
    1969             :   else
    1970         882 :     F = genus2_eulerfact(P,p, r[1],r[2]);
    1971         882 :   return gerepileupto(av, F);
    1972             : }

Generated by: LCOV version 1.16