public static GF2Matrix[] createRandomRegularMatrixAndItsInverse(int n,SecureRandom sr){
  GF2Matrix[] result=new GF2Matrix[2];
  int length=(n + 31) >> 5;
  GF2Matrix lm=new GF2Matrix(n,Matrix.MATRIX_TYPE_RANDOM_LT,sr);
  GF2Matrix um=new GF2Matrix(n,Matrix.MATRIX_TYPE_RANDOM_UT,sr);
  GF2Matrix rm=(GF2Matrix)lm.rightMultiply(um);
  Permutation p=new Permutation(n,sr);
  int[] pVec=p.getVector();
  int[][] matrix=new int[n][length];
  for (int i=0; i < n; i++) {
    System.arraycopy(rm.matrix[pVec[i]],0,matrix[i],0,length);
  }
  result[0]=new GF2Matrix(n,matrix);
  GF2Matrix invLm=new GF2Matrix(n,Matrix.MATRIX_TYPE_UNIT);
  for (int i=0; i < n; i++) {
    int rest=i & 0x1f;
    int q=i >>> 5;
    int r=1 << rest;
    for (int j=i + 1; j < n; j++) {
      int b=(lm.matrix[j][q]) & r;
      if (b != 0) {
        for (int k=0; k <= q; k++) {
          invLm.matrix[j][k]^=invLm.matrix[i][k];
        }
      }
    }
  }
  GF2Matrix invUm=new GF2Matrix(n,Matrix.MATRIX_TYPE_UNIT);
  for (int i=n - 1; i >= 0; i--) {
    int rest=i & 0x1f;
    int q=i >>> 5;
    int r=1 << rest;
    for (int j=i - 1; j >= 0; j--) {
      int b=(um.matrix[j][q]) & r;
      if (b != 0) {
        for (int k=q; k < length; k++) {
          invUm.matrix[j][k]^=invUm.matrix[i][k];
        }
      }
    }
  }
  result[1]=(GF2Matrix)invUm.rightMultiply(invLm.rightMultiply(p));
  return result;
}

public static GF2Matrix [ ] createRandomRegularMatrixAndItsInverse ( int n , SecureRandom sr ) { GF2Matrix [ ] result = new GF2Matrix [ 2 ] ; int length = ( n + 31 ) > > 5 ; GF2Matrix lm = new GF2Matrix ( n , Matrix . MATRIX_TYPE_RANDOM_LT , sr ) ; GF2Matrix um = new GF2Matrix ( n , Matrix . MATRIX_TYPE_RANDOM_UT , sr ) ; GF2Matrix rm = ( GF2Matrix ) lm . rightMultiply ( um ) ; Permutation p = new Permutation ( n , sr ) ; int [ ] pVec = p . getVector ( ) ; int [ ] [ ] matrix = new int [ n ] [ length ] ; for ( int i = 0 ; i < n ; i ++ ) { System . arraycopy ( rm . matrix [ pVec [ i ] ] , 0 , matrix [ i ] , 0 , length ) ; } result [ 0 ] = new GF2Matrix ( n , matrix ) ; GF2Matrix invLm = new GF2Matrix ( n , Matrix . MATRIX_TYPE_UNIT ) ; for ( int i = 0 ; i < n ; i ++ ) { int rest = i & 0x1f ; int q = i > > > 5 ; int r = 1 << rest ; for ( int j = i + 1 ; j < n ; j ++ ) { int b = ( lm . matrix [ j ] [ q ] ) & r ; if ( b != 0 ) { for ( int k = 0 ; k <= q ; k ++ ) { invLm . matrix [ j ] [ k ] ^= invLm . matrix [ i ] [ k ] ; } } } } GF2Matrix invUm = new GF2Matrix ( n , Matrix . MATRIX_TYPE_UNIT ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int rest = i & 0x1f ; int q = i > > > 5 ; int r = 1 << rest ; for ( int j = i - 1 ; j >= 0 ; j -- ) { int b = ( um . matrix [ j ] [ q ] ) & r ; if ( b != 0 ) { for ( int k = q ; k < length ; k ++ ) { invUm . matrix [ j ] [ k ] ^= invUm . matrix [ i ] [ k ] ; } } } } result [ 1 ] = ( GF2Matrix ) invUm . rightMultiply ( invLm . rightMultiply ( p ) ) ; return result ; }
create a nxn random regular matrix and its inverse .
