static long LLL_XD(mat_ZZ& B, mat_ZZ* U, xdouble delta, long deep, LLLCheckFct check) { long m = B.NumRows(); long n = B.NumCols(); long i, j; long new_m, dep, quit; xdouble s; ZZ MU; xdouble mu1; xdouble t1; ZZ T1; init_red_fudge(); if (U) ident(*U, m); Unique2DArray<xdouble> B1_store; B1_store.SetDimsFrom1(m+1, n+1); xdouble **B1 = B1_store.get(); // approximates B Unique2DArray<xdouble> mu_store; mu_store.SetDimsFrom1(m+1, m+1); xdouble **mu = mu_store.get(); UniqueArray<xdouble> c_store; c_store.SetLength(m+1); xdouble *c = c_store.get(); // squared lengths of Gramm-Schmidt basis vectors UniqueArray<xdouble> b_store; b_store.SetLength(m+1); xdouble *b = b_store.get(); // squared lengths of basis vectors for (i = 1; i <=m; i++) for (j = 1; j <= n; j++) conv(B1[i][j], B(i, j)); for (i = 1; i <= m; i++) { b[i] = InnerProduct(B1[i], B1[i], n); } new_m = ll_LLL_XD(B, U, delta, deep, check, B1, mu, b, c, m, 1, quit); dep = m - new_m; m = new_m; if (dep > 0) { // for consistency, we move all of the zero rows to the front for (i = 0; i < m; i++) { swap(B(m+dep-i), B(m-i)); if (U) swap((*U)(m+dep-i), (*U)(m-i)); } } return m; }
static long BKZ_XD(mat_ZZ& BB, mat_ZZ* UU, xdouble delta, long beta, long prune, LLLCheckFct check) { long m = BB.NumRows(); long n = BB.NumCols(); long m_orig = m; long i, j; ZZ MU; xdouble t1; ZZ T1; xdouble *tp; init_red_fudge(); mat_ZZ B; B = BB; B.SetDims(m+1, n); Unique2DArray<xdouble> B1_store; B1_store.SetDimsFrom1(m+2, n+1); xdouble **B1 = B1_store.get(); // approximates B Unique2DArray<xdouble> mu_store; mu_store.SetDimsFrom1(m+2, m+1); xdouble **mu = mu_store.get(); UniqueArray<xdouble> c_store; c_store.SetLength(m+2); xdouble *c = c_store.get(); // squared lengths of Gramm-Schmidt basis vectors UniqueArray<xdouble> b_store; b_store.SetLength(m+2); xdouble *b = b_store.get(); // squared lengths of basis vectors xdouble cbar; UniqueArray<xdouble> ctilda_store; ctilda_store.SetLength(m+2); xdouble *ctilda = ctilda_store.get(); UniqueArray<xdouble> vvec_store; vvec_store.SetLength(m+2); xdouble *vvec = vvec_store.get(); UniqueArray<xdouble> yvec_store; yvec_store.SetLength(m+2); xdouble *yvec = yvec_store.get(); UniqueArray<xdouble> uvec_store; uvec_store.SetLength(m+2); xdouble *uvec = uvec_store.get(); UniqueArray<xdouble> utildavec_store; utildavec_store.SetLength(m+2); xdouble *utildavec = utildavec_store.get(); UniqueArray<long> Deltavec_store; Deltavec_store.SetLength(m+2); long *Deltavec = Deltavec_store.get(); UniqueArray<long> deltavec_store; deltavec_store.SetLength(m+2); long *deltavec = deltavec_store.get();; mat_ZZ Ulocal; mat_ZZ *U; if (UU) { Ulocal.SetDims(m+1, m); for (i = 1; i <= m; i++) conv(Ulocal(i, i), 1); U = &Ulocal; } else U = 0; long quit; long new_m; long z, jj, kk; long s, t; long h; xdouble eta; for (i = 1; i <=m; i++) for (j = 1; j <= n; j++) conv(B1[i][j], B(i, j)); for (i = 1; i <= m; i++) { b[i] = InnerProduct(B1[i], B1[i], n); } // cerr << "\n"; // cerr << "first LLL\n"; m = ll_LLL_XD(B, U, delta, 0, check, B1, mu, b, c, m, 1, quit); double tt; double enum_time = 0; unsigned long NumIterations = 0; unsigned long NumTrivial = 0; unsigned long NumNonTrivial = 0; unsigned long NumNoOps = 0; long verb = verbose; verbose = 0; if (m < m_orig) { for (i = m_orig+1; i >= m+2; i--) { // swap i, i-1 swap(B(i), B(i-1)); if (U) swap((*U)(i), (*U)(i-1)); } } long clean = 1; if (!quit && m > 1) { // cerr << "continuing\n"; if (beta > m) beta = m; if (prune > 0) ComputeBKZConstant(beta, prune); z = 0; jj = 0; while (z < m-1) { jj++; kk = min(jj+beta-1, m); if (jj == m) { jj = 1; kk = beta; clean = 1; } if (verb) { tt = GetTime(); if (tt > LastTime + LLLStatusInterval) BKZStatus(tt, enum_time, NumIterations, NumTrivial, NumNonTrivial, NumNoOps, m, B); } // ENUM double tt1; if (verb) { tt1 = GetTime(); } if (prune > 0) ComputeBKZThresh(&c[jj], kk-jj+1); cbar = c[jj]; utildavec[jj] = uvec[jj] = 1; yvec[jj] = vvec[jj] = 0; Deltavec[jj] = 0; s = t = jj; deltavec[jj] = 1; for (i = jj+1; i <= kk+1; i++) { ctilda[i] = uvec[i] = utildavec[i] = yvec[i] = 0; Deltavec[i] = 0; vvec[i] = 0; deltavec[i] = 1; } long enum_cnt = 0; while (t <= kk) { if (verb) { enum_cnt++; if (enum_cnt > 100000) { enum_cnt = 0; tt = GetTime(); if (tt > LastTime + LLLStatusInterval) { enum_time += tt - tt1; tt1 = tt; BKZStatus(tt, enum_time, NumIterations, NumTrivial, NumNonTrivial, NumNoOps, m, B); } } } ctilda[t] = ctilda[t+1] + (yvec[t]+utildavec[t])*(yvec[t]+utildavec[t])*c[t]; if (prune > 0 && t > jj) { eta = BKZThresh(t-jj); } else eta = 0; if (ctilda[t] < cbar - eta) { if (t > jj) { t--; t1 = 0; for (i = t+1; i <= s; i++) { t1 += utildavec[i]*mu[i][t]; } yvec[t] = t1; t1 = -t1; if (t1 >= 0) t1 = ceil(t1-0.5); else t1 = floor(t1+0.5); utildavec[t] = vvec[t] = t1; Deltavec[t] = 0; if (utildavec[t] > -yvec[t]) deltavec[t] = -1; else deltavec[t] = 1; } else { cbar = ctilda[jj]; for (i = jj; i <= kk; i++) { uvec[i] = utildavec[i]; } } } else { t++; s = max(s, t); if (t < s) Deltavec[t] = -Deltavec[t]; if (Deltavec[t]*deltavec[t] >= 0) Deltavec[t] += deltavec[t]; utildavec[t] = vvec[t] + Deltavec[t]; } } if (verb) { tt1 = GetTime() - tt1; enum_time += tt1; } NumIterations++; h = min(kk+1, m); if ((delta-8*red_fudge)*c[jj] > cbar) { clean = 0; // we treat the case that the new vector is b_s (jj < s <= kk) // as a special case that appears to occur most of the time. s = 0; for (i = jj+1; i <= kk; i++) { if (uvec[i] != 0) { if (s == 0) s = i; else s = -1; } } if (s == 0) LogicError("BKZ_XD: internal error"); if (s > 0) { // special case NumTrivial++; for (i = s; i > jj; i--) { // swap i, i-1 swap(B(i-1), B(i)); if (U) swap((*U)(i-1), (*U)(i)); tp = B1[i-1]; B1[i-1] = B1[i]; B1[i] = tp; t1 = b[i-1]; b[i-1] = b[i]; b[i] = t1; } // cerr << "special case\n"; new_m = ll_LLL_XD(B, U, delta, 0, check, B1, mu, b, c, h, jj, quit); if (new_m != h) LogicError("BKZ_XD: internal error"); if (quit) break; } else { // the general case NumNonTrivial++; for (i = 1; i <= n; i++) conv(B(m+1, i), 0); if (U) { for (i = 1; i <= m_orig; i++) conv((*U)(m+1, i), 0); } for (i = jj; i <= kk; i++) { if (uvec[i] == 0) continue; conv(MU, uvec[i]); RowTransform2(B(m+1), B(i), MU); if (U) RowTransform2((*U)(m+1), (*U)(i), MU); } for (i = m+1; i >= jj+1; i--) { // swap i, i-1 swap(B(i-1), B(i)); if (U) swap((*U)(i-1), (*U)(i)); tp = B1[i-1]; B1[i-1] = B1[i]; B1[i] = tp; t1 = b[i-1]; b[i-1] = b[i]; b[i] = t1; } for (i = 1; i <= n; i++) conv(B1[jj][i], B(jj, i)); b[jj] = InnerProduct(B1[jj], B1[jj], n); if (b[jj] == 0) LogicError("BKZ_XD: internal error"); // remove linear dependencies // cerr << "general case\n"; new_m = ll_LLL_XD(B, U, delta, 0, 0, B1, mu, b, c, kk+1, jj, quit); if (new_m != kk) LogicError("BKZ_XD: internal error"); // remove zero vector for (i = kk+2; i <= m+1; i++) { // swap i, i-1 swap(B(i-1), B(i)); if (U) swap((*U)(i-1), (*U)(i)); tp = B1[i-1]; B1[i-1] = B1[i]; B1[i] = tp; t1 = b[i-1]; b[i-1] = b[i]; b[i] = t1; } quit = 0; if (check) { for (i = 1; i <= kk; i++) if ((*check)(B(i))) { quit = 1; break; } } if (quit) break; if (h > kk) { // extend reduced basis new_m = ll_LLL_XD(B, U, delta, 0, check, B1, mu, b, c, h, h, quit); if (new_m != h) LogicError("BKZ_XD: internal error"); if (quit) break; } } z = 0; } else { // LLL_XD // cerr << "progress\n"; NumNoOps++; if (!clean) { new_m = ll_LLL_XD(B, U, delta, 0, check, B1, mu, b, c, h, h, quit); if (new_m != h) LogicError("BKZ_XD: internal error"); if (quit) break; } z++; } } } if (verb) { BKZStatus(GetTime(), enum_time, NumIterations, NumTrivial, NumNonTrivial, NumNoOps, m, B); } // clean up if (m_orig > m) { // for consistency, we move zero vectors to the front for (i = m+1; i <= m_orig; i++) { swap(B(i), B(i+1)); if (U) swap((*U)(i), (*U)(i+1)); } for (i = 0; i < m; i++) { swap(B(m_orig-i), B(m-i)); if (U) swap((*U)(m_orig-i), (*U)(m-i)); } } B.SetDims(m_orig, n); BB = B; if (U) { U->SetDims(m_orig, m_orig); *UU = *U; } return m; }
static long LLL_XD(mat_ZZ& B, mat_ZZ* U, xdouble delta, long deep, LLLCheckFct check) { long m = B.NumRows(); long n = B.NumCols(); long i, j; long new_m, dep, quit; xdouble s; ZZ MU; xdouble mu1; xdouble t1; ZZ T1; init_red_fudge(); if (U) ident(*U, m); xdouble **B1; // approximates B typedef xdouble *xdoubleptr; B1 = NTL_NEW_OP xdoubleptr[m+1]; if (!B1) Error("LLL_XD: out of memory"); for (i = 1; i <= m; i++) { B1[i] = NTL_NEW_OP xdouble[n+1]; if (!B1[i]) Error("LLL_XD: out of memory"); } xdouble **mu; mu = NTL_NEW_OP xdoubleptr[m+1]; if (!mu) Error("LLL_XD: out of memory"); for (i = 1; i <= m; i++) { mu[i] = NTL_NEW_OP xdouble[m+1]; if (!mu[i]) Error("LLL_XD: out of memory"); } xdouble *c; // squared lengths of Gramm-Schmidt basis vectors c = NTL_NEW_OP xdouble[m+1]; if (!c) Error("LLL_XD: out of memory"); xdouble *b; // squared lengths of basis vectors b = NTL_NEW_OP xdouble[m+1]; if (!b) Error("LLL_XD: out of memory"); for (i = 1; i <=m; i++) for (j = 1; j <= n; j++) conv(B1[i][j], B(i, j)); for (i = 1; i <= m; i++) { b[i] = InnerProduct(B1[i], B1[i], n); } new_m = ll_LLL_XD(B, U, delta, deep, check, B1, mu, b, c, m, 1, quit); dep = m - new_m; m = new_m; if (dep > 0) { // for consistency, we move all of the zero rows to the front for (i = 0; i < m; i++) { swap(B(m+dep-i), B(m-i)); if (U) swap((*U)(m+dep-i), (*U)(m-i)); } } // clean-up for (i = 1; i <= m+dep; i++) { delete [] B1[i]; } delete [] B1; for (i = 1; i <= m+dep; i++) { delete [] mu[i]; } delete [] mu; delete [] c; delete [] b; return m; }