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 30734-f1f26dedcb) Lines: 1163 1246 93.3 %
Date: 2026-03-07 09:24:37 Functions: 104 107 97.2 %
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        1701 : get_basis(long p, long d)
      44             : {
      45        1701 :   if (odd(d))
      46         805 :     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 gc_GEN(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 gc_upto(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 gc_upto(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 gc_GEN(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 = gc_upto(av2, a);
     118             :     }
     119             :   }
     120        1309 :   return gc_upto(av, a);
     121             : }
     122             : 
     123             : static GEN
     124     1028762 : 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 = gc_upto(av2, M);
     175             :     }
     176             :   }
     177       10094 :   if (degpol(M)<dT-1)
     178        5488 :     return gc_upto(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 = gc_upto(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 gc_upto(av, R);
     201             :   } else
     202        1694 :     return gc_GEN(av, R);
     203             : }
     204             : 
     205             : static GEN
     206       11823 : revdigits(GEN v)
     207             : {
     208       11823 :   long i, n = lg(v)-1;
     209       11823 :   GEN w = cgetg(n+2, t_POL);
     210       11823 :   w[1] = evalsigne(1)|evalvarn(0);
     211      167783 :   for (i=0; i<n; i++)
     212      155960 :     gel(w,i+2) = gel(v,n-i);
     213       11823 :   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        1463 : ZX_to_padic(GEN P, GEN q)
     265             : {
     266        1463 :   long i, l = lg(P);
     267        1463 :   GEN Q = cgetg(l, t_POL);
     268        1463 :   Q[1] = P[1];
     269        4564 :   for (i=2; i<l ;i++)
     270        3101 :     gel(Q,i) = gadd(gel(P,i), q);
     271        1463 :   return normalizepol(Q);
     272             : }
     273             : 
     274             : static GEN
     275         329 : ZXC_to_padic(GEN x, GEN q)
     276        1792 : { pari_APPLY_type(t_COL, ZX_to_padic(gel(x, i), q)) }
     277             : 
     278             : static GEN
     279          77 : ZXM_to_padic(GEN x, GEN q)
     280         406 : { 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) = gc_GEN(av2, RgX_to_RgC(M, d-1));
     320             :   }
     321        1309 :   return gc_upto(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 gc_upto(av, ZM_to_padic(M, q));
     331             : }
     332             : 
     333             : INLINE GEN
     334        1281 : FpXXX_renormalize(GEN x, long lx)  { return ZXX_renormalize(x,lx); }
     335             : 
     336             : static GEN
     337        1050 : ZpXQXXQ_red(GEN F, GEN S, GEN T, GEN q, GEN p, long e)
     338             : {
     339        1050 :   pari_sp av = avma;
     340        1050 :   long i, dF = degpol(F);
     341             :   GEN A, C;
     342        1050 :   if (signe(F)==0) return pol_0(varn(S));
     343        1050 :   A = cgetg(dF+3, t_POL);
     344        1050 :   C = pol_0(varn(S));
     345       83797 :   for(i=dF; i>0; i--)
     346             :   {
     347       82747 :     GEN Fi = FpXX_add(C, gel(F,i+2), q);
     348       82747 :     GEN R, Q = ZpXQX_divrem(Fi, S, T, q, p, e, &R);
     349       82747 :     gel(A,i+2) = R;
     350       82747 :     C = Q;
     351             :   }
     352        1050 :   gel(A,2) = FpXX_add(C, gel(F,2), q);
     353        1050 :   A[1] = F[1];
     354        1050 :   return gc_GEN(av, FpXXX_renormalize(A,dF+3));
     355             : }
     356             : 
     357             : static GEN
     358         245 : ZpXQXXQ_sqr(GEN x, GEN S, GEN T, GEN q, GEN p, long e)
     359             : {
     360         245 :   pari_sp av = avma;
     361             :   GEN z, kx;
     362         245 :   long n = degpol(S);
     363         245 :   kx = RgXX_to_Kronecker(x, n);
     364         245 :   z = Kronecker_to_ZXX(FpXQX_sqr(kx, T, q), n, varn(S));
     365         245 :   return gc_upto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     366             : }
     367             : 
     368             : static GEN
     369         812 : ZpXQXXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN q, GEN p, long e)
     370             : {
     371         812 :   pari_sp av = avma;
     372             :   GEN z, kx, ky;
     373         812 :   long n = degpol(S);
     374         812 :   kx = RgXX_to_Kronecker(x, n);
     375         812 :   ky = RgXX_to_Kronecker(y, n);
     376         805 :   z = Kronecker_to_ZXX(FpXQX_mul(ky, kx, T, q), n, varn(S));
     377         805 :   return gc_upto(av, ZpXQXXQ_red(z, S, T, q, p, e));
     378             : }
     379             : 
     380             : static GEN
     381         231 : FpXXX_red(GEN z, GEN p)
     382             : {
     383             :   GEN res;
     384         231 :   long i, l = lg(z);
     385         231 :   res = cgetg(l,t_POL); res[1] = z[1];
     386       13867 :   for (i=2; i<l; i++)
     387             :   {
     388       13636 :     GEN zi = gel(z,i);
     389       13636 :     if (typ(zi)==t_INT)
     390         217 :       gel(res,i) = modii(zi,p);
     391             :     else
     392       13419 :      gel(res,i) = FpXX_red(zi,p);
     393             :   }
     394         231 :   return FpXXX_renormalize(res,lg(res));
     395             : }
     396             : 
     397             : static GEN
     398         231 : FpXXX_Fp_mul(GEN z, GEN a, GEN p)
     399             : {
     400         231 :   return FpXXX_red(RgX_Rg_mul(z, a), p);
     401             : }
     402             : 
     403             : static GEN
     404          91 : ZpXQXXQ_invsqrt(GEN F, GEN S, GEN T, ulong p, long e)
     405             : {
     406          91 :   pari_sp av = avma, av2, av3;
     407             :   ulong mask;
     408          91 :   long v = varn(F), n=1;
     409             :   pari_timer ti;
     410          91 :   GEN a = pol_1(v), pp = utoipos(p);
     411          91 :   if (DEBUGLEVEL>1) timer_start(&ti);
     412          91 :   if (e <= 1) return gc_GEN(av, a);
     413          91 :   mask = quadratic_prec_mask(e);
     414          91 :   av2 = avma;
     415         322 :   for (;mask>1;)
     416             :   {
     417             :     GEN q, q2, q22, f, fq, afq;
     418         238 :     long n2 = n;
     419         238 :     n<<=1; if (mask & 1) n--;
     420         238 :     mask >>= 1;
     421         238 :     q = powuu(p,n); q2 = powuu(p,n2);
     422         238 :     av3 = avma;
     423         238 :     f = RgX_sub(ZpXQXXQ_mul(F, ZpXQXXQ_sqr(a, S, T, q, pp, n), S, T, q, pp, n), pol_1(v));
     424         238 :     fq = gc_upto(av3, RgX_Rg_divexact(f, q2));
     425         238 :     q22 = shifti(addiu(q2,1),-1);
     426         238 :     afq = FpXXX_Fp_mul(ZpXQXXQ_mul(a, fq, S, T, q2, pp, n2), q22, q2);
     427         231 :     a = RgX_sub(a, RgX_Rg_mul(afq, q2));
     428         231 :     if (gc_needed(av2,1))
     429             :     {
     430           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_invsqrt, e = %ld", n);
     431           0 :       a = gc_upto(av2, a);
     432             :     }
     433             :   }
     434          84 :   return gc_upto(av, a);
     435             : }
     436             : 
     437             : static GEN
     438        6622 : frac_to_Fq(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     439             : {
     440        6622 :   GEN d = gcdii(ZX_content(a), ZX_content(b));
     441        6622 :   return ZpXQ_div(ZX_Z_divexact(a, d), ZX_Z_divexact(b, d), T, q, p, e);
     442             : }
     443             : 
     444             : static GEN
     445         329 : ZpXQXXQ_frob(GEN F, GEN U, GEN V, long k, GEN S, GEN T, ulong p, long e)
     446             : {
     447         329 :   pari_sp av = avma, av2;
     448         329 :   long i, pr = degpol(F), dS = degpol(S), v = varn(T);
     449         329 :   GEN q = powuu(p,e), pp = utoipos(p);
     450         329 :   GEN Sp = RgX_deriv(S), Sp1 = RgX_shift_shallow(Sp, 1);
     451         329 :   GEN M = gel(F,pr+2), R;
     452         329 :   av2 = avma;
     453       48587 :   for(i = pr-1; i>=k; i--)
     454             :   {
     455             :     GEN A, B, H, Bc;
     456             :     ulong v, r;
     457       48258 :     H = ZpXQX_divrem(FpXQX_mul(V, M, T, q), S, T, q, utoipos(p), e, &B);
     458       48258 :     A = FpXX_add(FpXQX_mul(U, M, T, q), FpXQX_mul(H, Sp, T, q),q);
     459       48258 :     v = u_lvalrem(2*i+1,p,&r);
     460       48258 :     Bc = RgX_deriv(B);
     461       48258 :     Bc = FpXX_Fp_mul(ZXX_Z_divexact(Bc,powuu(p,v)), Fp_divu(gen_2, r, q), q);
     462       48258 :     M = FpXX_add(gel(F,i+2), FpXX_add(A, Bc, q), q);
     463       48258 :     if (gc_needed(av2,1))
     464             :     {
     465           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_frob, step 1, i = %ld", i);
     466           0 :       M = gc_upto(av2, M);
     467             :     }
     468             :   }
     469         329 :   if (degpol(M)<dS-1)
     470         189 :     return gc_upto(av, M);
     471         140 :   R = RgX_shift_shallow(M,dS-degpol(M)-2);
     472         140 :   av2 = avma;
     473        7063 :   for(i = degpol(M)-dS+2; i>=1; i--)
     474             :   {
     475             :     GEN B, c;
     476        6923 :     R = RgX_shift_shallow(R, 1);
     477        6923 :     gel(R,2) = gel(M, i+1);
     478        6923 :     if (degpol(R) < dS) continue;
     479        6517 :     B = FpXX_add(FpXX_mulu(S, 2*i, q), Sp1, q);
     480        6517 :     c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(B),v), T, q, pp, e);
     481        6517 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(B, c, T, q), q);
     482        6517 :     if (gc_needed(av2,1))
     483             :     {
     484           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
     485           0 :       R = gc_upto(av2, R);
     486             :     }
     487             :   }
     488         140 :   if (degpol(R)==dS-1)
     489             :   {
     490         105 :     GEN c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(Sp),v), T, q, pp, e);
     491         105 :     R = FpXX_sub(R, FpXQX_FpXQ_mul(Sp, c, T, q), q);
     492         105 :     return gc_upto(av, R);
     493             :   } else
     494          35 :     return gc_GEN(av, R);
     495             : }
     496             : 
     497             : static GEN
     498         329 : 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         329 :   if (DEBUGLEVEL>1) timer_start(&ti);
     504         329 :   Q = revdigits(ZpXQX_digits(A, S, T, q, p, e));
     505         329 :   n = degpol(Q);
     506         329 :   if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
     507         329 :   sQ = ZpXQXXQ_mul(s, Q, S, T, q, p, e);
     508         329 :   if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
     509         329 :   qS = RgX_shift_shallow(sQ,m-n);
     510         329 :   v = ZX_val(sQ);
     511         329 :   if (n > m + v)
     512             :   {
     513         119 :     long i, l = n-m-v;
     514         119 :     GEN rS = cgetg(l+1,t_VEC);
     515        1407 :     for (i = l-1; i >=0 ; i--)
     516        1288 :       gel(rS,i+1) = gel(sQ, 1+v+l-i);
     517         119 :     rS = FpXQXV_FpXQX_fromdigits(rS, S, T, q);
     518         119 :     gel(qS,2) = FpXX_add(FpXQX_mul(rS, S, T, q), gel(qS, 2), q);
     519         119 :     if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
     520             :   }
     521         329 :   return qS;
     522             : }
     523             : 
     524             : static void
     525          84 : Fq_get_UV(GEN *U, GEN *V, GEN S, GEN T, ulong p, long e)
     526             : {
     527          84 :   GEN q = powuu(p, e), pp = utoipos(p), d;
     528          84 :   GEN dS = RgX_deriv(S), R  = polresultantext(S, dS), C;
     529          84 :   long v = varn(S);
     530          84 :   if (signe(FpX_red(to_ZX(gel(R,3),v), pp))==0) is_sing(S, p);
     531          77 :   C = FpXQ_red(to_ZX(gel(R, 3),v), T, q);
     532          77 :   d = ZpXQ_inv(C, T, pp, e);
     533          77 :   *U = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,1),v),T,q),d,T,q);
     534          77 :   *V = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,2),v),T,q),d,T,q);
     535          77 : }
     536             : 
     537             : static GEN
     538         329 : ZXX_to_FpXC(GEN x, long N, GEN p, long v)
     539             : {
     540             :   long i, l;
     541             :   GEN z;
     542         329 :   l = lg(x)-1; x++;
     543         329 :   if (l > N+1) l = N+1; /* truncate higher degree terms */
     544         329 :   z = cgetg(N+1,t_COL);
     545        1792 :   for (i=1; i<l ; i++)
     546             :   {
     547        1463 :     GEN xi = gel(x, i);
     548        1463 :     gel(z,i) = typ(xi)==t_INT? scalarpol(Fp_red(xi, p), v): FpX_red(xi, p);
     549             :   }
     550         329 :   for (   ; i<=N ; i++)
     551           0 :     gel(z,i) = pol_0(v);
     552         329 :   return z;
     553             : }
     554             : 
     555             : GEN
     556          91 : ZlXQX_hyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     557             : {
     558          91 :   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          91 :   if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
     563          91 :   if (p == 2) is_sing(H, 2);
     564          91 :   d = degpol(H);
     565          91 :   if (d <= 0) pari_err_CONSTPOL("hyperellpadicfrobenius");
     566          91 :   if (n < 1) pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
     567          91 :   k = get_basis(p, d); pp = utoipos(p);
     568          91 :   N = n + ulogint(2*n, p) + 1;
     569          91 :   q = powuu(p,n); N1 = N+1;
     570          91 :   pN1 = powuu(p,N1); T = FpX_get_red(T, pN1);
     571          91 :   Q = RgX_to_FqX(H, T, pN1);
     572          91 :   if (signe(FpX_red(to_ZX(leading_coeff(Q),varn(Q)),pp))==0) is_sing(H, p);
     573          91 :   if (DEBUGLEVEL>1) timer_start(&ti);
     574          91 :   xp = ZpX_Frobenius(T, pp, N1);
     575          91 :   s = RgX_inflate(FpXY_FpXQ_evalx(Q, xp, T, pN1), p);
     576          91 :   s = revdigits(ZpXQX_digits(s, Q, T, pN1, pp, N1));
     577          91 :   if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
     578          91 :   s = ZpXQXXQ_invsqrt(s, Q, T, p, N);
     579          84 :   if (k==3)
     580           7 :     s = ZpXQXXQ_mul(s, ZpXQXXQ_sqr(s, Q, T, pN1, pp, N1), Q, T, pN1, pp, N1);
     581          84 :   if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
     582          84 :   Fq_get_UV(&U, &V, Q, T, p, N+1);
     583          77 :   if (DEBUGLEVEL>1) timer_printf(&ti,"get_UV");
     584          77 :   F = cgetg(d, t_MAT);
     585         406 :   for (i = 1; i < d; i++)
     586             :   {
     587         329 :     pari_sp av2 = avma;
     588             :     GEN M, D;
     589         329 :     D = Fq_diff_red(s, monomial(pp,p*i-1,1),(k*p-1)>>1, Q, T, pN1, pp, N1);
     590         329 :     if (DEBUGLEVEL>1) timer_printf(&ti,"red");
     591         329 :     M = ZpXQXXQ_frob(D, U, V, (k - 1)>>1, Q, T, p, N1);
     592         329 :     if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
     593         329 :     gel(F, i) = gc_upto(av2, ZXX_to_FpXC(M, d-1, q, varn(T)));
     594             :   }
     595          77 :   return gc_upto(av, F);
     596             : }
     597             : 
     598             : GEN
     599          91 : nfhyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
     600             : {
     601          91 :   pari_sp av = avma;
     602          91 :   GEN pp = utoipos(p), q = zeropadic_shallow(pp, n);
     603          91 :   GEN M = ZlXQX_hyperellpadicfrobenius(lift_shallow(H),T,p,n);
     604          77 :   GEN MM = ZpXQM_prodFrobenius(M, T, pp, n);
     605          77 :   GEN m = gmul(ZXM_to_padic(MM, q), gmodulo(gen_1, T));
     606          77 :   return gc_upto(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         273 : Flx_difftable(GEN P, ulong p)
     637             : {
     638         273 :   long i, n = degpol(P);
     639         273 :   GEN V = cgetg(n+2, t_VEC);
     640         273 :   gel(V, n+1) = P;
     641        1869 :   for(i = n; i >= 1; i--)
     642        1596 :     gel(V, i) = Flx_diff1(gel(V, i+1), p);
     643         273 :   return V;
     644             : }
     645             : 
     646             : static GEN
     647        1617 : FlxV_Fl2_eval_pre(GEN V, GEN x, ulong D, ulong p, ulong pi)
     648             : {
     649        1617 :   long i, n = lg(V)-1;
     650        1617 :   GEN r = cgetg(n+1, t_VEC);
     651       12390 :   for (i = 1; i <= n; i++)
     652       10773 :     gel(r, i) = Flx_Fl2_eval_pre(gel(V, i), x, D, p, pi);
     653        1617 :   return r;
     654             : }
     655             : 
     656             : static GEN
     657       44898 : Fl2V_next(GEN V, ulong p)
     658             : {
     659       44898 :   long i, n = lg(V)-1;
     660       44898 :   GEN r = cgetg(n+1, t_VEC);
     661       44898 :   gel(r, 1) = gel(V, 1);
     662      288330 :   for (i = 2; i <= n; i++)
     663      243432 :     gel(r, i) = Flv_add(gel(V, i), gel(V, i-1), p);
     664       44898 :   return r;
     665             : }
     666             : 
     667             : static GEN
     668         273 : FlxV_constant(GEN x)
     669        2142 : { pari_APPLY_long(Flx_constant(gel(x,i))) }
     670             : 
     671             : static GEN
     672         273 : Flx_genus2charpoly_naive(GEN H, ulong p)
     673             : {
     674         273 :   pari_sp av = avma, av2;
     675         273 :   ulong pi = get_Fl_red(p);
     676         273 :   ulong i, j, p2 = p>>1, D = 2, e = ((p&2UL) == 0) ? -1 : 1;
     677         273 :   long a, b, c = 0, n = degpol(H);
     678         273 :   GEN t, d, k = const_vecsmall(p, -1);
     679         273 :   k[1] = 0;
     680        1890 :   for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p)) k[j+1] = 1;
     681         329 :   while (k[1+D] >= 0) D++;
     682         273 :   b = n == 5 ? 0 : 1;
     683         273 :   a = b ? k[1+Flx_lead(H)]: 0;
     684         273 :   t = Flx_difftable(H, p);
     685         273 :   d = FlxV_constant(t);
     686         273 :   av2 = avma;
     687        3780 :   for (i=0; i < p; i++)
     688             :   {
     689        3507 :     ulong v = uel(d,n+1);
     690        3507 :     a += k[1+v];
     691        3507 :     b += !!v;
     692        3507 :     if (n==6)
     693        2373 :       uel(d,7) = Fl_add(uel(d,7), uel(d,6), p);
     694        3507 :     uel(d,6) = Fl_add(uel(d,6), uel(d,5), p);
     695        3507 :     uel(d,5) = Fl_add(uel(d,5), uel(d,4), p);
     696        3507 :     uel(d,4) = Fl_add(uel(d,4), uel(d,3), p);
     697        3507 :     uel(d,3) = Fl_add(uel(d,3), uel(d,2), p);
     698        3507 :     uel(d,2) = Fl_add(uel(d,2), uel(d,1), p);
     699             :   }
     700        1890 :   for (j=1; j <= p2; j++)
     701             :   {
     702        1617 :     GEN V = FlxV_Fl2_eval_pre(t, mkvecsmall2(0, j), D, p, pi);
     703        1617 :     for (i=0;; i++)
     704       44898 :     {
     705       46515 :       GEN r2 = gel(V, n+1);
     706       93030 :       c += uel(r2,2) ?
     707       44135 :         (uel(r2,1) ? uel(k,1+Fl2_norm_pre(r2, D, p, pi)): e)
     708       90650 :          : !!uel(r2,1);
     709       46515 :       if (i == p-1) break;
     710       44898 :       V = Fl2V_next(V, p);
     711             :     }
     712        1617 :     set_avma(av2);
     713             :   }
     714         273 :   set_avma(av);
     715         273 :   return mkvecsmalln(6, 0UL, p*p, a*p, (b+2*c+a*a)>>1, a, 1UL);
     716             : }
     717             : 
     718             : static GEN
     719         609 : charpoly_funceq(GEN P, GEN q)
     720             : {
     721         609 :   long i, l, g = degpol(P)>>1;
     722         609 :   GEN R, Q = gpowers0(q, g-1, q); /* Q[i] = q^i, i <= g */
     723         609 :   R = cgetg_copy(P, &l); R[1] = P[1];
     724        3024 :   for (i=0; i<g; i++) gel(R, i+2) = mulii(gel(P, 2*g-i+2), gel(Q, g-i));
     725        3633 :   for (; i<=2*g; i++) gel(R, i+2) = icopy(gel(P, i+2));
     726         609 :   return R;
     727             : }
     728             : 
     729             : static long
     730         623 : hyperell_Weil_bound(GEN q, ulong g, GEN p)
     731             : {
     732         623 :   pari_sp av = avma;
     733         623 :   GEN w = mulii(binomialuu(2*g,g),sqrtint(shifti(powiu(q, g),2)));
     734         623 :   return gc_long(av, logint(w,p) + 1);
     735             : }
     736             : 
     737             : /* return 4P + Q^2 */
     738             : static GEN
     739      289482 : check_hyperell(GEN PQ)
     740             : {
     741             :   GEN H;
     742      289482 :   if (is_vec_t(typ(PQ)) && lg(PQ)==3)
     743      225686 :     H = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
     744             :   else
     745       63796 :     H = gmul2n(PQ, 2);
     746      289482 :   return typ(H) == t_POL? H: NULL;
     747             : }
     748             : 
     749             : GEN
     750         987 : hyperellcharpoly(GEN PQ)
     751             : {
     752         987 :   pari_sp av = avma;
     753         987 :   GEN M, R, T=NULL, pp=NULL, q;
     754         987 :   long d, n, eps = 0;
     755             :   ulong p;
     756         987 :   GEN H = check_hyperell(PQ);
     757         987 :   if (!H || !RgX_is_FpXQX(H, &T, &pp) || !pp)
     758           0 :     pari_err_TYPE("hyperellcharpoly", PQ);
     759         987 :   p = itou(pp);
     760         987 :   if (!T)
     761             :   {
     762         903 :     if (p==2 && is_vec_t(typ(PQ)))
     763             :     {
     764          84 :       long dP, dQ, v = varn(H);
     765          84 :       GEN P = gel(PQ,1), Q = gel(PQ,2);
     766          84 :       if (typ(P)!=t_POL)  P = scalarpol(P, v);
     767          84 :       if (typ(Q)!=t_POL)  Q = scalarpol(Q, v);
     768          84 :       dP = degpol(P); dQ = degpol(Q);
     769          84 :       if (dP<=6 && dQ <=3 && (dQ==3 || dP>=5))
     770             :       {
     771          84 :         GEN P2 = RgX_to_F2x(P), Q2 = RgX_to_F2x(Q);
     772          84 :         GEN D = F2x_add(F2x_mul(P2, F2x_sqr(F2x_deriv(Q2))), F2x_sqr(F2x_deriv(P2)));
     773          84 :         if (F2x_degree(F2x_gcd(D, Q2))) is_sing(PQ, 2);
     774          84 :         if (dP==6 && dQ<3 && F2x_coeff(P2,5)==F2x_coeff(Q2,2))
     775           0 :           is_sing(PQ, 2); /* The curve is singular at infinity */
     776          84 :         R = zx_to_ZX(F2x_genus2charpoly_naive(P2, Q2));
     777          84 :         return gc_upto(av, R);
     778             :       }
     779             :     }
     780         819 :     H = RgX_to_FpX(H, pp);
     781         819 :     d = degpol(H);
     782         819 :     if (d <= 0) is_sing(H, p);
     783         819 :     if (p > 2 && ((d == 5 && p < 17500) || (d == 6 && p < 24500)))
     784             :     {
     785         280 :       GEN Hp = ZX_to_Flx(H, p);
     786         280 :       if (!Flx_is_squarefree(Hp, p)) is_sing(H, p);
     787         273 :       R = zx_to_ZX(Flx_genus2charpoly_naive(Hp, p));
     788         273 :       return gc_upto(av, R);
     789             :     }
     790         539 :     n = hyperell_Weil_bound(pp, (d-1)>>1, pp);
     791         539 :     eps = odd(d)? 0: Fp_issquare(leading_coeff(H), pp);
     792         539 :     M = hyperellpadicfrobenius(H, p, n);
     793         539 :     R = centerlift(carberkowitz(M, 0));
     794         539 :     q = pp;
     795             :   }
     796             :   else
     797             :   {
     798             :     int fixvar;
     799          84 :     T = typ(T)==t_FFELT? FF_mod(T): RgX_to_FpX(T, pp);
     800          84 :     q = powuu(p, degpol(T));
     801          84 :     fixvar = (varncmp(varn(T),varn(H)) <= 0);
     802          84 :     if (fixvar) setvarn(T, fetch_var());
     803          84 :     H = RgX_to_FpXQX(H, T, pp);
     804          84 :     d = degpol(H);
     805          84 :     if (d <= 0) is_sing(H, p);
     806          84 :     eps = odd(d)? 0: Fq_issquare(leading_coeff(H), T, pp);
     807          84 :     n = hyperell_Weil_bound(q, (d-1)>>1, pp);
     808          84 :     M = nfhyperellpadicfrobenius(H, T, p, n);
     809          70 :     R = simplify_shallow(centerlift(liftpol_shallow(carberkowitz(M, 0))));
     810          70 :     if (fixvar) (void)delete_var();
     811             :   }
     812         609 :   if (!odd(d))
     813             :   {
     814         301 :     GEN b = get_basis(p, d) == 3 ? gen_1 : q;
     815         301 :     GEN pn = powuu(p, n);
     816         301 :     R = FpX_div_by_X_x(R, eps? b: negi(b), pn, NULL);
     817         301 :     R = FpX_center_i(R, pn, shifti(pn,-1));
     818             :   }
     819         609 :   return gc_upto(av, charpoly_funceq(R, q));
     820             : }
     821             : 
     822             : int
     823        3493 : hyperellisoncurve(GEN W, GEN P)
     824             : {
     825        3493 :   pari_sp av = avma;
     826             :   long res;
     827             :   GEN x, y;
     828        3493 :   if (typ(P)!=t_VEC || lg(P)!=3) pari_err_TYPE("hyperellisoncurve",P);
     829        3493 :   x = gel(P,1); y = gel(P,2);
     830        3493 :   if (typ(W)==t_POL)
     831           0 :     res = gequal(gsqr(y), poleval(W,x));
     832             :   else
     833             :   {
     834        3493 :     if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
     835        3493 :     res = gequal(gmul(y, gadd(y,poleval(gel(W,2), x))), poleval(gel(W,1), x));
     836             :   }
     837        3493 :   return gc_int(av, res);
     838             : }
     839             : 
     840             : GEN
     841          35 : hyperellordinate(GEN W, GEN x)
     842             : {
     843          35 :   pari_sp av = avma;
     844          35 :   if (typ(W)==t_POL)
     845             :   {
     846          14 :     GEN d = poleval(W,x), y;
     847          14 :     if (gequal0(d)) { return gc_GEN(av, mkvec(d)); }
     848          14 :     if (!issquareall(d, &y)) retgc_const(av, cgetg(1, t_VEC));
     849           7 :     return gc_GEN(av, mkvec2(y, gneg(y)));
     850             :   }
     851             :   else
     852             :   {
     853             :     GEN b, c, d, rd, y;
     854          21 :     if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
     855          21 :     b = poleval(gel(W,2), x); c = poleval(gel(W,1), x);
     856          21 :     d = gadd(gsqr(b), gmul2n(c, 2));
     857          21 :     if (gequal0(d)) { return gc_GEN(av, mkvec(gmul2n(gneg(b),-1))); }
     858          14 :     if (!issquareall(d, &rd)) retgc_const(av, cgetg(1, t_VEC));
     859           7 :     y = gmul2n(gsub(rd, b), -1);
     860           7 :     return gc_GEN(av, mkvec2(y, gsub(y,rd)));
     861             :   }
     862             : }
     863             : 
     864             : static long
     865      355646 : hyperellgenus(GEN H)
     866      355646 : { long d = degpol(H); return ((d+1)>>1)-1; }
     867             : 
     868             : GEN
     869      118971 : hyperelldisc(GEN PQ)
     870             : {
     871      118971 :   pari_sp av = avma;
     872      118971 :   GEN D, H = check_hyperell(PQ);
     873             :   long g;
     874      118971 :   if (!H || signe(H)==0) pari_err_TYPE("hyperelldisc",PQ);
     875      118971 :   g = hyperellgenus(H);
     876      118971 :   D = gmul2n(RgX_disc(H),-4*(g+1));
     877      118971 :   if (odd(degpol(H))) D = gmul(D, gsqr(leading_coeff(H)));
     878      118971 :   return gc_upto(av, D);
     879             : }
     880             : 
     881             : static long
     882      126677 : get_ep(GEN W)
     883             : {
     884      126677 :   GEN P = gel(W,1), Q = gel(W,2);
     885      126677 :   if (signe(Q)==0) return ZX_lval(P,2);
     886       86412 :   return minss(ZX_lval(P,2), ZX_lval(Q,2));
     887             : }
     888             : 
     889             : static GEN
     890       50910 : algo51(GEN W, GEN M)
     891             : {
     892       50910 :   GEN P = gel(W,1), Q = gel(W,2);
     893             :   for(;;)
     894       10647 :   {
     895       61557 :     long vP = ZX_lval(P,2);
     896       61557 :     long vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
     897             :     long r;
     898             :     /* 1 */
     899       61557 :     if (vQ==0) break;
     900             :     /* 2 */
     901       36204 :     if (vP==0)
     902             :     {
     903             :       GEN H, H1;
     904             :       /* a */
     905       29644 :       RgX_even_odd(FpX_red(P,gen_2),&H, &H1);
     906       29644 :       if (signe(H1)) break;
     907             :       /* b */
     908       14965 :       P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
     909       14965 :       Q = ZX_sub(Q, ZX_shifti(H, 1));
     910       14965 :       vP = ZX_lval(P,2);
     911       14965 :       vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
     912             :     }
     913             :     /* 2c */
     914       21525 :     if (vP==1) break;
     915             :     /* 2d */
     916       10647 :     r = minss(2*vQ, vP)>>1;
     917       10647 :     if (M) gel(M,1) = shifti(gel(M,1), r);
     918       10647 :     P = ZX_shifti(P, -2*r);
     919       10647 :     Q = ZX_shifti(Q, -r);
     920             :   }
     921       50910 :   return mkvec2(P,Q);
     922             : }
     923             : 
     924             : static GEN
     925      103469 : algo52(GEN W, GEN c, long *pt_lambda)
     926             : {
     927             :   long lambda;
     928      103469 :   GEN P = gel(W,1), Q = gel(W,2);
     929             :   for(;;)
     930      117043 :   {
     931             :     GEN H, H1;
     932             :     /* 1 */
     933      220512 :     GEN Pc = ZX_affine(P,gen_2,c), Qc = ZX_affine(Q,gen_2,c);
     934      220512 :     long mP = ZX_lval(Pc,2), mQ = signe(Qc) ? ZX_lval(Qc,2): mP+1;
     935             :     /* 2 */
     936      220512 :     if (2*mQ <= mP) { lambda = 2*mQ; break; }
     937             :     /* 3 */
     938      188220 :     if (odd(mP)) { lambda = mP; break; }
     939             :     /* 4 */
     940      127816 :     RgX_even_odd(FpX_red(ZX_shifti(Pc, -mP),gen_2),&H, &H1);
     941      127816 :     if (signe(H1)) { lambda = mP; break; }
     942             :     /* 5 */
     943      117043 :     P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
     944      117043 :     Q = ZX_sub(Q, ZX_shifti(H, 1));
     945             :   }
     946      103469 :   *pt_lambda = lambda;
     947      103469 :   return mkvec2(P,Q);
     948             : }
     949             : 
     950             : static long
     951      147407 : test53(long lambda, long ep, long g)
     952             : {
     953      147407 :   return (lambda <= g+1) || (odd(g) && lambda<g+3 && ep==1);
     954             : }
     955             : 
     956             : static long
     957      189648 : test55(GEN W, long ep, long g)
     958             : {
     959      189648 :   GEN P = gel(W,1), Q = gel(W,2);
     960      189648 :   GEN Pe = FpX_red(ep ? ZX_shifti(P,-1): P, gen_2);
     961      189648 :   GEN Qe = FpX_red(ep ? ZX_shifti(Q,-1): Q, gen_2);
     962      189648 :   if (ep==0)
     963             :   {
     964      149255 :     if (signe(Qe)!=0) return ZX_val(Qe) >= (g + 3)>>1;
     965       90814 :     else return ZX_val(FpX_deriv(Pe, gen_2)) >= g+1;
     966             :   }
     967             :   else
     968       40393 :     return ZX_val(Qe) >= (g+1)>>1 && ZX_val(Pe) >= g + 1;
     969             : }
     970             : 
     971             : static GEN
     972       50840 : hyperell_reverse(GEN W, long g)
     973             : {
     974       50840 :   return mkvec2(RgXn_recip_shallow(gel(W,1),2*g+3),
     975       50840 :                 RgXn_recip_shallow(gel(W,2),g+2));
     976             : }
     977             : 
     978             : /* [P,Q] -> [P(2x)/4^r, Q(2x)/2^r] */
     979             : static GEN
     980      169774 : ZX2_unscale(GEN W, long r)
     981             : {
     982      169774 :   GEN P = ZX_unscale2n(gel(W,1), 1);
     983      169774 :   GEN Q = ZX_unscale2n(gel(W,2), 1);
     984      169774 :   if (r)
     985             :   {
     986       30931 :     P = ZX_shifti(P, -2*r);
     987       30931 :     Q = ZX_shifti(Q, -r);
     988             :   }
     989      169774 :   return mkvec2(P,Q);
     990             : }
     991             : /* [P,Q] -> [P(2x+c)/4^r, Q(2x+c)/2^r] */
     992             : static GEN
     993      163756 : ZX2_affine_unscale(GEN W, long c, long r)
     994             : {
     995      238886 :   if (c) W = mkvec2(ZX_Z_translate(gel(W,1), gen_1),
     996       75130 :                     ZX_Z_translate(gel(W,2), gen_1));
     997      163756 :   return ZX2_unscale(W, r);
     998             : }
     999             : 
    1000             : static GEN
    1001       50805 : algo56(GEN W, long g)
    1002             : {
    1003             :   long ep;
    1004       50805 :   GEN M = mkvec2(gen_1, matid(2)), Woo;
    1005       50805 :   W = algo51(W, M);
    1006       50805 :   Woo = hyperell_reverse(W, g);
    1007       50805 :   ep = get_ep(Woo);
    1008       50805 :   if (test55(Woo,ep,g))
    1009             :   {
    1010             :     long lambda;
    1011       11744 :     Woo = algo52(Woo, gen_0, &lambda);
    1012       11744 :     if (!test53(lambda,ep,g))
    1013             :     {
    1014        5969 :       long r = lambda>>1;
    1015        5969 :       gel(M,1) = shifti(gel(M,1), r);
    1016        5969 :       gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0, gen_1, gen_2, gen_0));
    1017        5969 :       W = ZX2_unscale(Woo, r);
    1018             :     }
    1019             :   }
    1020             :   for(;;)
    1021       24892 :   {
    1022       75697 :     long j, ep = get_ep(W);
    1023      189403 :     for (j = 0; j < 2; j++)
    1024      138598 :       if (test55(ZX2_affine_unscale(W, j, 0), ep, g))
    1025             :       {
    1026             :         long lambda;
    1027       91578 :         GEN c = utoi(j), Wc = algo52(W, c, &lambda);
    1028       91578 :         if (!test53(lambda,ep,g))
    1029             :         {
    1030       24892 :           long r = lambda>>1;
    1031       24892 :           gel(M,1) = shifti(gel(M,1), r);
    1032       24892 :           gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_2, c, gen_0, gen_1));
    1033       24892 :           W = ZX2_affine_unscale(Wc, j, r);
    1034       24892 :           break;
    1035             :         }
    1036             :       }
    1037       75697 :     if (j==2) break;
    1038             :   }
    1039       50805 :   return mkvec2(W, M);
    1040             : }
    1041             : 
    1042             : static GEN
    1043         105 : algo56bis(GEN W, long g, long inf, long thr)
    1044             : {
    1045         105 :   pari_sp av = avma;
    1046         105 :   GEN vl = cgetg(3,t_VEC);
    1047         105 :   long nl = 1;
    1048         105 :   W = algo51(W, NULL);
    1049         105 :   if (inf)
    1050             :   {
    1051          35 :     GEN Woo = hyperell_reverse(W, g);
    1052          35 :     long ep = get_ep(Woo);
    1053          35 :     if (test55(ZX2_unscale(Woo, 0), ep, g))
    1054             :     {
    1055             :       long lambda;
    1056          28 :       Woo = algo52(Woo, gen_0, &lambda);
    1057          28 :       if (lambda == thr) gel(vl,nl++) = ZX2_unscale(Woo, lambda>>1);
    1058             :     }
    1059             :   }
    1060             :   {
    1061         105 :     long j, ep = get_ep(W);
    1062         315 :     for (j = 0; j < 2; j++)
    1063         210 :       if (test55(ZX2_affine_unscale(W, j, 0), ep, g))
    1064             :       {
    1065             :         long lambda;
    1066         119 :         GEN Wc = algo52(W, utoi(j), &lambda);
    1067         119 :         if (lambda == thr) gel(vl,nl++) = ZX2_affine_unscale(Wc, j, lambda>>1);
    1068             :       }
    1069             :   }
    1070         105 :   setlg(vl, nl);
    1071         105 :   return gc_GEN(av,vl);
    1072             : }
    1073             : 
    1074             : /* return the (degree 2) apolar invariant (the nth transvectant of P and P) */
    1075             : static GEN
    1076          91 : ZX_apolar(GEN P, long n)
    1077             : {
    1078          91 :   pari_sp av = avma;
    1079          91 :   long d = degpol(P), i;
    1080          91 :   GEN s = gen_0, g = cgetg(n+2,t_VEC);
    1081          91 :   gel(g,1) = gen_1;
    1082         637 :   for (i = 1; i <= n; i++) gel(g,i+1) = muliu(gel(g,i),i); /* g[i+1] = i! */
    1083         714 :   for (i = n-d; i <= d; i++)
    1084             :   {
    1085         623 :      GEN a = mulii(mulii(gel(g,i+1),gel(g,n-i+1)),
    1086         623 :                    mulii(gel(P,i+2),gel(P,n-i+2)));
    1087         623 :      s = odd(i)? subii(s, a): addii(s, a);
    1088             :   }
    1089          91 :   return gc_INT(av,s);
    1090             : }
    1091             : 
    1092             : static GEN
    1093       53038 : algo57(GEN F, long g, GEN pr)
    1094             : {
    1095             :   long i, l;
    1096       53038 :   GEN D, C = content(F);
    1097       53038 :   GEN e = gel(core2(shifti(C,-vali(C))),2);
    1098       53038 :   GEN M = mkvec2(e, matid(2));
    1099       53038 :   long minvd = (2*g+1)>>(odd(g) ? 4:2);
    1100       53038 :   F = ZX_Z_divexact(F, sqri(e));
    1101       53038 :   D = absi(hyperelldisc(F));
    1102       53038 :   if (!pr)
    1103             :   {
    1104          91 :     GEN A = gcdii(D, ZX_apolar(F, 2*g+2));
    1105          91 :     pr = gel(factor(shifti(A, -vali(A))),1);
    1106             :   }
    1107       53038 :   l = lg(pr);
    1108      312821 :   for (i = 1; i < l; i++)
    1109             :   {
    1110             :     long ep;
    1111      259783 :     GEN p = gel(pr, i), ps2 = shifti(p,-1), Fe;
    1112      259783 :     if (equaliu(p,2) || Z_pval(D,p) < minvd) continue;
    1113      197547 :     ep = ZX_pvalrem(F,p, &Fe); Fe = FpX_red(Fe, p);
    1114      197547 :     if (degpol(Fe) < g+1+ep)
    1115             :     {
    1116        6406 :       GEN Fi = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
    1117        6406 :       long lambda = ZX_pval(Fi,p);
    1118        6406 :       if (!test53(lambda,ep,g))
    1119             :       {
    1120        3815 :         GEN ppr = powiu(p,lambda>>1);
    1121        3815 :         F = ZX_Z_divexact(Fi,sqri(ppr));
    1122        3815 :         gel(M,1) = mulii(gel(M,1), ppr);
    1123        3815 :         gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0,gen_1,p,gen_0));
    1124             :       }
    1125             :     }
    1126             :     for(;;)
    1127       25186 :     {
    1128             :       GEN Fe, R;
    1129      222733 :       long j, lR, ep = ZX_pvalrem(F,p, &Fe);
    1130      222733 :       R = FpX_roots_mult(FpX_red(Fe, p), g+2-ep, p); lR = lg(R);
    1131      235226 :       for (j = 1; j<lR; j++)
    1132             :       {
    1133       37679 :         GEN c = Fp_center(gel(R,j), p, ps2);
    1134       37679 :         GEN Fi = ZX_affine(F,p,c);
    1135       37679 :         long lambda = ZX_pval(Fi,p);
    1136       37679 :         if (!test53(lambda,ep,g))
    1137             :         {
    1138       25186 :           GEN ppr = powiu(p,lambda>>1);
    1139       25186 :           F = ZX_Z_divexact(Fi, sqri(ppr));
    1140       25186 :           gel(M,1) = mulii(gel(M,1), ppr);
    1141       25186 :           gel(M,2) = ZM2_mul(gel(M,2), mkmat22(p,c,gen_0,gen_1));
    1142       25186 :           break;
    1143             :         }
    1144             :       }
    1145      222733 :       if (j==lR) break;
    1146             :     }
    1147             :   }
    1148       53038 :   return mkvec2(F, M);
    1149             : }
    1150             : 
    1151             : /* if inf=0, ignore point at infinity */
    1152             : static GEN
    1153        3080 : algo57bis(GEN F, long g, GEN p, long inf, long thr)
    1154             : {
    1155        3080 :   pari_sp av = avma;
    1156        3080 :   GEN vl = cgetg(3,t_VEC), Fe;
    1157        3080 :   long nl = 1, ep = ZX_pvalrem(F,p, &Fe);
    1158        3080 :   Fe = FpX_red(Fe, p);
    1159             :   {
    1160        3080 :     GEN R = FpX_roots_mult(Fe, thr-ep, p);
    1161        3080 :     long j, lR = lg(R);
    1162        5999 :     for (j = 1; j<lR; j++)
    1163             :     {
    1164        2919 :       GEN Fj = ZX_affine(F, p, gel(R,j));
    1165        2919 :       long lambda = ZX_pvalrem(Fj, p, &Fj);
    1166        2919 :       if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
    1167             :     }
    1168             :   }
    1169        3080 :   if (inf==1 && 2*g+2-degpol(Fe) >= thr-ep)
    1170             :   {
    1171           0 :     GEN Fj = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
    1172           0 :     long lambda = ZX_pvalrem(Fj, p, &Fj);
    1173           0 :     if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
    1174             :   }
    1175        3080 :   setlg(vl, nl);
    1176        3080 :   return gc_GEN(av,vl);
    1177             : }
    1178             : 
    1179             : static GEN
    1180        3185 : next_model(GEN G, long g, GEN p, long inf, long thr)
    1181             : {
    1182        3290 :   return equaliu(p,2) ? algo56bis(G, g,    inf, thr)
    1183        3290 :                       : algo57bis(G, g, p, inf, thr);
    1184             : }
    1185             : 
    1186             : static GEN
    1187        1498 : get_extremal_even(GEN F, GEN G, long g, GEN p, long *nb)
    1188             : {
    1189             :   while (1)
    1190        1274 :   {
    1191        1498 :     GEN Wi = next_model(G, g, p, 0, g+2);
    1192        1498 :     if (lg(Wi)==1) return F;
    1193        1365 :     F = gel(Wi,1); ++*nb;
    1194        1365 :     if (DEBUGLEVEL>1) err_printf("model %ld: %Ps\n", *nb, F);
    1195        1365 :     Wi = next_model(F, g, p, 0, g+1);
    1196        1365 :     if (lg(Wi)==1) return F;
    1197        1274 :     G = gel(Wi,1);
    1198             :   }
    1199             : }
    1200             : 
    1201             : static GEN
    1202           0 : get_extremal_odd(GEN F, long g, GEN p, long *nb)
    1203             : {
    1204             :   while (1)
    1205           0 :   {
    1206           0 :     GEN Wi = next_model(F, g, p, 0, g+2);
    1207           0 :     if (lg(Wi)==1) return F;
    1208           0 :     F = gel(Wi,1); ++*nb;
    1209           0 :     if (DEBUGLEVEL>1) err_printf("model %ld: %Ps\n", *nb, F);
    1210             :   }
    1211             : }
    1212             : 
    1213             : static GEN
    1214         357 : hyperellextremalmodels_nb(GEN F, long g, GEN p, long *nb)
    1215             : {
    1216         357 :   pari_sp av = avma;
    1217             :   GEN W, A, B;
    1218             :   long l;
    1219             : 
    1220         357 :   *nb = 1;
    1221         357 :   if (equaliu(p,2))
    1222             :   {
    1223          35 :     if (get_ep(F) > 0) retmkvec(gcopy(F));
    1224             :   } else
    1225             :   {
    1226         322 :     F = check_hyperell(F);
    1227         322 :     if (ZX_pval(F, p) > 0) return gc_GEN(av, mkvec(F));
    1228             :   }
    1229         322 :   if (DEBUGLEVEL>1) err_printf("model %ld: %Ps\n", *nb, F);
    1230         322 :   W = next_model(F, g, p, 1, odd(g)? g+2: g+1);
    1231         322 :   l = lg(W); if (l==1) return gc_GEN(av, mkvec(F));
    1232         210 :   if (odd(g))
    1233             :   {
    1234           0 :     *nb = l-1;
    1235           0 :     A = get_extremal_odd(gel(W,1), g, p, nb);
    1236           0 :     B = l==3 ? get_extremal_odd(gel(W,2), g, p, nb) : F;
    1237             :   }
    1238             :   else
    1239             :   {
    1240         210 :     A = get_extremal_even(F, gel(W,1), g, p, nb);
    1241         210 :     B = l==3 ? get_extremal_even(F, gel(W,2), g, p, nb) : F;
    1242             :   }
    1243         210 :   return gc_GEN(av, A == B? mkvec(A): mkvec2(A, B));
    1244             : }
    1245             : 
    1246             : static GEN
    1247         350 : hyperellextremalmodels_i(GEN F, long g, GEN p)
    1248             : {
    1249             :   long nb;
    1250         350 :   return hyperellextremalmodels_nb(F, g, p, &nb);
    1251             : }
    1252             : 
    1253             : GEN
    1254           7 : hyperellextremalmodels(GEN PQ, GEN p)
    1255             : {
    1256           7 :   pari_sp av = avma;
    1257           7 :   GEN H = check_hyperell(PQ), W, v;
    1258             :   long g, nb;
    1259           7 :   if (!H || signe(H)==0) pari_err_TYPE("hyperellextremalmodels",PQ);
    1260           7 :   if (typ(p)!=t_INT || signe(p)<=0) pari_err_TYPE("hyperellextremalmodels",p);
    1261           7 :   g = hyperellgenus(H);
    1262           7 :   W = hyperellminimalmodel(H,NULL,mkvec(p));
    1263           7 :   v = cgetg(3, t_VEC);
    1264           7 :   gel(v, 2) = hyperellextremalmodels_nb(W, g, p, &nb);
    1265           7 :   gel(v, 1) = stoi(nb);
    1266           7 :   return gc_upto(av, v);
    1267             : }
    1268             : 
    1269             : static GEN
    1270      303761 : RgX_RgM2_eval(GEN P, GEN A, GEN Bp, long d)
    1271             : {
    1272      303761 :   if (signe(P)==0)
    1273       79841 :     return P;
    1274             :   else
    1275             :   {
    1276      223920 :     long dP = degpol(P);
    1277      223920 :     GEN R = RgX_homogenous_evalpow(P, A, Bp);
    1278      223920 :     if (d > dP)
    1279       17298 :       R = gmul(R, gel(Bp,1+d-dP));
    1280      223920 :     return R;
    1281             :   }
    1282             : }
    1283             : 
    1284             : static GEN
    1285       53052 : minimalmodel_merge(GEN W2, GEN Modd, long g, long v)
    1286             : {
    1287       53052 :   GEN P = gel(W2,1), Q = gel(W2,2);
    1288       53052 :   GEN e = gel(Modd,1), M = gel(Modd,2);
    1289       53052 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1290       53052 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1291       53052 :   GEN Bp = gpowers(B, 2*g+2);
    1292       53052 :   long f = mod4(e)==1 ? 1: -1;
    1293       53052 :   GEN m = shifti(f > 0 ? subui(1,e): addui(1,e), -2);
    1294       53052 :   GEN  m24 = subii(shifti(m,1), shifti(sqri(m),2));
    1295       53052 :   P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
    1296       53052 :   Q = RgX_RgM2_eval(Q, A, Bp, g+1);
    1297       53052 :   P = ZX_Z_divexact(ZX_add(P, ZX_Z_mul(ZX_sqr(Q), m24)),sqri(e));
    1298       53052 :   if (f < 0) Q = ZX_neg(Q);
    1299       53052 :   return mkvec2(P,Q);
    1300             : }
    1301             : 
    1302             : static GEN
    1303      106090 : hyperell_redQ(GEN W)
    1304             : {
    1305      106090 :   GEN P = gel(W,1), Q = gel(W,2);
    1306      106090 :   GEN Pr, Qr = FpX_red(Q, gen_2);
    1307      106090 :   Pr = ZX_add(P, ZX_shifti(ZX_mul(ZX_sub(Q, Qr),ZX_add(Q, Qr)),-2));
    1308      106090 :   return mkvec2(Pr, Qr);
    1309             : }
    1310             : 
    1311             : static GEN
    1312       50735 : minimalmodel_getH(GEN W, GEN Qn, GEN e, GEN M, long g, long v)
    1313             : {
    1314       50735 :   GEN Q = gel(W,2);
    1315       50735 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1316       50735 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1317       50735 :   GEN Bp = gpowers(B, g+1);
    1318       50735 :   return ZX_shifti(ZX_sub(ZX_Z_mul(Qn,e),RgX_RgM2_eval(Q, A, Bp, g+1)), -1);
    1319             : }
    1320             : 
    1321             : static void
    1322       53094 : check_hyperell_Q(const char *fun, GEN *pW, GEN *pF)
    1323             : {
    1324       53094 :   GEN W = *pW, F = check_hyperell(W);
    1325             :   long v, g;
    1326       53094 :   if (!F || !signe(F) || !RgX_is_ZX(F)) pari_err_TYPE(fun, W);
    1327       53087 :   if (!signe(ZX_disc(F))) pari_err_DOMAIN(fun,"disc(W)","==",gen_0,W);
    1328       53080 :   v = varn(F); g = hyperellgenus(F);
    1329       53080 :   if (g == 0) pari_err_DOMAIN(fun, "genus", "=", gen_0, gen_0);
    1330       53066 :   if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
    1331             :   else
    1332             :   {
    1333       43610 :     GEN P = gel(W, 1), Q = gel(W, 2);
    1334       43610 :     if (typ(P)!=t_POL) P = scalarpol_shallow(P, v);
    1335       43610 :     if (typ(Q)!=t_POL) Q = scalarpol_shallow(Q, v);
    1336       43610 :     if (!RgX_is_ZX(P) || !RgX_is_ZX(Q)) pari_err_TYPE(fun,W);
    1337       43610 :     if (degpol(P) > 2*g+2) pari_err_DOMAIN(fun, "deg(P)", ">", utoi(2*g+2), P);
    1338       43610 :     if (degpol(Q) > g+1) pari_err_DOMAIN(fun, "deg(Q)", ">", utoi(g+1), Q);
    1339       43610 :     W = mkvec2(P, Q);
    1340             :   }
    1341       53066 :   *pW = W; *pF = F;
    1342       53066 : }
    1343             : 
    1344             : GEN
    1345       53052 : hyperellminimalmodel(GEN W, GEN *pM, GEN pr)
    1346             : {
    1347       53052 :   pari_sp av = avma;
    1348             :   GEN Wr, F, WM2, F2, W2, M2, Modd, Wf, ef, Mf, Hf;
    1349             :   long g, v;
    1350       53052 :   check_hyperell_Q("hyperellminimalmodel",&W, &F);
    1351       53052 :   if (pr && (!is_vec_t(typ(pr)) || !RgV_is_ZV(pr)))
    1352          14 :     pari_err_TYPE("hyperellminimalmodel",pr);
    1353       53038 :   g = hyperellgenus(F); v = varn(F);
    1354       53038 :   Wr = hyperell_redQ(W);
    1355       53038 :   if (!pr || RgV_isin(pr, gen_2))
    1356             :   {
    1357       50805 :     WM2 = algo56(Wr,g); W2 = gel(WM2, 1); M2 = gel(WM2, 2);
    1358       50805 :     F2 = check_hyperell(W2);
    1359             :   }
    1360             :   else
    1361             :   {
    1362        2233 :     W2 = Wr; F2 = F; M2 = mkvec2(gen_1, matid(2));
    1363             :   }
    1364       53038 :   Modd = gel(algo57(F2, g, pr), 2);
    1365       53038 :   Wf = hyperell_redQ(minimalmodel_merge(W2, Modd, g, v));
    1366       53038 :   if (!pM) return gc_GEN(av, Wf);
    1367       50721 :   ef = mulii(gel(M2,1), gel(Modd,1));
    1368       50721 :   Mf = ZM2_mul(gel(M2,2), gel(Modd,2));
    1369       50721 :   Hf = minimalmodel_getH(W, gel(Wf,2), ef, Mf, g, v);
    1370       50721 :   *pM =  mkvec3(ef, Mf, Hf);
    1371       50721 :   return gc_all(av, 2, &Wf, pM);
    1372             : }
    1373             : 
    1374             : GEN
    1375          14 : hyperellminimaldisc(GEN W, GEN pr)
    1376             : {
    1377          14 :   pari_sp av = avma;
    1378          14 :   GEN C = hyperellminimalmodel(W, NULL, pr);
    1379          14 :   return gc_INT(av, hyperelldisc(C));
    1380             : }
    1381             : 
    1382             : static GEN
    1383          35 : redqfbsplit(GEN a, GEN b, GEN c, GEN d)
    1384             : {
    1385          35 :   GEN p = subii(d,b), q = shifti(a,1);
    1386          35 :   GEN U, Q, u, v, w = bezout(p, q, &u, &v);
    1387             : 
    1388          35 :   if (!equali1(w)) { p = diviiexact(p, w); q = diviiexact(q, w); }
    1389          35 :   U = mkmat22(p, negi(v), q, u);
    1390          35 :   Q = qfb3_SL2_apply(mkvec3(a,b,c), U);
    1391          35 :   b = gel(Q, 2); c = gel(Q,3);
    1392          35 :   if (signe(b) < 0) gel(U,2) = mkcol2(v, negi(u));
    1393          35 :   gel(U,2) = ZC_lincomb(gen_1, truedivii(negi(c), d), gel(U,2), gel(U,1));
    1394          35 :   return U;
    1395             : }
    1396             : 
    1397             : static GEN
    1398       16386 : polreduce(GEN P, GEN M)
    1399             : {
    1400       16386 :   long v = varn(P), dP = degpol(P), d = odd(dP) ? dP+1: dP;
    1401       16386 :   GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1402       16386 :   GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1403       16386 :   return RgX_RgM2_eval(P, A, gpowers(B, d), d);
    1404             : }
    1405             : 
    1406             : /* assume deg(P) > 2 */
    1407             : static GEN
    1408        8193 : red_Cremona_Stoll(GEN P, GEN *pM)
    1409             : {
    1410             :   GEN q1, q2, q3, M, R;
    1411        8193 :   long i, prec = nbits2prec(2*gexpo(P)) + EXTRAPRECWORD, d = degpol(P);
    1412        8193 :   GEN dP = ZX_deriv(P);
    1413             :   for (;;)
    1414           0 :   {
    1415        8193 :     GEN r = QX_complex_roots(P, prec);
    1416        8193 :     q1 = gen_0; q2 = gen_0; q3 = gen_0;
    1417       41000 :     for (i = 1; i <= d; i++)
    1418             :     {
    1419       32807 :       GEN ri = gel(r,i);
    1420       32807 :       GEN s = ginv(gabs(RgX_cxeval(dP,ri,NULL), prec));
    1421       32807 :       if (d!=4) s = gpow(s, gdivgs(gen_2,d-2), prec);
    1422       32807 :       q1 = gadd(q1, s);
    1423       32807 :       q2 = gsub(q2, gmul(real_i(ri), s));
    1424       32807 :       q3 = gadd(q3, gmul(gnorm(ri), s));
    1425             :     }
    1426        8193 :     M = lllgram(mkmat22(q1,q2,q2,q3));
    1427        8193 :     if (M && lg(M) == 3) break;
    1428           0 :     prec = precdbl(prec);
    1429             :   }
    1430        8193 :   R = polreduce(P, M);
    1431        8193 :   *pM = M;
    1432        8193 :   return R;
    1433             : }
    1434             : 
    1435             : /* assume deg(P) > 2 */
    1436             : GEN
    1437        8193 : ZX_hyperellred(GEN P, GEN *pM)
    1438             : {
    1439        8193 :   pari_sp av = avma;
    1440        8193 :   long d = degpol(P);
    1441             :   GEN q1, q2, q3, D, vD;
    1442        8193 :   GEN a = gel(P,d+2), b = gel(P,d+1), c = gel(P, d);
    1443             :   GEN M, R, M2;
    1444             : 
    1445        8193 :   q1 = muliu(sqri(a), d);
    1446        8193 :   q2 = shifti(mulii(a,b), 1);
    1447        8193 :   q3 = subii(sqri(b), shifti(mulii(a,c), 1));
    1448        8193 :   D = gcdii(gcdii(q1, q2), q3);
    1449        8193 :   if (!equali1(D))
    1450             :   {
    1451        8172 :     q1 = diviiexact(q1, D);
    1452        8172 :     q2 = diviiexact(q2, D);
    1453        8172 :     q3 = diviiexact(q3, D);
    1454             :   }
    1455        8193 :   D = qfb_disc3(q1, q2, q3);
    1456        8193 :   if (!signe(D))
    1457          49 :     M = mkmat22(gen_1, truedivii(negi(q2),shifti(q1,1)), gen_0, gen_1);
    1458        8144 :   else if (issquareall(D,&vD))
    1459          35 :     M = redqfbsplit(q1, q2, q3, vD);
    1460             :   else
    1461        8109 :     M = gel(qfbredsl2(mkqfb(q1,q2,q3,D), NULL), 2);
    1462        8193 :   R = red_Cremona_Stoll(polreduce(P, M), &M2);
    1463        8193 :   if (pM) *pM = gmul(M, M2);
    1464        8193 :   return gc_all(av, pM ? 2: 1, &R, pM);
    1465             : }
    1466             : 
    1467             : GEN
    1468          42 : hyperellred(GEN W, GEN *pM)
    1469             : {
    1470          42 :   pari_sp av = avma;
    1471             :   long g, v;
    1472             :   GEN F, M, Wf, Hf;
    1473          42 :   check_hyperell_Q("hyperellred", &W, &F);
    1474          14 :   g = hyperellgenus(F); v = varn(F);
    1475          14 :   (void) ZX_hyperellred(F, &M);
    1476          14 :   Wf = hyperell_redQ(minimalmodel_merge(W, mkvec2(gen_1, M), g, v));
    1477          14 :   Hf = minimalmodel_getH(W, gel(Wf,2), gen_1, M, g, v);
    1478          14 :   if (pM) *pM = mkvec3(gen_1, M, Hf);
    1479          14 :   return gc_all(av, pM ? 2: 1, &Wf, pM);
    1480             : }
    1481             : 
    1482             : static void
    1483       65296 : check_hyperell_Rg(const char *fun, GEN *pW, GEN *pF)
    1484             : {
    1485       65296 :   GEN W = *pW, F = check_hyperell(W);
    1486             :   long v;
    1487       65296 :   if (!F)
    1488           7 :     pari_err_TYPE(fun, W);
    1489       65289 :   if (degpol(F) <= 0) pari_err_CONSTPOL(fun);
    1490       65282 :   v = varn(F);
    1491       65282 :   if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
    1492             :   else
    1493             :   {
    1494       65254 :     GEN P = gel(W, 1), Q = gel(W, 2);
    1495       65254 :     long g = hyperellgenus(F);
    1496       65254 :     if( typ(P)!=t_POL) P = scalarpol(P, v);
    1497       65254 :     if( typ(Q)!=t_POL) Q = scalarpol(Q, v);
    1498       65254 :     if (degpol(P) > 2*g+2)
    1499           0 :       pari_err_DOMAIN(fun, "poldegree(P)", ">", utoi(2*g+2), P);
    1500       65254 :     if (degpol(Q) > g+1)
    1501           0 :       pari_err_DOMAIN(fun, "poldegree(Q)", ">", utoi(g+1), Q);
    1502             : 
    1503       65254 :     W = mkvec2(P, Q);
    1504             :   }
    1505       65282 :   if (pF) *pF = F;
    1506       65282 :   *pW = W;
    1507       65282 : }
    1508             : 
    1509             : static void
    1510       65282 : check_hyperell_vc(const char *fun, GEN C, long v, GEN *e, GEN *M, GEN *H)
    1511             : {
    1512       65282 :   if (typ(C) != t_VEC || lg(C) != 4) pari_err_TYPE(fun,C);
    1513       65275 :   *e = gel(C,1); *M = gel(C,2); *H = gel(C,3);
    1514       65275 :   if (typ(*M) != t_MAT || lg(*M) != 3 || lgcols(*M) != 3) pari_err_TYPE(fun,C);
    1515       65268 :   if (typ(*H)!=t_POL || varncmp(varn(*H),v) > 0) *H = scalarpol_shallow(*H,v);
    1516       65268 : }
    1517             : 
    1518             : GEN
    1519       65296 : hyperellchangecurve(GEN W, GEN C)
    1520             : {
    1521       65296 :   pari_sp av = avma;
    1522             :   GEN F, P, Q, A, B, Bp, e, M, H;
    1523             :   long g, v;
    1524       65296 :   check_hyperell_Rg("hyperellchangecurve",&W,&F);
    1525       65282 :   P = gel(W,1); Q = gel(W,2);
    1526       65282 :   g = hyperellgenus(F); v = varn(F);
    1527       65282 :   check_hyperell_vc("hyperellchangecurve", C, v, &e, &M, &H);
    1528       65268 :   if (varncmp(gvar(M),v) <= 0)
    1529           0 :     pari_err_PRIORITY("hyperellchangecurve",M,"<=",v);
    1530       65268 :   A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
    1531       65268 :   B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
    1532       65268 :   Bp = gpowers(B, 2*g+2);
    1533       65268 :   P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
    1534       65268 :   Q = RgX_RgM2_eval(Q, A, Bp, g+1);
    1535       65268 :   P = RgX_Rg_div(RgX_sub(P, RgX_mul(H,RgX_add(Q,H))), gsqr(e));
    1536       65268 :   Q = RgX_Rg_div(RgX_add(Q, RgX_mul2n(H,1)), e);
    1537       65268 :   return gc_GEN(av, mkvec2(P,Q));
    1538             : }
    1539             : 
    1540             : /****************************************************************************/
    1541             : /***                                                                      ***/
    1542             : /***                        genus2charpoly                                ***/
    1543             : /***                                                                      ***/
    1544             : /****************************************************************************/
    1545             : 
    1546             : /* Half stable reduction */
    1547             : 
    1548             : static long
    1549         588 : Zst_val(GEN P, GEN f, GEN p, long vt, GEN *pR)
    1550             : {
    1551         588 :   pari_sp av = avma;
    1552         588 :   long v = varn(P);
    1553             :   while(1)
    1554        1260 :   {
    1555        1848 :     long i, j, dm = LONG_MAX;
    1556        1848 :     GEN Pm = NULL;
    1557        1848 :     long dP = degpol(P);
    1558        7532 :     for (i = 0; i <= minss(dP, dm); i++)
    1559             :     {
    1560        5684 :       GEN Py = gel(P, i+2);
    1561        5684 :       if (signe(Py))
    1562             :       {
    1563        4186 :         if (typ(Py)==t_POL)
    1564             :         {
    1565        3864 :           long dPy = degpol(Py);
    1566       12502 :           for (j = 0; j <= minss(dPy, dm-i); j++)
    1567             :           {
    1568        8638 :             GEN c = gel(Py, j+2);
    1569        8638 :             if (signe(c))
    1570             :             {
    1571        3556 :                 if (i+j < dm)
    1572             :                 {
    1573        1848 :                   dm = i+j;
    1574        1848 :                   Pm = monomial(gen_1, dm, v);
    1575        1848 :                   gel(Pm,dm+2) = gen_0;
    1576             :                 }
    1577        3556 :                 gel(Pm,i+2) = c;
    1578             :             }
    1579             :           }
    1580             :         } else
    1581             :         {
    1582         322 :           if (i < dm)
    1583             :           {
    1584          77 :             dm = i;
    1585          77 :             Pm = monomial(Py, dm, v);
    1586             :           }
    1587             :           else
    1588         245 :             gel(Pm, i+2) = Py;
    1589             :         }
    1590             :       }
    1591             :     }
    1592        1848 :     Pm = RgX_renormalize(Pm);
    1593        1848 :     if (ZX_pval(Pm,p)==0)
    1594             :     {
    1595         588 :       *pR = gc_GEN(av, P);
    1596         588 :       return dm;
    1597             :     }
    1598        1260 :     Pm = RgX_homogenize_deg(Pm, dm, vt);
    1599        1260 :     P = gadd(gsub(P, Pm), gmul(f, ZXX_Z_divexact(Pm, p)));
    1600             :   }
    1601             : }
    1602             : 
    1603             : static long
    1604         588 : Zst_normval(GEN P, GEN f, GEN p, long vt, GEN *pR)
    1605             : {
    1606         588 :   long v = Zst_val(P, f, p, vt, pR);
    1607         588 :   long e = RgX_val(*pR)>>1;
    1608         588 :   if (e > 0)
    1609             :   {
    1610           0 :     v -= 2*e;
    1611           0 :     *pR = RgX_shift(*pR, -2*e);
    1612             :   }
    1613         588 :   return v;
    1614             : }
    1615             : 
    1616             : static GEN
    1617        1176 : RgXY_swapsafe(GEN P, long v1, long v2)
    1618             : {
    1619        1176 :   if (varn(P)==v2)
    1620             :   {
    1621          77 :     P = shallowcopy(P); setvarn(P,v1); return P;
    1622             :   } else
    1623        1099 :     return RgXY_swap(P, RgXY_degreex(P), v2);
    1624             : }
    1625             : 
    1626             : static GEN
    1627         588 : Zst_red1(GEN P, GEN f, GEN p, long vt)
    1628             : {
    1629         588 :   pari_sp av = avma;
    1630             :   GEN r, f1, f2, P1, P2;
    1631         588 :   long vs = varn(P);
    1632         588 :   long w = Zst_normval(P, f, p, vt, &r), ww = w-odd(w);
    1633         588 :   GEN st = monomial(pol_x(vt), 1, vs);
    1634         588 :   f1 = gsubst(f, vt, st);
    1635         588 :   P1 = gsubst(gdiv(r, monomial(gen_1,ww,vs)),vt,st);
    1636         588 :   f2 = gsubst(f, vs, st);
    1637         588 :   P2 = gsubst(gdiv(r, monomial(gen_1,ww,vt)),vs,st);
    1638         588 :   f2 = RgXY_swapsafe(f2, vs, vt);
    1639         588 :   P2 = RgXY_swapsafe(P2, vs, vt);
    1640         588 :   return gc_GEN(av, mkvec4(P1, f1, P2, f2));
    1641             : }
    1642             : 
    1643             : static GEN
    1644        1176 : Zst_reduce(GEN P, GEN p, long vt, long *pv)
    1645             : {
    1646             :   GEN C;
    1647        1176 :   long v = RgX_val(P);
    1648        1176 :   *pv = v + ZXX_pvalrem(RgX_shift(P, -v), p, &P);
    1649        1176 :   C = constant_coeff(P);
    1650        1176 :   C = typ(C) == t_POL ? C: scalarpol_shallow(C, vt);
    1651        1176 :   return FpX_red(C, p);
    1652             : }
    1653             : 
    1654             : static GEN
    1655         588 : Zst_red3(GEN C, GEN p, long vt)
    1656             : {
    1657             :   while(1)
    1658         511 :   {
    1659         588 :     GEN P1 = gel(C,1) ,f1 = gel(C,2), Poo = gel(C,3), foo= gel(C,4);
    1660             :     long e;
    1661         588 :     GEN Qoop = Zst_reduce(Poo, p, vt, &e), Qp, R;
    1662         588 :     if (RgX_val(Qoop) >= 3-e)
    1663             :     {
    1664           0 :       C = Zst_red1(Poo, foo, p, vt);
    1665         511 :       continue;
    1666             :     }
    1667         588 :     Qp = Zst_reduce(P1, p, vt, &e);
    1668         588 :     R = FpX_roots_mult(Qp, 3-e, p);
    1669         588 :     if (lg(R) > 1)
    1670         511 :     {
    1671         511 :       GEN xz = deg1pol_shallow(gen_1, gel(R,1), vt);
    1672         511 :       C = Zst_red1(gsubst(P1, vt, xz), gsubst(f1, vt, xz), p, vt);
    1673         511 :       continue;
    1674             :     }
    1675          77 :     return Qp;
    1676             :   }
    1677             : }
    1678             : 
    1679             : static GEN
    1680          77 : genus2_halfstablemodel_i(GEN P, GEN p, long vt)
    1681             : {
    1682             :   GEN Qp, R, Poo, Qoop;
    1683          77 :   long e = ZX_pvalrem(P, p, &Qp);
    1684          77 :   R = FpX_roots_mult(FpX_red(Qp,p), 4-e, p);
    1685          77 :   if (lg(R) > 1)
    1686             :   {
    1687          77 :     GEN C = Zst_red1(ZX_Z_translate(P, gel(R,1)), pol_x(vt), p, vt);
    1688          77 :     return Zst_red3(C, p, vt);
    1689             :   }
    1690           0 :   Poo = RgXn_recip_shallow(P, 7);
    1691           0 :   e = ZX_pvalrem(Poo, p, &Qoop);
    1692           0 :   Qoop = FpX_red(Qoop,p);
    1693           0 :   if (RgX_val(Qoop)>=4-e)
    1694             :   {
    1695           0 :     GEN C = Zst_red1(Poo, pol_x(vt), p, vt);
    1696           0 :     return Zst_red3(C, p, vt);
    1697             :   }
    1698           0 :   return gcopy(P);
    1699             : }
    1700             : 
    1701             : static GEN
    1702          77 : genus2_halfstablemodel(GEN P, GEN p)
    1703             : {
    1704          77 :   pari_sp av = avma;
    1705          77 :   long vt = fetch_var(), vs = varn(P);
    1706          77 :   GEN S = genus2_halfstablemodel_i(P, p, vt);
    1707          77 :   setvarn(S, vs); delete_var();
    1708          77 :   return gc_GEN(av, S);
    1709             : }
    1710             : 
    1711             : /* semi-stable reduction */
    1712             : 
    1713             : static GEN
    1714         532 : genus2_redmodel(GEN P, GEN p)
    1715             : {
    1716             :   GEN LP, U, F;
    1717             :   long i, k, r;
    1718         532 :   if (degpol(P) < 0) return mkvec2(cgetg(1, t_COL), P);
    1719         497 :   F = FpX_factor_squarefree(P, p);
    1720         497 :   r = lg(F); U = NULL;
    1721        1967 :   for (i = k = 1; i < r; i++)
    1722             :   {
    1723        1470 :     GEN f = gel(F,i);
    1724        1470 :     long df = degpol(f);
    1725        1470 :     if (!df) continue;
    1726         777 :     if (odd(i)) U = U? FpX_mul(U, f, p): f;
    1727         777 :     if (i > 1) gel(F,k++) = df == 1? mkcol(f): gel(FpX_factor(f, p), 1);
    1728             :   }
    1729         497 :   LP = leading_coeff(P);
    1730         497 :   if (!U)
    1731         112 :     U = scalarpol_shallow(LP, varn(P));
    1732             :   else
    1733             :   {
    1734         385 :     GEN LU = leading_coeff(U);
    1735         385 :     if (!equalii(LU, LP)) U = FpX_Fp_mul(U, Fp_div(LP, LU, p), p);
    1736             :   }
    1737         497 :   setlg(F,k); if (k > 1) F = shallowconcat1(F);
    1738         497 :   return mkvec2(F, U);
    1739             : }
    1740             : 
    1741             : static GEN
    1742        2926 : xdminusone(long d)
    1743             : {
    1744        2926 :   return gsub(pol_xn(d, 0),gen_1);
    1745             : }
    1746             : 
    1747             : static GEN
    1748         140 : ellfromeqncharpoly(GEN P, GEN Q, GEN p)
    1749             : {
    1750             :   long v;
    1751             :   GEN E, F, t, y;
    1752         140 :   v = fetch_var();
    1753         140 :   y = pol_x(v);
    1754         140 :   F = gsub(gadd(ZX_sqr(y), gmul(y, Q)), P);
    1755         140 :   E = ellinit(ellfromeqn(F), p, DEFAULTPREC);
    1756         140 :   delete_var();
    1757         140 :   t = ellcharpoly(E, p);
    1758         140 :   obj_free(E);
    1759         140 :   return t;
    1760             : }
    1761             : 
    1762             : static GEN
    1763           0 : nfellcharpoly(GEN e, GEN T, GEN p)
    1764             : {
    1765             :   GEN nf, E, t;
    1766           0 :   e = shallowcopy(e);
    1767           0 :   nf = nfinit(mkvec2(T, mkvec(p)), DEFAULTPREC);
    1768             :   while(1)
    1769             :   {
    1770           0 :     E = ellinit(e, nf, DEFAULTPREC);
    1771           0 :     if (lg(E)!=1) break;
    1772           0 :     gel(e,5) = gadd(gel(e,5), p);
    1773             :   }
    1774           0 :   t = elleulerf(E, p);
    1775           0 :   obj_free(E);
    1776           0 :   return RgX_recip(ginv(t));
    1777             : }
    1778             : 
    1779             : static GEN
    1780           0 : genus2_red5(GEN P, GEN T, GEN p)
    1781             : {
    1782           0 :   long vx = varn(P), vy = varn(T);
    1783           0 :   GEN f = shallowcopy(T), pi = shifti(p,-1);
    1784           0 :   setvarn(f, vx);
    1785             :   while(1)
    1786           0 :   {
    1787             :     GEN Pr, R, r, Rs;
    1788           0 :     long v = ZXX_pvalrem(P, p, &Pr);
    1789           0 :     R = FpXQX_roots_mult(Pr, 2-v, T, p);
    1790           0 :     if (lg(R)==1) return P;
    1791           0 :     r = FpX_center(gel(R,1), p, pi);
    1792           0 :     Pr = RgX_affine(P, p, r);
    1793           0 :     setvarn(r, vx);
    1794           0 :     f = RgX_Rg_div(gsub(f, r), p);
    1795           0 :     Rs = RgX_rem(RgXY_swap(Pr, 3, vy), gsub(f, pol_x(vy)));
    1796           0 :     Pr = RgXY_swap(Rs, 3, vy);
    1797           0 :     if (ZXX_pvalrem(Pr, sqri(p), &Pr)==0) return P;
    1798           0 :     P = Pr;
    1799             :   }
    1800             : }
    1801             : 
    1802             : static GEN
    1803         350 : genus2_type5(GEN P, GEN p)
    1804             : {
    1805             :   GEN E, F, T, a, a2, Q;
    1806             :   long v;
    1807         350 :   if (equaliu(p, 2))
    1808          28 :     (void) ZXX_pvalrem(P, sqri(p), &P);
    1809         350 :   (void) ZX_pvalrem(P, p, &F);
    1810         350 :   F = FpX_red(F, p);
    1811         350 :   if (degpol(F) < 1) return NULL;
    1812         343 :   F = FpX_factor(F, p);
    1813         343 :   if (mael(F,2,1) != 3 || degpol(gmael(F,1,1)) != 2) return NULL;
    1814           0 :   T = gmael(F, 1, 1);
    1815           0 :   v = fetch_var_higher();
    1816           0 :   Q = RgV_to_RgX(ZX_digits(P, T), v);
    1817           0 :   Q = genus2_red5(Q, T, p);
    1818           0 :   a = gel(Q,5); a2 = ZX_sqr(a);
    1819           0 :   E = mkvec5(gen_0, gel(Q,4), gen_0, ZX_mul(gel(Q,3),a), ZX_mul(gel(Q,2),a2));
    1820           0 :   delete_var();
    1821           0 :   return nfellcharpoly(E, T, p);
    1822             : }
    1823             : 
    1824             : /* Assume P has semistable reduction at p */
    1825             : static GEN
    1826         532 : genus2_eulerfact_semistable(GEN P, GEN p)
    1827             : {
    1828         532 :   GEN Pp = FpX_red(P, p);
    1829         532 :   GEN GU = genus2_redmodel(Pp, p);
    1830         532 :   long d = 6-degpol(Pp), v = d/2, w = odd(d);
    1831             :   GEN abe, tor;
    1832         532 :   GEN ki, kp = pol_1(0), kq = pol_1(0);
    1833         532 :   GEN F = gel(GU,1), Q = gel(GU,2);
    1834         532 :   long dQ = degpol(Q), lF = lg(F)-1;
    1835             : 
    1836           7 :   abe = dQ >= 5 ? hyperellcharpoly(gmul(Q,gmodulo(gen_1,p)))
    1837        1057 :       : dQ >= 3 ? ellfromeqncharpoly(Q,gen_0,p)
    1838         525 :                 : pol_1(0);
    1839         420 :   ki = dQ != 0 ? xdminusone(1)
    1840         644 :               : Fp_issquare(gel(Q,2),p) ? ZX_sqr(xdminusone(1))
    1841         112 :                                         : xdminusone(2);
    1842         532 :   if (lF)
    1843             :   {
    1844             :     long i;
    1845        1064 :     for(i=1; i <= lF; i++)
    1846             :     {
    1847         616 :       GEN Fi = gel(F, i);
    1848         616 :       long d = degpol(Fi);
    1849         616 :       GEN e = FpX_rem(Q, Fi, p);
    1850         980 :       GEN kqf = lgpol(e)==0 ? xdminusone(d):
    1851         714 :                 FpXQ_issquare(e, Fi, p) ? ZX_sqr(xdminusone(d))
    1852         364 :                                         : xdminusone(2*d);
    1853         616 :       kp = gmul(kp, xdminusone(d));
    1854         616 :       kq = gmul(kq, kqf);
    1855             :     }
    1856             :   }
    1857         532 :   if (v)
    1858             :   {
    1859         245 :     GEN kqoo = w==1 ? xdminusone(1):
    1860           0 :                Fp_issquare(leading_coeff(Q), p)? ZX_sqr(xdminusone(1))
    1861           0 :                                               : xdminusone(2);
    1862         245 :     kp = gmul(kp, xdminusone(1));
    1863         245 :     kq = gmul(kq, kqoo);
    1864             :   }
    1865         532 :   tor = RgX_div(ZX_mul(xdminusone(1), kq), ZX_mul(ki, kp));
    1866         532 :   return ZX_mul(abe, tor);
    1867             : }
    1868             : 
    1869             : GEN
    1870         945 : genus2_eulerfact(GEN P, GEN p, long ra, long rt)
    1871             : {
    1872         945 :   pari_sp av = avma;
    1873             :   GEN W, R, E;
    1874         945 :   long d = 2*ra+rt;
    1875         945 :   if (d == 0) return pol_1(0);
    1876         322 :   R = genus2_type5(P, p);
    1877         322 :   if (R) return R;
    1878         322 :   W = hyperellextremalmodels_i(P, 2, p);
    1879         322 :   if (lg(W) < 3)
    1880             :   {
    1881         189 :     GEN F = genus2_eulerfact_semistable(P,p);
    1882         189 :     if (degpol(F)!=d)
    1883             :     {
    1884          77 :       GEN S = genus2_halfstablemodel(P, p);
    1885          77 :       F = genus2_eulerfact_semistable(S, p);
    1886          77 :       if (degpol(F)!=d) pari_err_BUG("genus2charpoly");
    1887             :     }
    1888         189 :     return F;
    1889             :   }
    1890         133 :   E =  gmul(genus2_eulerfact_semistable(gel(W,1),p),
    1891         133 :             genus2_eulerfact_semistable(gel(W,2),p));
    1892         133 :   return gc_upto(av, E);
    1893             : }
    1894             : 
    1895             : /*   p = 2  */
    1896             : 
    1897             : static GEN
    1898          28 : F2x_genus2_find_trans(GEN P, GEN Q, GEN F)
    1899             : {
    1900          28 :   pari_sp av = avma;
    1901          28 :   long i, d = F2x_degree(F), v = P[1];
    1902             :   GEN M, C, V;
    1903          28 :   M = cgetg(d+1, t_MAT);
    1904          84 :   for (i=1; i<=d; i++)
    1905             :   {
    1906          56 :     GEN Mi = F2x_rem(F2x_add(F2x_shift(Q,i-1), monomial_F2x(2*i-2,v)), F);
    1907          56 :     gel(M,i) = F2x_to_F2v(Mi, d);
    1908             :   }
    1909          28 :   C = F2x_to_F2v(F2x_rem(P, F), d);
    1910          28 :   V = F2m_F2c_invimage(M, C);
    1911          28 :   return gc_leaf(av, F2v_to_F2x(V, v));
    1912             : }
    1913             : 
    1914             : static GEN
    1915          42 : F2x_genus2_trans(GEN P, GEN Q, GEN H)
    1916             : {
    1917          42 :   return F2x_add(P,F2x_add(F2x_mul(H,Q), F2x_sqr(H)));
    1918             : }
    1919             : 
    1920             : static GEN
    1921         105 : F2x_genus_redoo(GEN P, GEN Q, long k)
    1922             : {
    1923         105 :   if (F2x_degree(P)==2*k)
    1924             :   {
    1925          21 :     long c = F2x_coeff(P,2*k-1), dQ = F2x_degree(Q);
    1926          21 :     if ((dQ==k-1 && c==1) || (dQ<k-1 && c==0))
    1927          14 :      return F2x_genus2_trans(P, Q, monomial_F2x(k, P[1]));
    1928             :   }
    1929          91 :   return P;
    1930             : }
    1931             : 
    1932             : static GEN
    1933          56 : F2x_pseudodisc(GEN P, GEN Q)
    1934             : {
    1935          56 :   GEN dP = F2x_deriv(P), dQ = F2x_deriv(Q);
    1936          56 :   return F2x_gcd(Q, F2x_add(F2x_mul(P, F2x_sqr(dQ)), F2x_sqr(dP)));
    1937             : }
    1938             : 
    1939             : static GEN
    1940          35 : F2x_genus_red(GEN P, GEN Q)
    1941             : {
    1942             :   long dP, dQ;
    1943             :   GEN F, FF;
    1944          35 :   P = F2x_genus_redoo(P, Q, 3);
    1945          35 :   P = F2x_genus_redoo(P, Q, 2);
    1946          35 :   P = F2x_genus_redoo(P, Q, 1);
    1947          35 :   dP = F2x_degree(P);
    1948          35 :   dQ = F2x_degree(Q);
    1949          35 :   FF = F = F2x_pseudodisc(P,Q);
    1950          56 :   while(F2x_degree(F)>0)
    1951             :   {
    1952          21 :     GEN M = gel(F2x_factor(F),1);
    1953          21 :     long i, l = lg(M);
    1954          49 :     for(i=1; i<l; i++)
    1955             :     {
    1956          28 :       GEN R = F2x_sqr(gel(M,i));
    1957          28 :       GEN H = F2x_genus2_find_trans(P, Q, R);
    1958          28 :       P = F2x_div(F2x_genus2_trans(P, Q, H), R);
    1959          28 :       Q = F2x_div(Q, gel(M,i));
    1960             :     }
    1961          21 :     F = F2x_pseudodisc(P, Q);
    1962             :   }
    1963          35 :   return mkvec4(P,Q,FF,mkvecsmall2(dP,dQ));
    1964             : }
    1965             : 
    1966             : /* Number of solutions of x^2+b*x+c */
    1967             : static long
    1968          21 : F2xqX_quad_nbroots(GEN b, GEN c, GEN T)
    1969             : {
    1970          21 :   if (lgpol(b) > 0)
    1971             :   {
    1972          14 :     GEN d = F2xq_div(c, F2xq_sqr(b, T), T);
    1973          14 :     return F2xq_trace(d, T)? 0: 2;
    1974             :   }
    1975             :   else
    1976           7 :     return 1;
    1977             : }
    1978             : 
    1979             : static GEN
    1980          35 : genus2_eulerfact2_semistable(GEN PQ)
    1981             : {
    1982          35 :   GEN V = F2x_genus_red(ZX_to_F2x(gel(PQ, 1)), ZX_to_F2x(gel(PQ, 2)));
    1983          35 :   GEN P = gel(V, 1), Q = gel(V, 2);
    1984          35 :   GEN F = gel(V, 3), v = gel(V, 4);
    1985             :   GEN abe, tor;
    1986          35 :   GEN ki, kp = pol_1(0), kq = pol_1(0);
    1987          35 :   long dP = F2x_degree(P), dQ = F2x_degree(Q), d = maxss(dP, 2*dQ);
    1988          35 :   if (!lgpol(F)) return pol_1(0);
    1989          35 :   ki = dQ!=0 || dP>0 ? xdminusone(1):
    1990           7 :       dP==-1 ? ZX_sqr(xdminusone(1)): xdminusone(2);
    1991          56 :   abe = d>=5? hyperellcharpoly(gmul(PQ,gmodulss(1,2))):
    1992          28 :         d>=3? ellfromeqncharpoly(F2x_to_ZX(P), F2x_to_ZX(Q), gen_2):
    1993          14 :         pol_1(0);
    1994          28 :   if (lgpol(F))
    1995             :   {
    1996          28 :     GEN M = gel(F2x_factor(F), 1);
    1997          28 :     long i, lF = lg(M)-1;
    1998          49 :     for(i=1; i <= lF; i++)
    1999             :     {
    2000          21 :       GEN Fi = gel(M, i);
    2001          21 :       long d = F2x_degree(Fi);
    2002          21 :       long nb  = F2xqX_quad_nbroots(F2x_rem(Q, Fi), F2x_rem(P, Fi), Fi);
    2003          35 :       GEN kqf = nb==1 ? xdminusone(d):
    2004           0 :                 nb==2 ? ZX_sqr(xdminusone(d))
    2005          14 :                       : xdminusone(2*d);
    2006          21 :       kp = gmul(kp, xdminusone(d));
    2007          21 :       kq = gmul(kq, kqf);
    2008             :     }
    2009             :   }
    2010          28 :   if (maxss(v[1],2*v[2])<5)
    2011             :   {
    2012          28 :     GEN kqoo = v[1]>2*v[2] ? xdminusone(1):
    2013           0 :                v[1]<2*v[2] ? ZX_sqr(xdminusone(1))
    2014           7 :                            : xdminusone(2);
    2015          21 :     kp = gmul(kp, xdminusone(1));
    2016          21 :     kq = gmul(kq, kqoo);
    2017             :   }
    2018          28 :   tor = RgX_div(ZX_mul(xdminusone(1),kq), ZX_mul(ki, kp));
    2019          28 :   return ZX_mul(abe, tor);
    2020             : }
    2021             : 
    2022             : GEN
    2023          28 : genus2_eulerfact2(GEN F, GEN PQ)
    2024             : {
    2025          28 :   pari_sp av = avma;
    2026          28 :   GEN W, R = genus2_type5(F, gen_2), E;
    2027          28 :   if (R) return R;
    2028          28 :   W = hyperellextremalmodels_i(PQ, 2, gen_2);
    2029          28 :   if (lg(W) < 3) return genus2_eulerfact2_semistable(PQ);
    2030           7 :   E = gmul(genus2_eulerfact2_semistable(gel(W,1)),
    2031           7 :            genus2_eulerfact2_semistable(gel(W,2)));
    2032           7 :   return gc_upto(av, E);
    2033             : }
    2034             : 
    2035             : GEN
    2036         889 : genus2charpoly(GEN G, GEN p)
    2037             : {
    2038         889 :   pari_sp av = avma;
    2039         889 :   GEN gr = genus2red(G, p), F;
    2040         889 :   GEN PQ = gel(gr, 3), L = gel(gr, 4), r = gel(L, 4);
    2041         889 :   GEN P = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
    2042         889 :   if (equaliu(p,2))
    2043           7 :     F = genus2_eulerfact2(P, PQ);
    2044             :   else
    2045         882 :     F = genus2_eulerfact(P,p, r[1],r[2]);
    2046         889 :   return gc_upto(av, F);
    2047             : }

Generated by: LCOV version 1.16