Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.pqc.math.linearalgebra;

This class represents polynomial rings GF(2^m)[X]/p(X) for m<32. If p(X) is irreducible, the polynomial ring is in fact an extension field of GF(2^m).
  
  public class PolynomialRingGF2m
  {

    
the finite field this polynomial ring is defined over
 
     private GF2mField field;

    
the reduction polynomial
 
     private PolynomialGF2mSmallM p;

    
the squaring matrix for this polynomial ring (given as the array of its row vectors)
 
     protected PolynomialGF2mSmallM[] sqMatrix;

    
the matrix for computing square roots in this polynomial ring (given as the array of its row vectors). This matrix is computed as the inverse of the squaring matrix.
 
     protected PolynomialGF2mSmallM[] sqRootMatrix;

    
Constructor.

Parameters:
field the finite field
p the reduction polynomial
 
     public PolynomialRingGF2m(GF2mField fieldPolynomialGF2mSmallM p)
     {
         this. = field;
         this. = p;
         computeSquaringMatrix();
         computeSquareRootMatrix();
     }

    

Returns:
the squaring matrix for this polynomial ring
 
     {
         return ;
     }

    

Returns:
the matrix for computing square roots for this polynomial ring
 
     {
         return ;
     }

    
Compute the squaring matrix for this polynomial ring, using the base field and the reduction polynomial.
 
     private void computeSquaringMatrix()
     {
         int numColumns = .getDegree();
          = new PolynomialGF2mSmallM[numColumns];
         for (int i = 0; i < numColumns >> 1; i++)
         {
             int[] monomCoeffs = new int[(i << 1) + 1];
             monomCoeffs[i << 1] = 1;
             [i] = new PolynomialGF2mSmallM(monomCoeffs);
         }
         for (int i = numColumns >> 1; i < numColumnsi++)
         {
             int[] monomCoeffs = new int[(i << 1) + 1];
             monomCoeffs[i << 1] = 1;
             PolynomialGF2mSmallM monomial = new PolynomialGF2mSmallM(,
                 monomCoeffs);
             [i] = monomial.mod();
         }
     }

    
Compute the matrix for computing square roots in this polynomial ring by inverting the squaring matrix.
 
     private void computeSquareRootMatrix()
     {
         int numColumns = .getDegree();
 
         // clone squaring matrix
         PolynomialGF2mSmallM[] tmpMatrix = new PolynomialGF2mSmallM[numColumns];
         for (int i = numColumns - 1; i >= 0; i--)
         {
            tmpMatrix[i] = new PolynomialGF2mSmallM([i]);
        }
        // initialize square root matrix as unit matrix
         = new PolynomialGF2mSmallM[numColumns];
        for (int i = numColumns - 1; i >= 0; i--)
        {
            [i] = new PolynomialGF2mSmallM(i);
        }
        // simultaneously compute Gaussian reduction of squaring matrix and unit
        // matrix
        for (int i = 0; i < numColumnsi++)
        {
            // if diagonal element is zero
            if (tmpMatrix[i].getCoefficient(i) == 0)
            {
                boolean foundNonZero = false;
                // find a non-zero element in the same row
                for (int j = i + 1; j < numColumnsj++)
                {
                    if (tmpMatrix[j].getCoefficient(i) != 0)
                    {
                        // found it, swap columns ...
                        foundNonZero = true;
                        swapColumns(tmpMatrixij);
                        swapColumns(ij);
                        // ... and quit searching
                        j = numColumns;
                        continue;
                    }
                }
                // if no non-zero element was found
                if (!foundNonZero)
                {
                    // the matrix is not invertible
                    throw new ArithmeticException(
                        "Squaring matrix is not invertible.");
                }
            }
            // normalize i-th column
            int coef = tmpMatrix[i].getCoefficient(i);
            int invCoef = .inverse(coef);
            tmpMatrix[i].multThisWithElement(invCoef);
            [i].multThisWithElement(invCoef);
            // normalize all other columns
            for (int j = 0; j < numColumnsj++)
            {
                if (j != i)
                {
                    coef = tmpMatrix[j].getCoefficient(i);
                    if (coef != 0)
                    {
                        PolynomialGF2mSmallM tmpSqColumn = tmpMatrix[i]
                            .multWithElement(coef);
                        PolynomialGF2mSmallM tmpInvColumn = [i]
                            .multWithElement(coef);
                        tmpMatrix[j].addToThis(tmpSqColumn);
                        [j].addToThis(tmpInvColumn);
                    }
                }
            }
        }
    }
    private static void swapColumns(PolynomialGF2mSmallM[] matrixint first,
                                    int second)
    {
        PolynomialGF2mSmallM tmp = matrix[first];
        matrix[first] = matrix[second];
        matrix[second] = tmp;
    }
New to GrepCode? Check out our FAQ X