# Non-finalized dictionary in RIPEMD160 allows forging of output

Unfinalized Cairo dictionary in Kakarot's RIPEMD160 precompile lets a malicious prover forge hash outputs for inputs over 55 bytes.

- Payout: Audit contest
- Severity: high
- Program: Kakarot
- Found by: RadiantLabs
- Disclosed: 2024-12-10
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2024-09-kakarot-findings/issues/54
- Library page: https://bugbountylibrary.com/r/c4%3A2024-09-kakarot%3AH-04
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:high tech:solidity technique:incomplete-fix technique:source-review vuln:crypto

**Target:** RIPEMD160 precompile (Cairo)
**Root cause:** When next_block is FALSE, the dictionary x is reassigned via default_dict_new(0) without calling default_dict_finalize on the old dictionary, so prior dict_read/dict_write entries are never squashed or validated.
**Steps:**
1. Call ripemd160.finish() with input size > 55 bytes so next_block == FALSE path is taken.
2. Observe old dictionary x is dropped at L470 without default_dict_finalize(start, x, 0).
3. Inject a hint modifying the prover's in-memory dict_tracker.data[index_4] before absorb_data's dict_read.
4. dict_read appends attacker value; since dict is never squashed, no consistency check fails.
5. Prove a forged RIPEMD160 output differing from the real hash (PoC test asserts mismatch).
**How found:** Code review of ripemd160.finish() control flow: noticed the early-return branch finalizes the dict but the multi-block branch reassigns x without finalizing, then traced dict_read in absorb_data to confirm unvalidated reads.
**Exploitation:** A malicious prover supplies a hint that overwrites the in-memory dictionary entry read by absorb_data; because the dictionary is never finalized/squashed, the forged read is accepted and an incorrect hash output can be proven for any input >55 bytes.
**Insight:** In Cairo/STARK provers, any dict reassignment without default_dict_finalize leaves prior reads unconstrained; audit every dict_new/reset path for a matching finalize, especially in precompiles with branching control flow.

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

**Test forging RIPEMD160 output via patched hint**

```
async def test_ripemd160_output_can_be_forged(self, cairo_program, cairo_run):
	msg_bytes = bytes([0x00] * 57)
	with (
		patch_hint(
			cairo_program,
			"vm_enter_scope()",
			"try:\n"
			"    dict_tracker = __dict_manager.get_tracker(ids.dict_ptr)\n"
			"    dict_tracker.data[ids.index_4] = 1\n"
			"except Exception: pass\n"
			"vm_enter_scope()"
		)
	):
		precompile_hash = cairo_run("test__ripemd160", msg=list(msg_bytes))

		# Hash with RIPEMD-160 to compare with precompile result
		ripemd160_crypto = RIPEMD160.new()
		ripemd160_crypto.update(msg_bytes)
		expected_hash = ripemd160_crypto.hexdigest()

		assert expected_hash.rjust(64, "0") != bytes(precompile_hash).hex()
```

**Noop hints enabling malicious dict injection**

```
%{ vm_enter_scope() %}
    %{ vm_exit_scope() %}
```

