Skip to main content

UInt64

Defined in: lib/provable/int.ts:30

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

Extends

  • CircuitValue

Constructors

new UInt64()

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

Defined in: lib/provable/int.ts:40

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

Properties

value

value: Field;

Defined in: lib/provable/int.ts:31


NUM_BITS

static NUM_BITS: number = 64;

Defined in: lib/provable/int.ts:32


Unsafe

static Unsafe: {
fromField: UInt64;
};

Defined in: lib/provable/int.ts:48

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

Accessors

one

Get Signature

get static one(): UInt64

Defined in: lib/provable/int.ts:69

Static method to create a UInt64 with value 1.

Returns

UInt64


zero

Get Signature

get static zero(): UInt64

Defined in: lib/provable/int.ts:63

Static method to create a UInt64 with value 0.

Returns

UInt64

Methods

add()

add(y: number | UInt64): UInt64

Defined in: lib/provable/int.ts:232

Addition with overflow checking.

Parameters

y

number | UInt64

Returns

UInt64


addMod64()

addMod64(y: UInt64): UInt64

Defined in: lib/provable/int.ts:157

Addition modulo 2^64. Check Gadgets.addMod64 for a detailed description.

Parameters

y

UInt64

Returns

UInt64


and()

and(x: UInt64): UInt64

Defined in: lib/provable/int.ts:400

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

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

Defined in: lib/provable/int.ts:482

Asserts that a UInt64 is greater than another one.

Parameters

y

UInt64

message?

string

Returns

void


assertGreaterThanOrEqual()

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

Defined in: lib/provable/int.ts:496

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

Parameters

y

UInt64

message?

string

Returns

void


assertLessThan()

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

Defined in: lib/provable/int.ts:462

Asserts that a UInt64 is less than another one.

Parameters

y

UInt64

message?

string

Returns

void


assertLessThanOrEqual()

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

Defined in: lib/provable/int.ts:436

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

Parameters

y

UInt64

message?

string

Returns

void


div()

div(y: number | UInt64): UInt64

Defined in: lib/provable/int.ts:206

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


divMod()

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

Defined in: lib/provable/int.ts:166

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;

equals()

equals(x: this): Bool

Defined in: lib/provable/types/circuit-value.ts:117

Parameters

x

this

Returns

Bool

Inherited from

CircuitValue.equals

greaterThan()

greaterThan(y: UInt64): Bool

Defined in: lib/provable/int.ts:475

Checks if a UInt64 is greater than another one.

Parameters

y

UInt64

Returns

Bool


greaterThanOrEqual()

greaterThanOrEqual(y: UInt64): Bool

Defined in: lib/provable/int.ts:489

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

Parameters

y

UInt64

Returns

Bool


isConstant()

isConstant(): boolean

Defined in: lib/provable/types/circuit-value.ts:125

Returns

boolean

Inherited from

CircuitValue.isConstant

leftShift()

leftShift(bits: number): UInt64

Defined in: lib/provable/int.ts:350

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

lessThan()

lessThan(y: UInt64): Bool

Defined in: lib/provable/int.ts:450

Checks if a UInt64 is less than another one.

Parameters

y

UInt64

Returns

Bool


lessThanOrEqual()

lessThanOrEqual(y: UInt64): Bool

Defined in: lib/provable/int.ts:424

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

Parameters

y

UInt64

Returns

Bool


mod()

mod(y: number | UInt64): UInt64

Defined in: lib/provable/int.ts:216

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


mul()

mul(y: number | UInt64): UInt64

Defined in: lib/provable/int.ts:223

Multiplication with overflow checking.

Parameters

y

number | UInt64

Returns

UInt64


not()

not(): UInt64

Defined in: lib/provable/int.ts:297

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


or()

or(x: UInt64): UInt64

Defined in: lib/provable/int.ts:417

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

UInt64

Returns

UInt64

Example

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

let c = a.or(b); // ... 000111
c.assertEquals(7);

rightShift()

rightShift(bits: number): UInt64

Defined in: lib/provable/int.ts:371

Performs a right shift 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); // right shift by 2 bits
y.assertEquals(0b000011); // 3 in binary

rotate()

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

Defined in: lib/provable/int.ts:329

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 or right rotation direction.

"left" | "right"

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

sub()

sub(y: number | UInt64): UInt64

Defined in: lib/provable/int.ts:241

Subtraction with underflow checking.

Parameters

y

number | UInt64

Returns

UInt64


toBigInt()

toBigInt(): bigint

Defined in: lib/provable/int.ts:83

Turns the UInt64 into a BigInt.

Returns

bigint


toBits()

toBits(length: number): Bool[]

Defined in: lib/provable/int.ts:550

Returns an array of Bool elements representing little endian binary representation of this UInt64 element.

If you use the optional length argument, proves that the UInt64 element fits in length bits. The length has to be between 0 and 64 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 64 bits. Prefer to pass a smaller length if possible.

Parameters

length

number = 64

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


toBytes()

toBytes(): [UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8]

Defined in: lib/provable/int.ts:511

Split a UInt64 into 8 UInt8s, in little-endian order.

Returns

[UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8]


toBytesBE()

toBytesBE(): [UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8]

Defined in: lib/provable/int.ts:518

Split a UInt64 into 8 UInt8s, in big-endian order.

Returns

[UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8]


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

Turns the UInt64 into a string.

Returns

string


toUInt32()

toUInt32(): UInt32

Defined in: lib/provable/int.ts:90

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

Returns

UInt32


toUInt32Clamped()

toUInt32Clamped(): UInt32

Defined in: lib/provable/int.ts:102

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


xor()

xor(x: UInt64): UInt64

Defined in: lib/provable/int.ts:266

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

check()

static check(x: UInt64): void

Defined in: lib/provable/int.ts:108

Parameters

x

UInt64

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

Defined in: lib/provable/int.ts:142

Creates a new UInt64.

Parameters

x

string | number | bigint | UInt64 | UInt32

Returns

UInt64


fromBits()

static fromBits(bits: (boolean | Bool)[]): UInt64

Defined in: lib/provable/int.ts:574

Convert a bit array into a UInt64 element using little endian binary representation

The method throws if the given bits do not fit in a single UInt64 element. In this case, no more than 64 bits are allowed.

Important: If the given bits array is an array of booleans or Bool elements that all are constant, the resulting UInt64 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 UInt64 will be a variable as well.

Parameters

bits

(boolean | Bool)[]

An array of Bool or boolean type.

Returns

UInt64

A UInt64 element matching the little endian binary representation of the given bits array.


fromBytes()

static fromBytes(bytes: UInt8[]): UInt64

Defined in: lib/provable/int.ts:525

Combine 8 UInt8s into a UInt64, in little-endian order.

Parameters

bytes

UInt8[]

Returns

UInt64


fromBytesBE()

static fromBytesBE(bytes: UInt8[]): UInt64

Defined in: lib/provable/int.ts:533

Combine 8 UInt8s into a UInt64, in big-endian order.

Parameters

bytes

UInt8[]

Returns

UInt64


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

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

Defined in: lib/provable/int.ts:504

Type Parameters

T extends AnyConstructor

Parameters

x

number | bigint | UInt64

Returns

InstanceType<T>

Overrides

CircuitValue.fromValue

MAXINT()

static MAXINT(): UInt64

Defined in: lib/provable/int.ts:150

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

Returns

UInt64


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

Defined in: lib/provable/int.ts:112

Parameters

x

UInt64

Returns

HashInput

Overrides

CircuitValue.toInput

toJSON()

static toJSON(x: UInt64): string

Defined in: lib/provable/int.ts:119

Encodes this structure into a JSON-like object.

Parameters

x

UInt64

Returns

string

Overrides

CircuitValue.toJSON

toValue()

static toValue(x: UInt64): bigint

Defined in: lib/provable/int.ts:500

Parameters

x

UInt64

Returns

bigint

Overrides

CircuitValue.toValue