Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - bb_group.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23008-54b2e2f01) Lines: 536 579 92.6 %
Date: 2018-09-20 05:37:46 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     3899560 : int_block(GEN n, long i, long l)
      32             : {
      33     3899560 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      34     3899695 :   GEN nw = int_W(n, q);
      35     3899695 :   ulong w = (ulong) *nw, w2;
      36     3899695 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      37      291907 :   w &= (1UL<<r)-1; lr = l-r;
      38      291907 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      39      291907 :   return (w<<lr)|w2;
      40             : }
      41             : 
      42             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      43             : static GEN
      44     6926028 : 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     6926028 :   long i, l = expu(n), u = (1UL<<(e-1));
      49             :   long w, v;
      50     6925586 :   GEN tab = cgetg(1+u, t_VEC);
      51     6928068 :   GEN x2 = sqr(E, x), z = NULL, tw;
      52     6455084 :   gel(tab, 1) = x;
      53     6455084 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      54     6447792 :   av = avma;
      55    61093895 :   while (l>=0)
      56             :   {
      57    47723185 :     if (e > l+1) e = l+1;
      58    47723185 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      59    47890469 :     tw = gel(tab, 1+(w>>(v+1)));
      60    47890469 :     if (z)
      61             :     {
      62    40966575 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
      63    40824460 :       z = mul(E, z, tw);
      64     6923894 :     } else z = tw;
      65    47717142 :     for (i=1; i<=v; i++) z = sqr(E, z);
      66   135896136 :     while (l>=0)
      67             :     {
      68    80858675 :       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    81380172 :       if (n&(1UL<<l)) break;
      74    40511069 :       z = sqr(E, z); l--;
      75             :     }
      76             :   }
      77     6922918 :   return z;
      78             : }
      79             : 
      80             : 
      81             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      82             : static GEN
      83       82247 : 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       82247 :   long i, l = expi(n), u = (1UL<<(e-1));
      88             :   long w, v;
      89       82243 :   GEN tab = cgetg(1+u, t_VEC);
      90       82242 :   GEN x2 = sqr(E, x), z = NULL, tw;
      91       78813 :   gel(tab, 1) = x;
      92       78813 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      93       78810 :   av = avma;
      94     3347812 :   while (l>=0)
      95             :   {
      96     3186806 :     if (e > l+1) e = l+1;
      97     3186806 :     w = int_block(n,l,e); v = vals(w); l-=e;
      98     3192093 :     tw = gel(tab, 1+(w>>(v+1)));
      99     3192093 :     if (z)
     100             :     {
     101     3109843 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
     102     3104211 :       z = mul(E, z, tw);
     103       82250 :     } else z = tw;
     104     3186924 :     for (i=1; i<=v; i++) z = sqr(E, z);
     105    13418252 :     while (l>=0)
     106             :     {
     107    10145898 :       if (gc_needed(av,1))
     108             :       {
     109         426 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110         426 :         z = gerepilecopy(av, z);
     111             :       }
     112    10145898 :       if (int_bit(n,l)) break;
     113     7040045 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116       82196 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    54299510 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                               GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    54299510 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    54299510 :   if (n == 1) return x;
     129    54299510 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    54299510 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   149686211 :   for (; j; n<<=1,j--)
     134             :   {
     135    95390739 :     y = sqr(E,y);
     136    95386323 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137    95386701 :     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    54295472 :   return y;
     144             : }
     145             : 
     146             : GEN
     147    61316588 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150             :   long l;
     151    61316588 :   if (n == 1) return x;
     152    61222026 :   l = expu(n);
     153    61224353 :   if (l<=8)
     154    54296411 :     return leftright_binary_powu(x, n, E, sqr, mul);
     155             :   else
     156     6927942 :     return sliding_window_powu(x, n, l<=24? 2: 3, E, sqr, mul);
     157             : }
     158             : 
     159             : GEN
     160     1324020 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     161             :                                   GEN (*mul)(void*,GEN,GEN))
     162             : {
     163     1324020 :   pari_sp av = avma;
     164     1324020 :   if (n == 1) return gcopy(x);
     165     1197417 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     166             : }
     167             : 
     168             : GEN
     169    22798868 : 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    22798868 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     174       82249 :   l = expi(n);
     175       82248 :   if      (l<=64)  e = 3;
     176       60334 :   else if (l<=160) e = 4;
     177       26559 :   else if (l<=384) e = 5;
     178       18516 :   else if (l<=896) e = 6;
     179        9839 :   else             e = 7;
     180       82248 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     181             : }
     182             : 
     183             : GEN
     184     1065306 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     185             :                                GEN (*mul)(void*,GEN,GEN))
     186             : {
     187     1065306 :   pari_sp av = avma;
     188     1065306 :   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      299708 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     194             :                                          GEN (*msqr)(void*,GEN))
     195             : {
     196      299708 :   pari_sp av = avma;
     197             :   GEN y;
     198             :   int j;
     199             : 
     200      299708 :   if (n == 1) return x;
     201      299708 :   y = x; j = 1+bfffo(n);
     202             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     203      299708 :   n<<=j; j = BITS_IN_LONG-j;
     204             :   /* first bit is now implicit */
     205     2828612 :   for (; j; n<<=1,j--)
     206             :   {
     207     2528904 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     208     1934951 :     else y = sqr(E,y);
     209     2528904 :     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      299708 :   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      198900 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     229             :                                       GEN (*msqr)(void*,GEN))
     230             : {
     231      198900 :   long ln = lgefint(N);
     232      198900 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     233             :   else
     234             :   {
     235       87362 :     GEN nd = int_MSW(N), y = x;
     236       87362 :     ulong n = *nd;
     237             :     long i;
     238             :     int j;
     239       87362 :     pari_sp av = avma;
     240             : 
     241       87362 :     if (n == 1)
     242        6559 :       j = 0;
     243             :     else
     244             :     {
     245       80803 :       j = 1+bfffo(n); /* < BIL */
     246             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     247       80803 :       n <<= j; j = BITS_IN_LONG - j;
     248             :     }
     249             :     /* first bit is now implicit */
     250       87362 :     for (i=ln-2;;)
     251             :     {
     252    26149356 :       for (; j; n<<=1,j--)
     253             :       {
     254    25223289 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     255    15491779 :         else y = sqr(E,y);
     256    25223076 :         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      506608 :       if (--i == 0) return y;
     263      419459 :       nd = int_precW(nd);
     264      419459 :       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       64737 : gen_pow_table(GEN R, GEN n, void *E, GEN (*one)(void*), GEN (*mul)(void*,GEN,GEN))
     297             : {
     298       64737 :   long e = expu(lg(R)-1) + 1;
     299       64737 :   long l = expi(n);
     300             :   long i, w;
     301       64737 :   GEN z = one(E), tw;
     302     1531622 :   for(i=0; i<=l; )
     303             :   {
     304     1402148 :     if (int_bit(n, i)==0) { i++; continue; }
     305      712747 :     if (i+e-1>l) e = l+1-i;
     306      712747 :     w = int_block(n,i+e-1,e);
     307      712747 :     tw = gmael(R, 1+(w>>1), i+1);
     308      712747 :     z = mul(E, z, tw);
     309      712747 :     i += e;
     310             :   }
     311       64737 :   return z;
     312             : }
     313             : 
     314             : GEN
     315     6906253 : 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     6906253 :   GEN V = cgetg(l+2,t_VEC);
     320     6906257 :   gel(V,1) = one(E); if (l==0) return V;
     321     6893263 :   gel(V,2) = gcopy(x); if (l==1) return V;
     322     4370094 :   gel(V,3) = sqr(E,x);
     323     4370097 :   if (use_sqr)
     324    12198290 :     for(i = 4; i < l+2; i++)
     325    21320923 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     326    12597942 :                       : mul(E,gel(V, i-1),x);
     327             :   else
     328     2520151 :     for(i = 4; i < l+2; i++)
     329     1625375 :       gel(V,i) = mul(E,gel(V,i-1),x);
     330     4370087 :   return V;
     331             : }
     332             : 
     333             : GEN
     334    50186192 : producttree_scheme(long n)
     335             : {
     336             :   GEN v, w;
     337             :   long i, j, k, u, l;
     338    50186192 :   if (n<=2) return mkvecsmall(n);
     339    42940703 :   u = expu(n-1);
     340    42940704 :   v = cgetg(n+1,t_VECSMALL);
     341    42940639 :   w = cgetg(n+1,t_VECSMALL);
     342    42940927 :   v[1] = n; l = 1;
     343   143575686 :   for (i=1; i<=u; i++)
     344             :   {
     345   359756093 :     for(j=1, k=1; j<=l; j++, k+=2)
     346             :     {
     347   259121334 :       long vj = v[j], v2 = vj>>1;
     348   259121334 :       w[k]    = vj-v2;
     349   259121334 :       w[k+1]  = v2;
     350             :     }
     351   100634759 :     swap(v,w); l<<=1;
     352             :   }
     353    42940927 :   fixlg(v, l+1);
     354    42940924 :   avma = (pari_sp) v;
     355    42940924 :   return v;
     356             : }
     357             : 
     358             : GEN
     359    51793441 : gen_product(GEN x, void *data, GEN (*mul)(void *,GEN,GEN))
     360             : {
     361             :   pari_sp ltop;
     362    51793441 :   long i,k,lx = lg(x),lv;
     363             :   pari_timer ti;
     364             :   GEN v;
     365    51793441 :   if (DEBUGLEVEL>7) timer_start(&ti);
     366             : 
     367    51793481 :   if (lx == 1) return gen_1;
     368    51790444 :   if (lx == 2) return gcopy(gel(x,1));
     369    50133056 :   x = leafcopy(x); k = lx;
     370    50133337 :   v = producttree_scheme(lx-1); lv = lg(v);
     371    50134050 :   ltop = avma;
     372   358850311 :   for (k=1, i=1; k<lv; i+=v[k++])
     373   308717277 :     gel(x,k) = v[k]==1 ? gel(x,i): mul(data,gel(x,i),gel(x,i+1));
     374   200754312 :   while (k > 2)
     375             :   {
     376   100488026 :     if (DEBUGLEVEL>7)
     377           0 :       timer_printf(&ti,"gen_product: remaining objects %ld",k-1);
     378   100487644 :     lx = k; k = 1;
     379   359061902 :     for (i=1; i<lx-1; i+=2)
     380   258573485 :       gel(x,k++) = mul(data,gel(x,i),gel(x,i+1));
     381   100488417 :     if (i < lx) gel(x,k++) = gel(x,i);
     382   100488417 :     if (gc_needed(ltop,1))
     383           6 :       gerepilecoeffs(ltop,x+1,k-1);
     384             :   }
     385    50133252 :   return gel(x,1);
     386             : }
     387             : 
     388             : /***********************************************************************/
     389             : /**                                                                   **/
     390             : /**                    DISCRETE LOGARITHM                             **/
     391             : /**                                                                   **/
     392             : /***********************************************************************/
     393             : 
     394             : static GEN
     395    57398664 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     396             : {
     397    57398664 :   GEN a = gel(A,1);
     398    57398664 :   switch((h|grp->hash(a))%3UL)
     399             :   {
     400             :     case 0:
     401    19139568 :       return mkvec3(grp->pow(E,a,gen_2),Fp_mulu(gel(A,2),2,q),
     402    19139568 :                                         Fp_mulu(gel(A,3),2,q));
     403             :     case 1:
     404    19125519 :       return mkvec3(grp->mul(E,a,x),addis(gel(A,2),1),gel(A,3));
     405             :     case 2:
     406    19133577 :       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    19132888 :       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    19132888 :       A = iter_rho(x, g, q, A, h, E, grp);
     432    19132888 :       B = iter_rho(x, g, q, B, h, E, grp);
     433    19132888 :       B = iter_rho(x, g, q, B, h, E, grp);
     434    19132888 :       if (gc_needed(av,2))
     435             :       {
     436        1673 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     437        1673 :         gerepileall(av, 2, &A, &B);
     438             :       }
     439    19132888 :       i++;
     440    19132888 :     } 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      209159 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     509             : {
     510      209159 :   pari_sp av=avma, av1;
     511             :   long lbaby, i, k;
     512             :   GEN p1, table, giant, perm, ginv;
     513      209159 :   p1 = sqrti(q);
     514      209159 :   if (abscmpiu(p1,LGBITS) >= 0)
     515           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     516      209159 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     517      209159 :   ginv = grp->pow(E,g,gen_m1);
     518      209159 :   av1 = avma;
     519     1242494 :   for (p1=x, i=1;;i++)
     520             :   {
     521     2275829 :     if (grp->equal1(p1)) { set_avma(av); return stoi(i-1); }
     522     1217394 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     523     1033336 :     p1 = grp->mul(E,p1,ginv);
     524     1033335 :     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      184058 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     531      184059 :   perm = vecsmall_indexsort(table);
     532      184059 :   table = vecsmallpermute(table,perm);
     533      184059 :   av1 = avma;
     534      417866 :   for (k=1; k<= lbaby; k++)
     535             :   {
     536      417857 :     long h = grp->hash(p1), i = zv_search(table, h);
     537      417857 :     if (i)
     538             :     {
     539      184051 :       while (table[i] == h && i) i--;
     540      184052 :       for (i++; i <= lbaby && table[i] == h; i++)
     541             :       {
     542      184051 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     543      184051 :         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      233807 :     p1 = grp->mul(E,p1,giant);
     549      233807 :     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           9 :   set_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      930332 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     561             : {
     562      930332 :   if (grp->easylog)
     563             :   {
     564      906375 :     GEN e = grp->easylog(E, x, g, p);
     565      906375 :     if (e) return e;
     566             :   }
     567      302240 :   if (grp->equal1(x)) return gen_0;
     568      302198 :   if (grp->equal(x,g)) return gen_1;
     569      209208 :   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     7263691 : get_arith_ZZM(GEN o)
     575             : {
     576     7263691 :   if (!o) return NULL;
     577     7263691 :   switch(typ(o))
     578             :   {
     579             :     case t_INT:
     580     4825922 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     581           7 :       break;
     582             :     case t_MAT:
     583     1379867 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     584          14 :       break;
     585             :     case t_VEC:
     586     1057895 :       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      234292 : get_arith_Z(GEN o)
     594             : {
     595      234292 :   if (!o) return NULL;
     596      234292 :   switch(typ(o))
     597             :   {
     598             :     case t_INT:
     599      180786 :       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       53485 :       if (lg(o) != 3) break;
     607       53485 :       o = gel(o,1);
     608       53485 :       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             : /* Generic Pohlig-Hellman discrete logarithm: smallest integer n >= 0 such that
     616             :  * g^n=a. Assume ord(g) | ord; grp->easylog() is an optional trapdoor
     617             :  * function that catches easy logarithms */
     618             : GEN
     619      712722 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     620             : {
     621      712722 :   pari_sp av = avma;
     622             :   GEN v, ginv, fa, ex;
     623             :   long i, j, l;
     624             : 
     625      712722 :   if (grp->equal(g, a)) /* frequent special case */
     626      136020 :     return grp->equal1(g)? gen_0: gen_1;
     627      576702 :   if (grp->easylog)
     628             :   {
     629      576597 :     GEN e = grp->easylog(E, a, g, ord);
     630      576569 :     if (e) return e;
     631             :   }
     632      276932 :   v = get_arith_ZZM(ord);
     633      276932 :   ord= gel(v,1);
     634      276932 :   fa = gel(v,2);
     635      276932 :   ex = gel(fa,2);
     636      276932 :   fa = gel(fa,1); l = lg(fa);
     637      276932 :   ginv = grp->pow(E,g,gen_m1);
     638      276932 :   v = cgetg(l, t_VEC);
     639      800746 :   for (i = 1; i < l; i++)
     640             :   {
     641      524119 :     GEN q = gel(fa,i), qj, gq, nq, ginv0, a0, t0;
     642      524119 :     long e = itos(gel(ex,i));
     643      524119 :     if (DEBUGLEVEL>5)
     644           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     645      524119 :     qj = new_chunk(e+1);
     646      524119 :     gel(qj,0) = gen_1;
     647      524119 :     gel(qj,1) = q;
     648      524119 :     for (j = 2; j <= e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     649      524119 :     t0 = diviiexact(ord, gel(qj,e));
     650      524119 :     a0 = grp->pow(E, a, t0);
     651      524119 :     ginv0 = grp->pow(E, ginv, t0); /* ord(ginv0) | q^e */
     652      524119 :     if (grp->equal1(ginv0)) { gel(v,i) = mkintmod(gen_0, gen_1); continue; }
     653      524112 :     do gq = grp->pow(E,g, mulii(t0, gel(qj,--e))); while (grp->equal1(gq));
     654             :     /* ord(gq) = q */
     655      524105 :     nq = gen_0;
     656      749632 :     for (j = 0;; j++)
     657      225527 :     { /* nq = sum_{i<j} b_i q^i */
     658      749632 :       GEN b = grp->pow(E,a0, gel(qj,e-j));
     659             :       /* cheap early abort: wrong local order */
     660      749632 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     661         296 :         set_avma(av); return cgetg(1, t_VEC);
     662             :       }
     663      749336 :       b = gen_plog(b, gq, q, E, grp);
     664      749336 :       if (typ(b) != t_INT) { set_avma(av); return cgetg(1, t_VEC); }
     665      749327 :       nq = addii(nq, mulii(b, gel(qj,j)));
     666      749327 :       if (j == e) break;
     667             : 
     668      225527 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     669      225527 :       ginv0 = grp->pow(E,ginv0, q);
     670             :     }
     671      523800 :     gel(v,i) = mkintmod(nq, gel(qj,e+1));
     672             :   }
     673      276627 :   return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
     674             : }
     675             : 
     676             : /***********************************************************************/
     677             : /**                                                                   **/
     678             : /**                    ORDER OF AN ELEMENT                            **/
     679             : /**                                                                   **/
     680             : /***********************************************************************/
     681             : /*Find the exact order of a assuming a^o==1*/
     682             : GEN
     683     3241567 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     684             : {
     685     3241567 :   pari_sp av = avma;
     686             :   long i, l;
     687             :   GEN m;
     688             : 
     689     3241567 :   m = get_arith_ZZM(o);
     690     3241567 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     691     3241567 :   o = gel(m,1);
     692     3241567 :   m = gel(m,2); l = lgcols(m);
     693    10109198 :   for (i = l-1; i; i--)
     694             :   {
     695     6867631 :     GEN t, y, p = gcoeff(m,i,1);
     696     6867631 :     long j, e = itos(gcoeff(m,i,2));
     697     6867631 :     if (l == 2) {
     698      679359 :       t = gen_1;
     699      679359 :       y = a;
     700             :     } else {
     701     6188272 :       t = diviiexact(o, powiu(p,e));
     702     6188272 :       y = grp->pow(E, a, t);
     703             :     }
     704     6867631 :     if (grp->equal1(y)) o = t;
     705             :     else {
     706     6370695 :       for (j = 1; j < e; j++)
     707             :       {
     708     2173235 :         y = grp->pow(E, y, p);
     709     2173235 :         if (grp->equal1(y)) break;
     710             :       }
     711     4720001 :       if (j < e) {
     712      522541 :         if (j > 1) p = powiu(p, j);
     713      522541 :         o = mulii(t, p);
     714             :       }
     715             :     }
     716             :   }
     717     3241567 :   return gerepilecopy(av, o);
     718             : }
     719             : 
     720             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     721             : GEN
     722     2777051 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     723             : {
     724     2777051 :   pari_sp av = avma;
     725             :   long i, l, ind;
     726             :   GEN m, F, P;
     727             : 
     728     2777051 :   m = get_arith_ZZM(o);
     729     2777051 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     730     2777051 :   o = gel(m,1);
     731     2777051 :   m = gel(m,2); l = lgcols(m);
     732     2777051 :   P = cgetg(l, t_COL); ind = 1;
     733     2777051 :   F = cgetg(l, t_COL);
     734     6673150 :   for (i = l-1; i; i--)
     735             :   {
     736     3896099 :     GEN t, y, p = gcoeff(m,i,1);
     737     3896099 :     long j, e = itos(gcoeff(m,i,2));
     738     3896099 :     if (l == 2) {
     739     1702992 :       t = gen_1;
     740     1702992 :       y = a;
     741             :     } else {
     742     2193107 :       t = diviiexact(o, powiu(p,e));
     743     2193107 :       y = grp->pow(E, a, t);
     744             :     }
     745     3896099 :     if (grp->equal1(y)) o = t;
     746             :     else {
     747     4884401 :       for (j = 1; j < e; j++)
     748             :       {
     749     1037313 :         y = grp->pow(E, y, p);
     750     1037313 :         if (grp->equal1(y)) break;
     751             :       }
     752     3860155 :       gel(P,ind) = p;
     753     3860155 :       gel(F,ind) = utoipos(j);
     754     3860155 :       if (j < e) {
     755       13067 :         if (j > 1) p = powiu(p, j);
     756       13067 :         o = mulii(t, p);
     757             :       }
     758     3860155 :       ind++;
     759             :     }
     760             :   }
     761     2777051 :   setlg(P, ind); P = vecreverse(P);
     762     2777051 :   setlg(F, ind); F = vecreverse(F);
     763     2777051 :   return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
     764             : }
     765             : 
     766             : /* E has order o[1], ..., or o[#o], draw random points until all solutions
     767             :  * but one are eliminated */
     768             : GEN
     769         945 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     770             : {
     771         945 :   pari_sp ltop = avma, btop;
     772             :   GEN lastgood, so, vo;
     773         945 :   long lo = lg(o), nbo=lo-1;
     774         945 :   if (nbo == 1) return icopy(gel(o,1));
     775         413 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     776         413 :   vo = zero_zv(lo);
     777         413 :   lastgood = gel(o, so[nbo]);
     778         413 :   btop = avma;
     779             :   for(;;)
     780           0 :   {
     781         413 :     GEN lasto = gen_0;
     782         413 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     783             :     long i;
     784         525 :     for (i = 1; i < lo; i++)
     785             :     {
     786         525 :       GEN newo = gel(o, so[i]);
     787         525 :       if (vo[i]) continue;
     788         525 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     789         525 :       lasto = newo;
     790         525 :       if (!grp->equal1(t))
     791             :       {
     792         455 :         if (--nbo == 1) { set_avma(ltop); return icopy(lastgood); }
     793          42 :         vo[i] = 1;
     794             :       }
     795             :       else
     796          70 :         lastgood = lasto;
     797             :     }
     798           0 :     set_avma(btop);
     799             :   }
     800             : }
     801             : 
     802             : /*******************************************************************/
     803             : /*                                                                 */
     804             : /*                          n-th ROOT                              */
     805             : /*                                                                 */
     806             : /*******************************************************************/
     807             : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
     808             :  * of order l.
     809             :  *
     810             :  * q = l^e*r, e>=1, (r,l)=1
     811             :  * UNCLEAN */
     812             : static GEN
     813      268354 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     814             : {
     815      268354 :   const pari_sp av1 = avma;
     816             :   GEN m, m1;
     817             :   long i;
     818      211250 :   for (;; set_avma(av1))
     819             :   {
     820      690856 :     m1 = m = grp->pow(E, grp->rand(E), r);
     821      479607 :     if (grp->equal1(m)) continue;
     822      872673 :     for (i=1; i<e; i++)
     823             :     {
     824      604319 :       m = grp->pow(E,m,l);
     825      604317 :       if (grp->equal1(m)) break;
     826             :     }
     827      389602 :     if (i==e) break;
     828             :   }
     829      268355 :   *zeta = m; return m1;
     830             : }
     831             : 
     832             : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
     833             : 
     834             : GEN
     835       15869 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     836             : {
     837       15869 :   pari_sp ltop = avma, av;
     838             :   long i, lpr;
     839       15869 :   GEN F, N, pr, z=NULL;
     840       15869 :   F = get_arith_ZZM(o);
     841       15869 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     842       15869 :   av = avma;
     843             : 
     844       51478 :   for (i = 1; i < lpr; i++)
     845             :   {
     846       35609 :     GEN l = gcoeff(pr,i,1);
     847       35609 :     long e = itos(gcoeff(pr,i,2));
     848       35609 :     GEN r = diviiexact(N,powis(l,e));
     849       35609 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     850       35609 :     z = i==1 ? zl: grp->mul(E,z,zl);
     851       35609 :     if (gc_needed(av,2))
     852             :     { /* n can have lots of prime factors*/
     853           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
     854           0 :       z = gerepileupto(av, z);
     855             :     }
     856             :   }
     857       15869 :   return gerepileupto(ltop, z);
     858             : }
     859             : 
     860             : /* solve x^l = a , l prime in G of order q.
     861             :  *
     862             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     863             :  * y is not an l-th power, hence generates the l-Sylow of G
     864             :  * m = y^(q/l) != 1 */
     865             : static GEN
     866      233390 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
     867             :                  const struct bb_group *grp)
     868             : {
     869      233390 :   pari_sp av = avma;
     870             :   long k;
     871             :   GEN p1, u1, u2, v, w, z, dl;
     872             : 
     873      233390 :   (void)bezout(r,l,&u1,&u2);
     874      233390 :   v = grp->pow(E,a,u2);
     875      233391 :   w = grp->pow(E,v,l);
     876      233391 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     877      647776 :   while (!grp->equal1(w))
     878             :   {
     879      183866 :     k = 0;
     880      183866 :     p1 = w;
     881             :     do
     882             :     {
     883      317670 :       z = p1; p1 = grp->pow(E,p1,l);
     884      317670 :       k++;
     885      317670 :     } while(!grp->equal1(p1));
     886      183866 :     if (k==e) return gc_NULL(av);
     887      180996 :     dl = gen_plog(z,m,l,E,grp);
     888      180996 :     if (typ(dl) != t_INT) return gc_NULL(av);
     889      180996 :     dl = negi(dl);
     890      180996 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     891      180996 :     m = grp->pow(E,m,dl);
     892      180995 :     e = k;
     893      180995 :     v = grp->mul(E,p1,v);
     894      180995 :     y = grp->pow(E,p1,l);
     895      180996 :     w = grp->mul(E,y,w);
     896      180996 :     if (gc_needed(av,1))
     897             :     {
     898           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
     899           0 :       gerepileall(av,4, &y,&v,&w,&m);
     900             :     }
     901             :   }
     902      230521 :   return gerepilecopy(av, v);
     903             : }
     904             : /* Return one solution of x^n = a in a cyclic group of order q
     905             :  *
     906             :  * 1) If there is no solution, return NULL.
     907             :  *
     908             :  * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
     909             :  * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
     910             :  * the set of solutions is { x*zetan^k; k=0..m-1 }
     911             :  */
     912             : GEN
     913      241840 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     914             : {
     915      241840 :   pari_sp ltop = avma;
     916             :   GEN m, u1, u2, z;
     917             :   int is_1;
     918             : 
     919      241840 :   if (is_pm1(n))
     920             :   {
     921           0 :     if (zetan) *zetan = grp->pow(E,a,gen_0);
     922           0 :     return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
     923             :   }
     924      241839 :   is_1 = grp->equal1(a);
     925      241839 :   if (is_1 && !zetan) return gcopy(a);
     926             : 
     927      234475 :   m = bezout(n,q,&u1,&u2);
     928      234474 :   z = grp->pow(E,a,gen_0);
     929      234474 :   if (!is_pm1(m))
     930             :   {
     931      232612 :     GEN F = Z_factor(m);
     932             :     long i, j, e;
     933             :     GEN r, zeta, y, l;
     934      232612 :     pari_sp av1 = avma;
     935      462489 :     for (i = nbrows(F); i; i--)
     936             :     {
     937      232745 :       l = gcoeff(F,i,1);
     938      232745 :       j = itos(gcoeff(F,i,2));
     939      232746 :       e = Z_pvalrem(q,l,&r);
     940      232745 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     941      232746 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     942      232746 :       if (!is_1) {
     943             :         do
     944             :         {
     945      233390 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     946      233391 :           if (!a) return gc_NULL(ltop);
     947      230521 :         } while (--j);
     948             :       }
     949      229877 :       if (gc_needed(ltop,1))
     950             :       { /* n can have lots of prime factors*/
     951           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
     952           0 :         gerepileall(av1, zetan? 2: 1, &a, &z);
     953             :       }
     954             :     }
     955             :   }
     956      231606 :   if (!equalii(m, n))
     957        1925 :     a = grp->pow(E,a,modii(u1,q));
     958      231606 :   if (zetan)
     959             :   {
     960         364 :     *zetan = z;
     961         364 :     gerepileall(ltop,2,&a,zetan);
     962             :   }
     963             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     964      231242 :     a = gerepileupto(ltop, a);
     965      231606 :   return a;
     966             : }
     967             : 
     968             : /*******************************************************************/
     969             : /*                                                                 */
     970             : /*               structure of groups with pairing                  */
     971             : /*                                                                 */
     972             : /*******************************************************************/
     973             : 
     974             : static GEN
     975       40866 : ellgroup_d2(GEN N, GEN d)
     976             : {
     977       40866 :   GEN r = gcdii(N, d);
     978       40866 :   GEN F1 = gel(Z_factor(r), 1);
     979       40866 :   long i, j, l1 = lg(F1);
     980       40866 :   GEN F = cgetg(3, t_MAT);
     981       40866 :   gel(F,1) = cgetg(l1, t_COL);
     982       40866 :   gel(F,2) = cgetg(l1, t_COL);
     983       71722 :   for (i = 1, j = 1; i < l1; ++i)
     984             :   {
     985       30856 :     long v = Z_pval(N, gel(F1, i));
     986       30856 :     if (v<=1) continue;
     987       15876 :     gcoeff(F, j  , 1) = gel(F1, i);
     988       15876 :     gcoeff(F, j++, 2) = stoi(v);
     989             :   }
     990       40866 :   setlg(F[1],j); setlg(F[2],j);
     991       40866 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     992             : }
     993             : 
     994             : GEN
     995       41027 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
     996             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
     997             : {
     998       41027 :   pari_sp av = avma;
     999             :   GEN N0, N1, F;
    1000       41027 :   if (pt_m) *pt_m = gen_1;
    1001       41027 :   if (is_pm1(N)) return cgetg(1,t_VEC);
    1002       40866 :   F = ellgroup_d2(N, d);
    1003       40866 :   if (!F) {set_avma(av); return mkveccopy(N);}
    1004       15148 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
    1005             :   while(1)
    1006       11745 :   {
    1007       26893 :     pari_sp av2 = avma;
    1008             :     GEN P, Q, d, s, t, m;
    1009             : 
    1010       26893 :     P = grp->pow(E,grp->rand(E), N1);
    1011       26893 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {set_avma(av); return mkveccopy(N);}
    1012             : 
    1013       21056 :     Q = grp->pow(E,grp->rand(E), N1);
    1014       21056 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {set_avma(av); return mkveccopy(N);}
    1015             : 
    1016       18311 :     m = lcmii(s, t);
    1017       18311 :     d = pairorder(E, P, Q, m, F);
    1018             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
    1019       18311 :     if (is_pm1(d) && equalii(m, N0)) {set_avma(av); return mkveccopy(N);}
    1020       18283 :     if (equalii(mulii(m, d), N0))
    1021             :     {
    1022        6538 :       GEN g = mkvec2(mulii(N1,m), d);
    1023        6538 :       if (pt_m) *pt_m = m;
    1024        6538 :       gerepileall(av,pt_m?2:1,&g,pt_m);
    1025        6538 :       return g;
    1026             :     }
    1027       11745 :     set_avma(av2);
    1028             :   }
    1029             : }
    1030             : 
    1031             : GEN
    1032        2723 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
    1033             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1034             : {
    1035        2723 :   pari_sp ltop = avma, av;
    1036             :   GEN F, d1, dm;
    1037             :   GEN P, Q, d, s;
    1038        2723 :   F = get_arith_ZZM(D1);
    1039        2723 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1040        2723 :   av = avma;
    1041             :   do
    1042             :   {
    1043        6735 :     set_avma(av);
    1044        6735 :     P = grp->rand(E);
    1045        6735 :     s = gen_order(P, F, E, grp);
    1046        6735 :   } while (!equalii(s, d1));
    1047        2723 :   av = avma;
    1048             :   do
    1049             :   {
    1050        5086 :     set_avma(av);
    1051        5086 :     Q = grp->rand(E);
    1052        5086 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1053        5086 :   } while (!equalii(d, d2));
    1054        2723 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1055             : }

Generated by: LCOV version 1.13