Skip to content

Commit

Permalink
Merge pull request #251 from fplll/sieve_api_248
Browse files Browse the repository at this point in the history
Sieve API #248
  • Loading branch information
malb committed Jun 6, 2017
2 parents 478833d + c0d06a8 commit 657b0d7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
37 changes: 24 additions & 13 deletions fplll/sieve/sieve_gauss.cpp
Expand Up @@ -27,17 +27,17 @@ GaussSieve<ZT, F>::GaussSieve(ZZ_mat<ZT> &B, int alg_arg, bool ver, int seed)
{

/* stats */
b = B;
nr = b.get_rows();
nc = b.get_cols();
max_list_size = 0;
iterations = 0;
collisions = 0;
reductions = 0;
samples = 0;
goal_sqr_norm = 0;
mem_lower = pow(2.0, 0.18 * nc);
alg = alg_arg;
b = B;
nr = b.get_rows();
nc = b.get_cols();
max_list_size = 0;
iterations = 0;
collisions = 0;
reductions = 0;
samples = 0;
target_sqr_norm = 0;
mem_lower = pow(2.0, 0.18 * nc);
alg = alg_arg;
set_verbose(ver);

/* sanity check */
Expand Down Expand Up @@ -238,9 +238,9 @@ template <class ZT, class F> void GaussSieve<ZT, F>::free_sampler() { delete Sam
/**
* set targeted norm^2
*/
template <class ZT, class F> void GaussSieve<ZT, F>::set_goal_norm2(Z_NR<ZT> norm)
template <class ZT, class F> void GaussSieve<ZT, F>::set_target_norm2(Z_NR<ZT> norm)
{
goal_sqr_norm = norm;
target_sqr_norm = norm;
}

/**
Expand Down Expand Up @@ -315,6 +315,17 @@ template <class ZT, class F> NumVect<Z_NR<ZT>> GaussSieve<ZT, F>::return_first()
return List.front()->v;
}

template <class ZT, class F> bool GaussSieve<ZT, F>::sieve(Z_NR<ZT> target_norm)
{
set_target_norm2(target_norm);
if (alg == 3)
return run_3sieve();
else if (alg == 4)
return run_4sieve();
else
return run_2sieve();
}

template class GaussSieve<mpz_t, FP_NR<double>>;
template class GaussSieve<long, FP_NR<double>>;

Expand Down
13 changes: 7 additions & 6 deletions fplll/sieve/sieve_gauss.h
Expand Up @@ -19,13 +19,10 @@ template <class ZT, class F> class GaussSieve
GaussSieve(ZZ_mat<ZT> &B, int alg, bool ver, int seed);
~GaussSieve();

bool run_2sieve();
bool run_3sieve();
bool run_4sieve();

void set_verbose(bool verbose);
void set_goal_norm2(Z_NR<ZT> norm);
void set_target_norm2(Z_NR<ZT> norm);
bool verbose;
bool sieve(Z_NR<ZT> target_norm);

/*
void Init(const mat_ZZ& B, KleinSampler* sampler);
Expand All @@ -47,7 +44,7 @@ template <class ZT, class F> class GaussSieve
ZZ_mat<ZT> b;

Z_NR<ZT> best_sqr_norm;
Z_NR<ZT> goal_sqr_norm;
Z_NR<ZT> target_sqr_norm;

/* statistics */
long max_list_size;
Expand Down Expand Up @@ -93,6 +90,10 @@ template <class ZT, class F> class GaussSieve
/* info functions */
void print_curr_info();
void print_final_info();

bool run_2sieve();
bool run_3sieve();
bool run_4sieve();
};

FPLLL_END_NAMESPACE
Expand Down
4 changes: 2 additions & 2 deletions fplll/sieve/sieve_gauss_2sieve.cpp
Expand Up @@ -109,7 +109,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_2sieve()
/*
Loop till you find a short enough vector,
or enough collisions. */
while ((best_sqr_norm > goal_sqr_norm) && (collisions < mult * max_list_size + add))
while ((best_sqr_norm > target_sqr_norm) && (collisions < mult * max_list_size + add))
{
/* update stats */
iterations++;
Expand Down Expand Up @@ -184,7 +184,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_2sieve()
cout << "# Error: check 2-reduced not OK" << endl;
#endif

if (best_sqr_norm > goal_sqr_norm)
if (best_sqr_norm > target_sqr_norm)
return false;
return true;
}
4 changes: 2 additions & 2 deletions fplll/sieve/sieve_gauss_3sieve.cpp
Expand Up @@ -209,7 +209,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_3sieve()
Z_NR<ZT> current_norm;

/* main iteration */
while ((best_sqr_norm > goal_sqr_norm) && (collisions < mult * max_list_size + add))
while ((best_sqr_norm > target_sqr_norm) && (collisions < mult * max_list_size + add))
{
iterations++;
max_list_size = max(max_list_size, long(List.size()));
Expand Down Expand Up @@ -269,7 +269,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_3sieve()
#endif
// print_list(List);

if (best_sqr_norm > goal_sqr_norm)
if (best_sqr_norm > target_sqr_norm)
return false;
return true;
}
4 changes: 2 additions & 2 deletions fplll/sieve/sieve_gauss_4sieve.cpp
Expand Up @@ -342,7 +342,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_4sieve()
Z_NR<ZT> current_norm;

/* main iteration */
while ((best_sqr_norm > goal_sqr_norm) && (collisions < mult * max_list_size + 200))
while ((best_sqr_norm > target_sqr_norm) && (collisions < mult * max_list_size + 200))
{
iterations++;
max_list_size = max(max_list_size, long(List.size()));
Expand Down Expand Up @@ -400,7 +400,7 @@ template <class ZT, class F> bool GaussSieve<ZT, F>::run_4sieve()
#endif
// print_list(List);

if (best_sqr_norm > goal_sqr_norm)
if (best_sqr_norm > target_sqr_norm)
return false;
return true;
}
40 changes: 17 additions & 23 deletions fplll/sieve/sieve_main.cpp
Expand Up @@ -33,17 +33,11 @@ static void main_usage(char *myself)
/**
* run sieve
*/
template <class ZT> int main_run_sieve(ZZ_mat<ZT> B, Z_NR<ZT> goal_norm, int alg, int ver, int seed)
template <class ZT>
int main_run_sieve(ZZ_mat<ZT> B, Z_NR<ZT> target_norm, int alg, int ver, int seed)
{
GaussSieve<ZT, FP_NR<double>> gsieve(B, alg, ver, seed);
gsieve.set_goal_norm2(goal_norm);
if (gsieve.alg == 3)
gsieve.run_3sieve();
else if (gsieve.alg == 4)
gsieve.run_4sieve();
else
gsieve.run_2sieve();

gsieve.sieve(target_norm);
return 0;
}

Expand All @@ -53,7 +47,7 @@ template <class ZT> int main_run_sieve(ZZ_mat<ZT> B, Z_NR<ZT> goal_norm, int alg
int main(int argc, char **argv)
{
char *input_file_name = NULL;
char *goal_norm_s = NULL;
char *target_norm_s = NULL;
bool flag_verbose = false, flag_file = false;
int option, alg, dim = 10, seed = 0, bs = 0;

Expand Down Expand Up @@ -100,9 +94,9 @@ int main(int argc, char **argv)
flag_verbose = true;
break;
case 't':
// ngoal_norm = atol(optarg);
// ntarget_norm = atol(optarg);
cout << optarg << endl;
goal_norm_s = optarg;
target_norm_s = optarg;
break;
case 'h':
main_usage(argv[0]);
Expand Down Expand Up @@ -148,15 +142,15 @@ int main(int argc, char **argv)
}

/* set targeted norm */
Z_NR<mpz_t> goal_norm, max;
if (goal_norm_s != NULL)
Z_NR<mpz_t> target_norm, max;
if (target_norm_s != NULL)
{
goal_norm.set_str(goal_norm_s);
target_norm.set_str(target_norm_s);
}
if (goal_norm < 0)
goal_norm = 0;
if (target_norm < 0)
target_norm = 0;
if (flag_verbose)
cout << "# [info] goal norm^2 is " << goal_norm << endl;
cout << "# [info] target norm^2 is " << target_norm << endl;

/* preprocessing of basis */
clock_t stime = clock();
Expand All @@ -183,18 +177,18 @@ int main(int argc, char **argv)
#if 1
if (max < std::numeric_limits<int>::max())
{
long goal_norm_l = abs(goal_norm.get_si());
Z_NR<long> goal_norm_lt;
goal_norm_lt = goal_norm_l;
long target_norm_l = abs(target_norm.get_si());
Z_NR<long> target_norm_lt;
target_norm_lt = target_norm_l;
ZZ_mat<long> B2(B.get_rows(), B.get_cols());
for (int i = 0; i < B.get_rows(); i++)
for (int j = 0; j < B.get_cols(); j++)
B2(i, j) = B(i, j).get_si();
main_run_sieve<long>(B2, goal_norm_lt, alg, flag_verbose, seed);
main_run_sieve<long>(B2, target_norm_lt, alg, flag_verbose, seed);
}
else
#endif
main_run_sieve<mpz_t>(B, goal_norm, alg, flag_verbose, seed);
main_run_sieve<mpz_t>(B, target_norm, alg, flag_verbose, seed);

etime = clock();
secs = (etime - stime) / (double)CLOCKS_PER_SEC;
Expand Down
8 changes: 1 addition & 7 deletions tests/test_sieve.cpp
Expand Up @@ -49,13 +49,7 @@ template <class ZT> int test_sieve_alg(ZZ_mat<ZT> &A, IntVect &b, int alg)
GaussSieve<ZT, FP_NR<double>> gsieve(A, alg, 0, 0);
Z_NR<ZT> goal_norm;
goal_norm = 0;
gsieve.set_goal_norm2(goal_norm);
if (gsieve.alg == 3)
gsieve.run_3sieve();
else if (gsieve.alg == 4)
gsieve.run_4sieve();
else
gsieve.run_2sieve();
gsieve.sieve(goal_norm);
NumVect<Z_NR<ZT>> v = gsieve.return_first();
Z_NR<ZT> tmp;
Z_NR<ZT> norm_s;
Expand Down

0 comments on commit 657b0d7

Please sign in to comment.