# Invariant Breaking1

Burve Closure.addValue grows targetX128 and balances proportionally without deMinimus control, letting the value-vs-target invariant drift far beyond designed limits.

- Payout: Audit contest
- Severity: medium
- Program: Burve
- Found by: 0xNov1ce
- Disclosed: 2025-04-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2025-04-burve-judging/issues/319
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-04-burve%3AM-5
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error

**Target:** Closure.addValue / ValueFacet in multi-token AMM
**Root cause:** addValue scales targetX128 and each balance by the same scaleX128 factor, so the gap between real value and n*target grows with each add instead of being bounded by deMinimus.
**Steps:**
1. Add realValue() helper summing ValueLib.v over tokens in closure.
2. Expose queryValue returning realValue, targetX128, deMinimusX128.
3. Set SearchParams(10, 500<<128, 1e18); addSingleForValue(alice,0x7,tokens[1],0.52019107e20,0,0).
4. Call addValue(alice,0x7,3e11/3e16/3e20/3e23,0) sequentially.
5. Observe valueX128 - targetX128*3 grows to ~1.45e42, far exceeding deMinimus*3.
**How found:** Author reasoned that since addValue scales target and balances together without deMinimus clamping, repeated adds should compound the value-target delta; verified with a custom queryValue harness and forge test.
**Insight:** When a protocol documents an invariant bounding value to deMinimus*n*target, instrument the actual stored targetX128 vs recomputed real value after sequences of proportional add/remove operations rather than single calls.

## Proof of concept (copied exactly from the original report)

**Foundry test test_invariant1 demonstrating invariant break**

```
function test_invariant1() public {
        SearchParams memory sp = SearchParams(10, 500 << 128, 1e18);
        vm.prank(owner);
        simplexFacet.setSearchParams(sp);

        valueFacet.addSingleForValue(alice, 0x7, tokens[1], 0.52019107e20, 0, 0);

        uint256 valueX128;
        uint256 targetX128;
        uint256 deMinimusX128;
        
        console.log("Step1:");
        (valueX128, targetX128, , deMinimusX128) = valueFacet.queryValue(address(0), 0x7);
        console.log("valueX128     :", valueX128);
        console.log("targetX128    :", targetX128);
        console.log("deMinimusX128 :", deMinimusX128);
        console.log("valueX128 - targetX128 * 3                   : ", (int256)(valueX128) - (int256)(targetX128) * 3);
        console.log("");

        valueFacet.addValue(alice, 0x7, 3e11, 0);
        valueFacet.addValue(alice, 0x7, 3e16, 0);
        valueFacet.addValue(alice, 0x7, 3e20, 0);
        valueFacet.addValue(alice, 0x7, 3e23, 0);

        console.log("Step2:");
        (valueX128, targetX128, , deMinimusX128) = valueFacet.queryValue(address(0), 0x7);
        console.log("valueX128     :", valueX128);
        console.log("targetX128    :", targetX128);
        console.log("deMinimusX128 :", deMinimusX128);
        console.log("1<<128        : %e", uint256(1<<128));
        console.log("valueX128 - targetX128 * 3                   : %e", (int256)(valueX128) - (int256)(targetX128) * 3);
        console.log("valueX128 - (targetX128 + deMinimusX128) * 3 : %e", (int256)(valueX128) - (int256)(targetX128 + deMinimusX128) * 3);
    }
```

**realValue helper added to Closure.sol**

```
function realValue(Closure storage self) internal returns (uint256 _realValueX128) {
        uint256[MAX_TOKENS] storage esX128 = SimplexLib.getEsX128();
        for (uint8 i = 0; i < MAX_TOKENS; ++i) {
            if (!self.cid.contains(i)) continue;
            _realValueX128 += ValueLib.v(self.targetX128, esX128[i], self.balances[i], false);
        }
    }
```

**queryValue modified in ValueFacet.sol**

```
function queryValue(address owner, uint16 closureId) external 
        returns (
            uint256 valueX128,              
            uint256 targetX128,           
            uint256[MAX_TOKENS] memory,
            uint256 deMinimusX128         
        ) {
        ClosureId cid = ClosureId.wrap(closureId);
        Closure storage c = Store.closure(cid); // Validates cid.
        valueX128 = c.realValue();
        targetX128 = c.targetX128;
        SearchParams memory search = Store.simplex().searchParams;
        deMinimusX128 = uint256(search.deMinimusX128);
    }
```

