noc20-cs01-lec10

IIT Kanpur July 201
20 followers
Jan 9, 2024
[Music]
[Music]
welcome back so we were discussing
Bitcoin scripting language and we ended
by saying that Bitcoin scripting
language is not turing-complete
and what what that means is that we
cannot express arbitrary functionality
with Bitcoin scripting language very
very simple things can be only expressed
with such a you know restricted language
now what happened is that we were when
the users of Bitcoin saw that the
limitations of this then the etherium
guys came in and they said that we need
a much more expressive language so they
created a new language called solidity
and in aetherium they started using
solidity to write this kind of complex
functions and those are called smart
contracts so smart contracts the Bitcoin
scripts are actually precursors of smart
contract and we'll see in in a few
within a few slides that the Bitcoin
script even though it's very restricted
still can express a number of things
that are very useful and interesting but
before that let's see what the Bitcoin
script execution looks like so let's say
I have this after concatenating the six
script
and then this script pop key I have this
so this part is actually part of the
script sig and this is the script pop
key and we have concatenated them so
this is my complete script and I want to
execute this in a stack-based machine so
what happens is that the the first no
now want things that are expressed with
the angular brackets or data because
this is just a name just a prototype
example so we don't have real data here
in the actual case there will be a
signature there will be a public key
which will be actually data right and
therefore and this will also be data and
rest our instructions right so so
whenever we are seeing these angular
brackets those are data
so as I said that when I encountered
data while executing I pushed them on
top of a stack so let's look at the
stack so when I encountered cig I will
put that on the stack now next I see
that there is another piece of data so I
have to put that on top of that so I
will put that also on top of the stack
the stack grows now I encounter an
instruction abduch so when I see up top
what it does is it duplicates the top of
the stack so when it duplicates the top
of the stack
without removing what was on top of the
stack some instructions remove the
entity on top of the stack but the
optically keeps the entity on top of the
stack so it creates another copy of this
key and we'll understand very soon why
it did that
now the next one is another instruction
this instruction is saying that hash
whatever is on the top of the stack
right so it is going to hash this top of
the stack so and this one replaces what
was on top of the stack so now we have
the hash of the public key on the top of
the stack now I encountered another data
so I put it on top of the stack right so
I have now this data and this data then
I have another instruction which says
equivalence verify so which means that
take the top two elements of the stack
this instruction semantics is that you
take the two elements on the top of the
stack and verify if they are equal if
they are equal then you know it will
return true which means those two things
will disappear from the stack but
nothing will be pushed on the stack so
now I have done that so now I have only
the public key and the signature and
then I encountered the next instruction
which says check the signature the
signature is done with the private key
so if this public key is the correct
public key for this private key then
we'll be getting verification otherwise
the verification will fail so if
assuming that the verification passes
then it is going to be empty
or it will be all true and then I have
succeeded in executing this entire
script
now let's little look at this at a
little bit more closely so the guy who
is spending the money he does a
signature and he gives his public key
because without the public key one
cannot change a check the signature so
he gives this public key
now if the users want they can check
from other other places because public
key is a public information that this is
indeed his public key right so it puts
the public key and he signs with the
private key so this is the the input
part of the transaction gives the output
part of the transaction is supposed to
only give you the public key hash right
of the person who is supposed to get the
money what it does is that it I'm going
to check whether this public key hash
matches with the public key of the
recipient so for this what I'm going to
do is that I'm going to duplicate the
public key and then I will make a hash
and I am going to compare it and then
I'm going to check verification of the
Equality and if they match then we know
that this is the correct hash and then I
will check the signature I have the
public key I have the signature so I can
check the signature so this is how this
thing actually makes sure that the
spender of the money is indeed the
correct spender has the authority to
spend that money now remember one thing
I forgot to tell you is that you
actually take the this part so when you
put together this you take this from
your current transaction and this part
you take from the previous transaction
why because see this is this is the part
see current transaction in the input
part I put this I signed it and this is
the in the output part of the previous
transaction which I am trying to use
right so this is not the output of the
current transaction this is
output of the previous transaction and
this is and the transaction which is
being used for as input and this part is
from the input so in the input I put my
signature saying that I am referring to
so answer transaction on which so-and-so
output 0 or 1 or 2 whatever is I am
going to redeem so what this has to do
is that it has to take that
corresponding outputs from the previous
transaction corresponding outputs output
scrape up script script pop key and then
concatenate them together so it's not
that the input of this and the output of
the current one are input and output of
the current transaction are concatenated
it's the input part of the current
transaction and this part is this part
is from the output of the transaction
referred in the input so that is very
important otherwise you will be confused
when we when you try to understand this
part ok so this is the how the execution
happens so it's completely stack based
and the script Bitcoin script
instructions 8 bits long and so there
are 2 to the 8 that is 256 different
possible op codes but right now there
are 15 of codes are not used they are
disabled and 75 were resolved but those
of course haven't given any
functionality yet so so out of 256 you
subtract 90 so you have 166 possible
upwards so some of the op codes are just
arithmetic in our example who didn't see
any arithmetic also some if then some
logical logic or data handling and
encrypted functions like hash computing
hash verifying signature verifying
multiple signature things like that so
that's what the very very simple simple
and small language so multisig is an
important opcode so what it does is that
it specifies in public keys and specify
a number T
and if if you want to verify joint
signatures you required that T of them
should verify right so T not all n
necessarily have to be signing it
signing to verify it but at least T of
them should verify now if you specified
TS n then then you require all the in
entities to sign the the transaction for
the transaction to be valid now there is
a bug which was there and in the
beginning that there was an extra data
value that is popped from the stack and
it is ignored when the op multis
multisig is executed remember each up
code when we as when I get the opcode I
often get the top of the stack or to top
things on the stack or three stop things
on the stack and use it for executing
the instruction but in this case they
are necessarily a pop another extra
value and therefore you when you write
your script you have to add that repeat
that data value that that gets
unnecessarily popped which is an
annoyance but it can be done so in
practice most nodes whitelist known
scripts with 160 or so of course you can
write many many scripts some of them
actually was aren't probably meaningless
but you can write a lot of scripts some
of the scripts are maybe malicious doing
something that you wouldn't want to do
and you don't know that it is malicious
so most nodes what they do is that they
whitelist a number of scripts and if the
script if any script defers from there
whitelisted set of scripts then they
actually don't even validate the
transaction and declared the transaction
to be invalid 99.9 percent scripts are
actually simple signature check the one
that we saw and we executed on the
slides and about one in hundred is
multisig script and there is another
thing called paid to script hash that
we'll discuss shortly that is another
part another
of script that we see about one in
hundred and then remainders are our
errors and something called proof of
burn so we'll also see what that means
so proof of burnt is an instruction so
what is a burnt
burn means that you are burning money
right so if you take some Bitcoin as
inputs and then you say that but you
don't specify who gets it but they but
the script executes fine then that
Bitcoin becomes eerily irredeemable
which means that you cannot redeem it
ever again because once you have used it
as input you cannot use it as input to
another transaction on the other hand in
this transaction you don't create an
output so you cannot refer to that
output to do the next transaction so
that that Bitcoin bit goes but you know
nowhere right so nobody can use it so we
say that we have burned that that amount
of Bitcoin so why why would I need to do
that the the reason is that the way the
proof of one happens is that you there
is an opcode called op return which
returns without error right now we said
that if when I when I execute this
script on this stack based machine then
if I have no errors then we assume that
the transaction is valid right so in
this case the input goes away but the
output is not gone is not going to
anyway anybody
so therefore the money is gone but what
happens is that op return is such that
anything written after the operator is
ignored because operatin basically
returns the script from execution so you
can put arbitrary data after the
operator and then the transaction will
be valid and then this transaction will
be eventually going to a block and will
become permanent in the blockchain so if
you want to keep some data forever into
the blockchain then you use a proof of
burn you pay a very small amount of
Bitcoin maybe point zero zero zero zero
one amount of Bitcoin burn it and then
basically H the data forever in one of
the blocks right so many times you will
hear that
nowadays giving like your course
certificate on the Bitcoin blockchain
this is how this is done this data about
your certificate will be actually etched
into a transaction using a proof of burn
transaction and then it will be in some
block and so if somebody wants to in
future wants to check if you have the
certificate they can actually go into
the Bitcoin blockchain
and retrieve that information and since
this is supposed to be non temporal and
supposed to be permanent then this is a
proof that you really got the
certificate and you didn't Forge this
certificate so that's how this is done
so there is another issue that we saw
that the script that we saw as the
script pop key was pretty long and not
very long but it was long and it has
multiple instructions and data values in
the right places and so on
now suppose suppose you are working with
the merchant and you buy stuff from him
and then one day he says that look we
have changed ownership and we now now
have multiple owners now we actually
need to sign three of us need to sign
before you're gamed right so you have to
send the money to a multi-sig address
right now now you get confused because
you are the sender and you you you are
used to just regular writing scripts
which basically looks like what we just
saw it's a check
Sig type of thing when it's multisig you
don't know so you will be confused and
if you make a mistake in writing the
script then that's where the money
cannot be redeemed by the merchant so
what the merchant can do is that he can
give you a script and he but he cave the
way he does it is that he writes a
script and puts this puts a hash pointer
to that script so when you send the
money you don't have to write a long
script all you have to do is to actually
send the money to that hash so this is a
technique that was introduced later
in in blockchain it was not there in the
beginning and this is called a way to
script hash type of payments right so
instead of paying it to a regular
address a person's address you are
sending it to a hash value which looks
like a regular address so you from
sender's point of view there is no
change but from the perspective of the
other side you actually have a much more
complex script that you are hiding from
the sender so what you do is that you
put the hash of the redemption script
and then you check the redemption script
and if the hash matches so this part
checks whether the redemption script to
which you are sending is indeed the
redemption script that you meant this is
all you have to write like you did in
case of a regular address so it could
have been a regular address and you
would have written the same thing right
you would have written the OP hash and
you would have written the hash of the
redemption script and then equal right
and then so then you do that pop key
then signature check and all that stuff
which was you you would have done anyway
so but what happens is that after you
have done this the actual script is
replaced and that script needs to be
executed before the actual the the
interest enter script finishes with with
with success so that's how this is this
works and this is called a pay to script
hash so the basic idea is that you you
actually have to the the receiver has a
complex business situation where he
needs a payment to be made to a through
a complex script but you don't want the
burden of writing a complex script so he
makes it easier for you by writing a
script and tell you the hash of the
script and then you basically use the
hash of the script as if you are sending
to a regular regular address which also
is a hash of once you know a size 160 so
so that's all that there is to this pay
to script hash so that's how it works
when you're ready to pay the
purchase this guy's give you an
basically a hash value as an address you
use that and that's what what the p2
script hash means now what we'll see is
that even though the language of bit
conscripting lang is very simple even
with that we can do a lot of smart
things right and what we do with with
those is called a sort of the precursor
of smart contracts so we can use scripts
to do multiple things but we'll show you
a couple of things so the first thing we
want to show you is the escrow
transaction and then we'll also see how
to do micro payments and then we'll show
you how to do a green payment so these
are the three examples we'll show but
there are many others that you can do
with this scripting language but as we
said that since this is not a Turing
complete language we cannot do a very
complex thing with this and therefore we
have a we have to have a you know Turing
complete language if we want to do very
very general things with with the help
of blockchain and so let's see how what
is in a scroll transaction so let's say
Alice wants to buy something online from
Bob and Alice doesn't want to pay until
Bob ships and shows the proof of
shipment but the Bob doesn't want to
ship until Alice pace so normally when
we buy something from Amazon what we do
is that we pay it by credit card and
then they actually ship the item and
then we can complain to the credit card
company if the item is not good and if
we trust the merchant then actually we
can even complain to the merchant
himself in case like like Amazon and
maybe sir we can get a refund or
something but in this case what have a
Skrill transactions do is that it allows
you to involve a trusted third party to
keep them money on a screw and then if
both sides trust that third party then
the Bob the merchant would know that the
even though he did not get the money it
is put in escrow
and a and the the scope keeper is
trustworthy so in case there is a
problem the screw keeper will pay the
money to Bob so what happens is that
Alice writes a transaction that says
that that sends it to an escrow account
and it's it's a multi-sig instruction
which says that it requires two out of
three and it Alice Bob and Judy who is
the adjudicator or who is the with the
escrow person so now Bob knows that you
you have put your money in the escrow so
he ships the item to Alice now in a
normal case suppose Alice gets the item
and is happy and they are both honest so
then the what the Alice says is that use
this transaction of the escrow
transaction as input and create a new
transaction that is given to Bob and
it's a signed by both Alice and Bob
right so so that's that's the normal
case now suppose what Alice gets is is
broken or has some problem or it never
reaches then there is a dispute and then
Judy has to decide who is at fault if
juice Duty decides that Bob is at fault
then she will sign this and then we'll
get Alice will get the money if Alice is
at fault then Bob will get money right
so this is for how the escrow
transaction works so you are basically
putting the firm money in the in an
escrow until both sides are satisfied it
remains in escrow if there is a no
dispute then normally two sides sign
it's a two out of three signature if
there is a dispute then Judy has to
decide and Judy will sign with whichever
party should get the money now there is
another thing called Green addresses so
in green addresses what happens is that
a similar problem Alice wants to pay Bob
but an Alice makes the payment
transaction but Bob
cannot wait for six verifications six
remember like we said that there has to
be six blocks built on top of the block
that that contains this transaction in
the blockchain in order to be you know
probabilistically not sure so and also
Bob doesn't want that Alice then goes
around and does a double spending so all
this stuff or Bob may be completely
offline and and he has no way to check
what's happening
so what Alice can do he can go to third
party in in this case we call it a bank
or it could be a cryptocurrency exchange
some entity that what they do is that
and they have a relation with Alice the
trustee Alice or Alice might have a lot
of money in the bank or lot of
cryptocurrency in the exchange or
whatever so Alice says could you make
out a green payment to Bob and then a
bank says okay I will make a green
payment to Bob so what what it does is
it puts a transaction that pay to Bob
and why to bank the why to bank parties
for maybe for fees that it charges Alice
or whatever and then Alice will then
make a transaction with the bank to
settle this and then it gives the
guarantee of no double spent because Bob
actually doesn't know Alice well so
Alice
he cannot trust and therefore even if
Alice paid X to Bob and it comes as a
transaction on the network Bob doesn't
know that Alice will not now use the
same input to another transaction and
and give it to her give the money to
herself so that would be a double spin
whereas bank is a more trusted entity so
it it probably won't do double spend
because it worries about its reputation
also it may have a some kind of a
certificate that says that you know it
hasn't made a double spent transaction
in so many years or whatever so Bob
cross the bank and Bob then ships the
item now what happened in the double
spend case is that is that the exchanges
when they started this green address
idea people were excited because let's
say you are
food so the margin cannot wait for one
r46 verifications to deliver your food
so Greene addresses idea was very
welcome so that the with the Bitcoin
transactions also you can buy things
which can be delivered fast on the other
hand the merchant is also guaranteed to
be paid eventually even though he you
may not know the customer but turns out
that mound ox we talked about in in in
in a very beginning mounted ox was one
of the exchanges that used to provide
these green addresses and green payments
and and there were other few other but
they all actually have ran out of
business so green addresses nowadays is
not very common because it didn't really
work out for other reasons mostly
outside the technology reason but so
green addresses and a good idea but it
didn't really work that well the third
thing that we want to talk about is
efficient micro payments so what is the
micro payments a say say a bob is mobile
service provider and it charges a list
by the minute right so when alice is
making a call then one possibility is
that alice Alice's whatever client she
is running as the minutes tick she pays
Bob you know payment for one minute and
then make another payment for second
minute and another payment for third
minute but every time she does that
first of all she will put too many
transactions on the network because this
scenario could be very common if it is
let's say Airtel and analysis an Airtel
user and therefore this is not very
efficient second thing is that the Alice
might incur fees part transaction right
so she doesn't want to do so many
transactions so she has to pay more fees
it may be like you know per minute
payment may be less than the payment for
the for putting transactions into blocks
so therefore the idea of micro payments
came into existence so what we do is
Alice initially pays an amount Y amount
let's say 100 or the maximum that Alice
thinks that she would need to make this
phone call from her whatever input
transaction that she's trying to redeem
and then it's a multi-sig and it is only
signed by Alice and this is not signed
by Bob so therefore this is not going to
be redeemed yet and then per minute a
new transaction is created which is
signed by Alice Bob doesn't sign them so
now we are doing a number of double
spins so this is your X transaction and
this is kind of like you made this
payment and then from this transaction
you're redeeming this transaction
although this transaction is not
completed yet you're redeeming this
transaction with one - Bob and 99 - LS
and then next minute you pay 2 - Bob 98
- Alice and so you are doing lot of
double spins but none of them are being
redeemed or they are actually not even
put into the blockchain because all of
this this cannot go through because
they're referring to the same
transaction and and doing various
different things and they're all double
spins but at the end when Alice says I'm
done and Bob says okay I'll publish the
transaction on the blockchain so what he
does is that he signs the last one of
them and then publishes it so what
happens now is that the last amount that
is due to Bob so say let's say it was 42
minutes so then Bob gets 42 and
fifty-eight goes back to Alice so all
the other previous transactions are not
put on the blockchain and therefore and
even if Bob tries to put them on the
blockchain he don't work because it will
be a double spin all these transactions
are referring to this transaction that's
why it's a double spend as we explained
before so now there is a possibility
that Bob never signs this transaction
now what that means is that Alice will
lose this 58 also she paid hundred
before and then now she has this 58 this
one was actually put into the blockchain
but it was not you know signed by
bomb but now Elise has a possibility of
losing the b-58 so therefore there is a
thing called timed refund transaction so
in a time different transaction Allie's
demand that when she does this one at
least demands that the Bob does another
one in which Bob signs and says @p
hundred to Alice and this is remembered
the lock time the lock time is what we
use here and therefore the lock time
will be set to some T appropriate T so
that if Bob refuses to sign this one
then Alice can above already sign this
so Alice can sign this and get all 100
back so that's where the lock time think
that we saw before is so usually we put
lock times zero for immediate use of the
transaction but here we put a time T and
therefore we Alice can get back all the
money after time T we'll come back to
this in the next session so for now we
have seen three different use of Bitcoin
scripts to do more complex transaction
than just paying from one one person to
the other
[Music]
[Music]
[Music]
