PPT7-Bitcoin Scripts
PPT7-Bitcoin Scripts
Athira Jayavarma
Dept. of EEE
1
• A transaction is characterized by two parameters
• Describes how the public key and private key interacts during a transaction.
3
4
FORTH sample execution using RPN
5
First put 30 to the stack.Then 10 is also pushed into the stack.
Pop out the two top operand from the stack and perform the operation and after
operation,push it again in the stack
Equivalent C Code
int floor5(int n) {
if (n < 6) {
return 5;
} else {
return n - 1; }}
7
• FLOOR5: This is the name of the word being defined.
• (n—n`) :is used to describe what the word expects on the stack before execution and what it
leaves on the stack after execution.
In this case, "n" represents the input value, and "n`" represents the modified value that will be left
on the stack.
• DUP: This word duplicates the top value on the stack. After this operation, the stack contains two
copies of the same value.
• 6 < IF: This checks if the duplicated value on the stack is less than 6.
• DROP: If the value is less than 6, this word discards (removes) the top value from the stack. This
effectively removes the duplicated value.
• 5: If the value is less than 6, the code pushes the value 5 onto the stack.
• ELSE: If the value is not less than 6, the execution continues here.
• 1 -: This subtracts 1 from the duplicated value on the stack.
• THEN: This marks the end of the conditional block. It corresponds to the end of the IF-ELSE
construct.
8
Bitcoin Transactions and Input and Output
9
• Transactions in a bitcoin can be characterized by the input and corresponding output.
• You can represent the bitcoin transactions in the form of a series of inputs and outputs
• If one user Alice has received some 100 bitcoins, out of this100 bitcoins, Alice sends 40 bitcoins to Bob and
60 bitcoins to Charley. This transaction has one input which is coming to Alice from someone else and has
two outputs one output is going to the input of Bob and another output is going as input of Charley
• With the help of bitcoin script we want to actually ensure that this particular input corresponds to this
particular output
10
An example, Alice wants to make a certain transaction with Bob. Alice
initiated a transaction with a certain amount that contain the address of
bob and Alice
The transaction includes two parameters one is the Alice public key and
signature.
Alice has the private key with her and with that, she has generated this
particular signature and if Alice public key is available, Bob can verify the
signature and he can be sure that the transaction has actually been sent by
Alice and is a valid one.
What bitcoin has does that instead of sending public key and digital
signature, Bitcoin actually transfer some scripts.
11
• Traditionally bitcoin uses two scripts one is corresponding to the signature which is called
scriptsig and another script corresponds to the public key which is scriptpubkey.
• scripts don’t support all the operations like a loop and it was intentionally made simple
• A public key that, when hashed, yields the address of Alice embedded in the script
• A signature to provide ownership of the private key corresponding to the public key of Alice
12
Example of Bitcoin Script
14
15
16
17
18
19
20
21
• There are the OP_CODES that in the operation are the following:
• OP_DUP: Duplicate the item on the top stack.
• OP_HASH160: The input is encoded twice: first with SHA-256 and
then with RIPEMD-160.
• OP_EQUALVERIFY: Verify that the data entered is correct and valid.
• OP_CHECKSIG: The outputs, inputs, and script of the entire
transaction are summarized in a hash. The signature used must be a
valid signature for this hash and must be next to the public key.
Checksig is the particular operator that check a signature against the
public key to make sure they match.
22
• Now what happens during script execution is as follows:
1.First, the original public key of the owner (which is in the scriptSig) of
the funds is duplicated.
2.The duplicate public key then goes through a hashing process. In this
process, a hash is applied first SHA-256 and a RIPEMD-160 hash is
then applied to the result.
3.The result of the hashes is compared with the hash of the public key
that is in the scriptPubKey to make sure it is EQUALVERIFY (that is, it is
the same key and is verified as valid).
4.If it matches, the script continues to run and CHECKSIG is performed
to verify the signature with the public key.
23
Bitcoin Script Instructions
Total256 Opcodes(15 disabled,75 reserved)
• Arithmetic operations
• If-then conditions
• Logical operators
• Data Handling
Cryptographic Operations
• Hash Functions
• Signature verification
• Multi signature verification
24
25
26
27
• Locking script can be found in the output of a transaction and
unlocking script can be found in input of a transaction .
28
Join in a Bitcoin Network
To join in the bitcoin network, open up your wallet and immediately start send a
join message to join in the network.
if the old nodes are not responding for certain duration in generally it is for three
hours then they are automatically removed from the bitcoin network
whenever a new node comes and once to join in the network, there are certain
nodes in the network which work as the seed node.
The task of the seed node is to provide the initial information to the new nodes
who are going to join in the network 29
whenever you are trying to join in the network you send a message to one of the
seed node.
In response, this seed node will send you a set of addresses whom you can
consider as the peer .
Now, among that set of addresses which has been returned by the seed node;
you can select certain random number of nodes and you can create a kind of
peering relationship with those particular nodes and you can join in this overly
network.
Once you have joint in the network the first task of a node is to get the most
recent blockchain from your peer nodes and update your local copy of
blockchain. 30