Reproduced Exploit
QuantAMM: tokenIndex minus 4 uses a token weight as its own multiplier
For a pool with more than 4 tokens, the multiplier for token 4 lives at index 4 of the second weight word. The code subtracts 4 from tokenIndex first, so the index is 0 and the weight is passed as the multiplier. With weight 0.2e18, multiplier wrongly 0.2e18, and 2 blocks elapsed, the normalized we…
Loss
A withdrawal pays 600e18 of token 4 instead of the fair 200e18. LPs keep 400e18 instead of 800e18
Chain
Ethereum
Category
logic
Date
Dec 2024
Source
AuditVault
EVM Playground
Source-level debugger — step opcodes and Solidity in sync
The attack is replayed in an in-browser EVM preloaded with the exact dumped fork state. The execution tree shows every call; step by Solidity line or by opcode across all depths — source, Stack, Memory, Storage, Balances (native / ERC-20 / NFT), Transient storage and Return value stay in sync. Click a tree node, opcode, or source line to jump. Query opens a search over the trace: previous and next writes, balance changes and calls, breakpoints, and a diff of everything that changed between two steps. No backend, no live RPC.
Source & credit. Reproduction of a public audit finding curated by AuditVault — the original finding: 44295-token-index-error-in-getnormalizedweights-calculates-incorre. Standalone Foundry PoC and full write-up: 44295-token-index-error-in-getnormalizedweights-calculates-incorre_exp in the
evm-hack-registrymirror.
Vulnerability classes:
vuln/logic/wrong-condition
Key info#
| Field | Value |
|---|---|
| Loss | A withdrawal pays 600e18 of token 4 instead of the fair 200e18. LPs keep 400e18 instead of 800e18 |
| Vulnerable contract | QuantAMMWeightedPool._getNormalizedWeights |
| Attacker EOA | 0x1111111111111111111111111111111111111111 |
| Attack contract | Exploit |
| Attack tx | Exploit.run() |
| Chain / block / date | Ethereum model · block 0 · 2024-12 |
| Compiler | solc 0.8.27, cancun |
| Bug class | second-slot multiplier index is reset to 0 |
TL;DR#
For a pool with more than 4 tokens, the multiplier for token 4 lives at index 4 of the second weight word. The code subtracts 4 from tokenIndex first, so the index is 0 and the weight is passed as the multiplier. With weight 0.2e18, multiplier wrongly 0.2e18, and 2 blocks elapsed, the normalized weight is 0.6e18 instead of 0.2e18. A withdrawal of the 1000e18 reserve pays 600e18.
The vulnerable code#
tokenIndex = this.shiftIndex(tokenIndex);
// shiftIndex returns tokenIndex - 4
// @> VULN: multiplier index collapses onto the weight slot
Do not subtract 4. The finding's mitigation is to leave tokenIndex at 4 so secondFourWeights[tokenIndex] is the multiplier.
Root cause#
The first slot correctly sets tokenIndex = 4 and reads multipliers at indices 4 through 7. The second slot then does tokenIndex - 4, which the comment in the report calls out as resetting the index to 0. Weight and multiplier become the same stored value, and calculateBlockNormalisedWeight treats a 0.2 wad weight as a per-block multiplier.
Preconditions#
nTokensis 5.- Token 4 weight is
2e17. The real multiplier at index 4 is 0. - Time since the last update is fixed at 2 so the result does not depend on the chain clock.
- The pool reserve of token 4 is
1000e18.
Attack walkthrough#
withdrawToken4loads normalized weights.shiftIndexreturns4 - 4 = 0.- Token 4 is interpolated with
secondFourWeights[0], the weight, as the multiplier. - The normalized weight is
6e17. - The pool transfers
600e18. The fair amount at weight2e17is200e18. - The pool is left holding
400e18.
Diagrams#
Impact#
LPs of token 4 lose 400e18 on this withdrawal: the caller receives 600 where a correct weight pays 200, and 400 remains in the pool instead of 800. Every later swap or join that reads normalized weight 4 is priced off the same wrong index.
How to reproduce#
cd 44295-token-index-error-in-getnormalizedweights-calculates-incorre_exp
forge test -vv
output.txt is the captured passing run. The control test checks the recommended fix.
Sources#
- AuditVault finding: https://github.com/Auditware/AuditVault/blob/main/findings/44295-token-index-error-in-getnormalizedweights-calculates-incorre.md
- Report: https://github.com/solodit/solodit_content/blob/main/reports/Cyfrin/2024-12-17-cyfrin-quantamm-v1.2.md
- Provenance: Cyfrin QuantAMM v1.2, 2024-12-17, 0kage. Fixed in QuantAMMProtocol/QuantAMM-V1 commit 60815f2. Vulnerable indexing is in QuantAMMWeightedPool._getNormalizedWeights.
Sources & further analysis#
Reproductions & code
- Standalone PoC + full trace: 44295-token-index-error-in-getnormalizedweights-calculates-incorre_exp (evm-hack-registry mirror).
- AuditVault finding: 44295-token-index-error-in-getnormalizedweights-calculates-incorre.
Alerts & third-party analyses
These dashboards index community alerts tweets, post-mortems, and independent write-ups. Reach them through the protocol name above to cross-check this reproduction against other analyses.