Keelung
Website
  • Hello, Keelung
  • Getting Started
    • Getting Started
    • Keelung Basics
  • Language Manual
    • Values & Expressions
      • Field elements
      • Booleans
      • Unsigned Integers
      • Built-in Parameterized Types
      • Advanced: Custom Types & Generic Instances
    • Statements
      • Input / Output
      • Assertions
      • Reusing existing computation
      • Mutable Arrays
    • Commands
    • Supported Fields
  • How-to Guides
    • Installation
    • Troubleshooting
    • Starting a Keelung project from scratch
  • Examples
    • Building a zkSNARK Application with Keelung: Merkle tree membership
    • Poseidon Hash Function
    • Sudoku
  • Downloads
    • Binaries
  • Integrations with Snarkjs
    • Generate Snakrjs-compatible R1CS and witness
Powered by GitBook
LogoLogo

Copyright © 2023 BTQ

On this page
  • Construction
  • Conversion
  • From Haskell Integer
  • From Haskell Rational
  • From Keelung Booleans
  • Functions
  • Addition
  • Subtraction
  • Multiplication
  • Fractional division (binary)
  • Reciprocal division (unary)
  • Unary negation
  • Absolute value
  • Sign of number
  • Exponentiation
  • Equality
  • Inequality
  1. Language Manual
  2. Values & Expressions

Field elements

PreviousValues & ExpressionsNextBooleans

Last updated 1 year ago

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

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

fromInteger :: Integer -> Field

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

fromRational :: Rational -> Field

Example usage: fromRational 3.5

BtoF converts to Booleans to field elements:

BtoF :: Boolean -> Field

Functions

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

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

neq :: Field -> Field -> Boolean

neq is implemented as such under the hood:

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

From Haskell

From Keelung

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

Returns when two field elements are the same:

Returns when two numbers are the same:

Integer
Rational
Booleans
Num
Fractional
true
false