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

Generated by: LCOV version 1.13