Skip to main content

Class: Field

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Field(x)

Coerces anything field-like to a Field.

Parameters

NameType
xstring | number | bigint | boolean | Field

Defined in

snarky.d.ts:53

Properties

ORDER

Static ORDER: bigint

The field order as a bigint.

Defined in

snarky.d.ts:314


minusOne

Static minusOne: Field

Deprecated

Static constant values on Field are deprecated in favor of using the constructor Field(-1).

The number -1 as a [[Field]].

Defined in

snarky.d.ts:310


one

Static one: Field

Deprecated

Static constant values on Field are deprecated in favor of using the constructor Field(1).

The number 1 as a [[Field]].

Defined in

snarky.d.ts:298


zero

Static zero: Field

Deprecated

Static constant values on Field are deprecated in favor of using the constructor Field(0).

The number 0 as a [[Field]].

Defined in

snarky.d.ts:304

Methods

add

add(y): Field

Adds this Field element to another to a Field element.

let a = Field(3);
let sum = a.add(5)

Parameters

NameType
ystring | number | boolean | Field

Returns

Field

Defined in

snarky.d.ts:86


assertBoolean

assertBoolean(message?): void

Assert that this Field is either 0 or 1.

Field(0).assertBoolean();

This function can only be called inside a checked computation, like a SmartContract method, and throws an error if the assertion fails.

Parameters

NameType
message?string

Returns

void

Defined in

snarky.d.ts:251


assertEquals

assertEquals(y, message?): void

Assert that this Field equals another Field-like value. Throws an error if the assertion fails.

Field(1).assertEquals(1);

Parameters

NameType
ystring | number | boolean | Field
message?string

Returns

void

Defined in

snarky.d.ts:240


assertGt

assertGt(y, message?): void

Assert that this Field is greater than another Field-like value.

Field(1).assertGt(0);

This function can only be called inside a checked computation, like a SmartContract method, and causes it to fail if the assertion fails.

Parameters

NameType
ystring | number | boolean | Field
message?string

Returns

void

Defined in

snarky.d.ts:219


assertGte

assertGte(y, message?): void

Assert that this Field is greater than or equal to another Field-like value.

Field(1).assertGte(0);

This function can only be called inside a checked computation, like a SmartContract method, and causes it to fail if the assertion fails.

Parameters

NameType
ystring | number | boolean | Field
message?string

Returns

void

Defined in

snarky.d.ts:230


assertLt

assertLt(y, message?): void

Assert that this Field is lower than another Field-like value.

Field(1).assertLt(2);

This function can only be called inside a checked computation, like a SmartContract method, and causes it to fail if the assertion fails.

Parameters

NameType
ystring | number | boolean | Field
message?string

Returns

void

Defined in

snarky.d.ts:197


assertLte

assertLte(y, message?): void

Assert that this Field is lower than or equal to another Field-like value.

Field(1).assertLte(2);

This function can only be called inside a checked computation, like a SmartContract method, and causes it to fail if the assertion fails.

Parameters

NameType
ystring | number | boolean | Field
message?string

Returns

void

Defined in

snarky.d.ts:208


div

div(y): Field

Divides this Field element through another coercible to a field.

Parameters

NameType
ystring | number | boolean | Field

Returns

Field

Defined in

snarky.d.ts:101


equals

equals(y): Bool

Check if this Field equals another Field-like value. Returns a Bool.

Field(2).equals(2); // Bool(true)

Parameters

NameType
ystring | number | boolean | Field

Returns

Bool

Defined in

snarky.d.ts:273


fromFields

fromFields(fields): Field

Creates a data structure from an array of serialized Field elements.

Parameters

NameType
fieldsField[]

Returns

Field

Defined in

snarky.d.ts:351


gt

gt(y): Bool

Check if this Field is greater than another Field-like value. Returns a Bool.

Field(2).gt(1); // Bool(true)

Parameters

NameType
ystring | number | boolean | Field

Returns

Bool

Defined in

snarky.d.ts:175


gte

gte(y): Bool

Check if this Field is greater than or equal to another Field-like value. Returns a Bool.

Field(2).gte(1); // Bool(true)

Parameters

NameType
ystring | number | boolean | Field

Returns

Bool

Defined in

snarky.d.ts:184


inv

inv(): Field

Inverts this Field element.

const invX = x.inv();
invX.assertEquals(Field(1).div(x));

Returns

Field

A Field element that is equivalent to one divided by this element.

Defined in

snarky.d.ts:76


isConstant

isConstant(): boolean

Checks whether this is a hard-coded constant in the Circuit.

Returns

boolean

Defined in

snarky.d.ts:283


isZero

isZero(): Bool

Returns

Bool

Defined in

snarky.d.ts:252


lt

lt(y): Bool

Check if this Field is lower than another Field-like value. Returns a Bool.

Field(2).lt(3); // Bool(true)

Parameters

NameType
ystring | number | boolean | Field

Returns

Bool

Defined in

snarky.d.ts:157


lte

lte(y): Bool

Check if this Field is lower than or equal to another Field-like value. Returns a Bool.

Field(2).lte(3); // Bool(true)

Parameters

NameType
ystring | number | boolean | Field

Returns

Bool

Defined in

snarky.d.ts:166


mul

mul(y): Field

Multiplies this Field element with another coercible to a field.

Parameters

NameType
ystring | number | boolean | Field

Returns

Field

Defined in

snarky.d.ts:96


neg

neg(): Field

Negates this Field. This is equivalent to multiplying the Field by -1.

const negOne = Field(1).neg();
negOne.assertEquals(-1);

Returns

Field

Defined in

snarky.d.ts:64


rangeCheckHelper

rangeCheckHelper(numBits): Field

Parameters

NameType
numBitsnumber

Returns

Field

Defined in

snarky.d.ts:278


seal

seal(): Field

Returns

Field

Defined in

snarky.d.ts:276


sizeInFields

sizeInFields(): number

Returns the size of this type.

Returns

number

Defined in

snarky.d.ts:141


sqrt

sqrt(): Field

Square roots this Field element.

x.square().sqrt().assertEquals(x);

Returns

Field

Defined in

snarky.d.ts:120


square

square(): Field

Squares this Field element.

const x2 = x.square();
x2.assertEquals(x.mul(x));

Returns

Field

Defined in

snarky.d.ts:111


sub

sub(y): Field

Subtracts another Field-like element from this one.

Parameters

NameType
ystring | number | boolean | Field

Returns

Field

Defined in

snarky.d.ts:91


toBigInt

toBigInt(): bigint

Serialize this instance of a Field to a bigint. This operation does NOT affect the circuit and can't be used to prove anything about the bigint representation of the Field.

Returns

bigint

Defined in

snarky.d.ts:131


toBits

toBits(): Bool[]

Little endian binary representation of the field element.

Returns

Bool[]

Defined in

snarky.d.ts:257

toBits(length): Bool[]

Little endian binary representation of the field element. Fails if the field element cannot fit in length bits.

Parameters

NameType
lengthnumber

Returns

Bool[]

Defined in

snarky.d.ts:263


toConstant

toConstant(): Field

Returns a constant.

Returns

Field

Defined in

snarky.d.ts:288


toFields

toFields(): Field[]

Serializes this data structure into Field elements.

Returns

Field[]

Defined in

snarky.d.ts:146


toJSON

toJSON(): string

Serialize this instance of a Field to a JSON string. This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.

Returns

string

Defined in

snarky.d.ts:136


toString

toString(): string

Serialize the Field to a string, e.g. for printing. This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.

Returns

string

Defined in

snarky.d.ts:126


check

Static check(x): void

Parameters

NameType
xField

Returns

void

Defined in

snarky.d.ts:409


fromBits

Static fromBits(x): Field

Converts a bit array into a field element (little endian) Fails if the field element cannot fit given too many bits.

Parameters

NameType
x(boolean | Bool)[]

Returns

Field

Defined in

snarky.d.ts:385


fromBytes

Static fromBytes(bytes): Field

Parameters

NameType
bytesnumber[]

Returns

Field

Defined in

snarky.d.ts:414


fromFields

Static fromFields(fields): Field

Creates a data structure from an array of serialized Field elements.

Parameters

NameType
fieldsField[]

Returns

Field

Defined in

snarky.d.ts:356


fromJSON

Static fromJSON(x): Field

Deserialize a JSON structure into a Field. This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.

Parameters

NameType
xstring

Returns

Field

Defined in

snarky.d.ts:407


random

Static random(): Field

A random field element.

Returns

Field

Defined in

snarky.d.ts:319


sizeInBytes

Static sizeInBytes(): number

Returns

number

Defined in

snarky.d.ts:415


sizeInFields

Static sizeInFields(): number

Returns the size of this type.

Returns

number

Defined in

snarky.d.ts:361


toAuxiliary

Static toAuxiliary(x?): []

Static method to serialize a Field into its auxiliary data.

Parameters

NameType
x?Field

Returns

[]

Defined in

snarky.d.ts:370


toBytes

Static toBytes(x): number[]

Parameters

NameType
xField

Returns

number[]

Defined in

snarky.d.ts:413


toFields

Static toFields(x): Field[]

Static method to serialize a Field into an array of Field elements.

Parameters

NameType
xField

Returns

Field[]

Defined in

snarky.d.ts:366


toInput

Static toInput(x): Object

Parameters

NameType
xField

Returns

Object

NameType
fieldsField[]

Defined in

snarky.d.ts:412


toJSON

Static toJSON(x): string

Serialize a Field to a JSON string. This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.

Parameters

NameType
xField

Returns

string

Defined in

snarky.d.ts:401