Cryptography Reference
In-Depth Information
//Another parameter specifies whether or not you wish the random
//matrix to be invertible; if NOT, matrix may still be invertible by accident
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);
}
}
}
}
Search WWH ::




Custom Search