📄 lll_qp.c
字号:
swap(B(i), B(i+1)); tp = B1[i]; B1[i] = B1[i+1]; B1[i+1] = tp; t1 = b[i]; b[i] = b[i+1]; b[i+1] = t1; t1 = max_b[i]; max_b[i] = max_b[i+1]; max_b[i+1] = t1; if (U) swap((*U)(i), (*U)(i+1)); } for (i = k; i <= m+1; i++) st[i] = 1; m--; if (quit) break; continue; } if (quit) break; if (deep > 0) { // deep insertions quad_float cc = b[k]; long l = 1; while (l <= k-1 && delta*c[l] <= cc) { cc = cc - mu[k][l]*mu[k][l]*c[l]; l++; } if (l <= k-1 && (l <= deep || k-l <= deep)) { // deep insertion at position l for (i = k; i > l; i--) { // swap rows i, i-1 swap(B(i), B(i-1)); tp = B1[i]; B1[i] = B1[i-1]; B1[i-1] = tp; tp = mu[i]; mu[i] = mu[i-1]; mu[i-1] = tp; t1 = b[i]; b[i] = b[i-1]; b[i-1] = t1; t1 = max_b[i]; max_b[i] = max_b[i-1]; max_b[i-1] = t1; if (U) swap((*U)(i), (*U)(i-1)); } k = l; NumSwaps++; continue; } } // end deep insertions // test LLL reduction condition if (k > 1 && delta*c[k-1] > c[k] + mu[k][k-1]*mu[k][k-1]*c[k-1]) { // swap rows k, k-1 swap(B(k), B(k-1)); tp = B1[k]; B1[k] = B1[k-1]; B1[k-1] = tp; tp = mu[k]; mu[k] = mu[k-1]; mu[k-1] = tp; t1 = b[k]; b[k] = b[k-1]; b[k-1] = t1; t1 = max_b[k]; max_b[k] = max_b[k-1]; max_b[k-1] = t1; if (U) swap((*U)(k), (*U)(k-1)); k--; NumSwaps++; // cout << "- " << k << "\n"; } else { k++; // cout << "+ " << k << "\n"; } } if (verbose) { LLLStatus(m+1, GetTime(), m, B); } delete [] buf; delete [] max_b; return m;}staticlong LLL_QP(mat_ZZ& B, mat_ZZ* U, quad_float delta, long deep, LLLCheckFct check){ long m = B.NumRows(); long n = B.NumCols(); long i, j; long new_m, dep, quit; quad_float s; ZZ MU; quad_float mu1; quad_float t1; ZZ T1; init_red_fudge(); if (U) ident(*U, m); quad_float **B1; // approximates B typedef quad_float *quad_floatptr; B1 = NTL_NEW_OP quad_floatptr[m+1]; if (!B1) Error("LLL_QP: out of memory"); for (i = 1; i <= m; i++) { B1[i] = NTL_NEW_OP quad_float[n+1]; if (!B1[i]) Error("LLL_QP: out of memory"); } quad_float **mu; mu = NTL_NEW_OP quad_floatptr[m+1]; if (!mu) Error("LLL_QP: out of memory"); for (i = 1; i <= m; i++) { mu[i] = NTL_NEW_OP quad_float[m+1]; if (!mu[i]) Error("LLL_QP: out of memory"); } quad_float *c; // squared lengths of Gramm-Schmidt basis vectors c = NTL_NEW_OP quad_float[m+1]; if (!c) Error("LLL_QP: out of memory"); quad_float *b; // squared lengths of basis vectors b = NTL_NEW_OP quad_float[m+1]; if (!b) Error("LLL_QP: 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); if (!IsFinite(&b[i])) Error("LLL_QP: numbers too big...use LLL_XD"); } new_m = ll_LLL_QP(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; i++) { delete [] B1[i]; } delete [] B1; for (i = 1; i <= m; i++) { delete [] mu[i]; } delete [] mu; delete [] c; delete [] b; return m;} long LLL_QP(mat_ZZ& B, double delta, long deep, LLLCheckFct check, long verb){ verbose = verb; NumSwaps = 0; if (verbose) { StartTime = GetTime(); LastTime = StartTime; } if (delta < 0.50 || delta >= 1) Error("LLL_QP: bad delta"); if (deep < 0) Error("LLL_QP: bad deep"); return LLL_QP(B, 0, to_quad_float(delta), deep, check);}long LLL_QP(mat_ZZ& B, mat_ZZ& U, double delta, long deep, LLLCheckFct check, long verb){ verbose = verb; NumSwaps = 0; if (verbose) { StartTime = GetTime(); LastTime = StartTime; } if (delta < 0.50 || delta >= 1) Error("LLL_QP: bad delta"); if (deep < 0) Error("LLL_QP: bad deep"); return LLL_QP(B, &U, to_quad_float(delta), deep, check);}static vec_quad_float BKZConstant;staticvoid ComputeBKZConstant(long beta, long p){ const quad_float c_PI = to_quad_float("3.141592653589793238462643383279502884197"); const quad_float LogPI = to_quad_float("1.144729885849400174143427351353058711647"); BKZConstant.SetLength(beta-1); vec_quad_float Log; Log.SetLength(beta); long i, j, k; quad_float x, y; for (j = 1; j <= beta; j++) Log(j) = log(to_quad_float(j)); for (i = 1; i <= beta-1; i++) { // First, we compute x = gamma(i/2)^{2/i} k = i/2; if ((i & 1) == 0) { // i even x = 0; for (j = 1; j <= k; j++) x = x + Log(j); x = x * (1/to_quad_float(k)); x = exp(x); } else { // i odd x = 0; for (j = k + 2; j <= 2*k + 2; j++) x = x + Log(j); x = 0.5*LogPI + x - 2*(k+1)*Log(2); x = x * (2.0/to_quad_float(i)); x = exp(x); } // Second, we compute y = 2^{2*p/i} y = -(2*p/to_quad_float(i))*Log(2); y = exp(y); BKZConstant(i) = x*y/c_PI; }}static vec_quad_float BKZThresh;static void ComputeBKZThresh(quad_float *c, long beta){ BKZThresh.SetLength(beta-1); long i; quad_float x; x = 0; for (i = 1; i <= beta-1; i++) { x += log(c[i-1]); BKZThresh(i) = exp(x/to_quad_float(i))*BKZConstant(i); if (!IsFinite(&BKZThresh(i))) BKZThresh(i) = 0; }}static void BKZStatus(double tt, double enum_time, long NumIterations, long NumTrivial, long NumNonTrivial, long NumNoOps, long m, const mat_ZZ& B){ cerr << "---- BKZ_QP status ----\n"; cerr << "elapsed time: "; PrintTime(cerr, tt-StartTime); cerr << ", enum time: "; PrintTime(cerr, enum_time); cerr << ", iter: " << NumIterations << "\n"; cerr << "triv: " << NumTrivial; cerr << ", nontriv: " << NumNonTrivial; cerr << ", no ops: " << NumNoOps; cerr << ", rank: " << m; cerr << ", swaps: " << NumSwaps << "\n"; ZZ t1; long i; double prodlen = 0; for (i = 1; i <= m; i++) { InnerProduct(t1, B(i), B(i)); if (!IsZero(t1)) prodlen += log(t1); } cerr << "log of prod of lengths: " << prodlen/(2.0*log(2.0)) << "\n"; if (LLLDumpFile) { cerr << "dumping to " << LLLDumpFile << "..."; ofstream f; OpenWrite(f, LLLDumpFile); f << "["; for (i = 1; i <= m; i++) { f << B(i) << "\n"; } f << "]\n"; f.close(); cerr << "\n"; } LastTime = tt; }staticlong BKZ_QP(mat_ZZ& BB, mat_ZZ* UU, quad_float delta, long beta, long prune, LLLCheckFct check){ long m = BB.NumRows(); long n = BB.NumCols(); long m_orig = m; long i, j; ZZ MU; quad_float t1; ZZ T1; quad_float *tp; init_red_fudge(); mat_ZZ B; B = BB; B.SetDims(m+1, n); quad_float **B1; // approximates B typedef quad_float *quad_floatptr; B1 = NTL_NEW_OP quad_floatptr[m+2]; if (!B1) Error("BKZ_QP: out of memory"); for (i = 1; i <= m+1; i++) { B1[i] = NTL_NEW_OP quad_float[n+1]; if (!B1[i]) Error("BKZ_QP: out of memory"); } quad_float **mu; mu = NTL_NEW_OP quad_floatptr[m+2]; if (!mu) Error("BKZ_QP: out of memory"); for (i = 1; i <= m+1; i++) { mu[i] = NTL_NEW_OP quad_float[m+1]; if (!mu[i]) Error("BKZ_QP: out of memory"); } quad_float *c; // squared lengths of Gramm-Schmidt basis vectors c = NTL_NEW_OP quad_float[m+2]; if (!c) Error("BKZ_QP: out of memory"); quad_float *b; // squared lengths of basis vectors b = NTL_NEW_OP quad_float[m+2]; if (!b) Error("BKZ_QP: out of memory"); quad_float cbar; quad_float *ctilda; ctilda = NTL_NEW_OP quad_float[m+2]; if (!ctilda) Error("BKZ_QP: out of memory"); quad_float *vvec; vvec = NTL_NEW_OP quad_float[m+2]; if (!vvec) Error("BKZ_QP: out of memory"); quad_float *yvec; yvec = NTL_NEW_OP quad_float[m+2]; if (!yvec) Error("BKZ_QP: out of memory"); quad_float *uvec; uvec = NTL_NEW_OP quad_float[m+2]; if (!uvec) Error("BKZ_QP: out of memory"); quad_float *utildavec; utildavec = NTL_NEW_OP quad_float[m+2]; if (!utildavec) Error("BKZ_QP: out of memory"); long *Deltavec; Deltavec = NTL_NEW_OP long[m+2]; if (!Deltavec) Error("BKZ_QP: out of memory"); long *deltavec; deltavec = NTL_NEW_OP long[m+2]; if (!deltavec) Error("BKZ_QP: out of memory"); 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; quad_float 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); if (!IsFinite(&b[i])) Error("BKZ_FD: numbers too big...use BKZ_XD"); } // cerr << "\n"; // cerr << "first LLL\n"; m = ll_LLL_QP(B, U, delta, 0, check, B1, mu, b, c, m, 1, quit); double tt; double enum_time = 0; long NumIterations = 0; long NumTrivial = 0; long NumNonTrivial = 0; long NumNoOps = 0; long verb = verbose; verbose = 0; long clean = 1; 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)); } } 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) Error("BKZ_QP: 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_QP(B, U, delta, 0, check, B1, mu, b, c, h, jj, quit); if (new_m != h) Error("BKZ_QP: 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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -