# In Stable2LUT1::getRatiosFromPriceLiquidity, in extreme cases, updateReserve will start breaking

Stable2LUT1's extreme price range and oversized lowPriceJ cause Newton's method reserve corrections to underflow or never converge, bricking getRatiosFromPriceLiquidity.

- Payout: Audit contest
- Severity: medium
- Program: Basin
- Found by: Egis\_Security
- Disclosed: 2024-09-03
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-basin-findings/issues/22
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-basin%3AM-02
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** Stable2LUT1::getRatiosFromPriceLiquidity / calcReserveAtRatioLiquidity
**Root cause:** The LUT price range between highPrice 0.27702e6 and lowPrice 0.001083e6 is too wide, and lowPriceJ (2000e18) makes step sizes so large that reserve corrections underflow or oscillate without converging.
**Steps:**
1. Set reserves=[1e18,1e18], ratios=[8,1], call `_f.calcReserveAtRatioLiquidity(reserves,0,ratios,data)` in BeanstalkStable2LiquidityTest
2. Observe target price 125000 vs current price 1083 (lowPrice)
3. Iteration 0: reserve 2000e18 decreased by 893.8e18; iteration 1: reserve 1106e18 decreased by 887.2e18; iteration 2: reserve 218.9e18 decreased by 823.6e18 -> underflow panic
4. Repeat with lowPriceJ lowered to 10e18: price overshoots to 272070, then oscillates; Newton's method exits after 255 iterations with reserve uninitialized, returning 0
**How found:** Reading the LUT edge-case branch for prices below 0.001083e6, noticing the wide high/low price gap and huge lowPriceJ, then hypothesizing large reserve corrections would underflow or fail to converge and testing with Foundry.
**Insight:** When auditing iterative numeric solvers (Newton's method) fed by lookup tables, check whether the LUT's step size (lowPriceJ) can exceed the reserve itself, causing underflow or non-convergence at range extremes.

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

**Foundry test triggering reserve underflow**

```
function test_calcReserveAtRatioLiquiditExtreme() public view {
        uint256[] memory reserves = new uint256[](2);
        reserves[0] = 1e18;
        reserves[1] = 1e18;
        uint256[] memory ratios = new uint256[](2);
        ratios[0] = 8;
        ratios[1] = 1;

        uint256 reserve0 = _f.calcReserveAtRatioLiquidity(reserves, 0, ratios, data);
    }
```

