/// <summary> /// Create an nxn random regular matrix /// </summary> /// /// <param name="N">Number of rows (and columns)</param> /// <param name="SecRnd">Source of randomness</param> private void AssignRandomRegularMatrix(int N, IRandom SecRnd) { RowCount = N; ColumnCount = N; _length = IntUtils.URShift((N + 31), 5); _matrix = ArrayUtils.CreateJagged <int[][]>(RowCount, _length); GF2Matrix lm = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_LT, SecRnd); GF2Matrix um = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_UT, SecRnd); GF2Matrix rm = (GF2Matrix)lm.RightMultiply(um); Permutation perm = new Permutation(N, SecRnd); int[] p = perm.GetVector(); for (int i = 0; i < N; i++) { Array.Copy(rm._matrix[i], 0, _matrix[p[i]], 0, _length); } }
private static MaMaPe GetMMP(GF2Matrix H, IRandom SecRnd) { int n = H.ColumnCount; GF2Matrix s = null; Permutation p = new Permutation(n, SecRnd); GF2Matrix hp = (GF2Matrix)H.RightMultiply(p); GF2Matrix sInv = hp.LeftSubMatrix(); if ((s = InvertMatrix(sInv)) == null) { return(null); } GF2Matrix shp = (GF2Matrix)s.RightMultiply(hp); GF2Matrix m = shp.RightSubMatrix(); return(new MaMaPe(sInv, m, p)); }
/// <summary> /// Create a nxn random regular matrix and its inverse /// </summary> /// /// <param name="N">Number of rows (and columns)</param> /// <param name="SecRnd">Source of randomness</param> /// <returns>The created random regular matrix and its inverse</returns> public static GF2Matrix[] CreateRandomRegularMatrixAndItsInverse(int N, IRandom SecRnd) { GF2Matrix[] result = new GF2Matrix[2]; // First part: create regular matrix int length = (N + 31) >> 5; GF2Matrix lm = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_LT, SecRnd); GF2Matrix um = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_UT, SecRnd); GF2Matrix rm = (GF2Matrix)lm.RightMultiply(um); Permutation p = new Permutation(N, SecRnd); int[] pVec = p.GetVector(); int[][] matrix = ArrayUtils.CreateJagged <int[][]>(N, length); for (int i = 0; i < N; i++) { Array.Copy(rm._matrix[pVec[i]], 0, matrix[i], 0, length); } result[0] = new GF2Matrix(N, matrix); // Second part: create inverse matrix // inverse to lm GF2Matrix invLm = new GF2Matrix(N, Matrix.MATRIX_TYPE_UNIT); for (int i = 0; i < N; i++) { int rest = i & 0x1f; int q = IntUtils.URShift(i, 5); int r = 1 << rest; for (int j = i + 1; j < N; j++) { int b = (lm._matrix[j][q]) & r; if (b != 0) { for (int k = 0; k <= q; k++) { invLm._matrix[j][k] ^= invLm._matrix[i][k]; } } } } // inverse to um GF2Matrix invUm = new GF2Matrix(N, Matrix.MATRIX_TYPE_UNIT); for (int i = N - 1; i >= 0; i--) { int rest = i & 0x1f; int q = IntUtils.URShift(i, 5); int r = 1 << rest; for (int j = i - 1; j >= 0; j--) { int b = (um._matrix[j][q]) & r; if (b != 0) { for (int k = q; k < length; k++) { invUm._matrix[j][k] ^= invUm._matrix[i][k]; } } } } // inverse matrix result[1] = (GF2Matrix)invUm.RightMultiply(invLm.RightMultiply(p)); return(result); }
/// <summary> /// Create a nxn random regular matrix and its inverse /// </summary> /// /// <param name="N">Number of rows (and columns)</param> /// <param name="SecRnd">Source of randomness</param> /// <returns>The created random regular matrix and its inverse</returns> public static GF2Matrix[] CreateRandomRegularMatrixAndItsInverse(int N, IRandom SecRnd) { GF2Matrix[] result = new GF2Matrix[2]; // First part: create regular matrix int length = (N + 31) >> 5; GF2Matrix lm = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_LT, SecRnd); GF2Matrix um = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_UT, SecRnd); GF2Matrix rm = (GF2Matrix)lm.RightMultiply(um); Permutation p = new Permutation(N, SecRnd); int[] pVec = p.GetVector(); int[][] matrix = ArrayUtils.CreateJagged<int[][]>(N, length); for (int i = 0; i < N; i++) Array.Copy(rm._matrix[pVec[i]], 0, matrix[i], 0, length); result[0] = new GF2Matrix(N, matrix); // Second part: create inverse matrix // inverse to lm GF2Matrix invLm = new GF2Matrix(N, Matrix.MATRIX_TYPE_UNIT); for (int i = 0; i < N; i++) { int rest = i & 0x1f; int q = IntUtils.URShift(i, 5); int r = 1 << rest; for (int j = i + 1; j < N; j++) { int b = (lm._matrix[j][q]) & r; if (b != 0) { for (int k = 0; k <= q; k++) invLm._matrix[j][k] ^= invLm._matrix[i][k]; } } } // inverse to um GF2Matrix invUm = new GF2Matrix(N, Matrix.MATRIX_TYPE_UNIT); for (int i = N - 1; i >= 0; i--) { int rest = i & 0x1f; int q = IntUtils.URShift(i, 5); int r = 1 << rest; for (int j = i - 1; j >= 0; j--) { int b = (um._matrix[j][q]) & r; if (b != 0) { for (int k = q; k < length; k++) invUm._matrix[j][k] ^= invUm._matrix[i][k]; } } } // inverse matrix result[1] = (GF2Matrix)invUm.RightMultiply(invLm.RightMultiply(p)); return result; }
/// <summary> /// Create an nxn random regular matrix /// </summary> /// /// <param name="N">Number of rows (and columns)</param> /// <param name="SecRnd">Source of randomness</param> private void AssignRandomRegularMatrix(int N, IRandom SecRnd) { RowCount = N; ColumnCount = N; _length = IntUtils.URShift((N + 31), 5); _matrix = ArrayUtils.CreateJagged<int[][]>(RowCount, _length); GF2Matrix lm = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_LT, SecRnd); GF2Matrix um = new GF2Matrix(N, Matrix.MATRIX_TYPE_RANDOM_UT, SecRnd); GF2Matrix rm = (GF2Matrix)lm.RightMultiply(um); Permutation perm = new Permutation(N, SecRnd); int[] p = perm.GetVector(); for (int i = 0; i < N; i++) Array.Copy(rm._matrix[i], 0, _matrix[p[i]], 0, _length); }
private static MaMaPe GetMMP(GF2Matrix H, IRandom SecRnd) { int n = H.ColumnCount; GF2Matrix s = null; Permutation p = new Permutation(n, SecRnd); GF2Matrix hp = (GF2Matrix)H.RightMultiply(p); GF2Matrix sInv = hp.LeftSubMatrix(); if ((s = InvertMatrix(sInv)) == null) return null; GF2Matrix shp = (GF2Matrix)s.RightMultiply(hp); GF2Matrix m = shp.RightSubMatrix(); return new MaMaPe(sInv, m, p); }