Top Description Fields Constructors Methods
org.bouncycastle.pqc.legacy.math.linearalgebra

public Class GF2Matrix

extends Matrix
Class Inheritance
Imports
java.security.SecureRandom, org.bouncycastle.util.Arrays

This class describes some operations with matrices over finite field GF(2) and is used in ecc and MQ-PKC (also has some specific methods and implementation)

Field Summary

Modifier and TypeField and Description
private int
length

the length of each array representing a row of this matrix, computed as (numColumns + 31) / 32

private int[][]
matrix

For the matrix representation the array of type int[][] is used, thus one element of the array keeps 32 elements of the matrix (from one row and 32 columns)

Inherited from org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix:
MATRIX_TYPE_RANDOM_LTMATRIX_TYPE_RANDOM_REGULARMATRIX_TYPE_RANDOM_UTMATRIX_TYPE_UNITMATRIX_TYPE_ZEROnumColumnsnumRows

Constructor Summary

AccessConstructor and Description
public
GF2Matrix(byte[]
the encoded matrix
enc
)

Create the matrix from encoded form.

public
GF2Matrix(int
the number of columns
numColumns
,
int[][]
the element array
matrix
)

Create the matrix with the contents of the given array.

public
GF2Matrix(int
the number of rows (and columns)
n
,
char
the martix type (see Matrix for predefined constants)
typeOfMatrix
)

Create an nxn matrix of the given type.

public
GF2Matrix(int
the matrix size
n
,
char
the matrix type
typeOfMatrix
,
SecureRandom
the source of randomness
sr
)

Create an nxn matrix of the given type.

public
GF2Matrix(GF2Matrix
another GF2Matrix
a
)

Copy constructor.

private
GF2Matrix(int m, int n)

create the mxn zero matrix

Method Summary

Modifier and TypeMethod and Description
private static void
addToRow(int[]
the addend
fromRow
,
int[]
the row to add to
toRow
,
int
the array index to start from
startIndex
)

Partially add one row to another.

private void
assignRandomLowerTriangularMatrix(int
number of rows (and columns)
n
,
SecureRandom
source of randomness
sr
)

Create a nxn random lower triangular matrix.

private void
assignRandomRegularMatrix(int
number of rows (and columns)
n
,
SecureRandom
source of randomness
sr
)

Create an nxn random regular matrix.

private void
assignRandomUpperTriangularMatrix(int
number of rows (and columns)
n
,
SecureRandom
source of randomness
sr
)

Create a nxn random upper triangular matrix.

private void
assignUnitMatrix(int
number of rows (and columns)
n
)

Create the mxn unit matrix.

private void
assignZeroMatrix(int
number of rows
m
,
int
number of columns
n
)

Create the mxn zero matrix.

public Matrix

Returns:

the inverse of this matrix (newly created).
computeInverse
()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.computeInverse.

Compute the inverse of this matrix.

public Matrix

Returns:

(this)T
computeTranspose
()

Compute the transpose of this matrix.

public static GF2Matrix[]

Returns:

the created random regular matrix and its inverse
createRandomRegularMatrixAndItsInverse
(int
number of rows (and columns)
n
,
SecureRandom
source of randomness
sr
)

Create a nxn random regular matrix and its inverse.

public boolean

Returns:

the result of the comparison
equals
(Object
another object
other
)

Overrides java.lang.Object.equals.

Compare this matrix with another object.

public GF2Matrix

Returns:

(this | Id)
extendLeftCompactForm
()

Compute the full form matrix (this | Id) from this matrix in left compact form, where Id is the k x k identity matrix and k is the number of rows of this matrix.

public GF2Matrix

Returns:

(Id | this)
extendRightCompactForm
()

Compute the full form matrix (Id | this) from this matrix in right compact form, where Id is the k x k identity matrix and k is the number of rows of this matrix.

public byte[]

Returns:

the encoded matrix
getEncoded
()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.getEncoded.

Returns encoded matrix, i.e., this matrix in byte array form

public double

Returns:

the Hamming weight of this matrix (as a ratio).
getHammingWeight
()

Returns the percentage of the number of "ones" in this matrix.

public int[][]

Returns:

the array keeping the matrix elements
getIntArray
()

public GF2Matrix

Returns:

the (numRows x numRows) submatrix
getLeftSubMatrix
()

Get the quadratic submatrix of this matrix consisting of the leftmost numRows columns.

public int

Returns:

the length of each array representing a row of this matrix
getLength
()

public GF2Matrix

Returns:

the (numRows x (numColumns-numRows)) submatrix
getRightSubMatrix
()

Get the submatrix of this matrix consisting of the rightmost numColumns-numRows columns.

public int[]

Returns:

the row of this matrix with the given index
getRow
(int
the index
index
)

Return the row of this matrix with the given index.

public int

Returns:

the hash code of this matrix
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true if this is the zero matrix
isZero
()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.isZero.

Check if this is the zero matrix (i.e., all entries are zero).

public Matrix

Returns:

GF2Matrix P*this
leftMultiply
(Permutation
the permutation
p
)

Compute the product of a permutation matrix (which is generated from an n-permutation) and this matrix.

public Vector

Returns:

Vector product a*matrix
leftMultiply
(Vector
a vector over GF(2)
vec
)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.leftMultiply.

compute product a row vector and this matrix

public Vector

Returns:

(this | Id)*vector
leftMultiplyLeftCompactForm
(Vector
the vector over GF(2)
vec
)

Compute the product of the matrix (this | Id) and a column vector, where Id is a (numRows x numRows) unit matrix.

public Matrix

Returns:

matrix product this*matrixA
rightMultiply
(Matrix
a matrix A over GF(2)
mat
)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and a matrix A over GF(2).

public Matrix

Returns:

GF2Matrix this*P
rightMultiply
(Permutation
the permutation
p
)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and a permutation matrix which is generated from an n-permutation.

public Vector

Returns:

this*vector
rightMultiply
(Vector
the vector over GF(2)
vec
)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and the given column vector.

public Vector

Returns:

(Id | this)*vector
rightMultiplyRightCompactForm
(Vector
the vector over GF(2)
vec
)

Compute the product of the matrix (Id | this) and a column vector, where Id is a (numRows x numRows) unit matrix.

private static void
swapRows(int[][]
the matrix
matrix
,
int
the index of the first row
first
,
int
the index of the second row
second
)

Swap two rows of the given matrix.

public String

Returns:

a human readable form of the matrix
toString
()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.toString.

Returns a string representation of the object.

Inherited from org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix:
getNumColumnsgetNumRows

Field Detail

lengthback to summary
private int length

the length of each array representing a row of this matrix, computed as (numColumns + 31) / 32

matrixback to summary
private int[][] matrix

For the matrix representation the array of type int[][] is used, thus one element of the array keeps 32 elements of the matrix (from one row and 32 columns)

Constructor Detail

GF2Matrixback to summary
public GF2Matrix(byte[] enc)

Create the matrix from encoded form.

Parameters
enc:byte[]

the encoded matrix

GF2Matrixback to summary
public GF2Matrix(int numColumns, int[][] matrix)

Create the matrix with the contents of the given array. The matrix is not copied. Unused coefficients are masked out.

Parameters
numColumns:int

the number of columns

matrix:int[][]

the element array

GF2Matrixback to summary
public GF2Matrix(int n, char typeOfMatrix)

Create an nxn matrix of the given type.

Parameters
n:int

the number of rows (and columns)

typeOfMatrix:char

the martix type (see Matrix for predefined constants)

GF2Matrixback to summary
public GF2Matrix(int n, char typeOfMatrix, SecureRandom sr)

Create an nxn matrix of the given type.

Parameters
n:int

the matrix size

typeOfMatrix:char

the matrix type

sr:SecureRandom

the source of randomness

GF2Matrixback to summary
public GF2Matrix(GF2Matrix a)

Copy constructor.

Parameters
a:GF2Matrix

another GF2Matrix

GF2Matrixback to summary
private GF2Matrix(int m, int n)

create the mxn zero matrix

Method Detail

addToRowback to summary
private static void addToRow(int[] fromRow, int[] toRow, int startIndex)

Partially add one row to another.

Parameters
fromRow:int[]

the addend

toRow:int[]

the row to add to

startIndex:int

the array index to start from

assignRandomLowerTriangularMatrixback to summary
private void assignRandomLowerTriangularMatrix(int n, SecureRandom sr)

Create a nxn random lower triangular matrix.

Parameters
n:int

number of rows (and columns)

sr:SecureRandom

source of randomness

assignRandomRegularMatrixback to summary
private void assignRandomRegularMatrix(int n, SecureRandom sr)

Create an nxn random regular matrix.

Parameters
n:int

number of rows (and columns)

sr:SecureRandom

source of randomness

assignRandomUpperTriangularMatrixback to summary
private void assignRandomUpperTriangularMatrix(int n, SecureRandom sr)

Create a nxn random upper triangular matrix.

Parameters
n:int

number of rows (and columns)

sr:SecureRandom

source of randomness

assignUnitMatrixback to summary
private void assignUnitMatrix(int n)

Create the mxn unit matrix.

Parameters
n:int

number of rows (and columns)

assignZeroMatrixback to summary
private void assignZeroMatrix(int m, int n)

Create the mxn zero matrix.

Parameters
m:int

number of rows

n:int

number of columns

computeInverseback to summary
public Matrix computeInverse()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.computeInverse.

Compute the inverse of this matrix.

Returns:Matrix

the inverse of this matrix (newly created).

Exceptions
ArithmeticException:
if this matrix is not invertible.
computeTransposeback to summary
public Matrix computeTranspose()

Compute the transpose of this matrix.

Returns:Matrix

(this)T

createRandomRegularMatrixAndItsInverseback to summary
public static GF2Matrix[] createRandomRegularMatrixAndItsInverse(int n, SecureRandom sr)

Create a nxn random regular matrix and its inverse.

Parameters
n:int

number of rows (and columns)

sr:SecureRandom

source of randomness

Returns:GF2Matrix[]

the created random regular matrix and its inverse

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Compare this matrix with another object.

Parameters
other:Object

another object

Returns:boolean

the result of the comparison

extendLeftCompactFormback to summary
public GF2Matrix extendLeftCompactForm()

Compute the full form matrix (this | Id) from this matrix in left compact form, where Id is the k x k identity matrix and k is the number of rows of this matrix.

Returns:GF2Matrix

(this | Id)

extendRightCompactFormback to summary
public GF2Matrix extendRightCompactForm()

Compute the full form matrix (Id | this) from this matrix in right compact form, where Id is the k x k identity matrix and k is the number of rows of this matrix.

Returns:GF2Matrix

(Id | this)

getEncodedback to summary
public byte[] getEncoded()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.getEncoded.

Returns encoded matrix, i.e., this matrix in byte array form

Returns:byte[]

the encoded matrix

getHammingWeightback to summary
public double getHammingWeight()

Returns the percentage of the number of "ones" in this matrix.

Returns:double

the Hamming weight of this matrix (as a ratio).

getIntArrayback to summary
public int[][] getIntArray()
Returns:int[][]

the array keeping the matrix elements

getLeftSubMatrixback to summary
public GF2Matrix getLeftSubMatrix()

Get the quadratic submatrix of this matrix consisting of the leftmost numRows columns.

Returns:GF2Matrix

the (numRows x numRows) submatrix

getLengthback to summary
public int getLength()
Returns:int

the length of each array representing a row of this matrix

getRightSubMatrixback to summary
public GF2Matrix getRightSubMatrix()

Get the submatrix of this matrix consisting of the rightmost numColumns-numRows columns.

Returns:GF2Matrix

the (numRows x (numColumns-numRows)) submatrix

getRowback to summary
public int[] getRow(int index)

Return the row of this matrix with the given index.

Parameters
index:int

the index

Returns:int[]

the row of this matrix with the given index

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

the hash code of this matrix

isZeroback to summary
public boolean isZero()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.isZero.

Check if this is the zero matrix (i.e., all entries are zero).

Returns:boolean

true if this is the zero matrix

leftMultiplyback to summary
public Matrix leftMultiply(Permutation p)

Compute the product of a permutation matrix (which is generated from an n-permutation) and this matrix.

Parameters
p:Permutation

the permutation

Returns:Matrix

GF2Matrix P*this

leftMultiplyback to summary
public Vector leftMultiply(Vector vec)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.leftMultiply.

compute product a row vector and this matrix

Parameters
vec:Vector

a vector over GF(2)

Returns:Vector

Vector product a*matrix

leftMultiplyLeftCompactFormback to summary
public Vector leftMultiplyLeftCompactForm(Vector vec)

Compute the product of the matrix (this | Id) and a column vector, where Id is a (numRows x numRows) unit matrix.

Parameters
vec:Vector

the vector over GF(2)

Returns:Vector

(this | Id)*vector

rightMultiplyback to summary
public Matrix rightMultiply(Matrix mat)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and a matrix A over GF(2).

Parameters
mat:Matrix

a matrix A over GF(2)

Returns:Matrix

matrix product this*matrixA

rightMultiplyback to summary
public Matrix rightMultiply(Permutation p)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and a permutation matrix which is generated from an n-permutation.

Parameters
p:Permutation

the permutation

Returns:Matrix

GF2Matrix this*P

rightMultiplyback to summary
public Vector rightMultiply(Vector vec)

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.rightMultiply.

Compute the product of this matrix and the given column vector.

Parameters
vec:Vector

the vector over GF(2)

Returns:Vector

this*vector

rightMultiplyRightCompactFormback to summary
public Vector rightMultiplyRightCompactForm(Vector vec)

Compute the product of the matrix (Id | this) and a column vector, where Id is a (numRows x numRows) unit matrix.

Parameters
vec:Vector

the vector over GF(2)

Returns:Vector

(Id | this)*vector

swapRowsback to summary
private static void swapRows(int[][] matrix, int first, int second)

Swap two rows of the given matrix.

Parameters
matrix:int[][]

the matrix

first:int

the index of the first row

second:int

the index of the second row

toStringback to summary
public String toString()

Implements abstract org.bouncycastle.pqc.legacy.math.linearalgebra.Matrix.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a human readable form of the matrix