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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/* Copyright (C) 2016 Martin Albrecht
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>
#include <fplll/io/json.hpp>
using json = nlohmann::json;
#ifndef TESTDATADIR
#define TESTDATADIR ".."
#endif
using namespace std;
using namespace fplll;
/**
@brief Test BKZ reduction.
@param A test matrix
@param block_size block size
@param float_type floating point type to test
@param flags flags to use
@param prec precision if mpfr is used
@return zero on success.
*/
template <class ZT>
int test_bkz(ZZ_mat<ZT> &A, const int block_size, FloatType float_type, int flags = BKZ_DEFAULT,
int prec = 0)
{
int status = 0;
// zero on success
status = bkz_reduction(A, block_size, flags, float_type, prec);
if (status != RED_SUCCESS)
{
cerr << "BKZ reduction failed with error '" << get_red_status_str(status);
cerr << " for float type " << FLOAT_TYPE_STR[float_type] << endl;
}
return status;
}
/**
@brief Test BKZ strategy interface.
@param A test matrix
@param block_size block size
@return zero on success.
*/
template <class ZT>
int test_bkz_param(ZZ_mat<ZT> &A, const int block_size, int flags = BKZ_DEFAULT,
string dump_gso_filename = string())
{
int status = 0;
vector<Strategy> strategies;
for (long b = 0; b <= block_size; b++)
{
Strategy strategy = Strategy::EmptyStrategy(b);
if (b == 10)
{
strategy.preprocessing_block_sizes.emplace_back(5);
}
else if (b == 20)
{
strategy.preprocessing_block_sizes.emplace_back(10);
}
else if (b == 30)
{
strategy.preprocessing_block_sizes.emplace_back(15);
}
strategies.emplace_back(std::move(strategy));
}
BKZParam params(block_size, strategies);
params.flags = flags;
params.dump_gso_filename = dump_gso_filename;
// zero on success
status = bkz_reduction(&A, NULL, params, FT_DEFAULT, 53);
if (status != RED_SUCCESS)
{
cerr << "BKZ parameter test failed with error '" << get_red_status_str(status) << "'" << endl;
}
return status;
}
/**
@brief Test BKZ with pruning.
@param A test matrix
@param block_size block size
@return zero on success.
*/
template <class ZT>
int test_bkz_param_linear_pruning(ZZ_mat<ZT> &A, const int block_size, int flags = BKZ_DEFAULT)
{
int status = 0;
vector<Strategy> strategies;
for (long b = 0; b < block_size; b++)
{
Strategy strategy = Strategy::EmptyStrategy(b);
if (b == 10)
{
strategy.preprocessing_block_sizes.emplace_back(5);
}
else if (b == 20)
{
strategy.preprocessing_block_sizes.emplace_back(10);
}
else if (b == 30)
{
strategy.preprocessing_block_sizes.emplace_back(15);
}
strategies.emplace_back(std::move(strategy));
}
Strategy strategy;
strategy.pruning_parameters.emplace_back(
PruningParams::LinearPruningParams(block_size, block_size / 2));
strategies.emplace_back(std::move(strategy));
BKZParam params(block_size, strategies);
params.flags = flags;
// zero on success
status = bkz_reduction(&A, NULL, params, FT_DEFAULT, 53);
if (status != RED_SUCCESS)
{
cerr << "BKZ parameter test failed with error '" << get_red_status_str(status) << "'" << endl;
}
return status;
}
template <class ZT>
int test_bkz_param_pruning(ZZ_mat<ZT> &A, const int block_size, int flags = BKZ_DEFAULT)
{
int status = 0;
vector<Strategy> strategies = load_strategies_json(TESTDATADIR "/strategies/default.json");
BKZParam params(block_size, strategies);
params.flags = flags;
// zero on success
status = bkz_reduction(&A, NULL, params, FT_DEFAULT, 53);
if (status != RED_SUCCESS)
{
cerr << "BKZ parameter test failed with error '" << get_red_status_str(status) << "'" << endl;
}
return status;
}
/**
@brief Test BKZ_DUMP_GSO for a matrix d × (d+1) integer relations matrix with bit size b (copied
from test_int_rel)
@param d dimension
@param b bit size
@param block_size block size
@param float_type floating point type to test
@param flags flags to use
@return zero on success.
*/
template <class ZT>
int test_int_rel_bkz_dump_gso(int d, int b, const int block_size,
int flags = BKZ_DEFAULT | BKZ_DUMP_GSO)
{
ZZ_mat<ZT> A, B;
A.resize(d, d + 1);
A.gen_intrel(b);
B = A;
int status = 0;
// TODO: maybe not safe.
string file_bkz_dump_gso = tmpnam(nullptr);
status |= test_bkz_param<ZT>(B, block_size, flags, file_bkz_dump_gso);
if (status != 0)
{
cerr << "Error in test_bkz_param." << endl;
return status;
}
json js;
std::ifstream fs(file_bkz_dump_gso);
if (fs.fail())
{
cerr << "File " << file_bkz_dump_gso << " cannot be loaded." << endl;
return 1;
}
fs >> js;
int loop = -1;
double time = 0.0;
for (auto i : js)
{
// Verify if there are as much norms as there are rows in A
if (A.get_rows() != (int)i["norms"].size())
{
cerr << "The array \"norms\" does not contain enough values (" << A.get_rows()
<< " expected but " << i["norms"].size() << " found)." << endl;
return 1;
}
// Extract data from json file
const string step_js = i["step"];
const int loop_js = i["loop"];
const double time_js = i["time"];
// Verify if loop of Input and Output have loop = -1
if (step_js.compare("Input") == 0 || step_js.compare("Output") == 0)
{
if (loop_js != -1)
{
cerr << "Steps Input or Output are not with \"loop\" = -1." << endl;
return 1;
}
}
else
{
// Verify that loop increases
loop++;
if (loop_js != loop)
{
cerr << "Loop does not increase." << endl;
return 1;
}
// Verify that time increases
if (time > time_js)
{
cerr << "Time does not increase." << endl;
return 1;
}
time = time_js;
}
}
return 0;
}
/**
@brief Test BKZ for matrix stored in file pointed to by `input_filename`.
@param input_filename a path
@param block_size block size
@param float_type floating point type to test
@param flags flags to use
@param prec precision if mpfr is used
@return zero on success
*/
template <class ZT>
int test_filename(const char *input_filename, const int block_size,
FloatType float_type = FT_DEFAULT, int flags = BKZ_DEFAULT, int prec = 0)
{
ZZ_mat<ZT> A, B;
int status = 0;
status |= read_file(A, input_filename);
B = A;
status |= test_bkz<ZT>(A, block_size, float_type, flags, prec);
status |= test_bkz_param<ZT>(B, block_size);
return status;
}
/**
@brief Construct d × (d+1) integer relations matrix with bit size b and test BKZ.
@param d dimension
@param b bit size
@param block_size block size
@param float_type floating point type to test
@param flags flags to use
@param prec precision if mpfr is used
@return zero on success
*/
template <class ZT>
int test_int_rel(int d, int b, const int block_size, FloatType float_type = FT_DEFAULT,
int flags = BKZ_DEFAULT, int prec = 0)
{
ZZ_mat<ZT> A, B;
A.resize(d, d + 1);
A.gen_intrel(b);
B = A;
int status = 0;
status |= test_bkz<ZT>(A, block_size, float_type, flags | BKZ_VERBOSE, prec);
status |= test_bkz_param<ZT>(B, block_size);
status |= test_bkz_param_linear_pruning<ZT>(B, block_size);
status |= test_bkz_param_pruning<ZT>(B, block_size);
return status;
}
int test_linear_dep()
{
ZZ_mat<mpz_t> A;
std::stringstream("[[1 2 3]\n [4 5 6]\n [7 8 9]]\n") >> A;
return test_bkz_param<mpz_t>(A, 3);
}
int main(int /*argc*/, char ** /*argv*/)
{
int status = 0;
status |= test_linear_dep();
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 10, FT_DEFAULT,
BKZ_DEFAULT | BKZ_AUTO_ABORT);
#ifdef FPLLL_WITH_QD
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 10, FT_DD,
BKZ_SD_VARIANT | BKZ_AUTO_ABORT);
#endif
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 10, FT_DEFAULT, BKZ_SLD_RED);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 20, FT_MPFR,
BKZ_DEFAULT | BKZ_AUTO_ABORT, 128);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 20, FT_MPFR,
BKZ_SD_VARIANT | BKZ_AUTO_ABORT, 128);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/dim55_in", 20, FT_MPFR, BKZ_SLD_RED, 128);
status |= test_int_rel<mpz_t>(50, 1000, 10, FT_DOUBLE, BKZ_DEFAULT | BKZ_AUTO_ABORT);
status |= test_int_rel<mpz_t>(50, 1000, 10, FT_DOUBLE, BKZ_SD_VARIANT | BKZ_AUTO_ABORT);
status |= test_int_rel<mpz_t>(50, 1000, 10, FT_DOUBLE, BKZ_SLD_RED);
status |= test_int_rel<mpz_t>(50, 1000, 15, FT_MPFR, BKZ_DEFAULT | BKZ_AUTO_ABORT, 100);
status |= test_int_rel<mpz_t>(50, 1000, 15, FT_MPFR, BKZ_SD_VARIANT | BKZ_AUTO_ABORT, 100);
status |= test_int_rel<mpz_t>(50, 1000, 15, FT_MPFR, BKZ_SLD_RED, 100);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_DPE, BKZ_DEFAULT | BKZ_AUTO_ABORT);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_DPE, BKZ_SD_VARIANT | BKZ_AUTO_ABORT);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_DPE, BKZ_SLD_RED);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_MPFR, BKZ_DEFAULT | BKZ_AUTO_ABORT, 53);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_MPFR, BKZ_SD_VARIANT | BKZ_AUTO_ABORT, 53);
status |= test_int_rel<mpz_t>(30, 2000, 10, FT_MPFR, BKZ_SLD_RED, 53);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DEFAULT,
BKZ_SD_VARIANT);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DEFAULT, BKZ_SLD_RED);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE, BKZ_SD_VARIANT);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE, BKZ_SLD_RED);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_MPFR,
BKZ_AUTO_ABORT, 212);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_MPFR,
BKZ_SD_VARIANT | BKZ_AUTO_ABORT, 212);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_MPFR,
BKZ_SLD_RED | BKZ_AUTO_ABORT, 212);
status |= test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE, BKZ_SD_VARIANT);
status |=
test_filename<mpz_t>(TESTDATADIR "/tests/lattices/example_in", 10, FT_DOUBLE, BKZ_SLD_RED);
// Test BKZ_DUMP_GSO
status |= test_int_rel_bkz_dump_gso<mpz_t>(50, 1000, 15, BKZ_DEFAULT | BKZ_DUMP_GSO);
if (status == 0)
{
cerr << "All tests passed." << endl;
return 0;
}
else
{
return -1;
}
return 0;
}
|