F1Field Represents a finite field of order 'order' providing arithmetic operations under modulus. This class includes operations such as addition, subtraction, multiplication, division, and inversion, all performed modulo the field's order. It's designed to work with bigints, supporting large numbers for cryptographic purposes and other applications requiring modular arithmetic. Note that the outputs of the functions will always be within the field if and only if the input values are within the field. Devs need to make sure of that.

Constructors

Properties

_half: bigint

Half the order of the field, used for certain comparisons.

_negone: bigint

The scalar value -1 in the field, represented positively.

_order: bigint

The order of the finite field (i.e., the modulus).

one: bigint = ...

Represents the scalar value 1 in the field.

zero: bigint = ...

Represents the scalar value 0 in the field.

Methods

  • Adds two bigint values together under modulus. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The first value.

    • b: bigint

      The second value.

    Returns bigint

    The sum of 'a' and 'b' modulo the field's order.

  • Divides one bigint by another within the field by multiplying the first value by the multiplicative inverse of the second.

    Parameters

    • a: bigint

      The dividend.

    • b: bigint

      The divisor.

    Returns bigint

    The result of the division of 'a' by 'b' modulo the field's order.

  • Ensures a given result falls within the field by applying modular reduction. This method also handles negative inputs, correctly mapping them into the field.

    Parameters

    • res: bigint

      The result to be normalized to the field.

    Returns bigint

    The equivalent value within the field.

  • Checks if two bigint values are equal within the context of the field. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The first value to compare.

    • b: bigint

      The second value to compare.

    Returns boolean

    True if 'a' equals 'b', false otherwise.

  • Compares two bigint values to determine if the first is greater than or equal to the second, considering the field's modular context. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The first value to compare.

    • b: bigint

      The second value to compare.

    Returns boolean

    True if 'a' is greater than or equal to 'b', false otherwise.

  • Computes the multiplicative inverse of a given value within the field. This method uses the Extended Euclidean Algorithm to find the inverse, ensuring the result is always a positive value less than the field's order. If the input value is zero, which has no inverse, an error is thrown.

    Parameters

    • a: bigint

      The value for which to compute the inverse.

    Returns bigint

    The multiplicative inverse of 'a' modulo the field's order.

    Throws

    if 'a' is zero.

  • Checks if a bigint value is zero within the context of the field.

    Parameters

    • a: bigint

      The value to check.

    Returns boolean

    True if 'a' is zero, false otherwise.

  • Compares two bigint values to determine if the first is less than the second, taking into account the field's order for modular comparison. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The first value to compare.

    • b: bigint

      The second value to compare.

    Returns boolean

    True if 'a' is less than 'b', false otherwise.

  • Performs modular multiplication of two bigint values within the field.

    Parameters

    • a: bigint

      The first value.

    • b: bigint

      The second value.

    Returns bigint

    The product of 'a' and 'b' modulo the field's order.

  • Computes the negation of a bigint value within the field. The result is the modular additive inverse that, when added to the original value, yields zero in the field's modulus. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The value to negate.

    Returns bigint

    The negation of 'a' modulo the field's order.

  • Raises a base to an exponent within the field, efficiently computing scalar exponentiation using the square-and-multiply algorithm. Supports both positive and negative exponents through the use of the inv method for negatives.

    Parameters

    • base: bigint

      The base to be exponentiated.

    • e: bigint

      The exponent.

    Returns bigint

    The result of raising 'base' to the power 'e' modulo the field's order.

  • Squares a bigint value within the field. This is a specific case of multiplication where the value is multiplied by itself, optimized for performance where applicable. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The value to square.

    Returns bigint

    The square of 'a' modulo the field's order.

  • Subtracts one bigint from another under modulus. It ensures the result is within the field if and only if the input values are within the field.

    Parameters

    • a: bigint

      The value from which to subtract.

    • b: bigint

      The value to be subtracted.

    Returns bigint

    The difference of 'a' and 'b' modulo the field's order.