Field elements

Field element is the most fundamental datatype in Keelung.

They can be constructed with numeric literals like 3 , 42, or -7.

three :: Field
three = 3

minusSeven :: Field
minusSeven = (-7)

Negative numbers don't really exist in finite fields, they will be normalized to their positive counterparts after compilation. For example, -7 would become:

1552511030102430251236801561344621993261920897571225594

in GF181 where there are only 1552511030102430251236801561344621993261920897571225601 numbers.

Construction

Field elements are constructed with the following constructor under the hood:

Field :: Integer -> Field

Conversion

From Haskell Integer

A numeric literal represents the application of the function fromInteger to the appropriate value of type Integer.

fromInteger :: Integer -> Field

From Haskell Rational

A floating literal stands for an application of fromRational to a value of type Rational.

fromRational :: Rational -> Field

Example usage: fromRational 3.5

From Keelung Booleans

BtoF converts to Booleans to field elements:

BtoF :: Boolean -> Field

Functions

Since Field is an instance of Num and Fractional, all methods of Num and Fractional works on Numbers.

three :: Field
three = negate 7 + (7 * 4 / fromInteger 2) - 4

Addition

(+) :: Field -> Field -> Field

Example usage: 3 + 4

Subtraction

(-) :: Field -> Field -> Field

Example usage: 3 - 4

Multiplication

(*) :: Field -> Field -> Field

Example usage: 3 * 4

Fractional division (binary)

(/) :: Field -> Field -> Field

Example usage: 3 / 4

Reciprocal division (unary)

recip :: Field -> Field

Example usage: recip 4

Unary negation

negate :: Field -> Field

Example usage: negate 4

Absolute value

Since there are no negative numbers in Field, the abs function has the same effect as the id function.

abs :: Field -> Field

Sign of number

signum would always return 1 on any field element.

signum :: Field -> Field

Exponentiation

pow takes a Field as the base and an Integer as the power, and computes the exponentiation.

pow :: Field -> Integer -> Field

If you are multiplying the same stuff for more than 3 times, for example:

x * x * x * x

You should consider replacing it with:

x `pow` 4

To allow the exponentiation by squaring optimization to kick in.

Equality

Returns true when two field elements are the same:

eq :: Field -> Field -> Boolean

Example usage: false `eq` false or eq false true.

You can also use EqF , which is the underlying implementation of eq on field elements.

Inequality

Returns false when two numbers are the same:

neq :: Field -> Field -> Boolean

neq is implemented as such under the hood:

neq x y = Not (x `eq` y)

Last updated

Logo

Copyright © 2023 BTQ