Skip to main content

Scalar

SnarkyJS / 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:31

Properties

constantValue

Optional constantValue: Uint8Array

Defined in

lib/scalar.ts:27


value

value: MlArray<FieldVar>

Defined in

lib/scalar.ts:26


ORDER

Static ORDER: bigint = Fq.modulus

Defined in

lib/scalar.ts:29

Methods

#assertConstant

Private #assertConstant(name): bigint

Parameters

NameType
namestring

Returns

bigint

Defined in

lib/scalar.ts:119


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


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


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


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


neg

neg(): Scalar

Negate a scalar field element.

Warning: This method is not available for provable code.

Returns

Scalar

Defined in

lib/scalar.ts:128


shift

shift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:185


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


toBigInt

toBigInt(): bigint

Convert this Scalar into a bigint

Returns

bigint

Defined in

lib/scalar.ts:84


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


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


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


toJSON

toJSON(): string

Serializes this Scalar to a string

Returns

string

Defined in

lib/scalar.ts:306


unshift

unshift(): Scalar

Returns

Scalar

Defined in

lib/scalar.ts:189


#assertConstantStatic

Static Private #assertConstantStatic(x, name): bigint

Parameters

NameType
xScalar
namestring

Returns

bigint

Defined in

lib/scalar.ts:110


check

Static check(): void

Part of the Provable interface.

Does nothing.

Returns

void

Defined in

lib/scalar.ts:276


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


fromBigInt

Static fromBigInt(x): Scalar

Deprecated

use from

Parameters

NameType
xbigint

Returns

Scalar

Defined in

lib/scalar.ts:77


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


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


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


random

Static random(): Scalar

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

Returns

Scalar

Defined in

lib/scalar.ts:103


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


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


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


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