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:37

Properties

constantValue

Optional constantValue: ScalarConst

Defined in

lib/scalar.ts:33


value

value: MlArray\<FieldVar>

Defined in

lib/scalar.ts:32


ORDER

Static ORDER: bigint = Fq.modulus

Defined in

lib/scalar.ts:35

Methods

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:131


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:168


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:62


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:155


neg

neg(): Scalar

Negate a scalar field element.

Warning: This method is not available for provable code.

Returns

Scalar

Defined in

lib/scalar.ts:120


shift

shift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:177


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:143


toBigInt

toBigInt(): bigint

Convert this Scalar into a bigint

Returns

bigint

Defined in

lib/scalar.ts:90


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:73


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:232


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:194


toJSON

toJSON(): string

Serializes this Scalar to a string

Returns

string

Defined in

lib/scalar.ts:298


unshift

unshift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:181


check

Static check(): void

Part of the Provable interface.

Does nothing.

Returns

void

Defined in

lib/scalar.ts:268


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 | Scalar | ScalarConst

Returns

Scalar

Defined in

lib/scalar.ts:50


fromBigInt

Static fromBigInt(x): Scalar

Parameters

NameType
xbigint

Returns

Scalar

Deprecated

use from

Defined in

lib/scalar.ts:83


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:101


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:250


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:306


random

Static random(): Scalar

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

Returns

Scalar

Defined in

lib/scalar.ts:109


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:259


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:241


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:217


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:290