I am interested in the math and code behind bitcoin multisignature technology. I've been looking for actual code that creates a multisignature address/pub key, and verifies transactions/digital signatures; that can be executed in one go. I didn't find anything in bitcoin core maybe because I dont know where to look. Does anyone know where I can find the actual code that creates a multisignature adress and verifications. I would also like to know if there are any m of n schemes written in code and the math explanations behind adress /pub key creation and signature verification.
Multisig bitcoin transactions have a fairly simple implementation and don't require any advanced cryptography knowledge beyond the regular ECDSA used for P2PKH transactions. There are no "mutltisig address" or "multisig pubkey", but regular public keys and regular signatures are used. A multisig transaction output essentially contains a list of public keys with their count, and a threshold count which is
<= to the pubkey count, and finally the
OP_CHECKMULTISIG(VERIFY) opcodes, which check the signatures provided against the public keys from the script.
In terms of creating multisig addresses, there are two RPC commands in Bitcoin Core.
addmultisigaddress. The code behind the multisig script creation is in standard.cpp#GetScriptForMultisig, which is quite trivial.
Spending the multisig output is not quite as simple. You need to use the
createrawtransaction command, followed by a call to
signrawtransaction for each signature required to meet the threshold, and finally you can broadcast the signed transaction with
As mentioned above, there is no standard address type for these transactions, but PSBT (Partially Signed Bitcoin Transactions), also known as BIP-174 defines a standard serialization and textual encoding (in Base64) which can be used for sharing the raw transaction at each point in the above process with other parties if the private keys are not owned by the same user.
There are proposals for doing mutlisig schemes in other ways which utilize key or signature aggregation. None of these have a concrete proposal in the form of a BIP yet. I suggest reading the MuSig paper from Maxwell et al. as a starting point, and have a look at BIP-340, which defines the Schnorr Signature scheme which is likely to be made standard into Bitcoin at some point in the near future. Note that the proposal for
OP_CHECKSIGADD in BIP-342 does not by itself allow for key or signature aggregation, but it does allow for more efficient batch verification of multiple signatures.
The difference between a regular address (p2pkh) and a multi-signature address (p2sh) is that the former is derived directly from a public key while the latter is derived from the redeem script that contains the public keys that can sign transactions to spend the funds locked in this address. In this redeem script, you set the amount of signatures required to unlock the funds (m) and also the total amount of signatures that can sign a transaction (n) alongside the public keys. Then, for obvious reasons, m must be <= n.
Multi-signature addresses are identified by starting with '3' (main net) or '2' (testnet) when encoded in base58. Check out this reference: https://en.bitcoin.it/wiki/List_of_address_prefixes
For the code, you can check out my repo: https://github.com/oscarsernarosero/blockchain/tree/master/bitcoin/basics/final
I am still working on it, but most of it is actually Jimmy Song's code from this book: https://github.com/jimmysong/programmingbitcoin
You'll have to take into account that Bitcoin went through a whole soft fork in order to process multi signature transactions the way it does right now. This was done this way to avoid high fees on multi-signature transactions. Check out https://github.com/jimmysong/programmingbitcoin/blob/master/ch08.asciidoc.
The math for multi-signature transactions is not different than the math from ECDSA since current multi-signature process in Bitcoin is more about smart contracts in Script (stack language that Bitcoin uses) than a math thing. That is why these addresses or transactions are called p2sh (Pay To Script Hash), because the address is the hash of the serialization of the smart contract that requires m-of-n signatures. Therefore, multi-signature transactions are achieved through a smart-contract mechanism more than through a mathematical procedure.
You can find the address part in my repo in the file wallet.py.
I hope this solved your question.