Cryptography Reference
In-Depth Information
public ModSquareMatrix(int s,BigInteger m,boolean makeZero,boolean makeInvertible)
throws MatricesNonConformableException {
//Call a superconstructor from ModMatrix-make the zero matrix,
//or a matrix with random entries
super(s,s,m,makeZero);
//Zero matrix is not invertible
if (makeZero&&makeInvertible) throw new IllegalArgumentException
(“Zero matrix cannot be inverted!”);
//A random invertible matrix is desired
if (makeInvertible) {
Random r=new Random();
SecureRandom sr=new SecureRandom();
boolean done=false;
//Do this until the matrix inverts
while (!done) {
try {
//Try to take the inverse-may throw an exception if not invertible
this.inverse();
done=true;
} catch (SingularMatrixException sme) {
//Change a random entry in the matrix
int row=Math.abs(r.nextInt())%numRows+1;
int col=Math.abs(r.nextInt())%numCols+1;
BigInteger value=new
BigInteger(modulus.bitLength(),sr).mod(modulus);
this.setElement(row,col,value);
} catch (ArithmeticException ae) {
//Change a random entry in the matrix
int row=Math.abs(r.nextInt())%numRows+1;
int col=Math.abs(r.nextInt())%numCols+1;
BigInteger value=new
BigInteger(modulus.bitLength(),sr).mod(modulus);
this.setElement(row,col,value);
}
}
}
}
The ModSquareMatrix constructor has an additional boolean variable to allow the user
to specify whether or not they wish to enforce invertibility on the new matrix. If so, we can
use this to generate random invertible square matrices modulo
; perfect for use as keys
n
with this cryptosystem.
Search WWH ::




Custom Search