fhenix-gas-optimization
Installation
SKILL.md
Fhenix Gas Optimization
Overview
FHE operations are computationally expensive and each one emits an event for off-chain compute, so optimization is mostly about doing fewer, smaller FHE ops and batching on-chain settlement. (The official gas-and-benchmarks page is marked "Coming Soon" — these strategies come from the FHE best-practices guidance; benchmark on testnet for real numbers.)
Strategies
- Minimize FHE operations. Each
FHE.*call adds overhead — combine/eliminate redundant ops. Reuse intermediate handles instead of recomputing. - Use the smallest safe bit-width.
euint8/euint16/euint32overeuint64/euint128when the value range allows — narrower types are cheaper to compute on. - Cache encrypted constants. Trivially-encrypt frequently used values (
0,1, thresholds) once (e.g. in the constructor),FHE.allowThis(...), and reuse — don't re-encrypt per call.euint32 ONE; constructor() { ONE = FHE.asEuint32(1); FHE.allowThis(ONE); } function inc() external { counter = FHE.add(counter, ONE); FHE.allowThis(counter); } - Batch decrypt settlement. Use
FHE.publishDecryptResultBatch/FHE.verifyDecryptResultBatchto handle multiple results/signatures in one tx instead of N transactions. - Avoid unnecessary
selectbranches. Both branches ofFHE.selectalways execute — precompute shared sub-expressions and don't put expensive ops in both paths. - Don't store
InE*inputs — convert toeuintXXwithFHE.asE…first (input structs carry extra metadata that costs gas).