risc0-circuit-keccak
Therisc0-circuit-keccak crate implements a specialized circuit for accelerating Keccak-256 (SHA-3) hash computations within the RISC Zero zkVM.
Installation
Overview
Keccak-256 is computationally expensive in the zkVM. This circuit provides hardware acceleration for Keccak operations, making them significantly faster and cheaper in terms of cycles.Most users don’t need to use this crate directly. The zkVM automatically uses the Keccak circuit when you call Keccak functions in guest code.
Feature Flags
Enables proof generation for Keccak operations.
- Implies:
std
Enables CUDA GPU acceleration.
- Implies:
prove
Enables standard library support.
Constants
Default power-of-2 size for Keccak circuit.Provides capacity for 2^17 cycles = 131,072 cycles
Valid power-of-2 range for Keccak circuit.Supports 2^14 to 2^18 cycles
Power-of-2 for recursion of Keccak receipts.
Cycles per Keccak permutation.
Types
Keccak state representation.Represents the 1600-bit (25 × 64-bit) Keccak sponge state.
Functions
get_control_id
Get the control ID for a specific circuit size.Parameters:
po2: Power-of-2 size (must be inKECCAK_PO2_RANGE)
max_keccak_inputs
Calculate maximum number of Keccak inputs for a circuit size.Parameters:
po2: Power-of-2 circuit size
compute_keccak_digest (with prove feature)
Compute the SHA-256 digest of Keccak states.Parameters:
input: Slice of bytes representingKeccakStates
Control IDs
Array of control IDs for different circuit sizes.One control ID per po2 in
KECCAK_PO2_RANGE.Root control ID for all Keccak circuits.
Usage in Guest Code
Using sha3 Crate
The zkVM automatically acceleratessha3 crate operations:
Direct State Manipulation
Performance
Cost Comparison
| Operation | Without Circuit | With Circuit | Speedup |
|---|---|---|---|
| Single Keccak-256 | ~50,000 cycles | ~200 cycles | 250x |
| 100 hashes | ~5M cycles | ~20K cycles | 250x |
The Keccak circuit provides dramatic cycle savings for hash-intensive applications.
Capacity Planning
Circuit Batching
The zkVM automatically batches Keccak operations:- Accumulation Phase: Collect all Keccak operations
- Circuit Invocation: Prove all operations in one circuit
- Result Delivery: Return results to guest
Proving (with prove feature)
Keccak circuit proving.Implements proof generation for batched Keccak operations.
Examples
Basic Keccak Usage
Batch Hashing
Merkle Tree
Ethereum Compatibility
The Keccak-256 implementation matches Ethereum’s hash function:GPU Acceleration
With CUDA enabled, Keccak proving is accelerated:- CPU: ~1 second for 1000 Keccak operations
- GPU: ~0.1 seconds for 1000 Keccak operations
Circuit Size Selection
Choose appropriate circuit size based on usage:| PO2 | Max Hashes | Use Case |
|---|---|---|
| 14 | ~81 | Minimal usage |
| 15 | ~163 | Light usage |
| 16 | ~327 | Moderate usage |
| 17 | ~655 | Default, heavy usage |
| 18 | ~1310 | Very heavy usage |
Zirgen Implementation
Low-level circuit implementation.Generated constraint code for the Keccak permutation.