net.i2p.crypto.eddsa.math

## Class FieldElement

- java.lang.Object
- - net.i2p.crypto.eddsa.math.FieldElement

- All Implemented Interfaces:
    Serializable
  Direct Known Subclasses:
    BigIntegerFieldElement, Ed25519FieldElement

      public abstract class FieldElement
      extends 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:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Field` | `f` The field this element belongs to

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FieldElement(Field f)` Creates a new field element in the specified field.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `abstract FieldElement` | `add(FieldElement val)` Adds another field element to this one.
    `FieldElement` | `addOne()` Adds one to this field element.
    `abstract FieldElement` | `cmov(FieldElement val, int b)` Conditional move - sets this element to val if b is 1, otherwise leaves it unchanged.
    `FieldElement` | `divide(FieldElement val)` Divides this field element by another one.
    `abstract FieldElement` | `invert()` Computes the multiplicative inverse of this field element.
    `boolean` | `isNegative()` Checks if this field element is negative.
    `abstract boolean` | `isNonZero()` Checks if this field element is non-zero.
    `abstract FieldElement` | `multiply(FieldElement val)` Multiplies this field element by another one.
    `abstract FieldElement` | `negate()` Negates this field element.
    `abstract FieldElement` | `pow22523()` Raises this field element to the power of $2^{252} - 3$.
    `abstract FieldElement` | `square()` Squares this field element.
    `abstract FieldElement` | `squareAndDouble()` Squares this field element and doubles the result.
    `abstract FieldElement` | `subtract(FieldElement val)` Subtracts another field element from this one.
    `FieldElement` | `subtractOne()` Subtracts one from this field element.
    `byte[]` | `toByteArray()` Encode a FieldElement in its $(b-1)$-bit encoding.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### f

          protected final Field f

      The field this element belongs to

  - ### Constructor Detail

    - #### 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 Detail

    - #### 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

    - #### 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

    - #### divide

          public FieldElement divide(FieldElement val)

      Divides this field element by another one.
      Parameters:
        `val` - the divisor
      Returns:
        this element divided by val

    - #### 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

    - #### 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

    - #### 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

    - #### multiply

          public abstract FieldElement multiply(FieldElement val)

      Multiplies this field element by another one.
      Parameters:
        `val` - the multiplier
      Returns:
        the product of this element and val

    - #### negate

          public abstract FieldElement negate()

      Negates this field element.
      Returns:
        the negation of this element

    - #### 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$

    - #### 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

    - #### 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

    - #### toByteArray

          public byte[] toByteArray()

      Encode a FieldElement in its $(b-1)$-bit encoding.
      Returns:
        the $(b-1)$-bit encoding of this FieldElement