Skip to main content

UInt32

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

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

UInt32

Overrides

CircuitValue.constructor

Source

lib/provable/int.ts:514

Properties

value

value: Field;

Source

lib/provable/int.ts:505


NUM_BITS

static NUM_BITS: number = 32;

Source

lib/provable/int.ts:506


Unsafe

static Unsafe: {
"fromField": UInt32;
};

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

Returns

UInt32

Source

lib/provable/int.ts:522

Accessors

one

get static one(): UInt32

Static method to create a UInt32 with value 0.

Returns

UInt32

Source

lib/provable/int.ts:544


zero

get static zero(): UInt32

Static method to create a UInt32 with value 0.

Returns

UInt32

Source

lib/provable/int.ts:537

Methods

add()

add(y: number | UInt32): UInt32

Addition with overflow checking.

Parameters

y: number | UInt32

Returns

UInt32

Source

lib/provable/int.ts:691


addMod32()

addMod32(y: UInt32): UInt32

Addition modulo 2^32. Check Gadgets.addMod32 for a detailed description.

Parameters

y: UInt32

Returns

UInt32

Source

lib/provable/int.ts:617


and()

and(x: UInt32): UInt32

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

Returns

UInt32

Example

let a = UInt32.from(3);    // ... 000011
let b = UInt32.from(5); // ... 000101

let c = a.and(b, 2); // ... 000001
c.assertEquals(1);

Source

lib/provable/int.ts:862


assertEquals()

assertEquals(x: this): void

Parameters

x: this

Returns

void

Inherited from

CircuitValue.assertEquals

Source

lib/provable/types/circuit-value.ts:130


assertGreaterThan()

assertGreaterThan(y: UInt32, message?: string): void

Asserts that a UInt32 is greater than another one.

Parameters

y: UInt32

message?: string

Returns

void

Source

lib/provable/int.ts:932


assertGreaterThanOrEqual()

assertGreaterThanOrEqual(y: UInt32, message?: string): void

Asserts that a UInt32 is greater than or equal to another one.

Parameters

y: UInt32

message?: string

Returns

void

Source

lib/provable/int.ts:946


assertLessThan()

assertLessThan(y: UInt32, message?: string): void

Asserts that a UInt32 is less than another one.

Parameters

y: UInt32

message?: string

Returns

void

Source

lib/provable/int.ts:909


assertLessThanOrEqual()

assertLessThanOrEqual(y: UInt32, message?: string): void

Asserts that a UInt32 is less than or equal to another one.

Parameters

y: UInt32

message?: string

Returns

void

Source

lib/provable/int.ts:881


div()

div(y: number | UInt32): UInt32

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

UInt32

Source

lib/provable/int.ts:668


divMod()

divMod(y: string | number | UInt32): {
"quotient": UInt32;
"rest": UInt32;
}

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;

Source

lib/provable/int.ts:626


equals()

equals(x: this): Bool

Parameters

x: this

Returns

Bool

Inherited from

CircuitValue.equals

Source

lib/provable/types/circuit-value.ts:126


greaterThan()

greaterThan(y: UInt32): Bool

Checks if a UInt32 is greater than another one.

Parameters

y: UInt32

Returns

Bool

Source

lib/provable/int.ts:925


greaterThanOrEqual()

greaterThanOrEqual(y: UInt32): Bool

Checks if a UInt32 is greater than or equal to another one.

Parameters

y: UInt32

Returns

Bool

Source

lib/provable/int.ts:939


isConstant()

isConstant(): boolean

Returns

boolean

Inherited from

CircuitValue.isConstant

Source

lib/provable/types/circuit-value.ts:134


leftShift()

leftShift(bits: number): UInt32

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

UInt32

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

Source

lib/provable/int.ts:810


lessThan()

lessThan(y: UInt32): Bool

Checks if a UInt32 is less than another one.

Parameters

y: UInt32

Returns

Bool

Source

lib/provable/int.ts:897


lessThanOrEqual()

lessThanOrEqual(y: UInt32): Bool

Checks if a UInt32 is less than or equal to another one.

Parameters

y: UInt32

Returns

Bool

Source

lib/provable/int.ts:869


mod()

mod(y: number | UInt32): UInt32

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

UInt32

Source

lib/provable/int.ts:677


mul()

mul(y: number | UInt32): UInt32

Multiplication with overflow checking.

Parameters

y: number | UInt32

Returns

UInt32

Source

lib/provable/int.ts:683


not()

not(): UInt32

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

UInt32

Example

// NOTing 4 bits with the unchecked version
let a = UInt32.from(0b0101);
let b = a.not();

console.log(b.toBigInt().toString(2));
// 11111111111111111111111111111010

Source

lib/provable/int.ts:755


rightShift()

rightShift(bits: number): UInt32

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

UInt32

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

Source

lib/provable/int.ts:833


rotate()

rotate(bits: number, direction: "left" | "right"): UInt32

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" | "right"= 'left'

left or right rotation direction.

Returns

UInt32

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);

Source

lib/provable/int.ts:787


sub()

sub(y: number | UInt32): UInt32

Subtraction with underflow checking.

Parameters

y: number | UInt32

Returns

UInt32

Source

lib/provable/int.ts:699


toBigint()

toBigint(): bigint

Turns the UInt32 into a BigInt.

Returns

bigint

Source

lib/provable/int.ts:556


toConstant()

toConstant(): this

Returns

this

Inherited from

CircuitValue.toConstant

Source

lib/provable/types/circuit-value.ts:122


toFields()

toFields(): Field[]

Returns

Field[]

Inherited from

CircuitValue.toFields

Source

lib/provable/types/circuit-value.ts:85


toJSON()

toJSON(): any

Returns

any

Inherited from

CircuitValue.toJSON

Source

lib/provable/types/circuit-value.ts:118


toString()

toString(): string

Turns the UInt32 into a string.

Returns

string

Source

lib/provable/int.ts:550


toUInt64()

toUInt64(): UInt64

Turns the UInt32 into a UInt64.

Returns

UInt64

Source

lib/provable/int.ts:562


xor()

xor(x: UInt32): UInt32

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

UInt32 element to compare.

Returns

UInt32

Example

let a = UInt32.from(0b0101);
let b = UInt32.from(0b0011);

let c = a.xor(b);
c.assertEquals(0b0110);

Source

lib/provable/int.ts:724


MAXINT()

static MAXINT(): UInt32

Creates a UInt32 with a value of 4,294,967,295.

Returns

UInt32

Source

lib/provable/int.ts:610


check()

static check(x: UInt32): void

Parameters

x: UInt32

Returns

void

Overrides

CircuitValue.check

Source

lib/provable/int.ts:567


empty()

static empty<T>(): InstanceType<T>

Type parameters

T extends AnyConstructor

Returns

InstanceType\<T>

Inherited from

CircuitValue.empty

Source

lib/provable/types/circuit-value.ts:218


from()

static from(x: string | number | bigint | UInt32): UInt32

Creates a new UInt32.

Parameters

x: string | number | bigint | UInt32

Returns

UInt32

Source

lib/provable/int.ts:602


fromFields()

static fromFields<T>(this: T, xs: Field[]): InstanceType<T>

Type parameters

T extends AnyConstructor

Parameters

this: T

xs: Field[]

Returns

InstanceType\<T>

Inherited from

CircuitValue.fromFields

Source

lib/provable/types/circuit-value.ts:138


fromJSON()

static fromJSON<T>(x: string): InstanceType<T>

Decodes a JSON-like object into this structure.

Type parameters

T extends AnyConstructor

Parameters

x: string

Returns

InstanceType\<T>

Overrides

CircuitValue.fromJSON

Source

lib/provable/int.ts:583


fromObject()

static fromObject<T>(this: T, value: NonMethods<InstanceType<T>>): InstanceType<T>

Type parameters

T extends AnyConstructor

Parameters

this: T

value: NonMethods\<InstanceType\<T>>

Returns

InstanceType\<T>

Inherited from

CircuitValue.fromObject

Source

lib/provable/types/circuit-value.ts:30


fromValue()

static fromValue<T>(x: bigint | UInt32): InstanceType<T>

Type parameters

T extends AnyConstructor

Parameters

x: bigint | UInt32

Returns

InstanceType\<T>

Overrides

CircuitValue.fromValue

Source

lib/provable/int.ts:954


sizeInFields()

static sizeInFields(): number

Returns

number

Inherited from

CircuitValue.sizeInFields

Source

lib/provable/types/circuit-value.ts:37


toAuxiliary()

static toAuxiliary(): []

Returns

[]

Inherited from

CircuitValue.toAuxiliary

Source

lib/provable/types/circuit-value.ts:59


toConstant()

static toConstant<T>(this: T, t: InstanceType<T>): InstanceType<T>

Type parameters

T extends AnyConstructor

Parameters

this: T

t: InstanceType\<T>

Returns

InstanceType\<T>

Inherited from

CircuitValue.toConstant

Source

lib/provable/types/circuit-value.ts:177


toFields()

static toFields<T>(this: T, v: InstanceType<T>): Field[]

Type parameters

T extends AnyConstructor

Parameters

this: T

v: InstanceType\<T>

Returns

Field[]

Inherited from

CircuitValue.toFields

Source

lib/provable/types/circuit-value.ts:42


toInput()

static toInput(x: UInt32): HashInput

Parameters

x: UInt32

Returns

HashInput

Overrides

CircuitValue.toInput

Source

lib/provable/int.ts:570


toJSON()

static toJSON(x: UInt32): string

Encodes this structure into a JSON-like object.

Parameters

x: UInt32

Returns

string

Overrides

CircuitValue.toJSON

Source

lib/provable/int.ts:576


toValue()

static toValue(x: UInt32): bigint

Parameters

x: UInt32

Returns

bigint

Overrides

CircuitValue.toValue

Source

lib/provable/int.ts:950