Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - FlxqE.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23332-367b47754) Lines: 840 867 96.9 %
Date: 2018-12-10 05:41:52 Functions: 82 83 98.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2012  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             : /* Not so fast arithmetic with points over elliptic curves over Fq,
      18             : small characteristic. */
      19             : 
      20             : /***********************************************************************/
      21             : /**                                                                   **/
      22             : /**                              FlxqE                                **/
      23             : /**                                                                   **/
      24             : /***********************************************************************/
      25             : 
      26             : /* Theses functions deal with point over elliptic curves over Fq defined
      27             :  * by an equation of the form y^2=x^3+a4*x+a6.
      28             :  * Most of the time a6 is omitted since it can be recovered from any point
      29             :  * on the curve.
      30             :  */
      31             : 
      32             : GEN
      33       63938 : RgE_to_FlxqE(GEN x, GEN T, ulong p)
      34             : {
      35       63938 :   if (ell_is_inf(x)) return x;
      36       63938 :   retmkvec2(Rg_to_Flxq(gel(x,1),T,p),Rg_to_Flxq(gel(x,2),T,p));
      37             : }
      38             : 
      39             : GEN
      40      154181 : FlxqE_changepoint(GEN x, GEN ch, GEN T, ulong p)
      41             : {
      42      154181 :   pari_sp av = avma;
      43             :   GEN p1,z,u,r,s,t,v,v2,v3;
      44      154181 :   if (ell_is_inf(x)) return x;
      45       91993 :   u = gel(ch,1); r = gel(ch,2);
      46       91993 :   s = gel(ch,3); t = gel(ch,4);
      47       91993 :   v = Flxq_inv(u, T, p); v2 = Flxq_sqr(v, T, p); v3 = Flxq_mul(v,v2, T, p);
      48       91993 :   p1 = Flx_sub(gel(x,1),r, p);
      49       91993 :   z = cgetg(3,t_VEC);
      50       91993 :   gel(z,1) = Flxq_mul(v2, p1, T, p);
      51       91993 :   gel(z,2) = Flxq_mul(v3, Flx_sub(gel(x,2), Flx_add(Flxq_mul(s, p1, T, p),t, p), p), T, p);
      52       91993 :   return gerepileupto(av, z);
      53             : }
      54             : 
      55             : GEN
      56       63938 : FlxqE_changepointinv(GEN x, GEN ch, GEN T, ulong p)
      57             : {
      58             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
      59       63938 :   if (ell_is_inf(x)) return x;
      60       63938 :   X = gel(x,1); Y = gel(x,2);
      61       63938 :   u = gel(ch,1); r = gel(ch,2);
      62       63938 :   s = gel(ch,3); t = gel(ch,4);
      63       63938 :   u2 = Flxq_sqr(u, T, p); u3 = Flxq_mul(u,u2, T, p);
      64       63938 :   u2X = Flxq_mul(u2,X, T, p);
      65       63938 :   z = cgetg(3, t_VEC);
      66       63938 :   gel(z,1) = Flx_add(u2X,r, p);
      67       63938 :   gel(z,2) = Flx_add(Flxq_mul(u3,Y, T, p), Flx_add(Flxq_mul(s,u2X, T, p), t, p), p);
      68       63938 :   return z;
      69             : }
      70             : 
      71             : static GEN
      72       22834 : nonsquare_Flxq(GEN T, ulong p)
      73             : {
      74       22834 :   pari_sp av = avma;
      75       22834 :   long n = degpol(T), vs = T[1];
      76             :   GEN a;
      77       22834 :   if (odd(n))
      78        7686 :     return mkvecsmall2(vs, nonsquare_Fl(p));
      79             :   do
      80             :   {
      81       30016 :     set_avma(av);
      82       30016 :     a = random_Flx(n, vs, p);
      83       30016 :   } while (Flxq_issquare(a, T, p));
      84       15148 :   return a;
      85             : }
      86             : 
      87             : void
      88       22834 : Flxq_elltwist(GEN a, GEN a6, GEN T, ulong p, GEN *pt_a, GEN *pt_a6)
      89             : {
      90       22834 :   GEN d = nonsquare_Flxq(T, p);
      91       22834 :   GEN d2 = Flxq_sqr(d, T, p), d3 = Flxq_mul(d2, d, T, p);
      92       22834 :   if (typ(a)==t_VECSMALL)
      93             :   {
      94       15232 :     *pt_a  = Flxq_mul(a,  d2, T, p);
      95       15232 :     *pt_a6 = Flxq_mul(a6, d3, T, p);
      96             :   } else
      97             :   {
      98        7602 :     *pt_a  = mkvec(Flxq_mul(gel(a,1), d, T, p));
      99        7602 :     *pt_a6 = Flxq_mul(a6, d3, T, p);
     100             :   }
     101       22834 : }
     102             : 
     103             : static GEN
     104     1304180 : FlxqE_dbl_slope(GEN P, GEN a4, GEN T, ulong p, GEN *slope)
     105             : {
     106             :   GEN x, y, Q;
     107     1304180 :   if (ell_is_inf(P) || !lgpol(gel(P,2))) return ellinf();
     108     1203177 :   x = gel(P,1); y = gel(P,2);
     109     1203177 :   if (p==3UL)
     110     1583379 :     *slope = typ(a4)==t_VEC ? Flxq_div(Flxq_mul(x, gel(a4, 1), T, p), y, T, p)
     111     1049398 :                             : Flxq_div(a4, Flx_neg(y, p), T, p);
     112             :   else
     113             :   {
     114      669196 :     GEN sx = Flx_add(Flx_triple(Flxq_sqr(x, T, p), p), a4, p);
     115      669196 :     *slope = Flxq_div(sx, Flx_double(y, p), T, p);
     116             :   }
     117     1203177 :   Q = cgetg(3,t_VEC);
     118     1203177 :   gel(Q, 1) = Flx_sub(Flxq_sqr(*slope, T, p), Flx_double(x, p), p);
     119     1203177 :   if (typ(a4)==t_VEC) gel(Q, 1) = Flx_sub(gel(Q, 1), gel(a4, 1), p);
     120     1203177 :   gel(Q, 2) = Flx_sub(Flxq_mul(*slope, Flx_sub(x, gel(Q, 1), p), T, p), y, p);
     121     1203177 :   return Q;
     122             : }
     123             : 
     124             : GEN
     125     1278513 : FlxqE_dbl(GEN P, GEN a4, GEN T, ulong p)
     126             : {
     127     1278513 :   pari_sp av = avma;
     128             :   GEN slope;
     129     1278513 :   return gerepileupto(av, FlxqE_dbl_slope(P,a4, T, p,&slope));
     130             : }
     131             : 
     132             : static GEN
     133      537635 : FlxqE_add_slope(GEN P, GEN Q, GEN a4, GEN T, ulong p, GEN *slope)
     134             : {
     135             :   GEN Px, Py, Qx, Qy, R;
     136      537635 :   if (ell_is_inf(P)) return Q;
     137      533716 :   if (ell_is_inf(Q)) return P;
     138      533604 :   Px = gel(P,1); Py = gel(P,2);
     139      533604 :   Qx = gel(Q,1); Qy = gel(Q,2);
     140      533604 :   if (Flx_equal(Px, Qx))
     141             :   {
     142       47211 :     if (Flx_equal(Py, Qy))
     143        1237 :       return FlxqE_dbl_slope(P, a4, T, p, slope);
     144             :     else
     145       45974 :       return ellinf();
     146             :   }
     147      486393 :   *slope = Flxq_div(Flx_sub(Py, Qy, p), Flx_sub(Px, Qx, p), T, p);
     148      486393 :   R = cgetg(3,t_VEC);
     149      486393 :   gel(R, 1) = Flx_sub(Flx_sub(Flxq_sqr(*slope, T, p), Px, p), Qx, p);
     150      486393 :   if (typ(a4)==t_VEC) gel(R, 1) = Flx_sub(gel(R, 1),gel(a4, 1), p);
     151      486393 :   gel(R, 2) = Flx_sub(Flxq_mul(*slope, Flx_sub(Px, gel(R, 1), p), T, p), Py, p);
     152      486393 :   return R;
     153             : }
     154             : 
     155             : GEN
     156      534182 : FlxqE_add(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     157             : {
     158      534182 :   pari_sp av = avma;
     159             :   GEN slope;
     160      534182 :   return gerepileupto(av, FlxqE_add_slope(P,Q,a4, T, p,&slope));
     161             : }
     162             : 
     163             : static GEN
     164        1092 : FlxqE_neg_i(GEN P, ulong p)
     165             : {
     166        1092 :   if (ell_is_inf(P)) return P;
     167        1092 :   return mkvec2(gel(P,1), Flx_neg(gel(P,2), p));
     168             : }
     169             : 
     170             : GEN
     171         490 : FlxqE_neg(GEN P, GEN T, ulong p)
     172             : {
     173             :   (void) T;
     174         490 :   if (ell_is_inf(P)) return ellinf();
     175         490 :   return mkvec2(gcopy(gel(P,1)), Flx_neg(gel(P,2), p));
     176             : }
     177             : 
     178             : GEN
     179        1092 : FlxqE_sub(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     180             : {
     181        1092 :   pari_sp av = avma;
     182             :   GEN slope;
     183        1092 :   return gerepileupto(av, FlxqE_add_slope(P, FlxqE_neg_i(Q, p), a4, T, p, &slope));
     184             : }
     185             : 
     186             : struct _FlxqE
     187             : {
     188             :   GEN a4, a6;
     189             :   GEN T;
     190             :   ulong p;
     191             : };
     192             : 
     193             : static GEN
     194     1278513 : _FlxqE_dbl(void *E, GEN P)
     195             : {
     196     1278513 :   struct _FlxqE *ell = (struct _FlxqE *) E;
     197     1278513 :   return FlxqE_dbl(P, ell->a4, ell->T, ell->p);
     198             : }
     199             : 
     200             : static GEN
     201      528029 : _FlxqE_add(void *E, GEN P, GEN Q)
     202             : {
     203      528029 :   struct _FlxqE *ell=(struct _FlxqE *) E;
     204      528029 :   return FlxqE_add(P, Q, ell->a4, ell->T, ell->p);
     205             : }
     206             : 
     207             : static GEN
     208      218278 : _FlxqE_mul(void *E, GEN P, GEN n)
     209             : {
     210      218278 :   pari_sp av = avma;
     211      218278 :   struct _FlxqE *e=(struct _FlxqE *) E;
     212      218278 :   long s = signe(n);
     213      218278 :   if (!s || ell_is_inf(P)) return ellinf();
     214      218097 :   if (s<0) P = FlxqE_neg(P, e->T, e->p);
     215      218097 :   if (is_pm1(n)) return s>0? gcopy(P): P;
     216      211957 :   return gerepileupto(av, gen_pow(P, n, e, &_FlxqE_dbl, &_FlxqE_add));
     217             : }
     218             : 
     219             : GEN
     220       64468 : FlxqE_mul(GEN P, GEN n, GEN a4, GEN T, ulong p)
     221             : {
     222             :   struct _FlxqE E;
     223       64468 :   E.a4= a4; E.T = T; E.p = p;
     224       64468 :   return _FlxqE_mul(&E, P, n);
     225             : }
     226             : 
     227             : /* 3*x^2+2*a2*x = -a2*x, and a2!=0 */
     228             : 
     229             : /* Finds a random non-singular point on E */
     230             : static GEN
     231       77252 : random_F3xqE(GEN a2, GEN a6, GEN T)
     232             : {
     233       77252 :   pari_sp ltop = avma;
     234             :   GEN x, y, rhs;
     235       77252 :   const ulong p = 3;
     236             :   do
     237             :   {
     238      153839 :     set_avma(ltop);
     239      153839 :     x   = random_Flx(get_Flx_degree(T),get_Flx_var(T),p);
     240      153839 :     rhs = Flx_add(Flxq_mul(Flxq_sqr(x, T, p), Flx_add(x, a2, p), T, p), a6, p);
     241      153839 :   } while ((!lgpol(rhs) && !lgpol(x)) || !Flxq_issquare(rhs, T, p));
     242       77252 :   y = Flxq_sqrt(rhs, T, p);
     243       77252 :   if (!y) pari_err_PRIME("random_F3xqE", T);
     244       77252 :   return gerepilecopy(ltop, mkvec2(x, y));
     245             : }
     246             : 
     247             : /* Finds a random non-singular point on E */
     248             : GEN
     249      144276 : random_FlxqE(GEN a4, GEN a6, GEN T, ulong p)
     250             : {
     251      144276 :   pari_sp ltop = avma;
     252             :   GEN x, x2, y, rhs;
     253      144276 :   if (typ(a4)==t_VEC)
     254       77252 :     return random_F3xqE(gel(a4,1), a6, T);
     255             :   do
     256             :   {
     257      132225 :     set_avma(ltop);
     258      132225 :     x   = random_Flx(get_Flx_degree(T),get_Flx_var(T),p);
     259      132225 :     x2  = Flxq_sqr(x, T, p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
     260      132225 :     rhs = Flx_add(Flxq_mul(x, Flx_add(x2, a4, p), T, p), a6, p);
     261      134199 :   } while ((!lgpol(rhs) && !lgpol(Flx_add(Flx_triple(x2, p), a4, p)))
     262      264408 :           || !Flxq_issquare(rhs, T, p));
     263       67024 :   y = Flxq_sqrt(rhs, T, p);
     264       67024 :   if (!y) pari_err_PRIME("random_FlxqE", T);
     265       67024 :   return gerepilecopy(ltop, mkvec2(x, y));
     266             : }
     267             : 
     268             : static GEN
     269       69152 : _FlxqE_rand(void *E)
     270             : {
     271       69152 :   struct _FlxqE *ell=(struct _FlxqE *) E;
     272       69152 :   return random_FlxqE(ell->a4, ell->a6, ell->T, ell->p);
     273             : }
     274             : 
     275             : static const struct bb_group FlxqE_group={_FlxqE_add,_FlxqE_mul,_FlxqE_rand,hash_GEN,zvV_equal,ell_is_inf, NULL};
     276             : 
     277             : const struct bb_group *
     278          34 : get_FlxqE_group(void ** pt_E, GEN a4, GEN a6, GEN T, ulong p)
     279             : {
     280          34 :   struct _FlxqE *e = (struct _FlxqE *) stack_malloc(sizeof(struct _FlxqE));
     281          34 :   e->a4 = a4; e->a6 = a6; e->T = Flx_get_red(T, p); e->p = p;
     282          34 :   *pt_E = (void *) e;
     283          34 :   return &FlxqE_group;
     284             : }
     285             : 
     286             : GEN
     287        2329 : FlxqE_order(GEN z, GEN o, GEN a4, GEN T, ulong p)
     288             : {
     289        2329 :   pari_sp av = avma;
     290             :   struct _FlxqE e;
     291        2329 :   e.a4=a4; e.T=T; e.p=p;
     292        2329 :   return gerepileuptoint(av, gen_order(z, o, (void*)&e, &FlxqE_group));
     293             : }
     294             : 
     295             : GEN
     296          49 : FlxqE_log(GEN a, GEN b, GEN o, GEN a4, GEN T, ulong p)
     297             : {
     298          49 :   pari_sp av = avma;
     299             :   struct _FlxqE e;
     300          49 :   e.a4=a4; e.T=T; e.p=p;
     301          49 :   return gerepileuptoint(av, gen_PH_log(a, b, o, (void*)&e, &FlxqE_group));
     302             : }
     303             : 
     304             : /***********************************************************************/
     305             : /**                                                                   **/
     306             : /**                            Pairings                               **/
     307             : /**                                                                   **/
     308             : /***********************************************************************/
     309             : 
     310             : /* Derived from APIP from and by Jerome Milan, 2012 */
     311             : 
     312             : static GEN
     313       69750 : FlxqE_vert(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     314             : {
     315       69750 :   long vT = get_Flx_var(T);
     316             :   GEN df;
     317       69750 :   if (ell_is_inf(P))
     318       23435 :     return pol1_Flx(vT);
     319       46315 :   if (!Flx_equal(gel(Q, 1), gel(P, 1)))
     320       41082 :     return Flx_sub(gel(Q, 1), gel(P, 1), p);
     321        5233 :   if (lgpol(gel(P,2))!=0) return pol1_Flx(vT);
     322       11420 :   df = typ(a4)==t_VEC ? Flxq_mul(gel(P,1), Flx_mulu(gel(a4, 1), 2, p), T, p)
     323        6970 :                       : a4;
     324        4450 :   return Flxq_inv(Flx_add(Flx_mulu(Flxq_sqr(gel(P,1), T, p), 3, p),
     325             :                           df, p), T, p);
     326             : }
     327             : 
     328             : static GEN
     329       26791 : FlxqE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN T, ulong p)
     330             : {
     331       26791 :   long vT = get_Flx_var(T);
     332       26791 :   GEN x = gel(Q, 1), y = gel(Q, 2);
     333       26791 :   GEN tmp1 = Flx_sub(x, gel(R, 1), p);
     334       26791 :   GEN tmp2 = Flx_add(Flxq_mul(tmp1, slope, T, p), gel(R, 2), p);
     335       26791 :   if (!Flx_equal(y, tmp2))
     336       24866 :     return Flx_sub(y, tmp2, p);
     337        1925 :   if (lgpol(y) == 0)
     338         657 :     return pol1_Flx(vT);
     339             :   else
     340             :   {
     341        1268 :     GEN s1, s2, a2 = typ(a4)==t_VEC ? gel(a4,1): NULL;
     342        1268 :     GEN y2i = Flxq_inv(Flx_mulu(y, 2, p), T, p);
     343        1268 :     GEN df = a2 ? Flxq_mul(x, Flx_mulu(a2, 2, p), T, p): a4;
     344             :     GEN x3, ddf;
     345        1268 :     s1 = Flxq_mul(Flx_add(Flx_mulu(Flxq_sqr(x, T, p), 3, p), df, p), y2i, T, p);
     346        1268 :     if (!Flx_equal(s1, slope))
     347         343 :       return Flx_sub(s1, slope, p);
     348         925 :     x3 = Flx_mulu(x, 3, p);
     349         925 :     ddf = a2 ? Flx_add(x3, a2, p): x3;
     350         925 :     s2 = Flxq_mul(Flx_sub(ddf, Flxq_sqr(s1, T, p), p), y2i, T, p);
     351         925 :     return lgpol(s2)!=0 ? s2: y2i;
     352             :   }
     353             : }
     354             : 
     355             : /* Computes the equation of the line tangent to R and returns its
     356             :    evaluation at the point Q. Also doubles the point R.
     357             :  */
     358             : 
     359             : static GEN
     360       46451 : FlxqE_tangent_update(GEN R, GEN Q, GEN a4, GEN T, ulong p, GEN *pt_R)
     361             : {
     362       46451 :   if (ell_is_inf(R))
     363             :   {
     364        3979 :     *pt_R = ellinf();
     365        3979 :     return pol1_Flx(get_Flx_var(T));
     366             :   }
     367       42472 :   else if (!lgpol(gel(R,2)))
     368             :   {
     369       18042 :     *pt_R = ellinf();
     370       18042 :     return FlxqE_vert(R, Q, a4, T, p);
     371             :   } else {
     372             :     GEN slope;
     373       24430 :     *pt_R = FlxqE_dbl_slope(R, a4, T, p, &slope);
     374       24430 :     return FlxqE_Miller_line(R, Q, slope, a4, T, p);
     375             :   }
     376             : }
     377             : 
     378             : /* Computes the equation of the line through R and P, and returns its
     379             :    evaluation at the point Q. Also adds P to the point R.
     380             :  */
     381             : 
     382             : static GEN
     383        3816 : FlxqE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN T, ulong p, GEN *pt_R)
     384             : {
     385        3816 :   if (ell_is_inf(R))
     386             :   {
     387          34 :     *pt_R = gcopy(P);
     388          34 :     return FlxqE_vert(P, Q, a4, T, p);
     389             :   }
     390        3782 :   else if (ell_is_inf(P))
     391             :   {
     392           0 :     *pt_R = gcopy(R);
     393           0 :     return FlxqE_vert(R, Q, a4, T, p);
     394             :   }
     395        3782 :   else if (Flx_equal(gel(P, 1), gel(R, 1)))
     396             :   {
     397        1421 :     if (Flx_equal(gel(P, 2), gel(R, 2)))
     398           7 :       return FlxqE_tangent_update(R, Q, a4, T, p, pt_R);
     399             :     else
     400             :     {
     401        1414 :       *pt_R = ellinf();
     402        1414 :       return FlxqE_vert(R, Q, a4, T, p);
     403             :     }
     404             :   } else {
     405             :     GEN slope;
     406        2361 :     *pt_R = FlxqE_add_slope(P, R, a4, T, p, &slope);
     407        2361 :     return FlxqE_Miller_line(R, Q, slope, a4, T, p);
     408             :   }
     409             : }
     410             : 
     411             : /* Returns the Miller function f_{m, Q} evaluated at the point P using
     412             :    the standard Miller algorithm.
     413             :  */
     414             : 
     415             : struct _FlxqE_miller
     416             : {
     417             :   ulong p;
     418             :   GEN T, a4, P;
     419             : };
     420             : 
     421             : static GEN
     422       46444 : FlxqE_Miller_dbl(void* E, GEN d)
     423             : {
     424       46444 :   struct _FlxqE_miller *m = (struct _FlxqE_miller *)E;
     425       46444 :   ulong p  = m->p;
     426       46444 :   GEN T = m->T, a4 = m->a4, P = m->P;
     427             :   GEN v, line;
     428       46444 :   GEN num = Flxq_sqr(gel(d,1), T, p);
     429       46444 :   GEN denom = Flxq_sqr(gel(d,2), T, p);
     430       46444 :   GEN point = gel(d,3);
     431       46444 :   line = FlxqE_tangent_update(point, P, a4, T, p, &point);
     432       46444 :   num  = Flxq_mul(num, line, T, p);
     433       46444 :   v = FlxqE_vert(point, P, a4, T, p);
     434       46444 :   denom = Flxq_mul(denom, v, T, p);
     435       46444 :   return mkvec3(num, denom, point);
     436             : }
     437             : 
     438             : static GEN
     439        3816 : FlxqE_Miller_add(void* E, GEN va, GEN vb)
     440             : {
     441        3816 :   struct _FlxqE_miller *m = (struct _FlxqE_miller *)E;
     442        3816 :   ulong p = m->p;
     443        3816 :   GEN T = m->T, a4 = m->a4, P = m->P;
     444             :   GEN v, line, point;
     445        3816 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
     446        3816 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
     447        3816 :   GEN num   = Flxq_mul(na, nb, T, p);
     448        3816 :   GEN denom = Flxq_mul(da, db, T, p);
     449        3816 :   line = FlxqE_chord_update(pa, pb, P, a4, T, p, &point);
     450        3816 :   num  = Flxq_mul(num, line, T, p);
     451        3816 :   v = FlxqE_vert(point, P, a4, T, p);
     452        3816 :   denom = Flxq_mul(denom, v, T, p);
     453        3816 :   return mkvec3(num, denom, point);
     454             : }
     455             : 
     456             : static GEN
     457       19422 : FlxqE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN T, ulong p)
     458             : {
     459       19422 :   pari_sp ltop = avma;
     460             :   struct _FlxqE_miller d;
     461             :   GEN v, num, denom, g1;
     462             : 
     463       19422 :   d.a4 = a4; d.T = T; d.p = p; d.P = P;
     464       19422 :   g1 = pol1_Flx(get_Flx_var(T));
     465       19422 :   v = gen_pow(mkvec3(g1,g1,Q), m, (void*)&d, FlxqE_Miller_dbl, FlxqE_Miller_add);
     466       19422 :   num = gel(v,1); denom = gel(v,2);
     467       19422 :   return gerepileupto(ltop, Flxq_div(num, denom, T, p));
     468             : }
     469             : 
     470             : GEN
     471       12742 : FlxqE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, ulong p)
     472             : {
     473       12742 :   pari_sp ltop = avma;
     474             :   GEN num, denom, result;
     475       12742 :   if (ell_is_inf(P) || ell_is_inf(Q) || Flx_equal(P,Q))
     476        3059 :     return pol1_Flx(get_Flx_var(T));
     477        9683 :   num    = FlxqE_Miller(P, Q, m, a4, T, p);
     478        9683 :   denom  = FlxqE_Miller(Q, P, m, a4, T, p);
     479        9683 :   result = Flxq_div(num, denom, T, p);
     480        9683 :   if (mpodd(m))
     481         644 :     result  = Flx_neg(result, p);
     482        9683 :   return gerepileupto(ltop, result);
     483             : }
     484             : 
     485             : GEN
     486          56 : FlxqE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, ulong p)
     487             : {
     488          56 :   if (ell_is_inf(P) || ell_is_inf(Q))
     489           0 :     return pol1_Flx(get_Flx_var(T));
     490          56 :   return FlxqE_Miller(P, Q, m, a4, T, p);
     491             : }
     492             : 
     493             : static GEN
     494       12728 : _FlxqE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
     495             : {
     496       12728 :   struct _FlxqE *e = (struct _FlxqE *) E;
     497       12728 :   return  Flxq_order(FlxqE_weilpairing(P,Q,m,e->a4,e->T,e->p), F, e->T, e->p);
     498             : }
     499             : 
     500             : GEN
     501       15609 : Flxq_ellgroup(GEN a4, GEN a6, GEN N, GEN T, ulong p, GEN *pt_m)
     502             : {
     503             :   struct _FlxqE e;
     504       15609 :   GEN q = powuu(p, get_Flx_degree(T));
     505       15609 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
     506       15609 :   return gen_ellgroup(N, subiu(q,1), pt_m, (void*)&e, &FlxqE_group, _FlxqE_pairorder);
     507             : }
     508             : 
     509             : GEN
     510       14363 : Flxq_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN T, ulong p)
     511             : {
     512             :   GEN P;
     513       14363 :   pari_sp av = avma;
     514             :   struct _FlxqE e;
     515       14363 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
     516       14363 :   switch(lg(D)-1)
     517             :   {
     518             :   case 0:
     519          63 :     return cgetg(1,t_VEC);
     520             :   case 1:
     521       11787 :     P = gen_gener(gel(D,1), (void*)&e, &FlxqE_group);
     522       11787 :     P = mkvec(FlxqE_changepoint(P, ch, T, p));
     523       11787 :     break;
     524             :   default:
     525        2513 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FlxqE_group, _FlxqE_pairorder);
     526        2513 :     gel(P,1) = FlxqE_changepoint(gel(P,1), ch, T, p);
     527        2513 :     gel(P,2) = FlxqE_changepoint(gel(P,2), ch, T, p);
     528        2513 :     break;
     529             :   }
     530       14300 :   return gerepilecopy(av, P);
     531             : }
     532             : /***********************************************************************/
     533             : /**                                                                   **/
     534             : /**                          Point counting                           **/
     535             : /**                                                                   **/
     536             : /***********************************************************************/
     537             : 
     538             : /* assume a and n  are coprime */
     539             : static GEN
     540       76258 : RgX_circular_shallow(GEN P, long a, long n)
     541             : {
     542       76258 :   long i, l = lgpol(P);
     543       76258 :   GEN Q = cgetg(2+n,t_POL);
     544       76258 :   Q[1] = P[1];
     545      512421 :   for(i=0; i<l; i++)
     546      436163 :     gel(Q,2+(i*a)%n) = gel(P,2+i);
     547      168707 :   for(   ; i<n; i++)
     548       92449 :     gel(Q,2+(i*a)%n) = gen_0;
     549       76258 :   return normalizepol_lg(Q,2+n);
     550             : }
     551             : 
     552             : static GEN
     553       76258 : ZpXQ_frob_cyc(GEN x, GEN T, GEN q, ulong p)
     554             : {
     555       76258 :   long n = get_FpX_degree(T);
     556       76258 :   return FpX_rem(RgX_circular_shallow(x,p,n+1), T, q);
     557             : }
     558             : 
     559             : static GEN
     560      113547 : ZpXQ_frob(GEN x, GEN Xm, GEN T, GEN q, ulong p)
     561             : {
     562      113547 :   if (lg(Xm)==1)
     563       43435 :     return ZpXQ_frob_cyc(x, T, q, p);
     564             :   else
     565             :   {
     566       70112 :     long n = get_FpX_degree(T);
     567       70112 :     GEN V = RgX_blocks(RgX_inflate(x, p), n, p);
     568       70112 :     GEN W = ZXV_dotproduct(V, Xm);
     569       70112 :     return FpX_rem(W, T, q);
     570             :   }
     571             : }
     572             : 
     573             : struct _lift_lin
     574             : {
     575             :   ulong p;
     576             :   GEN sqx, Tp;
     577             :   GEN ai, Xm;
     578             : };
     579             : 
     580       84035 : static GEN _lift_invl(void *E, GEN x)
     581             : {
     582       84035 :   struct _lift_lin *d = (struct _lift_lin *) E;
     583       84035 :   GEN T = d->Tp;
     584       84035 :   ulong p = d->p;
     585       84035 :   GEN xai = Flxq_mul(ZX_to_Flx(x, p), d->ai, T, p);
     586       84035 :   return Flx_to_ZX(Flxq_lroot_fast(xai, d->sqx, T, p));
     587             : }
     588             : 
     589       23744 : static GEN _lift_lin(void *E, GEN F, GEN x2, GEN q)
     590             : {
     591       23744 :   struct _lift_lin *d = (struct _lift_lin *) E;
     592       23744 :   pari_sp av = avma;
     593       23744 :   GEN T = gel(F,3), Xm = gel(F,4);
     594       23744 :   GEN y2  = ZpXQ_frob(x2, Xm, T, q, d->p);
     595       23744 :   GEN lin = FpX_add(ZX_mul(gel(F,1), y2), ZX_mul(gel(F,2), x2), q);
     596       23744 :   return gerepileupto(av, FpX_rem(lin, T, q));
     597             : }
     598             : 
     599             : static GEN
     600      180873 : FpM_FpXV_bilinear(GEN P, GEN X, GEN Y, GEN p)
     601             : {
     602      180873 :    pari_sp av = avma;
     603      180873 :    GEN s =  ZX_mul(FpXV_FpC_mul(X,gel(P,1),p),gel(Y,1));
     604      180873 :    long i, l = lg(P);
     605      849765 :    for(i=2; i<l; i++)
     606      668892 :      s = ZX_add(s, ZX_mul(FpXV_FpC_mul(X,gel(P,i),p),gel(Y,i)));
     607      180873 :    return gerepileupto(av, FpX_red(s, p));
     608             : }
     609             : 
     610             : static GEN
     611      180873 : FpM_FpXQV_bilinear(GEN P, GEN X, GEN Y, GEN T, GEN p)
     612             : {
     613      180873 :   return FpX_rem(FpM_FpXV_bilinear(P,X,Y,p),T,p);
     614             : }
     615             : 
     616             : static GEN
     617      120582 : FpXC_powderiv(GEN M, GEN p)
     618             : {
     619             :   long i, l;
     620      120582 :   long v = varn(gel(M,2));
     621      120582 :   GEN m = cgetg_copy(M, &l);
     622      120582 :   gel(m,1) = pol_0(v);
     623      120582 :   gel(m,2) = pol_1(v);
     624      445928 :   for(i=2; i<l-1; i++)
     625      325346 :     gel(m,i+1) = FpX_Fp_mul(gel(M,i),utoi(i), p);
     626      120582 :   return m;
     627             : }
     628             : 
     629             : struct _lift_iso
     630             : {
     631             :   GEN phi;
     632             :   GEN Xm,T;
     633             :   GEN sqx, Tp;
     634             :   ulong p;
     635             : };
     636             : 
     637             : static GEN
     638       60291 : _lift_iter(void *E, GEN x2, GEN q)
     639             : {
     640       60291 :   struct _lift_iso *d = (struct _lift_iso *) E;
     641       60291 :   ulong p = d->p;
     642       60291 :   long n = lg(d->phi)-2;
     643       60291 :   GEN TN = FpXT_red(d->T, q), XN = FpXV_red(d->Xm, q);
     644       60291 :   GEN y2 = ZpXQ_frob(x2, XN, TN, q, p);
     645       60291 :   GEN xp = FpXQ_powers(x2, n, TN, q);
     646       60291 :   GEN yp = FpXQ_powers(y2, n, TN, q);
     647       60291 :   GEN V  = FpM_FpXQV_bilinear(d->phi,xp,yp,TN,q);
     648       60291 :   return mkvec3(V,xp,yp);
     649             : }
     650             : 
     651             : static GEN
     652       60291 : _lift_invd(void *E, GEN V, GEN v, GEN qM, long M)
     653             : {
     654       60291 :   struct _lift_iso *d = (struct _lift_iso *) E;
     655             :   struct _lift_lin e;
     656       60291 :   ulong p = d->p;
     657       60291 :   GEN TM = FpXT_red(d->T, qM), XM = FpXV_red(d->Xm, qM);
     658       60291 :   GEN xp = FpXV_red(gel(v,2), qM);
     659       60291 :   GEN yp = FpXV_red(gel(v,3), qM);
     660       60291 :   GEN Dx = FpM_FpXQV_bilinear(d->phi, FpXC_powderiv(xp, qM), yp, TM, qM);
     661       60291 :   GEN Dy = FpM_FpXQV_bilinear(d->phi, xp, FpXC_powderiv(yp, qM), TM, qM);
     662       60291 :   GEN F = mkvec4(Dy, Dx, TM, XM);
     663       60291 :   e.ai = Flxq_inv(ZX_to_Flx(Dy,p),d->Tp,p);
     664       60291 :   e.sqx = d->sqx; e.Tp = d->Tp; e.p=p; e.Xm = XM;
     665       60291 :   return gen_ZpX_Dixon(F,V,qM,utoi(p),M,(void*) &e, _lift_lin, _lift_invl);
     666             : }
     667             : 
     668             : static GEN
     669       25032 : lift_isogeny(GEN phi, GEN x0, long n, GEN Xm, GEN T, GEN sqx, GEN Tp, ulong p)
     670             : {
     671             :   struct _lift_iso d;
     672       25032 :   d.phi=phi;
     673       25032 :   d.Xm=Xm; d.T=T;
     674       25032 :   d.sqx=sqx; d.Tp=Tp; d.p=p;
     675       25032 :   return gen_ZpX_Newton(x0, utoi(p), n,(void*)&d, _lift_iter, _lift_invd);
     676             : }
     677             : 
     678             : static GEN
     679       25011 : getc2(GEN act, GEN X, GEN T, GEN q, ulong p, long N)
     680             : {
     681       25011 :   GEN A1 = RgV_to_RgX(gel(act,1),0), A2 =  RgV_to_RgX(gel(act,2),0);
     682       25011 :   long n = brent_kung_optpow(maxss(degpol(A1),degpol(A2)),2,1);
     683       25011 :   GEN xp = FpXQ_powers(X,n,T,q);
     684       25011 :   GEN P  = FpX_FpXQV_eval(A1, xp, T, q);
     685       25011 :   GEN Q  = FpX_FpXQV_eval(A2, xp, T, q);
     686       25011 :   return ZpXQ_div(P, Q, T, q, utoi(p), N);
     687             : }
     688             : 
     689             : struct _ZpXQ_norm
     690             : {
     691             :   long n;
     692             :   GEN T, p;
     693             : };
     694             : 
     695             : static GEN
     696       32823 : ZpXQ_norm_mul(void *E, GEN x, GEN y)
     697             : {
     698       32823 :   struct _ZpXQ_norm *D = (struct _ZpXQ_norm*)E;
     699       32823 :   GEN P = gel(x,1), Q = gel(y,1);
     700       32823 :   long a = mael(x,2,1), b = mael(y,2,1);
     701       32823 :   retmkvec2(FpXQ_mul(P,ZpXQ_frob_cyc(Q, D->T, D->p, a), D->T, D->p),
     702             :             mkvecsmall((a*b)%D->n));
     703             : }
     704             : 
     705             : static GEN
     706       22715 : ZpXQ_norm_sqr(void *E, GEN x)
     707             : {
     708       22715 :   return ZpXQ_norm_mul(E, x, x);
     709             : }
     710             : 
     711             : /* Assume T = Phi_(n) and n prime */
     712             : GEN
     713       11340 : ZpXQ_norm_pcyc(GEN x, GEN T, GEN q, GEN p)
     714             : {
     715             :   GEN z;
     716             :   struct _ZpXQ_norm D;
     717       11340 :   long d = get_FpX_degree(T);
     718       11340 :   D.T = T; D.p = q; D.n = d+1;
     719       11340 :   if (d==1) return ZX_copy(x);
     720       11340 :   z = mkvec2(x,mkvecsmall(p[2]));
     721       11340 :   z = gen_powu(z,d,(void*)&D,ZpXQ_norm_sqr,ZpXQ_norm_mul);
     722       11340 :   return gmael(z,1,2);
     723             : }
     724             : 
     725             : /* Assume T = Phi_(n) and n prime */
     726             : static GEN
     727       11102 : ZpXQ_sqrtnorm_pcyc(GEN x, GEN T, GEN q, GEN p, long e)
     728             : {
     729       11102 :   GEN z = ZpXQ_norm_pcyc(x, T, q, p);
     730       11102 :   return Zp_sqrtlift(z,Fp_sqrt(z,p),p,e);
     731             : }
     732             : 
     733             : /* Assume a = 1 [p], return the square root of the norm */
     734             : static GEN
     735       13930 : ZpXQ_sqrtnorm(GEN a, GEN T, GEN q, GEN p, long e)
     736             : {
     737       13930 :   GEN s = Fp_div(FpXQ_trace(ZpXQ_log(a, T, p, e), T, q), gen_2, q);
     738       13930 :   return modii(gel(Qp_exp(cvtop(s, p, e-1)),4), q);
     739             : }
     740             : 
     741             : struct _teich_lin
     742             : {
     743             :   ulong p;
     744             :   GEN sqx, Tp;
     745             :   long m;
     746             : };
     747             : 
     748             : static GEN
     749       29470 : _teich_invl(void *E, GEN x)
     750             : {
     751       29470 :   struct _teich_lin *d = (struct _teich_lin *) E;
     752       29470 :   ulong p = d->p;
     753       29470 :   GEN T = d->Tp;
     754       29470 :   return Flx_to_ZX(Flxq_lroot_fast(ZX_to_Flx(x, p), d->sqx, T, p));
     755             : }
     756             : 
     757             : static GEN
     758        8953 : _teich_lin(void *E, GEN F, GEN x2, GEN q)
     759             : {
     760        8953 :   struct _teich_lin *d = (struct _teich_lin *) E;
     761        8953 :   pari_sp av = avma;
     762        8953 :   GEN T = gel(F,2), Xm = gel(F,3);
     763        8953 :   GEN y2  = ZpXQ_frob(x2, Xm, T, q, d->p);
     764        8953 :   GEN lin = FpX_sub(y2, ZX_mulu(ZX_mul(gel(F,1), x2), d->p), q);
     765        8953 :   return gerepileupto(av, FpX_rem(lin, T, q));
     766             : }
     767             : 
     768             : struct _teich_iso
     769             : {
     770             :   GEN Xm, T;
     771             :   GEN sqx, Tp;
     772             :   ulong p;
     773             : };
     774             : 
     775             : static GEN
     776       20517 : _teich_iter(void *E, GEN x2, GEN q)
     777             : {
     778       20517 :   struct _teich_iso *d = (struct _teich_iso *) E;
     779       20517 :   ulong p = d->p;
     780       20517 :   GEN TN = FpXT_red(d->T, q), XN = FpXV_red(d->Xm, q);
     781       20517 :   GEN y2 = ZpXQ_frob(x2, XN, TN, q, d->p);
     782       20517 :   GEN x1 = FpXQ_powu(x2, p-1, TN, q);
     783       20517 :   GEN xp = FpXQ_mul(x2, x1, TN, q);
     784       20517 :   GEN V = FpX_sub(y2,xp,q);
     785       20517 :   return mkvec2(V,x1);
     786             : }
     787             : 
     788             : static GEN
     789       20517 : _teich_invd(void *E, GEN V, GEN v, GEN qM, long M)
     790             : {
     791       20517 :   struct _teich_iso *d = (struct _teich_iso *) E;
     792             :   struct _teich_lin e;
     793       20517 :   ulong p = d->p;
     794       20517 :   GEN TM = FpXT_red(d->T, qM), XM = FpXV_red(d->Xm, qM);
     795       20517 :   GEN x1 = FpX_red(gel(v,2), qM);
     796       20517 :   GEN F = mkvec3(x1, TM, XM);
     797       20517 :   e.sqx = d->sqx; e.Tp = d->Tp; e.p=p;
     798       20517 :   return gen_ZpX_Dixon(F,V,qM,utoi(p),M,(void*) &e, _teich_lin, _teich_invl);
     799             : }
     800             : 
     801             : static GEN
     802       10213 : Teichmuller_lift(GEN x, GEN Xm, GEN T, GEN sqx, GEN Tp, ulong p, long N)
     803             : {
     804             :   struct _teich_iso d;
     805       10213 :   d.Xm = Xm; d.T = T; d.sqx = sqx; d.Tp = Tp; d.p = p;
     806       10213 :   return gen_ZpX_Newton(x,utoi(p), N,(void*)&d, _teich_iter, _teich_invd);
     807             : }
     808             : 
     809             : static GEN
     810       25032 : get_norm(GEN a4, GEN a6, GEN T, ulong p, long N)
     811             : {
     812       25032 :   long sv=T[1];
     813             :   GEN a;
     814       25032 :   if (p==3) a = gel(a4,1);
     815             :   else
     816             :   {
     817       10227 :     GEN P = mkpoln(4, pol1_Flx(sv), pol0_Flx(sv), a4, a6);
     818       10227 :     a = gel(FlxqX_powu(P,p>>1,T,p),2+p-1);
     819             :   }
     820       25032 :   return Zp_sqrtnlift(gen_1,subss(p,1),utoi(Flxq_norm(a,T,p)),utoi(p), N);
     821             : }
     822             : 
     823             : static GEN
     824       25011 : fill_pols(long n, const long *v, long m, const long *vn,
     825             :           const long *vd, GEN *act)
     826             : {
     827             :   long i, j;
     828       25011 :   long d = upowuu(n,12/(n-1));
     829       25011 :   GEN N, D, M = zeromatcopy(n+1,n+1);
     830       25011 :   gmael(M,1,n+1) = gen_1;
     831      120568 :   for(i=2;i<=n+1;i++)
     832      338373 :     for(j=i-1;j<=n;j++)
     833      242816 :       gmael(M,i,j) = mulis(powuu(d,i-2),v[j-i+1]);
     834       25011 :   N = cgetg(m+1,t_COL);
     835       25011 :   D = cgetg(m+1,t_COL);
     836      135359 :   for(i=1;i<=m;i++)
     837             :   {
     838      110348 :     gel(N,i) = stoi(*vn++);
     839      110348 :     gel(D,i) = stoi(*vd++);
     840             :   }
     841       25011 :   *act = mkmat2(N,D);
     842       25011 :   return M;
     843             : }
     844             : 
     845             : /*
     846             :   These polynomials were extracted from the ECHIDNA databases
     847             :   available at <http://echidna.maths.usyd.edu.au/echidna/>
     848             :   and computed by David R. Kohel.
     849             :   Return the matrix of the modular polynomial, set act to the parametrization,
     850             :   and set dj to the opposite of the supersingular j-invariant.
     851             : */
     852             : static GEN
     853       25011 : get_Kohel_polynomials(ulong p, GEN *act, long *dj)
     854             : {
     855       25011 :   const long mat3[] = {-1,-36,-270};
     856       25011 :   const long num3[] = {1,-483,-21141,-59049};
     857       25011 :   const long den3[] = {1,261, 4347, -6561};
     858       25011 :   const long mat5[] = {-1,-30,-315,-1300,-1575};
     859       25011 :   const long num5[] = {-1,490,20620,158750,78125};
     860       25011 :   const long den5[] = {-1,-254,-4124,-12250,3125};
     861       25011 :   const long mat7[] = {-1,-28,-322,-1904,-5915,-8624,-4018};
     862       25011 :   const long num7[] = {1,-485,-24058,-343833,-2021642,-4353013,-823543};
     863       25011 :   const long den7[] = {1,259,5894,49119,168406,166355,-16807};
     864       25011 :   const long mat13[]= {-1,-26,-325,-2548,-13832,-54340,-157118,-333580,-509366,
     865             :                        -534820,-354536,-124852,-15145};
     866       25011 :   const long num13[]= {1,-487,-24056,-391463,-3396483,-18047328,-61622301,
     867             :                        -133245853,-168395656,-95422301,-4826809};
     868       25011 :   const long den13[]= {1,257,5896,60649,364629,1388256,3396483,5089019,4065464,
     869             :                        1069939,-28561};
     870       25011 :   switch(p)
     871             :   {
     872             :   case 3:
     873       14805 :     *dj = 0;
     874       14805 :     return fill_pols(3,mat3,4,num3,den3,act);
     875             :   case 5:
     876       10171 :     *dj = 0;
     877       10171 :     return fill_pols(5,mat5,5,num5,den5,act);
     878             :   case 7:
     879          28 :     *dj = 1;
     880          28 :     return fill_pols(7,mat7,7,num7,den7,act);
     881             :   case 13:
     882           7 :     *dj = 8;
     883           7 :     return fill_pols(13,mat13,11,num13,den13,act);
     884             :   }
     885             :   *dj=0; *act = NULL; return NULL; /* LCOV_EXCL_LINE */
     886             : }
     887             : 
     888             : long
     889       32271 : zx_is_pcyc(GEN T)
     890             : {
     891       32271 :   long i, n = degpol(T);
     892       32271 :   if (!uisprime(n+1))
     893       11635 :     return 0;
     894       99148 :   for (i=0; i<=n; i++)
     895       87808 :     if (T[i+2]!=1UL)
     896        9296 :       return 0;
     897       11340 :   return 1;
     898             : }
     899             : 
     900             : static GEN
     901       25011 : Flxq_ellcard_Kohel(GEN a4, GEN a6, GEN T, ulong p)
     902             : {
     903       25011 :   pari_sp av = avma, av2;
     904             :   pari_timer ti;
     905       25011 :   long n = get_Flx_degree(T), N = (n+4)/2, dj;
     906       25011 :   GEN q = powuu(p, N);
     907             :   GEN T2, Xm, s1, c2, t, lr;
     908             :   GEN S1, sqx;
     909             :   GEN Nc2, Np;
     910       25011 :   GEN act, phi = get_Kohel_polynomials(p, &act, &dj);
     911       25011 :   long ispcyc = zx_is_pcyc(get_Flx_mod(T));
     912       25011 :   timer_start(&ti);
     913       25011 :   if (!ispcyc)
     914             :   {
     915       13916 :     T2 = Flx_Teichmuller(get_Flx_mod(T),p,N);
     916       13916 :     if (DEBUGLEVEL) timer_printf(&ti,"Teich");
     917             :   } else
     918       11095 :     T2 = Flx_to_ZX(get_Flx_mod(T));
     919       25011 :   T2 = FpX_get_red(T2, q); T = ZXT_to_FlxT(T2, p);
     920       25011 :   av2 = avma;
     921       25011 :   if (DEBUGLEVEL) timer_printf(&ti,"Barrett");
     922       25011 :   if (!ispcyc)
     923             :   {
     924       13916 :     Xm = FpXQ_powers(pol_xn(n,get_FpX_var(T2)),p-1,T2,q);
     925       13916 :     if (DEBUGLEVEL) timer_printf(&ti,"Xm");
     926             :   } else
     927       11095 :     Xm = cgetg(1,t_VEC);
     928       25011 :   s1 = Flxq_inv(Flx_Fl_add(Flxq_ellj(a4,a6,T,p),dj, p),T,p);
     929       25011 :   lr = Flxq_lroot(polx_Flx(get_Flx_var(T)), T, p);
     930       25011 :   sqx = Flxq_powers(lr, p-1, T, p);
     931       25011 :   S1 = lift_isogeny(phi, Flx_to_ZX(s1), N, Xm, T2, sqx, T ,p);
     932       25011 :   if (DEBUGLEVEL) timer_printf(&ti,"Lift isogeny");
     933       25011 :   c2 = getc2(act, S1, T2, q, p, N);
     934       25011 :   if (DEBUGLEVEL) timer_printf(&ti,"c^2");
     935       25011 :   if (p>3 && !ispcyc)
     936             :   {
     937       10199 :     GEN c2p = Flx_to_ZX(Flxq_inv(ZX_to_Flx(c2,p),T,p));
     938       10199 :     GEN tc2 = Teichmuller_lift(c2p,Xm, T2,sqx,T,p,N);
     939       10199 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fq");
     940       10199 :     c2 = FpX_rem(FpX_mul(tc2,c2,q),T2,q);
     941             :   }
     942       25011 :   c2 = gerepileupto(av2, c2);
     943       25011 :   if (DEBUGLEVEL) timer_printf(&ti,"tc2");
     944       25011 :   Nc2 = (ispcyc? ZpXQ_sqrtnorm_pcyc: ZpXQ_sqrtnorm)(c2, T2, q, utoi(p), N);
     945       25011 :   if (DEBUGLEVEL) timer_printf(&ti,"Norm");
     946       25011 :   Np = get_norm(a4,a6,T,p,N);
     947       25011 :   if (p>3 && ispcyc)
     948             :   {
     949           7 :     GEN Ncpi =  utoi(Fl_inv(umodiu(Nc2,p), p));
     950           7 :     GEN tNc2 = Zp_sqrtnlift(gen_1, subss(p,1), Ncpi, utoi(p),N);
     951           7 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fp");
     952           7 :     Nc2 = Fp_mul(Nc2,tNc2,q);
     953             :   }
     954       25011 :   t = Fp_center_i(Fp_mul(Nc2,Np,q),q,shifti(q,-1));
     955       25011 :   return gerepileupto(av, subii(addiu(powuu(p,n),1),t));
     956             : }
     957             : 
     958             : static void
     959          21 : liftcurve(GEN J, GEN T, GEN q, ulong p, long N, GEN *A4, GEN *A6)
     960             : {
     961          21 :   pari_sp av = avma;
     962          21 :   GEN r = ZpXQ_inv(Z_ZX_sub(utoi(1728),J),T,utoi(p),N);
     963          21 :   GEN g = FpXQ_mul(J,r,T,q);
     964          21 :   *A4 = FpX_mulu(g,3,q);
     965          21 :   *A6 = FpX_mulu(g,2,q);
     966          21 :   gerepileall(av,2,A4,A6);
     967          21 : }
     968             : 
     969             : static GEN
     970          21 : getc5(GEN H, GEN A40, GEN A60, GEN A41, GEN A61, GEN T, GEN q, ulong p, long N)
     971             : {
     972          21 :   long d = lg(H)-1;
     973          21 :   GEN s1 = gel(H,d-1), s2 = gel(H,d-2), s3 = d<5 ? pol_0(varn(T)): gel(H,d-3);
     974          21 :   GEN s12 = FpXQ_sqr(s1,T,q);
     975          21 :   GEN h2 = ZX_sub(ZX_shifti(s2,1),s12); /*2*s2-s1^2*/
     976          21 :   GEN h3 = ZX_sub(FpXQ_mul(ZX_add(h2,s2),s1,T,q),ZX_mulu(s3,3));
     977             :                                         /*3*s2*s1-s1^3-3s3*/
     978          21 :   GEN alpha= ZX_sub(ZX_mulu(h2,30), ZX_mulu(A40,5*p-6)); /* 30*h2+A40*(6-5*p)*/
     979          21 :   GEN beta = ZX_sub(ZX_sub(ZX_mulu(FpXQ_mul(A40,s1,T,q),42),ZX_mulu(A60,14*p-15)),
     980             :                     ZX_mulu(h3,70)); /* 42*A40*s1-A60*(14*p-15)-70*h3 */
     981          21 :   GEN u2 = FpXQ_mul(FpXQ_mul(A41,beta,T,q),
     982             :                     ZpXQ_inv(FpXQ_mul(A61,alpha,T,q),T,utoi(p),N),T,q);
     983          21 :   return u2;
     984             : }
     985             : 
     986             : static GEN
     987          21 : ZpXQX_liftrootmod_vald(GEN f, GEN H, long v, GEN T, GEN p, long e)
     988             : {
     989          21 :   pari_sp av = avma, av2;
     990          21 :   GEN pv = p, q, qv, W, df, Tq, fr, dfr;
     991             :   ulong mask;
     992             :   pari_timer ti;
     993          21 :   if (e <= v+1) return H;
     994          21 :   df = RgX_deriv(f);
     995          21 :   if (v) { pv = powiu(p,v); qv = mulii(pv,p); df = ZXX_Z_divexact(df, pv); }
     996           0 :   else qv = p;
     997          21 :   mask = quadratic_prec_mask(e-v);
     998          21 :   Tq = FpXT_red(T, qv); dfr = FpXQX_red(df, Tq, p);
     999          21 :   if (DEBUGLEVEL) timer_start(&ti);
    1000          21 :   W = FpXQXQ_inv(FpXQX_rem(dfr, H, Tq, p), H, Tq, p); /* 1/f'(a) mod (T,p) */
    1001          21 :   if (DEBUGLEVEL) timer_printf(&ti,"FpXQXQ_inv");
    1002          21 :   q = p; av2 = avma;
    1003             :   for (;;)
    1004          56 :   {
    1005             :     GEN u, fa, qv, q2v, Tq2, fadH;
    1006          77 :     GEN H2 = H, q2 = q;
    1007          77 :     q = sqri(q);
    1008          77 :     if (mask & 1) q = diviiexact(q,p);
    1009          77 :     mask >>= 1;
    1010          77 :     if (v) { qv = mulii(q, pv); q2v = mulii(q2, pv); }
    1011           0 :     else { qv = q; q2v = q2; }
    1012          77 :     Tq2 = FpXT_red(T, q2v); Tq = FpXT_red(T, qv);
    1013          77 :     fr = FpXQX_red(f, Tq, qv);
    1014          77 :     fa = FpXQX_rem(fr, H, Tq, qv);
    1015          77 :     fa = ZXX_Z_divexact(fa, q2v);
    1016          77 :     fadH = FpXQXQ_mul(RgX_deriv(H),fa,H,Tq2,q2);
    1017          77 :     H = FpXX_add(H, gmul(FpXQXQ_mul(W, fadH, H, Tq2, q2v), q2), qv);
    1018          77 :     if (mask == 1) return gerepileupto(av, H);
    1019          56 :     dfr = FpXQX_rem(FpXQX_red(df, Tq, q),H,Tq,q);
    1020          56 :     u = ZXX_Z_divexact(ZXX_Z_add_shallow(FpXQXQ_mul(W,dfr,H,Tq,q),gen_m1),q2);
    1021          56 :     W = gsub(W,gmul(FpXQXQ_mul(u,W,H2,Tq2,q2),q2));
    1022          56 :     if (gc_needed(av2,2))
    1023             :     {
    1024           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQX_liftroot, e = %ld", e);
    1025           0 :       gerepileall(av2, 3, &H, &W, &q);
    1026             :     }
    1027             :   }
    1028             : }
    1029             : 
    1030             : static GEN
    1031          21 : get_H1(GEN A41, GEN A61, GEN T2, ulong p)
    1032             : {
    1033          21 :   GEN q = utoi(p), T = FpXT_red(T2,q);
    1034          21 :   GEN pol = FpXQ_elldivpol(FpX_red(A41,q),FpX_red(A61,q),p,T,q);
    1035          21 :   return FpXQX_normalize(RgX_deflate(pol,p),T,q);
    1036             : }
    1037             : 
    1038             : static GEN
    1039          21 : Flxq_ellcard_Harley(GEN a4, GEN a6, GEN T, ulong p)
    1040             : {
    1041          21 :   pari_sp av = avma, av2;
    1042             :   pari_timer ti;
    1043          21 :   long n = get_Flx_degree(T), N = (n+5)/2;
    1044          21 :   GEN q = powuu(p, N);
    1045             :   GEN T2, j, t;
    1046             :   GEN J1,A40,A41,A60,A61, sqx,Xm;
    1047             :   GEN pol, h1, H;
    1048             :   GEN c2, tc2, c2p, Nc2, Np;
    1049          21 :   long ispcyc = zx_is_pcyc(get_Flx_mod(T));
    1050          21 :   timer_start(&ti);
    1051          21 :   if (!ispcyc)
    1052             :   {
    1053          14 :     T2 = Flx_Teichmuller(get_Flx_mod(T),p,N);
    1054          14 :     if (DEBUGLEVEL) timer_printf(&ti,"Teich");
    1055             :   } else
    1056           7 :     T2 = Flx_to_ZX(get_Flx_mod(T));
    1057          21 :   T2 = FpX_get_red(T2, q); T = ZXT_to_FlxT(T2, p);
    1058          21 :   av2 = avma;
    1059          21 :   if (DEBUGLEVEL) timer_printf(&ti,"Barrett");
    1060          21 :   if (!ispcyc)
    1061             :   {
    1062          14 :     Xm = FpXQ_powers(pol_xn(n,get_FpX_var(T2)),p-1,T2,q);
    1063          14 :     if (DEBUGLEVEL) timer_printf(&ti,"Xm");
    1064             :   } else
    1065           7 :     Xm = cgetg(1,t_VEC);
    1066          21 :   if (DEBUGLEVEL) timer_printf(&ti,"Xm");
    1067          21 :   j = Flxq_ellj(a4,a6,T,p);
    1068          21 :   sqx = Flxq_powers(Flxq_lroot(polx_Flx(T[1]), T, p), p-1, T, p);
    1069          21 :   J1 = lift_isogeny(polmodular_ZM(p, 0), Flx_to_ZX(j), N, Xm, T2,sqx,T,p);
    1070          21 :   if (DEBUGLEVEL) timer_printf(&ti,"Lift isogeny");
    1071          21 :   liftcurve(J1,T2,q,p,N,&A41,&A61);
    1072          21 :   A40 = ZpXQ_frob(A41, Xm, T2, q, p);
    1073          21 :   A60 = ZpXQ_frob(A61, Xm, T2, q, p);
    1074          21 :   if (DEBUGLEVEL) timer_printf(&ti,"liftcurve");
    1075          21 :   pol = FpXQ_elldivpol(A40,A60,p,T2,q);
    1076          21 :   if (DEBUGLEVEL) timer_printf(&ti,"p-division");
    1077          21 :   h1 = get_H1(A41,A61,T2,p);
    1078          21 :   H = ZpXQX_liftrootmod_vald(pol,h1,1,T2,utoi(p),N);
    1079          21 :   q = diviuexact(q,p); N--;
    1080          21 :   if (DEBUGLEVEL) timer_printf(&ti,"kernel");
    1081          21 :   c2 = getc5(H,A40,A60,A41,A61,T2,q,p,N);
    1082          21 :   if (DEBUGLEVEL) timer_printf(&ti,"c^2");
    1083          21 :   if (!ispcyc)
    1084             :   {
    1085          14 :     c2p = Flx_to_ZX(Flxq_inv(ZX_to_Flx(c2,p),T,p));
    1086          14 :     tc2 = Teichmuller_lift(c2p,Xm, T2,sqx,T,p,N);
    1087          14 :     if (DEBUGLEVEL) timer_printf(&ti,"teichmuller");
    1088          14 :     c2 = FpX_rem(FpX_mul(tc2,c2,q),T2,q);
    1089             :   }
    1090          21 :   c2 = gerepileupto(av2, c2);
    1091          21 :   q = powuu(p, N);
    1092          21 :   Nc2 = (ispcyc? ZpXQ_sqrtnorm_pcyc: ZpXQ_sqrtnorm)(c2, T2, q, utoi(p), N);
    1093          21 :   if (DEBUGLEVEL) timer_printf(&ti,"Norm");
    1094          21 :   Np = get_norm(a4,a6,T,p,N);
    1095          21 :   if (ispcyc)
    1096             :   {
    1097           7 :     GEN Ncpi = utoi(Fl_inv(umodiu(Nc2,p), p));
    1098           7 :     GEN tNc2 = Zp_sqrtnlift(gen_1, subss(p,1), Ncpi, utoi(p), N);
    1099           7 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fp");
    1100           7 :     Nc2 = Fp_mul(Nc2,tNc2,q);
    1101             :   }
    1102          21 :   t = Fp_center_i(Fp_mul(Nc2,Np,q),q,shifti(q,-1));
    1103          21 :   return gerepileupto(av, subii(addiu(powuu(p,n),1),t));
    1104             : }
    1105             : 
    1106             : /***************************************************************************/
    1107             : /*                                                                         */
    1108             : /*                          Shanks Mestre                                  */
    1109             : /*                                                                         */
    1110             : /***************************************************************************/
    1111             : 
    1112             : /* Return the lift of a (mod b), which is closest to h */
    1113             : static GEN
    1114        1629 : closest_lift(GEN a, GEN b, GEN h)
    1115             : {
    1116        1629 :   return addii(a, mulii(b, diviiround(subii(h,a), b)));
    1117             : }
    1118             : 
    1119             : static GEN
    1120         873 : FlxqE_find_order(GEN f, GEN h, GEN bound, GEN B, GEN a4, GEN T, ulong p)
    1121             : {
    1122         873 :   pari_sp av = avma, av1;
    1123             :   pari_timer Ti;
    1124         873 :   long s = itos( gceil(gsqrt(gdiv(bound,B),DEFAULTPREC)) ) >> 1;
    1125             :   GEN tx, ti;
    1126         873 :   GEN fh = FlxqE_mul(f, h, a4, T, p);
    1127         873 :   GEN F, P = fh, fg;
    1128             :   long i;
    1129         873 :   if (DEBUGLEVEL >= 6) timer_start(&Ti);
    1130         873 :   if (ell_is_inf(fh)) return h;
    1131         827 :   F = FlxqE_mul(f, B, a4, T, p);
    1132         827 :   if (s < 3)
    1133             :   { /* we're nearly done: naive search */
    1134         190 :     GEN Q = P;
    1135         602 :     for (i=1;; i++)
    1136             :     {
    1137        1014 :       P = FlxqE_add(P, F, a4, T, p); /* h.f + i.F */
    1138         602 :       if (ell_is_inf(P)) return gerepileupto(av, addii(h, mului(i,B)));
    1139         568 :       Q = FlxqE_sub(Q, F, a4, T, p); /* h.f - i.F */
    1140         568 :       if (ell_is_inf(Q)) return gerepileupto(av, subii(h, mului(i,B)));
    1141             :     }
    1142             :   }
    1143         637 :   tx = cgetg(s+1,t_VECSMALL);
    1144             :   /* Baby Step/Giant Step */
    1145         637 :   av1 = avma;
    1146        3729 :   for (i=1; i<=s; i++)
    1147             :   { /* baby steps */
    1148        3205 :     tx[i] = hash_GEN(gel(P, 1));
    1149        3205 :     P = FlxqE_add(P, F, a4, T, p); /* h.f + i.F */
    1150        3205 :     if (ell_is_inf(P)) return gerepileupto(av, addii(h, mului(i,B)));
    1151        3092 :     if (gc_needed(av1,3))
    1152             :     {
    1153           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[Flxq_ellcard] baby steps, i=%ld",i);
    1154           0 :       P = gerepileupto(av1,P);
    1155             :     }
    1156             :   }
    1157         524 :   if (DEBUGLEVEL >= 6) timer_printf(&Ti, "[Flxq_ellcard] baby steps, s = %ld",s);
    1158             :   /* giant steps: fg = s.F */
    1159         524 :   fg = gerepileupto(av1, FlxqE_sub(P, fh, a4, T, p));
    1160         524 :   if (ell_is_inf(fg)) return gerepileupto(av,mului(s,B));
    1161         524 :   ti = vecsmall_indexsort(tx); /* = permutation sorting tx */
    1162         524 :   tx = perm_mul(tx,ti);
    1163         524 :   if (DEBUGLEVEL >= 6) timer_printf(&Ti, "[Flxq_ellcard] sorting");
    1164         524 :   av1 = avma;
    1165        2346 :   for (P=fg, i=1; ; i++)
    1166        1822 :   {
    1167        2346 :     long k = hash_GEN(gel(P,1));
    1168        2346 :     long r = zv_search(tx, k);
    1169        2346 :     if (r)
    1170             :     {
    1171         524 :       while (r && tx[r] == k) r--;
    1172         524 :       for (r++; r <= s && tx[r] == k; r++)
    1173             :       {
    1174         524 :         long j = ti[r]-1;
    1175         524 :         GEN Q = FlxqE_add(FlxqE_mul(F, stoi(j), a4, T, p), fh, a4, T, p);
    1176         524 :         if (DEBUGLEVEL >= 6)
    1177           0 :           timer_printf(&Ti, "[Flxq_ellcard] giant steps, i = %ld",i);
    1178         524 :         if (Flx_equal(gel(P,1), gel(Q,1)))
    1179             :         {
    1180         524 :           if (Flx_equal(gel(P,2), gel(Q,2))) i = -i;
    1181         524 :           return gerepileupto(av,addii(h, mulii(addis(mulss(s,i), j), B)));
    1182             :         }
    1183             :       }
    1184             :     }
    1185        1822 :     P = FlxqE_add(P,fg,a4,T,p);
    1186        1822 :     if (gc_needed(av1,3))
    1187             :     {
    1188           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[Flxq_ellcard] giants steps, i=%ld",i);
    1189           0 :       P = gerepileupto(av1,P);
    1190             :     }
    1191             :   }
    1192             : }
    1193             : 
    1194             : static void
    1195       31962 : Flx_next(GEN t, ulong p)
    1196             : {
    1197             :   long i;
    1198       39725 :   for(i=2;;i++)
    1199       47488 :     if (uel(t,i)==p-1)
    1200        7763 :       t[i]=0;
    1201             :     else
    1202             :     {
    1203       31962 :       t[i]++;
    1204       31962 :       break;
    1205             :     }
    1206       31962 : }
    1207             : 
    1208             : static void
    1209       31962 : Flx_renormalize_ip(GEN x, long lx)
    1210             : {
    1211             :   long i;
    1212       39725 :   for (i = lx-1; i>=2; i--)
    1213       36813 :     if (x[i]) break;
    1214       31962 :   setlg(x, i+1);
    1215       31962 : }
    1216             : 
    1217             : static ulong
    1218        2240 : F3xq_ellcard_naive(GEN a2, GEN a6, GEN T)
    1219             : {
    1220        2240 :   pari_sp av = avma;
    1221        2240 :   long i, d = get_Flx_degree(T), lx = d+2;
    1222        2240 :   long q = upowuu(3, d), a;
    1223        2240 :   GEN x = zero_zv(lx); x[1] = get_Flx_var(T);
    1224       11186 :   for(a=1, i=0; i<q; i++)
    1225             :   {
    1226             :     GEN rhs;
    1227        8946 :     Flx_renormalize_ip(x, lx);
    1228        8946 :     rhs = Flx_add(Flxq_mul(Flxq_sqr(x, T, 3), Flx_add(x, a2, 3), T, 3), a6, 3);
    1229        8946 :     if (!lgpol(rhs)) a++; else if (Flxq_issquare(rhs, T, 3)) a+=2;
    1230        8946 :     Flx_next(x, 3);
    1231             :   }
    1232        2240 :   set_avma(av);
    1233        2240 :   return a;
    1234             : }
    1235             : 
    1236             : static ulong
    1237         672 : Flxq_ellcard_naive(GEN a4, GEN a6, GEN T, ulong p)
    1238             : {
    1239         672 :   pari_sp av = avma;
    1240         672 :   long i, d = get_Flx_degree(T), lx = d+2;
    1241         672 :   long q = upowuu(p, d), a;
    1242         672 :   GEN x = zero_zv(lx); x[1] = get_Flx_var(T);
    1243       23688 :   for(a=1, i=0; i<q; i++)
    1244             :   {
    1245             :     GEN x2, rhs;
    1246       23016 :     Flx_renormalize_ip(x, lx);
    1247       23016 :     x2  = Flxq_sqr(x, T, p);
    1248       23016 :     rhs = Flx_add(Flxq_mul(x, Flx_add(x2, a4, p), T, p), a6, p);
    1249       23016 :     if (!lgpol(rhs)) a++; else if (Flxq_issquare(rhs,T,p)) a+=2;
    1250       23016 :     Flx_next(x,p);
    1251             :   }
    1252         672 :   set_avma(av);
    1253         672 :   return a;
    1254             : }
    1255             : 
    1256             : /* assume T irreducible mod p, m = (q-1)/(p-1) */
    1257             : static long
    1258        1767 : Flxq_kronecker(GEN x, GEN m, GEN T, ulong p)
    1259             : {
    1260             :   pari_sp av;
    1261             :   ulong z;
    1262        1767 :   if (lgpol(x) == 0) return 0;
    1263        1760 :   av = avma; z = Flxq_pow(x, m, T, p)[2];
    1264        1760 :   return gc_long(av, krouu(z, p));
    1265             : }
    1266             : 
    1267             : /* Find x such that kronecker(u = x^3+a4x+a6, p) is KRO.
    1268             :  * Return point [x*u,u^2] on E (KRO=1) / E^twist (KRO=-1) */
    1269             : static GEN
    1270        1767 : Flxq_ellpoint(long KRO, GEN a4, GEN a6, GEN m, long n, long vn, GEN T, ulong p)
    1271             : {
    1272             :   for(;;)
    1273         894 :   {
    1274        1767 :     GEN x = random_Flx(n,vn,p);
    1275        1767 :     GEN u = Flx_add(a6, Flxq_mul(Flx_add(a4, Flxq_sqr(x,T,p), p), x, T,p), p);
    1276        1767 :     if (Flxq_kronecker(u, m,T,p) == KRO)
    1277        1746 :       return mkvec2(Flxq_mul(u,x, T,p), Flxq_sqr(u, T,p));
    1278             :   }
    1279             : }
    1280             : 
    1281             : static GEN
    1282         756 : Flxq_ellcard_Shanks(GEN a4, GEN a6, GEN q, GEN T, ulong p)
    1283             : {
    1284         756 :   pari_sp av = avma;
    1285         756 :   long vn = get_Flx_var(T), n = get_Flx_degree(T), KRO = -1;
    1286             :   GEN h,f, ta4, A, B, m;
    1287         756 :   GEN q1p = addiu(q,1), q2p = shifti(q1p, 1);
    1288         756 :   GEN bound = addiu(sqrti(gmul2n(q,4)), 1); /* ceil( 4sqrt(q) ) */
    1289             :   /* once #E(Flxq) is know mod B >= bound, it is completely determined */
    1290             :   /* how many 2-torsion points ? */
    1291         756 :   switch(FlxqX_nbroots(mkpoln(4, pol1_Flx(vn), pol0_Flx(vn), a4, a6), T, p))
    1292             :   {
    1293         266 :   case 3:  A = gen_0; B = utoipos(4); break;
    1294         231 :   case 1:  A = gen_0; B = gen_2; break;
    1295         259 :   default: A = gen_1; B = gen_2; break; /* 0 */
    1296             :   }
    1297         756 :   m = diviuexact(subiu(powuu(p,n), 1), p-1);
    1298             :   for(;;)
    1299             :   {
    1300         990 :     h = closest_lift(A, B, q1p);
    1301             :     /* [ux, u^2] is on E_u: y^2 = x^3 + c4 u^2 x + c6 u^3
    1302             :      * E_u isomorphic to E (resp. E') iff KRO = 1 (resp. -1)
    1303             :      * #E(F_p) = p+1 - a_p, #E'(F_p) = p+1 + a_p
    1304             :      *
    1305             :      * #E_u(Flxq) = A (mod B),  h is close to #E_u(Flxq) */
    1306         873 :     KRO = -KRO;
    1307         873 :     f = Flxq_ellpoint(KRO, a4,a6, m,n,vn, T,p);
    1308             : 
    1309         873 :     ta4 = Flxq_mul(a4, gel(f,2), T, p); /* a4 for E_u */
    1310         873 :     h = FlxqE_find_order(f, h, bound, B, ta4,T,p);
    1311         873 :     h = FlxqE_order(f, h, ta4, T, p);
    1312             :     /* h | #E_u(Flxq) = A (mod B) */
    1313         873 :     A = Z_chinese_all(A, gen_0, B, h, &B);
    1314         873 :     if (cmpii(B, bound) >= 0) break;
    1315             :     /* not done, update A mod B for the _next_ curve, isomorphic to
    1316             :      * the quadratic twist of this one */
    1317         117 :     A = remii(subii(q2p,A), B); /* #E(Fq)+#E'(Fq) = 2q+2 */
    1318             :   }
    1319         756 :   h = closest_lift(A, B, q1p);
    1320         756 :   return gerepileuptoint(av, KRO == 1? h: subii(q2p,h));
    1321             : }
    1322             : 
    1323             : static GEN
    1324       17045 : F3xq_ellcard(GEN a2, GEN a6, GEN T)
    1325             : {
    1326       17045 :   long n = get_Flx_degree(T);
    1327       17045 :   if (n <= 2)
    1328        1939 :     return utoi(F3xq_ellcard_naive(a2, a6, T));
    1329             :   else
    1330             :   {
    1331       15106 :     GEN q1 = addiu(powuu(3, get_Flx_degree(T)), 1), t;
    1332       15106 :     GEN a = Flxq_div(a6,Flxq_powu(a2,3,T,3),T,3);
    1333       15106 :     if (Flx_equal1(Flxq_powu(a, 8, T, 3)))
    1334             :     {
    1335         301 :       GEN P = Flxq_minpoly(a,T,3);
    1336         301 :       long dP = degpol(P); /* dP <= 2 */
    1337         301 :       ulong q = upowuu(3,dP);
    1338         301 :       GEN A2 = pol1_Flx(P[1]), A6 = Flx_rem(polx_Flx(P[1]), P, 3);
    1339         301 :       long tP = q + 1 - F3xq_ellcard_naive(A2, A6, P);
    1340         301 :       t = elltrace_extension(stoi(tP), n/dP, utoi(q));
    1341         301 :       if (umodiu(t, 3)!=1) t = negi(t);
    1342         301 :       return Flx_equal1(a2) || Flxq_issquare(a2,T,3) ? subii(q1,t): addii(q1,t);
    1343             :     }
    1344       14805 :     else return Flxq_ellcard_Kohel(mkvec(a2), a6, T, 3);
    1345             :   }
    1346             : }
    1347             : 
    1348             : static GEN
    1349       10899 : Flxq_ellcard_Satoh(GEN a4, GEN a6, GEN j, GEN T, ulong p)
    1350             : {
    1351       10899 :   long n = get_Flx_degree(T);
    1352       10899 :   if (n <= 2)
    1353         392 :     return utoi(Flxq_ellcard_naive(a4, a6, T, p));
    1354             :   else
    1355             :   {
    1356       10507 :     GEN jp = Flxq_powu(j, p, T, p);
    1357       10507 :     GEN s = Flx_add(j, jp, p);
    1358       10507 :     if (degpol(s) <= 0)
    1359             :     { /* it is assumed j not in F_p */
    1360         280 :       GEN m = Flxq_mul(j, jp, T, p);
    1361         280 :       if (degpol(m) <= 0)
    1362             :       {
    1363         280 :         GEN q = sqru(p);
    1364         280 :         GEN q1 = addiu(powuu(p, get_Flx_degree(T)), 1);
    1365         280 :         GEN sk = Flx_Fl_add(Flx_neg(j, p), 1728%p, p);
    1366         280 :         GEN sA4 = Flx_triple(Flxq_mul(sk, j, T, p), p);
    1367         280 :         GEN u = Flxq_div(a4, sA4, T, p);
    1368         280 :         ulong ns = lgpol(s) ? Fl_neg(s[2], p): 0UL;
    1369         280 :         GEN P = mkvecsmall4(T[1], m[2], ns, 1L);
    1370             :         GEN A4, A6, t, tP;
    1371         280 :         Flxq_ellj_to_a4a6(polx_Flx(T[1]), P, p, &A4, &A6);
    1372         280 :         tP = addis(q, 1 - Flxq_ellcard_naive(A4, A6, P, p));
    1373         280 :         t = elltrace_extension(tP, n>>1, q);
    1374         280 :         return Flxq_is2npower(u, 2, T, p) ? subii(q1,t): addii(q1,t);
    1375             :       }
    1376             :     }
    1377       10227 :     if (p<=7 || p==13 ) return Flxq_ellcard_Kohel(a4, a6, T, p);
    1378          21 :     else return Flxq_ellcard_Harley(a4, a6, T, p);
    1379             :   }
    1380             : }
    1381             : 
    1382             : static GEN
    1383           0 : Flxq_ellcard_Kedlaya(GEN a4, GEN a6, GEN T, ulong p)
    1384             : {
    1385           0 :   pari_sp av = avma;
    1386           0 :   GEN H = mkpoln(4, gen_1, gen_0, Flx_to_ZX(a4), Flx_to_ZX(a6));
    1387           0 :   GEN Tp = Flx_to_ZX(get_Flx_mod(T));
    1388           0 :   long n = degpol(Tp), e = ((p < 16 ? n+1: n)>>1)+1;
    1389           0 :   GEN M = ZlXQX_hyperellpadicfrobenius(H, Tp, p, e);
    1390           0 :   GEN N = ZpXQM_prodFrobenius(M, Tp, utoi(p), e);
    1391           0 :   GEN q = powuu(p, e);
    1392           0 :   GEN tp = Fq_add(gcoeff(N,1,1), gcoeff(N,2,2), Tp, q);
    1393           0 :   GEN t = Fp_center_i(typ(tp)==t_INT ? tp: leading_coeff(tp), q, shifti(q,-1));
    1394           0 :   return gerepileupto(av, subii(addiu(powuu(p, n), 1), t));
    1395             : }
    1396             : 
    1397             : GEN
    1398       51421 : Flxq_ellj(GEN a4, GEN a6, GEN T, ulong p)
    1399             : {
    1400       51421 :   pari_sp av=avma;
    1401       51421 :   if (p==3)
    1402             :   {
    1403             :     GEN J;
    1404       14805 :     if (typ(a4)!=t_VEC) return pol0_Flx(get_Flx_var(T));
    1405       14805 :     J = Flxq_div(Flxq_powu(gel(a4,1),3, T, p),Flx_neg(a6,p), T, p);
    1406       14805 :     return gerepileuptoleaf(av, J);
    1407             :   }
    1408             :   else
    1409             :   {
    1410       36616 :     pari_sp av=avma;
    1411       36616 :     GEN a43 = Flxq_mul(a4,Flxq_sqr(a4,T,p),T,p);
    1412       36616 :     GEN a62 = Flxq_sqr(a6,T,p);
    1413       36616 :     GEN num = Flx_mulu(a43,6912,p);
    1414       36616 :     GEN den = Flx_add(Flx_mulu(a43,4,p),Flx_mulu(a62,27,p),p);
    1415       36616 :     return gerepileuptoleaf(av, Flxq_div(num, den, T, p));
    1416             :   }
    1417             : }
    1418             : 
    1419             : void
    1420         280 : Flxq_ellj_to_a4a6(GEN j, GEN T, ulong p, GEN *pt_a4, GEN *pt_a6)
    1421             : {
    1422         280 :   ulong zagier = 1728 % p;
    1423         280 :   if (lgpol(j)==0)
    1424           0 :     { *pt_a4 = pol0_Flx(T[1]); *pt_a6 =pol1_Flx(T[1]); }
    1425         280 :   else if (lgpol(j)==1 && uel(j,2) == zagier)
    1426           0 :     { *pt_a4 = pol1_Flx(T[1]); *pt_a6 =pol0_Flx(T[1]); }
    1427             :   else
    1428             :   {
    1429         280 :     GEN k = Flx_Fl_add(Flx_neg(j, p), zagier, p);
    1430         280 :     GEN kj = Flxq_mul(k, j, T, p);
    1431         280 :     GEN k2j = Flxq_mul(kj, k, T, p);
    1432         280 :     *pt_a4 = Flx_triple(kj, p);
    1433         280 :     *pt_a6 = Flx_double(k2j, p);
    1434             :   }
    1435         280 : }
    1436             : 
    1437             : static GEN
    1438        6426 : F3xq_ellcardj(GEN a4, GEN a6, GEN T, GEN q, long n)
    1439             : {
    1440        6426 :   const ulong p = 3;
    1441             :   ulong t;
    1442        6426 :   GEN q1 = addiu(q,1);
    1443        6426 :   GEN na4 = Flx_neg(a4,p), ra4;
    1444        6426 :   if (!Flxq_issquare(na4,T,p))
    1445        3094 :     return q1;
    1446        3332 :   ra4 = Flxq_sqrt(na4,T,p);
    1447        3332 :   t = Flxq_trace(Flxq_div(a6,Flxq_mul(na4,ra4,T,p),T,p),T,p);
    1448        3332 :   if (n%2==1)
    1449             :   {
    1450             :     GEN q3;
    1451        1176 :     if (t==0) return q1;
    1452         301 :     q3 = powuu(p,(n+1)>>1);
    1453         301 :     return (t==1)^(n%4==1) ? subii(q1,q3): addii(q1,q3);
    1454             :   }
    1455             :   else
    1456             :   {
    1457        2156 :     GEN q22, q2 = powuu(p,n>>1);
    1458        2156 :     GEN W = Flxq_pow(a4,shifti(q,-2),T,p);
    1459        2156 :     long s = (W[2]==1)^(n%4==2);
    1460        2156 :     if (t!=0) return s ? addii(q1,q2): subii(q1, q2);
    1461        2156 :     q22 = shifti(q2,1);
    1462        2156 :     return s ? subii(q1,q22):  addii(q1, q22);
    1463             :   }
    1464             : }
    1465             : 
    1466             : static GEN
    1467       14707 : Flxq_ellcardj(GEN a4, GEN a6, ulong j, GEN T, GEN q, ulong p, long n)
    1468             : {
    1469       14707 :   GEN q1 = addiu(q,1);
    1470       14707 :   if (j==0)
    1471             :   {
    1472             :     ulong w;
    1473             :     GEN W, t, N;
    1474        5600 :     if (umodiu(q,6)!=1) return q1;
    1475        4200 :     N = Fp_ffellcard(gen_0,gen_1,q,n,utoi(p));
    1476        4200 :     t = subii(q1, N);
    1477        4200 :     W = Flxq_pow(a6,diviuexact(shifti(q,-1), 3),T,p);
    1478        4200 :     if (degpol(W)>0) /*p=5 mod 6*/
    1479        1407 :       return Flx_equal1(Flxq_powu(W,3,T,p)) ? addii(q1,shifti(t,-1)):
    1480         469 :                                               subii(q1,shifti(t,-1));
    1481        3262 :     w = W[2];
    1482        3262 :     if (w==1)   return N;
    1483        2590 :     if (w==p-1) return addii(q1,t);
    1484             :     else /*p=1 mod 6*/
    1485             :     {
    1486        1918 :       GEN u = shifti(t,-1), v = sqrtint(diviuexact(subii(q,sqri(u)),3));
    1487        1918 :       GEN a = addii(u,v), b = shifti(v,1);
    1488        1918 :       if (Fl_powu(w,3,p)==1)
    1489             :       {
    1490         959 :         if (Fl_add(umodiu(a,p),Fl_mul(w,umodiu(b,p),p),p)==0)
    1491         455 :           return subii(q1,subii(shifti(b,1),a));
    1492             :         else
    1493         504 :           return addii(q1,addii(a,b));
    1494             :       }
    1495             :       else
    1496             :       {
    1497         959 :         if (Fl_sub(umodiu(a,p),Fl_mul(w,umodiu(b,p),p),p)==0)
    1498         455 :           return subii(q1,subii(a,shifti(b,1)));
    1499             :         else
    1500         504 :           return subii(q1,addii(a,b));
    1501             :       }
    1502             :     }
    1503        9107 :   } else if (j==1728%p)
    1504             :   {
    1505             :     ulong w;
    1506             :     GEN W, N, t;
    1507        5614 :     if (mod4(q)==3) return q1;
    1508        4214 :     W = Flxq_pow(a4,shifti(q,-2),T,p);
    1509        4214 :     if (degpol(W)>0) return q1; /*p=3 mod 4*/
    1510        3542 :     w = W[2];
    1511        3542 :     N = Fp_ffellcard(gen_1,gen_0,q,n,utoi(p));
    1512        3542 :     if(w==1) return N;
    1513        2520 :     t = subii(q1, N);
    1514        2520 :     if(w==p-1) return addii(q1, t);
    1515             :     else /*p=1 mod 4*/
    1516             :     {
    1517        1484 :       GEN u = shifti(t,-1), v = sqrtint(subii(q,sqri(u)));
    1518        1484 :       if (Fl_add(umodiu(u,p),Fl_mul(w,umodiu(v,p),p),p)==0)
    1519         742 :         return subii(q1,shifti(v,1));
    1520             :       else
    1521         742 :         return addii(q1,shifti(v,1));
    1522             :     }
    1523             :   } else
    1524             :   {
    1525        3493 :     ulong g = Fl_div(j, Fl_sub(1728%p, j, p), p);
    1526        3493 :     GEN l = Flxq_div(Flx_triple(a6,p),Flx_double(a4,p),T,p);
    1527        3493 :     GEN N = Fp_ffellcard(utoi(Fl_triple(g,p)),utoi(Fl_double(g,p)),q,n,utoi(p));
    1528        3493 :     if (Flxq_issquare(l,T,p)) return N;
    1529        2072 :     return subii(shifti(q1,1),N);
    1530             :   }
    1531             : }
    1532             : 
    1533             : GEN
    1534       50070 : Flxq_ellcard(GEN a4, GEN a6, GEN T, ulong p)
    1535             : {
    1536       50070 :   pari_sp av = avma;
    1537       50070 :   long n = get_Flx_degree(T);
    1538       50070 :   GEN J, r, q = powuu(p,  n);
    1539       50070 :   if (typ(a4)==t_VEC)
    1540       17045 :     r = F3xq_ellcard(gel(a4,1), a6, T);
    1541       33025 :   else if (p==3)
    1542        6426 :     r = F3xq_ellcardj(a4, a6, T, q, n);
    1543       26599 :   else if (degpol(a4)<=0 && degpol(a6)<=0)
    1544         210 :     r = Fp_ffellcard(utoi(Flx_eval(a4,0,p)),utoi(Flx_eval(a6,0,p)),q,n,utoi(p));
    1545       26389 :   else if (degpol(J=Flxq_ellj(a4,a6,T,p))<=0)
    1546       14707 :     r = Flxq_ellcardj(a4,a6,lgpol(J)?J[2]:0,T,q,p,n);
    1547       11682 :   else if (p <= 7)
    1548       10836 :     r = Flxq_ellcard_Satoh(a4, a6, J, T, p);
    1549         846 :   else if (cmpis(q,100)<0)
    1550           0 :     r = utoi(Flxq_ellcard_naive(a4, a6, T, p));
    1551         846 :   else if (p == 13 || (7*p <= (ulong)10*n && (BITS_IN_LONG==64 || p <= 103)))
    1552          63 :     r = Flxq_ellcard_Satoh(a4, a6, J, T, p);
    1553         783 :   else if (p <= (ulong)2*n)
    1554           0 :     r = Flxq_ellcard_Kedlaya(a4, a6, T, p);
    1555         783 :   else if (expi(q)<=62)
    1556         756 :     r = Flxq_ellcard_Shanks(a4, a6, q, T, p);
    1557             :   else
    1558          27 :     r = Fq_ellcard_SEA(Flx_to_ZX(a4),Flx_to_ZX(a6),q,Flx_to_ZX(T),utoi(p),0);
    1559       50070 :   return gerepileuptoint(av, r);
    1560             : }

Generated by: LCOV version 1.13