Example #1
0
/**
   @brief Test sieve by checking if function returns correct vector.

   @param A              input lattice
   @param b              shortest vector
   @return
*/
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();
  NumVect<Z_NR<ZT>> v = gsieve.return_first();
  Z_NR<ZT> tmp;
  Z_NR<ZT> norm_s;
  Z_NR<ZT> norm_b;
  for (int i = 0; i < A.get_cols(); i++)
  {
    tmp.mul(v[i], v[i]);
    norm_s.add(norm_s, tmp);
    tmp.mul(b[i], b[i]);
    norm_b.add(norm_b, tmp);
  }
  if (norm_s != norm_b)
    return 1;
  return 0;
}
Example #2
0
/**
 * init pool of samples (used later)
 */
template <class ZT, class F> void GaussSieve<ZT, F>::init_list_rand()
{
  /* after transformation, the size could be large */
  ZZ_mat<mpz_t> NewZ(nr, nc);
  ZZ_mat<ZT> New(nr, nc);
  mpz_t tmp;
  Z_NR<mpz_t> tmpZ;
  mpz_init(tmp);
  FP_NR<double> c, t;
  Z_NR<ZT> x;
  c = 0.0;
  t = 32.0;

  /* init */
  for (int i = 0; i < nr; i++)
  {
    for (int j = 0; j < nc; j++)
    {
      (b[i][j]).get_mpz(tmp);
      NewZ[i][j] = tmp;
    }
  }

  /* randomization */
  for (int i = 0; i < nr; i++)
  {
    for (int k = 0; k < nr; k++)
    {
      if (i != k)
      {
        x = sample_z_basic_alt<ZT, FP_NR<double>>(c, t);
        x.get_mpz(tmp);
        tmpZ = tmp;
        (NewZ[i]).addmul(NewZ[k], tmpZ, (NewZ[k]).size());
      }
    }
  }

  /* reduce */
  lll_reduction(NewZ, LLL_DEF_DELTA, LLL_DEF_ETA, LM_FAST);

  /* set */
  for (int i = 0; i < nr; i++)
  {
    for (int j = 0; j < nc; j++)
    {
      tmpZ = (NewZ[i][j]).get_data();
      tmpZ.get_mpz(tmp);
      New[i][j] = tmp;
    }
  }

  /* add to list */
  add_mat_list(New);
  mpz_clear(tmp);
}
Example #3
0
template<> Obj GET_INTOBJ(Z_NR<mpz_t> &v) {
#ifdef FPLLL_VERSION
  mpz_t z;
  mpz_init2 (z, 8*sizeof(long)+1);
  v.get_mpz(z);
  Obj o = INT_mpz(z);
  mpz_clear(z);
  return o;
#else
  return INT_mpz(v.getData());
#endif
}
Example #4
0
template<> Obj GET_INTOBJ(Z_NR<double> &v) {
  mpz_t z;
  mpz_init2 (z, 8*sizeof(double)+1);
  mpz_set_d(z,v.getData());
  Obj o = INT_mpz(z);
  mpz_clear(z);
  return o;
}
Example #5
0
template<> Obj GET_INTOBJ(Z_NR<long> &v) {
  mpz_t z;
  mpz_init2 (z, 8*sizeof(long)+1);
  mpz_set_si(z,v.getData());
  Obj o = INT_mpz(z);
  mpz_clear(z);
  return o;
}
Example #6
0
template <class ZT> int test_svp(ZZ_mat<ZT> &A, IntVect &b)
{
  IntVect sol_coord;   // In the LLL-reduced basis
  IntVect sol_coord2;  // In the initial basis
  IntVect solution;
  IntMatrix u;

  int status =
      lll_reduction(A, u, LLL_DEF_DELTA, LLL_DEF_ETA, LM_WRAPPER, FT_DEFAULT, 0, LLL_DEFAULT);
  if (status != RED_SUCCESS)
  {
    cerr << "LLL reduction failed: " << get_red_status_str(status) << endl;
    return status;
  }

  status = shortest_vector(A, sol_coord, SVPM_PROVED, SVP_DEFAULT);

  if (status != RED_SUCCESS)
  {
    cerr << "Failure: " << get_red_status_str(status) << endl;
    return status;
  }

  vector_matrix_product(sol_coord2, sol_coord, u);
  vector_matrix_product(solution, sol_coord, A);

  Z_NR<ZT> tmp;
  Z_NR<ZT> norm_s;
  Z_NR<ZT> norm_b;

  for (int i = 0; i < A.get_cols(); i++)
  {
    tmp.mul(solution[i], solution[i]);
    norm_s.add(norm_s, tmp);

    tmp.mul(b[i], b[i]);
    norm_b.add(norm_b, tmp);
  }
  if (norm_s != norm_b)
    return 1;

  return 0;
}
Example #7
0
template<> void SET_INTOBJ(Z_NR<mpz_t> &v, Obj z) {
  if (IS_INTOBJ(z))
    v = INT_INTOBJ(z);
  else
#ifdef FPLLL_VERSION
  {
    mpz_t zz;
    mpz_init(zz);
    mpz_set(zz, mpz_MPZ(MPZ_LONGINT(z)));
    v = zz;
    mpz_clear(zz);
  }
#else
    mpz_set(v.getData(), mpz_MPZ(MPZ_LONGINT(z)));
#endif    
}
Example #8
0
void SET_Z(Integer &s, const Z_NR<long> &t)
{
  s = t.getData();
}
Example #9
0
void SET_Z(Integer &s, const Z_NR<double> &t)
{
  s = t.getData();
}
Example #10
0
template<> Obj GET_INTOBJ(Z_NR<mpz_t> &v) {
  return INT_mpz(v.getData());
}
Example #11
0
template<> void SET_INTOBJ(Z_NR<mpz_t> &v, Obj z) {
  if (IS_INTOBJ(z))
    v = INT_INTOBJ(z);
  else
    mpz_set(v.getData(), mpz_MPZ(MPZ_LONGINT(z)));
}