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.8.0 lcov report (development 19230-c71492b) Lines: 917 1003 91.4 %
Date: 2016-07-30 07:10:28 Functions: 98 106 92.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        1271 : RgE_to_FpE(GEN x, GEN p)
      33             : {
      34        1271 :   if (ell_is_inf(x)) return x;
      35        1273 :   retmkvec2(Rg_to_Fp(gel(x,1),p),Rg_to_Fp(gel(x,2),p));
      36             : }
      37             : 
      38             : GEN
      39         432 : FpE_to_mod(GEN x, GEN p)
      40             : {
      41         432 :   if (ell_is_inf(x)) return x;
      42         376 :   retmkvec2(Fp_to_mod(gel(x,1),p),Fp_to_mod(gel(x,2),p));
      43             : }
      44             : 
      45             : GEN
      46        1104 : FpE_changepoint(GEN x, GEN ch, GEN p)
      47             : {
      48        1104 :   pari_sp av = avma;
      49             :   GEN p1,z,u,r,s,t,v,v2,v3;
      50        1104 :   if (ell_is_inf(x)) return x;
      51        1048 :   u = gel(ch,1); r = gel(ch,2);
      52        1048 :   s = gel(ch,3); t = gel(ch,4);
      53        1048 :   v = Fp_inv(u, p); v2 = Fp_sqr(v,p); v3 = Fp_mul(v,v2,p);
      54        1048 :   p1 = Fp_sub(gel(x,1),r,p);
      55        1048 :   z = cgetg(3,t_VEC);
      56        1048 :   gel(z,1) = Fp_mul(v2, p1, p);
      57        1048 :   gel(z,2) = Fp_mul(v3, Fp_sub(gel(x,2), Fp_add(Fp_mul(s,p1, p),t, p),p),p);
      58        1048 :   return gerepileupto(av, z);
      59             : }
      60             : 
      61             : GEN
      62        2169 : FpE_changepointinv(GEN x, GEN ch, GEN p)
      63             : {
      64             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
      65        2169 :   if (ell_is_inf(x)) return x;
      66        2170 :   X = gel(x,1); Y = gel(x,2);
      67        2170 :   u = gel(ch,1); r = gel(ch,2);
      68        2170 :   s = gel(ch,3); t = gel(ch,4);
      69        2170 :   u2 = Fp_sqr(u, p); u3 = Fp_mul(u,u2,p);
      70        2171 :   u2X = Fp_mul(u2,X, p);
      71        2169 :   z = cgetg(3, t_VEC);
      72        2173 :   gel(z,1) = Fp_add(u2X,r,p);
      73        2172 :   gel(z,2) = Fp_add(Fp_mul(u3,Y,p), Fp_add(Fp_mul(s,u2X,p), t, p), p);
      74        2173 :   return z;
      75             : }
      76             : 
      77             : static GEN
      78         420 : nonsquare_Fp(GEN p)
      79             : {
      80         420 :   pari_sp av = avma;
      81             :   GEN a;
      82             :   do
      83             :   {
      84         826 :     avma = av;
      85         826 :     a = randomi(p);
      86         826 :   } while (kronecker(a, p) >= 0);
      87         420 :   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     1979551 : FpE_dbl_slope(GEN P, GEN a4, GEN p, GEN *slope)
     100             : {
     101             :   GEN x, y, Q;
     102     1979551 :   if (ell_is_inf(P) || !signe(gel(P,2))) return ellinf();
     103     1962015 :   x = gel(P,1); y = gel(P,2);
     104     1962015 :   *slope = Fp_div(Fp_add(Fp_mulu(Fp_sqr(x,p), 3, p), a4, p),
     105             :                   Fp_mulu(y, 2, p), p);
     106     1937630 :   Q = cgetg(3,t_VEC);
     107     1947419 :   gel(Q, 1) = Fp_sub(Fp_sqr(*slope, p), Fp_mulu(x, 2, p), p);
     108     1947868 :   gel(Q, 2) = Fp_sub(Fp_mul(*slope, Fp_sub(x, gel(Q, 1), p), p), y, p);
     109     1950669 :   return Q;
     110             : }
     111             : 
     112             : GEN
     113     1963831 : FpE_dbl(GEN P, GEN a4, GEN p)
     114             : {
     115     1963831 :   pari_sp av = avma;
     116             :   GEN slope;
     117     1963831 :   return gerepileupto(av, FpE_dbl_slope(P,a4,p,&slope));
     118             : }
     119             : 
     120             : static GEN
     121     1275342 : FpE_add_slope(GEN P, GEN Q, GEN a4, GEN p, GEN *slope)
     122             : {
     123             :   GEN Px, Py, Qx, Qy, R;
     124     1275342 :   if (ell_is_inf(P)) return Q;
     125     1273475 :   if (ell_is_inf(Q)) return P;
     126     1273566 :   Px = gel(P,1); Py = gel(P,2);
     127     1273566 :   Qx = gel(Q,1); Qy = gel(Q,2);
     128     1273566 :   if (equalii(Px, Qx))
     129             :   {
     130        5723 :     if (equalii(Py, Qy))
     131         629 :       return FpE_dbl_slope(P, a4, p, slope);
     132             :     else
     133        5094 :       return ellinf();
     134             :   }
     135     1267677 :   *slope = Fp_div(Fp_sub(Py, Qy, p), Fp_sub(Px, Qx, p), p);
     136     1267441 :   R = cgetg(3,t_VEC);
     137     1267705 :   gel(R, 1) = Fp_sub(Fp_sub(Fp_sqr(*slope, p), Px, p), Qx, p);
     138     1267435 :   gel(R, 2) = Fp_sub(Fp_mul(*slope, Fp_sub(Px, gel(R, 1), p), p), Py, p);
     139     1267740 :   return R;
     140             : }
     141             : 
     142             : GEN
     143     1272958 : FpE_add(GEN P, GEN Q, GEN a4, GEN p)
     144             : {
     145     1272958 :   pari_sp av = avma;
     146             :   GEN slope;
     147     1272958 :   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      372582 : FpE_neg(GEN P, GEN p)
     159             : {
     160      372582 :   if (ell_is_inf(P)) return ellinf();
     161      372582 :   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     1965411 : _FpE_dbl(void *E, GEN P)
     180             : {
     181     1965411 :   struct _FpE *ell = (struct _FpE *) E;
     182     1965411 :   return FpE_dbl(P, ell->a4, ell->p);
     183             : }
     184             : 
     185             : static GEN
     186     1253773 : _FpE_add(void *E, GEN P, GEN Q)
     187             : {
     188     1253773 :   struct _FpE *ell=(struct _FpE *) E;
     189     1253773 :   return FpE_add(P, Q, ell->a4, ell->p);
     190             : }
     191             : 
     192             : static GEN
     193      485898 : _FpE_mul(void *E, GEN P, GEN n)
     194             : {
     195      485898 :   pari_sp av = avma;
     196      485898 :   struct _FpE *e=(struct _FpE *) E;
     197      485898 :   long s = signe(n);
     198      485898 :   if (!s || ell_is_inf(P)) return ellinf();
     199      485872 :   if (s<0) P = FpE_neg(P, e->p);
     200      485872 :   if (is_pm1(n)) return s>0? gcopy(P): P;
     201       89565 :   return gerepileupto(av, gen_pow(P, n, e, &_FpE_dbl, &_FpE_add));
     202             : }
     203             : 
     204             : GEN
     205         680 : FpE_mul(GEN P, GEN n, GEN a4, GEN p)
     206             : {
     207             :   struct _FpE E;
     208         680 :   E.a4= a4; E.p = p;
     209         680 :   return _FpE_mul(&E, P, n);
     210             : }
     211             : 
     212             : /* Finds a random non-singular point on E */
     213             : 
     214             : GEN
     215       29275 : random_FpE(GEN a4, GEN a6, GEN p)
     216             : {
     217       29275 :   pari_sp ltop = avma;
     218             :   GEN x, x2, y, rhs;
     219             :   do
     220             :   {
     221       51486 :     avma= ltop;
     222       51486 :     x   = randomi(p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
     223       51486 :     x2  = Fp_sqr(x, p);
     224       51486 :     rhs = Fp_add(Fp_mul(x, Fp_add(x2, a4, p), p), a6, p);
     225       60300 :   } while ((!signe(rhs) && !signe(Fp_add(Fp_mulu(x2,3,p),a4,p)))
     226      102972 :           || kronecker(rhs, p) < 0);
     227       29275 :   y = Fp_sqrt(rhs, p);
     228       29275 :   if (!y) pari_err_PRIME("random_FpE", p);
     229       29275 :   return gerepilecopy(ltop, mkvec2(x, y));
     230             : }
     231             : 
     232             : static GEN
     233       29247 : _FpE_rand(void *E)
     234             : {
     235       29247 :   struct _FpE *e=(struct _FpE *) E;
     236       29247 :   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         840 : get_FpE_group(void ** pt_E, GEN a4, GEN a6, GEN p)
     243             : {
     244         840 :   struct _FpE *e = (struct _FpE *) stack_malloc(sizeof(struct _FpE));
     245         840 :   e->a4 = a4; e->a6 = a6; e->p  = p;
     246         840 :   *pt_E = (void *) e;
     247         840 :   return &FpE_group;
     248             : }
     249             : 
     250             : GEN
     251         819 : FpE_order(GEN z, GEN o, GEN a4, GEN p)
     252             : {
     253         819 :   pari_sp av = avma;
     254             :   struct _FpE e;
     255             :   GEN r;
     256         819 :   if (lgefint(p) == 3)
     257             :   {
     258         713 :     ulong pp = p[2];
     259         713 :     r = Fle_order(ZV_to_Flv(z, pp), o, umodiu(a4,pp), pp);
     260             :   }
     261             :   else
     262             :   {
     263         106 :     e.a4 = a4;
     264         106 :     e.p = p;
     265         106 :     r = gen_order(z, o, (void*)&e, &FpE_group);
     266             :   }
     267         819 :   return gerepileuptoint(av, r);
     268             : }
     269             : 
     270             : GEN
     271          42 : FpE_log(GEN a, GEN b, GEN o, GEN a4, GEN p)
     272             : {
     273          42 :   pari_sp av = avma;
     274             :   struct _FpE e;
     275             :   GEN r;
     276          42 :   if (lgefint(p) == 3)
     277             :   {
     278          42 :     ulong pp = p[2];
     279          42 :     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          42 :   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       54223 : FpE_vert(GEN P, GEN Q, GEN a4, GEN p)
     300             : {
     301       54223 :   if (ell_is_inf(P))
     302       19916 :     return gen_1;
     303       34307 :   if (!equalii(gel(Q, 1), gel(P, 1)))
     304       31696 :     return Fp_sub(gel(Q, 1), gel(P, 1), p);
     305        2611 :   if (signe(gel(P,2))!=0) return gen_1;
     306        2296 :   return Fp_inv(Fp_add(Fp_mulu(Fp_sqr(gel(P,1),p), 3, p), a4, p), p);
     307             : }
     308             : 
     309             : static GEN
     310       18101 : FpE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN p)
     311             : {
     312       18101 :   GEN x = gel(Q, 1), y = gel(Q, 2);
     313       18101 :   GEN tmp1 = Fp_sub(x, gel(R, 1), p);
     314       18101 :   GEN tmp2 = Fp_add(Fp_mul(tmp1, slope, p), gel(R,2), p);
     315       18101 :   if (!equalii(y, tmp2))
     316       16715 :     return Fp_sub(y, tmp2, p);
     317        1386 :   if (signe(y) == 0)
     318        1134 :     return gen_1;
     319             :   else
     320             :   {
     321             :     GEN s1, s2;
     322         252 :     GEN y2i = Fp_inv(Fp_mulu(y, 2, p), p);
     323         252 :     s1 = Fp_mul(Fp_add(Fp_mulu(Fp_sqr(x, p), 3, p), a4, p), y2i, p);
     324         252 :     if (!equalii(s1, slope))
     325         112 :       return Fp_sub(s1, slope, p);
     326         140 :     s2 = Fp_mul(Fp_sub(Fp_mulu(x, 3, p), Fp_sqr(s1, p), p), y2i, p);
     327         140 :     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       33887 : FpE_tangent_update(GEN R, GEN Q, GEN a4, GEN p, GEN *pt_R)
     337             : {
     338       33887 :   if (ell_is_inf(R))
     339             :   {
     340        4172 :     *pt_R = ellinf();
     341        4172 :     return gen_1;
     342             :   }
     343       29715 :   else if (signe(gel(R,2)) == 0)
     344             :   {
     345       14029 :     *pt_R = ellinf();
     346       14029 :     return FpE_vert(R, Q, a4, p);
     347             :   } else {
     348             :     GEN slope;
     349       15686 :     *pt_R = FpE_dbl_slope(R, a4, p, &slope);
     350       15686 :     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        4361 : FpE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN p, GEN *pt_R)
     360             : {
     361        4361 :   if (ell_is_inf(R))
     362             :   {
     363         231 :     *pt_R = gcopy(P);
     364         231 :     return FpE_vert(P, Q, a4, p);
     365             :   }
     366        4130 :   else if (ell_is_inf(P))
     367             :   {
     368           0 :     *pt_R = gcopy(R);
     369           0 :     return FpE_vert(R, Q, a4, p);
     370             :   }
     371        4130 :   else if (equalii(gel(P, 1), gel(R, 1)))
     372             :   {
     373        1715 :     if (equalii(gel(P, 2), gel(R, 2)))
     374           0 :       return FpE_tangent_update(R, Q, a4, p, pt_R);
     375             :     else {
     376        1715 :       *pt_R = ellinf();
     377        1715 :       return FpE_vert(R, Q, a4, p);
     378             :     }
     379             :   } else {
     380             :     GEN slope;
     381        2415 :     *pt_R = FpE_add_slope(P, R, a4, p, &slope);
     382        2415 :     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       33887 : FpE_Miller_dbl(void* E, GEN d)
     397             : {
     398       33887 :   struct _FpE_miller *m = (struct _FpE_miller *)E;
     399       33887 :   GEN p = m->p, a4 = m->a4, P = m->P;
     400             :   GEN v, line;
     401       33887 :   GEN num = Fp_sqr(gel(d,1), p);
     402       33887 :   GEN denom = Fp_sqr(gel(d,2), p);
     403       33887 :   GEN point = gel(d,3);
     404       33887 :   line = FpE_tangent_update(point, P, a4, p, &point);
     405       33887 :   num  = Fp_mul(num, line, p);
     406       33887 :   v = FpE_vert(point, P, a4, p);
     407       33887 :   denom = Fp_mul(denom, v, p);
     408       33887 :   return mkvec3(num, denom, point);
     409             : }
     410             : 
     411             : static GEN
     412        4361 : FpE_Miller_add(void* E, GEN va, GEN vb)
     413             : {
     414        4361 :   struct _FpE_miller *m = (struct _FpE_miller *)E;
     415        4361 :   GEN p = m->p, a4= m->a4, P = m->P;
     416             :   GEN v, line, point;
     417        4361 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
     418        4361 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
     419        4361 :   GEN num   = Fp_mul(na, nb, p);
     420        4361 :   GEN denom = Fp_mul(da, db, p);
     421        4361 :   line = FpE_chord_update(pa, pb, P, a4, p, &point);
     422        4361 :   num  = Fp_mul(num, line, p);
     423        4361 :   v = FpE_vert(point, P, a4, p);
     424        4361 :   denom = Fp_mul(denom, v, p);
     425        4361 :   return mkvec3(num, denom, point);
     426             : }
     427             : 
     428             : static GEN
     429       15513 : FpE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN p)
     430             : {
     431       15513 :   pari_sp ltop = avma;
     432             :   struct _FpE_miller d;
     433             :   GEN v, num, denom;
     434             : 
     435       15513 :   d.a4 = a4; d.p = p; d.P = P;
     436       15513 :   v = gen_pow(mkvec3(gen_1,gen_1,Q), m, (void*)&d, FpE_Miller_dbl, FpE_Miller_add);
     437       15513 :   num = gel(v,1); denom = gel(v,2);
     438       15513 :   return gerepileupto(ltop, Fp_div(num, denom, p));
     439             : }
     440             : 
     441             : GEN
     442       10841 : FpE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN p)
     443             : {
     444       10841 :   pari_sp ltop = avma;
     445             :   GEN num, denom, result;
     446       10841 :   if (ell_is_inf(P) || ell_is_inf(Q) || ZV_equal(P,Q))
     447        3186 :     return gen_1;
     448        7655 :   num    = FpE_Miller(P, Q, m, a4, p);
     449        7655 :   denom  = FpE_Miller(Q, P, m, a4, p);
     450        7655 :   result = Fp_div(num, denom, p);
     451        7655 :   if (mpodd(m))
     452         651 :     result  = Fp_neg(result, p);
     453        7655 :   return gerepileupto(ltop, result);
     454             : }
     455             : 
     456             : GEN
     457         203 : FpE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN p)
     458             : {
     459         203 :   if (ell_is_inf(P) || ell_is_inf(Q))
     460           0 :     return gen_1;
     461         203 :   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      926935 : is_CMj(long J, GEN jn, GEN jd, GEN p)
     473      926935 : { 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       20522 : u2_is_CMj(ulong a, ulong b, GEN jn, GEN jd, GEN p)
     478             : {
     479       20522 :   GEN mJ = uu32toi(a,b);
     480       20522 :   return remii(addii(mulii(jd,mJ), jn), p) == gen_0;
     481             : }
     482             : #endif
     483             : 
     484             : static long
     485       73773 : 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       73773 :   CHECK(-3,  0);
     489       73731 :   CHECK(-4,  1728);
     490       73682 :   CHECK(-7,  -3375);
     491       73521 :   CHECK(-8,  8000);
     492       73360 :   CHECK(-11, -32768);
     493       73199 :   CHECK(-12, 54000);
     494       72905 :   CHECK(-16, 287496);
     495       72744 :   CHECK(-19, -884736);
     496       72569 :   CHECK(-27, -12288000);
     497       72261 :   CHECK(-28, 16581375);
     498       72058 :   CHECK(-43, -884736000);
     499             : #ifdef LONG_IS_64BIT
     500       61620 :   CHECK(-67, -147197952000L);
     501       61512 :   CHECK(-163, -262537412640768000L);
     502             : #else
     503       10270 :   if (u2_is_CMj(0x00000022UL,0x45ae8000UL,jn,jd,p)) return -67;
     504       10252 :   if (u2_is_CMj(0x03a4b862UL,0xc4b40000UL,jn,jd,p)) return -163;
     505             : #endif
     506             : #undef CHECK
     507       71603 :   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        5509 : FqX_quad_root(GEN x, GEN T, GEN p)
     519             : {
     520        5509 :   GEN b = gel(x,3), c = gel(x,2);
     521        5509 :   GEN D = Fq_sub(Fq_sqr(b, T, p), Fq_mulu(c,4, T, p), T, p);
     522        5509 :   GEN s = Fq_sqrt(D,T, p);
     523        5509 :   if (!s) return NULL;
     524        3234 :   return Fq_Fp_mul(Fq_sub(s, b, T, p), shifti(addis(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        2527 : path_extends_to_floor(GEN j_prev, GEN j, GEN T, GEN p, GEN Phi2, ulong max_len)
     534             : {
     535        2527 :   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        5761 :   for (d = 1; d < max_len; ++d) {
     543             :     GEN j_next;
     544             : 
     545        5509 :     Phi2_j = FqX_div_by_X_x(FqXY_evalx(Phi2, j, T, p), j_prev, T, p, NULL);
     546        5509 :     j_next = FqX_quad_root(Phi2_j, T, p);
     547        5509 :     if (!j_next)
     548             :     { /* j is on the floor */
     549        2275 :       avma = ltop;
     550        2275 :       return 1;
     551             :     }
     552             : 
     553        3234 :     j_prev = j; j = j_next;
     554        3234 :     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         252 :   Phi2_j = FqX_div_by_X_x(FqXY_evalx(Phi2, j, T, p), j_prev, T, p, NULL);
     561         252 :   mult = FqX_nbroots(Phi2_j, T, p);
     562         252 :   avma = ltop;
     563         252 :   return mult == 0;
     564             : }
     565             : 
     566             : static int
     567       13783 : jissupersingular(GEN j, GEN S, GEN p)
     568             : {
     569       13783 :   long max_path_len = expi(p)+1;
     570       13783 :   GEN Phi2 = FpXX_red(polmodular_ZXX(2,0,0,1), p);
     571       13783 :   GEN Phi2_j = FqXY_evalx(Phi2, j, S, p);
     572       13783 :   GEN roots = FpXQX_roots(Phi2_j, S, p);
     573       13783 :   long nbroots = lg(roots)-1;
     574       13783 :   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       13783 :   if (nbroots==0 || (nbroots==1 && FqX_is_squarefree(Phi2_j, S, p)))
     579       11396 :     res = 0;
     580             :   else {
     581        2387 :     long i, l = lg(roots);
     582        2541 :     for (i = 1; i < l; ++i) {
     583        2527 :       if (path_extends_to_floor(j, gel(roots, i), S, p, Phi2, max_path_len)) {
     584        2373 :         res = 0;
     585        2373 :         break;
     586             :       }
     587             :     }
     588             :   }
     589             :   /* If none of the paths reached the floor, then the j-invariant is
     590             :    * supersingular. */
     591       13783 :   return res;
     592             : }
     593             : 
     594             : int
     595         952 : Fp_elljissupersingular(GEN j, GEN p)
     596             : {
     597         952 :   pari_sp ltop = avma;
     598             :   long CM;
     599         952 :   if (cmpiu(p, 5) <= 0) return signe(j) == 0; /* valid if p <= 5 */
     600         875 :   CM = Fp_ellj_get_CM(j, gen_1, p);
     601         875 :   if (CM < 0) return krosi(CM, p) < 0; /* valid if p > 3 */
     602             :   else
     603             :   {
     604         553 :     GEN S = init_Fq(p, 2, fetch_var());
     605         553 :     int res = jissupersingular(j, S, p);
     606         553 :     (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      204417 : Fl_elltrace_naive(ulong a4, ulong a6, ulong p)
     619             : {
     620             :   ulong i, j;
     621      204417 :   long a = 0;
     622             :   long d0, d1, d2, d3;
     623      204417 :   GEN k = const_vecsmall(p, -1);
     624      204417 :   k[1] = 0;
     625    61780431 :   for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p))
     626    61576014 :     k[j+1] = 1;
     627      204417 :   d0 = 6%p; d1 = d0; d2 = Fl_add(a4, 1, p); d3 = a6;
     628   123356445 :   for(i=0;; i++)
     629             :   {
     630   123356445 :     a -= k[1+d3];
     631   123356445 :     if (i==p-1) break;
     632   123152028 :     d3 = Fl_add(d3, d2, p);
     633   123152028 :     d2 = Fl_add(d2, d1, p);
     634   123152028 :     d1 = Fl_add(d1, d0, p);
     635   123152028 :   }
     636      204417 :   return a;
     637             : }
     638             : 
     639             : /* z1 <-- z1 + z2, with precomputed inverse */
     640             : static void
     641      305362 : 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      305362 :   x1 = gel(z1,1); y1 = gel(z1,2);
     646      305362 :   x2 = gel(z2,1); y2 = gel(z2,2);
     647      305362 :   if (x1 == x2)
     648          66 :     p1 = Fp_add(a4, mulii(x1,mului(3,x1)), p);
     649             :   else
     650      305296 :     p1 = Fp_sub(y2,y1, p);
     651             : 
     652      305362 :   p1 = Fp_mul(p1, p2inv, p);
     653      305362 :   x = Fp_sub(sqri(p1), addii(x1,x2), p);
     654      305362 :   y = Fp_sub(mulii(p1,subii(x1,x)), y1, p);
     655      305362 :   affii(x, x1);
     656      305362 :   affii(y, y1);
     657      305362 : }
     658             : 
     659             : /* make sure *x has lgefint >= k */
     660             : static void
     661       18872 : _fix(GEN x, long k)
     662             : {
     663       18872 :   GEN y = (GEN)*x;
     664       18872 :   if (lgefint(y) < k) { GEN p1 = cgeti(k); affii(y,p1); *x = (long)p1; }
     665       18872 : }
     666             : 
     667             : /* Return the lift of a (mod b), which is closest to c */
     668             : static GEN
     669      410200 : closest_lift(GEN a, GEN b, GEN c)
     670             : {
     671      410200 :   return addii(a, mulii(b, diviiround(subii(c,a), b)));
     672             : }
     673             : 
     674             : static long
     675          77 : get_table_size(GEN pordmin, GEN B)
     676             : {
     677          77 :   pari_sp av = avma;
     678          77 :   GEN t = ceilr( sqrtr( divri(itor(pordmin, DEFAULTPREC), B) ) );
     679          77 :   if (is_bigint(t))
     680           0 :     pari_err_OVERFLOW("ellap [large prime: install the 'seadata' package]");
     681          77 :   avma = av;
     682          77 :   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        7021 : Fl_ellpoint(long KRO, ulong *px, ulong c4, ulong c6, ulong p)
     703             : {
     704        7021 :   ulong t, u, x = *px;
     705             :   for(;;)
     706             :   {
     707       13335 :     if (++x >= p) pari_err_PRIME("ellap",utoi(p));
     708       13335 :     t = Fl_add(c4, Fl_sqr(x,p), p);
     709       13335 :     u = Fl_add(c6, Fl_mul(x, t, p), p);
     710       13335 :     if (krouu(u,p) == KRO) break;
     711        6314 :   }
     712        7021 :   *px = x;
     713        7021 :   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          77 : 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          77 :   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          77 :   tx = NULL;
     727          77 :   ty = ti = NULL; /* gcc -Wall */
     728             : 
     729          77 :   if (!signe(c6)) {
     730           0 :     GEN ap = ap_j1728(c4, p);
     731           0 :     return gerepileuptoint(av, subii(addiu(p,1), ap));
     732             :   }
     733             : 
     734          77 :   if (DEBUGLEVEL >= 6) timer_start(&T);
     735             :   /* once #E(Fp) is know mod B >= pordmin, it is completely determined */
     736          77 :   pordmin = addis(sqrti(gmul2n(p,4)), 1); /* ceil( 4sqrt(p) ) */
     737          77 :   p1p = addsi(1, p);
     738          77 :   p2p = shifti(p1p, 1);
     739          77 :   x = 0; KRO = 0;
     740             :   /* how many 2-torsion points ? */
     741          77 :   switch(FpX_nbroots(mkpoln(4, gen_1, gen_0, c4, c6), p))
     742             :   {
     743           9 :     case 3:  A = gen_0; B = utoipos(4); break;
     744          31 :     case 1:  A = gen_0; B = gen_2; break;
     745          37 :     default: A = gen_1; B = gen_2; break; /* 0 */
     746             :   }
     747             :   for(;;)
     748             :   {
     749          77 :     h = closest_lift(A, B, p1p);
     750          77 :     if (!KRO) /* first time, initialize */
     751             :     {
     752          77 :       KRO = kronecker(c6,p);
     753          77 :       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          77 :     a4 = modii(mulii(c4, gel(f,2)), p); /* c4 for E_u */
     766          77 :     fh = FpE_mul(f, h, a4, p);
     767          77 :     if (ell_is_inf(fh)) goto FOUND;
     768             : 
     769          77 :     s = get_table_size(pordmin, B);
     770             :     /* look for h s.t f^h = 0 */
     771          77 :     if (!tx)
     772             :     { /* first time: initialize */
     773          77 :       tx = newblock(3*(s+1));
     774          77 :       ty = tx + (s+1);
     775          77 :       ti = ty + (s+1);
     776             :     }
     777          77 :     F = FpE_mul(f,B,a4,p);
     778          77 :     *tx = evaltyp(t_VECSMALL) | evallg(s+1);
     779             : 
     780             :     /* F = B.f */
     781          77 :     P = gcopy(fh);
     782          77 :     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          77 :     nb = minss(128, s >> 1); /* > 0. Will do nb pts at a time: faster inverse */
     795          77 :     pts = cgetg(nb+1, t_VEC);
     796          77 :     j = lgefint(p);
     797        9513 :     for (i=1; i<=nb; i++)
     798             :     { /* baby steps */
     799        9436 :       gel(pts,i) = P; /* h.f + (i-1).F */
     800        9436 :       _fix(P+1, j); tx[i] = mod2BIL(gel(P,1));
     801        9436 :       _fix(P+2, j); ty[i] = mod2BIL(gel(P,2));
     802        9436 :       P = FpE_add(P,F,a4,p); /* h.f + i.F */
     803        9436 :       if (ell_is_inf(P)) { h = addii(h, mului(i,B)); goto FOUND; }
     804             :     }
     805          77 :     mfh = FpE_neg(fh, p);
     806          77 :     fg = FpE_add(P,mfh,a4,p); /* h.f + nb.F - h.f = nb.F */
     807          77 :     if (ell_is_inf(fg)) { h = mului(nb,B); goto FOUND; }
     808          77 :     u = cgetg(nb+1, t_VEC);
     809          77 :     av2 = avma; /* more baby steps, nb points at a time */
     810        1431 :     while (i <= s)
     811             :     {
     812             :       long maxj;
     813      164151 :       for (j=1; j<=nb; j++) /* adding nb.F (part 1) */
     814             :       {
     815      162874 :         P = gel(pts,j); /* h.f + (i-nb-1+j-1).F */
     816      162874 :         gel(u,j) = subii(gel(fg,1), gel(P,1));
     817      162874 :         if (!signe(gel(u,j))) /* sum = 0 or doubling */
     818             :         {
     819           1 :           long k = i+j-2;
     820           1 :           if (equalii(gel(P,2),gel(fg,2))) k -= 2*nb; /* fg == P */
     821           1 :           h = addii(h, mulsi(k,B)); goto FOUND;
     822             :         }
     823             :       }
     824        1277 :       v = FpV_inv(u, p);
     825        1277 :       maxj = (i-1 + nb <= s)? nb: s % nb;
     826      160461 :       for (j=1; j<=maxj; j++,i++) /* adding nb.F (part 2) */
     827             :       {
     828      159184 :         P = gel(pts,j);
     829      159184 :         FpE_add_ip(P,fg, a4,p, gel(v,j));
     830      159184 :         tx[i] = mod2BIL(gel(P,1));
     831      159184 :         ty[i] = mod2BIL(gel(P,2));
     832             :       }
     833        1277 :       avma = av2;
     834             :     }
     835          76 :     P = FpE_add(gel(pts,j-1),mfh,a4,p); /* = (s-1).F */
     836          76 :     if (ell_is_inf(P)) { h = mului(s-1,B); goto FOUND; }
     837          76 :     if (DEBUGLEVEL >= 6)
     838           0 :       timer_printf(&T, "[Fp_ellcard_Shanks] baby steps, s = %ld",s);
     839             : 
     840             :     /* giant steps: fg = s.F */
     841          76 :     fg = FpE_add(P,F,a4,p);
     842          76 :     if (ell_is_inf(fg)) { h = mului(s,B); goto FOUND; }
     843          76 :     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          76 :     p1 = vecsmall_indexsort(tx); /* = permutation sorting tx */
     847          76 :     for (i=1; i<=s; i++) ti[i] = tx[p1[i]];
     848             :     /* ti = tx sorted */
     849          76 :     for (i=1; i<=s; i++) { tx[i] = ti[i]; ti[i] = ty[p1[i]]; }
     850             :     /* tx is sorted. ti = ty sorted */
     851          76 :     for (i=1; i<=s; i++) { ty[i] = ti[i]; ti[i] = p1[i]; }
     852             :     /* ty is sorted. ti = permutation sorting tx */
     853          76 :     if (DEBUGLEVEL >= 6) timer_printf(&T, "[Fp_ellcard_Shanks] sorting");
     854          76 :     avma = av2;
     855             : 
     856          76 :     gaffect(fg, gel(pts,1));
     857        9357 :     for (j=2; j<=nb; j++) /* pts[j] = j.fg = (s*j).F */
     858             :     {
     859        9281 :       P = FpE_add(gel(pts,j-1),fg,a4,p);
     860        9281 :       if (ell_is_inf(P)) { h = mulii(mulss(s,j), B); goto FOUND; }
     861        9281 :       gaffect(P, gel(pts,j));
     862             :     }
     863             :     /* replace fg by nb.fg since we do nb points at a time */
     864          76 :     avma = av2;
     865          76 :     fg = gcopy(gel(pts,nb)); /* copy: we modify (temporarily) pts[nb] below */
     866          76 :     av2 = avma;
     867             : 
     868      151888 :     for (i=1,j=1; ; i++)
     869             :     {
     870      151888 :       GEN ftest = gel(pts,j);
     871      151888 :       long m, l = 1, r = s+1;
     872             :       long k, k2, j2;
     873             : 
     874      151888 :       avma = av2;
     875      151888 :       k = mod2BIL(gel(ftest,1));
     876     2080615 :       while (l < r)
     877             :       {
     878     1776839 :         m = (l+r) >> 1;
     879     1776839 :         if (tx[m] < k) l = m+1; else r = m;
     880             :       }
     881      151888 :       if (r <= s && tx[r] == k)
     882             :       {
     883          76 :         while (r && tx[r] == k) r--;
     884          76 :         k2 = mod2BIL(gel(ftest,2));
     885          76 :         for (r++; r <= s && tx[r] == k; r++)
     886          76 :           if (ty[r] == k2 || ty[r] == pfinal - k2)
     887             :           { /* [h+j2] f == +/- ftest (= [i.s] f)? */
     888          76 :             j2 = ti[r] - 1;
     889          76 :             if (DEBUGLEVEL >=6)
     890           0 :               timer_printf(&T, "[Fp_ellcard_Shanks] giant steps, i = %ld",i);
     891          76 :             P = FpE_add(FpE_mul(F,stoi(j2),a4,p),fh,a4,p);
     892          76 :             if (equalii(gel(P,1), gel(ftest,1)))
     893             :             {
     894          76 :               if (equalii(gel(P,2), gel(ftest,2))) i = -i;
     895          76 :               h = addii(h, mulii(addis(mulss(s,i), j2), B));
     896          76 :               goto FOUND;
     897             :             }
     898             :           }
     899             :       }
     900      151812 :       if (++j > nb)
     901             :       { /* compute next nb points */
     902        1146 :         long save = 0; /* gcc -Wall */;
     903      147324 :         for (j=1; j<=nb; j++)
     904             :         {
     905      146178 :           P = gel(pts,j);
     906      146178 :           gel(u,j) = subii(gel(fg,1), gel(P,1));
     907      146178 :           if (gel(u,j) == gen_0) /* occurs once: i = j = nb, P == fg */
     908             :           {
     909          66 :             gel(u,j) = shifti(gel(P,2),1);
     910          66 :             save = fg[1]; fg[1] = P[1];
     911             :           }
     912             :         }
     913        1146 :         v = FpV_inv(u, p);
     914      147324 :         for (j=1; j<=nb; j++)
     915      146178 :           FpE_add_ip(gel(pts,j),fg,a4,p, gel(v,j));
     916        1146 :         if (i == nb) { fg[1] = save; }
     917        1146 :         j = 1;
     918             :       }
     919      151812 :     }
     920             : FOUND: /* found a point of exponent h on E_u */
     921          77 :     h = FpE_order(f, h, a4, p);
     922             :     /* h | #E_u(Fp) = A (mod B) */
     923          77 :     A = Z_chinese_all(A, gen_0, B, h, &B);
     924          77 :     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          77 :   if (tx) killblock(tx);
     930          77 :   h = closest_lift(A, B, p1p);
     931          77 :   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    21609093 : 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      417067 : uclosest_lift(ulong a, ulong b, ulong c)
     947             : {
     948      417067 :   ulong x = (b + ((c-a) << 1)) / (b << 1);
     949      417067 :   return a + b * x;
     950             : }
     951             : 
     952             : static long
     953      374942 : Fle_dbl_inplace(GEN P, ulong a4, ulong p)
     954             : {
     955             :   ulong x, y, slope;
     956      374942 :   if (!P[2]) return 1;
     957      374907 :   x = P[1]; y = P[2];
     958      374907 :   slope = Fl_div(Fl_add(Fl_triple(Fl_sqr(x,p), p), a4, p),
     959             :                  Fl_double(y, p), p);
     960      374907 :   P[1] = Fl_sub(Fl_sqr(slope, p), Fl_double(x, p), p);
     961      374907 :   P[2] = Fl_sub(Fl_mul(slope, Fl_sub(x, P[1], p), p), y, p);
     962      374907 :   return 0;
     963             : }
     964             : 
     965             : static long
     966    10097820 : Fle_add_inplace(GEN P, GEN Q, ulong a4, ulong p)
     967             : {
     968             :   ulong Px, Py, Qx, Qy, slope;
     969    10097820 :   if (ell_is_inf(Q)) return 0;
     970    10097820 :   Px = P[1]; Py = P[2];
     971    10097820 :   Qx = Q[1]; Qy = Q[2];
     972    10097820 :   if (Px==Qx)
     973      389068 :     return Py==Qy ? Fle_dbl_inplace(P, a4, p): 1;
     974     9708752 :   slope = Fl_div(Fl_sub(Py, Qy, p), Fl_sub(Px, Qx, p), p);
     975     9708752 :   P[1] = Fl_sub(Fl_sub(Fl_sqr(slope, p), Px, p), Qx, p);
     976     9708752 :   P[2] = Fl_sub(Fl_mul(slope, Fl_sub(Px, P[1], p), p), Py, p);
     977     9708752 :   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      410060 : 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      410060 :   pari_sp av = avma;
     989             :   multiple *table;
     990             : 
     991      410060 :   if (!c6) {
     992          14 :     GEN ap = ap_j1728(utoi(c4), utoipos(p));
     993          14 :     avma = av; return p+1 - itos(ap);
     994             :   }
     995             : 
     996      410046 :   pordmin = (ulong)(1 + 4*sqrt((double)p));
     997      410046 :   p1p = p+1;
     998      410046 :   p2p = p1p << 1;
     999      410046 :   x = 0; KRO = 0;
    1000      410046 :   switch(Flx_nbroots(mkvecsmall5(0L, c6,c4,0L,1L), p))
    1001             :   {
    1002       70481 :     case 3:  A = 0; B = 4; break;
    1003      199245 :     case 1:  A = 0; B = 2; break;
    1004      140320 :     default: A = 1; B = 2; break; /* 0 */
    1005             :   }
    1006             :   for(;;)
    1007             :   { /* see comments in Fp_ellcard_Shanks */
    1008      417067 :     h = uclosest_lift(A, B, p1p);
    1009      417067 :     if (!KRO) /* first time, initialize */
    1010             :     {
    1011      410046 :       KRO = krouu(c6,p); /* != 0 */
    1012      410046 :       f = mkvecsmall2(0, Fl_sqr(c6,p));
    1013             :     }
    1014             :     else
    1015             :     {
    1016        7021 :       KRO = -KRO;
    1017        7021 :       f = Fl_ellpoint(KRO, &x, c4,c6,p);
    1018             :     }
    1019      417067 :     cp4 = Fl_mul(c4, f[2], p);
    1020      417067 :     fh = Fle_mulu(f, h, cp4, p);
    1021      417067 :     if (ell_is_inf(fh)) goto FOUND;
    1022             : 
    1023      411166 :     s = (ulong) (sqrt(((double)pordmin)/B) / 2);
    1024      411166 :     if (!s) s = 1;
    1025      411166 :     table = (multiple *) stack_malloc((s+1) * sizeof(multiple));
    1026      411166 :     F = Fle_mulu(f, B, cp4, p);
    1027     5823606 :     for (i=0; i < s; i++)
    1028             :     {
    1029     5426601 :       table[i].x = fh[1];
    1030     5426601 :       table[i].y = fh[2];
    1031     5426601 :       table[i].i = i;
    1032     5426601 :       if (Fle_add_inplace(fh, F, cp4, p)) { h += B*(i+1); goto FOUND; }
    1033             :     }
    1034      397005 :     qsort(table,s,sizeof(multiple),(QSCOMP)compare_multiples);
    1035      397005 :     fg = Fle_mulu(F, s, cp4, p); ftest = zv_copy(fg);
    1036      397005 :     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     5068224 :     for (i=1; ; i++)
    1041             :     {
    1042     5068224 :       l=0; r=s;
    1043    36294009 :       while (l<r)
    1044             :       {
    1045    26157561 :         ulong m = (l+r) >> 1;
    1046    26157561 :         if (table[m].x < uel(ftest,1)) l=m+1; else r=m;
    1047             :       }
    1048     5068224 :       if (r < s && table[r].x == uel(ftest,1)) break;
    1049     4671219 :       if (Fle_add_inplace(ftest, fg, cp4, p))
    1050           0 :         pari_err_PRIME("ellap",utoi(p));
    1051     4671219 :     }
    1052      397005 :     h += table[r].i * B;
    1053      397005 :     if (table[r].y == uel(ftest,2))
    1054      204489 :       h -= s * i * B;
    1055             :     else
    1056      192516 :       h += s * i * B;
    1057             : FOUND:
    1058      417067 :     h = itou(Fle_order(f, utoipos(h), cp4, p));
    1059             :     /* h | #E_u(Fp) = A (mod B) */
    1060             :     {
    1061             :       GEN C;
    1062      417067 :       A = itou( Z_chinese_all(gen_0, utoi(A), utoipos(h), utoipos(B), &C) );
    1063      417067 :       if (cmpiu(C, pordmin) >= 0) { /* uclosest_lift could overflow */
    1064      410046 :         h = itou( closest_lift(utoi(A), C, utoipos(p1p)) );
    1065      410046 :         break;
    1066             :       }
    1067        7021 :       B = itou(C);
    1068             :     }
    1069        7021 :     A = (p2p - A) % B; avma = av;
    1070        7021 :   }
    1071      610682 :   avma = av; return KRO==1? h: p2p-h;
    1072             : }
    1073             : 
    1074             : /** ellap from CM (original code contributed by Mark Watkins) **/
    1075             : 
    1076             : static ulong
    1077     3924102 : Mod16(GEN x) {
    1078     3924102 :   long s = signe(x);
    1079             :   ulong m;
    1080     3924102 :   if (!s) return 0;
    1081     3924102 :   m = mod16(x); if (!m) return m;
    1082     3760855 :   if (s < 0) m = 16 - m;
    1083     3760855 :   return m;
    1084             : }
    1085             : #define Mod2(x) (Mod16(x) & 1)
    1086             : #define Mod4(x) (Mod16(x) & 3)
    1087             : #define Mod8(x) (Mod16(x) & 7)
    1088             : 
    1089             : static GEN
    1090       43386 : ap_j0(GEN a6,GEN p)
    1091             : {
    1092             :   GEN a, b, e, d;
    1093       43386 :   if (umodiu(p,3) != 1) return gen_0;
    1094       21609 :   (void)cornacchia2(utoipos(27),p, &a,&b);
    1095       21609 :   if (umodiu(a, 3) == 1) a = negi(a);
    1096       21609 :   d = mulis(a6,-108);
    1097       21609 :   e = diviuexact(shifti(p,-1), 3); /* (p-1) / 6 */
    1098       21609 :   return centermod(mulii(a, Fp_pow(d, e, p)), p);
    1099             : }
    1100             : static GEN
    1101     2617825 : ap_j1728(GEN a4,GEN p)
    1102             : {
    1103             :   GEN a, b, e;
    1104     2617825 :   if (mod4(p) != 1) return gen_0;
    1105     1307922 :   (void)cornacchia2(utoipos(4),p, &a,&b);
    1106     1307922 :   if (Mod4(a)==0) a = b;
    1107     1307922 :   if (Mod2(a)==1) a = shifti(a,1);
    1108     1307922 :   if (Mod8(a)==6) a = negi(a);
    1109     1307922 :   e = shifti(p,-2); /* (p-1) / 4 */
    1110     1307922 :   return centermod(mulii(a, Fp_pow(a4, e, p)), p);
    1111             : }
    1112             : static GEN
    1113         140 : ap_j8000(GEN a6, GEN p)
    1114             : {
    1115             :   GEN a, b;
    1116         140 :   long r = mod8(p), s = 1;
    1117         140 :   if (r != 1 && r != 3) return gen_0;
    1118          63 :   (void)cornacchia2(utoipos(8),p, &a,&b);
    1119          63 :   switch(Mod16(a)) {
    1120          28 :     case 2: case 6:   if (Mod4(b)) s = -s;
    1121          28 :       break;
    1122          35 :     case 10: case 14: if (!Mod4(b)) s = -s;
    1123          35 :       break;
    1124             :   }
    1125          63 :   if (kronecker(mulis(a6, 42), p) < 0) s = -s;
    1126          63 :   return s > 0? a: negi(a);
    1127             : }
    1128             : static GEN
    1129         161 : ap_j287496(GEN a6, GEN p)
    1130             : {
    1131             :   GEN a, b;
    1132         161 :   long s = 1;
    1133         161 :   if (mod4(p) != 1) return gen_0;
    1134          70 :   (void)cornacchia2(utoipos(4),p, &a,&b);
    1135          70 :   if (Mod4(a)==0) a = b;
    1136          70 :   if (Mod2(a)==1) a = shifti(a,1);
    1137          70 :   if (Mod8(a)==6) s = -s;
    1138          70 :   if (krosi(2,p) < 0) s = -s;
    1139          70 :   if (kronecker(mulis(a6, -14), p) < 0) s = -s;
    1140          70 :   return s > 0? a: negi(a);
    1141             : }
    1142             : static GEN
    1143        1547 : ap_cm(int CM, long A6B, GEN a6, GEN p)
    1144             : {
    1145             :   GEN a, b;
    1146        1547 :   long s = 1;
    1147        1547 :   if (krosi(CM,p) < 0) return gen_0;
    1148         777 :   (void)cornacchia2(utoipos(-CM),p, &a, &b);
    1149         777 :   if ((CM&3) == 0) CM >>= 2;
    1150         777 :   if ((krois(a, -CM) > 0) ^ (CM == -7)) s = -s;
    1151         777 :   if (kronecker(mulis(a6,A6B), p) < 0) s = -s;
    1152         777 :   return s > 0? a: negi(a);
    1153             : }
    1154             : static GEN
    1155       11522 : ec_ap_cm(int CM, GEN a4, GEN a6, GEN p)
    1156             : {
    1157       11522 :   switch(CM)
    1158             :   {
    1159           0 :     case  -3: return ap_j0(a6, p);
    1160        9674 :     case  -4: return ap_j1728(a4, p);
    1161         140 :     case  -8: return ap_j8000(a6, p);
    1162         161 :     case -16: return ap_j287496(a6, p);
    1163         140 :     case  -7: return ap_cm(CM, -2, a6, p);
    1164         147 :     case -11: return ap_cm(CM, 21, a6, p);
    1165         252 :     case -12: return ap_cm(CM, 22, a6, p);
    1166         126 :     case -19: return ap_cm(CM, 1, a6, p);
    1167         266 :     case -27: return ap_cm(CM, 253, a6, p);
    1168         182 :     case -28: return ap_cm(-7, -114, a6, p); /* yes, -7 ! */
    1169         161 :     case -43: return ap_cm(CM, 21, a6, p);
    1170         126 :     case -67: return ap_cm(CM, 217, a6, p);
    1171         147 :     case -163:return ap_cm(CM, 185801, a6, p);
    1172           0 :     default: return NULL;
    1173             :   }
    1174             : }
    1175             : 
    1176             : static GEN
    1177       72898 : Fp_ellj_nodiv(GEN a4, GEN a6, GEN p)
    1178             : {
    1179       72898 :   GEN a43 = Fp_mulu(Fp_powu(a4, 3, p), 4, p);
    1180       72898 :   GEN a62 = Fp_mulu(Fp_sqr(a6, p), 27, p);
    1181       72898 :   return mkvec2(Fp_mulu(a43, 1728, p), Fp_add(a43, a62, p));
    1182             : }
    1183             : 
    1184             : GEN
    1185           0 : Fp_ellj(GEN a4, GEN a6, GEN p)
    1186             : {
    1187           0 :   pari_sp av=avma;
    1188           0 :   GEN z = Fp_ellj_nodiv(a4, a6, p);
    1189           0 :   return gerepileuptoint(av,Fp_div(gel(z,1),gel(z,2),p));
    1190             : }
    1191             : 
    1192             : static GEN /* Only compute a mod p, so assume p>=17 */
    1193     2724421 : Fp_ellcard_CM(GEN a4, GEN a6, GEN p)
    1194             : {
    1195     2724421 :   pari_sp av = avma;
    1196             :   GEN a;
    1197     2724421 :   if (!signe(a4)) a = ap_j0(a6,p);
    1198     2681035 :   else if (!signe(a6)) a = ap_j1728(a4,p);
    1199             :   else
    1200             :   {
    1201       72898 :     GEN j = Fp_ellj_nodiv(a4, a6, p);
    1202       72898 :     long CM = Fp_ellj_get_CM(gel(j,1), gel(j,2), p);
    1203       72898 :     if (!CM) { avma = av; return NULL; }
    1204        1848 :     a = ec_ap_cm(CM,a4,a6,p);
    1205             :   }
    1206     2653371 :   return gerepileuptoint(av, subii(addis(p,1),a));
    1207             : }
    1208             : 
    1209             : GEN
    1210     2840215 : Fp_ellcard(GEN a4, GEN a6, GEN p)
    1211             : {
    1212     2840215 :   long lp = expi(p);
    1213     2840215 :   ulong pp = p[2];
    1214     2840215 :   if (lp < 11)
    1215      115794 :     return utoi(pp+1 - Fl_elltrace_naive(umodiu(a4,pp), umodiu(a6,pp), pp));
    1216     2724421 :   { GEN a = Fp_ellcard_CM(a4,a6,p); if (a) return a; }
    1217       71050 :   if (lp >= 56)
    1218         854 :     return Fp_ellcard_SEA(a4, a6, p, 0);
    1219       70196 :   if (lp <= BITS_IN_LONG-2)
    1220       70119 :     return utoi(Fl_ellcard_Shanks(umodiu(a4,pp), umodiu(a6,pp), pp));
    1221          77 :   return Fp_ellcard_Shanks(a4, a6, p);
    1222             : }
    1223             : 
    1224             : long
    1225      408850 : Fl_elltrace(ulong a4, ulong a6, ulong p)
    1226             : {
    1227             :   pari_sp av;
    1228             :   long lp;
    1229             :   GEN a;
    1230      408850 :   if (p < (1<<11)) return Fl_elltrace_naive(a4, a6, p);
    1231      339941 :   lp = expu(p);
    1232      339941 :   if (lp <= minss(56, BITS_IN_LONG-2)) return p+1-Fl_ellcard_Shanks(a4, a6, p);
    1233           0 :   av = avma; a = subui(p+1, Fp_ellcard(utoi(a4), utoi(a6), utoipos(p)));
    1234           0 :   avma = av; return itos(a);
    1235             : }
    1236             : long
    1237      438196 : Fl_elltrace_CM(long CM, ulong a4, ulong a6, ulong p)
    1238             : {
    1239             :   pari_sp av;
    1240             :   GEN a;
    1241      438196 :   if (!CM) return Fl_elltrace(a4,a6,p);
    1242       29388 :   if (p < (1<<11)) return Fl_elltrace_naive(a4, a6, p);
    1243        9674 :   av = avma; a = ec_ap_cm(CM, utoi(a4), utoi(a6), utoipos(p));
    1244        9674 :   avma = av; return itos(a);
    1245             : }
    1246             : 
    1247             : static GEN
    1248       10596 : _FpE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
    1249             : {
    1250       10596 :   struct _FpE *e = (struct _FpE *) E;
    1251       10596 :   return  Fp_order(FpE_weilpairing(P,Q,m,e->a4,e->p), F, e->p);
    1252             : }
    1253             : 
    1254             : GEN
    1255       21539 : Fp_ellgroup(GEN a4, GEN a6, GEN N, GEN p, GEN *pt_m)
    1256             : {
    1257             :   struct _FpE e;
    1258       21539 :   e.a4=a4; e.a6=a6; e.p=p;
    1259       21539 :   return gen_ellgroup(N, subis(p, 1), pt_m, (void*)&e, &FpE_group, _FpE_pairorder);
    1260             : }
    1261             : 
    1262             : GEN
    1263         574 : Fp_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN p)
    1264             : {
    1265             :   GEN P;
    1266         574 :   pari_sp av = avma;
    1267             :   struct _FpE e;
    1268         574 :   e.a4=a4; e.a6=a6; e.p=p;
    1269         574 :   switch(lg(D)-1)
    1270             :   {
    1271             :   case 1:
    1272         476 :     P = gen_gener(gel(D,1), (void*)&e, &FpE_group);
    1273         476 :     P = mkvec(FpE_changepoint(P, ch, p));
    1274         476 :     break;
    1275             :   default:
    1276          98 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FpE_group, _FpE_pairorder);
    1277          98 :     gel(P,1) = FpE_changepoint(gel(P,1), ch, p);
    1278          98 :     gel(P,2) = FpE_changepoint(gel(P,2), ch, p);
    1279          98 :     break;
    1280             :   }
    1281         574 :   return gerepilecopy(av, P);
    1282             : }
    1283             : 
    1284             : /* Not so fast arithmetic with points over elliptic curves over FpXQ */
    1285             : 
    1286             : /***********************************************************************/
    1287             : /**                                                                   **/
    1288             : /**                              FpXQE                                  **/
    1289             : /**                                                                   **/
    1290             : /***********************************************************************/
    1291             : 
    1292             : /* Theses functions deal with point over elliptic curves over FpXQ defined
    1293             :  * by an equation of the form y^2=x^3+a4*x+a6.
    1294             :  * Most of the time a6 is omitted since it can be recovered from any point
    1295             :  * on the curve.
    1296             :  */
    1297             : 
    1298             : GEN
    1299         896 : RgE_to_FpXQE(GEN x, GEN T, GEN p)
    1300             : {
    1301         896 :   if (ell_is_inf(x)) return x;
    1302         896 :   retmkvec2(Rg_to_FpXQ(gel(x,1),T,p),Rg_to_FpXQ(gel(x,2),T,p));
    1303             : }
    1304             : 
    1305             : GEN
    1306        1715 : FpXQE_changepoint(GEN x, GEN ch, GEN T, GEN p)
    1307             : {
    1308        1715 :   pari_sp av = avma;
    1309             :   GEN p1,z,u,r,s,t,v,v2,v3;
    1310        1715 :   if (ell_is_inf(x)) return x;
    1311         861 :   u = gel(ch,1); r = gel(ch,2);
    1312         861 :   s = gel(ch,3); t = gel(ch,4);
    1313         861 :   v = FpXQ_inv(u, T, p); v2 = FpXQ_sqr(v, T, p); v3 = FpXQ_mul(v,v2, T, p);
    1314         861 :   p1 = FpX_sub(gel(x,1),r, p);
    1315         861 :   z = cgetg(3,t_VEC);
    1316         861 :   gel(z,1) = FpXQ_mul(v2, p1, T, p);
    1317         861 :   gel(z,2) = FpXQ_mul(v3, FpX_sub(gel(x,2), FpX_add(FpXQ_mul(s,p1, T, p),t, p), p), T, p);
    1318         861 :   return gerepileupto(av, z);
    1319             : }
    1320             : 
    1321             : GEN
    1322         896 : FpXQE_changepointinv(GEN x, GEN ch, GEN T, GEN p)
    1323             : {
    1324             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
    1325         896 :   if (ell_is_inf(x)) return x;
    1326         896 :   X = gel(x,1); Y = gel(x,2);
    1327         896 :   u = gel(ch,1); r = gel(ch,2);
    1328         896 :   s = gel(ch,3); t = gel(ch,4);
    1329         896 :   u2 = FpXQ_sqr(u, T, p); u3 = FpXQ_mul(u,u2, T, p);
    1330         896 :   u2X = FpXQ_mul(u2,X, T, p);
    1331         896 :   z = cgetg(3, t_VEC);
    1332         896 :   gel(z,1) = FpX_add(u2X,r, p);
    1333         896 :   gel(z,2) = FpX_add(FpXQ_mul(u3,Y, T, p), FpX_add(FpXQ_mul(s,u2X, T, p), t, p), p);
    1334         896 :   return z;
    1335             : }
    1336             : 
    1337             : static GEN
    1338         840 : nonsquare_FpXQ(GEN T, GEN p)
    1339             : {
    1340         840 :   pari_sp av = avma;
    1341         840 :   long n = degpol(T), v = varn(T);
    1342             :   GEN a;
    1343         840 :   if (odd(n))
    1344             :   {
    1345         420 :     GEN z = cgetg(3, t_POL);
    1346         420 :     z[1] = evalsigne(1) | evalvarn(v);
    1347         420 :     gel(z,2) = nonsquare_Fp(p); return z;
    1348             :   }
    1349             :   do
    1350             :   {
    1351         833 :     avma = av;
    1352         833 :     a = random_FpX(n, v, p);
    1353         833 :   } while (FpXQ_issquare(a, T, p));
    1354         420 :   return a;
    1355             : }
    1356             : 
    1357             : void
    1358         840 : FpXQ_elltwist(GEN a4, GEN a6, GEN T, GEN p, GEN *pt_a4, GEN *pt_a6)
    1359             : {
    1360         840 :   GEN d = nonsquare_FpXQ(T, p);
    1361         840 :   GEN d2 = FpXQ_sqr(d, T, p), d3 = FpXQ_mul(d2, d, T, p);
    1362         840 :   *pt_a4 = FpXQ_mul(a4, d2, T, p);
    1363         840 :   *pt_a6 = FpXQ_mul(a6, d3, T, p);
    1364         840 : }
    1365             : 
    1366             : static GEN
    1367      184678 : FpXQE_dbl_slope(GEN P, GEN a4, GEN T, GEN p, GEN *slope)
    1368             : {
    1369             :   GEN x, y, Q;
    1370      184678 :   if (ell_is_inf(P) || !signe(gel(P,2))) return ellinf();
    1371      183425 :   x = gel(P,1); y = gel(P,2);
    1372      183425 :   *slope = FpXQ_div(FpX_add(FpX_mulu(FpXQ_sqr(x, T, p), 3, p), a4, p),
    1373             :                             FpX_mulu(y, 2, p), T, p);
    1374      183425 :   Q = cgetg(3,t_VEC);
    1375      183425 :   gel(Q, 1) = FpX_sub(FpXQ_sqr(*slope, T, p), FpX_mulu(x, 2, p), p);
    1376      183425 :   gel(Q, 2) = FpX_sub(FpXQ_mul(*slope, FpX_sub(x, gel(Q, 1), p), T, p), y, p);
    1377      183425 :   return Q;
    1378             : }
    1379             : 
    1380             : GEN
    1381      179722 : FpXQE_dbl(GEN P, GEN a4, GEN T, GEN p)
    1382             : {
    1383      179722 :   pari_sp av = avma;
    1384             :   GEN slope;
    1385      179722 :   return gerepileupto(av, FpXQE_dbl_slope(P,a4,T,p,&slope));
    1386             : }
    1387             : 
    1388             : static GEN
    1389       35144 : FpXQE_add_slope(GEN P, GEN Q, GEN a4, GEN T, GEN p, GEN *slope)
    1390             : {
    1391             :   GEN Px, Py, Qx, Qy, R;
    1392       35144 :   if (ell_is_inf(P)) return Q;
    1393       35144 :   if (ell_is_inf(Q)) return P;
    1394       35144 :   Px = gel(P,1); Py = gel(P,2);
    1395       35144 :   Qx = gel(Q,1); Qy = gel(Q,2);
    1396       35144 :   if (ZX_equal(Px, Qx))
    1397             :   {
    1398         707 :     if (ZX_equal(Py, Qy))
    1399           7 :       return FpXQE_dbl_slope(P, a4, T, p, slope);
    1400             :     else
    1401         700 :       return ellinf();
    1402             :   }
    1403       34437 :   *slope = FpXQ_div(FpX_sub(Py, Qy, p), FpX_sub(Px, Qx, p), T, p);
    1404       34437 :   R = cgetg(3,t_VEC);
    1405       34437 :   gel(R, 1) = FpX_sub(FpX_sub(FpXQ_sqr(*slope, T, p), Px, p), Qx, p);
    1406       34437 :   gel(R, 2) = FpX_sub(FpXQ_mul(*slope, FpX_sub(Px, gel(R, 1), p), T, p), Py, p);
    1407       34437 :   return R;
    1408             : }
    1409             : 
    1410             : GEN
    1411       34332 : FpXQE_add(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1412             : {
    1413       34332 :   pari_sp av = avma;
    1414             :   GEN slope;
    1415       34332 :   return gerepileupto(av, FpXQE_add_slope(P,Q,a4,T,p,&slope));
    1416             : }
    1417             : 
    1418             : static GEN
    1419           0 : FpXQE_neg_i(GEN P, GEN p)
    1420             : {
    1421           0 :   if (ell_is_inf(P)) return P;
    1422           0 :   return mkvec2(gel(P,1), FpX_neg(gel(P,2), p));
    1423             : }
    1424             : 
    1425             : GEN
    1426         749 : FpXQE_neg(GEN P, GEN T, GEN p)
    1427             : {
    1428             :   (void) T;
    1429         749 :   if (ell_is_inf(P)) return ellinf();
    1430         749 :   return mkvec2(gcopy(gel(P,1)), FpX_neg(gel(P,2), p));
    1431             : }
    1432             : 
    1433             : GEN
    1434           0 : FpXQE_sub(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1435             : {
    1436           0 :   pari_sp av = avma;
    1437             :   GEN slope;
    1438           0 :   return gerepileupto(av, FpXQE_add_slope(P, FpXQE_neg_i(Q, p), a4, T, p, &slope));
    1439             : }
    1440             : 
    1441             : struct _FpXQE
    1442             : {
    1443             :   GEN a4,a6;
    1444             :   GEN T,p;
    1445             : };
    1446             : 
    1447             : static GEN
    1448      179722 : _FpXQE_dbl(void *E, GEN P)
    1449             : {
    1450      179722 :   struct _FpXQE *ell = (struct _FpXQE *) E;
    1451      179722 :   return FpXQE_dbl(P, ell->a4, ell->T, ell->p);
    1452             : }
    1453             : 
    1454             : static GEN
    1455       34332 : _FpXQE_add(void *E, GEN P, GEN Q)
    1456             : {
    1457       34332 :   struct _FpXQE *ell=(struct _FpXQE *) E;
    1458       34332 :   return FpXQE_add(P, Q, ell->a4, ell->T, ell->p);
    1459             : }
    1460             : 
    1461             : static GEN
    1462        2809 : _FpXQE_mul(void *E, GEN P, GEN n)
    1463             : {
    1464        2809 :   pari_sp av = avma;
    1465        2809 :   struct _FpXQE *e=(struct _FpXQE *) E;
    1466        2809 :   long s = signe(n);
    1467        2809 :   if (!s || ell_is_inf(P)) return ellinf();
    1468        2809 :   if (s<0) P = FpXQE_neg(P, e->T, e->p);
    1469        2809 :   if (is_pm1(n)) return s>0? gcopy(P): P;
    1470        1955 :   return gerepileupto(av, gen_pow(P, n, e, &_FpXQE_dbl, &_FpXQE_add));
    1471             : }
    1472             : 
    1473             : GEN
    1474         854 : FpXQE_mul(GEN P, GEN n, GEN a4, GEN T, GEN p)
    1475             : {
    1476             :   struct _FpXQE E;
    1477         854 :   E.a4= a4; E.T = T; E.p = p;
    1478         854 :   return _FpXQE_mul(&E, P, n);
    1479             : }
    1480             : 
    1481             : /* Finds a random non-singular point on E */
    1482             : 
    1483             : GEN
    1484         974 : random_FpXQE(GEN a4, GEN a6, GEN T, GEN p)
    1485             : {
    1486         974 :   pari_sp ltop = avma;
    1487             :   GEN x, x2, y, rhs;
    1488         974 :   long v = get_FpX_var(T), d = get_FpX_degree(T);
    1489             :   do
    1490             :   {
    1491        2021 :     avma= ltop;
    1492        2021 :     x   = random_FpX(d,v,p); /*  x^3+a4*x+a6 = x*(x^2+a4)+a6  */
    1493        2021 :     x2  = FpXQ_sqr(x, T, p);
    1494        2021 :     rhs = FpX_add(FpXQ_mul(x, FpX_add(x2, a4, p), T, p), a6, p);
    1495        2021 :   } while ((!signe(rhs) && !signe(FpX_add(FpX_mulu(x2,3,p), a4, p)))
    1496        4042 :           || !FpXQ_issquare(rhs, T, p));
    1497         974 :   y = FpXQ_sqrt(rhs, T, p);
    1498         974 :   if (!y) pari_err_PRIME("random_FpE", p);
    1499         974 :   return gerepilecopy(ltop, mkvec2(x, y));
    1500             : }
    1501             : 
    1502             : static GEN
    1503         120 : _FpXQE_rand(void *E)
    1504             : {
    1505         120 :   struct _FpXQE *e=(struct _FpXQE *) E;
    1506         120 :   return random_FpXQE(e->a4, e->a6, e->T, e->p);
    1507             : }
    1508             : 
    1509             : static const struct bb_group FpXQE_group={_FpXQE_add,_FpXQE_mul,_FpXQE_rand,hash_GEN,ZXV_equal,ell_is_inf};
    1510             : 
    1511             : const struct bb_group *
    1512           8 : get_FpXQE_group(void ** pt_E, GEN a4, GEN a6, GEN T, GEN p)
    1513             : {
    1514           8 :   struct _FpXQE *e = (struct _FpXQE *) stack_malloc(sizeof(struct _FpXQE));
    1515           8 :   e->a4 = a4; e->a6 = a6; e->T = T; e->p = p;
    1516           8 :   *pt_E = (void *) e;
    1517           8 :   return &FpXQE_group;
    1518             : }
    1519             : 
    1520             : GEN
    1521          14 : FpXQE_order(GEN z, GEN o, GEN a4, GEN T, GEN p)
    1522             : {
    1523          14 :   pari_sp av = avma;
    1524             :   struct _FpXQE e;
    1525          14 :   e.a4=a4; e.T=T; e.p=p;
    1526          14 :   return gerepileuptoint(av, gen_order(z, o, (void*)&e, &FpXQE_group));
    1527             : }
    1528             : 
    1529             : GEN
    1530           0 : FpXQE_log(GEN a, GEN b, GEN o, GEN a4, GEN T, GEN p)
    1531             : {
    1532           0 :   pari_sp av = avma;
    1533             :   struct _FpXQE e;
    1534           0 :   e.a4=a4; e.T=T; e.p=p;
    1535           0 :   return gerepileuptoint(av, gen_PH_log(a, b, o, (void*)&e, &FpXQE_group));
    1536             : }
    1537             : 
    1538             : 
    1539             : /***********************************************************************/
    1540             : /**                                                                   **/
    1541             : /**                            Pairings                               **/
    1542             : /**                                                                   **/
    1543             : /***********************************************************************/
    1544             : 
    1545             : /* Derived from APIP from and by Jerome Milan, 2012 */
    1546             : 
    1547             : static GEN
    1548        5936 : FpXQE_vert(GEN P, GEN Q, GEN a4, GEN T, GEN p)
    1549             : {
    1550        5936 :   long vT = get_FpX_var(T);
    1551        5936 :   if (ell_is_inf(P))
    1552          98 :     return pol_1(get_FpX_var(T));
    1553        5838 :   if (!ZX_equal(gel(Q, 1), gel(P, 1)))
    1554        5838 :     return FpX_sub(gel(Q, 1), gel(P, 1), p);
    1555           0 :   if (signe(gel(P,2))!=0) return pol_1(vT);
    1556           0 :   return FpXQ_inv(FpX_add(FpX_mulu(FpXQ_sqr(gel(P,1), T, p), 3, p),
    1557             :                   a4, p), T, p);
    1558             : }
    1559             : 
    1560             : static GEN
    1561        5761 : FpXQE_Miller_line(GEN R, GEN Q, GEN slope, GEN a4, GEN T, GEN p)
    1562             : {
    1563        5761 :   long vT = get_FpX_var(T);
    1564        5761 :   GEN x = gel(Q, 1), y = gel(Q, 2);
    1565        5761 :   GEN tmp1  = FpX_sub(x, gel(R, 1), p);
    1566        5761 :   GEN tmp2  = FpX_add(FpXQ_mul(tmp1, slope, T, p), gel(R, 2), p);
    1567        5761 :   if (!ZX_equal(y, tmp2))
    1568        5761 :     return FpX_sub(y, tmp2, p);
    1569           0 :   if (signe(y) == 0)
    1570           0 :     return pol_1(vT);
    1571             :   else
    1572             :   {
    1573             :     GEN s1, s2;
    1574           0 :     GEN y2i = FpXQ_inv(FpX_mulu(y, 2, p), T, p);
    1575           0 :     s1 = FpXQ_mul(FpX_add(FpX_mulu(FpXQ_sqr(x, T, p), 3, p), a4, p), y2i, T, p);
    1576           0 :     if (!ZX_equal(s1, slope))
    1577           0 :       return FpX_sub(s1, slope, p);
    1578           0 :     s2 = FpXQ_mul(FpX_sub(FpX_mulu(x, 3, p), FpXQ_sqr(s1, T, p), p), y2i, T, p);
    1579           0 :     return signe(s2)!=0 ? s2: y2i;
    1580             :   }
    1581             : }
    1582             : 
    1583             : /* Computes the equation of the line tangent to R and returns its
    1584             :    evaluation at the point Q. Also doubles the point R.
    1585             :  */
    1586             : 
    1587             : static GEN
    1588        5026 : FpXQE_tangent_update(GEN R, GEN Q, GEN a4, GEN T, GEN p, GEN *pt_R)
    1589             : {
    1590        5026 :   if (ell_is_inf(R))
    1591             :   {
    1592          21 :     *pt_R = ellinf();
    1593          21 :     return pol_1(get_FpX_var(T));
    1594             :   }
    1595        5005 :   else if (!signe(gel(R,2)))
    1596             :   {
    1597          56 :     *pt_R = ellinf();
    1598          56 :     return FpXQE_vert(R, Q, a4, T, p);
    1599             :   } else {
    1600             :     GEN slope;
    1601        4949 :     *pt_R = FpXQE_dbl_slope(R, a4, T, p, &slope);
    1602        4949 :     return FpXQE_Miller_line(R, Q, slope, a4, T, p);
    1603             :   }
    1604             : }
    1605             : 
    1606             : /* Computes the equation of the line through R and P, and returns its
    1607             :    evaluation at the point Q. Also adds P to the point R.
    1608             :  */
    1609             : 
    1610             : static GEN
    1611         833 : FpXQE_chord_update(GEN R, GEN P, GEN Q, GEN a4, GEN T, GEN p, GEN *pt_R)
    1612             : {
    1613         833 :   if (ell_is_inf(R))
    1614             :   {
    1615           0 :     *pt_R = gcopy(P);
    1616           0 :     return FpXQE_vert(P, Q, a4, T, p);
    1617             :   }
    1618         833 :   else if (ell_is_inf(P))
    1619             :   {
    1620           0 :     *pt_R = gcopy(R);
    1621           0 :     return FpXQE_vert(R, Q, a4, T, p);
    1622             :   }
    1623         833 :   else if (ZX_equal(gel(P, 1), gel(R, 1)))
    1624             :   {
    1625          21 :     if (ZX_equal(gel(P, 2), gel(R, 2)))
    1626           0 :       return FpXQE_tangent_update(R, Q, a4, T, p, pt_R);
    1627             :     else
    1628             :     {
    1629          21 :       *pt_R = ellinf();
    1630          21 :       return FpXQE_vert(R, Q, a4, T, p);
    1631             :     }
    1632             :   } else {
    1633             :     GEN slope;
    1634         812 :     *pt_R = FpXQE_add_slope(P, R, a4, T, p, &slope);
    1635         812 :     return FpXQE_Miller_line(R, Q, slope, a4, T, p);
    1636             :   }
    1637             : }
    1638             : 
    1639             : /* Returns the Miller function f_{m, Q} evaluated at the point P using
    1640             :    the standard Miller algorithm.
    1641             :  */
    1642             : 
    1643             : struct _FpXQE_miller
    1644             : {
    1645             :   GEN p;
    1646             :   GEN T, a4, P;
    1647             : };
    1648             : 
    1649             : static GEN
    1650        5026 : FpXQE_Miller_dbl(void* E, GEN d)
    1651             : {
    1652        5026 :   struct _FpXQE_miller *m = (struct _FpXQE_miller *)E;
    1653        5026 :   GEN p  = m->p;
    1654        5026 :   GEN T = m->T, a4 = m->a4, P = m->P;
    1655             :   GEN v, line;
    1656        5026 :   GEN num = FpXQ_sqr(gel(d,1), T, p);
    1657        5026 :   GEN denom = FpXQ_sqr(gel(d,2), T, p);
    1658        5026 :   GEN point = gel(d,3);
    1659        5026 :   line = FpXQE_tangent_update(point, P, a4, T, p, &point);
    1660        5026 :   num  = FpXQ_mul(num, line, T, p);
    1661        5026 :   v = FpXQE_vert(point, P, a4, T, p);
    1662        5026 :   denom = FpXQ_mul(denom, v, T, p);
    1663        5026 :   return mkvec3(num, denom, point);
    1664             : }
    1665             : 
    1666             : static GEN
    1667         833 : FpXQE_Miller_add(void* E, GEN va, GEN vb)
    1668             : {
    1669         833 :   struct _FpXQE_miller *m = (struct _FpXQE_miller *)E;
    1670         833 :   GEN p = m->p;
    1671         833 :   GEN T = m->T, a4 = m->a4, P = m->P;
    1672             :   GEN v, line, point;
    1673         833 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
    1674         833 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
    1675         833 :   GEN num   = FpXQ_mul(na, nb, T, p);
    1676         833 :   GEN denom = FpXQ_mul(da, db, T, p);
    1677         833 :   line = FpXQE_chord_update(pa, pb, P, a4, T, p, &point);
    1678         833 :   num  = FpXQ_mul(num, line, T, p);
    1679         833 :   v = FpXQE_vert(point, P, a4, T, p);
    1680         833 :   denom = FpXQ_mul(denom, v, T, p);
    1681         833 :   return mkvec3(num, denom, point);
    1682             : }
    1683             : 
    1684             : static GEN
    1685          77 : FpXQE_Miller(GEN Q, GEN P, GEN m, GEN a4, GEN T, GEN p)
    1686             : {
    1687          77 :   pari_sp ltop = avma;
    1688             :   struct _FpXQE_miller d;
    1689             :   GEN v, num, denom, g1;
    1690             : 
    1691          77 :   d.a4 = a4; d.T = T; d.p = p; d.P = P;
    1692          77 :   g1 = pol_1(get_FpX_var(T));
    1693          77 :   v = gen_pow(mkvec3(g1,g1,Q), m, (void*)&d, FpXQE_Miller_dbl, FpXQE_Miller_add);
    1694          77 :   num = gel(v,1); denom = gel(v,2);
    1695          77 :   return gerepileupto(ltop, FpXQ_div(num, denom, T, p));
    1696             : }
    1697             : 
    1698             : GEN
    1699          35 : FpXQE_weilpairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, GEN p)
    1700             : {
    1701          35 :   pari_sp ltop = avma;
    1702             :   GEN num, denom, result;
    1703          35 :   if (ell_is_inf(P) || ell_is_inf(Q) || ZXV_equal(P,Q))
    1704           0 :     return pol_1(get_FpX_var(T));
    1705          35 :   num    = FpXQE_Miller(P, Q, m, a4, T, p);
    1706          35 :   denom  = FpXQE_Miller(Q, P, m, a4, T, p);
    1707          35 :   result = FpXQ_div(num, denom, T, p);
    1708          35 :   if (mpodd(m))
    1709           0 :     result  = FpX_neg(result, p);
    1710          35 :   return gerepileupto(ltop, result);
    1711             : }
    1712             : 
    1713             : GEN
    1714           7 : FpXQE_tatepairing(GEN P, GEN Q, GEN m, GEN a4, GEN T, GEN p)
    1715             : {
    1716           7 :   if (ell_is_inf(P) || ell_is_inf(Q))
    1717           0 :     return pol_1(get_FpX_var(T));
    1718           7 :   return FpXQE_Miller(P, Q, m, a4, T, p);
    1719             : }
    1720             : 
    1721             : /***********************************************************************/
    1722             : /**                                                                   **/
    1723             : /**                           issupersingular                         **/
    1724             : /**                                                                   **/
    1725             : /***********************************************************************/
    1726             : 
    1727             : GEN
    1728        1695 : FpXQ_ellj(GEN a4, GEN a6, GEN T, GEN p)
    1729             : {
    1730        1695 :   if (equaliu(p,3)) return pol_0(get_FpX_var(T));
    1731             :   else
    1732             :   {
    1733        1695 :     pari_sp av=avma;
    1734        1695 :     GEN a43 = FpXQ_mul(a4,FpXQ_sqr(a4,T,p),T,p);
    1735        1695 :     GEN a62 = FpXQ_sqr(a6,T,p);
    1736        1695 :     GEN num = FpX_mulu(a43,6912,p);
    1737        1695 :     GEN den = FpX_add(FpX_mulu(a43,4,p),FpX_mulu(a62,27,p),p);
    1738        1695 :     return gerepileuptoleaf(av, FpXQ_div(num, den, T, p));
    1739             :   }
    1740             : }
    1741             : 
    1742             : int
    1743      164199 : FpXQ_elljissupersingular(GEN j, GEN T, GEN p)
    1744             : {
    1745      164199 :   pari_sp ltop = avma;
    1746             : 
    1747             :   /* All supersingular j-invariants are in FF_{p^2}, so we first check
    1748             :    * whether j is in FF_{p^2}.  If d is odd, then FF_{p^2} is not a
    1749             :    * subfield of FF_{p^d} so the j-invariants are all in FF_p.  Hence
    1750             :    * the j-invariants are in FF_{p^{2 - e}}. */
    1751      164199 :   ulong d = get_FpX_degree(T);
    1752             :   GEN S;
    1753             :   int res;
    1754             : 
    1755      164199 :   if (degpol(j) <= 0) return Fp_elljissupersingular(constant_coeff(j), p);
    1756      163758 :   if (cmpiu(p, 5) <= 0) return 0; /* j != 0*/
    1757             : 
    1758             :   /* Set S so that FF_p[T]/(S) is isomorphic to FF_{p^2}: */
    1759      163758 :   if (d == 2)
    1760       12642 :     S = T;
    1761             :   else { /* d > 2 */
    1762             :     /* We construct FF_{p^2} = FF_p[t]/((T - j)(T - j^p)) which
    1763             :      * injects into FF_{p^d} via the map T |--> j. */
    1764      151116 :     GEN j_pow_p = FpXQ_pow(j, p, T, p);
    1765      151116 :     GEN j_sum = FpX_add(j, j_pow_p, p), j_prod;
    1766      151116 :     long var = varn(T);
    1767      151116 :     if (degpol(j_sum) > 0) { avma = ltop; return 0; /* j not in Fp^2 */ }
    1768         588 :     j_prod = FpXQ_mul(j, j_pow_p, T, p);
    1769         588 :     if (degpol(j_prod) > 0 ) { avma = ltop; return 0; /* j not in Fp^2 */ }
    1770         588 :     j_sum = constant_coeff(j_sum); j_prod = constant_coeff(j_prod);
    1771         588 :     S = mkpoln(3, gen_1, Fp_neg(j_sum, p), j_prod);
    1772         588 :     setvarn(S, var);
    1773         588 :     j = pol_x(var);
    1774             :   }
    1775       13230 :   res = jissupersingular(j, S, p);
    1776       13230 :   avma = ltop;
    1777       13230 :   return res;
    1778             : }
    1779             : 
    1780             : /***********************************************************************/
    1781             : /**                                                                   **/
    1782             : /**                           Point counting                          **/
    1783             : /**                                                                   **/
    1784             : /***********************************************************************/
    1785             : 
    1786             : GEN
    1787       13594 : elltrace_extension(GEN t, long n, GEN q)
    1788             : {
    1789       13594 :   pari_sp av = avma;
    1790       13594 :   GEN v = RgX_to_RgC(RgXQ_powu(pol_x(0), n, mkpoln(3,gen_1,negi(t),q)),2);
    1791       13594 :   GEN te = addii(shifti(gel(v,1),1), mulii(t,gel(v,2)));
    1792       13594 :   return gerepileuptoint(av, te);
    1793             : }
    1794             : 
    1795             : GEN
    1796       12999 : Fp_ffellcard(GEN a4, GEN a6, GEN q, long n, GEN p)
    1797             : {
    1798       12999 :   pari_sp av = avma;
    1799       12999 :   GEN ap = subii(addis(p, 1), Fp_ellcard(a4, a6, p));
    1800       12999 :   GEN te = elltrace_extension(ap, n, p);
    1801       12999 :   return gerepileuptoint(av, subii(addis(q, 1), te));
    1802             : }
    1803             : 
    1804             : static GEN
    1805        1687 : FpXQ_ellcardj(GEN a4, GEN a6, GEN j, GEN T, GEN q, GEN p, long n)
    1806             : {
    1807        1687 :   GEN q1 = addis(q,1);
    1808        1687 :   if (signe(j)==0)
    1809             :   {
    1810             :     GEN W, w, t, N;
    1811         560 :     if (umodiu(q,6)!=1) return q1;
    1812         420 :     N = Fp_ffellcard(gen_0,gen_1,q,n,p);
    1813         420 :     t = subii(q1, N);
    1814         420 :     W = FpXQ_pow(a6,diviuexact(shifti(q,-1), 3),T,p);
    1815         420 :     if (degpol(W)>0) /*p=5 mod 6*/
    1816         105 :       return ZX_equal1(FpXQ_powu(W,3,T,p)) ? addii(q1,shifti(t,-1)):
    1817          35 :                                              subii(q1,shifti(t,-1));
    1818         350 :     w = modii(gel(W,2),p);
    1819         350 :     if (equali1(w))  return N;
    1820         287 :     if (equalii(w,subiu(p,1))) return addii(q1,t);
    1821             :     else /*p=1 mod 6*/
    1822             :     {
    1823         224 :       GEN u = shifti(t,-1), v = sqrtint(diviuexact(subii(q,sqri(u)),3));
    1824         224 :       GEN a = addii(u,v), b = shifti(v,1);
    1825         224 :       if (equali1(Fp_powu(w,3,p)))
    1826             :       {
    1827         112 :         if (signe(Fp_add(modii(a,p),Fp_mul(w,modii(b,p),p),p))==0)
    1828          56 :           return subii(q1,subii(shifti(b,1),a));
    1829             :         else
    1830          56 :           return addii(q1,addii(a,b));
    1831             :       }
    1832             :       else
    1833             :       {
    1834         112 :         if (signe(Fp_sub(modii(a,p),Fp_mul(w,modii(b,p),p),p))==0)
    1835          56 :           return subii(q1,subii(a,shifti(b,1)));
    1836             :         else
    1837          56 :           return subii(q1,addii(a,b));
    1838             :       }
    1839             :     }
    1840        1127 :   } else if (equalii(j,modsi(1728,p)))
    1841             :   {
    1842             :     GEN w, W, N, t;
    1843         567 :     if (mod4(q)==3) return q1;
    1844         427 :     W = FpXQ_pow(a4,shifti(q,-2),T,p);
    1845         427 :     if (degpol(W)>0) return q1; /*p=3 mod 4*/
    1846         385 :     w = modii(gel(W,2),p);
    1847         385 :     N = Fp_ffellcard(gen_1,gen_0,q,n,p);
    1848         385 :     if (equali1(w)) return N;
    1849         266 :     t = subii(q1, N);
    1850         266 :     if (equalii(w,subiu(p,1))) return addii(q1,t);
    1851             :     else /*p=1 mod 4*/
    1852             :     {
    1853         140 :       GEN u = shifti(t,-1), v = sqrtint(subii(q,sqri(u)));
    1854         140 :       if (signe(Fp_add(modii(u,p),Fp_mul(w,modii(v,p),p),p))==0)
    1855          70 :         return subii(q1,shifti(v,1));
    1856             :       else
    1857          70 :         return addii(q1,shifti(v,1));
    1858             :     }
    1859             :   } else
    1860             :   {
    1861         560 :     GEN g = Fp_div(j, Fp_sub(utoi(1728), j, p), p);
    1862         560 :     GEN l = FpXQ_div(FpX_mulu(a6,3,p),FpX_mulu(a4,2,p),T,p);
    1863         560 :     GEN N = Fp_ffellcard(Fp_mulu(g,3,p),Fp_mulu(g,2,p),q,n,p);
    1864         560 :     if (FpXQ_issquare(l,T,p)) return N;
    1865         280 :     return subii(shifti(q1,1),N);
    1866             :   }
    1867             : }
    1868             : 
    1869             : GEN
    1870        3662 : FpXQ_ellcard(GEN a4, GEN a6, GEN T, GEN p)
    1871             : {
    1872        3662 :   pari_sp av = avma;
    1873        3662 :   long n = get_FpX_degree(T);
    1874        3662 :   GEN q = powiu(p, n), r, J;
    1875        3662 :   if (degpol(a4)<=0 && degpol(a6)<=0)
    1876         133 :     r = Fp_ffellcard(constant_coeff(a4),constant_coeff(a6),q,n,p);
    1877        3529 :   else if (lgefint(p)==3)
    1878             :   {
    1879        1834 :     ulong pp = p[2];
    1880        1834 :     r =  Flxq_ellcard(ZX_to_Flx(a4,pp),ZX_to_Flx(a6,pp),ZX_to_Flx(T,pp),pp);
    1881             :   }
    1882        1695 :   else if (degpol(J=FpXQ_ellj(a4,a6,T,p))<=0)
    1883        1687 :     r = FpXQ_ellcardj(a4,a6,constant_coeff(J),T,q,p,n);
    1884             :   else
    1885           8 :     r = Fq_ellcard_SEA(a4, a6, q, T, p, 0);
    1886        3662 :   return gerepileuptoint(av, r);
    1887             : }
    1888             : 
    1889             : static GEN
    1890          28 : _FpXQE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
    1891             : {
    1892          28 :   struct _FpXQE *e = (struct _FpXQE *) E;
    1893          28 :   return  FpXQ_order(FpXQE_weilpairing(P,Q,m,e->a4,e->T,e->p), F, e->T, e->p);
    1894             : }
    1895             : 
    1896             : GEN
    1897          14 : FpXQ_ellgroup(GEN a4, GEN a6, GEN N, GEN T, GEN p, GEN *pt_m)
    1898             : {
    1899             :   struct _FpXQE e;
    1900          14 :   GEN q = powiu(p, get_FpX_degree(T));
    1901          14 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
    1902          14 :   return gen_ellgroup(N, subis(q,1), pt_m, (void*)&e, &FpXQE_group, _FpXQE_pairorder);
    1903             : }
    1904             : 
    1905             : GEN
    1906           7 : FpXQ_ellgens(GEN a4, GEN a6, GEN ch, GEN D, GEN m, GEN T, GEN p)
    1907             : {
    1908             :   GEN P;
    1909           7 :   pari_sp av = avma;
    1910             :   struct _FpXQE e;
    1911           7 :   e.a4=a4; e.a6=a6; e.T=T; e.p=p;
    1912           7 :   switch(lg(D)-1)
    1913             :   {
    1914             :   case 1:
    1915           7 :     P = gen_gener(gel(D,1), (void*)&e, &FpXQE_group);
    1916           7 :     P = mkvec(FpXQE_changepoint(P, ch, T, p));
    1917           7 :     break;
    1918             :   default:
    1919           0 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &FpXQE_group, _FpXQE_pairorder);
    1920           0 :     gel(P,1) = FpXQE_changepoint(gel(P,1), ch, T, p);
    1921           0 :     gel(P,2) = FpXQE_changepoint(gel(P,2), ch, T, p);
    1922           0 :     break;
    1923             :   }
    1924           7 :   return gerepilecopy(av, P);
    1925             : }
    1926             : 
    1927             : 

Generated by: LCOV version 1.11