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 20459-9710128) Lines: 916 944 97.0 %
Date: 2017-04-29 05:33:44 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       54804 : RgE_to_FlxqE(GEN x, GEN T, ulong p)
      34             : {
      35       54804 :   if (ell_is_inf(x)) return x;
      36       54804 :   retmkvec2(Rg_to_Flxq(gel(x,1),T,p),Rg_to_Flxq(gel(x,2),T,p));
      37             : }
      38             : 
      39             : GEN
      40      131136 : FlxqE_changepoint(GEN x, GEN ch, GEN T, ulong p)
      41             : {
      42      131136 :   pari_sp av = avma;
      43             :   GEN p1,z,u,r,s,t,v,v2,v3;
      44      131136 :   if (ell_is_inf(x)) return x;
      45       77832 :   u = gel(ch,1); r = gel(ch,2);
      46       77832 :   s = gel(ch,3); t = gel(ch,4);
      47       77832 :   v = Flxq_inv(u, T, p); v2 = Flxq_sqr(v, T, p); v3 = Flxq_mul(v,v2, T, p);
      48       77832 :   p1 = Flx_sub(gel(x,1),r, p);
      49       77832 :   z = cgetg(3,t_VEC);
      50       77832 :   gel(z,1) = Flxq_mul(v2, p1, T, p);
      51       77832 :   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       77832 :   return gerepileupto(av, z);
      53             : }
      54             : 
      55             : GEN
      56       54804 : FlxqE_changepointinv(GEN x, GEN ch, GEN T, ulong p)
      57             : {
      58             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
      59       54804 :   if (ell_is_inf(x)) return x;
      60       54804 :   X = gel(x,1); Y = gel(x,2);
      61       54804 :   u = gel(ch,1); r = gel(ch,2);
      62       54804 :   s = gel(ch,3); t = gel(ch,4);
      63       54804 :   u2 = Flxq_sqr(u, T, p); u3 = Flxq_mul(u,u2, T, p);
      64       54804 :   u2X = Flxq_mul(u2,X, T, p);
      65       54804 :   z = cgetg(3, t_VEC);
      66       54804 :   gel(z,1) = Flx_add(u2X,r, p);
      67       54804 :   gel(z,2) = Flx_add(Flxq_mul(u3,Y, T, p), Flx_add(Flxq_mul(s,u2X, T, p), t, p), p);
      68       54804 :   return z;
      69             : }
      70             : 
      71             : static ulong
      72       17664 : nonsquare_Fl(ulong p)
      73             : {
      74             :   ulong a;
      75             :   do
      76       17664 :     a = random_Fl(p);
      77       17664 :   while (krouu(a, p) >= 0);
      78        6588 :   return a;
      79             : }
      80             : 
      81             : static GEN
      82       19572 : nonsquare_Flxq(GEN T, ulong p)
      83             : {
      84       19572 :   pari_sp av = avma;
      85       19572 :   long n = degpol(T), vs = T[1];
      86             :   GEN a;
      87       19572 :   if (odd(n))
      88        6588 :     return mkvecsmall2(vs, nonsquare_Fl(p));
      89             :   do
      90             :   {
      91       25710 :     avma = av;
      92       25710 :     a = random_Flx(n, vs, p);
      93       25710 :   } while (Flxq_issquare(a, T, p));
      94       12984 :   return a;
      95             : }
      96             : 
      97             : void
      98       19572 : Flxq_elltwist(GEN a, GEN a6, GEN T, ulong p, GEN *pt_a, GEN *pt_a6)
      99             : {
     100       19572 :   GEN d = nonsquare_Flxq(T, p);
     101       19572 :   GEN d2 = Flxq_sqr(d, T, p), d3 = Flxq_mul(d2, d, T, p);
     102       19572 :   if (typ(a)==t_VECSMALL)
     103             :   {
     104       13056 :     *pt_a  = Flxq_mul(a,  d2, T, p);
     105       13056 :     *pt_a6 = Flxq_mul(a6, d3, T, p);
     106             :   } else
     107             :   {
     108        6516 :     *pt_a  = mkvec(Flxq_mul(gel(a,1), d, T, p));
     109        6516 :     *pt_a6 = Flxq_mul(a6, d3, T, p);
     110             :   }
     111       19572 : }
     112             : 
     113             : static GEN
     114     1086146 : FlxqE_dbl_slope(GEN P, GEN a4, GEN T, ulong p, GEN *slope)
     115             : {
     116             :   GEN x, y, Q;
     117     1086146 :   if (ell_is_inf(P) || !lgpol(gel(P,2))) return ellinf();
     118     1001003 :   x = gel(P,1); y = gel(P,2);
     119     1001003 :   if (p==3UL)
     120     1357458 :     *slope = typ(a4)==t_VEC ? Flxq_div(Flxq_mul(x, gel(a4, 1), T, p), y, T, p)
     121      899802 :                             : Flxq_div(a4, Flx_neg(y, p), T, p);
     122             :   else
     123             :   {
     124      543347 :     GEN sx = Flx_add(Flx_triple(Flxq_sqr(x, T, p), p), a4, p);
     125      543347 :     *slope = Flxq_div(sx, Flx_double(y, p), T, p);
     126             :   }
     127     1001003 :   Q = cgetg(3,t_VEC);
     128     1001003 :   gel(Q, 1) = Flx_sub(Flxq_sqr(*slope, T, p), Flx_double(x, p), p);
     129     1001003 :   if (typ(a4)==t_VEC) gel(Q, 1) = Flx_sub(gel(Q, 1), gel(a4, 1), p);
     130     1001003 :   gel(Q, 2) = Flx_sub(Flxq_mul(*slope, Flx_sub(x, gel(Q, 1), p), T, p), y, p);
     131     1001003 :   return Q;
     132             : }
     133             : 
     134             : GEN
     135     1063099 : FlxqE_dbl(GEN P, GEN a4, GEN T, ulong p)
     136             : {
     137     1063099 :   pari_sp av = avma;
     138             :   GEN slope;
     139     1063099 :   return gerepileupto(av, FlxqE_dbl_slope(P,a4, T, p,&slope));
     140             : }
     141             : 
     142             : static GEN
     143      453357 : FlxqE_add_slope(GEN P, GEN Q, GEN a4, GEN T, ulong p, GEN *slope)
     144             : {
     145             :   GEN Px, Py, Qx, Qy, R;
     146      453357 :   if (ell_is_inf(P)) return Q;
     147      450447 :   if (ell_is_inf(Q)) return P;
     148      450321 :   Px = gel(P,1); Py = gel(P,2);
     149      450321 :   Qx = gel(Q,1); Qy = gel(Q,2);
     150      450321 :   if (Flx_equal(Px, Qx))
     151             :   {
     152       40620 :     if (Flx_equal(Py, Qy))
     153        1134 :       return FlxqE_dbl_slope(P, a4, T, p, slope);
     154             :     else
     155       39486 :       return ellinf();
     156             :   }
     157      409701 :   *slope = Flxq_div(Flx_sub(Py, Qy, p), Flx_sub(Px, Qx, p), T, p);
     158      409701 :   R = cgetg(3,t_VEC);
     159      409701 :   gel(R, 1) = Flx_sub(Flx_sub(Flxq_sqr(*slope, T, p), Px, p), Qx, p);
     160      409701 :   if (typ(a4)==t_VEC) gel(R, 1) = Flx_sub(gel(R, 1),gel(a4, 1), p);
     161      409701 :   gel(R, 2) = Flx_sub(Flxq_mul(*slope, Flx_sub(Px, gel(R, 1), p), T, p), Py, p);
     162      409701 :   return R;
     163             : }
     164             : 
     165             : GEN
     166      450116 : FlxqE_add(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     167             : {
     168      450116 :   pari_sp av = avma;
     169             :   GEN slope;
     170      450116 :   return gerepileupto(av, FlxqE_add_slope(P,Q,a4, T, p,&slope));
     171             : }
     172             : 
     173             : static GEN
     174         870 : FlxqE_neg_i(GEN P, ulong p)
     175             : {
     176         870 :   if (ell_is_inf(P)) return P;
     177         870 :   return mkvec2(gel(P,1), Flx_neg(gel(P,2), p));
     178             : }
     179             : 
     180             : GEN
     181         372 : FlxqE_neg(GEN P, GEN T, ulong p)
     182             : {
     183             :   (void) T;
     184         372 :   if (ell_is_inf(P)) return ellinf();
     185         372 :   return mkvec2(gcopy(gel(P,1)), Flx_neg(gel(P,2), p));
     186             : }
     187             : 
     188             : GEN
     189         870 : FlxqE_sub(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     190             : {
     191         870 :   pari_sp av = avma;
     192             :   GEN slope;
     193         870 :   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     1063099 : _FlxqE_dbl(void *E, GEN P)
     205             : {
     206     1063099 :   struct _FlxqE *ell = (struct _FlxqE *) E;
     207     1063099 :   return FlxqE_dbl(P, ell->a4, ell->T, ell->p);
     208             : }
     209             : 
     210             : static GEN
     211      443918 : _FlxqE_add(void *E, GEN P, GEN Q)
     212             : {
     213      443918 :   struct _FlxqE *ell=(struct _FlxqE *) E;
     214      443918 :   return FlxqE_add(P, Q, ell->a4, ell->T, ell->p);
     215             : }
     216             : 
     217             : static GEN
     218      184686 : _FlxqE_mul(void *E, GEN P, GEN n)
     219             : {
     220      184686 :   pari_sp av = avma;
     221      184686 :   struct _FlxqE *e=(struct _FlxqE *) E;
     222      184686 :   long s = signe(n);
     223      184686 :   if (!s || ell_is_inf(P)) return ellinf();
     224      184440 :   if (s<0) P = FlxqE_neg(P, e->T, e->p);
     225      184440 :   if (is_pm1(n)) return s>0? gcopy(P): P;
     226      180586 :   return gerepileupto(av, gen_pow(P, n, e, &_FlxqE_dbl, &_FlxqE_add));
     227             : }
     228             : 
     229             : GEN
     230       55530 : FlxqE_mul(GEN P, GEN n, GEN a4, GEN T, ulong p)
     231             : {
     232             :   struct _FlxqE E;
     233       55530 :   E.a4= a4; E.T = T; E.p = p;
     234       55530 :   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       65208 : random_F3xqE(GEN a2, GEN a6, GEN T)
     242             : {
     243       65208 :   pari_sp ltop = avma;
     244             :   GEN x, y, rhs;
     245       65208 :   const ulong p=3;
     246             :   do
     247             :   {
     248      130500 :     avma= ltop;
     249      130500 :     x   = random_Flx(get_Flx_degree(T),get_Flx_var(T),p);
     250      130500 :     rhs = Flx_add(Flxq_mul(Flxq_sqr(x, T, p), Flx_add(x, a2, p), T, p), a6, p);
     251      130500 :   } while ((!lgpol(rhs) && !lgpol(x)) || !Flxq_issquare(rhs, T, p));
     252       65208 :   y = Flxq_sqrt(rhs, T, p);
     253       65208 :   if (!y) pari_err_PRIME("random_F3xqE", T);
     254       65208 :   return gerepilecopy(ltop, mkvec2(x, y));
     255             : }
     256             : 
     257             : /* Finds a random non-singular point on E */
     258             : GEN
     259      121037 : random_FlxqE(GEN a4, GEN a6, GEN T, ulong p)
     260             : {
     261      121037 :   pari_sp ltop = avma;
     262             :   GEN x, x2, y, rhs;
     263      121037 :   if (typ(a4)==t_VEC)
     264       65208 :     return random_F3xqE(gel(a4,1), a6, T);
     265             :   do
     266             :   {
     267      113120 :     avma= ltop;
     268      113120 :     x   = random_Flx(get_Flx_degree(T),get_Flx_var(T),p);
     269      113120 :     x2  = Flxq_sqr(x, T, p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
     270      113120 :     rhs = Flx_add(Flxq_mul(x, Flx_add(x2, a4, p), T, p), a6, p);
     271      113972 :   } while ((!lgpol(rhs) && !lgpol(Flx_add(Flx_triple(x2, p), a4, p)))
     272      226240 :           || !Flxq_issquare(rhs, T, p));
     273       55829 :   y = Flxq_sqrt(rhs, T, p);
     274       55829 :   if (!y) pari_err_PRIME("random_FlxqE", T);
     275       55829 :   return gerepilecopy(ltop, mkvec2(x, y));
     276             : }
     277             : 
     278             : static GEN
     279       56645 : _FlxqE_rand(void *E)
     280             : {
     281       56645 :   struct _FlxqE *ell=(struct _FlxqE *) E;
     282       56645 :   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          30 : get_FlxqE_group(void ** pt_E, GEN a4, GEN a6, GEN T, ulong p)
     289             : {
     290          30 :   struct _FlxqE *e = (struct _FlxqE *) stack_malloc(sizeof(struct _FlxqE));
     291          30 :   e->a4 = a4; e->a6 = a6; e->T = Flx_get_red(T, p); e->p = p;
     292          30 :   *pt_E = (void *) e;
     293          30 :   return &FlxqE_group;
     294             : }
     295             : 
     296             : GEN
     297        2106 : FlxqE_order(GEN z, GEN o, GEN a4, GEN T, ulong p)
     298             : {
     299        2106 :   pari_sp av = avma;
     300             :   struct _FlxqE e;
     301        2106 :   e.a4=a4; e.T=T; e.p=p;
     302        2106 :   return gerepileuptoint(av, gen_order(z, o, (void*)&e, &FlxqE_group));
     303             : }
     304             : 
     305             : GEN
     306          42 : FlxqE_log(GEN a, GEN b, GEN o, GEN a4, GEN T, ulong p)
     307             : {
     308          42 :   pari_sp av = avma;
     309             :   struct _FlxqE e;
     310          42 :   e.a4=a4; e.T=T; e.p=p;
     311          42 :   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       59816 : FlxqE_vert(GEN P, GEN Q, GEN a4, GEN T, ulong p)
     324             : {
     325       59816 :   long vT = get_Flx_var(T);
     326             :   GEN df;
     327       59816 :   if (ell_is_inf(P))
     328       19341 :     return pol1_Flx(vT);
     329       40475 :   if (!Flx_equal(gel(Q, 1), gel(P, 1)))
     330       36611 :     return Flx_sub(gel(Q, 1), gel(P, 1), p);
     331        3864 :   if (lgpol(gel(P,2))!=0) return pol1_Flx(vT);
     332        8476 :   df = typ(a4)==t_VEC ? Flxq_mul(gel(P,1), Flx_mulu(gel(a4, 1), 2, p), T, p)
     333        5174 :                       : a4;
     334        3302 :   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       24284 : FlxqE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN T, ulong p)
     340             : {
     341       24284 :   long vT = get_Flx_var(T);
     342       24284 :   GEN x = gel(Q, 1), y = gel(Q, 2);
     343       24284 :   GEN tmp1 = Flx_sub(x, gel(R, 1), p);
     344       24284 :   GEN tmp2 = Flx_add(Flxq_mul(tmp1, slope, T, p), gel(R, 2), p);
     345       24284 :   if (!Flx_equal(y, tmp2))
     346       22775 :     return Flx_sub(y, tmp2, p);
     347        1509 :   if (lgpol(y) == 0)
     348         421 :     return pol1_Flx(vT);
     349             :   else
     350             :   {
     351        1088 :     GEN s1, s2, a2 = typ(a4)==t_VEC ? gel(a4,1): NULL;
     352        1088 :     GEN y2i = Flxq_inv(Flx_mulu(y, 2, p), T, p);
     353        1088 :     GEN df = a2 ? Flxq_mul(x, Flx_mulu(a2, 2, p), T, p): a4;
     354             :     GEN x3, ddf;
     355        1088 :     s1 = Flxq_mul(Flx_add(Flx_mulu(Flxq_sqr(x, T, p), 3, p), df, p), y2i, T, p);
     356        1088 :     if (!Flx_equal(s1, slope))
     357         295 :       return Flx_sub(s1, slope, p);
     358         793 :     x3 = Flx_mulu(x, 3, p);
     359         793 :     ddf = a2 ? Flx_add(x3, a2, p): x3;
     360         793 :     s2 = Flxq_mul(Flx_sub(ddf, Flxq_sqr(s1, T, p), p), y2i, T, p);
     361         793 :     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       40102 : FlxqE_tangent_update(GEN R, GEN Q, GEN a4, GEN T, ulong p, GEN *pt_R)
     371             : {
     372       40102 :   if (ell_is_inf(R))
     373             :   {
     374        3244 :     *pt_R = ellinf();
     375        3244 :     return pol1_Flx(get_Flx_var(T));
     376             :   }
     377       36858 :   else if (!lgpol(gel(R,2)))
     378             :   {
     379       14945 :     *pt_R = ellinf();
     380       14945 :     return FlxqE_vert(R, Q, a4, T, p);
     381             :   } else {
     382             :     GEN slope;
     383       21913 :     *pt_R = FlxqE_dbl_slope(R, a4, T, p, &slope);
     384       21913 :     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        3570 : FlxqE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN T, ulong p, GEN *pt_R)
     394             : {
     395        3570 :   if (ell_is_inf(R))
     396             :   {
     397          47 :     *pt_R = gcopy(P);
     398          47 :     return FlxqE_vert(P, Q, a4, T, p);
     399             :   }
     400        3523 :   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        3523 :   else if (Flx_equal(gel(P, 1), gel(R, 1)))
     406             :   {
     407        1152 :     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        1152 :       *pt_R = ellinf();
     412        1152 :       return FlxqE_vert(R, Q, a4, T, p);
     413             :     }
     414             :   } else {
     415             :     GEN slope;
     416        2371 :     *pt_R = FlxqE_add_slope(P, R, a4, T, p, &slope);
     417        2371 :     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       40102 : FlxqE_Miller_dbl(void* E, GEN d)
     433             : {
     434       40102 :   struct _FlxqE_miller *m = (struct _FlxqE_miller *)E;
     435       40102 :   ulong p  = m->p;
     436       40102 :   GEN T = m->T, a4 = m->a4, P = m->P;
     437             :   GEN v, line;
     438       40102 :   GEN num = Flxq_sqr(gel(d,1), T, p);
     439       40102 :   GEN denom = Flxq_sqr(gel(d,2), T, p);
     440       40102 :   GEN point = gel(d,3);
     441       40102 :   line = FlxqE_tangent_update(point, P, a4, T, p, &point);
     442       40102 :   num  = Flxq_mul(num, line, T, p);
     443       40102 :   v = FlxqE_vert(point, P, a4, T, p);
     444       40102 :   denom = Flxq_mul(denom, v, T, p);
     445       40102 :   return mkvec3(num, denom, point);
     446             : }
     447             : 
     448             : static GEN
     449        3570 : FlxqE_Miller_add(void* E, GEN va, GEN vb)
     450             : {
     451        3570 :   struct _FlxqE_miller *m = (struct _FlxqE_miller *)E;
     452        3570 :   ulong p = m->p;
     453        3570 :   GEN T = m->T, a4 = m->a4, P = m->P;
     454             :   GEN v, line, point;
     455        3570 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
     456        3570 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
     457        3570 :   GEN num   = Flxq_mul(na, nb, T, p);
     458        3570 :   GEN denom = Flxq_mul(da, db, T, p);
     459        3570 :   line = FlxqE_chord_update(pa, pb, P, a4, T, p, &point);
     460        3570 :   num  = Flxq_mul(num, line, T, p);
     461        3570 :   v = FlxqE_vert(point, P, a4, T, p);
     462        3570 :   denom = Flxq_mul(denom, v, T, p);
     463        3570 :   return mkvec3(num, denom, point);
     464             : }
     465             : 
     466             : static GEN
     467       16050 : FlxqE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN T, ulong p)
     468             : {
     469       16050 :   pari_sp ltop = avma;
     470             :   struct _FlxqE_miller d;
     471             :   GEN v, num, denom, g1;
     472             : 
     473       16050 :   d.a4 = a4; d.T = T; d.p = p; d.P = P;
     474       16050 :   g1 = pol1_Flx(get_Flx_var(T));
     475       16050 :   v = gen_pow(mkvec3(g1,g1,Q), m, (void*)&d, FlxqE_Miller_dbl, FlxqE_Miller_add);
     476       16050 :   num = gel(v,1); denom = gel(v,2);
     477       16050 :   return gerepileupto(ltop, Flxq_div(num, denom, T, p));
     478             : }
     479             : 
     480             : GEN
     481       10536 : FlxqE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, ulong p)
     482             : {
     483       10536 :   pari_sp ltop = avma;
     484             :   GEN num, denom, result;
     485       10536 :   if (ell_is_inf(P) || ell_is_inf(Q) || Flx_equal(P,Q))
     486        2535 :     return pol1_Flx(get_Flx_var(T));
     487        8001 :   num    = FlxqE_Miller(P, Q, m, a4, T, p);
     488        8001 :   denom  = FlxqE_Miller(Q, P, m, a4, T, p);
     489        8001 :   result = Flxq_div(num, denom, T, p);
     490        8001 :   if (mpodd(m))
     491         516 :     result  = Flx_neg(result, p);
     492        8001 :   return gerepileupto(ltop, result);
     493             : }
     494             : 
     495             : GEN
     496          48 : FlxqE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, ulong p)
     497             : {
     498          48 :   if (ell_is_inf(P) || ell_is_inf(Q))
     499           0 :     return pol1_Flx(get_Flx_var(T));
     500          48 :   return FlxqE_Miller(P, Q, m, a4, T, p);
     501             : }
     502             : 
     503             : static GEN
     504       10524 : _FlxqE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
     505             : {
     506       10524 :   struct _FlxqE *e = (struct _FlxqE *) E;
     507       10524 :   return  Flxq_order(FlxqE_weilpairing(P,Q,m,e->a4,e->T,e->p), F, e->T, e->p);
     508             : }
     509             : 
     510             : GEN
     511       12342 : Flxq_ellgroup(GEN a4, GEN a6, GEN N, GEN T, ulong p, GEN *pt_m)
     512             : {
     513             :   struct _FlxqE e;
     514       12342 :   GEN q = powuu(p, get_Flx_degree(T));
     515       12342 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
     516       12342 :   return gen_ellgroup(N, subiu(q,1), pt_m, (void*)&e, &FlxqE_group, _FlxqE_pairorder);
     517             : }
     518             : 
     519             : GEN
     520       11286 : Flxq_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN T, ulong p)
     521             : {
     522             :   GEN P;
     523       11286 :   pari_sp av = avma;
     524             :   struct _FlxqE e;
     525       11286 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
     526       11286 :   switch(lg(D)-1)
     527             :   {
     528             :   case 1:
     529        9180 :     P = gen_gener(gel(D,1), (void*)&e, &FlxqE_group);
     530        9180 :     P = mkvec(FlxqE_changepoint(P, ch, T, p));
     531        9180 :     break;
     532             :   default:
     533        2106 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FlxqE_group, _FlxqE_pairorder);
     534        2106 :     gel(P,1) = FlxqE_changepoint(gel(P,1), ch, T, p);
     535        2106 :     gel(P,2) = FlxqE_changepoint(gel(P,2), ch, T, p);
     536        2106 :     break;
     537             :   }
     538       11286 :   return gerepilecopy(av, P);
     539             : }
     540             : /***********************************************************************/
     541             : /**                                                                   **/
     542             : /**                          Point counting                           **/
     543             : /**                                                                   **/
     544             : /***********************************************************************/
     545             : 
     546        9540 : static GEN _can_invl(void *E, GEN V) {(void) E; return V; }
     547             : 
     548        3132 : static GEN _can_lin(void *E, GEN F, GEN V, GEN q)
     549             : {
     550        3132 :   GEN v = RgX_splitting(V, 3);
     551             :   (void) E;
     552        3132 :   return FpX_sub(V,ZXV_dotproduct(v, F), q);
     553             : }
     554             : 
     555             : static GEN
     556        6408 : _can_iter(void *E, GEN f, GEN q)
     557             : {
     558        6408 :   GEN h = RgX_splitting(f,3);
     559        6408 :   GEN h1s = ZX_sqr(gel(h,1)), h2s = ZX_sqr(gel(h,2)), h3s = ZX_sqr(gel(h,3));
     560        6408 :   GEN h12 = ZX_mul(gel(h,1), gel(h,2));
     561        6408 :   GEN h13 = ZX_mul(gel(h,1), gel(h,3));
     562        6408 :   GEN h23 = ZX_mul(gel(h,2), gel(h,3));
     563        6408 :   GEN h1c = ZX_mul(gel(h,1), h1s);
     564        6408 :   GEN h3c = ZX_mul(gel(h,3), h3s);
     565        6408 :   GEN th = ZX_mul(ZX_sub(h2s,ZX_mulu(h13,3)),gel(h,2));
     566        6408 :   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        6408 :   return mkvecn(7,y,h1s,h2s,h3s,h12,h13,h23);
     569             : }
     570             : 
     571             : static GEN
     572        6408 : _can_invd(void *E, GEN V, GEN v, GEN qM, long M)
     573             : {
     574        6408 :   GEN h1s=gel(v,2), h2s=gel(v,3), h3s=gel(v,4);
     575        6408 :   GEN h12=gel(v,5), h13=gel(v,6), h23=gel(v,7);
     576        6408 :   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        6408 :   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        3186 : F3x_canonlift(GEN P, long n)
     585        3186 : { return gen_ZpX_Newton(Flx_to_ZX(P),utoi(3), n, NULL, _can_iter, _can_invd); }
     586             : 
     587       25296 : static GEN _can5_invl(void *E, GEN V) {(void) E; return V; }
     588             : 
     589        7686 : static GEN _can5_lin(void *E, GEN F, GEN V, GEN q)
     590             : {
     591        7686 :   ulong p = *(ulong*)E;
     592        7686 :   GEN v = RgX_splitting(V, p);
     593        7686 :   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       53076 : _shift(GEN P, long n, ulong p, long v)
     599             : {
     600       53076 :   long i, l=lg(P);
     601       53076 :   GEN r = cgetg(l,t_POL); r[1] = P[1];
     602      413556 :   for(i=2;i<l;i++)
     603             :   {
     604      360480 :     long s = n*(i-2)%p;
     605      360480 :     GEN ci = gel(P,i);
     606      360480 :     if (typ(ci)==t_INT)
     607       89814 :       gel(r,i) = monomial(ci, s, v);
     608             :     else
     609      270666 :       gel(r,i) = RgX_rotate_shallow(ci, s, p);
     610             :   }
     611       53076 :   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       35466 : _can5_mul(void *E, GEN A, GEN B)
     622             : {
     623       35466 :   struct _can_mul *d = (struct _can_mul *)E;
     624       35466 :   GEN a = gel(A,1), b = gel(B,1);
     625       35466 :   long n = itos(gel(A,2));
     626       35466 :   GEN bn = _shift(b, n, d->p, get_FpX_var(d->T));
     627       35466 :   GEN c = FpXQX_mul(a, bn, d->T, d->q);
     628       35466 :   return mkvec2(c, addii(gel(A,2), gel(B,2)));
     629             : }
     630             : 
     631             : static GEN
     632       35298 : _can5_sqr(void *E, GEN A)
     633             : {
     634       35298 :   return _can5_mul(E,A,A);
     635             : }
     636             : 
     637             : static GEN
     638       17610 : _can5_iter(void *E, GEN f, GEN q)
     639             : {
     640       17610 :   pari_sp av = avma;
     641             :   struct _can_mul D;
     642       17610 :   ulong p = *(ulong*)E;
     643       17610 :   long i, vT = fetch_var();
     644             :   GEN N, P, d, V, fs;
     645       17610 :   D.q = q; D.T = ZX_Z_sub(pol_xn(p,vT),gen_1);
     646       17610 :   D.p = p;
     647       17610 :   fs = mkvec2(_shift(f, 1, p, vT), gen_1);
     648       17610 :   N = gel(gen_powu(fs,p-1,(void*)&D,_can5_sqr,_can5_mul),1);
     649       17610 :   N = simplify_shallow(FpXQX_red(N,polcyclo(p,vT),q));
     650       17610 :   P = FpX_mul(N,f,q);
     651       17610 :   P = RgX_deflate(P, p);
     652       17610 :   d = RgX_splitting(N, p);
     653       17610 :   V = cgetg(p+1,t_VEC);
     654       17610 :   gel(V,1) = ZX_mulu(gel(d,1), p);
     655       88758 :   for(i=2; i<= (long)p; i++)
     656       71148 :     gel(V,i) = ZX_mulu(RgX_shift_shallow(gel(d,p+2-i), 1), p);
     657       17610 :   (void)delete_var(); return gerepilecopy(av, mkvec2(ZX_sub(f,P),V));
     658             : }
     659             : 
     660             : static GEN
     661       17610 : _can5_invd(void *E, GEN H, GEN v, GEN qM, long M)
     662             : {
     663       17610 :   ulong p = *(long*)E;
     664       17610 :   return gen_ZpX_Dixon(gel(v,2), H, qM, utoi(p), M, E, _can5_lin, _can5_invl);
     665             : }
     666             : 
     667             : static GEN
     668       11952 : Flx_canonlift(GEN P, long n, ulong p)
     669             : {
     670       20718 :   return p==3 ? F3x_canonlift(P,n):
     671        8766 :          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       65364 : RgX_circular_shallow(GEN P, long a, long n)
     677             : {
     678       65364 :   long i, l = lgpol(P);
     679       65364 :   GEN Q = cgetg(2+n,t_POL);
     680       65364 :   Q[1] = P[1];
     681      439218 :   for(i=0; i<l; i++)
     682      373854 :     gel(Q,2+(i*a)%n) = gel(P,2+i);
     683      144606 :   for(   ; i<n; i++)
     684       79242 :     gel(Q,2+(i*a)%n) = gen_0;
     685       65364 :   return normalizepol_lg(Q,2+n);
     686             : }
     687             : 
     688             : static GEN
     689       65364 : ZpXQ_frob_cyc(GEN x, GEN T, GEN q, ulong p)
     690             : {
     691       65364 :   long n = get_FpX_degree(T);
     692       65364 :   return FpX_rem(RgX_circular_shallow(x,p,n+1), T, q);
     693             : }
     694             : 
     695             : static GEN
     696       97374 : ZpXQ_frob(GEN x, GEN Xm, GEN T, GEN q, ulong p)
     697             : {
     698       97374 :   if (lg(Xm)==1)
     699       37230 :     return ZpXQ_frob_cyc(x, T, q, p);
     700             :   else
     701             :   {
     702       60144 :     long n = get_FpX_degree(T);
     703       60144 :     GEN V = RgX_blocks(RgX_inflate(x, p), n, p);
     704       60144 :     GEN W = ZXV_dotproduct(V, Xm);
     705       60144 :     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       72054 : static GEN _lift_invl(void *E, GEN x)
     717             : {
     718       72054 :   struct _lift_lin *d = (struct _lift_lin *) E;
     719       72054 :   GEN T = d->Tp;
     720       72054 :   ulong p = d->p;
     721       72054 :   GEN xai = Flxq_mul(ZX_to_Flx(x, p), d->ai, T, p);
     722       72054 :   return Flx_to_ZX(Flxq_lroot_fast(xai, d->sqx, T, p));
     723             : }
     724             : 
     725       20352 : static GEN _lift_lin(void *E, GEN F, GEN x2, GEN q)
     726             : {
     727       20352 :   struct _lift_lin *d = (struct _lift_lin *) E;
     728       20352 :   pari_sp av = avma;
     729       20352 :   GEN T = gel(F,3), Xm = gel(F,4);
     730       20352 :   GEN y2  = ZpXQ_frob(x2, Xm, T, q, d->p);
     731       20352 :   GEN lin = FpX_add(ZX_mul(gel(F,1), y2), ZX_mul(gel(F,2), x2), q);
     732       20352 :   return gerepileupto(av, FpX_rem(lin, T, q));
     733             : }
     734             : 
     735             : static GEN
     736      155106 : FpM_FpXV_bilinear(GEN P, GEN X, GEN Y, GEN p)
     737             : {
     738      155106 :    pari_sp av = avma;
     739      155106 :    GEN s =  ZX_mul(FpXV_FpC_mul(X,gel(P,1),p),gel(Y,1));
     740      155106 :    long i, l = lg(P);
     741      728946 :    for(i=2; i<l; i++)
     742      573840 :      s = ZX_add(s, ZX_mul(FpXV_FpC_mul(X,gel(P,i),p),gel(Y,i)));
     743      155106 :    return gerepileupto(av, FpX_red(s, p));
     744             : }
     745             : 
     746             : static GEN
     747      155106 : FpM_FpXQV_bilinear(GEN P, GEN X, GEN Y, GEN T, GEN p)
     748             : {
     749      155106 :   return FpX_rem(FpM_FpXV_bilinear(P,X,Y,p),T,p);
     750             : }
     751             : 
     752             : static GEN
     753      103404 : FpXC_powderiv(GEN M, GEN p)
     754             : {
     755             :   long i, l;
     756      103404 :   long v = varn(gel(M,2));
     757      103404 :   GEN m = cgetg_copy(M, &l);
     758      103404 :   gel(m,1) = pol_0(v);
     759      103404 :   gel(m,2) = pol_1(v);
     760      382560 :   for(i=2; i<l-1; i++)
     761      279156 :     gel(m,i+1) = FpX_Fp_mul(gel(M,i),utoi(i), p);
     762      103404 :   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       51702 : _lift_iter(void *E, GEN x2, GEN q)
     775             : {
     776       51702 :   struct _lift_iso *d = (struct _lift_iso *) E;
     777       51702 :   ulong p = d->p;
     778       51702 :   long n = lg(d->phi)-2;
     779       51702 :   GEN TN = FpXT_red(d->T, q), XN = FpXV_red(d->Xm, q);
     780       51702 :   GEN y2 = ZpXQ_frob(x2, XN, TN, q, p);
     781       51702 :   GEN xp = FpXQ_powers(x2, n, TN, q);
     782       51702 :   GEN yp = FpXQ_powers(y2, n, TN, q);
     783       51702 :   GEN V  = FpM_FpXQV_bilinear(d->phi,xp,yp,TN,q);
     784       51702 :   return mkvec3(V,xp,yp);
     785             : }
     786             : 
     787             : static GEN
     788       51702 : _lift_invd(void *E, GEN V, GEN v, GEN qM, long M)
     789             : {
     790       51702 :   struct _lift_iso *d = (struct _lift_iso *) E;
     791             :   struct _lift_lin e;
     792       51702 :   ulong p = d->p;
     793       51702 :   GEN TM = FpXT_red(d->T, qM), XM = FpXV_red(d->Xm, qM);
     794       51702 :   GEN xp = FpXV_red(gel(v,2), qM);
     795       51702 :   GEN yp = FpXV_red(gel(v,3), qM);
     796       51702 :   GEN Dx = FpM_FpXQV_bilinear(d->phi, FpXC_powderiv(xp, qM), yp, TM, qM);
     797       51702 :   GEN Dy = FpM_FpXQV_bilinear(d->phi, xp, FpXC_powderiv(yp, qM), TM, qM);
     798       51702 :   GEN F = mkvec4(Dy, Dx, TM, XM);
     799       51702 :   e.ai = Flxq_inv(ZX_to_Flx(Dy,p),d->Tp,p);
     800       51702 :   e.sqx = d->sqx; e.Tp = d->Tp; e.p=p; e.Xm = XM;
     801       51702 :   return gen_ZpX_Dixon(F,V,qM,utoi(p),M,(void*) &e, _lift_lin, _lift_invl);
     802             : }
     803             : 
     804             : static GEN
     805       21468 : 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       21468 :   d.phi=phi;
     809       21468 :   d.Xm=Xm; d.T=T;
     810       21468 :   d.sqx=sqx; d.Tp=Tp; d.p=p;
     811       21468 :   return gen_ZpX_Newton(x0, utoi(p), n,(void*)&d, _lift_iter, _lift_invd);
     812             : }
     813             : 
     814             : static GEN
     815       21450 : getc2(GEN act, GEN X, GEN T, GEN q, ulong p, long N)
     816             : {
     817       21450 :   GEN A1 = RgV_to_RgX(gel(act,1),0), A2 =  RgV_to_RgX(gel(act,2),0);
     818       21450 :   long n = brent_kung_optpow(maxss(degpol(A1),degpol(A2)),2,1);
     819       21450 :   GEN xp = FpXQ_powers(X,n,T,q);
     820       21450 :   GEN P  = FpX_FpXQV_eval(A1, xp, T, q);
     821       21450 :   GEN Q  = FpX_FpXQV_eval(A2, xp, T, q);
     822       21450 :   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       28134 : ZpXQ_norm_mul(void *E, GEN x, GEN y)
     833             : {
     834       28134 :   struct _ZpXQ_norm *D = (struct _ZpXQ_norm*)E;
     835       28134 :   GEN P = gel(x,1), Q = gel(y,1);
     836       28134 :   long a = mael(x,2,1), b = mael(y,2,1);
     837       28134 :   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       19470 : ZpXQ_norm_sqr(void *E, GEN x)
     843             : {
     844       19470 :   return ZpXQ_norm_mul(E, x, x);
     845             : }
     846             : 
     847             : /* Assume T = Phi_(n) and n prime */
     848             : GEN
     849        9720 : ZpXQ_norm_pcyc(GEN x, GEN T, GEN q, GEN p)
     850             : {
     851             :   GEN z;
     852             :   struct _ZpXQ_norm D;
     853        9720 :   long d = get_FpX_degree(T);
     854        9720 :   D.T = T; D.p = q; D.n = d+1;
     855        9720 :   if (d==1) return ZX_copy(x);
     856        9720 :   z = mkvec2(x,mkvecsmall(p[2]));
     857        9720 :   z = gen_powu(z,d,(void*)&D,ZpXQ_norm_sqr,ZpXQ_norm_mul);
     858        9720 :   return gmael(z,1,2);
     859             : }
     860             : 
     861             : /* Assume T = Phi_(n) and n prime */
     862             : static GEN
     863        9516 : ZpXQ_sqrtnorm_pcyc(GEN x, GEN T, GEN q, GEN p, long e)
     864             : {
     865        9516 :   GEN z = ZpXQ_norm_pcyc(x, T, q, p);
     866        9516 :   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       11952 : ZpXQ_sqrtnorm(GEN a, GEN T, GEN q, GEN p, long e)
     872             : {
     873       11952 :   GEN s = Fp_div(FpXQ_trace(ZpXQ_log(a, T, p, e), T, q), gen_2, q);
     874       11952 :   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       25284 : _teich_invl(void *E, GEN x)
     886             : {
     887       25284 :   struct _teich_lin *d = (struct _teich_lin *) E;
     888       25284 :   ulong p = d->p;
     889       25284 :   GEN T = d->Tp;
     890       25284 :   return Flx_to_ZX(Flxq_lroot_fast(ZX_to_Flx(x, p), d->sqx, T, p));
     891             : }
     892             : 
     893             : static GEN
     894        7674 : _teich_lin(void *E, GEN F, GEN x2, GEN q)
     895             : {
     896        7674 :   struct _teich_lin *d = (struct _teich_lin *) E;
     897        7674 :   pari_sp av = avma;
     898        7674 :   GEN T = gel(F,2), Xm = gel(F,3);
     899        7674 :   GEN y2  = ZpXQ_frob(x2, Xm, T, q, d->p);
     900        7674 :   GEN lin = FpX_sub(y2, ZX_mulu(ZX_mul(gel(F,1), x2), d->p), q);
     901        7674 :   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       17610 : _teich_iter(void *E, GEN x2, GEN q)
     913             : {
     914       17610 :   struct _teich_iso *d = (struct _teich_iso *) E;
     915       17610 :   ulong p = d->p;
     916       17610 :   GEN TN = FpXT_red(d->T, q), XN = FpXV_red(d->Xm, q);
     917       17610 :   GEN y2 = ZpXQ_frob(x2, XN, TN, q, d->p);
     918       17610 :   GEN x1 = FpXQ_powu(x2, p-1, TN, q);
     919       17610 :   GEN xp = FpXQ_mul(x2, x1, TN, q);
     920       17610 :   GEN V = FpX_sub(y2,xp,q);
     921       17610 :   return mkvec2(V,x1);
     922             : }
     923             : 
     924             : static GEN
     925       17610 : _teich_invd(void *E, GEN V, GEN v, GEN qM, long M)
     926             : {
     927       17610 :   struct _teich_iso *d = (struct _teich_iso *) E;
     928             :   struct _teich_lin e;
     929       17610 :   ulong p = d->p;
     930       17610 :   GEN TM = FpXT_red(d->T, qM), XM = FpXV_red(d->Xm, qM);
     931       17610 :   GEN x1 = FpX_red(gel(v,2), qM);
     932       17610 :   GEN F = mkvec3(x1, TM, XM);
     933       17610 :   e.sqx = d->sqx; e.Tp = d->Tp; e.p=p;
     934       17610 :   return gen_ZpX_Dixon(F,V,qM,utoi(p),M,(void*) &e, _teich_lin, _teich_invl);
     935             : }
     936             : 
     937             : static GEN
     938        8766 : Teichmuller_lift(GEN x, GEN Xm, GEN T, GEN sqx, GEN Tp, ulong p, long N)
     939             : {
     940             :   struct _teich_iso d;
     941        8766 :   d.Xm = Xm; d.T = T; d.sqx = sqx; d.Tp = Tp; d.p = p;
     942        8766 :   return gen_ZpX_Newton(x,utoi(p), N,(void*)&d, _teich_iter, _teich_invd);
     943             : }
     944             : 
     945             : static GEN
     946       21468 : get_norm(GEN a4, GEN a6, GEN T, ulong p, long N)
     947             : {
     948       21468 :   long sv=T[1];
     949             :   GEN a;
     950       21468 :   if (p==3) a = gel(a4,1);
     951             :   else
     952             :   {
     953        8778 :     GEN P = mkpoln(4, pol1_Flx(sv), pol0_Flx(sv), a4, a6);
     954        8778 :     a = gel(FlxqX_powu(P,p>>1,T,p),2+p-1);
     955             :   }
     956       21468 :   return Zp_sqrtnlift(gen_1,subss(p,1),utoi(Flxq_norm(a,T,p)),utoi(p), N);
     957             : }
     958             : 
     959             : static GEN
     960       21450 : fill_pols(long n, const long *v, long m, const long *vn,
     961             :           const long *vd, GEN *act)
     962             : {
     963             :   long i, j;
     964       21450 :   long d = upowuu(n,12/(n-1));
     965       21450 :   GEN N, D, M = zeromatcopy(n+1,n+1);
     966       21450 :   gmael(M,1,n+1) = gen_1;
     967      103440 :   for(i=2;i<=n+1;i++)
     968      290454 :     for(j=i-1;j<=n;j++)
     969      208464 :       gmael(M,i,j) = mulis(powuu(d,i-2),v[j-i+1]);
     970       21450 :   N = cgetg(m+1,t_COL);
     971       21450 :   D = cgetg(m+1,t_COL);
     972      116118 :   for(i=1;i<=m;i++)
     973             :   {
     974       94668 :     gel(N,i) = stoi(*vn++);
     975       94668 :     gel(D,i) = stoi(*vd++);
     976             :   }
     977       21450 :   *act = mkmat2(N,D);
     978       21450 :   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       21450 : get_Kohel_polynomials(ulong p, GEN *act, long *dj)
     990             : {
     991       21450 :   const long mat3[] = {-1,-36,-270};
     992       21450 :   const long num3[] = {1,-483,-21141,-59049};
     993       21450 :   const long den3[] = {1,261, 4347, -6561};
     994       21450 :   const long mat5[] = {-1,-30,-315,-1300,-1575};
     995       21450 :   const long num5[] = {-1,490,20620,158750,78125};
     996       21450 :   const long den5[] = {-1,-254,-4124,-12250,3125};
     997       21450 :   const long mat7[] = {-1,-28,-322,-1904,-5915,-8624,-4018};
     998       21450 :   const long num7[] = {1,-485,-24058,-343833,-2021642,-4353013,-823543};
     999       21450 :   const long den7[] = {1,259,5894,49119,168406,166355,-16807};
    1000       21450 :   const long mat13[]= {-1,-26,-325,-2548,-13832,-54340,-157118,-333580,-509366,
    1001             :                        -534820,-354536,-124852,-15145};
    1002       21450 :   const long num13[]= {1,-487,-24056,-391463,-3396483,-18047328,-61622301,
    1003             :                        -133245853,-168395656,-95422301,-4826809};
    1004       21450 :   const long den13[]= {1,257,5896,60649,364629,1388256,3396483,5089019,4065464,
    1005             :                        1069939,-28561};
    1006       21450 :   switch(p)
    1007             :   {
    1008             :   case 3:
    1009       12690 :     *dj = 0;
    1010       12690 :     return fill_pols(3,mat3,4,num3,den3,act);
    1011             :   case 5:
    1012        8718 :     *dj = 0;
    1013        8718 :     return fill_pols(5,mat5,5,num5,den5,act);
    1014             :   case 7:
    1015          36 :     *dj = 1;
    1016          36 :     return fill_pols(7,mat7,7,num7,den7,act);
    1017             :   case 13:
    1018           6 :     *dj = 8;
    1019           6 :     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       27627 : zx_is_pcyc(GEN T)
    1026             : {
    1027       27627 :   long i, n = degpol(T);
    1028       27627 :   if (!uisprime(n+1))
    1029        9939 :     return 0;
    1030       84984 :   for (i=0; i<=n; i++)
    1031       75264 :     if (T[i+2]!=1UL)
    1032        7968 :       return 0;
    1033        9720 :   return 1;
    1034             : }
    1035             : 
    1036             : static GEN
    1037       21450 : Flxq_ellcard_Kohel(GEN a4, GEN a6, GEN T, ulong p)
    1038             : {
    1039       21450 :   pari_sp av = avma, av2;
    1040             :   pari_timer ti;
    1041       21450 :   long n = get_Flx_degree(T), N = (n+4)/2, dj;
    1042       21450 :   GEN q = powuu(p, N);
    1043             :   GEN T2, Xm, s1, c2, t, lr;
    1044             :   GEN S1, sqx;
    1045             :   GEN Nc2, Np;
    1046       21450 :   GEN act, phi = get_Kohel_polynomials(p, &act, &dj);
    1047       21450 :   long ispcyc = zx_is_pcyc(get_Flx_mod(T));
    1048       21450 :   timer_start(&ti);
    1049       21450 :   if (!ispcyc)
    1050             :   {
    1051       11940 :     T2 = Flx_canonlift(get_Flx_mod(T),N,p);
    1052       11940 :     if (DEBUGLEVEL) timer_printf(&ti,"Teich");
    1053             :   } else
    1054        9510 :     T2 = Flx_to_ZX(get_Flx_mod(T));
    1055       21450 :   T2 = FpX_get_red(T2, q); T = ZXT_to_FlxT(T2, p);
    1056       21450 :   av2 = avma;
    1057       21450 :   if (DEBUGLEVEL) timer_printf(&ti,"Barrett");
    1058       21450 :   if (!ispcyc)
    1059             :   {
    1060       11940 :     Xm = FpXQ_powers(pol_xn(n,get_FpX_var(T2)),p-1,T2,q);
    1061       11940 :     if (DEBUGLEVEL) timer_printf(&ti,"Xm");
    1062             :   } else
    1063        9510 :     Xm = cgetg(1,t_VEC);
    1064       21450 :   s1 = Flxq_inv(Flx_Fl_add(Flxq_ellj(a4,a6,T,p),dj, p),T,p);
    1065       21450 :   lr = Flxq_lroot(polx_Flx(get_Flx_var(T)), T, p);
    1066       21450 :   sqx = Flxq_powers(lr, p-1, T, p);
    1067       21450 :   S1 = lift_isogeny(phi, Flx_to_ZX(s1), N, Xm, T2, sqx, T ,p);
    1068       21450 :   if (DEBUGLEVEL) timer_printf(&ti,"Lift isogeny");
    1069       21450 :   c2 = getc2(act, S1, T2, q, p, N);
    1070       21450 :   if (DEBUGLEVEL) timer_printf(&ti,"c^2");
    1071       21450 :   if (p>3 && !ispcyc)
    1072             :   {
    1073        8754 :     GEN c2p = Flx_to_ZX(Flxq_inv(ZX_to_Flx(c2,p),T,p));
    1074        8754 :     GEN tc2 = Teichmuller_lift(c2p,Xm, T2,sqx,T,p,N);
    1075        8754 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fq");
    1076        8754 :     c2 = FpX_rem(FpX_mul(tc2,c2,q),T2,q);
    1077             :   }
    1078       21450 :   c2 = gerepileupto(av2, c2);
    1079       21450 :   if (DEBUGLEVEL) timer_printf(&ti,"tc2");
    1080       21450 :   Nc2 = (ispcyc? ZpXQ_sqrtnorm_pcyc: ZpXQ_sqrtnorm)(c2, T2, q, utoi(p), N);
    1081       21450 :   if (DEBUGLEVEL) timer_printf(&ti,"Norm");
    1082       21450 :   Np = get_norm(a4,a6,T,p,N);
    1083       21450 :   if (p>3 && ispcyc)
    1084             :   {
    1085           6 :     GEN Ncpi =  utoi(Fl_inv(umodiu(Nc2,p), p));
    1086           6 :     GEN tNc2 = Zp_sqrtnlift(gen_1, subss(p,1), Ncpi, utoi(p),N);
    1087           6 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fp");
    1088           6 :     Nc2 = Fp_mul(Nc2,tNc2,q);
    1089             :   }
    1090       21450 :   t = Fp_center(Fp_mul(Nc2,Np,q),q,shifti(q,-1));
    1091       21450 :   return gerepileupto(av, subii(addiu(powuu(p,n),1),t));
    1092             : }
    1093             : 
    1094             : static void
    1095          18 : liftcurve(GEN J, GEN T, GEN q, ulong p, long N, GEN *A4, GEN *A6)
    1096             : {
    1097          18 :   pari_sp av = avma;
    1098          18 :   GEN r = ZpXQ_inv(Z_ZX_sub(utoi(1728),J),T,utoi(p),N);
    1099          18 :   GEN g = FpXQ_mul(J,r,T,q);
    1100          18 :   *A4 = FpX_mulu(g,3,q);
    1101          18 :   *A6 = FpX_mulu(g,2,q);
    1102          18 :   gerepileall(av,2,A4,A6);
    1103          18 : }
    1104             : 
    1105             : static GEN
    1106          18 : getc5(GEN H, GEN A40, GEN A60, GEN A41, GEN A61, GEN T, GEN q, ulong p, long N)
    1107             : {
    1108          18 :   long d = lg(H)-1;
    1109          18 :   GEN s1 = gel(H,d-1), s2 = gel(H,d-2), s3 = d<5 ? pol_0(varn(T)): gel(H,d-3);
    1110          18 :   GEN s12 = FpXQ_sqr(s1,T,q);
    1111          18 :   GEN h2 = ZX_sub(ZX_shifti(s2,1),s12); /*2*s2-s1^2*/
    1112          18 :   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          18 :   GEN alpha= ZX_sub(ZX_mulu(h2,30), ZX_mulu(A40,5*p-6)); /* 30*h2+A40*(6-5*p)*/
    1115          18 :   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          18 :   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          18 :   return u2;
    1120             : }
    1121             : 
    1122             : static GEN
    1123          18 : ZpXQX_liftrootmod_vald(GEN f, GEN H, long v, GEN T, GEN p, long e)
    1124             : {
    1125          18 :   pari_sp av = avma, av2, lim;
    1126          18 :   GEN pv = p, q, qv, W, df, Tq, fr, dfr;
    1127             :   ulong mask;
    1128             :   pari_timer ti;
    1129          18 :   if (e <= v+1) return H;
    1130          18 :   df = RgX_deriv(f);
    1131          18 :   if (v) { pv = powiu(p,v); qv = mulii(pv,p); df = ZXX_Z_divexact(df, pv); }
    1132           0 :   else qv = p;
    1133          18 :   mask = quadratic_prec_mask(e-v);
    1134          18 :   Tq = FpXT_red(T, qv); dfr = FpXQX_red(df, Tq, p);
    1135          18 :   if (DEBUGLEVEL) timer_start(&ti);
    1136          18 :   W = FpXQXQ_inv(FpXQX_rem(dfr, H, Tq, p), H, Tq, p); /* 1/f'(a) mod (T,p) */
    1137          18 :   if (DEBUGLEVEL) timer_printf(&ti,"FpXQXQ_inv");
    1138          18 :   q = p;
    1139          18 :   av2 = avma; lim = stack_lim(av2, 2);
    1140             :   for (;;)
    1141             :   {
    1142             :     GEN u, fa, qv, q2v, Tq2, fadH;
    1143          66 :     GEN H2 = H, q2 = q;
    1144          66 :     q = sqri(q);
    1145          66 :     if (mask & 1) q = diviiexact(q,p);
    1146          66 :     mask >>= 1;
    1147          66 :     if (v) { qv = mulii(q, pv); q2v = mulii(q2, pv); }
    1148           0 :     else { qv = q; q2v = q2; }
    1149          66 :     Tq2 = FpXT_red(T, q2v); Tq = FpXT_red(T, qv);
    1150          66 :     fr = FpXQX_red(f, Tq, qv);
    1151          66 :     fa = FpXQX_rem(fr, H, Tq, qv);
    1152          66 :     fa = ZXX_Z_divexact(fa, q2v);
    1153          66 :     fadH = FpXQXQ_mul(RgX_deriv(H),fa,H,Tq2,q2);
    1154          66 :     H = FpXX_add(H, gmul(FpXQXQ_mul(W, fadH, H, Tq2, q2v), q2), qv);
    1155          66 :     if (mask == 1) return gerepileupto(av, H);
    1156          48 :     dfr = FpXQX_rem(FpXQX_red(df, Tq, q),H,Tq,q);
    1157          48 :     u = ZXX_Z_divexact(ZXX_Z_add_shallow(FpXQXQ_mul(W,dfr,H,Tq,q),gen_m1),q2);
    1158          48 :     W = gsub(W,gmul(FpXQXQ_mul(u,W,H2,Tq2,q2),q2));
    1159          48 :     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          48 :   }
    1165             : }
    1166             : 
    1167             : static GEN
    1168          18 : get_H1(GEN A41, GEN A61, GEN T2, ulong p)
    1169             : {
    1170          18 :   GEN q = utoi(p), T = FpXT_red(T2,q);
    1171          18 :   GEN pol = FpXQ_elldivpol(FpX_red(A41,q),FpX_red(A61,q),p,T,q);
    1172          18 :   return FpXQX_normalize(RgX_deflate(pol,p),T,q);
    1173             : }
    1174             : 
    1175             : static GEN
    1176          18 : Flxq_ellcard_Harley(GEN a4, GEN a6, GEN T, ulong p)
    1177             : {
    1178          18 :   pari_sp av = avma, av2;
    1179             :   pari_timer ti;
    1180          18 :   long n = get_Flx_degree(T), N = (n+5)/2;
    1181          18 :   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          18 :   long ispcyc = zx_is_pcyc(get_Flx_mod(T));
    1187          18 :   timer_start(&ti);
    1188          18 :   if (!ispcyc)
    1189             :   {
    1190          12 :     T2 = Flx_canonlift(get_Flx_mod(T),N,p);
    1191          12 :     if (DEBUGLEVEL) timer_printf(&ti,"Teich");
    1192             :   } else
    1193           6 :     T2 = Flx_to_ZX(get_Flx_mod(T));
    1194          18 :   T2 = FpX_get_red(T2, q); T = ZXT_to_FlxT(T2, p);
    1195          18 :   av2 = avma;
    1196          18 :   if (DEBUGLEVEL) timer_printf(&ti,"Barrett");
    1197          18 :   if (!ispcyc)
    1198             :   {
    1199          12 :     Xm = FpXQ_powers(pol_xn(n,get_FpX_var(T2)),p-1,T2,q);
    1200          12 :     if (DEBUGLEVEL) timer_printf(&ti,"Xm");
    1201             :   } else
    1202           6 :     Xm = cgetg(1,t_VEC);
    1203          18 :   if (DEBUGLEVEL) timer_printf(&ti,"Xm");
    1204          18 :   j = Flxq_ellj(a4,a6,T,p);
    1205          18 :   sqx = Flxq_powers(Flxq_lroot(polx_Flx(T[1]), T, p), p-1, T, p);
    1206          18 :   J1 = lift_isogeny(polmodular_ZM(p, 0), Flx_to_ZX(j), N, Xm, T2,sqx,T,p);
    1207          18 :   if (DEBUGLEVEL) timer_printf(&ti,"Lift isogeny");
    1208          18 :   liftcurve(J1,T2,q,p,N,&A41,&A61);
    1209          18 :   A40 = ZpXQ_frob(A41, Xm, T2, q, p);
    1210          18 :   A60 = ZpXQ_frob(A61, Xm, T2, q, p);
    1211          18 :   if (DEBUGLEVEL) timer_printf(&ti,"liftcurve");
    1212          18 :   pol = FpXQ_elldivpol(A40,A60,p,T2,q);
    1213          18 :   if (DEBUGLEVEL) timer_printf(&ti,"p-division");
    1214          18 :   h1 = get_H1(A41,A61,T2,p);
    1215          18 :   H = ZpXQX_liftrootmod_vald(pol,h1,1,T2,utoi(p),N);
    1216          18 :   q = diviuexact(q,p); N--;
    1217          18 :   if (DEBUGLEVEL) timer_printf(&ti,"kernel");
    1218          18 :   c2 = getc5(H,A40,A60,A41,A61,T2,q,p,N);
    1219          18 :   if (DEBUGLEVEL) timer_printf(&ti,"c^2");
    1220          18 :   if (!ispcyc)
    1221             :   {
    1222          12 :     c2p = Flx_to_ZX(Flxq_inv(ZX_to_Flx(c2,p),T,p));
    1223          12 :     tc2 = Teichmuller_lift(c2p,Xm, T2,sqx,T,p,N);
    1224          12 :     if (DEBUGLEVEL) timer_printf(&ti,"teichmuller");
    1225          12 :     c2 = FpX_rem(FpX_mul(tc2,c2,q),T2,q);
    1226             :   }
    1227          18 :   c2 = gerepileupto(av2, c2);
    1228          18 :   q = powuu(p, N);
    1229          18 :   Nc2 = (ispcyc? ZpXQ_sqrtnorm_pcyc: ZpXQ_sqrtnorm)(c2, T2, q, utoi(p), N);
    1230          18 :   if (DEBUGLEVEL) timer_printf(&ti,"Norm");
    1231          18 :   Np = get_norm(a4,a6,T,p,N);
    1232          18 :   if (ispcyc)
    1233             :   {
    1234           6 :     GEN Ncpi = utoi(Fl_inv(umodiu(Nc2,p), p));
    1235           6 :     GEN tNc2 = Zp_sqrtnlift(gen_1, subss(p,1), Ncpi, utoi(p), N);
    1236           6 :     if (DEBUGLEVEL) timer_printf(&ti,"Teichmuller/Fp");
    1237           6 :     Nc2 = Fp_mul(Nc2,tNc2,q);
    1238             :   }
    1239          18 :   t = Fp_center(Fp_mul(Nc2,Np,q),q,shifti(q,-1));
    1240          18 :   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        1644 : closest_lift(GEN a, GEN b, GEN h)
    1252             : {
    1253        1644 :   return addii(a, mulii(b, diviiround(subii(h,a), b)));
    1254             : }
    1255             : 
    1256             : static GEN
    1257         858 : FlxqE_find_order(GEN f, GEN h, GEN bound, GEN B, GEN a4, GEN T, ulong p)
    1258             : {
    1259         858 :   pari_sp av = avma, av1;
    1260             :   pari_timer Ti;
    1261         858 :   long s = itos( gceil(gsqrt(gdiv(bound,B),DEFAULTPREC)) ) >> 1;
    1262             :   GEN tx, ti;
    1263         858 :   GEN fh = FlxqE_mul(f, h, a4, T, p);
    1264         858 :   GEN F, P = fh, fg;
    1265             :   long i;
    1266         858 :   if (DEBUGLEVEL >= 6) timer_start(&Ti);
    1267         858 :   if (ell_is_inf(fh)) return h;
    1268         780 :   F = FlxqE_mul(f, B, a4, T, p);
    1269         780 :   if (s < 3)
    1270             :   { /* we're nearly done: naive search */
    1271          96 :     GEN Q = P;
    1272         354 :     for (i=1;; i++)
    1273             :     {
    1274         354 :       P = FlxqE_add(P, F, a4, T, p); /* h.f + i.F */
    1275         354 :       if (ell_is_inf(P)) return gerepileupto(av, addii(h, mului(i,B)));
    1276         330 :       Q = FlxqE_sub(Q, F, a4, T, p); /* h.f - i.F */
    1277         330 :       if (ell_is_inf(Q)) return gerepileupto(av, subii(h, mului(i,B)));
    1278         258 :     }
    1279             :   }
    1280         684 :   tx = cgetg(s+1,t_VECSMALL);
    1281             :   /* Baby Step/Giant Step */
    1282         684 :   av1 = avma;
    1283        3828 :   for (i=1; i<=s; i++)
    1284             :   { /* baby steps */
    1285        3288 :     tx[i] = hash_GEN(gel(P, 1));
    1286        3288 :     P = FlxqE_add(P, F, a4, T, p); /* h.f + i.F */
    1287        3288 :     if (ell_is_inf(P)) return gerepileupto(av, addii(h, mului(i,B)));
    1288        3144 :     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         540 :   if (DEBUGLEVEL >= 6) timer_printf(&Ti, "[Flxq_ellcard] baby steps, s = %ld",s);
    1295             :   /* giant steps: fg = s.F */
    1296         540 :   fg = gerepileupto(av1, FlxqE_sub(P, fh, a4, T, p));
    1297         540 :   if (ell_is_inf(fg)) return gerepileupto(av,mului(s,B));
    1298         540 :   ti = vecsmall_indexsort(tx); /* = permutation sorting tx */
    1299         540 :   tx = perm_mul(tx,ti);
    1300         540 :   if (DEBUGLEVEL >= 6) timer_printf(&Ti, "[Flxq_ellcard] sorting");
    1301         540 :   av1 = avma;
    1302        2556 :   for (P=fg, i=1; ; i++)
    1303             :   {
    1304        2556 :     long k = hash_GEN(gel(P,1));
    1305        2556 :     long r = zv_search(tx, k);
    1306        2556 :     if (r)
    1307             :     {
    1308         540 :       while (r && tx[r] == k) r--;
    1309         540 :       for (r++; r <= s && tx[r] == k; r++)
    1310             :       {
    1311         540 :         long j = ti[r]-1;
    1312         540 :         GEN Q = FlxqE_add(FlxqE_mul(F, stoi(j), a4, T, p), fh, a4, T, p);
    1313         540 :         if (DEBUGLEVEL >= 6)
    1314           0 :           timer_printf(&Ti, "[Flxq_ellcard] giant steps, i = %ld",i);
    1315         540 :         if (Flx_equal(gel(P,1), gel(Q,1)))
    1316             :         {
    1317         540 :           if (Flx_equal(gel(P,2), gel(Q,2))) i = -i;
    1318         540 :           return gerepileupto(av,addii(h, mulii(addis(mulss(s,i), j), B)));
    1319             :         }
    1320             :       }
    1321             :     }
    1322        2016 :     P = FlxqE_add(P,fg,a4,T,p);
    1323        2016 :     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        2016 :   }
    1329             : }
    1330             : 
    1331             : static void
    1332       28410 : Flx_next(GEN t, ulong p)
    1333             : {
    1334             :   long i;
    1335       34824 :   for(i=2;;i++)
    1336       34824 :     if (uel(t,i)==p-1)
    1337        6414 :       t[i]=0;
    1338             :     else
    1339             :     {
    1340       28410 :       t[i]++;
    1341       28410 :       break;
    1342        6414 :     }
    1343       28410 : }
    1344             : 
    1345             : static void
    1346       28410 : Flx_renormalize_ip(GEN x, long lx)
    1347             : {
    1348             :   long i;
    1349       34824 :   for (i = lx-1; i>=2; i--)
    1350       32898 :     if (x[i]) break;
    1351       28410 :   setlg(x, i+1);
    1352       28410 : }
    1353             : 
    1354             : static ulong
    1355        1326 : F3xq_ellcard_naive(GEN a2, GEN a6, GEN T)
    1356             : {
    1357        1326 :   pari_sp av = avma;
    1358        1326 :   long i, d = get_Flx_degree(T), lx = d+2;
    1359        1326 :   long q = upowuu(3, d), a;
    1360        1326 :   GEN x = zero_zv(lx); x[1] = get_Flx_var(T);
    1361        7392 :   for(a=1, i=0; i<q; i++)
    1362             :   {
    1363             :     GEN rhs;
    1364        6066 :     Flx_renormalize_ip(x, lx);
    1365        6066 :     rhs = Flx_add(Flxq_mul(Flxq_sqr(x, T, 3), Flx_add(x, a2, 3), T, 3), a6, 3);
    1366        6066 :     if (!lgpol(rhs)) a++; else if (Flxq_issquare(rhs, T, 3)) a+=2;
    1367        6066 :     Flx_next(x, 3);
    1368             :   }
    1369        1326 :   avma = av;
    1370        1326 :   return a;
    1371             : }
    1372             : 
    1373             : static ulong
    1374         600 : Flxq_ellcard_naive(GEN a4, GEN a6, GEN T, ulong p)
    1375             : {
    1376         600 :   pari_sp av = avma;
    1377         600 :   long i, d = get_Flx_degree(T), lx = d+2;
    1378         600 :   long q = upowuu(p, d), a;
    1379         600 :   GEN x = zero_zv(lx); x[1] = get_Flx_var(T);
    1380       22944 :   for(a=1, i=0; i<q; i++)
    1381             :   {
    1382             :     GEN x2, rhs;
    1383       22344 :     Flx_renormalize_ip(x, lx);
    1384       22344 :     x2  = Flxq_sqr(x, T, p);
    1385       22344 :     rhs = Flx_add(Flxq_mul(x, Flx_add(x2, a4, p), T, p), a6, p);
    1386       22344 :     if (!lgpol(rhs)) a++; else if (Flxq_issquare(rhs,T,p)) a+=2;
    1387       22344 :     Flx_next(x,p);
    1388             :   }
    1389         600 :   avma = av;
    1390         600 :   return a;
    1391             : }
    1392             : 
    1393             : /* assume T irreducible mod p, m = (q-1)/(p-1) */
    1394             : static int
    1395        1671 : Flxq_kronecker(GEN x, GEN m, GEN T, ulong p)
    1396             : {
    1397             :   pari_sp av;
    1398             :   ulong z;
    1399        1671 :   if (lgpol(x) == 0) return 0;
    1400        1671 :   av = avma;
    1401        1671 :   z = Flxq_pow(x, m, T, p)[2];
    1402        1671 :   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        1671 : Flxq_ellpoint(long KRO, GEN a4, GEN a6, GEN m, long n, long vn, GEN T, ulong p)
    1409             : {
    1410             :   for(;;)
    1411             :   {
    1412        1671 :     GEN x = random_Flx(n,vn,p);
    1413        1671 :     GEN u = Flx_add(a6, Flxq_mul(Flx_add(a4, Flxq_sqr(x,T,p), p), x, T,p), p);
    1414        1671 :     if (Flxq_kronecker(u, m,T,p) == KRO)
    1415        1716 :       return mkvec2(Flxq_mul(u,x, T,p), Flxq_sqr(u, T,p));
    1416         813 :   }
    1417             : }
    1418             : 
    1419             : static GEN
    1420         786 : Flxq_ellcard_Shanks(GEN a4, GEN a6, GEN q, GEN T, ulong p)
    1421             : {
    1422         786 :   pari_sp av = avma;
    1423         786 :   long vn = get_Flx_var(T), n = get_Flx_degree(T), KRO = -1;
    1424             :   GEN h,f, ta4, A, B, m;
    1425         786 :   GEN q1p = addiu(q,1), q2p = shifti(q1p, 1);
    1426         786 :   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         786 :   switch(FlxqX_nbroots(mkpoln(4, pol1_Flx(vn), pol0_Flx(vn), a4, a6), T, p))
    1430             :   {
    1431         156 :   case 3:  A = gen_0; B = utoipos(4); break;
    1432         294 :   case 1:  A = gen_0; B = gen_2; break;
    1433         336 :   default: A = gen_1; B = gen_2; break; /* 0 */
    1434             :   }
    1435         786 :   m = diviuexact(subiu(powuu(p,n), 1), p-1);
    1436             :   for(;;)
    1437             :   {
    1438         858 :     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         858 :     KRO = -KRO;
    1445         858 :     f = Flxq_ellpoint(KRO, a4,a6, m,n,vn, T,p);
    1446             : 
    1447         858 :     ta4 = Flxq_mul(a4, gel(f,2), T, p); /* a4 for E_u */
    1448         858 :     h = FlxqE_find_order(f, h, bound, B, ta4,T,p);
    1449         858 :     h = FlxqE_order(f, h, ta4, T, p);
    1450             :     /* h | #E_u(Flxq) = A (mod B) */
    1451         858 :     A = Z_chinese_all(A, gen_0, B, h, &B);
    1452         858 :     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          72 :     A = remii(subii(q2p,A), B); /* #E(Fq)+#E'(Fq) = 2q+2 */
    1456          72 :   }
    1457         786 :   h = closest_lift(A, B, q1p);
    1458         786 :   return gerepileuptoint(av, KRO == 1? h: subii(q2p,h));
    1459             : }
    1460             : 
    1461             : static GEN
    1462       14016 : F3xq_ellcard(GEN a2, GEN a6, GEN T)
    1463             : {
    1464       14016 :   long n = get_Flx_degree(T);
    1465       14016 :   if (n <= 2)
    1466        1056 :     return utoi(F3xq_ellcard_naive(a2, a6, T));
    1467             :   else
    1468             :   {
    1469       12960 :     GEN q1 = addiu(powuu(3, get_Flx_degree(T)), 1), t;
    1470       12960 :     GEN a = Flxq_div(a6,Flxq_powu(a2,3,T,3),T,3);
    1471       12960 :     if (Flx_equal1(Flxq_powu(a, 8, T, 3)))
    1472             :     {
    1473         270 :       GEN P = Flxq_minpoly(a,T,3);
    1474         270 :       long dP = degpol(P); /* dP <= 2 */
    1475         270 :       ulong q = upowuu(3,dP);
    1476         270 :       GEN A2 = pol1_Flx(P[1]), A6 = Flx_rem(polx_Flx(P[1]), P, 3);
    1477         270 :       long tP = q + 1 - F3xq_ellcard_naive(A2, A6, P);
    1478         270 :       t = elltrace_extension(stoi(tP), n/dP, utoi(q));
    1479         270 :       if (umodiu(t, 3)!=1) t = negi(t);
    1480         270 :       return Flx_equal1(a2) || Flxq_issquare(a2,T,3) ? subii(q1,t): addii(q1,t);
    1481             :     }
    1482       12690 :     else return Flxq_ellcard_Kohel(mkvec(a2), a6, T, 3);
    1483             :   }
    1484             : }
    1485             : 
    1486             : static GEN
    1487        9378 : Flxq_ellcard_Satoh(GEN a4, GEN a6, GEN j, GEN T, ulong p)
    1488             : {
    1489        9378 :   long n = get_Flx_degree(T);
    1490        9378 :   if (n <= 2)
    1491         360 :     return utoi(Flxq_ellcard_naive(a4, a6, T, p));
    1492             :   else
    1493             :   {
    1494        9018 :     GEN jp = Flxq_powu(j, p, T, p);
    1495        9018 :     GEN s = Flx_add(j, jp, p);
    1496        9018 :     if (degpol(s) <= 0)
    1497             :     { /* it is assumed j not in F_p */
    1498         240 :       GEN m = Flxq_mul(j, jp, T, p);
    1499         240 :       if (degpol(m) <= 0)
    1500             :       {
    1501         240 :         GEN q = sqru(p);
    1502         240 :         GEN q1 = addiu(powuu(p, get_Flx_degree(T)), 1);
    1503         240 :         GEN sk = Flx_Fl_add(Flx_neg(j, p), 1728%p, p);
    1504         240 :         GEN sA4 = Flx_triple(Flxq_mul(sk, j, T, p), p);
    1505         240 :         GEN u = Flxq_div(a4, sA4, T, p);
    1506         240 :         ulong ns = lgpol(s) ? Fl_neg(s[2], p): 0UL;
    1507         240 :         GEN P = mkvecsmall4(T[1], m[2], ns, 1L);
    1508             :         GEN A4, A6, t, tP;
    1509         240 :         Flxq_ellj_to_a4a6(polx_Flx(T[1]), P, p, &A4, &A6);
    1510         240 :         tP = addis(q, 1 - Flxq_ellcard_naive(A4, A6, P, p));
    1511         240 :         t = elltrace_extension(tP, n>>1, q);
    1512         240 :         return Flxq_is2npower(u, 2, T, p) ? subii(q1,t): addii(q1,t);
    1513             :       }
    1514             :     }
    1515        8778 :     if (p<=7 || p==13 ) return Flxq_ellcard_Kohel(a4, a6, T, p);
    1516          18 :     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       44334 : Flxq_ellj(GEN a4, GEN a6, GEN T, ulong p)
    1537             : {
    1538       44334 :   pari_sp av=avma;
    1539       44334 :   if (p==3)
    1540             :   {
    1541             :     GEN J;
    1542       12690 :     if (typ(a4)!=t_VEC) return pol0_Flx(get_Flx_var(T));
    1543       12690 :     J = Flxq_div(Flxq_powu(gel(a4,1),3, T, p),Flx_neg(a6,p), T, p);
    1544       12690 :     return gerepileuptoleaf(av, J);
    1545             :   }
    1546             :   else
    1547             :   {
    1548       31644 :     pari_sp av=avma;
    1549       31644 :     GEN a43 = Flxq_mul(a4,Flxq_sqr(a4,T,p),T,p);
    1550       31644 :     GEN a62 = Flxq_sqr(a6,T,p);
    1551       31644 :     GEN num = Flx_mulu(a43,6912,p);
    1552       31644 :     GEN den = Flx_add(Flx_mulu(a43,4,p),Flx_mulu(a62,27,p),p);
    1553       31644 :     return gerepileuptoleaf(av, Flxq_div(num, den, T, p));
    1554             :   }
    1555             : }
    1556             : 
    1557             : void
    1558         240 : Flxq_ellj_to_a4a6(GEN j, GEN T, ulong p, GEN *pt_a4, GEN *pt_a6)
    1559             : {
    1560         240 :   ulong zagier = 1728 % p;
    1561         240 :   if (lgpol(j)==0)
    1562           0 :     { *pt_a4 = pol0_Flx(T[1]); *pt_a6 =pol1_Flx(T[1]); }
    1563         240 :   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         240 :     GEN k = Flx_Fl_add(Flx_neg(j, p), zagier, p);
    1568         240 :     GEN kj = Flxq_mul(k, j, T, p);
    1569         240 :     GEN k2j = Flxq_mul(kj, k, T, p);
    1570         240 :     *pt_a4 = Flx_triple(kj, p);
    1571         240 :     *pt_a6 = Flx_double(k2j, p);
    1572             :   }
    1573         240 : }
    1574             : 
    1575             : static GEN
    1576        5166 : F3xq_ellcardj(GEN a4, GEN a6, GEN T, GEN q, long n)
    1577             : {
    1578        5166 :   const ulong p = 3;
    1579             :   ulong t;
    1580        5166 :   GEN q1 = addiu(q,1);
    1581        5166 :   GEN na4 = Flx_neg(a4,p), ra4;
    1582        5166 :   if (!Flxq_issquare(na4,T,p))
    1583        2514 :     return q1;
    1584        2652 :   ra4 = Flxq_sqrt(na4,T,p);
    1585        2652 :   t = Flxq_trace(Flxq_div(a6,Flxq_mul(na4,ra4,T,p),T,p),T,p);
    1586        2652 :   if (n%2==1)
    1587             :   {
    1588             :     GEN q3;
    1589         780 :     if (t==0) return q1;
    1590         144 :     q3 = powuu(p,(n+1)>>1);
    1591         144 :     return (t==1)^(n%4==1) ? subii(q1,q3): addii(q1,q3);
    1592             :   }
    1593             :   else
    1594             :   {
    1595        1872 :     GEN q22, q2 = powuu(p,n>>1);
    1596        1872 :     GEN W = Flxq_pow(a4,shifti(q,-2),T,p);
    1597        1872 :     long s = (W[2]==1)^(n%4==2);
    1598        1872 :     if (t!=0) return s ? addii(q1,q2): subii(q1, q2);
    1599        1872 :     q22 = shifti(q2,1);
    1600        1872 :     return s ? subii(q1,q22):  addii(q1, q22);
    1601             :   }
    1602             : }
    1603             : 
    1604             : static GEN
    1605       12678 : Flxq_ellcardj(GEN a4, GEN a6, ulong j, GEN T, GEN q, ulong p, long n)
    1606             : {
    1607       12678 :   GEN q1 = addiu(q,1);
    1608       12678 :   if (j==0)
    1609             :   {
    1610             :     ulong w;
    1611             :     GEN W, t, N;
    1612        4794 :     if (umodiu(q,6)!=1) return q1;
    1613        3594 :     N = Fp_ffellcard(gen_0,gen_1,q,n,utoi(p));
    1614        3594 :     t = subii(q1, N);
    1615        3594 :     W = Flxq_pow(a6,diviuexact(shifti(q,-1), 3),T,p);
    1616        3594 :     if (degpol(W)>0) /*p=5 mod 6*/
    1617        1188 :       return Flx_equal1(Flxq_powu(W,3,T,p)) ? addii(q1,shifti(t,-1)):
    1618         396 :                                               subii(q1,shifti(t,-1));
    1619        2802 :     w = W[2];
    1620        2802 :     if (w==1)   return N;
    1621        2160 :     if (w==p-1) return addii(q1,t);
    1622             :     else /*p=1 mod 6*/
    1623             :     {
    1624        1512 :       GEN u = shifti(t,-1), v = sqrtint(diviuexact(subii(q,sqri(u)),3));
    1625        1512 :       GEN a = addii(u,v), b = shifti(v,1);
    1626        1512 :       if (Fl_powu(w,3,p)==1)
    1627             :       {
    1628         756 :         if (Fl_add(umodiu(a,p),Fl_mul(w,umodiu(b,p),p),p)==0)
    1629         426 :           return subii(q1,subii(shifti(b,1),a));
    1630             :         else
    1631         330 :           return addii(q1,addii(a,b));
    1632             :       }
    1633             :       else
    1634             :       {
    1635         756 :         if (Fl_sub(umodiu(a,p),Fl_mul(w,umodiu(b,p),p),p)==0)
    1636         426 :           return subii(q1,subii(a,shifti(b,1)));
    1637             :         else
    1638         330 :           return subii(q1,addii(a,b));
    1639             :       }
    1640             :     }
    1641        7884 :   } else if (j==1728%p)
    1642             :   {
    1643             :     ulong w;
    1644             :     GEN W, N, t;
    1645        4812 :     if (mod4(q)==3) return q1;
    1646        3612 :     W = Flxq_pow(a4,shifti(q,-2),T,p);
    1647        3612 :     if (degpol(W)>0) return q1; /*p=3 mod 4*/
    1648        3072 :     w = W[2];
    1649        3072 :     N = Fp_ffellcard(gen_1,gen_0,q,n,utoi(p));
    1650        3072 :     if(w==1) return N;
    1651        2136 :     t = subii(q1, N);
    1652        2136 :     if(w==p-1) return addii(q1, t);
    1653             :     else /*p=1 mod 4*/
    1654             :     {
    1655        1188 :       GEN u = shifti(t,-1), v = sqrtint(subii(q,sqri(u)));
    1656        1188 :       if (Fl_add(umodiu(u,p),Fl_mul(w,umodiu(v,p),p),p)==0)
    1657         594 :         return subii(q1,shifti(v,1));
    1658             :       else
    1659         594 :         return addii(q1,shifti(v,1));
    1660             :     }
    1661             :   } else
    1662             :   {
    1663        3072 :     ulong g = Fl_div(j, Fl_sub(1728%p, j, p), p);
    1664        3072 :     GEN l = Flxq_div(Flx_triple(a6,p),Flx_double(a4,p),T,p);
    1665        3072 :     GEN N = Fp_ffellcard(utoi(Fl_triple(g,p)),utoi(Fl_double(g,p)),q,n,utoi(p));
    1666        3072 :     if (Flxq_issquare(l,T,p)) return N;
    1667        1872 :     return subii(shifti(q1,1),N);
    1668             :   }
    1669             : }
    1670             : 
    1671             : GEN
    1672       42234 : Flxq_ellcard(GEN a4, GEN a6, GEN T, ulong p)
    1673             : {
    1674       42234 :   pari_sp av = avma;
    1675       42234 :   long n = get_Flx_degree(T);
    1676       42234 :   GEN J, r, q = powuu(p,  n);
    1677       42234 :   if (typ(a4)==t_VEC)
    1678       14016 :     r = F3xq_ellcard(gel(a4,1), a6, T);
    1679       28218 :   else if (p==3)
    1680        5166 :     r = F3xq_ellcardj(a4, a6, T, q, n);
    1681       23052 :   else if (degpol(a4)<=0 && degpol(a6)<=0)
    1682         186 :     r = Fp_ffellcard(utoi(Flx_eval(a4,0,p)),utoi(Flx_eval(a6,0,p)),q,n,utoi(p));
    1683       22866 :   else if (degpol(J=Flxq_ellj(a4,a6,T,p))<=0)
    1684       12678 :     r = Flxq_ellcardj(a4,a6,lgpol(J)?J[2]:0,T,q,p,n);
    1685       10188 :   else if (p <= 7)
    1686        9312 :     r = Flxq_ellcard_Satoh(a4, a6, J, T, p);
    1687         876 :   else if (cmpis(q,100)<0)
    1688           0 :     r = utoi(Flxq_ellcard_naive(a4, a6, T, p));
    1689         876 :   else if (p == 13 || (7*p <= (ulong)10*n && (BITS_IN_LONG==64 || p <= 103)))
    1690          66 :     r = Flxq_ellcard_Satoh(a4, a6, J, T, p);
    1691         810 :   else if (p <= (ulong)2*n)
    1692           0 :     r = Flxq_ellcard_Kedlaya(a4, a6, T, p);
    1693         810 :   else if (expi(q)<=62)
    1694         786 :     r = Flxq_ellcard_Shanks(a4, a6, q, T, p);
    1695             :   else
    1696          24 :     r = Fq_ellcard_SEA(Flx_to_ZX(a4),Flx_to_ZX(a6),q,Flx_to_ZX(T),utoi(p),0);
    1697       42234 :   return gerepileuptoint(av, r);
    1698             : }

Generated by: LCOV version 1.11