# Wrong leg chunkKey calculation in haircutPremia function

In PanopticMath.haircutPremia, the leg chunkKey was computed with index 0 instead of the actual leg, miscrediting settled premium during liquidations.

- Payout: Audit contest
- Severity: medium
- Program: Panoptic
- Found by: Aymen0909
- Disclosed: 2024-06-24
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-panoptic-findings/issues/374
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-panoptic%3AM-08
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error

**Target:** PanopticMath.haircutPremia / PanopticPool.liquidate (Solidity library)
**Root cause:** The chunkKey for settledTokens was built from tokenId.strike(0)/width(0)/tokenType(0) inside a loop over legs, so every long leg's haircut premium was credited to leg 0's chunk instead of its own chunk.
**Steps:**
1. Call PanopticPool.liquidate on a position with multiple long legs (legs 1-3).
2. liquidate invokes PanopticMath.haircutPremia, which loops legs and computes chunkKey via `keccak256(abi.encodePacked(tokenId.strike(0), tokenId.width(0), tokenId.tokenType(0)))`.
3. Observe settledTokens for legs 1-3 never updated; all haircut amounts accumulate on leg 0's chunkKey.
4. Fix: use `tokenId.strike(leg), tokenId.width(leg), tokenId.tokenType(leg)`.
**How found:** Code reading of the haircutPremia loop: noticed the chunkKey used hardcoded index 0 while iterating over leg, an inconsistency with the per-leg premium amounts being computed.
**Insight:** When auditing loops that index per-leg/per-item storage keys, verify every key derivation uses the loop variable, not a literal 0 — copy-paste of leg-0 accessors is a recurring source of misattributed accounting.

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

**haircutPremia loop using leg 0 chunkKey**

```
for (uint256 leg = 0; leg < tokenId.countLegs(); ++leg) {
            if (tokenId.isLong(leg) == 1) {
                mapping(bytes32 chunkKey => LeftRightUnsigned settledTokens)
                    storage _settledTokens = settledTokens;

                // calculate amounts to revoke from settled and subtract from haircut req
                uint256 settled0 = Math.unsafeDivRoundingUp(
                    uint128(-_premiasByLeg[i][leg].rightSlot()) * uint256(haircut0),
                    uint128(longPremium.rightSlot())
                );
                uint256 settled1 = Math.unsafeDivRoundingUp(
                    uint128(-_premiasByLeg[i][leg].leftSlot()) * uint256(haircut1),
                    uint128(longPremium.leftSlot())
                );

                //@audit always calculating the chunkKey of leg 0  
                bytes32 chunkKey = keccak256(
                    abi.encodePacked(
                        tokenId.strike(0),
                        tokenId.width(0),
                        tokenId.tokenType(0)
                    )
                );

                // The long premium is not commited to storage during the liquidation, so we add the entire adjusted amount
                // for the haircut directly to the accumulator
                settled0 = Math.max(
                    0,
                    uint128(-_premiasByLeg[i][leg].rightSlot()) - settled0
                );
                settled1 = Math.max(
                    0,
                    uint128(-_premiasByLeg[i][leg].leftSlot()) - settled1
                );

                _settledTokens[chunkKey] = _settledTokens[chunkKey].add(
                    LeftRightUnsigned.wrap(0).toRightSlot(uint128(settled0)).toLeftSlot(
                        uint128(settled1)
                    )
                );
            }
        }
    }

    return (collateralDelta0, collateralDelta1);
}
```

