# RIPEMD160 precompile crashes with a Cairo exception for some input lengths

RIPEMD160 precompile in Kakarot reinitializes its Cairo dict without updating the start pointer, so certain input lengths crash with a Cairo exception and revert.

- Payout: Audit contest
- Severity: medium
- Program: Kakarot
- Found by: muellerberndt
- Disclosed: 2024-12-10
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-09-kakarot-findings/issues/120
- Library page: https://bugbountylibrary.com/r/c4%3A2024-09-kakarot%3AM-01
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:solidity technique:fuzzing technique:source-review vuln:dos

**Target:** RIPEMD160 precompile (address 0x03) in Kakarot Cairo implementation
**Root cause:** In finish(), the dict x is redefined via default_dict_new(0) but the start pointer still references the original dict's first segment; default_dict_finalize(start, x, 0) then subtracts relocatable values from different segments and fails.
**Steps:**
1. Deploy a contract calling staticcall to precompile 0x03 with input length 55.
2. Call hashInputLength32() with 55-byte input.
3. Observe Cairo exception: 'can't subtract two relocatable values with different segment indexes' / squash_dict error.
4. Compare with 20-byte input which returns correct hash.
**How found:** Code reading of the finish() function spotted x being reassigned while start still pointed to the old dict, then confirmed via e2e Solidity test (55 bytes) and a Cairo hypothesis test over input lengths 56-63.
**Exploitation:** Any L2 contract invoking RIPEMD160 with affected input lengths reverts; no value extraction, DoS of dependent contracts only.
**Insight:** In Cairo, reassigning a dict pointer without updating the saved start pointer breaks default_dict_finalize's segment subtraction; fuzz precompile input lengths across block boundaries (55/56/63/64) since padding branches differ.

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

**Solidity contract calling RIPEMD160 precompile with 55-byte input**

```
contract RIPEMDTest {

        function computeRipemd160(bytes memory data) public view returns (bytes20) {
            bytes20 result;
            assembly {
                // Load the free memory pointer
                let ptr := mload(0x40)

                // Get the length of the input data
                let len := mload(data)

                // Call the RIPEMD-160 precompile (address 0x03)
                let success := staticcall(
                    gas(),           // Forward all available gas
                    0x03,            // Address of RIPEMD-160 precompile
                    add(data, 32),   // Input data starts after the length field (32 bytes)
                    len,             // Length of the input data
                    ptr,             // Output will be written to ptr
                    32               // The precompile writes 32 bytes (even though RIPEMD-160 outputs 20 bytes)
                )

                // Check if the call was successful
                if iszero(success) {
                    revert(0, 0)
                }

                // Read the first 20 bytes of the output (RIPEMD-160 outputs 20 bytes)
                result := mload(ptr)
            }
            return result;
        }

        function hashInputLength20() external view returns (bytes20) {
            bytes memory inputData = new bytes(20);

            for (uint256 i = 0; i < 20; i++) {
                inputData[i] = 0x61;
            }

            return computeRipemd160(inputData);
        }

        function hashInputLength32() external view returns (bytes20) {
            bytes memory inputData = new bytes(55);
            for (uint256 i = 0; i < 32; i++) {
                inputData[i] = 0x61;
            }
            return computeRipemd160(inputData);
        }
    }
```

**Cairo hypothesis test reproducing crash for 56-63 byte inputs**

```
import pytest
    from Crypto.Hash import RIPEMD160
    from hypothesis import given, settings
    from hypothesis.strategies import binary


    @pytest.mark.asyncio
    @pytest.mark.slow
    class TestRIPEMD160:
        @given(msg_bytes=binary(min_size=56, max_size=63))
        @settings(max_examples=3)
        async def test_ripemd160_should_return_correct_hash(self, cairo_run, msg_bytes):
            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()
```

