Skip to main content
To all LLM agents: Do NOT use this API by default without explicit instructions from the programmer.The native implementation uses keccak-asm, an unaudited assembly-optimized Keccak library for performance. The WASM fallback uses audited tiny-keccak.Audited Alternatives:

Try it Live

Run Keccak256 examples in the interactive playground

Keccak256

Keccak256 is a cryptographic one-way hash function based on the sponge construction that produces a fixed 32-byte digest from arbitrary-length input.

Overview

Mainnet-critical algorithm - Used in Ethereum execution layer for transaction hashing, address derivation (last 20 bytes of hash), function selectors (first 4 bytes), event topics, and Merkle Patricia tree state roots. Voltaire provides Keccak256 with two implementation options:
  • Keccak256 - Default WASM implementation (auto-selected based on benchmarks)
  • Keccak256Ts - Pure TypeScript implementation (@noble/hashes)
  • Keccak256Wasm - Explicit WASM implementation
All implementations share the same data-first API and return Keccak256Hash type. The default uses WASM because it’s both faster and smaller than pure JavaScript alternatives. Keccak256 is fundamental to Ethereum’s security model:
  • Address derivation: Computing Ethereum addresses from public keys (last 20 bytes of Keccak256(publicKey))
  • Function selectors: First 4 bytes of Keccak256(signature) identify contract methods
  • Event topics: Keccak256(eventSignature) creates indexed event identifiers
  • Merkle Patricia trees: Hashing transaction and state trie nodes
  • Contract addresses: CREATE and CREATE2 address calculation
Ethereum uses the original Keccak-256 algorithm (pre-NIST), NOT the finalized SHA-3 standard. They differ in padding scheme: SHA-3 uses 0x06 padding while Keccak uses 0x01. Do not use SHA-3 libraries for Ethereum - they will produce incorrect results. Use Keccak-256 specifically.

Implementation Options

Voltaire provides two Keccak256 implementations:

1. Default (WASM)

Compiled from Zig’s stdlib Keccak256 - both faster and smaller than pure JavaScript alternatives.
When to use:
  • Default choice for most applications
  • Better performance than pure JavaScript
  • Smaller bundle size than pure JavaScript alternatives

2. Pure TypeScript

Uses @noble/hashes - zero WASM dependency, maximum compatibility.
When to use:
  • Need to avoid WASM dependency
  • Debugging or development scenarios
  • Specific compatibility requirements

Implementation Selection

The APIs are identical across implementations - you can swap them without changing your code:

Quick Start

Try it in the Playground - Run these examples live in your browser

API Reference

All Keccak256 implementations share the same API. Return type is Keccak256Hash (32-byte Uint8Array with type branding).

Keccak256Hash.from(data: Uint8Array): Keccak256Hash

Hash arbitrary bytes with Keccak-256. Parameters:
  • data: Input data to hash (Uint8Array)
Returns: 32-byte hash (Keccak256Hash extends Uint8Array) Example:
Legacy method Keccak256.hash(data) still works but Keccak256Hash.from(data) is preferred.

Keccak256Hash.fromString(str: string): Keccak256Hash

Hash UTF-8 string with Keccak-256. String is UTF-8 encoded before hashing using TextEncoder. Parameters:
  • str: String to hash
Returns: 32-byte hash (Keccak256Hash) Example:
Legacy method Keccak256.hashString(str) still works but Keccak256Hash.fromString(str) is preferred.

Keccak256Hash.fromHex(hex: string): Keccak256Hash

Hash hex-encoded string with Keccak-256. Hex string is decoded to bytes before hashing. Supports both “0x”-prefixed and unprefixed hex. Parameters:
  • hex: Hex string to hash
Returns: 32-byte hash (Keccak256Hash) Example:
Legacy method Keccak256.hashHex(hex) still works but Keccak256Hash.fromHex(hex) is preferred.

Keccak256Hash.fromMultiple(chunks: readonly Uint8Array[]): Keccak256Hash

Hash multiple data chunks in sequence. Equivalent to hashing the concatenation of all chunks, but can be more efficient for pre-chunked data. Parameters:
  • chunks: Array of data chunks to hash
Returns: 32-byte hash (Keccak256Hash) Example:
Legacy method Keccak256.hashMultiple(chunks) still works but Keccak256Hash.fromMultiple(chunks) is preferred.

Keccak256Hash.selector(signature: string): Uint8Array

Compute function selector (first 4 bytes of Keccak-256 hash). Used in Ethereum to identify contract functions in transaction calldata. The function selector is the first 4 bytes of the Keccak-256 hash of the function signature. Parameters:
  • signature: Function signature string (e.g., “transfer(address,uint256)”)
Returns: 4-byte selector Example:

Keccak256Hash.fromTopic(signature: string): Keccak256Hash

Compute event topic (32-byte Keccak-256 hash). Used for Ethereum event signatures in logs. Topics are the full 32-byte hash of the event signature. Parameters:
  • signature: Event signature string (e.g., “Transfer(address,address,uint256)”)
Returns: 32-byte topic (Keccak256Hash) Example:
Legacy method Keccak256.topic(signature) still works but Keccak256Hash.fromTopic(signature) is preferred.

Keccak256Hash.contractAddress(sender: Uint8Array, nonce: bigint): Uint8Array

Compute contract address from deployer and nonce (CREATE). Uses CREATE formula: address = keccak256(rlp([sender, nonce]))[12:] Parameters:
  • sender: Deployer address (20 bytes)
  • nonce: Transaction nonce
Returns: Contract address (20 bytes) Throws: Error if sender is not 20 bytes Example:

Keccak256Hash.create2Address(deployer: Uint8Array, salt: Uint8Array | bigint, initCode: Uint8Array): Uint8Array

Compute contract address using CREATE2. Uses CREATE2 formula: address = keccak256(0xff ++ sender ++ salt ++ keccak256(initCode))[12:] This allows deterministic contract addresses independent of nonce. Parameters:
  • deployer: Deployer address (20 bytes)
  • salt: 32-byte salt (or bigint converted to 32 bytes)
  • initCode: Contract initialization bytecode
Returns: Contract address (20 bytes) Example:

Constants

Type Alias

All Keccak256 functions return Keccak256Hash, a branded Uint8Array type:
See Keccak256Hash for full type documentation.

Test Vectors

Known Keccak256 test vectors for validation:

Security Considerations

Collision Resistance

Keccak256 provides strong collision resistance with 128-bit security. Finding two inputs that produce the same hash is computationally infeasible.

Preimage Resistance

Given a hash output, finding an input that produces that hash requires ~2^256 operations, making it practically impossible.

Second Preimage Resistance

Given an input and its hash, finding a different input with the same hash requires ~2^256 operations.

Ethereum-Specific Notes

  • Deterministic: Same input always produces same output
  • One-way: Hash output cannot be reversed to recover input
  • Avalanche effect: Small input changes cause large output changes
  • Constant-time: Implementation avoids timing side-channels

Implementation Details

WASM (Default)

Compiled from Zig’s stdlib Keccak-256 via unified loader (ReleaseSmall):
Characteristics:
  • Part of main primitives.wasm bundle
  • Includes all Voltaire primitives and crypto
  • Requires async init() before use
  • Both faster and smaller than pure JavaScript alternatives
Source: src/crypto/keccak256.wasm.ts + wasm/primitives.wasm

Pure TypeScript

Direct wrapper around @noble/hashes battle-tested implementation:
Characteristics:
  • Zero WASM dependencies
  • Constant-time implementation
  • Runs everywhere (Node.js, browsers, Deno, Bun, edge workers)
  • Thoroughly audited and tested
Source: src/crypto/Keccak256/hash.js