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 - ellisog.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.1 lcov report (development 22708-0f0e6fe44) Lines: 943 977 96.5 %
Date: 2018-06-18 05:36:21 Functions: 79 80 98.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2014 The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. 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             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Return 1 if the point Q is a Weierstrass (2-torsion) point of the
      18             :  * curve E, return 0 otherwise */
      19             : static long
      20         903 : ellisweierstrasspoint(GEN E, GEN Q)
      21         903 : { return ell_is_inf(Q) || gequal0(ec_dmFdy_evalQ(E, Q)); }
      22             : 
      23             : 
      24             : /* Given an elliptic curve E = [a1, a2, a3, a4, a6] and t,w in the ring of
      25             :  * definition of E, return the curve
      26             :  *  E' = [a1, a2, a3, a4 - 5t, a6 - (E.b2 t + 7w)] */
      27             : static GEN
      28        3640 : make_velu_curve(GEN E, GEN t, GEN w)
      29             : {
      30        3640 :   GEN A4, A6, a1 = ell_get_a1(E), a2 = ell_get_a2(E), a3 = ell_get_a3(E);
      31        3640 :   A4 = gsub(ell_get_a4(E), gmulsg(5L, t));
      32        3640 :   A6 = gsub(ell_get_a6(E), gadd(gmul(ell_get_b2(E), t), gmulsg(7L, w)));
      33        3640 :   return mkvec5(a1,a2,a3,A4,A6);
      34             : }
      35             : 
      36             : /* If phi = (f(x)/h(x)^2, g(x,y)/h(x)^3) is an isogeny, return the
      37             :  * variables x and y in a vecsmall */
      38             : INLINE void
      39        1708 : get_isog_vars(GEN phi, long *vx, long *vy)
      40             : {
      41        1708 :   *vx = varn(gel(phi, 1));
      42        1708 :   *vy = varn(gel(phi, 2));
      43        1708 :   if (*vy == *vx) *vy = gvar2(gel(phi,2));
      44        1708 : }
      45             : 
      46             : static GEN
      47        4928 : RgX_homogenous_evalpow(GEN P, GEN A, GEN B)
      48             : {
      49        4928 :   pari_sp av = avma;
      50             :   long d, i, v;
      51             :   GEN s;
      52        4928 :   if (typ(P)!=t_POL)
      53        1512 :     return mkvec2(P, gen_1);
      54        3416 :   d = degpol(P); v = varn(A);
      55        3416 :   s = scalarpol_shallow(gel(P, d+2), v);
      56       17794 :   for (i = d-1; i >= 0; i--)
      57             :   {
      58       14378 :     s = gadd(gmul(s, A), gmul(gel(B,d+1-i), gel(P,i+2)));
      59       14378 :     if (gc_needed(av,1))
      60             :     {
      61           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
      62           0 :       s = gerepileupto(av, s);
      63             :     }
      64             :   }
      65        3416 :   s = gerepileupto(av, s);
      66        3416 :   return mkvec2(s, gel(B,d+1));
      67             : }
      68             : 
      69             : static GEN
      70        1792 : RgXQX_homogenous_evalpow(GEN P, GEN A, GEN B, GEN T)
      71             : {
      72        1792 :   pari_sp av = avma;
      73        1792 :   long i, d = degpol(P), v = varn(A);
      74             :   GEN s;
      75        1792 :   if (signe(P)==0) return mkvec2(pol_0(v), pol_1(v));
      76        1568 :   s = scalarpol_shallow(gel(P, d+2), v);
      77        5229 :   for (i = d-1; i >= 0; i--)
      78             :   {
      79        3661 :     s = RgX_add(RgXQX_mul(s, A, T), RgXQX_RgXQ_mul(gel(B,d+1-i), gel(P,i+2), T));
      80        3661 :     if (gc_needed(av,1))
      81             :     {
      82           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
      83           0 :       s = gerepileupto(av, s);
      84             :     }
      85             :   }
      86        1568 :   s = gerepileupto(av, s);
      87        1568 :   return mkvec2(s, gel(B,d+1));
      88             : }
      89             : 
      90             : /* Given isogenies F:E' -> E and G:E'' -> E', return the composite
      91             :  * isogeny F o G:E'' -> E */
      92             : static GEN
      93        1239 : ellcompisog(GEN F, GEN G)
      94             : {
      95        1239 :   pari_sp av = avma;
      96             :   GEN Fv, Gh, Gh2, Gh3, f, g, h, h2, h3, den, num;
      97             :   GEN K, K2, K3, F0, F1, g0, g1, Gp;
      98             :   long v, vx, vy, d;
      99        1239 :   checkellisog(F);
     100        1232 :   checkellisog(G);
     101        1232 :   get_isog_vars(F, &vx, &vy);
     102        1232 :   v = fetch_var_higher();
     103        1232 :   Fv = shallowcopy(gel(F,3)); setvarn(Fv, v);
     104        1232 :   Gh = gel(G,3); Gh2 = gsqr(Gh); Gh3 = gmul(Gh, Gh2);
     105        1232 :   K = gmul(polresultant0(Fv, deg1pol(gneg(Gh2),gel(G,1), v), v, 0), Gh);
     106        1232 :   delete_var();
     107        1232 :   K = RgX_normalize(RgX_div(K, RgX_gcd(K,deriv(K,0))));
     108        1232 :   K2 = gsqr(K); K3 = gmul(K, K2);
     109        1232 :   F0 = polcoeff0(gel(F,2), 0, vy); F1 = polcoeff0(gel(F,2), 1, vy);
     110        1232 :   d = maxss(maxss(degpol(gel(F,1)),degpol(gel(F,3))),maxss(degpol(F0),degpol(F1)));
     111        1232 :   Gp = gpowers(Gh2, d);
     112        1232 :   f  = RgX_homogenous_evalpow(gel(F,1), gel(G,1), Gp);
     113        1232 :   g0 = RgX_homogenous_evalpow(F0, gel(G,1), Gp);
     114        1232 :   g1 = RgX_homogenous_evalpow(F1, gel(G,1), Gp);
     115        1232 :   h =  RgX_homogenous_evalpow(gel(F,3), gel(G,1), Gp);
     116        1232 :   h2 = mkvec2(gsqr(gel(h,1)), gsqr(gel(h,2)));
     117        1232 :   h3 = mkvec2(gmul(gel(h,1),gel(h2,1)), gmul(gel(h,2),gel(h2,2)));
     118        1232 :   f  = gdiv(gmul(gmul(K2, gel(f,1)),gel(h2,2)), gmul(gel(f,2), gel(h2,1)));
     119        1232 :   den = gmul(Gh3, gel(g1,2));
     120        1232 :   num = gadd(gmul(gel(g0,1),den), gmul(gmul(gel(G,2),gel(g1,1)),gel(g0,2)));
     121        1232 :   g = gdiv(gmul(gmul(K3,num),gel(h3,2)),gmul(gmul(gel(g0,2),den), gel(h3,1)));
     122        1232 :   return gerepilecopy(av, mkvec3(f,g,K));
     123             : }
     124             : 
     125             : static GEN
     126        4032 : to_RgX(GEN P, long vx)
     127             : {
     128        4032 :   return typ(P) == t_POL ? lift(P): scalarpol_shallow(lift(P), vx);
     129             : }
     130             : 
     131             : static GEN
     132         448 : divy(GEN P0, GEN P1, GEN Q, GEN T, long vy)
     133             : {
     134         448 :   GEN DP0, P0r = Q_remove_denom(P0, &DP0), P0D;
     135         448 :   GEN DP1, P1r = Q_remove_denom(P1, &DP1), P1D;
     136         448 :   GEN DQ, Qr = Q_remove_denom(Q, &DQ), P2;
     137         448 :   P0D = RgXQX_div(P0r, Qr, T);
     138         448 :   if (DP0) P0D = gdiv(P0D, DP0);
     139         448 :   P1D = RgXQX_div(P1r, Qr, T);
     140         448 :   if (DP1) P1D = gdiv(P1D, DP1);
     141         448 :   P2 = gadd(gmul(P1D, pol_x(vy)), P0D);
     142         448 :   if (DQ) P2 = gmul(P2, DQ);
     143         448 :   return P2;
     144             : }
     145             : 
     146             : static GEN
     147        1666 : ellnfcompisog(GEN nf, GEN F, GEN G)
     148             : {
     149        1666 :   pari_sp av = avma;
     150             :   GEN Fv, Gh, Gh2, Gh3, f, g, gd, h, h21, h22, h31, h32, den;
     151             :   GEN K, K2, K3, F0, F1, G0, G1, g0, g1, Gp;
     152             :   GEN num0, num1, gn0, gn1;
     153             :   GEN g0d, g01, k3h32;
     154             :   GEN T, res;
     155             :   pari_timer ti;
     156             :   long v, vx, vy, d;
     157        1666 :   if (!nf) return ellcompisog(F, G);
     158         448 :   T = nf_get_pol(nf);
     159         448 :   timer_start(&ti);
     160         448 :   checkellisog(F);
     161         448 :   checkellisog(G);
     162         448 :   get_isog_vars(F, &vx, &vy);
     163         448 :   v = fetch_var_higher();
     164         448 :   Fv = shallowcopy(gel(F,3)); setvarn(Fv, v);
     165         448 :   Gh = lift(gel(G,3)); Gh2 = RgXQX_sqr(Gh, T); Gh3 = RgXQX_mul(Gh, Gh2, T);
     166         448 :   res = to_RgX(polresultant0(Fv, deg1pol(gmul(gneg(Gh2),gmodulo(gen_1,T)),gel(G,1), v), v, 0),vx);
     167         448 :   delete_var();
     168         448 :   K = Q_remove_denom(RgXQX_mul(res, Gh, T), NULL);
     169         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: resultant");
     170         448 :   K = RgXQX_div(K, nfgcd(K, deriv(K,0), T, NULL), T);
     171         448 :   K = RgX_normalize(K);
     172         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: nfgcd");
     173         448 :   K2 = RgXQX_sqr(K, T); K3 = RgXQX_mul(K, K2, T);
     174         448 :   F0 = to_RgX(polcoeff0(gel(F,2), 0, vy), vx);
     175         448 :   F1 = to_RgX(polcoeff0(gel(F,2), 1, vy), vx);
     176         448 :   G0 = to_RgX(polcoeff0(gel(G,2), 0, vy), vx);
     177         448 :   G1 = to_RgX(polcoeff0(gel(G,2), 1, vy), vx);
     178         448 :   d = maxss(maxss(degpol(gel(F,1)),degpol(gel(F,3))),maxss(degpol(F0),degpol(F1)));
     179         448 :   Gp = RgXQX_powers(Gh2, d, T);
     180         448 :   f  = RgXQX_homogenous_evalpow(to_RgX(gel(F,1),vx), gel(G,1), Gp, T);
     181         448 :   g0 = RgXQX_homogenous_evalpow(F0, to_RgX(gel(G,1),vx), Gp, T);
     182         448 :   g1 = RgXQX_homogenous_evalpow(F1, to_RgX(gel(G,1),vx), Gp, T);
     183         448 :   h  = RgXQX_homogenous_evalpow(to_RgX(gel(F,3),vx), gel(G,1), Gp, T);
     184         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: evalpow");
     185         448 :   h21 = RgXQX_sqr(gel(h,1),T);
     186         448 :   h22 = RgXQX_sqr(gel(h,2),T);
     187         448 :   h31 = RgXQX_mul(gel(h,1), h21,T);
     188         448 :   h32 = RgXQX_mul(gel(h,2), h22,T);
     189         448 :   if (DEBUGLEVEL) timer_printf(&ti,"h");
     190         448 :   f  = RgXQX_div(RgXQX_mul(RgXQX_mul(K2, gel(f,1), T), h22, T),
     191         448 :                            RgXQX_mul(gel(f,2), h21, T), T);
     192         448 :   if (DEBUGLEVEL) timer_printf(&ti,"f");
     193         448 :   den = RgXQX_mul(Gh3, gel(g1,2), T);
     194         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: den");
     195         448 :   g0d = RgXQX_mul(gel(g0,1),den, T);
     196         448 :   g01 = RgXQX_mul(gel(g1,1),gel(g0,2),T);
     197         448 :   num0 = RgX_add(g0d, RgXQX_mul(G0,g01, T));
     198         448 :   num1 = RgXQX_mul(G1,g01, T);
     199         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: num");
     200         448 :   k3h32 = RgXQX_mul(K3,h32,T);
     201         448 :   gn0 = RgXQX_mul(num0, k3h32, T);
     202         448 :   gn1 = RgXQX_mul(num1, k3h32, T);
     203         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: gn");
     204         448 :   gd = RgXQX_mul(RgXQX_mul(gel(g0,2), den, T), h31, T);
     205         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: gd");
     206         448 :   g = divy(gn0, gn1, gd, T, vy);
     207         448 :   if (DEBUGLEVEL) timer_printf(&ti,"ellnfcompisog: divy");
     208         448 :   return gerepilecopy(av, gmul(mkvec3(f,g,K),gmodulo(gen_1,T)));
     209             : }
     210             : 
     211             : /* Given an isogeny phi from ellisogeny() and a point P in the domain of phi,
     212             :  * return phi(P) */
     213             : GEN
     214          70 : ellisogenyapply(GEN phi, GEN P)
     215             : {
     216          70 :   pari_sp ltop = avma;
     217             :   GEN f, g, h, img_f, img_g, img_h, img_h2, img_h3, img, tmp;
     218             :   long vx, vy;
     219          70 :   if (lg(P) == 4) return ellcompisog(phi,P);
     220          49 :   checkellisog(phi);
     221          49 :   checkellpt(P);
     222          42 :   if (ell_is_inf(P)) return ellinf();
     223          28 :   f = gel(phi, 1);
     224          28 :   g = gel(phi, 2);
     225          28 :   h = gel(phi, 3);
     226          28 :   get_isog_vars(phi, &vx, &vy);
     227          28 :   img_h = poleval(h, gel(P, 1));
     228          28 :   if (gequal0(img_h)) { avma = ltop; return ellinf(); }
     229             : 
     230          21 :   img_h2 = gsqr(img_h);
     231          21 :   img_h3 = gmul(img_h, img_h2);
     232          21 :   img_f = poleval(f, gel(P, 1));
     233             :   /* FIXME: This calculation of g is perhaps not as efficient as it could be */
     234          21 :   tmp = gsubst(g, vx, gel(P, 1));
     235          21 :   img_g = gsubst(tmp, vy, gel(P, 2));
     236          21 :   img = cgetg(3, t_VEC);
     237          21 :   gel(img, 1) = gdiv(img_f, img_h2);
     238          21 :   gel(img, 2) = gdiv(img_g, img_h3);
     239          21 :   return gerepileupto(ltop, img);
     240             : }
     241             : 
     242             : /* isog = [f, g, h] = [x, y, 1] = identity */
     243             : static GEN
     244         252 : isog_identity(long vx, long vy)
     245         252 : { return mkvec3(pol_x(vx), pol_x(vy), pol_1(vx)); }
     246             : 
     247             : /* Returns an updated value for isog based (primarily) on tQ and uQ. Used to
     248             :  * iteratively compute the isogeny corresponding to a subgroup generated by a
     249             :  * given point. Ref: Equation 8 in Velu's paper.
     250             :  * isog = NULL codes the identity */
     251             : static GEN
     252         532 : update_isogeny_polys(GEN isog, GEN E, GEN Q, GEN tQ, GEN uQ, long vx, long vy)
     253             : {
     254         532 :   pari_sp ltop = avma, av;
     255         532 :   GEN xQ = gel(Q, 1), yQ = gel(Q, 2);
     256         532 :   GEN rt = deg1pol_shallow(gen_1, gneg(xQ), vx);
     257         532 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E);
     258             : 
     259         532 :   GEN gQx = ec_dFdx_evalQ(E, Q);
     260         532 :   GEN gQy = ec_dFdy_evalQ(E, Q);
     261             :   GEN tmp1, tmp2, tmp3, tmp4, f, g, h, rt_sqr, res;
     262             : 
     263             :   /* g -= uQ * (2 * y + E.a1 * x + E.a3)
     264             :    *   + tQ * rt * (E.a1 * rt + y - yQ)
     265             :    *   + rt * (E.a1 * uQ - gQx * gQy) */
     266         532 :   av = avma;
     267         532 :   tmp1 = gmul(uQ, gadd(deg1pol_shallow(gen_2, gen_0, vy),
     268             :                        deg1pol_shallow(a1, a3, vx)));
     269         532 :   tmp1 = gerepileupto(av, tmp1);
     270         532 :   av = avma;
     271         532 :   tmp2 = gmul(tQ, gadd(gmul(a1, rt),
     272             :                        deg1pol_shallow(gen_1, gneg(yQ), vy)));
     273         532 :   tmp2 = gerepileupto(av, tmp2);
     274         532 :   av = avma;
     275         532 :   tmp3 = gsub(gmul(a1, uQ), gmul(gQx, gQy));
     276         532 :   tmp3 = gerepileupto(av, tmp3);
     277             : 
     278         532 :   if (!isog) isog = isog_identity(vx,vy);
     279         532 :   f = gel(isog, 1);
     280         532 :   g = gel(isog, 2);
     281         532 :   h = gel(isog, 3);
     282         532 :   rt_sqr = gsqr(rt);
     283         532 :   res = cgetg(4, t_VEC);
     284         532 :   av = avma;
     285         532 :   tmp4 = gdiv(gadd(gmul(tQ, rt), uQ), rt_sqr);
     286         532 :   gel(res, 1) = gerepileupto(av, gadd(f, tmp4));
     287         532 :   av = avma;
     288         532 :   tmp4 = gadd(tmp1, gmul(rt, gadd(tmp2, tmp3)));
     289         532 :   gel(res, 2) = gerepileupto(av, gsub(g, gdiv(tmp4, gmul(rt, rt_sqr))));
     290         532 :   av = avma;
     291         532 :   gel(res, 3) = gerepileupto(av, gmul(h, rt));
     292         532 :   return gerepileupto(ltop, res);
     293             : }
     294             : 
     295             : /* Given a point P on E, return the curve E/<P> and, if only_image is zero,
     296             :  * the isogeny pi: E -> E/<P>. The variables vx and vy are used to describe
     297             :  * the isogeny (ignored if only_image is zero) */
     298             : static GEN
     299         427 : isogeny_from_kernel_point(GEN E, GEN P, int only_image, long vx, long vy)
     300             : {
     301         427 :   pari_sp av = avma;
     302             :   GEN isog, EE, f, g, h, h2, h3;
     303         427 :   GEN Q = P, t = gen_0, w = gen_0;
     304             :   long c;
     305         427 :   if (!oncurve(E,P))
     306           7 :     pari_err_DOMAIN("isogeny_from_kernel_point", "point", "not on", E, P);
     307         420 :   if (ell_is_inf(P))
     308             :   {
     309          42 :     if (only_image) return E;
     310          28 :     return mkvec2(E, isog_identity(vx,vy));
     311             :   }
     312             : 
     313         378 :   isog = NULL; c = 1;
     314             :   for (;;)
     315         525 :   {
     316         903 :     GEN tQ, xQ = gel(Q,1), uQ = ec_2divpol_evalx(E, xQ);
     317         903 :     int stop = 0;
     318         903 :     if (ellisweierstrasspoint(E,Q))
     319             :     { /* ord(P)=2c; take Q=[c]P into consideration and stop */
     320         196 :       tQ = ec_dFdx_evalQ(E, Q);
     321         196 :       stop = 1;
     322             :     }
     323             :     else
     324         707 :       tQ = ec_half_deriv_2divpol_evalx(E, xQ);
     325         903 :     t = gadd(t, tQ);
     326         903 :     w = gadd(w, gadd(uQ, gmul(tQ, xQ)));
     327         903 :     if (!only_image) isog = update_isogeny_polys(isog, E, Q,tQ,uQ, vx,vy);
     328         903 :     if (stop) break;
     329             : 
     330         707 :     Q = elladd(E, P, Q);
     331         707 :     ++c;
     332             :     /* IF x([c]P) = x([c-1]P) THEN [c]P = -[c-1]P and [2c-1]P = 0 */
     333         707 :     if (gequal(gel(Q,1), xQ)) break;
     334         525 :     if (gc_needed(av,1))
     335             :     {
     336           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"isogeny_from_kernel_point");
     337           0 :       gerepileall(av, isog? 4: 3, &Q, &t, &w, &isog);
     338             :     }
     339             :   }
     340             : 
     341         378 :   EE = make_velu_curve(E, t, w);
     342         378 :   if (only_image) return EE;
     343             : 
     344         224 :   if (!isog) isog = isog_identity(vx,vy);
     345         224 :   f = gel(isog, 1);
     346         224 :   g = gel(isog, 2);
     347         224 :   if ( ! (typ(f) == t_RFRAC && typ(g) == t_RFRAC))
     348           0 :     pari_err_BUG("isogeny_from_kernel_point (f or g has wrong type)");
     349             : 
     350             :   /* Clean up the isogeny polynomials f, g and h so that the isogeny
     351             :    * is given by (x,y) -> (f(x)/h(x)^2, g(x,y)/h(x)^3) */
     352         224 :   h = gel(isog, 3);
     353         224 :   h2 = gsqr(h);
     354         224 :   h3 = gmul(h, h2);
     355         224 :   f = gmul(f, h2);
     356         224 :   g = gmul(g, h3);
     357         224 :   if (typ(f) != t_POL || typ(g) != t_POL)
     358           0 :     pari_err_BUG("isogeny_from_kernel_point (wrong denominator)");
     359         224 :   return mkvec2(EE, mkvec3(f,g, gel(isog,3)));
     360             : }
     361             : 
     362             : /* Given a t_POL x^n - s1 x^{n-1} + s2 x^{n-2} - s3 x^{n-3} + ...
     363             :  * return the first three power sums (Newton's identities):
     364             :  *   p1 = s1
     365             :  *   p2 = s1^2 - 2 s2
     366             :  *   p3 = (s1^2 - 3 s2) s1 + 3 s3 */
     367             : static void
     368        3276 : first_three_power_sums(GEN pol, GEN *p1, GEN *p2, GEN *p3)
     369             : {
     370        3276 :   long d = degpol(pol);
     371             :   GEN s1, s2, ms3;
     372             : 
     373        3276 :   *p1 = s1 = gneg(RgX_coeff(pol, d-1));
     374             : 
     375        3276 :   s2 = RgX_coeff(pol, d-2);
     376        3276 :   *p2 = gsub(gsqr(s1), gmulsg(2L, s2));
     377             : 
     378        3276 :   ms3 = RgX_coeff(pol, d-3);
     379        3276 :   *p3 = gadd(gmul(s1, gsub(*p2, s2)), gmulsg(-3L, ms3));
     380        3276 : }
     381             : 
     382             : 
     383             : /* Let E and a t_POL h of degree 1 or 3 whose roots are 2-torsion points on E.
     384             :  * - if only_image != 0, return [t, w] used to compute the equation of the
     385             :  *   quotient by the given 2-torsion points
     386             :  * - else return [t,w, f,g,h], along with the contributions f, g and
     387             :  *   h to the isogeny giving the quotient by h. Variables vx and vy are used
     388             :  *   to create f, g and h, or ignored if only_image is zero */
     389             : 
     390             : /* deg h = 1; 2-torsion contribution from Weierstrass point */
     391             : static GEN
     392        1498 : contrib_weierstrass_pt(GEN E, GEN h, long only_image, long vx, long vy)
     393             : {
     394        1498 :   GEN p = ellbasechar(E);
     395        1498 :   GEN a1 = ell_get_a1(E);
     396        1498 :   GEN a3 = ell_get_a3(E);
     397        1498 :   GEN x0 = gneg(constant_coeff(h)); /* h = x - x0 */
     398        1498 :   GEN b = gadd(gmul(a1,x0), a3);
     399             :   GEN y0, Q, t, w, t1, t2, f, g;
     400             : 
     401        1498 :   if (!equalis(p, 2L)) /* char(k) != 2 ==> y0 = -b/2 */
     402        1456 :     y0 = gmul2n(gneg(b), -1);
     403             :   else
     404             :   { /* char(k) = 2 ==> y0 = sqrt(f(x0)) where E is y^2 + h(x) = f(x). */
     405          42 :     if (!gequal0(b)) pari_err_BUG("two_torsion_contrib (a1*x0+a3 != 0)");
     406          42 :     y0 = gsqrt(ec_f_evalx(E, x0), 0);
     407             :   }
     408        1498 :   Q = mkvec2(x0, y0);
     409        1498 :   t = ec_dFdx_evalQ(E, Q);
     410        1498 :   w = gmul(x0, t);
     411        1498 :   if (only_image) return mkvec2(t,w);
     412             : 
     413             :   /* Compute isogeny, f = (x - x0) * t */
     414         504 :   f = deg1pol_shallow(t, gmul(t, gneg(x0)), vx);
     415             : 
     416             :   /* g = (x - x0) * t * (a1 * (x - x0) + (y - y0)) */
     417         504 :   t1 = deg1pol_shallow(a1, gmul(a1, gneg(x0)), vx);
     418         504 :   t2 = deg1pol_shallow(gen_1, gneg(y0), vy);
     419         504 :   g = gmul(f, gadd(t1, t2));
     420         504 :   return mkvec5(t, w, f, g, h);
     421             : }
     422             : /* deg h =3; full 2-torsion contribution. NB: assume h is monic; base field
     423             :  * characteristic is odd or zero (cannot happen in char 2).*/
     424             : static GEN
     425          14 : contrib_full_tors(GEN E, GEN h, long only_image, long vx, long vy)
     426             : {
     427             :   GEN p1, p2, p3, half_b2, half_b4, t, w, f, g;
     428          14 :   first_three_power_sums(h, &p1,&p2,&p3);
     429          14 :   half_b2 = gmul2n(ell_get_b2(E), -1);
     430          14 :   half_b4 = gmul2n(ell_get_b4(E), -1);
     431             : 
     432             :   /* t = 3*(p2 + b4/2) + p1 * b2/2 */
     433          14 :   t = gadd(gmulsg(3L, gadd(p2, half_b4)), gmul(p1, half_b2));
     434             : 
     435             :   /* w = 3 * p3 + p2 * b2/2 + p1 * b4/2 */
     436          14 :   w = gadd(gmulsg(3L, p3), gadd(gmul(p2, half_b2),
     437             :                                 gmul(p1, half_b4)));
     438          14 :   if (only_image) return mkvec2(t,w);
     439             : 
     440             :   /* Compute isogeny */
     441             :   {
     442           7 :     GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), t1, t2;
     443           7 :     GEN s1 = gneg(RgX_coeff(h, 2));
     444           7 :     GEN dh = RgX_deriv(h);
     445           7 :     GEN psi2xy = gadd(deg1pol_shallow(a1, a3, vx),
     446             :                       deg1pol_shallow(gen_2, gen_0, vy));
     447             : 
     448             :     /* f = -3 (3 x + b2/2 + s1) h + (3 x^2 + (b2/2) x + (b4/2)) h'*/
     449           7 :     t1 = RgX_mul(h, gmulsg(-3, deg1pol(stoi(3), gadd(half_b2, s1), vx)));
     450           7 :     t2 = mkpoln(3, stoi(3), half_b2, half_b4);
     451           7 :     setvarn(t2, vx);
     452           7 :     t2 = RgX_mul(dh, t2);
     453           7 :     f = RgX_add(t1, t2);
     454             : 
     455             :     /* 2g = psi2xy * (f'*h - f*h') - (a1*f + a3*h) * h; */
     456           7 :     t1 = RgX_sub(RgX_mul(RgX_deriv(f), h), RgX_mul(f, dh));
     457           7 :     t2 = RgX_mul(h, RgX_add(RgX_Rg_mul(f, a1), RgX_Rg_mul(h, a3)));
     458           7 :     g = RgX_divs(gsub(gmul(psi2xy, t1), t2), 2L);
     459             : 
     460           7 :     f = RgX_mul(f, h);
     461           7 :     g = RgX_mul(g, h);
     462             :   }
     463           7 :   return mkvec5(t, w, f, g, h);
     464             : }
     465             : 
     466             : /* Given E and a t_POL T whose roots define a subgroup G of E, return the factor
     467             :  * of T that corresponds to the 2-torsion points E[2] \cap G in G */
     468             : INLINE GEN
     469        3269 : two_torsion_part(GEN E, GEN T)
     470        3269 : { return RgX_gcd(T, elldivpol(E, 2, varn(T))); }
     471             : 
     472             : /* Return the jth Hasse derivative of the polynomial f = \sum_{i=0}^n a_i x^i,
     473             :  * i.e. \sum_{i=j}^n a_i \binom{i}{j} x^{i-j}. It is a derivation even when the
     474             :  * coefficient ring has positive characteristic */
     475             : static GEN
     476          98 : derivhasse(GEN f, ulong j)
     477             : {
     478          98 :   ulong i, d = degpol(f);
     479             :   GEN df;
     480          98 :   if (gequal0(f) || d == 0) return pol_0(varn(f));
     481          56 :   if (j == 0) return gcopy(f);
     482          56 :   df = cgetg(2 + (d-j+1), t_POL);
     483          56 :   df[1] = f[1];
     484          56 :   for (i = j; i <= d; ++i) gel(df, i-j+2) = gmul(binomialuu(i,j), gel(f, i+2));
     485          56 :   return normalizepol(df);
     486             : }
     487             : 
     488             : static GEN
     489         812 : non_two_torsion_abscissa(GEN E, GEN h0, GEN x)
     490             : {
     491             :   GEN mp1, dh0, ddh0, t, u, t1, t2, t3;
     492         812 :   long m = degpol(h0);
     493         812 :   mp1 = gel(h0, m + 1); /* negative of first power sum */
     494         812 :   dh0 = RgX_deriv(h0);
     495         812 :   ddh0 = RgX_deriv(dh0);
     496         812 :   t = ec_2divpol_evalx(E, x);
     497         812 :   u = ec_half_deriv_2divpol_evalx(E, x);
     498         812 :   t1 = RgX_sub(RgX_sqr(dh0), RgX_mul(ddh0, h0));
     499         812 :   t2 = RgX_mul(u, RgX_mul(h0, dh0));
     500         812 :   t3 = RgX_mul(RgX_sqr(h0),
     501         812 :                deg1pol_shallow(stoi(2*m), gmulsg(2L, mp1), varn(x)));
     502             :   /* t * (dh0^2 - ddh0*h0) - u*dh0*h0 + (2*m*x - 2*s1) * h0^2); */
     503         812 :   return RgX_add(RgX_sub(RgX_mul(t, t1), t2), t3);
     504             : }
     505             : 
     506             : static GEN
     507        1288 : isog_abscissa(GEN E, GEN kerp, GEN h0, GEN x, GEN two_tors)
     508             : {
     509             :   GEN f0, f2, h2, t1, t2, t3;
     510        1288 :   f0 = (degpol(h0) > 0)? non_two_torsion_abscissa(E, h0, x): pol_0(varn(x));
     511        1288 :   f2 = gel(two_tors, 3);
     512        1288 :   h2 = gel(two_tors, 5);
     513             : 
     514             :   /* Combine f0 and f2 into the final abscissa of the isogeny. */
     515        1288 :   t1 = RgX_mul(x, RgX_sqr(kerp));
     516        1288 :   t2 = RgX_mul(f2, RgX_sqr(h0));
     517        1288 :   t3 = RgX_mul(f0, RgX_sqr(h2));
     518             :   /* x * kerp^2 + f2 * h0^2 + f0 * h2^2 */
     519        1288 :   return RgX_add(t1, RgX_add(t2, t3));
     520             : }
     521             : 
     522             : static GEN
     523        1239 : non_two_torsion_ordinate_char_not2(GEN E, GEN f, GEN h, GEN psi2)
     524             : {
     525        1239 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E);
     526        1239 :   GEN df = RgX_deriv(f), dh = RgX_deriv(h);
     527             :   /* g = df * h * psi2/2 - f * dh * psi2
     528             :    *   - (E.a1 * f + E.a3 * h^2) * h/2 */
     529        1239 :   GEN t1 = RgX_mul(df, RgX_mul(h, RgX_divs(psi2, 2L)));
     530        1239 :   GEN t2 = RgX_mul(f, RgX_mul(dh, psi2));
     531        1239 :   GEN t3 = RgX_mul(RgX_divs(h, 2L),
     532             :                    RgX_add(RgX_Rg_mul(f, a1), RgX_Rg_mul(RgX_sqr(h), a3)));
     533        1239 :   return RgX_sub(RgX_sub(t1, t2), t3);
     534             : }
     535             : 
     536             : /* h = kerq */
     537             : static GEN
     538          49 : non_two_torsion_ordinate_char2(GEN E, GEN h, GEN x, GEN y)
     539             : {
     540          49 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), a4 = ell_get_a4(E);
     541          49 :   GEN b2 = ell_get_b2(E), b4 = ell_get_b4(E), b6 = ell_get_b6(E);
     542             :   GEN h2, dh, dh2, ddh, D2h, D2dh, H, psi2, u, t, alpha;
     543             :   GEN p1, t1, t2, t3, t4;
     544          49 :   long m, vx = varn(x);
     545             : 
     546          49 :   h2 = RgX_sqr(h);
     547          49 :   dh = RgX_deriv(h);
     548          49 :   dh2 = RgX_sqr(dh);
     549          49 :   ddh = RgX_deriv(dh);
     550          49 :   H = RgX_sub(dh2, RgX_mul(h, ddh));
     551          49 :   D2h = derivhasse(h, 2);
     552          49 :   D2dh = derivhasse(dh, 2);
     553          49 :   psi2 = deg1pol_shallow(a1, a3, vx);
     554          49 :   u = mkpoln(3, b2, gen_0, b6);
     555          49 :   setvarn(u, vx);
     556          49 :   t = deg1pol_shallow(b2, b4, vx);
     557          49 :   alpha = mkpoln(4, a1, a3, gmul(a1, a4), gmul(a3, a4));
     558          49 :   setvarn(alpha, vx);
     559          49 :   m = degpol(h);
     560          49 :   p1 = RgX_coeff(h, m-1); /* first power sum */
     561             : 
     562          49 :   t1 = gmul(gadd(gmul(a1, p1), gmulgs(a3, m)), RgX_mul(h,h2));
     563             : 
     564          49 :   t2 = gmul(a1, gadd(gmul(a1, gadd(y, psi2)), RgX_add(RgX_Rg_add(RgX_sqr(x), a4), t)));
     565          49 :   t2 = gmul(t2, gmul(dh, h2));
     566             : 
     567          49 :   t3 = gadd(gmul(y, t), RgX_add(alpha, RgX_Rg_mul(u, a1)));
     568          49 :   t3 = gmul(t3, RgX_mul(h, H));
     569             : 
     570          49 :   t4 = gmul(u, psi2);
     571          49 :   t4 = gmul(t4, RgX_sub(RgX_sub(RgX_mul(h2, D2dh), RgX_mul(dh, H)),
     572             :                         RgX_mul(h, RgX_mul(dh, D2h))));
     573             : 
     574          49 :   return gadd(t1, gadd(t2, gadd(t3, t4)));
     575             : }
     576             : 
     577             : static GEN
     578        1288 : isog_ordinate(GEN E, GEN kerp, GEN kerq, GEN x, GEN y, GEN two_tors, GEN f)
     579             : {
     580             :   GEN g;
     581        1288 :   if (! equalis(ellbasechar(E), 2L)) {
     582             :     /* FIXME: We don't use (hence don't need to calculate)
     583             :      * g2 = gel(two_tors, 4) when char(k) != 2. */
     584        1239 :     GEN psi2 = ec_dmFdy_evalQ(E, mkvec2(x, y));
     585        1239 :     g = non_two_torsion_ordinate_char_not2(E, f, kerp, psi2);
     586             :   } else {
     587          49 :     GEN h2 = gel(two_tors, 5);
     588          49 :     GEN g2 = gmul(gel(two_tors, 4), RgX_mul(kerq, RgX_sqr(kerq)));
     589          49 :     GEN g0 = non_two_torsion_ordinate_char2(E, kerq, x, y);
     590          49 :     g0 = gmul(g0, RgX_mul(h2, RgX_sqr(h2)));
     591          49 :     g = gsub(gmul(y, RgX_mul(kerp, RgX_sqr(kerp))), gadd(g2, g0));
     592             :   }
     593        1288 :   return g;
     594             : }
     595             : 
     596             : /* Given an elliptic curve E and a polynomial kerp whose roots give the
     597             :  * x-coordinates of a subgroup G of E, return the curve E/G and,
     598             :  * if only_image is zero, the isogeny pi:E -> E/G. Variables vx and vy are
     599             :  * used to describe the isogeny (and are ignored if only_image is zero). */
     600             : static GEN
     601        3269 : isogeny_from_kernel_poly(GEN E, GEN kerp, long only_image, long vx, long vy)
     602             : {
     603             :   long m;
     604        3269 :   GEN b2 = ell_get_b2(E), b4 = ell_get_b4(E), b6 = ell_get_b6(E);
     605             :   GEN p1, p2, p3, x, y, f, g, two_tors, EE, t, w;
     606        3269 :   GEN kerh = two_torsion_part(E, kerp);
     607        3269 :   GEN kerq = RgX_divrem(kerp, kerh, ONLY_DIVIDES);
     608        3269 :   if (!kerq) pari_err_BUG("isogeny_from_kernel_poly");
     609             :   /* isogeny degree: 2*degpol(kerp)+1-degpol(kerh) */
     610        3269 :   m = degpol(kerq);
     611             : 
     612        3269 :   kerp = RgX_normalize(kerp);
     613        3269 :   kerq = RgX_normalize(kerq);
     614        3269 :   kerh = RgX_normalize(kerh);
     615        3269 :   switch(degpol(kerh))
     616             :   {
     617             :   case 0:
     618        2527 :     two_tors = only_image? mkvec2(gen_0, gen_0):
     619         777 :       mkvec5(gen_0, gen_0, pol_0(vx), pol_0(vx), pol_1(vx));
     620        1750 :     break;
     621             :   case 1:
     622        1498 :     two_tors = contrib_weierstrass_pt(E, kerh, only_image,vx,vy);
     623        1498 :     break;
     624             :   case 3:
     625          14 :     two_tors = contrib_full_tors(E, kerh, only_image,vx,vy);
     626          14 :     break;
     627             :   default:
     628           7 :     two_tors = NULL;
     629           7 :     pari_err_DOMAIN("isogeny_from_kernel_poly", "kernel polynomial",
     630             :                     "does not define a subgroup of", E, kerp);
     631             :   }
     632        3262 :   first_three_power_sums(kerq,&p1,&p2,&p3);
     633        3262 :   x = pol_x(vx);
     634        3262 :   y = pol_x(vy);
     635             : 
     636             :   /* t = 6 * p2 + b2 * p1 + m * b4, */
     637        3262 :   t = gadd(gmulsg(6L, p2), gadd(gmul(b2, p1), gmulsg(m, b4)));
     638             : 
     639             :   /* w = 10 * p3 + 2 * b2 * p2 + 3 * b4 * p1 + m * b6, */
     640        3262 :   w = gadd(gmulsg(10L, p3),
     641             :            gadd(gmul(gmulsg(2L, b2), p2),
     642             :                 gadd(gmul(gmulsg(3L, b4), p1), gmulsg(m, b6))));
     643             : 
     644        3262 :   EE = make_velu_curve(E, gadd(t, gel(two_tors, 1)),
     645        3262 :                           gadd(w, gel(two_tors, 2)));
     646        3262 :   if (only_image) return EE;
     647             : 
     648        1288 :   f = isog_abscissa(E, kerp, kerq, x, two_tors);
     649        1288 :   g = isog_ordinate(E, kerp, kerq, x, y, two_tors, f);
     650        1288 :   return mkvec2(EE, mkvec3(f,g,kerp));
     651             : }
     652             : 
     653             : /* Given an elliptic curve E and a subgroup G of E, return the curve
     654             :  * E/G and, if only_image is zero, the isogeny corresponding
     655             :  * to the canonical surjection pi:E -> E/G. The variables vx and
     656             :  * vy are used to describe the isogeny (and are ignored if
     657             :  * only_image is zero). The subgroup G may be given either as
     658             :  * a generating point P on E or as a polynomial kerp whose roots are
     659             :  * the x-coordinates of the points in G */
     660             : GEN
     661        1092 : ellisogeny(GEN E, GEN G, long only_image, long vx, long vy)
     662             : {
     663        1092 :   pari_sp av = avma;
     664             :   GEN j, z;
     665        1092 :   checkell(E);j = ell_get_j(E);
     666        1092 :   if (vx < 0) vx = 0;
     667        1092 :   if (vy < 0) vy = 1;
     668        1092 :   if (varncmp(vx, vy) >= 0)
     669           7 :     pari_err_PRIORITY("ellisogeny", pol_x(vx), "<=", vy);
     670        1085 :   if (!only_image && varncmp(vy, gvar(j)) >= 0)
     671           7 :     pari_err_PRIORITY("ellisogeny", j, ">=", vy);
     672        1078 :   switch(typ(G))
     673             :   {
     674             :   case t_VEC:
     675         441 :     checkellpt(G);
     676         441 :     if (!ell_is_inf(G))
     677             :     {
     678         399 :       GEN x =  gel(G,1), y = gel(G,2);
     679         399 :       if (!only_image)
     680             :       {
     681         245 :         if (varncmp(vy, gvar(x)) >= 0)
     682           7 :           pari_err_PRIORITY("ellisogeny", x, ">=", vy);
     683         238 :         if (varncmp(vy, gvar(y)) >= 0)
     684           7 :           pari_err_PRIORITY("ellisogeny", y, ">=", vy);
     685             :       }
     686             :     }
     687         427 :     z = isogeny_from_kernel_point(E, G, only_image, vx, vy);
     688         420 :     break;
     689             :   case t_POL:
     690         630 :     if (!only_image && varncmp(vy, gvar(constant_coeff(G))) >= 0)
     691           7 :       pari_err_PRIORITY("ellisogeny", constant_coeff(G), ">=", vy);
     692         623 :     z = isogeny_from_kernel_poly(E, G, only_image, vx, vy);
     693         616 :     break;
     694             :   default:
     695           7 :     z = NULL;
     696           7 :     pari_err_TYPE("ellisogeny", G);
     697             :   }
     698        1036 :   return gerepilecopy(av, z);
     699             : }
     700             : 
     701             : static GEN
     702        2520 : trivial_isogeny(void)
     703             : {
     704        2520 :   return mkvec3(pol_x(0), scalarpol(pol_x(1), 0), pol_1(0));
     705             : }
     706             : 
     707             : static GEN
     708         259 : isogeny_a4a6(GEN E)
     709             : {
     710         259 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), b2 = ell_get_b2(E);
     711         259 :   retmkvec3(deg1pol(gen_1, gdivgs(b2, 12), 0),
     712             :             deg1pol(gdivgs(a1,2), deg1pol(gen_1, gdivgs(a3,2), 1), 0),
     713             :             pol_1(0));
     714             : }
     715             : 
     716             : static GEN
     717         259 : invisogeny_a4a6(GEN E)
     718             : {
     719         259 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), b2 = ell_get_b2(E);
     720         259 :   retmkvec3(deg1pol(gen_1, gdivgs(b2, -12), 0),
     721             :             deg1pol(gdivgs(a1,-2),
     722             :               deg1pol(gen_1, gadd(gdivgs(a3,-2), gdivgs(gmul(b2,a1), 24)), 1), 0),
     723             :             pol_1(0));
     724             : }
     725             : 
     726             : static GEN
     727        1750 : RgXY_eval(GEN P, GEN x, GEN y)
     728             : {
     729        1750 :   return poleval(poleval(P,x), y);
     730             : }
     731             : 
     732             : static GEN
     733         553 : twistisogeny(GEN iso, GEN d)
     734             : {
     735         553 :   GEN d2 = gsqr(d), d3 = gmul(d, d2);
     736         553 :   return mkvec3(gdiv(gel(iso,1), d2), gdiv(gel(iso,2), d3), gel(iso, 3));
     737             : }
     738             : 
     739             : static GEN
     740        2093 : ellisog_by_Kohel(GEN a4, GEN a6, long n, GEN ker, GEN kert, long flag)
     741             : {
     742        2093 :   GEN E = ellinit(mkvec2(a4, a6), NULL, DEFAULTPREC);
     743        2093 :   GEN F = isogeny_from_kernel_poly(E, ker, flag, 0, 1);
     744        2093 :   GEN Et = ellinit(flag ? F: gel(F, 1), NULL, DEFAULTPREC);
     745        2093 :   GEN c4t = ell_get_c4(Et), c6t = ell_get_c6(Et), jt = ell_get_j(Et);
     746        2093 :   if (!flag)
     747             :   {
     748         553 :     GEN Ft = isogeny_from_kernel_poly(Et, kert, flag, 0, 1);
     749         553 :     GEN isot = twistisogeny(gel(Ft, 2), stoi(n));
     750         553 :     return mkvec5(c4t, c6t, jt, gel(F, 2), isot);
     751             :   }
     752        1540 :   else return mkvec3(c4t, c6t, jt);
     753             : }
     754             : 
     755             : static GEN
     756        1925 : ellisog_by_roots(GEN a4, GEN a6, long n, GEN z, long flag)
     757             : {
     758        1925 :   GEN k = deg1pol_shallow(gen_1, gneg(z), 0);
     759        1925 :   GEN kt= deg1pol_shallow(gen_1, gmulsg(n,z), 0);
     760        1925 :   return ellisog_by_Kohel(a4, a6, n, k, kt, flag);
     761             : }
     762             : 
     763             : /* n = 2 or 3 */
     764             : static GEN
     765        2870 : a4a6_divpol(GEN a4, GEN a6, long n)
     766             : {
     767        2870 :   if (n == 2) return mkpoln(4, gen_1, gen_0, a4, a6);
     768        1638 :   return mkpoln(5, utoi(3), gen_0, gmulgs(a4,6) , gmulgs(a6,12),
     769             :                    gneg(gsqr(a4)));
     770             : }
     771             : 
     772             : static GEN
     773        2870 : ellisograph_Kohel_iso(GEN nf, GEN e, long n, GEN z, GEN *pR, long flag)
     774             : {
     775             :   long i, r;
     776        2870 :   GEN R, V, c4 = gel(e,1), c6 = gel(e,2);
     777        2870 :   GEN a4 = gdivgs(c4, -48), a6 = gdivgs(c6, -864);
     778        2870 :   GEN P = a4a6_divpol(a4, a6, n);
     779        2870 :   R = nfroots(nf, z ? RgX_div_by_X_x(P, z, NULL): P);
     780        2870 :   if (pR) *pR = R;
     781        2870 :   r = lg(R); V = cgetg(r, t_VEC);
     782        2870 :   for (i=1; i < r; i++) gel(V,i) = ellisog_by_roots(a4, a6, n, gel(R,i), flag);
     783        2870 :   return V;
     784             : }
     785             : 
     786             : static GEN
     787        2730 : ellisograph_Kohel_r(GEN nf, GEN e, long n, GEN z, long flag)
     788             : {
     789        2730 :   GEN R, iso = ellisograph_Kohel_iso(nf, e, n, z, &R, flag);
     790        2730 :   long i, r = lg(iso);
     791        2730 :   GEN V = cgetg(r, t_VEC);
     792        4515 :   for (i=1; i < r; i++)
     793        1785 :     gel(V,i) = ellisograph_Kohel_r(nf, gel(iso,i), n, gmulgs(gel(R,i), -n), flag);
     794        2730 :   return mkvec2(e, V);
     795             : }
     796             : 
     797             : static GEN
     798         336 : corr(GEN c4, GEN c6)
     799             : {
     800         336 :   GEN c62 = gmul2n(c6, 1);
     801         336 :   return gadd(gdiv(gsqr(c4), c62), gdiv(c62, gmulgs(c4,3)));
     802             : }
     803             : 
     804             : static GEN
     805         336 : elkies98(GEN a4, GEN a6, long l, GEN s, GEN a4t, GEN a6t)
     806             : {
     807             :   GEN C, P, S;
     808             :   long i, n, d;
     809         336 :   d = l == 2 ? 1 : l>>1;
     810         336 :   C = cgetg(d+1, t_VEC);
     811         336 :   gel(C, 1) = gdivgs(gsub(a4, a4t), 5);
     812         336 :   if (d >= 2)
     813         336 :     gel(C, 2) = gdivgs(gsub(a6, a6t), 7);
     814         336 :   if (d >= 3)
     815         224 :     gel(C, 3) = gdivgs(gsub(gsqr(gel(C, 1)), gmul(a4, gel(C, 1))), 3);
     816        2870 :   for (n = 3; n < d; ++n)
     817             :   {
     818        2534 :     GEN s = gen_0;
     819       61390 :     for (i = 1; i < n; i++)
     820       58856 :       s = gadd(s, gmul(gel(C, i), gel(C, n-i)));
     821        2534 :     gel(C, n+1) = gdivgs(gsub(gsub(gmulsg(3, s), gmul(gmulsg((2*n-1)*(n-1), a4), gel(C, n-1))), gmul(gmulsg((2*n-2)*(n-2), a6), gel(C, n-2))), (n-1)*(2*n+5));
     822             :   }
     823         336 :   P = cgetg(d+2, t_VEC);
     824         336 :   gel(P, 1 + 0) = stoi(d);
     825         336 :   gel(P, 1 + 1) = s;
     826         336 :   if (d >= 2)
     827         336 :     gel(P, 1 + 2) = gdivgs(gsub(gel(C, 1), gmulgs(gmulsg(2, a4), d)), 6);
     828        3094 :   for (n = 2; n < d; ++n)
     829        2758 :     gel(P, 1 + n+1) = gdivgs(gsub(gsub(gel(C, n), gmul(gmulsg(4*n-2, a4), gel(P, 1+n-1))), gmul(gmulsg(4*n-4, a6), gel(P, 1+n-2))), 4*n+2);
     830         336 :   S = cgetg(d+3, t_POL);
     831         336 :   S[1] = evalsigne(1) | evalvarn(0);
     832         336 :   gel(S, 2 + d - 0) = gen_1;
     833         336 :   gel(S, 2 + d - 1) = gneg(s);
     834        3430 :   for (n = 2; n <= d; ++n)
     835             :   {
     836        3094 :     GEN s = gen_0;
     837       68362 :     for (i = 1; i <= n; ++i)
     838             :     {
     839       65268 :       GEN p = gmul(gel(P, 1+i), gel(S, 2 + d - (n-i)));
     840       65268 :       s = gadd(s, p);
     841             :     }
     842        3094 :     gel(S, 2 + d - n) = gdivgs(s, -n);
     843             :   }
     844         336 :   return S;
     845             : }
     846             : 
     847             : static GEN
     848         504 : ellisog_by_jt(GEN c4, GEN c6, GEN jt, GEN jtp, GEN s0, long n, long flag)
     849             : {
     850         504 :   GEN jtp2 = gsqr(jtp), den = gmul(jt, gsubgs(jt, 1728));
     851         504 :   GEN c4t = gdiv(jtp2, den);
     852         504 :   GEN c6t = gdiv(gmul(jtp, c4t), jt);
     853         504 :   if (flag)
     854         336 :     return mkvec3(c4t, c6t, jt);
     855             :   else
     856             :   {
     857         168 :     GEN co  = corr(c4, c6);
     858         168 :     GEN cot = corr(c4t, c6t);
     859         168 :     GEN s = gmul2n(gmulgs(gadd(gadd(s0, co), gmulgs(cot,-n)), -n), -2);
     860         168 :     GEN a4  = gdivgs(c4, -48), a6 = gdivgs(c6, -864);
     861         168 :     GEN a4t = gmul(gdivgs(c4t, -48), powuu(n,4)), a6t = gmul(gdivgs(c6t, -864), powuu(n,6));
     862         168 :     GEN ker = elkies98(a4, a6, n, s, a4t, a6t);
     863         168 :     GEN st = gmulgs(s, -n);
     864         168 :     GEN a4tt = gmul(a4,powuu(n,4)), a6tt = gmul(a6,powuu(n,6));
     865         168 :     GEN kert = elkies98(a4t, a6t, n, st, a4tt, a6tt);
     866         168 :     return ellisog_by_Kohel(a4, a6, n, ker, kert, flag);
     867             :   }
     868             : }
     869             : 
     870             : /*
     871             : Based on
     872             : RENE SCHOOF
     873             : Counting points on elliptic curves over finite fields
     874             : Journal de Theorie des Nombres de Bordeaux,
     875             : tome 7, no 1 (1995), p. 219-254.
     876             : <http://www.numdam.org/item?id=JTNB_1995__7_1_219_0>
     877             : */
     878             : 
     879             : static GEN
     880         350 : ellisog_by_j(GEN e, GEN jt, long n, GEN P, long flag)
     881             : {
     882         350 :   pari_sp av = avma;
     883         350 :   GEN c4  = gel(e,1), c6 = gel(e, 2), j = gel(e, 3);
     884         350 :   GEN Px = deriv(P, 0), Py = deriv(P, 1);
     885         350 :   GEN Pxj = RgXY_eval(Px, j, jt), Pyj = RgXY_eval(Py, j, jt);
     886         350 :   GEN Pxx  = deriv(Px, 0), Pxy = deriv(Py, 0), Pyy = deriv(Py, 1);
     887         350 :   GEN Pxxj = RgXY_eval(Pxx,j,jt);
     888         350 :   GEN Pxyj = RgXY_eval(Pxy,j,jt);
     889         350 :   GEN Pyyj = RgXY_eval(Pyy,j,jt);
     890         350 :   GEN c6c4 = gdiv(c6, c4);
     891         350 :   GEN jp = gmul(j, c6c4);
     892         350 :   GEN jtp = gdivgs(gmul(jp, gdiv(Pxj, Pyj)), -n);
     893         350 :   GEN jtpn = gmulgs(jtp, n);
     894         350 :   GEN s0 = gdiv(gadd(gadd(gmul(gsqr(jp),Pxxj),gmul(gmul(jp,jtpn),gmul2n(Pxyj,1))),
     895             :                 gmul(gsqr(jtpn),Pyyj)),gmul(jp,Pxj));
     896         350 :   GEN et = ellisog_by_jt(c4, c6, jt, jtp, s0, n, flag);
     897         350 :   return gerepilecopy(av, et);
     898             : }
     899             : 
     900             : static GEN
     901         784 : ellisograph_iso(GEN nf, GEN e, ulong p, GEN P, GEN oj, long flag)
     902             : {
     903             :   long i, r;
     904             :   GEN Pj, R, V;
     905         784 :   if (!P) return ellisograph_Kohel_iso(nf, e, p, oj, NULL, flag);
     906         644 :   Pj = poleval(P, gel(e,3));
     907         644 :   R = nfroots(nf,oj ? RgX_div_by_X_x(Pj, oj, NULL):Pj);
     908         644 :   r = lg(R);
     909         644 :   V = cgetg(r, t_VEC);
     910         994 :   for (i=1; i < r; i++)
     911         350 :     gel(V, i) = ellisog_by_j(e, gel(R, i), p, P, flag);
     912         644 :   return V;
     913             : }
     914             : 
     915             : static GEN
     916         602 : ellisograph_r(GEN nf, GEN e, ulong p, GEN P, GEN oj, long flag)
     917             : {
     918         602 :   GEN j = gel(e,3), iso = ellisograph_iso(nf, e, p, P, oj, flag);
     919         602 :   long i, r = lg(iso);
     920         602 :   GEN V = cgetg(r, t_VEC);
     921         602 :   for (i=1; i < r; i++) gel(V,i) = ellisograph_r(nf, gel(iso,i), p, P, j, flag);
     922         602 :   return mkvec2(e, V);
     923             : }
     924             : 
     925             : static GEN
     926         728 : ellisograph_a4a6(GEN E, long flag)
     927             : {
     928         728 :   GEN c4 = ell_get_c4(E), c6 = ell_get_c6(E), j = ell_get_j(E);
     929         987 :   return flag ? mkvec3(c4, c6, j):
     930         259 :                 mkvec5(c4, c6, j, isogeny_a4a6(E), invisogeny_a4a6(E));
     931             : }
     932             : 
     933             : static GEN
     934         154 : ellisograph_dummy(GEN E, long n, GEN jt, GEN jtt, GEN s0, long flag)
     935             : {
     936         154 :   GEN c4 = ell_get_c4(E), c6 = ell_get_c6(E), c6c4 = gdiv(c6, c4);
     937         154 :   GEN jtp = gmul(c6c4, gdivgs(gmul(jt, jtt), -n));
     938         154 :   GEN iso = ellisog_by_jt(c4, c6, jt, jtp, gmul(s0, c6c4), n, flag);
     939         154 :   GEN v = mkvec2(iso, cgetg(1, t_VEC));
     940         154 :   return mkvec2(ellisograph_a4a6(E, flag), mkvec(v));
     941             : }
     942             : 
     943             : static GEN
     944        1239 : isograph_p(GEN nf, GEN e, ulong p, GEN P, long flag)
     945             : {
     946        1239 :   pari_sp av = avma;
     947             :   GEN iso;
     948        1239 :   if (P)
     949         294 :     iso = ellisograph_r(nf, e, p, P, NULL, flag);
     950             :   else
     951         945 :     iso = ellisograph_Kohel_r(nf, e, p, NULL, flag);
     952        1239 :   return gerepilecopy(av, iso);
     953             : }
     954             : 
     955             : static GEN
     956         588 : get_polmodular(ulong p)
     957         588 : { return p > 3 ? polmodular_ZXX(p,0,0,1): NULL; }
     958             : static GEN
     959         413 : ellisograph_p(GEN nf, GEN E, ulong p, long flag)
     960             : {
     961         413 :   GEN e = ellisograph_a4a6(E, flag);
     962         413 :   GEN P = get_polmodular(p);
     963         413 :   return isograph_p(nf, e, p, P, flag);
     964             : }
     965             : 
     966             : static long
     967        7889 : etree_nbnodes(GEN T)
     968             : {
     969        7889 :   GEN F = gel(T,2);
     970        7889 :   long n = 1, i, l = lg(F);
     971       12859 :   for (i = 1; i < l; i++)
     972        4970 :     n += etree_nbnodes(gel(F, i));
     973        7889 :   return n;
     974             : }
     975             : 
     976             : static long
     977        3374 : etree_listr(GEN nf, GEN T, GEN V, long n, GEN u, GEN ut)
     978             : {
     979        3374 :   GEN E = gel(T, 1), F = gel(T,2);
     980        3374 :   long i, l = lg(F);
     981        3374 :   GEN iso, isot = NULL;
     982        3374 :   if (lg(E) == 6)
     983             :   {
     984         721 :     iso  = ellnfcompisog(nf,gel(E,4), u);
     985         721 :     isot = ellnfcompisog(nf,ut, gel(E,5));
     986         721 :     gel(V, n) = mkvec5(gel(E,1), gel(E,2), gel(E,3), iso, isot);
     987             :   } else
     988             :   {
     989        2653 :     gel(V, n) = mkvec3(gel(E,1), gel(E,2), gel(E,3));
     990        2653 :     iso = u;
     991             :   }
     992        5488 :   for (i = 1; i < l; i++)
     993        2114 :     n = etree_listr(nf, gel(F, i), V, n + 1, iso, isot);
     994        3374 :   return n;
     995             : }
     996             : 
     997             : static GEN
     998        1260 : etree_list(GEN nf, GEN T)
     999             : {
    1000        1260 :   long n = etree_nbnodes(T);
    1001        1260 :   GEN V = cgetg(n+1, t_VEC);
    1002        1260 :   (void) etree_listr(nf, T, V, 1, trivial_isogeny(), trivial_isogeny());
    1003        1260 :   return V;
    1004             : }
    1005             : 
    1006             : static long
    1007        3374 : etree_distmatr(GEN T, GEN M, long n)
    1008             : {
    1009        3374 :   GEN F = gel(T,2);
    1010        3374 :   long i, j, lF = lg(F), m = n + 1;
    1011        3374 :   GEN V = cgetg(lF, t_VECSMALL);
    1012        3374 :   mael(M, n, n) = 0;
    1013        5488 :   for(i = 1; i < lF; i++)
    1014        2114 :     V[i] = m = etree_distmatr(gel(F,i), M, m);
    1015        5488 :   for(i = 1; i < lF; i++)
    1016             :   {
    1017        2114 :     long mi = i==1 ? n+1: V[i-1];
    1018        4851 :     for(j = mi; j < V[i]; j++)
    1019             :     {
    1020        2737 :       mael(M,n,j) = 1 + mael(M, mi, j);
    1021        2737 :       mael(M,j,n) = 1 + mael(M, j, mi);
    1022             :     }
    1023        5194 :     for(j = 1; j < lF; j++)
    1024        3080 :       if (i != j)
    1025             :       {
    1026         966 :         long i1, j1, mj = j==1 ? n+1: V[j-1];
    1027        2121 :         for (i1 = mi; i1 < V[i]; i1++)
    1028        2611 :           for(j1 = mj; j1 < V[j]; j1++)
    1029        1456 :             mael(M,i1,j1) = 2 + mael(M,mj,j1) + mael(M,i1,mi);
    1030             :       }
    1031             :   }
    1032        3374 :   return m;
    1033             : }
    1034             : 
    1035             : static GEN
    1036        1260 : etree_distmat(GEN T)
    1037             : {
    1038        1260 :   long i, n = etree_nbnodes(T);
    1039        1260 :   GEN M = cgetg(n+1, t_MAT);
    1040        4634 :   for(i = 1; i <= n; i++)
    1041        3374 :     gel(M,i) = cgetg(n+1, t_VECSMALL);
    1042        1260 :   (void)etree_distmatr(T, M, 1);
    1043        1260 :   return M;
    1044             : }
    1045             : 
    1046             : static GEN
    1047        1260 : distmat_pow(GEN E, ulong p)
    1048             : {
    1049        1260 :   long i, j, l = lg(E);
    1050        1260 :   GEN M = cgetg(l, t_MAT);
    1051        4634 :   for(i = 1; i < l; i++)
    1052             :   {
    1053        3374 :     gel(M,i) = cgetg(l, t_COL);
    1054        3374 :     for(j = 1; j < l; j++) gmael(M,i,j) = powuu(p,mael(E,i,j));
    1055             :   }
    1056        1260 :   return M;
    1057             : }
    1058             : 
    1059             : /* Assume there is a single p-isogeny */
    1060             : static GEN
    1061         133 : isomatdbl(GEN nf, GEN L, GEN M, ulong p, GEN T2, long flag)
    1062             : {
    1063         133 :   long i, j, n = lg(L) -1;
    1064         133 :   GEN P = get_polmodular(p), V = cgetg(2*n+1, t_VEC), N = cgetg(2*n+1, t_MAT);
    1065         448 :   for (i=1; i <= n; i++)
    1066             :   {
    1067         315 :     GEN F, E, e = gel(L,i);
    1068         315 :     if (i == 1)
    1069         133 :       F = gmael(T2, 2, 1);
    1070             :     else
    1071             :     {
    1072         182 :       F = ellisograph_iso(nf, e, p, P, NULL, flag);
    1073         182 :       if (lg(F) != 2) pari_err_BUG("isomatdbl");
    1074             :     }
    1075         315 :     E = gel(F, 1);
    1076         315 :     if (flag)
    1077         203 :       E = mkvec3(gel(E,1), gel(E,2), gel(E,3));
    1078             :     else
    1079             :     {
    1080         112 :       GEN iso = ellnfcompisog(nf, gel(E,4), gel(e, 4));
    1081         112 :       GEN isot = ellnfcompisog(nf, gel(e,5), gel(E, 5));
    1082         112 :       E = mkvec5(gel(E,1), gel(E,2), gel(E,3), iso, isot);
    1083             :     }
    1084         315 :     gel(V, i)   = e;
    1085         315 :     gel(V, i+n) = E;
    1086             :   }
    1087         133 :   for (i=1; i <= 2*n; i++) gel(N, i) = cgetg(2*n+1, t_COL);
    1088         448 :   for (i=1; i <= n; i++)
    1089        1092 :     for (j=1; j <= n; j++)
    1090             :     {
    1091         777 :       gcoeff(N,i,j) = gcoeff(N,i+n,j+n) = gcoeff(M,i,j);
    1092         777 :       gcoeff(N,i,j+n) = gcoeff(N,i+n,j) = muliu(gcoeff(M,i,j), p);
    1093             :     }
    1094         133 :   return mkvec2(V, N);
    1095             : }
    1096             : 
    1097             : static ulong
    1098         427 : ellQ_exceptional_iso(GEN j, GEN *jt, GEN *jtp, GEN *s0)
    1099             : {
    1100         427 :   *jt = j; *jtp = gen_1;
    1101         427 :   if (typ(j)==t_INT)
    1102             :   {
    1103         252 :     long js = itos_or_0(j);
    1104             :     GEN j37;
    1105         252 :     if (js==-32768) { *s0 = mkfracss(-1156,539); return 11; }
    1106         238 :     if (js==-121)
    1107          14 :       { *jt = stoi(-24729001) ; *jtp = mkfracss(4973,5633);
    1108          14 :         *s0 = mkfracss(-1961682050,1204555087); return 11;}
    1109         224 :     if (js==-24729001)
    1110          14 :       { *jt = stoi(-121); *jtp = mkfracss(5633,4973);
    1111          14 :         *s0 = mkfracss(-1961682050,1063421347); return 11;}
    1112         210 :     if (js==-884736)
    1113          14 :       { *s0 = mkfracss(-1100,513); return 19; }
    1114         196 :     j37 = negi(uu32toi(37876312,1780746325));
    1115         196 :     if (js==-9317)
    1116             :     {
    1117          14 :       *jt = j37;
    1118          14 :       *jtp = mkfracss(1984136099,496260169);
    1119          14 :       *s0 = mkfrac(negi(uu32toi(457100760,4180820796UL)),
    1120             :                         uu32toi(89049913, 4077411069UL));
    1121          14 :       return 37;
    1122             :     }
    1123         182 :     if (equalii(j, j37))
    1124             :     {
    1125          14 :       *jt = stoi(-9317);
    1126          14 :       *jtp = mkfrac(utoi(496260169),utoi(1984136099UL));
    1127          14 :       *s0 = mkfrac(negi(uu32toi(41554614,2722784052UL)),
    1128             :                         uu32toi(32367030,2614994557UL));
    1129          14 :       return 37;
    1130             :     }
    1131         168 :     if (js==-884736000)
    1132          14 :     { *s0 = mkfracss(-1073708,512001); return 43; }
    1133         154 :     if (equalii(j, negi(powuu(5280,3))))
    1134          14 :     { *s0 = mkfracss(-176993228,85184001); return 67; }
    1135         140 :     if (equalii(j, negi(powuu(640320,3))))
    1136          14 :     { *s0 = mkfrac(negi(uu32toi(72512,1969695276)), uu32toi(35374,1199927297));
    1137          14 :       return 163; }
    1138             :   } else
    1139             :   {
    1140         175 :     GEN j1 = mkfracss(-297756989,2);
    1141         175 :     GEN j2 = mkfracss(-882216989,131072);
    1142         175 :     if (gequal(j, j1))
    1143             :     {
    1144          14 :       *jt = j2; *jtp = mkfracss(1503991,2878441);
    1145          14 :       *s0 = mkfrac(negi(uu32toi(121934,548114672)),uu32toi(77014,117338383));
    1146          14 :       return 17;
    1147             :     }
    1148         161 :     if (gequal(j, j2))
    1149             :     {
    1150          14 :       *jt = j1; *jtp = mkfracss(2878441,1503991);
    1151          14 :       *s0 = mkfrac(negi(uu32toi(121934,548114672)),uu32toi(40239,4202639633UL));
    1152          14 :       return 17;
    1153             :     }
    1154             :   }
    1155         273 :   return 0;
    1156             : }
    1157             : 
    1158             : static GEN
    1159        1260 : nfmkisomat(GEN nf, ulong p, GEN T)
    1160        1260 : { return mkvec2(etree_list(nf,T), distmat_pow(etree_distmat(T),p)); }
    1161             : static GEN
    1162         420 : mkisomat(ulong p, GEN T)
    1163         420 : { return nfmkisomat(NULL, p, T); }
    1164             : static GEN
    1165         133 : mkisomatdbl(ulong p, GEN T, ulong p2, GEN T2, long flag)
    1166             : {
    1167         133 :   GEN v = mkisomat(p,T);
    1168         133 :   return isomatdbl(NULL, gel(v,1), gel(v,2), p2, T2, flag);
    1169             : }
    1170             : 
    1171             : /*
    1172             : See
    1173             : M.A Kenku
    1174             : On the number of Q-isomorphism classes of elliptic curves in each Q-isogeny class
    1175             : Journal of Number Theory
    1176             : Volume 15, Issue 2, October 1982, Pages 199-202
    1177             : http://www.sciencedirect.com/science/article/pii/0022314X82900257
    1178             : */
    1179             : 
    1180             : enum { _2 = 1, _3 = 2, _5 = 4, _7 = 8, _13 = 16 };
    1181             : static ulong
    1182         273 : ellQ_goodl(GEN E)
    1183             : {
    1184             :   forprime_t T;
    1185         273 :   long i, CM = ellQ_get_CM(E);
    1186         273 :   ulong mask = 31;
    1187         273 :   GEN disc = ell_get_disc(E);
    1188         273 :   pari_sp av = avma;
    1189         273 :   u_forprime_init(&T, 17UL,ULONG_MAX);
    1190        5621 :   for(i=1; mask && i<=20; i++)
    1191             :   {
    1192        5348 :     ulong p = u_forprime_next(&T);
    1193        5348 :     if (umodiu(disc,p)==0) i--;
    1194             :     else
    1195             :     {
    1196        5348 :       long t = ellap_CM_fast(E, p, CM), D = t*t-4*p;
    1197        5348 :       if (t%2) mask &= ~_2;
    1198        5348 :       if ((mask & _3) && kross(D,3)==-1)  mask &= ~_3;
    1199        5348 :       if ((mask & _5) && kross(D,5)==-1)  mask &= ~_5;
    1200        5348 :       if ((mask & _7) && kross(D,7)==-1)  mask &= ~_7;
    1201        5348 :       if ((mask &_13) && kross(D,13)==-1) mask &= ~_13;
    1202             :     }
    1203             :   }
    1204         273 :   avma = av; return mask;
    1205             : }
    1206             : 
    1207             : static long
    1208          14 : ellQ_goodl_l(GEN E, long l)
    1209             : {
    1210             :   forprime_t T;
    1211             :   long i;
    1212          14 :   GEN disc = ell_get_disc(E);
    1213          14 :   pari_sp av = avma;
    1214          14 :   u_forprime_init(&T, 17UL,ULONG_MAX);
    1215         294 :   for(i=1; i<=20; i++)
    1216             :   {
    1217         280 :     ulong p = u_forprime_next(&T);
    1218         280 :     if (umodiu(disc,p)==0) { i--; continue; }
    1219             :     else
    1220             :     {
    1221         280 :       long t = itos(ellap(E, utoi(p)));
    1222         280 :       if (l==2)
    1223             :       {
    1224         140 :         if (t%2==1) return 0;
    1225             :       }
    1226             :       else
    1227             :       {
    1228         140 :         long D = t*t-4*p;
    1229         140 :         if (kross(D,l)==-1) return 0;
    1230             :       }
    1231         280 :       avma = av;
    1232             :     }
    1233             :   }
    1234          14 :   return 1;
    1235             : }
    1236             : 
    1237             : static ulong
    1238          21 : ellnf_goodl_l(GEN E, GEN v)
    1239             : {
    1240             :   forprime_t T;
    1241             :   long i;
    1242          21 :   GEN nf = ellnf_get_nf(E);
    1243          21 :   GEN disc = ell_get_disc(E);
    1244          21 :   long lv = lg(v);
    1245          21 :   ulong w = 0UL;
    1246          21 :   pari_sp av = avma;
    1247          21 :   u_forprime_init(&T, 17UL,ULONG_MAX);
    1248         448 :   for(i=1; i<=20; i++)
    1249             :   {
    1250         427 :     ulong p = u_forprime_next(&T);
    1251         427 :     GEN pr = idealprimedec(nf, utoi(p));
    1252         427 :     long j, k, lv = lg(v), g = lg(pr)-1;
    1253        1064 :     for (j=1; j<=g; j++)
    1254             :     {
    1255         637 :       GEN prj = gel(pr, j);
    1256         637 :       if (idealval(nf,disc,prj) > 0) {i--; continue;}
    1257             :       else
    1258             :       {
    1259         630 :         long t = itos(ellap(E, prj));
    1260        2730 :         for(k = 1; k < lv; k++)
    1261             :         {
    1262        2100 :           long l = v[k];
    1263        2100 :           if (l==2)
    1264             :           {
    1265         630 :             if (t%2==1) w |= 1<<(k-1);
    1266             :           }
    1267             :           else
    1268             :           {
    1269        1470 :             GEN D = subii(sqrs(t),shifti(pr_norm(prj),2));
    1270        1470 :             if (krois(D,l)==-1) w |= 1<<(k-1);
    1271             :           }
    1272             :         }
    1273             :       }
    1274             :     }
    1275         427 :     avma = av;
    1276             :   }
    1277          21 :   return w^((1UL<<(lv-1))-1);
    1278             : }
    1279             : 
    1280             : static GEN
    1281         602 : ellnf_charpoly(GEN E, GEN pr)
    1282             : {
    1283         602 :   return deg2pol_shallow(gen_1, negi(ellap(E,pr)), pr_norm(pr), 0);
    1284             : }
    1285             : 
    1286             : static GEN
    1287        1204 : RgX_homogenize(GEN P, long v)
    1288             : {
    1289        1204 :   GEN Q = leafcopy(P);
    1290        1204 :   long i, l = lg(P), d = degpol(P);
    1291        1204 :   for (i = 2; i < l; i++) gel(Q,i) = monomial(gel(Q,i), d--, v);
    1292        1204 :   return Q;
    1293             : }
    1294             : 
    1295             : static GEN
    1296        1204 : starlaw(GEN p, GEN q)
    1297             : {
    1298        1204 :   GEN Q = RgX_homogenize(RgX_recip(q), 1);
    1299        1204 :   return ZX_ZXY_resultant(p, Q);
    1300             : }
    1301             : 
    1302             : static GEN
    1303         602 : startor(GEN p, long r)
    1304             : {
    1305         602 :   GEN xr = pol_xn(r, 0);
    1306         602 :   GEN psir = gsub(xr, gen_1);
    1307         602 :   return gsubstpol(starlaw(p, psir),xr,pol_x(0));
    1308             : }
    1309             : 
    1310             : static GEN
    1311         420 : ellnf_get_degree(GEN E, GEN p)
    1312             : {
    1313         420 :   GEN nf = ellnf_get_nf(E);
    1314         420 :   long d = nf_get_degree(nf);
    1315         420 :   GEN dec = idealprimedec(nf, p);
    1316         420 :   long i, l = lg(dec), k;
    1317         420 :   GEN R, starl = deg1pol_shallow(gen_1, gen_m1, 0);
    1318        1022 :   for(i=1; i < l; i++)
    1319             :   {
    1320         602 :     GEN pr = gel(dec,i);
    1321         602 :     GEN q = ellnf_charpoly(E, pr);
    1322         602 :     starl = starlaw(starl, startor(q, 12*pr_get_e(pr)));
    1323             :   }
    1324         420 :   R = p;
    1325        1260 :   for(k=0; 2*k<=d; k++)
    1326         840 :     R = mulii(R, poleval(starl,powiu(p,12*k)));
    1327         420 :   return R;
    1328             : }
    1329             : 
    1330             : /*
    1331             : Based on a GP script by Nicolas Billerey itself
    1332             : based on Th\'eor\`emes 2.4 and 2.8 of the following article:
    1333             : N. Billerey, Crit\`eres d'irr\'eductibilit\'e pour les
    1334             : repr\'esentations des courbes elliptiques,
    1335             : Int. J. Number Theory 7 (2011), no. 4, 1001-1032.
    1336             : */
    1337             : 
    1338             : static GEN
    1339          21 : ellnf_prime_degree(GEN E)
    1340             : {
    1341             :   forprime_t T;
    1342             :   long i;
    1343          21 :   GEN nf = ellnf_get_nf(E);
    1344          21 :   GEN disc = ell_get_disc(E);
    1345          21 :   GEN P, B = gen_0, rB;
    1346          21 :   GEN bad = mulii(nfnorm(nf, disc),nf_get_disc(nf));
    1347          21 :   u_forprime_init(&T, 5UL,ULONG_MAX);
    1348         469 :   for(i=1; i<=20; i++)
    1349             :   {
    1350         448 :     ulong p = u_forprime_next(&T);
    1351         448 :     if (dvdiu(bad, p)) {i--; continue;}
    1352         420 :     B = gcdii(B, ellnf_get_degree(E, utoi(p)));
    1353         420 :     if (Z_issquareall(B,&rB)) B=rB;
    1354             :   }
    1355          21 :   if (signe(B)==0) pari_err_IMPL("ellisomat, CM case");
    1356          21 :   P = vec_to_vecsmall(gel(Z_factor(B),1));
    1357          21 :   return shallowextract(P, utoi(ellnf_goodl_l(E, P)));
    1358             : }
    1359             : 
    1360             : static GEN
    1361         427 : ellQ_isomat(GEN E, long flag)
    1362             : {
    1363         427 :   GEN K = NULL, T2 = NULL, T3 = NULL, T5, T7, T13;
    1364             :   ulong good;
    1365             :   long n2, n3, n5, n7, n13;
    1366         427 :   GEN jt, jtp, s0, j = ell_get_j(E);
    1367         427 :   long l = ellQ_exceptional_iso(j, &jt, &jtp, &s0);
    1368         427 :   if (l)
    1369             :   {
    1370             : #if 1
    1371         154 :     return mkisomat(l, ellisograph_dummy(E, l, jt, jtp, s0, flag));
    1372             : #else
    1373             :     return mkisomat(l, ellisograph_p(K, E, l), flag);
    1374             : #endif
    1375             :   }
    1376         273 :   good = ellQ_goodl(ellintegralmodel(E,NULL));
    1377         273 :   if (good & _2)
    1378             :   {
    1379         203 :     T2 = ellisograph_p(K, E, 2, flag);
    1380         203 :     n2 = etree_nbnodes(T2);
    1381         203 :     if (n2>4 || gequalgs(j, 1728) || gequalgs(j, 287496))
    1382          63 :       return mkisomat(2, T2);
    1383          70 :   } else n2 = 1;
    1384         210 :   if (good & _3)
    1385             :   {
    1386         140 :     T3 = ellisograph_p(K, E, 3, flag);
    1387         140 :     n3 = etree_nbnodes(T3);
    1388         140 :     if (n3>1 && n2==2) return mkisomatdbl(3,T3,2,T2, flag);
    1389          49 :     if (n3==2 && n2>1)  return mkisomatdbl(2,T2,3,T3, flag);
    1390          49 :     if (n3>2 || gequal0(j)) return mkisomat(3, T3);
    1391          70 :   } else n3 = 1;
    1392          84 :   if (good & _5)
    1393             :   {
    1394          28 :     T5 = ellisograph_p(K, E, 5, flag);
    1395          28 :     n5 = etree_nbnodes(T5);
    1396          28 :     if (n5>1 && n2>1) return mkisomatdbl(2,T2,5,T5, flag);
    1397          28 :     if (n5>1 && n3>1) return mkisomatdbl(3,T3,5,T5, flag);
    1398          14 :     if (n5>1) return mkisomat(5, T5);
    1399          56 :   } else n5 = 1;
    1400          56 :   if (good & _7)
    1401             :   {
    1402          28 :     T7 = ellisograph_p(K, E, 7, flag);
    1403          28 :     n7 = etree_nbnodes(T7);
    1404          28 :     if (n7>1 && n2>1) return mkisomatdbl(2,T2,7,T7, flag);
    1405           0 :     if (n7>1 && n3>1) return mkisomatdbl(3,T3,7,T7, flag);
    1406           0 :     if (n7>1) return mkisomat(7,T7);
    1407          28 :   } else n7 = 1;
    1408          28 :   if (n2>1) return mkisomat(2,T2);
    1409           7 :   if (n3>1) return mkisomat(3,T3);
    1410           7 :   if (good & _13)
    1411             :   {
    1412           0 :     T13 = ellisograph_p(K, E, 13, flag);
    1413           0 :     n13 = etree_nbnodes(T13);
    1414           0 :     if (n13>1) return mkisomat(13,T13);
    1415           7 :   } else n13 = 1;
    1416           7 :   return mkvec2(mkvec(ellisograph_a4a6(E,flag)), matid(1));
    1417             : }
    1418             : 
    1419             : static long
    1420         826 : fill_LM(GEN LM, GEN L, GEN M, GEN z, long k)
    1421             : {
    1422         826 :   GEN Li = gel(LM,1), Mi1 = gmael(LM,2,1);
    1423         826 :   long j, m = lg(Li);
    1424        2156 :   for (j = 2; j < m; j++)
    1425             :   {
    1426        1330 :     GEN d = gel(Mi1,j);
    1427        1330 :     gel(L, k) = gel(Li,j);
    1428        1330 :     gel(M, k) = z? mulii(d,z): d;
    1429        1330 :     k++;
    1430             :   }
    1431         826 :   return k;
    1432             : }
    1433             : static GEN
    1434         154 : ellnf_isocrv(GEN nf, GEN E, GEN v, GEN PE, long flag)
    1435             : {
    1436             :   long i, l, lv, n, k;
    1437         154 :   GEN L, M, LE = cgetg_copy(v,&lv), e = ellisograph_a4a6(E, flag);
    1438         504 :   for (i = n = 1; i < lv; i++)
    1439             :   {
    1440         350 :     ulong p = uel(v,i);
    1441         350 :     GEN T = isograph_p(nf, e, p, gel(PE,i), flag);
    1442         350 :     GEN LM = nfmkisomat(nf, p, T);
    1443         350 :     gel(LE,i) = LM;
    1444         350 :     n *= lg(gel(LM,1)) - 1;
    1445             :   }
    1446         154 :   L = cgetg(n+1,t_VEC); gel(L,1) = e;
    1447         154 :   M = cgetg(n+1,t_COL); gel(M,1) = gen_1;
    1448         504 :   for (i = 1, k = 2; i < lv; i++)
    1449             :   {
    1450         350 :     ulong p = uel(v,i);
    1451         350 :     GEN P = gel(PE,i);
    1452         350 :     long kk = k;
    1453         350 :     k = fill_LM(gel(LE,i), L, M, NULL, k);
    1454         826 :     for (l = 2; l < kk; l++)
    1455             :     {
    1456         476 :       GEN T = isograph_p(nf, gel(L,l), p, P, flag);
    1457         476 :       GEN LMe = nfmkisomat(nf, p, T);
    1458         476 :       k = fill_LM(LMe, L, M, gel(M,l), k);
    1459             :     }
    1460             :   }
    1461         154 :   return mkvec2(L, M);
    1462             : }
    1463             : 
    1464             : static long
    1465        1526 : nfispower(GEN nf, long d, GEN a, GEN b)
    1466             : {
    1467             :   GEN N;
    1468        1526 :   if (gequal(a,b)) return 1;
    1469        1120 :   N = nfroots(nf, gsub(monomial(b, d, 0), monomial(a,0,0)));
    1470        1120 :   return lg(N) > 1;
    1471             : }
    1472             : 
    1473             : static long
    1474        7791 : isomat_eq(GEN nf, GEN e1, GEN e2)
    1475             : {
    1476        7791 :   if (gequal(e1,e2)) return 1;
    1477        7791 :   if (!gequal(gel(e1,3), gel(e2,3))) return 0;
    1478        1526 :   if (gequal0(gel(e1,3)))
    1479           0 :     return nfispower(nf,6,gel(e1,2),gel(e2,2));
    1480        1526 :   if (gequalgs(gel(e1,3),1728))
    1481           0 :     return nfispower(nf,4,gel(e1,1),gel(e2,1));
    1482        1526 :   return nfispower(nf,2,gmul(gel(e1,1),gel(e2,2)),gmul(gel(e1,2),gel(e2,1)));
    1483             : }
    1484             : 
    1485             : static long
    1486        1330 : isomat_find(GEN nf, GEN e, GEN L)
    1487             : {
    1488        1330 :   long i, l = lg(L);
    1489        7791 :   for (i=1; i<l; i++)
    1490        7791 :     if (isomat_eq(nf, e, gel(L,i))) return i;
    1491             :   pari_err_BUG("isomat_find"); return 0; /* LCOV_EXCL_LINE */
    1492             : }
    1493             : 
    1494             : static GEN
    1495         133 : isomat_perm(GEN nf, GEN E, GEN L)
    1496             : {
    1497         133 :   long i, l = lg(E);
    1498         133 :   GEN v = cgetg(l, t_VECSMALL);
    1499        1463 :   for (i=1; i<l; i++)
    1500        1330 :     uel(v, i) = isomat_find(nf, gel(E,i), L);
    1501         133 :   return v;
    1502             : }
    1503             : 
    1504             : static GEN
    1505          21 : ellnf_modpoly(GEN v)
    1506             : {
    1507          21 :   long i, l = lg(v);
    1508          21 :   GEN P = cgetg(l, t_VEC);
    1509          21 :   for(i = 1; i < l; i++) gel(P, i) = get_polmodular(v[i]);
    1510          21 :   return P;
    1511             : }
    1512             : 
    1513             : static GEN
    1514          21 : ellnf_isomat(GEN E, long flag)
    1515             : {
    1516          21 :   GEN nf = ellnf_get_nf(E);
    1517          21 :   GEN v = ellnf_prime_degree(E);
    1518          21 :   GEN P = ellnf_modpoly(v);
    1519          21 :   GEN LM = ellnf_isocrv(nf, E, v, P, flag), L = gel(LM,1), M = gel(LM,2);
    1520          21 :   long i, l = lg(L);
    1521          21 :   GEN R = cgetg(l, t_MAT);
    1522          21 :   gel(R,1) = M;
    1523         154 :   for(i = 2; i < l; i++)
    1524             :   {
    1525         133 :     GEN Li = gel(L,i);
    1526         133 :     GEN e = mkvec2(gdivgs(gel(Li,1), -48), gdivgs(gel(Li,2), -864));
    1527         133 :     GEN LMi = ellnf_isocrv(nf, ellinit(e, nf, DEFAULTPREC), v, P, 1);
    1528         133 :     GEN LLi = gel(LMi, 1), Mi = gel(LMi, 2);
    1529         133 :     GEN r = isomat_perm(nf, L, LLi);
    1530         133 :     gel(R,i) = vecpermute(Mi, r);
    1531             :   }
    1532          21 :   return mkvec2(L, R);
    1533             : }
    1534             : 
    1535             : static GEN
    1536         462 : list_to_crv(GEN L)
    1537             : {
    1538             :   long i, l;
    1539         462 :   GEN V = cgetg_copy(L, &l);
    1540        2156 :   for (i=1; i < l; i++)
    1541             :   {
    1542        1694 :     GEN Li = gel(L,i);
    1543        1694 :     GEN e = mkvec2(gdivgs(gel(Li,1), -48), gdivgs(gel(Li,2), -864));
    1544        1694 :     gel(V,i) = lg(Li)==6 ? mkvec3(e, gel(Li,4), gel(Li,5)): e;
    1545             :   }
    1546         462 :   return V;
    1547             : }
    1548             : 
    1549             : GEN
    1550         462 : ellisomat(GEN E, long p, long flag)
    1551             : {
    1552         462 :   pari_sp av = avma;
    1553         462 :   GEN r = NULL, nf = NULL;
    1554         462 :   long good = 1;
    1555         462 :   if (flag < 0 || flag > 1) pari_err_FLAG("ellisomat");
    1556         462 :   if (p < 0) pari_err_PRIME("ellisomat", utoi(p));
    1557         462 :   if (p == 1) { flag = 1; p = 0; } /* for backward compatibility */
    1558         462 :   checkell(E);
    1559         462 :   switch(ell_get_type(E))
    1560             :   {
    1561             :     case t_ELL_Q:
    1562         441 :       if (p) good = ellQ_goodl_l(E, p);
    1563         441 :       break;
    1564             :     case t_ELL_NF:
    1565          21 :       if (p) good = ellnf_goodl_l(E, mkvecsmall(p));
    1566          21 :       nf = ellnf_get_nf(E);
    1567          21 :       break;
    1568           0 :     default: pari_err_TYPE("ellisomat",E);
    1569             :   }
    1570         462 :   if (!good) r = mkvec2(mkvec(ellisograph_a4a6(E, flag)),matid(1));
    1571             :   else
    1572             :   {
    1573         462 :     if (p)
    1574          14 :       r = nfmkisomat(nf, p, ellisograph_p(nf, E, p, flag));
    1575             :     else
    1576         448 :       r = nf? ellnf_isomat(E, flag): ellQ_isomat(E, flag);
    1577         462 :     gel(r,1) = list_to_crv(gel(r,1));
    1578             :   }
    1579         462 :   return gerepilecopy(av, r);
    1580             : }
    1581             : 
    1582             : static GEN
    1583          42 : get_isomat(GEN v)
    1584             : {
    1585             :   GEN M, vE, wE;
    1586             :   long i, l;
    1587          42 :   if (typ(v) != t_VEC) return NULL;
    1588          42 :   if (checkell_i(v))
    1589             :   {
    1590          28 :     v = ellisomat(v,0,1);
    1591          28 :     wE = gel(v,1); l = lg(wE);
    1592          28 :     M  = gel(v,2);
    1593             :   }
    1594             :   else
    1595             :   {
    1596          14 :     if (lg(v) != 3) return NULL;
    1597          14 :     vE = gel(v,1); l = lg(vE);
    1598          14 :     M  = gel(v,2);
    1599          14 :     if (typ(M) != t_MAT || !RgM_is_ZM(M)) return NULL;
    1600          14 :     if (typ(vE) != t_VEC || l == 1) return NULL;
    1601          14 :     if (lg(gel(vE,1)) == 3) wE = shallowcopy(vE);
    1602             :     else
    1603             :     { /* [[a4,a6],f,g] */
    1604           7 :       wE = cgetg_copy(vE,&l);
    1605           7 :       for (i = 1; i < l; i++) gel(wE,i) = gel(gel(vE,i),1);
    1606             :     }
    1607             :   }
    1608             :   /* wE a vector of [a4,a6] */
    1609         252 :   for (i = 1; i < l; i++)
    1610             :   {
    1611         210 :     GEN e = ellinit(gel(wE,i), gen_1, 0), E = ellminimalmodel(e, NULL);
    1612         210 :     obj_free(e); gel(wE,i) = E;
    1613             :   }
    1614          42 :   return mkvec2(wE, M);
    1615             : }
    1616             : 
    1617             : GEN
    1618          42 : ellweilcurve(GEN E, GEN *ms)
    1619             : {
    1620          42 :   pari_sp av = avma;
    1621          42 :   GEN vE = get_isomat(E), vL, Wx, W, XPM, Lf, Cf;
    1622             :   long i, l;
    1623             : 
    1624          42 :   if (!vE) pari_err_TYPE("ellweilcurve",E);
    1625          42 :   vE = gel(vE,1); l = lg(vE);
    1626          42 :   Wx = msfromell(vE, 0);
    1627          42 :   W = gel(Wx,1);
    1628          42 :   XPM = gel(Wx,2);
    1629             :   /* lattice attached to the Weil curve in the isogeny class */
    1630          42 :   Lf = mslattice(W, gmael(XPM,1,3));
    1631          42 :   Cf = ginv(Lf); /* left-inverse */
    1632          42 :   vL = cgetg(l, t_VEC);
    1633         252 :   for (i=1; i < l; i++)
    1634             :   {
    1635         210 :     GEN c, Ce, Le = gmael(XPM,i,3);
    1636         210 :     Ce = Q_primitive_part(RgM_mul(Cf, Le), &c);
    1637         210 :     Ce = ZM_snf(Ce);
    1638         210 :     if (c) { Ce = ZC_Q_mul(Ce,c); settyp(Ce,t_VEC); }
    1639         210 :     gel(vL,i) = Ce;
    1640             :   }
    1641          42 :   for (i = 1; i < l; i++) obj_free(gel(vE,i));
    1642          42 :   vE = mkvec2(vE, vL);
    1643          42 :   if (!ms) return gerepilecopy(av, vE);
    1644           7 :   *ms = Wx; gerepileall(av, 2, &vE, ms); return vE;
    1645             : }
    1646             : 
    1647             : GEN
    1648           0 : ellisotree(GEN E)
    1649             : {
    1650           0 :   pari_sp av = avma;
    1651           0 :   GEN L = get_isomat(E), vE, adj, M;
    1652             :   long i, j, n;
    1653           0 :   if (!L) pari_err_TYPE("ellisotree",E);
    1654           0 :   vE = gel(L,1);
    1655           0 :   adj = gel(L,2);
    1656           0 :   n = lg(vE)-1; L = cgetg(n+1, t_VEC);
    1657           0 :   for (i = 1; i <= n; i++) gel(L,i) = ellR_area(gel(vE,i), LOWDEFAULTPREC);
    1658           0 :   M = zeromatcopy(n,n);
    1659           0 :   for (i = 1; i <= n; i++)
    1660           0 :     for (j = i+1; j <= n; j++)
    1661             :     {
    1662           0 :       GEN p = gcoeff(adj,i,j);
    1663           0 :       if (!isprime(p)) continue;
    1664             :       /* L[i] / L[j] = p or 1/p; p iff E[i].lattice \subset E[j].lattice */
    1665           0 :       if (gcmp(gel(L,i), gel(L,j)) > 0)
    1666           0 :         gcoeff(M,i,j) = p;
    1667             :       else
    1668           0 :         gcoeff(M,j,i) = p;
    1669             :     }
    1670           0 :   for (i = 1; i <= n; i++) obj_free(gel(vE,i));
    1671           0 :   return gerepilecopy(av, mkvec2(vE,M));
    1672             : }

Generated by: LCOV version 1.13