1 /* Copyright (C) 2019 Martin Albrecht
2 
3    This file is part of fplll. fplll is free software: you
4    can redistribute it and/or modify it under the terms of the GNU Lesser
5    General Public License as published by the Free Software Foundation,
6    either version 2.1 of the License, or (at your option) any later version.
7 
8    fplll is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11    GNU Lesser General Public License for more details.
12 
13    You should have received a copy of the GNU Lesser General Public License
14    along with fplll. If not, see <http://www.gnu.org/licenses/>. */
15 
16 #include <cstring>
17 #include <fplll/fplll.h>
18 
19 using namespace fplll;
20 
test_enum(size_t d)21 template <class FT> int test_enum(size_t d)
22 {
23   RandGen::init_with_seed(0x1337);
24   ZZ_mat<mpz_t> A = ZZ_mat<mpz_t>(100, 100);
25   A.gen_qary_withq(50, 7681);
26   lll_reduction(A);
27   ZZ_mat<mpz_t> U;
28   MatGSO<Z_NR<mpz_t>, FP_NR<FT>> M(A, U, U, 0);
29   M.update_gso();
30 
31   FastEvaluator<FP_NR<FT>> evaluator;
32   Enumeration<Z_NR<mpz_t>, FP_NR<FT>> enum_obj(M, evaluator);
33   FP_NR<FT> max_dist;
34   M.get_r(max_dist, 0, 0);
35   max_dist *= 0.99;
36   enum_obj.enumerate(0, d, max_dist, 0);
37   if (evaluator.empty())
38   {
39     return 1;
40   }
41   else
42   {
43     return 0;
44   }
45 }
46 
callback_firstf(size_t n,enumf * new_sol_coord,void * ctx)47 bool callback_firstf(size_t n, enumf *new_sol_coord, void *ctx)
48 {
49   if (new_sol_coord[0] == static_cast<double *>(ctx)[0])
50   {
51     return true;
52   }
53   return false;
54 }
55 
test_callback_enum(size_t d)56 template <class FT> int test_callback_enum(size_t d)
57 {
58   RandGen::init_with_seed(0x1337);
59   ZZ_mat<mpz_t> A = ZZ_mat<mpz_t>(100, 100);
60   A.gen_qary_withq(50, 7681);
61   lll_reduction(A);
62   ZZ_mat<mpz_t> U;
63   MatGSO<Z_NR<mpz_t>, FP_NR<FT>> M(A, U, U, 0);
64   M.update_gso();
65 
66   enumf ctx = 2;
67   CallbackEvaluator<FP_NR<FT>> evaluator(callback_firstf, &ctx);
68   Enumeration<Z_NR<mpz_t>, FP_NR<FT>> enum_obj(M, evaluator);
69   FP_NR<FT> max_dist;
70   M.get_r(max_dist, 0, 0);
71   max_dist *= 0.99;
72   enum_obj.enumerate(0, d, max_dist, 0);
73   if (evaluator.empty())
74   {
75     return 1;
76   }
77   else
78   {
79     if (evaluator.begin()->second[0].get_si() == 2)
80     {
81       return 0;
82     }
83     else
84     {
85       return 1;
86     }
87   }
88 }
89 
main(int argc,char * argv[])90 int main(int argc, char *argv[])
91 {
92   int status = 0;
93   status |= test_enum<double>(30);
94   status |= test_callback_enum<double>(40);
95 
96   if (status == 0)
97   {
98     std::cerr << "All tests passed." << std::endl;
99     return 0;
100   }
101   else
102   {
103     return -1;
104   }
105 }
106