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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/* Copyright (C) 2005-2008 Damien Stehle.
Copyright (C) 2007 David Cade.
Copyright (C) 2008-2011 Xavier Pujol.
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 "main.h"
#include <config.h>
template <class ZT> int lll(Options &o, ZZ_mat<ZT> &b)
{
ZZ_mat<ZT> u, u_inv;
const char *format = o.output_format ? o.output_format : "b";
int status, flags = 0;
if (o.verbose)
flags |= LLL_VERBOSE;
if (o.early_red)
flags |= LLL_EARLY_RED;
if (o.siegel)
flags |= LLL_SIEGEL;
if (strchr(format, 'v') != NULL)
{
// LLL-reduction with transform and inverse transform
status = lll_reduction(b, u, u_inv, o.delta, o.eta, o.method, o.float_type, o.precision, flags);
}
else if (strchr(format, 'u') != NULL)
{
// LLL-reduction with transform
status = lll_reduction(b, u, o.delta, o.eta, o.method, o.float_type, o.precision, flags);
}
else
{
status = lll_reduction(b, o.delta, o.eta, o.method, o.float_type, o.precision, flags);
}
for (int i = 0; format[i]; i++)
{
switch (format[i])
{
case 'b':
cout << b << endl;
break;
case 'u':
cout << u << endl;
break;
case 'v':
cout << u_inv << endl;
break;
case 't':
cout << status << endl;
break;
case ' ':
cout << endl;
break;
}
}
if (status != RED_SUCCESS)
{
cerr << "Failure: " << get_red_status_str(status) << endl;
}
return status;
}
/* BKZ reduction */
void read_pruning_vector(const char *file_name, Pruning &pr, int n)
{
double x;
FILE *file = fopen(file_name, "r");
CHECK(file, "Cannot open '" << file_name << "'");
pr.coefficients.clear();
for (int i = 0; i <= n && fscanf(file, "%lf", &x) == 1; i++)
{
pr.coefficients.push_back(x);
CHECK(x > 0 && x <= 1, "Number " << x << " in file '" << file_name
<< "' is not in the interval (0,1]");
if (i == 0)
{
CHECK(x == 1, "The first number in file '" << file_name << "' should be 1");
}
else
{
CHECK(pr.coefficients[i] <= pr.coefficients[i - 1],
"File '" << file_name << "' should contain a non-increasing sequence of numbers");
}
}
CHECK(static_cast<int>(pr.coefficients.size()) == n,
"File '" << file_name << "' should contain exactly " << n << " numbers");
}
template <class ZT> int bkz(Options &o, ZZ_mat<ZT> &b) { ABORT_MSG("mpz required for BKZ"); }
template <> int bkz(Options &o, IntMatrix &b)
{
CHECK(o.block_size > 0, "Option -b is missing");
vector<Strategy> strategies;
if (!o.bkz_strategy_file.empty())
{
strategies = load_strategies_json(strategy_full_path(o.bkz_strategy_file));
}
BKZParam param(o.block_size, strategies);
IntMatrix u;
const char *format = o.output_format ? o.output_format : "b";
int status;
param.delta = o.delta;
param.flags = o.bkz_flags;
if (o.bkz_flags & BKZ_DUMP_GSO)
param.dump_gso_filename = o.bkz_dump_gso_filename;
if (o.bkz_flags & BKZ_GH_BND)
param.gh_factor = o.bkz_gh_factor;
if (o.bkz_flags & BKZ_MAX_LOOPS)
param.max_loops = o.bkz_max_loops;
if (o.verbose)
param.flags |= BKZ_VERBOSE;
if (o.no_lll)
param.flags |= BKZ_NO_LLL;
status = bkz_reduction(&b, strchr(format, 'u') ? &u : NULL, param, o.float_type, o.precision);
for (int i = 0; format[i]; i++)
{
switch (format[i])
{
case 'b':
cout << b << endl;
break;
case 'u':
cout << u << endl;
break;
case 't':
cout << status << endl;
break;
case ' ':
cout << endl;
break;
}
}
if (status != RED_SUCCESS)
{
cerr << "Failure: " << get_red_status_str(status) << endl;
}
return status;
}
/* HKZ reduction
Note: since we only force |mu_i,j| <= eta with eta > 0.5, the solution
is not unique even for a generic matrix */
template <class ZT> int hkz(Options &o, ZZ_mat<ZT> &b) { ABORT_MSG("mpz required for HKZ"); }
template <> int hkz(Options &o, ZZ_mat<mpz_t> &b)
{
int flags = 0;
if (o.verbose)
flags |= HKZ_VERBOSE;
int status = hkz_reduction(b, flags, o.float_type, o.precision);
cout << b << endl;
if (status != RED_SUCCESS)
{
cerr << "Failure: " << get_red_status_str(status) << endl;
}
return status;
}
/* Shortest vector problem and closest vector problem */
template <class ZT> int svpcvp(Options &o, ZZ_mat<ZT> &b, const vector<Z_NR<ZT>> &target)
{
if (target.empty())
{
ABORT_MSG("mpz required for SVP");
}
else
{
ABORT_MSG("mpz required for CVP");
}
}
template <> int svpcvp(Options &o, ZZ_mat<mpz_t> &b, const vector<Z_NR<mpz_t>> &target)
{
const char *format = o.output_format ? o.output_format : "s";
IntVect sol_coord; // In the LLL-reduced basis
IntVect sol_coord_2; // In the initial basis
IntVect solution;
IntMatrix u;
bool with_coord = strchr(format, 'c') != NULL;
bool with_coord_std = strchr(format, 's') != NULL;
int flags = SVP_DEFAULT | (o.verbose ? SVP_VERBOSE : 0);
int flagsLLL = LLL_DEFAULT | (o.verbose ? LLL_VERBOSE : 0);
int status;
if (!o.no_lll)
{
if (with_coord)
{
status = lll_reduction(b, u, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, flagsLLL);
}
else
{
status = lll_reduction(b, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, flagsLLL);
}
if (status != RED_SUCCESS)
{
cerr << "LLL reduction failed: " << get_red_status_str(status) << endl;
return status;
}
}
if (target.empty())
status = shortest_vector(b, sol_coord, SVPM_PROVED, flags);
else
status = closest_vector(b, target, sol_coord, flags);
if (status != RED_SUCCESS)
{
cerr << "Failure: " << get_red_status_str(status) << endl;
return status;
}
if (with_coord)
{
if (o.no_lll)
sol_coord_2 = sol_coord;
else
vector_matrix_product(sol_coord_2, sol_coord, u);
}
if (with_coord_std)
{
vector_matrix_product(solution, sol_coord, b);
}
for (int i = 0; format[i]; i++)
{
switch (format[i])
{
case 'c':
cout << sol_coord_2 << endl;
break;
case 's':
cout << solution << endl;
break;
case 't':
cout << status << endl;
break;
case ' ':
cout << endl;
break;
}
}
return status;
}
template <class ZT> int run_action(Options &o)
{
istream *is;
ZZ_mat<ZT> m;
vector<Z_NR<ZT>> target;
if (o.input_file)
is = new ifstream(o.input_file);
else
is = &cin;
*is >> m;
if (o.action == ACTION_CVP)
{
*is >> target;
}
if (!*is)
ABORT_MSG("invalid input");
if (o.input_file)
delete is;
int result = 0;
switch (o.action)
{
case ACTION_LLL:
result = lll(o, m);
break;
case ACTION_SVP:
result = svpcvp(o, m, target);
break;
case ACTION_CVP:
result = svpcvp(o, m, target);
break;
case ACTION_HKZ:
result = hkz(o, m);
break;
case ACTION_BKZ:
result = bkz(o, m);
break;
default:
ABORT_MSG("unimplemented action");
break;
}
return result;
}
/* Command line parsing */
void read_options(int argc, char **argv, Options &o)
{
for (int ac = 1; ac < argc; ac++)
{
if (strcmp(argv[ac], "-a") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -a switch");
if (strcmp(argv[ac], "lll") == 0)
o.action = ACTION_LLL;
else if (strcmp(argv[ac], "hkz") == 0)
o.action = ACTION_HKZ;
else if (strcmp(argv[ac], "bkz") == 0)
o.action = ACTION_BKZ;
else if (strcmp(argv[ac], "svp") == 0)
o.action = ACTION_SVP;
else if (strcmp(argv[ac], "cvp") == 0)
o.action = ACTION_CVP;
else if (strcmp(argv[ac], "sdb") == 0)
{
o.action = ACTION_BKZ;
o.bkz_flags |= BKZ_SD_VARIANT;
}
else if (strcmp(argv[ac], "sld") == 0)
{
o.action = ACTION_BKZ;
o.bkz_flags |= BKZ_SLD_RED;
}
else
ABORT_MSG("parse error in -a switch: lll or svp expected");
}
else if (strcmp(argv[ac], "-b") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -b switch");
o.block_size = atoi(argv[ac]);
}
else if (strcmp(argv[ac], "-bkzboundedlll") == 0)
{
o.bkz_flags |= BKZ_BOUNDED_LLL;
}
else if (strcmp(argv[ac], "-bkzmaxloops") == 0)
{
++ac;
CHECK(ac < argc, "missing value after '-bkzmaxloops'");
o.bkz_max_loops = atoi(argv[ac]);
o.bkz_flags |= BKZ_MAX_LOOPS;
}
else if (strcmp(argv[ac], "-bkzmaxtime") == 0)
{
++ac;
CHECK(ac < argc, "missing value after '-bkzmaxtime'");
o.bkz_max_time = atof(argv[ac]);
o.bkz_flags |= BKZ_MAX_TIME;
}
else if (strcmp(argv[ac], "-bkzautoabort") == 0)
{
o.bkz_flags |= BKZ_AUTO_ABORT;
}
else if (strcmp(argv[ac], "-bkzdumpgso") == 0)
{
++ac;
CHECK(ac < argc, "missing filename after -bkzdumpgso switch");
o.bkz_dump_gso_filename = argv[ac];
o.bkz_flags |= BKZ_DUMP_GSO;
}
else if (strcmp(argv[ac], "-c") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -c switch");
// o.c=atoi(argv[ac]); // ignored (was the number of columns)
}
else if (strcmp(argv[ac], "-bkzghbound") == 0)
{
++ac;
CHECK(ac < argc, "missing value after '-bkzghbound'");
o.bkz_gh_factor = atof(argv[ac]);
o.bkz_flags |= BKZ_GH_BND;
}
else if (strcmp(argv[ac], "-d") == 0 || strcmp(argv[ac], "-delta") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -d switch");
o.delta = atof(argv[ac]);
}
else if (strcmp(argv[ac], "-e") == 0 || strcmp(argv[ac], "-eta") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -e switch");
o.eta = atof(argv[ac]);
}
else if (strcmp(argv[ac], "-f") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -f switch");
if (strcmp("mpfr", argv[ac]) == 0)
o.float_type = FT_MPFR;
else if (strcmp("dpe", argv[ac]) == 0)
o.float_type = FT_DPE;
else if (strcmp("dd", argv[ac]) == 0)
o.float_type = FT_DD;
else if (strcmp("qd", argv[ac]) == 0)
o.float_type = FT_QD;
else if (strcmp("double", argv[ac]) == 0)
o.float_type = FT_DOUBLE;
else if (strcmp("longdouble", argv[ac]) == 0)
o.float_type = FT_LONG_DOUBLE;
else
ABORT_MSG("parse error in -f switch : mpfr, qd, dd, dpe or double expected");
}
else if (strcmp(argv[ac], "-s") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -s switch");
o.bkz_strategy_file = argv[ac];
}
else if (strcmp(argv[ac], "-l") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -l switch");
o.siegel = !atoi(argv[ac]);
}
else if (strcmp(argv[ac], "-m") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -m switch");
if (strcmp("wrapper", argv[ac]) == 0)
o.method = LM_WRAPPER;
else if (strcmp("proved", argv[ac]) == 0)
o.method = LM_PROVED;
else if (strcmp("heuristic", argv[ac]) == 0)
o.method = LM_HEURISTIC;
else if (strcmp("fast", argv[ac]) == 0)
o.method = LM_FAST;
else if (strcmp("fastearly", argv[ac]) == 0)
{
o.method = LM_FAST;
o.early_red = true;
}
else if (strcmp("heuristicearly", argv[ac]) == 0)
{
o.method = LM_HEURISTIC;
o.early_red = true;
}
else
ABORT_MSG("parse error in -m switch : proved, heuristic, fast, "
<< "or wrapper expected");
}
else if (strcmp(argv[ac], "-nolll") == 0)
{
o.no_lll = true;
}
else if (strcmp(argv[ac], "-of") == 0)
{
ac++;
CHECK(ac < argc, "missing value after -of switch");
o.output_format = argv[ac];
}
else if (strcmp(argv[ac], "-p") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -p switch");
o.precision = atoi(argv[ac]);
}
else if (strcmp(argv[ac], "-r") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -r switch");
// o.r = atoi(argv[ac]); // ignored (was the number of rows)
}
else if (strcmp(argv[ac], "-v") == 0)
{
o.verbose = true;
}
else if (strcmp(argv[ac], "-y") == 0)
{
o.early_red = true;
}
else if (strcmp(argv[ac], "-z") == 0)
{
++ac;
CHECK(ac < argc, "missing value after -z switch");
if (strcmp("mpz", argv[ac]) == 0)
o.int_type = ZT_MPZ;
else if (strcmp("long", argv[ac]) == 0 || strcmp("int", argv[ac]) == 0)
o.int_type = ZT_LONG;
else if (strcmp("double", argv[ac]) == 0)
o.int_type = ZT_DOUBLE;
else
ABORT_MSG("parse error in -z switch : int, double or mpz expected");
}
else if ((strcmp(argv[ac], "-h") == 0) || (strcmp(argv[ac], "--help") == 0))
{
cout << "Usage: " << argv[0] << " [options] [file]\n"
<< "List of options:\n"
<< " -a [lll|svp|bkz|sld|sdbkz]\n"
<< " lll = LLL-reduce the input matrix (default)\n"
<< " bkz = BKZ-reduce the input matrix\n"
<< " sdb = reduce the input matrix using the self dual BKZ variant\n"
<< " sld = slide reduce the input matrix\n"
<< " svp = compute a shortest non-zero vector of the lattice\n"
<< " -m [proved|heuristic|fast|wrapper]\n"
<< " LLL version (default: wrapper)\n"
<< " -z [int|mpz|double]\n"
<< " Integer type in LLL (default: mpz)\n"
<< " -f [mpfr|qd|dd|dpe|double]\n"
<< " Floating-point type in LLL (proved/heuristic method only; default: dpe)\n"
<< " -p <precision>\n"
<< " Floating-point precision (only with -f mpfr)\n"
<< " -d <delta> (default=0.99)\n"
<< " -e <eta> (default=0.51)\n"
<< " -l <lovasz>\n"
<< " -s <filename.json> load BKZ strategies from filename"
<< " -y\n"
<< " Enable early reduction\n"
<< " -b <block_size>\n"
<< " Size of BKZ blocks\n"
<< " -v\n"
<< " Enable verbose mode\n";
exit(0);
}
else if (strcmp(argv[ac], "--version") == 0)
{
cout << "fplll " << VERSION << endl
<< "Copyright 2005-2012 Damien Stehle, David Cade, Xavier Pujol." << endl
<< "fplll is free software. You can redistribute it and/or modify" << endl
<< "it under the terms of the GNU Lesser General Public License as published by" << endl
<< "the Free Software Foundation, either version 2.1 of the License, or" << endl
<< "(at your option) any later version." << endl;
exit(0);
}
else if (argv[ac][0] == '-')
{
ABORT_MSG("invalid option '" << argv[ac] << "'.\n"
"Try '"
<< argv[0] << " --help' for more information.");
}
else
{
CHECK(!o.input_file, "too many input files");
o.input_file = argv[ac];
}
}
}
int main(int argc, char **argv)
{
int result;
Options o;
read_options(argc, argv, o);
IntMatrix::set_print_mode(MAT_PRINT_REGULAR);
switch (o.int_type)
{
case ZT_MPZ:
result = run_action<mpz_t>(o);
break;
#ifdef FPLLL_WITH_ZLONG
case ZT_LONG:
result = run_action<long int>(o);
break;
#endif
#ifdef FPLLL_WITH_ZDOUBLE
case ZT_DOUBLE:
result = run_action<double>(o);
break;
#endif
default:
ABORT_MSG("compiled without support for this integer type");
}
return result;
}
|