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