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

Generated by: LCOV version 1.13