# Prover can cheat in felt_to_bytes_little due to value underflow

Cairo felt_to_bytes_little loop underflows when prover returns oversized byte, letting a malicious prover spoof arbitrary output bytes and forge create/create2 addresses.

- Payout: Audit contest
- Severity: high
- Program: Kakarot
- Found by: muellerberndt
- Disclosed: 2024-12-10
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2024-09-kakarot-findings/issues/118
- Library page: https://bugbountylibrary.com/r/c4%3A2024-09-kakarot%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:code4rena sev:high tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Cairo bytes.cairo felt_to_bytes_little and dependent conversion functions
**Root cause:** The loop computes value = (value - byte)/base without checking byte <= value, so a prover-supplied byte larger than remaining value underflows to STARKNET_PRIME-1 and the loop continues, allowing arbitrary subsequent bytes.
**Steps:**
1. Patch the split_int hint to return `memory[ids.output] = 2` while `ids.bytes_len < 135`.
2. At bytes_len==135 return the real modulo byte so value reaches zero.
3. Ensure pow256_table[bytes_len] reads a zero in the code segment (e.g. offset 59/61/64) so the minimal-length bound check passes.
4. Run test__felt_to_bytes_little with n=1; observe output b'\x02\x02...' instead of b'\x01'.
5. Brute-force the stop offset per compiler version to find a valid zero in code segment.
**How found:** Code-reading of the loop: noticed value can underflow when byte > remaining value, loop continues since value != 0, and the minimal-length check reads pow256_table from code segment where zeros exist.
**Exploitation:** A malicious prover crafts hints so felt_to_bytes_little returns attacker-chosen bytes for a given felt; these feed get_create_address/get_create2_address, letting the attacker deploy L2 contracts from a spoofed sender address (e.g. to steal funds from account-abstraction wallets).
**Escalation:** Chains byte-spoofing into address forgery for CREATE/CREATE2, enabling theft from AA wallets.
**Insight:** In Cairo/STARK provers, any hint-supplied value not range-checked against the remaining state can underflow the field prime; check whether bound tables read from code segment contain attacker-usable zeros.

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

**Malicious hint patch producing spoofed bytes**

```
hint = f"if ids.bytes_len < 135:\n  memory[ids.output] = 2\n"\
            f"elif ids.bytes_len == 135:\n  ids.base == 2 ** 8\n  memory[ids.output] = res = (int(ids.value) % PRIME) % ids.base\n"\
            "else:  memory[ids.output] = res = (int(ids.value) % PRIME) % ids.base\n  "
            
        with (
            patch_hint(
                cairo_program,
                "memory[ids.output] = res = (int(ids.value) % PRIME) % ids.base\nassert res < ids.bound, f'split_int(): Limb {res} is out of range.'",
                hint,
            ),
        ):
                
            output = cairo_run("test__felt_to_bytes_little", n=1)
```

