Signature Verification
Digital signatures allow users to approve transactions or data off-chain using their private key. The smart contract can then verify 'on-chain' that a specific user signed the message, without the user spending gas to send the data.
Syntax Structure
Key Components
- Boilerplate structure required by the Solc compiler.
- Placement of state variables versus functions.
Practical Implementation
Verifying a message was signed by an authorised signer.
Core Concepts
ECDSA
Elliptic Curve Digital Signature Algorithm. The math behind Ethereum keys.
ecrecover
The EVM precompile that takes a hash + signature and spits out an address.
Replay Attack
Using the same valid signature twice. Prevent using nonces.
Mental Model
The Wax Seal
A digital signature is like a King's ring pressed into wax. The messenger (relayer) carries the letter. The guard (contract) looks at the seal. If it matches the King's ring, the order is executed, even if the King isn't there in person.
Under the Hood
`ecrecover` is a **Precompile** at address 0x01. It costs 3000 gas. It performs complex elliptic curve math that would be too expensive in raw Solidity opcodes.
Gas Analysis
Cheaper than on-chain logic for complex validations.
- • Heavy logic off-chain + light verification on-chain = Low Gas.
- • Use EIP-712 for structured data (more readable for users but slightly more gas).
Security Risks
Signature Malleability is a common issue.
- ! Signature Replay: Always include a `nonce` or verify the hash hasn't been used before.
- ! Cross-Chain Replay: Include `chainid` in the hash so a signature for Mainnet doesn't work on Polygon.
Common Use Cases
Interactive Visualization
See exactly how the EVM handles this code in memory, storage, and the stack.
Click Run to execute the simulation
Then use the step controls to navigate