UInt8
Defined in: lib/provable/int.ts:1511
A 8 bit unsigned integer with values ranging from 0 to 255.
Extends
- {
value
:Field
; }
Constructors
Constructor
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
UInt8
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()
fromField(x: Field): UInt8;
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
UInt8
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
UInt8
zero
Get Signature
get static zero(): UInt8;
Defined in: lib/provable/int.ts:1543
Static method to create a UInt8 with value 0
.
Returns
UInt8
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
UInt8
Example
const x = UInt8.from(3);
const sum = x.add(5);
sum.assertEquals(8);
Throws
if the result is greater than 255.
and()
and(x: UInt8): UInt8;
Defined in: lib/provable/int.ts:1752
Bitwise AND gadget on UInt8 elements. Equivalent to the bitwise AND &
operator in JavaScript.
The AND gate works by comparing two bits and returning 1
if both bits are 1
, and 0
otherwise.
It can be checked by a double generic gate that verifies the following relationship between the values below.
The generic gate verifies:
a + b = sum
and the conjunction equation 2 * and = sum - xor
Where:
a + b = sum
a ^ b = xor
a & b = and
You can find more details about the implementation in the Mina book
Parameters
x
UInt8
Returns
UInt8
Example
let a = UInt8.from(3); // ... 000011
let b = UInt8.from(5); // ... 000101
let c = a.and(b); // ... 000001
c.assertEquals(1);
assertEquals()
assertEquals(y: number | bigint | UInt8, message?: string): void;
Defined in: lib/provable/int.ts:1911
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:1887
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:1899
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
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:1815
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:1837
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
UInt8
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:1861
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:1875
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:2005
Returns
boolean
lessThan()
lessThan(y: number | bigint | UInt8): Bool;
Defined in: lib/provable/int.ts:1799
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:1782
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
UInt8
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
UInt8
Example
const x = UInt8.from(3);
const product = x.mul(5);
product.assertEquals(15);
Throws
if the result is greater than 255.
not()
not(): UInt8;
Defined in: lib/provable/int.ts:1723
Bitwise NOT gate on Field elements. Similar to the [bitwise
NOT ~
operator in JavaScript](https://developer.mozilla.org/en-US/docs/
Web/JavaScript/Reference/Operators/Bitwise_NOT).
Note: The NOT gate operates over 8 bit for UInt8 types.
A NOT gate works by returning 1
in each bit position if the
corresponding bit of the operand is 0
, and returning 0
if the
corresponding bit of the operand is 1
.
NOT is implemented as a subtraction of the input from the all one bitmask
You can find more details about the implementation in the Mina book
Returns
UInt8
Example
// NOTing 4 bits with the unchecked version
let a = UInt8.from(0b0101);
let b = a.not();
console.log(b.toBigInt().toString(2));
// 11111010
or()
or(x: UInt8): UInt8;
Defined in: lib/provable/int.ts:1769
Bitwise OR gadget on UInt8 elements. Equivalent to the bitwise OR |
operator in JavaScript.
The OR gate works by comparing two bits and returning 1
if at least one bit is 1
, and 0
otherwise.
Parameters
x
UInt8
Returns
UInt8
Example
let a = UInt8.from(3); // ... 000011
let b = UInt8.from(5); // ... 000101
let c = a.or(b); // ... 000111
c.assertEquals(7);
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
UInt8
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:1939
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:2022
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:1930
Serialize the UInt8 to a number.
Warning: This operation is not provable.
Returns
number
toString()
toString(): string;
Defined in: lib/provable/int.ts:1921
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:1959
Turns a UInt8 into a UInt32.
Returns
toUInt64()
toUInt64(): UInt64;
Defined in: lib/provable/int.ts:1966
Turns a UInt8 into a UInt64.
Returns
xor()
xor(x: UInt8): UInt8;
Defined in: lib/provable/int.ts:1692
Bitwise XOR gadget on Field elements. Equivalent to the bitwise XOR ^
operator in JavaScript.
A XOR gate works by comparing two bits and returning 1
if two bits differ, and 0
if two bits are equal.
This gadget builds a chain of XOR gates recursively.
You can find more details about the implementation in the Mina book
Parameters
x
UInt8
UInt8 element to XOR.
Returns
UInt8
Example
let a = UInt8.from(0b0101);
let b = UInt8.from(0b0011);
let c = a.xor(b);
c.assertEquals(0b0110);
check()
static check(x:
| Field
| {
value: Field;
}): void;
Defined in: lib/provable/int.ts:1947
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:1980
Creates a new UInt8.
Parameters
x
number
| bigint
| Field
| UInt64
| UInt32
| UInt8
Returns
UInt8
fromBits()
static fromBits(bits: (boolean | Bool)[]): UInt8;
Defined in: lib/provable/int.ts:2046
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
UInt8
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:1991
Parameters
x
number
| UInt8
| {
value
: string
| number
| bigint
| Field
;
}
Returns
UInt8
Overrides
Struct({
value: Field,
}).fromValue
MAXINT()
static MAXINT(): UInt8;
Defined in: lib/provable/int.ts:1973
Creates a UInt8 with a value of 255.
Returns
UInt8
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:1952
Parameters
x
value
Returns
HashInput
Overrides
Struct({
value: Field,
}).toInput