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:142
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:179
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:74
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:166
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:131
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:154
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:94
Convert this Scalar into a bigint
Returns
bigint
toConstant()
toConstant(): Scalar;
Defined in: lib/provable/scalar.ts:86
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
toFields()
toFields(): Field[];
Defined in: lib/provable/scalar.ts:232
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:195
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:321
Serializes this Scalar to a string
Returns
string
check()
static check(s: Scalar): void;
Defined in: lib/provable/scalar.ts:283
Part of the Provable interface.
Parameters
s
Scalar
Returns
void
empty()
static empty(): Scalar;
Defined in: lib/provable/scalar.ts:333
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:104
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:65
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
Returns
Scalar
fromFields()
static fromFields(fields: Field[]): Scalar;
Defined in: lib/provable/scalar.ts:264
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:329
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
fromShiftedScalar()
static fromShiftedScalar(s: ShiftedScalar): Scalar;
Defined in: lib/provable/scalar.ts:56
Provable method to convert a ShiftedScalar to a Scalar.
Parameters
s
ShiftedScalar
Returns
Scalar
fromValue()
static fromValue(x: bigint): Scalar;
Defined in: lib/provable/scalar.ts:303
Parameters
x
bigint
Returns
Scalar
random()
static random(): Scalar;
Defined in: lib/provable/scalar.ts:120
Returns a random Scalar. Randomness can not be proven inside a circuit!
Returns
Scalar
sizeInFields()
static sizeInFields(): number;
Defined in: lib/provable/scalar.ts:276
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:255
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:291
Parameters
s
Scalar
Returns
Scalar
toFields()
static toFields(x: Scalar): Field[];
Defined in: lib/provable/scalar.ts:218
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:246
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:313
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:299
Parameters
x
Scalar
Returns
bigint