You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ch03.asciidoc
+14-36Lines changed: 14 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,23 +16,11 @@ Pi, sqrt(2), e+7th root of 19, etc are all part of real numbers.
16
16
This worked because _real_ numbers are also a field.
17
17
Note unlike a _finite_ field, there are an _infinite_ number of real numbers, but otherwise the same properties hold:
18
18
19
-
1.
20
-
if *a* and *b* are in the set, *a+b* and *a⋅b* is in the set.
21
-
We call this property _closed_
22
-
2.
23
-
The additive identity, *0* exists.
24
-
This means *a + 0 = a*
25
-
3.
26
-
The multiplicative identity, *1* exists.
27
-
This means *a ⋅ 1 = a*
28
-
4.
29
-
If *a* is in the set, *-a* is in the set.
30
-
*-a* is defined as the value that makes *a + (-a) = 0*.
31
-
This is what we call the _additive inverse_.
32
-
5.
33
-
If *a* is in the set and is not 0, *a^-1^* is in the set.
34
-
*a^-1^* is defined as the value that makes *a ⋅ a^-1^ = 1*.
35
-
This is what we call the _multiplicative inverse_.
19
+
1. If *a* and *b* are in the set, *a+b* and *a⋅b* are in the set.
20
+
2. The additive identity, *0* exists and has the property *a + 0 = a*.
21
+
3. The multiplicative identity, *1* exists and has the property *a ⋅ 1 = a*.
22
+
4. If *a* is in the set, *-a* is in the set, which is defined as the value that makes *a + (-a) = 0*.
23
+
5. If *a* is in the set and is not 0, *a^-1^* is in the set, which is defined as the value that makes *a ⋅ a^-1^ = 1*.
36
24
37
25
Clearly, all of these are true as normal addition and multiplication apply for the first part, additive and multiplicative identities 0 and 1 exist, -x is the additive inverse and 1/x is the multiplicative inverse.
38
26
@@ -697,14 +685,10 @@ The only way we could know the details of R beforehand is if we know `e`.
697
685
698
686
To whit, here are the steps:
699
687
700
-
1.
701
-
We are given (r, s) as the signature, `z` as the hash of the thing being signed and P, the public key (or public point) of the signer.
702
-
2.
703
-
We calculate u = z/s, v = r/s
704
-
3.
705
-
We calculate uG + vP = R
706
-
4.
707
-
If R's `x` coordinate equals `r`, the signature is valid.
688
+
1. We are given (r, s) as the signature, `z` as the hash of the thing being signed and P, the public key (or public point) of the signer.
689
+
2. We calculate u = z/s, v = r/s
690
+
3. We calculate uG + vP = R
691
+
4. If R's `x` coordinate equals `r`, the signature is valid.
708
692
709
693
[NOTE]
710
694
.Why two rounds of sha256?
@@ -774,17 +758,11 @@ We do this by choosing a random `k`.
774
758
775
759
Signing Procedure:
776
760
777
-
1.
778
-
We are given `z`.
779
-
We know `e` and eG=P.
780
-
2.
781
-
Choose a random `k`
782
-
3.
783
-
Calculate R=kG and r=x-coordinate of R
784
-
4.
785
-
Calculate s = (z+re)/k
786
-
5.
787
-
Signature is (r,s)
761
+
1. We are given `z` and know `e` such that eG=P.
762
+
2. Choose a random `k`
763
+
3. Calculate R=kG and r=x-coordinate of R
764
+
4. Calculate s = (z+re)/k
765
+
5. Signature is (r,s)
788
766
789
767
Note that the pubkey `P` has to be transmitted to whoever wants to verify and `z` must be known by the verifier.
790
768
We'll see later that `z` is computed and P is sent along with the signature.
Copy file name to clipboardExpand all lines: ch04.asciidoc
+22-44Lines changed: 22 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,12 +21,9 @@ There are two forms of SEC format that we need to be concerned with and the firs
21
21
22
22
Here is how the uncompressed SEC format for a given point P=(x,y) is generated:
23
23
24
-
1.
25
-
Start with the prefix byte which is `0x04`.
26
-
2.
27
-
Next, append the x-coordinate in 32 bytes as a Big-Endian integer.
28
-
3.
29
-
Next, append the y-coordinate in 32 bytes as a Big-Endian integer.
24
+
1. Start with the prefix byte which is `0x04`.
25
+
2. Next, append the x-coordinate in 32 bytes as a Big-Endian integer.
26
+
3. Next, append the y-coordinate in 32 bytes as a Big-Endian integer.
30
27
31
28
Here is what the uncompressed SEC format looks like:
32
29
@@ -99,11 +96,9 @@ We call this the *Compressed SEC format* because of how the y-coordinate is comp
99
96
100
97
Here is the serialization of the Compressed SEC format for a given point P=(x,y):
101
98
102
-
1.
103
-
Start with the prefix byte.
99
+
1. Start with the prefix byte.
104
100
If `y` is even, it's `0x02`, otherwise it's `0x03`.
105
-
2.
106
-
Next, append the x-coordinate in 32 bytes as a Big-Endian integer.
101
+
2. Next, append the x-coordinate in 32 bytes as a Big-Endian integer.
107
102
108
103
The Compressed SEC format looks like this:
109
104
@@ -212,20 +207,14 @@ This was most likely because the standard was already defined in 2008, supported
212
207
213
208
DER Signature format is defined like this:
214
209
215
-
1.
216
-
Start with the `0x30` byte
217
-
2.
218
-
Encode the length of the rest of the signature (usually `0x44` or `0x45`) and append
219
-
3.
220
-
Append the marker byte `0x02`
221
-
4.
222
-
Encode `r` as a Big-Endian integer, but prepend with `0x00` byte if `r`'s first byte >= `0x80`.
210
+
1. Start with the `0x30` byte
211
+
2. Encode the length of the rest of the signature (usually `0x44` or `0x45`) and append
212
+
3. Append the marker byte `0x02`
213
+
4. Encode `r` as a Big-Endian integer, but prepend with `0x00` byte if `r`'s first byte >= `0x80`.
223
214
Prepend the resulting length to `r`.
224
215
Add this to the result.
225
-
5.
226
-
Append the marker byte `0x02`
227
-
6.
228
-
Encode `s` as a Big-Endian integer, but prepend with `0x00` byte if `s`'s first byte >= `0x80`.
216
+
5. Append the marker byte `0x02`
217
+
6. Encode `s` as a Big-Endian integer, but prepend with `0x00` byte if `s`'s first byte >= `0x80`.
229
218
Prepend the resulting length to `s`.
230
219
Add this to the result.
231
220
@@ -331,16 +320,11 @@ To both shorten and increase security, we can use the ripemd160 hash to compress
331
320
By not using the SEC format directly, we can go from 33 bytes to 20 bytes, shortening the address significantly.
332
321
Here is how a Bitcoin address is created:
333
322
334
-
1.
335
-
For mainnet addresses, start with the prefix `0x00`, for testnet `0x6f`.
336
-
2.
337
-
Take the SEC format (compressed or uncompressed) and do a sha256 operation followed by the ripemd160 hash operation, the combination which is called a hash160 operation.
338
-
3.
339
-
Combine the prefix from #1 and resulting hash from #2
340
-
4.
341
-
Do a hash256 of the result from #3 and get the first 4 bytes.
342
-
5.
343
-
Take the combination of #3 and #4 and encode in Base58.
323
+
1. For mainnet addresses, start with the prefix `0x00`, for testnet `0x6f`.
324
+
2. Take the SEC format (compressed or uncompressed) and do a sha256 operation followed by the ripemd160 hash operation, the combination which is called a hash160 operation.
325
+
3. Combine the prefix from #1 and resulting hash from #2
326
+
4. Do a hash256 of the result from #3 and get the first 4 bytes.
327
+
5. Take the combination of #3 and #4 and encode in Base58.
344
328
345
329
Step 4 of this process is called the checksum.
346
330
We can do steps 4 and 5 in one go this way:
@@ -393,18 +377,12 @@ WIF uses the same Base58 encoding that addresses use.
393
377
394
378
Here is how the WIF format is created:
395
379
396
-
1.
397
-
For mainnet private keys, start with the prefix `0x80`, for testnet `0xef`.
398
-
2.
399
-
Encode the secret in 32-byte Big-Endian.
400
-
3.
401
-
If the SEC format used for the public key address was compressed add a suffix of `0x01`.
402
-
4.
403
-
Combine the prefix from #1, serialized secret from #2 and suffix from #3
404
-
5.
405
-
Do a hash256 of the result from #4 and get the first 4 bytes.
406
-
6.
407
-
Take the combination of #4 and #5 and encode in Base58.
380
+
1. For mainnet private keys, start with the prefix `0x80`, for testnet `0xef`.
381
+
2. Encode the secret in 32-byte Big-Endian.
382
+
3. If the SEC format used for the public key address was compressed add a suffix of `0x01`.
383
+
4. Combine the prefix from #1, serialized secret from #2 and suffix from #3
384
+
5. Do a hash256 of the result from #4 and get the first 4 bytes.
385
+
6. Take the combination of #4 and #5 and encode in Base58.
408
386
409
387
We can now create the `wif` method on the `PrivateKey` class.
Copy file name to clipboardExpand all lines: ch05.asciidoc
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,14 +14,10 @@ Let's first look at what Transactions in Bitcoin are, what they look like and ho
14
14
At a high level, a Transaction really only has 4 components.
15
15
They are:
16
16
17
-
1.
18
-
Version
19
-
2.
20
-
Inputs
21
-
3.
22
-
Outputs
23
-
4.
24
-
Locktime
17
+
1. Version
18
+
2. Inputs
19
+
3. Outputs
20
+
4. Locktime
25
21
26
22
A general overview of these fields might be helpful.
27
23
Version indicates what additional features the transaction uses, Inputs define what Bitcoins are being spent, Outputs define where the Bitcoins are going and Locktime defines when this Transaction starts being valid.
Copy file name to clipboardExpand all lines: ch08.asciidoc
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,18 +116,15 @@ Bare multisig is a bit ugly, but it's very much functional.
116
116
You can have `m` of `n` signatures required to unlock a UTXO and there is plenty of utility in making outputs multisig, especially if you're a business.
117
117
However, bare multisig suffers from a few problems:
118
118
119
-
1.
120
-
First problem: the long length of the ScriptPubKey.
119
+
1. First problem: the long length of the ScriptPubKey.
121
120
A hypothetical bare multisig address has to encompass many different public keys and that makes the ScriptPubKey extremely long.
122
121
Unlike p2pkh or even p2pk, these are not easily communicated using voice or even text message.
123
122
124
-
2.
125
-
Second problem: because the output is so long, it's rather taxing on node software.
123
+
2. Second problem: because the output is so long, it's rather taxing on node software.
126
124
Nodes have to keep track of the UTXO set, so keeping a particularly big ScriptPubKey ready is onerous.
127
125
A large output is more expensive to keep in fast-access storage (like RAM), being 5-20x larger than a normal p2pkh output.
128
126
129
-
3.
130
-
Third problem: because the ScriptPubKey can be so much bigger, bare multisig can and has been abused.
127
+
3. Third problem: because the ScriptPubKey can be so much bigger, bare multisig can and has been abused.
131
128
The entire pdf of the Satoshi's original whitepaper is actually encoded in this transaction in block 230009: `54e48e5f5c656b26c3bca14a8c95aa583d07ebe84dde3b7dd4a78f4e4186e713`.
132
129
The creator of this transaction actually split up the whitepaper pdf into 64 byte chunks which were then made into invalid uncompressed public keys.
133
130
These are not valid points and the actual whitepaper was encoded into 947 outputs as 1 of 3 bare multisig outputs.
Copy file name to clipboardExpand all lines: ch11.asciidoc
+10-20Lines changed: 10 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,8 @@ Is it possible to create a Bitcoin wallet on a phone without having all the data
16
16
17
17
For any wallet, there are two scenarios that we're concerned with:
18
18
19
-
1.
20
-
Paying someone
21
-
2.
22
-
Getting paid by someone
19
+
1. Paying someone
20
+
2. Getting paid by someone
23
21
24
22
If you are paying someone with your Bitcoin wallet, it is up to the person receiving your Bitcoins to verify that they've been paid.
25
23
Once they've verified that the transaction has been included in a block sufficiently deep, they'll give you the good or service you are expecting in return.
@@ -42,17 +40,12 @@ The prerequisites are an ordered list of items and a hash function.
42
40
In our case, transactions and hash256 are what we use.
43
41
To construct the Merkle Tree, we follow this algorithm:
44
42
45
-
1.
46
-
Hash all the items of the ordered list with the provided hash function
47
-
2.
48
-
If there is exactly 1 hash, we are done
49
-
3.
50
-
If there is an odd number of hashes, we add a copy of the last hash in the list to the end so that we have an even number.
51
-
4.
52
-
We pair the hashes in order and hash the concatenation to get the parent level.
43
+
1. Hash all the items of the ordered list with the provided hash function
44
+
2. If there is exactly 1 hash, we are done
45
+
3. If there is an odd number of hashes, we add a copy of the last hash in the list to the end so that we have an even number.
46
+
4. We pair the hashes in order and hash the concatenation to get the parent level.
53
47
We should have half the number of hashes as before.
54
-
5.
55
-
Go to 2.
48
+
5. Go to 2.
56
49
57
50
The idea is to come to a single hash that represents all of the hashes.
58
51
The gist of getting the Merkle Tree looks like this:
@@ -385,12 +378,9 @@ The flags are a list of bits that tell us about nodes in depth-first order.
385
378
386
379
The rules for the flags are:
387
380
388
-
1.
389
-
If the node is given to us (blue box in the Figure 11-7), the flag is 0 and the next hash is its hash value.
390
-
2.
391
-
If the node is an internal node and calculated, that is, calculated from its children (dotted outline in the Figure 11-7), the flag is 1.
392
-
3.
393
-
If the node is a leaf node and is a transaction we're interested in (green box in the Figure 11-7), the flag is 1 and the next hash is its hash value.
381
+
1. If the node is given to us (blue box in the Figure 11-7), the flag is 0 and the next hash is its hash value.
382
+
2. If the node is an internal node and calculated, that is, calculated from its children (dotted outline in the Figure 11-7), the flag is 1.
383
+
3. If the node is a leaf node and is a transaction we're interested in (green box in the Figure 11-7), the flag is 1 and the next hash is its hash value.
The hash function we used (and how we converted that to a number)
57
-
3.
58
-
The bit field, which tells the other node which bucket we're interested in.
53
+
1. The size of the bit field
54
+
2. The hash function we used (and how we converted that to a number)
55
+
3. The bit field, which tells the other node which bucket we're interested in.
59
56
60
57
This works great for a single item, so would be great if we only had a single address/ScriptPubKey/Transaction ID that we're interested in.
61
58
What do we do when there's more than 1 item?
@@ -112,13 +109,10 @@ This means that we can manipulate our Bloom Filter to have the optimum number of
112
109
BIP0037 defines exactly how Bitcoin uses Bloom Filters.
113
110
As before, the things that we need to let the other node know about are:
114
111
115
-
1.
116
-
The size of the bit field.
112
+
1. The size of the bit field.
117
113
We send this in bytes (8 bits per byte) and round up if we need to.
118
-
2.
119
-
We also send over how many hash functions we want to use along with a "tweak" to be able to change our bloom filter if our filter hits too many items.
120
-
3.
121
-
Lastly, we need to send over the actual bit field that results from running the Bloom Filter over our items.
114
+
2. We also send over how many hash functions we want to use along with a "tweak" to be able to change our bloom filter if our filter hits too many items.
115
+
3. Lastly, we need to send over the actual bit field that results from running the Bloom Filter over our items.
122
116
123
117
While we could define lots of hash functions (sha256, keccak, ripemd, blake, etc), in practice, we only really use a single hash function with a different seed.
0 commit comments