# RIPEMD-160 precompile yields wrong hashes for large set of inputs due to off-by-one error

Kakarot's RIPEMD-160 precompile used an off-by-one length check, producing wrong hashes for inputs of length 55 mod 64, breaking hash-dependent logic.

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

**Target:** RIPEMD-160 EVM precompile (Cairo implementation)
**Root cause:** The padding branch condition used `is_nn_le(55, len)` (len >= 55) instead of len >= 56, so inputs with len mod 64 == 55 take the wrong padding path and hash incorrectly.
**Steps:**
1. Compare Kakarot's `next_block = is_nn_le(55, len)` against Go/Geth's `tc%64 < 56` discriminator.
2. Note the edge case len == 55 selects opposite branches.
3. Add test hashing a 55-byte message via `test__ripemd160` and compare to Python RIPEMD160.
4. Observe mismatched digests confirming wrong output for len == 55 + k*64.
**How found:** Code-reading the Cairo padding logic and diffing the branch condition against the reference Go implementation used by Geth; the off-by-one at the 55/56 boundary stood out.
**Insight:** When auditing reimplementations of crypto primitives, diff branch boundary conditions against a reference implementation; the len mod 64 == 55/56 padding boundary is a classic off-by-one spot.

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

**Python test hashing 55-byte input**

```
async def test_ripemd160_on_55_length_input(self, cairo_run):
        msg_bytes = bytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmomnopnopq", "ascii")
        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()
```

