ECDSA Keys, Sign and Verify, Explained
After reading this you will understand how an elliptic-curve key pair works, what an ECDSA signature actually proves, why the per-signature nonce is the whole ballgame, and how P-256 gives you the same security as 3072-bit RSA in a fraction of the bytes.
What ECDSA is, in one example
ECDSA is the Elliptic Curve Digital Signature Algorithm, standardized in FIPS 186-5. It lets you prove that you hold a private key without revealing it. You sign a message with your private key, and anyone with your public key can verify that the signature matches both the message and the key.
Here is the shape of it. On the NIST P-256 curve, a private key is a single integer d in the range 1 to n-1, where n is a 256-bit prime. The public key is a point Q = d \cdot G, where G is a fixed base point on the curve. A signature is a pair of integers (r, s), each 256 bits. That is 64 bytes of signature and 32 bytes of private key. An RSA signature at the same security level is 384 bytes with a 3072-bit key.
The security rests on one hard problem: given Q and G, recovering d means solving the elliptic-curve discrete logarithm. For P-256 the best known attack costs roughly 2^{128} operations, which is out of reach.
When to use it, and when not
Reach for ECDSA when you need signatures and you care about size or speed. It dominates modern TLS certificate signing, SSH keys, JWT tokens (the ES256 algorithm is ECDSA over P-256), and blockchain transactions (Bitcoin and Ethereum use ECDSA over the secp256k1 curve, which this tool does not offer).
Do not use ECDSA to encrypt. It only signs. If you want to encrypt a small payload with a public key, that is RSA territory; see the RSA Encrypt and Decrypt tool. If you want key agreement rather than signatures, the elliptic-curve tool for that is ECDH, not ECDSA.
One more constraint: ECDSA requires a good random-number source at signing time. If you cannot guarantee that, EdDSA (Ed25519) is a deterministic alternative that removes the nonce risk entirely. When you control the platform and the Web Crypto API is available, ECDSA is a fine choice.
The three curves here differ only in size. P-256 targets 128-bit security, P-384 targets 192-bit, and P-521 targets roughly 256-bit. Pick P-256 unless a standard or policy requires more. It is the most widely supported and the fastest.
The math, with intuition
Signing hashes the message first, then does arithmetic modulo the curve order n. Let z be the leftmost bits of the message hash, truncated to the bit length of n. Pick a random secret nonce k in 1 to n-1. Then:
Here (k \cdot G)_x is the x-coordinate of the point you get by adding G to itself k times. Reduce it modulo n to get r. If r = 0, pick a new k.
Here k^{-1} is the modular inverse of the nonce, z is the truncated hash, r is the value above, and d is the private key. The signature is (r, s). Verification recomputes a point from (r, s), the hash, and the public key Q, then checks that its x-coordinate equals r:
where u_1 = z \cdot s^{-1} \bmod n and u_2 = r \cdot s^{-1} \bmod n. The check passes only if the signer used the private key d matching Q on that exact hash.
The nonce k must be unpredictable and never repeated. If you sign two different messages with the same k, both signatures share the same r, and an attacker can solve two linear equations for d. This broke the Sony PlayStation 3 code-signing key in 2010. The Web Crypto API generates k for you, so you cannot make this mistake here.
A worked example on P-256
The demo uses the tool defaults: curve P-256, hash SHA-256, and a short text message. The steps below trace what happens end to end.
Signing and verifying a short message
- Generate a key pair. The private key d is a random 256-bit integer, for example
0x519b...c3a1. The public key is Q = d \cdot G, a point with a 256-bit x and y, exported as a 65-byte uncompressed value starting with0x04. - Hash the message with SHA-256. A message of
"Hello, ECDSA!"produces a 32-byte digest. Truncate to 256 bits to get z. Sincenis 256 bits here, no bits are dropped. - Draw a fresh random nonce k. Compute r = (k \cdot G)_x \bmod n, a 256-bit number.
- Compute s = k^{-1}(z + r d) \bmod n. The pair (r, s) is your 64-byte signature.
- To verify, recompute u_1 and u_2, form the point u_1 G + u_2 Q, and confirm its x-coordinate mod
nequals r. It does, so the result isvalid.
Change one byte of the message and re-verify. The digest z changes completely, the recomputed x-coordinate no longer equals r, and verification returns invalid. That is the tamper-detection property doing its job.
Why the curve size matters
Elliptic-curve keys are small because the discrete-log problem on a well-chosen curve is much harder per bit than integer factoring. To match a 128-bit security level, RSA needs a 3072-bit modulus while EC needs only a 256-bit key. The gap widens as you climb.
| Security level (bits) | EC key (bits) | RSA key (bits) | Signature size (bytes) |
|---|---|---|---|
| 128 | 256 (P-256) | 3072 | 64 |
| 192 | 384 (P-384) | 7680 | 96 |
| 256 | 521 (P-521) | 15360 | 132 |
Reading and interpreting the result
Signing gives you a signature you can encode as raw (r, s) bytes or as ASN.1 DER. Web Crypto produces the raw form: a fixed 2 \times 32 = 64 byte string for P-256. DER wraps the same two integers in a variable-length structure, which is what OpenSSL and X.509 certificates use. If a verifier rejects a valid-looking signature, a format mismatch (raw versus DER) is the first thing to check.
Verification returns a single boolean. valid means three things held at once: the signature was made with the private key matching this public key, the message is byte-for-byte what was signed, and the same hash function was used. invalid tells you one of those failed but not which one. It does not tell you who signed, only that the holder of d did.
Common mistakes
Four errors account for most ECDSA failures in practice.
- Reusing or leaking the nonce
- Covered above. Any bias in k, even a few predictable bits, can leak d over many signatures via lattice attacks. Use a vetted library.
- Signing the message instead of its hash
- ECDSA signs a fixed-size hash. Passing raw multi-megabyte data where a 32-byte digest is expected either fails or silently truncates. Match the hash to the curve: SHA-256 with P-256, SHA-384 with P-384.
- Mixing signature encodings
- Raw (r, s) and DER are not interchangeable. Convert explicitly.
- Signature malleability
- Both (r, s) and (r, n - s) verify as valid. If you use a signature as a unique identifier, normalize to the low-s form. This bit Bitcoin early on.
Related tools
If your protocol mandates RSA, generate keys with the RSA Key Pair Generator and sign with the RSA Sign and Verify tool, which uses RSA-PSS. The choice between ECDSA and RSA signatures is mostly about size and ecosystem support; both are secure when used correctly.
Frequently asked questions
Is ECDSA more secure than RSA?
At matching key sizes ECDSA is stronger, but both are considered secure at their recommended sizes. P-256 (128-bit security) and RSA-3072 offer the same protection. ECDSA wins on key and signature size and on signing speed.
Which curve should I pick?
P-256 unless a standard says otherwise. It powers most TLS handshakes and JWT ES256. Use P-384 or P-521 only when a compliance rule demands 192-bit or 256-bit security.
Can I encrypt with an ECDSA key?
No. ECDSA only signs and verifies. For public-key encryption use RSA, and for key agreement use ECDH.
Why is my valid signature rejected by another library?
Almost always an encoding mismatch. Web Crypto emits raw 64-byte (r, s); OpenSSL expects ASN.1 DER. Convert between the two before verifying.
Does the nonce need to stay secret forever?
Yes. Recovering even one nonce recovers the private key through d = (s k - z) r^{-1} \bmod n. Treat every k as sensitive as the key itself.