Cryptography Reference
In-Depth Information
//Makes a square matrix from a 1D array of values
public ModSquareMatrix(int s,BigInteger[] a,BigInteger m) {
super(s,s,a,m);
}
//Makes a copy of a matrix
public ModSquareMatrix(ModSquareMatrix m) {
array=new BigInteger[m.numRows+1][m.numCols+1];
numRows=m.numRows;
numCols=m.numCols;
modulus=m.modulus;
for (int i=1;i<=m.numRows;i++) {
for (int j=1;j<=m.numCols;j++) {
array[i][j]=new BigInteger(m.array[i][j].toString());
}
}
}
//Method which uses Gaussian elimination to solve AX=B mod m for X
//A is the ModSquarematrix calling the method
//B is the Modmatrix constants - need not be a Vector
//X is the ModMatrix returned
public ModMatrix gaussianSolve(ModMatrix constants) throws
MatricesNonConformableException,SingularMatrixException {
//This method only works when the modulus is prime
if (!modulus.isProbablePrime(16)) throw new IllegalArgumentException
(“Gaussian elimination method currently requires modulus to be prime!”);
//Copy the matrices and modify the copies
ModSquareMatrix mat=new ModSquareMatrix(this);
ModMatrix b;
//If the ModMatrix constants is square, the answer should also be a
//ModSquareMatrix object
//(not just a ModMatrix)
//Check for this here
if (constants instanceof ModSquareMatrix)
b=new ModSquareMatrix((ModSquareMatrix)constants);
else b=new ModMatrix(constants);
//Check if matrices are of compatible size first
if (b.numRows!=mat.numRows) throw new MatricesNonConformableException
(“Matrix of coefficients and matrix of constants have different # of rows!”);
//Work the rows, starting with the first row
int currentRow=1;
while (currentRow<=mat.numRows) {
Search WWH ::




Custom Search