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 - ellsea.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23017-8c5e72c46) Lines: 1116 1164 95.9 %
Date: 2018-09-23 05:39:13 Functions: 82 84 97.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2008  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /* This file is a C version by Bill Allombert of the 'ellsea' GP package
      15             :  * whose copyright statement is as follows:
      16             : Authors:
      17             :   Christophe Doche   <cdoche@math.u-bordeaux.fr>
      18             :   Sylvain Duquesne <duquesne@math.u-bordeaux.fr>
      19             : 
      20             : Universite Bordeaux I, Laboratoire A2X
      21             : For the AREHCC project, see http://www.arehcc.com/
      22             : 
      23             : Contributors:
      24             :   Karim Belabas (code cleanup and package release, faster polynomial arithmetic)
      25             : 
      26             : 'ellsea' is free software; you can redistribute it and/or modify it under the
      27             : terms of the GNU General Public License as published by the Free Software
      28             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
      29             : ANY WARRANTY WHATSOEVER. */
      30             : 
      31             : /* Extension to non prime finite fields by Bill Allombert 2012 */
      32             : 
      33             : #include "pari.h"
      34             : #include "paripriv.h"
      35             : 
      36             : static GEN global_modular_eqn;
      37             : static THREAD GEN modular_eqn;
      38             : 
      39             : void
      40        1552 : pari_init_seadata(void)  { global_modular_eqn = NULL; }
      41             : void
      42      113148 : pari_thread_init_seadata(void)  { modular_eqn = global_modular_eqn; }
      43             : void
      44       11139 : pari_pthread_init_seadata(void)  { global_modular_eqn = modular_eqn; }
      45             : 
      46             : static char *
      47          98 : seadata_filename(ulong ell)
      48          98 : { return stack_sprintf("%s/seadata/sea%ld", pari_datadir, ell); }
      49             : 
      50             : static GEN
      51          98 : get_seadata(ulong ell)
      52             : {
      53          98 :   pari_sp av = avma;
      54             :   GEN eqn;
      55          98 :   char *s = seadata_filename(ell);
      56          98 :   pariFILE *F = pari_fopengz(s);
      57          98 :   if (!F) return NULL;
      58          42 :   if (ell) /* large single polynomial */
      59           7 :     eqn = gp_read_stream(F->file);
      60             :   else
      61             :   { /* table of polynomials of small level */
      62          35 :     eqn = gp_readvec_stream(F->file);
      63          35 :     modular_eqn = eqn = gclone(eqn);
      64          35 :     set_avma(av);
      65             :   }
      66          42 :   pari_fclose(F);
      67          42 :   return eqn;
      68             : }
      69             : 
      70             : /*Builds the modular equation corresponding to the vector list. Shallow */
      71             : static GEN
      72        9688 : list_to_pol(GEN list, long vx, long vy)
      73             : {
      74        9688 :   long i, l = lg(list);
      75        9688 :   GEN P = cgetg(l, t_VEC);
      76      195629 :   for (i = 1; i < l; i++)
      77             :   {
      78      185941 :     GEN L = gel(list,i);
      79      185941 :     if (typ(L) == t_VEC) L = RgV_to_RgX_reverse(L, vy);
      80      185941 :     gel(P, i) = L;
      81             :   }
      82        9688 :   return RgV_to_RgX_reverse(P, vx);
      83             : }
      84             : 
      85             : struct meqn {
      86             :   char type;
      87             :   GEN eq, eval;
      88             :   long vx,vy;
      89             : };
      90             : 
      91             : static GEN
      92        9744 : seadata_cache(ulong ell)
      93             : {
      94        9744 :   long n = uprimepi(ell)-1;
      95             :   GEN C;
      96        9744 :   if (!modular_eqn && !get_seadata(0))
      97          56 :     C = NULL;
      98        9688 :   else if (n && n < lg(modular_eqn))
      99        9681 :     C = gel(modular_eqn, n);
     100             :   else
     101           7 :     C = get_seadata(ell);
     102        9744 :   return C;
     103             : }
     104             : /* C = [prime level, type "A" or "C", pol. coeffs] */
     105             : static void
     106        9688 : seadata_parse(struct meqn *M, GEN C, long vx, long vy)
     107             : {
     108        9688 :   M->type = *GSTR(gel(C,2));
     109        9688 :   M->eq = list_to_pol(gel(C,3), vx, vy);
     110        9688 : }
     111             : static void
     112        9723 : get_modular_eqn(struct meqn *M, ulong ell, long vx, long vy)
     113             : {
     114        9723 :   GEN C = seadata_cache(ell);
     115        9723 :   M->vx = vx;
     116        9723 :   M->vy = vy;
     117        9723 :   M->eval = gen_0;
     118        9723 :   if (C) seadata_parse(M, C, vx, vy);
     119             :   else
     120             :   {
     121          56 :     M->type = 'J'; /* j^(1/3) for ell != 3, j for 3 */
     122          56 :     M->eq = polmodular_ZXX(ell, ell==3? 0: 5, vx, vy);
     123             :   }
     124        9723 : }
     125             : 
     126             : GEN
     127          35 : ellmodulareqn(long ell, long vx, long vy)
     128             : {
     129          35 :   pari_sp av = avma;
     130             :   struct meqn meqn;
     131             :   GEN C;
     132          35 :   if (vx < 0) vx = 0;
     133          35 :   if (vy < 0) vy = 1;
     134          35 :   if (varncmp(vx,vy) >= 0)
     135           7 :     pari_err_PRIORITY("ellmodulareqn", pol_x(vx), ">=", vy);
     136          28 :   if (ell < 2 || !uisprime(ell))
     137           7 :     pari_err_PRIME("ellmodulareqn (level)", stoi(ell));
     138          21 :   C = seadata_cache(ell);
     139          21 :   if (!C) pari_err_FILE("seadata file", seadata_filename(ell));
     140          21 :   seadata_parse(&meqn, C, vx, vy);
     141          21 :   return gerepilecopy(av, mkvec2(meqn.eq, meqn.type=='A'? gen_1: gen_0));
     142             : }
     143             : 
     144             : /***********************************************************************/
     145             : /**                                                                   **/
     146             : /**                      n-division polynomial                        **/
     147             : /**                                                                   **/
     148             : /***********************************************************************/
     149             : 
     150             : static GEN divpol(GEN t, GEN r2, long n, void *E, const struct bb_algebra *ff);
     151             : 
     152             : static GEN
     153      156702 : divpol_f2(GEN t, GEN r2, long n, void *E, const struct bb_algebra *ff)
     154             : {
     155      156702 :   if (n==0) return ff->zero(E);
     156      156702 :   if (n<=2) return ff->one(E);
     157      128534 :   if (gmael(t,2,n)) return gmael(t,2,n);
     158       51149 :   gmael(t,2,n) = gclone(ff->sqr(E,divpol(t,r2,n,E,ff)));
     159       51149 :   return gmael(t,2,n);
     160             : }
     161             : 
     162             : static GEN
     163      102186 : divpol_ff(GEN t, GEN r2, long n, void *E, const struct bb_algebra *ff)
     164             : {
     165      102186 :   if (n<=2) return ff->zero(E);
     166      102186 :   if (gmael(t,3,n)) return gmael(t,3,n);
     167       70021 :   if (n<=4) return divpol(t,r2,n,E,ff);
     168       29337 :   gmael(t,3,n) = gclone(ff->mul(E,divpol(t,r2,n,E,ff), divpol(t,r2,n-2,E,ff)));
     169       29337 :   return gmael(t,3,n);
     170             : }
     171             : 
     172             : static GEN
     173      214641 : divpol(GEN t, GEN r2, long n, void *E, const struct bb_algebra *ff)
     174             : {
     175      214641 :   long m = n/2;
     176      214641 :   pari_sp av = avma;
     177             :   GEN res;
     178      214641 :   if (n==0) return ff->zero(E);
     179      210959 :   if (gmael(t,1,n)) return gmael(t,1,n);
     180       58114 :   switch(n)
     181             :   {
     182             :   case 1:
     183             :   case 2:
     184        7021 :     res = ff->one(E);
     185        7021 :     break;
     186             :   default:
     187       51093 :     if (odd(n))
     188       30926 :       if (odd(m))
     189       25788 :         res = ff->sub(E, ff->mul(E, divpol_ff(t,r2,m+2,E,ff),
     190             :                                     divpol_f2(t,r2,m,E,ff)),
     191       12894 :                          ff->mul(E, r2,
     192       12894 :                                     ff->mul(E,divpol_ff(t,r2,m+1,E,ff),
     193             :                                               divpol_f2(t,r2,m+1,E,ff))));
     194             :       else
     195       54096 :         res = ff->sub(E, ff->mul(E, r2,
     196       18032 :                                     ff->mul(E, divpol_ff(t,r2,m+2,E,ff),
     197             :                                                divpol_f2(t,r2,m,E,ff))),
     198       18032 :                          ff->mul(E, divpol_ff(t,r2,m+1,E,ff),
     199             :                                     divpol_f2(t,r2,m+1,E,ff)));
     200             :     else
     201       40334 :       res = ff->sub(E, ff->mul(E, divpol_ff(t,r2,m+2,E,ff),
     202             :                                   divpol_f2(t,r2,m-1,E,ff)),
     203       20167 :                        ff->mul(E, divpol_ff(t,r2,m,E,ff),
     204             :                                   divpol_f2(t,r2,m+1,E,ff)));
     205             :   }
     206       58114 :   res = ff->red(E, res);
     207       58114 :   gmael(t,1,n) = gclone(res);
     208       58114 :   set_avma(av);
     209       58114 :   return gmael(t,1,n);
     210             : }
     211             : 
     212             : static void
     213       15932 : divpol_free(GEN t)
     214             : {
     215       15932 :   long i, l = lg(gel(t,1));
     216      251062 :   for (i=1; i<l; i++)
     217             :   {
     218      235130 :     if (gmael(t,1,i)) gunclone(gmael(t,1,i));
     219      235130 :     if (gmael(t,2,i)) gunclone(gmael(t,2,i));
     220      235130 :     if (gmael(t,3,i)) gunclone(gmael(t,3,i));
     221             :   }
     222       15932 : }
     223             : 
     224             : static GEN
     225         438 : Flxq_elldivpol34(long n, GEN a4, GEN a6, GEN S, GEN T, ulong p)
     226             : {
     227             :   GEN res;
     228         438 :   long vs = T[1];
     229         438 :   switch(n)
     230             :   {
     231             :   case 3:
     232         219 :     res = mkpoln(5, Fl_to_Flx(3%p,vs), pol0_Flx(vs), Flx_mulu(a4, 6, p),
     233             :                     Flx_mulu(a6, 12, p), Flx_neg(Flxq_sqr(a4, T, p), p));
     234         219 :     break;
     235             :   case 4:
     236             :     {
     237         219 :       GEN a42 = Flxq_sqr(a4, T, p);
     238         438 :       res = mkpoln(7, pol1_Flx(vs), pol0_Flx(vs), Flx_mulu(a4, 5, p),
     239             :           Flx_mulu(a6, 20, p), Flx_mulu(a42,p-5, p),
     240             :           Flx_mulu(Flxq_mul(a4, a6, T, p), p-4, p),
     241         219 :           Flx_sub(Flx_mulu(Flxq_sqr(a6, T, p), p-8%p, p),
     242             :             Flxq_mul(a4, a42, T, p), p));
     243         219 :       res = FlxX_double(res, p);
     244             :     }
     245         219 :     break;
     246             :     default:
     247           0 :       pari_err_BUG("Flxq_elldivpol34");
     248             :       return NULL;/*LCOV_EXCL_LINE*/
     249             :   }
     250         438 :   setvarn(res, get_FlxqX_var(S));
     251         438 :   return FlxqX_rem(res, S, T, p);
     252             : }
     253             : 
     254             : static GEN
     255       31426 : Fq_elldivpol34(long n, GEN a4, GEN a6, GEN S, GEN T, GEN p)
     256             : {
     257             :   GEN res;
     258       31426 :   switch(n)
     259             :   {
     260             :   case 3:
     261       15713 :     res = mkpoln(5, utoi(3), gen_0, Fq_mulu(a4, 6, T, p),
     262             :         Fq_mulu(a6, 12, T, p), Fq_neg(Fq_sqr(a4, T, p), T, p));
     263       15713 :     break;
     264             :   case 4:
     265             :     {
     266       15713 :       GEN a42 = Fq_sqr(a4, T, p);
     267       15713 :       res = mkpoln(7, gen_1, gen_0, Fq_mulu(a4, 5, T, p),
     268             :           Fq_mulu(a6, 20, T, p), Fq_Fp_mul(a42,stoi(-5), T, p),
     269             :           Fq_Fp_mul(Fq_mul(a4, a6, T, p), stoi(-4), T, p),
     270             :           Fq_sub(Fq_Fp_mul(Fq_sqr(a6, T, p), stoi(-8), T, p),
     271             :             Fq_mul(a4,a42, T, p), T, p));
     272       15713 :       res = FqX_mulu(res, 2, T, p);
     273             :     }
     274       15713 :     break;
     275             :     default:
     276           0 :       pari_err_BUG("Fq_elldivpol34");
     277             :       return NULL;/*LCOV_EXCL_LINE*/
     278             :   }
     279       31426 :   if (S)
     280             :   {
     281       31342 :     setvarn(res, get_FpXQX_var(S));
     282       31342 :     res = FqX_rem(res, S, T, p);
     283             :   }
     284       31426 :   return res;
     285             : }
     286             : 
     287             : static GEN
     288       22816 : rhs(GEN a4, GEN a6, long v)
     289             : {
     290       22816 :   GEN RHS = mkpoln(4, gen_1, gen_0, a4, a6);
     291       22816 :   setvarn(RHS, v);
     292       22816 :   return RHS;
     293             : }
     294             : 
     295             : static GEN
     296         438 : Flxq_rhs(GEN a4, GEN a6, long v, long vs)
     297             : {
     298         438 :   GEN RHS = mkpoln(4, pol1_Flx(vs),  pol0_Flx(vs), a4, a6);
     299         438 :   setvarn(RHS, v);
     300         438 :   return RHS;
     301             : }
     302             : 
     303             : struct divpolmod_red
     304             : {
     305             :   const struct bb_algebra *ff;
     306             :   void *E;
     307             :   GEN t, r2;
     308             : };
     309             : 
     310             : static void
     311       15932 : divpolmod_init(struct divpolmod_red *d, GEN D3, GEN D4, GEN RHS, long n,
     312             :                void *E, const struct bb_algebra *ff)
     313             : {
     314       15932 :   long k = n+2;
     315       15932 :   d->ff = ff; d->E = E;
     316       15932 :   d->t  = mkvec3(const_vec(k, NULL),const_vec(k, NULL),const_vec(k, NULL));
     317       15932 :   if (k>=3) gmael(d->t,1,3) = gclone(D3);
     318       15932 :   if (k>=4) gmael(d->t,1,4) = gclone(D4);
     319       15932 :   d->r2 = ff->sqr(E, RHS);
     320       15932 : }
     321             : 
     322             : static void
     323       15713 : Fq_elldivpolmod_init(struct divpolmod_red *d, GEN a4, GEN a6, long n, GEN h, GEN T, GEN p)
     324             : {
     325             :   void *E;
     326             :   const struct bb_algebra *ff;
     327       15713 :   GEN RHS, D3 = NULL, D4 = NULL;
     328       15713 :   long v = h ? get_FpXQX_var(h): 0;
     329       15713 :   D3 = n>=0 ? Fq_elldivpol34(3, a4, a6, h, T, p): NULL;
     330       15713 :   D4 = n>=1 ? Fq_elldivpol34(4, a4, a6, h, T, p): NULL;
     331       15713 :   RHS = rhs(a4, a6, v);
     332       15713 :   RHS = h ? FqX_rem(RHS, h, T, p): RHS;
     333       15713 :   RHS = FqX_mulu(RHS, 4, T, p);
     334       15755 :   ff = h ? T ? get_FpXQXQ_algebra(&E, h, T, p): get_FpXQ_algebra(&E, h, p):
     335          42 :            T ? get_FpXQX_algebra(&E, T, p, v): get_FpX_algebra(&E, p, v);
     336       15713 :   divpolmod_init(d, D3, D4, RHS, n, E, ff);
     337       15713 : }
     338             : 
     339             : static void
     340         219 : Flxq_elldivpolmod_init(struct divpolmod_red *d, GEN a4, GEN a6, long n, GEN h, GEN T, ulong p)
     341             : {
     342             :   void *E;
     343             :   const struct bb_algebra *ff;
     344         219 :   GEN RHS, D3 = NULL, D4 = NULL;
     345         219 :   long v = get_FlxqX_var(h), vT = get_Flx_var(T);
     346         219 :   D3 = n>=0 ? Flxq_elldivpol34(3, a4, a6, h, T, p): NULL;
     347         219 :   D4 = n>=1 ? Flxq_elldivpol34(4, a4, a6, h, T, p): NULL;
     348         219 :   RHS = FlxX_Fl_mul(FlxqX_rem(Flxq_rhs(a4, a6, v, vT), h, T, p), 4, p);
     349         219 :   ff = get_FlxqXQ_algebra(&E, h, T, p);
     350         219 :   divpolmod_init(d, D3, D4, RHS, n, E, ff);
     351         219 : }
     352             : 
     353             : /*Computes the n-division polynomial modulo the polynomial h \in Fq[x] */
     354             : GEN
     355        9618 : Fq_elldivpolmod(GEN a4, GEN a6, long n, GEN h, GEN T, GEN p)
     356             : {
     357             :   struct divpolmod_red d;
     358        9618 :   pari_sp ltop = avma;
     359             :   GEN res;
     360        9618 :   Fq_elldivpolmod_init(&d, a4, a6, n, h, T, p);
     361        9618 :   res = gcopy(divpol(d.t,d.r2,n,d.E,d.ff));
     362        9618 :   divpol_free(d.t);
     363        9618 :   return gerepileupto(ltop, res);
     364             : }
     365             : 
     366             : GEN
     367          42 : FpXQ_elldivpol(GEN a4, GEN a6, long n, GEN T, GEN p)
     368             : {
     369          42 :   return Fq_elldivpolmod(a4,a6,n,NULL,T,p);
     370             : }
     371             : 
     372             : GEN
     373           0 : Fp_elldivpol(GEN a4, GEN a6, long n, GEN p)
     374             : {
     375           0 :   return Fq_elldivpolmod(a4,a6,n,NULL,NULL,p);
     376             : }
     377             : 
     378             : static GEN
     379       23506 : Fq_ellyn(struct divpolmod_red *d, long k)
     380             : {
     381       23506 :   pari_sp av = avma;
     382       23506 :   void *E = d->E;
     383       23506 :   const struct bb_algebra *ff = d->ff;
     384       23506 :   if (k==1) return mkvec2(ff->one(E), ff->one(E));
     385             :   else
     386             :   {
     387       18172 :     GEN t = d->t, r2 = d->r2;
     388       18172 :     GEN pn2 = divpol(t,r2,k-2,E,ff);
     389       18172 :     GEN pp2 = divpol(t,r2,k+2,E,ff);
     390       18172 :     GEN pn12 = divpol_f2(t,r2,k-1,E,ff);
     391       18172 :     GEN pp12 = divpol_f2(t,r2,k+1,E,ff);
     392       18172 :     GEN on = ff->red(E,ff->sub(E, ff->mul(E,pp2,pn12), ff->mul(E,pn2,pp12)));
     393       18172 :     GEN f  = divpol(t,r2,k,E,ff);
     394       18172 :     GEN f2 = divpol_f2(t,r2,k,E,ff);
     395       18172 :     GEN f3 = ff->mul(E,f,f2);
     396       18172 :     if (!odd(k)) f3 = ff->mul(E,f3,r2);
     397       18172 :     return gerepilecopy(av,mkvec2(on, f3));
     398             :   }
     399             : }
     400             : 
     401             : static void
     402        6314 : Fq_elldivpolmod_close(struct divpolmod_red *d)
     403             : {
     404        6314 :   divpol_free(d->t);
     405        6314 : }
     406             : static GEN
     407       10255 : Fq_elldivpol2(GEN a4, GEN a6, GEN T, GEN p)
     408             : {
     409       10255 :   return mkpoln(4, utoi(4), gen_0, Fq_mulu(a4, 4, T, p), Fq_mulu(a6, 4, T, p));
     410             : }
     411             : 
     412             : static GEN
     413       10255 : Fq_elldivpol2d(GEN a4, GEN T, GEN p)
     414             : {
     415       10255 :   return mkpoln(3, utoi(6), gen_0, Fq_mulu(a4, 2, T, p));
     416             : }
     417             : 
     418             : static GEN
     419        1533 : FqX_numer_isog_abscissa(GEN h, GEN a4, GEN a6, GEN T, GEN p, long vx)
     420             : {
     421             :   GEN mp1, dh, ddh, t, u, t1, t2, t3, t4, f0;
     422        1533 :   long m = degpol(h);
     423        1533 :   mp1 = gel(h, m + 1); /* negative of first power sum */
     424        1533 :   dh = FqX_deriv(h, T, p);
     425        1533 :   ddh = FqX_deriv(dh, T, p);
     426        1533 :   t  = Fq_elldivpol2(a4, a6, T, p);
     427        1533 :   u  = Fq_elldivpol2d(a4, T, p);
     428        1533 :   t1 = FqX_sub(FqX_sqr(dh, T, p), FqX_mul(ddh, h, T, p), T, p);
     429        1533 :   t2 = FqX_mul(u, FqX_mul(h, dh, T, p), T, p);
     430        1533 :   t3 = FqX_mul(FqX_sqr(h, T, p),
     431             :                deg1pol_shallow(stoi(2*m), Fq_mulu(mp1, 2, T, p), vx), T, p);
     432        1533 :   f0 = FqX_add(FqX_sub(FqX_mul(t, t1, T, p), t2, T, p), t3, T, p);
     433        1533 :   t4 = FqX_mul(pol_x(vx),  FqX_sqr(h, T, p), T, p);
     434        1533 :   return FqX_add(t4, f0, T, p);
     435             : }
     436             : 
     437             : static GEN
     438        1092 : Zq_inv(GEN b, GEN T, GEN q, GEN p, long e)
     439             : {
     440        2135 :   return e==1 ? Fq_inv(b, T, p):
     441        1043 :          typ(b)==t_INT ? Fp_inv(b, q):  ZpXQ_inv(b, T, p, e);
     442             : }
     443             : 
     444             : static GEN
     445      248262 : Zq_div(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     446             : {
     447      248262 :   if (e==1) return Fq_div(a, b, T, q);
     448        1043 :   return Fq_mul(a, Zq_inv(b, T, q, p, e), T, q);
     449             : }
     450             : 
     451             : static GEN
     452           0 : Zq_sqrt(GEN b, GEN T, GEN q, GEN p, long e)
     453             : {
     454           0 :   return e==1 ? Fq_sqrt(b, T, q):
     455           0 :          typ(b)==t_INT ? Zp_sqrt(b, p, e):  ZpXQ_sqrt(b, T, p, e);
     456             : }
     457             : 
     458             : static GEN
     459       91119 : Zq_divexact(GEN a, GEN b)
     460             : {
     461       91119 :   return typ(a)==t_INT ? diviiexact(a, b): ZX_Z_divexact(a, b);
     462             : }
     463             : 
     464             : static long
     465       91084 : Zq_pval(GEN a, GEN p)
     466             : {
     467       91084 :   return typ(a)==t_INT ? Z_pval(a, p): ZX_pval(a, p);
     468             : }
     469             : 
     470             : static GEN
     471      147280 : Zq_Z_div_safe(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     472             : {
     473             :   long v;
     474      147280 :   if (e==1) return Fq_div(a, b, T, q);
     475         770 :   v = Z_pvalrem(b, p, &b);
     476         770 :   if (v>0)
     477             :   {
     478          35 :     long w = Z_pval(Q_content(a), p);
     479          35 :     if (v>w) pari_err_INV("Zq_div",b);
     480          35 :     a = Zq_divexact(a, powiu(p,v));
     481             :   }
     482         770 :   return Fq_Fp_mul(a, Fp_inv(b, q), T, q);
     483             : }
     484             : 
     485             : /*Gives the first precS terms of the Weierstrass series related to */
     486             : /*E: y^2 = x^3 + a4x + a6.  Assumes (precS-2)*(2precS+3) < ULONG_MAX, i.e.
     487             :  * precS < 46342 in 32-bit machines */
     488             : static GEN
     489       17444 : find_coeff(GEN a4, GEN a6, GEN T, GEN p, long precS, GEN pp, long e)
     490             : {
     491             :   GEN res, den;
     492             :   long k, h;
     493       17444 :   if (e > 1) { p = sqri(p); e *= 2; }
     494       17444 :   res = cgetg(precS+1, t_VEC);
     495       17444 :   den = cgetg(precS+1, t_VECSMALL);
     496       17444 :   if (precS == 0) return res;
     497       17444 :   gel(res, 1) = Fq_div(a4, stoi(-5), T, p);
     498       17444 :   den[1] = 0;
     499       17444 :   if (precS == 1) return res;
     500       17444 :   gel(res, 2) = Fq_div(a6, stoi(-7), T, p);
     501       17444 :   den[2] = 0;
     502      164724 :   for (k = 3; k <= precS; ++k)
     503             :   {
     504      147280 :     pari_sp btop = avma;
     505      147280 :     GEN a = gen_0, d;
     506      147280 :     long v=0;
     507      147280 :     if (e > 1)
     508        8358 :       for (h = 1; h <= k-2; h++)
     509        7588 :         v = maxss(v, den[h]+den[k-1-h]);
     510     1209516 :     for (h = 1; h <= k-2; h++)
     511             :     {
     512     1062236 :       GEN b = Fq_mul(gel(res, h), gel(res, k-1-h), T, p);
     513     1062236 :       if (v)
     514        1876 :         b = Fq_Fp_mul(b, powiu(pp, v-(den[h]+den[k-1-h])), T, p);
     515     1062236 :       a = Fq_add(a, b, T, p);
     516             :     }
     517      147280 :     v += Z_pvalrem(utoi((k-2) * (2*k + 3)), pp, &d);
     518      147280 :     a = Zq_div(gmulgs(a, 3), d, T, p, pp, e);
     519      147280 :     gel(res, k) = gerepileupto(btop, a);
     520      147280 :     den[k] = v;
     521             :   }
     522       17444 :   return mkvec2(res, den);
     523             : }
     524             : 
     525             : /****************************************************************************/
     526             : /*               SIMPLE ELLIPTIC CURVE OVER Fq                              */
     527             : /****************************************************************************/
     528             : 
     529             : static GEN
     530        2555 : Fq_ellj(GEN a4, GEN a6, GEN T, GEN p)
     531             : {
     532        2555 :   pari_sp ltop=avma;
     533        2555 :   GEN a43 = Fq_mulu(Fq_powu(a4, 3, T, p), 4, T, p);
     534        2555 :   GEN j   = Fq_div(Fq_mulu(a43, 1728, T, p),
     535             :                    Fq_add(a43, Fq_mulu(Fq_sqr(a6, T, p), 27, T, p), T, p), T, p);
     536        2555 :   return gerepileupto(ltop, j);
     537             : }
     538             : 
     539             : static GEN
     540        2534 : Zq_ellj(GEN a4, GEN a6, GEN T, GEN p, GEN pp, long e)
     541             : {
     542        2534 :   pari_sp ltop=avma;
     543        2534 :   GEN a43 = Fq_mulu(Fq_powu(a4, 3, T, p), 4, T, p);
     544        2534 :   GEN j   = Zq_div(Fq_mulu(a43, 1728, T, p),
     545             :                    Fq_add(a43, Fq_mulu(Fq_sqr(a6, T, p), 27, T, p), T, p), T, p, pp, e);
     546        2534 :   return gerepileupto(ltop, j);
     547             : }
     548             : /****************************************************************************/
     549             : /*                              EIGENVALUE                                  */
     550             : /****************************************************************************/
     551             : 
     552             : static GEN
     553          68 : Fq_to_Flx(GEN a4, GEN T, ulong p)
     554             : {
     555          68 :   return typ(a4)==t_INT ? Z_to_Flx(a4, p, get_Flx_var(T)): ZX_to_Flx(a4, p);
     556             : }
     557             : 
     558             : static GEN
     559         219 : Flxq_find_eigen_Frobenius(GEN a4, GEN a6, GEN h, GEN T, ulong p)
     560             : {
     561         219 :   long v = get_FlxqX_var(h), vT = get_Flx_var(T);
     562         219 :   GEN RHS = FlxqX_rem(Flxq_rhs(a4, a6, v, vT), h, T, p);
     563         219 :   return FlxqXQ_halfFrobenius(RHS, h, T, p);
     564             : }
     565             : 
     566             : static GEN
     567        6095 : Fq_find_eigen_Frobenius(GEN a4, GEN a6, GEN h, GEN T, GEN p)
     568             : {
     569        6095 :   long v = T ? get_FpXQX_var(h): get_FpX_var(h);
     570        6095 :   GEN RHS  = FqX_rem(rhs(a4, a6, v), h, T, p);
     571       12045 :   return T ? FpXQXQ_halfFrobenius(RHS, h, T, p):
     572        5950 :              FpXQ_pow(RHS, shifti(p, -1), h, p);
     573             : }
     574             : /*Finds the eigenvalue of the Frobenius given E, ell odd prime, h factor of the
     575             :  *ell-division polynomial, p and tr the possible values for the trace
     576             :  *(useful for primes with one root)*/
     577             : static ulong
     578         483 : find_eigen_value_oneroot(GEN a4, GEN a6, ulong ell, GEN tr, GEN h, GEN T, GEN p)
     579             : {
     580         483 :   pari_sp ltop = avma;
     581             :   ulong t;
     582             :   struct divpolmod_red d;
     583             :   GEN f, Dy, Gy;
     584         483 :   h = FqX_get_red(h, T, p);
     585         483 :   Gy = Fq_find_eigen_Frobenius(a4, a6, h, T, p);
     586         483 :   t = Fl_div(tr[1], 2, ell);
     587         483 :   if (t < (ell>>1)) t = ell - t;
     588         483 :   Fq_elldivpolmod_init(&d, a4, a6, t, h, T, p);
     589         483 :   f = Fq_ellyn(&d, t);
     590         483 :   Dy = FqXQ_mul(Gy, gel(f,2), h, T, p);
     591         483 :   if (!gequal(gel(f,1), Dy)) t = ell-t;
     592         483 :   Fq_elldivpolmod_close(&d);
     593         483 :   return gc_ulong(ltop, t);
     594             : }
     595             : 
     596             : static ulong
     597         219 : Flxq_find_eigen_value_power(GEN a4, GEN a6, ulong ell, long k, ulong lambda,
     598             :                             GEN h, GEN T, ulong p)
     599             : {
     600         219 :   pari_sp ltop = avma;
     601         219 :   ulong t, ellk1 = upowuu(ell, k-1), ellk = ell*ellk1;
     602             :   pari_timer ti;
     603             :   struct divpolmod_red d;
     604             :   GEN Gy;
     605         219 :   timer_start(&ti);
     606         219 :   h = FlxqX_get_red(h, T, p);
     607         219 :   Gy = Flxq_find_eigen_Frobenius(a4, a6, h, T, p);
     608         219 :   if (DEBUGLEVEL>2) err_printf(" (%ld ms)",timer_delay(&ti));
     609         219 :   Flxq_elldivpolmod_init(&d, a4, a6, ellk, h, T, p);
     610        1158 :   for (t = lambda; t < ellk; t += ellk1)
     611             :   {
     612        1158 :     GEN f = Fq_ellyn(&d, t);
     613        1158 :     GEN Dr = FlxqXQ_mul(Gy, gel(f,2), h, T, p);
     614        1158 :     if (varn(gel(f,1))!=varn(Dr)) pari_err_BUG("find_eigen_value_power");
     615        1158 :     if (gequal(gel(f,1), Dr)) break;
     616        1013 :     if (gequal(gel(f,1), FlxX_neg(Dr,p))) { t = ellk-t; break; }
     617             :   }
     618         219 :   if (DEBUGLEVEL>2) err_printf(" (%ld ms)",timer_delay(&ti));
     619         219 :   Fq_elldivpolmod_close(&d);
     620         219 :   return gc_ulong(ltop, t);
     621             : }
     622             : 
     623             : /*Finds the eigenvalue of the Frobenius modulo ell^k given E, ell, k, h factor
     624             :  *of the ell-division polynomial, lambda the previous eigen value and p */
     625             : static ulong
     626        5612 : Fq_find_eigen_value_power(GEN a4, GEN a6, ulong ell, long k, ulong lambda, GEN h, GEN T, GEN p)
     627             : {
     628        5612 :   pari_sp ltop = avma;
     629        5612 :   ulong t, ellk1 = upowuu(ell, k-1), ellk = ell*ellk1;
     630             :   pari_timer ti;
     631             :   struct divpolmod_red d;
     632             :   GEN Gy;
     633        5612 :   timer_start(&ti);
     634        5612 :   h = FqX_get_red(h, T, p);
     635        5612 :   Gy = Fq_find_eigen_Frobenius(a4, a6, h, T, p);
     636        5612 :   if (DEBUGLEVEL>2) err_printf(" (%ld ms)",timer_delay(&ti));
     637        5612 :   Fq_elldivpolmod_init(&d, a4, a6, ellk, h, T, p);
     638       21865 :   for (t = lambda; t < ellk; t += ellk1)
     639             :   {
     640       21865 :     GEN f = Fq_ellyn(&d, t);
     641       21865 :     GEN Dr = FqXQ_mul(Gy, gel(f,2), h, T, p);
     642       21865 :     if (varn(gel(f,1))!=varn(Dr)) pari_err_BUG("find_eigen_value_power");
     643       21865 :     if (gequal(gel(f,1), Dr)) break;
     644       17404 :     if (gequal(gel(f,1), FqX_neg(Dr,T,p))) { t = ellk-t; break; }
     645             :   }
     646        5612 :   if (DEBUGLEVEL>2) err_printf(" (%ld ms)",timer_delay(&ti));
     647        5612 :   Fq_elldivpolmod_close(&d);
     648        5612 :   return gc_ulong(ltop, t);
     649             : }
     650             : 
     651             : static ulong
     652        5831 : find_eigen_value_power(GEN a4, GEN a6, ulong ell, long k, ulong lambda, GEN hq, GEN T, GEN p)
     653             : {
     654        5831 :   ulong pp = itou_or_0(p);
     655        5831 :   if (pp && T)
     656             :   {
     657         219 :     GEN a4p = ZX_to_Flx(a4, pp);
     658         219 :     GEN a6p = ZX_to_Flx(a6, pp);
     659         219 :     GEN hp = ZXXT_to_FlxXT(hq, pp,varn(a4));
     660         219 :     GEN Tp = ZXT_to_FlxT(T, pp);
     661         219 :     return Flxq_find_eigen_value_power(a4p, a6p, ell, k, lambda, hp, Tp, pp);
     662             :   }
     663        5612 :   return Fq_find_eigen_value_power(a4, a6, ell, k, lambda, hq, T, p);
     664             : }
     665             : 
     666             : /*Finds the kernel polynomial h, dividing the ell-division polynomial from the
     667             :   isogenous curve Eb and trace term pp1. Uses CCR algorithm and returns h.
     668             :   Return NULL if E and Eb are *not* isogenous. */
     669             : static GEN
     670        8722 : find_kernel(GEN a4, GEN a6, ulong ell, GEN a4t, GEN a6t, GEN pp1, GEN T, GEN p, GEN pp, long e)
     671             : {
     672        8722 :   const long ext = 2;
     673        8722 :   pari_sp ltop = avma, btop;
     674             :   GEN P, v, tlist, h;
     675             :   long i, j, k;
     676        8722 :   long deg = (ell - 1)/2, dim = 2 + deg + ext;
     677        8722 :   GEN psi2 = Fq_elldivpol2(a4, a6, T, p);
     678        8722 :   GEN Dpsi2 = Fq_elldivpol2d(a4, T, p);
     679        8722 :   GEN C  = find_coeff(a4, a6, T, p, dim, pp, e);
     680        8722 :   GEN Ct = find_coeff(a4t, a6t, T, p, dim, pp, e);
     681        8722 :   GEN V = cgetg(dim+1, t_VEC);
     682       99806 :   for (k = 1; k <= dim; k++)
     683             :   {
     684       91084 :     long v = mael(C,2,k);
     685       91084 :     GEN z = gmul(gsub(gmael(Ct,1,k), gmael(C,1,k)), shifti(mpfact(2*k), -1));
     686       91084 :     if (signe(z) && Zq_pval(z, pp) < v) return NULL;
     687       91084 :     gel(V, k) = Zq_divexact(z, powiu(pp, v));
     688             :   }
     689        8722 :   btop = avma;
     690        8722 :   v = zerovec(dim);
     691        8722 :   gel(v, 1) = utoi(deg);
     692        8722 :   gel(v, 2) = pp1;
     693        8722 :   P = pol_x(0);
     694       82362 :   for (k = 3; k <= dim; k++)
     695             :   {
     696       73640 :     GEN s, r = FqX_Fq_mul(Dpsi2, gel(P, 3), T, p);
     697      531118 :     for (j = 4; j < lg(P); j++)
     698             :     {
     699      457478 :       long o = j - 2;
     700      457478 :       GEN D = FqX_add(RgX_shift_shallow(Dpsi2, 1), FqX_mulu(psi2, o-1, T, p), T, p);
     701      457478 :       GEN E = FqX_Fq_mul(D, Fq_mulu(gel(P, j), o, T, p), T, p);
     702      457478 :       r = FqX_add(r, RgX_shift_shallow(E, o-2), T, p);
     703             :     }
     704       73640 :     P = r;
     705       73640 :     s = Fq_mul(gel(P, 2), gel(v, 1), T, p);
     706      604758 :     for (j = 3; j < lg(P)-1; j++)
     707      531118 :       s = Fq_add(s, Fq_mul(gel(P, j), gel(v, j-1), T, p), T, p);
     708       73640 :     gel(v, k) = Zq_Z_div_safe(Fq_sub(gel(V, k-2), s, T, p), gel(P, j), T, p, pp, e);
     709       73640 :     if (gc_needed(btop, 1))
     710             :     {
     711           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"find_kernel");
     712           0 :       gerepileall(btop, 2, &v, &P);
     713             :     }
     714             :   }
     715        8722 :   tlist = cgetg(dim, t_VEC);
     716        8722 :   gel(tlist, dim-1) = gen_1;
     717       82362 :   for (k = 1; k <= dim-2; k++)
     718             :   {
     719       73640 :     pari_sp btop = avma;
     720       73640 :     GEN s = gel(v, k+1);
     721      531118 :     for (i = 1; i < k; i++)
     722      457478 :       s = Fq_add(s, Fq_mul(gel(tlist, dim-i-1), gel(v, k-i+1), T, p), T, p);
     723       73640 :     gel(tlist, dim-k-1) = gerepileupto(btop, Zq_Z_div_safe(s, stoi(-k), T, p, pp, e));
     724             :   }
     725       23324 :   for (i = 1; i <= ext; i++)
     726       16023 :     if (signe(Fq_red(gel(tlist, i),T, pp))) return gc_NULL(ltop);
     727        7301 :   h = FqX_red(RgV_to_RgX(vecslice(tlist, ext+1, dim-1), 0),T,p);
     728        7301 :   return signe(Fq_elldivpolmod(a4, a6, ell, h, T, pp)) ? NULL: h;
     729             : }
     730             : 
     731             : static GEN
     732        6279 : compute_u(GEN gprime, GEN Dxxg, GEN DxJg, GEN DJJg, GEN j, GEN pJ, GEN px, ulong q, GEN E4, GEN E6, GEN T, GEN p, GEN pp, long e)
     733             : {
     734        6279 :   pari_sp ltop = avma;
     735        6279 :   GEN dxxgj = FqX_eval(Dxxg, j, T, p);
     736        6279 :   GEN dxJgj = FqX_eval(DxJg, j, T, p);
     737        6279 :   GEN dJJgj = FqX_eval(DJJg, j, T, p);
     738        6279 :   GEN E42 = Fq_sqr(E4, T, p), E6ovE4 = Zq_div(E6, E4, T, p, pp, e);
     739        6279 :   GEN a = Fq_mul(gprime, dxxgj, T, p);
     740        6279 :   GEN b = Fq_mul(Fq_mul(Fq_mulu(j,2*q, T, p), dxJgj, T, p), E6ovE4, T, p);
     741        6279 :   GEN c = Fq_mul(Zq_div(Fq_sqr(E6ovE4, T, p), gprime, T, p, pp, e), j, T, p);
     742        6279 :   GEN d = Fq_mul(Fq_mul(c,sqru(q), T, p), Fq_add(pJ, Fq_mul(j, dJJgj, T, p), T, p), T, p);
     743        6279 :   GEN f = Fq_sub(Fq_div(E6ovE4,utoi(3), T, p),
     744             :                  Zq_div(E42, Fq_mulu(E6,2,T, p), T, p, pp, e), T, p);
     745        6279 :   GEN g = Fq_sub(Fq_sub(b,a,T,p), d, T, p);
     746        6279 :   return gerepileupto(ltop, Fq_add(Zq_div(g,px,T,p,pp,e), Fq_mulu(f,q,T,p), T, p));
     747             : }
     748             : 
     749             : /* Finds the isogenous EC, and the sum of the x-coordinates of the points in
     750             :  * the kernel of the isogeny E -> Eb
     751             :  * E: elliptic curve, ell: a prime, meqn: Atkin modular equation
     752             :  * g: root of meqn defining isogenous curve Eb. */
     753             : static GEN
     754        2464 : find_isogenous_from_Atkin(GEN a4, GEN a6, ulong ell, struct meqn *MEQN, GEN g, GEN T, GEN pp, long e)
     755             : {
     756        2464 :   pari_sp ltop = avma, btop;
     757        2464 :   GEN meqn = MEQN->eq, meqnx, Dmeqnx, Roots, gprime, u1;
     758        2464 :   long k, vJ = MEQN->vy;
     759        2464 :   GEN p = e==1 ? pp: powiu(pp, e);
     760        2464 :   GEN j = Zq_ellj(a4, a6, T, p, pp, e);
     761        2464 :   GEN E4 = Fq_div(a4, stoi(-3), T, p);
     762        2464 :   GEN E6 = Fq_neg(Fq_halve(a6, T, p), T, p);
     763        2464 :   GEN Dx = RgX_deriv(meqn);
     764        2464 :   GEN DJ = deriv(meqn, vJ);
     765        2464 :   GEN Dxg = FpXY_Fq_evaly(Dx, g, T, p, vJ);
     766        2464 :   GEN px = FqX_eval(Dxg, j, T, p), dx = Fq_mul(px, g, T, p);
     767        2464 :   GEN DJg = FpXY_Fq_evaly(DJ, g, T, p, vJ);
     768        2464 :   GEN pJ = FqX_eval(DJg, j, T, p), dJ = Fq_mul(pJ, j, T, p);
     769        2464 :   GEN Dxx = RgX_deriv(Dx);
     770        2464 :   GEN DxJg = FqX_deriv(Dxg, T, p);
     771             : 
     772        2464 :   GEN Dxxg = FpXY_Fq_evaly(Dxx, g, T, p, vJ);
     773        2464 :   GEN DJJg = FqX_deriv(DJg, T, p);
     774             :   GEN a, b;
     775        2464 :   if (!signe(Fq_red(dJ,T,pp)) || !signe(Fq_red(dx,T,pp)))
     776             :   {
     777          28 :     if (DEBUGLEVEL>0) err_printf("[A: d%c=0]",signe(dJ)? 'x': 'J');
     778          28 :     return gc_NULL(ltop);
     779             :   }
     780        2436 :   a = Fq_mul(dJ, Fq_mul(g, E6, T, p), T, p);
     781        2436 :   b = Fq_mul(E4, dx, T, p);
     782        2436 :   gprime = Zq_div(a, b, T, p, pp, e);
     783             : 
     784        2436 :   u1 = compute_u(gprime, Dxxg, DxJg, DJJg, j, pJ, px, 1, E4, E6, T, p, pp, e);
     785        2436 :   meqnx = FpXY_Fq_evaly(meqn, g, T, p, vJ);
     786        2436 :   Dmeqnx = FqX_deriv(meqnx, T, pp);
     787        2436 :   Roots = FqX_roots(meqnx, T, pp);
     788             : 
     789        2436 :   btop = avma;
     790        3857 :   for (k = lg(Roots)-1; k >= 1; k--, set_avma(btop))
     791             :   {
     792        3857 :     GEN jt = gel(Roots, k);
     793        3857 :     if (signe(FqX_eval(Dmeqnx, jt, T, pp))==0)
     794           0 :       continue;
     795        3857 :     if (e > 1)
     796          21 :       jt = ZqX_liftroot(meqnx, gel(Roots, k), T, pp, e);
     797        3857 :     if (signe(Fq_red(jt, T, pp)) == 0 || signe(Fq_sub(jt, utoi(1728), T, pp)) == 0)
     798             :     {
     799          14 :       if (DEBUGLEVEL>0) err_printf("[A: jt=%ld]",signe(Fq_red(jt,T,p))? 1728: 0);
     800          14 :       return gc_NULL(ltop);
     801             :     }
     802             :     else
     803             :     {
     804        3843 :       GEN pxstar = FqX_eval(Dxg, jt, T, p);
     805        3843 :       GEN dxstar = Fq_mul(pxstar, g, T, p);
     806        3843 :       GEN pJstar = FqX_eval(DJg, jt, T, p);
     807        3843 :       GEN dJstar = Fq_mul(Fq_mulu(jt, ell, T, p), pJstar, T, p);
     808        3843 :       GEN u = Fq_mul(Fq_mul(dxstar, dJ, T, p), E6, T, p);
     809        3843 :       GEN v = Fq_mul(Fq_mul(dJstar, dx, T, p), E4, T, p);
     810        3843 :       GEN E4t = Zq_div(Fq_mul(Fq_sqr(u, T, p), jt, T, p), Fq_mul(Fq_sqr(v, T, p), Fq_sub(jt, utoi(1728), T, p), T, p), T, p, pp, e);
     811        3843 :       GEN E6t = Zq_div(Fq_mul(u, E4t, T, p), v, T, p, pp, e);
     812        3843 :       GEN u2 = compute_u(gprime, Dxxg, DxJg, DJJg, jt, pJstar, pxstar, ell, E4t, E6t, T, p, pp, e);
     813        3843 :       GEN pp1 = Fq_mulu(Fq_sub(u1, u2, T, p), 3*ell, T, p);
     814        3843 :       GEN a4t = Fq_mul(mulsi(-3, powuu(ell,4)), E4t, T, p);
     815        3843 :       GEN a6t = Fq_mul(mulsi(-2, powuu(ell,6)), E6t, T, p);
     816        3843 :       GEN h = find_kernel(a4, a6, ell, a4t, a6t, pp1, T, p, pp, e);
     817        3843 :       if (h) return gerepilecopy(ltop, mkvec3(a4t, a6t, h));
     818             :     }
     819             :   }
     820           0 :   pari_err_BUG("find_isogenous_from_Atkin, kernel not found");
     821             :   return NULL;/*LCOV_EXCL_LINE*/
     822             : }
     823             : 
     824             : /* Finds E' ell-isogenous to E and the trace term p1 from canonical modular
     825             :  *   equation meqn
     826             :  * E: elliptic curve, ell: a prime, meqn: canonical modular equation
     827             :  * g: root of meqn defining isogenous curve Eb. */
     828             : static GEN
     829        4844 : find_isogenous_from_canonical(GEN a4, GEN a6, ulong ell, struct meqn *MEQN, GEN g, GEN T, GEN pp, long e)
     830             : {
     831        4844 :   pari_sp ltop = avma;
     832        4844 :   GEN meqn = MEQN->eq;
     833        4844 :   long vJ = MEQN->vy;
     834        4844 :   GEN p = e==1 ? pp: powiu(pp, e);
     835             :   GEN h;
     836        4844 :   GEN E4 = Fq_div(a4, stoi(-3), T, p);
     837        4844 :   GEN E6 = Fq_neg(Fq_halve(a6, T, p), T, p);
     838        4844 :   GEN E42 = Fq_sqr(E4, T, p);
     839        4844 :   GEN E43 = Fq_mul(E4, E42, T, p);
     840        4844 :   GEN E62 = Fq_sqr(E6, T, p);
     841        4844 :   GEN delta = Fq_div(Fq_sub(E43, E62, T, p), utoi(1728), T, p);
     842        4844 :   GEN j = Zq_div(E43, delta, T, p, pp, e);
     843        4844 :   GEN Dx = RgX_deriv(meqn);
     844        4844 :   GEN DJ = deriv(meqn, vJ);
     845        4844 :   GEN Dxg = FpXY_Fq_evaly(Dx, g, T, p, vJ);
     846        4844 :   GEN px  = FqX_eval(Dxg, j, T, p), dx  = Fq_mul(px, g, T, p);
     847        4844 :   GEN DJg = FpXY_Fq_evaly(DJ, g, T, p, vJ);
     848        4844 :   GEN pJ = FqX_eval(DJg, j, T, p), dJ = Fq_mul(j, pJ, T, p);
     849        4844 :   GEN Dxx = RgX_deriv(Dx);
     850        4844 :   GEN DxJg = FqX_deriv(Dxg, T, p);
     851             : 
     852        4844 :   GEN ExJ = FqX_eval(DxJg, j, T, p);
     853        4844 :   ulong tis = ugcd(12, ell-1), is = 12 / tis;
     854        4844 :   GEN itis = Fq_inv(stoi(-tis), T, p);
     855        4844 :   GEN deltal = Fq_div(Fq_mul(delta, Fq_powu(g, tis, T, p), T, p), powuu(ell, 12), T, p);
     856             :   GEN E4l, E6l, a4tilde, a6tilde, p_1;
     857        4844 :   if (signe(Fq_red(dx,T, pp))==0)
     858             :   {
     859           7 :     if (DEBUGLEVEL>0) err_printf("[C: dx=0]");
     860           7 :     return gc_NULL(ltop);
     861             :   }
     862        4837 :   if (signe(Fq_red(dJ, T, pp))==0)
     863             :   {
     864             :     GEN jl;
     865           0 :     if (DEBUGLEVEL>0) err_printf("[C: dJ=0]");
     866           0 :     E4l = Fq_div(E4, sqru(ell), T, p);
     867           0 :     jl  = Zq_div(Fq_powu(E4l, 3, T, p), deltal, T, p, pp, e);
     868           0 :     E6l = Zq_sqrt(Fq_mul(Fq_sub(jl, utoi(1728), T, p), deltal, T, p), T, p, pp, e);
     869           0 :     p_1 = gen_0;
     870             :   }
     871             :   else
     872             :   {
     873             :     GEN jl, f, fd, Dgs, Djs, jld;
     874        4837 :     GEN E2s = Zq_div(Fq_mul(Fq_neg(Fq_mulu(E6, 12, T, p), T, p), dJ, T, p), Fq_mul(Fq_mulu(E4, is, T, p), dx, T, p), T, p, pp, e);
     875        4837 :     GEN gd = Fq_mul(Fq_mul(E2s, itis, T, p), g, T, p);
     876        4837 :     GEN jd = Zq_div(Fq_mul(Fq_neg(E42, T, p), E6, T, p), delta, T, p, pp, e);
     877        4837 :     GEN E0b = Zq_div(E6, Fq_mul(E4, E2s, T, p), T, p, pp, e);
     878        4837 :     GEN Dxxgj = FqXY_eval(Dxx, g, j, T, p);
     879        4837 :     GEN Dgd = Fq_add(Fq_mul(gd, px, T, p), Fq_mul(g, Fq_add(Fq_mul(gd, Dxxgj, T, p), Fq_mul(jd, ExJ, T, p), T, p), T, p), T, p);
     880        4837 :     GEN DJgJj = FqX_eval(FqX_deriv(DJg, T, p), j, T, p);
     881        4837 :     GEN Djd = Fq_add(Fq_mul(jd, pJ, T, p), Fq_mul(j, Fq_add(Fq_mul(jd, DJgJj, T, p), Fq_mul(gd, ExJ, T, p), T, p), T, p), T, p);
     882        4837 :     GEN E0bd = Zq_div(Fq_sub(Fq_mul(Dgd, itis, T, p), Fq_mul(E0b, Djd, T, p), T, p), dJ, T, p, pp, e);
     883        4837 :     E4l = Zq_div(Fq_sub(E4, Fq_mul(E2s, Fq_sub(Fq_sub(Fq_add(Zq_div(Fq_mulu(E0bd, 12, T, p), E0b, T, p, pp, e), Zq_div(Fq_mulu(E42, 6, T, p), E6, T, p, pp, e), T, p), Zq_div(Fq_mulu(E6, 4, T, p), E4, T, p, pp, e), T, p), E2s, T, p), T, p), T, p), sqru(ell), T, p, pp, e);
     884        4837 :     jl = Zq_div(Fq_powu(E4l, 3, T, p), deltal, T, p, pp, e);
     885        4837 :     if (signe(Fq_red(jl,T,pp))==0)
     886             :     {
     887           7 :       if (DEBUGLEVEL>0) err_printf("[C: jl=0]");
     888           7 :       return gc_NULL(ltop);
     889             :     }
     890        4830 :     f =  Zq_div(powuu(ell, is), g, T, p, pp, e);
     891        4830 :     fd = Fq_neg(Fq_mul(Fq_mul(E2s, f, T, p), itis, T, p), T, p);
     892        4830 :     Dgs = FqXY_eval(Dx, f, jl, T, p);
     893        4830 :     Djs = FqXY_eval(DJ, f, jl, T, p);
     894        4830 :     jld = Zq_div(Fq_mul(Fq_neg(fd, T, p), Dgs, T, p), Fq_mulu(Djs, ell, T, p), T, p, pp, e);
     895        4830 :     E6l = Zq_div(Fq_mul(Fq_neg(E4l, T, p), jld, T, p), jl, T, p, pp, e);
     896        4830 :     p_1 = Fq_neg(Fq_halve(Fq_mulu(E2s, ell, T, p), T, p),T,p);
     897             :   }
     898        4830 :   a4tilde = Fq_mul(Fq_mul(stoi(-3), powuu(ell,4), T, p), E4l, T, p);
     899        4830 :   a6tilde = Fq_mul(Fq_mul(stoi(-2), powuu(ell,6), T, p), E6l, T, p);
     900        4830 :   h = find_kernel(a4, a6, ell, a4tilde, a6tilde, p_1, T, p, pp, e);
     901        4830 :   if (!h) return NULL;
     902        4830 :   return gerepilecopy(ltop, mkvec3(a4tilde, a6tilde, h));
     903             : }
     904             : 
     905             : static GEN
     906          98 : corr(GEN c4, GEN c6, GEN T, GEN p, GEN pp, long e)
     907             : {
     908          98 :   GEN c46 = Zq_div(Fq_sqr(c4, T, p), c6, T, p, pp, e);
     909          98 :   GEN c64 = Zq_div(c6, c4, T, p, pp, e);
     910          98 :   GEN a = Fp_div(gen_2, utoi(3), p);
     911          98 :   return Fq_add(Fq_halve(c46, T, p), Fq_mul(a, c64, T, p), T, p);
     912             : }
     913             : 
     914             : static GEN
     915         168 : RgXY_deflatex(GEN H, long n, long d)
     916             : {
     917         168 :   long i, l = lg(H);
     918         168 :   GEN R = cgetg(l, t_POL);
     919         168 :   R[1] = H[1];
     920         980 :   for(i = 2; i < l; i++)
     921             :   {
     922         812 :     GEN Hi = gel(H, i);
     923         812 :     gel(R,i) = typ(Hi)==t_POL? RgX_deflate(RgX_shift_shallow(Hi, d), n): Hi;
     924             :   }
     925         168 :   return RgX_renormalize_lg(R, l);
     926             : }
     927             : 
     928             : static GEN
     929          70 : Fq_polmodular_eval(GEN meqn, GEN j, long N, GEN T, GEN p, long vJ)
     930             : {
     931          70 :   pari_sp av = avma;
     932             :   GEN R, dR, ddR;
     933          70 :   long t0 = N%3 == 1 ? 2: 0;
     934          70 :   long t2 = N%3 == 1 ? 0: 2;
     935          70 :   if (N == 3)
     936             :   {
     937          14 :     GEN P = FpXX_red(meqn, p);
     938          14 :     GEN dP = deriv(P, -1), ddP = deriv(dP, -1);
     939          14 :     R = FpXY_Fq_evaly(P, j, T, p, vJ);
     940          14 :     dR = FpXY_Fq_evaly(dP, j, T, p, vJ);
     941          14 :     ddR = FpXY_Fq_evaly(ddP, j, T, p, vJ);
     942          14 :     return gerepilecopy(av, mkvec3(R,dR,ddR));
     943             :   }
     944             :   else
     945             :   {
     946          56 :     GEN P5 = FpXX_red(meqn, p);
     947          56 :     GEN H = RgX_splitting(P5, 3);
     948          56 :     GEN H0 = RgXY_deflatex(gel(H,1), 3, -t0);
     949          56 :     GEN H1 = RgXY_deflatex(gel(H,2), 3, -1);
     950          56 :     GEN H2 = RgXY_deflatex(gel(H,3), 3, -t2);
     951          56 :     GEN h0 = FpXY_Fq_evaly(H0, j, T, p, vJ);
     952          56 :     GEN h1 = FpXY_Fq_evaly(H1, j, T, p, vJ);
     953          56 :     GEN h2 = FpXY_Fq_evaly(H2, j, T, p, vJ);
     954          56 :     GEN dH0 = RgX_deriv(H0);
     955          56 :     GEN dH1 = RgX_deriv(H1);
     956          56 :     GEN dH2 = RgX_deriv(H2);
     957          56 :     GEN ddH0 = RgX_deriv(dH0);
     958          56 :     GEN ddH1 = RgX_deriv(dH1);
     959          56 :     GEN ddH2 = RgX_deriv(dH2);
     960          56 :     GEN d0 = FpXY_Fq_evaly(dH0, j, T, p, vJ);
     961          56 :     GEN d1 = FpXY_Fq_evaly(dH1, j, T, p, vJ);
     962          56 :     GEN d2 = FpXY_Fq_evaly(dH2, j, T, p, vJ);
     963          56 :     GEN dd0 = FpXY_Fq_evaly(ddH0, j, T, p, vJ);
     964          56 :     GEN dd1 = FpXY_Fq_evaly(ddH1, j, T, p, vJ);
     965          56 :     GEN dd2 = FpXY_Fq_evaly(ddH2, j, T, p, vJ);
     966             :     GEN h02, h12, h22, h03, h13, h23, h012, dh03, dh13, dh23, dh012;
     967             :     GEN ddh03, ddh13, ddh23, ddh012;
     968             :     GEN R1, dR1, ddR1, ddR2;
     969          56 :     h02 = FqX_sqr(h0, T, p);
     970          56 :     h12 = FqX_sqr(h1, T, p);
     971          56 :     h22 = FqX_sqr(h2, T, p);
     972          56 :     h03 = FqX_mul(h0, h02, T, p);
     973          56 :     h13 = FqX_mul(h1, h12, T, p);
     974          56 :     h23 = FqX_mul(h2, h22, T, p);
     975          56 :     h012 = FqX_mul(FqX_mul(h0, h1, T, p), h2, T, p);
     976          56 :     dh03 = FqX_mul(FqX_mulu(d0, 3, T, p), h02, T, p);
     977          56 :     dh13 = FqX_mul(FqX_mulu(d1, 3, T, p), h12, T, p);
     978          56 :     dh23 = FqX_mul(FqX_mulu(d2, 3, T, p), h22, T, p);
     979          56 :     dh012 = FqX_add(FqX_add(FqX_mul(FqX_mul(d0, h1, T, p), h2, T, p), FqX_mul(FqX_mul(h0, d1, T, p), h2, T, p), T, p), FqX_mul(FqX_mul(h0, h1, T, p), d2, T, p), T, p);
     980          56 :     R1 = FqX_sub(h13, FqX_mulu(h012, 3, T, p), T, p);
     981          56 :     R = FqX_add(FqX_add(FqX_Fq_mul(RgX_shift_shallow(h23, t2), Fq_sqr(j, T, p), T, p), FqX_Fq_mul(RgX_shift_shallow(R1, 1), j, T, p), T, p), RgX_shift_shallow(h03, t0), T, p);
     982          56 :     dR1 = FqX_sub(dh13, FqX_mulu(dh012, 3, T, p), T, p);
     983          56 :     dR = FqX_add(FqX_add(RgX_shift_shallow(FqX_add(FqX_Fq_mul(dh23, Fq_sqr(j, T, p), T, p), FqX_Fq_mul(h23, Fq_mulu(j, 2, T, p), T, p), T, p), t2), RgX_shift_shallow(FqX_add(FqX_Fq_mul(dR1, j, T, p), R1, T, p), 1), T, p), RgX_shift_shallow(dh03, t0), T, p);
     984          56 :     ddh03 = FqX_mulu(FqX_add(FqX_mul(dd0, h02, T, p), FqX_mul(FqX_mulu(FqX_sqr(d0, T, p), 2, T, p), h0, T, p), T, p), 3, T, p);
     985          56 :     ddh13 = FqX_mulu(FqX_add(FqX_mul(dd1, h12, T, p), FqX_mul(FqX_mulu(FqX_sqr(d1, T, p), 2, T, p), h1, T, p), T, p), 3, T, p);
     986          56 :     ddh23 = FqX_mulu(FqX_add(FqX_mul(dd2, h22, T, p), FqX_mul(FqX_mulu(FqX_sqr(d2, T, p), 2, T, p), h2, T, p), T, p), 3, T, p);
     987          56 :     ddh012 = FqX_add(FqX_add(FqX_add(FqX_mul(FqX_mul(dd0, h1, T, p), h2, T, p), FqX_mul(FqX_mul(h0, dd1, T, p), h2, T, p), T, p), FqX_mul(FqX_mul(h0, h1, T, p), dd2, T, p), T, p), FqX_mulu(FqX_add(FqX_add(FqX_mul(FqX_mul(d0, d1, T, p), h2, T, p), FqX_mul(FqX_mul(d0, h1, T, p), d2, T, p), T, p), FqX_mul(FqX_mul(h0, d1, T, p), d2, T, p), T, p), 2, T, p), T, p);
     988          56 :     ddR1 = FqX_sub(ddh13, FqX_mulu(ddh012, 3, T, p), T, p);
     989          56 :     ddR2 = FqX_add(FqX_add(FqX_Fq_mul(ddh23, Fq_sqr(j, T, p), T, p), FqX_Fq_mul(dh23, Fq_mulu(j, 4, T, p), T, p), T, p), FqX_mulu(h23, 2, T, p), T, p);
     990          56 :     ddR = FqX_add(FqX_add(RgX_shift_shallow(ddR2, t2), RgX_shift_shallow(FqX_add(FqX_mulu(dR1, 2, T, p), FqX_Fq_mul(ddR1, j, T, p), T, p), 1), T, p), RgX_shift_shallow(ddh03, t0), T, p);
     991          56 :     return gerepilecopy(av, mkvec3(R ,dR ,ddR));
     992             :   }
     993             : }
     994             : 
     995             : static GEN
     996       11277 : meqn_j(struct meqn *MEQN, GEN j, long ell, GEN T, GEN p)
     997             : {
     998       11277 :   if (MEQN->type=='J')
     999             :   {
    1000          70 :     MEQN->eval = Fq_polmodular_eval(MEQN->eq, j, ell, T, p, MEQN->vy);
    1001          70 :     return gel(MEQN->eval, 1);
    1002             :   }
    1003             :   else
    1004       11207 :     return FqXY_evalx(MEQN->eq, j, T, p);
    1005             : }
    1006             : 
    1007             : static GEN
    1008          49 : find_isogenous_from_J(GEN a4, GEN a6, ulong ell, struct meqn *MEQN, GEN g, GEN T, GEN pp, long e)
    1009             : {
    1010          49 :   pari_sp ltop = avma;
    1011          49 :   GEN meqn = MEQN->eval;
    1012          49 :   GEN p = e==1 ? pp: powiu(pp, e);
    1013             :   GEN h;
    1014             :   GEN C4, C6, C4t, C6t;
    1015             :   GEN j, jp, jtp, jtp2, jtp3;
    1016             :   GEN Py, Pxy, Pyy, Pxj, Pyj, Pxxj, Pxyj, Pyyj;
    1017             :   GEN s0, s1, s2, s3;
    1018             :   GEN den, D, co, cot, c0, p_1, a4tilde, a6tilde;
    1019          49 :   if (signe(g) == 0 || signe(Fq_sub(g, utoi(1728), T, p)) == 0)
    1020             :   {
    1021           0 :     if (DEBUGLEVEL>0) err_printf("[J: g=%ld]",signe(g)==0 ?0: 1728);
    1022           0 :     return gc_NULL(ltop);
    1023             :   }
    1024          49 :   C4 = Fq_mul(a4, stoi(-48), T, p);
    1025          49 :   C6 = Fq_mul(a6, stoi(-864), T, p);
    1026          49 :   if (signe(C4)==0 || signe(C6)==0)
    1027             :   {
    1028           0 :     if (DEBUGLEVEL>0) err_printf("[J: C%ld=0]",signe(C4)==0 ?4: 6);
    1029           0 :     return gc_NULL(ltop);
    1030             :   }
    1031          49 :   j = Zq_ellj(a4, a6, T, p, pp, e);
    1032          49 :   jp = Fq_mul(j, Zq_div(C6, C4, T, p, pp, e), T, p);
    1033          49 :   co = corr(C4, C6, T, p, pp, e);
    1034          49 :   Py = RgX_deriv(gel(meqn, 1));
    1035          49 :   Pxy = RgX_deriv(gel(meqn,2));
    1036          49 :   Pyy = RgX_deriv(Py);
    1037          49 :   Pxj = FqX_eval(gel(meqn, 2), g, T, p);
    1038          49 :   if (signe(Pxj)==0)
    1039             :   {
    1040           0 :     if (DEBUGLEVEL>0) err_printf("[J: Pxj=0]");
    1041           0 :     return gc_NULL(ltop);
    1042             :   }
    1043          49 :   Pyj = FqX_eval(Py, g, T, p);
    1044          49 :   Pxxj = FqX_eval(gel(meqn, 3), g, T, p);
    1045          49 :   Pxyj = FqX_eval(Pxy, g, T, p);
    1046          49 :   Pyyj = FqX_eval(Pyy, g, T, p);
    1047          49 :   jtp = Fq_div(Fq_mul(jp, Zq_div(Pxj, Pyj, T, p, pp, e), T, p), negi(utoi(ell)), T, p);
    1048          49 :   jtp2 = Fq_sqr(jtp,T,p);
    1049          49 :   jtp3 = Fq_mul(jtp,jtp2,T,p);
    1050          49 :   den = Fq_mul(Fq_sqr(g,T,p),Fq_sub(g,utoi(1728),T,p),T, p);
    1051          49 :   D  =  Zq_inv(den,T,p,pp, e);
    1052          49 :   C4t = Fq_mul(jtp2,Fq_mul(g, D, T, p), T, p);
    1053          49 :   C6t = Fq_mul(jtp3, D, T, p);
    1054          49 :   s0 = Fq_mul(Fq_sqr(jp, T, p), Pxxj, T, p);
    1055          49 :   s1 = Fq_mul(Fq_mulu(Fq_mul(jp,jtp,T,p),2*ell,T,p), Pxyj, T, p);
    1056          49 :   s2 = Fq_mul(Fq_mulu(jtp2,ell*ell,T,p), Pyyj, T, p);
    1057          49 :   s3 = Zq_div(Fq_add(s0, Fq_add(s1, s2, T, p), T, p),Fq_mul(jp, Pxj, T, p),T,p,pp,e);
    1058          49 :   cot = corr(C4t, C6t, T, p, pp, e);
    1059          49 :   c0 = Fq_sub(co,Fq_mulu(cot,ell,T,p),T,p);
    1060          49 :   p_1 = Fq_div(Fq_mulu(Fq_add(s3, c0, T, p),ell,T,p),stoi(-4),T,p);
    1061          49 :   a4tilde = Fq_mul(Fq_div(C4t, stoi(-48), T, p),powuu(ell,4), T, p);
    1062          49 :   a6tilde = Fq_mul(Fq_div(C6t, stoi(-864), T, p),powuu(ell,6), T, p);
    1063          49 :   h = find_kernel(a4, a6, ell, a4tilde, a6tilde, p_1, T, p, pp, e);
    1064          49 :   if (!h) return NULL;
    1065          49 :   return gerepilecopy(ltop, mkvec3(a4tilde, a6tilde, h));
    1066             : }
    1067             : 
    1068             : static GEN
    1069        7357 : find_isogenous(GEN a4,GEN a6, ulong ell, struct meqn *MEQN, GEN g, GEN T,GEN p)
    1070             : {
    1071        7357 :   ulong pp = itou_or_0(p);
    1072        7357 :   long e = (pp && pp <= 2*ell+3) ? 2+factorial_lval(ell, pp): 1;
    1073        7357 :   if (e > 1)
    1074             :   {
    1075          21 :     GEN pe = powiu(p, e);
    1076          21 :     GEN meqnj = meqn_j(MEQN, Zq_ellj(a4, a6, T, pe, p, e), ell, T, pe);
    1077          21 :     g = ZqX_liftroot(meqnj, g, T, p, e);
    1078             :   }
    1079        7357 :   switch(MEQN->type)
    1080             :   {
    1081        4844 :     case 'C': return find_isogenous_from_canonical(a4,a6,ell, MEQN, g, T,p,e);
    1082        2464 :     case 'A': return find_isogenous_from_Atkin(a4,a6,ell, MEQN, g, T,p,e);
    1083          49 :     default:  return find_isogenous_from_J(a4,a6,ell, MEQN, g, T,p,e);
    1084             :   }
    1085             : }
    1086             : 
    1087             : static GEN
    1088        6139 : FqX_homogenous_eval(GEN P, GEN A, GEN B, GEN T, GEN p)
    1089             : {
    1090        6139 :   long d = degpol(P), i, v = varn(A);
    1091        6139 :   GEN s =  scalar_ZX_shallow(gel(P, d+2), v), Bn = pol_1(v);
    1092       20384 :   for (i = d-1; i >= 0; i--)
    1093             :   {
    1094       14245 :     Bn = FqX_mul(Bn, B, T, p);
    1095       14245 :     s = FqX_add(FqX_mul(s, A, T, p), FqX_Fq_mul(Bn, gel(P,i+2), T, p), T, p);
    1096             :   }
    1097        6139 :   return s;
    1098             : }
    1099             : 
    1100             : static GEN
    1101        1288 : FqX_homogenous_div(GEN P, GEN Q, GEN A, GEN B, GEN T, GEN p)
    1102             : {
    1103        1288 :   GEN z = cgetg(3, t_RFRAC);
    1104        1288 :   long d = degpol(Q)-degpol(P);
    1105        1288 :   gel(z, 1) = FqX_homogenous_eval(P, A, B, T, p);
    1106        1288 :   gel(z, 2) = FqX_homogenous_eval(Q, A, B, T, p);
    1107        1288 :   if (d > 0)
    1108           0 :     gel(z, 1) = FqX_mul(gel(z, 1), FqX_powu(B, d, T, p), T, p);
    1109        1288 :   else if (d < 0)
    1110        1288 :     gel(z, 2) = FqX_mul(gel(z, 2), FqX_powu(B, -d, T, p), T, p);
    1111        1288 :   return z;
    1112             : }
    1113             : 
    1114             : static GEN
    1115        1533 : find_kernel_power(GEN Eba4, GEN Eba6, GEN Eca4, GEN Eca6, ulong ell, struct meqn *MEQN, GEN kpoly, GEN Ib, GEN T, GEN p)
    1116             : {
    1117        1533 :   pari_sp ltop = avma, btop;
    1118             :   GEN a4t, a6t, gtmp;
    1119        1533 :   GEN num_iso = FqX_numer_isog_abscissa(kpoly, Eba4, Eba6, T, p, 0);
    1120        1533 :   GEN mpoly = meqn_j(MEQN, Fq_ellj(Eca4, Eca6, T, p), ell, T, p);
    1121        1533 :   GEN mroots = FqX_roots(mpoly, T, p);
    1122        1533 :   GEN kpoly2 = FqX_sqr(kpoly, T, p);
    1123        1533 :   long i, l1 = lg(mroots);
    1124        1533 :   btop = avma;
    1125        2520 :   for (i = 1; i < l1; i++)
    1126             :   {
    1127             :     GEN h;
    1128        2282 :     GEN tmp = find_isogenous(Eca4, Eca6, ell, MEQN, gel(mroots, i), T, p);
    1129        2282 :     if (!tmp) return gc_NULL(ltop);
    1130        2275 :     a4t =  gel(tmp, 1);
    1131        2275 :     a6t =  gel(tmp, 2);
    1132        2275 :     gtmp = gel(tmp, 3);
    1133             : 
    1134             :     /*check that the kernel kpoly is the good one */
    1135        2275 :     h = FqX_homogenous_eval(gtmp, num_iso, kpoly2, T, p);
    1136        2275 :     if (signe(Fq_elldivpolmod(Eba4, Eba6, ell, h, T, p)))
    1137             :     {
    1138        1288 :       GEN Ic = FqX_homogenous_div(num_iso,kpoly2, numer_i(Ib),denom_i(Ib), T,p);
    1139        1288 :       GEN kpoly_new = FqX_homogenous_eval(gtmp,   numer_i(Ic),denom_i(Ic), T,p);
    1140        1288 :       return gerepilecopy(ltop, mkvecn(5, a4t, a6t, kpoly_new, gtmp, Ic));
    1141             :     }
    1142         987 :     set_avma(btop);
    1143             :   }
    1144         238 :   return gc_NULL(ltop);
    1145             : }
    1146             : 
    1147             : /****************************************************************************/
    1148             : /*                                  TRACE                                   */
    1149             : /****************************************************************************/
    1150             : enum mod_type {MTpathological, MTAtkin, MTElkies, MTone_root, MTroots};
    1151             : 
    1152             : static GEN
    1153         389 : Flxq_study_eqn(GEN mpoly, GEN T, ulong p, long *pt_dG, long *pt_r)
    1154             : {
    1155         389 :   GEN Xq = FlxqX_Frobenius(mpoly, T, p);
    1156         389 :   GEN G  = FlxqX_gcd(FlxX_sub(Xq, pol_x(0), p), mpoly, T, p);
    1157         389 :   *pt_dG = degpol(G);
    1158         389 :   if (!*pt_dG) { *pt_r = FlxqX_ddf_degree(mpoly, Xq, T, p); return NULL; }
    1159         257 :   return gel(FlxqX_roots(G, T, p), 1);
    1160             : }
    1161             : 
    1162             : static GEN
    1163        9205 : Fp_study_eqn(GEN mpoly, GEN p, long *pt_dG, long *pt_r)
    1164             : {
    1165        9205 :   GEN T  = FpX_get_red(mpoly, p);
    1166        9205 :   GEN XP = FpX_Frobenius(T, p);
    1167        9205 :   GEN G  = FpX_gcd(FpX_sub(XP, pol_x(0), p), mpoly, p);
    1168        9205 :   *pt_dG = degpol(G);
    1169        9205 :   if (!*pt_dG) { *pt_r = FpX_ddf_degree(T, XP, p); return NULL; }
    1170        4816 :   return FpX_oneroot(G, p);
    1171             : }
    1172             : 
    1173             : static GEN
    1174        9716 : Fq_study_eqn(GEN mpoly, GEN T, GEN p, long *pt_dG, long *pt_r)
    1175             : {
    1176             :   GEN G;
    1177        9716 :   if (!T) return Fp_study_eqn(mpoly, p, pt_dG, pt_r);
    1178         511 :   if (lgefint(p)==3)
    1179             :   {
    1180         389 :     ulong pp = p[2];
    1181         389 :     GEN Tp = ZXT_to_FlxT(T,pp);
    1182         389 :     GEN mpolyp = ZXX_to_FlxX(mpoly,pp,get_FpX_var(T));
    1183         389 :     G = Flxq_study_eqn(mpolyp, Tp, pp, pt_dG, pt_r);
    1184         389 :     return G ? Flx_to_ZX(G): NULL;
    1185             :   }
    1186             :   else
    1187             :   {
    1188         122 :     GEN Xq = FpXQX_Frobenius(mpoly, T, p);
    1189         122 :     G  = FpXQX_gcd(FpXX_sub(Xq, pol_x(0), p), mpoly, T, p);
    1190         122 :     *pt_dG = degpol(G);
    1191         122 :     if (!*pt_dG) { *pt_r = FpXQX_ddf_degree(mpoly, Xq, T, p); return NULL; }
    1192          72 :     return gel(FpXQX_roots(G, T, p), 1);
    1193             :   }
    1194             : }
    1195             : 
    1196             : /* Berlekamp variant */
    1197             : static GEN
    1198        9723 : study_modular_eqn(long ell, GEN mpoly, GEN T, GEN p, enum mod_type *mt, long *ptr_r)
    1199             : {
    1200        9723 :   pari_sp ltop = avma;
    1201        9723 :   GEN g = gen_0;
    1202        9723 :   *ptr_r = 0; /*gcc -Wall*/
    1203        9723 :   if (!FqX_is_squarefree(mpoly, T, p)) *mt = MTpathological;
    1204             :   else
    1205             :   {
    1206             :     long dG;
    1207        9716 :     g = Fq_study_eqn(mpoly, T, p, &dG, ptr_r);
    1208        9716 :     switch(dG)
    1209             :     {
    1210        4571 :       case 0:  *mt = MTAtkin; break;
    1211         518 :       case 1:  *mt = MTone_root; break;
    1212        4557 :       case 2:  *mt = MTElkies;   break;
    1213          70 :       default: *mt = (dG == ell + 1)? MTroots: MTpathological;
    1214             :     }
    1215             :   }
    1216        9723 :   if (DEBUGLEVEL) switch(*mt)
    1217             :   {
    1218           0 :     case MTone_root: err_printf("One root\t"); break;
    1219           0 :     case MTElkies: err_printf("Elkies\t"); break;
    1220           0 :     case MTroots: err_printf("l+1 roots\t"); break;
    1221           0 :     case MTAtkin: err_printf("Atkin\t"); break;
    1222           0 :     case MTpathological: err_printf("Pathological\n"); break;
    1223             :   }
    1224        9723 :   return g ? gerepilecopy(ltop, g): NULL;
    1225             : }
    1226             : 
    1227             : /*Returns the trace modulo ell^k when ell is an Elkies prime */
    1228             : static GEN
    1229        5075 : find_trace_Elkies_power(GEN a4, GEN a6, ulong ell, long *pt_k, struct meqn *MEQN, GEN g, GEN tr, GEN q, GEN T, GEN p, long smallfact, pari_timer *ti)
    1230             : {
    1231        5075 :   pari_sp ltop = avma, btop;
    1232             :   GEN tmp, Eba4, Eba6, Eca4, Eca6, Ib, kpoly;
    1233        5075 :   long k = *pt_k;
    1234        5075 :   ulong lambda, ellk = upowuu(ell, k), pellk = umodiu(q, ellk);
    1235             :   long cnt;
    1236             : 
    1237        5075 :   if (DEBUGLEVEL) { err_printf("mod %ld", ell); }
    1238        5075 :   Eba4 = a4;
    1239        5075 :   Eba6 = a6;
    1240        5075 :   tmp = find_isogenous(a4,a6, ell, MEQN, g, T, p);
    1241        5075 :   if (!tmp) return gc_NULL(ltop);
    1242        5026 :   Eca4 =  gel(tmp, 1);
    1243        5026 :   Eca6 =  gel(tmp, 2);
    1244        5026 :   kpoly = gel(tmp, 3);
    1245        5026 :   Ib = pol_x(0);
    1246        9569 :   lambda = tr ? find_eigen_value_oneroot(a4, a6, ell, tr, kpoly, T, p):
    1247        4543 :                 find_eigen_value_power(a4, a6, ell, 1, 1, kpoly, T, p);
    1248        5026 :   if (DEBUGLEVEL>1) err_printf(" [%ld ms]", timer_delay(ti));
    1249        5026 :   if (smallfact && smallfact%(long)ell!=0)
    1250             :   {
    1251         378 :     ulong pell = pellk%ell;
    1252         378 :     ulong ap = Fl_add(lambda, Fl_div(pell, lambda, ell), ell);
    1253         378 :     if (Fl_sub(pell, ap, ell)==ell-1) { set_avma(ltop); return mkvecsmall(ap); }
    1254         364 :     if (smallfact < 0 && Fl_add(pell, ap, ell)==ell-1) { set_avma(ltop); return mkvecsmall(ap); }
    1255             :   }
    1256        4998 :   btop = avma;
    1257        6286 :   for (cnt = 2; cnt <= k; cnt++)
    1258             :   {
    1259        1533 :     GEN tmp = find_kernel_power(Eba4, Eba6, Eca4, Eca6, ell, MEQN, kpoly, Ib, T, p);
    1260        1533 :     if (!tmp) { k = cnt-1; break; }
    1261        1288 :     if (DEBUGLEVEL) err_printf(", %Ps", powuu(ell, cnt));
    1262        1288 :     lambda = find_eigen_value_power(a4, a6, ell, cnt, lambda, gel(tmp,3), T, p);
    1263        1288 :     Eba4 = Eca4;
    1264        1288 :     Eba6 = Eca6;
    1265        1288 :     Eca4 = gel(tmp,1);
    1266        1288 :     Eca6 = gel(tmp,2);
    1267        1288 :     kpoly = gel(tmp,4);
    1268        1288 :     Ib = gel(tmp, 5);
    1269        1288 :     if (gc_needed(btop, 1))
    1270             :     {
    1271           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"find_trace_Elkies_power");
    1272           0 :       gerepileall(btop, 6, &Eba4, &Eba6, &Eca4, &Eca6, &kpoly, &Ib);
    1273             :     }
    1274        1288 :     if (DEBUGLEVEL>1) err_printf(" [%ld ms]", timer_delay(ti));
    1275             :   }
    1276        4998 :   set_avma(ltop);
    1277        4998 :   ellk = upowuu(ell, k);
    1278        4998 :   pellk = umodiu(q, ellk);
    1279        4998 :   *pt_k = k;
    1280        4998 :   return mkvecsmall(Fl_add(lambda, Fl_div(pellk, lambda, ellk), ellk));
    1281             : }
    1282             : 
    1283             : /*Returns the possible values of the trace when ell is an Atkin prime, */
    1284             : /*given r the splitting degree of the modular equation at J = E.j */
    1285             : static GEN
    1286        4571 : find_trace_Atkin(ulong ell, long r, GEN q)
    1287             : {
    1288        4571 :   pari_sp ltop = avma;
    1289        4571 :   long nval = 0;
    1290        4571 :   ulong teta, pell = umodiu(q, ell), invp = Fl_inv(pell, ell);
    1291        4571 :   GEN val_pos = cgetg(1+ell, t_VECSMALL), P = gel(factoru(r), 1);
    1292        4571 :   GEN S = mkvecsmall4(0, pell, 0, 1);
    1293        4571 :   GEN U = mkvecsmall3(0, ell-1, 0);
    1294        4571 :   pari_sp btop = avma;
    1295        4571 :   if (r==2 && krouu(ell-pell, ell) < 0)
    1296         812 :     val_pos[++nval] = 0;
    1297       87073 :   for (teta = 1; teta < ell; teta++, set_avma(btop))
    1298             :   {
    1299       82502 :     ulong disc = Fl_sub(Fl_sqr(teta,ell), Fl_mul(4UL,pell,ell), ell);
    1300             :     GEN a;
    1301       82502 :     if (krouu(disc, ell) >= 0) continue;
    1302       40628 :     S[3] = Fl_neg(teta, ell);
    1303       40628 :     U[3] = Fl_mul(invp, teta, ell);
    1304       40628 :     a = Flxq_powu(U, r/P[1], S, ell);
    1305       40628 :     if (!Flx_equal1(a) && Flx_equal1(Flxq_powu(a, P[1], S, ell)))
    1306             :     {
    1307       26586 :       pari_sp av = avma;
    1308       26586 :       long i, l=lg(P);
    1309       45290 :       for (i = 2; i < l; i++, set_avma(av))
    1310       23856 :         if (Flx_equal1(Flxq_powu(U, r/P[i], S, ell))) break;
    1311       26586 :       if (i==l) val_pos[++nval] = teta;
    1312             :     }
    1313             :   }
    1314        4571 :   return gerepileupto(ltop, vecsmall_shorten(val_pos, nval));
    1315             : }
    1316             : 
    1317             : /*Returns the possible traces when there is only one root */
    1318             : static GEN
    1319         518 : find_trace_one_root(ulong ell, GEN q)
    1320             : {
    1321         518 :   ulong a = Fl_double(Fl_sqrt(umodiu(q,ell), ell), ell);
    1322         518 :   return mkvecsmall2(a, ell - a);
    1323             : }
    1324             : 
    1325             : static GEN
    1326          70 : find_trace_lp1_roots(long ell, GEN q)
    1327             : {
    1328          70 :   ulong ell2 = ell * ell, pell = umodiu(q, ell2);
    1329          70 :   ulong a  = Fl_sqrt(pell%ell, ell);
    1330          70 :   ulong pa = Fl_add(Fl_div(pell, a, ell2), a, ell2);
    1331          70 :   return mkvecsmall2(pa, ell2 - pa);
    1332             : }
    1333             : 
    1334             : /*trace modulo ell^k: [], [t] or [t1,...,td] */
    1335             : static GEN
    1336        9723 : find_trace(GEN a4, GEN a6, GEN j, ulong ell, GEN q, GEN T, GEN p, long *ptr_kt,
    1337             :   long smallfact, long vx, long vy)
    1338             : {
    1339        9723 :   pari_sp ltop = avma;
    1340             :   GEN g, meqnj, tr, tr2;
    1341             :   long kt, r;
    1342             :   enum mod_type mt;
    1343             :   struct meqn MEQN;
    1344             :   pari_timer ti;
    1345             : 
    1346        9723 :   kt = maxss((long)(log(expi(q)*M_LN2)/log((double)ell)), 1);
    1347        9723 :   if (DEBUGLEVEL)
    1348           0 :   { err_printf("SEA: Prime %5ld ", ell); timer_start(&ti); }
    1349        9723 :   get_modular_eqn(&MEQN, ell, vx, vy);
    1350        9723 :   meqnj = meqn_j(&MEQN, j, ell, T, p);
    1351        9723 :   g = study_modular_eqn(ell, meqnj, T, p, &mt, &r);
    1352             :   /* If l is an Elkies prime, search for a factor of the l-division polynomial.
    1353             :   * Then deduce the trace by looking for eigenvalues of the Frobenius by
    1354             :   * computing modulo this factor */
    1355        9723 :   switch (mt)
    1356             :   {
    1357             :   case MTone_root:
    1358         518 :     tr2 = find_trace_one_root(ell, q);
    1359         518 :     tr = find_trace_Elkies_power(a4,a6,ell, &kt, &MEQN, g, tr2, q, T, p, smallfact, &ti);
    1360         518 :     if (!tr) { tr = tr2; kt = 1; }
    1361         518 :     break;
    1362             :   case MTElkies:
    1363             :     /* Contrary to MTone_root, may look mod higher powers of ell */
    1364        4557 :     if (abscmpiu(p, 2*ell+3) <= 0)
    1365          14 :       kt = 1; /* Not implemented in this case */
    1366        4557 :     tr = find_trace_Elkies_power(a4,a6,ell, &kt, &MEQN, g, NULL, q, T, p, smallfact, &ti);
    1367        4557 :     if (!tr)
    1368             :     {
    1369          14 :       if (DEBUGLEVEL) err_printf("[fail]\n");
    1370          14 :       return gc_NULL(ltop);
    1371             :     }
    1372        4543 :     break;
    1373             :   case MTroots:
    1374          70 :     tr = find_trace_lp1_roots(ell, q);
    1375          70 :     kt = 2;
    1376          70 :     break;
    1377             :   case MTAtkin:
    1378        4571 :     tr = find_trace_Atkin(ell, r, q);
    1379        4571 :     if (lg(tr)==1) pari_err_PRIME("ellap",p);
    1380        4571 :     kt = 1;
    1381        4571 :     break;
    1382             :   default: /* case MTpathological: */
    1383           7 :     return gc_NULL(ltop);
    1384             :   }
    1385        9702 :   if (DEBUGLEVEL) {
    1386           0 :     long n = lg(tr)-1;
    1387           0 :     if (n > 1 || mt == MTAtkin)
    1388             :     {
    1389           0 :       err_printf("%3ld trace(s)",n);
    1390           0 :       if (DEBUGLEVEL>1) err_printf(" [%ld ms]", timer_delay(&ti));
    1391             :     }
    1392           0 :     if (n > 1) err_printf("\n");
    1393             :   }
    1394        9702 :   *ptr_kt = kt;
    1395        9702 :   return gerepileupto(ltop, tr);
    1396             : }
    1397             : 
    1398             : /* A partition of compile_atkin in baby and giant is represented as the binary
    1399             :    developpement of an integer; if the i-th bit is 1, the i-th prime in
    1400             :    compile-atkin is a baby. The optimum is obtained when the ratio between
    1401             :    the number of possibilities for traces modulo giants (p_g) and babies (p_b)
    1402             :    is near 3/4. */
    1403             : static long
    1404         889 : separation(GEN cnt)
    1405             : {
    1406             :   pari_sp btop;
    1407         889 :   long k = lg(cnt)-1, l = (1L<<k)-1, best_i, i, j;
    1408             :   GEN best_r, P, P3, r;
    1409             : 
    1410         889 :   P = gen_1;
    1411         889 :   for (j = 1; j <= k; ++j) P = mulis(P, cnt[j]);
    1412             :   /* p_b * p_g = P is constant */
    1413         889 :   P3 = mulsi(3, P);
    1414         889 :   btop = avma;
    1415         889 :   best_i = 0;
    1416         889 :   best_r = P3;
    1417       32564 :   for (i = 1; i < l; i++)
    1418             :   {
    1419             :     /* scan all possibilities */
    1420       31759 :     GEN p_b = gen_1;
    1421      272447 :     for (j = 0; j < k; j++)
    1422      240688 :       if (i & (1L<<j)) p_b = mulis(p_b, cnt[1+j]);
    1423       31759 :     r = subii(shifti(sqri(p_b), 2), P3); /* (p_b/p_g - 3/4)*4*P */
    1424       31759 :     if (!signe(r)) { best_i = i; break; }
    1425       31675 :     if (abscmpii(r, best_r) < 0) { best_i = i; best_r = r; }
    1426       31675 :     if (gc_needed(btop, 1))
    1427           0 :       best_r = gerepileuptoint(btop, best_r);
    1428             :   }
    1429         889 :   return best_i;
    1430             : }
    1431             : 
    1432             : /* x VEC defined modulo P (= *P), y VECSMALL modulo q, (q,P) = 1. */
    1433             : /* Update in place:
    1434             :  *   x to vector mod q P congruent to x mod P (resp. y mod q). */
    1435             : /*   P ( <-- qP ) */
    1436             : static void
    1437        1757 : multiple_crt(GEN x, GEN y, GEN q, GEN P)
    1438             : {
    1439        1757 :   pari_sp ltop = avma, av;
    1440        1757 :   long i, j, k, lx = lg(x)-1, ly = lg(y)-1;
    1441             :   GEN  a1, a2, u, v, A2X;
    1442        1757 :   (void)bezout(P,q,&u,&v);
    1443        1757 :   a1 = mulii(P,u);
    1444        1757 :   a2 = mulii(q,v); A2X = ZC_Z_mul(x, a2);
    1445        1757 :   av = avma; affii(mulii(P,q), P);
    1446       61733 :   for (i = 1, k = 1; i <= lx; i++, set_avma(av))
    1447             :   {
    1448       59976 :     GEN a2x = gel(A2X,i);
    1449     1016960 :     for (j = 1; j <= ly; ++j)
    1450             :     {
    1451      956984 :       GEN t = Fp_add(Fp_mulu(a1, y[j], P), a2x, P);
    1452      956984 :       affii(t, gel(x, k++));
    1453             :     }
    1454             :   }
    1455        1757 :   setlg(x, k); set_avma(ltop);
    1456        1757 : }
    1457             : 
    1458             : /****************************************************************************/
    1459             : /*                              MATCH AND SORT                              */
    1460             : /****************************************************************************/
    1461             : 
    1462             : static GEN
    1463        1778 : possible_traces(GEN compile, GEN mask, GEN *P, int larger)
    1464             : {
    1465        1778 :   GEN V, Pfinal = gen_1, C = shallowextract(compile, mask);
    1466        1778 :   long i, lfinal = 1, lC = lg(C), lP;
    1467        1778 :   pari_sp av = avma;
    1468             : 
    1469        5313 :   for (i = 1; i < lC; i++)
    1470             :   {
    1471        3535 :     GEN c = gel(C,i), t;
    1472        3535 :     Pfinal = mulii(Pfinal, gel(c,1));
    1473        3535 :     t = muluu(lfinal, lg(gel(c,2))-1);
    1474        3535 :     lfinal = itou(t);
    1475             :   }
    1476        1778 :   Pfinal = gerepileuptoint(av, Pfinal);
    1477        1778 :   if (larger)
    1478         889 :     lP = lgefint(shifti(Pfinal,1));
    1479             :   else
    1480         889 :     lP = lgefint(Pfinal);
    1481        1778 :   lfinal++;
    1482             :   /* allocate room for final result */
    1483        1778 :   V = cgetg(lfinal, t_VEC);
    1484        1778 :   for (i = 1; i < lfinal; i++) gel(V,i) = cgeti(lP);
    1485             : 
    1486             :   {
    1487        1778 :     GEN c = gel(C,1), v = gel(c,2);
    1488        1778 :     long l = lg(v);
    1489        1778 :     for (i = 1; i < l; i++) affsi(v[i], gel(V,i));
    1490        1778 :     setlg(V, l); affii(gel(c,1), Pfinal); /* reset Pfinal */
    1491             :   }
    1492        3535 :   for (i = 2; i < lC; i++)
    1493             :   {
    1494        1757 :     GEN c = gel(C,i);
    1495        1757 :     multiple_crt(V, gel(c,2), gel(c,1), Pfinal); /* Pfinal updated! */
    1496             :   }
    1497        1778 :   *P = Pfinal; return V;
    1498             : }
    1499             : 
    1500             : static GEN
    1501      189399 : cost(long mask, GEN cost_vec)
    1502             : {
    1503      189399 :   pari_sp ltop = avma;
    1504             :   long i;
    1505      189399 :   GEN c = gen_1;
    1506     2007950 :   for (i = 1; i < lg(cost_vec); i++)
    1507     1818551 :     if (mask&(1L<<(i-1)))
    1508      788732 :       c = mulis(c, cost_vec[i]);
    1509      189399 :   return gerepileuptoint(ltop, c);
    1510             : }
    1511             : 
    1512             : static GEN
    1513      152166 : value(long mask, GEN atkin, long k)
    1514             : {
    1515      152166 :   pari_sp ltop = avma;
    1516             :   long i;
    1517      152166 :   GEN c = gen_1;
    1518     1613626 :   for (i = 1; i <= k; i++)
    1519     1461460 :     if (mask&(1L<<(i-1)))
    1520      636538 :       c = mulii(c, gmael(atkin, i, 1));
    1521      152166 :   return gerepileuptoint(ltop, c);
    1522             : }
    1523             : 
    1524             : static void
    1525       74739 : set_cost(GEN B, long b, GEN cost_vec, long *pi)
    1526             : {
    1527       74739 :   pari_sp av = avma;
    1528       74739 :   GEN costb = cost(b, cost_vec);
    1529       74739 :   long i = *pi;
    1530       74739 :   while (cmpii(costb, cost(B[i], cost_vec)) < 0) --i;
    1531       74739 :   B[++i] = b;
    1532       74739 :   *pi = i; set_avma(av);
    1533       74739 : }
    1534             : 
    1535             : static GEN
    1536        1862 : get_lgatkin(GEN compile_atkin, long k)
    1537             : {
    1538        1862 :   GEN v = cgetg(k+1, t_VECSMALL);
    1539             :   long j;
    1540        1862 :   for (j = 1; j <= k; ++j) v[j] = lg(gmael(compile_atkin, j, 2))-1;
    1541        1862 :   return v;
    1542             : }
    1543             : 
    1544             : static GEN
    1545         973 : champion(GEN atkin, long k, GEN bound_champ)
    1546             : {
    1547         973 :   const long two_k = 1L<<k;
    1548         973 :   pari_sp ltop = avma;
    1549             :   long i, j, n, i1, i2;
    1550         973 :   GEN B, Bp, cost_vec, res = NULL;
    1551             : 
    1552         973 :   cost_vec = get_lgatkin(atkin, k);
    1553         973 :   if (k == 1) return mkvec2(gen_1, utoipos(cost_vec[1]));
    1554             : 
    1555         959 :   B  = zero_zv(two_k);
    1556         959 :   Bp = zero_zv(two_k);
    1557         959 :   Bp[2] = 1;
    1558        4144 :   for (n = 2, j = 2; j <= k; j++)
    1559             :   {
    1560             :     long b;
    1561        3185 :     i = 1;
    1562       72268 :     for (i1 = 2, i2 = 1; i1 <= n; )
    1563             :     {
    1564       65898 :       pari_sp av = avma;
    1565       65898 :       long b1 = Bp[i1], b2 = Bp[i2]|(1L<<(j-1));
    1566       65898 :       if (cmpii(value(b1, atkin, k), value(b2, atkin, k)) < 0)
    1567       35777 :         { b = b1; i1++; } else { b = b2; i2++; }
    1568       65898 :       set_avma(av);
    1569       65898 :       set_cost(B, b, cost_vec, &i);
    1570             :     }
    1571       12026 :     for ( ; i2 <= n; i2++)
    1572             :     {
    1573        8841 :       b = Bp[i2]|(1L<<(j-1));
    1574        8841 :       set_cost(B, b, cost_vec, &i);
    1575             :     }
    1576        3185 :     n = i;
    1577       57694 :     for (i = 1; i <= n; i++)
    1578       54509 :       Bp[i] = B[i];
    1579             :   }
    1580      232715 :   for (i = 1; i <= two_k; i++)
    1581      231756 :     if (B[i])
    1582             :     {
    1583       16506 :       GEN b = cost (B[i], cost_vec);
    1584       16506 :       GEN v = value(B[i], atkin, k);
    1585       16506 :       if (cmpii(v, bound_champ) <=0) continue;
    1586        1883 :       if (res && gcmp(b, gel(res, 2)) >=0) continue;
    1587         959 :       res = mkvec2(utoi(B[i]), b);
    1588             :     }
    1589         959 :   return gerepilecopy(ltop, res);
    1590             : }
    1591             : 
    1592             : static GEN
    1593        1778 : compute_diff(GEN v)
    1594             : {
    1595        1778 :   pari_sp av = avma;
    1596        1778 :   long i, l = lg(v) - 1;
    1597        1778 :   GEN diff = cgetg(l, t_VEC);
    1598        1778 :   for (i = 1; i < l; i++) gel(diff, i) = subii(gel(v, i+1), gel(v, i));
    1599        1778 :   return gerepileupto(av, ZV_sort_uniq(diff));
    1600             : }
    1601             : 
    1602             : static int
    1603       16198 : cmp_atkin(void*E, GEN a, GEN b)
    1604             : {
    1605       16198 :   long ta=typ(a)==t_INT, tb=typ(b)==t_INT, c;
    1606             :   (void) E;
    1607       16198 :   if (ta || tb) return ta-tb;
    1608        5173 :   c = lg(gel(a,2)) - lg(gel(b,2));
    1609        5173 :   if (c) return c;
    1610         728 :   return cmpii(gel(b,1), gel(a,1));
    1611             : }
    1612             : 
    1613             : static void
    1614        3864 : add_atkin(GEN atkin, GEN trace, long *nb)
    1615             : {
    1616        3864 :   long l = lg(atkin)-1;
    1617        3864 :   long i, k = gen_search(atkin, trace, 1, NULL, cmp_atkin);
    1618        3864 :   if (k==0 || k > l) return;
    1619       75705 :   for (i = l; i > k; i--)
    1620       71841 :     gel(atkin,i) = gel(atkin,i-1);
    1621        3864 :   if (typ(gel(atkin,l))==t_INT) (*nb)++;
    1622        3864 :   gel(atkin,k) = trace;
    1623             : }
    1624             : 
    1625             : /* V = baby / giant, P = Pb / Pg */
    1626             : static GEN
    1627        1778 : BSGS_pre(GEN *pdiff, GEN V, GEN P, void *E, const struct bb_group *grp)
    1628             : {
    1629        1778 :   GEN diff = compute_diff(V);
    1630        1778 :   GEN pre = cgetg(lg(diff), t_VEC);
    1631        1778 :   long i, l = lg(diff);
    1632        1778 :   gel(pre, 1) = grp->pow(E, P, gel(diff, 1));
    1633             :   /* what we'd _really_ want here is a hashtable diff[i] -> pre[i]  */
    1634       33957 :   for (i = 2; i < l; i++)
    1635             :   {
    1636       32179 :     pari_sp av = avma;
    1637       32179 :     GEN d = subii(gel(diff, i), gel(diff, i-1));
    1638       32179 :     GEN Q = grp->mul(E, gel(pre, i-1), grp->pow(E, P, d));
    1639       32179 :     gel(pre, i) = gerepilecopy(av, Q);
    1640             :   }
    1641        1778 :   *pdiff = diff; return pre;
    1642             : }
    1643             : 
    1644             : /* u = trace_elkies, Mu = prod_elkies. Let caller collect garbage */
    1645             : /* Match & sort: variant from Lercier's thesis, section 11.2.3 */
    1646             : /* baby/giant/table updated in place: this routines uses
    1647             :  *   size(baby)+size(giant)+size(table)+size(table_ind) + O(log p)
    1648             :  * bits of stack */
    1649             : static GEN
    1650         945 : match_and_sort(GEN compile_atkin, GEN Mu, GEN u, GEN q, void *E, const struct bb_group *grp)
    1651             : {
    1652             :   pari_sp av1, av2;
    1653         945 :   GEN baby, giant, SgMb, Mb, Mg, den, Sg, dec_inf, div, pp1 = addiu(q,1);
    1654             :   GEN P, Pb, Pg, point, diff, pre, table, table_ind;
    1655         945 :   long best_i, i, lbaby, lgiant, k = lg(compile_atkin)-1;
    1656         945 :   GEN bound = sqrti(shifti(q, 2)), card;
    1657         945 :   const long lcard = 100;
    1658         945 :   long lq = lgefint(q), nbcard;
    1659             :   pari_timer ti;
    1660             : 
    1661         945 :   if (k == 1)
    1662             :   { /*only one Atkin prime, check the cardinality with random points */
    1663          56 :     GEN r = gel(compile_atkin, 1), r1 = gel(r,1), r2 = gel(r,2);
    1664          56 :     long l = lg(r2), j;
    1665          56 :     GEN card = cgetg(l, t_VEC), Cs2, C, U;
    1666          56 :     Z_chinese_pre(Mu, r1, &C,&U, NULL);
    1667          56 :     Cs2 = shifti(C, -1);
    1668         378 :     for (j = 1, i = 1; i < l; i++)
    1669             :     {
    1670         322 :       GEN t = Z_chinese_post(u, stoi(r2[i]), C, U, NULL);
    1671         322 :       t = Fp_center_i(t, C, Cs2);
    1672         322 :       if (abscmpii(t, bound) <= 0) gel(card, j++) = subii(pp1, t);
    1673             :     }
    1674          56 :     setlg(card, j);
    1675          56 :     return gen_select_order(card, E, grp);
    1676             :   }
    1677         889 :   if (DEBUGLEVEL>=2) timer_start(&ti);
    1678         889 :   av1 = avma;
    1679         889 :   best_i = separation( get_lgatkin(compile_atkin, k) );
    1680         889 :   set_avma(av1);
    1681             : 
    1682         889 :   baby  = possible_traces(compile_atkin, utoi(best_i), &Mb, 1);
    1683         889 :   giant = possible_traces(compile_atkin, subiu(int2n(k), best_i+1), &Mg, 0);
    1684         889 :   lbaby = lg(baby);
    1685         889 :   lgiant = lg(giant);
    1686         889 :   den = Fp_inv(Fp_mul(Mu, Mb, Mg), Mg);
    1687         889 :   av2 = avma;
    1688      527037 :   for (i = 1; i < lgiant; i++, set_avma(av2))
    1689      526148 :     affii(Fp_mul(gel(giant,i), den, Mg), gel(giant,i));
    1690         889 :   ZV_sort_inplace(giant);
    1691         889 :   Sg = Fp_mul(negi(u), den, Mg);
    1692         889 :   den = Fp_inv(Fp_mul(Mu, Mg, Mb), Mb);
    1693         889 :   dec_inf = divii(mulii(Mb,addii(Mg,shifti(Sg,1))), shifti(Mg,1));
    1694         889 :   togglesign(dec_inf); /* now, dec_inf = ceil(- (Mb/2 + Sg Mb/Mg) ) */
    1695         889 :   div = mulii(truedivii(dec_inf, Mb), Mb);
    1696         889 :   av2 = avma;
    1697      378749 :   for (i = 1; i < lbaby; i++, set_avma(av2))
    1698             :   {
    1699      377860 :     GEN b = addii(Fp_mul(Fp_sub(gel(baby,i), u, Mb), den, Mb), div);
    1700      377860 :     if (cmpii(b, dec_inf) < 0) b = addii(b, Mb);
    1701      377860 :     affii(b, gel(baby,i));
    1702             :   }
    1703         889 :   ZV_sort_inplace(baby);
    1704             : 
    1705         889 :   SgMb = mulii(Sg, Mb);
    1706         889 :   card = cgetg(lcard+1,t_VEC);
    1707         889 :   for (i = 1; i <= lcard; i++) gel(card,i) = cgetipos(lq+1);
    1708             : 
    1709         889 :   av2 = avma;
    1710             : MATCH_RESTART:
    1711         889 :   set_avma(av2);
    1712         889 :   nbcard = 0;
    1713         889 :   P = grp->rand(E);
    1714         889 :   point = grp->pow(E,P, Mu);
    1715         889 :   Pb = grp->pow(E,point, Mg);
    1716         889 :   Pg = grp->pow(E,point, Mb);
    1717             :   /* Precomputation for babies */
    1718         889 :   pre = BSGS_pre(&diff, baby, Pb, E, grp);
    1719             : 
    1720             :   /*Now we compute the table of babies, this table contains only the */
    1721             :   /*lifted x-coordinate of the points in order to use less memory */
    1722         889 :   table = cgetg(lbaby, t_VECSMALL);
    1723         889 :   av1 = avma;
    1724             :   /* (p+1 - u - Mu*Mb*Sg) P - (baby[1]) Pb */
    1725         889 :   point = grp->pow(E,P, subii(subii(pp1, u), mulii(Mu, addii(SgMb, mulii(Mg, gel(baby,1))))));
    1726         889 :   table[1] = grp->hash(gel(point,1));
    1727      377860 :   for (i = 2; i < lbaby; i++)
    1728             :   {
    1729      376971 :     GEN d = subii(gel(baby, i), gel(baby, i-1));
    1730      376971 :     point =  grp->mul(E, point, grp->pow(E, gel(pre, ZV_search(diff, d)), gen_m1));
    1731      376971 :     table[i] = grp->hash(gel(point,1));
    1732      376971 :     if (gc_needed(av1,3))
    1733             :     {
    1734           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"match_and_sort, baby = %ld", i);
    1735           0 :       point = gerepileupto(av1, point);
    1736             :     }
    1737             :   }
    1738         889 :   set_avma(av1);
    1739             :   /* Precomputations for giants */
    1740         889 :   pre = BSGS_pre(&diff, giant, Pg, E, grp);
    1741             : 
    1742             :   /* Look for a collision among the x-coordinates */
    1743         889 :   table_ind = vecsmall_indexsort(table);
    1744         889 :   table = perm_mul(table,table_ind);
    1745             : 
    1746         889 :   av1 = avma;
    1747         889 :   point = grp->pow(E, Pg, gel(giant, 1));
    1748      526148 :   for (i = 1; ; i++)
    1749      525259 :   {
    1750             :     GEN d;
    1751      526148 :     long h = grp->hash(gel(point, 1));
    1752      526148 :     long s = zv_search(table, h);
    1753      526148 :     if (s) {
    1754         889 :       while (table[s] == h && s) s--;
    1755        1778 :       for (s++; s < lbaby && table[s] == h; s++)
    1756             :       {
    1757         889 :         GEN B = gel(baby,table_ind[s]), G = gel(giant,i);
    1758         889 :         GEN GMb = mulii(G, Mb), BMg = mulii(B, Mg);
    1759         889 :         GEN Be = subii(subii(pp1, u), mulii(Mu, addii(SgMb, BMg)));
    1760         889 :         GEN Bp = grp->pow(E,P, Be);
    1761             :         /* p+1 - u - Mu (Sg Mb + GIANT Mb + BABY Mg) */
    1762         889 :         if (gequal(gel(Bp,1),gel(point,1)))
    1763             :         {
    1764         889 :           GEN card1 = subii(Be, mulii(Mu, GMb));
    1765         889 :           GEN card2 = addii(card1, mulii(mulsi(2,Mu), GMb));
    1766         889 :           if (abscmpii(subii(pp1, card1), bound) <= 0)
    1767         777 :             affii(card1, gel(card, ++nbcard));
    1768         889 :           if (nbcard >= lcard) goto MATCH_RESTART;
    1769         889 :           if (abscmpii(subii(pp1, card2), bound) <= 0)
    1770         476 :             affii(card2, gel(card, ++nbcard));
    1771         889 :           if (nbcard >= lcard) goto MATCH_RESTART;
    1772             :         }
    1773             :       }
    1774             :     }
    1775      526148 :     if (i==lgiant-1) break;
    1776      525259 :     d = subii(gel(giant, i+1), gel(giant, i));
    1777      525259 :     point = grp->mul(E,point, gel(pre, ZV_search(diff, d)));
    1778      525259 :     if (gc_needed(av1,3))
    1779             :     {
    1780           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"match_and_sort, giant = %ld", i);
    1781           0 :       point = gerepileupto(av1, point);
    1782             :     }
    1783             :   }
    1784         889 :   setlg(card, nbcard+1);
    1785         889 :   if (DEBUGLEVEL>=2) timer_printf(&ti,"match_and_sort");
    1786         889 :   return gen_select_order(card, E, grp);
    1787             : }
    1788             : 
    1789             : static GEN
    1790         994 : get_bound_bsgs(long lp)
    1791             : {
    1792             :   GEN B;
    1793         994 :   if (lp <= 160)
    1794         966 :     B = divru(powru(dbltor(1.048), lp), 9);
    1795          28 :   else if (lp <= 192)
    1796          21 :     B = divrr(powru(dbltor(1.052), lp), dbltor(16.65));
    1797             :   else
    1798           7 :     B = mulrr(powru(dbltor(1.035), minss(lp,307)), dbltor(1.35));
    1799         994 :   return mulru(B, 1000000);
    1800             : }
    1801             : 
    1802             : /*FIXME: the name of the function does not quite match what it does*/
    1803             : static const struct bb_group *
    1804         945 : get_FqE_group(void ** pt_E, GEN a4, GEN a6, GEN T, GEN p)
    1805             : {
    1806         945 :   if (!T) return get_FpE_group(pt_E,a4,a6,p);
    1807          42 :   else if (lgefint(p)==3)
    1808             :   {
    1809          34 :     ulong pp = uel(p,2);
    1810          34 :     GEN Tp = ZXT_to_FlxT(T,pp);
    1811          34 :     return get_FlxqE_group(pt_E, Fq_to_Flx(a4, Tp, pp), Fq_to_Flx(a6, Tp, pp),
    1812             :                            Tp, pp);
    1813             :   }
    1814           8 :   return get_FpXQE_group(pt_E,a4,a6,T,p);
    1815             : }
    1816             : 
    1817             : /* E is an elliptic curve defined over Z or over Fp in ellinit format, defined
    1818             :  * by the equation E: y^2 + a1*x*y + a2*y = x^3 + a2*x^2 + a4*x + a6
    1819             :  * p is a prime number
    1820             :  * set smallfact to stop whenever a small factor of the order, not dividing smallfact,
    1821             :  * is detected. Useful when searching for a good curve for cryptographic
    1822             :  * applications */
    1823             : GEN
    1824        1022 : Fq_ellcard_SEA(GEN a4, GEN a6, GEN q, GEN T, GEN p, long smallfact)
    1825             : {
    1826        1022 :   const long MAX_ATKIN = 21;
    1827        1022 :   pari_sp ltop = avma, btop;
    1828             :   long ell, i, nb_atkin, vx,vy;
    1829             :   GEN TR, TR_mod, compile_atkin, bound, bound_bsgs, champ;
    1830        1022 :   GEN prod_atkin = gen_1, max_traces = gen_0;
    1831             :   GEN j;
    1832        1022 :   double bound_gr = 1.;
    1833        1022 :   const double growth_factor = 1.26;
    1834             :   forprime_t TT;
    1835             : 
    1836        1022 :   j = Fq_ellj(a4, a6, T, p);
    1837        1022 :   if (signe(j) == 0 || signe(Fq_sub(j, utoi(1728), T, p)) == 0)
    1838           0 :     return T ? FpXQ_ellcard(Fq_to_FpXQ(a4, T, p), Fq_to_FpXQ(a6, T, p), T, p)
    1839          14 :              : Fp_ellcard(a4, a6, p);
    1840             :   /*First compute the trace modulo 2 */
    1841        1008 :   switch(FqX_nbroots(rhs(a4, a6, 0), T, p))
    1842             :   {
    1843             :   case 3: /* bonus time: 4 | #E(Fq) = q+1 - t */
    1844          77 :     i = mod4(q)+1; if (i > 2) i -= 4;
    1845          77 :     TR_mod = utoipos(4);
    1846          77 :     TR = stoi(i); break;
    1847             :   case 1:
    1848         490 :     TR_mod = gen_2;
    1849         490 :     TR = gen_0; break;
    1850             :   default : /* 0 */
    1851         441 :     TR_mod = gen_2;
    1852         441 :     TR = gen_1; break;
    1853             :   }
    1854        1008 :   if (odd(smallfact) && !mpodd(TR))
    1855             :   {
    1856          14 :     if (DEBUGLEVEL) err_printf("Aborting: #E(Fq) divisible by 2\n");
    1857          14 :     set_avma(ltop); return gen_0;
    1858             :   }
    1859         994 :   vy = fetch_var();
    1860         994 :   vx = fetch_var_higher();
    1861             : 
    1862             :   /* compile_atkin is a vector containing informations about Atkin primes,
    1863             :    * informations about Elkies primes lie in Mod(TR, TR_mod). */
    1864         994 :   u_forprime_init(&TT, 3, ULONG_MAX);
    1865         994 :   bound = sqrti(shifti(q, 4));
    1866         994 :   bound_bsgs = get_bound_bsgs(expi(q));
    1867         994 :   compile_atkin = zerovec(MAX_ATKIN); nb_atkin = 0;
    1868         994 :   btop = avma;
    1869       10724 :   while ( (ell = u_forprime_next(&TT)) )
    1870             :   {
    1871        9730 :     long ellkt, kt = 1, nbtrace;
    1872             :     GEN trace_mod;
    1873        9758 :     if (absequalui(ell, p)) continue;
    1874        9723 :     trace_mod = find_trace(a4, a6, j, ell, q, T, p, &kt, smallfact, vx,vy);
    1875        9723 :     if (!trace_mod) continue;
    1876             : 
    1877        9702 :     nbtrace = lg(trace_mod) - 1;
    1878        9702 :     ellkt = (long)upowuu(ell, kt);
    1879        9702 :     if (nbtrace == 1)
    1880             :     {
    1881        5838 :       long t_mod_ellkt = trace_mod[1];
    1882        5838 :       if (smallfact && smallfact%ell!=0)
    1883             :       { /* does ell divide q + 1 - t ? */
    1884         385 :         long q_mod_ell_plus_one = umodiu(q,ell) + 1;
    1885         385 :         ulong  card_mod_ell = umodsu(q_mod_ell_plus_one - t_mod_ellkt, ell);
    1886         385 :         ulong tcard_mod_ell = 1;
    1887         385 :         if (card_mod_ell && smallfact < 0)
    1888         133 :           tcard_mod_ell = umodsu(q_mod_ell_plus_one + t_mod_ellkt, ell);
    1889         385 :         if (!card_mod_ell || !tcard_mod_ell)
    1890             :         {
    1891          28 :           if (DEBUGLEVEL)
    1892           0 :             err_printf("\nAborting: #E%s(Fq) divisible by %ld\n",
    1893             :                        tcard_mod_ell ? "" : "_twist", ell);
    1894          28 :           delete_var();
    1895          28 :           delete_var();
    1896        1022 :           set_avma(ltop); return gen_0;
    1897             :         }
    1898             :       }
    1899        5810 :       (void)Z_incremental_CRT(&TR, t_mod_ellkt, &TR_mod, ellkt);
    1900        5810 :       if (DEBUGLEVEL)
    1901           0 :         err_printf(", missing %ld bits\n",expi(bound)-expi(TR_mod));
    1902             :     }
    1903             :     else
    1904             :     {
    1905        3864 :       add_atkin(compile_atkin, mkvec2(utoipos(ellkt), trace_mod), &nb_atkin);
    1906        3864 :       prod_atkin = value(-1, compile_atkin, nb_atkin);
    1907             :     }
    1908        9674 :     if (cmpii(mulii(TR_mod, prod_atkin), bound) > 0)
    1909             :     {
    1910             :       GEN bound_tr;
    1911        1008 :       if (!nb_atkin)
    1912             :       {
    1913          21 :         delete_var();
    1914          21 :         delete_var();
    1915          21 :         return gerepileuptoint(ltop, subii(addiu(q, 1), TR));
    1916             :       }
    1917         987 :       bound_tr = mulrr(bound_bsgs, dbltor(bound_gr));
    1918         987 :       bound_gr *= growth_factor;
    1919         987 :       if (signe(max_traces))
    1920             :       {
    1921          42 :         max_traces = divis(muliu(max_traces,nbtrace), ellkt);
    1922          42 :         if (DEBUGLEVEL>=3)
    1923           0 :           err_printf("At least %Ps remaining possibilities.\n",max_traces);
    1924             :       }
    1925         987 :       if (cmpir(max_traces, bound_tr) < 0)
    1926             :       {
    1927         973 :         GEN bound_atkin = truedivii(bound, TR_mod);
    1928         973 :         champ = champion(compile_atkin, nb_atkin, bound_atkin);
    1929         973 :         max_traces = gel(champ,2);
    1930         973 :         if (DEBUGLEVEL>=2)
    1931           0 :           err_printf("%Ps remaining possibilities.\n", max_traces);
    1932         973 :         if (cmpir(max_traces, bound_tr) < 0)
    1933             :         {
    1934         945 :           GEN res, cat = shallowextract(compile_atkin, gel(champ,1));
    1935             :           const struct bb_group *grp;
    1936             :           void *E;
    1937         945 :           if (DEBUGLEVEL)
    1938           0 :             err_printf("Match and sort for %Ps possibilities.\n", max_traces);
    1939         945 :           delete_var();
    1940         945 :           delete_var();
    1941         945 :           grp = get_FqE_group(&E,a4,a6,T,p);
    1942         945 :           res = match_and_sort(cat, TR_mod, TR, q, E, grp);
    1943         945 :           return gerepileuptoint(ltop, res);
    1944             :         }
    1945             :       }
    1946             :     }
    1947        8708 :     if (gc_needed(btop, 1))
    1948           0 :       gerepileall(btop,5, &TR,&TR_mod, &compile_atkin, &max_traces, &prod_atkin);
    1949             :   }
    1950             :   return NULL;/*LCOV_EXCL_LINE*/
    1951             : }
    1952             : 
    1953             : GEN
    1954         973 : Fp_ellcard_SEA(GEN a4, GEN a6, GEN p, long smallfact)
    1955             : {
    1956         973 :   return Fq_ellcard_SEA(a4, a6, p, NULL, p, smallfact);
    1957             : }

Generated by: LCOV version 1.13