oz-utils-safemath
FHESafeMath for Confidential Contracts
Use this skill when a contract mutates encrypted euint64 balances or counters and must
detect overflow, underflow, or uninitialized-handle edge cases. Raw FHE.add and FHE.sub
wrap silently and do not tell you whether the result was valid. FHESafeMath wraps those
operations, returns an ebool success flag, and provides explicit fallback behavior when the
operation would have been unsafe.
When To Use
- Updating encrypted balances in an ERC7984-style token, vault, or ledger
- Incrementing or decrementing encrypted counters where overflow is possible
- Summing encrypted deltas from untrusted sources (user deposits, stream flows)
- Handling the case where an operand may still be an uninitialized handle
- Replacing ad-hoc
FHE.select(FHE.ge(...), newValue, oldValue)patterns with the library form
If the operands are bounded by construction (for example, a small fixed-length tally where
you know the sum fits in euint64), raw FHE.add is fine. Use FHESafeMath where the risk
of wrap-around is real.
More from z-korp/fhevm-cookbook
fhevm-router
Routes Zama FHEVM tasks to the right official docs path and next step
11fhevm-testing
Use when writing, structuring, or debugging tests for FHEVM contracts. Covers mocked mode vs real protocol, Hardhat decrypt helpers, input encryption in tests, and the false-confidence gap between local and testnet behavior.
11fhevm-acl-lifecycle
Use when granting, auditing, or debugging ACL permissions on encrypted handles in FHEVM. Covers FHE.allow, FHE.allowThis, FHE.allowTransient, and the critical rule that new handles do not inherit prior persistent ACL grants.
11fhevm-control-flow
Use when replacing if/else, require, or any conditional logic that depends on encrypted values in FHEVM. Covers FHE.select as the inline branching primitive, fallback semantics on encrypted conditions, and async public decryption when logic must branch back to plaintext state.
11fhevm-public-decryption
Use when implementing two-step public decryption for state-changing operations in FHEVM. Covers makePubliclyDecryptable, off-chain proof retrieval, onchain verification with checkSignatures, and the critical single-step unwrap bug.
11fhevm-cross-contract
Use when passing encrypted handles between contracts, designing multi-contract FHE flows, or debugging handle-not-accessible errors at contract boundaries. Covers allowTransient, allow, permission chains, and factory patterns.
11