Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11