Skip to main content

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​

UInt8

Overrides​

Struct({ value: Field, }).constructor

Source​

lib/provable/int.ts:1456

Properties​

value​

value: Field = Field;

Inherited from​

Struct({ value: Field, }).value

Source​

lib/provable/int.ts:1446


NUM_BITS​

static NUM_BITS: number = 8;

Source​

lib/provable/int.ts:1448


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​

UInt8

Source​

lib/provable/int.ts:1462


_isStruct​

static _isStruct: true;

Inherited from​

Struct({ value: Field, })._isStruct

Source​

lib/provable/types/struct.ts:148


empty()​

static empty: () => {
"value": Field;
};

Returns​

{
"value": Field;
}
value​
value: Field = Field;

Inherited from​

Struct({ value: Field, }).empty

Source​

lib/provable/types/struct.ts:158


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:115


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:157


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:86


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:47


toCanonical()?​

static optional toCanonical: (x: {
"value": Field;
}) => {
"value": Field;
};

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

• x.value: Field= Field

Returns​

{
"value": Field;
}
value​
value: Field = Field;

Inherited from​

Struct({ value: Field, }).toCanonical

Source​

lib/provable/types/provable-intf.ts:104


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:36


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:156


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:81

Accessors​

one​

get static one(): UInt8

Static method to create a UInt8 with value 1.

Returns​

UInt8

Source​

lib/provable/int.ts:1483


zero​

get static zero(): UInt8

Static method to create a UInt8 with value 0.

Returns​

UInt8

Source​

lib/provable/int.ts:1477

Methods​

add()​

add(y: number | bigint | UInt8): UInt8

Add a UInt8 to another UInt8 without allowing overflow.

Parameters​

• y: number | bigint | UInt8

Returns​

UInt8

Example​

const x = UInt8.from(3);
const sum = x.add(5);
sum.assertEquals(8);

Throws​

if the result is greater than 255.

Source​

lib/provable/int.ts:1499


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​

lib/provable/int.ts:1765


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​

lib/provable/int.ts:1741


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​

lib/provable/int.ts:1753


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​

lib/provable/int.ts:1659


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​

lib/provable/int.ts:1684


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​

UInt8

Example​

const x = UInt8.from(7);
const quotient = x.div(2);
quotient.assertEquals(3);

Source​

lib/provable/int.ts:1552


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​

lib/provable/int.ts:1579


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​

Bool

Example​

// 5 > 3
UInt8.from(5).greaterThan(3);

Source​

lib/provable/int.ts:1715


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​

Bool

Example​

// 3 >= 3
UInt8.from(3).greaterThanOrEqual(3);

Source​

lib/provable/int.ts:1729


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​

Bool

Example​

UInt8.from(2).lessThan(UInt8.from(3));

Source​

lib/provable/int.ts:1638


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​

Bool

Example​

UInt8.from(3).lessThanOrEqual(UInt8.from(5));

Source​

lib/provable/int.ts:1616


mod()​

mod(y: number | bigint | UInt8): UInt8

Get the remainder a UInt8 of division of another UInt8.

Parameters​

• y: number | bigint | UInt8

Returns​

UInt8

Example​

const x = UInt8.from(50);
const mod = x.mod(30);
mod.assertEquals(20);

Source​

lib/provable/int.ts:1566


mul()​

mul(y: number | bigint | UInt8): UInt8

Multiply a UInt8 by another UInt8 without allowing overflow.

Parameters​

• y: number | bigint | UInt8

Returns​

UInt8

Example​

const x = UInt8.from(3);
const product = x.mul(5);
product.assertEquals(15);

Throws​

if the result is greater than 255.

Source​

lib/provable/int.ts:1535


sub()​

sub(y: number | bigint | UInt8): UInt8

Subtract a UInt8 from another UInt8 without allowing underflow.

Parameters​

• y: number | bigint | UInt8

Returns​

UInt8

Example​

const x = UInt8.from(8);
const difference = x.sub(5);
difference.assertEquals(3);

Throws​

if the result is less than 0.

Source​

lib/provable/int.ts:1517


toBigInt()​

toBigInt(): bigint

Serialize the UInt8 to a bigint.

Warning: This operation is not provable.

Returns​

bigint

Source​

lib/provable/int.ts:1793


toNumber()​

toNumber(): number

Serialize the UInt8 to a number.

Warning: This operation is not provable.

Returns​

number

Source​

lib/provable/int.ts:1784


toString()​

toString(): string

Serialize the UInt8 to a string, e.g. for printing.

Warning: This operation is not provable.

Returns​

string

Source​

lib/provable/int.ts:1775


toUInt32()​

toUInt32(): UInt32

Turns a UInt8 into a UInt32.

Returns​

UInt32

Source​

lib/provable/int.ts:1813


toUInt64()​

toUInt64(): UInt64

Turns a UInt8 into a UInt64.

Returns​

UInt64

Source​

lib/provable/int.ts:1820


MAXINT()​

static MAXINT(): UInt8

Creates a UInt8 with a value of 255.

Returns​

UInt8

Source​

lib/provable/int.ts:1827


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​

lib/provable/int.ts:1801


from()​

static from(x: 
| number
| bigint
| Field
| UInt64
| UInt32
| UInt8): UInt8

Creates a new UInt8.

Parameters​

• x: | number | bigint | Field | UInt64 | UInt32 | UInt8

Returns​

UInt8

Source​

lib/provable/int.ts:1834


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:66


toInput()​

static toInput(x: {
"value": Field;
}): HashInput

Parameters​

• x

• x.value: Field

Returns​

HashInput

Overrides​

Struct({ value: Field, }).toInput

Source​

lib/provable/int.ts:1806