Ejemplo n.º 1
0
vec_ZZ  GS_reduce_iLLL(mat_ZZ M, vec_ZZ v, ZZ det, int k)
{
    mat_ZZ          M_op;
    ZZ_mat<mpz_t>   M_fp;
    vec_ZZ          result;
    M_op.SetDims(k,k);
    result  =   v;
    if  (v[k-1]!=1||v[k]!=0)
    {
        cout<<"error: v["<<k-1<<"] = "<<v[k-1]<<"   v["<<k<<"] = "<<v[k]<<endl;
    }
    for (int i=0;i<k-1;i++)
    {
        for (int j=0;j<k-1;j++)
        {
            M_op[i][j]  =   M[i][j];
        }
        M_op[k-1][i]    =   v[i];
    }
    M_op[k-1][k-1]  =   det;
    M_fp    =   convert<ZZ_mat<mpz_t> ,mat_ZZ >(M_op);
    lllReduction(M_fp, 0.99,0.51, LM_WRAPPER,FT_DEFAULT, 0, LLL_EARLY_RED);
    M_op    =   convert<mat_ZZ,ZZ_mat<mpz_t> >(M_fp);
    for (int i=0;i<k-1;i++)
    {
       result[i]    =   M_op[k-1][i];
    }
    result[k-1] =   1;
    return result;
}
Ejemplo n.º 2
0
vec_ZZ  GS_reduce(mat_ZZ mat, vec_ZZ vec, ZZ weight)
{
    vec_ZZ          target;
    ZZ_mat<mpz_t>   mat_fp;
    mat_ZZ          mat_exp;
    int             row, col;


    row     =   mat.NumRows();
    col     =   mat.NumCols();
    mat_exp.SetDims(row+1, col+1);

    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            mat_exp[i][j] =   mat[i][j];
        }
    }
    for(int i=0;i<col;i++)
    {
        mat_exp[row][i] =   vec[i];
    }
    mat_exp[row][col]   =   weight;

    mat_fp  =   convert<ZZ_mat<mpz_t> ,mat_ZZ >(mat_exp);
    lllReduction(mat_fp, 0.99,0.51, LM_WRAPPER,FT_DEFAULT, 0, LLL_DEFAULT);
    mat_exp =   convert<mat_ZZ ,ZZ_mat<mpz_t> >(mat_fp);
    target.SetLength(col);
    for (int i=0;i<col;i++)
        target[i]   =   mat_exp[row][i];
//    cout<<target<<endl;
    return target;
}
Ejemplo n.º 3
0
mat_ZZ  iLLL(ZZ det, ZZ alpha, int dim, int k)
{
    mat_ZZ          M;
    ZZ_mat<mpz_t>   M_fp;
    vec_ZZ          v;

    M.SetDims(2,dim);
    M[0][0] =   det;
    M[1][0] =   alpha;
    M[1][1] =   1;
    v       =   vec_shift(M[1],dim);
    M_fp    =   convert<ZZ_mat<mpz_t> ,mat_ZZ >(M);
    lllReduction(M_fp, 0.99,0.51, LM_WRAPPER,FT_DEFAULT, 0, LLL_EARLY_RED);
    M       =   convert<mat_ZZ,ZZ_mat<mpz_t> >(M_fp);
    v       =   GS_reduce_iLLL(M, v, det, 3);

    for (int i=3;i<k;i++)
    {
        M.SetDims(i,dim);
        M[i-1]  =   v;
//        cout<<"before:"<<endl<<M<<endl;
//        cout<<"v:"<<endl<<v<<endl;
        M_fp    =   convert<ZZ_mat<mpz_t> ,mat_ZZ >(M);
        lllReduction(M_fp, 0.99,0.51, LM_WRAPPER,FT_DEFAULT, 0, LLL_EARLY_RED);
        M       =   convert<mat_ZZ,ZZ_mat<mpz_t> >(M_fp);
        v       =   vec_shift(v, dim);
        v       =   GS_reduce_iLLL(M, v, det, i+1);
//        cout<<"after"<<endl<<M<<endl<<"v after"<<v<<endl;
    }
    M.SetDims(k,dim);
    M[k-1]  =   v;
    M_fp    =   convert<ZZ_mat<mpz_t> ,mat_ZZ >(M);
    lllReduction(M_fp, 0.99,0.51, LM_WRAPPER,FT_DEFAULT, 0, LLL_EARLY_RED);
    M       =   convert<mat_ZZ,ZZ_mat<mpz_t> >(M_fp);

    return M;
}
Ejemplo n.º 4
0
int fplll_lll(fmpz_mat_t A)
{
  IntMatrix b;
  int status;
  double delta = LLL_DEF_DELTA, eta = LLL_DEF_ETA;

  b = IntMatrix(A->r, A->c);
  flint_to_fplll(b, A);

  status = lllReduction(b, delta, eta, LM_PROVED, FT_DEFAULT, 0, 0);

  fplll_to_flint(A, b);
  b.clear();

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

  return status;
}
Ejemplo n.º 5
0
template<class Z> Obj dofplll(Obj gapmat, Obj lllargs, Obj svpargs)
{
  if (!IS_PLIST(gapmat)) return INTOBJ_INT(-1);
  Int numrows = LEN_PLIST(gapmat), numcols = -1;
  
  for (int i = 1; i <= numrows; i++) {
    Obj row = ELM_PLIST(gapmat,i);
    if (numcols == -1)
      numcols = LEN_PLIST(row);
    if (numcols != LEN_PLIST(row))
      return INTOBJ_INT(-1);
  }
  if (numcols <= 0)
    return INTOBJ_INT(-1);

  ZZ_mat<Z> mat(numrows, numcols);
  for (int i = 1; i <= numrows; i++)
    for (int j = 1; j <= numcols; j++)
      SET_INTOBJ(mat[i-1][j-1], ELM_PLIST(ELM_PLIST(gapmat,i),j));

  if (lllargs != Fail) {
    double delta = 0.99;
    double eta = 0.51;
    LLLMethod method = LM_WRAPPER;
    FloatType floatType = FT_DEFAULT;
    int precision = 0;
    int flags = LLL_DEFAULT;

    if (lllargs != True) {
      if (!IS_PLIST(lllargs) || LEN_PLIST(lllargs) != 6) return INTOBJ_INT(-20);

      Obj v = ELM_PLIST(lllargs,1);
      if (IS_MACFLOAT(v)) delta = VAL_MACFLOAT(v);
      else if (v != Fail) return INTOBJ_INT(-21);

      v = ELM_PLIST(lllargs,2);
      if (IS_MACFLOAT(v)) eta = VAL_MACFLOAT(v);
      else if (v != Fail) return INTOBJ_INT(-22);

      v = ELM_PLIST(lllargs,3);
      if (v == INTOBJ_INT(0)) method = LM_WRAPPER;
      else if (v == INTOBJ_INT(1)) method = LM_PROVED;
      else if (v == INTOBJ_INT(2)) method = LM_HEURISTIC;
      else if (v == INTOBJ_INT(3)) method = LM_FAST;
      else if (v != Fail) return INTOBJ_INT(-23);

      v = ELM_PLIST(lllargs,4);
      if (v == INTOBJ_INT(0)) floatType = FT_DEFAULT;
      else if (v == INTOBJ_INT(1)) floatType = FT_DOUBLE;
      else if (v == INTOBJ_INT(2)) floatType = FT_DPE;
      else if (v == INTOBJ_INT(3)) floatType = FT_MPFR;
      else if (v != Fail) return INTOBJ_INT(-24);

      v = ELM_PLIST(lllargs,5);
      if (IS_INTOBJ(v)) precision = INT_INTOBJ(v);
      else if (v != Fail) return INTOBJ_INT(-25);

      v = ELM_PLIST(lllargs,6);
      if (IS_INTOBJ(v)) flags = INT_INTOBJ(v);
      else if (v != Fail) return INTOBJ_INT(-26);
    }
    int result = lllReduction(mat, delta, eta, method, floatType, precision, flags);

    if (result != RED_SUCCESS)
      return INTOBJ_INT(10*result+1);
  }

  if (svpargs != Fail) {
    SVPMethod method = SVPM_PROVED;
    int flags = SVP_DEFAULT;

    // __asm__ ("int3");
    if (svpargs != True) {
      if (!IS_PLIST(svpargs) || LEN_PLIST(svpargs) != 2) return INTOBJ_INT(-30);

      Obj v = ELM_PLIST(svpargs,1);
      if (v == INTOBJ_INT(0)) method = SVPM_PROVED;
      else if (v == INTOBJ_INT(1)) method = SVPM_FAST;
      else if (v != Fail) return INTOBJ_INT(-31);

      v = ELM_PLIST(svpargs,2);
      if (IS_INTOBJ(v)) flags = INT_INTOBJ(v);
      else if (v != Fail) return INTOBJ_INT(-32);
    }

    vector<Integer> sol(numrows);
    IntMatrix svpmat(numrows,numcols);

    for (int i = 0; i < numrows; i++)
      for (int j = 0; j < numcols; j++)
	SET_Z(svpmat[i][j],mat[i][j]);

    int result = shortestVector(svpmat, sol, method, flags);

    if (result != RED_SUCCESS)
      return INTOBJ_INT(10*result+2);

    Obj gapvec;
    if (lllargs == Fail) { // return coordinates of shortest vector in mat
      gapvec = NEW_PLIST(T_PLIST,numrows);
      SET_LEN_PLIST(gapvec,numrows);
      for (int i = 1; i <= numrows; i++) {
	Obj v = GET_INTOBJ(sol[i-1]);
	SET_ELM_PLIST(gapvec,i,v);
      }
    } else { // return shortest vector
      gapvec = NEW_PLIST(T_PLIST,numcols);
      SET_LEN_PLIST(gapvec,numcols);
      for (int i = 1; i <= numcols; i++) {
	Integer s;
	s = 0;
	for (int j = 0; j < numrows; j++)
	  s.addmul(sol[j],svpmat[j][i-1]);
	Obj v = GET_INTOBJ(s);
	SET_ELM_PLIST(gapvec,i,v);
      }
    }
    return gapvec;
  }

  gapmat = NEW_PLIST(T_PLIST,numrows);
  SET_LEN_PLIST(gapmat,numrows);
  for (int i = 1; i <= numrows; i++) {
    Obj gaprow = NEW_PLIST(T_PLIST,numcols);
    SET_LEN_PLIST(gaprow,numcols);
    SET_ELM_PLIST(gapmat,i,gaprow);
    for (int j = 1; j <= numcols; j++) {
      Obj v = GET_INTOBJ(mat[i-1][j-1]);
      SET_ELM_PLIST(gaprow,j,v);
    }
  }
  return gapmat;
}
Ejemplo n.º 6
0
 bool fp_LLL(MutableMatrix *M, MutableMatrix *U, int strategy)
 {
 
 #ifndef HAVE_FPLLL
     ERROR("fplll is not available (configure M2 with fplll!)");
     return 0;
 #else
     assert(U==NULL);
     double delta = 0.99;
     double eta = 0.51;
     LLLMethod method = LM_WRAPPER;
     FloatType floatType = FT_DEFAULT;
     int precision = 0;
     int flags = LLL_DEFAULT;
     
     int ncols = static_cast<int>(M->n_rows());
     int nrows = static_cast<int>(M->n_cols());
     
     ZZ_mat<mpz_t> mat (nrows,ncols);
     
     for (int i=0; i<nrows; i++)
         for (int j=0; j<ncols; j++)
         {
             ring_elem a;
             if (M->get_entry(j,i,a))
             {
                 mpz_set(mat[i][j].getData(), a.get_mpz() );
             }
         }
     
     
     int result = lllReduction(mat, delta, eta, method, floatType, precision, flags);
     
     switch (result)
     {
         case RED_SUCCESS :
             break;
         case RED_BABAI_FAILURE :
             ERROR("Error in fpLLL");
             return 0;
         case RED_LLL_FAILURE :
             ERROR("infinite loop in LLL");
             return 0;
         default:
             ERROR("unknown error in fpLLL");
             return 0;
     }
     
     
     /* Put this back into M */
     mpz_t a;
     mpz_init(a);
     
     for (int j=0; j<ncols; j++)
         for (int i=0; i<nrows; i++)
         {
             mpz_set(a, mat[i][j].getData());
             ring_elem b = globalZZ->from_int(a);
             M->set_entry(j,i,b);
         }
     return true;
 #endif
 }