Cryptography Reference
In-Depth Information
//This is the default constructor; it does nothing-required for subclass
public ModMatrix() {}
The methods of this class must provide us with the ability to retrieve the number of rows
or columns, the modulus, and individual elements in the matrix. We should also be able to
set entries in the matrix. Thus, the following methods are provided.
//Methods declared here-get rows or columns or modulus of the ModMatrix
public int rows() {return numRows;}
public int columns() {return numCols;}
public BigInteger getModulus() {return modulus;}
//Allows one to retrieve an element.
public BigInteger getElement(int row,int column) {return array[row][column];}
//Allows one to set the value of an element-least nonnegative residue always used
public void setElement(int row,int column,BigInteger value) {
array[row][column]=BigIntegerMath.lnr(value,modulus);
}
These are the methods that will be most useful; those that add, subtract, and multiply two
matrices. The least nonnegative residue modulo
is always maintained.
m
//Adds two matrices together and returns result.
public ModMatrix add(ModMatrix m) throws MatricesNonConformableException {
ModMatrix result;
//Matrices must be the same dimensions and have same modulus to be added
//together
if (!modulus.equals(m.modulus)) throw new MatricesNonConformableException
(“These matrices cannot be added; different moduli.”);
if (numRows==m.numRows&&numCols==m.numCols) {
//Make a new ModMatrix for the sum-start with zero matrix
result=new ModMatrix(numRows,numCols,modulus,true);
//Add i,j-th entries of each to get i,j-th entry of result
for (int i=1;i<=numRows;i++) {
for (int j=1;j<=numCols;j++) {
result.array[i][j]=BigIntegerMath.lnr(array[i][j].add(m.array[i][j]),modulus);
}
}
} else throw new MatricesNonConformableException
(“These matrices cannot be added; different dimensions.”);
return result;
}
//Subtracts 2nd matrix from 1st and returns result.
Search WWH ::




Custom Search