Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - F2xqE.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19350-bd5f220) Lines: 401 427 93.9 %
Date: 2016-08-24 06:11:24 Functions: 51 54 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2012  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 F_2^n */
      18             : 
      19             : /***********************************************************************/
      20             : /**                                                                   **/
      21             : /**                              F2xqE                                **/
      22             : /**                                                                   **/
      23             : /***********************************************************************/
      24             : 
      25             : /* Theses functions deal with point over elliptic curves over F_2^n defined
      26             :  * by an equation of the form:
      27             :  ** y^2+x*y=x^3+a_2*x^2+a_6 if the curve is ordinary.
      28             :  ** y^2+a_3*y=x^3+a_4*x+a_6 if the curve is supersingular.
      29             :  * Most of the time a6 is omitted since it can be recovered from any point
      30             :  * on the curve.
      31             :  * For supersingular curves, the parameter a2 is replaced by [a3,a4,a3^-1].
      32             :  */
      33             : 
      34             : GEN
      35       14658 : RgE_to_F2xqE(GEN x, GEN T)
      36             : {
      37       14658 :   if (ell_is_inf(x)) return x;
      38       14651 :   retmkvec2(Rg_to_F2xq(gel(x,1),T),Rg_to_F2xq(gel(x,2),T));
      39             : }
      40             : 
      41             : GEN
      42       35539 : F2xqE_changepoint(GEN x, GEN ch, GEN T)
      43             : {
      44       35539 :   pari_sp av = avma;
      45             :   GEN p1,z,u,r,s,t,v,v2,v3;
      46       35539 :   if (ell_is_inf(x)) return x;
      47       21364 :   u = gel(ch,1); r = gel(ch,2);
      48       21364 :   s = gel(ch,3); t = gel(ch,4);
      49       21364 :   v = F2xq_inv(u, T); v2 = F2xq_sqr(v, T); v3 = F2xq_mul(v,v2, T);
      50       21364 :   p1 = F2x_add(gel(x,1),r);
      51       21364 :   z = cgetg(3,t_VEC);
      52       21364 :   gel(z,1) = F2xq_mul(v2, p1, T);
      53       21364 :   gel(z,2) = F2xq_mul(v3, F2x_add(gel(x,2), F2x_add(F2xq_mul(s, p1, T),t)), T);
      54       21364 :   return gerepileupto(av, z);
      55             : }
      56             : 
      57             : GEN
      58       14658 : F2xqE_changepointinv(GEN x, GEN ch, GEN T)
      59             : {
      60             :   GEN u, r, s, t, X, Y, u2, u3, u2X, z;
      61       14658 :   if (ell_is_inf(x)) return x;
      62       14651 :   X = gel(x,1); Y = gel(x,2);
      63       14651 :   u = gel(ch,1); r = gel(ch,2);
      64       14651 :   s = gel(ch,3); t = gel(ch,4);
      65       14651 :   u2 = F2xq_sqr(u, T); u3 = F2xq_mul(u,u2, T);
      66       14651 :   u2X = F2xq_mul(u2,X, T);
      67       14651 :   z = cgetg(3, t_VEC);
      68       14651 :   gel(z,1) = F2x_add(u2X,r);
      69       14651 :   gel(z,2) = F2x_add(F2xq_mul(u3,Y, T), F2x_add(F2xq_mul(s,u2X, T), t));
      70       14651 :   return z;
      71             : }
      72             : 
      73             : static GEN
      74        3514 : nonzerotrace_F2xq(GEN T)
      75             : {
      76        3514 :   pari_sp av = avma;
      77        3514 :   long n = F2x_degree(T), vs = T[1];
      78             :   GEN a;
      79        3514 :   if (odd(n))
      80        1162 :     return pol1_F2x(vs);
      81             :   do
      82             :   {
      83        4515 :     avma = av;
      84        4515 :     a = random_F2x(n, vs);
      85        4515 :   } while (F2xq_trace(a, T)==0);
      86        2352 :   return a;
      87             : }
      88             : 
      89             : void
      90        3514 : F2xq_elltwist(GEN a, GEN a6, GEN T, GEN *pt_a, GEN *pt_a6)
      91             : {
      92        3514 :   pari_sp av = avma;
      93        3514 :   GEN n = nonzerotrace_F2xq(T);
      94        3514 :   if (typ(a)==t_VECSMALL)
      95             :   {
      96        3514 :     *pt_a = gerepileuptoleaf(av, F2x_add(n, a));
      97        3514 :     *pt_a6 = vecsmall_copy(a6);
      98             :   } else
      99             :   {
     100           0 :     GEN a6t = F2x_add(a6, F2xq_mul(n, F2xq_sqr(gel(a,1), T), T));
     101           0 :     *pt_a6 = gerepileuptoleaf(av, a6t);
     102           0 :     *pt_a = vecsmall_copy(a);
     103             :   }
     104        3514 : }
     105             : 
     106             : static GEN
     107      229397 : F2xqE_dbl_slope(GEN P, GEN a, GEN T, GEN *slope)
     108             : {
     109             :   GEN x, y, Q;
     110      229397 :   if (ell_is_inf(P)) return ellinf();
     111      216237 :   x = gel(P,1); y = gel(P,2);
     112      216237 :   if (typ(a)==t_VECSMALL)
     113             :   {
     114      207655 :     GEN a2 = a;
     115      207655 :     if (!lgpol(gel(P,1))) return ellinf();
     116      191261 :     *slope = F2x_add(x, F2xq_div(y, x, T));
     117      191261 :     Q = cgetg(3,t_VEC);
     118      191261 :     gel(Q, 1) = F2x_add(F2xq_sqr(*slope, T), F2x_add(*slope, a2));
     119      191261 :     gel(Q, 2) = F2x_add(F2xq_mul(*slope, F2x_add(x, gel(Q, 1)), T), F2x_add(y, gel(Q, 1)));
     120             :   }
     121             :   else
     122             :   {
     123        8582 :     GEN a3 = gel(a,1), a4 = gel(a,2), a3i = gel(a,3);
     124        8582 :     *slope = F2xq_mul(F2x_add(a4, F2xq_sqr(x, T)), a3i, T);
     125        8582 :     Q = cgetg(3,t_VEC);
     126        8582 :     gel(Q, 1) = F2xq_sqr(*slope, T);
     127        8582 :     gel(Q, 2) = F2x_add(F2xq_mul(*slope, F2x_add(x, gel(Q, 1)), T), F2x_add(y, a3));
     128             :   }
     129      199843 :   return Q;
     130             : }
     131             : 
     132             : GEN
     133      228627 : F2xqE_dbl(GEN P, GEN a, GEN T)
     134             : {
     135      228627 :   pari_sp av = avma;
     136             :   GEN slope;
     137      228627 :   return gerepileupto(av, F2xqE_dbl_slope(P, a, T,&slope));
     138             : }
     139             : 
     140             : static GEN
     141       74599 : F2xqE_add_slope(GEN P, GEN Q, GEN a, GEN T, GEN *slope)
     142             : {
     143             :   GEN Px, Py, Qx, Qy, R;
     144       74599 :   if (ell_is_inf(P)) return Q;
     145       73353 :   if (ell_is_inf(Q)) return P;
     146       73346 :   Px = gel(P,1); Py = gel(P,2);
     147       73346 :   Qx = gel(Q,1); Qy = gel(Q,2);
     148       73346 :   if (F2x_equal(Px, Qx))
     149             :   {
     150        4543 :     if (F2x_equal(Py, Qy))
     151         189 :       return F2xqE_dbl_slope(P, a, T, slope);
     152             :     else
     153        4354 :       return ellinf();
     154             :   }
     155       68803 :   *slope = F2xq_div(F2x_add(Py, Qy), F2x_add(Px, Qx), T);
     156       68803 :   R = cgetg(3,t_VEC);
     157       68803 :   if (typ(a)==t_VECSMALL)
     158             :   {
     159       66437 :     GEN a2 = a;
     160       66437 :     gel(R, 1) = F2x_add(F2x_add(F2x_add(F2x_add(F2xq_sqr(*slope, T), *slope), Px), Qx), a2);
     161       66437 :     gel(R, 2) = F2x_add(F2xq_mul(*slope, F2x_add(Px, gel(R, 1)), T), F2x_add(Py, gel(R, 1)));
     162             :   }
     163             :   else
     164             :   {
     165        2366 :     GEN a3 = gel(a,1);
     166        2366 :     gel(R, 1) = F2x_add(F2x_add(F2xq_sqr(*slope, T), Px), Qx);
     167        2366 :     gel(R, 2) = F2x_add(F2xq_mul(*slope, F2x_add(Px, gel(R, 1)), T), F2x_add(Py, a3));
     168             :   }
     169       68803 :   return R;
     170             : }
     171             : 
     172             : GEN
     173       74564 : F2xqE_add(GEN P, GEN Q, GEN a, GEN T)
     174             : {
     175       74564 :   pari_sp av = avma;
     176             :   GEN slope;
     177       74564 :   return gerepileupto(av, F2xqE_add_slope(P, Q, a, T, &slope));
     178             : }
     179             : 
     180             : static GEN
     181           0 : F2xqE_neg_i(GEN P, GEN a)
     182             : {
     183             :   GEN LHS;
     184           0 :   if (ell_is_inf(P)) return P;
     185           0 :   LHS = typ(a)==t_VECSMALL ? gel(P,1): gel(a,1);
     186           0 :   return mkvec2(gel(P,1), F2x_add(LHS, gel(P,2)));
     187             : }
     188             : 
     189             : GEN
     190          84 : F2xqE_neg(GEN P, GEN a, GEN T)
     191             : {
     192             :   GEN LHS;
     193             :   (void) T;
     194          84 :   if (ell_is_inf(P)) return ellinf();
     195          84 :   LHS = typ(a)==t_VECSMALL ? gel(P,1): gel(a,1);
     196          84 :   return mkvec2(gcopy(gel(P,1)), F2x_add(LHS, gel(P,2)));
     197             : }
     198             : 
     199             : GEN
     200           0 : F2xqE_sub(GEN P, GEN Q, GEN a2, GEN T)
     201             : {
     202           0 :   pari_sp av = avma;
     203             :   GEN slope;
     204           0 :   return gerepileupto(av, F2xqE_add_slope(P, F2xqE_neg_i(Q, a2), a2, T, &slope));
     205             : }
     206             : 
     207             : struct _F2xqE
     208             : {
     209             :   GEN a2, a6;
     210             :   GEN T;
     211             : };
     212             : 
     213             : static GEN
     214      228627 : _F2xqE_dbl(void *E, GEN P)
     215             : {
     216      228627 :   struct _F2xqE *ell = (struct _F2xqE *) E;
     217      228627 :   return F2xqE_dbl(P, ell->a2, ell->T);
     218             : }
     219             : 
     220             : static GEN
     221       74564 : _F2xqE_add(void *E, GEN P, GEN Q)
     222             : {
     223       74564 :   struct _F2xqE *ell=(struct _F2xqE *) E;
     224       74564 :   return F2xqE_add(P, Q, ell->a2, ell->T);
     225             : }
     226             : 
     227             : static GEN
     228       42805 : _F2xqE_mul(void *E, GEN P, GEN n)
     229             : {
     230       42805 :   pari_sp av = avma;
     231       42805 :   struct _F2xqE *e=(struct _F2xqE *) E;
     232       42805 :   long s = signe(n);
     233       42805 :   if (!s || ell_is_inf(P)) return ellinf();
     234       42784 :   if (s<0) P = F2xqE_neg(P, e->a2, e->T);
     235       42784 :   if (is_pm1(n)) return s>0? gcopy(P): P;
     236       41706 :   return gerepileupto(av, gen_pow(P, n, e, &_F2xqE_dbl, &_F2xqE_add));
     237             : }
     238             : 
     239             : GEN
     240       14224 : F2xqE_mul(GEN P, GEN n, GEN a2, GEN T)
     241             : {
     242             :   struct _F2xqE E;
     243       14224 :   E.a2 = a2; E.T = T;
     244       14224 :   return _F2xqE_mul(&E, P, n);
     245             : }
     246             : 
     247             : /* Finds a random non-singular point on E */
     248             : GEN
     249       31283 : random_F2xqE(GEN a, GEN a6, GEN T)
     250             : {
     251       31283 :   pari_sp ltop = avma;
     252             :   GEN x, y, rhs, u;
     253             :   do
     254             :   {
     255       63329 :     avma= ltop;
     256       63329 :     x   = random_F2x(F2x_degree(T),T[1]);
     257       63329 :     if (typ(a) == t_VECSMALL)
     258             :     {
     259       62909 :       GEN a2 = a, x2;
     260       62909 :       if (!lgpol(x))
     261         672 :         { avma=ltop; retmkvec2(pol0_Flx(T[1]), F2xq_sqrt(a6,T)); }
     262       62237 :       u = x; x2  = F2xq_sqr(x, T);
     263       62237 :       rhs = F2x_add(F2xq_mul(x2,F2x_add(x,a2),T),a6);
     264       62237 :       rhs = F2xq_div(rhs,x2,T);
     265             :     }
     266             :     else
     267             :     {
     268         420 :       GEN a3 = gel(a,1), a4 = gel(a,2), a3i = gel(a,3), u2i;
     269         420 :       u = a3; u2i = F2xq_sqr(a3i,T);
     270         420 :       rhs = F2x_add(F2xq_mul(x,F2x_add(F2xq_sqr(x,T),a4),T),a6);
     271         420 :       rhs = F2xq_mul(rhs,u2i,T);
     272             :     }
     273       62657 :   } while (F2xq_trace(rhs,T));
     274       30611 :   y = F2xq_mul(F2xq_Artin_Schreier(rhs, T), u, T);
     275       30611 :   return gerepilecopy(ltop, mkvec2(x, y));
     276             : }
     277             : 
     278             : static GEN
     279       13643 : _F2xqE_rand(void *E)
     280             : {
     281       13643 :   struct _F2xqE *ell=(struct _F2xqE *) E;
     282       13643 :   return random_F2xqE(ell->a2, ell->a6, ell->T);
     283             : }
     284             : 
     285             : static const struct bb_group F2xqE_group={_F2xqE_add,_F2xqE_mul,_F2xqE_rand,hash_GEN,zvV_equal,ell_is_inf, NULL};
     286             : 
     287             : const struct bb_group *
     288           0 : get_F2xqE_group(void ** pt_E, GEN a2, GEN a6, GEN T)
     289             : {
     290           0 :   struct _F2xqE *e = (struct _F2xqE *) stack_malloc(sizeof(struct _F2xqE));
     291           0 :   e->a2 = a2; e->a6 = a6; e->T = T;
     292           0 :   *pt_E = (void *) e;
     293           0 :   return &F2xqE_group;
     294             : }
     295             : 
     296             : GEN
     297         280 : F2xqE_order(GEN z, GEN o, GEN a2, GEN T)
     298             : {
     299         280 :   pari_sp av = avma;
     300             :   struct _F2xqE e;
     301         280 :   e.a2=a2; e.T=T;
     302         280 :   return gerepileuptoint(av, gen_order(z, o, (void*)&e, &F2xqE_group));
     303             : }
     304             : 
     305             : GEN
     306          42 : F2xqE_log(GEN a, GEN b, GEN o, GEN a2, GEN T)
     307             : {
     308          42 :   pari_sp av = avma;
     309             :   struct _F2xqE e;
     310          42 :   e.a2=a2; e.T=T;
     311          42 :   return gerepileuptoint(av, gen_PH_log(a, b, o, (void*)&e, &F2xqE_group));
     312             : }
     313             : 
     314             : /***********************************************************************/
     315             : /**                                                                   **/
     316             : /**                            Pairings                               **/
     317             : /**                                                                   **/
     318             : /***********************************************************************/
     319             : 
     320             : /* Derived from APIP from and by Jerome Milan, 2012 */
     321             : 
     322             : static GEN
     323        1694 : F2xqE_vert(GEN P, GEN Q, GEN T)
     324             : {
     325        1694 :   long vT = T[1];
     326        1694 :   if (ell_is_inf(P))
     327         539 :     return pol1_F2x(T[1]);
     328        1155 :   if (!F2x_equal(gel(Q, 1), gel(P, 1)))
     329         861 :     return F2x_add(gel(Q, 1), gel(P, 1));
     330         294 :   if (lgpol(gel(Q, 1))) return pol1_F2x(vT);
     331           0 :   return F2xq_inv(gel(Q,2), T);
     332             : }
     333             : 
     334             : static GEN
     335         616 : F2xqE_Miller_line(GEN R, GEN Q, GEN slope, GEN a, GEN T)
     336             : {
     337         616 :   long vT = T[1];
     338         616 :   GEN x = gel(Q, 1), y = gel(Q, 2);
     339         616 :   GEN tmp1 = F2x_add(x, gel(R, 1));
     340         616 :   GEN tmp2 = F2x_add(F2xq_mul(tmp1, slope, T), gel(R, 2));
     341             :   GEN s1, s2, ix;
     342         616 :   if (!F2x_equal(y, tmp2))
     343         462 :     return F2x_add(y, tmp2);
     344         154 :   if (lgpol(x) == 0) return pol1_F2x(vT);
     345         154 :   if (typ(a)==t_VEC)
     346             :   {
     347          42 :     GEN a4 = gel(a,2), a3i = gel(a,3);
     348          42 :     s1 = F2xq_mul(F2x_add(a4, F2xq_sqr(x, T)), a3i, T);
     349          42 :     if (!F2x_equal(s1, slope))
     350          14 :       return F2x_add(s1, slope);
     351          28 :     s2 = F2xq_mul(F2x_add(x, F2xq_sqr(s1, T)), a3i, T);
     352          28 :     if (lgpol(s2)) return s2;
     353          14 :     return zv_copy(a3i);
     354             :   } else
     355             :   {
     356         112 :     GEN a2 = a ;
     357         112 :     ix = F2xq_inv(x, T);
     358         112 :     s1 = F2x_add(x, F2xq_mul(y, ix, T));
     359         112 :     if (!F2x_equal(s1, slope))
     360           7 :       return F2x_add(s1, slope);
     361         105 :     s2 =F2x_add(a2, F2x_add(F2xq_sqr(s1,T), s1));
     362         105 :     if (!F2x_equal(s2, x))
     363           7 :       return  F2x_add(pol1_F2x(vT), F2xq_mul(s2, ix, T));
     364          98 :     return ix;
     365             :   }
     366             : }
     367             : 
     368             : /* Computes the equation of the line tangent to R and returns its
     369             :    evaluation at the point Q. Also doubles the point R.
     370             :  */
     371             : 
     372             : static GEN
     373         581 : F2xqE_tangent_update(GEN R, GEN Q, GEN a2, GEN T, GEN *pt_R)
     374             : {
     375         581 :   if (ell_is_inf(R))
     376             :   {
     377           0 :     *pt_R = ellinf();
     378           0 :     return pol1_F2x(T[1]);
     379             :   }
     380         581 :   else if (!lgpol(gel(R,1)))
     381             :   {
     382           0 :     *pt_R = ellinf();
     383           0 :     return F2xqE_vert(R, Q, T);
     384             :   } else {
     385             :     GEN slope;
     386         581 :     *pt_R = F2xqE_dbl_slope(R, a2, T, &slope);
     387         581 :     return F2xqE_Miller_line(R, Q, slope, a2, T);
     388             :   }
     389             : }
     390             : 
     391             : /* Computes the equation of the line through R and P, and returns its
     392             :    evaluation at the point Q. Also adds P to the point R.
     393             :  */
     394             : 
     395             : static GEN
     396         574 : F2xqE_chord_update(GEN R, GEN P, GEN Q, GEN a2, GEN T, GEN *pt_R)
     397             : {
     398         574 :   if (ell_is_inf(R))
     399             :   {
     400           0 :     *pt_R = gcopy(P);
     401           0 :     return F2xqE_vert(P, Q, T);
     402             :   }
     403         574 :   else if (ell_is_inf(P))
     404             :   {
     405           0 :     *pt_R = gcopy(R);
     406           0 :     return F2xqE_vert(R, Q, T);
     407             :   }
     408         574 :   else if (F2x_equal(gel(P, 1), gel(R, 1)))
     409             :   {
     410         539 :     if (F2x_equal(gel(P, 2), gel(R, 2)))
     411           0 :       return F2xqE_tangent_update(R, Q, a2, T, pt_R);
     412             :     else
     413             :     {
     414         539 :       *pt_R = ellinf();
     415         539 :       return F2xqE_vert(R, Q, T);
     416             :     }
     417             :   } else {
     418             :     GEN slope;
     419          35 :     *pt_R = F2xqE_add_slope(P, R, a2, T, &slope);
     420          35 :     return F2xqE_Miller_line(R, Q, slope, a2, T);
     421             :   }
     422             : }
     423             : 
     424             : /* Returns the Miller function f_{m, Q} evaluated at the point P using
     425             :    the standard Miller algorithm.
     426             :  */
     427             : 
     428             : struct _F2xqE_miller
     429             : {
     430             :   GEN T, a2, P;
     431             : };
     432             : 
     433             : static GEN
     434         581 : F2xqE_Miller_dbl(void* E, GEN d)
     435             : {
     436         581 :   struct _F2xqE_miller *m = (struct _F2xqE_miller *)E;
     437         581 :   GEN T = m->T, a2 = m->a2, P = m->P;
     438             :   GEN v, line;
     439         581 :   GEN num = F2xq_sqr(gel(d,1), T);
     440         581 :   GEN denom = F2xq_sqr(gel(d,2), T);
     441         581 :   GEN point = gel(d,3);
     442         581 :   line = F2xqE_tangent_update(point, P, a2, T, &point);
     443         581 :   num  = F2xq_mul(num, line, T);
     444         581 :   v = F2xqE_vert(point, P, T);
     445         581 :   denom = F2xq_mul(denom, v, T);
     446         581 :   return mkvec3(num, denom, point);
     447             : }
     448             : 
     449             : static GEN
     450         574 : F2xqE_Miller_add(void* E, GEN va, GEN vb)
     451             : {
     452         574 :   struct _F2xqE_miller *m = (struct _F2xqE_miller *)E;
     453         574 :   GEN T = m->T, a2 = m->a2, P = m->P;
     454             :   GEN v, line, point;
     455         574 :   GEN na = gel(va,1), da = gel(va,2), pa = gel(va,3);
     456         574 :   GEN nb = gel(vb,1), db = gel(vb,2), pb = gel(vb,3);
     457         574 :   GEN num   = F2xq_mul(na, nb, T);
     458         574 :   GEN denom = F2xq_mul(da, db, T);
     459         574 :   line = F2xqE_chord_update(pa, pb, P, a2, T, &point);
     460         574 :   num  = F2xq_mul(num, line, T);
     461         574 :   v = F2xqE_vert(point, P, T);
     462         574 :   denom = F2xq_mul(denom, v, T);
     463         574 :   return mkvec3(num, denom, point);
     464             : }
     465             : 
     466             : static GEN
     467         574 : F2xqE_Miller(GEN Q, GEN P, GEN m, GEN a2, GEN T)
     468             : {
     469         574 :   pari_sp ltop = avma;
     470             :   struct _F2xqE_miller d;
     471             :   GEN v, num, denom, g1;
     472             : 
     473         574 :   d.a2 = a2; d.T = T; d.P = P;
     474         574 :   g1 = pol1_F2x(T[1]);
     475         574 :   v = gen_pow(mkvec3(g1,g1,Q), m, (void*)&d, F2xqE_Miller_dbl, F2xqE_Miller_add);
     476         574 :   num = gel(v,1); denom = gel(v,2);
     477         574 :   return gerepileupto(ltop, F2xq_div(num, denom, T));
     478             : }
     479             : 
     480             : GEN
     481         350 : F2xqE_weilpairing(GEN P, GEN Q, GEN m, GEN a2, GEN T)
     482             : {
     483         350 :   pari_sp ltop = avma;
     484             :   GEN num, denom, result;
     485         350 :   if (ell_is_inf(P) || ell_is_inf(Q) || F2x_equal(P,Q))
     486          77 :     return pol1_F2x(T[1]);
     487         273 :   num    = F2xqE_Miller(P, Q, m, a2, T);
     488         273 :   denom  = F2xqE_Miller(Q, P, m, a2, T);
     489         273 :   result = F2xq_div(num, denom, T);
     490         273 :   return gerepileupto(ltop, result);
     491             : }
     492             : 
     493             : GEN
     494          28 : F2xqE_tatepairing(GEN P, GEN Q, GEN m, GEN a2, GEN T)
     495             : {
     496          28 :   if (ell_is_inf(P) || ell_is_inf(Q))
     497           0 :     return pol1_F2x(T[1]);
     498          28 :   return F2xqE_Miller(P, Q, m, a2, T);
     499             : }
     500             : 
     501             : /***********************************************************************/
     502             : /**                                                                   **/
     503             : /**                          Point counting                           **/
     504             : /**                                                                   **/
     505             : /***********************************************************************/
     506             : 
     507             : static GEN
     508       39182 : Z2x_rshift(GEN y, long x)
     509             : {
     510             :   GEN z;
     511             :   long i, l;
     512       39182 :   if (!x) return pol0_Flx(y[1]);
     513       39182 :   z = cgetg_copy(y, &l); z[1] = y[1];
     514       39208 :   for(i=2; i<l; i++) z[i] = y[i]>>x;
     515       39208 :   return Flx_renormalize(z, l);
     516             : }
     517             : 
     518             : /* Solve the linear equation approximation in the Newton algorithm */
     519             : 
     520             : static GEN
     521      108340 : gen_Z2x_Dixon(GEN F, GEN V, long N, void *E, GEN lin(void *E, GEN F, GEN d, long N), GEN invl(void *E, GEN d))
     522             : {
     523      108340 :   pari_sp av = avma;
     524             :   long N2, M;
     525             :   GEN VN2, V2, VM, bil;
     526      108340 :   ulong q = 1UL<<N;
     527      108340 :   if (N == 1) return invl(E, V);
     528       39204 :   V = Flx_red(V, q);
     529       39197 :   N2 = (N + 1)>>1; M = N - N2;
     530       39197 :   F = FlxT_red(F, q);
     531       39195 :   VN2 = gen_Z2x_Dixon(F, V, N2, E, lin, invl);
     532       39203 :   bil = lin(E, F, VN2, N);
     533       39200 :   V2 = Z2x_rshift(Flx_sub(V, bil, q), N2);
     534       39196 :   VM = gen_Z2x_Dixon(F, V2, M, E, lin, invl);
     535       39201 :   return gerepileupto(av, Flx_add(VN2, Flx_Fl_mul(VM, 1UL<<N2, q), q));
     536             : }
     537             : 
     538             : /* Solve F(X) = V mod 2^N
     539             :    F(Xn) = V [mod 2^n]
     540             :    Vm = (V-F(Xn))/(2^n)
     541             :    F(Xm) = Vm
     542             :    X = Xn + 2^n*Xm
     543             : */
     544             : 
     545             : static GEN
     546       29955 : gen_Z2X_Dixon(GEN F, GEN V, long N, void *E,
     547             :                      GEN lin(void *E, GEN F, GEN d, long N),
     548             :                      GEN lins(void *E, GEN F, GEN d, long N),
     549             :                      GEN invls(void *E, GEN d))
     550             : {
     551       29955 :   pari_sp av = avma;
     552             :   long n, m;
     553             :   GEN Xn, Xm, FXn, Vm;
     554       29955 :   if (N<BITS_IN_LONG)
     555             :   {
     556       29935 :     ulong q = 1UL<<N;
     557       29935 :     return Flx_to_ZX(gen_Z2x_Dixon(ZXT_to_FlxT(F,q), ZX_to_Flx(V,q),N,E,lins,invls));
     558             :   }
     559          20 :   V = ZX_remi2n(V, N);
     560          20 :   n = (N + 1)>>1; m = N - n;
     561          20 :   F = ZXT_remi2n(F, N);
     562          20 :   Xn = gen_Z2X_Dixon(F, V, n, E, lin, lins, invls);
     563          20 :   FXn = lin(E, F, Xn, N);
     564          20 :   Vm = ZX_shifti(ZX_sub(V, FXn), -n);
     565          20 :   Xm = gen_Z2X_Dixon(F, Vm, m, E, lin, lins, invls);
     566          20 :   return gerepileupto(av, ZX_remi2n(ZX_add(Xn, ZX_shifti(Xm, n)), N));
     567             : }
     568             : 
     569             : /* H -> H mod 2*/
     570             : 
     571       34278 : static GEN _can_invls(void *E, GEN V) {(void) E; return V; }
     572             : 
     573             : /* H -> H-(f0*H0-f1*H1) */
     574             : 
     575          10 : static GEN _can_lin(void *E, GEN F, GEN V, long N)
     576             : {
     577          10 :   pari_sp av=avma;
     578             :   GEN d0, d1, z;
     579             :   (void) E;
     580          10 :   RgX_even_odd(V, &d0, &d1);
     581          10 :   z =  ZX_sub(V, ZX_sub(ZX_mul(gel(F,1), d0), ZX_mul(gel(F,2), d1)));
     582          10 :   return gerepileupto(av, ZX_remi2n(z, N));
     583             : }
     584             : 
     585       19444 : static GEN _can_lins(void *E, GEN F, GEN V, long N)
     586             : {
     587       19444 :   GEN D=Flx_splitting(V, 2), z;
     588       19444 :   ulong q = 1UL<<N;
     589             :   (void) E;
     590       19444 :   z = Flx_sub(Flx_mul(gel(F,1), gel(D,1), q), Flx_mul(gel(F,2), gel(D,2), q), q);
     591       19449 :   return Flx_sub(V, z, q);
     592             : }
     593             : 
     594             : /* P -> P-(P0^2-X*P1^2) */
     595             : 
     596             : static GEN
     597       14821 : _can_iter(void *E, GEN f2, GEN q)
     598             : {
     599             :   GEN f0, f1, z;
     600             :   (void) E;
     601       14821 :   RgX_even_odd(f2, &f0, &f1);
     602       14821 :   z = ZX_add(ZX_sub(f2, FpX_sqr(f0, q)), RgX_shift_shallow(FpX_sqr(f1, q), 1));
     603       14821 :   return mkvec3(z,f0,f1);
     604             : }
     605             : 
     606             : /* H -> H-(2*P0*H0-2*X*P1*H1) */
     607             : 
     608             : static GEN
     609       14820 : _can_invd(void *E, GEN V, GEN v, GEN q, long M)
     610             : {
     611             :   GEN F;
     612             :   (void)E; (void)q;
     613       14820 :   F = mkvec2(ZX_shifti(gel(v,2),1), ZX_shifti(RgX_shift_shallow(gel(v,3),1),1));
     614       14821 :   return gen_Z2X_Dixon(F, V, M, NULL, _can_lin, _can_lins, _can_invls);
     615             : }
     616             : 
     617             : /* Lift P to Q such that Q(x^2)=Q(x)*Q(-x) mod 2^n
     618             :    if Q = Q0(X^2)+X*Q1(X^2), solve Q(x^2) = Q0^2-X*Q1^2
     619             : */
     620             : static GEN
     621        6921 : F2x_canonlift(GEN P, long n)
     622        6921 : { return gen_ZpX_Newton(F2x_to_ZX(P),gen_2, n, NULL, _can_iter, _can_invd); }
     623             : 
     624             : static GEN
     625       15104 : Z2XQ_frob(GEN x, GEN T, GEN q)
     626             : {
     627       15104 :   return FpX_rem(RgX_inflate(x, 2), T, q);
     628             : }
     629             : 
     630             : static GEN
     631       19755 : Z2xq_frob(GEN x, GEN T, ulong q)
     632             : {
     633       19755 :   return Flx_rem(Flx_inflate(x, 2), T, q);
     634             : }
     635             : 
     636             : struct _frob_lift
     637             : {
     638             :   GEN T, sqx;
     639             : };
     640             : 
     641             : /* H -> S^-1(H) mod 2 */
     642             : 
     643       34853 : static GEN _frob_invls(void *E, GEN V)
     644             : {
     645       34853 :   struct _frob_lift *F = (struct _frob_lift*) E;
     646       34853 :   GEN sqx = F->sqx;
     647       34853 :   return F2x_to_Flx(F2xq_sqrt_fast(Flx_to_F2x(V), gel(sqx,1), gel(sqx,2)));
     648             : }
     649             : 
     650             : /* H -> f1*S(H) + f2*H */
     651             : 
     652          10 : static GEN _frob_lin(void *E, GEN F, GEN x2, long N)
     653             : {
     654          10 :   GEN T = gel(F,3);
     655          10 :   GEN q = int2n(N);
     656          10 :   GEN y2  = Z2XQ_frob(x2, T, q);
     657          10 :   GEN lin = ZX_add(ZX_mul(gel(F,1), y2), ZX_mul(gel(F,2), x2));
     658             :   (void) E;
     659          10 :   return FpX_rem(ZX_remi2n(lin, N), T, q);
     660             : }
     661             : 
     662       19753 : static GEN _frob_lins(void *E, GEN F, GEN x2, long N)
     663             : {
     664       19753 :   GEN T = gel(F,3);
     665       19753 :   ulong q = 1UL<<N;
     666       19753 :   GEN y2  = Z2xq_frob(x2, T, q);
     667       19752 :   GEN lin = Flx_add(Flx_mul(gel(F,1), y2,q), Flx_mul(gel(F,2), x2,q),q);
     668             :   (void) E;
     669       19753 :   return Flx_rem(lin, T, q);
     670             : }
     671             : 
     672             : /* X -> P(X,S(X)) */
     673             : 
     674             : static GEN
     675       15094 : _lift_iter(void *E, GEN x2, GEN q)
     676             : {
     677       15094 :   struct _frob_lift *F = (struct _frob_lift*) E;
     678       15094 :   long N = expi(q);
     679       15094 :   GEN TN = ZXT_remi2n(F->T, N);
     680       15094 :   GEN y2 = Z2XQ_frob(x2, TN, q);
     681       15093 :   GEN x2y2 = FpX_rem(ZX_remi2n(ZX_mul(x2, y2), N), TN, q);
     682       15094 :   GEN s = ZX_add(ZX_add(x2, ZX_shifti(y2, 1)), ZX_shifti(x2y2, 3));
     683       15094 :   GEN V = ZX_add(ZX_add(ZX_sqr(s), y2), ZX_shifti(x2y2, 2));
     684       15094 :   return mkvec4(FpX_rem(ZX_remi2n(V, N), TN, q),x2,y2,s);
     685             : }
     686             : 
     687             : /* H -> Dx*H+Dy*S(H) */
     688             : 
     689             : static GEN
     690       15094 : _lift_invd(void *E, GEN V, GEN v, GEN qM, long M)
     691             : {
     692       15094 :   struct _frob_lift *F = (struct _frob_lift*) E;
     693       15094 :   GEN TM = ZXT_remi2n(F->T, M);
     694       15094 :   GEN x2 = gel(v,2), y2 = gel(v,3), s = gel(v,4), r;
     695       15094 :   GEN Dx = ZX_add(ZX_mul(ZX_Z_add(ZX_shifti(y2, 4), gen_2), s),
     696             :                          ZX_shifti(y2, 2));
     697       15094 :   GEN Dy = ZX_add(ZX_Z_add(ZX_mul(ZX_Z_add(ZX_shifti(x2, 4), utoi(4)), s),
     698             :                            gen_1), ZX_shifti(x2, 2));
     699       15094 :   Dx = FpX_rem(ZX_remi2n(Dx, M), TM, qM);
     700       15094 :   Dy = FpX_rem(ZX_remi2n(Dy, M), TM, qM);
     701       15094 :   r = mkvec3(Dy, Dx, TM);
     702       15094 :   return gen_Z2X_Dixon(r, V, M, E, _frob_lin, _frob_lins, _frob_invls);
     703             : }
     704             : 
     705             : /*
     706             :   Let P(X,Y)=(X+2*Y+8*X*Y)^2+Y+4*X*Y
     707             :   Solve   P(x,S(x))=0 [mod 2^n,T]
     708             :   assuming  x = x0    [mod 2,T]
     709             : 
     710             :   we set s = X+2*Y+8*X*Y, P = s^2+Y+4*X*Y
     711             :   Dx = dP/dx = (16*s+4)*x+(4*s+1)
     712             :   Dy = dP/dy = (16*y+2)*s+4*y
     713             : */
     714             : 
     715             : static GEN
     716        7159 : solve_AGM_eqn(GEN x0, long n, GEN T, GEN sqx)
     717             : {
     718             :   struct _frob_lift F;
     719        7159 :   F.T=T; F.sqx=sqx;
     720        7159 :   return gen_ZpX_Newton(x0, gen_2, n, &F, _lift_iter, _lift_invd);
     721             : }
     722             : 
     723             : static GEN
     724         238 : Z2XQ_invnorm_pcyc(GEN a, GEN T, long e)
     725             : {
     726         238 :   GEN q = int2n(e);
     727         238 :   GEN z = ZpXQ_norm_pcyc(a, T, q, gen_2);
     728         238 :   return Fp_inv(z, q);
     729             : }
     730             : 
     731             : /* Assume a = 1 [4] */
     732             : static GEN
     733        6921 : Z2XQ_invnorm(GEN a, GEN T, long e)
     734             : {
     735             :   pari_timer ti;
     736        6921 :   GEN pe = int2n(e), s;
     737        6921 :   if (degpol(a)==0)
     738          56 :     return Fp_inv(Fp_powu(gel(a,2), get_FpX_degree(T), pe), pe);
     739        6865 :   if (DEBUGLEVEL>=3) timer_start(&ti);
     740        6865 :   s = ZpXQ_log(a, T, gen_2, e);
     741        6865 :   if (DEBUGLEVEL>=3) timer_printf(&ti,"Z2XQ_log");
     742        6865 :   s = Fp_neg(FpXQ_trace(s, T, pe), pe);
     743        6865 :   if (DEBUGLEVEL>=3) timer_printf(&ti,"FpXQ_trace");
     744        6865 :   s = modii(gel(Qp_exp(cvtop(s, gen_2, e-2)),4),pe);
     745        6865 :   if (DEBUGLEVEL>=3) timer_printf(&ti,"Qp_exp");
     746        6865 :   return s;
     747             : }
     748             : 
     749             : /* Assume a2==0, so 4|E(F_p): if t^4 = a6 then (t,t^2) is of order 4
     750             :    8|E(F_p) <=> trace(a6)==0
     751             :  */
     752             : 
     753             : static GEN
     754        7341 : F2xq_elltrace_Harley(GEN a6, GEN T2)
     755             : {
     756        7341 :   pari_sp ltop = avma;
     757             :   pari_timer ti;
     758             :   GEN T, sqx;
     759             :   GEN x, x2, t;
     760        7341 :   long n = F2x_degree(T2), N = ((n + 1)>>1) + 2;
     761             :   long ispcyc;
     762        7341 :   if (n==1) return gen_m1;
     763        7313 :   if (n==2) return F2x_degree(a6) ? gen_1 : stoi(-3);
     764        7474 :   if (n==3) return F2x_degree(a6) ? (F2xq_trace(a6,T2) ?  stoi(-3): gen_1)
     765         203 :                                   : stoi(5);
     766        7159 :   timer_start(&ti);
     767        7159 :   sqx = mkvec2(F2xq_sqrt(polx_F2x(T2[1]),T2), T2);
     768        7159 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Sqrtx");
     769        7159 :   ispcyc = zx_is_pcyc(F2x_to_Flx(T2));
     770        7159 :   T = ispcyc? F2x_to_ZX(T2): F2x_canonlift(T2, N-2);
     771        7159 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Teich");
     772        7159 :   T = FpX_get_red(T, int2n(N));
     773        7159 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Barrett");
     774        7159 :   x = solve_AGM_eqn(F2x_to_ZX(a6), N-2, T, sqx);
     775        7159 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Lift");
     776        7159 :   x2 = ZX_Z_add_shallow(ZX_shifti(x,2), gen_1);
     777        7159 :   t = (ispcyc? Z2XQ_invnorm_pcyc: Z2XQ_invnorm)(x2, T, N);
     778        7159 :   if (DEBUGLEVEL>1) timer_printf(&ti,"Norm");
     779        7159 :   if (cmpii(sqri(t), int2n(n + 2)) > 0)
     780        3362 :     t = subii(t, int2n(N));
     781        7159 :   return gerepileuptoint(ltop, t);
     782             : }
     783             : 
     784             : GEN
     785        7551 : F2xq_ellcard(GEN a, GEN a6, GEN T)
     786             : {
     787        7551 :   pari_sp av = avma;
     788        7551 :   long n = F2x_degree(T);
     789        7551 :   GEN q = int2u(n), c;
     790        7551 :   if (typ(a)==t_VECSMALL)
     791             :   {
     792        7341 :     GEN t = F2xq_elltrace_Harley(a6, T);
     793        7341 :     c = addii(q, F2xq_trace(a,T) ? addui(1,t): subui(1,t));
     794         210 :   } else if (n==1)
     795             :   {
     796          77 :     long a4i = lgpol(gel(a,2)), a6i = lgpol(a6);
     797          77 :     return utoi(a4i? (a6i? 1: 5): 3);
     798             :   }
     799         133 :   else if (n==2)
     800             :   {
     801         105 :     GEN a3 = gel(a,1), a4 = gel(a,2), x = polx_F2x(T[1]), x1 = pol1_F2x(T[1]);
     802         105 :     GEN a613 = F2xq_mul(F2x_add(x1, a6),a3,T), a43= F2xq_mul(a4,a3,T);
     803         105 :     long f0= F2xq_trace(F2xq_mul(a6,a3,T),T);
     804         105 :     long f1= F2xq_trace(F2x_add(a43,a613),T);
     805         105 :     long f2= F2xq_trace(F2x_add(F2xq_mul(a43,x,T),a613),T);
     806         105 :     long f3= F2xq_trace(F2x_add(F2xq_mul(a43,F2x_add(x,x1),T),a613),T);
     807         105 :     c = utoi(9-2*(f0+f1+f2+f3));
     808             :   }
     809             :   else
     810             :   {
     811             :     struct _F2xqE e;
     812          28 :     long m = (n+1)>>1;
     813          28 :     GEN q1 = addis(q, 1);
     814          28 :     GEN v = n==4 ? mkvec4s(13,17,21,25)
     815          35 :                  : odd(n) ? mkvec3(subii(q1,int2u(m)),q1,addii(q1,int2u(m))):
     816           7 :                             mkvec5(subii(q1,int2u(m+1)),subii(q1,int2u(m)),q1,
     817           7 :                                    addii(q1,int2u(m)),addii(q1,int2u(m+1)));
     818          28 :     e.a2=a; e.a6=a6; e.T=T;
     819          28 :     c = gen_select_order(v,(void*)&e, &F2xqE_group);
     820          28 :     if (n==4 && absequaliu(c, 21)) /* Ambiguous case */
     821             :     {
     822           7 :       GEN d = F2xq_powu(polx_F2x(T[1]),3,T), a3 = gel(a,1);
     823           7 :       e.a6 = F2x_add(a6,F2xq_mul(d,F2xq_sqr(a3,T),T)); /* twist */
     824           7 :       c = subui(34, gen_select_order(mkvec2s(13,25),(void*)&e, &F2xqE_group));
     825             :     }
     826             :   }
     827        7474 :   return gerepileuptoint(av, c);
     828             : }
     829             : 
     830             : /***********************************************************************/
     831             : /**                                                                   **/
     832             : /**                          Group structure                          **/
     833             : /**                                                                   **/
     834             : /***********************************************************************/
     835             : 
     836             : static GEN
     837         343 : _F2xqE_pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)
     838             : {
     839         343 :   struct _F2xqE *e = (struct _F2xqE *) E;
     840         343 :   return  F2xq_order(F2xqE_weilpairing(P,Q,m,e->a2,e->T), F, e->T);
     841             : }
     842             : 
     843             : GEN
     844        3654 : F2xq_ellgroup(GEN a2, GEN a6, GEN N, GEN T, GEN *pt_m)
     845             : {
     846             :   struct _F2xqE e;
     847        3654 :   GEN q = int2u(F2x_degree(T));
     848        3654 :   e.a2=a2; e.a6=a6; e.T=T;
     849        3654 :   return gen_ellgroup(N, subis(q,1), pt_m, (void*)&e, &F2xqE_group,
     850             :                                                       _F2xqE_pairorder);
     851             : }
     852             : 
     853             : GEN
     854        3584 : F2xq_ellgens(GEN a2, GEN a6, GEN ch, GEN D, GEN m, GEN T)
     855             : {
     856             :   GEN P;
     857        3584 :   pari_sp av = avma;
     858             :   struct _F2xqE e;
     859        3584 :   e.a2=a2; e.a6=a6; e.T=T;
     860        3584 :   switch(lg(D)-1)
     861             :   {
     862             :   case 0:
     863           7 :     return cgetg(1,t_VEC);
     864             :   case 1:
     865        3479 :     P = gen_gener(gel(D,1), (void*)&e, &F2xqE_group);
     866        3479 :     P = mkvec(F2xqE_changepoint(P, ch, T));
     867        3479 :     break;
     868             :   default:
     869          98 :     P = gen_ellgens(gel(D,1), gel(D,2), m, (void*)&e, &F2xqE_group,
     870             :                                                       _F2xqE_pairorder);
     871          98 :     gel(P,1) = F2xqE_changepoint(gel(P,1), ch, T);
     872          98 :     gel(P,2) = F2xqE_changepoint(gel(P,2), ch, T);
     873          98 :     break;
     874             :   }
     875        3577 :   return gerepilecopy(av, P);
     876             : }

Generated by: LCOV version 1.11