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 - bb_group.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22860-5579deb0b) Lines: 538 581 92.6 %
Date: 2018-07-18 05:36:42 Functions: 35 36 97.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2004  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             : /***********************************************************************/
      15             : /**                                                                   **/
      16             : /**             GENERIC ALGORITHMS ON BLACKBOX GROUP                  **/
      17             : /**                                                                   **/
      18             : /***********************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : #undef pow /* AIX: pow(a,b) is a macro, wrongly expanded on grp->pow(a,b,c) */
      22             : 
      23             : /***********************************************************************/
      24             : /**                                                                   **/
      25             : /**                    POWERING                                       **/
      26             : /**                                                                   **/
      27             : /***********************************************************************/
      28             : 
      29             : /* return (n>>(i+1-l)) & ((1<<l)-1) */
      30             : static ulong
      31     3909226 : int_block(GEN n, long i, long l)
      32             : {
      33     3909226 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      34     3909365 :   GEN nw = int_W(n, q);
      35     3909365 :   ulong w = (ulong) *nw, w2;
      36     3909365 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      37      292701 :   w &= (1UL<<r)-1; lr = l-r;
      38      292701 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      39      292701 :   return (w<<lr)|w2;
      40             : }
      41             : 
      42             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      43             : static GEN
      44     6928166 : sliding_window_powu(GEN x, ulong n, long e, void *E, GEN (*sqr)(void*,GEN),
      45             :                                                      GEN (*mul)(void*,GEN,GEN))
      46             : {
      47             :   pari_sp av;
      48     6928166 :   long i, l = expu(n), u = (1UL<<(e-1));
      49             :   long w, v;
      50     6928392 :   GEN tab = cgetg(1+u, t_VEC);
      51     6930813 :   GEN x2 = sqr(E, x), z = NULL, tw;
      52     6474808 :   gel(tab, 1) = x;
      53     6474808 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      54     6468403 :   av = avma;
      55    61152426 :   while (l>=0)
      56             :   {
      57    47758302 :     if (e > l+1) e = l+1;
      58    47758302 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      59    47900622 :     tw = gel(tab, 1+(w>>(v+1)));
      60    47900622 :     if (z)
      61             :     {
      62    40974987 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
      63    40862611 :       z = mul(E, z, tw);
      64     6925635 :     } else z = tw;
      65    47729929 :     for (i=1; i<=v; i++) z = sqr(E, z);
      66   135980845 :     while (l>=0)
      67             :     {
      68    80925438 :       if (gc_needed(av,1))
      69             :       {
      70           0 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_powu (%ld)", l);
      71           0 :         z = gerepilecopy(av, z);
      72             :       }
      73    81429839 :       if (n&(1UL<<l)) break;
      74    40532454 :       z = sqr(E, z); l--;
      75             :     }
      76             :   }
      77     6925721 :   return z;
      78             : }
      79             : 
      80             : 
      81             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      82             : static GEN
      83       82337 : sliding_window_pow(GEN x, GEN n, long e, void *E, GEN (*sqr)(void*,GEN),
      84             :                                                   GEN (*mul)(void*,GEN,GEN))
      85             : {
      86             :   pari_sp av;
      87       82337 :   long i, l = expi(n), u = (1UL<<(e-1));
      88             :   long w, v;
      89       82336 :   GEN tab = cgetg(1+u, t_VEC);
      90       82336 :   GEN x2 = sqr(E, x), z = NULL, tw;
      91       78795 :   gel(tab, 1) = x;
      92       78795 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      93       78803 :   av = avma;
      94     3357585 :   while (l>=0)
      95             :   {
      96     3196510 :     if (e > l+1) e = l+1;
      97     3196510 :     w = int_block(n,l,e); v = vals(w); l-=e;
      98     3201401 :     tw = gel(tab, 1+(w>>(v+1)));
      99     3201401 :     if (z)
     100             :     {
     101     3119078 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
     102     3113852 :       z = mul(E, z, tw);
     103       82323 :     } else z = tw;
     104     3196725 :     for (i=1; i<=v; i++) z = sqr(E, z);
     105    13469258 :     while (l>=0)
     106             :     {
     107    10187030 :       if (gc_needed(av,1))
     108             :       {
     109         456 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110         456 :         z = gerepilecopy(av, z);
     111             :       }
     112    10187030 :       if (int_bit(n,l)) break;
     113     7071265 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116       82272 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    54717089 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                               GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    54717089 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    54717089 :   if (n == 1) return x;
     129    54717089 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    54717089 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   149862067 :   for (; j; n<<=1,j--)
     134             :   {
     135    95148244 :     y = sqr(E,y);
     136    95144557 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137    95144978 :     if (gc_needed(av,1))
     138             :     {
     139           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"leftright_powu (%d)", j);
     140           0 :       y = gerepilecopy(av, y);
     141             :     }
     142             :   }
     143    54713823 :   return y;
     144             : }
     145             : 
     146             : GEN
     147    61737610 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150             :   long l;
     151    61737610 :   if (n == 1) return x;
     152    61643046 :   l = expu(n);
     153    61645329 :   if (l<=8)
     154    54715172 :     return leftright_binary_powu(x, n, E, sqr, mul);
     155             :   else
     156     6930157 :     return sliding_window_powu(x, n, l<=24? 2: 3, E, sqr, mul);
     157             : }
     158             : 
     159             : GEN
     160     1324474 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     161             :                                   GEN (*mul)(void*,GEN,GEN))
     162             : {
     163     1324474 :   pari_sp av = avma;
     164     1324474 :   if (n == 1) return gcopy(x);
     165     1197910 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     166             : }
     167             : 
     168             : GEN
     169    23494397 : gen_pow_i(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     170             :                                  GEN (*mul)(void*,GEN,GEN))
     171             : {
     172             :   long l, e;
     173    23494397 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     174       82337 :   l = expi(n);
     175       82339 :   if      (l<=64)  e = 3;
     176       60452 :   else if (l<=160) e = 4;
     177       26585 :   else if (l<=384) e = 5;
     178       18542 :   else if (l<=896) e = 6;
     179        9865 :   else             e = 7;
     180       82339 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     181             : }
     182             : 
     183             : GEN
     184     1065497 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     185             :                                GEN (*mul)(void*,GEN,GEN))
     186             : {
     187     1065497 :   pari_sp av = avma;
     188     1065497 :   return gerepilecopy(av, gen_pow_i(x,n,E,sqr,mul));
     189             : }
     190             : 
     191             : /* assume n > 0. Compute x^n using left-right binary powering */
     192             : GEN
     193      311566 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     194             :                                          GEN (*msqr)(void*,GEN))
     195             : {
     196      311566 :   pari_sp av = avma;
     197             :   GEN y;
     198             :   int j;
     199             : 
     200      311566 :   if (n == 1) return x;
     201      311566 :   y = x; j = 1+bfffo(n);
     202             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     203      311566 :   n<<=j; j = BITS_IN_LONG-j;
     204             :   /* first bit is now implicit */
     205     2906977 :   for (; j; n<<=1,j--)
     206             :   {
     207     2595411 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     208     1984559 :     else y = sqr(E,y);
     209     2595411 :     if (gc_needed(av,1))
     210             :     {
     211           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"gen_powu_fold (%d)", j);
     212           0 :       y = gerepilecopy(av, y);
     213             :     }
     214             :   }
     215      311566 :   return y;
     216             : }
     217             : GEN
     218           0 : gen_powu_fold(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     219             :                                        GEN (*msqr)(void*,GEN))
     220             : {
     221           0 :   pari_sp av = avma;
     222           0 :   if (n == 1) return gcopy(x);
     223           0 :   return gerepilecopy(av, gen_powu_fold_i(x,n,E,sqr,msqr));
     224             : }
     225             : 
     226             : /* assume N != 0, t_INT. Compute x^|N| using left-right binary powering */
     227             : GEN
     228      208345 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     229             :                                       GEN (*msqr)(void*,GEN))
     230             : {
     231      208345 :   long ln = lgefint(N);
     232      208345 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     233             :   else
     234             :   {
     235       96807 :     GEN nd = int_MSW(N), y = x;
     236       96807 :     ulong n = *nd;
     237             :     long i;
     238             :     int j;
     239       96807 :     pari_sp av = avma;
     240             : 
     241       96807 :     if (n == 1)
     242       15737 :       j = 0;
     243             :     else
     244             :     {
     245       81070 :       j = 1+bfffo(n); /* < BIL */
     246             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     247       81070 :       n <<= j; j = BITS_IN_LONG - j;
     248             :     }
     249             :     /* first bit is now implicit */
     250       96807 :     for (i=ln-2;;)
     251             :     {
     252    26196475 :       for (; j; n<<=1,j--)
     253             :       {
     254    25269437 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     255    15530393 :         else y = sqr(E,y);
     256    25260048 :         if (gc_needed(av,1))
     257             :         {
     258           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"gen_pow_fold (%d)", j);
     259           0 :           y = gerepilecopy(av, y);
     260             :         }
     261             :       }
     262      507228 :       if (--i == 0) return y;
     263      419810 :       nd = int_precW(nd);
     264      419810 :       n = *nd; j = BITS_IN_LONG;
     265             :     }
     266             :   }
     267             : }
     268             : GEN
     269      111539 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     270             :                                     GEN (*msqr)(void*,GEN))
     271             : {
     272      111539 :   pari_sp av = avma;
     273      111539 :   return gerepilecopy(av, gen_pow_fold_i(x,n,E,sqr,msqr));
     274             : }
     275             : 
     276             : GEN
     277         126 : gen_pow_init(GEN x, GEN n, long k, void *E, GEN (*sqr)(void*,GEN), GEN (*mul)(void*,GEN,GEN))
     278             : {
     279         126 :   long i, j, l = expi(n);
     280         126 :   long m = 1UL<<(k-1);
     281         126 :   GEN x2 = sqr(E, x), y = gcopy(x);
     282         126 :   GEN R = cgetg(m+1, t_VEC);
     283         609 :   for(i = 1; i <= m; i++)
     284             :   {
     285         483 :     GEN C = cgetg(l+1, t_VEC);
     286         483 :     gel(C,1) = y;
     287       27622 :     for(j = 2; j <= l; j++)
     288       27139 :       gel(C,j) = sqr(E, gel(C,j-1));
     289         483 :     gel(R,i) = C;
     290         483 :     y = mul(E, y, x2);
     291             :   }
     292         126 :   return R;
     293             : }
     294             : 
     295             : GEN
     296       64735 : gen_pow_table(GEN R, GEN n, void *E, GEN (*one)(void*), GEN (*mul)(void*,GEN,GEN))
     297             : {
     298       64735 :   long e = expu(lg(R)-1) + 1;
     299       64735 :   long l = expi(n);
     300             :   long i, w;
     301       64735 :   GEN z = one(E), tw;
     302     1531596 :   for(i=0; i<=l; )
     303             :   {
     304     1402126 :     if (int_bit(n, i)==0) { i++; continue; }
     305      712728 :     if (i+e-1>l) e = l+1-i;
     306      712728 :     w = int_block(n,i+e-1,e);
     307      712728 :     tw = gmael(R, 1+(w>>1), i+1);
     308      712728 :     z = mul(E, z, tw);
     309      712728 :     i += e;
     310             :   }
     311       64735 :   return z;
     312             : }
     313             : 
     314             : GEN
     315     5781283 : gen_powers(GEN x, long l, int use_sqr, void *E, GEN (*sqr)(void*,GEN),
     316             :                                       GEN (*mul)(void*,GEN,GEN), GEN (*one)(void*))
     317             : {
     318             :   long i;
     319     5781283 :   GEN V = cgetg(l+2,t_VEC);
     320     5781287 :   gel(V,1) = one(E); if (l==0) return V;
     321     5768207 :   gel(V,2) = gcopy(x); if (l==1) return V;
     322     3244731 :   gel(V,3) = sqr(E,x);
     323     3244744 :   if (use_sqr)
     324    10797811 :     for(i = 4; i < l+2; i++)
     325    20676591 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     326    12230108 :                       : mul(E,gel(V, i-1),x);
     327             :   else
     328     2517487 :     for(i = 4; i < l+2; i++)
     329     1624085 :       gel(V,i) = mul(E,gel(V,i-1),x);
     330     3244731 :   return V;
     331             : }
     332             : 
     333             : GEN
     334    50174194 : producttree_scheme(long n)
     335             : {
     336             :   GEN v, w;
     337             :   long i, j, k, u, l;
     338    50174194 :   if (n<=2) return mkvecsmall(n);
     339    42929026 :   u = expu(n-1);
     340    42928995 :   v = cgetg(n+1,t_VECSMALL);
     341    42928960 :   w = cgetg(n+1,t_VECSMALL);
     342    42929183 :   v[1] = n; l = 1;
     343   143557785 :   for (i=1; i<=u; i++)
     344             :   {
     345   360126487 :     for(j=1, k=1; j<=l; j++, k+=2)
     346             :     {
     347   259497885 :       long vj = v[j], v2 = vj>>1;
     348   259497885 :       w[k]    = vj-v2;
     349   259497885 :       w[k+1]  = v2;
     350             :     }
     351   100628602 :     swap(v,w); l<<=1;
     352             :   }
     353    42929183 :   fixlg(v, l+1);
     354    42929212 :   avma = (pari_sp) v;
     355    42929212 :   return v;
     356             : }
     357             : 
     358             : GEN
     359    51762494 : gen_product(GEN x, void *data, GEN (*mul)(void *,GEN,GEN))
     360             : {
     361             :   pari_sp ltop;
     362    51762494 :   long i,k,lx = lg(x),lv;
     363             :   pari_timer ti;
     364             :   GEN v;
     365    51762494 :   if (DEBUGLEVEL>7) timer_start(&ti);
     366             : 
     367    51762514 :   if (lx == 1) return gen_1;
     368    51759484 :   if (lx == 2) return gcopy(gel(x,1));
     369    50121132 :   x = leafcopy(x); k = lx;
     370    50121330 :   v = producttree_scheme(lx-1); lv = lg(v);
     371    50122214 :   ltop = avma;
     372   359205818 :   for (k=1, i=1; k<lv; i+=v[k++])
     373   309084860 :     gel(x,k) = v[k]==1 ? gel(x,i): mul(data,gel(x,i),gel(x,i+1));
     374   200723996 :   while (k > 2)
     375             :   {
     376   100481846 :     if (DEBUGLEVEL>7)
     377           0 :       timer_printf(&ti,"gen_product: remaining objects %ld",k-1);
     378   100481566 :     lx = k; k = 1;
     379   359432703 :     for (i=1; i<lx-1; i+=2)
     380   258950406 :       gel(x,k++) = mul(data,gel(x,i),gel(x,i+1));
     381   100482297 :     if (i < lx) gel(x,k++) = gel(x,i);
     382   100482297 :     if (gc_needed(ltop,1))
     383          14 :       gerepilecoeffs(ltop,x+1,k-1);
     384             :   }
     385    50121192 :   return gel(x,1);
     386             : }
     387             : 
     388             : /***********************************************************************/
     389             : /**                                                                   **/
     390             : /**                    DISCRETE LOGARITHM                             **/
     391             : /**                                                                   **/
     392             : /***********************************************************************/
     393             : 
     394             : static GEN
     395    59543910 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     396             : {
     397    59543910 :   GEN a = gel(A,1);
     398    59543910 :   switch((h|grp->hash(a))%3UL)
     399             :   {
     400             :     case 0:
     401    19851546 :       return mkvec3(grp->pow(E,a,gen_2),Fp_mulu(gel(A,2),2,q),
     402    19851546 :                                         Fp_mulu(gel(A,3),2,q));
     403             :     case 1:
     404    19845229 :       return mkvec3(grp->mul(E,a,x),addis(gel(A,2),1),gel(A,3));
     405             :     case 2:
     406    19847135 :       return mkvec3(grp->mul(E,a,g),gel(A,2),addiu(gel(A,3),1));
     407             :   }
     408           0 :   return NULL;
     409             : }
     410             : 
     411             : /*Generic Pollard rho discrete log algorithm*/
     412             : static GEN
     413          49 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     414             : {
     415          49 :   pari_sp av=avma;
     416          49 :   GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
     417          49 :   ulong i, h = 0, imax = itou_or_0(sqrt4q);
     418          49 :   if (!imax) imax = ULONG_MAX;
     419             :   do {
     420             :  rho_restart:
     421          49 :     A = B = mkvec3(x,gen_1,gen_0);
     422          49 :     i=0;
     423             :     do {
     424    19847970 :       if (i>imax)
     425             :       {
     426           0 :         h++;
     427           0 :         if (DEBUGLEVEL)
     428           0 :           pari_warn(warner,"changing Pollard rho hash seed to %ld",h);
     429           0 :         goto rho_restart;
     430             :       }
     431    19847970 :       A = iter_rho(x, g, q, A, h, E, grp);
     432    19847970 :       B = iter_rho(x, g, q, B, h, E, grp);
     433    19847970 :       B = iter_rho(x, g, q, B, h, E, grp);
     434    19847970 :       if (gc_needed(av,2))
     435             :       {
     436        1740 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     437        1740 :         gerepileall(av, 2, &A, &B);
     438             :       }
     439    19847970 :       i++;
     440    19847970 :     } while (!grp->equal(gel(A,1), gel(B,1)));
     441          49 :     gel(A,2) = modii(gel(A,2), q);
     442          49 :     gel(B,2) = modii(gel(B,2), q);
     443          49 :     h++;
     444          49 :   } while (equalii(gel(A,2), gel(B,2)));
     445          49 :   l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
     446          49 :   return gerepileuptoint(av, l);
     447             : }
     448             : 
     449             : /* compute a hash of g^(i-1), 1<=i<=n. Return [sorted hash, perm, g^-n] */
     450             : GEN
     451     2679171 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
     452             : {
     453     2679171 :   GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
     454     2679171 :   pari_sp av=avma;
     455             :   long i;
     456     2679171 :   table[1] = grp->hash(grp->pow(E,g,gen_0));
     457    13416837 :   for (i=2; i<=n; i++)
     458             :   {
     459    10737666 :     table[i] = grp->hash(p1);
     460    10737666 :     p1 = grp->mul(E,p1,g);
     461    10737666 :     if (gc_needed(av,2))
     462             :     {
     463           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     464           0 :       p1 = gerepileupto(av, p1);
     465             :     }
     466             :   }
     467     2679171 :   G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
     468     2679171 :   perm = vecsmall_indexsort(table);
     469     2679171 :   table = vecsmallpermute(table,perm);
     470     2679171 :   return mkvec4(table,perm,g,G);
     471             : }
     472             : /* T from gen_Shanks_init(g,n). Return v < n*N such that x = g^v or NULL */
     473             : GEN
     474     2679619 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
     475             : {
     476     2679619 :   pari_sp av=avma;
     477     2679619 :   GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
     478     2679619 :   GEN p1 = x;
     479     2679619 :   long n = lg(table)-1;
     480             :   ulong k;
     481    14967656 :   for (k=0; k<N; k++)
     482             :   { /* p1 = x G^k, G = g^-n */
     483    14673411 :     long h = grp->hash(p1), i = zv_search(table, h);
     484    14673411 :     if (i)
     485             :     {
     486     2386119 :       do i--; while (i && table[i] == h);
     487     2385374 :       for (i++; i <= n && table[i] == h; i++)
     488             :       {
     489     2385374 :         GEN v = addiu(muluu(n,k), perm[i]-1);
     490     2385374 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     491           0 :         if (DEBUGLEVEL)
     492           0 :           err_printf("gen_Shanks_log: false positive %lu, %lu\n", k,h);
     493             :       }
     494             :     }
     495    12288037 :     p1 = grp->mul(E,p1,G);
     496    12288037 :     if (gc_needed(av,2))
     497             :     {
     498           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %lu", k);
     499           0 :       p1 = gerepileupto(av, p1);
     500             :     }
     501             :   }
     502      294245 :   return NULL;
     503             : }
     504             : /* Generic Shanks baby-step/giant-step algorithm. Return log_g(x), ord g = q.
     505             :  * One-shot: use gen_Shanks_init/log if many logs are desired; early abort
     506             :  * if log < sqrt(q) */
     507             : static GEN
     508      209122 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     509             : {
     510      209122 :   pari_sp av=avma, av1;
     511             :   long lbaby, i, k;
     512             :   GEN p1, table, giant, perm, ginv;
     513      209122 :   p1 = sqrti(q);
     514      209122 :   if (abscmpiu(p1,LGBITS) >= 0)
     515           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     516      209122 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     517      209122 :   ginv = grp->pow(E,g,gen_m1);
     518      209122 :   av1 = avma;
     519     1242434 :   for (p1=x, i=1;;i++)
     520             :   {
     521     2275746 :     if (grp->equal1(p1)) { avma = av; return stoi(i-1); }
     522     1217332 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     523     1033312 :     p1 = grp->mul(E,p1,ginv);
     524     1033312 :     if (gc_needed(av1,2))
     525             :     {
     526           7 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     527           7 :       p1 = gerepileupto(av1, p1);
     528             :     }
     529             :   }
     530      184020 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     531      184020 :   perm = vecsmall_indexsort(table);
     532      184020 :   table = vecsmallpermute(table,perm);
     533      184020 :   av1 = avma;
     534      421236 :   for (k=1; k<= lbaby; k++)
     535             :   {
     536      421226 :     long h = grp->hash(p1), i = zv_search(table, h);
     537      421226 :     if (i)
     538             :     {
     539      184011 :       while (table[i] == h && i) i--;
     540      184012 :       for (i++; i <= lbaby && table[i] == h; i++)
     541             :       {
     542      184011 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     543      184011 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     544           1 :         if (DEBUGLEVEL)
     545           0 :           err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
     546             :       }
     547             :     }
     548      237216 :     p1 = grp->mul(E,p1,giant);
     549      237216 :     if (gc_needed(av1,2))
     550             :     {
     551           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %ld", k);
     552           0 :       p1 = gerepileupto(av1, p1);
     553             :     }
     554             :   }
     555          10 :   avma = av; return cgetg(1, t_VEC); /* no solution */
     556             : }
     557             : 
     558             : /*Generic discrete logarithme in a group of prime order p*/
     559             : GEN
     560      928270 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     561             : {
     562      928270 :   if (grp->easylog)
     563             :   {
     564      904313 :     GEN e = grp->easylog(E, x, g, p);
     565      904313 :     if (e) return e;
     566             :   }
     567      302199 :   if (grp->equal1(x)) return gen_0;
     568      302157 :   if (grp->equal(x,g)) return gen_1;
     569      209171 :   if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
     570          49 :   return gen_Pollard_log(x, g, p, E, grp);
     571             : }
     572             : 
     573             : GEN
     574     7263062 : get_arith_ZZM(GEN o)
     575             : {
     576     7263062 :   if (!o) return NULL;
     577     7263062 :   switch(typ(o))
     578             :   {
     579             :     case t_INT:
     580     4825921 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     581           7 :       break;
     582             :     case t_MAT:
     583     1379859 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     584          14 :       break;
     585             :     case t_VEC:
     586     1057275 :       if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
     587           0 :       break;
     588             :   }
     589          28 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     590             :   return NULL; /* LCOV_EXCL_LINE */
     591             : }
     592             : GEN
     593      234207 : get_arith_Z(GEN o)
     594             : {
     595      234207 :   if (!o) return NULL;
     596      234207 :   switch(typ(o))
     597             :   {
     598             :     case t_INT:
     599      180715 :       if (signe(o) > 0) return o;
     600           7 :       break;
     601             :     case t_MAT:
     602          14 :       o = factorback(o);
     603           0 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     604           0 :       break;
     605             :     case t_VEC:
     606       53471 :       if (lg(o) != 3) break;
     607       53471 :       o = gel(o,1);
     608       53471 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     609           0 :       break;
     610             :   }
     611          14 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     612             :   return NULL; /* LCOV_EXCL_LINE */
     613             : }
     614             : 
     615             : /* grp->easylog() is an optional trapdoor function that catch easy logarithms*/
     616             : /* Generic Pohlig-Hellman discrete logarithm*/
     617             : /* smallest integer n such that g^n=a. Assume g has order ord */
     618             : GEN
     619      712650 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     620             : {
     621      712650 :   pari_sp av = avma;
     622             :   GEN v,t0,a0,b,q,g_q,n_q,ginv0,qj,ginv;
     623             :   GEN fa, ex;
     624             :   long e,i,j,l;
     625             : 
     626      712650 :   if (grp->equal(g, a)) /* frequent special case */
     627      136023 :     return grp->equal1(g)? gen_0: gen_1;
     628      576627 :   if (grp->easylog)
     629             :   {
     630      576522 :     GEN e = grp->easylog(E, a, g, ord);
     631      576494 :     if (e) return e;
     632             :   }
     633      276928 :   v = get_arith_ZZM(ord);
     634      276928 :   ord= gel(v,1);
     635      276928 :   fa = gel(v,2);
     636      276928 :   ex = gel(fa,2);
     637      276928 :   fa = gel(fa,1); l = lg(fa);
     638      276928 :   ginv = grp->pow(E,g,gen_m1);
     639      276928 :   v = cgetg(l, t_VEC);
     640      800737 :   for (i=1; i<l; i++)
     641             :   {
     642      524115 :     q = gel(fa,i);
     643      524115 :     e = itos(gel(ex,i));
     644      524115 :     if (DEBUGLEVEL>5)
     645           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     646      524115 :     qj = new_chunk(e+1);
     647      524115 :     gel(qj,0) = gen_1;
     648      524115 :     gel(qj,1) = q;
     649      524115 :     for (j=2; j<=e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     650      524115 :     t0 = diviiexact(ord, gel(qj,e));
     651      524115 :     a0 = grp->pow(E, a, t0);
     652      524115 :     ginv0 = grp->pow(E, ginv, t0); /* order q^e */
     653      524115 :     if (grp->equal1(ginv0))
     654             :     {
     655          14 :       gel(v,i) = mkintmod(gen_0, gen_1);
     656          14 :       continue;
     657             :     }
     658      524108 :     do { g_q = grp->pow(E,g, mulii(t0, gel(qj,--e))); /* order q */
     659      524108 :     } while (grp->equal1(g_q));
     660      524101 :     n_q = gen_0;
     661      749628 :     for (j=0;; j++)
     662             :     { /* n_q = sum_{i<j} b_i q^i */
     663      975155 :       b = grp->pow(E,a0, gel(qj,e-j));
     664             :       /* early abort: cheap and very effective */
     665      749628 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     666         296 :         avma = av; return cgetg(1, t_VEC);
     667             :       }
     668      749332 :       b = gen_plog(b, g_q, q, E, grp);
     669      749332 :       if (typ(b) != t_INT) { avma = av; return cgetg(1, t_VEC); }
     670      749322 :       n_q = addii(n_q, mulii(b, gel(qj,j)));
     671      749322 :       if (j == e) break;
     672             : 
     673      225527 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     674      225527 :       ginv0 = grp->pow(E,ginv0, q);
     675             :     }
     676      523795 :     gel(v,i) = mkintmod(n_q, gel(qj,e+1));
     677             :   }
     678      276622 :   return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
     679             : }
     680             : 
     681             : /***********************************************************************/
     682             : /**                                                                   **/
     683             : /**                    ORDER OF AN ELEMENT                            **/
     684             : /**                                                                   **/
     685             : /***********************************************************************/
     686             : /*Find the exact order of a assuming a^o==1*/
     687             : GEN
     688     3241014 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     689             : {
     690     3241014 :   pari_sp av = avma;
     691             :   long i, l;
     692             :   GEN m;
     693             : 
     694     3241014 :   m = get_arith_ZZM(o);
     695     3241014 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     696     3241014 :   o = gel(m,1);
     697     3241014 :   m = gel(m,2); l = lgcols(m);
     698    10108939 :   for (i = l-1; i; i--)
     699             :   {
     700     6867925 :     GEN t, y, p = gcoeff(m,i,1);
     701     6867925 :     long j, e = itos(gcoeff(m,i,2));
     702     6867925 :     if (l == 2) {
     703      678190 :       t = gen_1;
     704      678190 :       y = a;
     705             :     } else {
     706     6189735 :       t = diviiexact(o, powiu(p,e));
     707     6189735 :       y = grp->pow(E, a, t);
     708             :     }
     709     6867925 :     if (grp->equal1(y)) o = t;
     710             :     else {
     711     6371444 :       for (j = 1; j < e; j++)
     712             :       {
     713     2173907 :         y = grp->pow(E, y, p);
     714     2173907 :         if (grp->equal1(y)) break;
     715             :       }
     716     4720162 :       if (j < e) {
     717      522625 :         if (j > 1) p = powiu(p, j);
     718      522625 :         o = mulii(t, p);
     719             :       }
     720             :     }
     721             :   }
     722     3241014 :   return gerepilecopy(av, o);
     723             : }
     724             : 
     725             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     726             : GEN
     727     2777051 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     728             : {
     729     2777051 :   pari_sp av = avma;
     730             :   long i, l, ind;
     731             :   GEN m, F, P;
     732             : 
     733     2777051 :   m = get_arith_ZZM(o);
     734     2777051 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     735     2777051 :   o = gel(m,1);
     736     2777051 :   m = gel(m,2); l = lgcols(m);
     737     2777051 :   P = cgetg(l, t_COL); ind = 1;
     738     2777051 :   F = cgetg(l, t_COL);
     739     6673150 :   for (i = l-1; i; i--)
     740             :   {
     741     3896099 :     GEN t, y, p = gcoeff(m,i,1);
     742     3896099 :     long j, e = itos(gcoeff(m,i,2));
     743     3896099 :     if (l == 2) {
     744     1702992 :       t = gen_1;
     745     1702992 :       y = a;
     746             :     } else {
     747     2193107 :       t = diviiexact(o, powiu(p,e));
     748     2193107 :       y = grp->pow(E, a, t);
     749             :     }
     750     3896099 :     if (grp->equal1(y)) o = t;
     751             :     else {
     752     4884401 :       for (j = 1; j < e; j++)
     753             :       {
     754     1037313 :         y = grp->pow(E, y, p);
     755     1037313 :         if (grp->equal1(y)) break;
     756             :       }
     757     3860155 :       gel(P,ind) = p;
     758     3860155 :       gel(F,ind) = utoipos(j);
     759     3860155 :       if (j < e) {
     760       13067 :         if (j > 1) p = powiu(p, j);
     761       13067 :         o = mulii(t, p);
     762             :       }
     763     3860155 :       ind++;
     764             :     }
     765             :   }
     766     2777051 :   setlg(P, ind); P = vecreverse(P);
     767     2777051 :   setlg(F, ind); F = vecreverse(F);
     768     2777051 :   return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
     769             : }
     770             : 
     771             : /* E has order o[1], ..., or o[#o], draw random points until all solutions
     772             :  * but one are eliminated */
     773             : GEN
     774         945 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     775             : {
     776         945 :   pari_sp ltop = avma, btop;
     777             :   GEN lastgood, so, vo;
     778         945 :   long lo = lg(o), nbo=lo-1;
     779         945 :   if (nbo == 1) return icopy(gel(o,1));
     780         413 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     781         413 :   vo = zero_zv(lo);
     782         413 :   lastgood = gel(o, so[nbo]);
     783         413 :   btop = avma;
     784             :   for(;;)
     785           0 :   {
     786         413 :     GEN lasto = gen_0;
     787         413 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     788             :     long i;
     789         525 :     for (i = 1; i < lo; i++)
     790             :     {
     791         525 :       GEN newo = gel(o, so[i]);
     792         525 :       if (vo[i]) continue;
     793         525 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     794         525 :       lasto = newo;
     795         525 :       if (!grp->equal1(t))
     796             :       {
     797         455 :         if (--nbo == 1) { avma=ltop; return icopy(lastgood); }
     798          42 :         vo[i] = 1;
     799             :       }
     800             :       else
     801          70 :         lastgood = lasto;
     802             :     }
     803           0 :     avma = btop;
     804             :   }
     805             : }
     806             : 
     807             : /*******************************************************************/
     808             : /*                                                                 */
     809             : /*                          n-th ROOT                              */
     810             : /*                                                                 */
     811             : /*******************************************************************/
     812             : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
     813             :  * of order l.
     814             :  *
     815             :  * q = l^e*r, e>=1, (r,l)=1
     816             :  * UNCLEAN */
     817             : static GEN
     818      268319 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     819             : {
     820      268319 :   const pari_sp av1 = avma;
     821             :   GEN m, m1;
     822             :   long i;
     823      209391 :   for (;; avma = av1)
     824             :   {
     825      687101 :     m1 = m = grp->pow(E, grp->rand(E), r);
     826      477712 :     if (grp->equal1(m)) continue;
     827      864554 :     for (i=1; i<e; i++)
     828             :     {
     829      596233 :       m = grp->pow(E,m,l);
     830      596233 :       if (grp->equal1(m)) break;
     831             :     }
     832      387417 :     if (i==e) break;
     833             :   }
     834      268321 :   *zeta = m; return m1;
     835             : }
     836             : 
     837             : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
     838             : 
     839             : GEN
     840       15869 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     841             : {
     842       15869 :   pari_sp ltop = avma, av;
     843             :   long i, lpr;
     844       15869 :   GEN F, N, pr, z=NULL;
     845       15869 :   F = get_arith_ZZM(o);
     846       15869 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     847       15869 :   av = avma;
     848             : 
     849       51478 :   for (i = 1; i < lpr; i++)
     850             :   {
     851       35609 :     GEN l = gcoeff(pr,i,1);
     852       35609 :     long e = itos(gcoeff(pr,i,2));
     853       35609 :     GEN r = diviiexact(N,powis(l,e));
     854       35609 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     855       35609 :     z = i==1 ? zl: grp->mul(E,z,zl);
     856       35609 :     if (gc_needed(av,2))
     857             :     { /* n can have lots of prime factors*/
     858           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
     859           0 :       z = gerepileupto(av, z);
     860             :     }
     861             :   }
     862       15869 :   return gerepileupto(ltop, z);
     863             : }
     864             : 
     865             : /* solve x^l = a , l prime in G of order q.
     866             :  *
     867             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     868             :  * y is not an l-th power, hence generates the l-Sylow of G
     869             :  * m = y^(q/l) != 1 */
     870             : static GEN
     871      233356 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
     872             :                  const struct bb_group *grp)
     873             : {
     874      233356 :   pari_sp av = avma;
     875             :   long k;
     876             :   GEN p1, u1, u2, v, w, z, dl;
     877             : 
     878      233356 :   (void)bezout(r,l,&u1,&u2);
     879      233356 :   v = grp->pow(E,a,u2);
     880      233356 :   w = grp->pow(E,v,l);
     881      233356 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     882      645650 :   while (!grp->equal1(w))
     883             :   {
     884      181829 :     k = 0;
     885      181829 :     p1 = w;
     886             :     do
     887             :     {
     888      313246 :       z = p1; p1 = grp->pow(E,p1,l);
     889      313246 :       k++;
     890      313246 :     } while(!grp->equal1(p1));
     891      181829 :     if (k==e) { avma = av; return NULL; }
     892      178938 :     dl = gen_plog(z,m,l,E,grp);
     893      178938 :     if (typ(dl) != t_INT) { avma = av; return NULL; }
     894      178938 :     dl = negi(dl);
     895      178938 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     896      178938 :     m = grp->pow(E,m,dl);
     897      178938 :     e = k;
     898      178938 :     v = grp->mul(E,p1,v);
     899      178938 :     y = grp->pow(E,p1,l);
     900      178938 :     w = grp->mul(E,y,w);
     901      178938 :     if (gc_needed(av,1))
     902             :     {
     903           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
     904           0 :       gerepileall(av,4, &y,&v,&w,&m);
     905             :     }
     906             :   }
     907      230465 :   return gerepilecopy(av, v);
     908             : }
     909             : /* Return one solution of x^n = a in a cyclic group of order q
     910             :  *
     911             :  * 1) If there is no solution, return NULL.
     912             :  *
     913             :  * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
     914             :  * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
     915             :  * the set of solutions is { x*zetan^k; k=0..m-1 }
     916             :  */
     917             : GEN
     918      241818 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     919             : {
     920      241818 :   pari_sp ltop = avma;
     921             :   GEN m, u1, u2, z;
     922             :   int is_1;
     923             : 
     924      241818 :   if (is_pm1(n))
     925             :   {
     926           0 :     if (zetan) *zetan = grp->pow(E,a,gen_0);
     927           0 :     return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
     928             :   }
     929      241818 :   is_1 = grp->equal1(a);
     930      241818 :   if (is_1 && !zetan) return gcopy(a);
     931             : 
     932      234440 :   m = bezout(n,q,&u1,&u2);
     933      234440 :   z = grp->pow(E,a,gen_0);
     934      234439 :   if (!is_pm1(m))
     935             :   {
     936      232578 :     GEN F = Z_factor(m);
     937             :     long i, j, e;
     938             :     GEN r, zeta, y, l;
     939      232577 :     pari_sp av1 = avma;
     940      462398 :     for (i = nbrows(F); i; i--)
     941             :     {
     942      232710 :       l = gcoeff(F,i,1);
     943      232710 :       j = itos(gcoeff(F,i,2));
     944      232710 :       e = Z_pvalrem(q,l,&r);
     945      232710 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     946      232712 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     947      232712 :       if (!is_1) {
     948             :         do
     949             :         {
     950      233356 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     951      233356 :           if (!a) { avma = ltop; return NULL;}
     952      230465 :         } while (--j);
     953             :       }
     954      229821 :       if (gc_needed(ltop,1))
     955             :       { /* n can have lots of prime factors*/
     956           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
     957           0 :         gerepileall(av1, zetan? 2: 1, &a, &z);
     958             :       }
     959             :     }
     960             :   }
     961      231549 :   if (!equalii(m, n))
     962        1925 :     a = grp->pow(E,a,modii(u1,q));
     963      231550 :   if (zetan)
     964             :   {
     965         364 :     *zetan = z;
     966         364 :     gerepileall(ltop,2,&a,zetan);
     967             :   }
     968             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     969      231186 :     a = gerepileupto(ltop, a);
     970      231550 :   return a;
     971             : }
     972             : 
     973             : /*******************************************************************/
     974             : /*                                                                 */
     975             : /*               structure of groups with pairing                  */
     976             : /*                                                                 */
     977             : /*******************************************************************/
     978             : 
     979             : static GEN
     980       40866 : ellgroup_d2(GEN N, GEN d)
     981             : {
     982       40866 :   GEN r = gcdii(N, d);
     983       40866 :   GEN F1 = gel(Z_factor(r), 1);
     984       40866 :   long i, j, l1 = lg(F1);
     985       40866 :   GEN F = cgetg(3, t_MAT);
     986       40866 :   gel(F,1) = cgetg(l1, t_COL);
     987       40866 :   gel(F,2) = cgetg(l1, t_COL);
     988       71722 :   for (i = 1, j = 1; i < l1; ++i)
     989             :   {
     990       30856 :     long v = Z_pval(N, gel(F1, i));
     991       30856 :     if (v<=1) continue;
     992       15876 :     gcoeff(F, j  , 1) = gel(F1, i);
     993       15876 :     gcoeff(F, j++, 2) = stoi(v);
     994             :   }
     995       40866 :   setlg(F[1],j); setlg(F[2],j);
     996       40866 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     997             : }
     998             : 
     999             : GEN
    1000       41027 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
    1001             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1002             : {
    1003       41027 :   pari_sp av = avma;
    1004             :   GEN N0, N1, F;
    1005       41027 :   if (pt_m) *pt_m = gen_1;
    1006       41027 :   if (is_pm1(N)) return cgetg(1,t_VEC);
    1007       40866 :   F = ellgroup_d2(N, d);
    1008       40866 :   if (!F) {avma = av; return mkveccopy(N);}
    1009       15148 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
    1010             :   while(1)
    1011       11381 :   {
    1012       26529 :     pari_sp av2 = avma;
    1013             :     GEN P, Q, d, s, t, m;
    1014             : 
    1015       26529 :     P = grp->pow(E,grp->rand(E), N1);
    1016       26529 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {avma = av; return mkveccopy(N);}
    1017             : 
    1018       20657 :     Q = grp->pow(E,grp->rand(E), N1);
    1019       20657 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {avma = av; return mkveccopy(N);}
    1020             : 
    1021       17947 :     m = lcmii(s, t);
    1022       17947 :     d = pairorder(E, P, Q, m, F);
    1023             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
    1024       17947 :     if (is_pm1(d) && equalii(m, N0)) {avma = av; return mkveccopy(N);}
    1025       17919 :     if (equalii(mulii(m, d), N0))
    1026             :     {
    1027        6538 :       GEN g = mkvec2(mulii(N1,m), d);
    1028        6538 :       if (pt_m) *pt_m = m;
    1029        6538 :       gerepileall(av,pt_m?2:1,&g,pt_m);
    1030        6538 :       return g;
    1031             :     }
    1032       11381 :     avma = av2;
    1033             :   }
    1034             : }
    1035             : 
    1036             : GEN
    1037        2723 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
    1038             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1039             : {
    1040        2723 :   pari_sp ltop = avma, av;
    1041             :   GEN F, d1, dm;
    1042             :   GEN P, Q, d, s;
    1043        2723 :   F = get_arith_ZZM(D1);
    1044        2723 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1045        2723 :   av = avma;
    1046             :   do
    1047             :   {
    1048        7113 :     avma = av;
    1049        7113 :     P = grp->rand(E);
    1050        7113 :     s = gen_order(P, F, E, grp);
    1051        7113 :   } while (!equalii(s, d1));
    1052        2723 :   av = avma;
    1053             :   do
    1054             :   {
    1055        5282 :     avma = av;
    1056        5282 :     Q = grp->rand(E);
    1057        5282 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1058        5282 :   } while (!equalii(d, d2));
    1059        2723 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1060             : }

Generated by: LCOV version 1.13