Skip to main content

Try it Live

Run Uint examples in the interactive playground
New to unsigned integers? Start with Fundamentals for guided examples and concepts.

Type Definition

Branded bigint representing unsigned 256-bit integer (0 to 2^256-1). Supports tree-shakeable namespace methods and class instances with automatic wrapping on overflow.
export type BrandedUint256 = bigint & {
  readonly __tag: "Uint256"
}

Quick Reference

Effect Schema

import { UintSchema } from '@tevm/voltaire/Uint/effect'
import * as Effect from 'effect/Effect'

// Schema-validated construction
const u = new UintSchema({ value: 123n })

// Effect composition
const program = Effect.gen(function* () {
  const x = yield* Effect.sync(() => UintSchema.from(123n))
  return x.toHex()
})

await Effect.runPromise(program)

API Methods

Constructors

Conversions

Arithmetic

Bitwise

Comparisons

Validation

Utilities

Types

class Uint {
  // Constructors
  constructor(value: bigint | number | string): BrandedUint256
  static from(value: bigint | number | string): BrandedUint256
  static fromHex(hex: string): BrandedUint256
  static fromBigInt(value: bigint): BrandedUint256
  static fromNumber(value: number | bigint): BrandedUint256
  static fromBytes(bytes: Uint8Array): BrandedUint256
  static fromAbiEncoded(bytes: Uint8Array): BrandedUint256
  static tryFrom(value: bigint | number | string): BrandedUint256 | undefined

  // Conversions
  toHex(padded?: boolean): string
  toBigInt(): bigint
  toNumber(): number
  toBytes(): Uint8Array
  toAbiEncoded(): Uint8Array
  toString(radix?: number): string

  // Arithmetic
  plus(other: BrandedUint256): BrandedUint256
  minus(other: BrandedUint256): BrandedUint256
  times(other: BrandedUint256): BrandedUint256
  dividedBy(other: BrandedUint256): BrandedUint256
  modulo(other: BrandedUint256): BrandedUint256
  toPower(exponent: BrandedUint256): BrandedUint256

  // Bitwise
  bitwiseAnd(other: BrandedUint256): BrandedUint256
  bitwiseOr(other: BrandedUint256): BrandedUint256
  bitwiseXor(other: BrandedUint256): BrandedUint256
  bitwiseNot(): BrandedUint256
  shiftLeft(bits: number): BrandedUint256
  shiftRight(bits: number): BrandedUint256

  // Comparisons
  equals(other: BrandedUint256): boolean
  notEquals(other: BrandedUint256): boolean
  lessThan(other: BrandedUint256): boolean
  lessThanOrEqual(other: BrandedUint256): boolean
  greaterThan(other: BrandedUint256): boolean
  greaterThanOrEqual(other: BrandedUint256): boolean

  // Utilities
  isZero(): boolean
  minimum(other: BrandedUint256): BrandedUint256
  maximum(other: BrandedUint256): BrandedUint256
  bitLength(): number
  leadingZeros(): number
  popCount(): number

  // Constants
  static MAX: BrandedUint256
  static MIN: BrandedUint256
  static ZERO: BrandedUint256
  static ONE: BrandedUint256
  static SIZE: number
}
Source: UintConstructor.ts:68-114

Constants

Uint module exports constants for validation and arithmetic:
import { Uint } from 'tevm'

// Boundary values
Uint.MAX   // 2^256 - 1 (largest Uint256)
Uint.MIN   // 0 (smallest Uint256)
Uint.ZERO  // 0
Uint.ONE   // 1

// Size constant
Uint.SIZE  // 32 (bytes)

// Usage examples
const maxValue = Uint.MAX
const isMax = value.equals(Uint.MAX)
const isZero = value.equals(Uint.ZERO)
const increment = value.plus(Uint.ONE)
const buffer = new Uint8Array(Uint.SIZE)  // 32-byte buffer
Available constants:
  • Uint.MAX = 2^256 - 1 - Maximum Uint256 value (115792089237316195423570985008687907853269984665640564039457584007913129639935n)
  • Uint.MIN = 0 - Minimum Uint256 value
  • Uint.ZERO = 0 - Zero constant
  • Uint.ONE = 1 - One constant
  • Uint.SIZE = 32 - Size in bytes
Source: constants.ts

Wrapping Arithmetic

All arithmetic wraps on overflow (mod 2^256), matching EVM behavior:
import { Uint } from 'tevm';

// Overflow wraps to 0
const max = Uint.MAX;
const overflow = max.plus(Uint.ONE);
console.log(overflow.equals(Uint.ZERO));  // true

// Underflow wraps to MAX
const underflow = Uint.ZERO.minus(Uint.ONE);
console.log(underflow.equals(Uint.MAX));  // true
See Fundamentals for overflow examples and Arithmetic for detailed operations.

Tree-Shaking

Import only what you need for optimal bundle size:
// Import specific functions (tree-shakeable)
import { fromHex, plus, toHex } from 'tevm/Uint';

const a = fromHex("0x64");
const b = fromHex("0x32");
const sum = plus(a, b);
const hex = toHex(sum);

// Only these 3 functions included in bundle

Specification