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 - Flxq_log.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19352-1b11b25) Lines: 419 446 93.9 %
Date: 2016-08-25 06:11:27 Functions: 27 27 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      378644 : Flx_cindex(GEN P, ulong p)
      23             : {
      24      378644 :   long d = degpol(P), i;
      25      378644 :   ulong s = 0, p2 = (p-1)>>1;
      26     1711381 :   for (i = 0; i <= d; ++i)
      27             :   {
      28     1332737 :     ulong x = P[d-i+2];
      29     1332737 :     if (x<=p2) x = 2*x; else x = 1+2*(p-1-x);
      30     1332737 :     s = p*s+x;
      31             :   }
      32      378644 :   return s;
      33             : }
      34             : 
      35             : /* Compute the polynomial immediately after t for the [[ order */
      36             : 
      37             : static void
      38      367248 : Flx_cnext(GEN t, ulong p)
      39             : {
      40             :   long i;
      41      367248 :   long p2 = p>>1;
      42      457352 :   for(i=2;;i++)
      43      457352 :     if (t[i]==p2)
      44       90104 :       t[i]=0;
      45             :     else
      46             :     {
      47      367248 :       t[i] = t[i]<p2 ? p-1-t[i]: p-t[i];
      48      367248 :       break;
      49       90104 :     }
      50      367248 : }
      51             : 
      52             : static int
      53          21 : has_deg1_auto(GEN T, ulong p)
      54             : {
      55          21 :   long i, n = degpol(T);
      56          21 :   GEN a = polx_Flx(get_Flx_var(T));
      57         441 :   for (i=1; i<n; i++)
      58             :   {
      59         420 :     a = Flxq_powu(a, p, T, p);
      60         420 :     if (degpol(a)==1) return 1;
      61             :   }
      62          21 :   return 0;
      63             : }
      64             : 
      65             : static void
      66         581 : smallirred_Flx_next(GEN a, long p)
      67             : {
      68             :   do
      69             :   {
      70             :     long i;
      71         756 :     for(i=2;;i++)
      72         756 :       if (++a[i]==p) a[i]=0;
      73         756 :       else break;
      74         581 :   } while (!Flx_is_irred(a, p) || has_deg1_auto(a,p) );
      75          21 : }
      76             : 
      77             : /* Avoid automorphisms of degree 1 */
      78             : static GEN
      79          21 : smallirred_Flx(long p, ulong n, long sv)
      80             : {
      81          21 :   GEN a = zero_zv(n+2);
      82          21 :   a[1] = sv; a[3] = 1; a[n+2] = 1;
      83          21 :   smallirred_Flx_next(a, p);
      84          21 :   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        3318 : cindex_Flx(long c, long d, ulong p, long v)
      98             : {
      99        3318 :   GEN P = cgetg(d+3, t_VECSMALL);
     100             :   long i;
     101        3318 :   P[1] = v;
     102       20839 :   for (i = 0; i <= d; ++i)
     103             :   {
     104       17521 :     ulong x = c%p;
     105       17521 :     P[i+2] = (x&1) ? p-1-(x>>1) : x>>1;
     106       17521 :     c/=p;
     107             :   }
     108        3318 :   return Flx_renormalize(P, d+3);
     109             : }
     110             : 
     111             : static GEN
     112        7252 : factorel(GEN h, ulong p)
     113             : {
     114        7252 :   GEN F = Flx_factcantor(h, p, 0);
     115        7252 :   GEN F1 = gel(F, 1), F2 = gel(F, 2);
     116        7252 :   long i, l1 = lg(F1)-1;
     117        7252 :   GEN p2 = cgetg(l1+1, t_VECSMALL);
     118        7252 :   GEN e2 = cgetg(l1+1, t_VECSMALL);
     119       35553 :   for (i = 1; i <= l1; ++i)
     120             :   {
     121       28301 :     p2[i] = Flx_cindex(gel(F1, i), p);
     122       28301 :     e2[i] = F2[i];
     123             :   }
     124        7252 :   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 avma = *av;
     146       74256 :   return r->nbrel==r->nb || r->nbrel==r->nbmax;
     147             : }
     148             : 
     149             : static void
     150      367276 : Flx_renormalize_inplace(GEN x, long lx)
     151             : {
     152             :   long i;
     153     3024273 :   for (i = lx-1; i>1; i--)
     154     3021459 :     if (x[i]) break;
     155      367276 :   setlg(x, i+1);
     156      367276 : }
     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          14 :   long i,j,k, dh=0;
     173        2800 :   for(i=0; ; i++, Flx_cnext(a, p))
     174             :   {
     175        2800 :     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 :       dh = maxss(dh,degpol(h));
     193       71456 :       if (Flx_addifsmooth3(&av, r, h, i, j, k, p)) return;
     194             :     }
     195        2786 :   }
     196             : }
     197             : 
     198             : static GEN
     199          63 : Flxq_log_find_rel(GEN b, long r, GEN T, ulong p, GEN *g, long *e)
     200             : {
     201          63 :   pari_sp av = avma;
     202             :   while (1)
     203             :   {
     204             :     GEN M;
     205        3115 :     *g = Flxq_mul(*g, b, T, p); (*e)++;
     206        3115 :     M = Flx_halfgcd(*g,T,p);
     207        3115 :     if (Flx_is_smooth(gcoeff(M,1,1), r, p))
     208             :     {
     209         413 :       GEN z = Flx_add(Flx_mul(gcoeff(M,1,1),*g,p), Flx_mul(gcoeff(M,1,2),T,p),p);
     210         413 :       if (Flx_is_smooth(z, r, p))
     211             :       {
     212          63 :         GEN F = factorel(z, p);
     213          63 :         GEN G = factorel(gcoeff(M,1,1), p);
     214         126 :         GEN rel = mkmat2(vecsmall_concat(gel(F, 1),gel(G, 1)),
     215         126 :                          vecsmall_concat(gel(F, 2),zv_neg(gel(G, 2))));
     216         126 :         gerepileall(av,2,g,&rel); return rel;
     217             :       }
     218             :     }
     219        3052 :     if (gc_needed(av,2))
     220             :     {
     221           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"Flxq_log_find_rel");
     222           0 :       *g = gerepilecopy(av, *g);
     223             :     }
     224        3052 :   }
     225             : }
     226             : 
     227             : /* Generalised Odlyzko formulae ( EUROCRYPT '84, LNCS 209, pp. 224-314, 1985. ) */
     228             : /* Return the number of monic, k smooth, degree n polynomials for k=1..r */
     229             : static GEN
     230        1708 : smoothness_vec(ulong p, long r, long n)
     231             : {
     232             :   long i,j,k;
     233        1708 :   GEN R = cgetg(r+1, t_VEC);
     234        1708 :   GEN V = cgetg(n+1, t_VEC);
     235       18571 :   for (j = 1; j <= n; ++j)
     236       16863 :     gel(V, j) =  binomialuu(p+j-1,j);
     237        1708 :   gel(R, 1) = gel(V, n);
     238        4816 :   for (k = 2; k <= r; ++k)
     239             :   {
     240        3108 :     GEN W = cgetg(n+1, t_VEC);
     241        3108 :     GEN Ik = ffnbirred(utoi(p),k);
     242       36029 :     for (j = 1; j <= n; ++j)
     243             :     {
     244       32921 :       long l = j/k;
     245       32921 :       GEN s = gen_0;
     246       32921 :       pari_sp av2 = avma;
     247       32921 :       if (l*k == j)
     248             :       {
     249       10801 :         s = binomial(addis(Ik,l-1), l);
     250       10801 :         l--;
     251             :       }
     252      119847 :       for (i = 0; i <= l; ++i)
     253       86926 :         s = addii(s, mulii(gel(V, j-k*i), binomial(addis(Ik,i-1), i)));
     254       32921 :       gel(W, j) = gerepileuptoint(av2, s);
     255             :     }
     256        3108 :     V = W;
     257        3108 :     gel(R, k) = gel(V, n);
     258             :   }
     259        1708 :   return R;
     260             : }
     261             : 
     262             : /* Solve N^2*pr/6 + N*prC = N+fb
     263             :    N^2*pr/6 + N*(prC-1) -fb = 0
     264             :  */
     265             : 
     266             : static GEN
     267        1379 : smooth_cost(GEN fb, GEN pr, GEN prC)
     268             : {
     269        1379 :   GEN a = gdivgs(pr,6);
     270        1379 :   GEN b = gsubgs(prC,1);
     271        1379 :   GEN c = gneg(fb);
     272        1379 :   GEN vD = gsqrt(gsub(gsqr(b),gmul2n(gmul(a,c),2)),BIGDEFAULTPREC);
     273        1379 :   return ceil_safe(gdiv(gsub(vD,b),gmul2n(a,1)));
     274             : }
     275             : 
     276             : /* Return best choice of r.
     277             :    We loop over d until there is sufficiently many triples (a,b,c) (a+b+c=0)
     278             :    of degree <=d with respect to the probability of smoothness of (a*b-c^2)*C
     279             :  */
     280             : 
     281             : static GEN
     282         140 : smooth_best(long p, long n, long *pt_r, long *pt_nb)
     283             : {
     284         140 :   pari_sp av = avma, av2;
     285         140 :   GEN bestc = NULL;
     286         140 :   long bestr = 0, bestFB = 0;
     287         140 :   long r,d, dC = (n+2)/3;
     288         469 :   for (r = 1; r < dC; ++r)
     289             :   {
     290         329 :     GEN fb = ffsumnbirred(utoi(p), r);
     291         329 :     GEN smoothC = smoothness_vec(p,r,dC);
     292         329 :     GEN prC = gdiv(gel(smoothC,r), powuu(p,dC));
     293         329 :     ulong rels = 0;
     294         329 :     av2 = avma;
     295        2954 :     for(d=0; d<dC && rels < ULONG_MAX; d++)
     296             :     {
     297             :       GEN c;
     298        1379 :       long dt = dC+2*d;
     299        1379 :       GEN smooth = smoothness_vec(p,r,dt);
     300        1379 :       GEN pr = gdiv(gel(smooth,r), powuu(p,dt));
     301        1379 :       GEN FB = addii(fb,powuu(p,d));
     302        1379 :       GEN N = smooth_cost(subiu(FB,rels),pr,prC);
     303        1379 :       GEN Nmax = powuu(p,d+1);
     304        1379 :       if (gcmp(N,Nmax) >= 0)
     305             :       {
     306        1148 :         rels = itou_or_0(addui(rels, gceil(gmul(gdivgs(sqri(Nmax),6),pr))));
     307        1148 :         if (!rels) rels = ULONG_MAX;
     308        1148 :         avma = av2;
     309        1148 :         continue;
     310             :       }
     311         231 :       c = gdivgs(addii(powuu(p,2*d),sqri(N)),6);
     312         231 :       FB = addii(FB,N);
     313         231 :       if ((!bestc || gcmp(gmul2n(c,r), gmul2n(bestc,bestr)) < 0))
     314             :       {
     315         154 :         if (DEBUGLEVEL)
     316           0 :           err_printf("r=%ld d=%ld fb=%Ps early rels=%lu P=%.5Pe -> C=%.5Pe \n",
     317             :                       r, dt, FB, rels, pr, c);
     318         154 :         bestc = c;
     319         154 :         bestr = r;
     320         154 :         bestFB = itos_or_0(FB);
     321             :       }
     322         231 :       break;
     323             :     }
     324             :   }
     325         140 :   *pt_r=bestr;
     326         140 :   *pt_nb=bestFB;
     327         140 :   return bestc ? gerepileupto(av, gceil(bestc)): NULL;
     328             : }
     329             : 
     330             : static GEN
     331          21 : check_kernel(long r, GEN M, long nbi, long nbrow, GEN T, ulong p, GEN m)
     332             : {
     333          21 :   pari_sp av = avma;
     334          21 :   long N = 3*upowuu(p, r);
     335          21 :   GEN K = FpMs_leftkernel_elt(M, nbrow, m);
     336          21 :   long i, f=0;
     337          21 :   long lm = lgefint(m), u=1;
     338             :   GEN g;
     339          21 :   GEN idx = diviiexact(subis(powuu(p,degpol(T)),1),m);
     340             :   pari_timer ti;
     341          21 :   if (DEBUGLEVEL) timer_start(&ti);
     342         203 :   while (signe(gel(K,u))==0)
     343         161 :    u++;
     344          21 :   K = FpC_Fp_mul(K, Fp_inv(gel(K, u), m), m);
     345          21 :   g = Flxq_pow(cindex_Flx(u, r, p, T[1]), idx, T, p);
     346          21 :   setlg(K, N);
     347       31353 :   for (i=1; i<N; i++)
     348             :   {
     349       31332 :     GEN k = gel(K,i);
     350       31332 :     pari_sp av = avma;
     351       34475 :     long t = signe(k) && Flx_equal(Flxq_pow(g, k, T, p),
     352        3143 :                                    Flxq_pow(cindex_Flx(i,r,p,T[1]), idx, T, p));
     353       31332 :     avma = av;
     354       31332 :     if (!t)
     355       28189 :       gel(K,i) = cgetineg(lm);
     356             :     else
     357        3143 :       f++;
     358             :   }
     359          21 :   if (DEBUGLEVEL) timer_printf(&ti,"found %ld/%ld logs", f, nbi);
     360          21 :   if (f < maxss(3,maxss(p/2,nbi/p))) return NULL; /* Not enough logs found */
     361          21 :   return gerepilecopy(av, K);
     362             : }
     363             : 
     364             : static GEN
     365          28 : Flxq_log_rec(GEN W, GEN a, long r, GEN T, ulong p, GEN m)
     366             : {
     367          28 :   long AV = 0, u = 1;
     368          28 :   GEN g = a, b;
     369             :   pari_timer ti;
     370         308 :   while (!equali1(gel(W,u)))
     371         252 :    u++;
     372          28 :   b = cindex_Flx(u, r, p, T[1]);
     373             :   while(1)
     374             :   {
     375             :     long i, l;
     376             :     GEN V, F, E, Ao;
     377          35 :     timer_start(&ti);
     378          35 :     V = Flxq_log_find_rel(b, r, T, p, &g, &AV);
     379          35 :     if (DEBUGLEVEL>1) timer_printf(&ti,"%ld-smooth element",r);
     380          35 :     F = gel(V,1); E = gel(V,2);
     381          35 :     l = lg(F);
     382          35 :     Ao = gen_0;
     383         252 :     for(i=1; i<l; i++)
     384             :     {
     385         224 :       GEN R = gel(W,F[i]);
     386         224 :       if (signe(R)<=0)
     387           7 :         break;
     388         217 :       Ao = Fp_add(Ao, mulis(R, E[i]), m);
     389             :     }
     390          63 :     if (i==l) return subis(Ao,AV);
     391           7 :   }
     392             : }
     393             : 
     394             : static int
     395         126 : Flxq_log_use_index_cubic(GEN m, GEN T0, ulong p)
     396             : {
     397         126 :   pari_sp av = avma;
     398         126 :   long n = get_Flx_degree(T0), r, nb;
     399         126 :   GEN cost = smooth_best(p, n, &r, &nb);
     400         126 :   GEN cost_rho = sqrti(shifti(m,2));
     401         126 :   int use = (cost && gcmp(cost,cost_rho)<0);
     402         126 :   avma = av;
     403         126 :   return use;
     404             : }
     405             : 
     406             : static GEN
     407          14 : Flxq_log_index_cubic(GEN a0, GEN b0, GEN m, GEN T0, ulong p)
     408             : {
     409          14 :   long n = get_Flx_degree(T0), r, nb;
     410          14 :   pari_sp av = avma;
     411             :   struct Flxq_log_rel rel;
     412             :   long nbi;
     413             :   GEN W, M, S, T, a, b, Ao, Bo, e, C, R;
     414             :   pari_timer ti;
     415          14 :   GEN cost = smooth_best(p, n, &r, &nb);
     416          14 :   GEN cost_rho = sqrti(shifti(m,2));
     417          14 :   if (!cost || gcmp(cost,cost_rho)>=0) { avma = av; return NULL; }
     418          14 :   nbi = itos(ffsumnbirred(stoi(p), r));
     419          14 :   if (DEBUGLEVEL)
     420             :   {
     421           0 :     err_printf("Size FB=%ld, looking for %ld relations, %Ps tests needed\n", nbi, nb,cost);
     422           0 :     timer_start(&ti);
     423             :   }
     424          14 :   T = smallirred_Flx(p,n,get_Flx_var(T0));
     425             :   for(;;)
     426             :   {
     427          14 :     S = Flx_ffisom(T0,T,p);
     428          14 :     a = Flx_Flxq_eval(a0, S, T, p);
     429          14 :     b = Flx_Flxq_eval(b0, S, T, p);
     430          14 :     C = Flx_shift(pol1_Flx(get_Flx_var(T)), (n+2)/3);
     431          14 :     R = Flxq_powu(C,3,T,p);
     432          14 :     if (DEBUGLEVEL)
     433           0 :       timer_printf(&ti," model change: %Ps",Flx_to_ZX(T));
     434          14 :     rel.nbmax=2*nb;
     435          14 :     M = cgetg(rel.nbmax+1, t_VEC);
     436          14 :     rel.rel = M;
     437          14 :     rel.nbrel = 0; rel.r = r; rel.off = 3*upowuu(p,r);
     438          14 :     rel.nb = nbi; rel.nbexp = nb; rel.nbtest=0;
     439          14 :     Flxq_log_cubic(&rel, C, R, p);
     440          14 :     setlg(M,1+rel.nbrel);
     441          14 :     if (DEBUGLEVEL)
     442             :     {
     443           0 :       err_printf("\n");
     444           0 :       timer_printf(&ti," %ld relations, %ld generators (%ld tests)",rel.nbrel,rel.nb,rel.nbtest);
     445             :     }
     446          14 :     W = check_kernel(r, M, nbi, rel.off + rel.nb - nbi, T, p, m);
     447          14 :     if (W) break;
     448           0 :     if (DEBUGLEVEL) timer_start(&ti);
     449           0 :     smallirred_Flx_next(T,p);
     450           0 :   }
     451          14 :   if (DEBUGLEVEL) timer_start(&ti);
     452          14 :   Ao = Flxq_log_rec(W, a, r, T, p, m);
     453          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth element");
     454          14 :   Bo = Flxq_log_rec(W, b, r, T, p, m);
     455          14 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth generator");
     456          14 :   e = Fp_div(Ao, Bo, m);
     457          14 :   if (!Flx_equal(Flxq_pow(b0, e, T0, p), a0)) pari_err_BUG("Flxq_log");
     458          14 :   return gerepileupto(av, e);
     459             : }
     460             : 
     461        6958 : INLINE GEN Flx_frob(GEN u, ulong p) { return Flx_inflate(u, p); }
     462             : 
     463             : static GEN
     464        9996 : rel_Coppersmith(long r, GEN u, GEN v, long h, GEN R, long d, ulong p)
     465             : {
     466             :   GEN a, b, F, G, M;
     467        9996 :   if (degpol(Flx_gcd(u,v,p))) return NULL;
     468        6881 :   a = Flx_add(Flx_shift(u, h), v, p);
     469        6881 :   if (lgpol(a)==0 || !Flx_is_smooth(a, r, p)) return NULL;
     470        2835 :   b = Flx_add(Flx_mul(R, Flx_frob(u, p), p), Flx_shift(Flx_frob(v, p),d), p);
     471        2835 :   if (!Flx_is_smooth(b, r, p)) return NULL;
     472         707 :   F = factorel(a, p); G = factorel(b, p);
     473        1414 :   M = mkmat2(vecsmall_concat(gel(F, 1), vecsmall_append(gel(G, 1), 2*p)),
     474        1414 :              vecsmall_concat(zv_z_mul(gel(F, 2),p), vecsmall_append(zv_neg(gel(G, 2)),d)));
     475         707 :   return famatsmall_reduce(M);
     476             : }
     477             : 
     478             : static GEN
     479           7 : Flxq_log_Coppersmith(long nbrel, long r, GEN T, ulong p)
     480             : {
     481           7 :   long dT = degpol(T);
     482           7 :   long h = dT/p, d = dT-(h*p);
     483           7 :   GEN R = Flx_sub(Flx_shift(pol1_Flx(T[1]), dT), T, p);
     484           7 :   GEN u = zero_zv(dT+2), v = zero_zv(dT+2);
     485           7 :   long nbtest = 0, rel = 0;
     486           7 :   GEN M = cgetg(nbrel+1, t_VEC);
     487           7 :   pari_sp av = avma;
     488             :   long i, j;
     489           7 :   if (DEBUGLEVEL) err_printf("Coppersmith (R = %ld): ",degpol(R));
     490         448 :   for (i=1; ; i++)
     491             :   {
     492         448 :     Flx_cnext(u, p);
     493         448 :     Flx_renormalize_inplace(u, dT+2);
     494         448 :     for(j=2; j<lg(v); j++) v[j] = 0;
     495       14126 :     for(j=1; j<i; j++)
     496             :     {
     497             :       GEN z;
     498       13685 :       Flx_cnext(v, p);
     499       13685 :       Flx_renormalize_inplace(v, dT+2);
     500       13685 :       avma = av;
     501       13685 :       if (Flx_lead(u)==1)
     502             :       {
     503        5495 :         z = rel_Coppersmith(r, u, v, h, R, d, p);
     504        5495 :         nbtest++;
     505        5495 :         if (z)
     506             :         {
     507         322 :           gel(M,++rel) = gerepilecopy(av, z); av = avma;
     508         322 :           if (DEBUGLEVEL && (rel&511UL)==0)
     509           0 :             err_printf("%ld%%[%ld] ",rel*100/nbrel,i);
     510             :         }
     511        5495 :         if (rel>nbrel) break;
     512             :       }
     513       13678 :       if (i==j) continue;
     514       13678 :       if (Flx_lead(v)==1)
     515             :       {
     516        4501 :         z = rel_Coppersmith(r, v, u, h, R, d, p);
     517        4501 :         nbtest++;
     518        4501 :         if (z)
     519             :         {
     520         385 :           gel(M,++rel) = gerepilecopy(av, z); av = avma;
     521         385 :           if (DEBUGLEVEL && (rel&511UL)==0)
     522           0 :             err_printf("%ld%%[%ld] ",rel*100/nbrel,i);
     523             :         }
     524        4501 :         if (rel>nbrel) break;
     525             :       }
     526             :     }
     527         448 :     if (rel>nbrel) break;
     528         441 :   }
     529           7 :   if (DEBUGLEVEL) err_printf(": %ld tests\n", nbtest);
     530           7 :   return M;
     531             : }
     532             : 
     533             : static GEN Flxq_log_Coppersmith_d(GEN W, GEN g, long r, GEN T, ulong p, GEN mo);
     534             : 
     535             : static GEN
     536          21 : Flxq_log_from_rel(GEN W, GEN rel, long r, GEN T, ulong p, GEN m)
     537             : {
     538          21 :   pari_sp av = avma;
     539          21 :   GEN F = gel(rel,1), E = gel(rel,2), o = gen_0;
     540          21 :   long i, l = lg(F);
     541         112 :   for(i=1; i<l; i++)
     542             :   {
     543          91 :     GEN R = gel(W, F[i]);
     544          91 :     if (signe(R)==0) /* Already failed */
     545           0 :       return NULL;
     546          91 :     else if (signe(R)<0) /* Not yet tested */
     547             :     {
     548           0 :       setsigne(gel(W,F[i]),0);
     549           0 :       R = Flxq_log_Coppersmith_d(W, cindex_Flx(F[i],r,p,T[1]), r, T, p, m);
     550           0 :       if (!R) return NULL;
     551             :     }
     552          91 :     o = Fp_add(o, mulis(R, E[i]), m);
     553             :   }
     554          21 :   return gerepileuptoint(av, o);
     555             : }
     556             : 
     557             : static GEN
     558          28 : Flxq_log_Coppersmith_d(GEN W, GEN g, long r, GEN T, ulong p, GEN mo)
     559             : {
     560          28 :   pari_sp av = avma, av2;
     561          28 :   long dg = degpol(g), k = r-1, m = maxss((dg-k)/2,0);
     562          28 :   long i, j, l = dg-m, N;
     563          28 :   GEN v = cgetg(k+m+1,t_MAT);
     564          28 :   long dT = degpol(T);
     565          28 :   long h = dT/p, d = dT-h*p;
     566          28 :   GEN R = Flx_rem(Flx_shift(pol1_Flx(T[1]), dT), T, p);
     567          28 :   GEN z = Flx_rem(Flx_shift(pol1_Flx(T[1]), h), g, p);
     568         168 :   for(i=1; i<=k+m; i++)
     569             :   {
     570         140 :     gel(v,i) = Flx_to_Flv(Flx_shift(z,-l),m);
     571         140 :     z = Flx_rem(Flx_shift(z,1),g,p);
     572             :   }
     573          28 :   v = Flm_ker(v,p);
     574         140 :   for(i=1; i<=k; i++)
     575         112 :     gel(v,i) = Flv_to_Flx(gel(v,i),T[1]);
     576          28 :   N = upowuu(p,k);
     577          28 :   av2 = avma;
     578         651 :   for (i=1; i<N; i++)
     579             :   {
     580             :     GEN p0,q,qh,a,b;
     581         644 :     ulong el = i;
     582         644 :     avma = av2;
     583         644 :     q = pol0_Flx(T[1]);
     584        3220 :     for (j=1; j<=k; j++)
     585             :     {
     586        2576 :       ulong r = el % p;
     587        2576 :       el /= p;
     588        2576 :       if (r) q = Flx_add(q, Flx_Fl_mul(gel(v,j), r, p), p);
     589             :     }
     590         644 :     qh = Flx_shift(q, h);
     591         644 :     p0 = Flx_rem(qh, g, p);
     592         644 :     b = Flx_sub(Flx_mul(R, Flx_frob(q, p), p), Flx_shift(Flx_frob(p0, p), d), p);
     593         644 :     if (lgpol(b)==0 || !Flx_is_smooth(b, r, p)) continue;
     594          21 :     a = Flx_div(Flx_sub(qh, p0, p), g, p);
     595          21 :     if (degpol(Flx_gcd(a, q, p)) &&  degpol(Flx_gcd(a, p0 ,p)))
     596           0 :       continue;
     597          21 :     if (!(lgpol(a)==0 || !Flx_is_smooth(a, r, p)))
     598             :     {
     599          21 :       GEN F = factorel(b, p);
     600          21 :       GEN G = factorel(a, p);
     601          21 :       GEN FG = vecsmall_concat(vecsmall_append(gel(F, 1), 2*p), gel(G, 1));
     602          42 :       GEN E  = vecsmall_concat(vecsmall_append(gel(F, 2), -d),
     603          42 :           zv_z_mul(gel(G, 2),-p));
     604          21 :       GEN R  = famatsmall_reduce(mkmat2(FG, E));
     605          21 :       GEN l  = Flxq_log_from_rel(W, R, r, T, p, mo);
     606          21 :       if (!l) continue;
     607          21 :       l = Fp_div(l,utoi(p),mo);
     608          21 :       if (dg <= r)
     609             :       {
     610           0 :         affii(l,gel(W,g[2]));
     611           0 :         if (DEBUGLEVEL>1) err_printf("Found %lu\n", g[2]);
     612             :       }
     613          21 :       return gerepileuptoint(av, l);
     614             :     }
     615             :   }
     616           7 :   avma = av;
     617           7 :   return NULL;
     618             : }
     619             : 
     620             : static GEN
     621          14 : Flxq_log_Coppersmith_rec(GEN W, long r2, GEN a, long r, GEN T, ulong p, GEN m)
     622             : {
     623          14 :   GEN b = polx_Flx(T[1]);
     624          14 :   long AV = 0;
     625          14 :   GEN g = a, bad = pol0_Flx(T[1]);
     626             :   pari_timer ti;
     627             :   while(1)
     628             :   {
     629             :     long i, l;
     630             :     GEN V, F, E, Ao;
     631          28 :     timer_start(&ti);
     632          28 :     V = Flxq_log_find_rel(b, r2, T, p, &g, &AV);
     633          28 :     if (DEBUGLEVEL>1) timer_printf(&ti,"%ld-smooth element",r2);
     634          28 :     F = gel(V,1); E = gel(V,2);
     635          28 :     l = lg(F);
     636          28 :     Ao = gen_0;
     637         140 :     for(i=1; i<l; i++)
     638             :     {
     639         126 :       GEN Fi = cindex_Flx(F[i], r2, p, T[1]);
     640             :       GEN R;
     641         126 :       if (degpol(Fi) <= r)
     642             :       {
     643          91 :         if (signe(gel(W,F[i]))==0)
     644           0 :           break;
     645          91 :         else if (signe(gel(W,F[i]))<0)
     646             :         {
     647           0 :           setsigne(gel(W,F[i]),0);
     648           0 :           R = Flxq_log_Coppersmith_d(W,Fi,r,T,p,m);
     649             :         } else
     650          91 :           R = gel(W,F[i]);
     651             :       }
     652             :       else
     653             :       {
     654          35 :         if (Flx_equal(Fi,bad)) break;
     655          28 :         R = Flxq_log_Coppersmith_d(W,Fi,r,T,p,m);
     656          28 :         if (!R) bad = Fi;
     657             :       }
     658         119 :       if (!R) break;
     659         112 :       Ao = Fp_add(Ao, mulis(R, E[i]), m);
     660             :     }
     661          42 :     if (i==l) return subis(Ao,AV);
     662          14 :   }
     663             : }
     664             : 
     665             : 
     666             : static GEN
     667           7 : Flxq_log_index_Coppersmith(GEN a0, GEN b0, GEN m, GEN T0, ulong p)
     668             : {
     669           7 :   pari_sp av = avma;
     670           7 :   GEN  M, S, a, b, Ao=NULL, Bo=NULL, W, e;
     671             :   pari_timer ti;
     672           7 :   double rf = p ==3 ? 1.2 : .9;
     673           7 :   long n = degpol(T0), r = (long) sqrt(n*rf);
     674             :   GEN T;
     675           7 :   long r2 = 3*r/2;
     676           7 :   long nbi = itos(ffsumnbirred(utoi(p), r)), nbrel=nbi*5/4;
     677           7 :   if (DEBUGLEVEL)
     678             :   {
     679           0 :     err_printf("Coppersmith: Parameters r=%ld r2=%ld\n", r,r2);
     680           0 :     err_printf("Coppersmith: Size FB=%ld rel. needed=%ld\n", nbi, nbrel);
     681           0 :     timer_start(&ti);
     682             :   }
     683           7 :   T = smallirred_Flx(p,n,get_Flx_var(T0));
     684           7 :   S = Flx_ffisom(T0,T,p);
     685           7 :   a = Flx_Flxq_eval(a0, S, T, p);
     686           7 :   b = Flx_Flxq_eval(b0, S, T, p);
     687           7 :   if (DEBUGLEVEL) timer_printf(&ti,"model change");
     688           7 :   M = Flxq_log_Coppersmith(nbrel, r, T, p);
     689           7 :   if (DEBUGLEVEL) timer_printf(&ti,"relations");
     690           7 :   W = check_kernel(r, M, nbi, 3*upowuu(p,r), T, p, m);
     691           7 :   timer_start(&ti);
     692           7 :   Ao = Flxq_log_Coppersmith_rec(W, r2, a, r, T, p, m);
     693           7 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth element");
     694           7 :   Bo = Flxq_log_Coppersmith_rec(W, r2, b, r, T, p, m);
     695           7 :   if (DEBUGLEVEL) timer_printf(&ti,"smooth generator");
     696           7 :   e = Fp_div(Ao, Bo, m);
     697           7 :   if (!Flx_equal(Flxq_pow(b0,e,T0,p),a0)) pari_err_BUG("Flxq_log");
     698           7 :   return gerepileupto(av, e);
     699             : }
     700             : 
     701             : GEN
     702          21 : Flxq_log_index(GEN a, GEN b, GEN m, GEN T, ulong p)
     703             : {
     704          21 :   long d = get_Flx_degree(T);
     705          21 :   if (p==3 || (p==5 && d>41))
     706           7 :     return Flxq_log_index_Coppersmith(a, b, m, T, p);
     707          14 :   else    return Flxq_log_index_cubic(a, b, m, T, p);
     708             : }
     709             : 
     710             : int
     711       31366 : Flxq_log_use_index(GEN m, GEN T, ulong p)
     712             : {
     713       31366 :   long d = get_Flx_degree(T);
     714       31366 :   if (p==3 || (p==5 && d>41))
     715        7490 :     return 1;
     716       23876 :   else if (d<=4 || d==6)
     717       23750 :     return 0;
     718             :   else
     719         126 :     return Flxq_log_use_index_cubic(m, T, p);
     720             : }

Generated by: LCOV version 1.11