Skip to content
Back to Curriculum
SECURITY

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

Example.sol
Loading...

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.

Practical.sol
Loading...

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.

STATICCALLADDR 0x01

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

Gas-less transactions (Meta-transactions).
NFT Allowlist (Merkle Proofs vs Signatures).
Permit (ERC20 Approve via signature).

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate