Class GF2nPolynomialElement

java.lang.Object
org.bouncycastle.pqc.legacy.math.linearalgebra.GF2nElement
org.bouncycastle.pqc.legacy.math.linearalgebra.GF2nPolynomialElement
All Implemented Interfaces:
GFElement

public class GF2nPolynomialElement extends GF2nElement
This class implements elements of finite binary fields GF(2n) using polynomial representation. For more information on the arithmetic see for example IEEE Standard 1363 or Certicom online-tutorial.
See Also:
  • Constructor Details

    • GF2nPolynomialElement

      public GF2nPolynomialElement(GF2nPolynomialField f, Random rand)
      Create a new random GF2nPolynomialElement using the given field and source of randomness.
      Parameters:
      f - the GF2nField to use
      rand - the source of randomness
    • GF2nPolynomialElement

      public GF2nPolynomialElement(GF2nPolynomialField f, GF2Polynomial bs)
      Creates a new GF2nPolynomialElement using the given field and Bitstring.
      Parameters:
      f - the GF2nPolynomialField to use
      bs - the desired value as Bitstring
    • GF2nPolynomialElement

      public GF2nPolynomialElement(GF2nPolynomialField f, byte[] os)
      Creates a new GF2nPolynomialElement using the given field f and byte[] os as value. The conversion is done according to 1363.
      Parameters:
      f - the GF2nField to use
      os - the octet string to assign to this GF2nPolynomialElement
      See Also:
      • "P1363 5.5.5 p23, OS2FEP/OS2BSP"
    • GF2nPolynomialElement

      public GF2nPolynomialElement(GF2nPolynomialField f, int[] is)
      Creates a new GF2nPolynomialElement using the given field f and int[] is as value.
      Parameters:
      f - the GF2nField to use
      is - the integer string to assign to this GF2nPolynomialElement
    • GF2nPolynomialElement

      public GF2nPolynomialElement(GF2nPolynomialElement other)
      Creates a new GF2nPolynomialElement by cloning the given GF2nPolynomialElement b.
      Parameters:
      other - the GF2nPolynomialElement to clone
  • Method Details

    • clone

      public Object clone()
      Creates a new GF2nPolynomialElement by cloning this GF2nPolynomialElement.
      Specified by:
      clone in interface GFElement
      Specified by:
      clone in class GF2nElement
      Returns:
      a copy of this element
    • ZERO

      Create the zero element.
      Parameters:
      f - the finite field
      Returns:
      the zero element in the given finite field
    • ONE

      Create the one element.
      Parameters:
      f - the finite field
      Returns:
      the one element in the given finite field
    • isZero

      public boolean isZero()
      Checks whether this element is zero.
      Returns:
      true if this is the zero element
    • isOne

      public boolean isOne()
      Tests if the GF2nPolynomialElement has 'one' as value.
      Returns:
      true if this equals one (this == 1)
    • equals

      public boolean equals(Object other)
      Compare this element with another object.
      Specified by:
      equals in interface GFElement
      Overrides:
      equals in class Object
      Parameters:
      other - the other object
      Returns:
      true if the two objects are equal, false otherwise
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface GFElement
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this element
    • testRightmostBit

      public boolean testRightmostBit()
      Returns whether the rightmost bit of the bit representation is set. This is needed for data conversion according to 1363.
      Specified by:
      testRightmostBit in class GF2nElement
      Returns:
      true if the rightmost bit of this element is set
    • add

      public GFElement add(GFElement addend) throws RuntimeException
      Compute the sum of this element and addend.
      Parameters:
      addend - the addend
      Returns:
      this + other (newly created)
      Throws:
      RuntimeException
    • addToThis

      public void addToThis(GFElement addend) throws RuntimeException
      Compute this + addend (overwrite this).
      Parameters:
      addend - the addend
      Throws:
      RuntimeException
    • increase

      public GF2nElement increase()
      Returns this element + 'one".
      Specified by:
      increase in class GF2nElement
      Returns:
      this + 'one'
    • increaseThis

      public void increaseThis()
      Increases this element by 'one'.
      Specified by:
      increaseThis in class GF2nElement
    • multiply

      public GFElement multiply(GFElement factor) throws RuntimeException
      Compute the product of this element and factor.
      Parameters:
      factor - the factor
      Returns:
      this * factor (newly created)
      Throws:
      RuntimeException
    • multiplyThisBy

      public void multiplyThisBy(GFElement factor) throws RuntimeException
      Compute this * factor (overwrite this).
      Parameters:
      factor - the factor
      Throws:
      RuntimeException
    • invert

      public GFElement invert() throws ArithmeticException
      Compute the multiplicative inverse of this element.
      Returns:
      this-1 (newly created)
      Throws:
      ArithmeticException - if this is the zero element.
      See Also:
    • invertEEA

      public GF2nPolynomialElement invertEEA() throws ArithmeticException
      Calculates the multiplicative inverse of this and returns the result in a new GF2nPolynomialElement.
      Returns:
      this^(-1)
      Throws:
      ArithmeticException - if this equals zero
    • invertSquare

      public GF2nPolynomialElement invertSquare() throws ArithmeticException
      Calculates the multiplicative inverse of this and returns the result in a new GF2nPolynomialElement.
      Returns:
      this^(-1)
      Throws:
      ArithmeticException - if this equals zero
    • invertMAIA

      public GF2nPolynomialElement invertMAIA() throws ArithmeticException
      Calculates the multiplicative inverse of this using the modified almost inverse algorithm and returns the result in a new GF2nPolynomialElement.
      Returns:
      this^(-1)
      Throws:
      ArithmeticException - if this equals zero
    • square

      public GF2nElement square()
      This method is used internally to map the square()-calls within GF2nPolynomialElement to one of the possible squaring methods.
      Specified by:
      square in class GF2nElement
      Returns:
      this2 (newly created)
      See Also:
    • squareThis

      public void squareThis()
      This method is used internally to map the square()-calls within GF2nPolynomialElement to one of the possible squaring methods.
      Specified by:
      squareThis in class GF2nElement
    • squareMatrix

      public GF2nPolynomialElement squareMatrix()
      Squares this GF2nPolynomialElement using GF2nField's squaring matrix. This is supposed to be fast when using a polynomial (no tri- or pentanomial) as fieldpolynomial. Use squarePreCalc when using a tri- or pentanomial as fieldpolynomial instead.
      Returns:
      this2 (newly created)
      See Also:
    • squareThisMatrix

      public void squareThisMatrix()
      Squares this GF2nPolynomialElement using GF2nFields squaring matrix. This is supposed to be fast when using a polynomial (no tri- or pentanomial) as fieldpolynomial. Use squarePreCalc when using a tri- or pentanomial as fieldpolynomial instead.
      See Also:
    • squareBitwise

      public GF2nPolynomialElement squareBitwise()
      Squares this GF2nPolynomialElement by shifting left its Bitstring and reducing. This is supposed to be the slowest method. Use squarePreCalc or squareMatrix instead.
      Returns:
      this2 (newly created)
      See Also:
    • squareThisBitwise

      public void squareThisBitwise()
      Squares this GF2nPolynomialElement by shifting left its Bitstring and reducing. This is supposed to be the slowest method. Use squarePreCalc or squareMatrix instead.
      See Also:
    • squarePreCalc

      public GF2nPolynomialElement squarePreCalc()
      Squares this GF2nPolynomialElement by using precalculated values and reducing. This is supposed to de fastest when using a trinomial or pentanomial as field polynomial. Use squareMatrix when using a ordinary polynomial as field polynomial.
      Returns:
      this2 (newly created)
      See Also:
    • squareThisPreCalc

      public void squareThisPreCalc()
      Squares this GF2nPolynomialElement by using precalculated values and reducing. This is supposed to de fastest when using a tri- or pentanomial as fieldpolynomial. Use squareMatrix when using a ordinary polynomial as fieldpolynomial.
      See Also:
    • power

      public GF2nPolynomialElement power(int k)
      Calculates this to the power of k and returns the result in a new GF2nPolynomialElement.
      Parameters:
      k - the power
      Returns:
      this^k in a new GF2nPolynomialElement
    • squareRoot

      public GF2nElement squareRoot()
      Compute the square root of this element and return the result in a new GF2nPolynomialElement.
      Specified by:
      squareRoot in class GF2nElement
      Returns:
      this1/2 (newly created)
    • squareRootThis

      public void squareRootThis()
      Compute the square root of this element.
      Specified by:
      squareRootThis in class GF2nElement
    • solveQuadraticEquation

      public GF2nElement solveQuadraticEquation() throws RuntimeException
      Solves the quadratic equation z2 + z = this if such a solution exists. This method returns one of the two possible solutions. The other solution is z + 1. Use z.increase() to compute this solution.
      Specified by:
      solveQuadraticEquation in class GF2nElement
      Returns:
      a GF2nPolynomialElement representing one z satisfying the equation z2 + z = this
      Throws:
      RuntimeException
      See Also:
      • "IEEE 1363, Annex A.4.7"
    • trace

      public int trace()
      Returns the trace of this GF2nPolynomialElement.
      Specified by:
      trace in class GF2nElement
      Returns:
      the trace of this GF2nPolynomialElement
    • toString

      public String toString()
      Returns a string representing this Bitstrings value using hexadecimal radix in MSB-first order.
      Specified by:
      toString in interface GFElement
      Overrides:
      toString in class Object
      Returns:
      a String representing this Bitstrings value.
    • toString

      public String toString(int radix)
      Returns a string representing this Bitstrings value using hexadecimal or binary radix in MSB-first order.
      Parameters:
      radix - the radix to use (2 or 16, otherwise 2 is used)
      Returns:
      a String representing this Bitstrings value.
    • toByteArray

      public byte[] toByteArray()
      Converts this GF2nPolynomialElement to a byte[] according to 1363.
      Returns:
      a byte[] representing the value of this GF2nPolynomialElement
      See Also:
      • "P1363 5.5.2 p22f BS2OSP, FE2OSP"
    • toFlexiBigInt

      public BigInteger toFlexiBigInt()
      Converts this GF2nPolynomialElement to an integer according to 1363.
      Returns:
      a BigInteger representing the value of this GF2nPolynomialElement
      See Also:
      • "P1363 5.5.1 p22 BS2IP"