Skip to main content

UInt8

o1js / Modules / UInt8

Class: UInt8

A 8 bit unsigned integer with values ranging from 0 to 255.

Hierarchy

  • { value: Field = Field }

    UInt8

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new UInt8(x)

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

NameType
xnumber | bigint | FieldVar | UInt8

Overrides

Struct({ value: Field, }).constructor

Defined in

lib/int.ts:1314

Properties

value

value: Field = Field

Inherited from

Struct({ value: Field, }).value

Defined in

lib/int.ts:1304


NUM_BITS

Static NUM_BITS: number = 8

Defined in

lib/int.ts:1306


Unsafe

Static Unsafe: Object

Type declaration

NameType
fromField(x: Field) => UInt8

Defined in

lib/int.ts:1320


_isStruct

Static _isStruct: true

Inherited from

Struct({ value: Field, })._isStruct

Defined in

lib/circuit-value.ts:382


empty

Static empty: () => { value: Field = Field }

Type declaration

▸ (): Object

Returns

Object

NameType
valueField

Inherited from

Struct({ value: Field, }).empty

Defined in

lib/circuit-value.ts:391


fromFields

Static fromFields: (fields: Field[]) => { value: Field = Field }

Type declaration

▸ (fields): Object

A function that returns an element of type T from the given provable data.

Important: For any element of type T, this function is the reverse operation of calling toFields method on an element of type T.

Parameters
NameTypeDescription
fieldsField[]an array of Field elements describing the provable data of the new T element.
Returns

Object

An element of type T generated from the given provable data.

NameType
valueField

Inherited from

Struct({ value: Field, }).fromFields

Defined in

snarky.d.ts:138


fromJSON

Static fromJSON: (x: { value: string = Field }) => { value: Field = Field }

Type declaration

▸ (x): Object

Parameters
NameTypeDefault value
xObjectundefined
x.valuestringField
Returns

Object

NameType
valueField

Inherited from

Struct({ value: Field, }).fromJSON

Defined in

lib/circuit-value.ts:390


toAuxiliary

Static toAuxiliary: (value?: { value: Field = Field }) => any[]

Type declaration

▸ (value?): 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. As any element of the interface ProvablePure<T> includes no "auxiliary" data by definition, this function always returns a default value.

Parameters
NameTypeDefault valueDescription
value?Objectundefinedthe element of type T to generate the auxiliary data array from, optional. If not provided, a default value for auxiliary data is returned.
value.valueFieldField-
Returns

any[]

An empty array, as any element of the interface ProvablePure<T> includes no "auxiliary" data by definition.

Inherited from

Struct({ value: Field, }).toAuxiliary

Defined in

snarky.d.ts:127


toFields

Static toFields: (value: { value: Field = Field }) => Field[]

Type declaration

▸ (value): 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
NameTypeDefault valueDescription
valueObjectundefinedthe element of type T to generate the Field array from.
value.valueFieldField-
Returns

Field[]

A Field array describing how this T element is made up of Field elements.

Inherited from

Struct({ value: Field, }).toFields

Defined in

snarky.d.ts:117


toJSON

Static toJSON: (x: { value: Field = Field }) => { value: string = Field }

Type declaration

▸ (x): Object

Parameters
NameTypeDefault value
xObjectundefined
x.valueFieldField
Returns

Object

NameType
valuestring

Inherited from

Struct({ value: Field, }).toJSON

Defined in

lib/circuit-value.ts:389

Methods

add

add(y): UInt8

Add a UInt8 to another UInt8 without allowing overflow.

Parameters

NameType
ynumber | 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.

Defined in

lib/int.ts:1344


assertEquals

assertEquals(y, message?): void

Assert that this UInt8 is equal another UInt8 value.

Important: If an assertion fails, the code throws an error.

Parameters

NameTypeDescription
ynumber | bigint | UInt8the UInt8 value to compare & assert with this UInt8.
message?string-

Returns

void

Defined in

lib/int.ts:1598


assertGreaterThan

assertGreaterThan(y, message?): void

Assert that this UInt8 is greater than another UInt8 value.

Important: If an assertion fails, the code throws an error.

Parameters

NameTypeDescription
ynumber | bigint | UInt8the UInt8 value to compare & assert with this UInt8.
message?string-

Returns

void

Defined in

lib/int.ts:1574


assertGreaterThanOrEqual

assertGreaterThanOrEqual(y, message?): 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

NameTypeDescription
yUInt8the UInt8 value to compare & assert with this UInt8.
message?string-

Returns

void

Defined in

lib/int.ts:1586


assertLessThan

assertLessThan(y, message?): void

Assert that this UInt8 is less than another UInt8 value.

Important: If an assertion fails, the code throws an error.

Parameters

NameTypeDescription
ynumber | bigint | UInt8the UInt8 value to compare & assert with this UInt8.
message?string-

Returns

void

Defined in

lib/int.ts:1494


assertLessThanOrEqual

assertLessThanOrEqual(y, message?): 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

NameTypeDescription
ynumber | bigint | UInt8the UInt8 value to compare & assert with this UInt8.
message?string-

Returns

void

Defined in

lib/int.ts:1518


div

div(y): UInt8

Divide a UInt8 by another UInt8. This is integer division that rounds down.

Parameters

NameType
ynumber | bigint | UInt8

Returns

UInt8

Example

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

Defined in

lib/int.ts:1397


divMod

divMod(y): Object

Get the quotient and remainder of a UInt8 divided by another UInt8:

x == y * q + r, where 0 <= r < y.

Parameters

NameTypeDescription
ynumber | bigint | UInt8a UInt8 to get the quotient and remainder of another UInt8.

Returns

Object

The quotient q and remainder r.

NameType
quotientUInt8
remainderUInt8

Defined in

lib/int.ts:1424


greaterThan

greaterThan(y): Bool

Check if this UInt8 is greater than another UInt8. Returns a Bool.

Parameters

NameType
ynumber | bigint | UInt8

Returns

Bool

Example

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

Defined in

lib/int.ts:1548


greaterThanOrEqual

greaterThanOrEqual(y): Bool

Check if this UInt8 is greater than or equal another UInt8 value. Returns a Bool.

Parameters

NameType
ynumber | bigint | UInt8

Returns

Bool

Example

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

Defined in

lib/int.ts:1562


lessThan

lessThan(y): Bool

Check if this UInt8 is less than another UInt8 value. Returns a Bool.

Parameters

NameType
ynumber | bigint | UInt8

Returns

Bool

Example

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

Defined in

lib/int.ts:1478


lessThanOrEqual

lessThanOrEqual(y): Bool

Check if this UInt8 is less than or equal to another UInt8 value. Returns a Bool.

Parameters

NameType
ynumber | bigint | UInt8

Returns

Bool

Example

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

Defined in

lib/int.ts:1461


mod

mod(y): UInt8

Get the remainder a UInt8 of division of another UInt8.

Parameters

NameType
ynumber | bigint | UInt8

Returns

UInt8

Example

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

Defined in

lib/int.ts:1411


mul

mul(y): UInt8

Multiply a UInt8 by another UInt8 without allowing overflow.

Parameters

NameType
ynumber | 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.

Defined in

lib/int.ts:1380


sub

sub(y): UInt8

Subtract a UInt8 from another UInt8 without allowing underflow.

Parameters

NameType
ynumber | 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.

Defined in

lib/int.ts:1362


toBigInt

toBigInt(): bigint

Serialize the UInt8 to a bigint.

Warning: This operation is not provable.

Returns

bigint

Defined in

lib/int.ts:1626


toNumber

toNumber(): number

Serialize the UInt8 to a number.

Warning: This operation is not provable.

Returns

number

Defined in

lib/int.ts:1617


toString

toString(): string

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

Warning: This operation is not provable.

Returns

string

Defined in

lib/int.ts:1608


toUInt32

toUInt32(): UInt32

Turns a UInt8 into a UInt32.

Returns

UInt32

Defined in

lib/int.ts:1646


toUInt64

toUInt64(): UInt64

Turns a UInt8 into a UInt64.

Returns

UInt64

Defined in

lib/int.ts:1653


MAXINT

Static MAXINT(): UInt8

Creates a UInt8 with a value of 255.

Returns

UInt8

Defined in

lib/int.ts:1660


check

Static check(x): void

check for UInt8. Proves that the input is in the [0, 255] range.

Parameters

NameType
xField | { value: Field }

Returns

void

Overrides

Struct({ value: Field, }).check

Defined in

lib/int.ts:1634


checkConstant

Static Private checkConstant(x): void

Parameters

NameType
xField

Returns

void

Defined in

lib/int.ts:1678


from

Static from(x): UInt8

Creates a new UInt8.

Parameters

NameType
xnumber | bigint | Field | UInt64 | UInt32 | UInt8

Returns

UInt8

Defined in

lib/int.ts:1667


sizeInFields

Static sizeInFields(): number

Return the size of the T type in terms of Field type, as Field is the primitive type.

Warning: This function returns a number, so you cannot use it to prove something on chain. You can use it during debugging or to understand the memory complexity of some type.

Returns

number

A number representing the size of the T type in terms of Field type.

Inherited from

Struct({ value: Field, }).sizeInFields

Defined in

snarky.d.ts:147


toInput

Static toInput(x): HashInput

Parameters

NameType
xObject
x.valueField

Returns

HashInput

Overrides

Struct({ value: Field, }).toInput

Defined in

lib/int.ts:1639