Scalar
Represents a Scalar.
Implements
ShiftedScalar
Properties
high254
high254: Field;
Implementation of
ShiftedScalar.high254
Source
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
ORDER
static ORDER: bigint = Fq.modulus;
Source
Methods
add()
add(y: Scalar): Scalar
Add scalar field elements.
Warning: This method is not available for provable code.
Parameters
• y: Scalar
Returns
Source
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
Source
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
mul()
mul(y: Scalar): Scalar
Multiply scalar field elements.
Warning: This method is not available for provable code.
Parameters
• y: Scalar
Returns
Source
neg()
neg(): Scalar
Negate a scalar field element.
Warning: This method is not available for provable code.
Returns
Source
sub()
sub(y: Scalar): Scalar
Subtract scalar field elements.
Warning: This method is not available for provable code.
Parameters
• y: Scalar
Returns
Source
toBigInt()
toBigInt(): bigint
Convert this Scalar into a bigint
Returns
bigint
Source
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
Source
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
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
toJSON()
toJSON(): string
Serializes this Scalar to a string
Returns
string
Source
check()
static check(s: Scalar): void
Part of the Provable interface.
Parameters
• s: Scalar
Returns
void
Source
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
Source
fromBits()
static fromBits(bits: Bool[]): Scalar
Creates a Scalar from an array of Bool. This method is provable.
Parameters
• bits: Bool
[]
Returns
Source
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
Source
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
Source
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
Source
fromValue()
static fromValue(x: bigint): Scalar
Parameters
• x: bigint
Returns
Source
random()
static random(): Scalar
Returns a random Scalar. Randomness can not be proven inside a circuit!
Returns
Source
sizeInFields()
static sizeInFields(): number
Part of the Provable interface.
Returns the size of this type in Field elements.
Returns
number
Source
toAuxiliary()
static toAuxiliary(): never[]
Part of the Provable interface.
Serialize a Scalar into its auxiliary data, which are empty.
Returns
never
[]
Source
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
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
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
toValue()
static toValue(x: Scalar): bigint
Parameters
• x: Scalar
Returns
bigint