3

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?)

1 Answer 1

4

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
5
  • Thank you for such detailed response! You helped me a lot
    – newbie
    Commented Jun 7, 2019 at 17:14
  • 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
    Commented Jun 10, 2019 at 7:01
  • @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
    Commented Jun 10, 2019 at 7:14
  • second. if I use a few outputs of previous tx as inputs in the future transaction
    – newbie
    Commented Jun 10, 2019 at 7:22
  • @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
    Commented Jun 10, 2019 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.