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

Generated by: LCOV version 1.13