Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11