Skip to main content

CanonicalForeignField

o1js / Exports / CanonicalForeignField

Class: CanonicalForeignField

Hierarchy

  • ForeignFieldWithMul

    CanonicalForeignField

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new CanonicalForeignField(x)

Parameters

NameType
xstring | number | bigint | Field3 | CanonicalForeignField

Overrides

ForeignFieldWithMul.constructor

Defined in

lib/foreign-field.ts:505

Properties

type

type: "FullyReduced"

Defined in

lib/foreign-field.ts:503


value

value: Field3

The internal representation of a foreign field element, as a tuple of 3 limbs.

Inherited from

ForeignFieldWithMul.value

Defined in

lib/foreign-field.ts:39


_modulus

Static _modulus: undefined | bigint = undefined

Inherited from

ForeignFieldWithMul._modulus

Defined in

lib/foreign-field.ts:22


_provable

Static _provable: undefined | ProvablePure<CanonicalForeignField> = undefined

Overrides

ForeignFieldWithMul._provable

Defined in

lib/foreign-field.ts:509


_variants

Static _variants: undefined | { almostReduced: typeof AlmostForeignField ; canonical: typeof CanonicalForeignField ; unreduced: typeof UnreducedForeignField } = undefined

Sibling classes that represent different ranges of field elements.

Inherited from

ForeignFieldWithMul._variants

Defined in

lib/foreign-field.ts:48

Accessors

Constructor

get Constructor(): typeof ForeignField

Returns

typeof ForeignField

Inherited from

ForeignFieldWithMul.Constructor

Defined in

lib/foreign-field.ts:41


modulus

get modulus(): bigint

Returns

bigint

Inherited from

ForeignFieldWithMul.modulus

Defined in

lib/foreign-field.ts:29


AlmostReduced

Static get AlmostReduced(): typeof AlmostForeignField

Constructor for field elements that are "almost reduced", i.e. lie in the range [0, 2^ceil(log2(p))).

Returns

typeof AlmostForeignField

Inherited from

ForeignFieldWithMul.AlmostReduced

Defined in

lib/foreign-field.ts:66


Canonical

Static get Canonical(): typeof CanonicalForeignField

Constructor for field elements that are fully reduced, i.e. lie in the range [0, p).

Returns

typeof CanonicalForeignField

Inherited from

ForeignFieldWithMul.Canonical

Defined in

lib/foreign-field.ts:73


Unreduced

Static get Unreduced(): typeof UnreducedForeignField

Constructor for unreduced field elements.

Returns

typeof UnreducedForeignField

Inherited from

ForeignFieldWithMul.Unreduced

Defined in

lib/foreign-field.ts:59


modulus

Static get modulus(): bigint

Returns

bigint

Inherited from

ForeignFieldWithMul.modulus

Defined in

lib/foreign-field.ts:25


provable

Static get provable(): ProvablePure<CanonicalForeignField>

Returns

ProvablePure<CanonicalForeignField>

Overrides

ForeignFieldWithMul.provable

Defined in

lib/foreign-field.ts:510


sizeInBits

Static get sizeInBits(): number

Returns

number

Inherited from

ForeignFieldWithMul.sizeInBits

Defined in

lib/foreign-field.ts:32

Methods

add

add(y): UnreducedForeignField

Finite field addition

Example

x.add(2); // x + 2 mod p

Parameters

NameType
ynumber | bigint | ForeignField

Returns

UnreducedForeignField

Inherited from

ForeignFieldWithMul.add

Defined in

lib/foreign-field.ts:203


assertAlmostReduced

assertAlmostReduced(): AlmostForeignField

Assert that this field element lies in the range [0, 2^k), where k = ceil(log2(p)) and p is the foreign field modulus.

Returns the field element as a AlmostForeignField.

For a more efficient version of this for multiple field elements, see assertAlmostReduced.

Note: this does not ensure that the field elements is in the canonical range [0, p). To assert that stronger property, there is assertCanonical. You should typically use assertAlmostReduced though, because it is cheaper to prove and sufficient for ensuring validity of all our non-native field arithmetic methods.

Returns

AlmostForeignField

Inherited from

ForeignFieldWithMul.assertAlmostReduced

Defined in

lib/foreign-field.ts:156


assertCanonical

assertCanonical(): CanonicalForeignField

Assert that this field element is fully reduced, i.e. lies in the range [0, p), where p is the foreign field modulus.

Returns the field element as a CanonicalForeignField.

Returns

CanonicalForeignField

Inherited from

ForeignFieldWithMul.assertCanonical

Defined in

lib/foreign-field.ts:189


assertEquals

assertEquals(y, message?): CanonicalForeignField

Assert equality with a ForeignField-like value

Example

x.assertEquals(0, "x is zero");

Since asserting equality can also serve as a range check, this method returns x with the appropriate type:

Example

let xChecked = x.assertEquals(1, "x is 1");
xChecked satisfies CanonicalForeignField;

Parameters

NameType
ynumber | bigint | CanonicalForeignField
message?string

Returns

CanonicalForeignField

Inherited from

ForeignFieldWithMul.assertEquals

Defined in

lib/foreign-field.ts:278

assertEquals(y, message?): AlmostForeignField

Parameters

NameType
yAlmostForeignField
message?string

Returns

AlmostForeignField

Inherited from

ForeignFieldWithMul.assertEquals

Defined in

lib/foreign-field.ts:282

assertEquals(y, message?): ForeignField

Parameters

NameType
yForeignField
message?string

Returns

ForeignField

Inherited from

ForeignFieldWithMul.assertEquals

Defined in

lib/foreign-field.ts:283


assertLessThan

assertLessThan(c, message?): void

Assert that this field element is less than a constant c: x < c.

The constant must satisfy 0 <= c < 2^264, otherwise an error is thrown.

Example

x.assertLessThan(10);

Parameters

NameType
cnumber | bigint
message?string

Returns

void

Inherited from

ForeignFieldWithMul.assertLessThan

Defined in

lib/foreign-field.ts:325


div

div(y): AlmostForeignField

Division in the finite field, i.e. x*y^(-1) mod p where y^(-1) is the finite field inverse.

Example

let z = x.div(y); // x/y mod p
z.mul(y).assertEquals(x);

Parameters

NameType
ynumber | bigint | AlmostForeignField

Returns

AlmostForeignField

Inherited from

ForeignFieldWithMul.div

Defined in

lib/foreign-field.ts:453


equals

equals(y): Bool

Check equality with a ForeignField-like value

Example

let isXZero = x.equals(0);

Parameters

NameType
ynumber | bigint | ForeignField

Returns

Bool

Inherited from

ForeignFieldWithMul.equals

Defined in

lib/foreign-field.ts:344


inv

inv(): AlmostForeignField

Multiplicative inverse in the finite field

Example

let z = x.inv(); // 1/x mod p
z.mul(x).assertEquals(1);

Returns

AlmostForeignField

Inherited from

ForeignFieldWithMul.inv

Defined in

lib/foreign-field.ts:439


isConstant

isConstant(): boolean

Checks whether this field element is a constant.

See FieldVar to understand constants vs variables.

Returns

boolean

Inherited from

ForeignFieldWithMul.isConstant

Defined in

lib/foreign-field.ts:119


mul

mul(y): UnreducedForeignField

Finite field multiplication

Example

x.mul(y); // x*y mod p

Parameters

NameType
ynumber | bigint | AlmostForeignField

Returns

UnreducedForeignField

Inherited from

ForeignFieldWithMul.mul

Defined in

lib/foreign-field.ts:425


neg

neg(): UnreducedForeignField

Finite field negation

Example

x.neg(); // -x mod p = p - x

Returns

UnreducedForeignField

Inherited from

ForeignFieldWithMul.neg

Defined in

lib/foreign-field.ts:214


sub

sub(y): UnreducedForeignField

Finite field subtraction

Example

x.sub(1); // x - 1 mod p

Parameters

NameType
ynumber | bigint | ForeignField

Returns

UnreducedForeignField

Inherited from

ForeignFieldWithMul.sub

Defined in

lib/foreign-field.ts:226


toBigInt

toBigInt(): bigint

Convert this field element to a bigint.

Returns

bigint

Inherited from

ForeignFieldWithMul.toBigInt

Defined in

lib/foreign-field.ts:139


toBits

toBits(length?): Bool[]

Unpack a field element to its bits, as a Bool[] array.

This method is provable!

Parameters

NameType
length?number

Returns

Bool[]

Inherited from

ForeignFieldWithMul.toBits

Defined in

lib/foreign-field.ts:363


toConstant

toConstant(): ForeignField

Convert this field element to a constant.

See FieldVar to understand constants vs variables.

Warning: This function is only useful in witness or asProver blocks, that is, in situations where the prover computes a value outside provable code.

Returns

ForeignField

Inherited from

ForeignFieldWithMul.toConstant

Defined in

lib/foreign-field.ts:131


toFields

toFields(): Field[]

Instance version of Provable<ForeignField>.toFields, see toFields

Returns

Field[]

Inherited from

ForeignFieldWithMul.toFields

Defined in

lib/foreign-field.ts:398


assertAlmostReduced

Static assertAlmostReduced<T>(...xs): [...{ [i in string | number | symbol]: AlmostForeignField }[]]

Assert that one or more field elements lie in the range [0, 2^k), where k = ceil(log2(p)) and p is the foreign field modulus.

This is most efficient than when checking a multiple of 3 field elements at once.

Type parameters

NameType
Textends Tuple<ForeignField>

Parameters

NameType
...xsT

Returns

[...{ [i in string | number | symbol]: AlmostForeignField }[]]

Inherited from

ForeignFieldWithMul.assertAlmostReduced

Defined in

lib/foreign-field.ts:172


check

Static check(x): void

Parameters

NameType
xForeignField

Returns

void

Overrides

ForeignFieldWithMul.check

Defined in

lib/foreign-field.ts:515


from

Static from(x): CanonicalForeignField

Coerce the input to a ForeignField.

Parameters

NameType
xstring | number | bigint

Returns

CanonicalForeignField

Inherited from

ForeignFieldWithMul.from

Defined in

lib/foreign-field.ts:108


fromBits

Static fromBits(bits): AlmostForeignField

Create a field element from its bits, as a Bool[] array.

This method is provable!

Parameters

NameType
bitsBool[]

Returns

AlmostForeignField

Inherited from

ForeignFieldWithMul.fromBits

Defined in

lib/foreign-field.ts:384


sum

Static sum(xs, operations): UnreducedForeignField

Sum (or difference) of multiple finite field elements.

Example

let z = ForeignField.sum([3, 2, 1], [-1, 1]); // 3 - 2 + 1
z.assertEquals(2);

This method expects a list of ForeignField-like values, x0,...,xn, and a list of "operations" op1,...,opn where every op is 1 or -1 (plus or minus), and returns

x0 + op1*x1 + ... + opn*xn

where the sum is computed in finite field arithmetic.

Important: For more than two summands, this is significantly more efficient than chaining calls to add and sub.

Parameters

NameType
xs(number | bigint | ForeignField)[]
operations(1 | -1)[]

Returns

UnreducedForeignField

Inherited from

ForeignFieldWithMul.sum

Defined in

lib/foreign-field.ts:251


unsafeFrom

Static unsafeFrom(x): CanonicalForeignField

Coerce the input to a CanonicalForeignField without additional assertions.

Warning: Only use if you know what you're doing.

Parameters

NameType
xForeignField

Returns

CanonicalForeignField

Defined in

lib/foreign-field.ts:525