java.lang.Object
net.i2p.crypto.eddsa.math.FieldElement
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BigIntegerFieldElement, Ed25519FieldElement

public abstract class FieldElementextends Object implements Serializable
Represents an element in a finite field of order $2^{b}-3$. Note: concrete subclasses must implement hashCode() and equals().
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Pinned for serialized-form compatibility.
      See Also:
    • f

      protected final Field f
      The field this element belongs to.
  • Constructor Details

    • FieldElement

      public FieldElement(Field f)
      Creates a new field element in the specified field.
      Parameters:
      f - the field this element belongs to
      Throws:
      IllegalArgumentException - if f is null
  • Method Details

    • toByteArray

      public byte[] toByteArray()
      Encode a FieldElement in its $(b-1)$-bit encoding.
      Returns:
      the $(b-1)$-bit encoding of this FieldElement
    • isNonZero

      public abstract boolean isNonZero()
      Checks if this field element is non-zero.
      Returns:
      true if this element is not equal to zero, false otherwise
    • isNegative

      public boolean isNegative()
      Checks if this field element is negative. Negative is defined as the least significant bit being 1.
      Returns:
      true if this element is negative, false otherwise
    • add

      public abstract FieldElement add(FieldElement val)
      Adds another field element to this one.
      Parameters:
      val - the field element to add
      Returns:
      the sum of this element and val
    • addOne

      public FieldElement addOne()
      Adds one to this field element.
      Returns:
      this element plus one
    • subtract

      public abstract FieldElement subtract(FieldElement val)
      Subtracts another field element from this one.
      Parameters:
      val - the field element to subtract
      Returns:
      the difference of this element and val
    • subtractOne

      public FieldElement subtractOne()
      Subtracts one from this field element.
      Returns:
      this element minus one
    • negate

      public abstract FieldElement negate()
      Negates this field element.
      Returns:
      the negation of this element
    • divide

      public FieldElement divide(FieldElement val)
      Divides this field element by another one.
      Parameters:
      val - the divisor; it must be non-zero, since it is inverted
      Returns:
      this element divided by val
    • multiply

      public abstract FieldElement multiply(FieldElement val)
      Multiplies this field element by another one.
      Parameters:
      val - the field element to multiply by; implementations cast it to their own type
      Returns:
      the product of this element and val
    • square

      public abstract FieldElement square()
      Squares this field element.
      Returns:
      this element squared
    • squareAndDouble

      public abstract FieldElement squareAndDouble()
      Squares this field element and doubles the result.
      Returns:
      (this element squared) * 2
    • invert

      public abstract FieldElement invert()
      Computes the multiplicative inverse of this field element.
      Returns:
      the multiplicative inverse of this element
      Throws:
      ArithmeticException - if this element is zero
    • pow22523

      public abstract FieldElement pow22523()
      Raises this field element to the power of $2^{252} - 3$.
      Returns:
      this element raised to the power of $2^{252} - 3$
    • cmov

      public abstract FieldElement cmov(FieldElement val, int b)
      Conditional move - sets this element to val if b is 1, otherwise leaves it unchanged.
      Parameters:
      val - the field element to potentially assign
      b - the condition: 1 means assign val, 0 means keep this element
      Returns:
      this element, potentially modified