# MIPS - Incorrect implementation of SRAV instruction

Optimism's MIPS SRAV emulation failed to mask the shift count to 5 bits, producing wrong results and flipping true dispute-game root claims to false.

- Payout: Audit contest
- Severity: medium
- Program: Optimism
- Found by: KupiaSec
- Disclosed: 2024-08-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-optimism-findings/issues/41
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-optimism%3AM-07
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** MIPS fault-proof VM instruction emulation (Solidity)
**Root cause:** SRAV implementation shifts by the full rs register instead of the low-order five bits, so any higher bit set makes the shift result zero instead of the correct arithmetic shift.
**Steps:**
1. Read MIPS-ISA spec page A-141: SRAV shift count is low-order 5 bits of rs.
2. Inspect Solidity: `return SE(rt >> rs, 32 - rs);` with no `& 0x1F` mask.
3. Craft MIPS program where rs has a bit set above bit 4; emulator yields 0 instead of correct shifted value.
4. Observe dispute game resolves a true root claim as false.
**How found:** Code-reading against the MIPS ISA spec: noticed other shift instructions mask the count with 0x1F but SRAV does not, then reasoned that high bits in rs force a zero result.
**Exploitation:** A malicious or honest party submits a fault-proof dispute where the disputed MIPS execution uses SRAV with rs >= 32; the on-chain emulator computes a wrong result, causing the dispute game to incorrectly rule a valid root claim invalid.
**Insight:** When auditing an emulator/VM, diff each opcode implementation against the ISA spec's operand-width rules; shift-count masking is a classic spot where one opcode diverges from its siblings.

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

**SRAV implementation missing rs mask**

```
else if (func == 0x07) {
    return SE(rt >> rs, 32 - rs);
}
```

**Recommended fix masking rs with 0x1F**

```
else if (func == 0x07) {
    return SE(rt >> (rs & 0x1F), 32 - (rs - 0x1F));
}
```

