Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - ellsea.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21739-44c3c17) Lines: 1119 1168 95.8 %
Date: 2018-01-20 06:18:48 Functions: 81 83 97.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11