ECDSA
Elliptic Curve Digital Signature Algorithm - thuật toán chữ ký số đường cong elliptic
A Brief Introduction to ECDSA
It is impossible to overstate how modern cryptography redefines trust for all our digital interactions - from securing bank account logins with encryption to verifying the authenticity of your favorite apps through digital certificates.
Public key cryptography is a key concept empowering these interactions. It consists of two key pairs:
Public key: Widely distributed and used by anyone to verify an entity's identity. Private key: Confidential and known only to the owner, used for encryption and signing messages.
Elliptic curve cryptography (ECC) is a specific type of public key cryptography that uses mathematics of elliptic curves to create smaller, and more efficient keys. This is especially beneficial in resource-constrained environments like Ethereum. Within Ethereum, the Elliptic Curve Digital Signature Algorithm (ECDSA) helps verify the legitimacy of submitted transactions.
Let's consider a real-world scenario to understand how ECDSA works in action.
Alice, a diligent businesswoman, has been abducted and held captive on a remote island. Her captors demand a hefty ransom of $1 million for her release. With limited options for communication, they provide a single postcard for her to instruct her associate, Bob, to transfer the funds.
Alice considers writing the ransom amount and signing the postcard like a check. However, this method poses a significant risk: the kidnappers could easily forge the postcard, inflate the amount, and deceive Bob into sending them more money.
Alice needs a robust approach that allows:
- Bob to verify that the transfer has be authorized by her, and
- ensure that postcard's message has not been tampered with.
The goal of this exercise is device a method for Alice to create a secret key 🔑 known only to her. This key will be crucial for her to prove her identity and ensure the message's authenticity to Bob.
Mathematics, as always, comes to the rescue. Through ingenious use of Elliptic Curves, let's explore how Alice can generate the secret key 🔑.
Elliptic curves
An elliptic curve is a curve described by the equation:
Such that to ensure the curve is non-singular. The equation above is what is called the Weierstrass normal form of the long equation:
Examples:
Observe that elliptic curves are symmetric about the x-axis.
Ethereum uses a standard curve known as secp256k1 with parameters , and ; which is the curve:
Groups and Fields
Group
In mathematics, a GROUP is a set , containing at least two elements, which is closed under a binary operation usually referred to as addition (). A set is closed under an operation when the result of the operation is also a member of the set.
The set of real numbers is a familiar example of a group, since arithmetic addition of two real numbers is closed.
Field
Similarly, a FIELD is a set , containing at least two elements, which is closed under two binary operations usually referred to as addition (), and multiplication().
In other words, A FIELD is a GROUP under both addition and multiplication.
Elliptic curves are interesting because the points on the curve form a group, i.e the result of "addition" of two points remains on the curve. This geometric addition, distinct from arithmetic counterparts, involves drawing a line through chosen points (P and Q) and reflecting the resulting curve intersection(R') across the x-axis to yield their sum (R).
A point (P) can also be added to itself (), in which case the straight line becomes a tangent to P that reflects the sum (2P).
Repeated point-addition is known as scalar multiplication:
Discrete logarithm problem
Let's leverage scalar multiplication to generate the secret key 🔑. This key, denoted by , represents the number of times a base point is added to itself, yielding the resulting public point :
Given and it is possible derive the secret key by effectively reversing the multiplication, similar to the logarithm problem.
We need to ensure that scalar multiplication does not leak our secret key 🔑. In other words, scalar multiplication should be "easy" one way and "untraceable" the other way around.
The analogy of a clock helps illustrate the desired one-way nature. Imagine a task starting at 12 noon and ending at 3. Knowing only the final time (3) makes it impossible to determine the exact duration without additional information. This is because modular arithmetic introduces a "wrap-around" effect. The task could have taken 3 hours, 15 hours, or even 27 hours, all resulting in the same final time modulo 12.
Over a prime modulus, this is especially hard and is known as discrete logarithm problem.
Elliptic curves over finite field
So far, we have implicitly assumed elliptic curves over the rational field (). Ensuring secret key 🔑 security through the discrete logarithm problem requires a transition to elliptic curves over finite fields defined by a prime modulus. This essentially restricts the points on the curve to a finite set by performing modular reduction with a specific prime number.
For the sake of this discussion, we will consider the secp256k1 curve defined over an arbitrary finite field with prime modulus 997:
While the geometric representation of the curve in the finite field may appear abstract compared to a continuous curve, its symmetry remains intact. Additionally, scalar multiplication remains closed, although the "tangent" now "wraps around" given the modulus nature.
Generating key pair
Alice can finally generate a key pair using elliptic curve over finite field.
Let's define the elliptic curve over the finite field of prime modulus 997 in Sage.
sage: E = EllipticCurve(GF(997),[0,7])
Elliptic Curve defined by y^2 = x^3 + 7 over Finite Field of size 997Define the generator point by selecting an arbitrary point on the curve.
sage: G = E.random_point()
(174 : 487 : 1)Scalar multiplication over an elliptic curve defines a cyclic subgroup of order . This means that repeatedly adding any point in the subgroup times results in the point at infinity (), which acts as the identity element.
sage: n = E.order()
1057
# Illustrating that n*G (or any point) equals O, represented by (0 : 1 : 0).
sage: n*G
(0 : 1 : 0)A key pair consists of:
- Secret key 🔑(): A random integer chosen from the order of the subgroup . Ensures only Alice can produce valid signatures.
Alice randomly chooses 42 as the secret key 🔑.
sage: K = 42- Public key (): A point on the curve, the result of scalar multiplication of secret key 🔑() and generator point (). Allows anyone to verify Alice's signature.
sage: P = K*G
(858 : 832 : 1)We have established that Alice's key pair .
ECDSA in action
ECDSA is a variant of the Digital Signature Algorithm (DSA). It creates a signature based on a "fingerprint" of the message using a cryptographic hash.
For ECDSA to work, Alice and Bob must establish a common set of domain parameters. Domain parameters for this example are:
| Parameter | Value |
|---|---|
| The elliptic curve equation. | |
| The prime modulo of the finite field. | 997 |
| The generator point, . | (174, 487) |
| The order of the subgroup, . | 1057 |
Importantly, Bob is confident that the public key actually belongs to Alice.
Signing
Alice intends to sign the message "Send $1 million", by following the steps:
- Compute the cryptographic hash .
sage: m = hash("Send $1 million")
-7930066429007744594- For every signature, a random ephemeral key pair [, ] is generated to mitigate an attack exposing her secret key 🔑.
# Randomly selected ephemeral secret key.
sage: eK = 10
# Ephemeral public key.
sage: eP = eK*G
(215 : 295 : 1)Ephemeral key pair .
- Compute signature component :
Where is the x-coordinate of the ephemeral public key (eP), i.e 215. Notice the signature uses both Alice's secret key 🔑 () and the ephemeral key pair [, ].
# x-coordinate of the ephemeral public key.
sage: r = int(eP[0])
215
# Signature component, s.
sage: s = mod(eK**-1 * (m + r*K), n)
160The tuple is the signature pair.
Alice then writes the message and signature to the postcard.
Verification
Bob verifies the signature by independently calculating the exact same ephemeral public key from the signature pair , message, and Alice's public key :
- Compute the cryptographic hash .
sage: m = hash("Send $1 million")
-7930066429007744594- Compute the ephemeral public key , and compare it with :
sage: R = int(mod(m*s^-1,n)) * G + int(mod(r*s^-1,n)) * P
(215 : 295 : 1)
# Compare the x-coordinate of the ephemeral public key.
sage: R[0] == r
True # Signature is valid ✅If Alice's captors were to modify the message, it would alter the cryptographic hash, leading to verification failure due to the mismatch with the original signature.
sage: m = hash("Send $5 million")
7183426991750327432 # Hash is different!
sage: R = int(mod(m*s^-1,n)) * G + int(mod(r*s^-1,n)) * P
(892 : 284 : 1)
# Compare the x-coordinate of the ephemeral public key.
sage: R[0] == r
False # Signature is invalid ❌Verification of the signature assures Bob of the message's authenticity, enabling him to transfer the funds and rescue Alice. Elliptic curves saves the day!
Wrapping up
Just like Alice, every account on the Ethereum uses ECDSA to sign transactions. However, ECC in Ethereum involves additional security considerations. While the core principles remain the same, we use secure hash functions like keccak256 and much larger prime field, boasting 78 digits: .
This discussion is a preliminary treatment of Elliptic Curve Cryptography. For a nuanced understanding, consider the resources below.
And finally: never roll your own crypto! Use trusted libraries and protocols to protect your data and transactions.
ℹ️ Note
ECDSA faces potential obsolescence from quantum computers – learn about how Post-Quantum Cryptography tackles this challenge.
Further reading
Elliptic curve cryptography
- 📝 Standards for Efficient Cryptography Group (SECG), "SEC 1: Elliptic Curve Cryptography."
- 📝 Standards for Efficient Cryptography Group (SECG), "SEC 2: Recommended Elliptic Curve Domain Parameters."
- 📘 Alfred J. Menezes, Paul C. van Oorschot and Scott A. Vanstone, Handbook of Applied Cryptography
- 🎥 Fullstack Academy, "Understanding ECC through the Diffie-Hellman Key Exchange."
- 📝 Andrea Corbellini, "Elliptic Curve Cryptography: a gentle introduction."
- 📝 William A. Stein, "Elliptic Curves."
- 📝 Khan Academy, "Modular Arithmetic."
- 🎥 Khan Academy, "The discrete logarithm problem."
Mathematics of Elliptic Curves
- 📘 Joseph H. Silverman, "The Arithmetic of Elliptic Curves."
- 📝 Joseph H. Silverman, "An Introduction to the Theory of Elliptic Curves."
- 📘 Neal Koblitz, "A Course in Number Theory and Cryptography."
- 📝 Ben Lynn, "Stanford Crypto: Elliptic Curves."
- 📝 Rareskills.io, "Elliptic Curve Point Addition."
- 📝 John D. Cook, "Finite fields."
Useful tools
- 🎥 Tommy Occhipinti, "Elliptic curves in Sage."
- 🎥 Desmos, "Introduction to the Desmos Graphing Calculator."
- 🧮 Andrea Corbellini, "Interactive Elliptic Curve addition and multiplication."
Credits
- Thanks to Michael Driscoll for his work on animated elliptic curves.