Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13