Documentation Index
Fetch the complete documentation index at: https://mintlify.com/risc0/risc0/llms.txt
Use this file to discover all available pages before exploring further.
Cryptographic Precompiles
RISC Zero’s zkVM includes specialized extension circuits called “precompiles” for cryptographic and algebraic functions. These accelerators provide 10-100x speedups for supported operations.Overview
Precompiles are specialized circuits implemented directly in the “hardware” of the zkVM, similar to AES-NI or SHA extensions in x86 processors. Programs that use precompiles:- Execute faster (fewer cycles)
- Prove with significantly less resources
- Require no code changes when using patched crates
Supported Operations
- SHA-256: ~68 cycles per 64-byte block
- Keccak-256: Optimized hashing
- RSA: Modular exponentiation
- Elliptic Curves: secp256k1 (k256), secp256r1 (p256), ed25519, BLS12-381, BN254
- 256-bit Modular Multiplication: ~10 cycles
Using Patched Crates
RISC Zero provides patched versions of popular cryptographic crates that automatically use precompiles. Simply add the patch to your guest’sCargo.toml.
Available Patched Crates
| Crate | Versions Supported | Use Case |
|---|---|---|
sha2 | 0.10.9, 0.10.8, 0.10.7, 0.10.6, 0.9.9 | SHA-256, SHA-512 |
tiny-keccak | 2.0.2 | Keccak-256 |
k256 | 0.13.4, 0.13.3, 0.13.2, 0.13.1 | secp256k1 (Bitcoin, Ethereum) |
p256 | 0.13.2 | secp256r1 (NIST P-256) |
curve25519-dalek | 4.1.3, 4.1.2, 4.1.1, 4.1.0 | Ed25519 signatures |
rsa | 0.9.9 | RSA encryption/signatures |
substrate-bn | 0.6.0 | BN254 pairing |
bls12_381 | 0.8.0 | BLS12-381 pairing |
blst | 0.3.16, 0.3.15, 0.3.14 | BLS signatures |
crypto-bigint | 0.5.5, 0.5.4, 0.5.3, 0.5.2 | Big integer arithmetic |
c-kzg | 1.0.3, 2.1.0, 2.1.1, 2.1.5 | KZG commitments (EIP-4844) |
For the most up-to-date versions and tags, check the
/releases page in each fork’s GitHub repository.How to Apply Patches
Step 1: Add the Dependency
In your guest’sCargo.toml, specify the exact version:
methods/guest/Cargo.toml
Step 2: Apply the Patch
In the sameCargo.toml, add the patch:
methods/guest/Cargo.toml
Step 3: Update Cargo.lock
If needed, update the lockfile to the specific version:Step 4: Verify the Patch
Check that the patch is applied by searching for the crate in your guest’sCargo.lock:
Example: ECDSA Signature Verification
Here’s a complete example using precompiled k256 for ECDSA verification:Guest Cargo.toml
methods/guest/Cargo.toml
Guest Code
methods/guest/src/main.rs
Performance Comparison
SHA-256 Hashing
| Implementation | Cycles per 64-byte block |
|---|---|
| Pure Rust | ~2,000 |
| Precompile | ~68 |
secp256k1 Operations
| Operation | Without Precompile | With Precompile |
|---|---|---|
| Point multiplication | ~800,000 cycles | ~80,000 cycles |
| ECDSA verification | ~1,600,000 cycles | ~160,000 cycles |
| Linear combination (lincomb) | ~1,500,000 cycles | ~150,000 cycles |
Indirect Usage
Some crates use cryptography indirectly through dependencies. You can still benefit from precompiles by applying Cargo patches without code changes.Example: OAuth2 Crate
Theoauth2 crate uses sha2 internally. To accelerate it:
methods/guest/Cargo.toml
Selecting Backend Crates
Some crates allow choosing different cryptographic backends:methods/guest/Cargo.toml
If you need a crate that’s not listed, reach out on Discord to request it!
Adding Precompile Support to Your Crates
You can add precompile support to your own crates. Here’s the general approach:Example: Adding to k256
The k256 fork diff shows how to:- Detect when running in the zkVM using
#[cfg(target_os = "zkvm")] - Replace core operations with precompiled equivalents
- Use zkVM-specific intrinsics for accelerated operations
Key Implementation Example
Security Considerations
Timing Attacks
Be very careful if using precompiles with private data, such as:- Signing messages within the zkVM where observers can measure proving time
- Operations where cycle counts could leak information about secrets
Troubleshooting
Patch Not Applied
Problem: The patch doesn’t seem to be working. Solutions:- Check that versions match exactly:
sha2 = "=0.10.9"notsha2 = "0.10" - Run
cargo update -p sha2 --precise 0.10.9 - Delete
Cargo.lockandtarget/and rebuild - Verify in
Cargo.lockthat the source points to the fork repository
Version Conflicts
Problem: Multiple versions of a crate are required. Solutions:- Use
cargo tree -dto find duplicate dependencies - Patch all versions that are used:
- Consider updating dependencies to use compatible versions
Missing Precompile Stats
Problem: Not seeing precompile usage in output. Solution: Run withRISC0_INFO=1:
Next Steps
- See Performance Optimization for general optimization techniques
- Use Profiling to verify precompile benefits
- Explore GPU Acceleration for faster proving
- Learn about Recursive Proving for scalability