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

Generated by: LCOV version 1.13