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.1 lcov report (development 24988-2584e74448) Lines: 531 570 93.2 %
Date: 2020-01-26 05:57:03 Functions: 36 36 100.0 %
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     3924265 : int_block(GEN n, long i, long l)
      32             : {
      33     3924265 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      34     3924381 :   GEN nw = int_W(n, q);
      35     3924381 :   ulong w = (ulong) *nw, w2;
      36     3924381 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      37      293962 :   w &= (1UL<<r)-1; lr = l-r;
      38      293962 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      39      293962 :   return (w<<lr)|w2;
      40             : }
      41             : 
      42             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      43             : static GEN
      44     7213379 : 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     7213379 :   long i, l = expu(n), u = (1UL<<(e-1));
      49             :   long w, v;
      50     7213000 :   GEN tab = cgetg(1+u, t_VEC);
      51     7214599 :   GEN x2 = sqr(E, x), z = NULL, tw;
      52     6640019 :   gel(tab, 1) = x;
      53     6640019 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      54     6625752 :   av = avma;
      55    62956637 :   while (l>=0)
      56             :   {
      57    49126723 :     if (e > l+1) e = l+1;
      58    49126723 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      59    49313153 :     tw = gel(tab, 1+(w>>(v+1)));
      60    49313153 :     if (z)
      61             :     {
      62    42104878 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
      63    41940871 :       z = mul(E, z, tw);
      64     7208275 :     } else z = tw;
      65    49122649 :     for (i=1; i<=v; i++) z = sqr(E, z);
      66   139751908 :     while (l>=0)
      67             :     {
      68    82949803 :       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    83600996 :       if (n&(1UL<<l)) break;
      74    41594452 :       z = sqr(E, z); l--;
      75             :     }
      76             :   }
      77     7204162 :   return z;
      78             : }
      79             : 
      80             : 
      81             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      82             : static GEN
      83       84093 : 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       84093 :   long i, l = expi(n), u = (1UL<<(e-1));
      88             :   long w, v;
      89       84093 :   GEN tab = cgetg(1+u, t_VEC);
      90       84093 :   GEN x2 = sqr(E, x), z = NULL, tw;
      91       81164 :   gel(tab, 1) = x;
      92       81164 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      93       81158 :   av = avma;
      94     3376753 :   while (l>=0)
      95             :   {
      96     3211564 :     if (e > l+1) e = l+1;
      97     3211564 :     w = int_block(n,l,e); v = vals(w); l-=e;
      98     3216452 :     tw = gel(tab, 1+(w>>(v+1)));
      99     3216452 :     if (z)
     100             :     {
     101     3132368 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
     102     3127097 :       z = mul(E, z, tw);
     103       84084 :     } else z = tw;
     104     3211062 :     for (i=1; i<=v; i++) z = sqr(E, z);
     105    13529567 :     while (l>=0)
     106             :     {
     107    10231480 :       if (gc_needed(av,1))
     108             :       {
     109         452 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110         452 :         z = gerepilecopy(av, z);
     111             :       }
     112    10231480 :       if (int_bit(n,l)) break;
     113     7109637 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116       84031 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    49467544 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                               GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    49467544 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    49467544 :   if (n == 1) return x;
     129    49467544 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    49467544 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   145522031 :   for (; j; n<<=1,j--)
     134             :   {
     135    96063484 :     y = sqr(E,y);
     136    96055029 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137    96054487 :     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    49458547 :   return y;
     144             : }
     145             : 
     146             : GEN
     147    56947356 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150             :   long l;
     151    56947356 :   if (n == 1) return x;
     152    56675069 :   l = expu(n);
     153    56676796 :   if (l<=8)
     154    49461274 :     return leftright_binary_powu(x, n, E, sqr, mul);
     155             :   else
     156     7215522 :     return sliding_window_powu(x, n, l<=24? 2: 3, E, sqr, mul);
     157             : }
     158             : 
     159             : GEN
     160      176187 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     161             :                                   GEN (*mul)(void*,GEN,GEN))
     162             : {
     163      176187 :   pari_sp av = avma;
     164      176187 :   if (n == 1) return gcopy(x);
     165      152753 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     166             : }
     167             : 
     168             : GEN
     169    16587451 : 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    16587451 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     174       84093 :   l = expi(n);
     175       84093 :   if      (l<=64)  e = 3;
     176       60243 :   else if (l<=160) e = 4;
     177       26662 :   else if (l<=384) e = 5;
     178       18612 :   else if (l<=896) e = 6;
     179        9855 :   else             e = 7;
     180       84093 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     181             : }
     182             : 
     183             : GEN
     184        4783 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     185             :                                GEN (*mul)(void*,GEN,GEN))
     186             : {
     187        4783 :   pari_sp av = avma;
     188        4783 :   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      808984 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     194             :                                          GEN (*msqr)(void*,GEN))
     195             : {
     196      808984 :   pari_sp av = avma;
     197             :   GEN y;
     198             :   int j;
     199             : 
     200      808984 :   if (n == 1) return x;
     201      808984 :   y = x; j = 1+bfffo(n);
     202             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     203      808984 :   n<<=j; j = BITS_IN_LONG-j;
     204             :   /* first bit is now implicit */
     205     4843217 :   for (; j; n<<=1,j--)
     206             :   {
     207     4034233 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     208     2922238 :     else y = sqr(E,y);
     209     4034233 :     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      808984 :   return y;
     216             : }
     217             : GEN
     218        1270 : gen_powu_fold(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     219             :                                        GEN (*msqr)(void*,GEN))
     220             : {
     221        1270 :   pari_sp av = avma;
     222        1270 :   if (n == 1) return gcopy(x);
     223        1270 :   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      221812 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     229             :                                       GEN (*msqr)(void*,GEN))
     230             : {
     231      221812 :   long ln = lgefint(N);
     232      221812 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     233             :   else
     234             :   {
     235       99705 :     GEN nd = int_MSW(N), y = x;
     236       99705 :     ulong n = *nd;
     237             :     long i;
     238             :     int j;
     239       99705 :     pari_sp av = avma;
     240             : 
     241       99705 :     if (n == 1)
     242        7953 :       j = 0;
     243             :     else
     244             :     {
     245       91752 :       j = 1+bfffo(n); /* < BIL */
     246             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     247       91752 :       n <<= j; j = BITS_IN_LONG - j;
     248             :     }
     249             :     /* first bit is now implicit */
     250       99705 :     for (i=ln-2;;)
     251             :     {
     252    31023796 :       for (; j; n<<=1,j--)
     253             :       {
     254    29926506 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     255    20085883 :         else y = sqr(E,y);
     256    29924947 :         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      597718 :       if (--i == 0) return y;
     263      499572 :       nd = int_precW(nd);
     264      499572 :       n = *nd; j = BITS_IN_LONG;
     265             :     }
     266             :   }
     267             : }
     268             : GEN
     269      122108 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     270             :                                     GEN (*msqr)(void*,GEN))
     271             : {
     272      122108 :   pari_sp av = avma;
     273      122108 :   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       64733 : gen_pow_table(GEN R, GEN n, void *E, GEN (*one)(void*), GEN (*mul)(void*,GEN,GEN))
     297             : {
     298       64733 :   long e = expu(lg(R)-1) + 1;
     299       64733 :   long l = expi(n);
     300             :   long i, w;
     301       64733 :   GEN z = one(E), tw;
     302     1531517 :   for(i=0; i<=l; )
     303             :   {
     304     1402051 :     if (int_bit(n, i)==0) { i++; continue; }
     305      712698 :     if (i+e-1>l) e = l+1-i;
     306      712698 :     w = int_block(n,i+e-1,e);
     307      712698 :     tw = gmael(R, 1+(w>>1), i+1);
     308      712698 :     z = mul(E, z, tw);
     309      712698 :     i += e;
     310             :   }
     311       64733 :   return z;
     312             : }
     313             : 
     314             : GEN
     315     8308022 : 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     8308022 :   GEN V = cgetg(l+2,t_VEC);
     320     8308109 :   gel(V,1) = one(E); if (l==0) return V;
     321     8291988 :   gel(V,2) = gcopy(x); if (l==1) return V;
     322     4963811 :   gel(V,3) = sqr(E,x);
     323     4963625 :   if (use_sqr)
     324    13157669 :     for(i = 4; i < l+2; i++)
     325    22434212 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     326    13234567 :                       : mul(E,gel(V, i-1),x);
     327             :   else
     328     2731322 :     for(i = 4; i < l+2; i++)
     329     1725722 :       gel(V,i) = mul(E,gel(V,i-1),x);
     330     4963637 :   return V;
     331             : }
     332             : 
     333             : GEN
     334    51721559 : producttree_scheme(long n)
     335             : {
     336             :   GEN v, w;
     337             :   long i, j, k, u, l;
     338    51721559 :   if (n<=2) return mkvecsmall(n);
     339    43656628 :   u = expu(n-1);
     340    43656640 :   v = cgetg(n+1,t_VECSMALL);
     341    43656734 :   w = cgetg(n+1,t_VECSMALL);
     342    43657037 :   v[1] = n; l = 1;
     343   145738166 :   for (i=1; i<=u; i++)
     344             :   {
     345   365662987 :     for(j=1, k=1; j<=l; j++, k+=2)
     346             :     {
     347   263581858 :       long vj = v[j], v2 = vj>>1;
     348   263581858 :       w[k]    = vj-v2;
     349   263581858 :       w[k+1]  = v2;
     350             :     }
     351   102081129 :     swap(v,w); l<<=1;
     352             :   }
     353    43657037 :   fixlg(v, l+1); set_avma((pari_sp)v); return v;
     354             : }
     355             : 
     356             : GEN
     357    53927409 : gen_product(GEN x, void *E, GEN (*mul)(void *,GEN,GEN))
     358             : {
     359             :   pari_sp av;
     360    53927409 :   long i, k, l = lg(x);
     361             :   pari_timer ti;
     362             :   GEN y, v;
     363             : 
     364    53927409 :   if (l <= 2) return l == 1? gen_1: gcopy(gel(x,1));
     365    51662327 :   y = cgetg(l, t_VEC); av = avma;
     366    51662492 :   v = producttree_scheme(l-1);
     367    51662780 :   l = lg(v);
     368    51662780 :   if (DEBUGLEVEL>7) timer_start(&ti);
     369   366334231 :   for (k = i = 1; k < l; i += v[k++])
     370   314672016 :     gel(y,k) = v[k]==1? gel(x,i): mul(E, gel(x,i), gel(x,i+1));
     371   205244637 :   while (k > 2)
     372             :   {
     373   101919890 :     long n = k - 1;
     374   101919890 :     if (DEBUGLEVEL>7) timer_printf(&ti,"gen_product: remaining objects %ld",n);
     375   101919666 :     for (k = i = 1; i < n; i += 2) gel(y,k++) = mul(E, gel(y,i), gel(y,i+1));
     376   101920427 :     if (gc_needed(av,1)) gerepilecoeffs(av, y+1, k-1);
     377             :   }
     378    51662532 :   return gel(y,1);
     379             : }
     380             : 
     381             : /***********************************************************************/
     382             : /**                                                                   **/
     383             : /**                    DISCRETE LOGARITHM                             **/
     384             : /**                                                                   **/
     385             : /***********************************************************************/
     386             : static GEN
     387    37584096 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     388             : {
     389    37584096 :   GEN a = gel(A,1), b = gel(A,2), c = gel(A,3);
     390    37584096 :   switch((h | grp->hash(a)) % 3UL)
     391             :   {
     392    12542999 :     case 0: return mkvec3(grp->pow(E,a,gen_2), Fp_mulu(b,2,q), Fp_mulu(c,2,q));
     393    12529330 :     case 1: return mkvec3(grp->mul(E,a,x), addiu(b,1), c);
     394    12511767 :     case 2: return mkvec3(grp->mul(E,a,g), b, addiu(c,1));
     395             :   }
     396             :   return NULL; /* LCOV_EXCL_LINE */
     397             : }
     398             : 
     399             : /*Generic Pollard rho discrete log algorithm*/
     400             : static GEN
     401          49 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     402             : {
     403          49 :   pari_sp av=avma;
     404          49 :   GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
     405          49 :   ulong i, h = 0, imax = itou_or_0(sqrt4q);
     406          49 :   if (!imax) imax = ULONG_MAX;
     407             :   do {
     408             :  rho_restart:
     409          49 :     A = B = mkvec3(x,gen_1,gen_0);
     410          49 :     i=0;
     411             :     do {
     412    12528032 :       if (i>imax)
     413             :       {
     414           0 :         h++;
     415           0 :         if (DEBUGLEVEL)
     416           0 :           pari_warn(warner,"changing Pollard rho hash seed to %ld",h);
     417           0 :         goto rho_restart;
     418             :       }
     419    12528032 :       A = iter_rho(x, g, q, A, h, E, grp);
     420    12528032 :       B = iter_rho(x, g, q, B, h, E, grp);
     421    12528032 :       B = iter_rho(x, g, q, B, h, E, grp);
     422    12528032 :       if (gc_needed(av,2))
     423             :       {
     424         948 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     425         948 :         gerepileall(av, 2, &A, &B);
     426             :       }
     427    12528032 :       i++;
     428    12528032 :     } while (!grp->equal(gel(A,1), gel(B,1)));
     429          49 :     gel(A,2) = modii(gel(A,2), q);
     430          49 :     gel(B,2) = modii(gel(B,2), q);
     431          49 :     h++;
     432          49 :   } while (equalii(gel(A,2), gel(B,2)));
     433          49 :   l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
     434          49 :   return gerepileuptoint(av, l);
     435             : }
     436             : 
     437             : /* compute a hash of g^(i-1), 1<=i<=n. Return [sorted hash, perm, g^-n] */
     438             : GEN
     439     2679171 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
     440             : {
     441     2679171 :   GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
     442     2679171 :   pari_sp av=avma;
     443             :   long i;
     444     2679171 :   table[1] = grp->hash(grp->pow(E,g,gen_0));
     445    13416837 :   for (i=2; i<=n; i++)
     446             :   {
     447    10737666 :     table[i] = grp->hash(p1);
     448    10737666 :     p1 = grp->mul(E,p1,g);
     449    10737666 :     if (gc_needed(av,2))
     450             :     {
     451           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     452           0 :       p1 = gerepileupto(av, p1);
     453             :     }
     454             :   }
     455     2679171 :   G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
     456     2679171 :   perm = vecsmall_indexsort(table);
     457     2679171 :   table = vecsmallpermute(table,perm);
     458     2679171 :   return mkvec4(table,perm,g,G);
     459             : }
     460             : /* T from gen_Shanks_init(g,n). Return v < n*N such that x = g^v or NULL */
     461             : GEN
     462     2679619 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
     463             : {
     464     2679619 :   pari_sp av=avma;
     465     2679619 :   GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
     466     2679619 :   GEN p1 = x;
     467     2679619 :   long n = lg(table)-1;
     468             :   ulong k;
     469    14967656 :   for (k=0; k<N; k++)
     470             :   { /* p1 = x G^k, G = g^-n */
     471    14673411 :     long h = grp->hash(p1), i = zv_search(table, h);
     472    14673411 :     if (i)
     473             :     {
     474     2386119 :       do i--; while (i && table[i] == h);
     475     2385374 :       for (i++; i <= n && table[i] == h; i++)
     476             :       {
     477     2385374 :         GEN v = addiu(muluu(n,k), perm[i]-1);
     478     2385374 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     479           0 :         if (DEBUGLEVEL)
     480           0 :           err_printf("gen_Shanks_log: false positive %lu, %lu\n", k,h);
     481             :       }
     482             :     }
     483    12288037 :     p1 = grp->mul(E,p1,G);
     484    12288037 :     if (gc_needed(av,2))
     485             :     {
     486           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %lu", k);
     487           0 :       p1 = gerepileupto(av, p1);
     488             :     }
     489             :   }
     490      294245 :   return NULL;
     491             : }
     492             : /* Generic Shanks baby-step/giant-step algorithm. Return log_g(x), ord g = q.
     493             :  * One-shot: use gen_Shanks_init/log if many logs are desired; early abort
     494             :  * if log < sqrt(q) */
     495             : static GEN
     496      405099 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     497             : {
     498      405099 :   pari_sp av=avma, av1;
     499             :   long lbaby, i, k;
     500             :   GEN p1, table, giant, perm, ginv;
     501      405099 :   p1 = sqrti(q);
     502      405099 :   if (abscmpiu(p1,LGBITS) >= 0)
     503           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     504      405099 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     505      405099 :   ginv = grp->pow(E,g,gen_m1);
     506      405099 :   av1 = avma;
     507     1817074 :   for (p1=x, i=1;;i++)
     508             :   {
     509     3229049 :     if (grp->equal1(p1)) { set_avma(av); return stoi(i-1); }
     510     1761378 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     511     1411975 :     p1 = grp->mul(E,p1,ginv);
     512     1411975 :     if (gc_needed(av1,2))
     513             :     {
     514           7 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     515           7 :       p1 = gerepileupto(av1, p1);
     516             :     }
     517             :   }
     518      349403 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     519      349403 :   perm = vecsmall_indexsort(table);
     520      349403 :   table = vecsmallpermute(table,perm);
     521      349403 :   av1 = avma;
     522      743793 :   for (k=1; k<= lbaby; k++)
     523             :   {
     524      743793 :     long h = grp->hash(p1), i = zv_search(table, h);
     525      743793 :     if (i)
     526             :     {
     527      349403 :       while (table[i] == h && i) i--;
     528      349403 :       for (i++; i <= lbaby && table[i] == h; i++)
     529             :       {
     530      349403 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     531      349403 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     532           0 :         if (DEBUGLEVEL)
     533           0 :           err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
     534             :       }
     535             :     }
     536      394390 :     p1 = grp->mul(E,p1,giant);
     537      394390 :     if (gc_needed(av1,2))
     538             :     {
     539           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %ld", k);
     540           0 :       p1 = gerepileupto(av1, p1);
     541             :     }
     542             :   }
     543           0 :   set_avma(av); return cgetg(1, t_VEC); /* no solution */
     544             : }
     545             : 
     546             : /*Generic discrete logarithme in a group of prime order p*/
     547             : GEN
     548     1654203 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     549             : {
     550     1654203 :   if (grp->easylog)
     551             :   {
     552     1630264 :     GEN e = grp->easylog(E, x, g, p);
     553     1630264 :     if (e) return e;
     554             :   }
     555      616481 :   if (grp->equal1(x)) return gen_0;
     556      616439 :   if (grp->equal(x,g)) return gen_1;
     557      405148 :   if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
     558          49 :   return gen_Pollard_log(x, g, p, E, grp);
     559             : }
     560             : 
     561             : GEN
     562     7992165 : get_arith_ZZM(GEN o)
     563             : {
     564     7992165 :   if (!o) return NULL;
     565     7992165 :   switch(typ(o))
     566             :   {
     567             :     case t_INT:
     568     4915919 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     569           7 :       break;
     570             :     case t_MAT:
     571     1380228 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     572          14 :       break;
     573             :     case t_VEC:
     574     1696011 :       if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
     575           0 :       break;
     576             :   }
     577          28 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     578             :   return NULL; /* LCOV_EXCL_LINE */
     579             : }
     580             : GEN
     581      433299 : get_arith_Z(GEN o)
     582             : {
     583      433299 :   if (!o) return NULL;
     584      433299 :   switch(typ(o))
     585             :   {
     586             :     case t_INT:
     587      352320 :       if (signe(o) > 0) return o;
     588           7 :       break;
     589             :     case t_MAT:
     590          14 :       o = factorback(o);
     591           0 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     592           0 :       break;
     593             :     case t_VEC:
     594       80958 :       if (lg(o) != 3) break;
     595       80958 :       o = gel(o,1);
     596       80958 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     597           0 :       break;
     598             :   }
     599          14 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     600             :   return NULL; /* LCOV_EXCL_LINE */
     601             : }
     602             : 
     603             : /* Generic Pohlig-Hellman discrete logarithm: smallest integer n >= 0 such that
     604             :  * g^n=a. Assume ord(g) | ord; grp->easylog() is an optional trapdoor
     605             :  * function that catches easy logarithms */
     606             : GEN
     607     1146861 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     608             : {
     609     1146861 :   pari_sp av = avma;
     610             :   GEN v, ginv, fa, ex;
     611             :   long i, j, l;
     612             : 
     613     1146861 :   if (grp->equal(g, a)) /* frequent special case */
     614      186027 :     return grp->equal1(g)? gen_0: gen_1;
     615      960834 :   if (grp->easylog)
     616             :   {
     617      960722 :     GEN e = grp->easylog(E, a, g, ord);
     618      960694 :     if (e) return e;
     619             :   }
     620      509299 :   v = get_arith_ZZM(ord);
     621      509299 :   ord= gel(v,1);
     622      509299 :   fa = gel(v,2);
     623      509299 :   ex = gel(fa,2);
     624      509299 :   fa = gel(fa,1); l = lg(fa);
     625      509299 :   ginv = grp->pow(E,g,gen_m1);
     626      509299 :   v = cgetg(l, t_VEC);
     627     1582216 :   for (i = 1; i < l; i++)
     628             :   {
     629     1072924 :     GEN q = gel(fa,i), qj, gq, nq, ginv0, a0, t0;
     630     1072924 :     long e = itos(gel(ex,i));
     631     1072924 :     if (DEBUGLEVEL>5)
     632           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     633     1072924 :     qj = new_chunk(e+1);
     634     1072924 :     gel(qj,0) = gen_1;
     635     1072924 :     gel(qj,1) = q;
     636     1072924 :     for (j = 2; j <= e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     637     1072924 :     t0 = diviiexact(ord, gel(qj,e));
     638     1072924 :     a0 = grp->pow(E, a, t0);
     639     1072924 :     ginv0 = grp->pow(E, ginv, t0); /* ord(ginv0) | q^e */
     640     1072924 :     if (grp->equal1(ginv0)) { gel(v,i) = mkintmod(gen_0, gen_1); continue; }
     641     1072917 :     do gq = grp->pow(E,g, mulii(t0, gel(qj,--e))); while (grp->equal1(gq));
     642             :     /* ord(gq) = q */
     643     1072910 :     nq = gen_0;
     644     1470098 :     for (j = 0;; j++)
     645      397188 :     { /* nq = sum_{i<j} b_i q^i */
     646     1470098 :       GEN b = grp->pow(E,a0, gel(qj,e-j));
     647             :       /* cheap early abort: wrong local order */
     648     1470098 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     649           7 :         set_avma(av); return cgetg(1, t_VEC);
     650             :       }
     651     1470091 :       b = gen_plog(b, gq, q, E, grp);
     652     1470091 :       if (typ(b) != t_INT) { set_avma(av); return cgetg(1, t_VEC); }
     653     1470091 :       nq = addii(nq, mulii(b, gel(qj,j)));
     654     1470091 :       if (j == e) break;
     655             : 
     656      397188 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     657      397188 :       ginv0 = grp->pow(E,ginv0, q);
     658             :     }
     659     1072903 :     gel(v,i) = mkintmod(nq, gel(qj,e+1));
     660             :   }
     661      509292 :   return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
     662             : }
     663             : 
     664             : /***********************************************************************/
     665             : /**                                                                   **/
     666             : /**                    ORDER OF AN ELEMENT                            **/
     667             : /**                                                                   **/
     668             : /***********************************************************************/
     669             : /*Find the exact order of a assuming a^o==1*/
     670             : GEN
     671     3242865 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     672             : {
     673     3242865 :   pari_sp av = avma;
     674             :   long i, l;
     675             :   GEN m;
     676             : 
     677     3242865 :   m = get_arith_ZZM(o);
     678     3242864 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     679     3242864 :   o = gel(m,1);
     680     3242864 :   m = gel(m,2); l = lgcols(m);
     681    10114039 :   for (i = l-1; i; i--)
     682             :   {
     683     6871187 :     GEN t, y, p = gcoeff(m,i,1);
     684     6871187 :     long j, e = itos(gcoeff(m,i,2));
     685     6871198 :     if (l == 2) {
     686      679226 :       t = gen_1;
     687      679226 :       y = a;
     688             :     } else {
     689     6191972 :       t = diviiexact(o, powiu(p,e));
     690     6191929 :       y = grp->pow(E, a, t);
     691             :     }
     692     6871126 :     if (grp->equal1(y)) o = t;
     693             :     else {
     694     6378083 :       for (j = 1; j < e; j++)
     695             :       {
     696     2178286 :         y = grp->pow(E, y, p);
     697     2178291 :         if (grp->equal1(y)) break;
     698             :       }
     699     4723308 :       if (j < e) {
     700      523511 :         if (j > 1) p = powiu(p, j);
     701      523511 :         o = mulii(t, p);
     702             :       }
     703             :     }
     704             :   }
     705     3242852 :   return gerepilecopy(av, o);
     706             : }
     707             : 
     708             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     709             : GEN
     710     2777051 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     711             : {
     712     2777051 :   pari_sp av = avma;
     713             :   long i, l, ind;
     714             :   GEN m, F, P;
     715             : 
     716     2777051 :   m = get_arith_ZZM(o);
     717     2777051 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     718     2777051 :   o = gel(m,1);
     719     2777051 :   m = gel(m,2); l = lgcols(m);
     720     2777051 :   P = cgetg(l, t_COL); ind = 1;
     721     2777051 :   F = cgetg(l, t_COL);
     722     6673150 :   for (i = l-1; i; i--)
     723             :   {
     724     3896099 :     GEN t, y, p = gcoeff(m,i,1);
     725     3896099 :     long j, e = itos(gcoeff(m,i,2));
     726     3896099 :     if (l == 2) {
     727     1702992 :       t = gen_1;
     728     1702992 :       y = a;
     729             :     } else {
     730     2193107 :       t = diviiexact(o, powiu(p,e));
     731     2193107 :       y = grp->pow(E, a, t);
     732             :     }
     733     3896099 :     if (grp->equal1(y)) o = t;
     734             :     else {
     735     4884401 :       for (j = 1; j < e; j++)
     736             :       {
     737     1037313 :         y = grp->pow(E, y, p);
     738     1037313 :         if (grp->equal1(y)) break;
     739             :       }
     740     3860155 :       gel(P,ind) = p;
     741     3860155 :       gel(F,ind) = utoipos(j);
     742     3860155 :       if (j < e) {
     743       13067 :         if (j > 1) p = powiu(p, j);
     744       13067 :         o = mulii(t, p);
     745             :       }
     746     3860155 :       ind++;
     747             :     }
     748             :   }
     749     2777051 :   setlg(P, ind); P = vecreverse(P);
     750     2777051 :   setlg(F, ind); F = vecreverse(F);
     751     2777051 :   return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
     752             : }
     753             : 
     754             : /* E has order o[1], ..., or o[#o], draw random points until all solutions
     755             :  * but one are eliminated */
     756             : GEN
     757         938 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     758             : {
     759         938 :   pari_sp ltop = avma, btop;
     760             :   GEN lastgood, so, vo;
     761         938 :   long lo = lg(o), nbo=lo-1;
     762         938 :   if (nbo == 1) return icopy(gel(o,1));
     763         413 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     764         413 :   vo = zero_zv(lo);
     765         413 :   lastgood = gel(o, so[nbo]);
     766         413 :   btop = avma;
     767             :   for(;;)
     768           0 :   {
     769         413 :     GEN lasto = gen_0;
     770         413 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     771             :     long i;
     772         525 :     for (i = 1; i < lo; i++)
     773             :     {
     774         525 :       GEN newo = gel(o, so[i]);
     775         525 :       if (vo[i]) continue;
     776         525 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     777         525 :       lasto = newo;
     778         525 :       if (!grp->equal1(t))
     779             :       {
     780         455 :         if (--nbo == 1) { set_avma(ltop); return icopy(lastgood); }
     781          42 :         vo[i] = 1;
     782             :       }
     783             :       else
     784          70 :         lastgood = lasto;
     785             :     }
     786           0 :     set_avma(btop);
     787             :   }
     788             : }
     789             : 
     790             : /*******************************************************************/
     791             : /*                                                                 */
     792             : /*                          n-th ROOT                              */
     793             : /*                                                                 */
     794             : /*******************************************************************/
     795             : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
     796             :  * of order l.
     797             :  *
     798             :  * q = l^e*r, e>=1, (r,l)=1
     799             :  * UNCLEAN */
     800             : static GEN
     801      269203 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     802             : {
     803      269203 :   const pari_sp av1 = avma;
     804             :   GEN m, m1;
     805             :   long i;
     806      210647 :   for (;; set_avma(av1))
     807             :   {
     808      690497 :     m1 = m = grp->pow(E, grp->rand(E), r);
     809      479850 :     if (grp->equal1(m)) continue;
     810      875683 :     for (i=1; i<e; i++)
     811             :     {
     812      606480 :       m = grp->pow(E,m,l);
     813      606480 :       if (grp->equal1(m)) break;
     814             :     }
     815      390458 :     if (i==e) break;
     816             :   }
     817      269203 :   *zeta = m; return m1;
     818             : }
     819             : 
     820             : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
     821             : 
     822             : GEN
     823       15876 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     824             : {
     825       15876 :   pari_sp ltop = avma, av;
     826             :   long i, lpr;
     827       15876 :   GEN F, N, pr, z=NULL;
     828       15876 :   F = get_arith_ZZM(o);
     829       15876 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     830       15876 :   av = avma;
     831             : 
     832       51527 :   for (i = 1; i < lpr; i++)
     833             :   {
     834       35651 :     GEN l = gcoeff(pr,i,1);
     835       35651 :     long e = itos(gcoeff(pr,i,2));
     836       35651 :     GEN r = diviiexact(N,powis(l,e));
     837       35651 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     838       35651 :     z = i==1 ? zl: grp->mul(E,z,zl);
     839       35651 :     if (gc_needed(av,2))
     840             :     { /* n can have lots of prime factors*/
     841           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
     842           0 :       z = gerepileupto(av, z);
     843             :     }
     844             :   }
     845       15876 :   return gerepileupto(ltop, z);
     846             : }
     847             : 
     848             : /* solve x^l = a , l prime in G of order q.
     849             :  *
     850             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     851             :  * y is not an l-th power, hence generates the l-Sylow of G
     852             :  * m = y^(q/l) != 1 */
     853             : static GEN
     854      234335 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
     855             :                  const struct bb_group *grp)
     856             : {
     857      234335 :   pari_sp av = avma;
     858             :   long k;
     859             :   GEN p1, u1, u2, v, w, z, dl;
     860             : 
     861      234335 :   (void)bezout(r,l,&u1,&u2);
     862      234335 :   v = grp->pow(E,a,u2);
     863      234335 :   w = grp->pow(E,v,l);
     864      234335 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     865      652782 :   while (!grp->equal1(w))
     866             :   {
     867      186975 :     k = 0;
     868      186975 :     p1 = w;
     869             :     do
     870             :     {
     871      323444 :       z = p1; p1 = grp->pow(E,p1,l);
     872      323444 :       k++;
     873      323444 :     } while(!grp->equal1(p1));
     874      186975 :     if (k==e) return gc_NULL(av);
     875      184112 :     dl = gen_plog(z,m,l,E,grp);
     876      184112 :     if (typ(dl) != t_INT) return gc_NULL(av);
     877      184112 :     dl = negi(dl);
     878      184112 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     879      184112 :     m = grp->pow(E,m,dl);
     880      184112 :     e = k;
     881      184112 :     v = grp->mul(E,p1,v);
     882      184112 :     y = grp->pow(E,p1,l);
     883      184112 :     w = grp->mul(E,y,w);
     884      184112 :     if (gc_needed(av,1))
     885             :     {
     886           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
     887           0 :       gerepileall(av,4, &y,&v,&w,&m);
     888             :     }
     889             :   }
     890      231472 :   return gerepilecopy(av, v);
     891             : }
     892             : /* Return one solution of x^n = a in a cyclic group of order q
     893             :  *
     894             :  * 1) If there is no solution, return NULL.
     895             :  *
     896             :  * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
     897             :  * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
     898             :  * the set of solutions is { x*zetan^k; k=0..m-1 }
     899             :  */
     900             : GEN
     901      242983 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     902             : {
     903      242983 :   pari_sp ltop = avma;
     904             :   GEN m, u1, u2, z;
     905             :   int is_1;
     906             : 
     907      242983 :   if (is_pm1(n))
     908             :   {
     909           0 :     if (zetan) *zetan = grp->pow(E,a,gen_0);
     910           0 :     return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
     911             :   }
     912      242983 :   is_1 = grp->equal1(a);
     913      242983 :   if (is_1 && !zetan) return gcopy(a);
     914             : 
     915      235281 :   m = bezout(n,q,&u1,&u2);
     916      235281 :   z = grp->pow(E,a,gen_0);
     917      235281 :   if (!is_pm1(m))
     918             :   {
     919      233398 :     GEN F = Z_factor(m);
     920             :     long i, j, e;
     921             :     GEN r, zeta, y, l;
     922      233398 :     pari_sp av1 = avma;
     923      464087 :     for (i = nbrows(F); i; i--)
     924             :     {
     925      233552 :       l = gcoeff(F,i,1);
     926      233552 :       j = itos(gcoeff(F,i,2));
     927      233552 :       e = Z_pvalrem(q,l,&r);
     928      233552 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     929      233552 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     930      233552 :       if (!is_1) {
     931             :         do
     932             :         {
     933      234335 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     934      234335 :           if (!a) return gc_NULL(ltop);
     935      231472 :         } while (--j);
     936             :       }
     937      230689 :       if (gc_needed(ltop,1))
     938             :       { /* n can have lots of prime factors*/
     939           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
     940           0 :         gerepileall(av1, zetan? 2: 1, &a, &z);
     941             :       }
     942             :     }
     943             :   }
     944      232418 :   if (!equalii(m, n))
     945        1946 :     a = grp->pow(E,a,modii(u1,q));
     946      232418 :   if (zetan)
     947             :   {
     948         364 :     *zetan = z;
     949         364 :     gerepileall(ltop,2,&a,zetan);
     950             :   }
     951             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     952      232054 :     a = gerepileupto(ltop, a);
     953      232418 :   return a;
     954             : }
     955             : 
     956             : /*******************************************************************/
     957             : /*                                                                 */
     958             : /*               structure of groups with pairing                  */
     959             : /*                                                                 */
     960             : /*******************************************************************/
     961             : 
     962             : static GEN
     963       41188 : ellgroup_d2(GEN N, GEN d)
     964             : {
     965       41188 :   GEN r = gcdii(N, d);
     966       41188 :   GEN F1 = gel(Z_factor(r), 1);
     967       41188 :   long i, j, l1 = lg(F1);
     968       41188 :   GEN F = cgetg(3, t_MAT);
     969       41188 :   gel(F,1) = cgetg(l1, t_COL);
     970       41188 :   gel(F,2) = cgetg(l1, t_COL);
     971       72310 :   for (i = 1, j = 1; i < l1; ++i)
     972             :   {
     973       31122 :     long v = Z_pval(N, gel(F1, i));
     974       31122 :     if (v<=1) continue;
     975       15988 :     gcoeff(F, j  , 1) = gel(F1, i);
     976       15988 :     gcoeff(F, j++, 2) = stoi(v);
     977             :   }
     978       41188 :   setlg(F[1],j); setlg(F[2],j);
     979       41188 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     980             : }
     981             : 
     982             : GEN
     983       41349 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
     984             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
     985             : {
     986       41349 :   pari_sp av = avma;
     987             :   GEN N0, N1, F;
     988       41349 :   if (pt_m) *pt_m = gen_1;
     989       41349 :   if (is_pm1(N)) return cgetg(1,t_VEC);
     990       41188 :   F = ellgroup_d2(N, d);
     991       41188 :   if (!F) {set_avma(av); return mkveccopy(N);}
     992       15260 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
     993             :   while(1)
     994       11698 :   {
     995       26958 :     pari_sp av2 = avma;
     996             :     GEN P, Q, d, s, t, m;
     997             : 
     998       26958 :     P = grp->pow(E,grp->rand(E), N1);
     999       26958 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {set_avma(av); return mkveccopy(N);}
    1000             : 
    1001       21178 :     Q = grp->pow(E,grp->rand(E), N1);
    1002       21178 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {set_avma(av); return mkveccopy(N);}
    1003             : 
    1004       18292 :     m = lcmii(s, t);
    1005       18292 :     d = pairorder(E, P, Q, m, F);
    1006             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
    1007       18292 :     if (is_pm1(d) && equalii(m, N0)) {set_avma(av); return mkveccopy(N);}
    1008       18285 :     if (equalii(mulii(m, d), N0))
    1009             :     {
    1010        6587 :       GEN g = mkvec2(mulii(N1,m), d);
    1011        6587 :       if (pt_m) *pt_m = m;
    1012        6587 :       gerepileall(av,pt_m?2:1,&g,pt_m);
    1013        6587 :       return g;
    1014             :     }
    1015       11698 :     set_avma(av2);
    1016             :   }
    1017             : }
    1018             : 
    1019             : GEN
    1020        2716 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
    1021             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1022             : {
    1023        2716 :   pari_sp ltop = avma, av;
    1024             :   GEN F, d1, dm;
    1025             :   GEN P, Q, d, s;
    1026        2716 :   F = get_arith_ZZM(D1);
    1027        2716 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1028        2716 :   av = avma;
    1029             :   do
    1030             :   {
    1031        6747 :     set_avma(av);
    1032        6747 :     P = grp->rand(E);
    1033        6747 :     s = gen_order(P, F, E, grp);
    1034        6747 :   } while (!equalii(s, d1));
    1035        2716 :   av = avma;
    1036             :   do
    1037             :   {
    1038        5112 :     set_avma(av);
    1039        5112 :     Q = grp->rand(E);
    1040        5112 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1041        5112 :   } while (!equalii(d, d2));
    1042        2716 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1043             : }

Generated by: LCOV version 1.13