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.8.0 lcov report (development 19588-1c9967d) Lines: 640 654 97.9 %
Date: 2016-09-24 05:54:29 Functions: 54 54 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             : 
      16             : /* Return 1 if the point Q is a Weierstrass (2-torsion) point of the
      17             :  * curve E, return 0 otherwise */
      18             : static long
      19         903 : ellisweierstrasspoint(GEN E, GEN Q)
      20         903 : { return ell_is_inf(Q) || gequal0(ec_dmFdy_evalQ(E, Q)); }
      21             : 
      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        1694 : make_velu_curve(GEN E, GEN t, GEN w)
      28             : {
      29        1694 :   GEN A4, A6, a1 = ell_get_a1(E), a2 = ell_get_a2(E), a3 = ell_get_a3(E);
      30        1694 :   A4 = gsub(ell_get_a4(E), gmulsg(5L, t));
      31        1694 :   A6 = gsub(ell_get_a6(E), gadd(gmul(ell_get_b2(E), t), gmulsg(7L, w)));
      32        1694 :   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        1106 : get_isog_vars(GEN phi, long *vx, long *vy)
      39             : {
      40        1106 :   *vx = varn(gel(phi, 1));
      41        1106 :   *vy = varn(gel(phi, 2));
      42        1106 :   if (*vy == *vx) *vy = gvar2(gel(phi,2));
      43        1106 : }
      44             : 
      45             : static GEN
      46        4312 : RgX_homogenous_evalpow(GEN P, GEN A, GEN B)
      47             : {
      48        4312 :   pari_sp av = avma;
      49             :   long d, i, v;
      50             :   GEN s;
      51        4312 :   if (typ(P)!=t_POL)
      52        1351 :     return mkvec2(P, gen_1);
      53        2961 :   d = degpol(P); v = varn(A);
      54        2961 :   s = scalarpol_shallow(gel(P, d+2), v);
      55       16646 :   for (i = d-1; i >= 0; i--)
      56             :   {
      57       13685 :     s = gadd(gmul(s, A), gmul(gel(B,d+1-i), gel(P,i+2)));
      58       13685 :     if (gc_needed(av,1))
      59             :     {
      60           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"RgX_homogenous_eval(%ld)",i);
      61           0 :       s = gerepileupto(av, s);
      62             :     }
      63             :   }
      64        2961 :   s = gerepileupto(av, s);
      65        2961 :   return mkvec2(s, gel(B,d+1));
      66             : }
      67             : 
      68             : /* Given isogenies F:E' -> E and G:E'' -> E', return the composite
      69             :  * isogeny F o G:E'' -> E */
      70             : static GEN
      71        1085 : ellcompisog(GEN F, GEN G)
      72             : {
      73        1085 :   pari_sp av = avma;
      74             :   GEN Fv, Gh, Gh2, Gh3, f, g, h, h2, h3, den, num;
      75             :   GEN K, K2, K3, F0, F1, g0, g1, Gp;
      76             :   long v, vx, vy, d;
      77        1085 :   checkellisog(F);
      78        1078 :   checkellisog(G);
      79        1078 :   get_isog_vars(F, &vx, &vy);
      80        1078 :   v = fetch_var_higher();
      81        1078 :   Fv = shallowcopy(gel(F,3)); setvarn(Fv, v);
      82        1078 :   Gh = gel(G,3); Gh2 = gsqr(Gh); Gh3 = gmul(Gh, Gh2);
      83        1078 :   K = gmul(polresultant0(Fv, deg1pol(gneg(Gh2),gel(G,1), v), v, 0), Gh);
      84        1078 :   delete_var();
      85        1078 :   K = RgX_normalize(RgX_div(K, RgX_gcd(K,deriv(K,0))));
      86        1078 :   K2 = gsqr(K); K3 = gmul(K, K2);
      87        1078 :   F0 = polcoeff0(gel(F,2), 0, vy); F1 = polcoeff0(gel(F,2), 1, vy);
      88        1078 :   d = maxss(maxss(degpol(gel(F,1)),degpol(gel(F,3))),maxss(degpol(F0),degpol(F1)));
      89        1078 :   Gp = gpowers(Gh2, d);
      90        1078 :   f  = RgX_homogenous_evalpow(gel(F,1), gel(G,1), Gp);
      91        1078 :   g0 = RgX_homogenous_evalpow(F0, gel(G,1), Gp);
      92        1078 :   g1 = RgX_homogenous_evalpow(F1, gel(G,1), Gp);
      93        1078 :   h =  RgX_homogenous_evalpow(gel(F,3), gel(G,1), Gp);
      94        1078 :   h2 = mkvec2(gsqr(gel(h,1)), gsqr(gel(h,2)));
      95        1078 :   h3 = mkvec2(gmul(gel(h,1),gel(h2,1)), gmul(gel(h,2),gel(h2,2)));
      96        1078 :   f  = gdiv(gmul(gmul(K2, gel(f,1)),gel(h2,2)), gmul(gel(f,2), gel(h2,1)));
      97        1078 :   den = gmul(Gh3, gel(g1,2));
      98        1078 :   num = gadd(gmul(gel(g0,1),den), gmul(gmul(gel(G,2),gel(g1,1)),gel(g0,2)));
      99        1078 :   g = gdiv(gmul(gmul(K3,num),gel(h3,2)),gmul(gmul(gel(g0,2),den), gel(h3,1)));
     100        1078 :   return gerepilecopy(av, mkvec3(f,g,K));
     101             : }
     102             : 
     103             : /* Given an isogeny phi from ellisogeny() and a point P in the domain of phi,
     104             :  * return phi(P) */
     105             : GEN
     106        1134 : ellisogenyapply(GEN phi, GEN P)
     107             : {
     108        1134 :   pari_sp ltop = avma;
     109             :   GEN f, g, h, img_f, img_g, img_h, img_h2, img_h3, img, tmp;
     110             :   long vx, vy;
     111        1134 :   if (lg(P) == 4) return ellcompisog(phi,P);
     112          49 :   checkellisog(phi);
     113          49 :   checkellpt(P);
     114          42 :   if (ell_is_inf(P)) return ellinf();
     115          28 :   f = gel(phi, 1);
     116          28 :   g = gel(phi, 2);
     117          28 :   h = gel(phi, 3);
     118          28 :   get_isog_vars(phi, &vx, &vy);
     119          28 :   img_h = poleval(h, gel(P, 1));
     120          28 :   if (gequal0(img_h)) { avma = ltop; return ellinf(); }
     121             : 
     122          21 :   img_h2 = gsqr(img_h);
     123          21 :   img_h3 = gmul(img_h, img_h2);
     124          21 :   img_f = poleval(f, gel(P, 1));
     125             :   /* FIXME: This calculation of g is perhaps not as efficient as it could be */
     126          21 :   tmp = gsubst(g, vx, gel(P, 1));
     127          21 :   img_g = gsubst(tmp, vy, gel(P, 2));
     128          21 :   img = cgetg(3, t_VEC);
     129          21 :   gel(img, 1) = gdiv(img_f, img_h2);
     130          21 :   gel(img, 2) = gdiv(img_g, img_h3);
     131          21 :   return gerepileupto(ltop, img);
     132             : }
     133             : 
     134             : /* isog = [f, g, h] = [x, y, 1] = identity */
     135             : static GEN
     136         252 : isog_identity(long vx, long vy)
     137         252 : { return mkvec3(pol_x(vx), pol_x(vy), pol_1(vx)); }
     138             : 
     139             : /* Returns an updated value for isog based (primarily) on tQ and uQ. Used to
     140             :  * iteratively compute the isogeny corresponding to a subgroup generated by a
     141             :  * given point. Ref: Equation 8 in Velu's paper.
     142             :  * isog = NULL codes the identity */
     143             : static GEN
     144         532 : update_isogeny_polys(GEN isog, GEN E, GEN Q, GEN tQ, GEN uQ, long vx, long vy)
     145             : {
     146         532 :   pari_sp ltop = avma, av;
     147         532 :   GEN xQ = gel(Q, 1), yQ = gel(Q, 2);
     148         532 :   GEN rt = deg1pol_shallow(gen_1, gneg(xQ), vx);
     149         532 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E);
     150             : 
     151         532 :   GEN gQx = ec_dFdx_evalQ(E, Q);
     152         532 :   GEN gQy = ec_dFdy_evalQ(E, Q);
     153             :   GEN tmp1, tmp2, tmp3, tmp4, f, g, h, rt_sqr, res;
     154             : 
     155             :   /* g -= uQ * (2 * y + E.a1 * x + E.a3)
     156             :    *   + tQ * rt * (E.a1 * rt + y - yQ)
     157             :    *   + rt * (E.a1 * uQ - gQx * gQy) */
     158         532 :   av = avma;
     159         532 :   tmp1 = gmul(uQ, gadd(deg1pol_shallow(gen_2, gen_0, vy),
     160             :                        deg1pol_shallow(a1, a3, vx)));
     161         532 :   tmp1 = gerepileupto(av, tmp1);
     162         532 :   av = avma;
     163         532 :   tmp2 = gmul(tQ, gadd(gmul(a1, rt),
     164             :                        deg1pol_shallow(gen_1, gneg(yQ), vy)));
     165         532 :   tmp2 = gerepileupto(av, tmp2);
     166         532 :   av = avma;
     167         532 :   tmp3 = gsub(gmul(a1, uQ), gmul(gQx, gQy));
     168         532 :   tmp3 = gerepileupto(av, tmp3);
     169             : 
     170         532 :   if (!isog) isog = isog_identity(vx,vy);
     171         532 :   f = gel(isog, 1);
     172         532 :   g = gel(isog, 2);
     173         532 :   h = gel(isog, 3);
     174         532 :   rt_sqr = gsqr(rt);
     175         532 :   res = cgetg(4, t_VEC);
     176         532 :   av = avma;
     177         532 :   tmp4 = gdiv(gadd(gmul(tQ, rt), uQ), rt_sqr);
     178         532 :   gel(res, 1) = gerepileupto(av, gadd(f, tmp4));
     179         532 :   av = avma;
     180         532 :   tmp4 = gadd(tmp1, gmul(rt, gadd(tmp2, tmp3)));
     181         532 :   gel(res, 2) = gerepileupto(av, gsub(g, gdiv(tmp4, gmul(rt, rt_sqr))));
     182         532 :   av = avma;
     183         532 :   gel(res, 3) = gerepileupto(av, gmul(h, rt));
     184         532 :   return gerepileupto(ltop, res);
     185             : }
     186             : 
     187             : /* Given a point P on E, return the curve E/<P> and, if only_image is zero,
     188             :  * the isogeny pi: E -> E/<P>. The variables vx and vy are used to describe
     189             :  * the isogeny (ignored if only_image is zero) */
     190             : static GEN
     191         427 : isogeny_from_kernel_point(GEN E, GEN P, int only_image, long vx, long vy)
     192             : {
     193         427 :   pari_sp av = avma;
     194             :   GEN isog, EE, f, g, h, h2, h3;
     195         427 :   GEN Q = P, t = gen_0, w = gen_0;
     196             :   long c;
     197         427 :   if (!oncurve(E,P))
     198           7 :     pari_err_DOMAIN("isogeny_from_kernel_point", "point", "not on", E, P);
     199         420 :   if (ell_is_inf(P))
     200             :   {
     201          42 :     if (only_image) return E;
     202          28 :     return mkvec2(E, isog_identity(vx,vy));
     203             :   }
     204             : 
     205         378 :   isog = NULL; c = 1;
     206             :   for (;;)
     207             :   {
     208         903 :     GEN tQ, xQ = gel(Q,1), uQ = ec_2divpol_evalx(E, xQ);
     209         903 :     int stop = 0;
     210         903 :     if (ellisweierstrasspoint(E,Q))
     211             :     { /* ord(P)=2c; take Q=[c]P into consideration and stop */
     212         196 :       tQ = ec_dFdx_evalQ(E, Q);
     213         196 :       stop = 1;
     214             :     }
     215             :     else
     216         707 :       tQ = ec_half_deriv_2divpol_evalx(E, xQ);
     217         903 :     t = gadd(t, tQ);
     218         903 :     w = gadd(w, gadd(uQ, gmul(tQ, xQ)));
     219         903 :     if (!only_image) isog = update_isogeny_polys(isog, E, Q,tQ,uQ, vx,vy);
     220         903 :     if (stop) break;
     221             : 
     222         707 :     Q = elladd(E, P, Q);
     223         707 :     ++c;
     224             :     /* IF x([c]P) = x([c-1]P) THEN [c]P = -[c-1]P and [2c-1]P = 0 */
     225         707 :     if (gequal(gel(Q,1), xQ)) break;
     226         525 :     if (gc_needed(av,1))
     227             :     {
     228           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"isogeny_from_kernel_point");
     229           0 :       gerepileall(av, isog? 4: 3, &Q, &t, &w, &isog);
     230             :     }
     231         525 :   }
     232             : 
     233         378 :   EE = make_velu_curve(E, t, w);
     234         378 :   if (only_image) return EE;
     235             : 
     236         224 :   if (!isog) isog = isog_identity(vx,vy);
     237         224 :   f = gel(isog, 1);
     238         224 :   g = gel(isog, 2);
     239         224 :   if ( ! (typ(f) == t_RFRAC && typ(g) == t_RFRAC))
     240           0 :     pari_err_BUG("isogeny_from_kernel_point (f or g has wrong type)");
     241             : 
     242             :   /* Clean up the isogeny polynomials f, g and h so that the isogeny
     243             :    * is given by (x,y) -> (f(x)/h(x)^2, g(x,y)/h(x)^3) */
     244         224 :   h = gel(isog, 3);
     245         224 :   h2 = gsqr(h);
     246         224 :   h3 = gmul(h, h2);
     247         224 :   f = gmul(f, h2);
     248         224 :   g = gmul(g, h3);
     249         224 :   if (typ(f) != t_POL || typ(g) != t_POL)
     250           0 :     pari_err_BUG("isogeny_from_kernel_point (wrong denominator)");
     251         224 :   return mkvec2(EE, mkvec3(f,g, gel(isog,3)));
     252             : }
     253             : 
     254             : /* Given a t_POL x^n - s1 x^{n-1} + s2 x^{n-2} - s3 x^{n-3} + ...
     255             :  * return the first three power sums (Newton's identities):
     256             :  *   p1 = s1
     257             :  *   p2 = s1^2 - 2 s2
     258             :  *   p3 = (s1^2 - 3 s2) s1 + 3 s3 */
     259             : static void
     260        1330 : first_three_power_sums(GEN pol, GEN *p1, GEN *p2, GEN *p3)
     261             : {
     262        1330 :   long d = degpol(pol);
     263             :   GEN s1, s2, ms3;
     264             : 
     265        1330 :   *p1 = s1 = gneg(RgX_coeff(pol, d-1));
     266             : 
     267        1330 :   s2 = RgX_coeff(pol, d-2);
     268        1330 :   *p2 = gsub(gsqr(s1), gmulsg(2L, s2));
     269             : 
     270        1330 :   ms3 = RgX_coeff(pol, d-3);
     271        1330 :   *p3 = gadd(gmul(s1, gsub(*p2, s2)), gmulsg(-3L, ms3));
     272        1330 : }
     273             : 
     274             : 
     275             : /* Let E and a t_POL h of degree 1 or 3 whose roots are 2-torsion points on E.
     276             :  * - if only_image != 0, return [t, w] used to compute the equation of the
     277             :  *   quotient by the given 2-torsion points
     278             :  * - else return [t,w, f,g,h], along with the contributions f, g and
     279             :  *   h to the isogeny giving the quotient by h. Variables vx and vy are used
     280             :  *   to create f, g and h, or ignored if only_image is zero */
     281             : 
     282             : /* deg h = 1; 2-torsion contribution from Weierstrass point */
     283             : static GEN
     284         623 : contrib_weierstrass_pt(GEN E, GEN h, long only_image, long vx, long vy)
     285             : {
     286         623 :   GEN p = ellbasechar(E);
     287         623 :   GEN a1 = ell_get_a1(E);
     288         623 :   GEN a3 = ell_get_a3(E);
     289         623 :   GEN x0 = gneg(constant_coeff(h)); /* h = x - x0 */
     290         623 :   GEN b = gadd(gmul(a1,x0), a3);
     291             :   GEN y0, Q, t, w, t1, t2, f, g;
     292             : 
     293         623 :   if (!equalis(p, 2L)) /* char(k) != 2 ==> y0 = -b/2 */
     294         581 :     y0 = gmul2n(gneg(b), -1);
     295             :   else
     296             :   { /* char(k) = 2 ==> y0 = sqrt(f(x0)) where E is y^2 + h(x) = f(x). */
     297          42 :     if (!gequal0(b)) pari_err_BUG("two_torsion_contrib (a1*x0+a3 != 0)");
     298          42 :     y0 = gsqrt(ec_f_evalx(E, x0), 0);
     299             :   }
     300         623 :   Q = mkvec2(x0, y0);
     301         623 :   t = ec_dFdx_evalQ(E, Q);
     302         623 :   w = gmul(x0, t);
     303         623 :   if (only_image) return mkvec2(t,w);
     304             : 
     305             :   /* Compute isogeny, f = (x - x0) * t */
     306         392 :   f = deg1pol_shallow(t, gmul(t, gneg(x0)), vx);
     307             : 
     308             :   /* g = (x - x0) * t * (a1 * (x - x0) + (y - y0)) */
     309         392 :   t1 = deg1pol_shallow(a1, gmul(a1, gneg(x0)), vx);
     310         392 :   t2 = deg1pol_shallow(gen_1, gneg(y0), vy);
     311         392 :   g = gmul(f, gadd(t1, t2));
     312         392 :   return mkvec5(t, w, f, g, h);
     313             : }
     314             : /* deg h =3; full 2-torsion contribution. NB: assume h is monic; base field
     315             :  * characteristic is odd or zero (cannot happen in char 2).*/
     316             : static GEN
     317          14 : contrib_full_tors(GEN E, GEN h, long only_image, long vx, long vy)
     318             : {
     319             :   GEN p1, p2, p3, half_b2, half_b4, t, w, f, g;
     320          14 :   first_three_power_sums(h, &p1,&p2,&p3);
     321          14 :   half_b2 = gmul2n(ell_get_b2(E), -1);
     322          14 :   half_b4 = gmul2n(ell_get_b4(E), -1);
     323             : 
     324             :   /* t = 3*(p2 + b4/2) + p1 * b2/2 */
     325          14 :   t = gadd(gmulsg(3L, gadd(p2, half_b4)), gmul(p1, half_b2));
     326             : 
     327             :   /* w = 3 * p3 + p2 * b2/2 + p1 * b4/2 */
     328          14 :   w = gadd(gmulsg(3L, p3), gadd(gmul(p2, half_b2),
     329             :                                 gmul(p1, half_b4)));
     330          14 :   if (only_image) return mkvec2(t,w);
     331             : 
     332             :   /* Compute isogeny */
     333             :   {
     334           7 :     GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), t1, t2;
     335           7 :     GEN s1 = gneg(RgX_coeff(h, 2));
     336           7 :     GEN dh = RgX_deriv(h);
     337           7 :     GEN psi2xy = gadd(deg1pol_shallow(a1, a3, vx),
     338             :                       deg1pol_shallow(gen_2, gen_0, vy));
     339             : 
     340             :     /* f = -3 (3 x + b2/2 + s1) h + (3 x^2 + (b2/2) x + (b4/2)) h'*/
     341           7 :     t1 = RgX_mul(h, gmulsg(-3, deg1pol(stoi(3), gadd(half_b2, s1), vx)));
     342           7 :     t2 = mkpoln(3, stoi(3), half_b2, half_b4);
     343           7 :     setvarn(t2, vx);
     344           7 :     t2 = RgX_mul(dh, t2);
     345           7 :     f = RgX_add(t1, t2);
     346             : 
     347             :     /* 2g = psi2xy * (f'*h - f*h') - (a1*f + a3*h) * h; */
     348           7 :     t1 = RgX_sub(RgX_mul(RgX_deriv(f), h), RgX_mul(f, dh));
     349           7 :     t2 = RgX_mul(h, RgX_add(RgX_Rg_mul(f, a1), RgX_Rg_mul(h, a3)));
     350           7 :     g = RgX_divs(gsub(gmul(psi2xy, t1), t2), 2L);
     351             : 
     352           7 :     f = RgX_mul(f, h);
     353           7 :     g = RgX_mul(g, h);
     354             :   }
     355           7 :   return mkvec5(t, w, f, g, h);
     356             : }
     357             : 
     358             : /* Given E and a t_POL T whose roots define a subgroup G of E, return the factor
     359             :  * of T that corresponds to the 2-torsion points E[2] \cap G in G */
     360             : INLINE GEN
     361        1323 : two_torsion_part(GEN E, GEN T)
     362        1323 : { return RgX_gcd(T, elldivpol(E, 2, varn(T))); }
     363             : 
     364             : /* Return the jth Hasse derivative of the polynomial f = \sum_{i=0}^n a_i x^i,
     365             :  * i.e. \sum_{i=j}^n a_i \binom{i}{j} x^{i-j}. It is a derivation even when the
     366             :  * coefficient ring has positive characteristic */
     367             : static GEN
     368          98 : derivhasse(GEN f, ulong j)
     369             : {
     370          98 :   ulong i, d = degpol(f);
     371             :   GEN df;
     372          98 :   if (gequal0(f) || d == 0) return pol_0(varn(f));
     373          56 :   if (j == 0) return gcopy(f);
     374          56 :   df = cgetg(2 + (d-j+1), t_POL);
     375          56 :   df[1] = f[1];
     376          56 :   for (i = j; i <= d; ++i) gel(df, i-j+2) = gmul(binomialuu(i,j), gel(f, i+2));
     377          56 :   return normalizepol(df);
     378             : }
     379             : 
     380             : static GEN
     381         546 : non_two_torsion_abscissa(GEN E, GEN h0, GEN x)
     382             : {
     383             :   GEN mp1, dh0, ddh0, t, u, t1, t2, t3;
     384         546 :   long m = degpol(h0);
     385         546 :   mp1 = gel(h0, m + 1); /* negative of first power sum */
     386         546 :   dh0 = RgX_deriv(h0);
     387         546 :   ddh0 = RgX_deriv(dh0);
     388         546 :   t = ec_2divpol_evalx(E, x);
     389         546 :   u = ec_half_deriv_2divpol_evalx(E, x);
     390         546 :   t1 = RgX_sub(RgX_sqr(dh0), RgX_mul(ddh0, h0));
     391         546 :   t2 = RgX_mul(u, RgX_mul(h0, dh0));
     392         546 :   t3 = RgX_mul(RgX_sqr(h0),
     393         546 :                deg1pol_shallow(stoi(2*m), gmulsg(2L, mp1), varn(x)));
     394             :   /* t * (dh0^2 - ddh0*h0) - u*dh0*h0 + (2*m*x - 2*s1) * h0^2); */
     395         546 :   return RgX_add(RgX_sub(RgX_mul(t, t1), t2), t3);
     396             : }
     397             : 
     398             : static GEN
     399         910 : isog_abscissa(GEN E, GEN kerp, GEN h0, GEN x, GEN two_tors)
     400             : {
     401             :   GEN f0, f2, h2, t1, t2, t3;
     402         910 :   f0 = (degpol(h0) > 0)? non_two_torsion_abscissa(E, h0, x): pol_0(varn(x));
     403         910 :   f2 = gel(two_tors, 3);
     404         910 :   h2 = gel(two_tors, 5);
     405             : 
     406             :   /* Combine f0 and f2 into the final abcissa of the isogeny. */
     407         910 :   t1 = RgX_mul(x, RgX_sqr(kerp));
     408         910 :   t2 = RgX_mul(f2, RgX_sqr(h0));
     409         910 :   t3 = RgX_mul(f0, RgX_sqr(h2));
     410             :   /* x * kerp^2 + f2 * h0^2 + f0 * h2^2 */
     411         910 :   return RgX_add(t1, RgX_add(t2, t3));
     412             : }
     413             : 
     414             : static GEN
     415         861 : non_two_torsion_ordinate_char_not2(GEN E, GEN f, GEN h, GEN psi2)
     416             : {
     417         861 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E);
     418         861 :   GEN df = RgX_deriv(f), dh = RgX_deriv(h);
     419             :   /* g = df * h * psi2/2 - f * dh * psi2
     420             :    *   - (E.a1 * f + E.a3 * h^2) * h/2 */
     421         861 :   GEN t1 = RgX_mul(df, RgX_mul(h, RgX_divs(psi2, 2L)));
     422         861 :   GEN t2 = RgX_mul(f, RgX_mul(dh, psi2));
     423         861 :   GEN t3 = RgX_mul(RgX_divs(h, 2L),
     424             :                    RgX_add(RgX_Rg_mul(f, a1), RgX_Rg_mul(RgX_sqr(h), a3)));
     425         861 :   return RgX_sub(RgX_sub(t1, t2), t3);
     426             : }
     427             : 
     428             : /* h = kerq */
     429             : static GEN
     430          49 : non_two_torsion_ordinate_char2(GEN E, GEN h, GEN x, GEN y)
     431             : {
     432          49 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), a4 = ell_get_a4(E);
     433          49 :   GEN b2 = ell_get_b2(E), b4 = ell_get_b4(E), b6 = ell_get_b6(E);
     434             :   GEN h2, dh, dh2, ddh, D2h, D2dh, H, psi2, u, t, alpha;
     435             :   GEN p1, t1, t2, t3, t4;
     436          49 :   long m, vx = varn(x);
     437             : 
     438          49 :   h2 = RgX_sqr(h);
     439          49 :   dh = RgX_deriv(h);
     440          49 :   dh2 = RgX_sqr(dh);
     441          49 :   ddh = RgX_deriv(dh);
     442          49 :   H = RgX_sub(dh2, RgX_mul(h, ddh));
     443          49 :   D2h = derivhasse(h, 2);
     444          49 :   D2dh = derivhasse(dh, 2);
     445          49 :   psi2 = deg1pol_shallow(a1, a3, vx);
     446          49 :   u = mkpoln(3, b2, gen_0, b6);
     447          49 :   setvarn(u, vx);
     448          49 :   t = deg1pol_shallow(b2, b4, vx);
     449          49 :   alpha = mkpoln(4, a1, a3, gmul(a1, a4), gmul(a3, a4));
     450          49 :   setvarn(alpha, vx);
     451          49 :   m = degpol(h);
     452          49 :   p1 = RgX_coeff(h, m-1); /* first power sum */
     453             : 
     454          49 :   t1 = gmul(gadd(gmul(a1, p1), gmulgs(a3, m)), RgX_mul(h,h2));
     455             : 
     456          49 :   t2 = gmul(a1, gadd(gmul(a1, gadd(y, psi2)), RgX_add(RgX_Rg_add(RgX_sqr(x), a4), t)));
     457          49 :   t2 = gmul(t2, gmul(dh, h2));
     458             : 
     459          49 :   t3 = gadd(gmul(y, t), RgX_add(alpha, RgX_Rg_mul(u, a1)));
     460          49 :   t3 = gmul(t3, RgX_mul(h, H));
     461             : 
     462          49 :   t4 = gmul(u, psi2);
     463          49 :   t4 = gmul(t4, RgX_sub(RgX_sub(RgX_mul(h2, D2dh), RgX_mul(dh, H)),
     464             :                         RgX_mul(h, RgX_mul(dh, D2h))));
     465             : 
     466          49 :   return gadd(t1, gadd(t2, gadd(t3, t4)));
     467             : }
     468             : 
     469             : static GEN
     470         910 : isog_ordinate(GEN E, GEN kerp, GEN kerq, GEN x, GEN y, GEN two_tors, GEN f)
     471             : {
     472             :   GEN g;
     473         910 :   if (! equalis(ellbasechar(E), 2L)) {
     474             :     /* FIXME: We don't use (hence don't need to calculate)
     475             :      * g2 = gel(two_tors, 4) when char(k) != 2. */
     476         861 :     GEN psi2 = ec_dmFdy_evalQ(E, mkvec2(x, y));
     477         861 :     g = non_two_torsion_ordinate_char_not2(E, f, kerp, psi2);
     478             :   } else {
     479          49 :     GEN h2 = gel(two_tors, 5);
     480          49 :     GEN g2 = gmul(gel(two_tors, 4), RgX_mul(kerq, RgX_sqr(kerq)));
     481          49 :     GEN g0 = non_two_torsion_ordinate_char2(E, kerq, x, y);
     482          49 :     g0 = gmul(g0, RgX_mul(h2, RgX_sqr(h2)));
     483          49 :     g = gsub(gmul(y, RgX_mul(kerp, RgX_sqr(kerp))), gadd(g2, g0));
     484             :   }
     485         910 :   return g;
     486             : }
     487             : 
     488             : /* Given an elliptic curve E and a polynomial kerp whose roots give the
     489             :  * x-coordinates of a subgroup G of E, return the curve E/G and,
     490             :  * if only_image is zero, the isogeny pi:E -> E/G. Variables vx and vy are
     491             :  * used to describe the isogeny (and are ignored if only_image is zero). */
     492             : static GEN
     493        1323 : isogeny_from_kernel_poly(GEN E, GEN kerp, long only_image, long vx, long vy)
     494             : {
     495             :   long m;
     496        1323 :   GEN b2 = ell_get_b2(E), b4 = ell_get_b4(E), b6 = ell_get_b6(E);
     497             :   GEN p1, p2, p3, x, y, f, g, two_tors, EE, t, w;
     498        1323 :   GEN kerh = two_torsion_part(E, kerp);
     499        1323 :   GEN kerq = RgX_divrem(kerp, kerh, ONLY_DIVIDES);
     500        1323 :   if (!kerq) pari_err_BUG("isogeny_from_kernel_poly");
     501             :   /* isogeny degree: 2*degpol(kerp)+1-degpol(kerh) */
     502        1323 :   m = degpol(kerq);
     503             : 
     504        1323 :   kerp = RgX_Rg_div(kerp, leading_coeff(kerp));
     505        1323 :   kerq = RgX_Rg_div(kerq, leading_coeff(kerq));
     506        1323 :   kerh = RgX_Rg_div(kerh, leading_coeff(kerh));
     507        1323 :   switch(degpol(kerh))
     508             :   {
     509             :   case 0:
     510         679 :     two_tors = mkvec5(gen_0, gen_0, pol_0(vx), pol_0(vx), pol_1(vx));
     511         679 :     break;
     512             :   case 1:
     513         623 :     two_tors = contrib_weierstrass_pt(E, kerh, only_image,vx,vy);
     514         623 :     break;
     515             :   case 3:
     516          14 :     two_tors = contrib_full_tors(E, kerh, only_image,vx,vy);
     517          14 :     break;
     518             :   default:
     519           7 :     two_tors = NULL;
     520           7 :     pari_err_DOMAIN("isogeny_from_kernel_poly", "kernel polynomial",
     521             :                     "does not define a subgroup of", E, kerp);
     522             :   }
     523        1316 :   first_three_power_sums(kerq,&p1,&p2,&p3);
     524        1316 :   x = pol_x(vx);
     525        1316 :   y = pol_x(vy);
     526             : 
     527             :   /* t = 6 * p2 + b2 * p1 + m * b4, */
     528        1316 :   t = gadd(gmulsg(6L, p2), gadd(gmul(b2, p1), gmulsg(m, b4)));
     529             : 
     530             :   /* w = 10 * p3 + 2 * b2 * p2 + 3 * b4 * p1 + m * b6, */
     531        1316 :   w = gadd(gmulsg(10L, p3),
     532             :            gadd(gmul(gmulsg(2L, b2), p2),
     533             :                 gadd(gmul(gmulsg(3L, b4), p1), gmulsg(m, b6))));
     534             : 
     535        1316 :   EE = make_velu_curve(E, gadd(t, gel(two_tors, 1)),
     536        1316 :                           gadd(w, gel(two_tors, 2)));
     537        1316 :   if (only_image) return EE;
     538             : 
     539         910 :   f = isog_abscissa(E, kerp, kerq, x, two_tors);
     540         910 :   g = isog_ordinate(E, kerp, kerq, x, y, two_tors, f);
     541         910 :   return mkvec2(EE, mkvec3(f,g,kerp));
     542             : }
     543             : 
     544             : /* Given an elliptic curve E and a subgroup G of E, return the curve
     545             :  * E/G and, if only_image is zero, the isogeny corresponding
     546             :  * to the canonical surjection pi:E -> E/G. The variables vx and
     547             :  * vy are used to describe the isogeny (and are ignored if
     548             :  * only_image is zero). The subgroup G may be given either as
     549             :  * a generating point P on E or as a polynomial kerp whose roots are
     550             :  * the x-coordinates of the points in G */
     551             : GEN
     552         833 : ellisogeny(GEN E, GEN G, long only_image, long vx, long vy)
     553             : {
     554         833 :   pari_sp av = avma;
     555             :   GEN j, z;
     556         833 :   checkell(E);j = ell_get_j(E);
     557         833 :   if (vx < 0) vx = 0;
     558         833 :   if (vy < 0) vy = 1;
     559         833 :   if (varncmp(vx, vy) >= 0) pari_err_PRIORITY("ellisogeny", pol_x(vx), "<=", vy);
     560         826 :   if (varncmp(vy, gvar(j)) >= 0) pari_err_PRIORITY("ellisogeny", j, ">=", vy);
     561         819 :   switch(typ(G))
     562             :   {
     563             :   case t_VEC:
     564         441 :     checkellpt(G);
     565         441 :     if (!ell_is_inf(G))
     566             :     {
     567         399 :       GEN x =  gel(G,1), y = gel(G,2);
     568         399 :       if (varncmp(vy, gvar(x)) >= 0) pari_err_PRIORITY("ellisogeny", x, ">=", vy);
     569         392 :       if (varncmp(vy, gvar(y)) >= 0) pari_err_PRIORITY("ellisogeny", y, ">=", vy);
     570             :     }
     571         427 :     z = isogeny_from_kernel_point(E, G, only_image, vx, vy);
     572         420 :     break;
     573             :   case t_POL:
     574         371 :     if (varncmp(vy, gvar(constant_coeff(G))) >= 0)
     575           7 :       pari_err_PRIORITY("ellisogeny", constant_coeff(G), ">=", vy);
     576         364 :     z = isogeny_from_kernel_poly(E, G, only_image, vx, vy);
     577         357 :     break;
     578             :   default:
     579           7 :     z = NULL;
     580           7 :     pari_err_TYPE("ellisogeny", G);
     581             :   }
     582         777 :   return gerepilecopy(av, z);
     583             : }
     584             : 
     585             : static GEN
     586         658 : trivial_isogeny(void)
     587             : {
     588         658 :   return mkvec3(pol_x(0), scalarpol(pol_x(1), 0), pol_1(0));
     589             : }
     590             : 
     591             : static GEN
     592         294 : isogeny_a4a6(GEN E)
     593             : {
     594         294 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), b2 = ell_get_b2(E);
     595         294 :   retmkvec3(deg1pol(gen_1, gdivgs(b2, 12), 0),
     596             :             deg1pol(gdivgs(a1,2), deg1pol(gen_1, gdivgs(a3,2), 1), 0),
     597             :             pol_1(0));
     598             : }
     599             : 
     600             : static GEN
     601         294 : invisogeny_a4a6(GEN E)
     602             : {
     603         294 :   GEN a1 = ell_get_a1(E), a3 = ell_get_a3(E), b2 = ell_get_b2(E);
     604         294 :   retmkvec3(deg1pol(gen_1, gdivgs(b2, -12), 0),
     605             :             deg1pol(gdivgs(a1,-2),
     606             :               deg1pol(gen_1, gadd(gdivgs(a3,-2), gdivgs(gmul(b2,a1), 24)), 1), 0),
     607             :             pol_1(0));
     608             : }
     609             : 
     610             : static GEN
     611         490 : RgXY_eval(GEN P, GEN x, GEN y)
     612             : {
     613         490 :   return poleval(poleval(P,x), y);
     614             : }
     615             : 
     616             : static GEN
     617         364 : twistisogeny(GEN iso, GEN d)
     618             : {
     619         364 :   GEN d2 = gsqr(d), d3 = gmul(d, d2);
     620         364 :   return mkvec3(gdiv(gel(iso,1), d2), gdiv(gel(iso,2), d3), gel(iso, 3));
     621             : }
     622             : 
     623             : static GEN
     624         595 : ellisog_by_Kohel(GEN a4, GEN a6, long n, GEN ker, GEN kert, long flag)
     625             : {
     626         595 :   GEN E = ellinit(mkvec2(a4, a6), NULL, DEFAULTPREC);
     627         595 :   GEN F = isogeny_from_kernel_poly(E, ker, flag, 0, 1);
     628         595 :   GEN Et = ellinit(flag ? F: gel(F, 1), NULL, DEFAULTPREC);
     629         595 :   GEN c4t = ell_get_c4(Et), c6t = ell_get_c6(Et), jt = ell_get_j(Et);
     630         595 :   if (!flag)
     631             :   {
     632         364 :     GEN Ft = isogeny_from_kernel_poly(Et, kert, flag, 0, 1);
     633         364 :     GEN isot = twistisogeny(gel(Ft, 2), stoi(n));
     634         364 :     return mkvec5(c4t, c6t, jt, gel(F, 2), isot);
     635             :   }
     636         231 :   else return mkvec3(c4t, c6t, jt);
     637             : }
     638             : 
     639             : static GEN
     640         462 : ellisog_by_roots(GEN a4, GEN a6, long n, GEN z, long flag)
     641             : {
     642         462 :   return ellisog_by_Kohel(a4, a6, n, deg1pol(gen_1, gneg(z), 0),
     643             :                                   deg1pol(gen_1, gmulsg(n, z), 0), flag);
     644             : }
     645             : 
     646             : static GEN
     647         770 : a4a6_divpol(GEN a4, GEN a6, long n)
     648             : {
     649         770 :   switch(n)
     650             :   {
     651             :     case 2:
     652         497 :       return mkpoln(4, gen_1, gen_0, a4, a6);
     653             :     case 3:
     654         273 :       return mkpoln(5, utoi(3), gen_0, gmulgs(a4,6) , gmulgs(a6,12),
     655             :                        gneg(gsqr(a4)));
     656             :   }
     657           0 :   return NULL;
     658             : }
     659             : 
     660             : static GEN
     661         770 : ellisograph_Kohel_iso(GEN nf, GEN e, long n, GEN z, long flag)
     662             : {
     663             :   long i, r;
     664             :   GEN R, V;
     665         770 :   GEN c4 = gel(e,1), c6 = gel(e, 2);
     666         770 :   GEN a4 = gdivgs(c4, -48), a6 = gdivgs(c6, -864);
     667         770 :   GEN P = a4a6_divpol(a4, a6, n);
     668         770 :   R = nfroots(nf, z ? RgX_div_by_X_x(P, z, NULL): P);
     669         770 :   r = lg(R);
     670         770 :   V = cgetg(r, t_VEC);
     671        1232 :   for (i=1; i < r; i++)
     672         462 :     gel(V, i) = ellisog_by_roots(a4, a6, n, gel(R, i), flag);
     673         770 :   return mkvec2(V, R);
     674             : }
     675             : 
     676             : static GEN
     677         714 : ellisograph_Kohel_r(GEN nf, GEN e, long n, GEN z, long flag)
     678             : {
     679         714 :   GEN W = ellisograph_Kohel_iso(nf, e, n, z, flag);
     680         714 :   GEN iso = gel(W, 1), R = gel(W, 2);
     681         714 :   long i, r = lg(iso);
     682         714 :   GEN V = cgetg(r, t_VEC);
     683        1120 :   for (i=1; i < r; i++)
     684         406 :     gel(V, i) = ellisograph_Kohel_r(nf, gel(iso, i), n, gmulgs(gel(R, i), -n), flag);
     685         714 :   return mkvec2(e, V);
     686             : }
     687             : 
     688             : static GEN
     689         266 : corr(GEN c4, GEN c6)
     690             : {
     691         266 :   GEN c62 = gmul2n(c6, 1);
     692         266 :   return gadd(gdiv(gsqr(c4), c62), gdiv(c62, gmulgs(c4,3)));
     693             : }
     694             : 
     695             : static GEN
     696         266 : elkies98(GEN a4, GEN a6, long l, GEN s, GEN a4t, GEN a6t)
     697             : {
     698             :   GEN C, P, S;
     699             :   long i, n, d;
     700         266 :   d = l == 2 ? 1 : l>>1;
     701         266 :   C = cgetg(d+1, t_VEC);
     702         266 :   gel(C, 1) = gdivgs(gsub(a4, a4t), 5);
     703         266 :   if (d >= 2)
     704         266 :     gel(C, 2) = gdivgs(gsub(a6, a6t), 7);
     705         266 :   if (d >= 3)
     706         210 :     gel(C, 3) = gdivgs(gsub(gsqr(gel(C, 1)), gmul(a4, gel(C, 1))), 3);
     707        2758 :   for (n = 3; n < d; ++n)
     708             :   {
     709        2492 :     GEN s = gen_0;
     710       61222 :     for (i = 1; i < n; i++)
     711       58730 :       s = gadd(s, gmul(gel(C, i), gel(C, n-i)));
     712        2492 :     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));
     713             :   }
     714         266 :   P = cgetg(d+2, t_VEC);
     715         266 :   gel(P, 1 + 0) = stoi(d);
     716         266 :   gel(P, 1 + 1) = s;
     717         266 :   if (d >= 2)
     718         266 :     gel(P, 1 + 2) = gdivgs(gsub(gel(C, 1), gmulgs(gmulsg(2, a4), d)), 6);
     719        2968 :   for (n = 2; n < d; ++n)
     720        2702 :     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);
     721         266 :   S = cgetg(d+3, t_POL);
     722         266 :   S[1] = evalsigne(1) | evalvarn(0);
     723         266 :   gel(S, 2 + d - 0) = gen_1;
     724         266 :   gel(S, 2 + d - 1) = gneg(s);
     725        3234 :   for (n = 2; n <= d; ++n)
     726             :   {
     727        2968 :     GEN s = gen_0;
     728       67844 :     for (i = 1; i <= n; ++i)
     729             :     {
     730       64876 :       GEN p = gmul(gel(P, 1+i), gel(S, 2 + d - (n-i)));
     731       64876 :       s = gadd(s, p);
     732             :     }
     733        2968 :     gel(S, 2 + d - n) = gdivgs(s, -n);
     734             :   }
     735         266 :   return S;
     736             : }
     737             : 
     738             : static GEN
     739         252 : ellisog_by_jt(GEN c4, GEN c6, GEN jt, GEN jtp, GEN s0, long n, long flag)
     740             : {
     741         252 :   GEN jtp2 = gsqr(jtp), den = gmul(jt, gsubgs(jt, 1728));
     742         252 :   GEN c4t = gdiv(jtp2, den);
     743         252 :   GEN c6t = gdiv(gmul(jtp, c4t), jt);
     744         252 :   if (flag)
     745         119 :     return mkvec3(c4t, c6t, jt);
     746             :   else
     747             :   {
     748         133 :     GEN co  = corr(c4, c6);
     749         133 :     GEN cot = corr(c4t, c6t);
     750         133 :     GEN s = gmul2n(gmulgs(gadd(gadd(s0, co), gmulgs(cot,-n)), -n), -2);
     751         133 :     GEN a4  = gdivgs(c4, -48), a6 = gdivgs(c6, -864);
     752         133 :     GEN a4t = gmul(gdivgs(c4t, -48), powuu(n,4)), a6t = gmul(gdivgs(c6t, -864), powuu(n,6));
     753         133 :     GEN ker = elkies98(a4, a6, n, s, a4t, a6t);
     754         133 :     GEN st = gmulgs(s, -n);
     755         133 :     GEN a4tt = gmul(a4,powuu(n,4)), a6tt = gmul(a6,powuu(n,6));
     756         133 :     GEN kert = elkies98(a4t, a6t, n, st, a4tt, a6tt);
     757         133 :     return ellisog_by_Kohel(a4, a6, n, ker, kert, flag);
     758             :   }
     759             : }
     760             : 
     761             : /*
     762             : Based on
     763             : RENE SCHOOF
     764             : Counting points on elliptic curves over finite fields
     765             : Journal de Theorie des Nombres de Bordeaux,
     766             : tome 7, no 1 (1995), p. 219-254.
     767             : <http://www.numdam.org/item?id=JTNB_1995__7_1_219_0>
     768             : */
     769             : 
     770             : static GEN
     771          98 : ellisog_by_j(GEN e, GEN jt, long n, GEN P, long flag)
     772             : {
     773          98 :   pari_sp av = avma;
     774          98 :   GEN c4  = gel(e,1), c6 = gel(e, 2), j = gel(e, 3);
     775          98 :   GEN Px = deriv(P, 0), Py = deriv(P, 1);
     776          98 :   GEN Pxj = RgXY_eval(Px, j, jt), Pyj = RgXY_eval(Py, j, jt);
     777          98 :   GEN Pxx  = deriv(Px, 0), Pxy = deriv(Py, 0), Pyy = deriv(Py, 1);
     778          98 :   GEN Pxxj = RgXY_eval(Pxx,j,jt);
     779          98 :   GEN Pxyj = RgXY_eval(Pxy,j,jt);
     780          98 :   GEN Pyyj = RgXY_eval(Pyy,j,jt);
     781          98 :   GEN c6c4 = gdiv(c6, c4);
     782          98 :   GEN jp = gmul(j, c6c4);
     783          98 :   GEN jtp = gdivgs(gmul(jp, gdiv(Pxj, Pyj)), -n);
     784          98 :   GEN jtpn = gmulgs(jtp, n);
     785          98 :   GEN s0 = gdiv(gadd(gadd(gmul(gsqr(jp),Pxxj),gmul(gmul(jp,jtpn),gmul2n(Pxyj,1))),
     786             :                 gmul(gsqr(jtpn),Pyyj)),gmul(jp,Pxj));
     787          98 :   GEN et = ellisog_by_jt(c4, c6, jt, jtp, s0, n, flag);
     788          98 :   return gerepilecopy(av, et);
     789             : }
     790             : 
     791             : static GEN
     792         203 : ellisograph_iso(GEN nf, GEN e, ulong p, GEN P, GEN oj, long flag)
     793             : {
     794             :   long i, r;
     795             :   GEN Pj, R, V;
     796         203 :   GEN j = gel(e, 3);
     797         203 :   Pj = poleval(P, j);
     798         203 :   R = nfroots(nf,oj ? RgX_div_by_X_x(Pj, oj, NULL):Pj);
     799         203 :   r = lg(R);
     800         203 :   V = cgetg(r, t_VEC);
     801         301 :   for (i=1; i < r; i++)
     802          98 :     gel(V, i) = ellisog_by_j(e, gel(R, i), p, P, flag);
     803         203 :   return V;
     804             : }
     805             : 
     806             : static GEN
     807         161 : ellisograph_r(GEN nf, GEN e, ulong p, GEN P, GEN oj, long flag)
     808             : {
     809         161 :   GEN iso = ellisograph_iso(nf, e, p, P, oj, flag);
     810         161 :   GEN j = gel(e, 3);
     811         161 :   long i, r = lg(iso);
     812         161 :   GEN V = cgetg(r, t_VEC);
     813         217 :   for (i=1; i < r; i++)
     814          56 :     gel(V, i) = ellisograph_r(nf, gel(iso, i), p, P, j, flag);
     815         161 :   return mkvec2(e, V);
     816             : }
     817             : 
     818             : static GEN
     819         567 : ellisograph_a4a6(GEN E, long flag)
     820             : {
     821         567 :   GEN c4 = ell_get_c4(E), c6 = ell_get_c6(E), j = ell_get_j(E);
     822         861 :   return flag ? mkvec3(c4, c6, j):
     823         294 :                 mkvec5(c4, c6, j, isogeny_a4a6(E), invisogeny_a4a6(E));
     824             : }
     825             : 
     826             : static GEN
     827         154 : ellisograph_dummy(GEN E, long n, GEN jt, GEN jtt, GEN s0, long flag)
     828             : {
     829         154 :   GEN c4 = ell_get_c4(E), c6 = ell_get_c6(E);
     830         154 :   GEN c6c4 = gdiv(c6, c4);
     831         154 :   GEN jtp = gmul(c6c4, gdivgs(gmul(jt, jtt), -n));
     832         154 :   GEN iso = ellisog_by_jt(c4, c6, jt, jtp, gmul(s0, c6c4), n, flag);
     833         154 :   GEN v = mkvec2(iso, cgetg(1, t_VEC));
     834         154 :   return mkvec2(ellisograph_a4a6(E, flag), mkvec(v));
     835             : }
     836             : 
     837             : static GEN
     838         413 : ellisograph_p(GEN nf, GEN E, ulong p, long flag)
     839             : {
     840         413 :   pari_sp av = avma;
     841         413 :   GEN iso, e = ellisograph_a4a6(E, flag);
     842         413 :   if (p > 3)
     843             :   {
     844         105 :     GEN P = polmodular_ZXX(p, 0, 0, 1);
     845         105 :     iso = ellisograph_r(nf, e, p, P, NULL, flag);
     846             :   }
     847             :   else
     848         308 :     iso = ellisograph_Kohel_r(nf, e, p, NULL, flag);
     849         413 :   return gerepilecopy(av, iso);
     850             : }
     851             : 
     852             : static long
     853        2597 : etree_nbnodes(GEN T)
     854             : {
     855        2597 :   GEN F = gel(T,2);
     856        2597 :   long n = 1, i, l = lg(F);
     857        4123 :   for (i = 1; i < l; i++)
     858        1526 :     n += etree_nbnodes(gel(F, i));
     859        2597 :   return n;
     860             : }
     861             : 
     862             : static long
     863         861 : etree_listr(GEN T, GEN V, long n, GEN u, GEN ut)
     864             : {
     865         861 :   GEN E = gel(T, 1), F = gel(T,2);
     866         861 :   long i, l = lg(F);
     867         861 :   GEN iso, isot = NULL;
     868         861 :   if (lg(E) == 6)
     869             :   {
     870         441 :     iso  = ellisogenyapply(gel(E,4), u);
     871         441 :     isot = ellisogenyapply(ut, gel(E,5));
     872         441 :     gel(V, n) = mkvec5(gel(E,1), gel(E,2), gel(E,3), iso, isot);
     873             :   } else
     874             :   {
     875         420 :     gel(V, n) = mkvec3(gel(E,1), gel(E,2), gel(E,3));
     876         420 :     iso = u;
     877             :   }
     878        1393 :   for (i = 1; i < l; i++)
     879         532 :     n = etree_listr(gel(F, i), V, n + 1, iso, isot);
     880         861 :   return n;
     881             : }
     882             : 
     883             : static GEN
     884         329 : etree_list(GEN T)
     885             : {
     886         329 :   long n = etree_nbnodes(T);
     887         329 :   GEN V = cgetg(n+1, t_VEC);
     888         329 :   (void) etree_listr(T, V, 1, trivial_isogeny(), trivial_isogeny());
     889         329 :   return V;
     890             : }
     891             : 
     892             : static long
     893         861 : etree_distmatr(GEN T, GEN M, long n)
     894             : {
     895         861 :   GEN F = gel(T,2);
     896         861 :   long i, j, lF = lg(F), m = n + 1;
     897         861 :   GEN V = cgetg(lF, t_VECSMALL);
     898         861 :   mael(M, n, n) = 0;
     899        1393 :   for(i = 1; i < lF; i++)
     900         532 :     V[i] = m = etree_distmatr(gel(F,i), M, m);
     901        1393 :   for(i = 1; i < lF; i++)
     902             :   {
     903         532 :     long mi = i==1 ? n+1: V[i-1];
     904        1218 :     for(j = mi; j < V[i]; j++)
     905             :     {
     906         686 :       mael(M,n,j) = 1 + mael(M, mi, j);
     907         686 :       mael(M,j,n) = 1 + mael(M, j, mi);
     908             :     }
     909        1330 :     for(j = 1; j < lF; j++)
     910         798 :       if (i != j)
     911             :       {
     912         266 :         long i1, j1, mj = j==1 ? n+1: V[j-1];
     913         644 :         for (i1 = mi; i1 < V[i]; i1++)
     914         980 :           for(j1 = mj; j1 < V[j]; j1++)
     915         602 :             mael(M,i1,j1) = 2 + mael(M,mj,j1) + mael(M,i1,mi);
     916             :       }
     917             :   }
     918         861 :   return m;
     919             : }
     920             : 
     921             : static GEN
     922         329 : etree_distmat(GEN T)
     923             : {
     924         329 :   long i, n = etree_nbnodes(T);
     925         329 :   GEN M = cgetg(n+1, t_MAT);
     926        1190 :   for(i = 1; i <= n; i++)
     927         861 :     gel(M,i) = cgetg(n+1, t_VECSMALL);
     928         329 :   (void)etree_distmatr(T, M, 1);
     929         329 :   return M;
     930             : }
     931             : 
     932             : static GEN
     933         329 : list_to_crv(GEN L)
     934             : {
     935             :   long i, l;
     936         329 :   GEN V = cgetg_copy(L, &l);
     937        1372 :   for(i=1; i < l; i++)
     938             :   {
     939        1043 :     GEN Li = gel(L, i);
     940        1043 :     GEN e = mkvec2(gdivgs(gel(Li,1), -48), gdivgs(gel(Li,2), -864));
     941        1043 :     gel(V, i) = lg(Li)==6 ? mkvec3(e, gel(Li,4), gel(Li,5)): e;
     942             :   }
     943         329 :   return V;
     944             : }
     945             : 
     946             : static GEN
     947         329 : distmat_pow(GEN E, ulong p)
     948             : {
     949         329 :   long i, j, n = lg(E)-1;
     950         329 :   GEN M = cgetg(n+1, t_MAT);
     951        1190 :   for(i = 1; i <= n; i++)
     952             :   {
     953         861 :     gel(M,i) = cgetg(n+1, t_VEC);
     954        3696 :     for(j = 1; j <= n; j++)
     955        2835 :       gmael(M,i,j) = powuu(p,mael(E,i,j));
     956             :   }
     957         329 :   return M;
     958             : }
     959             : 
     960             : /* Assume there is a single p-isogeny */
     961             : 
     962             : static GEN
     963          84 : isomatdbl(GEN nf, GEN L, GEN M, ulong p, GEN T2, long flag)
     964             : {
     965          84 :   long i, j, n = lg(L) -1;
     966          84 :   GEN P = p > 3 ? polmodular_ZXX(p, 0, 0, 1): NULL;
     967          84 :   GEN V = cgetg(2*n+1, t_VEC);
     968          84 :   GEN N = cgetg(2*n+1, t_MAT);
     969         266 :   for(i=1; i <= n; i++)
     970         182 :     gel(V, i) = gel(L, i);
     971         266 :   for(i=1; i <= n; i++)
     972             :   {
     973         182 :     GEN e = gel(L, i);
     974             :     GEN F, E;
     975         182 :     if (i == 1)
     976          84 :       F = gmael(T2, 2, 1);
     977             :     else
     978             :     {
     979          98 :       if (p > 3)
     980          42 :         F = ellisograph_iso(nf, e, p, P, NULL, flag);
     981             :       else
     982          56 :         F = gel(ellisograph_Kohel_iso(nf, e, p, NULL, flag), 1);
     983          98 :       if (lg(F) != 2) pari_err_BUG("isomatdbl");
     984             :     }
     985         182 :     E = gel(F, 1);
     986         182 :     if (flag)
     987          91 :       gel(V, i+n) = mkvec3(gel(E,1), gel(E,2), gel(E,3));
     988             :     else
     989             :     {
     990          91 :       GEN iso = ellisogenyapply(gel(E,4), gel(e, 4));
     991          91 :       GEN isot = ellisogenyapply(gel(e,5), gel(E, 5));
     992          91 :       gel(V, i+n) = mkvec5(gel(E,1), gel(E,2), gel(E,3), iso, isot);
     993             :     }
     994             :   }
     995         448 :   for(i=1; i <= 2*n; i++)
     996         364 :     gel(N, i) = cgetg(2*n+1, t_COL);
     997         266 :   for(i=1; i <= n; i++)
     998         588 :     for(j=1; j <= n; j++)
     999             :     {
    1000         406 :       gcoeff(N,i,j) = gcoeff(N,i+n,j+n) = gcoeff(M,i,j);
    1001         406 :       gcoeff(N,i,j+n) = gcoeff(N,i+n,j) = muliu(gcoeff(M,i,j), p);
    1002             :     }
    1003          84 :   return mkvec2(list_to_crv(V), N);
    1004             : }
    1005             : 
    1006             : INLINE GEN
    1007         336 : mkfracss(long x, long y) { retmkfrac(stoi(x),stoi(y)); }
    1008             : 
    1009             : static ulong
    1010         329 : ellQ_exceptional_iso(GEN j, GEN *jt, GEN *jtp, GEN *s0)
    1011             : {
    1012         329 :   *jt = j; *jtp = gen_1;
    1013         329 :   if (typ(j)==t_INT)
    1014             :   {
    1015         238 :     long js = itos_or_0(j);
    1016             :     GEN j37;
    1017         238 :     if (js==-32768) { *s0 = mkfracss(-1156,539); return 11; }
    1018         224 :     if (js==-121)
    1019          14 :       { *jt = stoi(-24729001) ; *jtp = mkfracss(4973,5633);
    1020          14 :         *s0 = mkfracss(-1961682050,1204555087); return 11;}
    1021         210 :     if (js==-24729001)
    1022          14 :       { *jt = stoi(-121); *jtp = mkfracss(5633,4973);
    1023          14 :         *s0 = mkfracss(-1961682050,1063421347); return 11;}
    1024         196 :     if (js==-884736)
    1025          14 :       { *s0 = mkfracss(-1100,513); return 19; }
    1026         182 :     j37 = negi(uu32toi(37876312,1780746325));
    1027         182 :     if (js==-9317)
    1028             :     {
    1029          14 :       *jt = j37;
    1030          14 :       *jtp = mkfracss(1984136099,496260169);
    1031          14 :       *s0 = mkfrac(negi(uu32toi(457100760,4180820796UL)),
    1032             :                         uu32toi(89049913, 4077411069UL));
    1033          14 :       return 37;
    1034             :     }
    1035         168 :     if (equalii(j, j37))
    1036             :     {
    1037          14 :       *jt = stoi(-9317);
    1038          14 :       *jtp = mkfrac(utoi(496260169),utoi(1984136099UL));
    1039          14 :       *s0 = mkfrac(negi(uu32toi(41554614,2722784052UL)),
    1040             :                         uu32toi(32367030,2614994557UL));
    1041          14 :       return 37;
    1042             :     }
    1043         154 :     if (js==-884736000)
    1044          14 :     { *s0 = mkfracss(-1073708,512001); return 43; }
    1045         140 :     if (equalii(j, negi(powuu(5280,3))))
    1046          14 :     { *s0 = mkfracss(-176993228,85184001); return 67; }
    1047         126 :     if (equalii(j, negi(powuu(640320,3))))
    1048          14 :     { *s0 = mkfrac(negi(uu32toi(72512,1969695276)), uu32toi(35374,1199927297));
    1049          14 :       return 163; }
    1050             :   } else
    1051             :   {
    1052          91 :     GEN j1 = mkfracss(-297756989,2);
    1053          91 :     GEN j2 = mkfracss(-882216989,131072);
    1054          91 :     if (gequal(j, j1))
    1055             :     {
    1056          14 :       *jt = j2; *jtp = mkfracss(1503991,2878441);
    1057          14 :       *s0 = mkfrac(negi(uu32toi(121934,548114672)),uu32toi(77014,117338383));
    1058          14 :       return 17;
    1059             :     }
    1060          77 :     if (gequal(j, j2))
    1061             :     {
    1062          14 :       *jt = j1; *jtp = mkfracss(2878441,1503991);
    1063          14 :       *s0 = mkfrac(negi(uu32toi(121934,548114672)),uu32toi(40239,4202639633UL));
    1064          14 :       return 17;
    1065             :     }
    1066             :   }
    1067         175 :   return 0;
    1068             : }
    1069             : 
    1070             : static GEN
    1071         245 : mkisomat(ulong p, GEN T)
    1072             : {
    1073         245 :   pari_sp av = avma;
    1074         245 :   GEN L = list_to_crv(etree_list(T));
    1075         245 :   GEN M = distmat_pow(etree_distmat(T), p);
    1076         245 :   return gerepilecopy(av, mkvec2(L, M));
    1077             : }
    1078             : 
    1079             : static GEN
    1080          84 : mkisomatdbl(ulong p, GEN T, ulong p2, GEN T2, long flag)
    1081             : {
    1082          84 :   GEN L = etree_list(T);
    1083          84 :   GEN M = distmat_pow(etree_distmat(T), p);
    1084          84 :   return isomatdbl(NULL, L, M, p2, T2, flag);
    1085             : }
    1086             : 
    1087             : /*
    1088             : See
    1089             : M.A Kenku
    1090             : On the number of Q-isomorphism classes of elliptic curves in each Q-isogeny class
    1091             : Journal of Number Theory
    1092             : Volume 15, Issue 2, October 1982, Pages 199-202
    1093             : http://www.sciencedirect.com/science/article/pii/0022314X82900257
    1094             : */
    1095             : 
    1096             : static GEN
    1097         329 : ellQ_isomat(GEN E, long flag)
    1098             : {
    1099         329 :   GEN K = NULL;
    1100             :   GEN T2, T3, T5, T7, T13;
    1101             :   long n2, n3, n5, n7, n13;
    1102             :   GEN jt, jtp, s0;
    1103         329 :   GEN c4 = ell_get_c4(E), c6 = ell_get_c6(E), j = ell_get_j(E);
    1104         329 :   long l = ellQ_exceptional_iso(j, &jt, &jtp, &s0);
    1105         329 :   if (l)
    1106             :   {
    1107             : #if 1
    1108         154 :       return mkisomat(l, ellisograph_dummy(E, l, jt, jtp, s0, flag));
    1109             : #else
    1110             :       return mkisomat(l, ellisograph_p(K, E, l), flag);
    1111             : #endif
    1112             :   }
    1113         175 :   T2 = ellisograph_p(K, E, 2, flag);
    1114         175 :   n2 = etree_nbnodes(T2);
    1115         175 :   if (n2>4 || gequalgs(j, 1728) || gequalgs(j, 287496))
    1116          42 :     return mkisomat(2, T2);
    1117         133 :   T3 = ellisograph_p(K, E, 3, flag);
    1118         133 :   n3 = etree_nbnodes(T3);
    1119         133 :   if (n3>1 && n2==2) return mkisomatdbl(3,T3,2,T2, flag);
    1120          91 :   if (n3==2 && n2>1)  return mkisomatdbl(2,T2,3,T3, flag);
    1121          91 :   if (n3>2 || gequal0(j)) return mkisomat(3, T3);
    1122          63 :   T5 = ellisograph_p(K, E, 5, flag);
    1123          63 :   n5 = etree_nbnodes(T5);
    1124          63 :   if (n5>1 && n2>1) return mkisomatdbl(2,T2,5,T5, flag);
    1125          63 :   if (n5>1 && n3>1) return mkisomatdbl(3,T3,5,T5, flag);
    1126          49 :   if (n5>1) return mkisomat(5, T5);
    1127          42 :   T7 = ellisograph_p(K, E, 7, flag);
    1128          42 :   n7 = etree_nbnodes(T7);
    1129          42 :   if (n7>1 && n2>1) return mkisomatdbl(2,T2,7,T7, flag);
    1130          14 :   if (n7>1 && n3>1) return mkisomatdbl(3,T3,7,T7, flag);
    1131          14 :   if (n7>1) return mkisomat(7,T7);
    1132          14 :   if (n2>1) return mkisomat(2,T2);
    1133           0 :   if (n3>1) return mkisomat(3,T3);
    1134           0 :   T13 = ellisograph_p(K, E, 13, flag);
    1135           0 :   n13 = etree_nbnodes(T13);
    1136           0 :   if (n13>1) return mkisomat(13,T13);
    1137           0 :   if (flag)
    1138           0 :     retmkvec2(list_to_crv(mkvec(mkvec3(c4, c6, j))), matid(1));
    1139             :   else
    1140           0 :     retmkvec2(list_to_crv(mkvec(mkvec5(c4, c6, j, isogeny_a4a6(E), invisogeny_a4a6(E)))), matid(1));
    1141             : }
    1142             : 
    1143             : GEN
    1144         329 : ellisomat(GEN E, long flag)
    1145             : {
    1146         329 :   pari_sp av = avma;
    1147             :   GEN LM;
    1148         329 :   checkell_Q(E);
    1149         329 :   if (flag < 0 || flag > 1) pari_err_FLAG("ellisomat");
    1150         329 :   LM = ellQ_isomat(E, flag);
    1151         329 :   return gerepilecopy(av, LM);
    1152             : }

Generated by: LCOV version 1.11