Agentic MarketAgentic Market Docs
Security & Audits

11 Mandatory Invariants & Testing

Formal mathematical invariants (INV-1 through INV-11), 16+ adversarial attack tests, and stateful cross-market Foundry fuzzing.

Every state transition in BinaryEngine is continuously checked against 11 formal security invariants inside contract/test/invariants/BinaryEngine.invariant.t.sol.


The 11 Mandatory Formal Invariants

INV-1: Global Cross-Market Solvency (Critical Singleton Gate)

Across any number of concurrent markets mm and any interleaved sequence of launches, trades, pauses, disputes, sweeps, bond updates, and redemptions:

USDT.balanceOf(Engine)≥1012×(vaultBalance+∑a(internalBalancea+bondFreea+bondLockeda)+∑m(collateralm+feeAccm))\text{USDT.balanceOf}(\text{Engine}) \ge 10^{12} \times \left(\text{vaultBalance} + \sum_a \left(\text{internalBalance}_a + \text{bondFree}_a + \text{bondLocked}_a\right) + \sum_{m} \left(\text{collateral}_m + \text{feeAcc}_m\right)\right)

INV-2: Per-Market Solvency & Cross-Market Isolation

Complete-set conservation ∀i, ri+vi+ui=Cm\forall i,\ r_i + v_i + u_i = C_m holds after every operation, redemption liabilities never exceed markets[marketId].collateral, and mutating market AA leaves every field of market B≠AB \ne A bit-for-bit unchanged.

INV-3: Probability Unity & Virtual TWAP Cutoff

At every block, ∑Pi=\sum P_i = PRICE_SCALE and ∑TWAPi=\sum \text{TWAP}_i = PRICE_SCALE. Triggering Void days after cutoffTime yields the exact TWAP frozen at tstop=min⁡(block.timestamp, cutoffTime, pauseTime)t_{\text{stop}} = \min(\text{block.timestamp},\ \text{cutoffTime},\ \text{pauseTime}).

INV-4: Anti-Void Front-Running

A large 1-block buy immediately preceding pause() never yields positive net arbitrage profit upon TWAP Void redemption.

INV-5: Overflow Immunity, Exact 101210^{12} Conversion & Hint Equivalence

Zero arithmetic overflows at extreme reserves; every external USDT transfer is an exact multiple of 1012 wei10^{12}\ \text{wei}; multi-outcome sell hints (V1.1) match deterministic bisection.

INV-6: Strict sweep() Non-Interference

resolvePriceMarketAndSweep(), finalizeAndSweep(), arbitrate(), and voidIf*() never reduce markets[marketId].collateral below the sum of unclaimed user entitlements.

INV-7: MarketKey Cryptographic Integrity & Saturating netCostBasis

Mutating any single bit in MarketKey reverts with MarketNotInitialized. Profitable sells saturate netCostBasis to 0 without arithmetic underflow.

INV-8: Unstoppable Exits & Full-Exit Exemption

Even under total AdapterRegistry failure (revert, OOG, or malformed return), sell(), redeem(), redeemVoid(), redeemBatch(), and withdrawBond() succeed 100% of the time. Full exits (type(uint40).max) bypass MIN_TRADE_AMOUNT.

INV-9: Strict Share Non-Transferability

No internal or external code path permits transferring position shares between distinct addresses.

INV-10: O(1)O(1) Leaky-Bucket Circuit Breaker & Vault Loss Bound

Rolling 7-day net Vault debits never exceed maxOutflowPerWindow plus linear time recovery, and net Vault loss on any single market never exceeds key.seed.

INV-11: Absolute Liveness (Zero Permanent Deadlocks)

Every non-terminal state (Active, Paused, TradingClosed, Proposed, Disputed) has a verified permissionless timeout path to Final or Void.


Verification Suites & Attack Coverage

  • Unit & Library Tests (contract/test/unit/*.t.sol): Exact fuzz & unit coverage for BinaryEngine, AdapterRegistry, BinaryFpmmLib, BinaryTwapLib, FeeMathLib, and LeakyBucketLib.
  • 16 Adversarial Attack Scenarios (BinaryEngineAttacks.t.sol): Dust buy revert, full-exit dust sell, MarketKey bit-flipping, cross-market EIP-1153 reentrancy, duplicate redeemBatch, zero-volume fallback proposer reward, 7-day post-cutoff TWAP freeze, phaseId + 1 Chainlink rollover, second-dispute rejection, stalled Guardian/Arbiter timeouts, and total AdapterRegistry outage.
  • 10 Audit Regression Tests (BinaryEngineAuditRegressions.t.sol): Proves every fix (M-01–M-03, L-01–L-02), assembly log3 differential parity, dirty calldata upper-bit immunity, 85-checkpoint ring wraparound, and unsolicited 1-wei USDT donations.
  • Live Anvil + Viem E2E (verify-anvil-e2e.ts & app/test/integration/anvil-e2e.test.ts): Verifies Viem decoding of assembly log3 MarketCreated and unit-for-unit quote-vs-onchain parity.