Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
LABS
Glossary

ecrecover

ecrecover is a low-level Ethereum Virtual Machine (EVM) function that recovers the signer's address from a cryptographic signature and the original message hash.
Chainscore © 2026
definition
ETHEREUM CRYPTOGRAPHIC FUNCTION

What is ecrecover?

ecrecover is a low-level, built-in cryptographic function in the Ethereum Virtual Machine (EVM) used to verify the authenticity of a digital signature and recover the signer's public address from a signed message hash.

The ecrecover function is a core precompiled contract (at address 0x1) that performs Elliptic Curve Digital Signature Algorithm (ECDSA) recovery on the secp256k1 curve. It takes four inputs: the 32-byte message hash (e.g., keccak256 hash), the recovery identifier v, and the signature components r and s. Its output is the 20-byte Ethereum address of the account that originally signed the message. This function is fundamental for on-chain signature verification, enabling smart contracts to authenticate actions like token approvals, meta-transactions, and decentralized identity proofs without relying on external oracles.

A critical nuance is that ecrecover does not verify the signature itself; it merely performs mathematical recovery. The calling contract must explicitly check that the returned address is non-zero and matches an expected signer. Furthermore, developers must ensure the provided hash is the exact data the user intended to sign, often a structured EIP-712 hash or a prefixed message hash ("\x19Ethereum Signed Message:\n" + len(message) + message) to prevent replay attacks across different contexts. Mismanagement of these inputs is a common source of security vulnerabilities.

In practice, ecrecover is invoked within Solidity using the global ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) function. For example, a multisig wallet contract uses it to validate that a transaction proposal was signed by a sufficient number of authorized signers. While ecrecover is gas-efficient for single verifications, newer abstractions like EIP-1271 for contract signatures and account abstraction (EIP-4337) offer more flexible verification schemes, though they often rely on ecrecover as a foundational primitive.

etymology
TERM ROOTS

Etymology and Origin

The term `ecrecover` is a portmanteau and function name that originates from the Ethereum Virtual Machine (EVM) and its underlying cryptographic primitives. Its etymology reveals its core purpose: to recover a signer's address from a digital signature.

The name ecrecover is a contraction of Elliptic Curve Recover. It is a precompiled contract—a highly optimized, native function—within the Ethereum Virtual Machine. Its sole purpose is to execute the ECDSA public key recovery operation, which is the process of deriving the public key (and thus the Ethereum address) from a signed message hash and its corresponding signature. This function is fundamental to verifying the authenticity of transactions and signed messages on-chain.

The Elliptic Curve Digital Signature Algorithm (ECDSA) is the cryptographic standard used by Ethereum, specifically the secp256k1 curve. In a typical signature verification, you have the public key and verify it matches the signature. ECDSA recovery is the inverse: given the signature and the original message hash, the algorithm can compute the public key. The ecrecover function encapsulates this complex mathematical operation, providing a gas-efficient way for smart contracts to perform identity verification without implementing the cryptography themselves.

The function's signature is ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address). The inputs are the components of an ECDSA signature: the hash of the signed data, and the v, r, and s values that constitute the signature itself. A critical security note is that ecrecover returns address(0) on failure, which smart contracts must explicitly check. Its origin is deeply tied to Ethereum's need for native cryptographic primitives that enable trustless verification, forming the bedrock for systems like multi-signature wallets and permissioning schemes.

how-it-works
CRYPTOGRAPHIC PRIMITIVE

How ecrecover Works

An in-depth look at the EVM's built-in function for recovering an Ethereum address from a digital signature and the original message hash.

The ecrecover function is a precompiled contract within the Ethereum Virtual Machine (EVM) that performs Elliptic Curve Digital Signature Algorithm (ECDSA) signature recovery. Given a 65-byte signature (comprising v, r, and s values) and the original 32-byte message hash (messageHash), it computes and returns the 20-byte Ethereum address of the signer. This process is fundamental for verifying that a specific account authorized a transaction or message, as the recovered address must match the expected signer.

The function operates on the secp256k1 elliptic curve used by Ethereum. It takes the signature components—r and s (the signature itself) and v (the recovery identifier, 27 or 28)—and mathematically derives the public key from them using the signed hash. This public key is then hashed with Keccak-256, and the last 20 bytes of that hash become the final Ethereum address. A critical prerequisite is that the messageHash input must be the exact Keccak-256 hash of the original data, often requiring senders to follow the EIP-191 or EIP-712 structured signing standards to prevent replay attacks.

Developers most commonly interact with ecrecover in Solidity via the global ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) function. A typical verification pattern involves hashing the message with keccak256, calling ecrecover, and comparing the returned address to a known signer. It is essential to validate the s value is within the lower half of the curve's order to prevent signature malleability, a check often enforced by OpenZeppelin's ECDSA library which provides a safer wrapper for this low-level operation.

While ecrecover is a core verification tool, its direct use has pitfalls. The caller is responsible for all pre-hashing logic and security checks. Furthermore, it only returns an address; for broader signature schemes, the newer EIP-1271 standard for contract signatures is used, allowing smart contracts to validate signatures in a more flexible way. Understanding ecrecover is key to building secure systems for meta-transactions, multi-signature wallets, and any application requiring off-chain authorization.

code-example
ECRECOVER

Code Example

A practical demonstration of the `ecrecover` precompiled contract in Ethereum, used to verify the signer of a cryptographic signature.

The following Solidity code snippet demonstrates a common pattern for signature verification using ecrecover. The function verifySignature takes a messageHash (typically a keccak256 hash of structured data), a v, r, s signature tuple, and the expected signer address. It uses the ecrecover precompile to derive the signing address from the hash and signature, then compares it to the provided signer address, returning true if they match. This is the foundational mechanism for meta-transactions, permit functions (like ERC-2612), and off-chain authorization.

Key components of the example include: the use of keccak256 to create a Ethereum Signed Message hash, the proper splitting of the signature bytes into v, r, and s components, and the critical check that recoveredSigner is not the zero address, which indicates an invalid signature. The ecrecover function is called as a low-level staticcall, and its return value is the 20-byte Ethereum address of the entity that signed the original message. It is essential that the exact same messageHash that was signed is provided for verification.

In practice, developers must be aware of signature malleability and replay attack vectors. To prevent malleability, ensure the s value is in the lower half of the secp256k1 curve's order (checking s <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0). To prevent cross-context replay, the hashed message should include the contract's address (address(this)) and a nonce. The upcoming EIP-7212 aims to standardize and optimize secp256r1 signature verification, which may offer a more gas-efficient and standardized alternative for certain use cases compared to the current ecrecover pattern.

key-features
ECDSA RECOVERY FUNCTION

Key Features and Characteristics

ecrecover is a cryptographic precompiled contract in the Ethereum Virtual Machine (EVM) that recovers the signer's address from a digital signature and the original message hash.

01

Cryptographic Foundation

ecrecover implements the public key recovery mechanism of the Elliptic Curve Digital Signature Algorithm (ECDSA) using the secp256k1 curve. Given a signature (composed of r, s, and v components) and the original message's keccak256 hash, it mathematically derives the corresponding public key and returns its 20-byte Ethereum address.

02

Input Parameters

The function requires four precise 32-byte inputs:

  • hash: The 32-byte Keccak-256 hash of the signed message.
  • v: The recovery identifier (27 or 28 for legacy, 0/1 for EIP-155).
  • r: The first 32 bytes of the ECDSA signature.
  • s: The second 32 bytes of the ECDSA signature. It returns the 20-byte address of the signer, or 0 on failure.
03

Core Use Case: Signature Verification

Its primary use is to verify that a message (e.g., a transaction, a login request) was authorized by the holder of a specific private key. This enables:

  • Meta-transactions and gasless interactions via EIP-712 signed typed data.
  • Off-chain authorization for on-chain actions.
  • Verifying the sender in smart contract logic, as seen in multi-signature wallets and permissioned functions.
04

Security Critical: Hash Malleability

A major security pitfall is failing to verify the message hash itself. ecrecover only validates the signature against the hash you provide. Attackers can exploit this by getting a signature on a harmless hash, then presenting it with a malicious hash that resolves to the same address. Always hash the message with keccak256 inside the contract or use ecrecover with EIP-712 structured hashing.

05

Gas Cost & EVM Precompile

ecrecover is implemented as a precompiled contract at address 0x01. Calling it consumes a fixed 3,000 gas, which is significantly cheaper than performing the same elliptic curve operations in a standard smart contract. This efficiency makes it practical for on-chain signature verification.

ecosystem-usage
ECRECOVER

Ecosystem Usage and Applications

The ecrecover precompile is a cryptographic utility function used to verify the authenticity of digital signatures, primarily for validating transaction and message signers on-chain.

04

Cryptographic Primitives & Risks

ecrecover is a low-level building block for elliptic curve cryptography on the secp256k1 curve. Developers must handle it carefully to avoid critical vulnerabilities:

  • Signature Malleability: Historically, certain v values could lead to two valid signatures for the same digest. Most libraries now enforce canonical signatures.
  • Frontrunning Risks: A signed message intended for one contract could be replayed in another if the domain separator (EIP-712) is not used.
  • Gas Cost: As a precompiled contract, calling ecrecover costs ~3,000 gas, which is cheap compared to implementing the math in Solidity.
security-considerations
ECRECOVER

Security Considerations and Risks

The ecrecover precompile is a critical but often misused function for signature verification on Ethereum. Its improper use can lead to severe security vulnerabilities.

01

Signature Malleability

The ecrecover function can return two different, valid Ethereum addresses for a single signature due to ECDSA signature malleability. This occurs because the v value (recovery identifier) can be 27 or 28, and the s value can be its secp256k1 curve order complement. Failing to check both possibilities can allow attackers to bypass signature checks.

  • Example: A contract storing (v, r, s) to prevent replay could be bypassed if an attacker submits the malleable form.
  • Mitigation: Use OpenZeppelin's ECDSA library, which handles canonical signature checking.
02

Lack of Built-in Replay Protection

ecrecover only validates that a signature corresponds to a signer's address for a given message hash. It provides no inherent protection against signature replay across different contexts, chains, or contract functions.

  • Risk: A signature authorizing one action could be reused (replayed) to authorize a different, unintended action.
  • Mitigation: Always include nonces, contract addresses (address(this)), and chain IDs in the signed message hash (EIP-712 is the standard for structured data).
03

The "Delegated Transfer" Vulnerability

A classic attack vector involves contracts that use ecrecover to authorize token transfers without verifying the signed message's intent. An attacker can trick a user into signing a benign message (e.g., "Hello World") and then submit that signature to a vulnerable contract that interprets the hash of that message as authorization for a transfer.

  • Root Cause: The contract does not enforce a specific, unambiguous message format.
  • Solution: Use EIP-712 typed structured data to define the exact schema of the signable message, making user intent clear.
04

Front-Running with Malleable `s`

Even if a contract checks for canonical s values, transactions can be front-run before they are mined. A malicious actor can observe a pending transaction with signature (v, r, s), compute its malleable counterpart (v', r, s'), and broadcast a duplicate transaction with a higher gas price. If the contract only checks for spent nonces after ecrecover, both transactions might appear valid, leading to double-spends.

  • Mitigation: Implement strict nonce checking and state commitment before the signature verification step.
05

Edge Cases and Invalid Inputs

ecrecover returns the zero address (address(0)) for invalid signatures. A common critical bug is failing to explicitly check for this return value, allowing an "empty" signature to be treated as valid.

  • Other Edge Cases:
    • Signatures where s > secp256k1n/2 (non-canonical).
    • Malformed v values (not 27 or 28).
    • r or s equal to zero.
  • Best Practice: Always wrap ecrecover in a library like OpenZeppelin's ECDSA.recover that performs these safety checks.
06

Gas Cost and Reentrancy Context

While ecrecover is a precompile, it still consumes gas. Calling it within a loop or in a function that itself is called via a low-level call can lead to unexpected out-of-gas errors. Furthermore, because it is a pure cryptographic operation, it is generally safe from reentrancy. However, the logic that uses the recovered address must be protected if it makes external calls or updates state, to prevent business logic reentrancy attacks after the signature is verified.

ECRECOVER

Common Misconceptions

The `ecrecover` precompile is a fundamental cryptographic primitive in Ethereum, but its proper use is often misunderstood. This section clarifies frequent points of confusion regarding signature malleability, address handling, and security best practices.

The ecrecover precompile is cryptographically secure for recovering an address from a signature, but it is a low-level function that requires careful handling to be used securely. Developers must explicitly check for several edge cases that ecrecover does not validate:

  • Signature Malleability: The function does not reject high-s signatures (where s > secp256k1n/2), which can lead to signature malleability issues. The OpenZeppelin ECDSA library enforces a low-s requirement to prevent this.
  • Invalid Signatures: ecrecover returns address(0) for invalid signatures, but this is not a unique sentinel value. A zero address could theoretically be a valid signer. You must also check that the recovered address is not zero.
  • Signed Message Context: ecrecover only recovers the signer; it does not know what was signed. You must ensure the signed message hash (e.g., an EIP-712 typed hash or an eth_sign message) matches the expected application context to prevent replay attacks.

Best Practice: Use audited, high-level libraries like OpenZeppelin's ECDSA.recover or ECDSA.tryRecover which bundle these essential checks.

ECRECOVER

Technical Deep Dive

A deep dive into the `ecrecover` precompiled contract, the cryptographic function used to verify Ethereum signatures and recover signer addresses.

ecrecover is an Ethereum precompiled contract that executes the Elliptic Curve Digital Signature Algorithm (ECDSA) recovery function, allowing anyone to verify a cryptographic signature and derive the Ethereum address of the signer from a signed message hash. It takes four inputs: the 32-byte message hash, the recovery identifier v, and the signature components r and s. The function performs the mathematical operation to compute the public key from the signature and then derives the 20-byte Ethereum address from that key. This mechanism is fundamental for proving ownership and authorization in transactions and smart contract interactions without revealing the private key.

ECRECOVER

Frequently Asked Questions (FAQ)

Common questions about the `ecrecover` precompiled contract, a fundamental cryptographic function for verifying Ethereum signatures.

ecrecover is a precompiled contract in the Ethereum Virtual Machine (EVM) that recovers the signer's address from a cryptographic signature. It takes the Keccak-256 hash of a message and an ECDSA signature (composed of v, r, and s values) as inputs, performs elliptic curve mathematics, and returns the 20-byte Ethereum address that signed the message. This allows smart contracts to verify that a specific entity authorized an action without needing to store or transmit their private key. The process is the computational reverse of signing: while signing uses a private key to generate a signature from a hash, ecrecover uses the signature and hash to derive the public address.

further-reading
DEEP DIVE

Further Reading

Explore the cryptographic primitives, security considerations, and practical applications of the ecrecover function.

02

Signature Malleability & Replay Attacks

Using ecrecover directly requires careful handling of edge cases:

  • Malleable Signatures: The s value can be transformed (using s' = n - s, where n is the curve order) to create a different, valid signature for the same message. Contracts must check the s value is in the lower half.
  • Replay Protection: A signature is not bound to a specific contract or chain. Implement nonces or include the contract address in the signed message hash to prevent replay across contexts.
05

Gas Cost & Precompiled Contract

ecrecover is implemented as a precompiled contract at address 0x1. Calling it consumes a fixed 3,000 gas, making it a relatively inexpensive cryptographic operation on-chain. This efficiency enables:

  • On-chain signature verification for permissions (e.g., multi-sig, meta-transactions).
  • Gas-efficient proof-of-ownership checks.
  • Building decentralized exchanges and other applications requiring trustless verification of off-chain messages.
06

Use Case: Meta-Transactions & Gasless UX

ecrecover is fundamental to meta-transactions and gas abstraction. A typical flow:

  1. A user signs a transaction request off-chain.
  2. A relayer submits the transaction and signature to a contract.
  3. The contract uses ecrecover to validate the user's signature.
  4. If valid, the contract executes the requested action, paying gas from its own balance. This pattern, used by systems like Gas Station Network (GSN), allows users to interact with dApps without holding the native token for gas.
ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team
What is ecrecover? Ethereum Signature Recovery Function | ChainScore Glossary