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 - lll.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23694-b3ccec097) Lines: 440 478 92.1 %
Date: 2019-03-20 05:44:21 Functions: 30 32 93.8 %
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       34186 : lll_trivial(GEN x, long flag)
      23             : {
      24             :   GEN y;
      25       34186 :   if (lg(x) == 1)
      26             :   { /* dim x = 0 */
      27        7763 :     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       26423 :   if (gequal0(gel(x,1)))
      34             :   {
      35          84 :     if (flag & LLL_KER) return matid(1);
      36          84 :     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       26339 :   if (flag & LLL_INPLACE) return gcopy(x);
      42        9448 :   if (flag & LLL_KER) return cgetg(1,t_MAT);
      43        9448 :   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     2326590 : lll_get_im(GEN h, long k)
      53             : {
      54     2326590 :   ulong mask = h[0] & ~LGBITS;
      55     2326590 :   long l = lg(h) - k;
      56     2326590 :   h += k; h[0] = mask | evallg(l);
      57     2326590 :   return h;
      58             : }
      59             : 
      60             : /* k = dim Kernel */
      61             : static GEN
      62     2326604 : lll_finish(GEN h, long k, long flag)
      63             : {
      64             :   GEN g;
      65     2326604 :   if (flag & LLL_KER) { setlg(h,k+1); return h; }
      66     2326576 :   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             : INLINE GEN
      72     1785802 : mulshift(GEN y, GEN z, long e)
      73             : {
      74     1785802 :   long ly = lgefint(y), lz;
      75             :   pari_sp av;
      76             :   GEN t;
      77     1785802 :   if (ly == 2) return gen_0;
      78       84767 :   lz = lgefint(z);
      79       84767 :   av = avma; (void)new_chunk(ly+lz+nbits2lg(e)); /* HACK */
      80       84767 :   t = mulii(z, y);
      81       84767 :   set_avma(av); return shifti(t, e);
      82             : }
      83             : 
      84             : INLINE GEN
      85    13543256 : submulshift(GEN x, GEN y, GEN z, long e)
      86             : {
      87    13543256 :   long lx = lgefint(x), ly, lz;
      88             :   pari_sp av;
      89             :   GEN t;
      90    13543256 :   if (!e) return submulii(x, y, z);
      91    13205874 :   if (lx == 2) { t = mulshift(y, z, e); togglesign(t); return t; }
      92    11420072 :   ly = lgefint(y);
      93    11420072 :   if (ly == 2) return icopy(x);
      94    10319161 :   lz = lgefint(z);
      95    10319161 :   av = avma; (void)new_chunk(lx+ly+lz+nbits2lg(e)); /* HACK */
      96    10319161 :   t = shifti(mulii(z, y), e);
      97    10319161 :   set_avma(av); return subii(x, t);
      98             : }
      99             : 
     100             : /********************************************************************/
     101             : /**                                                                **/
     102             : /**                   FPLLL (adapted from D. Stehle's code)        **/
     103             : /**                                                                **/
     104             : /********************************************************************/
     105             : /* Babai() and fplll() are a conversion to libpari API and data types
     106             :    of the file proved.c in fplll-1.3 by Damien Stehle'.
     107             : 
     108             :   Copyright 2005, 2006 Damien Stehle'.
     109             : 
     110             :   This program is free software; you can redistribute it and/or modify it
     111             :   under the terms of the GNU General Public License as published by the
     112             :   Free Software Foundation; either version 2 of the License, or (at your
     113             :   option) any later version.
     114             : 
     115             :   This program implements ideas from the paper "Floating-point LLL Revisited",
     116             :   by Phong Nguyen and Damien Stehle', in the Proceedings of Eurocrypt'2005,
     117             :   Springer-Verlag; and was partly inspired by Shoup's NTL library:
     118             :   http://www.shoup.net/ntl/
     119             : */
     120             : 
     121             : /***********************************************/
     122             : /* Babai's Nearest Plane algorithm (iterative) */
     123             : /***********************************************/
     124             : /* Size-reduces b_kappa using mu_{i,j} and r_{i,j} for j<=i <kappa
     125             : Updates B (kappa); computes mu_{kappa,j}, r_{kappa,j} for j<=kappa, and s(kappa)
     126             : mu, r, s updated in place (affrr).
     127             : */
     128             : static long
     129    20373255 : Babai(pari_sp av, long kappa, GEN *pG, GEN *pB, GEN *pU, GEN mu, GEN r, GEN s,
     130             :       long a, long zeros, long maxG, long n, GEN eta, GEN halfplus1, long prec)
     131             : {
     132    20373255 :   pari_sp av0 = avma;
     133    20373255 :   GEN B = *pB, G = *pG, U = *pU, tmp, rtmp, ztmp;
     134    20373255 :   long k, aa = (a > zeros)? a : zeros+1;
     135    20373255 :   GEN maxmu = gen_0, max2mu = gen_0;
     136             :   /* N.B: we set d = 0 (resp. n = 0) to avoid updating U (resp. B) */
     137    20373255 :   const long d = U ? lg(U)-1: 0;
     138             : 
     139    20373255 :   if (gc_needed(av,2))
     140             :   {
     141        3847 :     if(DEBUGMEM>1) pari_warn(warnmem,"Babai[0], a=%ld", aa);
     142        3847 :     gerepileall(av,U?3:2,&B,&G,&U);
     143             :   }
     144    12325607 :   for (;;) {
     145    32698862 :     int go_on = 0;
     146             :     GEN max3mu;
     147             :     long i, j;
     148             : 
     149    32698862 :     if (gc_needed(av0,2))
     150             :     {
     151          16 :       if(DEBUGMEM>1) pari_warn(warnmem,"Babai[1], a=%ld", aa);
     152          16 :       gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     153             :     }
     154             :     /* Step2: compute the GSO for stage kappa */
     155    32698862 :     max3mu = max2mu;
     156    32698862 :     max2mu = maxmu;
     157    32698862 :     maxmu = real_0(prec);
     158   138709276 :     for (j=aa; j<kappa; j++)
     159             :     {
     160   106010414 :       pari_sp btop = avma;
     161   106010414 :       k = zeros+1;
     162   106010414 :       if (j > k)
     163             :       {
     164    81511949 :         tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     165    81511949 :         rtmp = subir(gmael(G,kappa,j), tmp);
     166   624886435 :         for (k++; k<j; k++)
     167             :         {
     168   543374486 :           tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     169   543374486 :           rtmp = subrr(rtmp,tmp);
     170             :         }
     171    81511949 :         affrr(rtmp, gmael(r,kappa,j));
     172             :       }
     173             :       else
     174    24498465 :         affir(gmael(G,kappa,j), gmael(r,kappa,j));
     175   106010414 :       affrr(divrr(gmael(r,kappa,j), gmael(r,j,j)), gmael(mu,kappa,j));
     176   106010414 :       if (abscmprr(maxmu, gmael(mu,kappa,j))<0)
     177    49127647 :         maxmu = gmael(mu,kappa,j);
     178   106010414 :       set_avma(btop);
     179             :     }
     180    32698862 :     maxmu = absr(maxmu);
     181    32698862 :     if (typ(max3mu)==t_REAL && abscmprr(max3mu, shiftr(max2mu, 5))<=0)
     182             :     {
     183           0 :       *pB = B; *pG = G; *pU = U;
     184           0 :       if (DEBUGLEVEL>5) err_printf("prec too low\n");
     185           0 :       return kappa;
     186             :     }
     187             : 
     188             :     /* Step3--5: compute the X_j's  */
     189   189765968 :     for (j=kappa-1; j>zeros; j--)
     190             :     {
     191   157067106 :       tmp = gmael(mu,kappa,j);
     192   157067106 :       if (abscmprr(tmp, eta) <= 0) continue; /* (essentially) size-reduced */
     193             : 
     194    28538827 :       if (gc_needed(av0,2))
     195             :       {
     196         140 :         if(DEBUGMEM>1) pari_warn(warnmem,"Babai[2], a=%ld, j=%ld", aa,j);
     197         140 :         gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     198             :       }
     199    28538827 :       go_on = 1;
     200             :       /* we consider separately the case |X| = 1 */
     201    28538827 :       if (abscmprr(tmp, halfplus1) <= 0)
     202             :       {
     203    19050558 :         if (signe(tmp) > 0) { /* in this case, X = 1 */
     204    10135450 :           pari_sp btop = avma;
     205    65187682 :           for (k=zeros+1; k<j; k++)
     206    55052232 :             affrr(subrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     207    10135450 :           set_avma(btop);
     208             : 
     209   169908782 :           for (i=1; i<=n; i++)
     210   159773332 :             gmael(B,kappa,i) = subii(gmael(B,kappa,i), gmael(B,j,i));
     211   119731694 :           for (i=1; i<=d; i++)
     212   109596244 :             gmael(U,kappa,i) = subii(gmael(U,kappa,i), gmael(U,j,i));
     213    10135450 :           btop = avma;
     214    10135450 :           ztmp = subii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     215    10135450 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     216    10135450 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     217    75658392 :           for (i=1; i<=j; i++)
     218    65522942 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,j,i));
     219    68949799 :           for (i=j+1; i<kappa; i++)
     220    58814349 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,i,j));
     221    53543415 :           for (i=kappa+1; i<=maxG; i++)
     222    43407965 :             gmael(G,i,kappa) = subii(gmael(G,i,kappa), gmael(G,i,j));
     223             :         } else { /* otherwise X = -1 */
     224     8915108 :           pari_sp btop = avma;
     225    63609489 :           for (k=zeros+1; k<j; k++)
     226    54694381 :             affrr(addrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     227     8915108 :           set_avma(btop);
     228             : 
     229   165491852 :           for (i=1; i<=n; i++)
     230   156576744 :             gmael(B,kappa,i) = addii(gmael(B,kappa,i), gmael(B,j,i));
     231   114761084 :           for (i=1; i<=d; i++)
     232   105845976 :             gmael(U,kappa,i) = addii(gmael(U,kappa,i),gmael(U,j,i));
     233     8915108 :           btop = avma;
     234     8915108 :           ztmp = addii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     235     8915108 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     236     8915108 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     237    72747686 :           for (i=1; i<=j; i++)
     238    63832578 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,j,i));
     239    67505384 :           for (i=j+1; i<kappa; i++)
     240    58590276 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,i,j));
     241    52353683 :           for (i=kappa+1; i<=maxG; i++)
     242    43438575 :             gmael(G,i,kappa) = addii(gmael(G,i,kappa), gmael(G,i,j));
     243             :         }
     244    19050558 :         continue;
     245             :       }
     246             :       /* we have |X| >= 2 */
     247     9488269 :       ztmp = roundr_safe(tmp);
     248     9488269 :       if (lgefint(ztmp) == 3)
     249             :       {
     250     9070382 :         pari_sp btop = avma;
     251     9070382 :         ulong xx = ztmp[2]; /* X fits in an ulong */
     252     9070382 :         if (signe(ztmp) > 0) /* = xx */
     253             :         {
     254    15237047 :           for (k=zeros+1; k<j; k++)
     255             :           {
     256    10677693 :             rtmp = subrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     257    10677693 :             affrr(rtmp, gmael(mu,kappa,k));
     258             :           }
     259     4559354 :           set_avma(btop);
     260    56132372 :           for (i=1; i<=n; i++)
     261    51573018 :             gmael(B,kappa,i) = submuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     262    33531115 :           for (i=1; i<=d; i++)
     263    28971761 :             gmael(U,kappa,i) = submuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     264     4559354 :           btop = avma;
     265     4559354 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     266     4559354 :           ztmp = subii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     267     4559354 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     268     4559354 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     269    20248311 :           for (i=1; i<=j; i++)
     270    15688957 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     271    26660518 :           for (i=j+1; i<kappa; i++)
     272    22101164 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     273    11289674 :           for (i=kappa+1; i<=maxG; i++)
     274     6730320 :             gmael(G,i,kappa) = submuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     275             :         }
     276             :         else /* = -xx */
     277             :         {
     278    15107658 :           for (k=zeros+1; k<j; k++)
     279             :           {
     280    10596630 :             rtmp = addrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     281    10596630 :             affrr(rtmp, gmael(mu,kappa,k));
     282             :           }
     283     4511028 :           set_avma(btop);
     284    56031313 :           for (i=1; i<=n; i++)
     285    51520285 :             gmael(B,kappa,i) = addmuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     286    32583260 :           for (i=1; i<=d; i++)
     287    28072232 :             gmael(U,kappa,i) = addmuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     288     4511028 :           btop = avma;
     289     4511028 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     290     4511028 :           ztmp = addii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     291     4511028 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     292     4511028 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     293    19862803 :           for (i=1; i<=j; i++)
     294    15351775 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     295    26593013 :           for (i=j+1; i<kappa; i++)
     296    22081985 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     297    10934561 :           for (i=kappa+1; i<=maxG; i++)
     298     6423533 :             gmael(G,i,kappa) = addmuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     299             :         }
     300             :       }
     301             :       else
     302             :       {
     303             :         pari_sp btop;
     304      417887 :         GEN X, tmp2  = itor(ztmp,prec);
     305      417887 :         long e = expo(tmp2) - prec2nbits(prec);
     306             : 
     307      417887 :         X = trunc2nr(tmp2, -e); if (e < 0) { X = shifti(X,e); e = 0; }
     308      417887 :         btop = avma;
     309     2774418 :         for (k=zeros+1; k<j; k++)
     310             :         {
     311     2356531 :           rtmp = subrr(gmael(mu,kappa,k), mulir(ztmp, gmael(mu,j,k)));
     312     2356531 :           affrr(rtmp, gmael(mu,kappa,k));
     313             :         }
     314      417887 :         set_avma(btop);
     315     7575034 :         for (i=1; i<=n; i++)
     316     7157147 :           gmael(B,kappa,i) = submulshift(gmael(B,kappa,i), gmael(B,j,i), X, e);
     317     1334762 :         for (i=1; i<=d; i++)
     318      916875 :           gmael(U,kappa,i) = submulshift(gmael(U,kappa,i), gmael(U,j,i), X, e);
     319      417887 :         btop = avma;
     320      417887 :         ztmp = shifti(mulii(gmael(G,kappa,j), X), e+1);
     321      417887 :         ztmp = subii(shifti(mulii(gmael(G,j,j), sqri(X)), 2*e), ztmp);
     322      417887 :         ztmp = addii(gmael(G,kappa,kappa), ztmp);
     323      417887 :         gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     324     3192305 :         for (i=1; i<=j; i++)
     325     2774418 :           gmael(G,kappa,i) = submulshift(gmael(G,kappa,i), gmael(G,j,i), X, e);
     326     3015982 :         for (   ; i<kappa; i++)
     327     2598095 :           gmael(G,kappa,i) = submulshift(gmael(G,kappa,i), gmael(G,i,j), X, e);
     328      514608 :         for (i=kappa+1; i<=maxG; i++)
     329       96721 :           gmael(G,i,kappa) = submulshift(gmael(G,i,kappa), gmael(G,i,j), X, e);
     330             :       }
     331             :     }
     332    32698862 :     if (!go_on) break; /* Anything happened? */
     333    12325607 :     aa = zeros+1;
     334             :   }
     335             : 
     336    20373255 :   affir(gmael(G,kappa,kappa), gel(s,zeros+1));
     337             :   /* the last s[kappa-1]=r[kappa][kappa] is computed only if kappa increases */
     338    20373255 :   av = avma;
     339   103421734 :   for (k=zeros+1; k<=kappa-2; k++)
     340             :   {
     341    83048479 :     tmp = subrr(gel(s,k), mulrr(gmael(mu,kappa,k), gmael(r,kappa,k)));
     342    83048479 :     affrr(tmp, gel(s,k+1));
     343             :   }
     344    20373255 :   *pB = B; *pG = G; *pU = U; set_avma(av);
     345    20373255 :   return 0;
     346             : }
     347             : 
     348             : static void
     349    31239706 : rotate(GEN mu, long kappa2, long kappa, long d)
     350             : {
     351             :   long i, j;
     352    31239706 :   pari_sp av = avma;
     353    31239706 :   GEN mutmp = leafcopy(gel(mu,kappa2));
     354    91268696 :   for (i=kappa2; i>kappa; i--)
     355    60028990 :     for (j=1;j<=d;j++) gmael(mu,i,j) = gmael(mu,i-1,j);
     356    31239706 :   for (j=1;j<=d;j++)   gmael(mu,kappa,j) = gel(mutmp,j);
     357    31239706 :   set_avma(av);
     358    31239706 : }
     359             : 
     360             : /* ****************** */
     361             : /* The LLL Algorithm  */
     362             : /* ****************** */
     363             : 
     364             : /* LLL-reduces the integer matrix(ces) (G,B,U)? "in place" */
     365             : static GEN
     366     2444509 : fplll(GEN *ptrB, GEN *ptrU, GEN *ptrr, double DELTA, double ETA, long flag, long prec)
     367             : {
     368     2444509 :   const long gram = flag & LLL_GRAM; /*Gram matrix*/
     369     2444509 :   const long keepfirst = flag & LLL_KEEP_FIRST; /*never swap with first vector*/
     370             :   pari_sp av, av2;
     371             :   long kappa, kappa2, d, n, i, j, zeros, kappamax, maxG, bab;
     372             :   GEN G, mu, r, s, tmp, SPtmp, alpha;
     373     2444509 :   GEN delta = dbltor(DELTA), eta = dbltor(ETA), halfplus1 = dbltor(1.5);
     374     2444509 :   const long triangular = 0;
     375             :   pari_timer T;
     376     2444509 :   GEN B = *ptrB, U;
     377     2444509 :   long cnt = 0;
     378             : 
     379     2444509 :   d = lg(B)-1;
     380     2444509 :   if (gram)
     381             :   {
     382       26602 :     G = B;
     383       26602 :     n = d;
     384       26602 :     B = cgetg(1, t_VECSMALL); /* dummy */
     385             :   }
     386             :   else
     387             :   {
     388     2417907 :     G = zeromatcopy(d,d);
     389     2417907 :     n = nbrows(B);
     390             :   }
     391     2444509 :   U = *ptrU; /* NULL if inplace */
     392             : 
     393     2444509 :   if(DEBUGLEVEL>=4)
     394             :   {
     395           0 :     timer_start(&T);
     396           0 :     err_printf("Entering L^2: LLL-parameters (%P.3f,%.3Pf), working precision %d words\n",delta,eta, prec);
     397             :   }
     398             : 
     399     2444509 :   mu = cgetg(d+1, t_MAT);
     400     2444509 :   r  = cgetg(d+1, t_MAT);
     401     2444509 :   s  = cgetg(d+1, t_VEC);
     402     8625326 :   for (j = 1; j <= d; j++)
     403             :   {
     404     6180817 :     GEN M = cgetg(d+1, t_COL), R = cgetg(d+1, t_COL);
     405     6180817 :     gel(mu,j)= M;
     406     6180817 :     gel(r,j) = R;
     407     6180817 :     gel(s,j) = cgetr(prec);
     408    27498322 :     for (i = 1; i <= d; i++) {
     409    21317505 :       gel(R,i) = cgetr(prec);
     410    21317505 :       gel(M,i) = cgetr(prec);
     411             :     }
     412             :   }
     413     2444509 :   SPtmp = zerovec(d+1);
     414     2444509 :   alpha = cgetg(d+1, t_VECSMALL);
     415     2444509 :   av = avma;
     416             : 
     417             :   /* Step2: Initializing the main loop */
     418     2444509 :   kappamax = 1;
     419     2444509 :   i = 1;
     420     2444509 :   maxG = d; /* later updated to kappamax if (!gram) */
     421             : 
     422             :   do {
     423     2449766 :     if (!gram) gmael(G,i,i) = ZV_dotsquare(gel(B,i));
     424     2449766 :     affir(gmael(G,i,i), gmael(r,i,i));
     425     2449766 :   } while (signe(gmael(G,i,i)) == 0 && (++i <=d));
     426     2444509 :   zeros = i-1; /* all vectors B[i] with i <= zeros are zero vectors */
     427     2444509 :   kappa = i;
     428     2444509 :   if (zeros < d) affir(gmael(G,zeros+1,zeros+1), gmael(r,zeros+1,zeros+1));
     429     2444509 :   for (i=zeros+1; i<=d; i++) alpha[i]=1;
     430             : 
     431    25262273 :   while (++kappa <= d)
     432             :   {
     433    20373255 :     if (kappa>kappamax)
     434             :     {
     435     3731051 :       if (DEBUGLEVEL>=4) err_printf("K%ld ",kappa);
     436     3731051 :       kappamax = kappa;
     437     3731051 :       if (!gram) {
     438    13985636 :         for (i=zeros+1; i<=kappa; i++)
     439    10381581 :           gmael(G,kappa,i) = ZV_dotproduct(gel(B,kappa), gel(B,i));
     440     3604055 :         maxG = kappamax;
     441             :       }
     442             :     }
     443             :     /* Step3: Call to the Babai algorithm, mu,r,s updated in place */
     444    38618119 :     bab = Babai(av, kappa, &G,&B,&U, mu,r,s, alpha[kappa], zeros, maxG,
     445    18244864 :       gram? 0 : ((triangular && kappamax <= n) ? kappamax: n),
     446             :       eta, halfplus1, prec);
     447    20373255 :     if (bab) {*ptrB=(gram?G:B); *ptrU=U; return NULL; }
     448             : 
     449    20373255 :     av2 = avma;
     450    40728254 :     if ((keepfirst && kappa == 2) ||
     451    20354999 :         cmprr(mulrr(gmael(r,kappa-1,kappa-1), delta), gel(s,kappa-1)) <= 0)
     452             :     { /* Step4: Success of Lovasz's condition */
     453    11888695 :       alpha[kappa] = kappa;
     454    11888695 :       tmp = mulrr(gmael(mu,kappa,kappa-1), gmael(r,kappa,kappa-1));
     455    11888695 :       affrr(subrr(gel(s,kappa-1), tmp), gmael(r,kappa,kappa));
     456    11888695 :       set_avma(av2);
     457             :     }
     458             :     else
     459             :     { /* Step5: Find the right insertion index kappa, kappa2 = initial kappa */
     460     8484560 :       if (DEBUGLEVEL>=4 && kappa==kappamax && signe(gel(s,kappa-1)))
     461           0 :         if (++cnt > 20) { cnt = 0; err_printf("(%ld) ", expo(gel(s,1))); }
     462     8484560 :       kappa2 = kappa;
     463             :       do {
     464    16677407 :         kappa--;
     465    16677407 :         if (kappa<zeros+2 + (keepfirst ? 1: 0)) break;
     466    11488927 :         tmp = mulrr(gmael(r,kappa-1,kappa-1), delta);
     467    11488927 :       } while (cmprr(gel(s,kappa-1), tmp) <=0 );
     468     8484560 :       set_avma(av2);
     469             : 
     470    25161967 :       for (i=kappa; i<kappa2; i++)
     471    16677407 :         if (kappa <= alpha[i]) alpha[i] = kappa;
     472     8484560 :       for (i=kappa2; i>kappa; i--) alpha[i] = alpha[i-1];
     473    21220109 :       for (i=kappa2+1; i<=kappamax; i++)
     474    12735549 :         if (kappa < alpha[i]) alpha[i] = kappa;
     475     8484560 :       alpha[kappa] = kappa;
     476             : 
     477             :       /* Step6: Update the mu's and r's */
     478     8484560 :       rotate(mu,kappa2,kappa,d);
     479     8484560 :       rotate(r,kappa2,kappa,d);
     480     8484560 :       affrr(gel(s,kappa), gmael(r,kappa,kappa));
     481             : 
     482             :       /* Step7: Update B, G, U */
     483     8484560 :       if (!gram) rotate(B,kappa2,kappa,n);
     484     8484560 :       if (U) rotate(U,kappa2,kappa,d);
     485             : 
     486     8484560 :       for (i=1; i<=kappa2; i++) gel(SPtmp,i) = gmael(G,kappa2,i);
     487     8484560 :       for (i=kappa2+1; i<=maxG; i++) gel(SPtmp,i) = gmael(G,i,kappa2);
     488    25161967 :       for (i=kappa2; i>kappa; i--)
     489             :       {
     490    16677407 :         for (j=1; j<kappa; j++) gmael(G,i,j) = gmael(G,i-1,j);
     491    16677407 :         gmael(G,i,kappa) = gel(SPtmp,i-1);
     492    16677407 :         for (j=kappa+1; j<=i; j++) gmael(G,i,j) = gmael(G,i-1,j-1);
     493    16677407 :         for (j=kappa2+1; j<=maxG; j++) gmael(G,j,i) = gmael(G,j,i-1);
     494             :       }
     495     8484560 :       for (i=1; i<kappa; i++) gmael(G,kappa,i) = gel(SPtmp,i);
     496     8484560 :       gmael(G,kappa,kappa) = gel(SPtmp,kappa2);
     497     8484560 :       for (i=kappa2+1; i<=maxG; i++) gmael(G,i,kappa) = gel(SPtmp,i);
     498             : 
     499             :       /* Step8: Prepare the next loop iteration */
     500     8484560 :       if (kappa == zeros+1 && !signe(gmael(G,kappa,kappa)))
     501             :       {
     502       35203 :         zeros++; kappa++;
     503       35203 :         affir(gmael(G,kappa,kappa), gmael(r,kappa,kappa));
     504             :       }
     505             :     }
     506             :   }
     507             : 
     508     2444509 :   if (DEBUGLEVEL>=4) timer_printf(&T,"LLL");
     509     2444509 :   if (ptrr) *ptrr = RgM_diagonal_shallow(r);
     510     2444509 :   if (!U)
     511             :   {
     512      103941 :     if (zeros) {
     513          14 :       if (gram) {
     514           0 :         G = lll_get_im(G, zeros);
     515           0 :         d -= zeros;
     516           0 :         for (i = 1; i <= d; i++) gel(G,i) = lll_get_im(gel(G,i), zeros);
     517             :       }
     518             :       else
     519          14 :         B = lll_get_im(B, zeros);
     520             :     }
     521             :   }
     522     2340568 :   else if (flag & (LLL_IM|LLL_KER|LLL_ALL))
     523     2326541 :     U = lll_finish(U, zeros, flag);
     524     2444509 :   if (gram)
     525             :   {
     526       26602 :     if (U) return U;
     527           0 :     for (i = 1; i <= d; i++)
     528           0 :       for (j = i+1; j <= d; j++) gmael(G,i,j) = gmael(G,j,i);
     529           0 :     return G;
     530             :   }
     531     2417907 :   return U? U: B;
     532             : }
     533             : 
     534             : /* Assume x a ZM, if ptB != NULL, set it to Gram-Schmidt (squared) norms */
     535             : GEN
     536     2475434 : ZM_lll_norms(GEN x, double DELTA, long flag, GEN *B)
     537             : {
     538     2475434 :   pari_sp ltop = avma;
     539     2475434 :   const long compat = flag & LLL_COMPATIBLE;
     540     2475434 :   const double ETA = 0.51;
     541     2475434 :   long p, n = lg(x)-1;
     542             :   GEN U;
     543     2475434 :   if (n <= 1) return lll_trivial(x, flag);
     544     2444509 :   x = RgM_shallowcopy(x);
     545     2444509 :   U = (flag & LLL_INPLACE)? NULL: matid(n);
     546     2444509 :   for (p = compat? DEFAULTPREC: LOWDEFAULTPREC;;)
     547           0 :   {
     548     2444509 :     GEN m = fplll(&x, &U, B, DELTA, ETA, flag, p);
     549     2444509 :     if (m) return m;
     550           0 :     if (compat)
     551           0 :       p += DEFAULTPREC-2;
     552             :     else
     553           0 :       incrprec(p);
     554           0 :     gerepileall(ltop, U? 2: 1, &x, &U);
     555             :   }
     556             : }
     557             : 
     558             : /********************************************************************/
     559             : /**                                                                **/
     560             : /**                        LLL OVER K[X]                           **/
     561             : /**                                                                **/
     562             : /********************************************************************/
     563             : static int
     564         378 : pslg(GEN x)
     565             : {
     566             :   long tx;
     567         378 :   if (gequal0(x)) return 2;
     568         336 :   tx = typ(x); return is_scalar_t(tx)? 3: lg(x);
     569             : }
     570             : 
     571             : static int
     572         147 : REDgen(long k, long l, GEN h, GEN L, GEN B)
     573             : {
     574         147 :   GEN q, u = gcoeff(L,k,l);
     575             :   long i;
     576             : 
     577         147 :   if (pslg(u) < pslg(B)) return 0;
     578             : 
     579         105 :   q = gneg(gdeuc(u,B));
     580         105 :   gel(h,k) = gadd(gel(h,k), gmul(q,gel(h,l)));
     581         105 :   for (i=1; i<l; i++) gcoeff(L,k,i) = gadd(gcoeff(L,k,i), gmul(q,gcoeff(L,l,i)));
     582         105 :   gcoeff(L,k,l) = gadd(gcoeff(L,k,l), gmul(q,B)); return 1;
     583             : }
     584             : 
     585             : static int
     586         147 : do_SWAPgen(GEN h, GEN L, GEN B, long k, GEN fl, int *flc)
     587             : {
     588             :   GEN p1, la, la2, Bk;
     589             :   long ps1, ps2, i, j, lx;
     590             : 
     591         147 :   if (!fl[k-1]) return 0;
     592             : 
     593         105 :   la = gcoeff(L,k,k-1); la2 = gsqr(la);
     594         105 :   Bk = gel(B,k);
     595         105 :   if (fl[k])
     596             :   {
     597          42 :     GEN q = gadd(la2, gmul(gel(B,k-1),gel(B,k+1)));
     598          42 :     ps1 = pslg(gsqr(Bk));
     599          42 :     ps2 = pslg(q);
     600          42 :     if (ps1 <= ps2 && (ps1 < ps2 || !*flc)) return 0;
     601          21 :     *flc = (ps1 != ps2);
     602          21 :     gel(B,k) = gdiv(q, Bk);
     603             :   }
     604             : 
     605          84 :   swap(gel(h,k-1), gel(h,k)); lx = lg(L);
     606          84 :   for (j=1; j<k-1; j++) swap(gcoeff(L,k-1,j), gcoeff(L,k,j));
     607          84 :   if (fl[k])
     608             :   {
     609          21 :     for (i=k+1; i<lx; i++)
     610             :     {
     611           0 :       GEN t = gcoeff(L,i,k);
     612           0 :       p1 = gsub(gmul(gel(B,k+1),gcoeff(L,i,k-1)), gmul(la,t));
     613           0 :       gcoeff(L,i,k) = gdiv(p1, Bk);
     614           0 :       p1 = gadd(gmul(la,gcoeff(L,i,k-1)), gmul(gel(B,k-1),t));
     615           0 :       gcoeff(L,i,k-1) = gdiv(p1, Bk);
     616             :     }
     617             :   }
     618          63 :   else if (!gequal0(la))
     619             :   {
     620          21 :     p1 = gdiv(la2, Bk);
     621          21 :     gel(B,k+1) = gel(B,k) = p1;
     622          21 :     for (i=k+2; i<=lx; i++) gel(B,i) = gdiv(gmul(p1,gel(B,i)),Bk);
     623          21 :     for (i=k+1; i<lx; i++)
     624           0 :       gcoeff(L,i,k-1) = gdiv(gmul(la,gcoeff(L,i,k-1)), Bk);
     625          21 :     for (j=k+1; j<lx-1; j++)
     626           0 :       for (i=j+1; i<lx; i++)
     627           0 :         gcoeff(L,i,j) = gdiv(gmul(p1,gcoeff(L,i,j)), Bk);
     628             :   }
     629             :   else
     630             :   {
     631          42 :     gcoeff(L,k,k-1) = gen_0;
     632          42 :     for (i=k+1; i<lx; i++)
     633             :     {
     634           0 :       gcoeff(L,i,k) = gcoeff(L,i,k-1);
     635           0 :       gcoeff(L,i,k-1) = gen_0;
     636             :     }
     637          42 :     B[k] = B[k-1]; fl[k] = 1; fl[k-1] = 0;
     638             :   }
     639          84 :   return 1;
     640             : }
     641             : 
     642             : static void
     643         126 : incrementalGSgen(GEN x, GEN L, GEN B, long k, GEN fl)
     644             : {
     645         126 :   GEN u = NULL; /* gcc -Wall */
     646             :   long i, j, tu;
     647         315 :   for (j=1; j<=k; j++)
     648         189 :     if (j==k || fl[j])
     649             :     {
     650         189 :       u = gcoeff(x,k,j); tu = typ(u);
     651         189 :       if (! is_extscalar_t(tu)) pari_err_TYPE("incrementalGSgen",u);
     652         252 :       for (i=1; i<j; i++)
     653          63 :         if (fl[i])
     654             :         {
     655          63 :           u = gsub(gmul(gel(B,i+1),u), gmul(gcoeff(L,k,i),gcoeff(L,j,i)));
     656          63 :           u = gdiv(u, gel(B,i));
     657             :         }
     658         189 :       gcoeff(L,k,j) = u;
     659             :     }
     660         126 :   if (gequal0(u)) B[k+1] = B[k];
     661             :   else
     662             :   {
     663          84 :     gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1; fl[k] = 1;
     664             :   }
     665         126 : }
     666             : 
     667             : static GEN
     668         126 : lllgramallgen(GEN x, long flag)
     669             : {
     670         126 :   long lx = lg(x), i, j, k, l, n;
     671             :   pari_sp av;
     672             :   GEN B, L, h, fl;
     673             :   int flc;
     674             : 
     675         126 :   n = lx-1; if (n<=1) return lll_trivial(x,flag);
     676          63 :   if (lgcols(x) != lx) pari_err_DIM("lllgramallgen");
     677             : 
     678          63 :   fl = cgetg(lx, t_VECSMALL);
     679             : 
     680          63 :   av = avma;
     681          63 :   B = scalarcol_shallow(gen_1, lx);
     682          63 :   L = cgetg(lx,t_MAT);
     683          63 :   for (j=1; j<lx; j++) { gel(L,j) = zerocol(n); fl[j] = 0; }
     684             : 
     685          63 :   h = matid(n);
     686         189 :   for (i=1; i<lx; i++)
     687         126 :     incrementalGSgen(x, L, B, i, fl);
     688          63 :   flc = 0;
     689          63 :   for(k=2;;)
     690             :   {
     691         231 :     if (REDgen(k, k-1, h, L, gel(B,k))) flc = 1;
     692         147 :     if (do_SWAPgen(h, L, B, k, fl, &flc)) { if (k > 2) k--; }
     693             :     else
     694             :     {
     695          63 :       for (l=k-2; l>=1; l--)
     696           0 :         if (REDgen(k, l, h, L, gel(B,l+1))) flc = 1;
     697          63 :       if (++k > n) break;
     698             :     }
     699          84 :     if (gc_needed(av,1))
     700             :     {
     701           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"lllgramallgen");
     702           0 :       gerepileall(av,3,&B,&L,&h);
     703             :     }
     704             :   }
     705          63 :   k=1; while (k<lx && !fl[k]) k++;
     706          63 :   return lll_finish(h,k-1,flag);
     707             : }
     708             : 
     709             : static GEN
     710         126 : lllallgen(GEN x, long flag)
     711             : {
     712         126 :   pari_sp av = avma;
     713         126 :   if ((flag & LLL_GRAM) == 0) x = gram_matrix(x);
     714         126 :   return gerepilecopy(av, lllgramallgen(x, flag));
     715             : }
     716             : GEN
     717          42 : lllgen(GEN x) { return lllallgen(x, LLL_IM); }
     718             : GEN
     719          42 : lllkerimgen(GEN x) { return lllallgen(x, LLL_ALL); }
     720             : GEN
     721           0 : lllgramgen(GEN x)  { return lllallgen(x, LLL_IM|LLL_GRAM); }
     722             : GEN
     723          42 : lllgramkerimgen(GEN x)  { return lllallgen(x, LLL_ALL|LLL_GRAM); }
     724             : 
     725             : static GEN
     726       28221 : lllall(GEN x, long flag)
     727             : {
     728       28221 :   pari_sp av = avma;
     729       28221 :   return gerepilecopy(av, ZM_lll(x, LLLDFT, flag));
     730             : }
     731             : GEN
     732        6861 : lllint(GEN x) { return lllall(x, LLL_IM); }
     733             : GEN
     734          35 : lllkerim(GEN x) { return lllall(x, LLL_ALL); }
     735             : GEN
     736       21290 : lllgramint(GEN x) { return lllall(x, LLL_IM | LLL_GRAM); }
     737             : GEN
     738          35 : lllgramkerim(GEN x) { return lllall(x, LLL_ALL | LLL_GRAM); }
     739             : 
     740             : GEN
     741      235016 : lllfp(GEN x, double D, long flag)
     742             : {
     743      235016 :   long n = lg(x)-1;
     744      235016 :   pari_sp av = avma;
     745             :   GEN h;
     746      235016 :   if (n <= 1) return lll_trivial(x,flag);
     747      231818 :   h = ZM_lll(RgM_rescale_to_int(x), D, flag);
     748      231790 :   return gerepilecopy(av, h);
     749             : }
     750             : 
     751             : GEN
     752          56 : lllgram(GEN x) { return lllfp(x,LLLDFT,LLL_GRAM|LLL_IM); }
     753             : GEN
     754      220933 : lll(GEN x) { return lllfp(x,LLLDFT,LLL_IM); }
     755             : 
     756             : GEN
     757         294 : qflll0(GEN x, long flag)
     758             : {
     759         294 :   if (typ(x) != t_MAT) pari_err_TYPE("qflll",x);
     760         294 :   switch(flag)
     761             :   {
     762          49 :     case 0: return lll(x);
     763          63 :     case 1: RgM_check_ZM(x,"qflll"); return lllint(x);
     764          49 :     case 2: RgM_check_ZM(x,"qflll"); return lllintpartial(x);
     765          49 :     case 4: RgM_check_ZM(x,"qflll"); return lllkerim(x);
     766          42 :     case 5: return lllkerimgen(x);
     767          42 :     case 8: return lllgen(x);
     768           0 :     default: pari_err_FLAG("qflll");
     769             :   }
     770             :   return NULL; /* LCOV_EXCL_LINE */
     771             : }
     772             : 
     773             : GEN
     774         196 : qflllgram0(GEN x, long flag)
     775             : {
     776         196 :   if (typ(x) != t_MAT) pari_err_TYPE("qflllgram",x);
     777         196 :   switch(flag)
     778             :   {
     779          56 :     case 0: return lllgram(x);
     780          49 :     case 1: RgM_check_ZM(x,"qflllgram"); return lllgramint(x);
     781          49 :     case 4: RgM_check_ZM(x,"qflllgram"); return lllgramkerim(x);
     782          42 :     case 5: return lllgramkerimgen(x);
     783           0 :     case 8: return lllgramgen(x);
     784           0 :     default: pari_err_FLAG("qflllgram");
     785             :   }
     786             :   return NULL; /* LCOV_EXCL_LINE */
     787             : }
     788             : 
     789             : /********************************************************************/
     790             : /**                                                                **/
     791             : /**                   INTEGRAL KERNEL (LLL REDUCED)                **/
     792             : /**                                                                **/
     793             : /********************************************************************/
     794             : static GEN
     795          28 : kerint0(GEN M)
     796             : {
     797             :   /* return ZM_lll(M, LLLDFT, LLL_KER); */
     798          28 :   GEN U, H = ZM_hnflll(M,&U,1);
     799          28 :   long d = lg(M)-lg(H);
     800          28 :   if (!d) return cgetg(1, t_MAT);
     801          28 :   return ZM_lll(vecslice(U,1,d), LLLDFT, LLL_INPLACE);
     802             : }
     803             : GEN
     804           0 : kerint(GEN M)
     805             : {
     806           0 :   pari_sp av = avma;
     807           0 :   return gerepilecopy(av, kerint0(M));
     808             : }
     809             : /* OBSOLETE: use kerint */
     810             : GEN
     811          28 : matkerint0(GEN M, long flag)
     812             : {
     813          28 :   pari_sp av = avma;
     814          28 :   if (typ(M) != t_MAT) pari_err_TYPE("matkerint",M);
     815          28 :   M = Q_primpart(M);
     816          28 :   RgM_check_ZM(M, "kerint");
     817          28 :   switch(flag)
     818             :   {
     819             :     case 0:
     820          28 :     case 1: return gerepilecopy(av, kerint0(M));
     821           0 :     default: pari_err_FLAG("matkerint");
     822             :   }
     823             :   return NULL; /* LCOV_EXCL_LINE */
     824             : }

Generated by: LCOV version 1.13