Skip to main content

Scalar

Defined in: lib/provable/scalar.ts:24

Represents a Scalar.

Implements

  • ShiftedScalar

Properties

high254

high254: Field;

Defined in: lib/provable/scalar.ts:31

Implementation of

ShiftedScalar.high254

lowBit

lowBit: Bool;

Defined in: lib/provable/scalar.ts:30

We represent a scalar s in shifted form t = s - 2^255 mod q, split into its low bit (t & 1) and high 254 bits (t >> 1). The reason is that we can efficiently compute the scalar multiplication (t + 2^255) * P = s * P.

Implementation of

ShiftedScalar.lowBit

ORDER

static ORDER: bigint = Fq.modulus;

Defined in: lib/provable/scalar.ts:33

Methods

add()

add(y: Scalar): Scalar

Defined in: lib/provable/scalar.ts:144

Add scalar field elements.

Warning: This method is not available for provable code.

Parameters

y

Scalar

Returns

Scalar


div()

div(y: Scalar): Scalar

Defined in: lib/provable/scalar.ts:181

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

Warning: This method is not available for provable code.

Parameters

y

Scalar

Returns

Scalar


isConstant()

isConstant(): boolean

Defined in: lib/provable/scalar.ts:75

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

boolean


mul()

mul(y: Scalar): Scalar

Defined in: lib/provable/scalar.ts:168

Multiply scalar field elements.

Warning: This method is not available for provable code.

Parameters

y

Scalar

Returns

Scalar


neg()

neg(): Scalar

Defined in: lib/provable/scalar.ts:133

Negate a scalar field element.

Warning: This method is not available for provable code.

Returns

Scalar


sub()

sub(y: Scalar): Scalar

Defined in: lib/provable/scalar.ts:156

Subtract scalar field elements.

Warning: This method is not available for provable code.

Parameters

y

Scalar

Returns

Scalar


toBigInt()

toBigInt(): bigint

Defined in: lib/provable/scalar.ts:96

Convert this Scalar into a bigint

Returns

bigint


toFields()

toFields(): Field[]

Defined in: lib/provable/scalar.ts:234

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 Scalar.toFieldsCompressed for a user-friendly serialization that can be used outside proofs.

Returns

Field[]


toFieldsCompressed()

toFieldsCompressed(): {
field: Field;
highBit: Bool;
}

Defined in: lib/provable/scalar.ts:197

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

{
field: Field;
highBit: Bool;
}
field
field: Field;
highBit
highBit: Bool;

toJSON()

toJSON(): string

Defined in: lib/provable/scalar.ts:323

Serializes this Scalar to a string

Returns

string


check()

static check(s: Scalar): void

Defined in: lib/provable/scalar.ts:285

Part of the Provable interface.

Parameters

s

Scalar

Returns

void


empty()

static empty(): Scalar

Defined in: lib/provable/scalar.ts:335

Returns

Scalar


from()

static from(s: string | number | bigint | Scalar): Scalar

Defined in: lib/provable/scalar.ts:45

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

s

string | number | bigint | Scalar

Returns

Scalar


fromBits()

static fromBits(bits: Bool[]): Scalar

Defined in: lib/provable/scalar.ts:106

Creates a Scalar from an array of Bool. This method is provable.

Parameters

bits

Bool[]

Returns

Scalar


fromField()

static fromField(s: Field): Scalar

Defined in: lib/provable/scalar.ts:66

Provable method to convert a Field into a Scalar.

This is always possible and unambiguous, since the scalar field is larger than the base field.

Parameters

s

Field

Returns

Scalar


fromFields()

static fromFields(fields: Field[]): Scalar

Defined in: lib/provable/scalar.ts:266

Part of the Provable interface.

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

Parameters

fields

Field[]

Returns

Scalar


fromJSON()

static fromJSON(x: string): Scalar

Defined in: lib/provable/scalar.ts:331

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

x

string

Returns

Scalar


fromValue()

static fromValue(x: bigint): Scalar

Defined in: lib/provable/scalar.ts:305

Parameters

x

bigint

Returns

Scalar


random()

static random(): Scalar

Defined in: lib/provable/scalar.ts:122

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

Returns

Scalar


sizeInFields()

static sizeInFields(): number

Defined in: lib/provable/scalar.ts:278

Part of the Provable interface.

Returns the size of this type in Field elements.

Returns

number


toAuxiliary()

static toAuxiliary(): never[]

Defined in: lib/provable/scalar.ts:257

Part of the Provable interface.

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

Returns

never[]


toCanonical()

static toCanonical(s: Scalar): Scalar

Defined in: lib/provable/scalar.ts:293

Parameters

s

Scalar

Returns

Scalar


toFields()

static toFields(x: Scalar): Field[]

Defined in: lib/provable/scalar.ts:220

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

x

Scalar

Returns

Field[]


toInput()

static toInput(value: Scalar): HashInput

Defined in: lib/provable/scalar.ts:248

Warning: This function is mainly for internal use. Normally it is not intended to be used by a zkApp developer.

This function is the implementation of ProvableExtended.toInput() for the Scalar type.

Parameters

value

Scalar

The Scalar element to get the input array.

Returns

HashInput

An object where the fields key is a Field array of length 1 created from this Field.


toJSON()

static toJSON(x: Scalar): string

Defined in: lib/provable/scalar.ts:315

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

x

Scalar

Returns

string


toValue()

static toValue(x: Scalar): bigint

Defined in: lib/provable/scalar.ts:301

Parameters

x

Scalar

Returns

bigint