Optimizing Poseidon and BLAKE3 in Solidity

2026-08-15

Recent hash-related news from the Ethereum Foundation made me revisit the Solidity implementations of Poseidon and BLAKE3, purely out of curiosity. Looking at the old code, both had plenty of room for improvement.

Poseidon was doing dense matrix multiplication in the partial rounds and reducing modulo the field more often than necessary. BLAKE3 was moving a large hashing state between library calls and doing 32-bit arithmetic through Solidity arrays.

So I pointed Codex 5.6-sol at both implementations to see what it could optimize:

ImplementationOriginal gasOptimized gasReduction
Poseidon T321,06415,26527.5%
Poseidon T437,59519,38048.5%
BLAKE3, 64 bytes879,23811,39798.7%
BLAKE3, 1 KiB4,849,482118,14497.6%

These measurements compare the original and optimized versions in each repository's Foundry benchmark harness. A 27.5% reduction for Poseidon is already substantial. The 98.7% reduction for BLAKE3 surprised me.

I differentially fuzzed both optimized versions against Rust reference implementations to check that the rewrites preserve the expected hash outputs.

The code and benchmark details are in blake3-sol and poseidon-solidity. To my eye, the optimized BLAKE3 implementation is surprisingly simple and elegant. It's an exciting time to work on cryptography and low-level engineering.