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
|
/* Copyright (C) 2016 Martin Albrecht
Copyright (C) 2019 Koen de Boer & Wessel van Woerden
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/gso_gram.h>
#include <fplll/gso_interface.h>
#include <fplll/io/json.hpp>
using json = nlohmann::json;
#ifndef TESTDATADIR
#define TESTDATADIR ".."
#endif
using namespace std;
using namespace fplll;
template <class ZT> bool gram_is_equal(ZZ_mat<ZT> b, ZZ_mat<ZT> G)
{
int r = b.r;
int c = b.c;
ZZ_mat<ZT> G_reduced;
G_reduced.resize(r, r);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
(b)[i].dot_product(G_reduced(i, j), (b)[j], c);
}
}
// ------------------------------------------------
// ************************************************
// _______________________________________________
// -----------------------------------------------
// Test whether G_reduced = G
for (int i = 0; i < r; i++)
{
for (int j = 0; j < i; j++)
{
if (G(i, j) != G_reduced(i, j))
{
cerr << "The gram-representation and the basis-representation of the same lattice have an "
"unequal gram matrix.\n";
return 1;
}
}
}
return 0;
}
/**
@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)
{
FloatType sel_ft = (float_type != FT_DEFAULT) ? float_type : FT_DOUBLE;
ZZ_mat<ZT> U;
ZZ_mat<ZT> UT;
/* lllwrapper (no FloatType needed, -m ignored) */
if (flags & BKZ_NO_LLL)
zeros_last(A, U, UT);
else
{
Wrapper wrapper(A, U, UT, LLL_DEF_DELTA, LLL_DEF_ETA, LLL_DEFAULT);
if (!wrapper.lll())
return wrapper.status;
}
// _______________________________________________
// -----------------------------------------------
// Create the Gram matrix G of the basis A
ZZ_mat<ZT> G;
int r = A.r;
int c = A.c;
G.resize(r, r);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
A[i].dot_product(G(i, j), A[j], c);
}
}
// ------------------------------------------------
// ************************************************
// _______________________________________________
// -----------------------------------------------
// Create a MatGSO-object M (basis gso) for A
// and a MatGSOGram-object Mgram (gram gso) for G.
vector<Strategy> strategies;
BKZParam param(block_size, strategies);
param.flags = flags;
if (sel_ft == FT_DOUBLE)
{
MatGSO<Z_NR<ZT>, FP_NR<double>> M(A, U, UT, 0);
M.update_gso();
MatGSOGram<Z_NR<ZT>, FP_NR<double>> Mgram(G, U, UT, 1);
Mgram.update_gso();
LLLReduction<Z_NR<ZT>, FP_NR<double>> LLLObj(M, LLL_DEF_DELTA, LLL_DEF_ETA, 0);
BKZReduction<Z_NR<ZT>, FP_NR<double>> BKZObj(M, LLLObj, param);
BKZObj.bkz();
LLLReduction<Z_NR<ZT>, FP_NR<double>> LLLObjgram(Mgram, LLL_DEF_DELTA, LLL_DEF_ETA, 0);
BKZReduction<Z_NR<ZT>, FP_NR<double>> BKZObjgram(Mgram, LLLObjgram, param);
BKZObjgram.bkz();
return gram_is_equal(A, G);
}
else if (sel_ft == FT_MPFR)
{
int old_prec = FP_NR<mpfr_t>::set_prec(prec);
MatGSO<Z_NR<ZT>, FP_NR<mpfr_t>> M(A, U, UT, 1);
M.update_gso();
MatGSOGram<Z_NR<ZT>, FP_NR<mpfr_t>> Mgram(G, U, UT, 1);
Mgram.update_gso();
LLLReduction<Z_NR<ZT>, FP_NR<mpfr_t>> LLLObj(M, LLL_DEF_DELTA, LLL_DEF_ETA, 0);
BKZReduction<Z_NR<ZT>, FP_NR<mpfr_t>> BKZObj(M, LLLObj, param);
BKZObj.bkz();
LLLReduction<Z_NR<ZT>, FP_NR<mpfr_t>> LLLObjgram(Mgram, LLL_DEF_DELTA, LLL_DEF_ETA, 0);
BKZReduction<Z_NR<ZT>, FP_NR<mpfr_t>> BKZObjgram(Mgram, LLLObjgram, param);
BKZObjgram.bkz();
FP_NR<mpfr_t>::set_prec(old_prec);
return gram_is_equal(A, G);
}
else
{
cerr << "Type not supported for test" << endl;
return 0;
}
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)
{
printf("%s %d\n", input_filename, block_size);
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);
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)
{
cerr << "test_int_rel " << d << " " << b << " " << block_size << endl;
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);
return status;
}
int main(int /*argc*/, char ** /*argv*/)
{
int status = 0;
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);
if (status == 0)
{
cerr << "All tests passed." << endl;
return 0;
}
else
{
return -1;
}
return 0;
}
|