UInt8
A 8 bit unsigned integer with values ranging from 0 to 255.
Extends
- {
"value"
:Field
; }
Constructors
new UInt8()
new UInt8(x: number | bigint | FieldVar | UInt8): UInt8
Create a UInt8 from a bigint or number.
The max value of a UInt8 is 2^8 - 1 = 255
.
Warning: Cannot overflow past 255, an error is thrown if the result is greater than 255.
Parameters
• x: number
| bigint
| FieldVar
| UInt8
Returns
Overrides
Struct({
value: Field,
}).constructor
Source
Properties
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).value
Source
NUM_BITS
static NUM_BITS: number = 8;
Source
Unsafe
static Unsafe: {
"fromField": UInt8;
};
fromField()
Create a UInt8 from a Field without constraining its range.
Warning: This is unsafe, because it does not prove that the input Field actually fits in 8 bits.\ Only use this if you know what you are doing, otherwise use the safe UInt8.from.
Parameters
• x: Field
Returns
Source
_isStruct
static _isStruct: true;
Inherited from
Struct({
value: Field,
})._isStruct
Source
lib/provable/types/struct.ts:145
empty()
static empty: () => {
"value": Field;
};
Returns
{
"value": Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).empty
Source
lib/provable/types/struct.ts:155
fromFields()
static fromFields: (fields: Field[]) => {
"value": Field;
};
Parameters
• fields: Field
[]
Returns
{
"value": Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).fromFields
Source
lib/provable/types/provable-intf.ts:87
fromJSON()
static fromJSON: (x: {
"value": Field;
}) => {
"value": Field;
};
Parameters
• x
• x.value: string
= Field
Returns
{
"value": Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).fromJSON
Source
lib/provable/types/struct.ts:154
fromValue
static fromValue: (x: {
"value": Field;
} | {
"value": Field;
}) => {
"value": Field;
} & (value: {
"value": Field;
}) => {
"value": Field;
};
Convert provable type from a normal JS type.
Inherited from
Struct({
value: Field,
}).fromValue
Source
lib/provable/types/provable-intf.ts:76
toAuxiliary()
static toAuxiliary: (value?: {
"value": Field;
}) => any[];
A function that takes value
(optional), an element of type T
, as argument and
returns an array of any type that make up the "auxiliary" (non-provable) data of value
.
Parameters
• value?
the element of type T
to generate the auxiliary data array from, optional.
If not provided, a default value for auxiliary data is returned.
• value.value?: Field
= Field
Returns
any
[]
Inherited from
Struct({
value: Field,
}).toAuxiliary
Source
lib/provable/types/provable-intf.ts:37
toFields()
static toFields: (value: {
"value": Field;
}) => Field[];
A function that takes value
, an element of type T
, as argument and returns
an array of Field elements that make up the provable data of value
.
Parameters
• value
the element of type T
to generate the Field array from.
• value.value: Field
= Field
Returns
Field
[]
Inherited from
Struct({
value: Field,
}).toFields
Source
lib/provable/types/provable-intf.ts:26
toJSON()
static toJSON: (x: {
"value": Field;
}) => {
"value": Field;
};
Parameters
• x
• x.value: Field
= Field
Returns
{
"value": Field;
}
value
value: string = Field;
Inherited from
Struct({
value: Field,
}).toJSON
Source
lib/provable/types/struct.ts:153
toValue()
static toValue: (x: {
"value": Field;
}) => {
"value": Field;
};
Convert provable type to a normal JS type.
Parameters
• x
• x.value: Field
= Field
Returns
{
"value": Field;
}
value
value: bigint = Field;
Inherited from
Struct({
value: Field,
}).toValue
Source
lib/provable/types/provable-intf.ts:71
Methods
add()
add(y: number | bigint | UInt8): UInt8
Add a UInt8 to another UInt8 without allowing overflow.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
const x = UInt8.from(3);
const sum = x.add(5);
sum.assertEquals(8);
Throws
if the result is greater than 255.
Source
assertEquals()
assertEquals(y: number | bigint | UInt8, message?: string): void
Assert that this UInt8 is equal another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
• y: number
| bigint
| UInt8
the UInt8 value to compare & assert with this UInt8.
• message?: string
Returns
void
Source
assertGreaterThan()
assertGreaterThan(y: number | bigint | UInt8, message?: string): void
Assert that this UInt8 is greater than another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
• y: number
| bigint
| UInt8
the UInt8 value to compare & assert with this UInt8.
• message?: string
Returns
void
Source
assertGreaterThanOrEqual()
assertGreaterThanOrEqual(y: UInt8, message?: string): void
Assert that this UInt8 is greater than or equal to another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
• y: UInt8
the UInt8 value to compare & assert with this UInt8.
• message?: string
Returns
void
Source
assertLessThan()
assertLessThan(y: number | bigint | UInt8, message?: string): void
Assert that this UInt8 is less than another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
• y: number
| bigint
| UInt8
the UInt8 value to compare & assert with this UInt8.
• message?: string
Returns
void
Source
assertLessThanOrEqual()
assertLessThanOrEqual(y: number | bigint | UInt8, message?: string): void
Assert that this UInt8 is less than or equal to another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
• y: number
| bigint
| UInt8
the UInt8 value to compare & assert with this UInt8.
• message?: string
Returns
void
Source
div()
div(y: number | bigint | UInt8): UInt8
Divide a UInt8 by another UInt8. This is integer division that rounds down.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
const x = UInt8.from(7);
const quotient = x.div(2);
quotient.assertEquals(3);
Source
divMod()
divMod(y: number | bigint | UInt8): {
"quotient": UInt8;
"remainder": UInt8;
}
Get the quotient and remainder of a UInt8 divided by another UInt8:
x == y * q + r
, where 0 <= r < y
.
Parameters
• y: number
| bigint
| UInt8
a UInt8 to get the quotient and remainder of another UInt8.
Returns
{
"quotient": UInt8;
"remainder": UInt8;
}
The quotient q
and remainder r
.
quotient
quotient: UInt8;
remainder
remainder: UInt8;
Source
greaterThan()
greaterThan(y: number | bigint | UInt8): Bool
Check if this UInt8 is greater than another UInt8. Returns a Bool.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
// 5 > 3
UInt8.from(5).greaterThan(3);
Source
greaterThanOrEqual()
greaterThanOrEqual(y: number | bigint | UInt8): Bool
Check if this UInt8 is greater than or equal another UInt8 value. Returns a Bool.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
// 3 >= 3
UInt8.from(3).greaterThanOrEqual(3);
Source
lessThan()
lessThan(y: number | bigint | UInt8): Bool
Check if this UInt8 is less than another UInt8 value. Returns a Bool.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
UInt8.from(2).lessThan(UInt8.from(3));
Source
lessThanOrEqual()
lessThanOrEqual(y: number | bigint | UInt8): Bool
Check if this UInt8 is less than or equal to another UInt8 value. Returns a Bool.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
UInt8.from(3).lessThanOrEqual(UInt8.from(5));
Source
mod()
mod(y: number | bigint | UInt8): UInt8
Get the remainder a UInt8 of division of another UInt8.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
const x = UInt8.from(50);
const mod = x.mod(30);
mod.assertEquals(20);
Source
mul()
mul(y: number | bigint | UInt8): UInt8
Multiply a UInt8 by another UInt8 without allowing overflow.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
const x = UInt8.from(3);
const product = x.mul(5);
product.assertEquals(15);
Throws
if the result is greater than 255.
Source
sub()
sub(y: number | bigint | UInt8): UInt8
Subtract a UInt8 from another UInt8 without allowing underflow.
Parameters
• y: number
| bigint
| UInt8
Returns
Example
const x = UInt8.from(8);
const difference = x.sub(5);
difference.assertEquals(3);
Throws
if the result is less than 0.
Source
toBigInt()
toBigInt(): bigint
Serialize the UInt8 to a bigint.
Warning: This operation is not provable.
Returns
bigint
Source
toNumber()
toNumber(): number
Serialize the UInt8 to a number.
Warning: This operation is not provable.
Returns
number
Source
toString()
toString(): string
Serialize the UInt8 to a string, e.g. for printing.
Warning: This operation is not provable.
Returns
string
Source
toUInt32()
toUInt32(): UInt32
Returns
Source
toUInt64()
toUInt64(): UInt64
Returns
Source
MAXINT()
static MAXINT(): UInt8
Creates a UInt8 with a value of 255.
Returns
Source
check()
static check(x: Field | {
"value": Field;
}): void
Provable.check for UInt8. Proves that the input is in the [0, 255] range.
Parameters
• x: Field
| {
"value"
: Field
;
}
Returns
void
Overrides
Struct({
value: Field,
}).check
Source
from()
static from(x:
| number
| bigint
| Field
| UInt64
| UInt32
| UInt8): UInt8
Creates a new UInt8.
Parameters
• x:
| number
| bigint
| Field
| UInt64
| UInt32
| UInt8
Returns
Source
sizeInFields()
static sizeInFields(): number
Return the size of the T
type in terms of Field type, as Field is the primitive type.
Returns
number
A number
representing the size of the T
type in terms of Field type.
Inherited from
Struct({
value: Field,
}).sizeInFields
Source
lib/provable/types/provable-intf.ts:56
toInput()
static toInput(x: {
"value": Field;
}): HashInput
Parameters
• x
• x.value: Field
Returns
HashInput
Overrides
Struct({
value: Field,
}).toInput