Try it Live
Run Keccak256 examples in the interactive playground
Source: keccak_asm.zig • keccak256.wasm.tsTests: keccak256.test.ts • keccak256.wasm.test.ts
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
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
Implementation Options
Voltaire provides two Keccak256 implementations:1. Default (WASM)
Compiled from Zig’s stdlib Keccak256 - both faster and smaller than pure JavaScript alternatives.- 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.- 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
- Basic Hashing
- Ethereum Functions
- Multiple Chunks
API Reference
All Keccak256 implementations share the same API. Return type isKeccak256Hash (32-byte Uint8Array with type branding).
Keccak256Hash.from(data: Uint8Array): Keccak256Hash
Hash arbitrary bytes with Keccak-256.
Parameters:
data: Input data to hash (Uint8Array)
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
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
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
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)”)
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)”)
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
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
Constants
Type Alias
All Keccak256 functions returnKeccak256Hash, a branded Uint8Array type:
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):- Part of main
primitives.wasmbundle - Includes all Voltaire primitives and crypto
- Requires async
init()before use - Both faster and smaller than pure JavaScript alternatives
src/crypto/keccak256.wasm.ts + wasm/primitives.wasm
Pure TypeScript
Direct wrapper around @noble/hashes battle-tested implementation:- Zero WASM dependencies
- Constant-time implementation
- Runs everywhere (Node.js, browsers, Deno, Bun, edge workers)
- Thoroughly audited and tested
src/crypto/Keccak256/hash.js
Related
- Keccak256Hash - 32-byte hash type used by Keccak256
- Address - Address derivation uses Keccak256
- SHA256 - Alternative hash function
- RIPEMD160 - Used in Bitcoin address derivation
- Blake2 - High-performance alternative hash

