Skip to main content

AlmostForeignField

Extends​

  • ForeignFieldWithMul

Constructors​

new AlmostForeignField()​

new AlmostForeignField(x: 
| string
| number
| bigint
| Field3
| AlmostForeignField): AlmostForeignField

Parameters​

• x: | string | number | bigint | Field3 | AlmostForeignField

Returns​

AlmostForeignField

Overrides​

ForeignFieldWithMul.constructor

Source​

lib/provable/foreign-field.ts:487

Properties​

type​

type: "AlmostReduced" | "FullyReduced" = 'AlmostReduced';

Source​

lib/provable/foreign-field.ts:485


value​

value: Field3;

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

Inherited from​

ForeignFieldWithMul.value

Source​

lib/provable/foreign-field.ts:49


_Bigint​

static _Bigint: undefined | {} = undefined;

Inherited from​

ForeignFieldWithMul._Bigint

Source​

lib/provable/foreign-field.ts:27


_modulus​

static _modulus: undefined | bigint = undefined;

Inherited from​

ForeignFieldWithMul._modulus

Source​

lib/provable/foreign-field.ts:28


_provable​

static _provable: undefined | ProvablePureExtended<AlmostForeignField, bigint, string> = undefined;

Overrides​

ForeignFieldWithMul._provable

Source​

lib/provable/foreign-field.ts:491


_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

Source​

lib/provable/foreign-field.ts:58

Accessors​

Constructor​

get Constructor(): typeof ForeignField

Returns​

typeof ForeignField

Source​

lib/provable/foreign-field.ts:51


modulus​

get modulus(): bigint

Returns​

bigint

Source​

lib/provable/foreign-field.ts:39


AlmostReduced​

get static 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

Source​

lib/provable/foreign-field.ts:76


Bigint​

get static Bigint(): {}

Returns​

{}

Source​

lib/provable/foreign-field.ts:31


Canonical​

get static Canonical(): typeof CanonicalForeignField

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

Returns​

typeof CanonicalForeignField

Source​

lib/provable/foreign-field.ts:83


Unreduced​

get static Unreduced(): typeof UnreducedForeignField

Constructor for unreduced field elements.

Returns​

typeof UnreducedForeignField

Source​

lib/provable/foreign-field.ts:69


modulus​

get static modulus(): bigint

Returns​

bigint

Source​

lib/provable/foreign-field.ts:35


provable​

get static provable(): ProvablePureExtended<AlmostForeignField, bigint, string>

Returns​

ProvablePureExtended\<AlmostForeignField, bigint, string>

Source​

lib/provable/foreign-field.ts:494


sizeInBits​

get static sizeInBits(): number

Returns​

number

Source​

lib/provable/foreign-field.ts:42

Methods​

add()​

add(y: number | bigint | ForeignField): UnreducedForeignField

Finite field addition

Parameters​

• y: number | bigint | ForeignField

Returns​

UnreducedForeignField

Inherited from​

ForeignFieldWithMul.add

Example​

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

Source​

lib/provable/foreign-field.ts:218


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

Source​

lib/provable/foreign-field.ts:173


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

Source​

lib/provable/foreign-field.ts:204


assertEquals()​

assertEquals(y, message)​

assertEquals(y: number | bigint | CanonicalForeignField, message?: string): CanonicalForeignField

Assert equality with a ForeignField-like value

Parameters​

• y: number | bigint | CanonicalForeignField

• message?: string

Returns​

CanonicalForeignField

Inherited from​

ForeignFieldWithMul.assertEquals

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;
Source​

lib/provable/foreign-field.ts:296

assertEquals(y, message)​

assertEquals(y: AlmostForeignField, message?: string): AlmostForeignField
Parameters​

• y: AlmostForeignField

• message?: string

Returns​

AlmostForeignField

Inherited from​

ForeignFieldWithMul.assertEquals

Source​

lib/provable/foreign-field.ts:300

assertEquals(y, message)​

assertEquals(y: ForeignField, message?: string): ForeignField
Parameters​

• y: ForeignField

• message?: string

Returns​

ForeignField

Inherited from​

ForeignFieldWithMul.assertEquals

Source​

lib/provable/foreign-field.ts:301


assertLessThan()​

assertLessThan(c: number | bigint, message?: string): 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.

Parameters​

• c: number | bigint

• message?: string

Returns​

void

Inherited from​

ForeignFieldWithMul.assertLessThan

Example​

x.assertLessThan(10);

Source​

lib/provable/foreign-field.ts:339


div()​

div(y: number | bigint | AlmostForeignField): AlmostForeignField

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

Parameters​

• y: number | bigint | AlmostForeignField

Returns​

AlmostForeignField

Inherited from​

ForeignFieldWithMul.div

Example​

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

Source​

lib/provable/foreign-field.ts:461


equals()​

equals(y: number | bigint): Bool

Check equality with a constant value.

Parameters​

• y: number | bigint

Returns​

Bool

Example​

let isXZero = x.equals(0);

Source​

lib/provable/foreign-field.ts:521


inv()​

inv(): AlmostForeignField

Multiplicative inverse in the finite field

Returns​

AlmostForeignField

Inherited from​

ForeignFieldWithMul.inv

Example​

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

Source​

lib/provable/foreign-field.ts:447


isConstant()​

isConstant(): boolean

Checks whether this field element is a constant.

See FieldVar to understand constants vs variables.

Returns​

boolean

Inherited from​

ForeignFieldWithMul.isConstant

Source​

lib/provable/foreign-field.ts:136


mul()​

mul(y: number | bigint | AlmostForeignField): UnreducedForeignField

Finite field multiplication

Parameters​

• y: number | bigint | AlmostForeignField

Returns​

UnreducedForeignField

Inherited from​

ForeignFieldWithMul.mul

Example​

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

Source​

lib/provable/foreign-field.ts:433


neg()​

neg(): AlmostForeignField

Finite field negation

Returns​

AlmostForeignField

Inherited from​

ForeignFieldWithMul.neg

Example​

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

Source​

lib/provable/foreign-field.ts:229


sub()​

sub(y: number | bigint | ForeignField): UnreducedForeignField

Finite field subtraction

Parameters​

• y: number | bigint | ForeignField

Returns​

UnreducedForeignField

Inherited from​

ForeignFieldWithMul.sub

Example​

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

Source​

lib/provable/foreign-field.ts:244


toBigInt()​

toBigInt(): bigint

Convert this field element to a bigint.

Returns​

bigint

Inherited from​

ForeignFieldWithMul.toBigInt

Source​

lib/provable/foreign-field.ts:156


toBits()​

toBits(length?: number): Bool[]

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

This method is provable!

Parameters​

• length?: number

Returns​

Bool[]

Inherited from​

ForeignFieldWithMul.toBits

Source​

lib/provable/foreign-field.ts:358


toConstant()​

toConstant(): ForeignField

Convert this field element to a constant.

See FieldVar to understand constants vs variables.

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

Returns​

ForeignField

Inherited from​

ForeignFieldWithMul.toConstant

Source​

lib/provable/foreign-field.ts:148


toFields()​

toFields(): Field[]

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

Returns​

Field[]

Inherited from​

ForeignFieldWithMul.toFields

Source​

lib/provable/foreign-field.ts:406


assertAlmostReduced()​

static assertAlmostReduced<T>(...xs: T): [...{ [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​

• T extends Tuple\<ForeignField>

Parameters​

• ...xs: T

Returns​

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

Inherited from​

ForeignFieldWithMul.assertAlmostReduced

Source​

lib/provable/foreign-field.ts:187


check()​

static check(x: ForeignField): void

Parameters​

• x: ForeignField

Returns​

void

Overrides​

ForeignFieldWithMul.check

Source​

lib/provable/foreign-field.ts:499


from()​

from(x)​

static from(x: string | number | bigint): CanonicalForeignField

Coerce the input to a ForeignField.

Parameters​

• x: string | number | bigint

Returns​

CanonicalForeignField

Inherited from​

ForeignFieldWithMul.from

Source​

lib/provable/foreign-field.ts:124

from(x)​

static from(x: string | number | bigint | ForeignField): ForeignField
Parameters​

• x: string | number | bigint | ForeignField

Returns​

ForeignField

Inherited from​

ForeignFieldWithMul.from

Source​

lib/provable/foreign-field.ts:125


fromBits()​

static fromBits(bits: Bool[]): AlmostForeignField

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

This method is provable!

Parameters​

• bits: Bool[]

Returns​

AlmostForeignField

Inherited from​

ForeignFieldWithMul.fromBits

Source​

lib/provable/foreign-field.ts:388


random()​

static random(): CanonicalForeignField

Returns​

CanonicalForeignField

Inherited from​

ForeignFieldWithMul.random

Source​

lib/provable/foreign-field.ts:399


sum()​

static sum(xs: (number | bigint | ForeignField)[], operations: (-1 | 1)[]): UnreducedForeignField

Sum (or difference) of multiple finite field elements.

Parameters​

• xs: (number | bigint | ForeignField)[]

• operations: (-1 | 1)[]

Returns​

UnreducedForeignField

Inherited from​

ForeignFieldWithMul.sum

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 ForeignField.add and ForeignField.sub.

Source​

lib/provable/foreign-field.ts:269


unsafeFrom()​

static unsafeFrom(x: ForeignField): AlmostForeignField

Coerce the input to an AlmostForeignField without additional assertions.

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

Parameters​

• x: ForeignField

Returns​

AlmostForeignField

Source​

lib/provable/foreign-field.ts:509