How does the CHECKSIGADD opcode (defined in the Tapscript BIP) BIP342 work? How does it compare to its predecessor CHECKMULTISIG?
The design of CHECKSIGADD was discussed in this StackExchange question.
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.Visit Stack Exchange
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.Sign up to join this community
James Chiang explained how the CHECKSIGADD opcode works in this Bitcoin Optech workshop. (The code for CHECKSIGADD in the Bitcoin Core codebase is here.)
As you can see on the left we have an output script that uses CHECKSIGADD and CHECKSIG opcodes to express a n-of-n multisig output.
pk0 checksig pk1 checksigadd pk2 checksigadd 3 equal
This is our initial stack which is populated with 3 signatures:
sig0 sig1 sig2
In the first step we will push this public key (pk0) onto the stack and evaluate that against sig0 with the opcode CHECKSIG. Let’s assume this evaluates to TRUE. Since it evaluates to TRUE we will now have a
1byte on the top of the stack:
1 sig1 sig2
We will use a counter.
As we evaluate the signatures against other public keys, if that script evaluates successfully we will then have a counter of
3on the stack and that counter can then be compared to the one encoded in the output script. If they are equal we have successfully spent a 3-of-3 multisig output which uses CHECKSIGADD to encode that multisig.
How does it compare to its predecessor CHECKMULTISIG?
CHECKMULTISIG doesn't (it is still used for SegWit v0 spends) use an increasing counter. The equivalent locking script using CHECKMULTISIG would be:
3 pk0 pk1 pk2 3 checkmultisig
CHECKMULTISIG also contains a longstanding bug which means the unlocking script requires a
0 at the bottom to satisfy the locking script.
sig0 sig1 sig2 0
The operation of CHECKMULTISIG is explained in this StackExchange question.