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 21741-70cf009) Lines: 519 560 92.7 %
Date: 2018-01-21 06:18:30 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     3573160 : int_block(GEN n, long i, long l)
      32             : {
      33     3573160 :   long q = divsBIL(i), r = remsBIL(i)+1, lr;
      34     3573282 :   GEN nw = int_W(n, q);
      35     3573282 :   ulong w = (ulong) *nw, w2;
      36     3573282 :   if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
      37      329295 :   w &= (1UL<<r)-1; lr = l-r;
      38      329295 :   w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
      39      329295 :   return (w<<lr)|w2;
      40             : }
      41             : 
      42             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      43             : static GEN
      44     7037383 : 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     7037383 :   long i, l = expu(n), u = (1UL<<(e-1));
      49             :   long w, v;
      50     7037712 :   GEN tab = cgetg(1+u, t_VEC);
      51     7037828 :   GEN x2 = sqr(E, x), z = NULL, tw;
      52     6783207 :   gel(tab, 1) = x;
      53     6783207 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      54     6780010 :   av = avma;
      55    62914240 :   while (l>=0)
      56             :   {
      57    49098557 :     if (e > l+1) e = l+1;
      58    49098557 :     w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
      59    49295032 :     tw = gel(tab, 1+(w>>(v+1)));
      60    49295032 :     if (z)
      61             :     {
      62    42258656 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
      63    42077174 :       z = mul(E, z, tw);
      64     7036376 :     } else z = tw;
      65    49085688 :     for (i=1; i<=v; i++) z = sqr(E, z);
      66   139718890 :     while (l>=0)
      67             :     {
      68    83377473 :       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    83662368 :       if (n&(1UL<<l)) break;
      74    41566218 :       z = sqr(E, z); l--;
      75             :     }
      76             :   }
      77     7035673 :   return z;
      78             : }
      79             : 
      80             : 
      81             : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
      82             : static GEN
      83       86523 : 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       86523 :   long i, l = expi(n), u = (1UL<<(e-1));
      88             :   long w, v;
      89       86530 :   GEN tab = cgetg(1+u, t_VEC);
      90       86521 :   GEN x2 = sqr(E, x), z = NULL, tw;
      91       87017 :   gel(tab, 1) = x;
      92       87017 :   for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
      93       87005 :   av = avma;
      94     3746725 :   while (l>=0)
      95             :   {
      96     3573218 :     if (e > l+1) e = l+1;
      97     3573218 :     w = int_block(n,l,e); v = vals(w); l-=e;
      98     3574930 :     tw = gel(tab, 1+(w>>(v+1)));
      99     3574930 :     if (z)
     100             :     {
     101     3488401 :       for (i=1; i<=e-v; i++) z = sqr(E, z);
     102     3486563 :       z = mul(E, z, tw);
     103       86529 :     } else z = tw;
     104     3573080 :     for (i=1; i<=v; i++) z = sqr(E, z);
     105    14662948 :     while (l>=0)
     106             :     {
     107    11003862 :       if (gc_needed(av,1))
     108             :       {
     109         381 :         if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
     110         381 :         z = gerepilecopy(av, z);
     111             :       }
     112    11003862 :       if (int_bit(n,l)) break;
     113     7520466 :       z = sqr(E, z); l--;
     114             :     }
     115             :   }
     116       86502 :   return z;
     117             : }
     118             : 
     119             : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
     120             : static GEN
     121    51625733 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     122             :                                               GEN (*mul)(void*,GEN,GEN))
     123             : {
     124    51625733 :   pari_sp av = avma;
     125             :   GEN  y;
     126             :   int j;
     127             : 
     128    51625733 :   if (n == 1) return x;
     129    51625733 :   y = x; j = 1+bfffo(n);
     130             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     131    51625733 :   n<<=j; j = BITS_IN_LONG-j;
     132             :   /* first bit is now implicit */
     133   142094575 :   for (; j; n<<=1,j--)
     134             :   {
     135    90469681 :     y = sqr(E,y);
     136    90469258 :     if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
     137    90468842 :     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    51624894 :   return y;
     144             : }
     145             : 
     146             : GEN
     147    58751234 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     148             :                                     GEN (*mul)(void*,GEN,GEN))
     149             : {
     150             :   long l;
     151    58751234 :   if (n == 1) return x;
     152    58662154 :   l = expu(n);
     153    58663400 :   if (l<=8)
     154    51625301 :     return leftright_binary_powu(x, n, E, sqr, mul);
     155             :   else
     156     7038099 :     return sliding_window_powu(x, n, l<=24? 2: 3, E, sqr, mul);
     157             : }
     158             : 
     159             : GEN
     160     1122684 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
     161             :                                   GEN (*mul)(void*,GEN,GEN))
     162             : {
     163     1122684 :   pari_sp av = avma;
     164     1122684 :   if (n == 1) return gcopy(x);
     165     1083233 :   return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
     166             : }
     167             : 
     168             : GEN
     169    22716386 : 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    22716386 :   if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
     174       86528 :   l = expi(n);
     175       86532 :   if      (l<=64)  e = 3;
     176       57373 :   else if (l<=160) e = 4;
     177       27074 :   else if (l<=384) e = 5;
     178       20531 :   else if (l<=896) e = 6;
     179       13132 :   else             e = 7;
     180       86532 :   return sliding_window_pow(x, n, e, E, sqr, mul);
     181             : }
     182             : 
     183             : GEN
     184     1029806 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     185             :                                GEN (*mul)(void*,GEN,GEN))
     186             : {
     187     1029806 :   pari_sp av = avma;
     188     1029806 :   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      270763 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN  (*sqr)(void*,GEN),
     194             :                                          GEN (*msqr)(void*,GEN))
     195             : {
     196      270763 :   pari_sp av = avma;
     197             :   GEN y;
     198             :   int j;
     199             : 
     200      270763 :   if (n == 1) return x;
     201      270763 :   y = x; j = 1+bfffo(n);
     202             :   /* normalize, i.e set highest bit to 1 (we know n != 0) */
     203      270763 :   n<<=j; j = BITS_IN_LONG-j;
     204             :   /* first bit is now implicit */
     205     2988790 :   for (; j; n<<=1,j--)
     206             :   {
     207     2718027 :     if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     208     2102683 :     else y = sqr(E,y);
     209     2718027 :     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      270763 :   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      197143 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
     229             :                                       GEN (*msqr)(void*,GEN))
     230             : {
     231      197143 :   long ln = lgefint(N);
     232      197143 :   if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
     233             :   else
     234             :   {
     235       88636 :     GEN nd = int_MSW(N), y = x;
     236       88636 :     ulong n = *nd;
     237             :     long i;
     238             :     int j;
     239       88636 :     pari_sp av = avma;
     240             : 
     241       88636 :     if (n == 1)
     242        6928 :       j = 0;
     243             :     else
     244             :     {
     245       81708 :       j = 1+bfffo(n); /* < BIL */
     246             :       /* normalize, i.e set highest bit to 1 (we know n != 0) */
     247       81708 :       n <<= j; j = BITS_IN_LONG - j;
     248             :     }
     249             :     /* first bit is now implicit */
     250       88636 :     for (i=ln-2;;)
     251             :     {
     252    25312546 :       for (; j; n<<=1,j--)
     253             :       {
     254    24812404 :         if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
     255    15290280 :         else y = sqr(E,y);
     256    24811828 :         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      500142 :       if (--i == 0) return y;
     263      412082 :       nd = int_precW(nd);
     264      412082 :       n = *nd; j = BITS_IN_LONG;
     265      412082 :     }
     266             :   }
     267             : }
     268             : GEN
     269      108508 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
     270             :                                     GEN (*msqr)(void*,GEN))
     271             : {
     272      108508 :   pari_sp av = avma;
     273      108508 :   return gerepilecopy(av, gen_pow_fold_i(x,n,E,sqr,msqr));
     274             : }
     275             : 
     276             : GEN
     277     4662930 : 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     4662930 :   GEN V = cgetg(l+2,t_VEC);
     282     4662925 :   gel(V,1) = one(E); if (l==0) return V;
     283     4650172 :   gel(V,2) = gcopy(x); if (l==1) return V;
     284     2980165 :   gel(V,3) = sqr(E,x);
     285     2980165 :   if (use_sqr)
     286    10146265 :     for(i = 4; i < l+2; i++)
     287    19695991 :       gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
     288    11664274 :                       : mul(E,gel(V, i-1),x);
     289             :   else
     290     2257854 :     for(i = 4; i < l+2; i++)
     291     1392238 :       gel(V,i) = mul(E,gel(V,i-1),x);
     292     2980164 :   return V;
     293             : }
     294             : 
     295             : GEN
     296    49788167 : producttree_scheme(long n)
     297             : {
     298             :   GEN v, w;
     299             :   long i, j, k, u, l;
     300    49788167 :   if (n<=2) return mkvecsmall(n);
     301    42753928 :   u = expu(n-1);
     302    42753913 :   v = cgetg(n+1,t_VECSMALL);
     303    42753880 :   w = cgetg(n+1,t_VECSMALL);
     304    42754001 :   v[1] = n; l = 1;
     305   143114846 :   for (i=1; i<=u; i++)
     306             :   {
     307   353796691 :     for(j=1, k=1; j<=l; j++, k+=2)
     308             :     {
     309   253435846 :       long vj = v[j], v2 = vj>>1;
     310   253435846 :       w[k]    = vj-v2;
     311   253435846 :       w[k+1]  = v2;
     312             :     }
     313   100360845 :     swap(v,w); l<<=1;
     314             :   }
     315    42754001 :   fixlg(v, l+1);
     316    42753933 :   avma = (pari_sp) v;
     317    42753933 :   return v;
     318             : }
     319             : 
     320             : GEN
     321    51312329 : gen_product(GEN x, void *data, GEN (*mul)(void *,GEN,GEN))
     322             : {
     323             :   pari_sp ltop;
     324    51312329 :   long i,k,lx = lg(x),lv;
     325             :   pari_timer ti;
     326             :   GEN v;
     327    51312329 :   if (DEBUGLEVEL>7) timer_start(&ti);
     328             : 
     329    51312352 :   if (lx == 1) return gen_1;
     330    51310134 :   if (lx == 2) return gcopy(gel(x,1));
     331    49736499 :   x = leafcopy(x); k = lx;
     332    49736622 :   v = producttree_scheme(lx-1); lv = lg(v);
     333    49736839 :   ltop = avma;
     334   352385243 :   for (k=1, i=1; k<lv; i+=v[k++])
     335   302648768 :     gel(x,k) = v[k]==1 ? gel(x,i): mul(data,gel(x,i),gel(x,i+1));
     336   199691477 :   while (k > 2)
     337             :   {
     338   100218522 :     if (DEBUGLEVEL>7)
     339           0 :       timer_printf(&ti,"gen_product: remaining objects %ld",k-1);
     340   100218849 :     lx = k; k = 1;
     341   353127670 :     for (i=1; i<lx-1; i+=2)
     342   252909202 :       gel(x,k++) = mul(data,gel(x,i),gel(x,i+1));
     343   100218468 :     if (i < lx) gel(x,k++) = gel(x,i);
     344   100218468 :     if (gc_needed(ltop,1))
     345           8 :       gerepilecoeffs(ltop,x+1,k-1);
     346             :   }
     347    49736480 :   return gel(x,1);
     348             : }
     349             : 
     350             : /***********************************************************************/
     351             : /**                                                                   **/
     352             : /**                    DISCRETE LOGARITHM                             **/
     353             : /**                                                                   **/
     354             : /***********************************************************************/
     355             : 
     356             : static GEN
     357    57284427 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
     358             : {
     359    57284427 :   GEN a = gel(A,1);
     360    57284427 :   switch((h|grp->hash(a))%3UL)
     361             :   {
     362             :     case 0:
     363    19094121 :       return mkvec3(grp->pow(E,a,gen_2),Fp_mulu(gel(A,2),2,q),
     364    19094121 :                                         Fp_mulu(gel(A,3),2,q));
     365             :     case 1:
     366    19081082 :       return mkvec3(grp->mul(E,a,x),addis(gel(A,2),1),gel(A,3));
     367             :     case 2:
     368    19109224 :       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          49 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     376             : {
     377          49 :   pari_sp av=avma;
     378          49 :   GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
     379          49 :   ulong i, h = 0, imax = itou_or_0(sqrt4q);
     380          49 :   if (!imax) imax = ULONG_MAX;
     381             :   do {
     382             :  rho_restart:
     383          49 :     A = B = mkvec3(x,gen_1,gen_0);
     384          49 :     i=0;
     385             :     do {
     386    19094809 :       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    19094809 :       A = iter_rho(x, g, q, A, h, E, grp);
     394    19094809 :       B = iter_rho(x, g, q, B, h, E, grp);
     395    19094809 :       B = iter_rho(x, g, q, B, h, E, grp);
     396    19094809 :       if (gc_needed(av,2))
     397             :       {
     398        1771 :         if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
     399        1771 :         gerepileall(av, 2, &A, &B);
     400             :       }
     401    19094809 :       i++;
     402    19094809 :     } while (!grp->equal(gel(A,1), gel(B,1)));
     403          49 :     gel(A,2) = modii(gel(A,2), q);
     404          49 :     gel(B,2) = modii(gel(B,2), q);
     405          49 :     h++;
     406          49 :   } while (equalii(gel(A,2), gel(B,2)));
     407          49 :   l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
     408          49 :   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     2638722 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
     414             : {
     415     2638722 :   GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
     416     2638722 :   pari_sp av=avma;
     417             :   long i;
     418     2638722 :   table[1] = grp->hash(grp->pow(E,g,gen_0));
     419    13202467 :   for (i=2; i<=n; i++)
     420             :   {
     421    10563745 :     table[i] = grp->hash(p1);
     422    10563745 :     p1 = grp->mul(E,p1,g);
     423    10563745 :     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     2638722 :   G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
     430     2638722 :   perm = vecsmall_indexsort(table);
     431     2638722 :   table = vecsmallpermute(table,perm);
     432     2638722 :   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     2639170 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
     437             : {
     438     2639170 :   pari_sp av=avma;
     439     2639170 :   GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
     440     2639170 :   GEN p1 = x;
     441     2639170 :   long n = lg(table)-1;
     442             :   ulong k;
     443    14731594 :   for (k=0; k<N; k++)
     444             :   { /* p1 = x G^k, G = g^-n */
     445    14440659 :     long h = grp->hash(p1), i = zv_search(table, h);
     446    14440659 :     if (i)
     447             :     {
     448     2348980 :       do i--; while (i && table[i] == h);
     449     2348235 :       for (i++; i <= n && table[i] == h; i++)
     450             :       {
     451     2348235 :         GEN v = addiu(muluu(n,k), perm[i]-1);
     452     2348235 :         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    12092424 :     p1 = grp->mul(E,p1,G);
     458    12092424 :     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      290935 :   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      195926 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
     471             : {
     472      195926 :   pari_sp av=avma, av1;
     473             :   long lbaby, i, k;
     474             :   GEN p1, table, giant, perm, ginv;
     475      195926 :   p1 = sqrti(q);
     476      195926 :   if (abscmpiu(p1,LGBITS) >= 0)
     477           0 :     pari_err_OVERFLOW("gen_Shanks_log [order too large]");
     478      195926 :   lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
     479      195926 :   ginv = grp->pow(E,g,gen_m1);
     480      195926 :   av1 = avma;
     481     1213222 :   for (p1=x, i=1;;i++)
     482             :   {
     483     1213222 :     if (grp->equal1(p1)) { avma = av; return stoi(i-1); }
     484     1188735 :     table[i] = grp->hash(p1); if (i==lbaby) break;
     485     1017296 :     p1 = grp->mul(E,p1,ginv);
     486     1017296 :     if (gc_needed(av1,2))
     487             :     {
     488           7 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
     489           7 :       p1 = gerepileupto(av1, p1);
     490             :     }
     491     1017296 :   }
     492      171439 :   p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
     493      171439 :   perm = vecsmall_indexsort(table);
     494      171439 :   table = vecsmallpermute(table,perm);
     495      171439 :   av1 = avma;
     496      579675 :   for (k=1; k<= lbaby; k++)
     497             :   {
     498      579667 :     long h = grp->hash(p1), i = zv_search(table, h);
     499      579667 :     if (i)
     500             :     {
     501      171433 :       while (table[i] == h && i) i--;
     502      171435 :       for (i++; i <= lbaby && table[i] == h; i++)
     503             :       {
     504      171433 :         GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
     505      171433 :         if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
     506           2 :         if (DEBUGLEVEL)
     507           0 :           err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
     508             :       }
     509             :     }
     510      408236 :     p1 = grp->mul(E,p1,giant);
     511      408236 :     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           8 :   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      826426 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
     523             : {
     524      826426 :   if (grp->easylog)
     525             :   {
     526      802917 :     GEN e = grp->easylog(E, x, g, p);
     527      802917 :     if (e) return e;
     528             :   }
     529      264619 :   if (grp->equal1(x)) return gen_0;
     530      264577 :   if (grp->equal(x,g)) return gen_1;
     531      195975 :   if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
     532          49 :   return gen_Pollard_log(x, g, p, E, grp);
     533             : }
     534             : 
     535             : GEN
     536     6989252 : get_arith_ZZM(GEN o)
     537             : {
     538     6989252 :   if (!o) return NULL;
     539     6989252 :   switch(typ(o))
     540             :   {
     541             :     case t_INT:
     542     4690408 :       if (signe(o) > 0) return mkvec2(o, Z_factor(o));
     543           7 :       break;
     544             :     case t_MAT:
     545     1361656 :       if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
     546          14 :       break;
     547             :     case t_VEC:
     548      937181 :       if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
     549           0 :       break;
     550             :   }
     551          28 :   pari_err_TYPE("generic discrete logarithm (order factorization)",o);
     552             :   return NULL; /* LCOV_EXCL_LINE */
     553             : }
     554             : GEN
     555      194357 : get_arith_Z(GEN o)
     556             : {
     557      194357 :   if (!o) return NULL;
     558      194357 :   switch(typ(o))
     559             :   {
     560             :     case t_INT:
     561      146885 :       if (signe(o) > 0) return o;
     562           7 :       break;
     563             :     case t_MAT:
     564          14 :       o = factorback(o);
     565           0 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     566           0 :       break;
     567             :     case t_VEC:
     568       47451 :       if (lg(o) != 3) break;
     569       47451 :       o = gel(o,1);
     570       47451 :       if (typ(o) == t_INT && signe(o) > 0) return o;
     571           0 :       break;
     572             :   }
     573          14 :   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      563990 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
     582             : {
     583      563990 :   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      563990 :   if (grp->equal(g, a)) /* frequent special case */
     589      101370 :     return grp->equal1(g)? gen_0: gen_1;
     590      462620 :   if (grp->easylog)
     591             :   {
     592      462515 :     GEN e = grp->easylog(E, a, g, ord);
     593      462487 :     if (e) return e;
     594             :   }
     595      241414 :   v = get_arith_ZZM(ord);
     596      241414 :   ord= gel(v,1);
     597      241414 :   fa = gel(v,2);
     598      241414 :   ex = gel(fa,2);
     599      241414 :   fa = gel(fa,1); l = lg(fa);
     600      241414 :   ginv = grp->pow(E,g,gen_m1);
     601      241414 :   v = cgetg(l, t_VEC);
     602      712921 :   for (i=1; i<l; i++)
     603             :   {
     604      471851 :     q = gel(fa,i);
     605      471851 :     e = itos(gel(ex,i));
     606      471851 :     if (DEBUGLEVEL>5)
     607           0 :       err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
     608      471851 :     qj = new_chunk(e+1);
     609      471851 :     gel(qj,0) = gen_1;
     610      471851 :     gel(qj,1) = q;
     611      471851 :     for (j=2; j<=e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
     612      471851 :     t0 = diviiexact(ord, gel(qj,e));
     613      471851 :     a0 = grp->pow(E, a, t0);
     614      471851 :     ginv0 = grp->pow(E, ginv, t0); /* order q^e */
     615      471851 :     if (grp->equal1(ginv0))
     616             :     {
     617          14 :       gel(v,i) = mkintmod(gen_0, gen_1);
     618          14 :       continue;
     619             :     }
     620      471844 :     do { g_q = grp->pow(E,g, mulii(t0, gel(qj,--e))); /* order q */
     621      471844 :     } while (grp->equal1(g_q));
     622      471837 :     n_q = gen_0;
     623      649353 :     for (j=0;; j++)
     624             :     { /* n_q = sum_{i<j} b_i q^i */
     625      649353 :       b = grp->pow(E,a0, gel(qj,e-j));
     626             :       /* early abort: cheap and very effective */
     627      649353 :       if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
     628         336 :         avma = av; return cgetg(1, t_VEC);
     629             :       }
     630      649017 :       b = gen_plog(b, g_q, q, E, grp);
     631      649017 :       if (typ(b) != t_INT) { avma = av; return cgetg(1, t_VEC); }
     632      649009 :       n_q = addii(n_q, mulii(b, gel(qj,j)));
     633      649009 :       if (j == e) break;
     634             : 
     635      177516 :       a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
     636      177516 :       ginv0 = grp->pow(E,ginv0, q);
     637      177516 :     }
     638      471493 :     gel(v,i) = mkintmod(n_q, gel(qj,e+1));
     639             :   }
     640      241070 :   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     3200441 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     651             : {
     652     3200441 :   pari_sp av = avma;
     653             :   long i, l;
     654             :   GEN m;
     655             : 
     656     3200441 :   m = get_arith_ZZM(o);
     657     3200441 :   if (!m) pari_err_TYPE("gen_order [missing order]",a);
     658     3200441 :   o = gel(m,1);
     659     3200441 :   m = gel(m,2); l = lgcols(m);
     660     9985191 :   for (i = l-1; i; i--)
     661             :   {
     662     6784750 :     GEN t, y, p = gcoeff(m,i,1);
     663     6784750 :     long j, e = itos(gcoeff(m,i,2));
     664     6784750 :     if (l == 2) {
     665      670644 :       t = gen_1;
     666      670644 :       y = a;
     667             :     } else {
     668     6114106 :       t = diviiexact(o, powiu(p,e));
     669     6114106 :       y = grp->pow(E, a, t);
     670             :     }
     671     6784750 :     if (grp->equal1(y)) o = t;
     672             :     else {
     673     6292010 :       for (j = 1; j < e; j++)
     674             :       {
     675     2150696 :         y = grp->pow(E, y, p);
     676     2150696 :         if (grp->equal1(y)) break;
     677             :       }
     678     4659825 :       if (j < e) {
     679      518511 :         if (j > 1) p = powiu(p, j);
     680      518511 :         o = mulii(t, p);
     681             :       }
     682             :     }
     683             :   }
     684     3200441 :   return gerepilecopy(av, o);
     685             : }
     686             : 
     687             : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
     688             : GEN
     689     2732832 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
     690             : {
     691     2732832 :   pari_sp av = avma;
     692             :   long i, l, ind;
     693             :   GEN m, F, P;
     694             : 
     695     2732832 :   m = get_arith_ZZM(o);
     696     2732832 :   if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
     697     2732832 :   o = gel(m,1);
     698     2732832 :   m = gel(m,2); l = lgcols(m);
     699     2732832 :   P = cgetg(l, t_COL); ind = 1;
     700     2732832 :   F = cgetg(l, t_COL);
     701     6564069 :   for (i = l-1; i; i--)
     702             :   {
     703     3831237 :     GEN t, y, p = gcoeff(m,i,1);
     704     3831237 :     long j, e = itos(gcoeff(m,i,2));
     705     3831237 :     if (l == 2) {
     706     1678184 :       t = gen_1;
     707     1678184 :       y = a;
     708             :     } else {
     709     2153053 :       t = diviiexact(o, powiu(p,e));
     710     2153053 :       y = grp->pow(E, a, t);
     711             :     }
     712     3831237 :     if (grp->equal1(y)) o = t;
     713             :     else {
     714     4800695 :       for (j = 1; j < e; j++)
     715             :       {
     716     1017685 :         y = grp->pow(E, y, p);
     717     1017685 :         if (grp->equal1(y)) break;
     718             :       }
     719     3795895 :       gel(P,ind) = p;
     720     3795895 :       gel(F,ind) = utoipos(j);
     721     3795895 :       if (j < e) {
     722       12885 :         if (j > 1) p = powiu(p, j);
     723       12885 :         o = mulii(t, p);
     724             :       }
     725     3795895 :       ind++;
     726             :     }
     727             :   }
     728     2732832 :   setlg(P, ind); P = vecreverse(P);
     729     2732832 :   setlg(F, ind); F = vecreverse(F);
     730     2732832 :   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         903 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
     737             : {
     738         903 :   pari_sp ltop = avma, btop;
     739             :   GEN lastgood, so, vo;
     740         903 :   long lo = lg(o), nbo=lo-1;
     741         903 :   if (nbo == 1) return icopy(gel(o,1));
     742         420 :   so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
     743         420 :   vo = zero_zv(lo);
     744         420 :   lastgood = gel(o, so[nbo]);
     745         420 :   btop = avma;
     746             :   for(;;)
     747             :   {
     748         420 :     GEN lasto = gen_0;
     749         420 :     GEN P = grp->rand(E), t = mkvec(gen_0);
     750             :     long i;
     751         532 :     for (i = 1; i < lo; i++)
     752             :     {
     753         532 :       GEN newo = gel(o, so[i]);
     754         532 :       if (vo[i]) continue;
     755         532 :       t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
     756         532 :       lasto = newo;
     757         532 :       if (!grp->equal1(t))
     758             :       {
     759         462 :         if (--nbo == 1) { avma=ltop; return icopy(lastgood); }
     760          42 :         vo[i] = 1;
     761             :       }
     762             :       else
     763          70 :         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      265054 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
     781             : {
     782      265054 :   const pari_sp av1 = avma;
     783             :   GEN m, m1;
     784             :   long i;
     785      206866 :   for (;; avma = av1)
     786             :   {
     787      471920 :     m1 = m = grp->pow(E, grp->rand(E), r);
     788      471925 :     if (grp->equal1(m)) continue;
     789      856470 :     for (i=1; i<e; i++)
     790             :     {
     791      591411 :       m = grp->pow(E,m,l);
     792      591411 :       if (grp->equal1(m)) break;
     793             :     }
     794      382644 :     if (i==e) break;
     795      206866 :   }
     796      265058 :   *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       14686 : gen_gener(GEN o, void *E, const struct bb_group *grp)
     803             : {
     804       14686 :   pari_sp ltop = avma, av;
     805             :   long i, lpr;
     806       14686 :   GEN F, N, pr, z=NULL;
     807       14686 :   F = get_arith_ZZM(o);
     808       14686 :   N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
     809       14686 :   av = avma;
     810             : 
     811       48902 :   for (i = 1; i < lpr; i++)
     812             :   {
     813       34216 :     GEN l = gcoeff(pr,i,1);
     814       34216 :     long e = itos(gcoeff(pr,i,2));
     815       34216 :     GEN r = diviiexact(N,powis(l,e));
     816       34216 :     GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
     817       34216 :     z = i==1 ? zl: grp->mul(E,z,zl);
     818       34216 :     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       14686 :   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      231193 : 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      231193 :   pari_sp av = avma;
     837             :   long k;
     838             :   GEN p1, u1, u2, v, w, z, dl;
     839             : 
     840      231193 :   (void)bezout(r,l,&u1,&u2);
     841      231192 :   v = grp->pow(E,a,u2);
     842      231192 :   w = grp->pow(E,v,l);
     843      231192 :   w = grp->mul(E,w,grp->pow(E,a,gen_m1));
     844      639793 :   while (!grp->equal1(w))
     845             :   {
     846      180278 :     k = 0;
     847      180278 :     p1 = w;
     848             :     do
     849             :     {
     850      309973 :       z = p1; p1 = grp->pow(E,p1,l);
     851      309974 :       k++;
     852      309974 :     } while(!grp->equal1(p1));
     853      180279 :     if (k==e) { avma = av; return NULL; }
     854      177409 :     dl = gen_plog(z,m,l,E,grp);
     855      177409 :     if (typ(dl) != t_INT) { avma = av; return NULL; }
     856      177409 :     dl = negi(dl);
     857      177409 :     p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
     858      177409 :     m = grp->pow(E,m,dl);
     859      177409 :     e = k;
     860      177409 :     v = grp->mul(E,p1,v);
     861      177407 :     y = grp->pow(E,p1,l);
     862      177409 :     w = grp->mul(E,y,w);
     863      177409 :     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      228323 :   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      238316 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
     881             : {
     882      238316 :   pari_sp ltop = avma;
     883             :   GEN m, u1, u2, z;
     884             :   int is_1;
     885             : 
     886      238316 :   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      238317 :   is_1 = grp->equal1(a);
     892      238318 :   if (is_1 && !zetan) return gcopy(a);
     893             : 
     894      232529 :   m = bezout(n,q,&u1,&u2);
     895      232526 :   z = grp->pow(E,a,gen_0);
     896      232526 :   if (!is_pm1(m))
     897             :   {
     898      230706 :     GEN F = Z_factor(m);
     899             :     long i, j, e;
     900             :     GEN r, zeta, y, l;
     901      230702 :     pari_sp av1 = avma;
     902      458675 :     for (i = nbrows(F); i; i--)
     903             :     {
     904      230836 :       l = gcoeff(F,i,1);
     905      230836 :       j = itos(gcoeff(F,i,2));
     906      230839 :       e = Z_pvalrem(q,l,&r);
     907      230833 :       y = gen_lgener(l,e,r,&zeta,E,grp);
     908      230842 :       if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
     909      230842 :       if (!is_1) {
     910             :         do
     911             :         {
     912      231192 :           a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
     913      231193 :           if (!a) { avma = ltop; return NULL;}
     914      228323 :         } while (--j);
     915             :       }
     916      227973 :       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      229660 :   if (!equalii(m, n))
     924        1883 :     a = grp->pow(E,a,modii(u1,q));
     925      229660 :   if (zetan)
     926             :   {
     927         266 :     *zetan = z;
     928         266 :     gerepileall(ltop,2,&a,zetan);
     929             :   }
     930             :   else /* is_1 is 0: a was modified above -> gerepileupto valid */
     931      229394 :     a = gerepileupto(ltop, a);
     932      229660 :   return a;
     933             : }
     934             : 
     935             : /*******************************************************************/
     936             : /*                                                                 */
     937             : /*               structure of groups with pairing                  */
     938             : /*                                                                 */
     939             : /*******************************************************************/
     940             : 
     941             : static GEN
     942       39606 : ellgroup_d2(GEN N, GEN d)
     943             : {
     944       39606 :   GEN r = gcdii(N, d);
     945       39606 :   GEN F1 = gel(Z_factor(r), 1);
     946       39606 :   long i, j, l1 = lg(F1);
     947       39606 :   GEN F = cgetg(3, t_MAT);
     948       39606 :   gel(F,1) = cgetg(l1, t_COL);
     949       39606 :   gel(F,2) = cgetg(l1, t_COL);
     950       69776 :   for (i = 1, j = 1; i < l1; ++i)
     951             :   {
     952       30170 :     long v = Z_pval(N, gel(F1, i));
     953       30170 :     if (v<=1) continue;
     954       15582 :     gcoeff(F, j  , 1) = gel(F1, i);
     955       15582 :     gcoeff(F, j++, 2) = stoi(v);
     956             :   }
     957       39606 :   setlg(F[1],j); setlg(F[2],j);
     958       39606 :   return j==1 ? NULL : mkvec2(factorback(F), F);
     959             : }
     960             : 
     961             : GEN
     962       39683 : 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       39683 :   pari_sp av = avma;
     966             :   GEN N0, N1, F;
     967       39683 :   if (pt_m) *pt_m = gen_1;
     968       39683 :   if (is_pm1(N)) return cgetg(1,t_VEC);
     969       39606 :   F = ellgroup_d2(N, d);
     970       39606 :   if (!F) {avma = av; return mkveccopy(N);}
     971       14854 :   N0 = gel(F,1); N1 = diviiexact(N, N0);
     972             :   while(1)
     973             :   {
     974       26172 :     pari_sp av2 = avma;
     975             :     GEN P, Q, d, s, t, m;
     976             : 
     977       26172 :     P = grp->pow(E,grp->rand(E), N1);
     978       26172 :     s = gen_order(P, F, E, grp); if (equalii(s, N0)) {avma = av; return mkveccopy(N);}
     979             : 
     980       20398 :     Q = grp->pow(E,grp->rand(E), N1);
     981       20398 :     t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {avma = av; return mkveccopy(N);}
     982             : 
     983       17814 :     m = lcmii(s, t);
     984       17814 :     d = pairorder(E, P, Q, m, F);
     985             :     /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
     986       17814 :     if (is_pm1(d) && equalii(m, N0)) {avma = av; return mkveccopy(N);}
     987       17786 :     if (equalii(mulii(m, d), N0))
     988             :     {
     989        6468 :       GEN g = mkvec2(mulii(N1,m), d);
     990        6468 :       if (pt_m) *pt_m = m;
     991        6468 :       gerepileall(av,pt_m?2:1,&g,pt_m);
     992        6468 :       return g;
     993             :     }
     994       11318 :     avma = av2;
     995       11318 :   }
     996             : }
     997             : 
     998             : GEN
     999        2660 : 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        2660 :   pari_sp ltop = avma, av;
    1003             :   GEN F, d1, dm;
    1004             :   GEN P, Q, d, s;
    1005        2660 :   F = get_arith_ZZM(D1);
    1006        2660 :   d1 = gel(F, 1), dm =  diviiexact(d1,m);
    1007        2660 :   av = avma;
    1008             :   do
    1009             :   {
    1010        7009 :     avma = av;
    1011        7009 :     P = grp->rand(E);
    1012        7009 :     s = gen_order(P, F, E, grp);
    1013        7009 :   } while (!equalii(s, d1));
    1014        2660 :   av = avma;
    1015             :   do
    1016             :   {
    1017        5212 :     avma = av;
    1018        5212 :     Q = grp->rand(E);
    1019        5212 :     d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
    1020        5212 :   } while (!equalii(d, d2));
    1021        2660 :   return gerepilecopy(ltop, mkvec2(P,Q));
    1022             : }

Generated by: LCOV version 1.11