mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-11-23 00:28:14 +00:00
no message
This commit is contained in:
parent
10047ce80a
commit
71a9fb520b
@ -52,22 +52,17 @@ With SHA-256, the output is always 256 bits long, regardless of the size of the
|
|||||||
[[sha256_example1]]
|
[[sha256_example1]]
|
||||||
.SHA256 Example
|
.SHA256 Example
|
||||||
====
|
====
|
||||||
[source, shell]
|
++++
|
||||||
----
|
$ python
|
||||||
$ python <1>
|
|
||||||
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
|
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
|
||||||
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
|
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
|
||||||
Type "help", "copyright", "credits" or "license" for more information.
|
Type "help", "copyright", "credits" or "license" for more information.
|
||||||
|
|
||||||
>>> import hashlib <2>
|
>>> import hashlib
|
||||||
>>> print hashlib.sha256("I am Satoshi Nakamoto").hexdigest() <3>
|
>>> print hashlib.sha256("I am Satoshi Nakamoto").hexdigest()
|
||||||
5d7c7ba21cbbcd75d14800b100252d5b428e5b1213d27c385bc141ca6b47989e <4>
|
5d7c7ba21cbbcd75d14800b100252d5b428e5b1213d27c385bc141ca6b47989e
|
||||||
|
|
||||||
----
|
++++
|
||||||
<1> Start the python interpreter
|
|
||||||
<2> Python's hashlib package provides hashing algorithms
|
|
||||||
<3> The python command to show the hash of our example phrase
|
|
||||||
<4> The resulting hash, in hexadecimal notation
|
|
||||||
====
|
====
|
||||||
|
|
||||||
The example shows that if we calculate the hash of the phrase +"I am Satoshi Nakamoto"+, it will produce +5d7c7ba21cbbcd75d14800b100252d5b428e5b1213d27c385bc141ca6b47989e+. This 256-bit number is the _hash_ or _digest_ of the phrase and depends on every part of the phrase. Adding a single letter, punctuation mark or any character will produce a different hash.
|
The example shows that if we calculate the hash of the phrase +"I am Satoshi Nakamoto"+, it will produce +5d7c7ba21cbbcd75d14800b100252d5b428e5b1213d27c385bc141ca6b47989e+. This 256-bit number is the _hash_ or _digest_ of the phrase and depends on every part of the phrase. Adding a single letter, punctuation mark or any character will produce a different hash.
|
||||||
@ -88,8 +83,7 @@ Running this will produce the hashes of several phrases, made different by addin
|
|||||||
[[sha256_example_generator_output]]
|
[[sha256_example_generator_output]]
|
||||||
.SHA256 Output of a script for generating many hashes by iterating on a nonce
|
.SHA256 Output of a script for generating many hashes by iterating on a nonce
|
||||||
====
|
====
|
||||||
[source, shell]
|
++++
|
||||||
----
|
|
||||||
$ python hash_example.py
|
$ python hash_example.py
|
||||||
I am Satoshi Nakamoto0 => a80a81401765c8eddee25df36728d732acb6d135bcdee6c2f87a3784279cfaed
|
I am Satoshi Nakamoto0 => a80a81401765c8eddee25df36728d732acb6d135bcdee6c2f87a3784279cfaed
|
||||||
I am Satoshi Nakamoto1 => f7bc9a6304a4647bb41241a677b5345fe3cd30db882c8281cf24fbb7645b6240
|
I am Satoshi Nakamoto1 => f7bc9a6304a4647bb41241a677b5345fe3cd30db882c8281cf24fbb7645b6240
|
||||||
@ -113,7 +107,7 @@ I am Satoshi Nakamoto18 => 9989a401b2a3a318b01e9ca9a22b0f39d82e48bb51e0d324aaa44
|
|||||||
I am Satoshi Nakamoto19 => cda56022ecb5b67b2bc93a2d764e75fc6ec6e6e79ff6c39e21d03b45aa5b303a
|
I am Satoshi Nakamoto19 => cda56022ecb5b67b2bc93a2d764e75fc6ec6e6e79ff6c39e21d03b45aa5b303a
|
||||||
|
|
||||||
$
|
$
|
||||||
----
|
++++
|
||||||
====
|
====
|
||||||
|
|
||||||
Each phrase produces a completely different hash result. They seem completely random, but you can re-produce the exact results in this example on any computer with Python and see the same exact hashes.
|
Each phrase produces a completely different hash result. They seem completely random, but you can re-produce the exact results in this example on any computer with Python and see the same exact hashes.
|
||||||
@ -144,8 +138,7 @@ Running the code above, you can set the desired difficulty (in bits, how many of
|
|||||||
[[pow_example_outputs]]
|
[[pow_example_outputs]]
|
||||||
.Running the proof-of-work example for various difficulties
|
.Running the proof-of-work example for various difficulties
|
||||||
====
|
====
|
||||||
[source, shell]
|
++++
|
||||||
----
|
|
||||||
$ python proof-of-work-example.py
|
$ python proof-of-work-example.py
|
||||||
|
|
||||||
Difficulty: 0
|
Difficulty: 0
|
||||||
@ -196,7 +189,7 @@ Success with nonce 18779387
|
|||||||
Hash is 000000a7e616c6968f22435687aae9e071cd5a8cb5b704ef2bff67bd258e4aab
|
Hash is 000000a7e616c6968f22435687aae9e071cd5a8cb5b704ef2bff67bd258e4aab
|
||||||
Elapsed Time: 106.63 seconds
|
Elapsed Time: 106.63 seconds
|
||||||
Hashing Power: 176122 hashes per second
|
Hashing Power: 176122 hashes per second
|
||||||
----
|
++++
|
||||||
====
|
====
|
||||||
|
|
||||||
As you can see, increasing the difficulty by 4 bits causes an exponential increase in the time it takes to find a solution. If you think of the entire 256-bit number space, each time you constrain one more bit to zero, you decrease the search space by half. In the example above, it takes 18 million hash attempts to find a nonce that produces a hash with 24 leading bits as zero. Even at a speed of more than 170 thousand hashes per second, it almost two minutes to find this solution.
|
As you can see, increasing the difficulty by 4 bits causes an exponential increase in the time it takes to find a solution. If you think of the entire 256-bit number space, each time you constrain one more bit to zero, you decrease the search space by half. In the example above, it takes 18 million hash attempts to find a nonce that produces a hash with 24 leading bits as zero. Even at a speed of more than 170 thousand hashes per second, it almost two minutes to find this solution.
|
||||||
@ -212,8 +205,6 @@ Bitcoin is tuned to generate blocks approximately every 10 minutes. This is achi
|
|||||||
The difficulty of finding a bitcoin block is approximately '10 minutes of processing' for the entire network, based on the time it took to find the previous 2106 blocks, adjusted every 2106 blocks. If you know the processing power of the network in hashes per second, you can calculate how many hashes per 10 minutes, which is how many on avergae to find a block, ie. the current difficulty.
|
The difficulty of finding a bitcoin block is approximately '10 minutes of processing' for the entire network, based on the time it took to find the previous 2106 blocks, adjusted every 2106 blocks. If you know the processing power of the network in hashes per second, you can calculate how many hashes per 10 minutes, which is how many on avergae to find a block, ie. the current difficulty.
|
||||||
====
|
====
|
||||||
|
|
||||||
===
|
|
||||||
|
|
||||||
=== Transaction Fees
|
=== Transaction Fees
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user