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
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -733,7 +733,7 @@ To whit, here are the steps:
733
733
====
734
734
The calculation of z requires two rounds of sha256. You may be wondering why there are two rounds when only 1 is necessary to get a 256-bit number. The reason is for security.
735
735
736
-
There is a well-known hash collision attack on SHA1 called a _birthday attack_ which basically makes finding collisions much easier. This is how Google found a SHA1 collision in 2017 (https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html). Using SHA1 twice, or double-SHA1 is the way to defeat this attack.
736
+
There is a well-known hash collision attack on sha1 called a _birthday attack_ which basically makes finding collisions much easier. This is how Google found a sha1 collision in 2017 (https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html). Using sha1 twice, or double-sha1 is the way to defeat this attack.
737
737
738
738
There is no known sha256 weakness like a birthday attack, but is doing two rounds is a defense against possible potential weaknesses.
Copy file name to clipboardExpand all lines: ch06.asciidoc
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -223,11 +223,11 @@ class Script:
223
223
<5> Any element longer than 520 bytes cannot be serialized.
224
224
<6> We prepend with the length of the entire Script.
225
225
226
-
Note that both the parser and serializer were used in Chapter 5 for Transaction parsing/serializing. The Script object represents the instruction set that needs to be validated.
226
+
Note that both the parser and serializer were used in Chapter 5 for Transaction parsing/serializing. The Script object represents the instruction set that requires validation.
227
227
228
228
=== Combining the Script fields
229
229
230
-
It's important to realize at this point that the lockbox (ScriptPubKey) and the unlocking (ScriptSig) are in *different* transactions. Specifically, the lockbox is where the bitcoins are received, the unlocking is where the bitcoins are spent. The input in the spending transaction *points to the receiving transaction*. Essentially, we have a situation like this:
230
+
To evaluate Script, we need to combine the ScriptPubKey and ScriptSig fields. The lockbox (ScriptPubKey) and the unlocking (ScriptSig) are in _different_ transactions. Specifically, the lockbox is where the bitcoins are received, the unlocking is where the bitcoins are spent. The input in the spending transaction _points to the receiving transaction_. Essentially, we have a situation like this:
231
231
232
232
.ScriptPubKey and ScriptSig
233
233
image::Script3.png[ScriptPubKey and ScriptSig]
@@ -247,7 +247,7 @@ class Script:
247
247
----
248
248
<1> We are combining the instruction set to create a new Script object.
249
249
250
-
We will utilize this for Script evaluation later in this chapter.
250
+
We will use this ability to combine Scripts for evaluation later in this chapter.
251
251
252
252
=== Stardard Scripts
253
253
@@ -339,7 +339,7 @@ True
339
339
<2> We can do this because of the $$__add__$$ method we created above.
340
340
<3> We want to go through the instructions and see if the result is `True` or not.
341
341
342
-
Here is the method that we'll use for the *combined* instruction set (combination of ScriptSig of the current transaction and the ScriptPubKey of the previous transaction).
342
+
Here is the method that we'll use for the _combined_ instruction set (combination of the ScriptPubKey of the previous transaction and the ScriptSig of the current transaction).
343
343
344
344
[source,python]
345
345
----
@@ -395,7 +395,7 @@ class Script:
395
395
====
396
396
The code shown here is a little bit of a cheat as the combined Script is not executed this way exactly. The ScriptSig is evaluated separately from the ScriptPubKey as to not allow operations from ScriptSig to affect the ScriptPubKey instructions.
397
397
398
-
Specifically, the stack after all the ScriptSig instructions are evaluated are stored and then the ScriptPubkey is evaluated on its own.
398
+
Specifically, the stack after all the ScriptSig instructions are evaluated are stored and then the ScriptPubkey instructions are evaluated on their own.
399
399
====
400
400
401
401
@@ -451,7 +451,7 @@ def op_0(stack):
451
451
return True
452
452
----
453
453
454
-
Numbers being pushed onto the stack are essentially encoded into bytes and decoded when the numerical value is need.
454
+
Numbers being pushed onto the stack are encoded into bytes and decoded from bytes when the numerical value is needed.
455
455
456
456
==== Exercise {counter:exercise}
457
457
@@ -636,9 +636,9 @@ Figure out what this Script is doing:
636
636
.Exercise 4
637
637
image::exercise2.png[Exercise 4]
638
638
639
-
==== SHA1 Piñata
639
+
==== Sha1 Piñata
640
640
641
-
In 2013, Peter Todd created a Script very similar to the exercise above and put some Bitcoins into it to create an economic incentive for people to find hash collisions. The donations reached 2.49153717 BTC and when Google actually found a hash collision for SHA1 in February of 2017 (https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html), this Script was promptly redeemed. The transaction output was 2.48 coins which was $2848.88 USD at the time.
641
+
In 2013, Peter Todd created a Script very similar to the Script in Exercise 4 and put some Bitcoins into it to create an economic incentive for people to find hash collisions. The donations reached 2.49153717 BTC and when Google actually found a hash collision for sha1 in February of 2017 (https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html), this Script was promptly redeemed. The transaction output was 2.48 coins which was $2848.88 USD at the time.
642
642
643
643
Peter created more piñatas for sha256, hash256 and hash160, which add economic incentives to find collisions for these hashing functions.
Copy file name to clipboardExpand all lines: ch07.asciidoc
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ A full node can check the spentness of an input pretty easily, a light client ha
36
36
37
37
The second part of the validation is making sure that the sum of the inputs is greater than or equal to the sum of the outputs. This ensures that the transaction does not create new coins. The one exception is a Coinbase transaction which we'll study more in Chapter 9. The inputs don't have an amount, so this must be looked up on the blockchain. Once again, full nodes will have access to the amounts associated with the unspent output, but light clients will have to depend on full nodes to supply this information.
38
38
39
-
We covered how to calculate fees in Chapter 5. This validation step is essentially the same as checking that the fee is not negative (that is, creating money). Recall the last exercise in Chapter 5. The answer should have looked something like this:
39
+
We covered how to calculate fees in Chapter 5. This validation step is essentially the same as checking that the fee is not negative (that is, creating money). Recall the last exercise in Chapter 5. The method `fee` looks like this:
40
40
41
41
[source,python]
42
42
----
@@ -66,16 +66,16 @@ True
66
66
<1> As with many other code samples, the ... is not literal, it's there to represent data that's too long to put into this book.
67
67
<2> This only works because we're using Python. See Note.
68
68
69
-
If the fee is negative, we know that the `output_sum` is greater than the `input_sum`, which is another way of saying that this transaction is trying to create money out of thin air.
69
+
If the fee is negative, we know that the `output_sum` is greater than the `input_sum`, which is another way of saying that this transaction is trying to create Bitcoins out of the ether.
70
70
71
71
[NOTE]
72
72
.Value Overflow Incident
73
73
====
74
-
Back in 2010, there was a transaction that created 184 Billion new bitcoins. This is due to the fact that in C++, the value field is actually an integer and not an unsigned integer. That is, the value could be negative!
74
+
Back in 2010, there was a transaction that created 184 billion new Bitcoins. This is due to the fact that in C++, the value field is a _signed_ integer and not an _unsigned_ integer. That is, the value could be negative!
75
75
76
-
The clever transaction passed all the checks with regard to fees and not creating new bitcoins, but only because the amount overflowed past the maximum number. Note 2^64^ is roughly 1.84 * 10^19^ satoshis, which is 184 Billion Bitcoins. The fee was negative by enough that the C++ code was essentially tricked into believing that the fee was actually positive by 0.1 BTC!
76
+
The clever transaction passed all the checks including the one for not creating new bitcoins, but only because the output amounts overflowed past the maximum number. Note 2^64^ is roughly 1.84 * 10^19^ satoshis, which is 184 billion Bitcoins. The fee was negative by enough that the C++ code was tricked into believing that the fee was actually positive by 0.1 BTC!
77
77
78
-
The vulnerability is detailed in CVE-2010-5139 and was patched via soft-fork in Bitcoin 0.3.11. The transaction and the extra bitcoins it created was invalidated retroactively by a block reorganization, which is another way of saying a new chain of blocks that's longer was mined to replace it.
78
+
The vulnerability is detailed in CVE-2010-5139 and was patched via soft-fork in Bitcoin Core 0.3.11. The transaction and the extra Bitcoins it created was invalidated retroactively by a block reorganization, which is another way of saying a new chain of blocks that's longer was mined to replace it.
79
79
====
80
80
81
81
=== Checking the Signature
@@ -94,12 +94,12 @@ Perhaps the trickiest part of validating a transaction is the process of checkin
94
94
True
95
95
----
96
96
97
-
SEC public keys and DER signatures are in the stack when an instruction like `OP_CHECKSIG` is executed make getting the public key `P` and signature `(r,s)` pretty straightforward. The hard part is getting the actual signature hash `z`. You would think that this would be easy since you can just hash the transaction. But you can't do that since the signature is part of the ScriptSig and a signature can't sign itself.
97
+
SEC public keys and DER signatures are in the stack when an instruction like `OP_CHECKSIG` is executed make getting the public key `P` and signature `(r,s)` pretty straightforward. The hard part is getting the actual signature hash `z`. A naive way to get the signature hash would be to hash the entire transaction. Unfortunately, we can't do that since the signature is part of the ScriptSig and a signature can't sign itself.
98
98
99
99
.Validation Start
100
100
image::validation1.png[Validation Start]
101
101
102
-
Instead, what you need to do is to modify the transaction before actually signing it. That is, we have to compute a different `z` _for each input_. The procedure is as follows.
102
+
Instead, what you need to do is to modify the transaction before actually signing it. That is, we have to compute a different signature hash, or `z`, _for each input_. The procedure is as follows.
103
103
104
104
==== Step 1: Empty all the ScriptSigs
105
105
@@ -156,14 +156,13 @@ We can now make this transaction validation process into a method for `Tx`. Than
156
156
[NOTE]
157
157
.Quadratic Hashing
158
158
====
159
-
One of the reasons why this method of creating the signature hash is inefficient is because of the Quadratic Hashing problem. The Quadratic Hashing problem is the fact that calculating the signature hashes, or `z`'s varies quadratically with the number of inputs in a transaction. Specifically, the number of `hash256` operations for calculating the `z` will increase on a per-input basis, but in addition, the length of the transaction will increase, slowing down each hash256 operation as the entire signature hash will need to be calculated anew for each input.
159
+
One of the reasons why this method of creating the signature hash is inefficient is because of the Quadratic Hashing problem. The Quadratic Hashing problem is the fact that calculating the signature hashes, or `z`'s increases quadratically with the number of inputs in a transaction. Specifically, the number of hash256 operations for calculating the `z` will increase on a per-input basis, but in addition, the length of the transaction will increase, slowing down each hash256 operation as the entire signature hash will need to be calculated anew for each input.
160
160
161
-
This was particularly obvious with the biggest transaction ever mined: `bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08`. This transaction had 5569 inputs and 1 output and took many miners over a minute to validate as the signature hashes were so expensive to calculate.
161
+
This was particularly obvious with the biggest transaction ever mined: `bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08`. This transaction had 5569 inputs and 1 output and took many miners over a minute to validate as the signature hashes for the transaction were so expensive to calculate.
162
162
163
163
Segwit (Chapter 13) fixes this with a different way of calculating the signature hash, which is specified in BIP0143.
Copy file name to clipboardExpand all lines: ch08.asciidoc
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -267,7 +267,7 @@ We now need to special case the particular sequence of redeem_script, `OP_HASH16
267
267
return False
268
268
return True
269
269
----
270
-
<1> 0xa9 is `OP_HASH160`, 0x87 is `OP_EQUAL`. We're checking here that the next 3 instructions are exactly the pattern we're looking for.
270
+
<1> `0xa9` is `OP_HASH160`, `0x87` is `OP_EQUAL`. We're checking here that the next 3 instructions are exactly the pattern we're looking for.
271
271
<2> We know that this is `OP_HASH160`, so we just pop it off. Similarly, we know the next one is the 20-byte hash value and the third item is `OP_EQUAL`, which is what we tested for in the if statement above it.
272
272
<3> We run the `OP_HASH160`, 20-byte hash push on the stack and `OP_EQUAL` as normal.
273
273
<4> There should be a 1 remaining, which is what op_verify checks for (`OP_VERIFY` consumes 1 element and does not put anything back).
@@ -278,7 +278,7 @@ We now need to special case the particular sequence of redeem_script, `OP_HASH16
278
278
279
279
The nice thing about p2sh is that the RedeemScript can be as long as the largest single element from `OP_PUSHDATA2`, which is 520 bytes. Multisig is just one possibility. You can have Scripts that define more complicated logic like "2 of 3 of these keys or 5 of 7 of these other keys". The main feature of p2sh is that it's very flexible and at the same time reduces the UTXO set size by pushing the burden of storing part of the Script back to the user.
280
280
281
-
As we'll see in Chapter 13, p2sh will be used for backwards compatibility with Segwit.
281
+
As we'll see in Chapter 13, p2sh is used to make Segwit backwards compatible.
0 commit comments