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

Generated by: LCOV version 1.11