<p>Let's re-write and calculate from scratch as the calculations that you have posted might not be self-explanatory in itself and some of them are not entirely accurate. From the question, I presume you are trying to find the size of transaction for spending bitcoins from a multi-sig address.</p>

<p><strong>Redeem Script Size</strong></p>

<p>I think there is an error in the size of redeem script that you posted. You need to include the size of <code>OP_DATA</code> as well which comes prior to each public key. It tells the program how many bytes needs to be pushed to the stack. Let's assume we use compressed public keys which are 33 bytes in size. So the total size of redeem_script is 1 + N + 33*N + 1 + 1 = 3+34*N</p>

<pre><code>OP_M: 1 byte
OP_DATA: 1 byte (pub_key length) * N
pub_key: 33 bytes * N
OP_N: 1 byte
OP_CHECKMULTISIG: 1 byte
</code></pre>

<p><strong>scriptSig size</strong></p>

<p>I think there is also inaccuracy in the data you posted for this section. ECDSA signatures (including <code>SIGHASH</code> flag) can be 71, 72 or 73 bytes (73 bytes is non-standard as standardness requires low-S value but still flies by consensus). So from a conservative perspective you need to assume 73 bytes for each signature required. Below is the calculation. So you need 1+M+73*M+1+3+34*N = 5 + 74*M + 34*N</p>

<pre><code>nil_length: 1 byte
OP_DATA: 1 byte (len of signature to follow) * M
signature: 73*M
redeem_script_length: 1 byte
redeem_script_size: 3 + 34*N
</code></pre>

<p><strong>Input Size Calculation</strong></p>

<p>This section assumes there is only one input that consumes the bitcoins from the multi-sig address. Total input size is 46 + 74*M + 34*N</p>

<pre><code>previous_out_point: 36 bytes
    txid: 32 bytes
    vout_index: 4 bytes
var_int: 1 byte (script_sig length)
script_sig: 5+74*M+34*N bytes
sequence: 4 bytes
</code></pre>

<p><strong>Locking Script Size Calculation</strong></p>

<pre><code>P2PKH: 25 bytes
    OP_DUP: 1byte
    OP_HASH160: 1 byte
    OP_DATA (size of scriptPubKey to push): 1 byte
    scriptPubKey: 20 bytes
    OP_EQUALVERIFY: 1 byte
    OP_CHECKSIG: 1 byte

P2SH: 23 bytes
    OP_HASH160: 1 byte
    OP_DATA: 1 byte
    scriptPubKey: 20 byte
    OP_EQUAL: 1 byte

P2WSH: 34 bytes
    - OP_0: 1 byte
    - OP_DATA: 1 byte (witness_script_SHA256 length)
    - witness_script_SHA256: 32 bytes

P2WPKH: 22 bytes
    - OP_0: 1 byte
    - OP_DATA: 1 byte (public_key_HASH160 length)
    - public_key_HASH160: 20 bytes
</code></pre>

<p><strong><em>TX_out_size</em></strong></p>

<pre><code>value: 8 bytes
len of script: 1 byte
script length: variable as defined in previous section
</code></pre>

<p><strong>Total Transaction Size</strong></p>

<p>Let's first calculate the size assuming no SegWit. Then we will look at how SegWit helps in reducing transaction size.</p>

<pre><code>version: 4 bytes
count_tx_in: 1 byte (variable but since we have one input can be represented in 1 byte)
inputsize: 46 +74*M + 34*N
count_tx_out: 1 byte (variable again)
tx_out (assuming one input to P2PKH): 34 bytes
locktime: 4 bytes
</code></pre>

<p>So your total size is 90 + 74*M + 34*N. Assuming M=N (as your question dictates) the size is 90+108*N.</p>

<p><strong><em>Size with SegWit</em></strong></p>

<p>With the implementation of Segwit, we now count transaction size in weight units and not bytes. Normal transaction size is multiplied by 4 and witness data is multiplied by one. In the previous example our WU will be 360+432*N.</p>

<p>In segwit, we will discount the scriptSig by 4 and add one weight unit that signifies the  number of witness elements. So our transaction size is</p>

<pre><code>traditonal tx: 360 + 432*N WU
- scriptSig: (5 + 108*N)*4 = 20 + 432*N WU
= witness_stripped: = 340 WU
+ number of witness elements: 1WU
+ scriptSig: 5 + 108*N WU
= total: 346 + 108*N WU
</code></pre>

CC BY-SA 4.0
2019-06-07T17:14:29.463Z
Thank you for such detailed response!  You helped me a lot
newbie
CC BY-SA 4.0
2019-06-10T07:01:04.880Z
what if I have multiple unspent outputs in my multi-sig wallet? Then I have to multiply locking script * K (K - is the number of unspent outputs) 

and the total Tx size will be: 4 + 1 + (46 +74*M + 34*N) + 1 + 34*K + 4  ?
newbie
CC BY-SA 4.0
2019-06-10T07:14:17.960Z
@newbie What do you mean you have multiple unspent outputs? You are creating outputs from the multi-sig or using these ourputs as inputs in the transactions?
Ugam Kamat
CC BY-SA 4.0
2019-06-10T07:22:12.987Z
second. if I use a few outputs of previous tx as inputs in the future transaction
newbie
CC BY-SA 4.0
2019-06-10T07:58:34.143Z
@newbie No your formula isn't correct then. The way you have defined is using the multi-sig input and creating K outputs from the transaction. If you are consuming the P2PKH output your redeem_script is the DER encoded pubkey which is 33 bytes if compressed and 65 bytes if uncompressed. So use the above formulas I gave and calculate the size.
Ugam Kamat
CC BY-SA 4.0
2019-06-07T12:41:17.640Z
2019-06-08T06:37:59.823Z
2019-06-08T06:37:59.823Z
Ugam Kamat
<p>Can you help or suggest the formula for calculation of simple multi-sig transactions?</p>

<p>I have an issue to calculate the size of multi signature  2-2 transaction.</p>

<p>I tried to follow this: 
<a href="https://bitcoin.stackexchange.com/questions/30570/predict-multi-signature-transaction-size">Predict multi signature transaction size</a> answer, however it is vague and unclear. 
Here are my calculations:</p>

<pre><code> pubKeySize = 33;
 sigSize = 72;
 n = (m = 2);
 sizeOfRedeemScript = 1 + n * pubKeySize + 2;
 sizeOfScriptSig = 1 + m * (1 + sigSize) + 2 + sizeOfRedeemScript;
 sizeOfInput = 32 + 4 + 2 + sizeOfScriptSig + 4;
 sizeOfScriptPubKey = 2 + /** what is the length of script??  */ len(script);
 sizeOfOutput = 8 + sizeOfScriptPubKey;
 sizeOfTx = 4 + 2 + sizeOfInput + sizeOfOutput + 4;
</code></pre>

<p>If to be more precise I didn't get what is <code>SizeOfVarIntFor(lengthOfScript)</code> and what is the  <code>len(script)</code> (how to calculate it?)</p>

<multi-signature><transaction-weight>
How to calculate the size of multisig transaction?
newbie
# How to calculate the size of multisig transaction?

(Asked by: username_0 on 2019-06-07)

Can you help or suggest the formula for calculation of simple multi-sig transactions?
I have an issue to calculate the size of multi signature  2-2 transaction.
I tried to follow this: 
Predict multi signature transaction size answer, however it is vague and unclear. 
Here are my calculations:
 pubKeySize = 33;
 sigSize = 72;
 n = (m = 2);
 sizeOfRedeemScript = 1 + n * pubKeySize + 2;
 sizeOfScriptSig = 1 + m * (1 + sigSize) + 2 + sizeOfRedeemScript;
 sizeOfInput = 32 + 4 + 2 + sizeOfScriptSig + 4;
 sizeOfScriptPubKey = 2 + /** what is the length of script??  */ len(script);
 sizeOfOutput = 8 + sizeOfScriptPubKey;
 sizeOfTx = 4 + 2 + sizeOfInput + sizeOfOutput + 4;

If to be more precise I didn't get what is SizeOfVarIntFor(lengthOfScript) and what is the  len(script) (how to calculate it?)

**Tags:** multi-signature, transaction-weight


---

## 1 answers

### Answer by username_1 on 2019-06-07. Score: 4 | Accepted answer

Let's re-write and calculate from scratch as the calculations that you have posted might not be self-explanatory in itself and some of them are not entirely accurate. From the question, I presume you are trying to find the size of transaction for spending bitcoins from a multi-sig address.
Redeem Script Size
I think there is an error in the size of redeem script that you posted. You need to include the size of OP_DATA as well which comes prior to each public key. It tells the program how many bytes needs to be pushed to the stack. Let's assume we use compressed public keys which are 33 bytes in size. So the total size of redeem_script is 1 + N + 33*N + 1 + 1 = 3+34*N
OP_M: 1 byte
OP_DATA: 1 byte (pub_key length) * N
pub_key: 33 bytes * N
OP_N: 1 byte
OP_CHECKMULTISIG: 1 byte

scriptSig size
I think there is also inaccuracy in the data you posted for this section. ECDSA signatures (including SIGHASH flag) can be 71, 72 or 73 bytes (73 bytes is non-standard as standardness requires low-S value but still flies by consensus). So from a conservative perspective you need to assume 73 bytes for each signature required. Below is the calculation. So you need 1+M+73*M+1+3+34*N = 5 + 74*M + 34*N
nil_length: 1 byte
OP_DATA: 1 byte (len of signature to follow) * M
signature: 73*M
redeem_script_length: 1 byte
redeem_script_size: 3 + 34*N

Input Size Calculation
This section assumes there is only one input that consumes the bitcoins from the multi-sig address. Total input size is 46 + 74*M + 34*N
previous_out_point: 36 bytes
    txid: 32 bytes
    vout_index: 4 bytes
var_int: 1 byte (script_sig length)
script_sig: 5+74*M+34*N bytes
sequence: 4 bytes

Locking Script Size Calculation
P2PKH: 25 bytes
    OP_DUP: 1byte
    OP_HASH160: 1 byte
    OP_DATA (size of scriptPubKey to push): 1 byte
    scriptPubKey: 20 bytes
    OP_EQUALVERIFY: 1 byte
    OP_CHECKSIG: 1 byte

P2SH: 23 bytes
    OP_HASH160: 1 byte
    OP_DATA: 1 byte
    scriptPubKey: 20 byte
    OP_EQUAL: 1 byte

P2WSH: 34 bytes
    - OP_0: 1 byte
    - OP_DATA: 1 byte (witness_script_SHA256 length)
    - witness_script_SHA256: 32 bytes

P2WPKH: 22 bytes
    - OP_0: 1 byte
    - OP_DATA: 1 byte (public_key_HASH160 length)
    - public_key_HASH160: 20 bytes

TX_out_size
value: 8 bytes
len of script: 1 byte
script length: variable as defined in previous section

Total Transaction Size
Let's first calculate the size assuming no SegWit. Then we will look at how SegWit helps in reducing transaction size.
version: 4 bytes
count_tx_in: 1 byte (variable but since we have one input can be represented in 1 byte)
inputsize: 46 +74*M + 34*N
count_tx_out: 1 byte (variable again)
tx_out (assuming one input to P2PKH): 34 bytes
locktime: 4 bytes

So your total size is 90 + 74*M + 34*N. Assuming M=N (as your question dictates) the size is 90+108*N.
Size with SegWit
With the implementation of Segwit, we now count transaction size in weight units and not bytes. Normal transaction size is multiplied by 4 and witness data is multiplied by one. In the previous example our WU will be 360+432*N.
In segwit, we will discount the scriptSig by 4 and add one weight unit that signifies the  number of witness elements. So our transaction size is
traditonal tx: 360 + 432*N WU
- scriptSig: (5 + 108*N)*4 = 20 + 432*N WU
= witness_stripped: = 340 WU
+ number of witness elements: 1WU
+ scriptSig: 5 + 108*N WU
= total: 346 + 108*N WU

> **Comments:**
> **username_0**: Thank you for such detailed response!  You helped me a lot  
> **username_0**: what if I have multiple unspent outputs in my multi-sig wallet? Then I have to multiply locking script * K (K - is the number of unspent outputs)   and the total Tx size will be: 4 + 1 + (46 +74*M + 34*N) + 1 + 34*K + 4  ?  
> **username_1**: @username_0 What do you mean you have multiple unspent outputs? You are creating outputs from the multi-sig or using these ourputs as inputs in the transactions?  
> **username_0**: second. if I use a few outputs of previous tx as inputs in the future transaction  
> **username_1**: @username_0 No your formula isn't correct then. The way you have defined is using the multi-sig input and creating K outputs from the transaction. If you are consuming the P2PKH output your redeem_script is the DER encoded pubkey which is 33 bytes if compressed and 65 bytes if uncompressed. So use the above formulas I gave and calculate the size.  

---

