[appendix]
[[appendix_solutions]]
== Solutions

=== <<chapter_finite_fields>>: Finite Fields((("exercises", "answers to", "Chapter 1: Finite Fields", tertiary-sortas="Chapter 01: Finite Fields")))

include::code-ch01/answers.py[tag=exercise1,indent=0]

[source,python]
----
class FieldElement:
...
include::code-ch01/answers.py[tag=answer1,indent=4]
----

include::code-ch01/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch01/answers.py[tag=answer2,indent=0]
----

include::code-ch01/answers.py[tag=exercise3,indent=0]

[source,python]
----
class FieldElement:
...
include::code-ch01/answers.py[tag=answer3,indent=4]
----

include::code-ch01/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch01/answers.py[tag=answer4,indent=0]
----

include::code-ch01/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch01/answers.py[tag=answer5,indent=0]
----

When sorted, the results are always the same set.

include::code-ch01/answers.py[tag=exercise6,indent=0]

[source,python]
----
class FieldElement:
...
include::code-ch01/answers.py[tag=answer6,indent=4]
----

include::code-ch01/answers.py[tag=exercise7,indent=0]

[source,python]
----
include::code-ch01/answers.py[tag=answer7,indent=0]
----

include::code-ch01/answers.py[tag=exercise8,indent=0]

[source,python]
----
include::code-ch01/answers.py[tag=answer8,indent=0]
----

include::code-ch01/answers.py[tag=exercise9,indent=0]

[source,python]
----
class FieldElement:
...
include::code-ch01/answers.py[tag=answer9,indent=4]
----

=== <<chapter_elliptic_curves>>: Elliptic Curves((("exercises", "answers to", "Chapter 2: Elliptic Curves", tertiary-sortas="Chapter 02: Elliptic Curves")))

include::code-ch02/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch02/answers.py[tag=answer1,indent=0]
----

include::code-ch02/answers.py[tag=exercise2,indent=0]

[source,python]
----
class Point:
...
include::code-ch02/answers.py[tag=answer2,indent=4]
----

include::code-ch02/answers.py[tag=exercise3,indent=0]

[source,python]
----
class Point:
...
include::code-ch02/answers.py[tag=answer3,indent=4]
----

include::code-ch02/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch02/answers.py[tag=answer4,indent=0]
----

include::code-ch02/answers.py[tag=exercise5,indent=0]

[source,python]
----
class Point:
...
    def __add__(self, other):
    ...
include::code-ch02/answers.py[tag=answer5,indent=4]
----

include::code-ch02/answers.py[tag=exercise6,indent=0]

[source,python]
----
include::code-ch02/answers.py[tag=answer6,indent=0]
----

include::code-ch02/answers.py[tag=exercise7,indent=0]

[source,python]
----
class Point:
...
    def __add__(self, other):
    ...
include::code-ch02/answers.py[tag=answer7,indent=4]
----

=== <<chapter_elliptic_curve_cryptography>>: Elliptic Curve Cryptography((("exercises", "answers to", "Chapter 3: Elliptic Curve Cryptography", tertiary-sortas="Chapter 03: Elliptic Curve Cryptography")))

include::code-ch03/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer1,indent=0]
----

include::code-ch03/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer2,indent=0]
----

include::code-ch03/answers.py[tag=exercise3,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer3,indent=0]
----

include::code-ch03/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer4,indent=0]
----

include::code-ch03/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer5,indent=0]
----

include::code-ch03/answers.py[tag=exercise6,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer6,indent=0]
----

include::code-ch03/answers.py[tag=exercise7,indent=0]

[source,python]
----
include::code-ch03/answers.py[tag=answer7,indent=0]
----

=== <<chapter_serialization>>: Serialization((("exercises", "answers to", "Chapter 4: Serialization", tertiary-sortas="Chapter 04: Serialization")))

include::code-ch04/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer1,indent=0]
----

include::code-ch04/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer2,indent=0]
----

include::code-ch04/answers.py[tag=exercise3,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer3,indent=0]
----

include::code-ch04/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer4,indent=0]
----

include::code-ch04/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer5,indent=0]
----

include::code-ch04/answers.py[tag=exercise6,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer6,indent=0]
----

include::code-ch04/answers.py[tag=exercise7,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer7,indent=0]
----

include::code-ch04/answers.py[tag=exercise8,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer8,indent=0]
----

include::code-ch04/answers.py[tag=exercise9,indent=0]

[source,python]
----
include::code-ch04/answers.py[tag=answer9,indent=0]
----

=== <<chapter_tx_parsing>>: Transactions((("exercises", "answers to", "Chapter 5: Transactions", tertiary-sortas="Chapter 05: Transactions")))

include::code-ch05/answers.py[tag=exercise1,indent=0]

[source,python]
----
class Tx:
...
include::code-ch05/answers.py[tag=answer1,indent=0]
----

include::code-ch05/answers.py[tag=exercise2,indent=0]

[source,python]
----
class Tx:
...
include::code-ch05/answers.py[tag=answer2.1]
...

class TxIn:
...
include::code-ch05/answers.py[tag=answer2.2, indent=4]
----

include::code-ch05/answers.py[tag=exercise3,indent=0]

[source,python]
----
class Tx:
...
include::code-ch05/answers.py[tag=answer3.1]
...

class TxOut:
...
include::code-ch05/answers.py[tag=answer3.2, indent=4]
----

include::code-ch05/answers.py[tag=exercise4,indent=0]

[source,python]
----
class Tx:
...
include::code-ch05/answers.py[tag=answer4,indent=0]
----

include::code-ch05/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch05/answers.py[tag=answer5,indent=0]
----

include::code-ch05/answers.py[tag=exercise6,indent=0]

[source,python]
----
class Tx:
...
include::code-ch05/answers.py[tag=answer6,indent=4]
----

=== <<chapter_script>>: Script((("exercises", "answers to", "Chapter 6: Script", tertiary-sortas="Chapter 06: Script")))

include::code-ch06/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch06/answers.py[tag=answer1,indent=0]
----

include::code-ch06/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch06/answers.py[tag=answer2,indent=0]
----

include::code-ch06/answers.py[tag=exercise3,indent=0]

[source,python]
----
include::code-ch06/answers.py[tag=answer3,indent=0]
----

`OP_2` or `52` will satisfy the equation __x__^2^ + _x_ – 6 = 0.

include::code-ch06/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch06/answers.py[tag=answer4,indent=0]
----
<1> `collision1` and `collision2` are from the SHA-1 preimages that http://bit.ly/2HZF3om[Google found to collide].

This is looking for a SHA-1 collision. The only way to satisfy this script is to give `x` and `y` such that `x≠y` but `sha1(x)=sha1(y)`.

=== <<chapter_tx>>: Transaction Creation and Validation((("exercises", "answers to", "Chapter 7: Transaction Creation and Validation", tertiary-sortas="Chapter 07: Transaction Creation and Validation")))

include::code-ch07/answers.py[tag=exercise1,indent=0]

[source,python]
----
class Tx:
...
include::code-ch07/answers.py[tag=answer1,indent=4]
----

include::code-ch07/answers.py[tag=exercise2,indent=0]

[source,python]
----
class Tx:
...
include::code-ch07/answers.py[tag=answer2,indent=4]
----

include::code-ch07/answers.py[tag=exercise3,indent=0]

[source,python]
----
class Tx:
...
include::code-ch07/answers.py[tag=answer3,indent=4]
----

include::code-ch07/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch07/answers.py[tag=answer4,indent=0]
----

include::code-ch07/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch07/answers.py[tag=answer5,indent=0]
----

=== <<chapter_p2sh>>: Pay to Script Hash((("exercises", "answers to", "Chapter 8: Pay to Script Hash", tertiary-sortas="Chapter 08: Pay to Script Hash")))

include::code-ch08/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch08/answers.py[tag=answer1,indent=0]
----

include::code-ch08/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch08/answers.py[tag=answer2,indent=0]
----

include::code-ch08/answers.py[tag=exercise3,indent=0]

[source,python]
----
include::code-ch08/answers.py[tag=answer3,indent=0]
----

[role="pagebreak-before"]
include::code-ch08/answers.py[tag=exercise4,indent=0]

[source,python]
----
include::code-ch08/answers.py[tag=answer4,indent=0]
----

include::code-ch08/answers.py[tag=exercise5,indent=0]

[source,python]
----
class Tx:
...
include::code-ch08/answers.py[tag=answer5,indent=4]
----

=== <<chapter_blocks>>: Blocks((("exercises", "answers to", "Chapter 9: Blocks", tertiary-sortas="Chapter 09: Blocks")))

include::code-ch09/answers.py[tag=exercise1,indent=0]

[source,python]
----
class Tx:
...
include::code-ch09/answers.py[tag=answer1,indent=4]
----

include::code-ch09/answers.py[tag=exercise2,indent=0]

[source,python]
----
class Tx:
...
include::code-ch09/answers.py[tag=answer2,indent=4]
----

include::code-ch09/answers.py[tag=exercise3,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer3,indent=4]
----

[role="pagebreak-before"]
include::code-ch09/answers.py[tag=exercise4,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer4,indent=4]
----

include::code-ch09/answers.py[tag=exercise5,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer5,indent=4]
----

include::code-ch09/answers.py[tag=exercise6,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer6,indent=4]
----

include::code-ch09/answers.py[tag=exercise7,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer7,indent=4]
----

include::code-ch09/answers.py[tag=exercise8,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer8,indent=4]
----

include::code-ch09/answers.py[tag=exercise9,indent=0]

[source,python]
----
include::code-ch09/answers.py[tag=answer9,indent=0]
----

include::code-ch09/answers.py[tag=exercise10,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer10,indent=4]
----

include::code-ch09/answers.py[tag=exercise11,indent=0]

[source,python]
----
class Block:
...
include::code-ch09/answers.py[tag=answer11,indent=4]
----

include::code-ch09/answers.py[tag=exercise12,indent=0]

[source,python]
----
include::code-ch09/answers.py[tag=answer12,indent=0]
----

include::code-ch09/answers.py[tag=exercise13,indent=0]

[source,python]
----
include::code-ch09/answers.py[tag=answer13,indent=0]
----

=== <<chapter_networking>>: Networking((("exercises", "answers to", "Chapter 10: Networking")))

include::code-ch10/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch10/answers.py[tag=answer1,indent=0]
----

include::code-ch10/answers.py[tag=exercise2,indent=0]

[source,python]
----
class NetworkEnvelope:
...
include::code-ch10/answers.py[tag=answer2,indent=4]
----

include::code-ch10/answers.py[tag=exercise3,indent=0]

[source,python]
----
class NetworkEnvelope:
...
include::code-ch10/answers.py[tag=answer3,indent=4]
----

include::code-ch10/answers.py[tag=exercise4,indent=0]

[source,python]
----
class VersionMessage:
...
include::code-ch10/answers.py[tag=answer4,indent=4]
----

include::code-ch10/answers.py[tag=exercise5,indent=0]

[source,python]
----
class SimpleNode:
...
include::code-ch10/answers.py[tag=answer5,indent=4]
----

include::code-ch10/answers.py[tag=exercise6,indent=0]

[source,python]
----
class GetHeadersMessage:
...
include::code-ch10/answers.py[tag=answer6,indent=4]
----

=== <<chapter_spv>>: Simplified Payment Verification((("exercises", "answers to", "Chapter 11: Simplified Payment Verification")))

include::code-ch11/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch11/answers.py[tag=answer1,indent=0]
----

include::code-ch11/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch11/answers.py[tag=answer2,indent=0]
----

include::code-ch11/answers.py[tag=exercise3,indent=0]

[source,python]
----
include::code-ch11/answers.py[tag=answer3,indent=0]
----

include::code-ch11/answers.py[tag=exercise4,indent=0]

[source,python]
----
class Block:
...
include::code-ch11/answers.py[tag=answer4,indent=4]
----

include::code-ch11/answers.py[tag=exercise5,indent=0]

[source,python]
----
include::code-ch11/answers.py[tag=answer5,indent=0]
----

include::code-ch11/answers.py[tag=exercise6,indent=0]

[source,python]
----
class MerkleBlock:
...
include::code-ch11/answers.py[tag=answer6,indent=4]
----

include::code-ch11/answers.py[tag=exercise7,indent=0]

[source,python]
----
class MerkleBlock:
...
include::code-ch11/answers.py[tag=answer7,indent=4]
----

=== <<chapter_bloom_filters>>: Bloom Filters((("exercises", "answers to", "Chapter 12: Bloom Filters")))

include::code-ch12/answers.py[tag=exercise1,indent=0]

[source,python]
----
include::code-ch12/answers.py[tag=answer1,indent=0]
----

include::code-ch12/answers.py[tag=exercise2,indent=0]

[source,python]
----
include::code-ch12/answers.py[tag=answer2,indent=0]
----

include::code-ch12/answers.py[tag=exercise3,indent=0]

[source,python]
----
class BloomFilter:
...
include::code-ch12/answers.py[tag=answer3,indent=4]
----

include::code-ch12/answers.py[tag=exercise4,indent=0]

[source,python]
----
class BloomFilter:
...
include::code-ch12/answers.py[tag=answer4,indent=4]
----

include::code-ch12/answers.py[tag=exercise5,indent=0]

[source,python]
----
class GetDataMessage:
...
include::code-ch12/answers.py[tag=answer5,indent=4]
----

include::code-ch12/answers.py[tag=exercise6,indent=0]

[source,python]
----
include::code-ch12/answers.py[tag=answer6,indent=0]
----
