Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - Flxq_log.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23339-b1c33c51a) Lines: 447 467 95.7 %
Date: 2018-12-11 05:41:34 Functions: 28 28 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2013 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             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Let [ be the following order on Fp: 0 [ p-1 [ 1 [ p-2 [ 2 .. [ p\2
      18             : and [[ the lexicographic extension of [ to Fp[T]. Compute the
      19             : isomorphism (Fp[X], [[) -> (N,<) on P */
      20             : 
      21             : static long
      22      392239 : Flx_cindex(GEN P, ulong p)
      23             : {
      24      392239 :   long d = degpol(P), i;
      25      392239 :   ulong s = 0, p2 = (p-1)>>1;
      26     1782819 :   for (i = 0; i <= d; ++i)
      27             :   {
      28     1390580 :     ulong x = P[d-i+2];
      29     1390580 :     if (x<=p2) x = 2*x; else x = 1+2*(p-1-x);
      30     1390580 :     s = p*s+x;
      31             :   }
      32      392239 :   return s;
      33             : }
      34             : 
      35             : /* Compute the polynomial immediately after t for the [[ order */
      36             : 
      37             : static void
      38      433530 : Flx_cnext(GEN t, ulong p)
      39             : {
      40             :   long i;
      41      433530 :   long p2 = p>>1;
      42      554786 :   for(i=2;;i++)
      43      676042 :     if (t[i]==p2)
      44      121256 :       t[i]=0;
      45             :     else
      46             :     {
      47      433530 :       t[i] = t[i]<p2 ? p-1-t[i]: p-t[i];
      48      433530 :       break;
      49             :     }
      50      433530 : }
      51             : 
      52             : static int
      53          28 : has_deg1_auto(GEN T, ulong p)
      54             : {
      55          28 :   long i, n = degpol(T);
      56          28 :   GEN a = polx_Flx(get_Flx_var(T));
      57         672 :   for (i=1; i<n; i++)
      58             :   {
      59         644 :     a = Flxq_powu(a, p, T, p);
      60         644 :     if (degpol(a)==1) return 1;
      61             :   }
      62          28 :   return 0;
      63             : }
      64             : 
      65             : static void
      66        1057 : smallirred_Flx_next(GEN a, long p)
      67             : {
      68             :   do
      69             :   {
      70             :     long i;
      71        1449 :     for(i=2;;i++)
      72        1841 :       if (++a[i]==p) a[i]=0;
      73        1057 :       else break;
      74        1057 :   } while (!Flx_is_irred(a, p) || has_deg1_auto(a,p) );
      75          28 : }
      76             : 
      77             : /* Avoid automorphisms of degree 1 */
      78             : static GEN
      79          28 : smallirred_Flx(long p, ulong n, long sv)
      80             : {
      81          28 :   GEN a = zero_zv(n+2);
      82          28 :   a[1] = sv; a[3] = 1; a[n+2] = 1;
      83          28 :   smallirred_Flx_next(a, p);
      84          28 :   return a;
      85             : }
      86             : 
      87             : struct Flxq_log_rel
      88             : {
      89             :   long nbrel;
      90             :   GEN rel;
      91             :   long nb;
      92             :   long r, off, nbmax, nbexp;
      93             :   ulong nbtest;
      94             : };
      95             : 
      96             : static GEN
      97        4662 : cindex_Flx(long c, long d, ulong p, long v)
      98             : {
      99        4662 :   GEN P = cgetg(d+3, t_VECSMALL);
     100             :   long i;
     101        4662 :   P[1] = v;
     102       31833 :   for (i = 0; i <= d; ++i)
     103             :   {
     104       27171 :     ulong x = c%p;
     105       27171 :     P[i+2] = (x&1) ? p-1-(x>>1) : x>>1;
     106       27171 :     c/=p;
     107             :   }
     108        4662 :   return Flx_renormalize(P, d+3);
     109             : }
     110             : 
     111             : static GEN
     112       11212 : factorel(GEN h, ulong p)
     113             : {
     114       11212 :   GEN F = Flx_factor(h, p);
     115       11212 :   GEN F1 = gel(F, 1), F2 = gel(F, 2);
     116       11212 :   long i, l1 = lg(F1)-1;
     117       11212 :   GEN p2 = cgetg(l1+1, t_VECSMALL);
     118       11212 :   GEN e2 = cgetg(l1+1, t_VECSMALL);
     119       53081 :   for (i = 1; i <= l1; ++i)
     120             :   {
     121       41869 :     p2[i] = Flx_cindex(gel(F1, i), p);
     122       41869 :     e2[i] = F2[i];
     123             :   }
     124       11212 :   return mkmat2(p2, e2);
     125             : }
     126             : 
     127             : static long
     128       74256 : Flx_addifsmooth3(pari_sp *av, struct Flxq_log_rel *r, GEN h, long u, long v, long w, ulong p)
     129             : {
     130       74256 :   long off = r->off;
     131       74256 :   r->nbtest++;
     132       74256 :   if (Flx_is_smooth(h, r->r, p))
     133             :   {
     134        5670 :     GEN z = factorel(h, p);
     135        5670 :     if (v<0)
     136        1225 :       z = mkmat2(vecsmall_append(gel(z,1),off+u),vecsmall_append(gel(z,2),-1));
     137             :     else
     138       13335 :       z = famatsmall_reduce(mkmat2(
     139        4445 :             vecsmall_concat(gel(z,1),mkvecsmall3(off+u,off+v,off+w)),
     140        4445 :             vecsmall_concat(gel(z,2),mkvecsmall3(-1,-1,-1))));
     141        5670 :     gel(r->rel,++r->nbrel) = gerepilecopy(*av,z);
     142        5670 :     if (DEBUGLEVEL && (r->nbrel&511UL)==0)
     143           0 :       err_printf("%ld%% ",r->nbrel*100/r->nbexp);
     144        5670 :     *av = avma;
     145       68586 :   } else set_avma(*av);
     146       74256 :   return r->nbrel==r->nb || r->nbrel==r->nbmax;
     147             : }
     148             : 
     149             : static void
     150      433566 : Flx_renormalize_inplace(GEN x, long lx)
     151             : {
     152             :   long i;
     153     4941593 :   for (i = lx-1; i>1; i--)
     154     4938702 :     if (x[i]) break;
     155      433566 :   setlg(x, i+1);
     156      433684 : }
     157             : 
     158             : /*
     159             :    Let T*X^e=C^3-R
     160             :    a+b+c = 0
     161             :    (C+a)*(C+b)*(C+c) = C^3+ (a*b+a*c+b*c)*C+a*b*c
     162             :    = R + (a*b+a*c+b*c)*C+a*b*c
     163             :    = R + (a*b-c^2)*C+a*b*c
     164             :  */
     165             : static void
     166          14 : Flxq_log_cubic(struct Flxq_log_rel *r, GEN C, GEN R, ulong p)
     167             : {
     168          14 :   long l = lg(C);
     169          14 :   GEN a = zero_zv(l); /*We allocate one extra word to catch overflow*/
     170          14 :   GEN b = zero_zv(l);
     171          14 :   pari_sp av = avma;
     172             :   long i,j,k;
     173        2800 :   for(i=0; ; i++, Flx_cnext(a, p))
     174             :   {
     175        5586 :     Flx_renormalize_inplace(a, l+1);
     176        2800 :     r->nb++;
     177        2800 :     if (Flx_addifsmooth3(&av, r, Flx_add(a, C, p), i, -1, -1, p)) return;
     178        2800 :     for(j=2; j<=l; j++) b[j] = 0;
     179      353129 :     for(j=0; j<=i; j++, Flx_cnext(b, p))
     180             :     {
     181             :       GEN h,c;
     182             :       GEN pab,pabc,pabc2;
     183      350343 :       Flx_renormalize_inplace(b, l+1);
     184      350343 :       c = Flx_neg(Flx_add(a,b,p),p);
     185      350343 :       k = Flx_cindex(c, p);
     186      350343 :       if (k > j) continue;
     187       71456 :       pab  = Flx_mul(a, b, p);
     188       71456 :       pabc = Flx_mul(pab,c,p);
     189       71456 :       pabc2= Flx_sub(pab,Flx_sqr(c,p),p);
     190       71456 :       h = Flx_add(R,Flx_add(Flx_mul(C,pabc2,p),pabc,p), p);
     191       71456 :       h = Flx_normalize(h, p);
     192       71456 :       if (Flx_addifsmooth3(&av, r, h, i, j, k, p)) return;
     193             :     }
     194             :   }
     195             : }
     196             : 
     197             : static GEN
     198          81 : Flxq_log_find_rel(GEN b, long r, GEN T, ulong p, GEN *g, long *e)
     199             : {
     200          81 :   pari_sp av = avma;
     201             :   while (1)
     202        2797 :   {
     203             :     GEN M;
     204        2878 :     *g = Flxq_mul(*g, b, T, p); (*e)++;
     205        2878 :     M = Flx_halfgcd(*g,T,p);
     206        2878 :     if (Flx_is_smooth(gcoeff(M,1,1), r, p))
     207             :     {
     208         436 :       GEN z = Flx_add(Flx_mul(gcoeff(M,1,1),*g,p), Flx_mul(gcoeff(M,1,2),T,p),p);
     209         436 :       if (Flx_is_smooth(z, r, p))
     210             :       {
     211          81 :         GEN F = factorel(z, p);
     212          81 :         GEN G = factorel(gcoeff(M,1,1), p);
     213         162 :         GEN rel = mkmat2(vecsmall_concat(gel(F, 1),gel(G, 1)),
     214         162 :                          vecsmall_concat(gel(F, 2),zv_neg(gel(G, 2))));
     215         162 :         gerepileall(av,2,g,&rel); return rel;
     216             :       }
     217             :     }
     218        2797 :     if (gc_needed(av,2))
     219             :     {
     220           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flxq_log_find_rel");
     221           0 :       *g = gerepilecopy(av, *g);
     222             :     }
     223             :   }
     224             : }
     225             : 
     226             : /* Generalised Odlyzko formulae ( EUROCRYPT '84, LNCS 209, pp. 224-314, 1985. ) */
     227             : /* Return the number of monic, k smooth, degree n polynomials for k=1..r */
     228             : static GEN
     229        1708 : smoothness_vec(ulong p, long r, long n)
     230             : {
     231             :   long i,j,k;
     232        1708 :   GEN R = cgetg(r+1, t_VEC);
     233        1708 :   GEN V = cgetg(n+1, t_VEC);
     234       18571 :   for (j = 1; j <= n; ++j)
     235       16863 :     gel(V, j) =  binomialuu(p+j-1,j);
     236        1708 :   gel(R, 1) = gel(V, n);
     237        4816 :   for (k = 2; k <= r; ++k)
     238             :   {
     239        3108 :     GEN W = cgetg(n+1, t_VEC);
     240        3108 :     GEN Ik = ffnbirred(utoi(p),k);
     241       36029 :     for (j = 1; j <= n; ++j)
     242             :     {
     243       32921 :       long l = j/k;
     244       32921 :       GEN s = gen_0;
     245       32921 :       pari_sp av2 = avma;
     246       32921 :       if (l*k == j)
     247             :       {
     248       10801 :         s = binomial(addiu(Ik,l-1), l);
     249       10801 :         l--;
     250             :       }
     251      119847 :       for (i = 0; i <= l; ++i)
     252       86926 :         s = addii(s, mulii(gel(V, j-k*i), binomial(addis(Ik,i-1), i)));
     253       32921 :       gel(W, j) = gerepileuptoint(av2, s);
     254             :     }
     255        3108 :     V = W;
     256        3108 :     gel(R, k) = gel(V, n);
     257             :   }
     258        1708 :   return R;
     259             : }
     260             : 
     261             : /* Solve N^2*pr/6 + N*prC = N+fb
     262             :    N^2*pr/6 + N*(prC-1) -fb = 0
     263             :  */
     264             : 
     265             : static GEN
     266        1379 : smooth_cost(GEN fb, GEN pr, GEN prC)
     267             : {
     268        1379 :   GEN a = gdivgs(pr,6);
     269        1379 :   GEN b = gsubgs(prC,1);
     270        1379 :   GEN c = gneg(fb);
     271        1379 :   GEN vD = gsqrt(gsub(gsqr(b),gmul2n(gmul(a,c),2)),BIGDEFAULTPREC);
     272        1379 :   return ceil_safe(gdiv(gsub(vD,b),gmul2n(a,1)));
     273             : }
     274             : 
     275             : /* Return best choice of r.
     276             :    We loop over d until there is sufficiently many triples (a,b,c) (a+b+c=0)
     277             :    of degree <=d with respect to the probability of smoothness of (a*b-c^2)*C
     278             :  */
     279             : 
     280             : static GEN
     281         140 : smooth_best(long p, long n, long *pt_r, long *pt_nb)
     282             : {
     283         140 :   pari_sp av = avma, av2;
     284         140 :   GEN bestc = NULL;
     285         140 :   long bestr = 0, bestFB = 0;
     286         140 :   long r,d, dC = (n+2)/3;
     287         469 :   for (r = 1; r < dC; ++r)
     288             :   {
     289         329 :     GEN fb = ffsumnbirred(utoi(p), r);
     290         329 :     GEN smoothC = smoothness_vec(p,r,dC);
     291         329 :     GEN prC = gdiv(gel(smoothC,r), powuu(p,dC));
     292         329 :     ulong rels = 0;
     293         329 :     av2 = avma;
     294        2954 :     for(d=0; d<dC && rels < ULONG_MAX; d++)
     295             :     {
     296             :       GEN c;
     297        1379 :       long dt = dC+2*d;
     298        1379 :       GEN smooth = smoothness_vec(p,r,dt);
     299        1379 :       GEN pr = gdiv(gel(smooth,r), powuu(p,dt));
     300        1379 :       GEN FB = addii(fb,powuu(p,d));
     301        1379 :       GEN N = smooth_cost(subiu(FB,rels),pr,prC);
     302        1379 :       GEN Nmax = powuu(p,d+1);
     303        1379 :       if (gcmp(N,Nmax) >= 0)
     304             :       {
     305        1148 :         rels = itou_or_0(addui(rels, gceil(gmul(gdivgs(sqri(Nmax),6),pr))));
     306        1148 :         if (!rels) rels = ULONG_MAX;
     307        1148 :         set_avma(av2);
     308        1148 :         continue;
     309             :       }
     310         231 :       c = gdivgs(addii(powuu(p,2*d),sqri(N)),6);
     311         231 :       FB = addii(FB,N);
     312         231 :       if ((!bestc || gcmp(gmul2n(c,r), gmul2n(bestc,bestr)) < 0))
     313             :       {
     314         154 :         if (DEBUGLEVEL)
     315           0 :           err_printf("r=%ld d=%ld fb=%Ps early rels=%lu P=%.5Pe -> C=%.5Pe \n",
     316             :                       r, dt, FB, rels, pr, c);
     317         154 :         bestc = c;
     318         154 :         bestr = r;
     319         154 :         bestFB = itos_or_0(FB);
     320             :       }
     321         231 :       break;
     322             :     }
     323             :   }
     324         140 :   *pt_r=bestr;
     325         140 :   *pt_nb=bestFB;
     326         140 :   return bestc ? gerepileupto(av, gceil(bestc)): NULL;
     327             : }
     328             : 
     329             : static GEN
     330          28 : check_kernel(long r, GEN M, long nbi, long nbrow, GEN T, ulong p, GEN m)
     331             : {
     332          28 :   pari_sp av = avma;
     333          28 :   long N = 3*upowuu(p, r);
     334          28 :   GEN K = FpMs_leftkernel_elt(M, nbrow, m);
     335          28 :   long i, f=0, tbs;
     336          28 :   long lm = lgefint(m), u=1;
     337             :   GEN tab, g;
     338          28 :   GEN q = powuu(p,degpol(T));
     339          28 :   GEN idx = diviiexact(subiu(q,1),m);
     340             :   pari_timer ti;
     341          28 :   if (DEBUGLEVEL) timer_start(&ti);
     342         252 :   while (signe(gel(K,u))==0)
     343         196 :     u++;
     344          28 :   K = FpC_Fp_mul(K, Fp_inv(gel(K, u), m), m);
     345          28 :   g = Flxq_pow(cindex_Flx(u, r, p, T[1]), idx, T, p);
     346          28 :   tbs = maxss(1, expu(nbi/expi(m)));
     347          28 :   tab = Flxq_pow_init(g, q, tbs, T, p);
     348          28 :   setlg(K, N);
     349       46662 :   for (i=1; i<N; i++)
     350             :   {
     351       46634 :     GEN k = gel(K,i);
     352       46634 :     pari_sp av = avma;
     353       51031 :     long t = signe(k) && Flx_equal(Flxq_pow_table(tab, k, T, p),
     354        4397 :                                    Flxq_pow(cindex_Flx(i,r,p,T[1]), idx, T, p));
     355       46634 :     set_avma(av);
     356       46634 :     if (!t)
     357       42237 :       gel(K,i) = cgetineg(lm);
     358             :     else
     359        4397 :       f++;
     360             :   }
     361          28 :   if (DEBUGLEVEL) timer_printf(&ti,"found %ld/%ld logs", f, nbi);
     362          28 :   if (f < maxss(3,maxss(p/2,nbi/p))) return NULL; /* Not enough logs found */
     363          28 :   return gerepilecopy(av, K);
     364             : }
     365             : 
     366             : static GEN
     367          28 : Flxq_log_rec(GEN W, GEN a, long r, GEN T, ulong p, GEN m)
     368             : {
     369          28 :   long AV = 0, u = 1;
     370          28 :   GEN g = a, b;
     371             :   pari_timer ti;
     372         308 :   while (!equali1(gel(W,u)))
     373         252 :    u++;
     374          28 :   b = cindex_Flx(u, r, p, T[1]);
     375             :   while(1)
     376          25 :   {
     377             :     long i, l;
     378             :     GEN V, F, E, Ao;
     379          53 :     timer_start(&ti);
     380          53 :     V = Flxq_log_find_rel(b, r, T, p, &g, &AV);
     381          53 :     if (DEBUGLEVEL>1) timer_printf(&ti,"%ld-smooth element",r);
     382          53 :     F = gel(V,1); E = gel(V,2);
     383          53 :     l = lg(F);
     384          53 :     Ao = gen_0;
     385         391 :     for(i=1; i<l; i++)
     386             :     {
     387         363 :       GEN R = gel(W,F[i]);
     388         363 :       if (signe(R)<=0)
     389          25 :         break;
     390         338 :       Ao = Fp_add(Ao, mulis(R, E[i]), m);
     391             :     }
     392          81 :     if (i==l) return subis(Ao,AV);
     393             :   }
     394             : }
     395             : 
     396             : static int
     397         126 : Flxq_log_use_index_cubic(GEN m, GEN T0, ulong p)
     398             : {
     399         126 :   pari_sp av = avma;
     400         126 :   long n = get_Flx_degree(T0), r, nb;
     401         126 :   GEN cost = smooth_best(p, n, &r, &nb);
     402         126 :   GEN cost_rho = sqrti(shifti(m,2));
     403         126 :   int use = (cost && gcmp(cost,cost_rho)<0);
     404         126 :   set_avma(av);
     405         126 :   return use;
     406             : }
     407             : 
     408             : static GEN
     409          14 : Flxq_log_index_cubic(GEN a0, GEN b0, GEN m, GEN T0, ulong p)
     410             : {
     411          14 :   long n = get_Flx_degree(T0), r, nb;
     412          14 :   pari_sp av = avma;
     413             :   struct Flxq_log_rel rel;
     414             :   long nbi;
     415             :   GEN W, M, S, T, a, b, Ao, Bo, e, C, R;
     416             :   pari_timer ti;
     417          14 :   GEN cost = smooth_best(p, n, &r, &nb);
     418          14 :   GEN cost_rho = sqrti(shifti(m,2));
     419          14 :   if (!cost || gcmp(cost,cost_rho)>=0) return gc_NULL(av);
     420          14 :   nbi = itos(ffsumnbirred(stoi(p), r));
     421          14 :   if (DEBUGLEVEL)
     422             :   {
     423           0 :     err_printf("Size FB=%ld, looking for %ld relations, %Ps tests needed\n", nbi, nb,cost);
     424           0 :     timer_start(&ti);
     425             :   }
     426          14 :   T = smallirred_Flx(p,n,get_Flx_var(T0));
     427             :   for(;;)
     428             :   {
     429          14 :     S = Flx_ffisom(T0,T,p);
     430          14 :     a = Flx_Flxq_eval(a0, S, T, p);
     431          14 :     b = Flx_Flxq_eval(b0, S, T, p);
     432          14 :     C = Flx_shift(pol1_Flx(get_Flx_var(T)), (n+2)/3);
     433          14 :     R = Flxq_powu(C,3,T,p);
     434          14 :     if (DEBUGLEVEL)
     435           0 :       timer_printf(&ti," model change: %Ps",Flx_to_ZX(T));
     436          14 :     rel.nbmax=2*nb;
     437          14 :     M = cgetg(rel.nbmax+1, t_VEC);
     438          14 :     rel.rel = M;
     439          14 :     rel.nbrel = 0; rel.r = r; rel.off = 3*upowuu(p,r);
     440          14 :     rel.nb = nbi; rel.nbexp = nb; rel.nbtest=0;
     441          14 :     Flxq_log_cubic(&rel, C, R, p);
     442          14 :     setlg(M,1+rel.nbrel);
     443          14 :     if (DEBUGLEVEL)
     444             :     {
     445           0 :       err_printf("\n");
     446           0 :       timer_printf(&ti," %ld relations, %ld generators (%ld tests)",rel.nbrel,rel.nb,rel.nbtest);
     447             :     }
     448          14 :     W = check_kernel(r, M, nbi, rel.off + rel.nb - nbi, T, p, m);
     449          14 :     if (W) break;
     450           0 :     if (DEBUGLEVEL) timer_start(&ti);
     451           0 :     smallirred_Flx_next(T,p);
     452             :   }
     453          14 :   if (DEBUGLEVEL) timer_start(&ti);
     454          14 :   Ao = Flxq_log_rec(W, a, r, T, p, m);
     455          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth element");
     456          14 :   Bo = Flxq_log_rec(W, b, r, T, p, m);
     457          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth generator");
     458          14 :   e = Fp_div(Ao, Bo, m);
     459          14 :   if (!Flx_equal(Flxq_pow(b0, e, T0, p), a0)) pari_err_BUG("Flxq_log");
     460          14 :   return gerepileupto(av, e);
     461             : }
     462             : 
     463       23468 : INLINE GEN Flx_frob(GEN u, ulong p) { return Flx_inflate(u, p); }
     464             : 
     465             : static GEN
     466       39682 : rel_Coppersmith(long r, GEN u, GEN v, long h, GEN R, long d, ulong p)
     467             : {
     468             :   GEN a, b, F, G, M;
     469       39682 :   if (degpol(Flx_gcd(u,v,p))) return NULL;
     470       39598 :   a = Flx_add(Flx_shift(u, h), v, p);
     471       39649 :   if (lgpol(a)==0 || !Flx_is_smooth(a, r, p)) return NULL;
     472        9687 :   b = Flx_add(Flx_mul(R, Flx_frob(u, p), p), Flx_shift(Flx_frob(v, p),d), p);
     473        9692 :   if (!Flx_is_smooth(b, r, p)) return NULL;
     474        2615 :   F = factorel(a, p); G = factorel(b, p);
     475        5230 :   M = mkmat2(vecsmall_concat(gel(F, 1), vecsmall_append(gel(G, 1), 2*p)),
     476        5230 :              vecsmall_concat(zv_z_mul(gel(F, 2),p), vecsmall_append(zv_neg(gel(G, 2)),d)));
     477        2615 :   return famatsmall_reduce(M);
     478             : }
     479             : 
     480             : GEN
     481        1449 : Flxq_log_Coppersmith_worker(GEN u, long i, GEN V, GEN R)
     482             : {
     483        1449 :   long r = V[1], h = V[2], d = V[3], p = V[4], dT = V[5];
     484        1449 :   pari_sp ltop = avma;
     485        1449 :   GEN v = zero_zv(dT+2);
     486        1452 :   GEN L = cgetg(2*i+1, t_VEC);
     487        1450 :   pari_sp av = avma;
     488             :   long j;
     489        1450 :   long nbtest=0, rel = 1;
     490        1450 :   ulong lu = Flx_lead(u), lv;
     491       80331 :   for (j=1; j<=i; j++)
     492             :   {
     493             :     GEN z;
     494       78877 :     Flx_cnext(v, p);
     495       78907 :     Flx_renormalize_inplace(v, dT+2);
     496       79016 :     lv = Flx_lead(v);
     497       79001 :     set_avma(av);
     498       78995 :     if (lu != 1 && lv != 1) continue;
     499       48562 :     if (degpol(Flx_gcd(u, v, p))!=0) continue;
     500       32696 :     if (lu==1)
     501             :     {
     502       20238 :       z = rel_Coppersmith(r, u, v, h, R, d, p);
     503       20258 :       nbtest++;
     504       20258 :       if (z) { gel(L, rel++) = z; av = avma; }
     505             :     }
     506       32716 :     if (i==j) continue;
     507       32532 :     if (lv==1)
     508             :     {
     509       19427 :       z = rel_Coppersmith(r, v, u, h, R, d, p);
     510       19440 :       nbtest++;
     511       19440 :       if (z) { gel(L, rel++) = z; av = avma; }
     512             :     }
     513             :   }
     514        1454 :   setlg(L,rel);
     515        1454 :   return gerepilecopy(ltop, mkvec2(stoi(nbtest), L));
     516             : }
     517             : 
     518             : static GEN
     519          14 : Flxq_log_Coppersmith(long nbrel, long r, GEN T, ulong p)
     520             : {
     521             :   pari_sp av;
     522          14 :   long dT = degpol(T);
     523          14 :   long h = dT/p, d = dT-(h*p);
     524          14 :   GEN R = Flx_sub(Flx_shift(pol1_Flx(T[1]), dT), T, p);
     525          14 :   GEN u = zero_zv(dT+2);
     526             :   GEN done;
     527          14 :   long nbtest = 0, rel = 0;
     528          14 :   GEN M = cgetg(nbrel+1, t_VEC);
     529          14 :   long i = 1;
     530          14 :   GEN worker = snm_closure(is_entry("_Flxq_log_Coppersmith_worker"),
     531             :                mkvec2(mkvecsmall5(r,h,d,p,dT), R));
     532             :   struct pari_mt pt;
     533          14 :   long running, pending = 0, stop=0;
     534          14 :   if (DEBUGLEVEL) err_printf("Coppersmith (R = %ld): ",degpol(R));
     535          14 :   mt_queue_start(&pt, worker);
     536          14 :   av = avma;
     537        1544 :   while ((running = !stop) || pending)
     538             :   {
     539             :     GEN L;
     540             :     long l, j;
     541        1516 :     Flx_cnext(u, p);
     542        1516 :     Flx_renormalize_inplace(u, dT+2);
     543        1516 :     mt_queue_submit(&pt, 0, running ? mkvec2(u, stoi(i)): NULL);
     544        1516 :     done = mt_queue_get(&pt, NULL, &pending);
     545        1516 :     if (!done) continue;
     546        1454 :     L = gel(done, 2); nbtest += itos(gel(done,1));
     547        1454 :     l = lg(L);
     548        1454 :     if (l > 1)
     549             :     {
     550        3427 :       for (j=1; j<l; j++)
     551             :       {
     552        2486 :         if (rel>nbrel) break;
     553        2429 :         gel(M,++rel) = gel(L,j);
     554        2429 :         if (DEBUGLEVEL && (rel&511UL)==0)
     555           0 :           err_printf("%ld%%[%ld] ",rel*100/nbrel,i);
     556             :       }
     557         998 :       av = avma;
     558             :     }
     559         456 :     else set_avma(av);
     560        1454 :     if (rel>nbrel) stop = 1;
     561        1454 :     i++;
     562             :   }
     563          14 :   mt_queue_end(&pt);
     564          14 :   if (DEBUGLEVEL) err_printf(": %ld tests\n", nbtest);
     565          14 :   return M;
     566             : }
     567             : 
     568             : static GEN Flxq_log_Coppersmith_d(GEN W, GEN g, long r, GEN T, ulong p, GEN mo);
     569             : 
     570             : static GEN
     571          75 : Flxq_log_from_rel(GEN W, GEN rel, long r, GEN T, ulong p, GEN m)
     572             : {
     573          75 :   pari_sp av = avma;
     574          75 :   GEN F = gel(rel,1), E = gel(rel,2), o = gen_0;
     575          75 :   long i, l = lg(F);
     576         527 :   for(i=1; i<l; i++)
     577             :   {
     578         452 :     GEN R = gel(W, F[i]);
     579         452 :     if (signe(R)==0) /* Already failed */
     580           0 :       return NULL;
     581         452 :     else if (signe(R)<0) /* Not yet tested */
     582             :     {
     583          21 :       setsigne(gel(W,F[i]),0);
     584          21 :       R = Flxq_log_Coppersmith_d(W, cindex_Flx(F[i],r,p,T[1]), r, T, p, m);
     585          21 :       if (!R) return NULL;
     586             :     }
     587         452 :     o = Fp_add(o, mulis(R, E[i]), m);
     588             :   }
     589          75 :   return gerepileuptoint(av, o);
     590             : }
     591             : 
     592             : static GEN
     593          75 : Flxq_log_Coppersmith_d(GEN W, GEN g, long r, GEN T, ulong p, GEN mo)
     594             : {
     595          75 :   pari_sp av = avma, av2;
     596          75 :   long dg = degpol(g), k = r-1, m = maxss((dg-k)/2,0);
     597          75 :   long i, j, l = dg-m, N;
     598          75 :   GEN v = cgetg(k+m+1,t_MAT);
     599          75 :   long dT = degpol(T);
     600          75 :   long h = dT/p, d = dT-h*p;
     601          75 :   GEN R = Flx_rem(Flx_shift(pol1_Flx(T[1]), dT), T, p);
     602          75 :   GEN z = Flx_rem(Flx_shift(pol1_Flx(T[1]), h), g, p);
     603         484 :   for(i=1; i<=k+m; i++)
     604             :   {
     605         409 :     gel(v,i) = Flx_to_Flv(Flx_shift(z,-l),m);
     606         409 :     z = Flx_rem(Flx_shift(z,1),g,p);
     607             :   }
     608          75 :   v = Flm_ker(v,p);
     609         430 :   for(i=1; i<=k; i++)
     610         355 :     gel(v,i) = Flv_to_Flx(gel(v,i),T[1]);
     611          75 :   N = upowuu(p,k);
     612          75 :   av2 = avma;
     613        2046 :   for (i=1; i<N; i++)
     614             :   {
     615             :     GEN p0,q,qh,a,b;
     616        2046 :     ulong el = i;
     617        2046 :     set_avma(av2);
     618        2046 :     q = pol0_Flx(T[1]);
     619       11883 :     for (j=1; j<=k; j++)
     620             :     {
     621        9837 :       ulong r = el % p;
     622        9837 :       el /= p;
     623        9837 :       if (r) q = Flx_add(q, Flx_Fl_mul(gel(v,j), r, p), p);
     624             :     }
     625        2046 :     qh = Flx_shift(q, h);
     626        2046 :     p0 = Flx_rem(qh, g, p);
     627        2046 :     b = Flx_sub(Flx_mul(R, Flx_frob(q, p), p), Flx_shift(Flx_frob(p0, p), d), p);
     628        2046 :     if (lgpol(b)==0 || !Flx_is_smooth(b, r, p)) continue;
     629         123 :     a = Flx_div(Flx_sub(qh, p0, p), g, p);
     630         123 :     if (degpol(Flx_gcd(a, q, p)) &&  degpol(Flx_gcd(a, p0 ,p)))
     631          12 :       continue;
     632         111 :     if (!(lgpol(a)==0 || !Flx_is_smooth(a, r, p)))
     633             :     {
     634          75 :       GEN F = factorel(b, p);
     635          75 :       GEN G = factorel(a, p);
     636          75 :       GEN FG = vecsmall_concat(vecsmall_append(gel(F, 1), 2*p), gel(G, 1));
     637         150 :       GEN E  = vecsmall_concat(vecsmall_append(gel(F, 2), -d),
     638         150 :           zv_z_mul(gel(G, 2),-p));
     639          75 :       GEN R  = famatsmall_reduce(mkmat2(FG, E));
     640          75 :       GEN l  = Flxq_log_from_rel(W, R, r, T, p, mo);
     641          75 :       if (!l) continue;
     642          75 :       l = Fp_divu(l,p,mo);
     643          75 :       if (dg <= r)
     644             :       {
     645          27 :         long idx = Flx_cindex(g, p);
     646          27 :         affii(l, gel(W, idx));
     647          27 :         if (DEBUGLEVEL>1) err_printf("Found %lu\n", idx);
     648             :       }
     649          75 :       return gerepileuptoint(av, l);
     650             :     }
     651             :   }
     652           0 :   set_avma(av);
     653           0 :   return NULL;
     654             : }
     655             : 
     656             : static GEN
     657          28 : Flxq_log_Coppersmith_rec(GEN W, long r2, GEN a, long r, GEN T, ulong p, GEN m)
     658             : {
     659          28 :   GEN b = polx_Flx(T[1]);
     660          28 :   long AV = 0;
     661          28 :   GEN g = a, bad = pol0_Flx(T[1]);
     662             :   pari_timer ti;
     663             :   while(1)
     664           0 :   {
     665             :     long i, l;
     666             :     GEN V, F, E, Ao;
     667          28 :     timer_start(&ti);
     668          28 :     V = Flxq_log_find_rel(b, r2, T, p, &g, &AV);
     669          28 :     if (DEBUGLEVEL>1) timer_printf(&ti,"%ld-smooth element",r2);
     670          28 :     F = gel(V,1); E = gel(V,2);
     671          28 :     l = lg(F);
     672          28 :     Ao = gen_0;
     673         216 :     for(i=1; i<l; i++)
     674             :     {
     675         188 :       GEN Fi = cindex_Flx(F[i], r2, p, T[1]);
     676             :       GEN R;
     677         188 :       if (degpol(Fi) <= r)
     678             :       {
     679         140 :         if (signe(gel(W,F[i]))==0)
     680           0 :           break;
     681         140 :         else if (signe(gel(W,F[i]))<0)
     682             :         {
     683           6 :           setsigne(gel(W,F[i]),0);
     684           6 :           R = Flxq_log_Coppersmith_d(W,Fi,r,T,p,m);
     685             :         } else
     686         134 :           R = gel(W,F[i]);
     687             :       }
     688             :       else
     689             :       {
     690          48 :         if (Flx_equal(Fi,bad)) break;
     691          48 :         R = Flxq_log_Coppersmith_d(W,Fi,r,T,p,m);
     692          48 :         if (!R) bad = Fi;
     693             :       }
     694         188 :       if (!R) break;
     695         188 :       Ao = Fp_add(Ao, mulis(R, E[i]), m);
     696             :     }
     697          56 :     if (i==l) return subis(Ao,AV);
     698             :   }
     699             : }
     700             : 
     701             : static GEN
     702          14 : Flxq_log_index_Coppersmith(GEN a0, GEN b0, GEN m, GEN T0, ulong p)
     703             : {
     704          14 :   pari_sp av = avma;
     705          14 :   GEN  M, S, a, b, Ao=NULL, Bo=NULL, W, e;
     706             :   pari_timer ti;
     707          14 :   double rf = p ==3 ? 1.2 : .9;
     708          14 :   long n = degpol(T0), r = (long) sqrt(n*rf);
     709             :   GEN T;
     710          14 :   long r2 = 3*r/2;
     711          14 :   long nbi = itos(ffsumnbirred(utoi(p), r)), nbrel=nbi*5/4;
     712          14 :   if (DEBUGLEVEL)
     713             :   {
     714           0 :     err_printf("Coppersmith: Parameters r=%ld r2=%ld\n", r,r2);
     715           0 :     err_printf("Coppersmith: Size FB=%ld rel. needed=%ld\n", nbi, nbrel);
     716           0 :     timer_start(&ti);
     717             :   }
     718          14 :   T = smallirred_Flx(p,n,get_Flx_var(T0));
     719          14 :   S = Flx_ffisom(T0,T,p);
     720          14 :   a = Flx_Flxq_eval(a0, S, T, p);
     721          14 :   b = Flx_Flxq_eval(b0, S, T, p);
     722          14 :   if (DEBUGLEVEL) timer_printf(&ti,"model change");
     723          14 :   M = Flxq_log_Coppersmith(nbrel, r, T, p);
     724          14 :   if (DEBUGLEVEL) timer_printf(&ti,"relations");
     725          14 :   W = check_kernel(r, M, nbi, 3*upowuu(p,r), T, p, m);
     726          14 :   timer_start(&ti);
     727          14 :   Ao = Flxq_log_Coppersmith_rec(W, r2, a, r, T, p, m);
     728          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth element");
     729          14 :   Bo = Flxq_log_Coppersmith_rec(W, r2, b, r, T, p, m);
     730          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth generator");
     731          14 :   e = Fp_div(Ao, Bo, m);
     732          14 :   if (!Flx_equal(Flxq_pow(b0,e,T0,p),a0)) pari_err_BUG("Flxq_log");
     733          14 :   return gerepileupto(av, e);
     734             : }
     735             : 
     736             : GEN
     737          28 : Flxq_log_index(GEN a, GEN b, GEN m, GEN T, ulong p)
     738             : {
     739          28 :   long d = get_Flx_degree(T);
     740          28 :   if (p==3 || (p==5 && d>41))
     741          14 :     return Flxq_log_index_Coppersmith(a, b, m, T, p);
     742          14 :   else    return Flxq_log_index_cubic(a, b, m, T, p);
     743             : }
     744             : 
     745             : int
     746       37036 : Flxq_log_use_index(GEN m, GEN T, ulong p)
     747             : {
     748       37036 :   long d = get_Flx_degree(T);
     749       37036 :   if (p==3 || (p==5 && d>41))
     750        9975 :     return 1;
     751       27061 :   else if (d<=4 || d==6)
     752       26935 :     return 0;
     753             :   else
     754         126 :     return Flxq_log_use_index_cubic(m, T, p);
     755             : }

Generated by: LCOV version 1.13