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.10.0 lcov report (development 20459-9710128) Lines: 422 462 91.3 %
Date: 2017-04-28 05:33:48 Functions: 28 30 93.3 %
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        5358 : lll_trivial(GEN x, long flag)
      23             : {
      24             :   GEN y;
      25        5358 :   if (lg(x) == 1)
      26             :   { /* dim x = 0 */
      27         396 :     if (! (flag & LLL_ALL)) return cgetg(1,t_MAT);
      28          24 :     y=cgetg(3,t_VEC);
      29          24 :     gel(y,1) = cgetg(1,t_MAT);
      30          24 :     gel(y,2) = cgetg(1,t_MAT); return y;
      31             :   }
      32             :   /* dim x = 1 */
      33        4962 :   if (gequal0(gel(x,1)))
      34             :   {
      35          54 :     if (flag & LLL_KER) return matid(1);
      36          54 :     if (flag & (LLL_IM|LLL_INPLACE)) return cgetg(1,t_MAT);
      37          24 :     y = cgetg(3,t_VEC);
      38          24 :     gel(y,1) = matid(1);
      39          24 :     gel(y,2) = cgetg(1,t_MAT); return y;
      40             :   }
      41        4908 :   if (flag & LLL_INPLACE) return gcopy(x);
      42        3732 :   if (flag & LLL_KER) return cgetg(1,t_MAT);
      43        3732 :   if (flag & LLL_IM)  return matid(1);
      44          24 :   y=cgetg(3,t_VEC);
      45          24 :   gel(y,1) = cgetg(1,t_MAT);
      46          24 :   gel(y,2) = (flag & LLL_GRAM)? gcopy(x): matid(1);
      47          24 :   return y;
      48             : }
      49             : 
      50             : /* vecslice(h,#h-k,#h) in place. Works for t_MAT, t_VEC/t_COL */
      51             : static GEN
      52      837624 : lll_get_im(GEN h, long k)
      53             : {
      54      837624 :   ulong mask = h[0] & ~LGBITS;
      55      837624 :   long l = lg(h) - k;
      56      837624 :   h += k; h[0] = mask | evallg(l);
      57      837624 :   return h;
      58             : }
      59             : 
      60             : /* k = dim Kernel */
      61             : static GEN
      62      837648 : lll_finish(GEN h, long k, long flag)
      63             : {
      64             :   GEN g;
      65      837648 :   if (flag & LLL_KER) { setlg(h,k+1); return h; }
      66      837624 :   if (flag & LLL_IM) return lll_get_im(h, k);
      67          60 :   g = vecslice(h,1,k);
      68          60 :   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    12780306 : 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    12780306 :   pari_sp av0 = avma;
     104    12780306 :   GEN B = *pB, G = *pG, U = *pU, tmp, rtmp, ztmp;
     105    12780306 :   long k, aa = (a > zeros)? a : zeros+1;
     106    12780306 :   GEN maxmu = gen_0, max2mu = gen_0;
     107             :   /* N.B: we set d = 0 (resp. n = 0) to avoid updating U (resp. B) */
     108    12780306 :   const long d = U ? lg(U)-1: 0;
     109             : 
     110    12780306 :   if (gc_needed(av,2))
     111             :   {
     112        3249 :     if(DEBUGMEM>1) pari_warn(warnmem,"Babai[0], a=%ld", aa);
     113        3249 :     gerepileall(av,U?3:2,&B,&G,&U);
     114             :   }
     115             :   for (;;) {
     116    19988820 :     int go_on = 0;
     117             :     GEN max3mu;
     118             :     long i, j;
     119             : 
     120    19988820 :     if (gc_needed(av0,2))
     121             :     {
     122          12 :       if(DEBUGMEM>1) pari_warn(warnmem,"Babai[1], a=%ld", aa);
     123          12 :       gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     124             :     }
     125             :     /* Step2: compute the GSO for stage kappa */
     126    19988820 :     max3mu = max2mu;
     127    19988820 :     max2mu = maxmu;
     128    19988820 :     maxmu = real_0(prec);
     129    95365116 :     for (j=aa; j<kappa; j++)
     130             :     {
     131    75376296 :       pari_sp btop = avma;
     132    75376296 :       k = zeros+1;
     133    75376296 :       if (j > k)
     134             :       {
     135    61496772 :         tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     136    61496772 :         rtmp = subir(gmael(G,kappa,j), tmp);
     137   478162170 :         for (k++; k<j; k++)
     138             :         {
     139   416665398 :           tmp  = mulrr(gmael(mu,j,k), gmael(r,kappa,k));
     140   416665398 :           rtmp = subrr(rtmp,tmp);
     141             :         }
     142    61496772 :         affrr(rtmp, gmael(r,kappa,j));
     143             :       }
     144             :       else
     145    13879524 :         affir(gmael(G,kappa,j), gmael(r,kappa,j));
     146    75376296 :       affrr(divrr(gmael(r,kappa,j), gmael(r,j,j)), gmael(mu,kappa,j));
     147    75376296 :       if (abscmprr(maxmu, gmael(mu,kappa,j))<0)
     148    32481267 :         maxmu = gmael(mu,kappa,j);
     149    75376296 :       avma = btop;
     150             :     }
     151    19988820 :     maxmu = absr(maxmu);
     152    19988820 :     if (typ(max3mu)==t_REAL && abscmprr(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   133831662 :     for (j=kappa-1; j>zeros; j--)
     161             :     {
     162   113842842 :       tmp = gmael(mu,kappa,j);
     163   113842842 :       if (abscmprr(tmp, eta) <= 0) continue; /* (essentially) size-reduced */
     164             : 
     165    19408368 :       if (gc_needed(av0,2))
     166             :       {
     167          87 :         if(DEBUGMEM>1) pari_warn(warnmem,"Babai[2], a=%ld, j=%ld", aa,j);
     168          87 :         gerepileall(av,U?5:4,&B,&G,&maxmu,&max2mu,&U);
     169             :       }
     170    19408368 :       go_on = 1;
     171             :       /* we consider separately the case |X| = 1 */
     172    19408368 :       if (abscmprr(tmp, halfplus1) <= 0)
     173             :       {
     174    13771770 :         if (signe(tmp) > 0) { /* in this case, X = 1 */
     175     7126122 :           pari_sp btop = avma;
     176    48988044 :           for (k=zeros+1; k<j; k++)
     177    41861922 :             affrr(subrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     178     7126122 :           avma = btop;
     179             : 
     180   129634884 :           for (i=1; i<=n; i++)
     181   122508762 :             gmael(B,kappa,i) = subii(gmael(B,kappa,i), gmael(B,j,i));
     182    86929392 :           for (i=1; i<=d; i++)
     183    79803270 :             gmael(U,kappa,i) = subii(gmael(U,kappa,i), gmael(U,j,i));
     184     7126122 :           btop = avma;
     185     7126122 :           ztmp = subii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     186     7126122 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     187     7126122 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     188    56325708 :           for (i=1; i<=j; i++)
     189    49199586 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,j,i));
     190    52498200 :           for (i=j+1; i<kappa; i++)
     191    45372078 :             gmael(G,kappa,i) = subii(gmael(G,kappa,i), gmael(G,i,j));
     192    41647104 :           for (i=kappa+1; i<=maxG; i++)
     193    34520982 :             gmael(G,i,kappa) = subii(gmael(G,i,kappa), gmael(G,i,j));
     194             :         } else { /* otherwise X = -1 */
     195     6645648 :           pari_sp btop = avma;
     196    48152556 :           for (k=zeros+1; k<j; k++)
     197    41506908 :             affrr(addrr(gmael(mu,kappa,k), gmael(mu,j,k)), gmael(mu,kappa,k));
     198     6645648 :           avma = btop;
     199             : 
     200   127261272 :           for (i=1; i<=n; i++)
     201   120615624 :             gmael(B,kappa,i) = addii(gmael(B,kappa,i), gmael(B,j,i));
     202    84449736 :           for (i=1; i<=d; i++)
     203    77804088 :             gmael(U,kappa,i) = addii(gmael(U,kappa,i),gmael(U,j,i));
     204     6645648 :           btop = avma;
     205     6645648 :           ztmp = addii(gmael(G,j,j), shifti(gmael(G,kappa,j), 1));
     206     6645648 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     207     6645648 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     208    54923958 :           for (i=1; i<=j; i++)
     209    48278310 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,j,i));
     210    51745632 :           for (i=j+1; i<kappa; i++)
     211    45099984 :             gmael(G,kappa,i) = addii(gmael(G,kappa,i), gmael(G,i,j));
     212    41148270 :           for (i=kappa+1; i<=maxG; i++)
     213    34502622 :             gmael(G,i,kappa) = addii(gmael(G,i,kappa), gmael(G,i,j));
     214             :         }
     215    13771770 :         continue;
     216             :       }
     217             :       /* we have |X| >= 2 */
     218     5636598 :       ztmp = roundr_safe(tmp);
     219     5636598 :       if (lgefint(ztmp) == 3)
     220             :       {
     221     5362962 :         pari_sp btop = avma;
     222     5362962 :         ulong xx = ztmp[2]; /* X fits in an ulong */
     223     5362962 :         if (signe(ztmp) > 0) /* = xx */
     224             :         {
     225    10859406 :           for (k=zeros+1; k<j; k++)
     226             :           {
     227     8166522 :             rtmp = subrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     228     8166522 :             affrr(rtmp, gmael(mu,kappa,k));
     229             :           }
     230     2692884 :           avma = btop;
     231    41072760 :           for (i=1; i<=n; i++)
     232    38379876 :             gmael(B,kappa,i) = submuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     233    23763414 :           for (i=1; i<=d; i++)
     234    21070530 :             gmael(U,kappa,i) = submuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     235     2692884 :           btop = avma;
     236     2692884 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     237     2692884 :           ztmp = subii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     238     2692884 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     239     2692884 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     240    13831506 :           for (i=1; i<=j; i++)
     241    11138622 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     242    20217138 :           for (i=j+1; i<kappa; i++)
     243    17524254 :             gmael(G,kappa,i) = submuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     244     8051058 :           for (i=kappa+1; i<=maxG; i++)
     245     5358174 :             gmael(G,i,kappa) = submuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     246             :         }
     247             :         else /* = -xx */
     248             :         {
     249    10745838 :           for (k=zeros+1; k<j; k++)
     250             :           {
     251     8075760 :             rtmp = addrr(gmael(mu,kappa,k), mulur(xx, gmael(mu,j,k)));
     252     8075760 :             affrr(rtmp, gmael(mu,kappa,k));
     253             :           }
     254     2670078 :           avma = btop;
     255    41027196 :           for (i=1; i<=n; i++)
     256    38357118 :             gmael(B,kappa,i) = addmuliu_inplace(gmael(B,kappa,i), gmael(B,j,i), xx);
     257    23118000 :           for (i=1; i<=d; i++)
     258    20447922 :             gmael(U,kappa,i) = addmuliu_inplace(gmael(U,kappa,i), gmael(U,j,i), xx);
     259     2670078 :           btop = avma;
     260     2670078 :           ztmp = shifti(muliu(gmael(G,kappa,j), xx), 1);
     261     2670078 :           ztmp = addii(mulii(gmael(G,j,j), sqru(xx)), ztmp);
     262     2670078 :           ztmp = addii(gmael(G,kappa,kappa), ztmp);
     263     2670078 :           gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     264    13536324 :           for (i=1; i<=j; i++)
     265    10866246 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,j,i), xx);
     266    20169054 :           for (i=j+1; i<kappa; i++)
     267    17498976 :             gmael(G,kappa,i) = addmuliu_inplace(gmael(G,kappa,i), gmael(G,i,j), xx);
     268     7789644 :           for (i=kappa+1; i<=maxG; i++)
     269     5119566 :             gmael(G,i,kappa) = addmuliu_inplace(gmael(G,i,kappa), gmael(G,i,j), xx);
     270             :         }
     271             :       }
     272             :       else
     273             :       {
     274      273636 :         GEN tmp2  = itor(ztmp,prec);
     275      273636 :         long e = expo(tmp2)-prec2nbits(prec);
     276      273636 :         GEN X = shifti(trunc2nr(tmp2, -e), e);
     277      273636 :         pari_sp btop = avma;
     278             : 
     279     1864326 :         for (k=zeros+1; k<j; k++)
     280             :         {
     281     1590690 :           rtmp = subrr(gmael(mu,kappa,k), mulir(ztmp, gmael(mu,j,k)));
     282     1590690 :           affrr(rtmp, gmael(mu,kappa,k));
     283             :         }
     284      273636 :         avma = btop;
     285     4952640 :         for (i=1; i<=n; i++)
     286     4679004 :           gmael(B,kappa,i) = submulii(gmael(B,kappa,i), gmael(B,j,i), X);
     287      890406 :         for (i=1; i<=d; i++)
     288      616770 :           gmael(U,kappa,i) = submulii(gmael(U,kappa,i), gmael(U,j,i), X);
     289      273636 :         btop = avma;
     290      273636 :         ztmp = shifti(mulii(gmael(G,kappa,j), X), 1);
     291      273636 :         ztmp = subii(mulii(gmael(G,j,j), sqri(X)), ztmp);
     292      273636 :         ztmp = addii(gmael(G,kappa,kappa), ztmp);
     293      273636 :         gmael(G,kappa,kappa) = gerepileuptoint(btop, ztmp);
     294     2137962 :         for (i=1; i<=j; i++)
     295     1864326 :           gmael(G,kappa,i) = submulii(gmael(G,kappa,i), gmael(G,j,i), X);
     296     1990926 :         for (   ; i<kappa; i++)
     297     1717290 :           gmael(G,kappa,i) = submulii(gmael(G,kappa,i), gmael(G,i,j), X);
     298      331158 :         for (i=kappa+1; i<=maxG; i++)
     299       57522 :           gmael(G,i,kappa) = submulii(gmael(G,i,kappa), gmael(G,i,j), X);
     300             :       }
     301             :     }
     302    19988820 :     if (!go_on) break; /* Anything happened? */
     303     7208514 :     aa = zeros+1;
     304     7208514 :   }
     305             : 
     306    12780306 :   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    12780306 :   av = avma;
     309    75832626 :   for (k=zeros+1; k<=kappa-2; k++)
     310             :   {
     311    63052320 :     tmp = subrr(gel(s,k), mulrr(gmael(mu,kappa,k), gmael(r,kappa,k)));
     312    63052320 :     affrr(tmp, gel(s,k+1));
     313             :   }
     314    12780306 :   *pB = B; *pG = G; *pU = U; avma = av;
     315    12780306 :   return 0;
     316             : }
     317             : 
     318             : static void
     319    16921404 : rotate(GEN mu, long kappa2, long kappa, long d)
     320             : {
     321             :   long i, j;
     322    16921404 :   pari_sp av = avma;
     323    16921404 :   GEN mutmp = leafcopy(gel(mu,kappa2));
     324    55361316 :   for (i=kappa2; i>kappa; i--)
     325    38439912 :     for (j=1;j<=d;j++) gmael(mu,i,j) = gmael(mu,i-1,j);
     326    16921404 :   for (j=1;j<=d;j++)   gmael(mu,kappa,j) = gel(mutmp,j);
     327    16921404 :   avma = av;
     328    16921404 : }
     329             : 
     330             : /* ****************** */
     331             : /* The LLL Algorithm  */
     332             : /* ****************** */
     333             : 
     334             : /* LLL-reduces the integer matrix(ces) (G,B,U)? "in place" */
     335             : static GEN
     336      899064 : fplll(GEN *ptrB, GEN *ptrU, GEN *ptrr, double DELTA, double ETA, long flag, long prec)
     337             : {
     338      899064 :   const long gram = flag & LLL_GRAM; /*Gram matrix*/
     339      899064 :   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      899064 :   GEN delta = dbltor(DELTA), eta = dbltor(ETA), halfplus1 = dbltor(1.5);
     344      899064 :   const long triangular = 0;
     345             :   pari_timer T;
     346      899064 :   GEN B = *ptrB, U;
     347      899064 :   long cnt = 0;
     348             : 
     349      899064 :   d = lg(B)-1;
     350      899064 :   if (gram)
     351             :   {
     352       19014 :     G = B;
     353       19014 :     n = d;
     354       19014 :     B = cgetg(1, t_VECSMALL); /* dummy */
     355             :   }
     356             :   else
     357             :   {
     358      880050 :     G = zeromatcopy(d,d);
     359      880050 :     n = nbrows(B);
     360             :   }
     361      899064 :   U = *ptrU; /* NULL if inplace */
     362             : 
     363      899064 :   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      899064 :   mu = cgetg(d+1, t_MAT);
     370      899064 :   r  = cgetg(d+1, t_MAT);
     371      899064 :   s  = cgetg(d+1, t_VEC);
     372     3721554 :   for (j = 1; j <= d; j++)
     373             :   {
     374     2822490 :     GEN M = cgetg(d+1, t_COL), R = cgetg(d+1, t_COL);
     375     2822490 :     gel(mu,j)= M;
     376     2822490 :     gel(r,j) = R;
     377     2822490 :     gel(s,j) = cgetr(prec);
     378    15255984 :     for (i = 1; i <= d; i++) {
     379    12433494 :       gel(R,i) = cgetr(prec);
     380    12433494 :       gel(M,i) = cgetr(prec);
     381             :     }
     382             :   }
     383      899064 :   SPtmp = zerovec(d+1);
     384      899064 :   alpha = cgetg(d+1, t_VECSMALL);
     385      899064 :   av = avma;
     386             : 
     387             :   /* Step2: Initializing the main loop */
     388      899064 :   kappamax = 1;
     389      899064 :   i = 1;
     390      899064 :   maxG = d; /* later updated to kappamax if (!gram) */
     391             : 
     392             :   do {
     393      902652 :     if (!gram) gmael(G,i,i) = ZV_dotsquare(gel(B,i));
     394      902652 :     affir(gmael(G,i,i), gmael(r,i,i));
     395      902652 :   } while (signe(gmael(G,i,i)) == 0 && (++i <=d));
     396      899064 :   zeros = i-1; /* all vectors B[i] with i <= zeros are zero vectors */
     397      899064 :   kappa = i;
     398      899064 :   if (zeros < d) affir(gmael(G,zeros+1,zeros+1), gmael(r,zeros+1,zeros+1));
     399      899064 :   for (i=zeros+1; i<=d; i++) alpha[i]=1;
     400             : 
     401    14578434 :   while (++kappa <= d)
     402             :   {
     403    12780306 :     if (kappa>kappamax)
     404             :     {
     405     1919838 :       if (DEBUGLEVEL>=4) err_printf("K%ld ",kappa);
     406     1919838 :       kappamax = kappa;
     407     1919838 :       if (!gram) {
     408     7912146 :         for (i=zeros+1; i<=kappa; i++)
     409     6086844 :           gmael(G,kappa,i) = ZV_dotproduct(gel(B,kappa), gel(B,i));
     410     1825302 :         maxG = kappamax;
     411             :       }
     412             :     }
     413             :     /* Step3: Call to the Babai algorithm, mu,r,s updated in place */
     414    23929872 :     bab = Babai(av, kappa, &G,&B,&U, mu,r,s, alpha[kappa], zeros, maxG,
     415    11149566 :       gram? 0 : ((triangular && kappamax <= n) ? kappamax: n),
     416             :       eta, halfplus1, prec);
     417    12780306 :     if (bab) {*ptrB=(gram?G:B); *ptrU=U; return NULL; }
     418             : 
     419    12780306 :     av2 = avma;
     420    25555944 :     if ((keepfirst && kappa == 2) ||
     421    12775638 :         cmprr(mulrr(gmael(r,kappa-1,kappa-1), delta), gel(s,kappa-1)) <= 0)
     422             :     { /* Step4: Success of Lovasz's condition */
     423     8056128 :       alpha[kappa] = kappa;
     424     8056128 :       tmp = mulrr(gmael(mu,kappa,kappa-1), gmael(r,kappa,kappa-1));
     425     8056128 :       affrr(subrr(gel(s,kappa-1), tmp), gmael(r,kappa,kappa));
     426     8056128 :       avma = av2;
     427             :     }
     428             :     else
     429             :     { /* Step5: Find the right insertion index kappa, kappa2 = initial kappa */
     430     4724178 :       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     4724178 :       kappa2 = kappa;
     433             :       do {
     434    10883910 :         kappa--;
     435    10883910 :         if (kappa<zeros+2 + (keepfirst ? 1: 0)) break;
     436     8628462 :         tmp = mulrr(gmael(r,kappa-1,kappa-1), delta);
     437     8628462 :       } while (cmprr(gel(s,kappa-1), tmp) <=0 );
     438     4724178 :       avma = av2;
     439             : 
     440    15608088 :       for (i=kappa; i<kappa2; i++)
     441    10883910 :         if (kappa <= alpha[i]) alpha[i] = kappa;
     442     4724178 :       for (i=kappa2; i>kappa; i--) alpha[i] = alpha[i-1];
     443    14869722 :       for (i=kappa2+1; i<=kappamax; i++)
     444    10145544 :         if (kappa < alpha[i]) alpha[i] = kappa;
     445     4724178 :       alpha[kappa] = kappa;
     446             : 
     447             :       /* Step6: Update the mu's and r's */
     448     4724178 :       rotate(mu,kappa2,kappa,d);
     449     4724178 :       rotate(r,kappa2,kappa,d);
     450     4724178 :       affrr(gel(s,kappa), gmael(r,kappa,kappa));
     451             : 
     452             :       /* Step7: Update B, G, U */
     453     4724178 :       if (!gram) rotate(B,kappa2,kappa,n);
     454     4724178 :       if (U) rotate(U,kappa2,kappa,d);
     455             : 
     456     4724178 :       for (i=1; i<=kappa2; i++) gel(SPtmp,i) = gmael(G,kappa2,i);
     457     4724178 :       for (i=kappa2+1; i<=maxG; i++) gel(SPtmp,i) = gmael(G,i,kappa2);
     458    15608088 :       for (i=kappa2; i>kappa; i--)
     459             :       {
     460    10883910 :         for (j=1; j<kappa; j++) gmael(G,i,j) = gmael(G,i-1,j);
     461    10883910 :         gmael(G,i,kappa) = gel(SPtmp,i-1);
     462    10883910 :         for (j=kappa+1; j<=i; j++) gmael(G,i,j) = gmael(G,i-1,j-1);
     463    10883910 :         for (j=kappa2+1; j<=maxG; j++) gmael(G,j,i) = gmael(G,j,i-1);
     464             :       }
     465     4724178 :       for (i=1; i<kappa; i++) gmael(G,kappa,i) = gel(SPtmp,i);
     466     4724178 :       gmael(G,kappa,kappa) = gel(SPtmp,kappa2);
     467     4724178 :       for (i=kappa2+1; i<=maxG; i++) gmael(G,i,kappa) = gel(SPtmp,i);
     468             : 
     469             :       /* Step8: Prepare the next loop iteration */
     470     4724178 :       if (kappa == zeros+1 && !signe(gmael(G,kappa,kappa)))
     471             :       {
     472       23442 :         zeros++; kappa++;
     473       23442 :         affir(gmael(G,kappa,kappa), gmael(r,kappa,kappa));
     474             :       }
     475             :     }
     476             :   }
     477             : 
     478      899064 :   if (DEBUGLEVEL>=4) timer_printf(&T,"LLL");
     479      899064 :   if (ptrr) *ptrr = RgM_diagonal_shallow(r);
     480      899064 :   if (!U)
     481             :   {
     482       57774 :     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      841290 :   else if (flag & (LLL_IM|LLL_KER|LLL_ALL))
     493      837594 :     U = lll_finish(U, zeros, flag);
     494      899064 :   if (gram)
     495             :   {
     496       19014 :     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      880050 :   return U? U: B;
     502             : }
     503             : 
     504             : /* Assume x a ZM, if ptB != NULL, set it to Gram-Schmidt (squared) norms */
     505             : GEN
     506      903504 : ZM_lll_norms(GEN x, double DELTA, long flag, GEN *B)
     507             : {
     508      903504 :   pari_sp ltop = avma;
     509      903504 :   const long compat = flag & LLL_COMPATIBLE;
     510      903504 :   const double ETA = 0.51;
     511      903504 :   long p, n = lg(x)-1;
     512             :   GEN U;
     513      903504 :   if (n <= 1) return lll_trivial(x, flag);
     514      899064 :   x = RgM_shallowcopy(x);
     515      899064 :   U = (flag & LLL_INPLACE)? NULL: matid(n);
     516      899064 :   for (p = compat? DEFAULTPREC: LOWDEFAULTPREC;;)
     517             :   {
     518      899064 :     GEN m = fplll(&x, &U, B, DELTA, ETA, flag, p);
     519      899064 :     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         324 : pslg(GEN x)
     535             : {
     536             :   long tx;
     537         324 :   if (gequal0(x)) return 2;
     538         288 :   tx = typ(x); return is_scalar_t(tx)? 3: lg(x);
     539             : }
     540             : 
     541             : static int
     542         126 : REDgen(long k, long l, GEN h, GEN L, GEN B)
     543             : {
     544         126 :   GEN q, u = gcoeff(L,k,l);
     545             :   long i;
     546             : 
     547         126 :   if (pslg(u) < pslg(B)) return 0;
     548             : 
     549          90 :   q = gneg(gdeuc(u,B));
     550          90 :   gel(h,k) = gadd(gel(h,k), gmul(q,gel(h,l)));
     551          90 :   for (i=1; i<l; i++) gcoeff(L,k,i) = gadd(gcoeff(L,k,i), gmul(q,gcoeff(L,l,i)));
     552          90 :   gcoeff(L,k,l) = gadd(gcoeff(L,k,l), gmul(q,B)); return 1;
     553             : }
     554             : 
     555             : static int
     556         126 : 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         126 :   if (!fl[k-1]) return 0;
     562             : 
     563          90 :   la = gcoeff(L,k,k-1); la2 = gsqr(la);
     564          90 :   Bk = gel(B,k);
     565          90 :   if (fl[k])
     566             :   {
     567          36 :     GEN q = gadd(la2, gmul(gel(B,k-1),gel(B,k+1)));
     568          36 :     ps1 = pslg(gsqr(Bk));
     569          36 :     ps2 = pslg(q);
     570          36 :     if (ps1 <= ps2 && (ps1 < ps2 || !*flc)) return 0;
     571          18 :     *flc = (ps1 != ps2);
     572          18 :     gel(B,k) = gdiv(q, Bk);
     573             :   }
     574             : 
     575          72 :   swap(gel(h,k-1), gel(h,k)); lx = lg(L);
     576          72 :   for (j=1; j<k-1; j++) swap(gcoeff(L,k-1,j), gcoeff(L,k,j));
     577          72 :   if (fl[k])
     578             :   {
     579          18 :     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          54 :   else if (!gequal0(la))
     589             :   {
     590          18 :     p1 = gdiv(la2, Bk);
     591          18 :     gel(B,k+1) = gel(B,k) = p1;
     592          18 :     for (i=k+2; i<=lx; i++) gel(B,i) = gdiv(gmul(p1,gel(B,i)),Bk);
     593          18 :     for (i=k+1; i<lx; i++)
     594           0 :       gcoeff(L,i,k-1) = gdiv(gmul(la,gcoeff(L,i,k-1)), Bk);
     595          18 :     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          36 :     gcoeff(L,k,k-1) = gen_0;
     602          36 :     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          36 :     B[k] = B[k-1]; fl[k] = 1; fl[k-1] = 0;
     608             :   }
     609          72 :   return 1;
     610             : }
     611             : 
     612             : static void
     613         108 : incrementalGSgen(GEN x, GEN L, GEN B, long k, GEN fl)
     614             : {
     615         108 :   GEN u = NULL; /* gcc -Wall */
     616             :   long i, j, tu;
     617         270 :   for (j=1; j<=k; j++)
     618         162 :     if (j==k || fl[j])
     619             :     {
     620         162 :       u = gcoeff(x,k,j); tu = typ(u);
     621         162 :       if (! is_extscalar_t(tu)) pari_err_TYPE("incrementalGSgen",u);
     622         216 :       for (i=1; i<j; i++)
     623          54 :         if (fl[i])
     624             :         {
     625          54 :           u = gsub(gmul(gel(B,i+1),u), gmul(gcoeff(L,k,i),gcoeff(L,j,i)));
     626          54 :           u = gdiv(u, gel(B,i));
     627             :         }
     628         162 :       gcoeff(L,k,j) = u;
     629             :     }
     630         108 :   if (gequal0(u)) B[k+1] = B[k];
     631             :   else
     632             :   {
     633          72 :     gel(B,k+1) = gcoeff(L,k,k); gcoeff(L,k,k) = gen_1; fl[k] = 1;
     634             :   }
     635         108 : }
     636             : 
     637             : static GEN
     638         108 : lllgramallgen(GEN x, long flag)
     639             : {
     640         108 :   long lx = lg(x), i, j, k, l, n;
     641             :   pari_sp av;
     642             :   GEN B, L, h, fl;
     643             :   int flc;
     644             : 
     645         108 :   n = lx-1; if (n<=1) return lll_trivial(x,flag);
     646          54 :   if (lgcols(x) != lx) pari_err_DIM("lllgramallgen");
     647             : 
     648          54 :   fl = cgetg(lx, t_VECSMALL);
     649             : 
     650          54 :   av = avma;
     651          54 :   B = scalarcol_shallow(gen_1, lx);
     652          54 :   L = cgetg(lx,t_MAT);
     653          54 :   for (j=1; j<lx; j++) { gel(L,j) = zerocol(n); fl[j] = 0; }
     654             : 
     655          54 :   h = matid(n);
     656         162 :   for (i=1; i<lx; i++)
     657         108 :     incrementalGSgen(x, L, B, i, fl);
     658          54 :   flc = 0;
     659          54 :   for(k=2;;)
     660             :   {
     661         126 :     if (REDgen(k, k-1, h, L, gel(B,k))) flc = 1;
     662         126 :     if (do_SWAPgen(h, L, B, k, fl, &flc)) { if (k > 2) k--; }
     663             :     else
     664             :     {
     665          54 :       for (l=k-2; l>=1; l--)
     666           0 :         if (REDgen(k, l, h, L, gel(B,l+1))) flc = 1;
     667          54 :       if (++k > n) break;
     668             :     }
     669          72 :     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          72 :   }
     675          54 :   k=1; while (k<lx && !fl[k]) k++;
     676          54 :   return lll_finish(h,k-1,flag);
     677             : }
     678             : 
     679             : static GEN
     680         108 : lllallgen(GEN x, long flag)
     681             : {
     682         108 :   pari_sp av = avma;
     683         108 :   if ((flag & LLL_GRAM) == 0) x = gram_matrix(x);
     684         108 :   return gerepilecopy(av, lllgramallgen(x, flag));
     685             : }
     686             : GEN
     687          36 : lllgen(GEN x) { return lllallgen(x, LLL_IM); }
     688             : GEN
     689          36 : 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          36 : lllgramkerimgen(GEN x)  { return lllallgen(x, LLL_ALL|LLL_GRAM); }
     694             : 
     695             : static GEN
     696       23052 : lllall(GEN x, long flag)
     697             : {
     698       23052 :   pari_sp av = avma;
     699       23052 :   return gerepilecopy(av, ZM_lll(x, LLLDFT, flag));
     700             : }
     701             : GEN
     702        5880 : lllint(GEN x) { return lllall(x, LLL_IM); }
     703             : GEN
     704          30 : lllkerim(GEN x) { return lllall(x, LLL_ALL); }
     705             : GEN
     706       17112 : lllgramint(GEN x) { return lllall(x, LLL_IM | LLL_GRAM); }
     707             : GEN
     708          30 : lllgramkerim(GEN x) { return lllall(x, LLL_ALL | LLL_GRAM); }
     709             : 
     710             : GEN
     711      184476 : lllfp(GEN x, double D, long flag)
     712             : {
     713      184476 :   long n = lg(x)-1;
     714      184476 :   pari_sp av = avma;
     715             :   GEN h;
     716      184476 :   if (n <= 1) return lll_trivial(x,flag);
     717      183612 :   h = ZM_lll(RgM_rescale_to_int(x), D, flag);
     718      183588 :   return gerepilecopy(av, h);
     719             : }
     720             : 
     721             : GEN
     722          48 : lllgram(GEN x) { return lllfp(x,LLLDFT,LLL_GRAM|LLL_IM); }
     723             : GEN
     724      180732 : lll(GEN x) { return lllfp(x,LLLDFT,LLL_IM); }
     725             : 
     726             : GEN
     727         252 : qflll0(GEN x, long flag)
     728             : {
     729         252 :   if (typ(x) != t_MAT) pari_err_TYPE("qflll",x);
     730         252 :   switch(flag)
     731             :   {
     732          42 :     case 0: return lll(x);
     733          54 :     case 1: RgM_check_ZM(x,"qflll"); return lllint(x);
     734          42 :     case 2: RgM_check_ZM(x,"qflll"); return lllintpartial(x);
     735          42 :     case 4: RgM_check_ZM(x,"qflll"); return lllkerim(x);
     736          36 :     case 5: return lllkerimgen(x);
     737          36 :     case 8: return lllgen(x);
     738           0 :     default: pari_err_FLAG("qflll");
     739             :   }
     740             :   return NULL; /* LCOV_EXCL_LINE */
     741             : }
     742             : 
     743             : GEN
     744         168 : qflllgram0(GEN x, long flag)
     745             : {
     746         168 :   if (typ(x) != t_MAT) pari_err_TYPE("qflllgram",x);
     747         168 :   switch(flag)
     748             :   {
     749          48 :     case 0: return lllgram(x);
     750          42 :     case 1: RgM_check_ZM(x,"qflllgram"); return lllgramint(x);
     751          42 :     case 4: RgM_check_ZM(x,"qflllgram"); return lllgramkerim(x);
     752          36 :     case 5: return lllgramkerimgen(x);
     753           0 :     case 8: return lllgramgen(x);
     754           0 :     default: pari_err_FLAG("qflllgram");
     755             :   }
     756             :   return NULL; /* LCOV_EXCL_LINE */
     757             : }
     758             : 
     759             : /********************************************************************/
     760             : /**                                                                **/
     761             : /**                   INTEGRAL KERNEL (LLL REDUCED)                **/
     762             : /**                                                                **/
     763             : /********************************************************************/
     764             : static GEN
     765          24 : kerint0(GEN M)
     766             : {
     767          24 :   GEN U, H = ZM_hnfall(M,&U,1);
     768          24 :   long d = lg(M)-lg(H);
     769          24 :   if (!d) return cgetg(1, t_MAT);
     770          24 :   return ZM_lll(vecslice(U,1,d), LLLDFT, LLL_INPLACE);
     771             : }
     772             : GEN
     773           0 : kerint(GEN M)
     774             : {
     775           0 :   pari_sp av = avma;
     776           0 :   return gerepilecopy(av, kerint0(M));
     777             : }
     778             : /* OBSOLETE: use kerint */
     779             : GEN
     780          24 : matkerint0(GEN M, long flag)
     781             : {
     782          24 :   pari_sp av = avma;
     783          24 :   if (typ(M) != t_MAT) pari_err_TYPE("matkerint",M);
     784          24 :   M = Q_primpart(M);
     785          24 :   RgM_check_ZM(M, "kerint");
     786          24 :   switch(flag)
     787             :   {
     788             :     case 0:
     789          24 :     case 1: return gerepilecopy(av, kerint0(M));
     790           0 :     default: pari_err_FLAG("matkerint");
     791             :   }
     792             :   return NULL; /* LCOV_EXCL_LINE */
     793             : }

Generated by: LCOV version 1.11