Skip to main content
Version: 11.x

Class: CairoUint512

Defined in: src/utils/cairoDataTypes/uint512.ts:56

A Cairo core::integer::u512 : a whole number from 0 to 2^512 - 1, carried in four felts.

The number is cut into four u128 limbs, limb0 holding the bottom 128 bits and limb3 the top, and a contract call carries all four, lowest limb first. The limbs can be given directly, which is how a response from a node is read back.

Example​

const small = new CairoUint512(255);
// small.limb0 = 255n, and limb1, limb2, limb3 are 0n
const big = new CairoUint512(2n ** 390n + 7n);
// big.limb0 = 7n, big.limb3 = 64n

Constructors​

Constructor​

new CairoUint512(bigNumberish): CairoUint512

Defined in: src/utils/cairoDataTypes/uint512.ts:120

Build from one number, cutting it into its four limbs (Lib usage).

A Uint512 object is accepted here too : it already carries the four limbs, and each is checked as a u128 rather than the whole being cut again.

Parameters​

bigNumberish​

unknown

the value to carry, within [0, 2^512 - 1]

Returns​

CairoUint512

Throws​

when the value is null, undefined, text, '0x', of an unread type, a decimal number, or out of range

Example​

const result = new CairoUint512(255).toApiRequest();
// result = ["255", "0", "0", "0"]

Constructor​

new CairoUint512(limb0, limb1, limb2, limb3): CairoUint512

Defined in: src/utils/cairoDataTypes/uint512.ts:134

Build from the four limbs as they arrive, already cut (Api response).

Parameters​

limb0​

BigNumberish

bits 0 to 127

limb1​

BigNumberish

bits 128 to 255

limb2​

BigNumberish

bits 256 to 383

limb3​

BigNumberish

bits 384 to 511

Returns​

CairoUint512

Throws​

when any limb is null, undefined, text, '0x', not a number, a decimal number, or outside the u128 range

Example​

const result = new CairoUint512(7, 0, 0, 64).toBigInt() === 2n ** 390n + 7n;
// result = true

Properties​

abiSelector​

static abiSelector: string = 'core::integer::u512'

Defined in: src/utils/cairoDataTypes/uint512.ts:105

The abi type this class serializes.

Example​

const result = CairoUint512.abiSelector;
// result = "core::integer::u512"

limb0​

limb0: bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:65

Bits 0 to 127 of the value.

Example​

const result = new CairoUint512(2n ** 390n + 7n).limb0;
// result = 7n

limb1​

limb1: bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:75

Bits 128 to 255 of the value.

Example​

const result = new CairoUint512(2n ** 390n + 7n).limb1;
// result = 0n

limb2​

limb2: bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:85

Bits 256 to 383 of the value.

Example​

const result = new CairoUint512(2n ** 390n + 7n).limb2;
// result = 0n

limb3​

limb3: bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:95

Bits 384 to 511 of the value.

Example​

const result = new CairoUint512(2n ** 390n + 7n).limb3;
// result = 64n

Methods​

validate()​

static validate(bigNumberish): bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:198

Throw unless a whole value can be represented as a u512, and give back its number.

A string is only accepted while it spells a number, in base 10 or 16 : one that does not is refused as text, and '0x', which holds no digit, is refused too. So is an object, a Uint512 one included : its four limbs are checked by CairoUint512.validateProps, which is what the constructor calls for it.

Parameters​

bigNumberish​

unknown

the value to check

Returns​

bigint

the value as a number, once accepted

Throws​

when the value is null, undefined, text, '0x', of an unread type, a decimal number, or out of range

Example​

const result = CairoUint512.validate(255);
// result = 255n
CairoUint512.validate(-1);
// throws Error("bigNumberish is smaller than UINT_512_MIN.")
CairoUint512.validate('abc');
// throws Error("Invalid input: a u512 cannot be built from text")
CairoUint512.validate(1.5);
// throws Error("Invalid input: decimal numbers are not supported, only integers")

validateProps()​

static validateProps(limb0, limb1, limb2, limb3): object

Defined in: src/utils/cairoDataTypes/uint512.ts:244

Throw unless all four limbs are u128, and give them back as numbers.

This is the check for a value that arrives already cut. Each limb is bounded on its own, and the message names the one at fault by its index.

Parameters​

limb0​

BigNumberish

bits 0 to 127

limb1​

BigNumberish

bits 128 to 255

limb2​

BigNumberish

bits 256 to 383

limb3​

BigNumberish

bits 384 to 511

Returns​

object

the four limbs as numbers

limb0​

limb0: bigint

limb1​

limb1: bigint

limb2​

limb2: bigint

limb3​

limb3: bigint

Throws​

when any limb is null, undefined, text, '0x', not a number, a decimal number, or outside the u128 range

Example​

const result = CairoUint512.validateProps(7, 0, 0, 64);
// result = { limb0: 7n, limb1: 0n, limb2: 0n, limb3: 64n }
CairoUint512.validateProps(-1, 0, 0, 0);
// throws Error("limb0 is not in the range of a u128 number")
CairoUint512.validateProps(1.5, 0, 0, 0);
// throws Error("limb0 must be an integer")
CairoUint512.validateProps('abc', 0, 0, 0);
// throws Error("limb0 cannot be built from text")

is()​

static is(bigNumberish): boolean

Defined in: src/utils/cairoDataTypes/uint512.ts:287

Can this value be represented as a u512?

The non-throwing form of CairoUint512.validate, so it answers false for every input that one refuses, whatever the reason.

Parameters​

bigNumberish​

unknown

the value to test

Returns​

boolean

true when the value fits in a u512

Example​

const result = CairoUint512.is(255);
// result = true
const result2 = CairoUint512.is(-1);
// result2 = false

isAbiType()​

static isAbiType(abiType): boolean

Defined in: src/utils/cairoDataTypes/uint512.ts:308

Is this abi type the one this class serializes?

Parameters​

abiType​

string

the abi type to test

Returns​

boolean

true for core::integer::u512

Example​

const result = CairoUint512.isAbiType('core::integer::u512');
// result = true
const result2 = CairoUint512.isAbiType('core::integer::u256');
// result2 = false

factoryFromApiResponse()​

static factoryFromApiResponse(responseIterator): CairoUint512

Defined in: src/utils/cairoDataTypes/uint512.ts:325

Read one u512 off a contract response, advancing the iterator past it.

Four felts are consumed, lowest limb first, since that is how a u512 travels.

Parameters​

responseIterator​

Iterator<string>

the response felts, positioned on this u512

Returns​

CairoUint512

the u512 that was read

Example​

const response = ['0xff', '0x0', '0x0', '0x0'];
const result = CairoUint512.factoryFromApiResponse(response.values()).toBigInt();
// result = 255n

toBigInt()​

toBigInt(): bigint

Defined in: src/utils/cairoDataTypes/uint512.ts:342

The four limbs put back together as one number.

Returns​

bigint

the value this u512 holds

Example​

const result = new CairoUint512(255).toBigInt();
// result = 255n

toUint512HexString()​

toUint512HexString(): object

Defined in: src/utils/cairoDataTypes/uint512.ts:355

The four limbs as a Uint512 of hexadecimal strings.

Returns​

object

the limbs, 0x-prefixed and unpadded

limb0​

limb0: string

limb1​

limb1: string

limb2​

limb2: string

limb3​

limb3: string

Example​

const result = new CairoUint512(255).toUint512HexString();
// result = { limb0: "0xff", limb1: "0x0", limb2: "0x0", limb3: "0x0" }

toUint512DecimalString()​

toUint512DecimalString(): object

Defined in: src/utils/cairoDataTypes/uint512.ts:373

The four limbs as a Uint512 of decimal strings.

Returns​

object

the limbs, in base 10

limb0​

limb0: string

limb1​

limb1: string

limb2​

limb2: string

limb3​

limb3: string

Example​

const result = new CairoUint512(255).toUint512DecimalString();
// result = { limb0: "255", limb1: "0", limb2: "0", limb3: "0" }

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/uint512.ts:391

Serialize to the four felts a contract call carries.

Returns​

string[]

the four limbs as decimal strings, lowest first, flagged as compiled

Example​

const result = new CairoUint512(255).toApiRequest();
// result = ["255", "0", "0", "0"]