Skip to main content

Scalar

Represents a Scalar.

Implements

  • ShiftedScalar

Properties

high254

high254: Field;

Implementation of

ShiftedScalar.high254

Source

lib/provable/scalar.ts:31


lowBit

lowBit: Bool;

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

Source

lib/provable/scalar.ts:30


ORDER

static ORDER: bigint = Fq.modulus;

Source

lib/provable/scalar.ts:33

Methods

add()

add(y: Scalar): Scalar

Add scalar field elements.

Warning: This method is not available for provable code.

Parameters

y: Scalar

Returns

Scalar

Source

lib/provable/scalar.ts:135


div()

div(y: Scalar): Scalar

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

Warning: This method is not available for provable code.

Parameters

y: Scalar

Returns

Scalar

Source

lib/provable/scalar.ts:172


isConstant()

isConstant(): boolean

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

Source

lib/provable/scalar.ts:67


mul()

mul(y: Scalar): Scalar

Multiply scalar field elements.

Warning: This method is not available for provable code.

Parameters

y: Scalar

Returns

Scalar

Source

lib/provable/scalar.ts:159


neg()

neg(): Scalar

Negate a scalar field element.

Warning: This method is not available for provable code.

Returns

Scalar

Source

lib/provable/scalar.ts:124


sub()

sub(y: Scalar): Scalar

Subtract scalar field elements.

Warning: This method is not available for provable code.

Parameters

y: Scalar

Returns

Scalar

Source

lib/provable/scalar.ts:147


toBigInt()

toBigInt(): bigint

Convert this Scalar into a bigint

Returns

bigint

Source

lib/provable/scalar.ts:87


toConstant()

toConstant(): Scalar

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

Scalar

Source

lib/provable/scalar.ts:79


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

Returns

Field[]

Source

lib/provable/scalar.ts:225


toFieldsCompressed()

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

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;

Source

lib/provable/scalar.ts:188


toJSON()

toJSON(): string

Serializes this Scalar to a string

Returns

string

Source

lib/provable/scalar.ts:309


check()

static check(s: Scalar): void

Part of the Provable interface.

Parameters

s: Scalar

Returns

void

Source

lib/provable/scalar.ts:279


from()

static from(s: string | number | bigint | Scalar): 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

s: string | number | bigint | Scalar

Returns

Scalar

Source

lib/provable/scalar.ts:45


fromBits()

static fromBits(bits: Bool[]): Scalar

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

Parameters

bits: Bool[]

Returns

Scalar

Source

lib/provable/scalar.ts:97


fromField()

static fromField(s: Field): Scalar

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

Source

lib/provable/scalar.ts:58


fromFields()

static fromFields(fields: Field[]): Scalar

Part of the Provable interface.

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

Parameters

fields: Field[]

Returns

Scalar

Source

lib/provable/scalar.ts:257


fromJSON()

static fromJSON(x: string): 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

x: string

Returns

Scalar

Source

lib/provable/scalar.ts:317


fromValue()

static fromValue(x: bigint): Scalar

Parameters

x: bigint

Returns

Scalar

Source

lib/provable/scalar.ts:291


random()

static random(): Scalar

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

Returns

Scalar

Source

lib/provable/scalar.ts:113


sizeInFields()

static sizeInFields(): number

Part of the Provable interface.

Returns the size of this type in Field elements.

Returns

number

Source

lib/provable/scalar.ts:272


toAuxiliary()

static toAuxiliary(): never[]

Part of the Provable interface.

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

Returns

never[]

Source

lib/provable/scalar.ts:248


toFields()

static toFields(x: Scalar): 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

x: Scalar

Returns

Field[]

Source

lib/provable/scalar.ts:211


toInput()

static toInput(x: Scalar): HashInput

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

x: Scalar

Returns

HashInput

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

Source

lib/provable/scalar.ts:239


toJSON()

static toJSON(x: Scalar): 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

x: Scalar

Returns

string

Source

lib/provable/scalar.ts:301


toValue()

static toValue(x: Scalar): bigint

Parameters

x: Scalar

Returns

bigint

Source

lib/provable/scalar.ts:287