mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-22 16:18:11 +00:00
chapter 4 re-flowed, new diagrams, re-written introduction
This commit is contained in:
parent
5f75f408a2
commit
74b8c0c313
192
ch04.asciidoc
192
ch04.asciidoc
@ -29,17 +29,6 @@ In most implementations, the private and public keys are stored together as a _k
|
||||
((("elliptic curve cryptography", "ECC")))
|
||||
Elliptic Curve Cryptography is a type of asymmetric or public-key cryptography based on the discrete logarithm problem as expressed by addition and multiplication on the points of an elliptic curve.
|
||||
|
||||
Starting with a private key in the form of a randomly generated number +k+, we multiply it with a predetermined point on the curve called the _generator point_ +G+ to produce another point somewhere else on the curve, which is the corresponding public key +K+.
|
||||
|
||||
[latexmath]
|
||||
++++
|
||||
\begin{equation}
|
||||
{K = k * G}
|
||||
\end{equation}
|
||||
++++
|
||||
|
||||
[[key_derivation]]
|
||||
where +k+ is the private key, +G+ is a fixed point on the curve called the _generator point_, ((("generator point"))) and +K+ is the resulting public key, another point on the curve.
|
||||
|
||||
|
||||
[[ecc_addition]]
|
||||
@ -104,6 +93,78 @@ Below is a randomly generated private key shown in hexadecimal format (256 binar
|
||||
1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD
|
||||
----
|
||||
|
||||
To generate a new key with bitcoind, use the +getnewaddress+ command. For security reasons it displays the public key only, not the private key. To ask bitcoind to expose the private key, use the +dumpprivkey+ command. Here's an example of both commands:
|
||||
|
||||
----
|
||||
$ bitcoind getnewaddress
|
||||
1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy
|
||||
$ bitcoind dumpprivkey 1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy
|
||||
KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ
|
||||
----
|
||||
|
||||
The +dumpprivkey+ command is opening the wallet and extracting the private key that was generated by the +getnewaddress+ command. It is not otherwise possible for bitcoind to know the private key from the public key, unless they are both stored in the wallet. In the example above, we see that the private key has a "K" prefix, indicating it is encoded as a WIF-compressed format. This means that the key-pair is stored in the wallet with both keys compressed, saving 31 bytes of space. If the prefix had been "5", indicating the WIF format, we would know that the key-pair is uncompressed.
|
||||
|
||||
You can also use +sx tools+ to generate keys and convert them between formats:
|
||||
|
||||
===== New key
|
||||
----
|
||||
$ sx newkey
|
||||
5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn
|
||||
----
|
||||
|
||||
==== Public Keys
|
||||
|
||||
The public key is calculated from the private key using elliptic curve multiplication, which is irreversible: latexmath:[\(K = k * G\)]+ where +k+ is the private key, +G+ is a constant point called the _Generator Point_ and +K+ is the resulting public key. The reverse (division), or calculating +k+ if you know +K+ is as difficult as trying all possible values of +k+, i.e. a brute-force search.
|
||||
|
||||
Starting with a private key in the form of a randomly generated number +k+, we multiply it with a predetermined point on the curve called the _generator point_ +G+ to produce another point somewhere else on the curve, which is the corresponding public key +K+.
|
||||
|
||||
[latexmath]
|
||||
++++
|
||||
\begin{equation}
|
||||
{K = k * G}
|
||||
\end{equation}
|
||||
++++
|
||||
|
||||
[[key_derivation]]
|
||||
where +k+ is the private key, +G+ is a fixed point on the curve called the _generator point_, ((("generator point"))) and +K+ is the resulting public key, another point on the curve.
|
||||
|
||||
[TIP]
|
||||
====
|
||||
The private key is just a number. A public key can be generated from any private key. Therefore, a public key can be generated from any number, up to 256 bits long. You can pick your keys randomly using a method as simple as tossing a coin, pencil and paper. Toss a coin 256 times and you have the binary digits of a random private key you can use in a bitcoin wallet. Keys really are just a pair of numbers, one calculated from the other.
|
||||
====
|
||||
|
||||
The public key is a point on the elliptic curve, and consists of a pair of coordinates +(x,y)+, normally represented by a 512-bit number with the added prefix +04+.
|
||||
|
||||
Here's the public key generated by the private key we created above, shown as the coordinates +(x,y)+
|
||||
|
||||
.Public Key K defined as a point +K = (x,y)+
|
||||
----
|
||||
x = 32 5D 52 E3 B7 ... E5 D3 78
|
||||
y = 7A 3D 41 E6 70 ... CD 90 C2
|
||||
----
|
||||
|
||||
Here's the same public key shown as a 512-bit number (130 hex digits) with the prefix +04+ followed by +x+ and then +y+
|
||||
|
||||
.Uncompressed Public Key K shown in hex (130 hex digits) as +04 x y+
|
||||
----
|
||||
K = 04 32 5D 52 E3 B7 ... CD 90 C2
|
||||
----
|
||||
|
||||
[TIP]
|
||||
====
|
||||
A private key can be converted into a public key, but a public key cannot be converted back into a private key because the math only works one way.
|
||||
====
|
||||
|
||||
==== Addresses
|
||||
|
||||
An address is a string of digits and characters that can be shared with anyone who wants to send you money. In bitcoin, addresses begin with the digit "1". This is an address made by hashing the public key twice through two different hashing algorithms.
|
||||
|
||||
===== Base58 and Base58Check Encoding
|
||||
|
||||
==== Key Formats and Addresses
|
||||
|
||||
===== Private Key Formats
|
||||
|
||||
The private key can be represented in a number of different formats, all of which correspond to the same 256-bit number. These formats include:
|
||||
|
||||
.Private Key Representations (Encoding Formats)
|
||||
@ -128,24 +189,6 @@ The key above, for example can be represented as:
|
||||
|
||||
All of the above representations are different ways of showing the same number, the same private key. They look different, but any one format can easily be converted to any other format.
|
||||
|
||||
To generate a new key with bitcoind, use the +getnewaddress+ command. For security reasons it displays the public key only, not the private key. To ask bitcoind to expose the private key, use the +dumpprivkey+ command. Here's an example of both commands:
|
||||
|
||||
----
|
||||
$ bitcoind getnewaddress
|
||||
1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy
|
||||
$ bitcoind dumpprivkey 1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy
|
||||
KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ
|
||||
----
|
||||
|
||||
The +dumpprivkey+ command is opening the wallet and extracting the private key that was generated by the +getnewaddress+ command. It is not otherwise possible for bitcoind to know the private key from the public key, unless they are both stored in the wallet. In the example above, we see that the private key has a "K" prefix, indicating it is encoded as a WIF-compressed format. This means that the key-pair is stored in the wallet with both keys compressed, saving 31 bytes of space. If the prefix had been "5", indicating the WIF format, we would know that the key-pair is uncompressed.
|
||||
|
||||
You can also use +sx tools+ to generate keys and convert them between formats:
|
||||
|
||||
===== New key
|
||||
----
|
||||
$ sx newkey
|
||||
5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn
|
||||
----
|
||||
|
||||
===== Decoded from the Base58Check encoding to Hex
|
||||
----
|
||||
@ -165,26 +208,7 @@ $ sx base58check-encode 1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6
|
||||
KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ
|
||||
----
|
||||
|
||||
==== From Private Key to Public Key
|
||||
|
||||
The public key is calculated from the private key using elliptic curve multiplication, which is irreversible: latexmath:[\(K = k * G\)]+ where +k+ is the private key, +G+ is a constant point called the _Generator Point_ and +K+ is the resulting public key. The reverse (division), or calculating +k+ if you know +K+ is as difficult as trying all possible values of +k+, i.e. a brute-force search.
|
||||
|
||||
The public key is a point on the elliptic curve, and consists of a pair of coordinates +(x,y)+, normally represented by a 512-bit number with the added prefix +04+.
|
||||
|
||||
Here's the public key generated by the private key we created above, shown as the coordinates +(x,y)+
|
||||
|
||||
.Public Key K defined as a point +K = (x,y)+
|
||||
----
|
||||
x = 32 5D 52 E3 B7 ... E5 D3 78
|
||||
y = 7A 3D 41 E6 70 ... CD 90 C2
|
||||
----
|
||||
|
||||
Here's the same public key shown as a 512-bit number (130 hex digits) with the prefix +04+ followed by +x+ and then +y+
|
||||
|
||||
.Uncompressed Public Key K shown in hex (130 hex digits) as +04 x y+
|
||||
----
|
||||
K = 04 32 5D 52 E3 B7 ... CD 90 C2
|
||||
----
|
||||
===== Compressed Keys
|
||||
|
||||
The +y+ coordinate can be deduced from the +x+ coordinate, since they both lie on the same curved line defined by the elliptic curve equation. This makes it possible to store the public key _compressed_, with the +y+ omitted. A +compressed public key+ has the prefix +02+ if the +y+ is above the x-axis, and +03+ if it is below the x-axis, allowing the software to calculate it from +x+.
|
||||
|
||||
@ -195,21 +219,6 @@ Here's the same public key above, shown as a +compressed public key+ stored in 2
|
||||
K = 02 32 5D 52 E3 B7 ... E5 D3 78
|
||||
----
|
||||
|
||||
[TIP]
|
||||
====
|
||||
A private key can be converted into a public key, but a public key cannot be converted back into a private key because the math only works one way.
|
||||
====
|
||||
|
||||
==== From Public Key to Address
|
||||
|
||||
An address is a string of digits and characters that can be shared with anyone who wants to send you money. In bitcoin, addresses begin with the digit "1". This is an address made by hashing the public key twice through two different hashing algorithms.
|
||||
|
||||
==== Private Keys
|
||||
==== Public Keys
|
||||
==== Addresses
|
||||
==== Key Formats and Addresses
|
||||
===== Base58 and Base58Check Encoding
|
||||
===== Compressed Keys
|
||||
==== Wallets
|
||||
|
||||
There are many ways to generate keys for use in bitcoin. The simplest is to pick a large random number and turn it into a key pair (See <<key_derivation>>). A random key can be generated with very simple hardware or even manually with pen, paper and dice. The disadvantage of random keys is that if you generate many of them you must keep copies of all of them. Another method for making keys is _deterministic key generation_. Here you generate each new key as a function of the previous key, linking them in a sequence. As long as you can re-create that sequence, you only need the first key to generate them all. In this section we will examine the different methods for key generation.
|
||||
@ -219,20 +228,14 @@ There are many ways to generate keys for use in bitcoin. The simplest is to pick
|
||||
Wallets contain keys, not coins. The coins are stored on the blockchain in the form of transaction-outputs (often noted as vout or txout). Each user has a wallet containing keys. Wallets are really keychains containing pairs of private/public keys (See <<public key>>). Users sign transactions with the keys, thereby proving they own the transaction outputs (their coins).
|
||||
====
|
||||
|
||||
|
||||
[TIP]
|
||||
====
|
||||
The private key is just a number. A public key can be generated from any private key. Therefore, a public key can be generated from any number, up to 256 bits long. You can pick your keys randomly using a method as simple as tossing a coin, pencil and paper. Toss a coin 256 times and you have the binary digits of a random private key you can use in a bitcoin wallet. Keys really are just a pair of numbers, one calculated from the other.
|
||||
====
|
||||
|
||||
===== Type-0 or non-deterministic (random) keys
|
||||
===== Non-Deterministic (Random) Wallets
|
||||
|
||||
The first and most important step in generating keys is to find a secure source of entropy, or randomness. The private key is a 256-bit number, which must be selected at random. Creating a bitcoin key is essentially the same as "Pick a number between 1 and 2^256^". The exact method you use to pick that number does not matter as long as it is not predictable or repeatable. Bitcoin software will use the underlying operating system's random number generators to produce 256 bits of entropy. Usually, the OS random number generator is initialized by a human source of randomness, which is why you may be asked to wiggle your mouse around for a few seconds. For the truly paranoid, nothing beats dice, pencil and paper.
|
||||
|
||||
|
||||
[[Type0_keygen]]
|
||||
.Private key generation: From random mouse movements to a 256-bit number used as the private key
|
||||
image::images/Type-0 keygen.png["Private key generation"]
|
||||
[[Type0_wallet]]
|
||||
.Type-0 Non-Deterministic (Random) Wallet: A Collection of Randomly Generated Keys
|
||||
image::images/non-deterministic wallet.png["non-deterministic wallet"]
|
||||
|
||||
|
||||
[TIP]
|
||||
@ -240,8 +243,6 @@ image::images/Type-0 keygen.png["Private key generation"]
|
||||
The bitcoin private key is just a number. A public key can be generated from any private key. Therefore, a public key can be generated from any number, up to 256 bits long. You can pick your keys randomly using a method as simple as dice, pencil and paper.
|
||||
====
|
||||
|
||||
Once a private key has been generated, the public key equivalent can be derived from it using the elliptic curve multiplication function. Many software implementations of bitcoin use the OpenSSL library, specifically the https://www.openssl.org/docs/crypto/ec.html[Elliptic Curve library].
|
||||
|
||||
[TIP]
|
||||
====
|
||||
The size of bitcoin's private key, 2^256^ is a truly unfathomable number. It is equal to approximately 10^77^ in decimal. The visible universe contains approximately 10^80^ atoms.
|
||||
@ -249,35 +250,24 @@ The size of bitcoin's private key, 2^256^ is a truly unfathomable number. It is
|
||||
|
||||
This most basic form of key generation generates what are known as _Type-0_ or _Non-Deterministic_ (i.e. random) keys. When a sequence of keys is generated for a single user's wallet, each key is randomly generated when needed.
|
||||
|
||||
[[Type0_chain]]
|
||||
.Type-0 or Non-Deterministic Keys are randomly generated as needed
|
||||
image::images/type0_chain.png["Key generation"]
|
||||
|
||||
===== Type-1 deterministic (non-random) key chains
|
||||
|
||||
[[Type1_chain]]
|
||||
.Type-1 Deterministic Keys are generated from a phrase and index number
|
||||
image::images/type1_chain.png["Key generation"]
|
||||
|
||||
===== Type-2 chained deterministic keys
|
||||
|
||||
[[Type2_chain]]
|
||||
.Type-2 Chained Deterministic Keys are generated from a binary seed and index number
|
||||
image::images/type2_chain.png["Key generation"]
|
||||
|
||||
===== Type-2 hierarchical deterministic keys
|
||||
|
||||
[[Type2_tree]]
|
||||
.Type-2 Hierarchical Deterministic Keys are derived from a master seed using a tree structure
|
||||
image::images/BIP32-derivation.png["Key generation"]
|
||||
|
||||
|
||||
|
||||
===== Non-Deterministic (Random)
|
||||
===== Deterministic (Seeded)
|
||||
|
||||
===== Seed Words (BIP0039)
|
||||
|
||||
===== Deterministic Chains (Electrum Key Chains)
|
||||
|
||||
|
||||
[[Type1_wallet]]
|
||||
.Type-1 Deterministic Wallet: A Chain of Keys Generared from a Seed
|
||||
image::images/chained wallet.png["chained wallet"]
|
||||
|
||||
|
||||
===== Deterministic Trees (BIP0032)
|
||||
|
||||
[[Type2_wallet]]
|
||||
.Type-2 Hierarchical Deterministic Wallet: A Tree of Keys Generared from a Seed
|
||||
image::images/HD wallet.png["HD wallet"]
|
||||
|
||||
==== Key Storage
|
||||
===== Software Wallets
|
||||
===== Hardware Wallets
|
||||
|
BIN
images/HD wallet.png
Normal file
BIN
images/HD wallet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
BIN
images/chained wallet.png
Normal file
BIN
images/chained wallet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
images/non-deterministic wallet.png
Normal file
BIN
images/non-deterministic wallet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
Loading…
Reference in New Issue
Block a user