// Find A^{-1} public static Matrix Invert(Matrix A) { Matrix E = new Matrix(A.size[0], A.size[0]); for (int i = 0; i < A.size[0]; ++i) { E.data[i][i] = 1; } return(MatrixFuncs.SolveEq(A, E)); }
public double AttackHam(out Matrix N1, out Matrix P1) { int k = Gp.size[0], n = Gp.size[1]; N1 = new Matrix(k, k); P1 = new Matrix(n, n); Code code = new Code(Gp); // compute r int r = 0; while ((1 << r) - 1 < n) { ++r; } Stopwatch t = new Stopwatch(); t.Start(); // Matrix P Ham ham = new Ham(r); IEnumerable <int> tmp1, tmp2; Matrix P1_inv = new Matrix(n, n); for (int j = 0; j < n; ++j) { tmp1 = code.H.data.Select(x => x[j]); for (int jj = 0; jj < n; ++jj) { tmp2 = ham.H.data.Select(x => x[jj]); if (tmp1.SequenceEqual(tmp2)) { P1.data[jj][j] = 1; P1_inv.data[j][jj] = 1; break; } } } // Matrix N code.G = code.G * P1_inv; N1 = MatrixFuncs.Transp(MatrixFuncs.SolveEq(MatrixFuncs.Transp(ham.G), MatrixFuncs.Transp(code.G))); t.Stop(); return(t.ElapsedMilliseconds / 1000.00); }
public double AttackHam(out List <Matrix> Nlist, out Matrix P1) { int k = Gp.size[0], nn = Gp.size[1]; Nlist = new List <Matrix>(); P1 = new Matrix(nn, nn); int r = (int)Math.Log((nn + 2) / 2, 2); int tmp = nn % ((1 << r) - 1), s; while (tmp != 0) { --r; tmp = nn % ((1 << r) - 1); } s = nn / ((1 << r) - 1); int n = nn / s; Matrix Ni = new Matrix(k, k); P1 = new Matrix(nn, nn); Matrix P1_inv = new Matrix(nn, nn); Code code = new Code(Gp); Ham ham = new Ham(r); Stopwatch t = new Stopwatch(); t.Start(); // Matrix P1 List <int>[] ham_idx = GetHamIndices(code, ham, r, s, n, k); Matrix Zero = new Matrix(k, n - k); BigInteger iter_count = 0, max_iter_num = 1, ind_count = 0; List <int>[] idx_copy = new List <int> [n]; int[][] indices = new int[s][]; for (int i = 0; i < s; ++i) { indices[i] = new int[n]; max_iter_num = BigInteger.Multiply(max_iter_num, BigInteger.Pow(s - i, n)); } do { ++iter_count; ++ind_count; if (iter_count > max_iter_num) { ham_idx = GetHamIndices(code, ham, r, s, n, k); for (int i = 0; i < s; ++i) { indices[i] = new int[n]; } iter_count = 1; ind_count = 1; } idx_copy = ham_idx.Select(x => new List <int>(x)).ToArray(); P1 = new Matrix(nn, nn); P1_inv = new Matrix(nn, nn); for (int i = 0; i < s; i++) { for (int j = 0; j < n; ++j) { P1.data[i * n + j][idx_copy[j][indices[i][j]]] = 1; P1_inv.data[idx_copy[j][indices[i][j]]][i * n + j] = 1; idx_copy[j].RemoveAt(indices[i][j]); } } Common.NextSet(indices[0], s, n); if (ind_count == BigInteger.Pow(s, n)) { indices[0] = new int[n]; Common.NextSet(indices[1], s - 1, n); ind_count = 0; } } while (checkP(Gp, P1_inv, ham, Zero, s, n) == false); // Matrices N Matrix NG = Gp * P1_inv; for (int i = 0; i < s; ++i) { Ni = MatrixFuncs.SolveEq(MatrixFuncs.Transp(ham.G), MatrixFuncs.Transp(MatrixFuncs.SubMatrix(NG, i * n, (i + 1) * n))); var ttt = MatrixFuncs.Transp(Ni) * ham.G == MatrixFuncs.SubMatrix(NG, i * n, (i + 1) * n); Nlist.Add(MatrixFuncs.Transp(Ni)); } NG = new Matrix(); for (int i = 0; i < Nlist.Count; ++i) { NG |= Nlist[i] * ham.G; } t.Stop(); return(t.ElapsedMilliseconds / 1000.00); }
public double AttackRM(out Matrix N1, out Matrix P1) { int k = Gp.size[0], n = Gp.size[1]; N1 = new Matrix(k, k); P1 = new Matrix(n, n); Code code = new Code(Gp); Code Gp_reduced; // Compute r, m int r = -1, m = (int)Math.Log(n, 2), k1 = 0; while (k1 < k) { ++r; k1 += Common.C(m, r); } RM rm = new RM(r, m); Stopwatch t = new Stopwatch(); t.Start(); // Reduction //2 * r >= m => reduction on dual code if (2 * r < m || m - r == 1) { Gp_reduced = new Code(code.G); } else { Gp_reduced = new Code(code.H); r = m - r - 1; } while (r > 1) { Gp_reduced = AttackFuncs.Reduction(Gp_reduced, r, m); --r; } // Matrix P Matrix Tmp; int idx = -1; Vector e = new Vector(Enumerable.Repeat(1, n).ToList()); do { ++idx; Tmp = (new Matrix(Gp_reduced.G) - idx) & e; }while (MatrixFuncs.Gauss(Tmp) < m + 1); Tmp = Gp_reduced.G; Tmp -= idx; Matrix RM_1 = (new RM(1, m)).G -= 0; IEnumerable <int> tmp1, tmp2; Matrix P1_inv = new Matrix(n, n); for (int j = 0; j < n; ++j) { tmp1 = Tmp.data.Select(x => x[j]); for (int jj = 0; jj < n; ++jj) { tmp2 = RM_1.data.Select(x => x[jj]); if (tmp1.SequenceEqual(tmp2)) { P1.data[jj][j] = 1; P1_inv.data[j][jj] = 1; break; } } } // Matrix N code.G = code.G * P1_inv; N1 = MatrixFuncs.Transp(MatrixFuncs.SolveEq(MatrixFuncs.Transp(rm.G), MatrixFuncs.Transp(code.G))); t.Stop(); return(t.ElapsedMilliseconds / 1000.00); }