0% found this document useful (0 votes)
9 views

Lab02Slide

Uploaded by

pmnhung21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab02Slide

Uploaded by

pmnhung21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Bitcoin Script

Technical overview

[email protected]

Knowledge Engineering Department

December 19, 2024

ndhy (Knowledge Engineering Department) Bitcoin Script 1 / 15


Introduction to Bitcoin Script

Bitcoin Script is a stack-based programming language used by Bitcoin.


It is not Turing complete, which increases security by preventing
infinite loops.
Used for defining transaction processing rules.

ndhy (Knowledge Engineering Department) Bitcoin Script 2 / 15


Structure of a Bitcoin Transaction

Transaction Input: References an existing unspent transaction


output (UTXO).
Transaction Output: Contains the amount and a locking script
(scriptPubKey) defining spending conditions.
Unlocking Script (scriptSig): Provided by the spender to satisfy
the conditions of the locking script.

ndhy (Knowledge Engineering Department) Bitcoin Script 3 / 15


Bitcoin Script Basics

Stack Operations: Operations manipulate data on a stack.


Opcodes: Instructions that perform various operations.
Examples:
OP DUP
OP HASH160
OP EQUALVERIFY
OP CHECKSIG

ndhy (Knowledge Engineering Department) Bitcoin Script 4 / 15


Example Script Execution
Funding Transaction (scriptPubKey)

OP DUP OP HASH160 <PubKeyHash> OP EQUALVERIFY OP CHECKSIG

OP DUP: Duplicates the top stack item.


OP HASH160: Hashes the top stack item twice.
<PubKeyHash>: Public key hash from the recipient.
OP EQUALVERIFY: Verifies that the top two stack items are equal,
removes them if true.
OP CHECKSIG: Verifies the transaction signature.

ndhy (Knowledge Engineering Department) Bitcoin Script 5 / 15


Example Script Execution
Spending Transaction (scriptSig)

<Signature> <PublicKey>

<Signature>: Signature created by the sender’s private key.


<PublicKey>: Sender’s public key.

ndhy (Knowledge Engineering Department) Bitcoin Script 6 / 15


Step-by-Step Verification Flow

1 Initialization:
Combine the scriptSig and scriptPubKey.
Resulting script: <Signature> <PublicKey> OP DUP OP HASH160
<PubKeyHash> OP EQUALVERIFY OP CHECKSIG.
2 Execution:
Push <Signature> onto the stack.
Push <PublicKey> onto the stack.
OP DUP: Duplicate the public key.
OP HASH160: Hash the duplicated public key.
Push <PubKeyHash> (from script) onto the stack.
OP EQUALVERIFY: Check if hashes are equal.
OP CHECKSIG: Verify the signature.

ndhy (Knowledge Engineering Department) Bitcoin Script 7 / 15


Script Execution Example
Detailed Steps

scriptPubKey:
OP DUP OP HASH160
ab6809ae7b19d5f785e899017cfa6dd1e30f8c3f OP EQUALVERIFY
OP CHECKSIG
scriptSig:
3045022100d0a8b046f7e465fa02b9cba8f0-
90b1f8b76b0b6f93eacff33b8bcf84d5767e-
1d02202e5044313fa76ebcaddf49e7a50947-
2abf1d6b21f53101d5f243a2ff7a0d2688
04b0bd634234abbb1ba1e986e884185c6b38-
90bc5f40e6db70a0ad34a4f5a02c276de0b0-
b91a0da7e6d23243adfc6cdd99d0d96a50a6-
1138a334ce37e3a4d1b8b

ndhy (Knowledge Engineering Department) Bitcoin Script 8 / 15


Script Execution Example
Execution Steps

1 Push 3045022100d0a8b046... (signature) onto the stack.


2 Push 04b0bd6342... (public key) onto the stack.
3 OP DUP: Duplicate the public key.
Stack: [signature, public key, public key]
4 OP HASH160: Hash the duplicated public key.
Stack: [signature, public key, hash160(public key)]

ndhy (Knowledge Engineering Department) Bitcoin Script 9 / 15


Script Execution Example
Execution Steps

1 Push ab6809ae7b19d5f785e899017cfa6dd1e30f8c3f (expected


hash) onto the stack.
Stack: [signature, public key, hash160(public key),
ab6809ae7b19d5f785e899017cfa6dd1e30f8c3f]
2 OP EQUALVERIFY: Compare the two topmost items.
If equal, continue. Stack: [signature, public key]
If not, script execution fails.
3 OP CHECKSIG: Verify the signature using the public key.
If valid, the script returns true, and the transaction is accepted.
If invalid, the script returns false, and the transaction is rejected.

ndhy (Knowledge Engineering Department) Bitcoin Script 10 / 15


Advanced Bitcoin Scripts
Multi-signature (multisig): Require multiple signatures to spend.
Timelocks (CheckLockTimeVerify and CheckSequenceVerify):
Restrict spending until a certain time or condition.
Pay-to-Script-Hash (P2SH): Allows complex scripts to be hidden
behind a single address.

Figure: P2SH (Magnus Hensley )

ndhy (Knowledge Engineering Department) Bitcoin Script 11 / 15


Advanced Bitcoin Scripts
Multi-signature (multisig): Require multiple signatures to spend.
Timelocks (CheckLockTimeVerify and CheckSequenceVerify):
Restrict spending until a certain time or condition.
Pay-to-Script-Hash (P2SH): Allows complex scripts to be hidden
behind a single address.

Figure: P2SH for MultiSig (Magnus Hensley )

ndhy (Knowledge Engineering Department) Bitcoin Script 12 / 15


Comparison with Ethereum’s Solidity

Bitcoin Script is simpler and more secure due to non-Turing


completeness.
Solidity allows for more complex smart contracts but with higher risk
of vulnerabilities.

ndhy (Knowledge Engineering Department) Bitcoin Script 13 / 15


Submission guidelines

Contents:
Source code in <Code> folder.
Project report (Report.pdf).
Format: Group’s ID.zip

ndhy (Knowledge Engineering Department) Bitcoin Script 14 / 15


Q&A

Questions?

ndhy (Knowledge Engineering Department) Bitcoin Script 15 / 15

You might also like