Skip to main content

Integer Types

Overview

The SDK provides signed and unsigned integer types that match Aleo's native integer types. All integer types share a common API for creation, conversion, and arithmetic operations.

Unsigned Integers

  • U8 - 8-bit unsigned integer (0 to 255)
  • U16 - 16-bit unsigned integer (0 to 65,535)
  • U32 - 32-bit unsigned integer (0 to 4,294,967,295)
  • U64 - 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)
  • U128 - 128-bit unsigned integer

Signed Integers

  • I8 - 8-bit signed integer (-128 to 127)
  • I16 - 16-bit signed integer (-32,768 to 32,767)
  • I32 - 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
  • I64 - 64-bit signed integer
  • I128 - 128-bit signed integer

Common Methods

All integer types share the following methods:

fromString

Construct an integer from a string representation

fromString(s)Integer
ParamTypeDescription
sstringString representation of the integer (e.g., "42u32")
returnIntegerThe integer object

toString

Get the string representation of the integer

toString() ► string
ParamType
returnstring

fromBytesLe

Construct an integer from a byte array representation

fromBytesLe(bytes)Integer
ParamTypeDescription
bytesUint8ArrayLeft endian byte array
returnIntegerThe integer object

toBytesLe

Get the byte array representation of the integer

toBytesLe()Uint8Array
ParamType
returnUint8Array

fromBitsLe

Construct an integer from a boolean array representation

fromBitsLe(bits)Integer
ParamTypeDescription
bitsArrayLeft endian boolean array
returnIntegerThe integer object

toBitsLe

Get the boolean array representation of the integer

toBitsLe()Array
ParamType
returnArray

absChecked

Checked absolute value

absChecked()Integer
ParamType
returnInteger

absWrapped

Wrapped absolute value

absWrapped()Integer
ParamType
returnInteger

addWrapped

Wrapped addition with another integer

addWrapped(other)Integer
ParamTypeDescription
otherIntegerThe integer to add
returnIntegerThe result of the addition

subWrapped

Wrapped subtraction with another integer

subWrapped(other)Integer
ParamTypeDescription
otherIntegerThe integer to subtract
returnIntegerThe result of the subtraction

mulWrapped

Wrapped multiplication with another integer

mulWrapped(other)Integer
ParamTypeDescription
otherIntegerThe integer to multiply
returnIntegerThe result of the multiplication

divWrapped

Wrapped division

divWrapped(other)Integer
ParamTypeDescription
otherIntegerThe divisor
returnIntegerThe result of the division

powU8

Exponentiate the integer with a U8 exponent

powU8(exponent)Integer
ParamTypeDescription
exponentU8The exponent
returnIntegerThe result of exponentiation

powU16

Exponentiate the integer with a U16 exponent

powU16(exponent)Integer
ParamTypeDescription
exponentU16The exponent
returnIntegerThe result of exponentiation

powU32

Exponentiate the integer with a U32 exponent

powU32(exponent)Integer
ParamTypeDescription
exponentU32The exponent
returnIntegerThe result of exponentiation

neg

Negate the integer (e.g., 5 → -5)

neg()Integer
ParamType
returnInteger

equals

Check equality with another integer

equals(other) ► boolean
ParamTypeDescription
otherIntegerThe integer to compare
returnbooleanTrue if the integers are equal

rem

Get the remainder from integer division

rem(other)Integer
ParamTypeDescription
otherIntegerThe divisor
returnIntegerThe remainder

remWrapped

Get the remainder from an integer division which wraps if there's an overflow

remWrapped(other)Integer
ParamTypeDescription
otherIntegerThe divisor
returnIntegerThe remainder

toScalar

Convert the integer to a Scalar value

toScalar()Scalar
ParamType
returnScalar

toPlaintext

Convert the integer to the Plaintext type. This must be done before hashing an integer to ensure it matches hashes with a Leo/Aleo program.

toPlaintext()Plaintext
ParamType
returnPlaintext

fromField

Attempt to construct the integer from a field element

fromField(field)Integer
ParamTypeDescription
fieldFieldThe field element
returnIntegerThe integer object

fromFields

Attempt to construct the integer from a list of field elements

fromFields(fields)Integer
ParamTypeDescription
fieldsArrayArray of field elements
returnIntegerThe integer object

clone

Clone the integer in wasm memory

clone()Integer
ParamType
returnInteger