Some time ago I did a post about
creating a private key by flipping a coin. Recently, I stumbled upon
this video related to Elliptic Cryptography, in which the author goes through a simple python code [1] for
generating Bitcoin public key from a known private key.
Among the other interesting details, the author mentions that
NOT every 256-bit private key is acceptable as an input for his script, which made me thinking about my previous (coin flipping) post.
(Note: I recommend watching this video even if you don't know anything about python programming. You may learn a lot about how the math of Elliptic Cryptography works, which is what protects Bitcoin in the first place.)At first, this looked strange, but then I found
this post which confirmed that the number of possible private keys is less than 2
256.
So it turns out that this is a fine detail related to
Elliptic Curve math, so when you flip a coin, a very small subset of private keys won't fit into the Bitcoin's derivation scheme.
The probability of getting one of these unacceptable private keys by flipping a coin is
so minute, that it's not worth considering. Still, one should know about it.
Let's see how come this is even possible.
Some Elliptic Curve ExamplesBitcoin uses the following elliptic curve formula [2]:
y
2 = x
3 + 7
The curve looks like shown below (if you plot it over real numbers):
(plotted on https://www.desmos.com/calculator)Bit in Bitcoin, we don't use real numbers for x. In Bitcoin, we have to restrict x (
x is on the horizontal axis) to a discrete set (field) of positive integers.
Because of this discrete nature of x, the curve above is no longer a curve but a set of points. Each point has an x and y coordinate, like this (
please note that this is only an illustration on a much smaller field, the image is taken from reference [3]):
The number of points, albeit very very large, is finite. There are several very important points, and one of them is called
generator point or
base point:
G=(G
x, G
y)
Just like an illustration as to the exact position of G, here are the coordinates of the generator point:
G
x= 55066263022277343669578718895168534326250603453777594175500187360389116729240
G
y= 32670510020758816978083085130507043184471273380659243275938904335757337482424
Now, if you have a private key (Priv), the way you obtain your public key (Pub) by multiplying the generator point with your private key Priv:
Pub = Priv * G,
or equivalently, we can replace the operation of multiplication with addition:
Pub = G + G + G + ...... + G
(Priv times)
We won't go into details here, but when you add G to itself (G+G), you end up on another point in the graph above. And then you continue adding another G. In a nutshell, your private key is a very large number, while you public key is a point on this graph where you end up after a huge number of additions (starting from the generator point).
It turns out that the maximum number of additions of G to itself is predetermined by the field itself and is expressed by another properties of this field called
order N of G [4]:
N is very large:
N = 115792089237316195423570985008687907852837564279074904382605163141518161494336
If you add G to itself N+1 times, you will leave (break) the graph, so
the private key has to be a number between 1 and N.
Back to coin flipping.When you flip a coin there are exactly 2
256 possibilities, and 2
256 distinct private keys can be generated.
How large this number is? It is somewhat larger than the order N of G:
the number of possible private keys generated by coin flipping = 115792089237316195423570985008687907853269984665640564039457584007913129639936
So out of all possible combinations that produce a private key, about this many are not acceptable:
432420386565659656852420866394968145600
It may look like a huge number, but it's not. Not, given the size of the entire space (2
256).
NOT that this will ever be the case, but if you use coin flipping for your private key and get a number with a lot of leading 1s, like this:
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111010111010101011101101110011100110101011110100100010100000001110111011111111010010010111101000110011010000001101100100000101000001
it won't be a valid private key. (Fewer leading 1s in your private key are fine, but more 1s won't be acceptable as well).
References:[1]
https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart4-PrivateKeyToPublicKey.py[2]
https://en.bitcoin.it/wiki/Secp256k1[3]
https://bitcoin.stackexchange.com/questions/21907/what-does-the-curve-used-in-bitcoin-secp256k1-look-like[4]
https://www.coindesk.com/math-behind-bitcoin/
Edit: @achow101, in the discussion below, helped me realize that the usual notation for
N is
n. Mind this if you consult literature. Moreover, all private keys are modulo
n (akka N above). This means that software producers can and should take this fact into account. If the private key is larger than
n (akka N above), they can compute the following simple operation: "private key modulo
n (akka N above)", and get a valid private key. If you flip a coin and it happens that you get one of these large private keys, you can do the same by hand.