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 - FpE.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20459-9710128) Lines: 761 992 76.7 %
Date: 2017-03-30 05:32:39 Functions: 92 104 88.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2009  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 Fp */
      18             : 
      19             : /***********************************************************************/
      20             : /**                                                                   **/
      21             : /**                              FpE                                  **/
      22             : /**                                                                   **/
      23             : /***********************************************************************/
      24             : 
      25             : /* These functions deal with point over elliptic curves over Fp defined
      26             :  * by an equation of the form y^2=x^3+a4*x+a6.
      27             :  * Most of the time a6 is omitted since it can be recovered from any point
      28             :  * on the curve.
      29             :  */
      30             : 
      31             : GEN
      32        1150 : RgE_to_FpE(GEN x, GEN p)
      33             : {
      34        1150 :   if (ell_is_inf(x)) return x;
      35        1149 :   retmkvec2(Rg_to_Fp(gel(x,1),p),Rg_to_Fp(gel(x,2),p));
      36             : }
      37             : 
      38             : GEN
      39         426 : FpE_to_mod(GEN x, GEN p)
      40             : {
      41         426 :   if (ell_is_inf(x)) return x;
      42         372 :   retmkvec2(Fp_to_mod(gel(x,1),p),Fp_to_mod(gel(x,2),p));
      43             : }
      44             : 
      45             : GEN
      46        1002 : FpE_changepoint(GEN x, GEN ch, GEN p)
      47             : {
      48        1002 :   pari_sp av = avma;
      49             :   GEN p1,z,u,r,s,t,v,v2,v3;
      50        1002 :   if (ell_is_inf(x)) return x;
      51         948 :   u = gel(ch,1); r = gel(ch,2);
      52         948 :   s = gel(ch,3); t = gel(ch,4);
      53         948 :   v = Fp_inv(u, p); v2 = Fp_sqr(v,p); v3 = Fp_mul(v,v2,p);
      54         948 :   p1 = Fp_sub(gel(x,1),r,p);
      55         948 :   z = cgetg(3,t_VEC);
      56         948 :   gel(z,1) = Fp_mul(v2, p1, p);
      57         948 :   gel(z,2) = Fp_mul(v3, Fp_sub(gel(x,2), Fp_add(Fp_mul(s,p1, p),t, p),p),p);
      58         948 :   return gerepileupto(av, z);
      59             : }
      60             : 
      61             : GEN
      62        1919 : FpE_changepointinv(GEN x, GEN ch, GEN p)
      63             : {
      64             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
      65        1919 :   if (ell_is_inf(x)) return x;
      66        1919 :   X = gel(x,1); Y = gel(x,2);
      67        1919 :   u = gel(ch,1); r = gel(ch,2);
      68        1919 :   s = gel(ch,3); t = gel(ch,4);
      69        1919 :   u2 = Fp_sqr(u, p); u3 = Fp_mul(u,u2,p);
      70        1923 :   u2X = Fp_mul(u2,X, p);
      71        1920 :   z = cgetg(3, t_VEC);
      72        1920 :   gel(z,1) = Fp_add(u2X,r,p);
      73        1921 :   gel(z,2) = Fp_add(Fp_mul(u3,Y,p), Fp_add(Fp_mul(s,u2X,p), t, p), p);
      74        1923 :   return z;
      75             : }
      76             : 
      77             : static GEN
      78         360 : nonsquare_Fp(GEN p)
      79             : {
      80         360 :   pari_sp av = avma;
      81             :   GEN a;
      82             :   do
      83             :   {
      84         714 :     avma = av;
      85         714 :     a = randomi(p);
      86         714 :   } while (kronecker(a, p) >= 0);
      87         360 :   return a;
      88             : }
      89             : 
      90             : void
      91           0 : Fp_elltwist(GEN a4, GEN a6, GEN p, GEN *pt_a4, GEN *pt_a6)
      92             : {
      93           0 :   GEN d = nonsquare_Fp(p), d2 = Fp_sqr(d, p), d3 = Fp_mul(d2, d, p);
      94           0 :   *pt_a4 = Fp_mul(a4, d2, p);
      95           0 :   *pt_a6 = Fp_mul(a6, d3, p);
      96           0 : }
      97             : 
      98             : static GEN
      99     1936074 : FpE_dbl_slope(GEN P, GEN a4, GEN p, GEN *slope)
     100             : {
     101             :   GEN x, y, Q;
     102     1936074 :   if (ell_is_inf(P) || !signe(gel(P,2))) return ellinf();
     103     1922622 :   x = gel(P,1); y = gel(P,2);
     104     1922622 :   *slope = Fp_div(Fp_add(Fp_mulu(Fp_sqr(x,p), 3, p), a4, p),
     105             :                   Fp_mulu(y, 2, p), p);
     106     1897048 :   Q = cgetg(3,t_VEC);
     107     1895875 :   gel(Q, 1) = Fp_sub(Fp_sqr(*slope, p), Fp_mulu(x, 2, p), p);
     108     1910543 :   gel(Q, 2) = Fp_sub(Fp_mul(*slope, Fp_sub(x, gel(Q, 1), p), p), y, p);
     109     1910834 :   return Q;
     110             : }
     111             : 
     112             : GEN
     113     1924704 : FpE_dbl(GEN P, GEN a4, GEN p)
     114             : {
     115     1924704 :   pari_sp av = avma;
     116             :   GEN slope;
     117     1924704 :   return gerepileupto(av, FpE_dbl_slope(P,a4,p,&slope));
     118             : }
     119             : 
     120             : static GEN
     121     1103895 : FpE_add_slope(GEN P, GEN Q, GEN a4, GEN p, GEN *slope)
     122             : {
     123             :   GEN Px, Py, Qx, Qy, R;
     124     1103895 :   if (ell_is_inf(P)) return Q;
     125     1102717 :   if (ell_is_inf(Q)) return P;
     126     1102816 :   Px = gel(P,1); Py = gel(P,2);
     127     1102816 :   Qx = gel(Q,1); Qy = gel(Q,2);
     128     1102816 :   if (equalii(Px, Qx))
     129             :   {
     130        5475 :     if (equalii(Py, Qy))
     131         432 :       return FpE_dbl_slope(P, a4, p, slope);
     132             :     else
     133        5043 :       return ellinf();
     134             :   }
     135     1097133 :   *slope = Fp_div(Fp_sub(Py, Qy, p), Fp_sub(Px, Qx, p), p);
     136     1096930 :   R = cgetg(3,t_VEC);
     137     1096999 :   gel(R, 1) = Fp_sub(Fp_sub(Fp_sqr(*slope, p), Px, p), Qx, p);
     138     1097127 :   gel(R, 2) = Fp_sub(Fp_mul(*slope, Fp_sub(Px, gel(R, 1), p), p), Py, p);
     139     1097204 :   return R;
     140             : }
     141             : 
     142             : GEN
     143     1101471 : FpE_add(GEN P, GEN Q, GEN a4, GEN p)
     144             : {
     145     1101471 :   pari_sp av = avma;
     146             :   GEN slope;
     147     1101471 :   return gerepileupto(av, FpE_add_slope(P,Q,a4,p,&slope));
     148             : }
     149             : 
     150             : static GEN
     151           0 : FpE_neg_i(GEN P, GEN p)
     152             : {
     153           0 :   if (ell_is_inf(P)) return P;
     154           0 :   return mkvec2(gel(P,1), Fp_neg(gel(P,2), p));
     155             : }
     156             : 
     157             : GEN
     158      319290 : FpE_neg(GEN P, GEN p)
     159             : {
     160      319290 :   if (ell_is_inf(P)) return ellinf();
     161      319290 :   return mkvec2(gcopy(gel(P,1)), Fp_neg(gel(P,2), p));
     162             : }
     163             : 
     164             : GEN
     165           0 : FpE_sub(GEN P, GEN Q, GEN a4, GEN p)
     166             : {
     167           0 :   pari_sp av = avma;
     168             :   GEN slope;
     169           0 :   return gerepileupto(av, FpE_add_slope(P, FpE_neg_i(Q, p), a4, p, &slope));
     170             : }
     171             : 
     172             : struct _FpE
     173             : {
     174             :   GEN a4,a6;
     175             :   GEN p;
     176             : };
     177             : 
     178             : static GEN
     179     1926447 : _FpE_dbl(void *E, GEN P)
     180             : {
     181     1926447 :   struct _FpE *ell = (struct _FpE *) E;
     182     1926447 :   return FpE_dbl(P, ell->a4, ell->p);
     183             : }
     184             : 
     185             : static GEN
     186     1101462 : _FpE_add(void *E, GEN P, GEN Q)
     187             : {
     188     1101462 :   struct _FpE *ell=(struct _FpE *) E;
     189     1101462 :   return FpE_add(P, Q, ell->a4, ell->p);
     190             : }
     191             : 
     192             : static GEN
     193      415100 : _FpE_mul(void *E, GEN P, GEN n)
     194             : {
     195      415100 :   pari_sp av = avma;
     196      415100 :   struct _FpE *e=(struct _FpE *) E;
     197      415100 :   long s = signe(n);
     198      415100 :   if (!s || ell_is_inf(P)) return ellinf();
     199      415071 :   if (s<0) P = FpE_neg(P, e->p);
     200      415071 :   if (is_pm1(n)) return s>0? gcopy(P): P;
     201       74641 :   return gerepileupto(av, gen_pow(P, n, e, &_FpE_dbl, &_FpE_add));
     202             : }
     203             : 
     204             : GEN
     205         446 : FpE_mul(GEN P, GEN n, GEN a4, GEN p)
     206             : {
     207             :   struct _FpE E;
     208         446 :   E.a4= a4; E.p = p;
     209         446 :   return _FpE_mul(&E, P, n);
     210             : }
     211             : 
     212             : /* Finds a random non-singular point on E */
     213             : 
     214             : GEN
     215       24437 : random_FpE(GEN a4, GEN a6, GEN p)
     216             : {
     217       24437 :   pari_sp ltop = avma;
     218             :   GEN x, x2, y, rhs;
     219             :   do
     220             :   {
     221       43379 :     avma= ltop;
     222       43379 :     x   = randomi(p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
     223       43379 :     x2  = Fp_sqr(x, p);
     224       43379 :     rhs = Fp_add(Fp_mul(x, Fp_add(x2, a4, p), p), a6, p);
     225       50829 :   } while ((!signe(rhs) && !signe(Fp_add(Fp_mulu(x2,3,p),a4,p)))
     226       86758 :           || kronecker(rhs, p) < 0);
     227       24437 :   y = Fp_sqrt(rhs, p);
     228       24437 :   if (!y) pari_err_PRIME("random_FpE", p);
     229       24437 :   return gerepilecopy(ltop, mkvec2(x, y));
     230             : }
     231             : 
     232             : static GEN
     233       24413 : _FpE_rand(void *E)
     234             : {
     235       24413 :   struct _FpE *e=(struct _FpE *) E;
     236       24413 :   return random_FpE(e->a4, e->a6, e->p);
     237             : }
     238             : 
     239             : static const struct bb_group FpE_group={_FpE_add,_FpE_mul,_FpE_rand,hash_GEN,ZV_equal,ell_is_inf,NULL};
     240             : 
     241             : const struct bb_group *
     242         720 : get_FpE_group(void ** pt_E, GEN a4, GEN a6, GEN p)
     243             : {
     244         720 :   struct _FpE *e = (struct _FpE *) stack_malloc(sizeof(struct _FpE));
     245         720 :   e->a4 = a4; e->a6 = a6; e->p  = p;
     246         720 :   *pt_E = (void *) e;
     247         720 :   return &FpE_group;
     248             : }
     249             : 
     250             : GEN
     251         636 : FpE_order(GEN z, GEN o, GEN a4, GEN p)
     252             : {
     253         636 :   pari_sp av = avma;
     254             :   struct _FpE e;
     255             :   GEN r;
     256         636 :   if (lgefint(p) == 3)
     257             :   {
     258         636 :     ulong pp = p[2];
     259         636 :     r = Fle_order(ZV_to_Flv(z, pp), o, umodiu(a4,pp), pp);
     260             :   }
     261             :   else
     262             :   {
     263           0 :     e.a4 = a4;
     264           0 :     e.p = p;
     265           0 :     r = gen_order(z, o, (void*)&e, &FpE_group);
     266             :   }
     267         636 :   return gerepileuptoint(av, r);
     268             : }
     269             : 
     270             : GEN
     271          36 : FpE_log(GEN a, GEN b, GEN o, GEN a4, GEN p)
     272             : {
     273          36 :   pari_sp av = avma;
     274             :   struct _FpE e;
     275             :   GEN r;
     276          36 :   if (lgefint(p) == 3)
     277             :   {
     278          36 :     ulong pp = p[2];
     279          36 :     r = Fle_log(ZV_to_Flv(a,pp), ZV_to_Flv(b,pp), o, umodiu(a4,pp), pp);
     280             :   }
     281             :   else
     282             :   {
     283           0 :     e.a4 = a4;
     284           0 :     e.p = p;
     285           0 :     r = gen_PH_log(a, b, o, (void*)&e, &FpE_group);
     286             :   }
     287          36 :   return gerepileuptoint(av, r);
     288             : }
     289             : 
     290             : /***********************************************************************/
     291             : /**                                                                   **/
     292             : /**                            Pairings                               **/
     293             : /**                                                                   **/
     294             : /***********************************************************************/
     295             : 
     296             : /* Derived from APIP from and by Jerome Milan, 2012 */
     297             : 
     298             : static GEN
     299       44472 : FpE_vert(GEN P, GEN Q, GEN a4, GEN p)
     300             : {
     301       44472 :   if (ell_is_inf(P))
     302       15861 :     return gen_1;
     303       28611 :   if (!equalii(gel(Q, 1), gel(P, 1)))
     304       26677 :     return Fp_sub(gel(Q, 1), gel(P, 1), p);
     305        1934 :   if (signe(gel(P,2))!=0) return gen_1;
     306        1682 :   return Fp_inv(Fp_add(Fp_mulu(Fp_sqr(gel(P,1),p), 3, p), a4, p), p);
     307             : }
     308             : 
     309             : static GEN
     310       15421 : FpE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN p)
     311             : {
     312       15421 :   GEN x = gel(Q, 1), y = gel(Q, 2);
     313       15421 :   GEN tmp1 = Fp_sub(x, gel(R, 1), p);
     314       15421 :   GEN tmp2 = Fp_add(Fp_mul(tmp1, slope, p), gel(R,2), p);
     315       15421 :   if (!equalii(y, tmp2))
     316       14394 :     return Fp_sub(y, tmp2, p);
     317        1027 :   if (signe(y) == 0)
     318         829 :     return gen_1;
     319             :   else
     320             :   {
     321             :     GEN s1, s2;
     322         198 :     GEN y2i = Fp_inv(Fp_mulu(y, 2, p), p);
     323         198 :     s1 = Fp_mul(Fp_add(Fp_mulu(Fp_sqr(x, p), 3, p), a4, p), y2i, p);
     324         198 :     if (!equalii(s1, slope))
     325         114 :       return Fp_sub(s1, slope, p);
     326          84 :     s2 = Fp_mul(Fp_sub(Fp_mulu(x, 3, p), Fp_sqr(s1, p), p), y2i, p);
     327          84 :     return signe(s2)!=0 ? s2: y2i;
     328             :   }
     329             : }
     330             : 
     331             : /* Computes the equation of the line tangent to R and returns its
     332             :    evaluation at the point Q. Also doubles the point R.
     333             :  */
     334             : 
     335             : static GEN
     336       27238 : FpE_tangent_update(GEN R, GEN Q, GEN a4, GEN p, GEN *pt_R)
     337             : {
     338       27238 :   if (ell_is_inf(R))
     339             :   {
     340        3139 :     *pt_R = ellinf();
     341        3139 :     return gen_1;
     342             :   }
     343       24099 :   else if (signe(gel(R,2)) == 0)
     344             :   {
     345       11096 :     *pt_R = ellinf();
     346       11096 :     return FpE_vert(R, Q, a4, p);
     347             :   } else {
     348             :     GEN slope;
     349       13003 :     *pt_R = FpE_dbl_slope(R, a4, p, &slope);
     350       13003 :     return FpE_Miller_line(R, Q, slope, a4, p);
     351             :   }
     352             : }
     353             : 
     354             : /* Computes the equation of the line through R and P, and returns its
     355             :    evaluation at the point Q. Also adds P to the point R.
     356             :  */
     357             : 
     358             : static GEN
     359        4278 : FpE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN p, GEN *pt_R)
     360             : {
     361        4278 :   if (ell_is_inf(R))
     362             :   {
     363         234 :     *pt_R = gcopy(P);
     364         234 :     return FpE_vert(P, Q, a4, p);
     365             :   }
     366        4044 :   else if (ell_is_inf(P))
     367             :   {
     368           0 :     *pt_R = gcopy(R);
     369           0 :     return FpE_vert(R, Q, a4, p);
     370             :   }
     371        4044 :   else if (equalii(gel(P, 1), gel(R, 1)))
     372             :   {
     373        1626 :     if (equalii(gel(P, 2), gel(R, 2)))
     374           0 :       return FpE_tangent_update(R, Q, a4, p, pt_R);
     375             :     else {
     376        1626 :       *pt_R = ellinf();
     377        1626 :       return FpE_vert(R, Q, a4, p);
     378             :     }
     379             :   } else {
     380             :     GEN slope;
     381        2418 :     *pt_R = FpE_add_slope(P, R, a4, p, &slope);
     382        2418 :     return FpE_Miller_line(R, Q, slope, a4, p);
     383             :   }
     384             : }
     385             : 
     386             : /* Returns the Miller function f_{m, Q} evaluated at the point P using
     387             :    the standard Miller algorithm.
     388             :  */
     389             : 
     390             : struct _FpE_miller
     391             : {
     392             :   GEN p, a4, P;
     393             : };
     394             : 
     395             : static GEN
     396       27238 : FpE_Miller_dbl(void* E, GEN d)
     397             : {
     398       27238 :   struct _FpE_miller *m = (struct _FpE_miller *)E;
     399       27238 :   GEN p = m->p, a4 = m->a4, P = m->P;
     400             :   GEN v, line;
     401       27238 :   GEN num = Fp_sqr(gel(d,1), p);
     402       27238 :   GEN denom = Fp_sqr(gel(d,2), p);
     403       27238 :   GEN point = gel(d,3);
     404       27238 :   line = FpE_tangent_update(point, P, a4, p, &point);
     405       27238 :   num  = Fp_mul(num, line, p);
     406       27238 :   v = FpE_vert(point, P, a4, p);
     407       27238 :   denom = Fp_mul(denom, v, p);
     408       27238 :   return mkvec3(num, denom, point);
     409             : }
     410             : 
     411             : static GEN
     412        4278 : FpE_Miller_add(void* E, GEN va, GEN vb)
     413             : {
     414        4278 :   struct _FpE_miller *m = (struct _FpE_miller *)E;
     415        4278 :   GEN p = m->p, a4= m->a4, P = m->P;
     416             :   GEN v, line, point;
     417        4278 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
     418        4278 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
     419        4278 :   GEN num   = Fp_mul(na, nb, p);
     420        4278 :   GEN denom = Fp_mul(da, db, p);
     421        4278 :   line = FpE_chord_update(pa, pb, P, a4, p, &point);
     422        4278 :   num  = Fp_mul(num, line, p);
     423        4278 :   v = FpE_vert(point, P, a4, p);
     424        4278 :   denom = Fp_mul(denom, v, p);
     425        4278 :   return mkvec3(num, denom, point);
     426             : }
     427             : 
     428             : static GEN
     429       12488 : FpE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN p)
     430             : {
     431       12488 :   pari_sp ltop = avma;
     432             :   struct _FpE_miller d;
     433             :   GEN v, num, denom;
     434             : 
     435       12488 :   d.a4 = a4; d.p = p; d.P = P;
     436       12488 :   v = gen_pow(mkvec3(gen_1,gen_1,Q), m, (void*)&d, FpE_Miller_dbl, FpE_Miller_add);
     437       12488 :   num = gel(v,1); denom = gel(v,2);
     438       12488 :   return gerepileupto(ltop, Fp_div(num, denom, p));
     439             : }
     440             : 
     441             : GEN
     442        8929 : FpE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN p)
     443             : {
     444        8929 :   pari_sp ltop = avma;
     445             :   GEN num, denom, result;
     446        8929 :   if (ell_is_inf(P) || ell_is_inf(Q) || ZV_equal(P,Q))
     447        2772 :     return gen_1;
     448        6157 :   num    = FpE_Miller(P, Q, m, a4, p);
     449        6157 :   denom  = FpE_Miller(Q, P, m, a4, p);
     450        6157 :   result = Fp_div(num, denom, p);
     451        6157 :   if (mpodd(m))
     452         606 :     result  = Fp_neg(result, p);
     453        6157 :   return gerepileupto(ltop, result);
     454             : }
     455             : 
     456             : GEN
     457         174 : FpE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN p)
     458             : {
     459         174 :   if (ell_is_inf(P) || ell_is_inf(Q))
     460           0 :     return gen_1;
     461         174 :   return FpE_Miller(P, Q, m, a4, p);
     462             : }
     463             : 
     464             : /***********************************************************************/
     465             : /**                                                                   **/
     466             : /**                   CM by principal order                           **/
     467             : /**                                                                   **/
     468             : /***********************************************************************/
     469             : 
     470             : /* is jn/jd = J (mod p) */
     471             : static int
     472      726774 : is_CMj(long J, GEN jn, GEN jd, GEN p)
     473      726774 : { return remii(subii(mulis(jd,J), jn), p) == gen_0; }
     474             : #ifndef LONG_IS_64BIT
     475             : /* is jn/jd = -(2^32 a + b) (mod p) */
     476             : static int
     477             : u2_is_CMj(ulong a, ulong b, GEN jn, GEN jd, GEN p)
     478             : {
     479             :   GEN mJ = uu32toi(a,b);
     480             :   return remii(addii(mulii(jd,mJ), jn), p) == gen_0;
     481             : }
     482             : #endif
     483             : 
     484             : static long
     485       56664 : Fp_ellj_get_CM(GEN jn, GEN jd, GEN p)
     486             : {
     487             : #define CHECK(CM,J) if (is_CMj(J,jn,jd,p)) return CM;
     488       56664 :   CHECK(-3,  0);
     489       56628 :   CHECK(-4,  1728);
     490       56586 :   CHECK(-7,  -3375);
     491       56448 :   CHECK(-8,  8000);
     492       56304 :   CHECK(-11, -32768);
     493       56166 :   CHECK(-12, 54000);
     494       55926 :   CHECK(-16, 287496);
     495       55794 :   CHECK(-19, -884736);
     496       55644 :   CHECK(-27, -12288000);
     497       55380 :   CHECK(-28, 16581375);
     498       55206 :   CHECK(-43, -884736000);
     499             : #ifdef LONG_IS_64BIT
     500       55068 :   CHECK(-67, -147197952000L);
     501       54960 :   CHECK(-163, -262537412640768000L);
     502             : #else
     503             :   if (u2_is_CMj(0x00000022UL,0x45ae8000UL,jn,jd,p)) return -67;
     504             :   if (u2_is_CMj(0x03a4b862UL,0xc4b40000UL,jn,jd,p)) return -163;
     505             : #endif
     506             : #undef CHECK
     507       54822 :   return 0;
     508             : }
     509             : 
     510             : /***********************************************************************/
     511             : /**                                                                   **/
     512             : /**                            issupersingular                        **/
     513             : /**                                                                   **/
     514             : /***********************************************************************/
     515             : 
     516             : /* assume x reduced mod p, monic. Return one root, or NULL if irreducible */
     517             : static GEN
     518        4878 : FqX_quad_root(GEN x, GEN T, GEN p)
     519             : {
     520        4878 :   GEN b = gel(x,3), c = gel(x,2);
     521        4878 :   GEN D = Fq_sub(Fq_sqr(b, T, p), Fq_mulu(c,4, T, p), T, p);
     522        4878 :   GEN s = Fq_sqrt(D,T, p);
     523        4878 :   if (!s) return NULL;
     524        2892 :   return Fq_Fp_mul(Fq_sub(s, b, T, p), shifti(addiu(p, 1),-1),T, p);
     525             : }
     526             : 
     527             : /*
     528             :  * pol is the modular polynomial of level 2 modulo p.
     529             :  *
     530             :  * (T, p) defines the field FF_{p^2} in which j_prev and j live.
     531             :  */
     532             : static long
     533        2214 : path_extends_to_floor(GEN j_prev, GEN j, GEN T, GEN p, GEN Phi2, ulong max_len)
     534             : {
     535        2214 :   pari_sp ltop = avma;
     536             :   GEN Phi2_j;
     537             :   ulong mult, d;
     538             : 
     539             :   /* A path made its way to the floor if (i) its length was cut off
     540             :    * before reaching max_path_len, or (ii) it reached max_path_len but
     541             :    * only has one neighbour. */
     542        5106 :   for (d = 1; d < max_len; ++d) {
     543             :     GEN j_next;
     544             : 
     545        4878 :     Phi2_j = FqX_div_by_X_x(FqXY_evalx(Phi2, j, T, p), j_prev, T, p, NULL);
     546        4878 :     j_next = FqX_quad_root(Phi2_j, T, p);
     547        4878 :     if (!j_next)
     548             :     { /* j is on the floor */
     549        1986 :       avma = ltop;
     550        1986 :       return 1;
     551             :     }
     552             : 
     553        2892 :     j_prev = j; j = j_next;
     554        2892 :     if (gc_needed(ltop, 2))
     555           0 :       gerepileall(ltop, 2, &j, &j_prev);
     556             :   }
     557             : 
     558             :   /* Check that we didn't end up at the floor on the last step (j will
     559             :    * point to the last element in the path. */
     560         228 :   Phi2_j = FqX_div_by_X_x(FqXY_evalx(Phi2, j, T, p), j_prev, T, p, NULL);
     561         228 :   mult = FqX_nbroots(Phi2_j, T, p);
     562         228 :   avma = ltop;
     563         228 :   return mult == 0;
     564             : }
     565             : 
     566             : static int
     567       11880 : jissupersingular(GEN j, GEN S, GEN p)
     568             : {
     569       11880 :   long max_path_len = expi(p)+1;
     570       11880 :   GEN Phi2 = FpXX_red(polmodular_ZXX(2,0,0,1), p);
     571       11880 :   GEN Phi2_j = FqXY_evalx(Phi2, j, S, p);
     572       11880 :   GEN roots = FpXQX_roots(Phi2_j, S, p);
     573       11880 :   long nbroots = lg(roots)-1;
     574       11880 :   int res = 1;
     575             : 
     576             :   /* Every node in a supersingular L-volcano has L + 1 neighbours. */
     577             :   /* Note: a multiple root only occur when j has CM by sqrt(-15). */
     578       11880 :   if (nbroots==0 || (nbroots==1 && FqX_is_squarefree(Phi2_j, S, p)))
     579        9798 :     res = 0;
     580             :   else {
     581        2082 :     long i, l = lg(roots);
     582        2226 :     for (i = 1; i < l; ++i) {
     583        2214 :       if (path_extends_to_floor(j, gel(roots, i), S, p, Phi2, max_path_len)) {
     584        2070 :         res = 0;
     585        2070 :         break;
     586             :       }
     587             :     }
     588             :   }
     589             :   /* If none of the paths reached the floor, then the j-invariant is
     590             :    * supersingular. */
     591       11880 :   return res;
     592             : }
     593             : 
     594             : int
     595         900 : Fp_elljissupersingular(GEN j, GEN p)
     596             : {
     597         900 :   pari_sp ltop = avma;
     598             :   long CM;
     599         900 :   if (abscmpiu(p, 5) <= 0) return signe(j) == 0; /* valid if p <= 5 */
     600         804 :   CM = Fp_ellj_get_CM(j, gen_1, p);
     601         804 :   if (CM < 0) return krosi(CM, p) < 0; /* valid if p > 3 */
     602             :   else
     603             :   {
     604         522 :     GEN S = init_Fq(p, 2, fetch_var());
     605         522 :     int res = jissupersingular(j, S, p);
     606         522 :     (void)delete_var(); avma = ltop; return res;
     607             :   }
     608             : }
     609             : 
     610             : /***********************************************************************/
     611             : /**                                                                   **/
     612             : /**                            Cardinal                               **/
     613             : /**                                                                   **/
     614             : /***********************************************************************/
     615             : 
     616             : /*assume a4,a6 reduced mod p odd */
     617             : static ulong
     618      177317 : Fl_elltrace_naive(ulong a4, ulong a6, ulong p)
     619             : {
     620             :   ulong i, j;
     621      177317 :   long a = 0;
     622             :   long d0, d1, d2, d3;
     623      177317 :   GEN k = const_vecsmall(p, -1);
     624      177317 :   k[1] = 0;
     625    52377558 :   for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p))
     626    52200241 :     k[j+1] = 1;
     627      177317 :   d0 = 6%p; d1 = d0; d2 = Fl_add(a4, 1, p); d3 = a6;
     628   104577799 :   for(i=0;; i++)
     629             :   {
     630   104577799 :     a -= k[1+d3];
     631   104577799 :     if (i==p-1) break;
     632   104400482 :     d3 = Fl_add(d3, d2, p);
     633   104400482 :     d2 = Fl_add(d2, d1, p);
     634   104400482 :     d1 = Fl_add(d1, d0, p);
     635   104400482 :   }
     636      177317 :   return a;
     637             : }
     638             : 
     639             : /* z1 <-- z1 + z2, with precomputed inverse */
     640             : static void
     641           0 : FpE_add_ip(GEN z1, GEN z2, GEN a4, GEN p, GEN p2inv)
     642             : {
     643             :   GEN p1,x,x1,x2,y,y1,y2;
     644             : 
     645           0 :   x1 = gel(z1,1); y1 = gel(z1,2);
     646           0 :   x2 = gel(z2,1); y2 = gel(z2,2);
     647           0 :   if (x1 == x2)
     648           0 :     p1 = Fp_add(a4, mulii(x1,mului(3,x1)), p);
     649             :   else
     650           0 :     p1 = Fp_sub(y2,y1, p);
     651             : 
     652           0 :   p1 = Fp_mul(p1, p2inv, p);
     653           0 :   x = Fp_sub(sqri(p1), addii(x1,x2), p);
     654           0 :   y = Fp_sub(mulii(p1,subii(x1,x)), y1, p);
     655           0 :   affii(x, x1);
     656           0 :   affii(y, y1);
     657           0 : }
     658             : 
     659             : /* make sure *x has lgefint >= k */
     660             : static void
     661           0 : _fix(GEN x, long k)
     662             : {
     663           0 :   GEN y = (GEN)*x;
     664           0 :   if (lgefint(y) < k) { GEN p1 = cgeti(k); affii(y,p1); *x = (long)p1; }
     665           0 : }
     666             : 
     667             : /* Return the lift of a (mod b), which is closest to c */
     668             : static GEN
     669      201096 : closest_lift(GEN a, GEN b, GEN c)
     670             : {
     671      201096 :   return addii(a, mulii(b, diviiround(subii(c,a), b)));
     672             : }
     673             : 
     674             : static long
     675           0 : get_table_size(GEN pordmin, GEN B)
     676             : {
     677           0 :   pari_sp av = avma;
     678           0 :   GEN t = ceilr( sqrtr( divri(itor(pordmin, DEFAULTPREC), B) ) );
     679           0 :   if (is_bigint(t))
     680           0 :     pari_err_OVERFLOW("ellap [large prime: install the 'seadata' package]");
     681           0 :   avma = av;
     682           0 :   return itos(t) >> 1;
     683             : }
     684             : 
     685             : /* Find x such that kronecker(u = x^3+c4x+c6, p) is KRO.
     686             :  * Return point [x*u,u^2] on E (KRO=1) / E^twist (KRO=-1) */
     687             : static GEN
     688           0 : Fp_ellpoint(long KRO, ulong *px, GEN c4, GEN c6, GEN p)
     689             : {
     690           0 :   ulong x = *px;
     691             :   GEN u;
     692             :   for(;;)
     693             :   {
     694           0 :     x++; /* u = x^3 + c4 x + c6 */
     695           0 :     u = modii(addii(c6, mului(x, addii(c4, sqru(x)))), p);
     696           0 :     if (kronecker(u,p) == KRO) break;
     697           0 :   }
     698           0 :   *px = x;
     699           0 :   return mkvec2(modii(mului(x,u),p), Fp_sqr(u,p));
     700             : }
     701             : static GEN
     702        5022 : Fl_ellpoint(long KRO, ulong *px, ulong c4, ulong c6, ulong p)
     703             : {
     704        5022 :   ulong t, u, x = *px;
     705             :   for(;;)
     706             :   {
     707        9294 :     if (++x >= p) pari_err_PRIME("ellap",utoi(p));
     708        9294 :     t = Fl_add(c4, Fl_sqr(x,p), p);
     709        9294 :     u = Fl_add(c6, Fl_mul(x, t, p), p);
     710        9294 :     if (krouu(u,p) == KRO) break;
     711        4272 :   }
     712        5022 :   *px = x;
     713        5022 :   return mkvecsmall2(Fl_mul(x,u,p), Fl_sqr(u,p));
     714             : }
     715             : 
     716             : static GEN ap_j1728(GEN a4,GEN p);
     717             : /* compute a_p using Shanks/Mestre + Montgomery's trick. Assume p > 457 */
     718             : static GEN
     719           0 : Fp_ellcard_Shanks(GEN c4, GEN c6, GEN p)
     720             : {
     721             :   pari_timer T;
     722             :   long *tx, *ty, *ti, pfinal, i, j, s, KRO, nb;
     723             :   ulong x;
     724           0 :   pari_sp av = avma, av2;
     725             :   GEN p1, P, mfh, h, F,f, fh,fg, pordmin, u, v, p1p, p2p, A, B, a4, pts;
     726           0 :   tx = NULL;
     727           0 :   ty = ti = NULL; /* gcc -Wall */
     728             : 
     729           0 :   if (!signe(c6)) {
     730           0 :     GEN ap = ap_j1728(c4, p);
     731           0 :     return gerepileuptoint(av, subii(addiu(p,1), ap));
     732             :   }
     733             : 
     734           0 :   if (DEBUGLEVEL >= 6) timer_start(&T);
     735             :   /* once #E(Fp) is know mod B >= pordmin, it is completely determined */
     736           0 :   pordmin = addiu(sqrti(gmul2n(p,4)), 1); /* ceil( 4sqrt(p) ) */
     737           0 :   p1p = addiu(p, 1);
     738           0 :   p2p = shifti(p1p, 1);
     739           0 :   x = 0; KRO = 0;
     740             :   /* how many 2-torsion points ? */
     741           0 :   switch(FpX_nbroots(mkpoln(4, gen_1, gen_0, c4, c6), p))
     742             :   {
     743           0 :     case 3:  A = gen_0; B = utoipos(4); break;
     744           0 :     case 1:  A = gen_0; B = gen_2; break;
     745           0 :     default: A = gen_1; B = gen_2; break; /* 0 */
     746             :   }
     747             :   for(;;)
     748             :   {
     749           0 :     h = closest_lift(A, B, p1p);
     750           0 :     if (!KRO) /* first time, initialize */
     751             :     {
     752           0 :       KRO = kronecker(c6,p);
     753           0 :       f = mkvec2(gen_0, Fp_sqr(c6,p));
     754             :     }
     755             :     else
     756             :     {
     757           0 :       KRO = -KRO;
     758           0 :       f = Fp_ellpoint(KRO, &x, c4,c6,p);
     759             :     }
     760             :     /* [ux, u^2] is on E_u: y^2 = x^3 + c4 u^2 x + c6 u^3
     761             :      * E_u isomorphic to E (resp. E') iff KRO = 1 (resp. -1)
     762             :      * #E(F_p) = p+1 - a_p, #E'(F_p) = p+1 + a_p
     763             :      *
     764             :      * #E_u(Fp) = A (mod B),  h is close to #E_u(Fp) */
     765           0 :     a4 = modii(mulii(c4, gel(f,2)), p); /* c4 for E_u */
     766           0 :     fh = FpE_mul(f, h, a4, p);
     767           0 :     if (ell_is_inf(fh)) goto FOUND;
     768             : 
     769           0 :     s = get_table_size(pordmin, B);
     770             :     /* look for h s.t f^h = 0 */
     771           0 :     if (!tx)
     772             :     { /* first time: initialize */
     773           0 :       tx = newblock(3*(s+1));
     774           0 :       ty = tx + (s+1);
     775           0 :       ti = ty + (s+1);
     776             :     }
     777           0 :     F = FpE_mul(f,B,a4,p);
     778           0 :     *tx = evaltyp(t_VECSMALL) | evallg(s+1);
     779             : 
     780             :     /* F = B.f */
     781           0 :     P = gcopy(fh);
     782           0 :     if (s < 3)
     783             :     { /* we're nearly done: naive search */
     784           0 :       GEN q1 = P, mF = FpE_neg(F, p); /* -F */
     785           0 :       for (i=1;; i++)
     786             :       {
     787           0 :         P = FpE_add(P,F,a4,p); /* h.f + i.F */
     788           0 :         if (ell_is_inf(P)) { h = addii(h, mului(i,B)); goto FOUND; }
     789           0 :         q1 = FpE_add(q1,mF,a4,p); /* h.f - i.F */
     790           0 :         if (ell_is_inf(q1)) { h = subii(h, mului(i,B)); goto FOUND; }
     791           0 :       }
     792             :     }
     793             :     /* Baby Step/Giant Step */
     794           0 :     nb = minss(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */
     795           0 :     pts = cgetg(nb+1, t_VEC);
     796           0 :     j = lgefint(p);
     797           0 :     for (i=1; i<=nb; i++)
     798             :     { /* baby steps */
     799           0 :       gel(pts,i) = P; /* h.f + (i-1).F */
     800           0 :       _fix(P+1, j); tx[i] = mod2BIL(gel(P,1));
     801           0 :       _fix(P+2, j); ty[i] = mod2BIL(gel(P,2));
     802           0 :       P = FpE_add(P,F,a4,p); /* h.f + i.F */
     803           0 :       if (ell_is_inf(P)) { h = addii(h, mului(i,B)); goto FOUND; }
     804             :     }
     805           0 :     mfh = FpE_neg(fh, p);
     806           0 :     fg = FpE_add(P,mfh,a4,p); /* h.f + nb.F - h.f = nb.F */
     807           0 :     if (ell_is_inf(fg)) { h = mului(nb,B); goto FOUND; }
     808           0 :     u = cgetg(nb+1, t_VEC);
     809           0 :     av2 = avma; /* more baby steps, nb points at a time */
     810           0 :     while (i <= s)
     811             :     {
     812             :       long maxj;
     813           0 :       for (j=1; j<=nb; j++) /* adding nb.F (part 1) */
     814             :       {
     815           0 :         P = gel(pts,j); /* h.f + (i-nb-1+j-1).F */
     816           0 :         gel(u,j) = subii(gel(fg,1), gel(P,1));
     817           0 :         if (!signe(gel(u,j))) /* sum = 0 or doubling */
     818             :         {
     819           0 :           long k = i+j-2;
     820           0 :           if (equalii(gel(P,2),gel(fg,2))) k -= 2*nb; /* fg == P */
     821           0 :           h = addii(h, mulsi(k,B)); goto FOUND;
     822             :         }
     823             :       }
     824           0 :       v = FpV_inv(u, p);
     825           0 :       maxj = (i-1 + nb <= s)? nb: s % nb;
     826           0 :       for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */
     827             :       {
     828           0 :         P = gel(pts,j);
     829           0 :         FpE_add_ip(P,fg, a4,p, gel(v,j));
     830           0 :         tx[i] = mod2BIL(gel(P,1));
     831           0 :         ty[i] = mod2BIL(gel(P,2));
     832             :       }
     833           0 :       avma = av2;
     834             :     }
     835           0 :     P = FpE_add(gel(pts,j-1),mfh,a4,p); /* = (s-1).F */
     836           0 :     if (ell_is_inf(P)) { h = mului(s-1,B); goto FOUND; }
     837           0 :     if (DEBUGLEVEL >= 6)
     838           0 :       timer_printf(&T, "[Fp_ellcard_Shanks] baby steps, s = %ld",s);
     839             : 
     840             :     /* giant steps: fg = s.F */
     841           0 :     fg = FpE_add(P,F,a4,p);
     842           0 :     if (ell_is_inf(fg)) { h = mului(s,B); goto FOUND; }
     843           0 :     pfinal = mod2BIL(p); av2 = avma;
     844             :     /* Goal of the following: sort points by increasing x-coordinate hash.
     845             :      * Done in a complicated way to avoid allocating a large temp vector */
     846           0 :     p1 = vecsmall_indexsort(tx); /* = permutation sorting tx */
     847           0 :     for (i=1; i<=s; i++) ti[i] = tx[p1[i]];
     848             :     /* ti = tx sorted */
     849           0 :     for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; }
     850             :     /* tx is sorted. ti = ty sorted */
     851           0 :     for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; }
     852             :     /* ty is sorted. ti = permutation sorting tx */
     853           0 :     if (DEBUGLEVEL >= 6) timer_printf(&T, "[Fp_ellcard_Shanks] sorting");
     854           0 :     avma = av2;
     855             : 
     856           0 :     gaffect(fg, gel(pts,1));
     857           0 :     for (j=2; j<=nb; j++) /* pts[j] = j.fg = (s*j).F */
     858             :     {
     859           0 :       P = FpE_add(gel(pts,j-1),fg,a4,p);
     860           0 :       if (ell_is_inf(P)) { h = mulii(mulss(s,j), B); goto FOUND; }
     861           0 :       gaffect(P, gel(pts,j));
     862             :     }
     863             :     /* replace fg by nb.fg since we do nb points at a time */
     864           0 :     avma = av2;
     865           0 :     fg = gcopy(gel(pts,nb)); /* copy: we modify (temporarily) pts[nb] below */
     866           0 :     av2 = avma;
     867             : 
     868           0 :     for (i=1,j=1; ; i++)
     869             :     {
     870           0 :       GEN ftest = gel(pts,j);
     871           0 :       long m, l = 1, r = s+1;
     872             :       long k, k2, j2;
     873             : 
     874           0 :       avma = av2;
     875           0 :       k = mod2BIL(gel(ftest,1));
     876           0 :       while (l < r)
     877             :       {
     878           0 :         m = (l+r) >> 1;
     879           0 :         if (tx[m] < k) l = m+1; else r = m;
     880             :       }
     881           0 :       if (r <= s && tx[r] == k)
     882             :       {
     883           0 :         while (r && tx[r] == k) r--;
     884           0 :         k2 = mod2BIL(gel(ftest,2));
     885           0 :         for (r++; r <= s && tx[r] == k; r++)
     886           0 :           if (ty[r] == k2 || ty[r] == pfinal - k2)
     887             :           { /* [h+j2] f == +/- ftest (= [i.s] f)? */
     888           0 :             j2 = ti[r] - 1;
     889           0 :             if (DEBUGLEVEL >=6)
     890           0 :               timer_printf(&T, "[Fp_ellcard_Shanks] giant steps, i = %ld",i);
     891           0 :             P = FpE_add(FpE_mul(F,stoi(j2),a4,p),fh,a4,p);
     892           0 :             if (equalii(gel(P,1), gel(ftest,1)))
     893             :             {
     894           0 :               if (equalii(gel(P,2), gel(ftest,2))) i = -i;
     895           0 :               h = addii(h, mulii(addis(mulss(s,i), j2), B));
     896           0 :               goto FOUND;
     897             :             }
     898             :           }
     899             :       }
     900           0 :       if (++j > nb)
     901             :       { /* compute next nb points */
     902           0 :         long save = 0; /* gcc -Wall */;
     903           0 :         for (j=1; j<=nb; j++)
     904             :         {
     905           0 :           P = gel(pts,j);
     906           0 :           gel(u,j) = subii(gel(fg,1), gel(P,1));
     907           0 :           if (gel(u,j) == gen_0) /* occurs once: i = j = nb, P == fg */
     908             :           {
     909           0 :             gel(u,j) = shifti(gel(P,2),1);
     910           0 :             save = fg[1]; fg[1] = P[1];
     911             :           }
     912             :         }
     913           0 :         v = FpV_inv(u, p);
     914           0 :         for (j=1; j<=nb; j++)
     915           0 :           FpE_add_ip(gel(pts,j),fg,a4,p, gel(v,j));
     916           0 :         if (i == nb) { fg[1] = save; }
     917           0 :         j = 1;
     918             :       }
     919           0 :     }
     920             : FOUND: /* found a point of exponent h on E_u */
     921           0 :     h = FpE_order(f, h, a4, p);
     922             :     /* h | #E_u(Fp) = A (mod B) */
     923           0 :     A = Z_chinese_all(A, gen_0, B, h, &B);
     924           0 :     if (cmpii(B, pordmin) >= 0) break;
     925             :     /* not done: update A mod B for the _next_ curve, isomorphic to
     926             :      * the quadratic twist of this one */
     927           0 :     A = remii(subii(p2p,A), B); /* #E(Fp)+#E'(Fp) = 2p+2 */
     928           0 :   }
     929           0 :   if (tx) killblock(tx);
     930           0 :   h = closest_lift(A, B, p1p);
     931           0 :   return gerepileuptoint(av, KRO==1? h: subii(p2p,h));
     932             : }
     933             : 
     934             : typedef struct
     935             : {
     936             :   ulong x,y,i;
     937             : } multiple;
     938             : 
     939             : static int
     940    14124234 : compare_multiples(multiple *a, multiple *b) { return a->x > b->x? 1:a->x<b->x?-1:0; }
     941             : 
     942             : /* find x such that h := a + b x is closest to c and return h:
     943             :  * x = round((c-a) / b) = floor( (2(c-a) + b) / 2b )
     944             :  * Assume 0 <= a < b < c  and b + 2c < 2^BIL */
     945             : static ulong
     946      206118 : uclosest_lift(ulong a, ulong b, ulong c)
     947             : {
     948      206118 :   ulong x = (b + ((c-a) << 1)) / (b << 1);
     949      206118 :   return a + b * x;
     950             : }
     951             : 
     952             : static long
     953      180060 : Fle_dbl_inplace(GEN P, ulong a4, ulong p)
     954             : {
     955             :   ulong x, y, slope;
     956      180060 :   if (!P[2]) return 1;
     957      180030 :   x = P[1]; y = P[2];
     958      180030 :   slope = Fl_div(Fl_add(Fl_triple(Fl_sqr(x,p), p), a4, p),
     959             :                  Fl_double(y, p), p);
     960      180030 :   P[1] = Fl_sub(Fl_sqr(slope, p), Fl_double(x, p), p);
     961      180030 :   P[2] = Fl_sub(Fl_mul(slope, Fl_sub(x, P[1], p), p), y, p);
     962      180030 :   return 0;
     963             : }
     964             : 
     965             : static long
     966     4925034 : Fle_add_inplace(GEN P, GEN Q, ulong a4, ulong p)
     967             : {
     968             :   ulong Px, Py, Qx, Qy, slope;
     969     4925034 :   if (ell_is_inf(Q)) return 0;
     970     4925034 :   Px = P[1]; Py = P[2];
     971     4925034 :   Qx = Q[1]; Qy = Q[2];
     972     4925034 :   if (Px==Qx)
     973      189066 :     return Py==Qy ? Fle_dbl_inplace(P, a4, p): 1;
     974     4735968 :   slope = Fl_div(Fl_sub(Py, Qy, p), Fl_sub(Px, Qx, p), p);
     975     4735968 :   P[1] = Fl_sub(Fl_sub(Fl_sqr(slope, p), Px, p), Qx, p);
     976     4735968 :   P[2] = Fl_sub(Fl_mul(slope, Fl_sub(Px, P[1], p), p), Py, p);
     977     4735968 :   return 0;
     978             : }
     979             : 
     980             : /* assume 99 < p < 2^(BIL-1) - 2^((BIL+1)/2) and e has good reduction at p.
     981             :  * Should use Barett reduction + multi-inverse. See Fp_ellcard_Shanks() */
     982             : static long
     983      201108 : Fl_ellcard_Shanks(ulong c4, ulong c6, ulong p)
     984             : {
     985             :   GEN f, fh, fg, ftest, F;
     986             :   ulong i, l, r, s, h, x, cp4, p1p, p2p, pordmin,A,B;
     987             :   long KRO;
     988      201108 :   pari_sp av = avma;
     989             :   multiple *table;
     990             : 
     991      201108 :   if (!c6) {
     992          12 :     GEN ap = ap_j1728(utoi(c4), utoipos(p));
     993          12 :     avma = av; return p+1 - itos(ap);
     994             :   }
     995             : 
     996      201096 :   pordmin = (ulong)(1 + 4*sqrt((double)p));
     997      201096 :   p1p = p+1;
     998      201096 :   p2p = p1p << 1;
     999      201096 :   x = 0; KRO = 0;
    1000      201096 :   switch(Flx_nbroots(mkvecsmall5(0L, c6,c4,0L,1L), p))
    1001             :   {
    1002       35478 :     case 3:  A = 0; B = 4; break;
    1003       95802 :     case 1:  A = 0; B = 2; break;
    1004       69816 :     default: A = 1; B = 2; break; /* 0 */
    1005             :   }
    1006             :   for(;;)
    1007             :   { /* see comments in Fp_ellcard_Shanks */
    1008      206118 :     h = uclosest_lift(A, B, p1p);
    1009      206118 :     if (!KRO) /* first time, initialize */
    1010             :     {
    1011      201096 :       KRO = krouu(c6,p); /* != 0 */
    1012      201096 :       f = mkvecsmall2(0, Fl_sqr(c6,p));
    1013             :     }
    1014             :     else
    1015             :     {
    1016        5022 :       KRO = -KRO;
    1017        5022 :       f = Fl_ellpoint(KRO, &x, c4,c6,p);
    1018             :     }
    1019      206118 :     cp4 = Fl_mul(c4, f[2], p);
    1020      206118 :     fh = Fle_mulu(f, h, cp4, p);
    1021      206118 :     if (ell_is_inf(fh)) goto FOUND;
    1022             : 
    1023      201966 :     s = (ulong) (sqrt(((double)pordmin)/B) / 2);
    1024      201966 :     if (!s) s = 1;
    1025      201966 :     table = (multiple *) stack_malloc((s+1) * sizeof(multiple));
    1026      201966 :     F = Fle_mulu(f, B, cp4, p);
    1027     2824692 :     for (i=0; i < s; i++)
    1028             :     {
    1029     2631762 :       table[i].x = fh[1];
    1030     2631762 :       table[i].y = fh[2];
    1031     2631762 :       table[i].i = i;
    1032     2631762 :       if (Fle_add_inplace(fh, F, cp4, p)) { h += B*(i+1); goto FOUND; }
    1033             :     }
    1034      192930 :     qsort(table,s,sizeof(multiple),(QSCOMP)compare_multiples);
    1035      192930 :     fg = Fle_mulu(F, s, cp4, p); ftest = zv_copy(fg);
    1036      192930 :     if (ell_is_inf(ftest)) {
    1037           0 :       if (!uisprime(p)) pari_err_PRIME("ellap",utoi(p));
    1038           0 :       pari_err_BUG("ellap (f^(i*s) = 1)");
    1039             :     }
    1040     2486202 :     for (i=1; ; i++)
    1041             :     {
    1042     2486202 :       l=0; r=s;
    1043    20922576 :       while (l<r)
    1044             :       {
    1045    15950172 :         ulong m = (l+r) >> 1;
    1046    15950172 :         if (table[m].x < uel(ftest,1)) l=m+1; else r=m;
    1047             :       }
    1048     2486202 :       if (r < s && table[r].x == uel(ftest,1)) break;
    1049     2293272 :       if (Fle_add_inplace(ftest, fg, cp4, p))
    1050           0 :         pari_err_PRIME("ellap",utoi(p));
    1051     2293272 :     }
    1052      192930 :     h += table[r].i * B;
    1053      192930 :     if (table[r].y == uel(ftest,2))
    1054      100866 :       h -= s * i * B;
    1055             :     else
    1056       92064 :       h += s * i * B;
    1057             : FOUND:
    1058      206118 :     h = itou(Fle_order(f, utoipos(h), cp4, p));
    1059             :     /* h | #E_u(Fp) = A (mod B) */
    1060             :     {
    1061             :       GEN C;
    1062      206118 :       A = itou( Z_chinese_all(gen_0, utoi(A), utoipos(h), utoipos(B), &C) );
    1063      206118 :       if (abscmpiu(C, pordmin) >= 0) { /* uclosest_lift could overflow */
    1064      201096 :         h = itou( closest_lift(utoi(A), C, utoipos(p1p)) );
    1065      201096 :         break;
    1066             :       }
    1067        5022 :       B = itou(C);
    1068             :     }
    1069        5022 :     A = (p2p - A) % B; avma = av;
    1070        5022 :   }
    1071      298032 :   avma = av; return KRO==1? h: p2p-h;
    1072             : }
    1073             : 
    1074             : /** ellap from CM (original code contributed by Mark Watkins) **/
    1075             : 
    1076             : static GEN
    1077       37188 : ap_j0(GEN a6,GEN p)
    1078             : {
    1079             :   GEN a, b, e, d;
    1080       37188 :   if (umodiu(p,3) != 1) return gen_0;
    1081       18522 :   (void)cornacchia2(utoipos(27),p, &a,&b);
    1082       18522 :   if (umodiu(a, 3) == 1) a = negi(a);
    1083       18522 :   d = mulis(a6,-108);
    1084       18522 :   e = diviuexact(shifti(p,-1), 3); /* (p-1) / 6 */
    1085       18522 :   return centermod(mulii(a, Fp_pow(d, e, p)), p);
    1086             : }
    1087             : static GEN
    1088     2243850 : ap_j1728(GEN a4,GEN p)
    1089             : {
    1090             :   GEN a, b, e;
    1091     2243850 :   if (mod4(p) != 1) return gen_0;
    1092     1121076 :   (void)cornacchia2(utoipos(4),p, &a,&b);
    1093     1121076 :   if (Mod4(a)==0) a = b;
    1094     1121076 :   if (Mod2(a)==1) a = shifti(a,1);
    1095     1121076 :   if (Mod8(a)==6) a = negi(a);
    1096     1121076 :   e = shifti(p,-2); /* (p-1) / 4 */
    1097     1121076 :   return centermod(mulii(a, Fp_pow(a4, e, p)), p);
    1098             : }
    1099             : static GEN
    1100         120 : ap_j8000(GEN a6, GEN p)
    1101             : {
    1102             :   GEN a, b;
    1103         120 :   long r = mod8(p), s = 1;
    1104         120 :   if (r != 1 && r != 3) return gen_0;
    1105          54 :   (void)cornacchia2(utoipos(8),p, &a,&b);
    1106          54 :   switch(Mod16(a)) {
    1107          24 :     case 2: case 6:   if (Mod4(b)) s = -s;
    1108          24 :       break;
    1109          30 :     case 10: case 14: if (!Mod4(b)) s = -s;
    1110          30 :       break;
    1111             :   }
    1112          54 :   if (kronecker(mulis(a6, 42), p) < 0) s = -s;
    1113          54 :   return s > 0? a: negi(a);
    1114             : }
    1115             : static GEN
    1116         132 : ap_j287496(GEN a6, GEN p)
    1117             : {
    1118             :   GEN a, b;
    1119         132 :   long s = 1;
    1120         132 :   if (mod4(p) != 1) return gen_0;
    1121          60 :   (void)cornacchia2(utoipos(4),p, &a,&b);
    1122          60 :   if (Mod4(a)==0) a = b;
    1123          60 :   if (Mod2(a)==1) a = shifti(a,1);
    1124          60 :   if (Mod8(a)==6) s = -s;
    1125          60 :   if (krosi(2,p) < 0) s = -s;
    1126          60 :   if (kronecker(mulis(a6, -14), p) < 0) s = -s;
    1127          60 :   return s > 0? a: negi(a);
    1128             : }
    1129             : static GEN
    1130        1308 : ap_cm(int CM, long A6B, GEN a6, GEN p)
    1131             : {
    1132             :   GEN a, b;
    1133        1308 :   long s = 1;
    1134        1308 :   if (krosi(CM,p) < 0) return gen_0;
    1135         660 :   (void)cornacchia2(utoipos(-CM),p, &a, &b);
    1136         660 :   if ((CM&3) == 0) CM >>= 2;
    1137         660 :   if ((krois(a, -CM) > 0) ^ (CM == -7)) s = -s;
    1138         660 :   if (kronecker(mulis(a6,A6B), p) < 0) s = -s;
    1139         660 :   return s > 0? a: negi(a);
    1140             : }
    1141             : static GEN
    1142        9852 : ec_ap_cm(int CM, GEN a4, GEN a6, GEN p)
    1143             : {
    1144        9852 :   switch(CM)
    1145             :   {
    1146           0 :     case  -3: return ap_j0(a6, p);
    1147        8292 :     case  -4: return ap_j1728(a4, p);
    1148         120 :     case  -8: return ap_j8000(a6, p);
    1149         132 :     case -16: return ap_j287496(a6, p);
    1150         120 :     case  -7: return ap_cm(CM, -2, a6, p);
    1151         126 :     case -11: return ap_cm(CM, 21, a6, p);
    1152         204 :     case -12: return ap_cm(CM, 22, a6, p);
    1153         108 :     case -19: return ap_cm(CM, 1, a6, p);
    1154         228 :     case -27: return ap_cm(CM, 253, a6, p);
    1155         156 :     case -28: return ap_cm(-7, -114, a6, p); /* yes, -7 ! */
    1156         132 :     case -43: return ap_cm(CM, 21, a6, p);
    1157         108 :     case -67: return ap_cm(CM, 217, a6, p);
    1158         126 :     case -163:return ap_cm(CM, 185801, a6, p);
    1159           0 :     default: return NULL;
    1160             :   }
    1161             : }
    1162             : 
    1163             : static GEN
    1164       55860 : Fp_ellj_nodiv(GEN a4, GEN a6, GEN p)
    1165             : {
    1166       55860 :   GEN a43 = Fp_mulu(Fp_powu(a4, 3, p), 4, p);
    1167       55860 :   GEN a62 = Fp_mulu(Fp_sqr(a6, p), 27, p);
    1168       55860 :   return mkvec2(Fp_mulu(a43, 1728, p), Fp_add(a43, a62, p));
    1169             : }
    1170             : 
    1171             : GEN
    1172           0 : Fp_ellj(GEN a4, GEN a6, GEN p)
    1173             : {
    1174           0 :   pari_sp av=avma;
    1175           0 :   GEN z = Fp_ellj_nodiv(a4, a6, p);
    1176           0 :   return gerepileuptoint(av,Fp_div(gel(z,1),gel(z,2),p));
    1177             : }
    1178             : 
    1179             : static GEN /* Only compute a mod p, so assume p>=17 */
    1180     2328594 : Fp_ellcard_CM(GEN a4, GEN a6, GEN p)
    1181             : {
    1182     2328594 :   pari_sp av = avma;
    1183             :   GEN a;
    1184     2328594 :   if (!signe(a4)) a = ap_j0(a6,p);
    1185     2291406 :   else if (!signe(a6)) a = ap_j1728(a4,p);
    1186             :   else
    1187             :   {
    1188       55860 :     GEN j = Fp_ellj_nodiv(a4, a6, p);
    1189       55860 :     long CM = Fp_ellj_get_CM(gel(j,1), gel(j,2), p);
    1190       55860 :     if (!CM) { avma = av; return NULL; }
    1191        1560 :     a = ec_ap_cm(CM,a4,a6,p);
    1192             :   }
    1193     2274294 :   return gerepileuptoint(av, subii(addiu(p,1),a));
    1194             : }
    1195             : 
    1196             : GEN
    1197     2425674 : Fp_ellcard(GEN a4, GEN a6, GEN p)
    1198             : {
    1199     2425674 :   long lp = expi(p);
    1200     2425674 :   ulong pp = p[2];
    1201     2425674 :   if (lp < 11)
    1202       97080 :     return utoi(pp+1 - Fl_elltrace_naive(umodiu(a4,pp), umodiu(a6,pp), pp));
    1203     2328594 :   { GEN a = Fp_ellcard_CM(a4,a6,p); if (a) return a; }
    1204       54300 :   if (lp >= 56)
    1205         732 :     return Fp_ellcard_SEA(a4, a6, p, 0);
    1206       53568 :   if (lp <= BITS_IN_LONG-2)
    1207       53568 :     return utoi(Fl_ellcard_Shanks(umodiu(a4,pp), umodiu(a6,pp), pp));
    1208           0 :   return Fp_ellcard_Shanks(a4, a6, p);
    1209             : }
    1210             : 
    1211             : long
    1212      208823 : Fl_elltrace(ulong a4, ulong a6, ulong p)
    1213             : {
    1214             :   pari_sp av;
    1215             :   long lp;
    1216             :   GEN a;
    1217      208823 :   if (p < (1<<11)) return Fl_elltrace_naive(a4, a6, p);
    1218      147540 :   lp = expu(p);
    1219      147540 :   if (lp <= minss(56, BITS_IN_LONG-2)) return p+1-Fl_ellcard_Shanks(a4, a6, p);
    1220           0 :   av = avma; a = subui(p+1, Fp_ellcard(utoi(a4), utoi(a6), utoipos(p)));
    1221           0 :   avma = av; return itos(a);
    1222             : }
    1223             : long
    1224      236034 : Fl_elltrace_CM(long CM, ulong a4, ulong a6, ulong p)
    1225             : {
    1226             :   pari_sp av;
    1227             :   GEN a;
    1228      236034 :   if (!CM) return Fl_elltrace(a4,a6,p);
    1229       27246 :   if (p < (1<<11)) return Fl_elltrace_naive(a4, a6, p);
    1230        8292 :   av = avma; a = ec_ap_cm(CM, utoi(a4), utoi(a6), utoipos(p));
    1231        8292 :   avma = av; return itos(a);
    1232             : }
    1233             : 
    1234             : static GEN
    1235        8719 : _FpE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
    1236             : {
    1237        8719 :   struct _FpE *e = (struct _FpE *) E;
    1238        8719 :   return  Fp_order(FpE_weilpairing(P,Q,m,e->a4,e->p), F, e->p);
    1239             : }
    1240             : 
    1241             : GEN
    1242       18498 : Fp_ellgroup(GEN a4, GEN a6, GEN N, GEN p, GEN *pt_m)
    1243             : {
    1244             :   struct _FpE e;
    1245       18498 :   e.a4=a4; e.a6=a6; e.p=p;
    1246       18498 :   return gen_ellgroup(N, subiu(p,1), pt_m, (void*)&e, &FpE_group, _FpE_pairorder);
    1247             : }
    1248             : 
    1249             : GEN
    1250         492 : Fp_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN p)
    1251             : {
    1252             :   GEN P;
    1253         492 :   pari_sp av = avma;
    1254             :   struct _FpE e;
    1255         492 :   e.a4=a4; e.a6=a6; e.p=p;
    1256         492 :   switch(lg(D)-1)
    1257             :   {
    1258             :   case 1:
    1259         408 :     P = gen_gener(gel(D,1), (void*)&e, &FpE_group);
    1260         408 :     P = mkvec(FpE_changepoint(P, ch, p));
    1261         408 :     break;
    1262             :   default:
    1263          84 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FpE_group, _FpE_pairorder);
    1264          84 :     gel(P,1) = FpE_changepoint(gel(P,1), ch, p);
    1265          84 :     gel(P,2) = FpE_changepoint(gel(P,2), ch, p);
    1266          84 :     break;
    1267             :   }
    1268         492 :   return gerepilecopy(av, P);
    1269             : }
    1270             : 
    1271             : /* Not so fast arithmetic with points over elliptic curves over FpXQ */
    1272             : 
    1273             : /***********************************************************************/
    1274             : /**                                                                   **/
    1275             : /**                              FpXQE                                  **/
    1276             : /**                                                                   **/
    1277             : /***********************************************************************/
    1278             : 
    1279             : /* Theses functions deal with point over elliptic curves over FpXQ defined
    1280             :  * by an equation of the form y^2=x^3+a4*x+a6.
    1281             :  * Most of the time a6 is omitted since it can be recovered from any point
    1282             :  * on the curve.
    1283             :  */
    1284             : 
    1285             : GEN
    1286         768 : RgE_to_FpXQE(GEN x, GEN T, GEN p)
    1287             : {
    1288         768 :   if (ell_is_inf(x)) return x;
    1289         768 :   retmkvec2(Rg_to_FpXQ(gel(x,1),T,p),Rg_to_FpXQ(gel(x,2),T,p));
    1290             : }
    1291             : 
    1292             : GEN
    1293        1470 : FpXQE_changepoint(GEN x, GEN ch, GEN T, GEN p)
    1294             : {
    1295        1470 :   pari_sp av = avma;
    1296             :   GEN p1,z,u,r,s,t,v,v2,v3;
    1297        1470 :   if (ell_is_inf(x)) return x;
    1298         738 :   u = gel(ch,1); r = gel(ch,2);
    1299         738 :   s = gel(ch,3); t = gel(ch,4);
    1300         738 :   v = FpXQ_inv(u, T, p); v2 = FpXQ_sqr(v, T, p); v3 = FpXQ_mul(v,v2, T, p);
    1301         738 :   p1 = FpX_sub(gel(x,1),r, p);
    1302         738 :   z = cgetg(3,t_VEC);
    1303         738 :   gel(z,1) = FpXQ_mul(v2, p1, T, p);
    1304         738 :   gel(z,2) = FpXQ_mul(v3, FpX_sub(gel(x,2), FpX_add(FpXQ_mul(s,p1, T, p),t, p), p), T, p);
    1305         738 :   return gerepileupto(av, z);
    1306             : }
    1307             : 
    1308             : GEN
    1309         768 : FpXQE_changepointinv(GEN x, GEN ch, GEN T, GEN p)
    1310             : {
    1311             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
    1312         768 :   if (ell_is_inf(x)) return x;
    1313         768 :   X = gel(x,1); Y = gel(x,2);
    1314         768 :   u = gel(ch,1); r = gel(ch,2);
    1315         768 :   s = gel(ch,3); t = gel(ch,4);
    1316         768 :   u2 = FpXQ_sqr(u, T, p); u3 = FpXQ_mul(u,u2, T, p);
    1317         768 :   u2X = FpXQ_mul(u2,X, T, p);
    1318         768 :   z = cgetg(3, t_VEC);
    1319         768 :   gel(z,1) = FpX_add(u2X,r, p);
    1320         768 :   gel(z,2) = FpX_add(FpXQ_mul(u3,Y, T, p), FpX_add(FpXQ_mul(s,u2X, T, p), t, p), p);
    1321         768 :   return z;
    1322             : }
    1323             : 
    1324             : static GEN
    1325         720 : nonsquare_FpXQ(GEN T, GEN p)
    1326             : {
    1327         720 :   pari_sp av = avma;
    1328         720 :   long n = degpol(T), v = varn(T);
    1329             :   GEN a;
    1330         720 :   if (odd(n))
    1331             :   {
    1332         360 :     GEN z = cgetg(3, t_POL);
    1333         360 :     z[1] = evalsigne(1) | evalvarn(v);
    1334         360 :     gel(z,2) = nonsquare_Fp(p); return z;
    1335             :   }
    1336             :   do
    1337             :   {
    1338         672 :     avma = av;
    1339         672 :     a = random_FpX(n, v, p);
    1340         672 :   } while (FpXQ_issquare(a, T, p));
    1341         360 :   return a;
    1342             : }
    1343             : 
    1344             : void
    1345         720 : FpXQ_elltwist(GEN a4, GEN a6, GEN T, GEN p, GEN *pt_a4, GEN *pt_a6)
    1346             : {
    1347         720 :   GEN d = nonsquare_FpXQ(T, p);
    1348         720 :   GEN d2 = FpXQ_sqr(d, T, p), d3 = FpXQ_mul(d2, d, T, p);
    1349         720 :   *pt_a4 = FpXQ_mul(a4, d2, T, p);
    1350         720 :   *pt_a6 = FpXQ_mul(a6, d3, T, p);
    1351         720 : }
    1352             : 
    1353             : static GEN
    1354      158832 : FpXQE_dbl_slope(GEN P, GEN a4, GEN T, GEN p, GEN *slope)
    1355             : {
    1356             :   GEN x, y, Q;
    1357      158832 :   if (ell_is_inf(P) || !signe(gel(P,2))) return ellinf();
    1358      157736 :   x = gel(P,1); y = gel(P,2);
    1359      157736 :   *slope = FpXQ_div(FpX_add(FpX_mulu(FpXQ_sqr(x, T, p), 3, p), a4, p),
    1360             :                             FpX_mulu(y, 2, p), T, p);
    1361      157736 :   Q = cgetg(3,t_VEC);
    1362      157736 :   gel(Q, 1) = FpX_sub(FpXQ_sqr(*slope, T, p), FpX_mulu(x, 2, p), p);
    1363      157736 :   gel(Q, 2) = FpX_sub(FpXQ_mul(*slope, FpX_sub(x, gel(Q, 1), p), T, p), y, p);
    1364      157736 :   return Q;
    1365             : }
    1366             : 
    1367             : GEN
    1368      154584 : FpXQE_dbl(GEN P, GEN a4, GEN T, GEN p)
    1369             : {
    1370      154584 :   pari_sp av = avma;
    1371             :   GEN slope;
    1372      154584 :   return gerepileupto(av, FpXQE_dbl_slope(P,a4,T,p,&slope));
    1373             : }
    1374             : 
    1375             : static GEN
    1376       30426 : FpXQE_add_slope(GEN P, GEN Q, GEN a4, GEN T, GEN p, GEN *slope)
    1377             : {
    1378             :   GEN Px, Py, Qx, Qy, R;
    1379       30426 :   if (ell_is_inf(P)) return Q;
    1380       30426 :   if (ell_is_inf(Q)) return P;
    1381       30426 :   Px = gel(P,1); Py = gel(P,2);
    1382       30426 :   Qx = gel(Q,1); Qy = gel(Q,2);
    1383       30426 :   if (ZX_equal(Px, Qx))
    1384             :   {
    1385         543 :     if (ZX_equal(Py, Qy))
    1386           6 :       return FpXQE_dbl_slope(P, a4, T, p, slope);
    1387             :     else
    1388         537 :       return ellinf();
    1389             :   }
    1390       29883 :   *slope = FpXQ_div(FpX_sub(Py, Qy, p), FpX_sub(Px, Qx, p), T, p);
    1391       29883 :   R = cgetg(3,t_VEC);
    1392       29883 :   gel(R, 1) = FpX_sub(FpX_sub(FpXQ_sqr(*slope, T, p), Px, p), Qx, p);
    1393       29883 :   gel(R, 2) = FpX_sub(FpXQ_mul(*slope, FpX_sub(Px, gel(R, 1), p), T, p), Py, p);
    1394       29883 :   return R;
    1395             : }
    1396             : 
    1397             : GEN
    1398       29730 : FpXQE_add(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1399             : {
    1400       29730 :   pari_sp av = avma;
    1401             :   GEN slope;
    1402       29730 :   return gerepileupto(av, FpXQE_add_slope(P,Q,a4,T,p,&slope));
    1403             : }
    1404             : 
    1405             : static GEN
    1406           0 : FpXQE_neg_i(GEN P, GEN p)
    1407             : {
    1408           0 :   if (ell_is_inf(P)) return P;
    1409           0 :   return mkvec2(gel(P,1), FpX_neg(gel(P,2), p));
    1410             : }
    1411             : 
    1412             : GEN
    1413         642 : FpXQE_neg(GEN P, GEN T, GEN p)
    1414             : {
    1415             :   (void) T;
    1416         642 :   if (ell_is_inf(P)) return ellinf();
    1417         642 :   return mkvec2(gcopy(gel(P,1)), FpX_neg(gel(P,2), p));
    1418             : }
    1419             : 
    1420             : GEN
    1421           0 : FpXQE_sub(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1422             : {
    1423           0 :   pari_sp av = avma;
    1424             :   GEN slope;
    1425           0 :   return gerepileupto(av, FpXQE_add_slope(P, FpXQE_neg_i(Q, p), a4, T, p, &slope));
    1426             : }
    1427             : 
    1428             : struct _FpXQE
    1429             : {
    1430             :   GEN a4,a6;
    1431             :   GEN T,p;
    1432             : };
    1433             : 
    1434             : static GEN
    1435      154584 : _FpXQE_dbl(void *E, GEN P)
    1436             : {
    1437      154584 :   struct _FpXQE *ell = (struct _FpXQE *) E;
    1438      154584 :   return FpXQE_dbl(P, ell->a4, ell->T, ell->p);
    1439             : }
    1440             : 
    1441             : static GEN
    1442       29730 : _FpXQE_add(void *E, GEN P, GEN Q)
    1443             : {
    1444       29730 :   struct _FpXQE *ell=(struct _FpXQE *) E;
    1445       29730 :   return FpXQE_add(P, Q, ell->a4, ell->T, ell->p);
    1446             : }
    1447             : 
    1448             : static GEN
    1449        2413 : _FpXQE_mul(void *E, GEN P, GEN n)
    1450             : {
    1451        2413 :   pari_sp av = avma;
    1452        2413 :   struct _FpXQE *e=(struct _FpXQE *) E;
    1453        2413 :   long s = signe(n);
    1454        2413 :   if (!s || ell_is_inf(P)) return ellinf();
    1455        2413 :   if (s<0) P = FpXQE_neg(P, e->T, e->p);
    1456        2413 :   if (is_pm1(n)) return s>0? gcopy(P): P;
    1457        1681 :   return gerepileupto(av, gen_pow(P, n, e, &_FpXQE_dbl, &_FpXQE_add));
    1458             : }
    1459             : 
    1460             : GEN
    1461         732 : FpXQE_mul(GEN P, GEN n, GEN a4, GEN T, GEN p)
    1462             : {
    1463             :   struct _FpXQE E;
    1464         732 :   E.a4= a4; E.T = T; E.p = p;
    1465         732 :   return _FpXQE_mul(&E, P, n);
    1466             : }
    1467             : 
    1468             : /* Finds a random non-singular point on E */
    1469             : 
    1470             : GEN
    1471         839 : random_FpXQE(GEN a4, GEN a6, GEN T, GEN p)
    1472             : {
    1473         839 :   pari_sp ltop = avma;
    1474             :   GEN x, x2, y, rhs;
    1475         839 :   long v = get_FpX_var(T), d = get_FpX_degree(T);
    1476             :   do
    1477             :   {
    1478        1567 :     avma= ltop;
    1479        1567 :     x   = random_FpX(d,v,p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
    1480        1567 :     x2  = FpXQ_sqr(x, T, p);
    1481        1567 :     rhs = FpX_add(FpXQ_mul(x, FpX_add(x2, a4, p), T, p), a6, p);
    1482        1567 :   } while ((!signe(rhs) && !signe(FpX_add(FpX_mulu(x2,3,p), a4, p)))
    1483        3134 :           || !FpXQ_issquare(rhs, T, p));
    1484         839 :   y = FpXQ_sqrt(rhs, T, p);
    1485         839 :   if (!y) pari_err_PRIME("random_FpE", p);
    1486         839 :   return gerepilecopy(ltop, mkvec2(x, y));
    1487             : }
    1488             : 
    1489             : static GEN
    1490         107 : _FpXQE_rand(void *E)
    1491             : {
    1492         107 :   struct _FpXQE *e=(struct _FpXQE *) E;
    1493         107 :   return random_FpXQE(e->a4, e->a6, e->T, e->p);
    1494             : }
    1495             : 
    1496             : static const struct bb_group FpXQE_group={_FpXQE_add,_FpXQE_mul,_FpXQE_rand,hash_GEN,ZXV_equal,ell_is_inf};
    1497             : 
    1498             : const struct bb_group *
    1499           6 : get_FpXQE_group(void ** pt_E, GEN a4, GEN a6, GEN T, GEN p)
    1500             : {
    1501           6 :   struct _FpXQE *e = (struct _FpXQE *) stack_malloc(sizeof(struct _FpXQE));
    1502           6 :   e->a4 = a4; e->a6 = a6; e->T = T; e->p = p;
    1503           6 :   *pt_E = (void *) e;
    1504           6 :   return &FpXQE_group;
    1505             : }
    1506             : 
    1507             : GEN
    1508          12 : FpXQE_order(GEN z, GEN o, GEN a4, GEN T, GEN p)
    1509             : {
    1510          12 :   pari_sp av = avma;
    1511             :   struct _FpXQE e;
    1512          12 :   e.a4=a4; e.T=T; e.p=p;
    1513          12 :   return gerepileuptoint(av, gen_order(z, o, (void*)&e, &FpXQE_group));
    1514             : }
    1515             : 
    1516             : GEN
    1517           0 : FpXQE_log(GEN a, GEN b, GEN o, GEN a4, GEN T, GEN p)
    1518             : {
    1519           0 :   pari_sp av = avma;
    1520             :   struct _FpXQE e;
    1521           0 :   e.a4=a4; e.T=T; e.p=p;
    1522           0 :   return gerepileuptoint(av, gen_PH_log(a, b, o, (void*)&e, &FpXQE_group));
    1523             : }
    1524             : 
    1525             : 
    1526             : /***********************************************************************/
    1527             : /**                                                                   **/
    1528             : /**                            Pairings                               **/
    1529             : /**                                                                   **/
    1530             : /***********************************************************************/
    1531             : 
    1532             : /* Derived from APIP from and by Jerome Milan, 2012 */
    1533             : 
    1534             : static GEN
    1535        5088 : FpXQE_vert(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1536             : {
    1537        5088 :   long vT = get_FpX_var(T);
    1538        5088 :   if (ell_is_inf(P))
    1539          84 :     return pol_1(get_FpX_var(T));
    1540        5004 :   if (!ZX_equal(gel(Q, 1), gel(P, 1)))
    1541        5004 :     return FpX_sub(gel(Q, 1), gel(P, 1), p);
    1542           0 :   if (signe(gel(P,2))!=0) return pol_1(vT);
    1543           0 :   return FpXQ_inv(FpX_add(FpX_mulu(FpXQ_sqr(gel(P,1), T, p), 3, p),
    1544             :                   a4, p), T, p);
    1545             : }
    1546             : 
    1547             : static GEN
    1548        4938 : FpXQE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN T, GEN p)
    1549             : {
    1550        4938 :   long vT = get_FpX_var(T);
    1551        4938 :   GEN x = gel(Q, 1), y = gel(Q, 2);
    1552        4938 :   GEN tmp1  = FpX_sub(x, gel(R, 1), p);
    1553        4938 :   GEN tmp2  = FpX_add(FpXQ_mul(tmp1, slope, T, p), gel(R, 2), p);
    1554        4938 :   if (!ZX_equal(y, tmp2))
    1555        4938 :     return FpX_sub(y, tmp2, p);
    1556           0 :   if (signe(y) == 0)
    1557           0 :     return pol_1(vT);
    1558             :   else
    1559             :   {
    1560             :     GEN s1, s2;
    1561           0 :     GEN y2i = FpXQ_inv(FpX_mulu(y, 2, p), T, p);
    1562           0 :     s1 = FpXQ_mul(FpX_add(FpX_mulu(FpXQ_sqr(x, T, p), 3, p), a4, p), y2i, T, p);
    1563           0 :     if (!ZX_equal(s1, slope))
    1564           0 :       return FpX_sub(s1, slope, p);
    1565           0 :     s2 = FpXQ_mul(FpX_sub(FpX_mulu(x, 3, p), FpXQ_sqr(s1, T, p), p), y2i, T, p);
    1566           0 :     return signe(s2)!=0 ? s2: y2i;
    1567             :   }
    1568             : }
    1569             : 
    1570             : /* Computes the equation of the line tangent to R and returns its
    1571             :    evaluation at the point Q. Also doubles the point R.
    1572             :  */
    1573             : 
    1574             : static GEN
    1575        4308 : FpXQE_tangent_update(GEN R, GEN Q, GEN a4, GEN T, GEN p, GEN *pt_R)
    1576             : {
    1577        4308 :   if (ell_is_inf(R))
    1578             :   {
    1579          18 :     *pt_R = ellinf();
    1580          18 :     return pol_1(get_FpX_var(T));
    1581             :   }
    1582        4290 :   else if (!signe(gel(R,2)))
    1583             :   {
    1584          48 :     *pt_R = ellinf();
    1585          48 :     return FpXQE_vert(R, Q, a4, T, p);
    1586             :   } else {
    1587             :     GEN slope;
    1588        4242 :     *pt_R = FpXQE_dbl_slope(R, a4, T, p, &slope);
    1589        4242 :     return FpXQE_Miller_line(R, Q, slope, a4, T, p);
    1590             :   }
    1591             : }
    1592             : 
    1593             : /* Computes the equation of the line through R and P, and returns its
    1594             :    evaluation at the point Q. Also adds P to the point R.
    1595             :  */
    1596             : 
    1597             : static GEN
    1598         714 : FpXQE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN T, GEN p, GEN *pt_R)
    1599             : {
    1600         714 :   if (ell_is_inf(R))
    1601             :   {
    1602           0 :     *pt_R = gcopy(P);
    1603           0 :     return FpXQE_vert(P, Q, a4, T, p);
    1604             :   }
    1605         714 :   else if (ell_is_inf(P))
    1606             :   {
    1607           0 :     *pt_R = gcopy(R);
    1608           0 :     return FpXQE_vert(R, Q, a4, T, p);
    1609             :   }
    1610         714 :   else if (ZX_equal(gel(P, 1), gel(R, 1)))
    1611             :   {
    1612          18 :     if (ZX_equal(gel(P, 2), gel(R, 2)))
    1613           0 :       return FpXQE_tangent_update(R, Q, a4, T, p, pt_R);
    1614             :     else
    1615             :     {
    1616          18 :       *pt_R = ellinf();
    1617          18 :       return FpXQE_vert(R, Q, a4, T, p);
    1618             :     }
    1619             :   } else {
    1620             :     GEN slope;
    1621         696 :     *pt_R = FpXQE_add_slope(P, R, a4, T, p, &slope);
    1622         696 :     return FpXQE_Miller_line(R, Q, slope, a4, T, p);
    1623             :   }
    1624             : }
    1625             : 
    1626             : /* Returns the Miller function f_{m, Q} evaluated at the point P using
    1627             :    the standard Miller algorithm.
    1628             :  */
    1629             : 
    1630             : struct _FpXQE_miller
    1631             : {
    1632             :   GEN p;
    1633             :   GEN T, a4, P;
    1634             : };
    1635             : 
    1636             : static GEN
    1637        4308 : FpXQE_Miller_dbl(void* E, GEN d)
    1638             : {
    1639        4308 :   struct _FpXQE_miller *m = (struct _FpXQE_miller *)E;
    1640        4308 :   GEN p  = m->p;
    1641        4308 :   GEN T = m->T, a4 = m->a4, P = m->P;
    1642             :   GEN v, line;
    1643        4308 :   GEN num = FpXQ_sqr(gel(d,1), T, p);
    1644        4308 :   GEN denom = FpXQ_sqr(gel(d,2), T, p);
    1645        4308 :   GEN point = gel(d,3);
    1646        4308 :   line = FpXQE_tangent_update(point, P, a4, T, p, &point);
    1647        4308 :   num  = FpXQ_mul(num, line, T, p);
    1648        4308 :   v = FpXQE_vert(point, P, a4, T, p);
    1649        4308 :   denom = FpXQ_mul(denom, v, T, p);
    1650        4308 :   return mkvec3(num, denom, point);
    1651             : }
    1652             : 
    1653             : static GEN
    1654         714 : FpXQE_Miller_add(void* E, GEN va, GEN vb)
    1655             : {
    1656         714 :   struct _FpXQE_miller *m = (struct _FpXQE_miller *)E;
    1657         714 :   GEN p = m->p;
    1658         714 :   GEN T = m->T, a4 = m->a4, P = m->P;
    1659             :   GEN v, line, point;
    1660         714 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
    1661         714 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
    1662         714 :   GEN num   = FpXQ_mul(na, nb, T, p);
    1663         714 :   GEN denom = FpXQ_mul(da, db, T, p);
    1664         714 :   line = FpXQE_chord_update(pa, pb, P, a4, T, p, &point);
    1665         714 :   num  = FpXQ_mul(num, line, T, p);
    1666         714 :   v = FpXQE_vert(point, P, a4, T, p);
    1667         714 :   denom = FpXQ_mul(denom, v, T, p);
    1668         714 :   return mkvec3(num, denom, point);
    1669             : }
    1670             : 
    1671             : static GEN
    1672          66 : FpXQE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN T, GEN p)
    1673             : {
    1674          66 :   pari_sp ltop = avma;
    1675             :   struct _FpXQE_miller d;
    1676             :   GEN v, num, denom, g1;
    1677             : 
    1678          66 :   d.a4 = a4; d.T = T; d.p = p; d.P = P;
    1679          66 :   g1 = pol_1(get_FpX_var(T));
    1680          66 :   v = gen_pow(mkvec3(g1,g1,Q), m, (void*)&d, FpXQE_Miller_dbl, FpXQE_Miller_add);
    1681          66 :   num = gel(v,1); denom = gel(v,2);
    1682          66 :   return gerepileupto(ltop, FpXQ_div(num, denom, T, p));
    1683             : }
    1684             : 
    1685             : GEN
    1686          31 : FpXQE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, GEN p)
    1687             : {
    1688          31 :   pari_sp ltop = avma;
    1689             :   GEN num, denom, result;
    1690          31 :   if (ell_is_inf(P) || ell_is_inf(Q) || ZXV_equal(P,Q))
    1691           1 :     return pol_1(get_FpX_var(T));
    1692          30 :   num    = FpXQE_Miller(P, Q, m, a4, T, p);
    1693          30 :   denom  = FpXQE_Miller(Q, P, m, a4, T, p);
    1694          30 :   result = FpXQ_div(num, denom, T, p);
    1695          30 :   if (mpodd(m))
    1696           0 :     result  = FpX_neg(result, p);
    1697          30 :   return gerepileupto(ltop, result);
    1698             : }
    1699             : 
    1700             : GEN
    1701           6 : FpXQE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, GEN p)
    1702             : {
    1703           6 :   if (ell_is_inf(P) || ell_is_inf(Q))
    1704           0 :     return pol_1(get_FpX_var(T));
    1705           6 :   return FpXQE_Miller(P, Q, m, a4, T, p);
    1706             : }
    1707             : 
    1708             : /***********************************************************************/
    1709             : /**                                                                   **/
    1710             : /**                           issupersingular                         **/
    1711             : /**                                                                   **/
    1712             : /***********************************************************************/
    1713             : 
    1714             : GEN
    1715        1452 : FpXQ_ellj(GEN a4, GEN a6, GEN T, GEN p)
    1716             : {
    1717        1452 :   if (absequaliu(p,3)) return pol_0(get_FpX_var(T));
    1718             :   else
    1719             :   {
    1720        1452 :     pari_sp av=avma;
    1721        1452 :     GEN a43 = FpXQ_mul(a4,FpXQ_sqr(a4,T,p),T,p);
    1722        1452 :     GEN a62 = FpXQ_sqr(a6,T,p);
    1723        1452 :     GEN num = FpX_mulu(a43,6912,p);
    1724        1452 :     GEN den = FpX_add(FpX_mulu(a43,4,p),FpX_mulu(a62,27,p),p);
    1725        1452 :     return gerepileuptoleaf(av, FpXQ_div(num, den, T, p));
    1726             :   }
    1727             : }
    1728             : 
    1729             : int
    1730      140766 : FpXQ_elljissupersingular(GEN j, GEN T, GEN p)
    1731             : {
    1732      140766 :   pari_sp ltop = avma;
    1733             : 
    1734             :   /* All supersingular j-invariants are in FF_{p^2}, so we first check
    1735             :    * whether j is in FF_{p^2}.  If d is odd, then FF_{p^2} is not a
    1736             :    * subfield of FF_{p^d} so the j-invariants are all in FF_p.  Hence
    1737             :    * the j-invariants are in FF_{p^{2 - e}}. */
    1738      140766 :   ulong d = get_FpX_degree(T);
    1739             :   GEN S;
    1740             :   int res;
    1741             : 
    1742      140766 :   if (degpol(j) <= 0) return Fp_elljissupersingular(constant_coeff(j), p);
    1743      140388 :   if (abscmpiu(p, 5) <= 0) return 0; /* j != 0*/
    1744             : 
    1745             :   /* Set S so that FF_p[T]/(S) is isomorphic to FF_{p^2}: */
    1746      140382 :   if (d == 2)
    1747       10854 :     S = T;
    1748             :   else { /* d > 2 */
    1749             :     /* We construct FF_{p^2} = FF_p[t]/((T - j)(T - j^p)) which
    1750             :      * injects into FF_{p^d} via the map T |--> j. */
    1751      129528 :     GEN j_pow_p = FpXQ_pow(j, p, T, p);
    1752      129528 :     GEN j_sum = FpX_add(j, j_pow_p, p), j_prod;
    1753      129528 :     long var = varn(T);
    1754      129528 :     if (degpol(j_sum) > 0) { avma = ltop; return 0; /* j not in Fp^2 */ }
    1755         504 :     j_prod = FpXQ_mul(j, j_pow_p, T, p);
    1756         504 :     if (degpol(j_prod) > 0 ) { avma = ltop; return 0; /* j not in Fp^2 */ }
    1757         504 :     j_sum = constant_coeff(j_sum); j_prod = constant_coeff(j_prod);
    1758         504 :     S = mkpoln(3, gen_1, Fp_neg(j_sum, p), j_prod);
    1759         504 :     setvarn(S, var);
    1760         504 :     j = pol_x(var);
    1761             :   }
    1762       11358 :   res = jissupersingular(j, S, p);
    1763       11358 :   avma = ltop;
    1764       11358 :   return res;
    1765             : }
    1766             : 
    1767             : /***********************************************************************/
    1768             : /**                                                                   **/
    1769             : /**                           Point counting                          **/
    1770             : /**                                                                   **/
    1771             : /***********************************************************************/
    1772             : 
    1773             : GEN
    1774       11712 : elltrace_extension(GEN t, long n, GEN q)
    1775             : {
    1776       11712 :   pari_sp av = avma;
    1777       11712 :   GEN v = RgX_to_RgC(RgXQ_powu(pol_x(0), n, mkpoln(3,gen_1,negi(t),q)),2);
    1778       11712 :   GEN te = addii(shifti(gel(v,1),1), mulii(t,gel(v,2)));
    1779       11712 :   return gerepileuptoint(av, te);
    1780             : }
    1781             : 
    1782             : GEN
    1783       11202 : Fp_ffellcard(GEN a4, GEN a6, GEN q, long n, GEN p)
    1784             : {
    1785       11202 :   pari_sp av = avma;
    1786       11202 :   GEN ap = subii(addiu(p, 1), Fp_ellcard(a4, a6, p));
    1787       11202 :   GEN te = elltrace_extension(ap, n, p);
    1788       11202 :   return gerepileuptoint(av, subii(addiu(q, 1), te));
    1789             : }
    1790             : 
    1791             : static GEN
    1792        1446 : FpXQ_ellcardj(GEN a4, GEN a6, GEN j, GEN T, GEN q, GEN p, long n)
    1793             : {
    1794        1446 :   GEN q1 = addiu(q,1);
    1795        1446 :   if (signe(j)==0)
    1796             :   {
    1797             :     GEN W, w, t, N;
    1798         480 :     if (umodiu(q,6)!=1) return q1;
    1799         360 :     N = Fp_ffellcard(gen_0,gen_1,q,n,p);
    1800         360 :     t = subii(q1, N);
    1801         360 :     W = FpXQ_pow(a6,diviuexact(shifti(q,-1), 3),T,p);
    1802         360 :     if (degpol(W)>0) /*p=5 mod 6*/
    1803         144 :       return ZX_equal1(FpXQ_powu(W,3,T,p)) ? addii(q1,shifti(t,-1)):
    1804          48 :                                              subii(q1,shifti(t,-1));
    1805         264 :     w = modii(gel(W,2),p);
    1806         264 :     if (equali1(w))  return N;
    1807         204 :     if (equalii(w,subiu(p,1))) return addii(q1,t);
    1808             :     else /*p=1 mod 6*/
    1809             :     {
    1810         144 :       GEN u = shifti(t,-1), v = sqrtint(diviuexact(subii(q,sqri(u)),3));
    1811         144 :       GEN a = addii(u,v), b = shifti(v,1);
    1812         144 :       if (equali1(Fp_powu(w,3,p)))
    1813             :       {
    1814          72 :         if (signe(Fp_add(modii(a,p),Fp_mul(w,modii(b,p),p),p))==0)
    1815          42 :           return subii(q1,subii(shifti(b,1),a));
    1816             :         else
    1817          30 :           return addii(q1,addii(a,b));
    1818             :       }
    1819             :       else
    1820             :       {
    1821          72 :         if (signe(Fp_sub(modii(a,p),Fp_mul(w,modii(b,p),p),p))==0)
    1822          42 :           return subii(q1,subii(a,shifti(b,1)));
    1823             :         else
    1824          30 :           return subii(q1,addii(a,b));
    1825             :       }
    1826             :     }
    1827         966 :   } else if (equalii(j,modsi(1728,p)))
    1828             :   {
    1829             :     GEN w, W, N, t;
    1830         486 :     if (mod4(q)==3) return q1;
    1831         366 :     W = FpXQ_pow(a4,shifti(q,-2),T,p);
    1832         366 :     if (degpol(W)>0) return q1; /*p=3 mod 4*/
    1833         330 :     w = modii(gel(W,2),p);
    1834         330 :     N = Fp_ffellcard(gen_1,gen_0,q,n,p);
    1835         330 :     if (equali1(w)) return N;
    1836         210 :     t = subii(q1, N);
    1837         210 :     if (equalii(w,subiu(p,1))) return addii(q1,t);
    1838             :     else /*p=1 mod 4*/
    1839             :     {
    1840          84 :       GEN u = shifti(t,-1), v = sqrtint(subii(q,sqri(u)));
    1841          84 :       if (signe(Fp_add(modii(u,p),Fp_mul(w,modii(v,p),p),p))==0)
    1842          42 :         return subii(q1,shifti(v,1));
    1843             :       else
    1844          42 :         return addii(q1,shifti(v,1));
    1845             :     }
    1846             :   } else
    1847             :   {
    1848         480 :     GEN g = Fp_div(j, Fp_sub(utoi(1728), j, p), p);
    1849         480 :     GEN l = FpXQ_div(FpX_mulu(a6,3,p),FpX_mulu(a4,2,p),T,p);
    1850         480 :     GEN N = Fp_ffellcard(Fp_mulu(g,3,p),Fp_mulu(g,2,p),q,n,p);
    1851         480 :     if (FpXQ_issquare(l,T,p)) return N;
    1852         240 :     return subii(shifti(q1,1),N);
    1853             :   }
    1854             : }
    1855             : 
    1856             : GEN
    1857        3108 : FpXQ_ellcard(GEN a4, GEN a6, GEN T, GEN p)
    1858             : {
    1859        3108 :   pari_sp av = avma;
    1860        3108 :   long n = get_FpX_degree(T);
    1861        3108 :   GEN q = powiu(p, n), r, J;
    1862        3108 :   if (degpol(a4)<=0 && degpol(a6)<=0)
    1863         108 :     r = Fp_ffellcard(constant_coeff(a4),constant_coeff(a6),q,n,p);
    1864        3000 :   else if (lgefint(p)==3)
    1865             :   {
    1866        1548 :     ulong pp = p[2];
    1867        1548 :     r =  Flxq_ellcard(ZX_to_Flx(a4,pp),ZX_to_Flx(a6,pp),ZX_to_Flx(T,pp),pp);
    1868             :   }
    1869        1452 :   else if (degpol(J=FpXQ_ellj(a4,a6,T,p))<=0)
    1870        1446 :     r = FpXQ_ellcardj(a4,a6,constant_coeff(J),T,q,p,n);
    1871             :   else
    1872           6 :     r = Fq_ellcard_SEA(a4, a6, q, T, p, 0);
    1873        3108 :   return gerepileuptoint(av, r);
    1874             : }
    1875             : 
    1876             : static GEN
    1877          25 : _FpXQE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
    1878             : {
    1879          25 :   struct _FpXQE *e = (struct _FpXQE *) E;
    1880          25 :   return  FpXQ_order(FpXQE_weilpairing(P,Q,m,e->a4,e->T,e->p), F, e->T, e->p);
    1881             : }
    1882             : 
    1883             : GEN
    1884          12 : FpXQ_ellgroup(GEN a4, GEN a6, GEN N, GEN T, GEN p, GEN *pt_m)
    1885             : {
    1886             :   struct _FpXQE e;
    1887          12 :   GEN q = powiu(p, get_FpX_degree(T));
    1888          12 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
    1889          12 :   return gen_ellgroup(N, subiu(q,1), pt_m, (void*)&e, &FpXQE_group, _FpXQE_pairorder);
    1890             : }
    1891             : 
    1892             : GEN
    1893           6 : FpXQ_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN T, GEN p)
    1894             : {
    1895             :   GEN P;
    1896           6 :   pari_sp av = avma;
    1897             :   struct _FpXQE e;
    1898           6 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
    1899           6 :   switch(lg(D)-1)
    1900             :   {
    1901             :   case 1:
    1902           6 :     P = gen_gener(gel(D,1), (void*)&e, &FpXQE_group);
    1903           6 :     P = mkvec(FpXQE_changepoint(P, ch, T, p));
    1904           6 :     break;
    1905             :   default:
    1906           0 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FpXQE_group, _FpXQE_pairorder);
    1907           0 :     gel(P,1) = FpXQE_changepoint(gel(P,1), ch, T, p);
    1908           0 :     gel(P,2) = FpXQE_changepoint(gel(P,2), ch, T, p);
    1909           0 :     break;
    1910             :   }
    1911           6 :   return gerepilecopy(av, P);
    1912             : }
    1913             : 
    1914             : 

Generated by: LCOV version 1.11