Cryptography Reference
In-Depth Information
public ModMatrix subtract(ModMatrix m) throws MatricesNonConformableException {
//Multiply the 2nd matrix by the scalar -1 then add them-see
//multiply(BigInteger) method
return this.add(m.multiply(new BigInteger(“-1”)));
}
//Multiplies two matrices.
public ModMatrix multiply(ModMatrix m) throws MatricesNonConformableException {
ModMatrix result;
//Both matrices must be using the same modulus
if (!modulus.equals(m.modulus)) throw new MatricesNonConformableException
(“These matrices cannot be multiplied; different moduli.”);
//If # rows in 2nd matrix = # columns in 1st matrix, they can be multiplied
//together
if (m.numRows==numCols) {
result=new ModMatrix(numRows,m.numCols,modulus,true);
//Move down the rows in outer loop
for (int i=1;i<=numRows;i++) {
//Multiply i-th row of 1st by j-th column of 2nd
for (int j=1;j<=m.numCols;j++) {
//Start the i,j-th entry of result at zero
result.array[i][j]=new BigInteger(“0”);
//i,j-th entry is sum of i,k-th entry of 1st times k,j-th
//entry of 2nd for all k
for (int k=1;k<=m.numRows;k++)
result.array[i][j]=BigIntegerMath.lnr
(result.array[i][j].add(array[i][k].multiply(m.array[k][j])),modulus);
}
}
} else throw new MatricesNonConformableException
(“These matrices cannot be multiplied!”);
return result;
}
//Multiplies a matrix by a scalar.
public ModMatrix multiply(BigInteger scalar) {
ModMatrix result=new ModMatrix(numRows,numCols,modulus,true);
for (int i=1;i<=numRows;i++)
for (int j=1;j<=numCols;j++)
//Multiply i,j-th entry by the scalar
result.array[i][j]=BigIntegerMath.lnr(array[i][j].multiply(scalar),modulus);
return result;
}
Search WWH ::




Custom Search