1 /* Copyright (C) 2005-2008 Damien Stehle.
2    Copyright (C) 2007 David Cade.
3    Copyright (C) 2011 Xavier Pujol.
4 
5    This file is part of fplll. fplll is free software: you
6    can redistribute it and/or modify it under the terms of the GNU Lesser
7    General Public License as published by the Free Software Foundation,
8    either version 2.1 of the License, or (at your option) any later version.
9 
10    fplll is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public License
16    along with fplll. If not, see <http://www.gnu.org/licenses/>. */
17 
18 #ifndef FPLLL_WRAPPER_H
19 #define FPLLL_WRAPPER_H
20 
21 #include "nr/matrix.h"
22 
23 FPLLL_BEGIN_NAMESPACE
24 
25 /* The matrix b must not be modified before calling lll().
26    lll() must be called only once. */
27 
28 class Wrapper
29 {
30 public:
31   /* u must be either empty or the identity matrix */
32   Wrapper(ZZ_mat<mpz_t> &b, ZZ_mat<mpz_t> &u, ZZ_mat<mpz_t> &u_inv, double delta, double eta,
33           int flags);
34 
35   // Used for HLLL
36   Wrapper(ZZ_mat<mpz_t> &b, ZZ_mat<mpz_t> &u, ZZ_mat<mpz_t> &u_inv, double delta, double eta,
37           double theta, double c, int flags);
38 
39   bool lll();
40 
41   // Call HLLL on the wrapper object
42   // TODO: this wrapper does not offers as many options as the one of LLL (for example, use long
43   // instead of mpz_t, modify delta, ...)
44   bool hlll();
45 
46   int status;
47 
48 private:
49   ZZ_mat<mpz_t> &b;
50   ZZ_mat<mpz_t> &u;
51   ZZ_mat<mpz_t> &u_inv;
52 
53 #ifdef FPLLL_WITH_ZLONG
54   ZZ_mat<long> b_long;
55   ZZ_mat<long> u_long;      // Always empty
56   ZZ_mat<long> u_inv_long;  // Always empty
57 #endif
58 
59   double delta;
60   double eta;
61   int good_prec;
62   bool use_long;
63   int flags;
64 
65   bool little(int kappa, int precision);
66 
67   template <class Z, class F>
68   int call_lll(ZZ_mat<Z> &bz, ZZ_mat<Z> &uz, ZZ_mat<Z> &u_inv_z, LLLMethod method, int precision,
69                double delta, double eta);
70 
71   template <class F> int fast_lll(double delta, double eta);
72 
73   template <class Z, class F>
74   int heuristic_lll(ZZ_mat<Z> &bz, ZZ_mat<Z> &uz, ZZ_mat<Z> &u_inv_z, int precision, double delta,
75                     double eta);
76 
77   template <class Z, class F>
78   int proved_lll(ZZ_mat<Z> &bz, ZZ_mat<Z> &uz, ZZ_mat<Z> &u_inv_z, int precision, double delta,
79                  double eta);
80 
81   int heuristic_loop(int precision);
82   int proved_loop(int precision);
83   int last_lll();
84 
85   void set_use_long(bool value);
86   int increase_prec(int precision);
87 
88   int max_exponent;
89   int n;
90   int d;
91   int last_early_red;
92 
93   // For HLLL
94   double theta;
95   double c;
96 
97   // High level function, select depending on method and precision the best way to call
98   // HLLL (types, flags enabled, ...)
99   template <class F> bool call_hlll(LLLMethod method, int precision);
100 
101   // = call_hlll(LM_FAST, 0);
102   template <class F> bool fast_hlll();
103 
104   // = call_hlll(LM_PROVED, precision);
105   template <class F> bool proved_hlll(int precision);
106 
107   // Perform proved_hlll until grood_prec is reached or the lattice is reduced
108   int hlll_proved_loop(int precision);
109 
110   // Perform proved version of HLLL with good_prec
111   bool last_hlll();
112 };
113 
114 #define FPLLL_DECLARE_LLL(T)                                                                       \
115   int lll_reduction(ZZ_mat<T> &b, double delta = LLL_DEF_DELTA, double eta = LLL_DEF_ETA,          \
116                     LLLMethod method = LM_WRAPPER, FloatType floatType = FT_DEFAULT,               \
117                     int precision = 0, int flags = LLL_DEFAULT);                                   \
118                                                                                                    \
119   int lll_reduction(ZZ_mat<T> &b, ZZ_mat<T> &u, double delta = LLL_DEF_DELTA,                      \
120                     double eta = LLL_DEF_ETA, LLLMethod method = LM_WRAPPER,                       \
121                     FloatType floatType = FT_DEFAULT, int precision = 0, int flags = LLL_DEFAULT); \
122                                                                                                    \
123   int lll_reduction(ZZ_mat<T> &b, ZZ_mat<T> &u, ZZ_mat<T> &u_inv, double delta = LLL_DEF_DELTA,    \
124                     double eta = LLL_DEF_ETA, LLLMethod method = LM_WRAPPER,                       \
125                     FloatType floatType = FT_DEFAULT, int precision = 0, int flags = LLL_DEFAULT);
126 
127 FPLLL_DECLARE_LLL(mpz_t)
128 
129 #ifdef FPLLL_WITH_ZLONG
130 FPLLL_DECLARE_LLL(long)
131 #endif
132 
133 #ifdef FPLLL_WITH_ZDOUBLE
134 FPLLL_DECLARE_LLL(double)
135 #endif
136 
137 // HLLL
138 
139 /**
140  * We define HLLL for each input type instead of using a template,
141  * in order to force the compiler to instantiate the functions.
142  */
143 #define FPLLL_DECLARE_HLLL(T)                                                                      \
144   int hlll_reduction(ZZ_mat<T> &b, double delta = LLL_DEF_DELTA, double eta = LLL_DEF_ETA,         \
145                      double theta = HLLL_DEF_THETA, double c = HLLL_DEF_C,                         \
146                      LLLMethod method = LM_WRAPPER, FloatType float_type = FT_DEFAULT,             \
147                      int precision = 0, int flags = LLL_DEFAULT, bool nolll = false);              \
148   int hlll_reduction(ZZ_mat<T> &b, ZZ_mat<T> &u, double delta = LLL_DEF_DELTA,                     \
149                      double eta = LLL_DEF_ETA, double theta = HLLL_DEF_THETA,                      \
150                      double c = HLLL_DEF_C, LLLMethod method = LM_WRAPPER,                         \
151                      FloatType float_type = FT_DEFAULT, int precision = 0,                         \
152                      int flags = LLL_DEFAULT, bool nolll = false);                                 \
153   int hlll_reduction(ZZ_mat<T> &b, ZZ_mat<T> &u, ZZ_mat<T> &u_inv, double delta = LLL_DEF_DELTA,   \
154                      double eta = LLL_DEF_ETA, double theta = HLLL_DEF_THETA,                      \
155                      double c = HLLL_DEF_C, LLLMethod method = LM_WRAPPER,                         \
156                      FloatType float_type = FT_DEFAULT, int precision = 0,                         \
157                      int flags = LLL_DEFAULT, bool nolll = false);
158 
159 FPLLL_DECLARE_HLLL(mpz_t)
160 
161 #ifdef FPLLL_WITH_ZLONG
162 FPLLL_DECLARE_HLLL(long)
163 #endif
164 
165 #ifdef FPLLL_WITH_ZDOUBLE
166 FPLLL_DECLARE_HLLL(double)
167 #endif
168 
169 FPLLL_END_NAMESPACE
170 
171 #endif
172