MetaMask Sign Demo

๐Ÿง  How Ethereum Signatures Work

A signature is cryptographic proof that a message was approved by a specific wallet (Ethereum address) without revealing the private key.

โœ… Step-by-step:
  1. You type a message (e.g. "Login to MyApp")
  2. MetaMask signs it using your private key stored in the wallet
  3. A signature is generated (a long 132-character hex string)
  4. Verification is done using the message + signature to recover the signing address
๐Ÿ” What is actually signed?

Ethereum signs a special version of the message with a prefix to prevent misuse:

"\x19Ethereum Signed Message:\n" + message.length + message
  
๐Ÿงช What does a signature look like?

A typical Ethereum signature is 132 hex characters and contains 3 parts:

๐ŸŽฏ Why use Ethereum signatures?
๐Ÿ’ก Bonus (JavaScript examples)
// Signing
const signature = await ethereum.request({
  method: 'personal_sign',
  params: [message, userAddress],
});

// Verifying
const recoveredAddress = ethers.utils.verifyMessage(message, signature);

๐Ÿ“ The Math Behind Ethereum Signatures

Ethereum uses the Elliptic Curve Digital Signature Algorithm (ECDSA) over the secp256k1 curve to sign messages. The process involves creating a unique mathematical proof that only the holder of the private key can generate.

๐Ÿ”‘ Key Concepts:
โœ๏ธ Signature Generation:
  1. Generate a random number r
  2. Compute point (x, y) = r ร— G
  3. R = x mod n (R is part of the signature)
  4. Compute s = (z + R ร— k) / r mod n
  5. The final signature is the pair (R, s) plus recovery id v (27 or 28)
โœ… Verification (done without the private key):
s ร— r ร— G = z ร— G + R ร— K
  

This checks if the signature is consistent with the public key and the hashed message.

๐Ÿ’ก Ethereum specifics:

๐Ÿ”ค Explanation of Each Letter in Ethereum Signature Math

Symbol Meaning Details
k Private Key A secret 256-bit number known only to the wallet owner. Used to generate the public key and signature.
K Public Key Derived from k ร— G. Anyone can compute it if they know k. Publicly visible.
G Generator Point A fixed base point on the secp256k1 elliptic curve. Defined by the curveโ€™s standard.
r Random Nonce A random number generated during signing. Must be unique and secret for every signature.
(x, y) Point on Curve Result of multiplying r ร— G. Used to compute R = x mod n.
R First Part of Signature Equal to the x-coordinate of the point r ร— G, reduced modulo the curve order.
z Message Hash The keccak256 hash of the signed message (with Ethereum prefix).
s Second Part of Signature Computed as s = (z + R ร— k) / r mod n. Must be less than curve order / 2 for canonical form.
v Recovery ID A small number (27 or 28) that helps recover the public key from the signature and the message hash.
n Curve Order The total number of valid points on the curve. Used for modulo operations.

๐Ÿ“Š Known Numbers and Constants in Ethereum Signatures

Symbol Name Value Notes
p Prime Field (modulo) 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F The prime over which the curve is defined (secp256k1 field size)
n Curve Order 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 Total number of valid points (mod n used in sig math)
G Generator Point
x = 55066263022277343669578718895168534326250603453777594175500187360389116729240
y = 32670510020758816978083085130507043184471273380659243275938904335757337482424
Base point used in all key generation
k Private Key (example) 0x1c3a6c...f8a3b2 (256-bit random) Must be a number between 1 and nโˆ’1
K Public Key Uncompressed: 0x04 + x + y
Compressed: 0x02/03 + x
Derived from K = k ร— G
z Message Hash keccak256("\x19Ethereum Signed Message:\n" + len + msg) Used as input to signature equation
v Recovery ID 27 or 28 (sometimes 0 or 1) Helps recover public key from signature
r Signature Component 32-byte integer X-coordinate of r ร— G mod n
s Signature Component 32-byte integer Signature solution derived from private key