Skip to main content

UInt64

A 64 bit unsigned integer with values ranging from 0 to 18,446,744,073,709,551,615.

Extends

  • CircuitValue

Constructors

new UInt64(x)

new UInt64(x: 
| string
| number
| bigint
| FieldVar
| UInt64
| UInt32): UInt64

Create a UInt64. The max value of a UInt64 is 2^64 - 1 = UInt64.MAXINT().

Warning: Cannot overflow, an error is thrown if the result is greater than UInt64.MAXINT()

Parameters

x: | string | number | bigint | FieldVar | UInt64 | UInt32

Returns

UInt64

Overrides

CircuitValue.constructor

Source

lib/provable/int.ts:38

Properties

value

value: Field;

Source

lib/provable/int.ts:29


NUM_BITS

static NUM_BITS: number = 64;

Source

lib/provable/int.ts:30


Unsafe

static Unsafe: {
fromField: UInt64;
};

fromField()

Create a UInt64 from a Field without constraining its range.

Warning: This is unsafe, because it does not prove that the input Field actually fits in 64 bits.\ Only use this if you know what you are doing, otherwise use the safe UInt64.from.

Parameters

x: Field

Returns

UInt64

Source

lib/provable/int.ts:46

Accessors

one

get static one(): UInt64

Static method to create a UInt64 with value 1.

Returns

UInt64

Source

lib/provable/int.ts:67


zero

get static zero(): UInt64

Static method to create a UInt64 with value 0.

Returns

UInt64

Source

lib/provable/int.ts:61

Methods

add()

add(y: number | UInt64): UInt64

Addition with overflow checking.

Parameters

y: number | UInt64

Returns

UInt64

Source

lib/provable/int.ts:232


and()

and(x: UInt64): UInt64

Bitwise AND gadget on UInt64 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: UInt64

Returns

UInt64

Example

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

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

Source

lib/provable/int.ts:402


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: UInt64, message?: string): void

Asserts that a UInt64 is greater than another one.

Parameters

y: UInt64

message?: string

Returns

void

Source

lib/provable/int.ts:473


assertGreaterThanOrEqual()

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

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

Parameters

y: UInt64

message?: string

Returns

void

Source

lib/provable/int.ts:487


assertLessThan()

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

Asserts that a UInt64 is less than another one.

Parameters

y: UInt64

message?: string

Returns

void

Source

lib/provable/int.ts:450


assertLessThanOrEqual()

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

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

Parameters

y: UInt64

message?: string

Returns

void

Source

lib/provable/int.ts:421


div()

div(y: number | UInt64): UInt64

Integer division.

x.div(y) returns the floor of x / y, that is, the greatest z such that z * y <= x.

Parameters

y: number | UInt64

Returns

UInt64

Source

lib/provable/int.ts:206


divMod()

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

Integer division with remainder.

x.divMod(y) returns the quotient and the remainder.

Parameters

y: string | number | UInt64

Returns

{
quotient: UInt64;
rest: UInt64;
}
quotient
quotient: UInt64;
rest
rest: UInt64;

Source

lib/provable/int.ts:163


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: UInt64): Bool

Checks if a UInt64 is greater than another one.

Parameters

y: UInt64

Returns

Bool

Source

lib/provable/int.ts:466


greaterThanOrEqual()

greaterThanOrEqual(y: UInt64): Bool

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

Parameters

y: UInt64

Returns

Bool

Source

lib/provable/int.ts:480


isConstant()

isConstant(): boolean

Returns

boolean

Inherited from

CircuitValue.isConstant

Source

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


leftShift()

leftShift(bits: number): UInt64

Performs a left shift operation on the provided UInt64 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 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.

Parameters

bits: number

Amount of bits to shift the UInt64 element to the left. The amount should be between 0 and 64 (or else the shift will fail).

Returns

UInt64

Example

const x = UInt64.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:352


lessThan()

lessThan(y: UInt64): Bool

Checks if a UInt64 is less than another one.

Parameters

y: UInt64

Returns

Bool

Source

lib/provable/int.ts:438


lessThanOrEqual()

lessThanOrEqual(y: UInt64): Bool

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

Parameters

y: UInt64

Returns

Bool

Source

lib/provable/int.ts:409


mod()

mod(y: number | UInt64): UInt64

Integer remainder.

x.mod(y) returns the value z such that 0 <= z < y and x - z is divisible by y.

Parameters

y: number | UInt64

Returns

UInt64

Source

lib/provable/int.ts:216


mul()

mul(y: number | UInt64): UInt64

Multiplication with overflow checking.

Parameters

y: number | UInt64

Returns

UInt64

Source

lib/provable/int.ts:223


not()

not(): UInt64

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 64 bit for UInt64 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

UInt64

Example

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

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

Source

lib/provable/int.ts:299


rightShift()

rightShift(bits: number): UInt64

Performs a left right operation on the provided UInt64 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 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.

Parameters

bits: number

Amount of bits to shift the UInt64 element to the right. The amount should be between 0 and 64 (or else the shift will fail).

Returns

UInt64

Example

const x = UInt64.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:373


rotate()

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

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 UInt64 element with.

direction: "left" | "right"= 'left'

left or right rotation direction.

Returns

UInt64

Example

const x = UInt64.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:331


sub()

sub(y: number | UInt64): UInt64

Subtraction with underflow checking.

Parameters

y: number | UInt64

Returns

UInt64

Source

lib/provable/int.ts:241


toBigInt()

toBigInt(): bigint

Turns the UInt64 into a BigInt.

Returns

bigint

Source

lib/provable/int.ts:81


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 UInt64 into a string.

Returns

string

Source

lib/provable/int.ts:74


toUInt32()

toUInt32(): UInt32

Turns the UInt64 into a UInt32, asserting that it fits in 32 bits.

Returns

UInt32

Source

lib/provable/int.ts:88


toUInt32Clamped()

toUInt32Clamped(): UInt32

Turns the UInt64 into a UInt32, clamping to the 32 bits range if it's too large.

UInt64.from(4294967296).toUInt32Clamped().toString(); // "4294967295"

Returns

UInt32

Source

lib/provable/int.ts:100


xor()

xor(x: UInt64): UInt64

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

UInt64 element to XOR.

Returns

UInt64

Example

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

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

Source

lib/provable/int.ts:266


MAXINT()

static MAXINT(): UInt64

Creates a UInt64 with a value of 18,446,744,073,709,551,615.

Returns

UInt64

Source

lib/provable/int.ts:154


check()

static check(x: UInt64): void

Parameters

x: UInt64

Returns

void

Overrides

CircuitValue.check

Source

lib/provable/int.ts:110


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

Creates a new UInt64.

Parameters

x: | string | number | bigint | UInt64 | UInt32

Returns

UInt64

Source

lib/provable/int.ts:146


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


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 | UInt64): InstanceType<T>

Type parameters

T extends AnyConstructor

Parameters

x: bigint | UInt64

Returns

InstanceType\<T>

Overrides

CircuitValue.fromValue

Source

lib/provable/int.ts:495


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: UInt64): HashInput

Parameters

x: UInt64

Returns

HashInput

Overrides

CircuitValue.toInput

Source

lib/provable/int.ts:114


toJSON()

static toJSON(x: UInt64): string

Encodes this structure into a JSON-like object.

Parameters

x: UInt64

Returns

string

Overrides

CircuitValue.toJSON

Source

lib/provable/int.ts:121


toValue()

static toValue(x: UInt64): bigint

Parameters

x: UInt64

Returns

bigint

Overrides

CircuitValue.toValue

Source

lib/provable/int.ts:491