<p>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 &lt;= n.  </p>

<p>Multi-signature addresses are identified by starting with '3' (main net) or '2' (testnet) when encoded in base58.
Check out this reference: <a href="https://en.bitcoin.it/wiki/List_of_address_prefixes" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/List_of_address_prefixes</a></p>

<p>For the code, you can check out my repo:
<a href="https://github.com/oscarsernarosero/blockchain/tree/master/bitcoin/basics/final" rel="nofollow noreferrer">https://github.com/oscarsernarosero/blockchain/tree/master/bitcoin/basics/final</a></p>

<p>I am still working on it, but most of it is actually Jimmy Song's code from this book:
<a href="https://github.com/jimmysong/programmingbitcoin" rel="nofollow noreferrer">https://github.com/jimmysong/programmingbitcoin</a></p>

<p>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 <a href="https://github.com/jimmysong/programmingbitcoin/blob/master/ch08.asciidoc" rel="nofollow noreferrer">https://github.com/jimmysong/programmingbitcoin/blob/master/ch08.asciidoc</a>.</p>

<p>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. </p>

<p>You can find the address part in my repo in the file wallet.py.</p>

<p>I hope this solved your question.</p>

CC BY-SA 4.0
2020-03-30T12:13:57.247Z
Thanks so much for explanations. Explains everything
frturtle
CC BY-SA 4.0
2020-03-31T01:09:31.283Z
Not a problem. I'm glad it helped. By the way, If you consider my answer useful,  please remember to mark it by clicking the upward arrow. Also, if you think it answered your question thoroughly, please accept this answer by marking it by clicking the check symbol.
Oscar Serna
CC BY-SA 4.0
2020-03-22T02:25:29.380Z
2020-03-24T02:22:52.723Z
2020-03-24T02:22:52.723Z
Oscar Serna
<p>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 <code>&lt;=</code> to the pubkey count, and finally the <a href="https://github.com/bitcoin/bitcoin/blob/5504703a9f8388dff66d33bd077bcc4c82dff6c8/src/script/interpreter.cpp#L1020" rel="nofollow noreferrer"><code>OP_CHECKMULTISIG(VERIFY)</code></a> opcodes, which check the signatures provided against the public keys from the script.</p>

<p>In terms of creating multisig addresses, there are two RPC commands in Bitcoin Core. <code>createmultisig</code> and <code>addmultisigaddress</code>. The code behind the multisig script creation is in <a href="https://github.com/bitcoin/bitcoin/blob/5504703a9f8388dff66d33bd077bcc4c82dff6c8/src/script/standard.cpp#L302" rel="nofollow noreferrer">standard.cpp#GetScriptForMultisig</a>, which is quite trivial.</p>

<p>Spending the multisig output is not quite as simple. You need to use the <code>createrawtransaction</code> command, followed by a call to <code>signrawtransaction</code> for each signature required to meet the threshold, and finally you can broadcast the signed transaction with <code>sendrawtransaction</code>.</p>

<p>As mentioned above, there is no standard address type for these transactions, but PSBT (Partially Signed Bitcoin Transactions), also known as <a href="https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki" rel="nofollow noreferrer">BIP-174</a> 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.</p>

<p>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 <a href="https://eprint.iacr.org/2018/068" rel="nofollow noreferrer">MuSig</a> paper from Maxwell et al. as a starting point, and have a look at <a href="https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki" rel="nofollow noreferrer">BIP-340</a>, 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 <code>OP_CHECKSIGADD</code> 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.</p>

CC BY-SA 4.0
2020-03-21T21:59:19.927Z
2020-03-21T21:59:19.927Z
Mark H
<p>I am interested in the math and code behind bitcoin multisignature technology. I've been looking for actual code that creates a <strong>multisignature address/pub key</strong>, and <strong>verifies transactions/digital signatures</strong>; 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 <strong>m of n schemes written in code and the math explanations behind adress /pub key creation and signature verification.</strong> </p>

<multi-signature><signature><verification>
Multisignature technology in bitcoin - math and code
frturtle
# Multisignature technology in bitcoin - math and code

(Asked by: username_0 on 2020-03-21)

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.**

**Tags:** multi-signature, signature, verification


---

## 2 answers

### Answer by username_1 on 2020-03-22. Score: 1

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.

> **Comments:**
> **username_0**: Thanks so much for explanations. Explains everything  
> **username_1**: Not a problem. I'm glad it helped. By the way, If you consider my answer useful,  please remember to mark it by clicking the upward arrow. Also, if you think it answered your question thoroughly, please accept this answer by marking it by clicking the check symbol.  

---

### Answer by username_2 on 2020-03-21. Score: 1

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)`](https://github.com/bitcoin/bitcoin/blob/5504703a9f8388dff66d33bd077bcc4c82dff6c8/src/script/interpreter.cpp#L1020) 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. `createmultisig` and `addmultisigaddress`. The code behind the multisig script creation is in [standard.cpp#GetScriptForMultisig](https://github.com/bitcoin/bitcoin/blob/5504703a9f8388dff66d33bd077bcc4c82dff6c8/src/script/standard.cpp#L302), 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 `sendrawtransaction`.

As mentioned above, there is no standard address type for these transactions, but PSBT (Partially Signed Bitcoin Transactions), also known as [BIP-174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki) 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](https://eprint.iacr.org/2018/068) paper from Maxwell et al. as a starting point, and have a look at [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki), 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.


---

