Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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


This class implements polynomials over GF2nElements.

See also:
GF2nElement
  
  
 public class GF2nPolynomial
 {
 
     private GF2nElement[] coeff// keeps the coefficients of this polynomial
 
     private int size// the size of this polynomial
 
    
Creates a new PolynomialGF2n of size deg and elem as coefficients.

Parameters:
deg - the maximum degree + 1
elem - a GF2nElement
 
     public GF2nPolynomial(int degGF2nElement elem)
     {
          = deg;
          = new GF2nElement[];
         for (int i = 0; i < i++)
         {
             [i] = (GF2nElement)elem.clone();
         }
     }

    
Creates a new PolynomialGF2n of size deg.

Parameters:
deg the maximum degree + 1
 
     private GF2nPolynomial(int deg)
     {
          = deg;
          = new GF2nElement[];
     }

    
Creates a new PolynomialGF2n by cloning the given PolynomialGF2n a.

Parameters:
a the PolynomialGF2n to clone
 
     public GF2nPolynomial(GF2nPolynomial a)
     {
         int i;
          = new GF2nElement[a.size];
          = a.size;
         for (i = 0; i < i++)
         {
             [i] = (GF2nElement)a.coeff[i].clone();
         }
     }

    
Creates a new PolynomialGF2n from the given Bitstring polynomial over the GF2nField B1.

Parameters:
polynomial the Bitstring to use
B1 the field
 
     public GF2nPolynomial(GF2Polynomial polynomialGF2nField B1)
     {
          = B1.getDegree() + 1;
          = new GF2nElement[];
         int i;
         if (B1 instanceof GF2nONBField)
         {
             for (i = 0; i < i++)
             {
                 if (polynomial.testBit(i))
                 {
                     [i] = GF2nONBElement.ONE((GF2nONBField)B1);
                 }
                 else
                 {
                     [i] = GF2nONBElement.ZERO((GF2nONBField)B1);
                 }
             }
         }
         else if (B1 instanceof GF2nPolynomialField)
         {
             for (i = 0; i < i++)
             {
                 if (polynomial.testBit(i))
                 {
                     [i] = GF2nPolynomialElement
                         .ONE((GF2nPolynomialField)B1);
                 }
                 else
                 {
                    [i] = GF2nPolynomialElement
                        .ZERO((GF2nPolynomialField)B1);
                }
            }
        }
        else
        {
            throw new IllegalArgumentException(
                "PolynomialGF2n(Bitstring, GF2nField): B1 must be "
                    + "an instance of GF2nONBField or GF2nPolynomialField!");
        }
    }
    public final void assignZeroToElements()
    {
        int i;
        for (i = 0; i < i++)
        {
            [i].assignZero();
        }
    }

    
Returns the size (=maximum degree + 1) of this PolynomialGF2n. This is not the degree, use getDegree instead.

Returns:
the size (=maximum degree + 1) of this PolynomialGF2n.
    public final int size()
    {
        return ;
    }

    
Returns the degree of this PolynomialGF2n.

Returns:
the degree of this PolynomialGF2n.
    public final int getDegree()
    {
        int i;
        for (i =  - 1; i >= 0; i--)
        {
            if (![i].isZero())
            {
                return i;
            }
        }
        return -1;
    }

    
Enlarges the size of this PolynomialGF2n to k + 1.

Parameters:
k the new maximum degree
    public final void enlarge(int k)
    {
        if (k <= )
        {
            return;
        }
        int i;
        GF2nElement[] res = new GF2nElement[k];
        System.arraycopy(, 0, res, 0, );
        GF2nField f = [0].getField();
        if ([0] instanceof GF2nPolynomialElement)
        {
            for (i = i < ki++)
            {
                res[i] = GF2nPolynomialElement.ZERO((GF2nPolynomialField)f);
            }
        }
        else if ([0] instanceof GF2nONBElement)
        {
            for (i = i < ki++)
            {
                res[i] = GF2nONBElement.ZERO((GF2nONBField)f);
            }
        }
         = k;
         = res;
    }
    public final void shrink()
    {
        int i =  - 1;
        while ([i].isZero() && (i > 0))
        {
            i--;
        }
        i++;
        if (i < )
        {
            GF2nElement[] res = new GF2nElement[i];
            System.arraycopy(, 0, res, 0, i);
             = res;
             = i;
        }
    }

    
Sets the coefficient at index to elem.

Parameters:
index the index
elem the GF2nElement to store as coefficient index
    public final void set(int indexGF2nElement elem)
    {
        if (!(elem instanceof GF2nPolynomialElement)
            && !(elem instanceof GF2nONBElement))
        {
            throw new IllegalArgumentException(
                "PolynomialGF2n.set f must be an "
                    + "instance of either GF2nPolynomialElement or GF2nONBElement!");
        }
        [index] = (GF2nElement)elem.clone();
    }

    
Returns the coefficient at index.

Parameters:
index the index
Returns:
the GF2nElement stored as coefficient index
    public final GF2nElement at(int index)
    {
        return [index];
    }

    
Returns true if all coefficients equal zero.

Returns:
true if all coefficients equal zero.
    public final boolean isZero()
    {
        int i;
        for (i = 0; i < i++)
        {
            if ([i] != null)
            {
                if (![i].isZero())
                {
                    return false;
                }
            }
        }
        return true;
    }
    public final boolean equals(Object other)
    {
        if (other == null || !(other instanceof GF2nPolynomial))
        {
            return false;
        }
        GF2nPolynomial otherPol = (GF2nPolynomial)other;
        if (getDegree() != otherPol.getDegree())
        {
            return false;
        }
        int i;
        for (i = 0; i < i++)
        {
            if (![i].equals(otherPol.coeff[i]))
            {
                return false;
            }
        }
        return true;
    }

    

Returns:
the hash code of this polynomial
    public int hashCode()
    {
        return getDegree() + .hashCode();
    }

    
Adds the PolynomialGF2n b to this and returns the result in a new PolynomialGF2n.

Parameters:
b - the PolynomialGF2n to add
Returns:
this + b
Throws:
DifferentFieldsException if this and b are not defined over the same field.
    public final GF2nPolynomial add(GF2nPolynomial b)
        throws RuntimeException
    {
        GF2nPolynomial result;
        if (size() >= b.size())
        {
            result = new GF2nPolynomial(size());
            int i;
            for (i = 0; i < b.size(); i++)
            {
                result.coeff[i] = (GF2nElement)[i].add(b.coeff[i]);
            }
            for (; i < size(); i++)
            {
                result.coeff[i] = [i];
            }
        }
        else
        {
            result = new GF2nPolynomial(b.size());
            int i;
            for (i = 0; i < size(); i++)
            {
                result.coeff[i] = (GF2nElement)[i].add(b.coeff[i]);
            }
            for (; i < b.size(); i++)
            {
                result.coeff[i] = b.coeff[i];
            }
        }
        return result;
    }

    
Multiplies the scalar s to each coefficient of this PolynomialGF2n and returns the result in a new PolynomialGF2n.

Parameters:
s the scalar to multiply
Returns:
this x s
Throws:
DifferentFieldsException if this and s are not defined over the same field.
    public final GF2nPolynomial scalarMultiply(GF2nElement s)
        throws RuntimeException
    {
        GF2nPolynomial result = new GF2nPolynomial(size());
        int i;
        for (i = 0; i < size(); i++)
        {
            result.coeff[i] = (GF2nElement)[i].multiply(s); // result[i]
            // =
            // a[i]*s
        }
        return result;
    }

    
Multiplies this by b and returns the result in a new PolynomialGF2n.

Parameters:
b the PolynomialGF2n to multiply
Returns:
this * b
Throws:
DifferentFieldsException if this and b are not defined over the same field.
    public final GF2nPolynomial multiply(GF2nPolynomial b)
        throws RuntimeException
    {
        int ij;
        int aDegree = size();
        int bDegree = b.size();
        if (aDegree != bDegree)
        {
            throw new IllegalArgumentException(
                "PolynomialGF2n.multiply: this and b must "
                    + "have the same size!");
        }
        GF2nPolynomial result = new GF2nPolynomial((aDegree << 1) - 1);
        for (i = 0; i < size(); i++)
        {
            for (j = 0; j < b.size(); j++)
            {
                if (result.coeff[i + j] == null)
                {
                    result.coeff[i + j] = (GF2nElement)[i]
                        .multiply(b.coeff[j]);
                }
                else
                {
                    result.coeff[i + j] = (GF2nElement)result.coeff[i + j]
                        .add([i].multiply(b.coeff[j]));
                }
            }
        }
        return result;
    }

    
Multiplies this by b, reduces the result by g and returns it in a new PolynomialGF2n.

Parameters:
b the PolynomialGF2n to multiply
g the modul
Returns:
this * b mod g
Throws:
DifferentFieldsException if this, b and g are not all defined over the same field.
                                                  GF2nPolynomial g)
        throws RuntimeException,
        ArithmeticException
    {
        return multiply(b).reduce(g);
    }

    
Reduces this by g and returns the result in a new PolynomialGF2n.

Parameters:
g - the modulus
Returns:
this % g
Throws:
DifferentFieldsException if this and g are not defined over the same field.
    public final GF2nPolynomial reduce(GF2nPolynomial g)
        throws RuntimeExceptionArithmeticException
    {
        return remainder(g); // return this % g
    }

    
Shifts left this by amount and stores the result in this PolynomialGF2n.

Parameters:
amount the amount to shift the coefficients
    public final void shiftThisLeft(int amount)
    {
        if (amount > 0)
        {
            int i;
            int oldSize = ;
            GF2nField f = [0].getField();
            enlarge( + amount);
            for (i = oldSize - 1; i >= 0; i--)
            {
                [i + amount] = [i];
            }
            if ([0] instanceof GF2nPolynomialElement)
            {
                for (i = amount - 1; i >= 0; i--)
                {
                    [i] = GF2nPolynomialElement
                        .ZERO((GF2nPolynomialField)f);
                }
            }
            else if ([0] instanceof GF2nONBElement)
            {
                for (i = amount - 1; i >= 0; i--)
                {
                    [i] = GF2nONBElement.ZERO((GF2nONBField)f);
                }
            }
        }
    }
    public final GF2nPolynomial shiftLeft(int amount)
    {
        if (amount <= 0)
        {
            return new GF2nPolynomial(this);
        }
        GF2nPolynomial result = new GF2nPolynomial( + amount[0]);
        result.assignZeroToElements();
        for (int i = 0; i < i++)
        {
            result.coeff[i + amount] = [i];
        }
        return result;
    }

    
Divides this by b and stores the result in a new PolynomialGF2n[2], quotient in result[0] and remainder in result[1].

Parameters:
b the divisor
Returns:
the quotient and remainder of this / b
Throws:
DifferentFieldsException if this and b are not defined over the same field.
    public final GF2nPolynomial[] divide(GF2nPolynomial b)
        throws RuntimeExceptionArithmeticException
    {
        GF2nPolynomial[] result = new GF2nPolynomial[2];
        GF2nPolynomial a = new GF2nPolynomial(this);
        a.shrink();
        GF2nPolynomial shift;
        GF2nElement factor;
        int bDegree = b.getDegree();
        GF2nElement inv = (GF2nElement)b.coeff[bDegree].invert();
        if (a.getDegree() < bDegree)
        {
            result[0] = new GF2nPolynomial(this);
            result[0].assignZeroToElements();
            result[0].shrink();
            result[1] = new GF2nPolynomial(this);
            result[1].shrink();
            return result;
        }
        result[0] = new GF2nPolynomial(this);
        result[0].assignZeroToElements();
        int i = a.getDegree() - bDegree;
        while (i >= 0)
        {
            factor = (GF2nElement)a.coeff[a.getDegree()].multiply(inv);
            shift = b.scalarMultiply(factor);
            shift.shiftThisLeft(i);
            a = a.add(shift);
            a.shrink();
            result[0].[i] = (GF2nElement)factor.clone();
            i = a.getDegree() - bDegree;
        }
        result[1] = a;
        result[0].shrink();
        return result;
    }

    
Divides this by b and stores the remainder in a new PolynomialGF2n.

Parameters:
b the divisor
Returns:
the remainder this % b
Throws:
DifferentFieldsException if this and b are not defined over the same field.
    public final GF2nPolynomial remainder(GF2nPolynomial b)
        throws RuntimeExceptionArithmeticException
    {
        GF2nPolynomial[] result = new GF2nPolynomial[2];
        result = divide(b);
        return result[1];
    }

    
Divides this by b and stores the quotient in a new PolynomialGF2n.

Parameters:
b the divisor
Returns:
the quotient this / b
Throws:
DifferentFieldsException if this and b are not defined over the same field.
    public final GF2nPolynomial quotient(GF2nPolynomial b)
        throws RuntimeExceptionArithmeticException
    {
        GF2nPolynomial[] result = new GF2nPolynomial[2];
        result = divide(b);
        return result[0];
    }

    
Computes the greatest common divisor of this and g and returns the result in a new PolynomialGF2n.

Parameters:
g - a GF2nPolynomial
Returns:
gcd(this, g)
Throws:
DifferentFieldsException if the coefficients of this and g use different fields
java.lang.ArithmeticException if coefficients are zero.
    public final GF2nPolynomial gcd(GF2nPolynomial g)
        throws RuntimeExceptionArithmeticException
    {
        GF2nPolynomial a = new GF2nPolynomial(this);
        GF2nPolynomial b = new GF2nPolynomial(g);
        a.shrink();
        b.shrink();
        GF2nPolynomial c;
        GF2nPolynomial result;
        GF2nElement alpha;
        while (!b.isZero())
        {
            c = a.remainder(b);
            a = b;
            b = c;
        }
        alpha = a.coeff[a.getDegree()];
        result = a.scalarMultiply((GF2nElement)alpha.invert());
        return result;
    }
New to GrepCode? Check out our FAQ X