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.18.0 lcov report (development 29732-95c6201d93) Lines: 532 568 93.7 %
Date: 2024-11-21 09:08:54 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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /***********************************************************************/
      16             : /**                                                                   **/
      17             : /**             GENERIC ALGORITHMS ON BLACKBOX GROUP                  **/
      18             : /**                                                                   **/
      19             : /***********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : #undef pow /* AIX: pow(a,b) is a macro, wrongly expanded on grp->pow(a,b,c) */
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_bb_group
      25             : 
      26             : /***********************************************************************/
      27             : /**                                                                   **/
      28             : /**                    POWERING                                       **/
      29             : /**                                                                   **/
      30             : /***********************************************************************/
      31             : 
      32             : /* return (n>>(i+1-l)) & ((1<<l)-1) */
      33             : static ulong
      34     8320679 : int_block(GEN n, long i, long l)
      35             : {
      36     8320679 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      37     8319893 :   GEN nw = int_W(n, q);
      38     8319893 :   ulong w = (ulong) *nw, w2;
      39     8319893 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      40      572373 :   w &= (1UL<<r)-1; lr = l-r;
      41      572373 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      42      572373 :   return (w<<lr)|w2;
      43             : }
      44             : 
      45             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      46             : static GEN
      47     9313906 : sliding_window_powu(GEN x, ulong n, long e, void *E, GEN (*sqr)(void*,GEN),
      48             :                                                      GEN (*mul)(void*,GEN,GEN))
      49             : {
      50     9313906 :   long i, l = expu(n), u = (1UL<<(e-1));
      51     9313563 :   GEN tab = cgetg(1+u, t_VEC), x2 = sqr(E, x), z = NULL;
      52             : 
      53     9320228 :   gel(tab, 1) = x;
      54    24636641 :   for (i = 2; i <= u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      55    68861437 :   while (l >= 0)
      56             :   {
      57             :     long w, v;
      58             :     GEN tw;
      59    59698028 :     if (e > l+1) e = l+1;
      60    59698028 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      61    59704286 :     tw = gel(tab, 1 + (w>>(v+1)));
      62    59704286 :     if (!z) z = tw;
      63             :     else
      64             :     {
      65   137081135 :       for (i = 1; i <= e-v; i++) z = sqr(E, z);
      66    50261525 :       z = mul(E, z, tw);
      67             :     }
      68    97282391 :     for (i = 1; i <= v; i++) z = sqr(E, z);
      69   109742911 :     while (l >= 0)
      70             :     {
      71   100692250 :       if (n&(1UL<<l)) break;
      72    50183511 :       z = sqr(E, z); l--;
      73             :     }
      74             :   }
      75     9163409 :   return z;
      76             : }
      77             : 
      78             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      79             : static GEN
      80      226051 : sliding_window_pow(GEN x, GEN n, long e, void *E, GEN (*sqr)(void*,GEN),
      81             :                                                   GEN (*mul)(void*,GEN,GEN))
      82             : {
      83             :   pari_sp av;
      84      226051 :   long i, l = expi(n), u = (1UL<<(e-1));
      85      226049 :   GEN tab = cgetg(1+u, t_VEC);
      86      226050 :   GEN x2 = sqr(E, x), z = NULL;
      87             : 
      88      226047 :   gel(tab, 1) = x;
      89     2995148 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      90      226055 :   av = avma;
      91     7975171 :   while (l >= 0)
      92             :   {
      93             :     long w, v;
      94             :     GEN tw;
      95     7715726 :     if (e > l+1) e = l+1;
      96     7715726 :     w = int_block(n,l,e); v = vals(w); l-=e;
      97     7716535 :     tw = gel(tab, 1+(w>>(v+1)));
      98     7716535 :     if (!z) z = tw;
      99             :     else
     100             :     {
     101    40710609 :       for (i = 1; i <= e-v; i++) z = sqr(E, z);
     102     7480990 :       z = mul(E, z, tw);
     103             :     }
     104    14667310 :     for (i = 1; i <= v; i++) z = sqr(E, z);
     105    20751573 :     while (l >= 0)
     106             :     {
     107    20492679 :       if (gc_needed(av,1))
     108             :       {
     109        1049 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110        1049 :         z = gerepilecopy(av, z);
     111             :       }
     112    20492679 :       if (int_bit(n,l)) break;
     113    12982582 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116      259445 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    99159205 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                                GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    99159205 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    99159205 :   if (n == 1) return x;
     129    99159205 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    99159205 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   324260512 :   for (; j; n<<=1,j--)
     134             :   {
     135   225141263 :     y = sqr(E,y);
     136   225104490 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137   225093810 :     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    99119249 :   return y;
     144             : }
     145             : 
     146             : GEN
     147   111119727 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150   111119727 :   if (n == 1) return x;
     151   108470708 :   if (n < 512)
     152    99158922 :     return leftright_binary_powu(x, n, E, sqr, mul);
     153             :   else
     154     9311786 :     return sliding_window_powu(x, n, n < (1UL<<25)? 2: 3, E, sqr, mul);
     155             : }
     156             : 
     157             : GEN
     158      204889 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     159             :                                   GEN (*mul)(void*,GEN,GEN))
     160             : {
     161      204889 :   pari_sp av = avma;
     162      204889 :   if (n == 1) return gcopy(x);
     163      171269 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     164             : }
     165             : 
     166             : GEN
     167    40054284 : gen_pow_i(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     168             :                                  GEN (*mul)(void*,GEN,GEN))
     169             : {
     170             :   long l, e;
     171    40054284 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     172      226051 :   l = expi(n);
     173      226050 :   if      (l<=64)  e = 3;
     174      167404 :   else if (l<=160) e = 4;
     175       85806 :   else if (l<=384) e = 5;
     176       23003 :   else if (l<=896) e = 6;
     177       11536 :   else             e = 7;
     178      226050 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     179             : }
     180             : 
     181             : GEN
     182    11034648 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     183             :                                GEN (*mul)(void*,GEN,GEN))
     184             : {
     185    11034648 :   pari_sp av = avma;
     186    11034648 :   return gerepilecopy(av, gen_pow_i(x,n,E,sqr,mul));
     187             : }
     188             : 
     189             : /* assume n > 0. Compute x^n using left-right binary powering */
     190             : GEN
     191     1325756 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     192             :                                          GEN (*msqr)(void*,GEN))
     193             : {
     194     1325756 :   pari_sp av = avma;
     195             :   GEN y;
     196             :   int j;
     197             : 
     198     1325756 :   if (n == 1) return x;
     199     1325756 :   y = x; j = 1+bfffo(n);
     200             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     201     1325756 :   n<<=j; j = BITS_IN_LONG-j;
     202             :   /* first bit is now implicit */
     203     5896896 :   for (; j; n<<=1,j--)
     204             :   {
     205     4571199 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     206     3347458 :     else y = sqr(E,y);
     207     4571154 :     if (gc_needed(av,1))
     208             :     {
     209           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"gen_powu_fold (%d)", j);
     210           0 :       y = gerepilecopy(av, y);
     211             :     }
     212             :   }
     213     1325697 :   return y;
     214             : }
     215             : GEN
     216        5030 : gen_powu_fold(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     217             :                                        GEN (*msqr)(void*,GEN))
     218             : {
     219        5030 :   pari_sp av = avma;
     220        5030 :   if (n == 1) return gcopy(x);
     221        5030 :   return gerepilecopy(av, gen_powu_fold_i(x,n,E,sqr,msqr));
     222             : }
     223             : 
     224             : /* assume N != 0, t_INT. Compute x^|N| using left-right binary powering */
     225             : GEN
     226      688614 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     227             :                                       GEN (*msqr)(void*,GEN))
     228             : {
     229      688614 :   long ln = lgefint(N);
     230      688614 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     231             :   else
     232             :   {
     233      145807 :     GEN nd = int_MSW(N), y = x;
     234      145807 :     ulong n = *nd;
     235             :     long i;
     236             :     int j;
     237      145807 :     pari_sp av = avma;
     238             : 
     239      145807 :     if (n == 1)
     240        7356 :       j = 0;
     241             :     else
     242             :     {
     243      138451 :       j = 1+bfffo(n); /* < BIL */
     244             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     245      138451 :       n <<= j; j = BITS_IN_LONG - j;
     246             :     }
     247             :     /* first bit is now implicit */
     248      145807 :     for (i=ln-2;;)
     249             :     {
     250    54338065 :       for (; j; n<<=1,j--)
     251             :       {
     252    53540200 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     253    37429988 :         else y = sqr(E,y);
     254    53583998 :         if (gc_needed(av,1))
     255             :         {
     256           0 :           if (DEBUGMEM>1) pari_warn(warnmem,"gen_pow_fold (%ld,%d)", i,j);
     257           0 :           y = gerepilecopy(av, y);
     258             :         }
     259             :       }
     260      843841 :       if (--i == 0) return y;
     261      884035 :       nd = int_precW(nd);
     262      884035 :       n = *nd; j = BITS_IN_LONG;
     263             :     }
     264             :   }
     265             : }
     266             : GEN
     267      542807 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     268             :                                     GEN (*msqr)(void*,GEN))
     269             : {
     270      542807 :   pari_sp av = avma;
     271      542807 :   return gerepilecopy(av, gen_pow_fold_i(x,n,E,sqr,msqr));
     272             : }
     273             : 
     274             : GEN
     275         147 : gen_pow_init(GEN x, GEN n, long k, void *E, GEN (*sqr)(void*,GEN), GEN (*mul)(void*,GEN,GEN))
     276             : {
     277         147 :   long i, j, l = expi(n);
     278         147 :   long m = 1UL<<(k-1);
     279         147 :   GEN x2 = sqr(E, x), y = gcopy(x);
     280         147 :   GEN R = cgetg(m+1, t_VEC);
     281         644 :   for(i = 1; i <= m; i++)
     282             :   {
     283         497 :     GEN C = cgetg(l+1, t_VEC);
     284         497 :     gel(C,1) = y;
     285       27118 :     for(j = 2; j <= l; j++)
     286       26621 :       gel(C,j) = sqr(E, gel(C,j-1));
     287         497 :     gel(R,i) = C;
     288         497 :     y = mul(E, y, x2);
     289             :   }
     290         147 :   return R;
     291             : }
     292             : 
     293             : GEN
     294       53330 : gen_pow_table(GEN R, GEN n, void *E, GEN (*one)(void*), GEN (*mul)(void*,GEN,GEN))
     295             : {
     296       53330 :   long e = expu(lg(R)-1) + 1;
     297       53330 :   long l = expi(n);
     298             :   long i, w;
     299       53330 :   GEN z = one(E), tw;
     300     1241265 :   for(i=0; i<=l; )
     301             :   {
     302     1187935 :     if (int_bit(n, i)==0) { i++; continue; }
     303      605931 :     if (i+e-1>l) e = l+1-i;
     304      605931 :     w = int_block(n,i+e-1,e);
     305      605931 :     tw = gmael(R, 1+(w>>1), i+1);
     306      605931 :     z = mul(E, z, tw);
     307      605931 :     i += e;
     308             :   }
     309       53330 :   return z;
     310             : }
     311             : 
     312             : GEN
     313    28682148 : gen_powers(GEN x, long l, int use_sqr, void *E, GEN (*sqr)(void*,GEN),
     314             :                                       GEN (*mul)(void*,GEN,GEN), GEN (*one)(void*))
     315             : {
     316             :   long i;
     317    28682148 :   GEN V = cgetg(l+2,t_VEC);
     318    28681952 :   gel(V,1) = one(E); if (l==0) return V;
     319    28656722 :   gel(V,2) = gcopy(x); if (l==1) return V;
     320    12576127 :   gel(V,3) = sqr(E,x);
     321    12578788 :   if (use_sqr)
     322    40481067 :     for(i = 4; i < l+2; i++)
     323    30871872 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     324    30872496 :                       : mul(E,gel(V, i-1),x);
     325             :   else
     326     7297519 :     for(i = 4; i < l+2; i++)
     327     4327945 :       gel(V,i) = mul(E,gel(V,i-1),x);
     328    12578145 :   return V;
     329             : }
     330             : 
     331             : GEN
     332    57408310 : producttree_scheme(long n)
     333             : {
     334             :   GEN v, w;
     335             :   long i, j, k, u, l;
     336    57408310 :   if (n<=2) return mkvecsmall(n);
     337    47759571 :   u = expu(n-1);
     338    47759590 :   v = cgetg(n+1,t_VECSMALL);
     339    47759617 :   w = cgetg(n+1,t_VECSMALL);
     340    47760000 :   v[1] = n; l = 1;
     341   156041345 :   for (i=1; i<=u; i++)
     342             :   {
     343   416244918 :     for(j=1, k=1; j<=l; j++, k+=2)
     344             :     {
     345   307963573 :       long vj = v[j], v2 = vj>>1;
     346   307963573 :       w[k]    = vj-v2;
     347   307963573 :       w[k+1]  = v2;
     348             :     }
     349   108281345 :     swap(v,w); l<<=1;
     350             :   }
     351    47760000 :   fixlg(v, l+1); set_avma((pari_sp)v); return v;
     352             : }
     353             : 
     354             : GEN
     355    60844557 : gen_product(GEN x, void *E, GEN (*mul)(void *,GEN,GEN))
     356             : {
     357             :   pari_sp av;
     358    60844557 :   long i, k, l = lg(x);
     359             :   pari_timer ti;
     360             :   GEN y, v;
     361             : 
     362    60844557 :   if (l <= 2) return l == 1? gen_1: gcopy(gel(x,1));
     363    56751074 :   y = cgetg(l, t_VEC); av = avma;
     364    56751365 :   v = producttree_scheme(l-1);
     365    56751820 :   l = lg(v);
     366    56751820 :   if (DEBUGLEVEL>7) timer_start(&ti);
     367   414797261 :   for (k = i = 1; k < l; i += v[k++])
     368   358048450 :     gel(y,k) = v[k]==1? gel(x,i): mul(E, gel(x,i), gel(x,i+1));
     369   163268323 :   while (k > 2)
     370             :   {
     371   106516820 :     long n = k - 1;
     372   106516820 :     if (DEBUGLEVEL>7) timer_printf(&ti,"gen_product: remaining objects %ld",n);
     373   407797939 :     for (k = i = 1; i < n; i += 2) gel(y,k++) = mul(E, gel(y,i), gel(y,i+1));
     374   106518711 :     if (gc_needed(av,1)) gerepilecoeffs(av, y+1, k-1);
     375             :   }
     376    56751503 :   return gel(y,1);
     377             : }
     378             : 
     379             : /***********************************************************************/
     380             : /**                                                                   **/
     381             : /**                    DISCRETE LOGARITHM                             **/
     382             : /**                                                                   **/
     383             : /***********************************************************************/
     384             : static GEN
     385    66913167 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     386             : {
     387    66913167 :   GEN a = gel(A,1), b = gel(A,2), c = gel(A,3);
     388    66913167 :   switch((h | grp->hash(a)) % 3UL)
     389             :   {
     390    22316102 :     case 0: return mkvec3(grp->pow(E,a,gen_2), Fp_mulu(b,2,q), Fp_mulu(c,2,q));
     391    22299331 :     case 1: return mkvec3(grp->mul(E,a,x), addiu(b,1), c);
     392    22297734 :     case 2: return mkvec3(grp->mul(E,a,g), b, addiu(c,1));
     393             :   }
     394             :   return NULL; /* LCOV_EXCL_LINE */
     395             : }
     396             : 
     397             : /*Generic Pollard rho discrete log algorithm*/
     398             : static GEN
     399          49 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     400             : {
     401          49 :   pari_sp av=avma;
     402          49 :   GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
     403          49 :   ulong i, h = 0, imax = itou_or_0(sqrt4q);
     404          49 :   if (!imax) imax = ULONG_MAX;
     405             :   do {
     406          49 :  rho_restart:
     407          49 :     A = B = mkvec3(x,gen_1,gen_0);
     408          49 :     i=0;
     409             :     do {
     410    22304389 :       if (i>imax)
     411             :       {
     412           0 :         h++;
     413           0 :         if (DEBUGLEVEL)
     414           0 :           pari_warn(warner,"changing Pollard rho hash seed to %ld",h);
     415           0 :         goto rho_restart;
     416             :       }
     417    22304389 :       A = iter_rho(x, g, q, A, h, E, grp);
     418    22304389 :       B = iter_rho(x, g, q, B, h, E, grp);
     419    22304389 :       B = iter_rho(x, g, q, B, h, E, grp);
     420    22304389 :       if (gc_needed(av,2))
     421             :       {
     422        1548 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     423        1548 :         gerepileall(av, 2, &A, &B);
     424             :       }
     425    22304389 :       i++;
     426    22304389 :     } while (!grp->equal(gel(A,1), gel(B,1)));
     427          49 :     gel(A,2) = modii(gel(A,2), q);
     428          49 :     gel(B,2) = modii(gel(B,2), q);
     429          49 :     h++;
     430          49 :   } while (equalii(gel(A,2), gel(B,2)));
     431          49 :   l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
     432          49 :   return gerepileuptoint(av, l);
     433             : }
     434             : 
     435             : /* compute a hash of g^(i-1), 1<=i<=n. Return [sorted hash, perm, g^-n] */
     436             : GEN
     437      723137 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
     438             : {
     439      723137 :   GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
     440      723137 :   pari_sp av=avma;
     441             :   long i;
     442      723137 :   table[1] = grp->hash(grp->pow(E,g,gen_0));
     443     4107090 :   for (i=2; i<=n; i++)
     444             :   {
     445     3383953 :     table[i] = grp->hash(p1);
     446     3383953 :     p1 = grp->mul(E,p1,g);
     447     3383953 :     if (gc_needed(av,2))
     448             :     {
     449           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     450           0 :       p1 = gerepileupto(av, p1);
     451             :     }
     452             :   }
     453      723137 :   G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
     454      723137 :   perm = vecsmall_indexsort(table);
     455      723137 :   table = vecsmallpermute(table,perm);
     456      723137 :   return mkvec4(table,perm,g,G);
     457             : }
     458             : /* T from gen_Shanks_init(g,n). Return v < n*N such that x = g^v or NULL */
     459             : GEN
     460      728513 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
     461             : {
     462      728513 :   pari_sp av=avma;
     463      728513 :   GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
     464      728513 :   GEN p1 = x;
     465      728513 :   long n = lg(table)-1;
     466             :   ulong k;
     467     4360123 :   for (k=0; k<N; k++)
     468             :   { /* p1 = x G^k, G = g^-n */
     469     4025990 :     long h = grp->hash(p1), i = zv_search(table, h);
     470     4025990 :     if (i)
     471             :     {
     472      395190 :       do i--; while (i && table[i] == h);
     473      394380 :       for (i++; i <= n && table[i] == h; i++)
     474             :       {
     475      394380 :         GEN v = addiu(muluu(n,k), perm[i]-1);
     476      394380 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     477           0 :         if (DEBUGLEVEL)
     478           0 :           err_printf("gen_Shanks_log: false positive %lu, %lu\n", k,h);
     479             :       }
     480             :     }
     481     3631610 :     p1 = grp->mul(E,p1,G);
     482     3631610 :     if (gc_needed(av,2))
     483             :     {
     484           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %lu", k);
     485           0 :       p1 = gerepileupto(av, p1);
     486             :     }
     487             :   }
     488      334133 :   return NULL;
     489             : }
     490             : /* Generic Shanks baby-step/giant-step algorithm. Return log_g(x), ord g = q.
     491             :  * One-shot: use gen_Shanks_init/log if many logs are desired; early abort
     492             :  * if log < sqrt(q) */
     493             : static GEN
     494     1516390 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     495             : {
     496     1516390 :   pari_sp av=avma, av1;
     497             :   long lbaby, i, k;
     498             :   GEN p1, table, giant, perm, ginv;
     499     1516390 :   p1 = sqrti(q);
     500     1516390 :   if (abscmpiu(p1,LGBITS) >= 0)
     501           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     502     1516390 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     503     1516390 :   ginv = grp->pow(E,g,gen_m1);
     504     1516390 :   av1 = avma;
     505     5028931 :   for (p1=x, i=1;;i++)
     506             :   {
     507     5028931 :     if (grp->equal1(p1)) return gc_stoi(av, i-1);
     508     4878049 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     509     3512540 :     p1 = grp->mul(E,p1,ginv);
     510     3512541 :     if (gc_needed(av1,2))
     511             :     {
     512           6 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     513           6 :       p1 = gerepileupto(av1, p1);
     514             :     }
     515             :   }
     516     1365509 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     517     1365509 :   perm = vecsmall_indexsort(table);
     518     1365509 :   table = vecsmallpermute(table,perm);
     519     1365509 :   av1 = avma;
     520     2191762 :   for (k=1; k<= lbaby; k++)
     521             :   {
     522     2191762 :     long h = grp->hash(p1), i = zv_search(table, h);
     523     2191762 :     if (i)
     524             :     {
     525     2731020 :       while (table[i] == h && i) i--;
     526     1365511 :       for (i++; i <= lbaby && table[i] == h; i++)
     527             :       {
     528     1365510 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     529     1365510 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     530           1 :         if (DEBUGLEVEL)
     531           0 :           err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
     532             :       }
     533             :     }
     534      826253 :     p1 = grp->mul(E,p1,giant);
     535      826253 :     if (gc_needed(av1,2))
     536             :     {
     537           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %ld", k);
     538           0 :       p1 = gerepileupto(av1, p1);
     539             :     }
     540             :   }
     541           0 :   set_avma(av); return cgetg(1, t_VEC); /* no solution */
     542             : }
     543             : 
     544             : /*Generic discrete logarithme in a group of prime order p*/
     545             : GEN
     546     6131369 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     547             : {
     548     6131369 :   if (grp->easylog)
     549             :   {
     550     6054065 :     GEN e = grp->easylog(E, x, g, p);
     551     6054065 :     if (e) return e;
     552             :   }
     553     2754921 :   if (grp->equal1(x)) return gen_0;
     554     2748607 :   if (grp->equal(x,g)) return gen_1;
     555     1516437 :   if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
     556          49 :   return gen_Pollard_log(x, g, p, E, grp);
     557             : }
     558             : 
     559             : GEN
     560    11133443 : get_arith_ZZM(GEN o)
     561             : {
     562    11133443 :   if (!o) return NULL;
     563    11133443 :   switch(typ(o))
     564             :   {
     565     3703312 :     case t_INT:
     566     3703312 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     567           7 :       break;
     568     1369945 :     case t_MAT:
     569     1369945 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     570          14 :       break;
     571     6060192 :     case t_VEC:
     572     6060192 :       if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
     573           0 :       break;
     574             :   }
     575          24 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     576             :   return NULL; /* LCOV_EXCL_LINE */
     577             : }
     578             : GEN
     579     1365887 : get_arith_Z(GEN o)
     580             : {
     581     1365887 :   if (!o) return NULL;
     582     1365887 :   switch(typ(o))
     583             :   {
     584     1118338 :     case t_INT:
     585     1118338 :       if (signe(o) > 0) return o;
     586           7 :       break;
     587          14 :     case t_MAT:
     588          14 :       o = factorback(o);
     589           0 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     590           0 :       break;
     591      247528 :     case t_VEC:
     592      247528 :       if (lg(o) != 3) break;
     593      247528 :       o = gel(o,1);
     594      247528 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     595           0 :       break;
     596             :   }
     597          14 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     598             :   return NULL; /* LCOV_EXCL_LINE */
     599             : }
     600             : 
     601             : /* Generic Pohlig-Hellman discrete logarithm: smallest integer n >= 0 such that
     602             :  * g^n=a. Assume ord(g) | ord; grp->easylog() is an optional trapdoor
     603             :  * function that catches easy logarithms */
     604             : GEN
     605     4688404 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     606             : {
     607     4688404 :   pari_sp av = avma;
     608             :   GEN v, ginv, fa, ex;
     609             :   long i, j, l;
     610             : 
     611     4688404 :   if (grp->equal(g, a)) /* frequent special case */
     612      978077 :     return grp->equal1(g)? gen_0: gen_1;
     613     3710322 :   if (grp->easylog)
     614             :   {
     615     3660430 :     GEN e = grp->easylog(E, a, g, ord);
     616     3660402 :     if (e) return e;
     617             :   }
     618     2151684 :   v = get_arith_ZZM(ord);
     619     2151725 :   ord= gel(v,1);
     620     2151725 :   fa = gel(v,2);
     621     2151725 :   ex = gel(fa,2);
     622     2151725 :   fa = gel(fa,1); l = lg(fa);
     623     2151725 :   ginv = grp->pow(E,g,gen_m1);
     624     2151725 :   v = cgetg(l, t_VEC);
     625     6243194 :   for (i = 1; i < l; i++)
     626             :   {
     627     4091476 :     GEN q = gel(fa,i), qj, gq, nq, ginv0, a0, t0;
     628     4091476 :     long e = itos(gel(ex,i));
     629     4091476 :     if (DEBUGLEVEL>5)
     630           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     631     4091476 :     qj = new_chunk(e+1);
     632     4091476 :     gel(qj,0) = gen_1;
     633     4091476 :     gel(qj,1) = q;
     634     5929145 :     for (j = 2; j <= e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     635     4091476 :     t0 = diviiexact(ord, gel(qj,e));
     636     4091476 :     a0 = grp->pow(E, a, t0);
     637     4091476 :     ginv0 = grp->pow(E, ginv, t0); /* ord(ginv0) | q^e */
     638     4091476 :     if (grp->equal1(ginv0)) { gel(v,i) = mkintmod(gen_0, gen_1); continue; }
     639     4091469 :     do gq = grp->pow(E,g, mulii(t0, gel(qj,--e))); while (grp->equal1(gq));
     640             :     /* ord(gq) = q */
     641     4091462 :     nq = gen_0;
     642     4091462 :     for (j = 0;; j++)
     643     1837633 :     { /* nq = sum_{i<j} b_i q^i */
     644     5929095 :       GEN b = grp->pow(E,a0, gel(qj,e-j));
     645             :       /* cheap early abort: wrong local order */
     646     5929096 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     647           7 :         set_avma(av); return cgetg(1, t_VEC);
     648             :       }
     649     5929089 :       b = gen_plog(b, gq, q, E, grp);
     650     5929089 :       if (typ(b) != t_INT) { set_avma(av); return cgetg(1, t_VEC); }
     651     5929089 :       nq = addii(nq, mulii(b, gel(qj,j)));
     652     5929088 :       if (j == e) break;
     653             : 
     654     1837633 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     655     1837633 :       ginv0 = grp->pow(E,ginv0, q);
     656             :     }
     657     4091455 :     gel(v,i) = mkintmod(nq, gel(qj,e+1));
     658             :   }
     659     2151718 :   return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
     660             : }
     661             : 
     662             : /***********************************************************************/
     663             : /**                                                                   **/
     664             : /**                    ORDER OF AN ELEMENT                            **/
     665             : /**                                                                   **/
     666             : /***********************************************************************/
     667             : /*Find the exact order of a assuming a^o==1*/
     668             : GEN
     669     4088284 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     670             : {
     671     4088284 :   pari_sp av = avma;
     672             :   long i, l;
     673             :   GEN m;
     674             : 
     675     4088284 :   m = get_arith_ZZM(o);
     676     4088284 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     677     4088284 :   o = gel(m,1);
     678     4088284 :   m = gel(m,2); l = lgcols(m);
     679    12493189 :   for (i = l-1; i; i--)
     680             :   {
     681     8404915 :     GEN t, y, p = gcoeff(m,i,1);
     682     8404915 :     long j, e = itos(gcoeff(m,i,2));
     683     8404923 :     if (l == 2) {
     684     1026203 :       t = gen_1;
     685     1026203 :       y = a;
     686             :     } else {
     687     7378720 :       t = diviiexact(o, powiu(p,e));
     688     7378699 :       y = grp->pow(E, a, t);
     689             :     }
     690     8404878 :     if (grp->equal1(y)) o = t;
     691             :     else {
     692     7851657 :       for (j = 1; j < e; j++)
     693             :       {
     694     2857329 :         y = grp->pow(E, y, p);
     695     2857338 :         if (grp->equal1(y)) break;
     696             :       }
     697     5782893 :       if (j < e) {
     698      788565 :         if (j > 1) p = powiu(p, j);
     699      788565 :         o = mulii(t, p);
     700             :       }
     701             :     }
     702             :   }
     703     4088274 :   return gerepilecopy(av, o);
     704             : }
     705             : 
     706             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     707             : GEN
     708        5467 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     709             : {
     710        5467 :   pari_sp av = avma;
     711             :   long i, l, ind;
     712             :   GEN m, F, P;
     713             : 
     714        5467 :   m = get_arith_ZZM(o);
     715        5467 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     716        5467 :   o = gel(m,1);
     717        5467 :   m = gel(m,2); l = lgcols(m);
     718        5467 :   P = cgetg(l, t_COL); ind = 1;
     719        5467 :   F = cgetg(l, t_COL);
     720       18879 :   for (i = l-1; i; i--)
     721             :   {
     722       13412 :     GEN t, y, p = gcoeff(m,i,1);
     723       13412 :     long j, e = itos(gcoeff(m,i,2));
     724       13412 :     if (l == 2) {
     725         672 :       t = gen_1;
     726         672 :       y = a;
     727             :     } else {
     728       12740 :       t = diviiexact(o, powiu(p,e));
     729       12740 :       y = grp->pow(E, a, t);
     730             :     }
     731       13412 :     if (grp->equal1(y)) o = t;
     732             :     else {
     733       16198 :       for (j = 1; j < e; j++)
     734             :       {
     735        4655 :         y = grp->pow(E, y, p);
     736        4655 :         if (grp->equal1(y)) break;
     737             :       }
     738       12250 :       gel(P,ind) = p;
     739       12250 :       gel(F,ind) = utoipos(j);
     740       12250 :       if (j < e) {
     741         707 :         if (j > 1) p = powiu(p, j);
     742         707 :         o = mulii(t, p);
     743             :       }
     744       12250 :       ind++;
     745             :     }
     746             :   }
     747        5467 :   setlg(P, ind); P = vecreverse(P);
     748        5467 :   setlg(F, ind); F = vecreverse(F);
     749        5467 :   return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
     750             : }
     751             : 
     752             : /* E has order o[1], ..., or o[#o], draw random points until all solutions
     753             :  * but one are eliminated */
     754             : GEN
     755         980 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     756             : {
     757         980 :   pari_sp ltop = avma, btop;
     758             :   GEN lastgood, so, vo;
     759         980 :   long lo = lg(o), nbo=lo-1;
     760         980 :   if (nbo == 1) return icopy(gel(o,1));
     761         441 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     762         441 :   vo = zero_zv(lo);
     763         441 :   lastgood = gel(o, so[nbo]);
     764         441 :   btop = avma;
     765             :   for(;;)
     766           0 :   {
     767         441 :     GEN lasto = gen_0;
     768         441 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     769             :     long i;
     770         567 :     for (i = 1; i < lo; i++)
     771             :     {
     772         567 :       GEN newo = gel(o, so[i]);
     773         567 :       if (vo[i]) continue;
     774         567 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     775         567 :       lasto = newo;
     776         567 :       if (!grp->equal1(t))
     777             :       {
     778         483 :         if (--nbo == 1) { set_avma(ltop); return icopy(lastgood); }
     779          42 :         vo[i] = 1;
     780             :       }
     781             :       else
     782          84 :         lastgood = lasto;
     783             :     }
     784           0 :     set_avma(btop);
     785             :   }
     786             : }
     787             : 
     788             : /*******************************************************************/
     789             : /*                                                                 */
     790             : /*                          n-th ROOT                              */
     791             : /*                                                                 */
     792             : /*******************************************************************/
     793             : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
     794             :  * of order l.
     795             :  *
     796             :  * q = l^e*r, e>=1, (r,l)=1
     797             :  * UNCLEAN */
     798             : static GEN
     799      283483 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     800             : {
     801      283483 :   const pari_sp av1 = avma;
     802             :   GEN m, m1;
     803             :   long i;
     804      220401 :   for (;; set_avma(av1))
     805             :   {
     806      503882 :     m1 = m = grp->pow(E, grp->rand(E), r);
     807      503877 :     if (grp->equal1(m)) continue;
     808      926288 :     for (i=1; i<e; i++)
     809             :     {
     810      642806 :       m = grp->pow(E,m,l);
     811      642808 :       if (grp->equal1(m)) break;
     812             :     }
     813      408401 :     if (i==e) break;
     814             :   }
     815      283480 :   *zeta = m; return m1;
     816             : }
     817             : 
     818             : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
     819             : 
     820             : GEN
     821       15883 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     822             : {
     823       15883 :   pari_sp ltop = avma, av;
     824             :   long i, lpr;
     825       15883 :   GEN F, N, pr, z=NULL;
     826       15883 :   F = get_arith_ZZM(o);
     827       15883 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     828       15883 :   av = avma;
     829             : 
     830       51562 :   for (i = 1; i < lpr; i++)
     831             :   {
     832       35679 :     GEN l = gcoeff(pr,i,1);
     833       35679 :     long e = itos(gcoeff(pr,i,2));
     834       35679 :     GEN r = diviiexact(N,powis(l,e));
     835       35679 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     836       35679 :     z = i==1 ? zl: grp->mul(E,z,zl);
     837       35679 :     if (gc_needed(av,2))
     838             :     { /* n can have lots of prime factors*/
     839           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
     840           0 :       z = gerepileupto(av, z);
     841             :     }
     842             :   }
     843       15883 :   return gerepileupto(ltop, z);
     844             : }
     845             : 
     846             : /* solve x^l = a , l prime in G of order q.
     847             :  *
     848             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     849             :  * y is not an l-th power, hence generates the l-Sylow of G
     850             :  * m = y^(q/l) != 1 */
     851             : static GEN
     852      263580 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
     853             :                  const struct bb_group *grp)
     854             : {
     855      263580 :   pari_sp av = avma;
     856             :   long k;
     857             :   GEN p1, u1, u2, v, w, z, dl;
     858             : 
     859      263580 :   (void)bezout(r,l,&u1,&u2);
     860      263585 :   v = grp->pow(E,a,u2);
     861      263580 :   w = grp->pow(E,v,l);
     862      263582 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     863      465866 :   while (!grp->equal1(w))
     864             :   {
     865      202449 :     k = 0;
     866      202449 :     p1 = w;
     867             :     do
     868             :     {
     869      361291 :       z = p1; p1 = grp->pow(E,p1,l);
     870      361289 :       k++;
     871      361289 :     } while(!grp->equal1(p1));
     872      202448 :     if (k==e) return gc_NULL(av);
     873      202280 :     dl = gen_plog(z,m,l,E,grp);
     874      202282 :     if (typ(dl) != t_INT) return gc_NULL(av);
     875      202282 :     dl = negi(dl);
     876      202282 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     877      202282 :     m = grp->pow(E,m,dl);
     878      202283 :     e = k;
     879      202283 :     v = grp->mul(E,p1,v);
     880      202283 :     y = grp->pow(E,p1,l);
     881      202281 :     w = grp->mul(E,y,w);
     882      202283 :     if (gc_needed(av,1))
     883             :     {
     884           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
     885           0 :       gerepileall(av,4, &y,&v,&w,&m);
     886             :     }
     887             :   }
     888      263414 :   return gerepilecopy(av, v);
     889             : }
     890             : /* Return one solution of x^n = a in a cyclic group of order q
     891             :  *
     892             :  * 1) If there is no solution, return NULL.
     893             :  *
     894             :  * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
     895             :  * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
     896             :  * the set of solutions is { x*zetan^k; k=0..m-1 }
     897             :  */
     898             : GEN
     899      253735 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     900             : {
     901      253735 :   pari_sp ltop = avma;
     902             :   GEN m, u1, u2, z;
     903             :   int is_1;
     904             : 
     905      253735 :   if (is_pm1(n))
     906             :   {
     907           0 :     if (zetan) *zetan = grp->pow(E,a,gen_0);
     908           0 :     return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
     909             :   }
     910      253735 :   is_1 = grp->equal1(a);
     911      253735 :   if (is_1 && !zetan) return gcopy(a);
     912             : 
     913      245011 :   m = bezout(n,q,&u1,&u2);
     914      245015 :   z = grp->pow(E,a,gen_0);
     915      245013 :   if (!is_pm1(m))
     916             :   {
     917      244614 :     GEN F = Z_factor(m);
     918             :     long i, j, e;
     919             :     GEN r, zeta, y, l;
     920      244618 :     pari_sp av1 = avma;
     921      492254 :     for (i = nbrows(F); i; i--)
     922             :     {
     923      247804 :       l = gcoeff(F,i,1);
     924      247804 :       j = itos(gcoeff(F,i,2));
     925      247804 :       e = Z_pvalrem(q,l,&r);
     926      247804 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     927      247801 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     928      247801 :       if (!is_1) {
     929             :         do
     930             :         {
     931      263580 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     932      263583 :           if (!a) return gc_NULL(ltop);
     933      263415 :         } while (--j);
     934             :       }
     935      247636 :       if (gc_needed(ltop,1))
     936             :       { /* n can have lots of prime factors*/
     937           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
     938           0 :         gerepileall(av1, zetan? 2: 1, &a, &z);
     939             :       }
     940             :     }
     941             :   }
     942      244849 :   if (!equalii(m, n))
     943         462 :     a = grp->pow(E,a,modii(u1,q));
     944      244849 :   if (zetan)
     945             :   {
     946         252 :     *zetan = z;
     947         252 :     gerepileall(ltop,2,&a,zetan);
     948             :   }
     949             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     950      244597 :     a = gerepileupto(ltop, a);
     951      244849 :   return a;
     952             : }
     953             : 
     954             : /*******************************************************************/
     955             : /*                                                                 */
     956             : /*               structure of groups with pairing                  */
     957             : /*                                                                 */
     958             : /*******************************************************************/
     959             : 
     960             : static GEN
     961      146671 : ellgroup_d2(GEN N, GEN d)
     962             : {
     963      146671 :   GEN r = gcdii(N, d);
     964      146671 :   GEN F1 = gel(Z_factor(r), 1);
     965      146671 :   long i, j, l1 = lg(F1);
     966      146671 :   GEN F = cgetg(3, t_MAT);
     967      146671 :   gel(F,1) = cgetg(l1, t_COL);
     968      146671 :   gel(F,2) = cgetg(l1, t_COL);
     969      271747 :   for (i = 1, j = 1; i < l1; ++i)
     970             :   {
     971      125076 :     long v = Z_pval(N, gel(F1, i));
     972      125076 :     if (v<=1) continue;
     973       68558 :     gcoeff(F, j  , 1) = gel(F1, i);
     974       68558 :     gcoeff(F, j++, 2) = stoi(v);
     975             :   }
     976      146671 :   setlg(F[1],j); setlg(F[2],j);
     977      146671 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     978             : }
     979             : 
     980             : GEN
     981      146832 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
     982             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
     983             : {
     984      146832 :   pari_sp av = avma;
     985             :   GEN N0, N1, F;
     986      146832 :   if (pt_m) *pt_m = gen_1;
     987      146832 :   if (is_pm1(N)) return cgetg(1,t_VEC);
     988      146671 :   F = ellgroup_d2(N, d);
     989      146671 :   if (!F) {set_avma(av); return mkveccopy(N);}
     990       65100 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
     991             :   while(1)
     992       53916 :   {
     993      119016 :     pari_sp av2 = avma;
     994             :     GEN P, Q, d, s, t, m;
     995             : 
     996      119016 :     P = grp->pow(E,grp->rand(E), N1);
     997      119016 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {set_avma(av); return mkveccopy(N);}
     998             : 
     999       95709 :     Q = grp->pow(E,grp->rand(E), N1);
    1000       95709 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {set_avma(av); return mkveccopy(N);}
    1001             : 
    1002       84380 :     m = lcmii(s, t);
    1003       84380 :     d = pairorder(E, P, Q, m, F);
    1004             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
    1005       84380 :     if (is_pm1(d) && equalii(m, N0)) {set_avma(av); return mkveccopy(N);}
    1006       84212 :     if (equalii(mulii(m, d), N0))
    1007             :     {
    1008       30296 :       GEN g = mkvec2(mulii(N1,m), d);
    1009       30296 :       if (!pt_m) return gerepilecopy(av, g);
    1010       30296 :       *pt_m = m; return gc_all(av, 2, &g, pt_m);
    1011             :     }
    1012       53916 :     set_avma(av2);
    1013             :   }
    1014             : }
    1015             : 
    1016             : GEN
    1017        2716 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
    1018             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1019             : {
    1020        2716 :   pari_sp ltop = avma, av;
    1021             :   GEN F, d1, dm;
    1022             :   GEN P, Q, d, s;
    1023        2716 :   F = get_arith_ZZM(D1);
    1024        2716 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1025        2716 :   av = avma;
    1026             :   do
    1027             :   {
    1028        6422 :     set_avma(av);
    1029        6422 :     P = grp->rand(E);
    1030        6422 :     s = gen_order(P, F, E, grp);
    1031        6422 :   } while (!equalii(s, d1));
    1032        2716 :   av = avma;
    1033             :   do
    1034             :   {
    1035        5031 :     set_avma(av);
    1036        5031 :     Q = grp->rand(E);
    1037        5031 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1038        5031 :   } while (!equalii(d, d2));
    1039        2716 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1040             : }

Generated by: LCOV version 1.16