UInt8
Defined in: lib/provable/int.ts:1511
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
Defined in: lib/provable/int.ts:1522
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
Properties
value
value: Field = Field;
Defined in: lib/provable/int.ts:1512
Inherited from
Struct({
value: Field,
}).value
_isStruct
static _isStruct: true;
Defined in: lib/provable/types/struct.ts:133
Inherited from
Struct({
value: Field,
})._isStruct
empty()
static empty: () => {
value: Field;
};
Defined in: lib/provable/types/struct.ts:143
Returns
{
value: Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).empty
fromFields()
static fromFields: (fields: Field[]) => {
value: Field;
};
Defined in: lib/provable/types/provable-intf.ts:115
Parameters
fields
Field
[]
Returns
{
value: Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).fromFields
fromJSON()
static fromJSON: (x: {
value: string;
}) => {
value: Field;
};
Defined in: lib/provable/types/struct.ts:142
Parameters
x
value
string
= Field
Returns
{
value: Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).fromJSON
NUM_BITS
static NUM_BITS: number = 8;
Defined in: lib/provable/int.ts:1514
toAuxiliary()
static toAuxiliary: (value?: {
value: Field;
}) => any[];
Defined in: lib/provable/types/provable-intf.ts:47
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
Field
= Field
Returns
any
[]
An array of any type describing how this T
element is made up of "auxiliary" (non-provable) data.
Inherited from
Struct({
value: Field,
}).toAuxiliary
toCanonical()?
static optional toCanonical: (x: {
value: Field;
}) => {
value: Field;
};
Defined in: lib/provable/types/provable-intf.ts:104
Optional method which transforms a provable type into its canonical representation.
This is needed for types that have multiple representations of the same underlying value, and might even not have perfect completeness for some of those representations.
An example is the ForeignField
class, which allows non-native field elements to exist in unreduced form.
The unreduced form is not perfectly complete, for example, addition of two unreduced field elements can cause a prover error.
Specific protocols need to be able to protect themselves against incomplete operations at all costs.
For example, when using actions and reducer, the reducer must be able to produce a proof regardless of the input action.
toCanonical()
converts any input into a safe form and enables us to handle cases like this generically.
Note: For most types, this method is the identity function.
The identity function will also be used when the toCanonical()
is not present on a type.
Parameters
x
value
Field
= Field
Returns
{
value: Field;
}
value
value: Field = Field;
Inherited from
Struct({
value: Field,
}).toCanonical
toFields()
static toFields: (value: {
value: Field;
}) => Field[];
Defined in: lib/provable/types/provable-intf.ts:36
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
Field
= Field
Returns
Field
[]
A Field array describing how this T
element is made up of Field elements.
Inherited from
Struct({
value: Field,
}).toFields
toJSON()
static toJSON: (x: {
value: Field;
}) => {
value: string;
};
Defined in: lib/provable/types/struct.ts:141
Parameters
x
value
Field
= Field
Returns
{
value: string;
}
value
value: string = Field;
Inherited from
Struct({
value: Field,
}).toJSON
toValue()
static toValue: (x: {
value: Field;
}) => {
value: bigint;
};
Defined in: lib/provable/types/provable-intf.ts:81
Convert provable type to a normal JS type.
Parameters
x
value
Field
= Field
Returns
{
value: bigint;
}
value
value: bigint = Field;
Inherited from
Struct({
value: Field,
}).toValue
Unsafe
static Unsafe: {
fromField: UInt8;
};
Defined in: lib/provable/int.ts:1528
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
Returns
Accessors
one
Get Signature
get static one(): UInt8
Defined in: lib/provable/int.ts:1549
Static method to create a UInt8 with value 1
.
Returns
zero
Get Signature
get static zero(): UInt8
Defined in: lib/provable/int.ts:1543
Static method to create a UInt8 with value 0
.
Returns
Methods
add()
add(y: number | bigint | UInt8): UInt8
Defined in: lib/provable/int.ts:1565
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.
assertEquals()
assertEquals(y: number | bigint | UInt8, message?: string): void
Defined in: lib/provable/int.ts:1811
Assert that this UInt8 is equal another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
y
the UInt8 value to compare & assert with this UInt8.
number
| bigint
| UInt8
message?
string
a string error message to print if the assertion fails, optional.
Returns
void
assertGreaterThan()
assertGreaterThan(y: number | bigint | UInt8, message?: string): void
Defined in: lib/provable/int.ts:1787
Assert that this UInt8 is greater than another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
y
the UInt8 value to compare & assert with this UInt8.
number
| bigint
| UInt8
message?
string
a string error message to print if the assertion fails, optional.
Returns
void
assertGreaterThanOrEqual()
assertGreaterThanOrEqual(y: UInt8, message?: string): void
Defined in: lib/provable/int.ts:1799
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
the UInt8 value to compare & assert with this UInt8.
message?
string
a string error message to print if the assertion fails, optional.
Returns
void
assertLessThan()
assertLessThan(y: number | bigint | UInt8, message?: string): void
Defined in: lib/provable/int.ts:1715
Assert that this UInt8 is less than another UInt8 value.
Important: If an assertion fails, the code throws an error.
Parameters
y
the UInt8 value to compare & assert with this UInt8.
number
| bigint
| UInt8
message?
string
a string error message to print if the assertion fails, optional.
Returns
void
assertLessThanOrEqual()
assertLessThanOrEqual(y: number | bigint | UInt8, message?: string): void
Defined in: lib/provable/int.ts:1737
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
the UInt8 value to compare & assert with this UInt8.
number
| bigint
| UInt8
message?
string
a string error message to print if the assertion fails, optional.
Returns
void
div()
div(y: number | bigint | UInt8): UInt8
Defined in: lib/provable/int.ts:1618
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);
divMod()
divMod(y: number | bigint | UInt8): {
quotient: UInt8;
remainder: UInt8;
}
Defined in: lib/provable/int.ts:1645
Get the quotient and remainder of a UInt8 divided by another UInt8:
x == y * q + r
, where 0 <= r < y
.
Parameters
y
a UInt8 to get the quotient and remainder of another UInt8.
number
| bigint
| UInt8
Returns
{
quotient: UInt8;
remainder: UInt8;
}
The quotient q
and remainder r
.
quotient
quotient: UInt8;
remainder
remainder: UInt8;
greaterThan()
greaterThan(y: number | bigint | UInt8): Bool
Defined in: lib/provable/int.ts:1761
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);
greaterThanOrEqual()
greaterThanOrEqual(y: number | bigint | UInt8): Bool
Defined in: lib/provable/int.ts:1775
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);
isConstant()
isConstant(): boolean
Defined in: lib/provable/int.ts:1905
Returns
boolean
lessThan()
lessThan(y: number | bigint | UInt8): Bool
Defined in: lib/provable/int.ts:1699
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));
lessThanOrEqual()
lessThanOrEqual(y: number | bigint | UInt8): Bool
Defined in: lib/provable/int.ts:1682
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));
mod()
mod(y: number | bigint | UInt8): UInt8
Defined in: lib/provable/int.ts:1632
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);
mul()
mul(y: number | bigint | UInt8): UInt8
Defined in: lib/provable/int.ts:1601
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.
sub()
sub(y: number | bigint | UInt8): UInt8
Defined in: lib/provable/int.ts:1583
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.
toBigInt()
toBigInt(): bigint
Defined in: lib/provable/int.ts:1839
Serialize the UInt8 to a bigint.
Warning: This operation is not provable.
Returns
bigint
toBits()
toBits(length: number): Bool[]
Defined in: lib/provable/int.ts:1922
Returns an array of Bool elements representing little endian binary representation of this UInt8 element.
If you use the optional length
argument, proves that the UInt8 element fits in length
bits.
The length
has to be between 0 and 8 and the method throws if it isn't.
Warning: The cost of this operation in a zk proof depends on the length
you specify,
which by default is 8 bits. Prefer to pass a smaller length
if possible.
Parameters
length
number
= 8
the number of bits to fit the element. If the element does not fit in length
bits, the functions throws an error.
Returns
Bool
[]
An array of Bool element representing little endian binary representation of this UInt8.
toNumber()
toNumber(): number
Defined in: lib/provable/int.ts:1830
Serialize the UInt8 to a number.
Warning: This operation is not provable.
Returns
number
toString()
toString(): string
Defined in: lib/provable/int.ts:1821
Serialize the UInt8 to a string, e.g. for printing.
Warning: This operation is not provable.
Returns
string
toUInt32()
toUInt32(): UInt32
Defined in: lib/provable/int.ts:1859
Returns
toUInt64()
toUInt64(): UInt64
Defined in: lib/provable/int.ts:1866
Returns
check()
static check(x:
| Field
| {
value: Field;
}): void
Defined in: lib/provable/int.ts:1847
Provable.check for UInt8. Proves that the input is in the [0, 255] range.
Parameters
x
Returns
void
Overrides
Struct({
value: Field,
}).check
from()
static from(x:
| number
| bigint
| Field
| UInt64
| UInt32
| UInt8): UInt8
Defined in: lib/provable/int.ts:1880
Creates a new UInt8.
Parameters
x
number
| bigint
| Field
| UInt64
| UInt32
| UInt8
Returns
fromBits()
static fromBits(bits: (boolean | Bool)[]): UInt8
Defined in: lib/provable/int.ts:1946
Convert a bit array into a UInt8 element using little endian binary representation
The method throws if the given bits do not fit in a single UInt8 element. In this case, no more than 8 bits are allowed.
Important: If the given bits
array is an array of booleans
or Bool elements that all are constant
,
the resulting UInt8 element will be a constant as well. Or else, if the given array is a mixture of constants and variables of Bool type,
the resulting UInt8 will be a variable as well.
Parameters
bits
(boolean
| Bool
)[]
An array of Bool or boolean
type.
Returns
A UInt8 element matching the little endian binary representation of the given bits
array.
fromValue()
static fromValue(x:
| number
| UInt8
| {
value: string | number | bigint | Field;
}): UInt8
Defined in: lib/provable/int.ts:1891
Parameters
x
number
| UInt8
| {
value
: string
| number
| bigint
| Field
;
}
Returns
Overrides
Struct({
value: Field,
}).fromValue
MAXINT()
static MAXINT(): UInt8
Defined in: lib/provable/int.ts:1873
Creates a UInt8 with a value of 255.
Returns
sizeInFields()
static sizeInFields(): number
Defined in: lib/provable/types/provable-intf.ts:66
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
toInput()
static toInput(x: {
value: Field;
}): HashInput
Defined in: lib/provable/int.ts:1852
Parameters
x
value
Returns
HashInput
Overrides
Struct({
value: Field,
}).toInput