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 - lll.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19222-cc5b867) Lines: 443 490 90.4 %
Date: 2016-07-28 07:10:28 Functions: 29 31 93.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2008  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             : /* default quality ratio for LLL */
      18             : static const double LLLDFT = 0.99;
      19             : 
      20             : /* assume flag & (LLL_KER|LLL_IM|LLL_ALL). LLL_INPLACE implies LLL_IM */
      21             : static GEN
      22        5255 : lll_trivial(GEN x, long flag)
      23             : {
      24             :   GEN y;
      25        5255 :   if (lg(x) == 1)
      26             :   { /* dim x = 0 */
      27         406 :     if (! (flag & LLL_ALL)) return cgetg(1,t_MAT);
      28          28 :     y=cgetg(3,t_VEC);
      29          28 :     gel(y,1) = cgetg(1,t_MAT);
      30          28 :     gel(y,2) = cgetg(1,t_MAT); return y;
      31             :   }
      32             :   /* dim x = 1 */
      33        4849 :   if (gequal0(gel(x,1)))
      34             :   {
      35          63 :     if (flag & LLL_KER) return matid(1);
      36          63 :     if (flag & (LLL_IM|LLL_INPLACE)) return cgetg(1,t_MAT);
      37          28 :     y = cgetg(3,t_VEC);
      38          28 :     gel(y,1) = matid(1);
      39          28 :     gel(y,2) = cgetg(1,t_MAT); return y;
      40             :   }
      41        4786 :   if (flag & LLL_INPLACE) return gcopy(x);
      42        3379 :   if (flag & LLL_KER) return cgetg(1,t_MAT);
      43        3379 :   if (flag & LLL_IM)  return matid(1);
      44          28 :   y=cgetg(3,t_VEC);
      45          28 :   gel(y,1) = cgetg(1,t_MAT);
      46          28 :   gel(y,2) = (flag & LLL_GRAM)? gcopy(x): matid(1);
      47          28 :   return y;
      48             : }
      49             : 
      50             : /* vecslice(h,#h-k,#h) in place. Works for t_MAT, t_VEC/t_COL */
      51             : static GEN
      52      963747 : lll_get_im(GEN h, long k)
      53             : {
      54      963747 :   ulong mask = h[0] & ~LGBITS;
      55      963747 :   long l = lg(h) - k;
      56      963747 :   h += k; h[0] = mask | evallg(l);
      57      963747 :   return h;
      58             : }
      59             : 
      60             : /* k = dim Kernel */
      61             : static GEN
      62      963775 : lll_finish(GEN h, long k, long flag)
      63             : {
      64             :   GEN g;
      65      963775 :   if (flag & LLL_KER) { setlg(h,k+1); return h; }
      66      963747 :   if (flag & LLL_IM) return lll_get_im(h, k);
      67          70 :   g = vecslice(h,1,k);
      68          70 :   return mkvec2(g, lll_get_im(h, k));
      69             : }
      70             : 
      71             : /********************************************************************/
      72             : /**                                                                **/
      73             : /**                   FPLLL (adapted from D. Stehle's code)        **/
      74             : /**                                                                **/
      75             : /********************************************************************/
      76             : /* Babai() and fplll() are a conversion to libpari API and data types
      77             :    of the file proved.c in fplll-1.3 by Damien Stehle'.
      78             : 
      79             :   Copyright 2005, 2006 Damien Stehle'.
      80             : 
      81             :   This program is free software; you can redistribute it and/or modify it
      82             :   under the terms of the GNU General Public License as published by the
      83             :   Free Software Foundation; either version 2 of the License, or (at your
      84             :   option) any later version.
      85             : 
      86             :   This program implements ideas from the paper "Floating-point LLL Revisited",
      87             :   by Phong Nguyen and Damien Stehle', in the Proceedings of Eurocrypt'2005,
      88             :   Springer-Verlag; and was partly inspired by Shoup's NTL library:
      89             :   http://www.shoup.net/ntl/
      90             : */
      91             : 
      92             : /***********************************************/
      93             : /* Babai's Nearest Plane algorithm (iterative) */
      94             : /***********************************************/
      95             : /* Size-reduces b_kappa using mu_{i,j} and r_{i,j} for j<=i <kappa
      96             : Updates B (kappa); computes mu_{kappa,j}, r_{kappa,j} for j<=kappa, and s(kappa)
      97             : mu, r, s updated in place (affrr).
      98             : */
      99             : static long
     100    19004375 : Babai(pari_sp av, long kappa, GEN *pG, GEN *pB, GEN *pU, GEN mu, GEN r, GEN s,
     101             :       long a, long zeros, long maxG, long n, GEN eta, GEN halfplus1, long prec)
     102             : {
     103    19004375 :   pari_sp av0 = avma;
     104    19004375 :   GEN B = *pB, G = *pG, U = *pU, tmp, rtmp, ztmp;
     105    19004375 :   long k, aa = (a > zeros)? a : zeros+1;
     106    19004375 :   GEN maxmu = gen_0, max2mu = gen_0;
     107             :   /* N.B: we set d = 0 (resp. n = 0) to avoid updating U (resp. B) */
     108    19004375 :   const long d = U ? lg(U)-1: 0;
     109             : 
     110    19004375 :   if (gc_needed(av,2))
     111             :   {
     112        7788 :     if(DEBUGMEM>1) pari_warn(warnmem,"Babai[0], a=%ld", aa);
     113        7788 :     gerepileall(av,U?3:2,&B,&G,&U);
     114             :   }
     115             :   for (;;) {
     116    29014547 :     int go_on = 0;
     117             :     GEN max3mu;
     118             :     long i, j;
     119             : 
     120    29014547 :     if (gc_needed(av0,2))
     121             :     {
     122          70 :       if(DEBUGMEM>1) pari_warn(warnmem,"Babai[1], a=%ld", aa);
     123          70 :       gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     124             :     }
     125             :     /* Step2: compute the GSO for stage kappa */
     126    29014547 :     max3mu = max2mu;
     127    29014547 :     max2mu = maxmu;
     128    29014547 :     maxmu = real_0(prec);
     129   160684433 :     for (j=aa; j<kappa; j++)
     130             :     {
     131   131669886 :       pari_sp btop = avma;
     132   131669886 :       k = zeros+1;
     133   131669886 :       if (j > k)
     134             :       {
     135   112679359 :         tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     136   112679359 :         rtmp = subir(gmael(G,kappa,j), tmp);
     137  1148211079 :         for (k++; k<j; k++)
     138             :         {
     139  1035531720 :           tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     140  1035531720 :           rtmp = subrr(rtmp,tmp);
     141             :         }
     142   112679359 :         affrr(rtmp, gmael(r,kappa,j));
     143             :       }
     144             :       else
     145    18990527 :         affir(gmael(G,kappa,j), gmael(r,kappa,j));
     146   131669886 :       affrr(divrr(gmael(r,kappa,j), gmael(r,j,j)), gmael(mu,kappa,j));
     147   131669886 :       if (absr_cmp(maxmu, gmael(mu,kappa,j))<0)
     148    49972911 :         maxmu = gmael(mu,kappa,j);
     149   131669886 :       avma = btop;
     150             :     }
     151    29014547 :     maxmu = absr(maxmu);
     152    29014547 :     if (typ(max3mu)==t_REAL && absr_cmp(max3mu, shiftr(max2mu, 5))<=0)
     153             :     {
     154           0 :       *pB = B; *pG = G; *pU = U;
     155           0 :       if (DEBUGLEVEL>5) err_printf("prec too low\n");
     156           0 :       return kappa;
     157             :     }
     158             : 
     159             :     /* Step3--5: compute the X_j's  */
     160   245173688 :     for (j=kappa-1; j>zeros; j--)
     161             :     {
     162   216159141 :       tmp = gmael(mu,kappa,j);
     163   216159141 :       if (absr_cmp(tmp, eta) <= 0) continue; /* (essentially) size-reduced */
     164             : 
     165    35204082 :       if (gc_needed(av0,2))
     166             :       {
     167         377 :         if(DEBUGMEM>1) pari_warn(warnmem,"Babai[2], a=%ld, j=%ld", aa,j);
     168         377 :         gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     169             :       }
     170    35204082 :       go_on = 1;
     171             :       /* we consider separately the case |X| = 1 */
     172    35204082 :       if (absr_cmp(tmp, halfplus1) <= 0)
     173             :       {
     174    25612769 :         if (signe(tmp) > 0) { /* in this case, X = 1 */
     175    13083455 :           pari_sp btop = avma;
     176   123868161 :           for (k=zeros+1; k<j; k++)
     177   110784706 :             affrr(subrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     178    13083455 :           avma = btop;
     179             : 
     180   410703685 :           for (i=1; i<=n; i++)
     181   397620230 :             gmael(B,kappa,i) = subii(gmael(B,kappa,i), gmael(B,j,i));
     182   105625505 :           for (i=1; i<=d; i++)
     183    92542050 :             gmael(U,kappa,i) = subii(gmael(U,kappa,i), gmael(U,j,i));
     184    13083455 :           btop = avma;
     185    13083455 :           ztmp = subii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     186    13083455 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     187    13083455 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     188   137198436 :           for (i=1; i<=j; i++)
     189   124114981 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,j,i));
     190   128727632 :           for (i=j+1; i<kappa; i++)
     191   115644177 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,i,j));
     192   106452761 :           for (i=kappa+1; i<=maxG; i++)
     193    93369306 :             gmael(G,i,kappa) = subii(gmael(G,i,kappa), gmael(G,i,j));
     194             :         } else { /* otherwise X = -1 */
     195    12529314 :           pari_sp btop = avma;
     196   123056528 :           for (k=zeros+1; k<j; k++)
     197   110527214 :             affrr(addrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     198    12529314 :           avma = btop;
     199             : 
     200   408185213 :           for (i=1; i<=n; i++)
     201   395655899 :             gmael(B,kappa,i) = addii(gmael(B,kappa,i), gmael(B,j,i));
     202   103172468 :           for (i=1; i<=d; i++)
     203    90643154 :             gmael(U,kappa,i) = addii(gmael(U,kappa,i),gmael(U,j,i));
     204    12529314 :           btop = avma;
     205    12529314 :           ztmp = addii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     206    12529314 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     207    12529314 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     208   135732534 :           for (i=1; i<=j; i++)
     209   123203220 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,j,i));
     210   127925457 :           for (i=j+1; i<kappa; i++)
     211   115396143 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,i,j));
     212   105904438 :           for (i=kappa+1; i<=maxG; i++)
     213    93375124 :             gmael(G,i,kappa) = addii(gmael(G,i,kappa), gmael(G,i,j));
     214             :         }
     215    25612769 :         continue;
     216             :       }
     217             :       /* we have |X| >= 2 */
     218     9591313 :       ztmp = roundr_safe(tmp);
     219     9591313 :       if (lgefint(ztmp) == 3)
     220             :       {
     221     8769713 :         pari_sp btop = avma;
     222     8769713 :         ulong xx = ztmp[2]; /* X fits in an ulong */
     223     8769713 :         if (signe(ztmp) > 0) /* = xx */
     224             :         {
     225    26633636 :           for (k=zeros+1; k<j; k++)
     226             :           {
     227    22235478 :             rtmp = subrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     228    22235478 :             affrr(rtmp, gmael(mu,kappa,k));
     229             :           }
     230     4398158 :           avma = btop;
     231   131884127 :           for (i=1; i<=n; i++)
     232   127485969 :             gmael(B,kappa,i) = submuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     233    27834301 :           for (i=1; i<=d; i++)
     234    23436143 :             gmael(U,kappa,i) = submuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     235     4398158 :           btop = avma;
     236     4398158 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     237     4398158 :           ztmp = subii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     238     4398158 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     239     4398158 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     240    31357546 :           for (i=1; i<=j; i++)
     241    26959388 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     242    62703380 :           for (i=j+1; i<kappa; i++)
     243    58305222 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     244    24311765 :           for (i=kappa+1; i<=maxG; i++)
     245    19913607 :             gmael(G,i,kappa) = submuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     246             :         }
     247             :         else /* = -xx */
     248             :         {
     249    26628076 :           for (k=zeros+1; k<j; k++)
     250             :           {
     251    22256521 :             rtmp = addrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     252    22256521 :             affrr(rtmp, gmael(mu,kappa,k));
     253             :           }
     254     4371555 :           avma = btop;
     255   132245177 :           for (i=1; i<=n; i++)
     256   127873622 :             gmael(B,kappa,i) = addmuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     257    27094065 :           for (i=1; i<=d; i++)
     258    22722510 :             gmael(U,kappa,i) = addmuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     259     4371555 :           btop = avma;
     260     4371555 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     261     4371555 :           ztmp = addii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     262     4371555 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     263     4371555 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     264    31140107 :           for (i=1; i<=j; i++)
     265    26768552 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     266    62779386 :           for (i=j+1; i<kappa; i++)
     267    58407831 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     268    24042060 :           for (i=kappa+1; i<=maxG; i++)
     269    19670505 :             gmael(G,i,kappa) = addmuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     270             :         }
     271             :       }
     272             :       else
     273             :       {
     274      821600 :         GEN tmp2  = itor(ztmp,prec);
     275      821600 :         long e = expo(tmp2)-prec2nbits(prec);
     276      821600 :         GEN X = shifti(trunc2nr(tmp2, -e), e);
     277      821600 :         pari_sp btop = avma;
     278             : 
     279     8860391 :         for (k=zeros+1; k<j; k++)
     280             :         {
     281     8038791 :           rtmp = subrr(gmael(mu,kappa,k), mulir(ztmp, gmael(mu,j,k)));
     282     8038791 :           affrr(rtmp, gmael(mu,kappa,k));
     283             :         }
     284      821600 :         avma = btop;
     285    24120769 :         for (i=1; i<=n; i++)
     286    23299169 :           gmael(B,kappa,i) = submulii(gmael(B,kappa,i), gmael(B,j,i), X);
     287     1705235 :         for (i=1; i<=d; i++)
     288      883635 :           gmael(U,kappa,i) = submulii(gmael(U,kappa,i), gmael(U,j,i), X);
     289      821600 :         btop = avma;
     290      821600 :         ztmp = shifti(mulii(gmael(G,kappa,j), X), 1);
     291      821600 :         ztmp = subii(mulii(gmael(G,j,j), sqri(X)), ztmp);
     292      821600 :         ztmp = addii(gmael(G,kappa,kappa), ztmp);
     293      821600 :         gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     294     9681991 :         for (i=1; i<=j; i++)
     295     8860391 :           gmael(G,kappa,i) = submulii(gmael(G,kappa,i), gmael(G,j,i), X);
     296     8951654 :         for (   ; i<kappa; i++)
     297     8130054 :           gmael(G,kappa,i) = submulii(gmael(G,kappa,i), gmael(G,i,j), X);
     298      900605 :         for (i=kappa+1; i<=maxG; i++)
     299       79005 :           gmael(G,i,kappa) = submulii(gmael(G,i,kappa), gmael(G,i,j), X);
     300             :       }
     301             :     }
     302    29014547 :     if (!go_on) break; /* Anything happened? */
     303    10010172 :     aa = zeros+1;
     304    10010172 :   }
     305             : 
     306    19004375 :   affir(gmael(G,kappa,kappa), gel(s,zeros+1));
     307             :   /* the last s[kappa-1]=r[kappa][kappa] is computed only if kappa increases */
     308    19004375 :   av = avma;
     309   146583047 :   for (k=zeros+1; k<=kappa-2; k++)
     310             :   {
     311   127578672 :     tmp = subrr(gel(s,k), mulrr(gmael(mu,kappa,k), gmael(r,kappa,k)));
     312   127578672 :     affrr(tmp, gel(s,k+1));
     313             :   }
     314    19004375 :   *pB = B; *pG = G; *pU = U; avma = av;
     315    19004375 :   return 0;
     316             : }
     317             : 
     318             : static void
     319    23029239 : rotate(GEN mu, long kappa2, long kappa, long d)
     320             : {
     321             :   long i, j;
     322    23029239 :   pari_sp av = avma;
     323    23029239 :   GEN mutmp = leafcopy(gel(mu,kappa2));
     324    78944184 :   for (i=kappa2; i>kappa; i--)
     325    55914945 :     for (j=1;j<=d;j++) gmael(mu,i,j) = gmael(mu,i-1,j);
     326    23029239 :   for (j=1;j<=d;j++)   gmael(mu,kappa,j) = gel(mutmp,j);
     327    23029239 :   avma = av;
     328    23029239 : }
     329             : 
     330             : /* ****************** */
     331             : /* The LLL Algorithm  */
     332             : /* ****************** */
     333             : 
     334             : /* LLL-reduces the integer matrix(ces) (G,B,U)? "in place" */
     335             : static GEN
     336     1310309 : fplll(GEN *ptrB, GEN *ptrU, GEN *ptrr, double DELTA, double ETA, long flag, long prec)
     337             : {
     338     1310309 :   const long gram = flag & LLL_GRAM; /*Gram matrix*/
     339     1310309 :   const long keepfirst = flag & LLL_KEEP_FIRST; /*never swap with first vector*/
     340             :   pari_sp av, av2;
     341             :   long kappa, kappa2, d, n, i, j, zeros, kappamax, maxG, bab;
     342             :   GEN G, mu, r, s, tmp, SPtmp, alpha;
     343     1310309 :   GEN delta = dbltor(DELTA), eta = dbltor(ETA), halfplus1 = dbltor(1.5);
     344     1310309 :   const long triangular = 0;
     345             :   pari_timer T;
     346     1310309 :   GEN B = *ptrB, U;
     347     1310309 :   long cnt = 0;
     348             : 
     349     1310309 :   d = lg(B)-1;
     350     1310309 :   if (gram)
     351             :   {
     352       22112 :     G = B;
     353       22112 :     n = d;
     354       22112 :     B = cgetg(1, t_VECSMALL); /* dummy */
     355             :   }
     356             :   else
     357             :   {
     358     1288197 :     G = zeromatcopy(d,d);
     359     1288197 :     n = nbrows(B);
     360             :   }
     361     1310309 :   U = *ptrU; /* NULL if inplace */
     362             : 
     363     1310309 :   if(DEBUGLEVEL>=4)
     364             :   {
     365           0 :     timer_start(&T);
     366           0 :     err_printf("Entering L^2: LLL-parameters (%P.3f,%.3Pf), working precision %d words\n",delta,eta, prec);
     367             :   }
     368             : 
     369     1310309 :   mu = cgetg(d+1, t_MAT);
     370     1310309 :   r  = cgetg(d+1, t_MAT);
     371     1310309 :   s  = cgetg(d+1, t_VEC);
     372     5135460 :   for (j = 1; j <= d; j++)
     373             :   {
     374     3825151 :     GEN M = cgetg(d+1, t_COL), R = cgetg(d+1, t_COL);
     375     3825151 :     gel(mu,j)= M;
     376     3825151 :     gel(r,j) = R;
     377     3825151 :     gel(s,j) = cgetr(prec);
     378    19258818 :     for (i = 1; i <= d; i++) {
     379    15433667 :       gel(R,i) = cgetr(prec);
     380    15433667 :       gel(M,i) = cgetr(prec);
     381             :     }
     382             :   }
     383     1310309 :   SPtmp = zerovec(d+1);
     384     1310309 :   alpha = cgetg(d+1, t_VECSMALL);
     385     1310309 :   av = avma;
     386             : 
     387             :   /* Step2: Initializing the main loop */
     388     1310309 :   kappamax = 1;
     389     1310309 :   i = 1;
     390     1310309 :   maxG = d; /* later updated to kappamax if (!gram) */
     391             : 
     392             :   do {
     393     1314495 :     if (!gram) gmael(G,i,i) = ZV_dotsquare(gel(B,i));
     394     1314495 :     affir(gmael(G,i,i), gmael(r,i,i));
     395     1314495 :   } while (signe(gmael(G,i,i)) == 0 && (++i <=d));
     396     1310309 :   zeros = i-1; /* all vectors B[i] with i <= zeros are zero vectors */
     397     1310309 :   kappa = i;
     398     1310309 :   if (zeros < d) affir(gmael(G,zeros+1,zeros+1), gmael(r,zeros+1,zeros+1));
     399     1310309 :   for (i=zeros+1; i<=d; i++) alpha[i]=1;
     400             : 
     401    21624993 :   while (++kappa <= d)
     402             :   {
     403    19004375 :     if (kappa>kappamax)
     404             :     {
     405     2510656 :       if (DEBUGLEVEL>=4) err_printf("K%ld ",kappa);
     406     2510656 :       kappamax = kappa;
     407     2510656 :       if (!gram) {
     408     9970808 :         for (i=zeros+1; i<=kappa; i++)
     409     7570343 :           gmael(G,kappa,i) = ZV_dotproduct(gel(B,kappa), gel(B,i));
     410     2400465 :         maxG = kappamax;
     411             :       }
     412             :     }
     413             :     /* Step3: Call to the Babai algorithm, mu,r,s updated in place */
     414    36025649 :     bab = Babai(av, kappa, &G,&B,&U, mu,r,s, alpha[kappa], zeros, maxG,
     415    17021274 :       gram? 0 : ((triangular && kappamax <= n) ? kappamax: n),
     416             :       eta, halfplus1, prec);
     417    19004375 :     if (bab) {*ptrB=(gram?G:B); *ptrU=U; return NULL; }
     418             : 
     419    19004375 :     av2 = avma;
     420    38003703 :     if ((keepfirst && kappa == 2) ||
     421    18999328 :         cmprr(mulrr(gmael(r,kappa-1,kappa-1), delta), gel(s,kappa-1)) <= 0)
     422             :     { /* Step4: Success of Lovasz's condition */
     423    12334362 :       alpha[kappa] = kappa;
     424    12334362 :       tmp = mulrr(gmael(mu,kappa,kappa-1), gmael(r,kappa,kappa-1));
     425    12334362 :       affrr(subrr(gel(s,kappa-1), tmp), gmael(r,kappa,kappa));
     426    12334362 :       avma = av2;
     427             :     }
     428             :     else
     429             :     { /* Step5: Find the right insertion index kappa, kappa2 = initial kappa */
     430     6670013 :       if (DEBUGLEVEL>=4 && kappa==kappamax && signe(gel(s,kappa-1)))
     431           0 :         if (++cnt > 20) { cnt = 0; err_printf("(%ld) ", expo(gel(s,1))); }
     432     6670013 :       kappa2 = kappa;
     433             :       do {
     434    16521068 :         kappa--;
     435    16521068 :         if (kappa<zeros+2 + (keepfirst ? 1: 0)) break;
     436    13679584 :         tmp = mulrr(gmael(r,kappa-1,kappa-1), delta);
     437    13679584 :       } while (cmprr(gel(s,kappa-1), tmp) <=0 );
     438     6670013 :       avma = av2;
     439             : 
     440    23191081 :       for (i=kappa; i<kappa2; i++)
     441    16521068 :         if (kappa <= alpha[i]) alpha[i] = kappa;
     442     6670013 :       for (i=kappa2; i>kappa; i--) alpha[i] = alpha[i-1];
     443    28576063 :       for (i=kappa2+1; i<=kappamax; i++)
     444    21906050 :         if (kappa < alpha[i]) alpha[i] = kappa;
     445     6670013 :       alpha[kappa] = kappa;
     446             : 
     447             :       /* Step6: Update the mu's and r's */
     448     6670013 :       rotate(mu,kappa2,kappa,d);
     449     6670013 :       rotate(r,kappa2,kappa,d);
     450     6670013 :       affrr(gel(s,kappa), gmael(r,kappa,kappa));
     451             : 
     452             :       /* Step7: Update B, G, U */
     453     6670013 :       if (!gram) rotate(B,kappa2,kappa,n);
     454     6670013 :       if (U) rotate(U,kappa2,kappa,d);
     455             : 
     456     6670013 :       for (i=1; i<=kappa2; i++) gel(SPtmp,i) = gmael(G,kappa2,i);
     457     6670013 :       for (i=kappa2+1; i<=maxG; i++) gel(SPtmp,i) = gmael(G,i,kappa2);
     458    23191081 :       for (i=kappa2; i>kappa; i--)
     459             :       {
     460    16521068 :         for (j=1; j<kappa; j++) gmael(G,i,j) = gmael(G,i-1,j);
     461    16521068 :         gmael(G,i,kappa) = gel(SPtmp,i-1);
     462    16521068 :         for (j=kappa+1; j<=i; j++) gmael(G,i,j) = gmael(G,i-1,j-1);
     463    16521068 :         for (j=kappa2+1; j<=maxG; j++) gmael(G,j,i) = gmael(G,j,i-1);
     464             :       }
     465     6670013 :       for (i=1; i<kappa; i++) gmael(G,kappa,i) = gel(SPtmp,i);
     466     6670013 :       gmael(G,kappa,kappa) = gel(SPtmp,kappa2);
     467     6670013 :       for (i=kappa2+1; i<=maxG; i++) gmael(G,i,kappa) = gel(SPtmp,i);
     468             : 
     469             :       /* Step8: Prepare the next loop iteration */
     470     6670013 :       if (kappa == zeros+1 && !signe(gmael(G,kappa,kappa)))
     471             :       {
     472       27349 :         zeros++; kappa++;
     473       27349 :         affir(gmael(G,kappa,kappa), gmael(r,kappa,kappa));
     474             :       }
     475             :     }
     476             :   }
     477             : 
     478     1310309 :   if (DEBUGLEVEL>=4) timer_printf(&T,"LLL");
     479     1310309 :   if (ptrr) *ptrr = RgM_diagonal_shallow(r);
     480     1310309 :   if (!U)
     481             :   {
     482      342516 :     if (zeros) {
     483           0 :       if (gram) {
     484           0 :         G = lll_get_im(G, zeros);
     485           0 :         d -= zeros;
     486           0 :         for (i = 1; i <= d; i++) gel(G,i) = lll_get_im(gel(G,i), zeros);
     487             :       }
     488             :       else
     489           0 :         B = lll_get_im(B, zeros);
     490             :     }
     491             :   }
     492      967793 :   else if (flag & (LLL_IM|LLL_KER|LLL_ALL))
     493      963712 :     U = lll_finish(U, zeros, flag);
     494     1310309 :   if (gram)
     495             :   {
     496       22112 :     if (U) return U;
     497           0 :     for (i = 1; i <= d; i++)
     498           0 :       for (j = i+1; j <= d; j++) gmael(G,i,j) = gmael(G,j,i);
     499           0 :     return G;
     500             :   }
     501     1288197 :   return U? U: B;
     502             : }
     503             : 
     504             : /* Assume x a ZM, if ptB != NULL, set it to Gram-Schmidt (squared) norms */
     505             : GEN
     506     1314613 : ZM_lll_norms(GEN x, double DELTA, long flag, GEN *B)
     507             : {
     508     1314613 :   pari_sp ltop = avma;
     509     1314613 :   const long compat = flag & LLL_COMPATIBLE;
     510     1314613 :   const double ETA = 0.51;
     511     1314613 :   long p, n = lg(x)-1;
     512             :   GEN U;
     513     1314613 :   if (n <= 1) return lll_trivial(x, flag);
     514     1310309 :   x = RgM_shallowcopy(x);
     515     1310309 :   U = (flag & LLL_INPLACE)? NULL: matid(n);
     516     1310309 :   for (p = compat? DEFAULTPREC: LOWDEFAULTPREC;;)
     517             :   {
     518     1310309 :     GEN m = fplll(&x, &U, B, DELTA, ETA, flag, p);
     519     1310309 :     if (m) return m;
     520           0 :     if (compat)
     521           0 :       p += DEFAULTPREC-2;
     522             :     else
     523           0 :       incrprec(p);
     524           0 :     gerepileall(ltop, U? 2: 1, &x, &U);
     525           0 :   }
     526             : }
     527             : 
     528             : /********************************************************************/
     529             : /**                                                                **/
     530             : /**                        LLL OVER K[X]                           **/
     531             : /**                                                                **/
     532             : /********************************************************************/
     533             : static int
     534         378 : pslg(GEN x)
     535             : {
     536             :   long tx;
     537         378 :   if (gequal0(x)) return 2;
     538         336 :   tx = typ(x); return is_scalar_t(tx)? 3: lg(x);
     539             : }
     540             : 
     541             : static int
     542         147 : REDgen(long k, long l, GEN h, GEN L, GEN B)
     543             : {
     544         147 :   GEN q, u = gcoeff(L,k,l);
     545             :   long i;
     546             : 
     547         147 :   if (pslg(u) < pslg(B)) return 0;
     548             : 
     549         105 :   q = gneg(gdeuc(u,B));
     550         105 :   gel(h,k) = gadd(gel(h,k), gmul(q,gel(h,l)));
     551         105 :   for (i=1; i<l; i++) gcoeff(L,k,i) = gadd(gcoeff(L,k,i), gmul(q,gcoeff(L,l,i)));
     552         105 :   gcoeff(L,k,l) = gadd(gcoeff(L,k,l), gmul(q,B)); return 1;
     553             : }
     554             : 
     555             : static int
     556         147 : do_SWAPgen(GEN h, GEN L, GEN B, long k, GEN fl, int *flc)
     557             : {
     558             :   GEN p1, la, la2, Bk;
     559             :   long ps1, ps2, i, j, lx;
     560             : 
     561         147 :   if (!fl[k-1]) return 0;
     562             : 
     563         105 :   la = gcoeff(L,k,k-1); la2 = gsqr(la);
     564         105 :   Bk = gel(B,k);
     565         105 :   if (fl[k])
     566             :   {
     567          42 :     GEN q = gadd(la2, gmul(gel(B,k-1),gel(B,k+1)));
     568          42 :     ps1 = pslg(gsqr(Bk));
     569          42 :     ps2 = pslg(q);
     570          42 :     if (ps1 <= ps2 && (ps1 < ps2 || !*flc)) return 0;
     571          21 :     *flc = (ps1 != ps2);
     572          21 :     gel(B,k) = gdiv(q, Bk);
     573             :   }
     574             : 
     575          84 :   swap(gel(h,k-1), gel(h,k)); lx = lg(L);
     576          84 :   for (j=1; j<k-1; j++) swap(gcoeff(L,k-1,j), gcoeff(L,k,j));
     577          84 :   if (fl[k])
     578             :   {
     579          21 :     for (i=k+1; i<lx; i++)
     580             :     {
     581           0 :       GEN t = gcoeff(L,i,k);
     582           0 :       p1 = gsub(gmul(gel(B,k+1),gcoeff(L,i,k-1)), gmul(la,t));
     583           0 :       gcoeff(L,i,k) = gdiv(p1, Bk);
     584           0 :       p1 = gadd(gmul(la,gcoeff(L,i,k-1)), gmul(gel(B,k-1),t));
     585           0 :       gcoeff(L,i,k-1) = gdiv(p1, Bk);
     586             :     }
     587             :   }
     588          63 :   else if (!gequal0(la))
     589             :   {
     590          21 :     p1 = gdiv(la2, Bk);
     591          21 :     gel(B,k+1) = gel(B,k) = p1;
     592          21 :     for (i=k+2; i<=lx; i++) gel(B,i) = gdiv(gmul(p1,gel(B,i)),Bk);
     593          21 :     for (i=k+1; i<lx; i++)
     594           0 :       gcoeff(L,i,k-1) = gdiv(gmul(la,gcoeff(L,i,k-1)), Bk);
     595          21 :     for (j=k+1; j<lx-1; j++)
     596           0 :       for (i=j+1; i<lx; i++)
     597           0 :         gcoeff(L,i,j) = gdiv(gmul(p1,gcoeff(L,i,j)), Bk);
     598             :   }
     599             :   else
     600             :   {
     601          42 :     gcoeff(L,k,k-1) = gen_0;
     602          42 :     for (i=k+1; i<lx; i++)
     603             :     {
     604           0 :       gcoeff(L,i,k) = gcoeff(L,i,k-1);
     605           0 :       gcoeff(L,i,k-1) = gen_0;
     606             :     }
     607          42 :     B[k] = B[k-1]; fl[k] = 1; fl[k-1] = 0;
     608             :   }
     609          84 :   return 1;
     610             : }
     611             : 
     612             : static void
     613         126 : incrementalGSgen(GEN x, GEN L, GEN B, long k, GEN fl)
     614             : {
     615         126 :   GEN u = NULL; /* gcc -Wall */
     616             :   long i, j, tu;
     617         315 :   for (j=1; j<=k; j++)
     618         189 :     if (j==k || fl[j])
     619             :     {
     620         189 :       u = gcoeff(x,k,j); tu = typ(u);
     621         189 :       if (! is_extscalar_t(tu)) pari_err_TYPE("incrementalGSgen",u);
     622         252 :       for (i=1; i<j; i++)
     623          63 :         if (fl[i])
     624             :         {
     625          63 :           u = gsub(gmul(gel(B,i+1),u), gmul(gcoeff(L,k,i),gcoeff(L,j,i)));
     626          63 :           u = gdiv(u, gel(B,i));
     627             :         }
     628         189 :       gcoeff(L,k,j) = u;
     629             :     }
     630         126 :   if (gequal0(u)) B[k+1] = B[k];
     631             :   else
     632             :   {
     633          84 :     gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1; fl[k] = 1;
     634             :   }
     635         126 : }
     636             : 
     637             : static GEN
     638         126 : lllgramallgen(GEN x, long flag)
     639             : {
     640         126 :   long lx = lg(x), i, j, k, l, n;
     641             :   pari_sp av;
     642             :   GEN B, L, h, fl;
     643             :   int flc;
     644             : 
     645         126 :   n = lx-1; if (n<=1) return lll_trivial(x,flag);
     646          63 :   if (lgcols(x) != lx) pari_err_DIM("lllgramallgen");
     647             : 
     648          63 :   fl = cgetg(lx, t_VECSMALL);
     649             : 
     650          63 :   av = avma;
     651          63 :   B = scalarcol_shallow(gen_1, lx);
     652          63 :   L = cgetg(lx,t_MAT);
     653          63 :   for (j=1; j<lx; j++) { gel(L,j) = zerocol(n); fl[j] = 0; }
     654             : 
     655          63 :   h = matid(n);
     656         189 :   for (i=1; i<lx; i++)
     657         126 :     incrementalGSgen(x, L, B, i, fl);
     658          63 :   flc = 0;
     659          63 :   for(k=2;;)
     660             :   {
     661         147 :     if (REDgen(k, k-1, h, L, gel(B,k))) flc = 1;
     662         147 :     if (do_SWAPgen(h, L, B, k, fl, &flc)) { if (k > 2) k--; }
     663             :     else
     664             :     {
     665          63 :       for (l=k-2; l>=1; l--)
     666           0 :         if (REDgen(k, l, h, L, gel(B,l+1))) flc = 1;
     667          63 :       if (++k > n) break;
     668             :     }
     669          84 :     if (gc_needed(av,1))
     670             :     {
     671           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"lllgramallgen");
     672           0 :       gerepileall(av,3,&B,&L,&h);
     673             :     }
     674          84 :   }
     675          63 :   k=1; while (k<lx && !fl[k]) k++;
     676          63 :   return lll_finish(h,k-1,flag);
     677             : }
     678             : 
     679             : static GEN
     680         126 : lllallgen(GEN x, long flag)
     681             : {
     682         126 :   pari_sp av = avma;
     683         126 :   if ((flag & LLL_GRAM) == 0) x = gram_matrix(x);
     684         126 :   return gerepilecopy(av, lllgramallgen(x, flag));
     685             : }
     686             : GEN
     687          42 : lllgen(GEN x) { return lllallgen(x, LLL_IM); }
     688             : GEN
     689          42 : lllkerimgen(GEN x) { return lllallgen(x, LLL_ALL); }
     690             : GEN
     691           0 : lllgramgen(GEN x)  { return lllallgen(x, LLL_IM|LLL_GRAM); }
     692             : GEN
     693          42 : lllgramkerimgen(GEN x)  { return lllallgen(x, LLL_ALL|LLL_GRAM); }
     694             : 
     695             : static GEN
     696       26893 : lllall(GEN x, long flag)
     697             : {
     698       26893 :   pari_sp av = avma;
     699       26893 :   return gerepilecopy(av, ZM_lll(x, LLLDFT, flag));
     700             : }
     701             : GEN
     702        6860 : lllint(GEN x) { return lllall(x, LLL_IM); }
     703             : GEN
     704          35 : lllkerim(GEN x) { return lllall(x, LLL_ALL); }
     705             : GEN
     706       19963 : lllgramint(GEN x) { return lllall(x, LLL_IM | LLL_GRAM); }
     707             : GEN
     708          35 : lllgramkerim(GEN x) { return lllall(x, LLL_ALL | LLL_GRAM); }
     709             : 
     710             : static GEN
     711      211791 : rescale_to_int(GEN x)
     712             : {
     713             :   long e, i,j, lx, hx, emin;
     714      211791 :   GEN D = gen_1;
     715      211791 :   int exact = 1;
     716             : 
     717      211791 :   lx = lg(x); if (lx == 1) return x;
     718      211791 :   hx = lgcols(x);
     719      211791 :   emin = HIGHEXPOBIT;
     720      763929 :   for (j = 1; j < lx; j++)
     721     2245963 :     for (i = 1; i < hx; i++)
     722             :     {
     723     1693825 :       GEN c = gcoeff(x,i,j);
     724     1693825 :       switch(typ(c))
     725             :       {
     726             :         case t_REAL:
     727     1105268 :           exact = 0;
     728     1105268 :           if (!signe(c)) continue;
     729     1103453 :           e = expo(c) - bit_prec(c);
     730     1103453 :           break;
     731             :         case t_INT:
     732      588529 :           if (!signe(c)) continue;
     733      228087 :           e = expi(c) + 32;
     734      228087 :           break;
     735             :         case t_FRAC:
     736           0 :           e = expi(gel(c,1)) - expi(gel(c,2)) + 32;
     737           0 :           if (exact) D = lcmii(D, gel(c,2));
     738           0 :           break;
     739             :         default:
     740          28 :           pari_err_TYPE("rescale_to_int",c);
     741           0 :           return NULL; /* not reached */
     742             :       }
     743     1331540 :       if (e < emin) emin = e;
     744             :     }
     745      211763 :   if (exact) return D == gen_1 ? x: Q_muli_to_int(x, D);
     746      211707 :   return grndtoi(gmul2n(x, -emin), &e);
     747             : }
     748             : 
     749             : GEN
     750      212679 : lllfp(GEN x, double D, long flag)
     751             : {
     752      212679 :   long n = lg(x)-1;
     753      212679 :   pari_sp av = avma;
     754             :   GEN h;
     755      212679 :   if (n <= 1) return lll_trivial(x,flag);
     756      211791 :   h = ZM_lll(rescale_to_int(x), D, flag);
     757      211763 :   return gerepilecopy(av, h);
     758             : }
     759             : 
     760             : GEN
     761          56 : lllgram(GEN x) { return lllfp(x,LLLDFT,LLL_GRAM|LLL_IM); }
     762             : GEN
     763      208542 : lll(GEN x) { return lllfp(x,LLLDFT,LLL_IM); }
     764             : 
     765             : GEN
     766         294 : qflll0(GEN x, long flag)
     767             : {
     768         294 :   if (typ(x) != t_MAT) pari_err_TYPE("qflll",x);
     769         294 :   switch(flag)
     770             :   {
     771          49 :     case 0: return lll(x);
     772          63 :     case 1: RgM_check_ZM(x,"qflll"); return lllint(x);
     773          49 :     case 2: RgM_check_ZM(x,"qflll"); return lllintpartial(x);
     774          49 :     case 4: RgM_check_ZM(x,"qflll"); return lllkerim(x);
     775          42 :     case 5: return lllkerimgen(x);
     776          42 :     case 8: return lllgen(x);
     777           0 :     default: pari_err_FLAG("qflll");
     778             :   }
     779           0 :   return NULL; /* not reached */
     780             : }
     781             : 
     782             : GEN
     783         196 : qflllgram0(GEN x, long flag)
     784             : {
     785         196 :   if (typ(x) != t_MAT) pari_err_TYPE("qflllgram",x);
     786         196 :   switch(flag)
     787             :   {
     788          56 :     case 0: return lllgram(x);
     789          49 :     case 1: RgM_check_ZM(x,"qflllgram"); return lllgramint(x);
     790          49 :     case 4: RgM_check_ZM(x,"qflllgram"); return lllgramkerim(x);
     791          42 :     case 5: return lllgramkerimgen(x);
     792           0 :     case 8: return lllgramgen(x);
     793           0 :     default: pari_err_FLAG("qflllgram");
     794             :   }
     795           0 :   return NULL; /* not reached */
     796             : }
     797             : 
     798             : /********************************************************************/
     799             : /**                                                                **/
     800             : /**                   INTEGRAL KERNEL (LLL REDUCED)                **/
     801             : /**                                                                **/
     802             : /********************************************************************/
     803             : static GEN
     804          28 : kerint0(GEN M)
     805             : {
     806          28 :   GEN U, H = ZM_hnfall(M,&U,1);
     807          28 :   long d = lg(M)-lg(H);
     808          28 :   if (!d) return cgetg(1, t_MAT);
     809          28 :   return ZM_lll(vecslice(U,1,d), LLLDFT, LLL_INPLACE);
     810             : }
     811             : GEN
     812           0 : kerint(GEN M)
     813             : {
     814           0 :   pari_sp av = avma;
     815           0 :   return gerepilecopy(av, kerint0(M));
     816             : }
     817             : /* OBSOLETE: use kerint */
     818             : GEN
     819          28 : matkerint0(GEN M, long flag)
     820             : {
     821          28 :   pari_sp av = avma;
     822          28 :   if (typ(M) != t_MAT) pari_err_TYPE("matkerint",M);
     823          28 :   M = Q_primpart(M);
     824          28 :   RgM_check_ZM(M, "kerint");
     825          28 :   switch(flag)
     826             :   {
     827             :     case 0:
     828          28 :     case 1: return gerepilecopy(av, kerint0(M));
     829           0 :     default: pari_err_FLAG("matkerint");
     830             :   }
     831           0 :   return NULL; /* not reached */
     832             : }

Generated by: LCOV version 1.11