Skip to main content

Scalar

o1js / Modules / Scalar

Class: Scalar

Represents a Scalar.

Table of contents

Constructors

Properties

Methods

Constructors

constructor

Private new Scalar(bits, constantValue?)

Parameters

NameType
bitsMlArray<FieldVar>
constantValue?bigint

Defined in

lib/scalar.ts:34

Properties

constantValue

Optional constantValue: Uint8Array

Defined in

lib/scalar.ts:30


value

value: MlArray<FieldVar>

Defined in

lib/scalar.ts:29


ORDER

Static ORDER: bigint = Fq.modulus

Defined in

lib/scalar.ts:32

Methods

#assertConstant

Private #assertConstant(name): bigint

Parameters

NameType
namestring

Returns

bigint

Defined in

lib/scalar.ts:112


add

add(y): Scalar

Add scalar field elements.

Warning: This method is not available for provable code.

Parameters

NameType
yScalar

Returns

Scalar

Defined in

lib/scalar.ts:132


div

div(y): Scalar

Divide scalar field elements. Throws if the denominator is zero.

Warning: This method is not available for provable code.

Parameters

NameType
yScalar

Returns

Scalar

Defined in

lib/scalar.ts:169


isConstant

isConstant(): this is Scalar & Object

Check whether this Scalar is a hard-coded constant in the constraint system. If a Scalar is constructed outside provable code, it is a constant.

Returns

this is Scalar & Object

Defined in

lib/scalar.ts:59


mul

mul(y): Scalar

Multiply scalar field elements.

Warning: This method is not available for provable code.

Parameters

NameType
yScalar

Returns

Scalar

Defined in

lib/scalar.ts:156


neg

neg(): Scalar

Negate a scalar field element.

Warning: This method is not available for provable code.

Returns

Scalar

Defined in

lib/scalar.ts:121


shift

shift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:178


sub

sub(y): Scalar

Subtract scalar field elements.

Warning: This method is not available for provable code.

Parameters

NameType
yScalar

Returns

Scalar

Defined in

lib/scalar.ts:144


toBigInt

toBigInt(): bigint

Convert this Scalar into a bigint

Returns

bigint

Defined in

lib/scalar.ts:87


toConstant

toConstant(): ConstantScalar

Convert this Scalar into a constant if it isn't already.

If the scalar is a variable, this only works inside asProver or witness blocks.

See FieldVar for an explanation of constants vs. variables.

Returns

ConstantScalar

Defined in

lib/scalar.ts:70


toFields

toFields(): Field[]

Serialize this Scalar to Field elements.

Warning: This function is for internal usage. It returns 255 field elements which represent the Scalar in a shifted, bitwise format. The fields are not constrained to be boolean.

Check out toFieldsCompressed for a user-friendly serialization that can be used outside proofs.

Returns

Field[]

Defined in

lib/scalar.ts:233


toFieldsCompressed

toFieldsCompressed(): Object

Serialize a Scalar into a Field element plus one bit, where the bit is represented as a Bool.

Warning: This method is not available for provable code.

Note: Since the Scalar field is slightly larger than the base Field, an additional high bit is needed to represent all Scalars. However, for a random Scalar, the high bit will be false with overwhelming probability.

Returns

Object

NameType
fieldField
highBitBool

Defined in

lib/scalar.ts:195


toJSON

toJSON(): string

Serializes this Scalar to a string

Returns

string

Defined in

lib/scalar.ts:299


unshift

unshift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:182


check

Static check(): void

Part of the Provable interface.

Does nothing.

Returns

void

Defined in

lib/scalar.ts:269


from

Static from(x): Scalar

Create a constant Scalar from a bigint, number, string or Scalar.

If the input is too large, it is reduced modulo the scalar field size.

Parameters

NameType
xstring | number | bigint | Uint8Array | Scalar

Returns

Scalar

Defined in

lib/scalar.ts:47


fromBigInt

Static fromBigInt(x): Scalar

Deprecated

use from

Parameters

NameType
xbigint

Returns

Scalar

Defined in

lib/scalar.ts:80


fromBits

Static fromBits(bits): Scalar

Creates a data structure from an array of serialized Bool.

Warning: The bits are interpreted as the bits of 2s + 1 + 2^255, where s is the Scalar.

Parameters

NameType
bitsBool[]

Returns

Scalar

Defined in

lib/scalar.ts:98


fromFields

Static fromFields(fields): Scalar

Part of the Provable interface.

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

Parameters

NameType
fieldsField[]

Returns

Scalar

Defined in

lib/scalar.ts:251


fromJSON

Static fromJSON(x): Scalar

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

Parameters

NameType
xstring

Returns

Scalar

Defined in

lib/scalar.ts:307


random

Static random(): Scalar

Returns a random Scalar. Randomness can not be proven inside a circuit!

Returns

Scalar

Defined in

lib/scalar.ts:106


sizeInFields

Static sizeInFields(): number

Part of the Provable interface.

Returns the size of this type in Field elements.

Returns

number

Defined in

lib/scalar.ts:260


toAuxiliary

Static toAuxiliary(): never[]

Part of the Provable interface.

Serialize a Scalar into its auxiliary data, which are empty.

Returns

never[]

Defined in

lib/scalar.ts:242


toFields

Static toFields(x): Field[]

Part of the Provable interface.

Serialize a Scalar into an array of Field elements.

Warning: This function is for internal usage. It returns 255 field elements which represent the Scalar in a shifted, bitwise format. The fields are not constrained to be boolean.

Parameters

NameType
xScalar

Returns

Field[]

Defined in

lib/scalar.ts:218


toJSON

Static toJSON(x): string

Serialize a Scalar 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 Scalar.

Parameters

NameType
xScalar

Returns

string

Defined in

lib/scalar.ts:291