<commit_before>/* Copyright (C) 2005-2008 <NAME>.
   Copyright (C) 2007 <NAME>.
   Copyright (C) 2011 <NAME>.

   This file is part of fplll. fplll is free software: you
   can redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License as published by the Free Software Foundation,
   either version 2.1 of the License, or (at your option) any later version.

   fplll is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with fplll. If not, see <http://www.gnu.org/licenses/>. */

/* Template source file */

#include "lll.h"
#include "util.h"

FPLLL_BEGIN_NAMESPACE

static inline bool is_power_of_2(int i) { return (i & (i - 1)) == 0; }

template <class ZT, class FT>
LLLReduction<ZT, FT>::LLLReduction(MatGSO<ZT, FT> &m, double delta, double eta, int flags)
    : status(RED_SUCCESS), final_kappa(0), last_early_red(0), n_swaps(0), m(m)
{
  /* No early reduction in proved mode (i.e. enable_int_gram=true).
     NOTE: To make this possible, the hypothesis "g(i, j) is valid if
     0 <= i < n_known_rows and j <= i" in gso.h should be changed and
     MatGSO<ZT, FT>::discover_row() should be rewritten. */
  enable_early_red = (flags & LLL_EARLY_RED) && !m.enable_int_gram;
  siegel           = flags & LLL_SIEGEL;
  verbose          = flags & LLL_VERBOSE;
  this->delta      = delta;
  this->eta        = eta;
  swap_threshold   = siegel ? delta - eta * eta : delta;
  zeros            = 0;
}

template <class ZT, class FT>
bool LLLReduction<ZT, FT>::lll(int kappa_min, int kappa_start, int kappa_end)
{
  if (kappa_end == -1)
    kappa_end = m.d;

  FPLLL_DEBUG_CHECK(kappa_min <= kappa_start && kappa_start < kappa_end && kappa_end <= m.d);
  int start_time = cputime();
  int kappa      = kappa_start + 1;
  int kappa_max  = 0;
  int d          = kappa_end - kappa_min;

  zeros       = 0;
  n_swaps     = 0;
  final_kappa = 0;
  if (verbose)
    print_params();
  extend_vect(lovasz_tests, kappa_end);
  extend_vect(babai_mu, kappa_end);
  extend_vect(babai_expo, kappa_end);

  for (; zeros < d && m.b[0].is_zero(); zeros++)
  {
    m.move_row(kappa_min, kappa_end - 1 - zeros);
  }

  if (zeros < d &&
      ((kappa_start > 0 && !babai(kappa_start, kappa_start)) || !m.update_gso_row(kappa_start)))
  {
    final_kappa = kappa_start;
    return false;
  }

  long long iter, max_iter;
  max_iter = static_cast<long long>(
      d - 2 * d * (d + 1) * ((m.b.get_max_exp() + 3) / std::log(delta.get_d())));

  for (iter = 0; iter < max_iter && kappa < kappa_end - zeros; iter++)
  {
    if (kappa > kappa_max)
    {
      if (verbose)
      {
        cerr << "Discovering vector " << kappa - kappa_min + 1 + zeros << "/" << d
             << " cputime=" << cputime() - start_time << endl;
      }
      kappa_max = kappa;
      if (enable_early_red && is_power_of_2(kappa) && kappa > last_early_red)
      {
        if (!early_reduction(kappa))
        {
          final_kappa = kappa;
          return false;
        }
      }
    }

    // Lazy size reduction
    if (!babai(kappa, kappa))
    {
      final_kappa = kappa;
      return false;
    }

    // Tests Lovasz's condition
    m.get_gram(lovasz_tests[0], kappa, kappa);
    for (int i = 1; i <= kappa; i++)
    {
      ftmp1.mul(m.get_mu_exp(kappa, i - 1), m.get_r_exp(kappa, i - 1));
      lovasz_tests[i].sub(lovasz_tests[i - 1], ftmp1);
    }
    ftmp1.mul(m.get_r_exp(kappa - 1, kappa - 1), swap_threshold);
    if (m.enable_row_expo)
    {
      ftmp1.mul_2si(ftmp1, 2 * (m.row_expo[kappa - 1] - m.row_expo[kappa]));
    }

    if (ftmp1 > lovasz_tests[siegel ? kappa : kappa - 1])
    {
      n_swaps++;
      // Failure, computes the insertion index
      int old_k = kappa;
      for (kappa--; kappa > kappa_start; kappa--)
      {
        ftmp1.mul(m.get_r_exp(kappa - 1, kappa - 1), swap_threshold);
        if (m.enable_row_expo)
        {
          ftmp1.mul_2si(ftmp1, 2 * (m.row_expo[kappa - 1] - m.row_expo[old_k]));
        }
        if (ftmp1 < lovasz_tests[siegel ? kappa : kappa - 1])
          break;<commit_msg>fix bug: kappa_min not kappa_start<commit_after>/* Copyright (C) 2005-2008 <NAME>.
   Copyright (C) 2007 <NAME>.
   Copyright (C) 2011 <NAME>.

   This file is part of fplll. fplll is free software: you
   can redistribute it and/or modify it under the terms of the GNU Lesser
   General Public License as published by the Free Software Foundation,
   either version 2.1 of the License, or (at your option) any later version.

   fplll is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with fplll. If not, see <http://www.gnu.org/licenses/>. */

/* Template source file */

#include "lll.h"
#include "util.h"

FPLLL_BEGIN_NAMESPACE

static inline bool is_power_of_2(int i) { return (i & (i - 1)) == 0; }

template <class ZT, class FT>
LLLReduction<ZT, FT>::LLLReduction(MatGSO<ZT, FT> &m, double delta, double eta, int flags)
    : status(RED_SUCCESS), final_kappa(0), last_early_red(0), n_swaps(0), m(m)
{
  /* No early reduction in proved mode (i.e. enable_int_gram=true).
     NOTE: To make this possible, the hypothesis "g(i, j) is valid if
     0 <= i < n_known_rows and j <= i" in gso.h should be changed and
     MatGSO<ZT, FT>::discover_row() should be rewritten. */
  enable_early_red = (flags & LLL_EARLY_RED) && !m.enable_int_gram;
  siegel           = flags & LLL_SIEGEL;
  verbose          = flags & LLL_VERBOSE;
  this->delta      = delta;
  this->eta        = eta;
  swap_threshold   = siegel ? delta - eta * eta : delta;
  zeros            = 0;
}

template <class ZT, class FT>
bool LLLReduction<ZT, FT>::lll(int kappa_min, int kappa_start, int kappa_end)
{
  if (kappa_end == -1)
    kappa_end = m.d;

  FPLLL_DEBUG_CHECK(kappa_min <= kappa_start && kappa_start < kappa_end && kappa_end <= m.d);
  int start_time = cputime();
  int kappa      = kappa_start + 1;
  int kappa_max  = 0;
  int d          = kappa_end - kappa_min;

  zeros       = 0;
  n_swaps     = 0;
  final_kappa = 0;
  if (verbose)
    print_params();
  extend_vect(lovasz_tests, kappa_end);
  extend_vect(babai_mu, kappa_end);
  extend_vect(babai_expo, kappa_end);

  for (; zeros < d && m.b[0].is_zero(); zeros++)
  {
    m.move_row(kappa_min, kappa_end - 1 - zeros);
  }

  if (zeros < d &&
      ((kappa_start > 0 && !babai(kappa_start, kappa_start)) || !m.update_gso_row(kappa_start)))
  {
    final_kappa = kappa_start;
    return false;
  }

  long long iter, max_iter;
  max_iter = static_cast<long long>(
      d - 2 * d * (d + 1) * ((m.b.get_max_exp() + 3) / std::log(delta.get_d())));

  for (iter = 0; iter < max_iter && kappa < kappa_end - zeros; iter++)
  {
    if (kappa > kappa_max)
    {
      if (verbose)
      {
        cerr << "Discovering vector " << kappa - kappa_min + 1 + zeros << "/" << d
             << " cputime=" << cputime() - start_time << endl;
      }
      kappa_max = kappa;
      if (enable_early_red && is_power_of_2(kappa) && kappa > last_early_red)
      {
        if (!early_reduction(kappa))
        {
          final_kappa = kappa;
          return false;
        }
      }
    }

    // Lazy size reduction
    if (!babai(kappa, kappa))
    {
      final_kappa = kappa;
      return false;
    }

    // Tests Lovasz's condition
    m.get_gram(lovasz_tests[0], kappa, kappa);
    for (int i = 1; i <= kappa; i++)
    {
      ftmp1.mul(m.get_mu_exp(kappa, i - 1), m.get_r_exp(kappa, i - 1));
      lovasz_tests[i].sub(lovasz_tests[i - 1], ftmp1);
    }
    ftmp1.mul(m.get_r_exp(kappa - 1, kappa - 1), swap_threshold);
    if (m.enable_row_expo)
    {
      ftmp1.mul_2si(ftmp1, 2 * (m.row_expo[kappa - 1] - m.row_expo[kappa]));
    }

    if (ftmp1 > lovasz_tests[siegel ? kappa : kappa - 1])
    {
      n_swaps++;
      // Failure, computes the insertion index
      int old_k = kappa;
      for (kappa--; kappa > kappa_min; kappa--)
      {
        ftmp1.mul(m.get_r_exp(kappa - 1, kappa - 1), swap_threshold);
        if (m.enable_row_expo)
        {
          ftmp1.mul_2si(ftmp1, 2 * (m.row_expo[kappa - 1] - m.row_expo[old_k]));
        }
        if (ftmp1 < lovasz_tests[siegel ? kappa : kappa - 1])
          break;
{'fraction_non_alphanumeric': 0.12875188318461003, 'fraction_numerical': 0.015760806582454514, 'mean_word_length': 2.591344153141906, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2301557', 'n_tokens_mistral': 3342, 'n_tokens_neox': 3190, 'n_words': 855}