File: test_cvp.cpp

package info (click to toggle)
fplll 5.5.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,984 kB
  • sloc: cpp: 21,104; javascript: 1,284; sh: 1,050; makefile: 198; perl: 46; python: 42
file content (143 lines) | stat: -rw-r--r-- 4,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright (C) 2016 Guillaume Bonnoron

   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/>. */

#include "test_utils.h"
#include <cstring>
#include <fplll/fplll.h>

#ifndef TESTDATADIR
#define TESTDATADIR ".."
#endif
using namespace std;
using namespace fplll;

/**
   @brief Test if CVP function returns correct vector.

   @param A              input lattice
   @param b              expected vector
   @return
*/

template <class ZT>
int test_cvp(ZZ_mat<ZT> &A, vector<Z_NR<mpz_t>> &target, vector<Z_NR<mpz_t>> &b, const int method)
{
  vector<Z_NR<mpz_t>> sol_coord;  // In the LLL-reduced basis
  vector<Z_NR<mpz_t>> solution;
  ZZ_mat<mpz_t> u;

  // cerr << "A " << endl << A << endl;
  int status =
      lll_reduction(A, u, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, LLL_DEFAULT);
  if (status != RED_SUCCESS)
  {
    cerr << "LLL reduction failed: " << get_red_status_str(status) << endl;
    return status;
  }

  status = closest_vector(A, target, sol_coord, method);

  if (status != RED_SUCCESS)
  {
    cerr << "Failure: " << get_red_status_str(status) << endl;
    return status;
  }

  vector_matrix_product(solution, sol_coord, A);

  // cerr << "A red" << endl << A << endl;
  // cerr << "sol_coord : " << sol_coord << endl;
  // cerr << "solution : " << solution << endl;
  // cerr << "expected : " << b << endl;
  // cerr << "target : " << target << endl;

  bool correct = true;
  for (int i = 0; i < A.get_cols(); i++)
  {
    correct = correct && (solution[i] == b[i]);
  }
  if (!correct)
    return 1;

  return 0;
}

/**
   @brief Test if CVP function returns the correct vector

   @param input_filename_lattice   filename of an input lattice
   @param input_filename_target    filename of a target vector
   @param output_filename  filename of the expected vector
   @return
*/

template <class ZT>
int test_filename(const char *input_filename_lattice, const char *input_filename_target,
                  const char *output_filename, const int method = CVPM_FAST)
{
  ZZ_mat<ZT> A;
  int status = 0;
  status |= read_file(A, input_filename_lattice);

  vector<Z_NR<mpz_t>> t;
  status |= read_file(t, input_filename_target);

  vector<Z_NR<mpz_t>> b;
  status |= read_file(b, output_filename);

  status |= test_cvp<ZT>(A, t, b, method);
  return status;
}

/**
   @brief Run CVP tests.

   @param argc             ignored
   @param argv             ignored
   @return
*/

int main(int argc, char *argv[])
{

  int status = 0;
  status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_cvp_in_lattice",
                                 TESTDATADIR "/tests/lattices/example_cvp_in_target",
                                 TESTDATADIR "/tests/lattices/example_cvp_out");
  status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_cvp_in_lattice2",
                                 TESTDATADIR "/tests/lattices/example_cvp_in_target2",
                                 TESTDATADIR "/tests/lattices/example_cvp_out2");
  status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_cvp_in_lattice3",
                                 TESTDATADIR "/tests/lattices/example_cvp_in_target3",
                                 TESTDATADIR "/tests/lattices/example_cvp_out3");
  status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_cvp_in_lattice4",
                                 TESTDATADIR "/tests/lattices/example_cvp_in_target4",
                                 TESTDATADIR "/tests/lattices/example_cvp_out4", CVPM_PROVED);
  status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_cvp_in_lattice5",
                                 TESTDATADIR "/tests/lattices/example_cvp_in_target5",
                                 TESTDATADIR "/tests/lattices/example_cvp_out5", CVPM_PROVED);

  if (status == 0)
  {
    cerr << "All tests passed." << endl;
    return 0;
  }
  else
  {
    return -1;
  }

  return 0;
}