Elliptic Curve Digital Signature Algorithm tools
Elliptic curves are defined as
These curves are symmetric about the x-axis. A straight line can intersect the curve at a maximum of three points.
Domain parameters
-
$a$ and$b$ are the equation constants above -
$G$ is the generator point, a point on the curve above -
$p$ is the (prime) congruence modulo above, ie$lhs \bmod p = rhs \bmod p$ -
$n$ is the number of possible points on the curve, note that$n < p$
Note that
References
- https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
- https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm
The point at infinity is a special point that does not lie on the curve, resulting from adding two points whose sum is not on the curve. In addition it acts as an identity element, adding it to any point results in itself.
Adding a point and its negation results in the point at infinity. Negated points have the same x coordinate and negated y coordinate.
Adding (the x, y components of) one point
"Division" is via modular inverse.
Modular inverse: Find
Same as point addition but with
If
If
Given
- Let
$L$ be the bit length of$n$ - Let
$z$ be the leftmost$L$ bits of$hash(message)$ - Select a random integer
$k$ in the range$[1, n-1]$ - Calculate
$(x, y) = k * G$ - Calculte
$r = x \bmod n$ - If
$r = 0$ then choose a different$k$
- If
- Calculate
$s = (k^-1 * (z + r * privkey)) \bmod n = (modinv(k, n) * (z + r * privkey)) \bmod n$ - If
$s = 0$ then choose a different$k$
- If
- The signature is
$(r, s)$ - If
$r$ or$s$ is negative make it positive with$a = -a \bmod n$
- If
- Verify the
$pubkey \neq O$ (point at infinity) - Verify the
$pubkey$ lies on the curve - Verify
$n * pubkey = O$ - Verify
$r$ and$s$ are in the range$[1, n-1]$ - Let
$L$ be the bit length of$n$ - Let
$z$ be the leftmost$L$ bits of$hash(message)$ - Calculate
$u = (z * s^-1) \bmod n = (z * modinv(s, n)) \bmod n$ - Calculate
$v = (r * s^-1) \bmod n = (r * modinv(s, n)) \bmod n$ - Calculate
$(x, y) = uG + v * pubkey$ - If the point
$(x, y) = O$ then the signature is invalid
- If the point
- Verify
$r = x \bmod n$
A Bitcoin address is created by hashing a public key
References
- https://ethereum.github.io/yellowpaper/paper.pdf, Appendix F: Signing Transactions
- https://eklitzke.org/bitcoin-transaction-malleability
- Bitcoin and Ethereum use the same Elliptic curve (secp256k1)
A signature is invalid unless:
$0 < r < n$ -
$0 < s < (n >> 1) + 1$ - Restricted to the lower half to prevent transaction malleability
-
$v$ is zero or one (often shifted to 27 or 28)- The lower (higher) value represents an even (odd)
$y$
- The lower (higher) value represents an even (odd)
https://pkg.go.dev/github.com/jo-makar/ecdsa-tools
OpenSSL signature verification demo
OpenSSL signature (generation) demo
Bitcoin private key to address demo