Skip to content

Commit

Permalink
Bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Nov 30, 2014
1 parent 23c23ce commit 5e3018f
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 87 deletions.
71 changes: 2 additions & 69 deletions algorithm.md
Expand Up @@ -27,6 +27,8 @@ Some of the major classes of structures are:

You need to decide what algorithm is better than what: <http://en.wikipedia.org/wiki/Analysis_of_algorithms>

The first thing to understand is how to model a computer: the most common way is to use a Turing Machine or one of its variants.

The consumption of the following resources must be analyzed:

- time
Expand All @@ -50,75 +52,6 @@ Also, our measures will often be asymptotic (input $n$ tends to infinity), so we

One important idea is then to classify algorithm complexities into larger classes, e.g. P and NP. Those classifications are meaningful because exponential time algorithms will never be implementable in practice even for relatively small inputs. P vs NP is one of the many questions about the equality or not of such nested complexity classes.

### Computation model

The fist thing to do is to decide on a computer model to work with.

#### Turing machine

Classical model.

##### Variants

###### Non-deterministic Turing Machine

###### NTM

Turing machine that has multiple possible transitions per input and state.

It decides between those transitions either:

- optimally through a magic oracle.
- by following all paths at once. TODO: what is the correct output if multiple paths halt?

##### Limitations of Turing machines

While Turing machines accurately describe decidability of existing systems, it does not model performance so well for the following reasons:

- modern computers have random access memory. Fortunately it is simple to model performance by using the so called RAM computation model.

- out-of-core operations: for very large inputs, it is necessary to store data in lower speed access media like hard disks. It then becomes necessary to model how much slower those accesses are.

#### RAM model

Random data access. Same computability class as Turing machine, but models currently existing memories better.

TODO vs Turing machine?

#### Out of core

#### External memory

Sometimes algorithms must operate on data that is too large to fit in RAM, e.g. hard disks. In those cases, it may be necessary to take that into consideration, since disk IO is 1000 slower than RAM access.

Certain algorithms are developed with that restriction in mind, e.g., the B-tree, which is less efficient than other binary search trees for in RAM computing, but much more efficient of out-of-core problems.

There is no simple way of modeling the performance of out-of-core algorithms: we just have to give different weights to certain operations, and then solve complex numerical optimization decisions.

#### Input length vs value

Keep in mind that big O analysis uses a Turing machine, so what matters is the *length* of the input, *not* its value.

For example, deciding if a number $n$ is prime takes at most $\sqrt(n)$ steps (try dividing by each number smaller than $\sqrt(n)$), so one might think that deciding primeness is $Polynomial$.

However, $n$ is exponential on the number of digits of $n$! Adding a single digit 0 to the beginning of a number, multiplies it by 2 (in binary)!

Therefore, deciding primeness via trial division is not polynomial.

The practical importance of this depends on the nature of the input:

- if the input is man generated, such as a prime used for cryptography, it is easy to add lots zeros and ones to the beginning of a number so we have to consider the exponential aspect.

- if however $n$ is a number that comes out in some natural model in which $n$ itself cannot be too large because it cannot simply double too quickly (say, the number of people on the planet), then the exponential bound is not very meaningful.

##### Strongly NP

A problem is *strongly NP* if it is NP even if the input values are considered instead of the input lengths.

Therefore, the naive primeness test is not strongly NP complete, since if values were considered instead of input lengths, then it would be P.

Known strongly NP problems can be found here: <http://en.wikipedia.org/wiki/Category:Strongly_NP-complete_problems>

### Big O

*The* major concept to classify algorithms.
Expand Down
163 changes: 163 additions & 0 deletions bitcoin.md
@@ -0,0 +1,163 @@
# Bitcoin

<http://en.wikipedia.org/wiki/Bitcoin>

## Alternatives

Bitcoin is the most popular one today, but there are others:

- <http://en.wikipedia.org/wiki/Litecoin>
- <http://en.wikipedia.org/wiki/Ripple_%28payment_protocol%29>

## Specification

Bitcoin is a protocol.

TODO There seems to be no specification besides the source of the original implementation?

- <https://en.bitcoin.it/wiki/Protocol_specification> seems to be a high level view of the source, and as close as it gets to a standard. TODO who can edit that?

- <http://www.michaelnielsen.org/ddi/how-the-bitcoin-protocol-actually-works/> is a good intro, but starts too verbose. Jump directly to the "Bitcoin" section.

## General concepts

There is no centralized entity that verifies the money. That is why anarchists like Bitcoin. And why it's more efficient and makes part of baking useless.

If a transaction is done, everyone knows it, can attach it to an user ID, and where the money come from. This is interesting as it puts trust on everyone's eyes instead of that of a bank.

The question is of course who is behind the hash ID.

There is no entity that corresponds to a single bitcoin: only transactions. When you send a bitcoin, you are making a transaction, and saying from which other transaction the money comes from.

### Ledger

<http://en.wikipedia.org/wiki/Ledger>

## Specification Overview

This supposes you know what SHA-256 does.

Data is JSON encoded as follows (with abbreviated hex SHA-256 hashes):

{
"hash": "7c4025...",
"ver": 1,
"vin_sz": 1,
"vout_sz": 1,
"lock_time": 0,
"size": 224,
"in": [
{
"prev_out": {
"hash": "2007ae...",
"n": 0
},
"scriptSig": "304502... 042b2d..."
}
],
"out": [
{
"value": "0.31900000",
"scriptPubKey": "OP_DUP OP_HASH160 a7db6f... OP_EQUALVERIFY OP_CHECKSIG"
}
]
}

Where:

- `hash`: hash of the rest of the transaction. This is the ID of the transaction.

- `ver`: protocol version. The only one so far is `1`.

- `vin_sz`, `vout_sz`: number of inputs and outputs. This transaction has one of each, but more are possible. TODO.

- `lock_time`: `0` is the most common. TODO.

- `size`: length in bytes of the data that follows

- `in`:

List of transactions from which the money to be transfered comes from: every sent bitcoin comes from an older transaction.

Their valued is summed. Each input comes from an older transaction.

- `prev_out`:

- `hash`: hash of the transcation the money for this transaction comes from
- `n`: the number of output of that transacation that the money comes from, in this case the first, `0`

If you backtrack more and more, you will reach either of:

- <https://en.bitcoin.it/wiki/Genesis_block> which was necessary to start up the system, and must be treated specially.
- coinbase transaction (huge majority), which were bitcoins generated as miner rewards

- `scriptSig`:

- `304502`: TODO
- `042b2d`: TODO

- `out`:

- `value`: value being sent

The sum of the output values of the input transactions must be equal or greater than this.

If greater, the remainder goes to the miner who verified the transaction. TODO rationale?

To avoid losing money this way, you can create a transaction with 2 outputs:

- the real target with the correct address
- yourself for the rest

- `scriptPubKey`:

- `a7db6f`: destination address

You can trace transactions back

## Scripting

TODO

## Implementations

Bitcoin core is the reference implementation by the core developers.

## Tools

- <http://blockexplorer.com/> web UI for viewing bitcoin information

## Trivia

- <http://en.wikipedia.org/wiki/Satoshi_Nakamoto>

The mysterious creator(s) of bitcoin.

- <http://en.wikipedia.org/wiki/Mt._Gox>

Originally the domain and software were used for Magic the Gathering trading: it stands for MtG Onlilne eXchange.

Interesting how that was converted to become a bitcoin trading pole.

- <http://en.wikipedia.org/wiki/Winklevoss_twins>

After being involved with Facebook lawsuits winning 65M, they still went to the 2008 Olympics in China and in 2014 own 1% of the bitcoins! How many crazy things can you do in a lifetime?

- <http://www.coindesk.com/194993-btc-transaction-147m-mystery-and-speculation>

The largest bitcoin transaction so far, worth 150M at the time.

Commentators noted how such a transaction in regular currencies would be a logistic nightmare to make, while with Bitcoin it comes down to setting the value of an integer.

- <https://en.bitcoin.it/wiki/Laszlo%27s_pizza>

- <http://panampost.com/belen-marty/2014/10/07/couple-make-history-with-worlds-first-bitcoin-wedding/>

- Bitcoin based proof of knowledge at a given time without revealing it:

- <https://www.btproof.com/>
- <http://www.proofofexistence.com/>

- <http://en.wikipedia.org/wiki/SatoshiDice>

Bitcoin gambling website. Sold for 12M in 2013.
8 changes: 3 additions & 5 deletions p-vs-np.md
@@ -1,15 +1,13 @@
# P vs NP

The speed with which certain problems can be solved in terms of time and space
can be divided into two categories:
The speed with which certain problems can be solved in terms of time and space can be divided into two categories:

- polynomial
- non-polynomial: growth larger than any polynomial

The division is interesting because non-polyimial problems are *MUCH* harder to solve than polynomial ones.
The division is interesting because non-polynomial problems are *MUCH* harder to solve than polynomial ones.

Just consider the fact that on an exponential problem with $2^x$ complexit, increasing the problem size `x`
by one *doubles* the time it takes to solve the problem.
Just consider the fact that on an exponential problem with $2^x$ complexity, increasing the problem size `x` by one *doubles* the time it takes to solve the problem.

## P

Expand Down

0 comments on commit 5e3018f

Please sign in to comment.