UInt32
Defined in: lib/provable/int.ts:583
A 32 bit unsigned integer with values ranging from 0 to 4,294,967,295.
Extends
CircuitValue
Constructors
new UInt32()
new UInt32(x: string | number | bigint | FieldVar | UInt32): UInt32
Defined in: lib/provable/int.ts:593
Create a UInt32.
The max value of a UInt32 is 2^32 - 1 = UInt32.MAXINT()
.
Warning: Cannot overflow, an error is thrown if the result is greater than UInt32.MAXINT()
Parameters
x
string
| number
| bigint
| FieldVar
| UInt32
Returns
Overrides
CircuitValue.constructor
Properties
value
value: Field;
Defined in: lib/provable/int.ts:584
NUM_BITS
static NUM_BITS: number = 32;
Defined in: lib/provable/int.ts:585
Unsafe
static Unsafe: {
fromField: UInt32;
};
Defined in: lib/provable/int.ts:601
fromField()
Create a UInt32 from a Field without constraining its range.
Warning: This is unsafe, because it does not prove that the input Field actually fits in 32 bits.
Only use this if you know what you are doing, otherwise use the safe UInt32.from.
Parameters
x
Returns
Accessors
one
Get Signature
get static one(): UInt32
Defined in: lib/provable/int.ts:623
Static method to create a UInt32 with value 0
.
Returns
zero
Get Signature
get static zero(): UInt32
Defined in: lib/provable/int.ts:616
Static method to create a UInt32 with value 0
.
Returns
Methods
add()
add(y: number | UInt32): UInt32
Defined in: lib/provable/int.ts:765
Addition with overflow checking.
Parameters
y
number
| UInt32
Returns
addMod32()
addMod32(y: UInt32): UInt32
Defined in: lib/provable/int.ts:694
Addition modulo 2^32. Check Gadgets.addMod32 for a detailed description.
Parameters
y
Returns
and()
and(x: UInt32): UInt32
Defined in: lib/provable/int.ts:935
Bitwise AND gadget on UInt32 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
Returns
Example
let a = UInt32.from(3); // ... 000011
let b = UInt32.from(5); // ... 000101
let c = a.and(b, 2); // ... 000001
c.assertEquals(1);
assertEquals()
assertEquals(x: this): void
Defined in: lib/provable/types/circuit-value.ts:121
Parameters
x
this
Returns
void
Inherited from
CircuitValue.assertEquals
assertGreaterThan()
assertGreaterThan(y: UInt32, message?: string): void
Defined in: lib/provable/int.ts:1016
Asserts that a UInt32 is greater than another one.
Parameters
y
message?
string
Returns
void
assertGreaterThanOrEqual()
assertGreaterThanOrEqual(y: UInt32, message?: string): void
Defined in: lib/provable/int.ts:1030
Asserts that a UInt32 is greater than or equal to another one.
Parameters
y
message?
string
Returns
void
assertLessThan()
assertLessThan(y: UInt32, message?: string): void
Defined in: lib/provable/int.ts:996
Asserts that a UInt32 is less than another one.
Parameters
y
message?
string
Returns
void
assertLessThanOrEqual()
assertLessThanOrEqual(y: UInt32, message?: string): void
Defined in: lib/provable/int.ts:971
Asserts that a UInt32 is less than or equal to another one.
Parameters
y
message?
string
Returns
void
div()
div(y: number | UInt32): UInt32
Defined in: lib/provable/int.ts:742
Integer division.
x.div(y)
returns the floor of x / y
, that is, the greatest
z
such that x * y <= x
.
Parameters
y
number
| UInt32
Returns
divMod()
divMod(y: string | number | UInt32): {
quotient: UInt32;
rest: UInt32;
}
Defined in: lib/provable/int.ts:703
Integer division with remainder.
x.divMod(y)
returns the quotient and the remainder.
Parameters
y
string
| number
| UInt32
Returns
{
quotient: UInt32;
rest: UInt32;
}
quotient
quotient: UInt32;
rest
rest: UInt32;
equals()
equals(x: this): Bool
Defined in: lib/provable/types/circuit-value.ts:117
Parameters
x
this
Returns
Inherited from
CircuitValue.equals
greaterThan()
greaterThan(y: UInt32): Bool
Defined in: lib/provable/int.ts:1009
Checks if a UInt32 is greater than another one.
Parameters
y
Returns
greaterThanOrEqual()
greaterThanOrEqual(y: UInt32): Bool
Defined in: lib/provable/int.ts:1023
Checks if a UInt32 is greater than or equal to another one.
Parameters
y
Returns
isConstant()
isConstant(): boolean
Defined in: lib/provable/types/circuit-value.ts:125
Returns
boolean
Inherited from
CircuitValue.isConstant
leftShift()
leftShift(bits: number): UInt32
Defined in: lib/provable/int.ts:883
Performs a left shift operation on the provided UInt32 element.
This operation is similar to the <<
shift operation in JavaScript,
where bits are shifted to the left, and the overflowing bits are discarded.
It’s important to note that these operations are performed considering the big-endian 32-bit representation of the number, where the most significant (32th) bit is on the left end and the least significant bit is on the right end.
The operation expects the input to be range checked to 32 bit.
Parameters
bits
number
Amount of bits to shift the UInt32 element to the left. The amount should be between 0 and 32 (or else the shift will fail).
Returns
Example
const x = UInt32.from(0b001100); // 12 in binary
const y = x.leftShift(2); // left shift by 2 bits
y.assertEquals(0b110000); // 48 in binary
lessThan()
lessThan(y: UInt32): Bool
Defined in: lib/provable/int.ts:984
Checks if a UInt32 is less than another one.
Parameters
y
Returns
lessThanOrEqual()
lessThanOrEqual(y: UInt32): Bool
Defined in: lib/provable/int.ts:959
Checks if a UInt32 is less than or equal to another one.
Parameters
y
Returns
mod()
mod(y: number | UInt32): UInt32
Defined in: lib/provable/int.ts:751
Integer remainder.
x.mod(y)
returns the value z
such that 0 <= z < y
and
x - z
is divisible by y
.
Parameters
y
number
| UInt32
Returns
mul()
mul(y: number | UInt32): UInt32
Defined in: lib/provable/int.ts:757
Multiplication with overflow checking.
Parameters
y
number
| UInt32
Returns
not()
not(): UInt32
Defined in: lib/provable/int.ts:828
Bitwise NOT gate on UInt32 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 32 bit for UInt32 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
Example
// NOTing 4 bits with the unchecked version
let a = UInt32.from(0b0101);
let b = a.not();
console.log(b.toBigInt().toString(2));
// 11111111111111111111111111111010
or()
or(x: UInt32): UInt32
Defined in: lib/provable/int.ts:952
Bitwise OR gadget on UInt32 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
Returns
Example
let a = UInt32.from(3); // ... 000011
let b = UInt32.from(5); // ... 000101
let c = a.or(b); // ... 000111
c.assertEquals(7);
rightShift()
rightShift(bits: number): UInt32
Defined in: lib/provable/int.ts:906
Performs a left right operation on the provided UInt32 element.
This operation is similar to the >>
shift operation in JavaScript,
where bits are shifted to the right, and the overflowing bits are discarded.
It’s important to note that these operations are performed considering the big-endian 32-bit representation of the number, where the most significant (32th) bit is on the left end and the least significant bit is on the right end.
Parameters
bits
number
Amount of bits to shift the UInt32 element to the right. The amount should be between 0 and 32 (or else the shift will fail).
The operation expects the input to be range checked to 32 bit.
Returns
Example
const x = UInt32.from(0b001100); // 12 in binary
const y = x.rightShift(2); // left shift by 2 bits
y.assertEquals(0b000011); // 48 in binary
rotate()
rotate(bits: number, direction: "left" | "right"): UInt32
Defined in: lib/provable/int.ts:860
A (left and right) rotation operates similarly to the shift operation (<<
for left and >>
for right) in JavaScript,
with the distinction that the bits are circulated to the opposite end of a 64-bit representation rather than being discarded.
For a left rotation, this means that bits shifted off the left end reappear at the right end.
Conversely, for a right rotation, bits shifted off the right end reappear at the left end.
It’s important to note that these operations are performed considering the big-endian 64-bit representation of the number,
where the most significant (64th) bit is on the left end and the least significant bit is on the right end.
The direction
parameter is a string that accepts either 'left'
or 'right'
, determining the direction of the rotation.
To safely use rotate()
, you need to make sure that the value passed in is range-checked to 64 bits;
for example, using Gadgets.rangeCheck64.
You can find more details about the implementation in the Mina book
Parameters
bits
number
amount of bits to rotate this UInt32 element with.
direction
left or right rotation direction.
"left"
| "right"
Returns
Example
const x = UInt32.from(0b001100);
const y = x.rotate(2, 'left');
const z = x.rotate(2, 'right'); // right rotation by 2 bits
y.assertEquals(0b110000);
z.assertEquals(0b000011);
sub()
sub(y: number | UInt32): UInt32
Defined in: lib/provable/int.ts:773
Subtraction with underflow checking.
Parameters
y
number
| UInt32
Returns
toBigint()
toBigint(): bigint
Defined in: lib/provable/int.ts:635
Turns the UInt32 into a BigInt.
Returns
bigint
toBits()
toBits(length: number): Bool[]
Defined in: lib/provable/int.ts:1084
Returns an array of Bool elements representing little endian binary representation of this UInt32 element.
If you use the optional length
argument, proves that the UInt32 element fits in length
bits.
The length
has to be between 0 and 32 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 32 bits. Prefer to pass a smaller length
if possible.
Parameters
length
number
= 32
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 UInt32.
toBytes()
toBytes(): [UInt8, UInt8, UInt8, UInt8]
Defined in: lib/provable/int.ts:1045
Split a UInt32 into 4 UInt8s, in little-endian order.
Returns
toBytesBE()
toBytesBE(): [UInt8, UInt8, UInt8, UInt8]
Defined in: lib/provable/int.ts:1052
Split a UInt32 into 4 UInt8s, in big-endian order.
Returns
toConstant()
toConstant(): this
Defined in: lib/provable/types/circuit-value.ts:113
Returns
this
Inherited from
CircuitValue.toConstant
toFields()
toFields(): Field[]
Defined in: lib/provable/types/circuit-value.ts:79
Returns
Field
[]
Inherited from
CircuitValue.toFields
toJSON()
toJSON(): any
Defined in: lib/provable/types/circuit-value.ts:109
Returns
any
Inherited from
CircuitValue.toJSON
toString()
toString(): string
Defined in: lib/provable/int.ts:629
Turns the UInt32 into a string.
Returns
string
toUInt64()
toUInt64(): UInt64
Defined in: lib/provable/int.ts:641
Turns the UInt32 into a UInt64.
Returns
xor()
xor(x: UInt32): UInt32
Defined in: lib/provable/int.ts:798
Bitwise XOR gadget on UInt32 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
UInt32 element to compare.
Returns
Example
let a = UInt32.from(0b0101);
let b = UInt32.from(0b0011);
let c = a.xor(b);
c.assertEquals(0b0110);
check()
static check(x: UInt32): void
Defined in: lib/provable/int.ts:646
Parameters
x
Returns
void
Overrides
CircuitValue.check
empty()
static empty<T>(): InstanceType<T>
Defined in: lib/provable/types/circuit-value.ts:205
Type Parameters
• T extends AnyConstructor
Returns
InstanceType
<T
>
Inherited from
CircuitValue.empty
from()
static from(x: string | number | bigint | UInt32): UInt32
Defined in: lib/provable/int.ts:679
Creates a new UInt32.
Parameters
x
string
| number
| bigint
| UInt32
Returns
fromBits()
static fromBits(bits: (boolean | Bool)[]): UInt32
Defined in: lib/provable/int.ts:1108
Convert a bit array into a UInt32 element using little endian binary representation
The method throws if the given bits do not fit in a single UInt32 element. In this case, no more than 32 bits are allowed.
Important: If the given bits
array is an array of booleans
or Bool elements that all are constant
,
the resulting UInt32 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 UInt32 will be a variable as well.
Parameters
bits
(boolean
| Bool
)[]
An array of Bool or boolean
type.
Returns
A UInt32 element matching the little endian binary representation of the given bits
array.
fromBytes()
static fromBytes(bytes: UInt8[]): UInt32
Defined in: lib/provable/int.ts:1059
Combine 4 UInt8s into a UInt32, in little-endian order.
Parameters
bytes
UInt8
[]
Returns
fromBytesBE()
static fromBytesBE(bytes: UInt8[]): UInt32
Defined in: lib/provable/int.ts:1067
Combine 4 UInt8s into a UInt32, in big-endian order.
Parameters
bytes
UInt8
[]
Returns
fromFields()
static fromFields<T>(this: T, xs: Field[]): InstanceType<T>
Defined in: lib/provable/types/circuit-value.ts:129
Type Parameters
• T extends AnyConstructor
Parameters
this
T
xs
Field
[]
Returns
InstanceType
<T
>
Inherited from
CircuitValue.fromFields
fromJSON()
static fromJSON<T>(x: string): InstanceType<T>
Defined in: lib/provable/int.ts:662
Decodes a JSON-like object into this structure.
Type Parameters
• T extends AnyConstructor
Parameters
x
string
Returns
InstanceType
<T
>
Overrides
CircuitValue.fromJSON
fromObject()
static fromObject<T>(this: T, value: NonMethods<InstanceType<T>>): InstanceType<T>
Defined in: lib/provable/types/circuit-value.ts:30
Type Parameters
• T extends AnyConstructor
Parameters
this
T
value
NonMethods
<InstanceType
<T
>>
Returns
InstanceType
<T
>
Inherited from
CircuitValue.fromObject
fromValue()
static fromValue<T>(x: number | bigint | UInt32): InstanceType<T>
Defined in: lib/provable/int.ts:1038
Type Parameters
• T extends AnyConstructor
Parameters
x
number
| bigint
| UInt32
Returns
InstanceType
<T
>
Overrides
CircuitValue.fromValue
MAXINT()
static MAXINT(): UInt32
Defined in: lib/provable/int.ts:687
Creates a UInt32 with a value of 4,294,967,295.
Returns
sizeInFields()
static sizeInFields(): number
Defined in: lib/provable/types/circuit-value.ts:37
Returns
number
Inherited from
CircuitValue.sizeInFields
toAuxiliary()
static toAuxiliary(): []
Defined in: lib/provable/types/circuit-value.ts:56
Returns
[]
Inherited from
CircuitValue.toAuxiliary
toCanonical()
static toCanonical<T>(this: T, value: InstanceType<T>): InstanceType<T>
Defined in: lib/provable/types/circuit-value.ts:161
Type Parameters
• T extends AnyConstructor
Parameters
this
T
value
InstanceType
<T
>
Returns
InstanceType
<T
>
Inherited from
CircuitValue.toCanonical
toConstant()
static toConstant<T>(this: T, t: InstanceType<T>): InstanceType<T>
Defined in: lib/provable/types/circuit-value.ts:170
Type Parameters
• T extends AnyConstructor
Parameters
this
T
t
InstanceType
<T
>
Returns
InstanceType
<T
>
Inherited from
CircuitValue.toConstant
toFields()
static toFields<T>(this: T, v: InstanceType<T>): Field[]
Defined in: lib/provable/types/circuit-value.ts:42
Type Parameters
• T extends AnyConstructor
Parameters
this
T
v
InstanceType
<T
>
Returns
Field
[]
Inherited from
CircuitValue.toFields
toInput()
static toInput(x: UInt32): HashInput
Defined in: lib/provable/int.ts:649
Parameters
x
Returns
HashInput
Overrides
CircuitValue.toInput
toJSON()
static toJSON(x: UInt32): string
Defined in: lib/provable/int.ts:655
Encodes this structure into a JSON-like object.
Parameters
x
Returns
string
Overrides
CircuitValue.toJSON
toValue()
static toValue(x: UInt32): bigint
Defined in: lib/provable/int.ts:1034
Parameters
x
Returns
bigint
Overrides
CircuitValue.toValue