Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - bb_group.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20459-9710128) Lines: 518 560 92.5 %
Date: 2017-04-29 05:33:44 Functions: 33 34 97.1 %
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      559552 : int_block(GEN n, long i, long l)
      32             : {
      33      559552 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      34      559675 :   GEN nw = int_W(n, q);
      35      559675 :   ulong w = (ulong) *nw, w2;
      36      559675 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      37       38233 :   w &= (1UL<<r)-1; lr = l-r;
      38       38233 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      39       38233 :   return (w<<lr)|w2;
      40             : }
      41             : 
      42             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      43             : static GEN
      44     5866296 : 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     5866296 :   long i, l = expu(n), u = (1UL<<(e-1));
      49             :   long w, v;
      50     5866638 :   GEN tab = cgetg(1+u, t_VEC);
      51     5866991 :   GEN x2 = sqr(E, x), z = NULL, tw;
      52     5629882 :   gel(tab, 1) = x;
      53     5629882 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      54     5626147 :   av = avma;
      55    52207632 :   while (l>=0)
      56             :   {
      57    40716691 :     if (e > l+1) e = l+1;
      58    40716691 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      59    40886488 :     tw = gel(tab, 1+(w>>(v+1)));
      60    40886488 :     if (z)
      61             :     {
      62    35020968 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
      63    34865915 :       z = mul(E, z, tw);
      64     5865520 :     } else z = tw;
      65    40703389 :     for (i=1; i<=v; i++) z = sqr(E, z);
      66   115673209 :     while (l>=0)
      67             :     {
      68    68898121 :       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    69163068 :       if (n&(1UL<<l)) break;
      74    34279628 :       z = sqr(E, z); l--;
      75             :     }
      76             :   }
      77     5864794 :   return z;
      78             : }
      79             : 
      80             : 
      81             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      82             : static GEN
      83       37318 : 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       37318 :   long i, l = expi(n), u = (1UL<<(e-1));
      88             :   long w, v;
      89       37319 :   GEN tab = cgetg(1+u, t_VEC);
      90       37319 :   GEN x2 = sqr(E, x), z = NULL, tw;
      91       37185 :   gel(tab, 1) = x;
      92       37185 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      93       37171 :   av = avma;
      94      634067 :   while (l>=0)
      95             :   {
      96      559622 :     if (e > l+1) e = l+1;
      97      559622 :     w = int_block(n,l,e); v = vals(w); l-=e;
      98      562767 :     tw = gel(tab, 1+(w>>(v+1)));
      99      562767 :     if (z)
     100             :     {
     101      525447 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
     102      522171 :       z = mul(E, z, tw);
     103       37320 :     } else z = tw;
     104      559626 :     for (i=1; i<=v; i++) z = sqr(E, z);
     105     4540360 :     while (l>=0)
     106             :     {
     107     3943372 :       if (gc_needed(av,1))
     108             :       {
     109          42 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110          42 :         z = gerepilecopy(av, z);
     111             :       }
     112     3943372 :       if (int_bit(n,l)) break;
     113     3422059 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116       37274 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    36370200 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                               GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    36370200 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    36370200 :   if (n == 1) return x;
     129    36370200 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    36370200 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   101352948 :   for (; j; n<<=1,j--)
     134             :   {
     135    64983150 :     y = sqr(E,y);
     136    64982779 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137    64982748 :     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    36369798 :   return y;
     144             : }
     145             : 
     146             : GEN
     147    42289476 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150             :   long l;
     151    42289476 :   if (n == 1) return x;
     152    42235986 :   l = expu(n);
     153    42237008 :   if (l<=8)
     154    36370175 :     return leftright_binary_powu(x, n, E, sqr, mul);
     155             :   else
     156     5866833 :     return sliding_window_powu(x, n, l<=24? 2: 3, E, sqr, mul);
     157             : }
     158             : 
     159             : GEN
     160      937294 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     161             :                                   GEN (*mul)(void*,GEN,GEN))
     162             : {
     163      937294 :   pari_sp av = avma;
     164      937294 :   if (n == 1) return gcopy(x);
     165      910185 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     166             : }
     167             : 
     168             : GEN
     169    14792313 : 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    14792313 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     174       37318 :   l = expi(n);
     175       37319 :   if      (l<=64)  e = 3;
     176       29240 :   else if (l<=160) e = 4;
     177        5181 :   else if (l<=384) e = 5;
     178        1119 :   else if (l<=896) e = 6;
     179         609 :   else             e = 7;
     180       37319 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     181             : }
     182             : 
     183             : GEN
     184      809888 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     185             :                                GEN (*mul)(void*,GEN,GEN))
     186             : {
     187      809888 :   pari_sp av = avma;
     188      809888 :   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      196270 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     194             :                                          GEN (*msqr)(void*,GEN))
     195             : {
     196      196270 :   pari_sp av = avma;
     197             :   GEN y;
     198             :   int j;
     199             : 
     200      196270 :   if (n == 1) return x;
     201      196270 :   y = x; j = 1+bfffo(n);
     202             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     203      196270 :   n<<=j; j = BITS_IN_LONG-j;
     204             :   /* first bit is now implicit */
     205     2640992 :   for (; j; n<<=1,j--)
     206             :   {
     207     2444722 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     208     1911334 :     else y = sqr(E,y);
     209     2444722 :     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      196270 :   return y;
     216             : }
     217             : GEN
     218           0 : gen_powu_fold(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     219             :                                        GEN (*msqr)(void*,GEN))
     220             : {
     221           0 :   pari_sp av = avma;
     222           0 :   if (n == 1) return gcopy(x);
     223           0 :   return gerepilecopy(av, gen_powu_fold_i(x,n,E,sqr,msqr));
     224             : }
     225             : 
     226             : /* assume N != 0, t_INT. Compute x^|N| using left-right binary powering */
     227             : GEN
     228       89162 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     229             :                                       GEN (*msqr)(void*,GEN))
     230             : {
     231       89162 :   long ln = lgefint(N);
     232       89162 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     233             :   else
     234             :   {
     235       34352 :     GEN nd = int_MSW(N), y = x;
     236       34352 :     ulong n = *nd;
     237             :     long i;
     238             :     int j;
     239       34352 :     pari_sp av = avma;
     240             : 
     241       34352 :     if (n == 1)
     242        5538 :       j = 0;
     243             :     else
     244             :     {
     245       28814 :       j = 1+bfffo(n); /* < BIL */
     246             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     247       28814 :       n <<= j; j = BITS_IN_LONG - j;
     248             :     }
     249             :     /* first bit is now implicit */
     250       34352 :     for (i=ln-2;;)
     251             :     {
     252     5649917 :       for (; j; n<<=1,j--)
     253             :       {
     254     5551546 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     255     4774319 :         else y = sqr(E,y);
     256     5550982 :         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       98371 :       if (--i == 0) return y;
     263       64583 :       nd = int_precW(nd);
     264       64583 :       n = *nd; j = BITS_IN_LONG;
     265       64583 :     }
     266             :   }
     267             : }
     268             : GEN
     269       54810 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     270             :                                     GEN (*msqr)(void*,GEN))
     271             : {
     272       54810 :   pari_sp av = avma;
     273       54810 :   return gerepilecopy(av, gen_pow_fold_i(x,n,E,sqr,msqr));
     274             : }
     275             : 
     276             : GEN
     277     3584958 : gen_powers(GEN x, long l, int use_sqr, void *E, GEN (*sqr)(void*,GEN),
     278             :                                       GEN (*mul)(void*,GEN,GEN), GEN (*one)(void*))
     279             : {
     280             :   long i;
     281     3584958 :   GEN V = cgetg(l+2,t_VEC);
     282     3584958 :   gel(V,1) = one(E); if (l==0) return V;
     283     3576028 :   gel(V,2) = gcopy(x); if (l==1) return V;
     284     2416170 :   gel(V,3) = sqr(E,x);
     285     2416170 :   if (use_sqr)
     286     7950989 :     for(i = 4; i < l+2; i++)
     287    15363790 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     288     9098092 :                       : mul(E,gel(V, i-1),x);
     289             :   else
     290     1858026 :     for(i = 4; i < l+2; i++)
     291     1127146 :       gel(V,i) = mul(E,gel(V,i-1),x);
     292     2416169 :   return V;
     293             : }
     294             : 
     295             : GEN
     296    21133475 : producttree_scheme(long n)
     297             : {
     298             :   GEN v, w;
     299             :   long i, j, k, u, l;
     300    21133475 :   if (n<=2) return mkvecsmall(n);
     301    20206472 :   u = expu(n-1);
     302    20206477 :   v = cgetg(n+1,t_VECSMALL);
     303    20206474 :   w = cgetg(n+1,t_VECSMALL);
     304    20206610 :   v[1] = n; l = 1;
     305    85561951 :   for (i=1; i<=u; i++)
     306             :   {
     307   255225826 :     for(j=1, k=1; j<=l; j++, k+=2)
     308             :     {
     309   189870485 :       long vj = v[j], v2 = vj>>1;
     310   189870485 :       w[k]    = vj-v2;
     311   189870485 :       w[k+1]  = v2;
     312             :     }
     313    65355341 :     swap(v,w); l<<=1;
     314             :   }
     315    20206610 :   fixlg(v, l+1);
     316    20206535 :   avma = (pari_sp) v;
     317    20206535 :   return v;
     318             : }
     319             : 
     320             : GEN
     321    21270633 : gen_product(GEN x, void *data, GEN (*mul)(void *,GEN,GEN))
     322             : {
     323             :   pari_sp ltop;
     324    21270633 :   long i,k,lx = lg(x),lv;
     325             :   pari_timer ti;
     326             :   GEN v;
     327    21270633 :   if (DEBUGLEVEL>7) timer_start(&ti);
     328             : 
     329    21270671 :   if (lx == 1) return gen_1;
     330    21270101 :   if (lx == 2) return gcopy(gel(x,1));
     331    21102348 :   x = leafcopy(x); k = lx;
     332    21102541 :   v = producttree_scheme(lx-1); lv = lg(v);
     333    21102851 :   ltop = avma;
     334   231687017 :   for (k=1, i=1; k<lv; i+=v[k++])
     335   210584604 :     gel(x,k) = v[k]==1 ? gel(x,i): mul(data,gel(x,i),gel(x,i+1));
     336   107460220 :   while (k > 2)
     337             :   {
     338    65255372 :     if (DEBUGLEVEL>7)
     339           0 :       timer_printf(&ti,"gen_product: remaining objects %ld",k-1);
     340    65255851 :     lx = k; k = 1;
     341   254734370 :     for (i=1; i<lx-1; i+=2)
     342   189479084 :       gel(x,k++) = mul(data,gel(x,i),gel(x,i+1));
     343    65255286 :     if (i < lx) gel(x,k++) = gel(x,i);
     344    65255286 :     if (gc_needed(ltop,1))
     345           6 :       gerepilecoeffs(ltop,x+1,k-1);
     346             :   }
     347    21102435 :   return gel(x,1);
     348             : }
     349             : 
     350             : /***********************************************************************/
     351             : /**                                                                   **/
     352             : /**                    DISCRETE LOGARITHM                             **/
     353             : /**                                                                   **/
     354             : /***********************************************************************/
     355             : 
     356             : static GEN
     357    33983709 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     358             : {
     359    33983709 :   GEN a = gel(A,1);
     360    33983709 :   switch((h|grp->hash(a))%3UL)
     361             :   {
     362             :     case 0:
     363    11336955 :       return mkvec3(grp->pow(E,a,gen_2),Fp_mulu(gel(A,2),2,q),
     364    11336955 :                                         Fp_mulu(gel(A,3),2,q));
     365             :     case 1:
     366    11320867 :       return mkvec3(grp->mul(E,a,x),addis(gel(A,2),1),gel(A,3));
     367             :     case 2:
     368    11325887 :       return mkvec3(grp->mul(E,a,g),gel(A,2),addiu(gel(A,3),1));
     369             :   }
     370           0 :   return NULL;
     371             : }
     372             : 
     373             : /*Generic Pollard rho discrete log algorithm*/
     374             : static GEN
     375          42 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     376             : {
     377          42 :   pari_sp av=avma;
     378          42 :   GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
     379          42 :   ulong i, h = 0, imax = itou_or_0(sqrt4q);
     380          42 :   if (!imax) imax = ULONG_MAX;
     381             :   do {
     382             :  rho_restart:
     383          42 :     A = B = mkvec3(x,gen_1,gen_0);
     384          42 :     i=0;
     385             :     do {
     386    11327903 :       if (i>imax)
     387             :       {
     388           0 :         h++;
     389           0 :         if (DEBUGLEVEL)
     390           0 :           pari_warn(warner,"changing Pollard rho hash seed to %ld",h);
     391           0 :         goto rho_restart;
     392             :       }
     393    11327903 :       A = iter_rho(x, g, q, A, h, E, grp);
     394    11327903 :       B = iter_rho(x, g, q, B, h, E, grp);
     395    11327903 :       B = iter_rho(x, g, q, B, h, E, grp);
     396    11327903 :       if (gc_needed(av,2))
     397             :       {
     398         990 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     399         990 :         gerepileall(av, 2, &A, &B);
     400             :       }
     401    11327903 :       i++;
     402    11327903 :     } while (!grp->equal(gel(A,1), gel(B,1)));
     403          42 :     gel(A,2) = modii(gel(A,2), q);
     404          42 :     gel(B,2) = modii(gel(B,2), q);
     405          42 :     h++;
     406          42 :   } while (equalii(gel(A,2), gel(B,2)));
     407          42 :   l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
     408          42 :   return gerepileuptoint(av, l);
     409             : }
     410             : 
     411             : /* compute a hash of g^(i-1), 1<=i<=n. Return [sorted hash, perm, g^-n] */
     412             : GEN
     413     1870860 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
     414             : {
     415     1870860 :   GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
     416     1870860 :   pari_sp av=avma;
     417             :   long i;
     418     1870860 :   table[1] = grp->hash(grp->pow(E,g,gen_0));
     419     9433092 :   for (i=2; i<=n; i++)
     420             :   {
     421     7562232 :     table[i] = grp->hash(p1);
     422     7562232 :     p1 = grp->mul(E,p1,g);
     423     7562232 :     if (gc_needed(av,2))
     424             :     {
     425           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     426           0 :       p1 = gerepileupto(av, p1);
     427             :     }
     428             :   }
     429     1870860 :   G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
     430     1870860 :   perm = vecsmall_indexsort(table);
     431     1870860 :   table = vecsmallpermute(table,perm);
     432     1870860 :   return mkvec4(table,perm,g,G);
     433             : }
     434             : /* T from gen_Shanks_init(g,n). Return v < n*N such that x = g^v or NULL */
     435             : GEN
     436     1871244 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
     437             : {
     438     1871244 :   pari_sp av=avma;
     439     1871244 :   GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
     440     1871244 :   GEN p1 = x;
     441     1871244 :   long n = lg(table)-1;
     442             :   ulong k;
     443    10500564 :   for (k=0; k<N; k++)
     444             :   { /* p1 = x G^k, G = g^-n */
     445    10293834 :     long h = grp->hash(p1), i = zv_search(table, h);
     446    10293834 :     if (i)
     447             :     {
     448     1665162 :       do i--; while (i && table[i] == h);
     449     1664514 :       for (i++; i <= n && table[i] == h; i++)
     450             :       {
     451     1664514 :         GEN v = addiu(muluu(n,k), perm[i]-1);
     452     1664514 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     453           0 :         if (DEBUGLEVEL)
     454           0 :           err_printf("gen_Shanks_log: false positive %lu, %lu\n", k,h);
     455             :       }
     456             :     }
     457     8629320 :     p1 = grp->mul(E,p1,G);
     458     8629320 :     if (gc_needed(av,2))
     459             :     {
     460           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %lu", k);
     461           0 :       p1 = gerepileupto(av, p1);
     462             :     }
     463             :   }
     464      206730 :   return NULL;
     465             : }
     466             : /* Generic Shanks baby-step/giant-step algorithm. Return log_g(x), ord g = q.
     467             :  * One-shot: use gen_Shanks_init/log if many logs are desired; early abort
     468             :  * if log < sqrt(q) */
     469             : static GEN
     470       79667 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     471             : {
     472       79667 :   pari_sp av=avma, av1;
     473             :   long lbaby, i, k;
     474             :   GEN p1, table, giant, perm, ginv;
     475       79667 :   p1 = sqrti(q);
     476       79667 :   if (abscmpiu(p1,LGBITS) >= 0)
     477           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     478       79667 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     479       79667 :   ginv = grp->pow(E,g,gen_m1);
     480       79667 :   av1 = avma;
     481      768629 :   for (p1=x, i=1;;i++)
     482             :   {
     483      768629 :     if (grp->equal1(p1)) { avma = av; return stoi(i-1); }
     484      760357 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     485      688962 :     p1 = grp->mul(E,p1,ginv);
     486      688962 :     if (gc_needed(av1,2))
     487             :     {
     488           6 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     489           6 :       p1 = gerepileupto(av1, p1);
     490             :     }
     491      688962 :   }
     492       71395 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     493       71395 :   perm = vecsmall_indexsort(table);
     494       71395 :   table = vecsmallpermute(table,perm);
     495       71395 :   av1 = avma;
     496      480032 :   for (k=1; k<= lbaby; k++)
     497             :   {
     498      480021 :     long h = grp->hash(p1), i = zv_search(table, h);
     499      480021 :     if (i)
     500             :     {
     501       71384 :       while (table[i] == h && i) i--;
     502       71384 :       for (i++; i <= lbaby && table[i] == h; i++)
     503             :       {
     504       71384 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     505       71384 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     506           0 :         if (DEBUGLEVEL)
     507           0 :           err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
     508             :       }
     509             :     }
     510      408637 :     p1 = grp->mul(E,p1,giant);
     511      408637 :     if (gc_needed(av1,2))
     512             :     {
     513           6 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %ld", k);
     514           6 :       p1 = gerepileupto(av1, p1);
     515             :     }
     516             :   }
     517          11 :   avma = av; return cgetg(1, t_VEC); /* no solution */
     518             : }
     519             : 
     520             : /*Generic discrete logarithme in a group of prime order p*/
     521             : GEN
     522      382388 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     523             : {
     524      382388 :   if (grp->easylog)
     525             :   {
     526      351029 :     GEN e = grp->easylog(E, x, g, p);
     527      351029 :     if (e) return e;
     528             :   }
     529      103030 :   if (grp->equal1(x)) return gen_0;
     530      102994 :   if (grp->equal(x,g)) return gen_1;
     531       79709 :   if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
     532          42 :   return gen_Pollard_log(x, g, p, E, grp);
     533             : }
     534             : 
     535             : GEN
     536     4615364 : get_arith_ZZM(GEN o)
     537             : {
     538     4615364 :   if (!o) return NULL;
     539     4615364 :   switch(typ(o))
     540             :   {
     541             :     case t_INT:
     542     3261150 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     543           6 :       break;
     544             :     case t_MAT:
     545      973346 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     546          12 :       break;
     547             :     case t_VEC:
     548      380862 :       if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
     549           0 :       break;
     550             :   }
     551          24 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     552             :   return NULL; /* LCOV_EXCL_LINE */
     553             : }
     554             : GEN
     555       62671 : get_arith_Z(GEN o)
     556             : {
     557       62671 :   if (!o) return NULL;
     558       62671 :   switch(typ(o))
     559             :   {
     560             :     case t_INT:
     561       42757 :       if (signe(o) > 0) return o;
     562           6 :       break;
     563             :     case t_MAT:
     564          12 :       o = factorback(o);
     565           0 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     566           0 :       break;
     567             :     case t_VEC:
     568       19896 :       if (lg(o) != 3) break;
     569       19896 :       o = gel(o,1);
     570       19896 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     571           0 :       break;
     572             :   }
     573          12 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     574             :   return NULL; /* LCOV_EXCL_LINE */
     575             : }
     576             : 
     577             : /* grp->easylog() is an optional trapdoor function that catch easy logarithms*/
     578             : /* Generic Pohlig-Hellman discrete logarithm*/
     579             : /* smallest integer n such that g^n=a. Assume g has order ord */
     580             : GEN
     581      171974 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     582             : {
     583      171974 :   pari_sp av = avma;
     584             :   GEN v,t0,a0,b,q,g_q,n_q,ginv0,qj,ginv;
     585             :   GEN fa, ex;
     586             :   long e,i,j,l;
     587             : 
     588      171974 :   if (grp->equal(g, a)) /* frequent special case */
     589       31412 :     return grp->equal1(g)? gen_0: gen_1;
     590      140562 :   if (grp->easylog)
     591             :   {
     592      140472 :     GEN e = grp->easylog(E, a, g, ord);
     593      140448 :     if (e) return e;
     594             :   }
     595       86379 :   v = get_arith_ZZM(ord);
     596       86379 :   ord= gel(v,1);
     597       86379 :   fa = gel(v,2);
     598       86379 :   ex = gel(fa,2);
     599       86379 :   fa = gel(fa,1); l = lg(fa);
     600       86379 :   ginv = grp->pow(E,g,gen_m1);
     601       86379 :   v = cgetg(l, t_VEC);
     602      229068 :   for (i=1; i<l; i++)
     603             :   {
     604      142989 :     q = gel(fa,i);
     605      142989 :     e = itos(gel(ex,i));
     606      142989 :     if (DEBUGLEVEL>5)
     607           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     608      142989 :     qj = new_chunk(e+1);
     609      142989 :     gel(qj,0) = gen_1;
     610      142989 :     gel(qj,1) = q;
     611      142989 :     for (j=2; j<=e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     612      142989 :     t0 = diviiexact(ord, gel(qj,e));
     613      142989 :     a0 = grp->pow(E, a, t0);
     614      142989 :     ginv0 = grp->pow(E, ginv, t0); /* order q^e */
     615      142989 :     if (grp->equal1(ginv0))
     616             :     {
     617          12 :       gel(v,i) = mkintmod(gen_0, gen_1);
     618          12 :       continue;
     619             :     }
     620      142983 :     do { g_q = grp->pow(E,g, mulii(t0, gel(qj,--e))); /* order q */
     621      142983 :     } while (grp->equal1(g_q));
     622      142977 :     n_q = gen_0;
     623      219741 :     for (j=0;; j++)
     624             :     { /* n_q = sum_{i<j} b_i q^i */
     625      219741 :       b = grp->pow(E,a0, gel(qj,e-j));
     626             :       /* early abort: cheap and very effective */
     627      219741 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     628         289 :         avma = av; return cgetg(1, t_VEC);
     629             :       }
     630      219452 :       b = gen_plog(b, g_q, q, E, grp);
     631      219452 :       if (typ(b) != t_INT) { avma = av; return cgetg(1, t_VEC); }
     632      219441 :       n_q = addii(n_q, mulii(b, gel(qj,j)));
     633      219441 :       if (j == e) break;
     634             : 
     635       76764 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     636       76764 :       ginv0 = grp->pow(E,ginv0, q);
     637       76764 :     }
     638      142677 :     gel(v,i) = mkintmod(n_q, gel(qj,e+1));
     639             :   }
     640       86079 :   return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
     641             : }
     642             : 
     643             : /***********************************************************************/
     644             : /**                                                                   **/
     645             : /**                    ORDER OF AN ELEMENT                            **/
     646             : /**                                                                   **/
     647             : /***********************************************************************/
     648             : /*Find the exact order of a assuming a^o==1*/
     649             : GEN
     650     2335005 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     651             : {
     652     2335005 :   pari_sp av = avma;
     653             :   long i, l;
     654             :   GEN m;
     655             : 
     656     2335005 :   m = get_arith_ZZM(o);
     657     2335005 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     658     2335005 :   o = gel(m,1);
     659     2335005 :   m = gel(m,2); l = lgcols(m);
     660     7331686 :   for (i = l-1; i; i--)
     661             :   {
     662     4996681 :     GEN t, y, p = gcoeff(m,i,1);
     663     4996681 :     long j, e = itos(gcoeff(m,i,2));
     664     4996681 :     if (l == 2) {
     665      480732 :       t = gen_1;
     666      480732 :       y = a;
     667             :     } else {
     668     4515949 :       t = diviiexact(o, powiu(p,e));
     669     4515949 :       y = grp->pow(E, a, t);
     670             :     }
     671     4996681 :     if (grp->equal1(y)) o = t;
     672             :     else {
     673     4635113 :       for (j = 1; j < e; j++)
     674             :       {
     675     1575251 :         y = grp->pow(E, y, p);
     676     1575251 :         if (grp->equal1(y)) break;
     677             :       }
     678     3447181 :       if (j < e) {
     679      387319 :         if (j > 1) p = powiu(p, j);
     680      387319 :         o = mulii(t, p);
     681             :       }
     682             :     }
     683             :   }
     684     2335005 :   return gerepilecopy(av, o);
     685             : }
     686             : 
     687             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     688             : GEN
     689     1928340 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     690             : {
     691     1928340 :   pari_sp av = avma;
     692             :   long i, l, ind;
     693             :   GEN m, F, P;
     694             : 
     695     1928340 :   m = get_arith_ZZM(o);
     696     1928340 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     697     1928340 :   o = gel(m,1);
     698     1928340 :   m = gel(m,2); l = lgcols(m);
     699     1928340 :   P = cgetg(l, t_COL); ind = 1;
     700     1928340 :   F = cgetg(l, t_COL);
     701     4633482 :   for (i = l-1; i; i--)
     702             :   {
     703     2705142 :     GEN t, y, p = gcoeff(m,i,1);
     704     2705142 :     long j, e = itos(gcoeff(m,i,2));
     705     2705142 :     if (l == 2) {
     706     1182660 :       t = gen_1;
     707     1182660 :       y = a;
     708             :     } else {
     709     1522482 :       t = diviiexact(o, powiu(p,e));
     710     1522482 :       y = grp->pow(E, a, t);
     711             :     }
     712     2705142 :     if (grp->equal1(y)) o = t;
     713             :     else {
     714     3392550 :       for (j = 1; j < e; j++)
     715             :       {
     716      721356 :         y = grp->pow(E, y, p);
     717      721356 :         if (grp->equal1(y)) break;
     718             :       }
     719     2680260 :       gel(P,ind) = p;
     720     2680260 :       gel(F,ind) = utoipos(j);
     721     2680260 :       if (j < e) {
     722        9066 :         if (j > 1) p = powiu(p, j);
     723        9066 :         o = mulii(t, p);
     724             :       }
     725     2680260 :       ind++;
     726             :     }
     727             :   }
     728     1928340 :   setlg(P, ind); P = vecreverse(P);
     729     1928340 :   setlg(F, ind); F = vecreverse(F);
     730     1928340 :   return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
     731             : }
     732             : 
     733             : /* E has order o[1], ..., or o[#o], draw random points until all solutions
     734             :  * but one are eliminated */
     735             : GEN
     736         756 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     737             : {
     738         756 :   pari_sp ltop = avma, btop;
     739             :   GEN lastgood, so, vo;
     740         756 :   long lo = lg(o), nbo=lo-1;
     741         756 :   if (nbo == 1) return icopy(gel(o,1));
     742         354 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     743         354 :   vo = zero_zv(lo);
     744         354 :   lastgood = gel(o, so[nbo]);
     745         354 :   btop = avma;
     746             :   for(;;)
     747             :   {
     748         354 :     GEN lasto = gen_0;
     749         354 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     750             :     long i;
     751         444 :     for (i = 1; i < lo; i++)
     752             :     {
     753         444 :       GEN newo = gel(o, so[i]);
     754         444 :       if (vo[i]) continue;
     755         444 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     756         444 :       lasto = newo;
     757         444 :       if (!grp->equal1(t))
     758             :       {
     759         390 :         if (--nbo == 1) { avma=ltop; return icopy(lastgood); }
     760          36 :         vo[i] = 1;
     761             :       }
     762             :       else
     763          54 :         lastgood = lasto;
     764             :     }
     765           0 :     avma = btop;
     766           0 :   }
     767             : }
     768             : 
     769             : /*******************************************************************/
     770             : /*                                                                 */
     771             : /*                          n-th ROOT                              */
     772             : /*                                                                 */
     773             : /*******************************************************************/
     774             : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
     775             :  * of order l.
     776             :  *
     777             :  * q = l^e*r, e>=1, (r,l)=1
     778             :  * UNCLEAN */
     779             : static GEN
     780      248861 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     781             : {
     782      248861 :   const pari_sp av1 = avma;
     783             :   GEN m, m1;
     784             :   long i;
     785      189098 :   for (;; avma = av1)
     786             :   {
     787      437959 :     m1 = m = grp->pow(E, grp->rand(E), r);
     788      437960 :     if (grp->equal1(m)) continue;
     789      781630 :     for (i=1; i<e; i++)
     790             :     {
     791      532769 :       m = grp->pow(E,m,l);
     792      532768 :       if (grp->equal1(m)) break;
     793             :     }
     794      354493 :     if (i==e) break;
     795      189098 :   }
     796      248862 :   *zeta = m; return m1;
     797             : }
     798             : 
     799             : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
     800             : 
     801             : GEN
     802       12576 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     803             : {
     804       12576 :   pari_sp ltop = avma, av;
     805             :   long i, lpr;
     806       12576 :   GEN F, N, pr, z=NULL;
     807       12576 :   F = get_arith_ZZM(o);
     808       12576 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     809       12576 :   av = avma;
     810             : 
     811       41826 :   for (i = 1; i < lpr; i++)
     812             :   {
     813       29250 :     GEN l = gcoeff(pr,i,1);
     814       29250 :     long e = itos(gcoeff(pr,i,2));
     815       29250 :     GEN r = diviiexact(N,powis(l,e));
     816       29250 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     817       29250 :     z = i==1 ? zl: grp->mul(E,z,zl);
     818       29250 :     if (gc_needed(av,2))
     819             :     { /* n can have lots of prime factors*/
     820           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
     821           0 :       z = gerepileupto(av, z);
     822             :     }
     823             :   }
     824       12576 :   return gerepileupto(ltop, z);
     825             : }
     826             : 
     827             : /* solve x^l = a , l prime in G of order q.
     828             :  *
     829             :  * q =  (l^e)*r, e >= 1, (r,l) = 1
     830             :  * y is not an l-th power, hence generates the l-Sylow of G
     831             :  * m = y^(q/l) != 1 */
     832             : static GEN
     833      219504 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
     834             :                  const struct bb_group *grp)
     835             : {
     836      219504 :   pari_sp av = avma;
     837             :   long k;
     838             :   GEN p1, u1, u2, v, w, z, dl;
     839             : 
     840      219504 :   (void)bezout(r,l,&u1,&u2);
     841      219504 :   v = grp->pow(E,a,u2);
     842      219504 :   w = grp->pow(E,v,l);
     843      219504 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     844      601918 :   while (!grp->equal1(w))
     845             :   {
     846      167586 :     k = 0;
     847      167586 :     p1 = w;
     848             :     do
     849             :     {
     850      285523 :       z = p1; p1 = grp->pow(E,p1,l);
     851      285524 :       k++;
     852      285524 :     } while(!grp->equal1(p1));
     853      167587 :     if (k==e) { avma = av; return NULL; }
     854      162936 :     dl = gen_plog(z,m,l,E,grp);
     855      162936 :     if (typ(dl) != t_INT) { avma = av; return NULL; }
     856      162936 :     dl = negi(dl);
     857      162936 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     858      162936 :     m = grp->pow(E,m,dl);
     859      162936 :     e = k;
     860      162936 :     v = grp->mul(E,p1,v);
     861      162936 :     y = grp->pow(E,p1,l);
     862      162936 :     w = grp->mul(E,y,w);
     863      162936 :     if (gc_needed(av,1))
     864             :     {
     865           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
     866           0 :       gerepileall(av,4, &y,&v,&w,&m);
     867             :     }
     868             :   }
     869      214841 :   return gerepilecopy(av, v);
     870             : }
     871             : /* Return one solution of x^n = a in a cyclic group of order q
     872             :  *
     873             :  * 1) If there is no solution, return NULL.
     874             :  *
     875             :  * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
     876             :  * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
     877             :  * the set of solutions is { x*zetan^k; k=0..m-1 }
     878             :  */
     879             : GEN
     880      234509 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     881             : {
     882      234509 :   pari_sp ltop = avma;
     883             :   GEN m, u1, u2, z;
     884             :   int is_1;
     885             : 
     886      234509 :   if (is_pm1(n))
     887             :   {
     888           0 :     if (zetan) *zetan = grp->pow(E,a,gen_0);
     889           0 :     return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
     890             :   }
     891      234510 :   is_1 = grp->equal1(a);
     892      234509 :   if (is_1 && !zetan) return gcopy(a);
     893             : 
     894      225083 :   m = bezout(n,q,&u1,&u2);
     895      225083 :   z = grp->pow(E,a,gen_0);
     896      225083 :   if (!is_pm1(m))
     897             :   {
     898      219551 :     GEN F = Z_factor(m);
     899             :     long i, j, e;
     900             :     GEN r, zeta, y, l;
     901      219548 :     pari_sp av1 = avma;
     902      434497 :     for (i = nbrows(F); i; i--)
     903             :     {
     904      219608 :       l = gcoeff(F,i,1);
     905      219608 :       j = itos(gcoeff(F,i,2));
     906      219611 :       e = Z_pvalrem(q,l,&r);
     907      219610 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     908      219612 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     909      219612 :       if (!is_1) {
     910             :         do
     911             :         {
     912      219504 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     913      219492 :           if (!a) { avma = ltop; return NULL;}
     914      214841 :         } while (--j);
     915             :       }
     916      214949 :       if (gc_needed(ltop,1))
     917             :       { /* n can have lots of prime factors*/
     918           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
     919           0 :         gerepileall(av1, zetan? 2: 1, &a, &z);
     920             :       }
     921             :     }
     922             :   }
     923      220421 :   if (!equalii(m, n))
     924        5550 :     a = grp->pow(E,a,modii(u1,q));
     925      220421 :   if (zetan)
     926             :   {
     927         468 :     *zetan = z;
     928         468 :     gerepileall(ltop,2,&a,zetan);
     929             :   }
     930             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     931      219953 :     a = gerepileupto(ltop, a);
     932      220421 :   return a;
     933             : }
     934             : 
     935             : /*******************************************************************/
     936             : /*                                                                 */
     937             : /*               structure of groups with pairing                  */
     938             : /*                                                                 */
     939             : /*******************************************************************/
     940             : 
     941             : static GEN
     942       33930 : ellgroup_d2(GEN N, GEN d)
     943             : {
     944       33930 :   GEN r = gcdii(N, d);
     945       33930 :   GEN F1 = gel(Z_factor(r), 1);
     946       33930 :   long i, j, l1 = lg(F1);
     947       33930 :   GEN F = cgetg(3, t_MAT);
     948       33930 :   gel(F,1) = cgetg(l1, t_COL);
     949       33930 :   gel(F,2) = cgetg(l1, t_COL);
     950       59688 :   for (i = 1, j = 1; i < l1; ++i)
     951             :   {
     952       25758 :     long v = Z_pval(N, gel(F1, i));
     953       25758 :     if (v<=1) continue;
     954       13254 :     gcoeff(F, j  , 1) = gel(F1, i);
     955       13254 :     gcoeff(F, j++, 2) = stoi(v);
     956             :   }
     957       33930 :   setlg(F[1],j); setlg(F[2],j);
     958       33930 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     959             : }
     960             : 
     961             : GEN
     962       33996 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
     963             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
     964             : {
     965       33996 :   pari_sp av = avma;
     966             :   GEN N0, N1, F;
     967       33996 :   if (pt_m) *pt_m = gen_1;
     968       33996 :   if (is_pm1(N)) return cgetg(1,t_VEC);
     969       33930 :   F = ellgroup_d2(N, d);
     970       33930 :   if (!F) {avma = av; return mkveccopy(N);}
     971       12654 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
     972             :   while(1)
     973             :   {
     974       22236 :     pari_sp av2 = avma;
     975             :     GEN P, Q, d, s, t, m;
     976             : 
     977       22236 :     P = grp->pow(E,grp->rand(E), N1);
     978       22236 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {avma = av; return mkveccopy(N);}
     979             : 
     980       17301 :     Q = grp->pow(E,grp->rand(E), N1);
     981       17301 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {avma = av; return mkveccopy(N);}
     982             : 
     983       15084 :     m = lcmii(s, t);
     984       15084 :     d = pairorder(E, P, Q, m, F);
     985             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
     986       15084 :     if (is_pm1(d) && equalii(m, N0)) {avma = av; return mkveccopy(N);}
     987       15060 :     if (equalii(mulii(m, d), N0))
     988             :     {
     989        5478 :       GEN g = mkvec2(mulii(N1,m), d);
     990        5478 :       if (pt_m) *pt_m = m;
     991        5478 :       gerepileall(av,pt_m?2:1,&g,pt_m);
     992        5478 :       return g;
     993             :     }
     994        9582 :     avma = av2;
     995        9582 :   }
     996             : }
     997             : 
     998             : GEN
     999        2286 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
    1000             :              GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
    1001             : {
    1002        2286 :   pari_sp ltop = avma, av;
    1003             :   GEN F, d1, dm;
    1004             :   GEN P, Q, d, s;
    1005        2286 :   F = get_arith_ZZM(D1);
    1006        2286 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1007        2286 :   av = avma;
    1008             :   do
    1009             :   {
    1010        6028 :     avma = av;
    1011        6028 :     P = grp->rand(E);
    1012        6028 :     s = gen_order(P, F, E, grp);
    1013        6028 :   } while (!equalii(s, d1));
    1014        2286 :   av = avma;
    1015             :   do
    1016             :   {
    1017        4502 :     avma = av;
    1018        4502 :     Q = grp->rand(E);
    1019        4502 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1020        4502 :   } while (!equalii(d, d2));
    1021        2286 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1022             : }

Generated by: LCOV version 1.11