Skip to content

Conversation

@omarinuwa
Copy link

Implements 13 comprehensive property tests for concentrated liquidity AMMs addressing GitHub issue #50. These properties detect critical vulnerabilities including the KyberSwap Elastic hack ($50M exploit from November 2023).

Properties Implemented

Tick Math Properties (5 properties)

  • CL-TICK-001: Tick direction matches price direction (monotonicity)
  • CL-TICK-002: No price overshoot past tick boundaries (KyberSwap fix)
  • CL-TICK-003: Tick spacing alignment requirements

The price overshoot property directly addresses the KyberSwap vulnerability where using inequality (!=) instead of directional comparison (<, >) allowed price to exceed the next tick boundary, causing liquidity to be double-counted.

Liquidity Conservation Properties (3 properties)

  • CL-LIQUIDITY-001: LiquidityNet sums to zero (conservation law)
  • CL-LIQUIDITY-002: Current liquidity equals sum of deltas ≤ currentTick
  • CL-LIQUIDITY-003: Liquidity values cannot be negative

These verify the fundamental conservation laws of the asymmetric liquidity accounting system where every position adds +L at tickLower and -L at tickUpper.

Mint/Burn Properties (4 properties)

  • CL-MINT-001: Mint increases current liquidity only when in range
  • CL-MINT-002: Mint always increases liquidityGross at both ticks
  • CL-MINT-003: Mint updates liquidityNet asymmetrically (+L/-L)
  • CL-BURN-001: Burn exactly reverses mint state changes

The asymmetric update pattern is critical - using symmetric updates would cause positions to never deactivate or incorrectly accumulate liquidity.

Swap Properties (4 properties)

  • CL-SWAP-001: Price moves in correct direction based on trade side
  • CL-SWAP-002: Swap respects sqrtPriceLimitX96 (slippage protection)
  • CL-SWAP-003: Liquidity updated when crossing tick boundaries
  • CL-SWAP-004: Price stays within valid bounds [MIN_SQRT_RATIO, MAX_SQRT_RATIO]

Resolves #50

Implements 13 comprehensive property tests for concentrated liquidity AMMs
addressing GitHub issue crytic#50. These properties detect critical vulnerabilities
including the KyberSwap Elastic hack ($50M exploit from November 2023).

## Properties Implemented

### Tick Math Properties (5 properties)
- CL-TICK-001: Tick direction matches price direction (monotonicity)
- CL-TICK-002: No price overshoot past tick boundaries (KyberSwap fix)
- CL-TICK-003: Tick spacing alignment requirements

The price overshoot property directly addresses the KyberSwap vulnerability where
using inequality (!=) instead of directional comparison (<, >) allowed price to
exceed the next tick boundary, causing liquidity to be double-counted.

### Liquidity Conservation Properties (3 properties)
- CL-LIQUIDITY-001: LiquidityNet sums to zero (conservation law)
- CL-LIQUIDITY-002: Current liquidity equals sum of deltas ≤ currentTick
- CL-LIQUIDITY-003: Liquidity values cannot be negative

These verify the fundamental conservation laws of the asymmetric liquidity
accounting system where every position adds +L at tickLower and -L at tickUpper.

### Mint/Burn Properties (4 properties)
- CL-MINT-001: Mint increases current liquidity only when in range
- CL-MINT-002: Mint always increases liquidityGross at both ticks
- CL-MINT-003: Mint updates liquidityNet asymmetrically (+L/-L)
- CL-BURN-001: Burn exactly reverses mint state changes

The asymmetric update pattern is critical - using symmetric updates would cause
positions to never deactivate or incorrectly accumulate liquidity.

### Swap Properties (4 properties)
- CL-SWAP-001: Price moves in correct direction based on trade side
- CL-SWAP-002: Swap respects sqrtPriceLimitX96 (slippage protection)
- CL-SWAP-003: Liquidity updated when crossing tick boundaries
- CL-SWAP-004: Price stays within valid bounds [MIN_SQRT_RATIO, MAX_SQRT_RATIO]

## Files Added

**Core Property Contracts:**
- contracts/ConcentratedLiquidity/internal/properties/TickMathProperties.sol
- contracts/ConcentratedLiquidity/internal/properties/LiquidityProperties.sol
- contracts/ConcentratedLiquidity/internal/properties/MintBurnProperties.sol
- contracts/ConcentratedLiquidity/internal/properties/SwapProperties.sol

**Utilities and Base Contracts:**
- contracts/ConcentratedLiquidity/util/IConcentratedLiquidityPool.sol
- contracts/ConcentratedLiquidity/util/TickMath.sol (from Uniswap V3)
- contracts/ConcentratedLiquidity/internal/util/ConcentratedLiquidityTestBase.sol

**Documentation and Tests:**
- contracts/ConcentratedLiquidity/README.md (comprehensive guide)
- tests/ConcentratedLiquidity/foundry/src/SimpleMockPool.sol
- tests/ConcentratedLiquidity/foundry/test/CryticConcentratedLiquidityTest.sol

**Updated:**
- CLAUDE.md (added ConcentratedLiquidity to project overview)

## Key Features

1. **Comprehensive NatSpec Documentation**: Each property includes:
   - WHY THIS MUST BE TRUE explanation
   - Mathematical proof or detailed example
   - WHAT BUG THIS CATCHES section
   - Real-world vulnerability references (KyberSwap)

2. **Real Vulnerability Detection**: Properties specifically target:
   - KyberSwap Elastic hack (price overshoot, liquidity double-counting)
   - Conservation law violations (liquidity leaks)
   - Asymmetric update errors (positions not deactivating)
   - Tick spacing violations (orphaned liquidity)

3. **Production-Ready TickMath Library**: Complete Uniswap V3 implementation
   for accurate tick-price conversions in property tests.

4. **Example Test Harness**: SimpleMockPool with complete harness demonstrating
   how to combine properties with pool implementations.

5. **Extensive README**: Includes usage examples, vulnerability explanations,
   integration patterns, and resources.

## Testing Approach

These properties support internal testing mode (test contract inherits from pool).
External testing mode can be added following the same pattern as ERC20/ERC721.

## Integration

Properties can be combined with existing ERC20 properties since pools typically
use ERC20 tokens:

```solidity
contract CombinedTest is
    CryticERC20BasicProperties,
    CryticTickMathProperties
{ ... }
```

Resolves crytic#50
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Omar Inuwa seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature-request]: concentrated liquidity/ tick math properties

2 participants