Skip to main content

Try it Live

Run Bytes examples in the interactive playground
Extended Size Type - Bytes64 provides type-safe handling of 512-bit values, commonly used for concatenated hashes and extended data structures.

Overview

Bytes64 is a branded Uint8Array type representing exactly 64 bytes (512 bits). It provides strict size validation and type safety for large fixed-size byte values.
import * as Bytes64 from 'tevm/primitives/Bytes/Bytes64';

// Create from hex
const bytes = Bytes64('0x' + '12'.repeat(64));
console.log(bytes.length); // 64

// Create zero-filled
const zeros = Bytes64.zero();
console.log(Bytes64.isZero(zeros)); // true

// Size is strictly enforced
Bytes64(new Uint8Array(63)); // Error: must be 64 bytes

Use Cases

Concatenated Hashes (2x32 bytes)
// Combine two 32-byte hashes
const hash1 = Bytes32().fill(0xaa);
const hash2 = Bytes32().fill(0xbb);
const combined = Bytes64();
combined.set(hash1, 0);
combined.set(hash2, 32);
const bytes64 = Bytes64(combined);

console.log(bytes64[0]);  // 0xaa
console.log(bytes64[32]); // 0xbb
Extended Hashes
// SHA-512, Blake2b-512, etc.
const extendedHash = Bytes64('0x...');
Public Key Material
// Some public keys are 64 bytes (uncompressed)
const pubkey = Bytes64('0x...');
Signature Components
// r + s components concatenated (32 + 32 bytes)
const signature = Bytes64('0x...');

Type Safety

Bytes64 uses Symbol branding for compile-time safety:
type Bytes64Type = Uint8Array & {
  readonly [Symbol.for("Bytes64")]: true;
  readonly length: 64;
};
This prevents mixing different byte types:
const bytes64 = Bytes64('0x' + '12'.repeat(64));
const bytes32 = Bytes32('0x' + '12'.repeat(32));

// Type error: cannot assign Bytes32 to Bytes64
const wrong: Bytes64.Bytes64Type = bytes32;

Hex Variants

Bytes64 also supports hex string variants with case flags:
// Lowercase hex (default)
type HexBytes64 = string & {
  readonly [Symbol.for("HexBytes64")]: true;
  readonly length: 130; // "0x" + 128 hex chars
  readonly lowercase: true;
};

// Uppercase hex
type HexBytes64Upper = string & {
  readonly [Symbol.for("HexBytes64")]: true;
  readonly length: 130;
  readonly uppercase: true;
};
For generic byte concepts, see Bytes.

Constructors

from

Create Bytes64 from hex string or bytes:
// From hex
const b1 = Bytes64('0x' + '12'.repeat(64));

// From bytes
const b2 = Bytes64(Bytes64());

fromHex

Create from hex string with strict validation:
const bytes = Bytes64('0x' + 'ab'.repeat(64));

// Without 0x prefix
const bytes2 = Bytes64('12'.repeat(64));

// Throws on wrong length
Bytes64('0x1234'); // Error: must be 128 hex chars

fromBytes

Create from Uint8Array with size check:
const arr = Bytes64();
const bytes = Bytes64(arr);

// Throws on wrong size
Bytes64(new Uint8Array(63)); // Error

zero

Create zero-filled Bytes64:
const zeros = Bytes64.zero();
console.log(Bytes64.isZero(zeros)); // true

Conversions

toHex

Convert to hex string:
const bytes = Bytes64(Bytes64());
const hex = Bytes64.toHex(bytes);
console.log(hex.length); // 130 (0x + 128 hex chars)

toUint8Array

Convert to raw bytes:
const bytes = Bytes64('0x' + '12'.repeat(64));
const arr = Bytes64.toUint8Array(bytes);
console.log(arr instanceof Uint8Array); // true

Operations

equals

Check equality:
const a = Bytes64('0x' + 'aa'.repeat(64));
const b = Bytes64('0x' + 'aa'.repeat(64));
console.log(Bytes64.equals(a, b)); // true

compare

Compare two values:
const a = Bytes64('0x' + '01'.repeat(64));
const b = Bytes64('0x' + '02'.repeat(64));
console.log(Bytes64.compare(a, b)); // -1 (a < b)

clone

Create independent copy:
const original = Bytes64('0x' + '12'.repeat(64));
const copy = Bytes64.clone(original);

isZero

Check if all zeros:
const zeros = Bytes64.zero();
console.log(Bytes64.isZero(zeros)); // true

const nonZero = Bytes64('0x' + '00'.repeat(63) + '01');
console.log(Bytes64.isZero(nonZero)); // false

size

Get size (always 64):
const bytes = Bytes64('0x' + '12'.repeat(64));
console.log(Bytes64.size(bytes)); // 64

Constants

SIZE

Bytes64 size in bytes:
console.log(Bytes64.SIZE); // 64

ZERO

Zero-filled constant:
console.log(Bytes64.ZERO.length); // 64
console.log(Bytes64.isZero(Bytes64.ZERO)); // true

Error Handling

Bytes64 validates size strictly:
try {
  Bytes64(new Uint8Array(63));
} catch (error) {
  console.log(error.code); // "BYTES64_INVALID_LENGTH"
  console.log(error.message); // "Bytes64 must be 64 bytes, got 63"
}

try {
  Bytes64('0x1234');
} catch (error) {
  console.log(error.code); // "BYTES64_INVALID_HEX_LENGTH"
  console.log(error.message); // "Bytes64 hex must be 128 characters..."
}

Working with Hash Pairs

Common pattern: concatenate two 32-byte hashes:
import { Keccak256 } from 'tevm/Keccak256';

// Two hashes to concatenate
const hash1 = Keccak256.hash(data1);
const hash2 = Keccak256.hash(data2);

// Combine into Bytes64
const combined = Bytes64();
combined.set(hash1, 0);   // First 32 bytes
combined.set(hash2, 32);  // Second 32 bytes
const bytes64 = Bytes64(combined);

// Access components
const firstHash = bytes64.slice(0, 32);
const secondHash = bytes64.slice(32, 64);

Extended Hash Functions

Some hash functions output 64 bytes:
// SHA-512 (hypothetical)
const sha512Result = Bytes64('0x...');

// Blake2b-512
const blake2b512Result = Bytes64('0x...');
  • Bytes32 - 256-bit fixed-size bytes (most common)
  • Bytes16 - 128-bit fixed-size bytes
  • Keccak256 - 256-bit cryptographic hashes
  • Hex - Variable-length hex strings