Академический Документы
Профессиональный Документы
Культура Документы
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
1
Cryptocurrency Technologies Mechanics of Bitcoin
time
might need
Create 25 coins and credit to AliceASSERTED BY MINERS
to scan
Transfer 17 coins from Alice to BobSIGNED(Alice)
backwards
until
Transfer 8 coins from Bob to CarolSIGNED(Bob)
genesis!
is this
Transfer 15 coins from Alice to DavidSIGNED(Alice)
valid?
time
1
Inputs: Ø
Outputs: 25.0→Alice
we implement this
change with hash pointers
2
Inputs: 1[0]
address
SIGNED(Alice)
finite scan
3
Inputs: 2[0]
to check
Outputs: 10.0→Carol, 7.0→Bob
for validity
SIGNED(Bob)
4
Inputs: 2[1]
is this
Outputs: 6.0→David, 2.0→Alice
valid?
SIGNED(Alice)
2
Cryptocurrency Technologies Mechanics of Bitcoin
Merging Value
time
1 Inputs: ...
SIGNED(Alice)
..
2. Inputs: 1[1]
SIGNED(Alice)
..
3. Inputs: 1[0], 2[1]
Outputs: 19.0→Bob
SIGNED(Bob)
Joint Payments
time
1
Inputs: ...
SIGNED(Alice)
..
2
.
Inputs: 1[1]
SIGNED(Alice)
..
3
.
Inputs: 2[0], 2[1]
two signatures!
Outputs: 8.0→David
SIGNED(Carol), SIGNED(Bob)
3
Cryptocurrency Technologies Mechanics of Bitcoin
{
"hash":"5a42590fbe0a90ee8e8747244d6c84f0db1a3a24e8f1b95b10c9e050990b8b6b",
"ver":1,
"vin_sz":2,
metadata
"vout_sz":1,
"lock_time":0,
"size":404,
"in":[
{
"prev_out":{
"hash":"3be4ac9728a0823cf5e2deb2e86fc0bd2aa503a91d307b42ba76117d79280260",
"n":0
},
"scriptSig":"30440..."
},
input(s)
{
"prev_out":{
"hash":"7508e6ab259b4df0fd5147bab0c949d81473db4518f81afc5c3f52f91ff6b34e",
"n":0
},
"scriptSig":"3f3a4ce81...."
}
],
"out":[
{
"value":"10.12287097",
output(s)
"scriptPubKey":"OP_DUP OP_HASH160 69e02e18b5705a05dd6b28ed517716c894b3d42e
OP_EQUALVERIFY OP_CHECKSIG"
}
]
}
{
transaction hash
"hash":"5a42590...b8b6b",
"ver":1,
housekeeping
"vin_sz":2,
"vout_sz":1,
“not valid before”
"lock_time":0, more on lock_time later...
housekeeping
"size":404,
...
4
Cryptocurrency Technologies Mechanics of Bitcoin
"in":[
{
"prev_out":{
previous
"hash":"3be4...80260",
transaction
"n":0
},
signature
"scriptSig":"30440....3f3a4ce81"
},
...
(more inputs)
],
"out":[
{
output
value
"value":"10.12287097",
recipient "scriptPubKey":"OP_DUP OP_HASH160 69e...3d42e
address??
OP_EQUALVERIFY OP_CHECKSIG"
},
more on this soon...
...
(more
outputs)
]
5
Cryptocurrency Technologies Mechanics of Bitcoin
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
OP_DUP
OP_HASH160
69e02e18...
OP_EQUALVERIFY OP_CHECKSIG
6
Cryptocurrency Technologies Mechanics of Bitcoin
Sig-script
30440220...
0467d2c9…
OP_DUP
PubKey-script
OP_HASH160
69e02e18...
OP_EQUALVERIFY OP_CHECKSIG
Why Scripts?!
7
Cryptocurrency Technologies Mechanics of Bitcoin
Design “goals”:
– Simple, compact
– Stack-based
I am not
impressed
– No looping
– Limits on time/memory
30440220...
0467d2c9…
✓
<pubKeyHash?>
OP_DUP
<pubKey>
<pubKeyHash>
OP_HASH160
69e02e18...
<pubKey>
OP_EQUALVERIFY OP_CHECKSIG
<sig>
true
8
Cryptocurrency Technologies Mechanics of Bitcoin
• Arithmetic
• If/then
the Bitcoin language and one has to deal with it by putting an extra dummy variable onto the stack.
The bug• wasLogic/data handling
in the original implementation, and the costs of fixing it are much higher than the
damage it causes, as we’ll see later in Section 3.5. At this point, this bug is considered a feature in
Bitcoin,• inCrypto!
OP_EQUALVERIFY Returns true if the inputs are equal. Returns false and marks the
transaction as invalid if they are unequal
OP_CHECKSIG Checks that the input signature is a valid signature using the input public
key for the hash of the current transaction
OP_CHECKMULTISIG Checks that the k signatures on the transaction are valid signatures from
k of the specified public keys.
Executing a script. To execute a script in a stack-based programming language, all we’ll need is a stack
that we can push data to and pop data from. We won’t need any other memory or variables. That’s
what makes it so computationally simple. There are two types of instructions: data instructions and
opcodes. When a data instruction appears in a script, that data is simply pushed onto the top of the
stack. Opcodes, on the other hand, perform some function, often taking as input data that is on top of
the stack. OP_CHECKMULTISIG
Now let’s look at how the Bitcoin script in Figure 3.5 is executed. Refer to Figure 3.7, where we show
the state of the stack after each instruction. The first two instructions in this script are data
Built-in support for joint signatures
instructions — the signature and the public key used to verify that signature — specified in the
scriptSig component of a transaction input in the redeeming transaction. As we mentioned, when we
see a data instruction, we just push it onto the stack. The rest of the script was specified in the
scriptPubKey component of a transaction output in the referenced transaction.
Specify t
duplicate instruction, OP_DUP, so we just push a copy of the public key onto the top
First we have the
of the stack. The next instruction is OP_HASH160, which tells us to pop the top value, compute its
cryptographic hash, and push the result onto the top of the stack. When this instruction finishes
executing, we will have replaced the public key on the top of the stack with its hash.
9
Cryptocurrency Technologies Mechanics of Bitcoin
Proof-of-Burn
OP_RETURN
<arbitrary data>
10
Cryptocurrency Technologies Mechanics of Bitcoin
purchases!
Bob
Alice
• ensures the redeem script hashes to the same value as the script
hash Alice put in her output;
• letting Bob spend the output if the redeem script does not return
false.
11
Cryptocurrency Technologies Mechanics of Bitcoin
<signature>
<signature>
<<pubkey> OP_CHECKSIG>
OP_HASH160
<pubkey>
<hash of redemption script>
OP_CHECKSIG
OP_EQUAL
Pay-to-Script-Hash
purchases!
12
Cryptocurrency Technologies Mechanics of Bitcoin
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
(disputedcase)
(normal case)
Bob
Pay x to Alice
Judy
SIGNED(ALICE,JUDY)
SIGNED(ALICE, BOB)
To: Alice
From: Bob
Alice
PROBLEM: Alice of
Pay x to 2-of-3 wants toBob,
Alice, buy Judy
online from Bob. Bob
(MULTISIG)
SIGNED(ALICE)
13
Cryptocurrency Technologies Mechanics of Bitcoin
payment to Bob?
cage
SIGNED(BANK)
Alice
PROBLEM: Alice wants to pay Bob.
Bob
could be ...
spends!
SIGNED(ALICE)___________
I’ll
done!
x; Pay 03 to Bob, 97 to Alice
SIGNED(ALICE)___________
SIGNED(ALICE)___________
SIGNED(ALICE)___________
Alice
Input: y; of
minute Payphone service.
100 to She (MULTISIG)
SIGNED(ALICE)
14
Cryptocurrency Technologies Mechanics of Bitcoin
SIGNED(ALICE)___________
SIGNED(ALICE) SIGNED(BOB)
Bob
Alice
Input: y; Pay 100 to Bob/Alice (MULTISIG)
SIGNED(ALICE)
lock_time
{
"hash":"5a42590...b8b6b",
"ver":1,
"vin_sz":2,
"vout_sz":1,
"lock_time":315415,
"size":404,
...
Block index or real-world
timestamp before which this
}
15
Cryptocurrency Technologies Mechanics of Bitcoin
Multiplayer Lotteries
Coin-swapping Protocols
“Smart Contracts”
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
16
Cryptocurrency Technologies Mechanics of Bitcoin
Bitcoin Blocks
prev: H(
) prev: H(
) prev: H(
)
trans: H(
) trans: H(
) trans: H(
)
Hash tree
(Merkle tree)
H( ) H( )
of transactions
in each block
H( ) H( )
H( ) H( )
transaction
transaction
transaction
transaction
17
Cryptocurrency Technologies Mechanics of Bitcoin
"hash":"00000000000000001aad2...",
"ver":2,
"prev_block":"00000000000000003043...",
"time":1391279636,
block header
"bits":419558700,
"nonce":459459841,
"mrkl_root":"89776...",
"n_tx":354,
"size":181520,
"tx":[
...
],
"mrkl_tree":[
transaction data
"6bd5eb25...",
...
"89776cdb..."
hash
"hash":"00000000000000001aad2...",
"ver":2,
"prev_block":"00000000000000003043...",
timestamp
"time":1391279636,
hashed
indication of difficulty
"bits":419558700,
during
chosen nonce
"nonce":459459841,
mining
...
}
not
hashed
18
Cryptocurrency Technologies Mechanics of Bitcoin
coinbase Transaction
"in":[
{
"prev_out":{ Null hash pointer
redeeming "hash":"000000.....0000000",
nothing
"n":4294967295
},
First ever coinbase parameter:
arbitrary
"coinbase":”…”
“The Times 03/Jan/2009 Chancellor
}
on brink of second bailout for
banks”
]
"out":[
{
"value":"25.03371419",
"scriptPubKey":"OPDUP OPHASH160 ... ”
}
block reward + transaction fees
19
Cryptocurrency Technologies Mechanics of Bitcoin
20
Cryptocurrency Technologies Mechanics of Bitcoin
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
Participants can
– publish transactions
The network:
21
Cryptocurrency Technologies Mechanics of Bitcoin
1
ready to Bitcoin!
getaddr()
getaddr()
1, 7
getaddr()
Already
heard
5
that!
A→B
A→B
A→B
A→B
New
tx!
3
6
A→B
A→B
A→B
2
A→B
A→B
A→B
A→B
22
Cryptocurrency Technologies Mechanics of Bitcoin
– Avoid double-spends
New
tx!
A→C
A→C
5
A→B
1 A→C
A→C
A→C
7
A→B
A→C
A→B
A→B
6
A→B
A→B
2
A→B
A→B
23
Cryptocurrency Technologies Mechanics of Bitcoin
Race Conditions
Orphaned Blocks
24
Cryptocurrency Technologies Mechanics of Bitcoin
Block Propagation
– Avoid forks
Source: Yonatan Sompolinsky and Aviv Zohar: “Accelerating Bitcoin’s Transaction Processing” 2014
25
Cryptocurrency Technologies Mechanics of Bitcoin
• Estimates-up to 1M IP addresses/month
– Permanently connected
Fully-validating Nodes:
– Fully-validating
• Permanently connected
Storage Costs
26
Cryptocurrency Technologies Mechanics of Bitcoin
27
Cryptocurrency Technologies Mechanics of Bitcoin
Software Diversity
– BitcoinJ (Java)
– Libbitcoin (C++)
– btcd (Go)
Mechanics of Bitcoin
• Bitcoin Transactions
• Bitcoin Scripts
• Bitcoin Blocks
28
Cryptocurrency Technologies Mechanics of Bitcoin
• 1 M bytes in a block
With
at least 250 bytes/transaction
7 transactions/sec!
Compare to:
29
Cryptocurrency Technologies Mechanics of Bitcoin
30
Cryptocurrency Technologies Mechanics of Bitcoin
1 Block 23
24
Block 23
Block 23
24
That’s
crazy 8
That’s
talk!!
Block 23
24
crazy
24
talk!!
I found a
nifty new 3
6
24
Block 23
24
Block 23
block!
2
Block 23
24
24
Block 23
24
31
Cryptocurrency Technologies Mechanics of Bitcoin
Soft Forks
<signature>
<<pubkey> OP_CHECKSIG>
OP_HASH160
<hash of redemption script>
OP_EQUAL
Old nodes will just approve the hash, not run the embedded script.
32
Cryptocurrency Technologies Mechanics of Bitcoin
Hard Forks
• New op codes
33