Scalar
Class: Scalar
Represents a Scalar.
Table of contents
Constructors
Properties
Methods
- #assertConstant
- add
- div
- isConstant
- mul
- neg
- shift
- sub
- toBigInt
- toConstant
- toFields
- toFieldsCompressed
- toJSON
- unshift
- check
- from
- fromBigInt
- fromBits
- fromFields
- fromJSON
- random
- sizeInFields
- toAuxiliary
- toFields
- toJSON
Constructors
constructor
• Private
new Scalar(bits
, constantValue?
)
Parameters
Name | Type |
---|---|
bits | MlArray <FieldVar > |
constantValue? | bigint |
Defined in
Properties
constantValue
• Optional
constantValue: Uint8Array
Defined in
value
• value: MlArray
<FieldVar
>
Defined in
ORDER
▪ Static
ORDER: bigint
= Fq.modulus
Defined in
Methods
#assertConstant
▸ Private
#assertConstant(name
): bigint
Parameters
Name | Type |
---|---|
name | string |
Returns
bigint
Defined in
add
▸ add(y
): Scalar
Add scalar field elements.
Warning: This method is not available for provable code.
Parameters
Name | Type |
---|---|
y | Scalar |
Returns
Defined in
div
▸ div(y
): Scalar
Divide scalar field elements. Throws if the denominator is zero.
Warning: This method is not available for provable code.
Parameters
Name | Type |
---|---|
y | Scalar |
Returns
Defined in
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
mul
▸ mul(y
): Scalar
Multiply scalar field elements.
Warning: This method is not available for provable code.
Parameters
Name | Type |
---|---|
y | Scalar |
Returns
Defined in
neg
▸ neg(): Scalar
Negate a scalar field element.
Warning: This method is not available for provable code.
Returns
Defined in
shift
▸ shift(): Scalar
Returns
Defined in
sub
▸ sub(y
): Scalar
Subtract scalar field elements.
Warning: This method is not available for provable code.
Parameters
Name | Type |
---|---|
y | Scalar |
Returns
Defined in
toBigInt
▸ toBigInt(): bigint
Convert this Scalar into a bigint
Returns
bigint
Defined in
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
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
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
Name | Type |
---|---|
field | Field |
highBit | Bool |
Defined in
toJSON
▸ toJSON(): string
Serializes this Scalar to a string
Returns
string
Defined in
unshift
▸ unshift(): Scalar
Returns
Defined in
check
▸ Static
check(): void
Part of the Provable interface.
Does nothing.
Returns
void
Defined in
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
Name | Type |
---|---|
x | string | number | bigint | Uint8Array | Scalar |
Returns
Defined in
fromBigInt
▸ Static
fromBigInt(x
): Scalar
Deprecated
use from
Parameters
Name | Type |
---|---|
x | bigint |
Returns
Defined in
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
Name | Type |
---|---|
bits | Bool [] |
Returns
Defined in
fromFields
▸ Static
fromFields(fields
): Scalar
Part of the Provable interface.
Creates a data structure from an array of serialized Field elements.
Parameters
Name | Type |
---|---|
fields | Field [] |
Returns
Defined in
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
Name | Type |
---|---|
x | string |
Returns
Defined in
random
▸ Static
random(): Scalar
Returns a random Scalar. Randomness can not be proven inside a circuit!
Returns
Defined in
sizeInFields
▸ Static
sizeInFields(): number
Part of the Provable interface.
Returns the size of this type in Field elements.
Returns
number
Defined in
toAuxiliary
▸ Static
toAuxiliary(): never
[]
Part of the Provable interface.
Serialize a Scalar into its auxiliary data, which are empty.
Returns
never
[]
Defined in
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
Name | Type |
---|---|
x | Scalar |
Returns
Field
[]
Defined in
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
Name | Type |
---|---|
x | Scalar |
Returns
string